大家好,我需要一些帮助将以下 C 代码转换为 MIPS:
main()
{
int i;
for (i=0;i<5;++i)
power(2,1)
factorial(i)
return 0;
}
int power(int base,int n)
{
int i,p;
p=0;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
int factorial(int a)
{
if(a==1)
return 1;
else
{
a *=factorial(a-1);
return a;
}
}
到目前为止,我只完成了阶乘部分,但我被困在 a*=factorial(a-1),我理解 multi 用于将两个寄存器相乘,但它如何获取递归循环的输入?也可以乘以常数而不是另一个寄存器吗?
factorial:
addi $sp, $sp, -8 #adjust stack for 2 items
sw $ra, 4($sp) #save return address
sw $a0, 0($sp) #save argument
bne $a0, 1, Else #if !(a==1), go to else address
addi $v0, $zero, 1 #result is 1
addi $sp, $sp, 8 #pop 2 items from stack
jr $ra #return
Else: addi $a0, $a0, -1 #decrement s-1
jal factorial #recursive call
lw $a0, 0($sp) #restore original a
lw $ra, 4($sp) #and return address
addi $sp, $sp, 8 #pop two items from stack