Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

The table below shows 32-bit values of an array stored in memory.

2.6.1 For the memory locations in the table above, write C code to sort the data from lowest to highest, placing the lowest value in the smallest memory location shown in the figure. Assume that the data shown represents the C variable called Array, which is an array of type int, and that the first number in the array shown is the first element in the array. Assume that this particular machine is a byte-addressable machine and a word consists of four bytes.

2.6.2 For the memory locations in the table above, write MIPS code to sort the data from lowest to highest, placing the lowest value in the smallest memory location. Use a minimum number of MIPS instructions. Assume the base address of Array is stored in register $s6.

Short Answer

Expert verified

2.6.1)

#sort the data from lowest to highest

temp1 = Array[0]; //assign 1st value of array to temp1

temp2 = Array[1]; //assign 2nd value of array to temp1

Array[1] = temp1; //store the temp1 value to 2nd value of array

Array{0] = Array[4]: //store the value at index 4 of

//array into 1st value of array

Array[4] = Array{3);//store the value at index 3 of array

//into the value at index 4 of array

Array[3] = temp2; store the temp2 value into index 3 of array

2.6.2)

#data section

data

#Declare array

Array: .word 2, 4, 3, 6, 1

#Declare the strings

result: .asciiz "The sorted data: \n:"

newline: .asciiz "\n"

#Text section

text

#main

Step by step solution

01

Given memory locations 

2.6.1)

Consider the following memory locations:

Consider the name of the variable as Array, which is an array of type int. It contains the given data values. Assume the integer variables temp1 and temp2 for temporary

02

C program to sort

#sort the data from lowest to highest

temp1 = Array[0]; //assign 1st value of array to temp1

temp2 = Array[1]; //assign 2nd value of array to temp1

Array[1] = temp1; //store the temp1 value to 2nd value of array

Array{0] = Array[4]: //store the value at index 4 of

//array into 1st value of array

Array[4] = Array{3);//store the value at index 3 of array

//into the value at index 4 of array

Array[3] = temp2; store the temp2 value into index 3 of array

03

Result after sorting the memory location

After sorting the data, the lowest value is placed in the smallest memory location.

The memory locations and data after sorting the data are shown in the below table:

04

Sample output

The sorted data:

1

2

3

4

6

05

MIPS code to sort the data from lowest to highest

#data section

data

#Declare array

Array: .word 2, 4, 3, 6, 1

#Declare the strings

result: .asciiz "The sorted data: \n:"

newline: .asciiz "\n"

#Text section

text

#main

06

:  code to sort the data from lowest to highest

#the base address of Array is stored in register $s6.

la $s6, Array

lw $t0, 0($s6) # load the data in Array[0] to $t0

lw $t1, 4($s6) #load the data in Array[1] to $t1

sw $t0, 4($s6) #store the data from $t0 to Array[1]

lw $t2, 16($s6) # load the data in Array[4] to $t2

sw $t2, 0($s6) #store the data from $t2 to Array[0]

lw $t3,12($s6) # load the data in Array[3] to $t3

sw $t3, 16($s6) #store the data from $t3 to Array[4]

sw $t1, 12($s6) #store the d

07

Sample output

The sorted data:

1

2

3

4

6

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Find the shortest sequence of MIPS instructions that extracts bits 16 down to 11 from register \(t0 and uses the value of this field to replace bits 31 down to 26 in register \)t1 without changing the other 26 bits of register $t1.

Question: Right before your function f from Exercise 2.34 returns, what do we know about contents of registers \(t5, \)s3, \(ra, and \)sp? Keep in mind that we know what the entire function f looks like, but for function func, we only know its declaration.

[5] Consider the following MIPS loop:

LOOP: slt \(t2, \)0, \(t1

beq \)t2, \(0, DONE

subi \)t1, \(t1, 1

addi \)s2, \(s2, 2

j LOOP

DONE:

2.26.1 [5] <§2.7> Assume that the register \)t1 is initialized to the value 10. What is the value in register \(s2 assuming \)s2 is initially zero?

2.26.2 [5] <§2.7> For each of the loops above, write the equivalent C code routine. Assume that the registers \(s1, \)s2, \(t1, and \)t2 are integers A, B, i, and temp, respectively.

2.26.3 [5] <§2.7> For the loops written in MIPS assembly above, assume that the register $t1 is initialized to the value N. How many MIPS instructions are executed?

Question:2.18 Assume that we would like to expand the MIPS register file to 128 registers and expand the instruction set to contain four times as many instructions.

2.18.1 [5] How this would this affect the size of each of the bit fields in the R-type instructions?

2.18.2 [5] How this would this affect the size of each of the bit fields in the I-type instructions?

2.18.3 [5] How could each of the two proposed changes decrease the size of an MIPS assembly program? On the other hand, how could the proposed change increase the size of an MIPS assembly program?

Show how the value 0xabcdef12 would be arranged in memory of a little-endian and a big-endian machine. Assume the data is stored starting at address 0

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free