Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (12 points) The table below shows an array of 32-bit words stored in memory M

ID: 3756011 • Letter: 2

Question

2. (12 points) The table below shows an array of 32-bit words stored in memory Memory Address Data (Decimal) -2 140 136 132 128 124 13 Write MIPS code to list the data in a descending order like below, i.e., placing the greatest value iin the smallest memory address. Use a minimum number of MIPS instructions. Assume that the base address of the array is stored in register $s0. Don't use any sorting algorithm. Simply use LW and SVW nstructions. Memory Address Data (Decimal) 40 36 32 28 24 -2 13 61

Explanation / Answer

.text
.globl main
main:
la $s7, Array #load address Array in $s7
li $s0, 0 #load immediate i
li $s6, 9 #load immediate N-1
li $s1, 0 #load immediate j
sub_routine1:
sll $t7, $s1, 2
add $t7, $s7, $t7 # got the address of A[j]
lw $t0, 0($t7) #load word $t0 is A[j]
lw $t1, 4($t7) #load word $t1 is A[j+1]
sub $t2, $t0, $t1 #subtract $t0 to $t1 in $t2
bgtz $t2, no_swap #branch > 0
sw $t1, 0($t7) #$t0 is A[j]
sw $t0, 4($t7) #$t0 is A[j+1]
no_swap:

addi $s1, $s1, 1 #add immediate
sub $s5, $s6, $s0 #$s3 is N-i-1
bne $s1, $s5, sub_routine1 #branch not equal
addi $s0, $s0, 1 #add immediate
li $s1, 0 #load immediate j
bne $s0, $s6, sub_routine1 #branch not equal

done:
li $v0,4 #load immediate $v0
syscall
jr $ra #jump register

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote