Translate the following C code to MIPS Assume that the varia
Translate the following C code to MIPS. Assume that the variables i, and j are assigned to registers $s1 and $s2, respectively. Assume that the base address of the arrays A and B are in registers $s3 and $s4, respectively. Assume that the elements of the arrays A and B are 32 bit words: B[8] = A[i] + A[j]
Solution
mult $s1,$s1,4 # $s1 = 4*i
mult $s2,$s2,4 # $s2 = 4*j
add $t0,$s3,$s1 # $t0 = &A[i]
lw $t1, 0($t0) # $t0 = A[i]
add $t0,$s3,$s2 # $t0 = &A[j]
lw $t2, 0($t0) # $t0 = A[j]
add $t3,$t1,4t2 # $t3 = A[i] + A[j]
mult $t6,$t6,8 # $t6 = 4*8
add $t0,$s4,$t6 # $t0 = &B[8]
sw $t3,0($t0) # B[8] = A[i] + A[j]
