Mips 递归问题

发布于 2024-10-26 18:58:04 字数 2950 浏览 2 评论 0原文

我必须创建斐波那契数列,然后检查用户输入的数字是否是有效的斐波那契数列。数字。如果是的话,我必须显示 n 个数字,最多可达输入的数字。 N由用户给定。 例如,如果他们选择 3 作为 Fib。 number 和 2 作为应显示的计数 13, 8

我已经完成了所有工作,直到显示“13, 8”完成。任何关于如何退回堆栈并显示已创建并随后被覆盖的变量的指导将不胜感激。谢谢!

    ##### 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 

I have to create the Fibonacci sequence numbers and then check if a number that the user enters is a valid Fib. number. If it is I have to display n number of numbers up to their entered number. N is given by the user.
So for example, if they select 3 as their Fib. number and 2 as the count it should display
13, 8

I have everything up until displaying the "13, 8" finished. Any guidance on how to step back through the stack and display the variables that were created, and subsequently overwritten, would be appreciated. Thanks!

    ##### 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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-11-02 18:58:04

事实上,这并不是你问题的答案,而是一些在哪里寻找错误的指导。

研究一下“延迟槽”以及它与 MIPS 处理器中的分支指令的关系。

It is not the answer to your question in fact, but some guidance where to look for the mistake.

Do some research about "delay slots" and how it is related to branch instructions in MIPS processors.

迷离° 2024-11-02 18:58:04

除了代码看起来不完整之外,其中还有很多奇怪的事情,例如:

  • 没有指令来递增或递减
  • 进入 RecursiveFunction 时保存的堆栈指针寄存器永远不会恢复

我的建议是用 C 语言编写大部分代码,并开始只使用 MIPS 来实现递归函数。当你得到它时,然后完成你的工作并写剩下的部分。

Beside the fact that the code looks incomplete, there are a lot of weird things in it, example:

  • there is no instruction to increment or decrement the stack pointer
  • register saved when entering RecursiveFunction are never restored

My advice is to write most of your code in C, and start to play with MIPS only for the recursive function. When you got it, then finish your work with writing the rest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文