Chapter 2: Q27E (page 170)
Question [5] <§2.7> Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers \(s0, \)s1, \(t0, and \)t1, respectively. Also, assume that register $s2 holds the base address of the array D.
for(i=0; i<a; i++)
for(j=0; j<b; j++)
D[4*j] = i + j;
Short Answer
The corresponding MIPS code is :
add $t0, $0, $0
LP1: slt $t2, $t0, $s0
beq $t2, $0, EXIT
add $t1, $0, $0
LP2: slt $t2, $t1, $s1
beq $t2, $0, LP3
add $t2, $t0, $t1
sll $t4, $t1, 4
add $t3, $t4, $s2
sw $t2, 0($t3)
addi $t1, $t1, 1
j LP2
LP3: addi $t0, $t0, 1
j LP1
EXIT: