[英]C to MIPS Code translation problem with nested procedure
I need to tranlate piece of C code我需要翻译一段 C 代码
int main(){
int a, b, result;
if(a == b)
result = a*b;
else
result = assess(a, b);
return result;
}
int assess(int a, int b){
if(b<a)
return upgrade(a, b);
else
return demote(a, b);
}
int upgrade(int a, int b)
{return 4*(a+b);}
int demote(int a, int b)
{return 4*(b-a);}
a and b will be tested for a=8 b=8 a=3 b=5 a=5 b=3 here is what i tried a 和 b 将被测试 a=8 b=8 a=3 b=5 a=5 b=3 这是我尝试过的
.text
main:
add $s0,$s0,5
add $s1,$s1,3
add $s3,$s3,0
beq $s0,$s1,Resultmul
bne $s0,$s1,assess
li $v0, 10
syscall
assess:
addi $sp,$sp,-8
sw $s3,0($sp)
sw $ra,4($sp)
jal upgrade
lw $ra,4($sp)
add $sp,$sp,4
jr $ra
Resultmul :
mul $s3,$s1,$s0
li $v0, 10
syscall
upgrade:
add $s3,$s0,$s1
mul $s3,$s3,4
jr $ra
demote:
sub $v0,$s1,$s0
mul $v0,$v0,4
jr $ra
But it gets stuck in jr $ra in the assess procedure can someone fix this issue that would be great.但是它在评估过程中卡在 jr $ra 中,有人可以解决这个问题吗?
You are branching to assess
instead of calling it like a function via jal
.您正在分支进行assess
,而不是通过jal
将其称为 function 。 Thus, there is no proper value in $ra
upon the entry to assess
for it use upon completion to return to main
.因此,在$ra
输入时没有适当的值来assess
它在完成时用于返回main
。
You are (almost) properly saving $ra
and restoring it later, but it never had a good value in the first place, so the save & restore (which will be needed) doesn't help yet.您(几乎)正确地保存$ra
并在以后恢复它,但它一开始就没有很好的价值,所以保存和恢复(将需要)还没有帮助。
You should pop as many bytes off the stack as you push — you're pushing 8 but popping only 4.您应该从堆栈中弹出尽可能多的字节,因为您压入了 8 个但仅弹出了 4 个。
You are also not restoring $s3
though you do save it.尽管您确实保存了它,但您也没有恢复$s3
。
You might consider $ra
as a parameter passed to a function, and inspect its value upon function entry and during function execution to see where becomes incorrect.您可以将$ra
视为传递给 function 的参数,并在 function 条目和 function 执行期间检查其值以查看哪里不正确。 The value passed to the callee should be the address of the return point in the caller — a code address.传递给被调用者的值应该是调用者中返回点的地址——代码地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.