Home > other >  [MIPS write a recursive function calls]
[MIPS write a recursive function calls]

Time:09-24

For passing the great spirit guide to save
Code will be converted into the following function MIPS, and write a tone function (main) to invoke,
Int fib (int n)
{
{if (n==0) return 1; }
{if (n==1) return 1; }
Return (fib (n - 1) + fib (n - 2));
}
Below is my own writing code but don't know what is going wrong
Main:
Li $where v0, 5
The syscall
Move $a0, $where v0
Jal Fib
Move $a0, $where v0
Li $where v0, 1
The syscall
Li $where v0, 10
The syscall
Fib:
Addi $sp, $sp, 4
Sw $ra, 4 ($sp)
Sw $a0, 0 ($sp)
Slti $t0, $a0, 2
Beq $t0, $zero, L
Add $where v0, $t0, $zero
Addi $sp, $sp, 4
Jr $ra
L:
Lw $a0, 0 ($sp)
Addi $a0, $a0-1
Jal Fib
Add $t1, $where v0, $zero
Lw $a0, 0 ($sp)
Addi $a0, $a0-2
Jal Fib
Add $where v0, $t1, $where v0
Lw $ra, 4 ($sp)
Addi $sp, $sp, 4
Jr $ra


CodePudding user response:

I think computing Fib (n - 1) and Fib (n - 2) part should be placed inside the Fib, rather than L,

CodePudding user response:

The first addi $sp, $sp, addi - 4 should be $sp, $sp, 8
-Behind the addi $sp, $sp, this should be unified with addi $sp, $sp, 8
  • Related