Mips中的Fibonacci递归函数



我正在尝试实践递归如何在Mips中工作。所以我试着写一个fibonacci函数fib。起初,我用addi $a0, $a0, n来编写一个通用的解决方案,但我认为如果我想在Qtspim中检查我的结果,也许我需要添加一个实数作为参数。如果代码背后的想法是错误的,我不想得到完整的答案,但为了在Qtspim中运行它,并自己发现我的错误(逻辑错误(,我需要一些帮助
这是我的代码:

.globl main
.text
main:
addi $a0, $a0, 4

fib:
addi $sp, $sp, -8
sw $ra, 4($sp) 
sw $a0, 0($sp) 
slti $t0, $a0, 1
beq $t0, 1, L2  #if n<1     
beq $a0, 1, L2  # if n=1
beq $t0, 0, L1 # if n>1
#what to do when n<=1
L2:
addi $v0, $v0, 1 
jr $ra 
#what to do when n>1
L1: 
addi $a0, $a0, -1 
jal fib
lw $a0, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
lw $t1, 0($v0)
add $v0, $t1, $v0
jr $ra
li $v0,10
syscall

我收到一条错误消息,如下所示:数据/堆栈读取中的错误地址:0x00000000

.text
main:
addi $a0, $a0, 4
#### you've place fib inline inside main,
#### you should have all of main here, and "call" fib using jal instruction  
#### and the syscall for exit goes up here as well
fib:
addi $sp, $sp, -8        # you will want one more word of stack space
sw $ra, 4($sp) 
sw $a0, 0($sp) 
slti $t0, $a0, 1            # i would have use 2 here instead of 1
beq $t0, 1, L2  #if n<1     # this is ok, but better to use bne $t0, $0, L2
beq $a0, 1, L2  # if n=1    # and then this would not be needed
beq $t0, 0, L1 # if n>1     # this doesn't need to be conditional, if the program reaches here then L1 is the thing to do next.
#what to do when n<=1
L2:
addi $v0, $v0, 1            # here you want to return just 1 not v0+1
jr $ra 
#what to do when n>1
L1: 
addi $a0, $a0, -1 
jal fib                        # fib(n-1), good
lw $a0, 0($sp)                 # this reloads $a0 the original n
lw $ra, 4($sp)
addi $sp, $sp, 8
lw $t1, 0($v0)                 # after a call to fib $v0 holds fib(n)
# an integer value but you're treating it like a pointer and dereferencing it
add $v0, $t1, $v0              # here doing fib(n-1) + n
# you want fib(n-1) + fib(n-2) instead
# so you're missing a fib(n-2)
jr $ra
li $v0,10                  # this is part of main, so move it to where main is
syscall                    # realize that code located here is unreachable (aka dead)
# anything after an unconditional branch (here the jr $ra just above)
# and without a label is very suspicious as unreachable

更新,一些改进的(我希望(代码

.globl main
.text
main:
addi $a0, $a0, 4
jal fib 
li $v0, 10
syscall 
fib:
addi $sp, $sp, -8
sw $ra, 4($sp) 
sw $a0, 0($sp) 
slti $t0, $a0, 2
beq $t0, 1, L2      
#what to do when n<=1
L2:
addi $v0, $v0, 1 
jr $ra 
#what to do when n>1
L1: 
addi $a0, $a0, -1  # a0 -> n-1
jal fib   # fib(n-1)
lw $a0, 0($sp) #load word from memory adress 0($sp) to register $a0
lw $ra, 4($sp)  #load word from memory adress 4($sp) to register $ra
addi $sp, $sp, 8 
add $t1, $zero, $v1  # in register $t1 store current $v1
add $v0, $t2, $v0  # v0_ n+1 =v0 _n + v0_n-1
add $t2, $t1, $zero  # in $t2 save the previous value of v1 

.data
prompt: .ascii "Fibonacci Programn"
.asciiz "Enter N value: "
results: .asciiz "nFibonacci of N = "
n: .word 0
answer: .word 0
.text
.globl main
.ent main
main:
# Read n value from user
li $v0, 4 # print prompt string
la $a0, prompt
syscall
li $v0, 5 # read N (as integer)
syscall
sw $v0, n
# Call Fibonacci function.
lw $a0, n
jal fib
sw $v0, answer
# Display result
li $v0, 4 # print prompt string
la $a0, results
syscall
li $v0, 1 # print integer
lw $a0, answer
syscall
# Done, terminate program.
li $v0, 10 # terminate
syscall # system call
.end main
# Fibonacci function
# Recursive definition:
# = 0 if n = 0
# = 1 if n = 1
# = fib(n-1) + fib(n-2) if n > 2
# Arguments
# $a0 - n
# Returns
# $v0 set to fib(n)
.globl fib
.ent fib
fib:
subu $sp, $sp, 8
sw $ra, ($sp)
sw $s0, 4($sp)
move $v0, $a0 # check for base cases
ble $a0, 1, fibDone
move $s0, $a0 # get fib(n-1)
sub $a0, $a0, 1
jal fib
move $a0, $s0
sub $a0, $a0, 2 # set n-2
move $s0, $v0 # save fib(n-1)
jal fib # get fib(n-2)
add $v0, $s0, $v0 # fib(n-1)+fib(n-2)
fibDone:
lw $ra, ($sp)
lw $s0, 4($sp)
addu $sp, $sp, 8
jr $ra
.end fib

最新更新