In: Computer Science
Here is a traditional loop in C:
while (A{i} == 0)
A{i} = A{5} + A{i+3};
(NOTE: please consider braces {} here as usual square brackets for array index).
Assume that i is stored in register $s1, and the base address of the array A is in $s2. Fill in the multiple blank spaces in the following MIPS program that is supposed to be compiled from the above C loop:
loop: sll $t0, $s1,
add $t0, $t0,
lw $t1, 20($s2)
lw $t2, ($t0)
bne $t1, $zero, exit
j loop
exit:
Answer :-
loop:
sll $t0, $s1,
2
#creating offset for i
add $t0, $t0, $s2 #creating address of A{i}
lw $t1, 20($s2) #retrieving value for A{5}
lw $t2, 0($t0) #retrieving value for A{i}
bne $t2, $zero,
exit #checking A[i]==0 or not
lw $t3,
12{$t0}
#retrieving value for A{i+3}
add $t4, $t1,
$t3 #A{5} +
A{i+3}
sw $t4,
0($t0)
#A{i} = A{5} + A{i+3}
j loop
exit: