Problem in translating from MIPS to C
I was trying to solve this homework assignment but was unable to come up with a solution. Below is the problem,
Translate the following MIPS code into a high-level language program. Assume that $t0, $t1, and $t2 contain base addresses of arrays A, B and C respectively.
add $t4, $zero, $zero
Loop:
add $t5, $t4, $t1
lw $t6, 0($t5)
add $t5, $t4, $t2
lw $t7, 0($t5)
add $t6, $t6, $t7
add $t5, $t4, $t0
sw $t6, 0($t5)
addi $t4, $t4, 4
slti $t5, $t4, 256
bne $t5, $zero, Loop
Also find the decimal value in the offset field of bne
instruction.
Here is what I have tried, but I yet don't find the position of 256
.
int *temp4 = 0;
while(1)
{
*int temp5 = temp4 +B[0];
a:
*int temp6 = temp5;
temp5 = C[0] + temp4;
*temp7 = temp5;
temp6 = temp6 + temp7;
temp5 = temp4 + A[0];
temp6 = temp5;
temp4 += 4;
开发者_如何转开发 if(temp5 < temp4)
goto __;
if(temp5 != 0)
goto a;
}
I think that you are overthinking things.
What that code does is something like this
for (int i =0 ; i< 64; i++){
A[i] = B[i] + C[i];
}
Not going to explain why, since this looks a lot like is homework.
Here is what I have tried, but I yet don't find the position of 256.
You are mixing things up. See that i
at the end of slti
? That means that it uses an inmediate operand, in this case 256
. So what the slti $t5, $t4, 256
instruction is doing, is setting a 1
in register $t5
if the contents of $t4
are lower than 256. Otherwise, $t5
gets a 0
.
Consequently, the loop will take 256/4 iterations, since the bne
will fallthrough (i.e not jump) only when the contents of $t4
are greater than 256
.
精彩评论