【问题标题】:MIPS stuck in infinite loopMIPS陷入无限循环
【发布时间】:2022-01-23 06:12:33
【问题描述】:

作为我的 uni 报告的一部分,它希望我编辑一些 MIPS 代码并将重复代码放入子例程中,但是,每当我调用我的子例程时,它就会陷入重复整个子例程的无限循环中。该程序假设从用户那里获取 4 个输入并将它们加在一起并显示在没有子例程的情况下工作的总数,我不确定我的代码是否遗漏了某些内容,或者我只是写错了?谢谢你的帮助! :]

.data

enterMsg1: .asciiz "Please enter the last four digits of your student id \n"
enterMsg2: .asciiz "Press enter between each digit \n"
enterMsg3: .asciiz "Enter next digit \n"
totalMsg1: .asciiz "The total of the digits is: "
    
.text

# output the initial instruction text to the console
addi $v0, $zero, 4
la $a0, enterMsg1
syscall

# output the second instruction text to the console
addi $v0, $zero, 4
la $a0, enterMsg2
syscall

# read an integer from keyboard input and store the input in $s0 for the total
addi $v0, $zero, 5
syscall
add $s0, $zero, $v0

jal NextDigit
jal NextDigit
jal NextDigit

# output the text asking for the next digit to the console
# then receive the input, add to total ($s0)
NextDigit:
addi $v0, $zero, 4
la $a0, enterMsg3
syscall

addi $v0, $zero, 5
syscall
add $s0, $s0, $v0
jr $ra


# output the total instruction text to the console
addi $v0, $zero, 4
la $a0, totalMsg1
syscall


add $a0, $s0, $zero
addi $v0, $zero, 1
syscall

addi $v0, $zero, 10
syscall ```

【问题讨论】:

    标签: assembly mips mars-simulator


    【解决方案1】:

    您已将 NextDigit 函数嵌入到 main 中,这很糟糕。

    那么,从main 开始,该函数将被调用 3 次,并且会返回到 main 3 次,但之后会发生什么?

    由于该函数嵌入在main 中,因此main 不小心掉入了嵌入函数中,但这一次它没有被正确调用。因此,当它尝试返回时,它将返回与函数最后一次正确调用关联的返回地址(第三个 jal 的返回地址)。

    最常见的解决方案和有效的解决方案是将两个功能完全分开。将NextDigit的所有代码放在main的所有代码之后。

    (或者,您可以让您的main 在嵌入函数中跳转。)

    标签不执行;它们被汇编器删除并且不会出现在机器代码中,因此处理器永远不会看到它们。标签会影响使用它们的机器代码指令,但它们本身并不是指令。

    处理器看到的唯一内容是机器代码指令——每条指令都使用分支/跳转规则或顺序执行规则告诉处理器接下来要执行什么指令。因此,无论是分支/跳转到(如jaljr)还是像许多其他指令一样落空,都将执行带标签的部分。

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 1970-01-01
      • 2013-03-17
      • 2021-02-15
      • 2021-01-23
      • 2015-04-02
      相关资源
      最近更新 更多