Chapter 2: 6E (page 165)
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
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