Translate the following C code to MIPS Assume that the varia
Translate the following C code to MIPS. Assume that the variables f, g, h, i, and j are assigned to registers $s0, Ss1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively. Assume that the elements of the arrays A and B are 4-byte words: B[8] = A[i] + A[j];
Solution
Solution:
The MIPS code for the above C code is given below:
li $s1, 4 // load constant 4 in $s1
multu $s1, $s3 // multiply $s3 by 4 which is present in $s1
mflo $s3 // result stored back in $s3
multu $s1, $s4 // multiply $s4 by 4 which is present in $s1
mflo $s4 // result stored back in $s4
add $s4, $s6, $s4 // $s4 = &A[j]
add $s3, $s6, $s3 // $s3 = &A[i]
lw $t0, ($s4) // $t0 = A[j]
lw $t1, ($s3) // $t1 = A[i]
add $t0, $t0, $t1 // $t0 = A[j] + A[i]
sw $t0, 32($s7) // Store the result into $t0 which gives B[8] = A[i] + A[j]
