Professional Documents
Culture Documents
add $t0,$zero,$zero
Loop: slt $t2,$t0,$s0
beq $t2,$zero,Exit
add $t1,$zero,$zero
Loopa: slt $t3,$t1,$s1
beq $t3,$zero,Exita
sll $t4,$t1,4
add $t4,$t4,$s2
add $t5,$t0,$t1
sw $t5,0($t4)
addi $t1,$t1,1
j loopa
addi $t0,$t0,1
Exita: j loop
Exit:
First, it is noticed that the goal is to get the sum of the array elements, but the loop
above does not do this job, instead, it is an infinite loop as the address of
MemArray[i] cannot equal i at any iteration. Then, the branch should occur
when i<100 (i.e. when $t2 != 0).
Note that we want to reduce dynamic code size not the static one.
addi $t1, $0, $0 #initialize i=0
LOOP: lw $s1, 0($s0) #get MemArray[$s0]
add $s2, $s2, $s1 #result += MemArray[$s0]
addi $s0, $s0, 4 #$s0 += 4
addi $t1, $t1, 1 #i++
slti $t2, $t1, 100 #check if(i<100)then $t2=1
bne $t2, $zero, LOOP #branch if $t2 != zero