In: Computer Science
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1, and base address of b[] in $s2, respectively. Note that the element’s data type in array b is integer (word). Please do not change the C code structure and please comment your MIPS code.
for (i = 100; i>a; i--) {
b[i] = 16*i;
b[i+1] = 16*(i+1);
b[i+2] = 2*i + 16;
}
loop:
slt $t1, $s0, $s1 # if
a<i set $t1 = 1 else $t1 = 0
beq $t1, 0,
end # if $t1 is 0,
end loop
sll $t2, $s1, 2
# calculate offset address by multiplying i by
4
add $t2, $t2, $s2 #
adding base address to offset address
sll $t3, $s1, 4
# calculating 16*i by shifting 4 bits
sw $t3, ($t2)
# store word to b[i]
addi $t3, $t3, 16 # add
16 to previously calculated (16*i) , 16 * (i+1) = (16 * i) +
16
addi $t2, $t2, 4 #
increasing address by adding 4
sw $t3, ($t2)
# store word to b[i+1]
add $t3, $s1, $s1 #
adding i+i = 2*i and storing to $t3
addi $t3, $t3, 16 #
adding 16, so $t3 = ((2*i)+16)
addi $t2, $t2, 4 #
increasing address by adding 4
sw $t3, ($t2)
# store word to b[i+1]
addi $s1, $s1, 1 #
incrementing i
j loop
# back to
loop