Recursion 递归问题
我必须创建斐波那契序列号,然后检查用户输入的数字是否是有效的斐波那契序列号。号码。如果是,我必须显示n个数字,直到输入的数字为止。N由用户给出。 例如,如果他们选择3作为他们的Fib。数字和2作为应显示的计数 13,8 在“13,8”显示完成之前,我已经准备好了所有的东西。任何关于如何在堆栈中后退并显示已创建并随后被覆盖的变量的指导都将不胜感激。谢谢Recursion 递归问题,recursion,mips,Recursion,Mips,我必须创建斐波那契序列号,然后检查用户输入的数字是否是有效的斐波那契序列号。号码。如果是,我必须显示n个数字,直到输入的数字为止。N由用户给出。 例如,如果他们选择3作为他们的Fib。数字和2作为应显示的计数 13,8 在“13,8”显示完成之前,我已经准备好了所有的东西。任何关于如何在堆栈中后退并显示已创建并随后被覆盖的变量的指导都将不胜感激。谢谢 ##### The Data Segment ######### .data strNumber: .asciiz "Enter
##### The Data Segment #########
.data
strNumber: .asciiz "Enter a valid Fibonacci Number: "
strCount: .asciiz "Enter the numbers of Fibonacci numbers to be displayed: "
strError: .asciiz " is not a valid Fibonacci Number.\n"
strMore: .asciiz "There are no more Fibonacci Numbers to be displayed."
newLine: .asciiz "\n"
strBadEntry: .asciiz "is not a valid entry."
strValid: .asciiz "Valid Fib Number\n\n"
#### The Text Segment ##########
.text
.globl main
main:
li $t3, 39
li $t2, 0
li $t4, 1
li $t5, 1
#Get the User's Number #Gets the number from the user
li $v0, 4
la $a0, strNumber
syscall
li $v0, 5 #prepares to take in the user entered value
syscall #retrives what the user entered in the console
move $s1, $v0
bltz $v0, in_error #calls the error function if less than 0.
j DoneIf #if those conditions aren't meant it jumps to the DoneIf
in_error:
li $t4, 1
li $t5, 1
li $v0, 1 # print int
move $a0, $s1 # prints the user's number
syscall
li $v0, 4
la $a0, strError
syscall
li $v0, 4
la $a0, strNumber
syscall
li $v0, 5
syscall
move $s1, $v0
bltz $v0, in_error #recall the inerror function if still less than 0
DoneIf:
move $t0, $v0 #moves the value to a new location, for future use
li $v0, 4
la $a0, newLine
syscall
#Second Number #Gets the second number from the user
li $v0, 4
la $a0, strCount
syscall
li $v0, 5
syscall #retrieves what the user entered in the console
bltz $v0, in_error2 #calls the second error function if less than 0
bgeu $v0, $t3, in_error2 #calls the second error function if greater than 63
j DoneIf2 #jumps to the DoneIf2 if those conditions aren't met
in_error2:
li $v0, 4
la $a0, strBadEntry
syscall
li $v0, 4
la $a0, newLine
syscall
li $v0, 4
la $a0, strCount
syscall
li $v0, 5
syscall
blez $v0, in_error2 #recalls the error2 function if number conditions stil aren't met
bgeu $v0, $t3, in_error2 #recalls the error2 function if number conditions still aren't meet
DoneIf2:
move $t1, $v0
jal RecursiveFunction #Jump to Recursive Function
Exit:
RecursiveFunction:
sw $ra, 0($sp)
sw $t4, 4($sp)
sw $t5, 8($sp)
bge $t5, $t4, t5_Greater
bgt $t4, $t5, t4_Greater
Check:
bgt $t4, $t0, check_t5
check_t5:
bgt $t5, $t0, in_error
beq $t4, $t0, Valid
beq $t5, $t0, Valid
jal RecursiveFunction
t5_Greater:
add $t4, $t5, $t4
j Check
t4_Greater:
add $t5, $t5, $t4
j Check
Valid:
li $v0, 4
la $a0, strValid
syscall
lw $ra, 20($sp) # Restore return address
lw $fp, 16($sp) # Restore frame pointer
li $v0, 1
move $a0, $t5
syscall
事实上,这并不是你问题的答案,而是一些指引,指引你去哪里寻找错误
研究一下“延迟槽”以及它与MIPS处理器中的分支指令的关系。除了代码看起来不完整之外,还有很多奇怪的东西,例如:
- 没有递增或递减堆栈指针的指令
- 输入递归函数时保存的寄存器永远不会恢复
我的建议是用C语言编写大部分代码,并开始仅为递归函数使用MIPS。当你得到它,然后完成你的工作,写剩下的。通常,汇编程序会为你处理延迟槽。至少GNU汇编程序会这样做,除非您要求不要使用
。set noreorder
directive@Laurent认真地好吧,那就好了。我在最低级别上使用过MIPS(动态地发出二进制代码本身),因此延迟槽非常混乱。这就是为什么我不知道汇编程序如何解决这种情况。好吧,GNU汇编程序只会尝试将具有延迟槽的指令与其前一个指令交换,同时确保它不会破坏依赖关系。大多数情况下,它会失败,只需在之后插入一个NOP。老实说,对于值得在给定项目的汇编中编写的行,我个人更喜欢自己处理延迟槽(因此.set noreorder
指令)。当您的项目主要处于组装阶段时,您当然具有自己处理这些项目所需的技能。同样,让汇编程序完成这项工作是不值得的。