[英]MIPS stuck in infinite loop
作为我的 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 ```
您已将NextDigit
function 嵌入到main
中,这很糟糕。
那么,从main
开始, function 将被调用 3 次,它会返回到 main 3 次,但那之后会发生什么?
由于 function 嵌入在main
中,因此main
意外掉入了嵌入式 function,但这次它没有被正确调用。 因此,当它尝试返回时,它将 go 回到与最后一次正确调用 function 相关联的返回地址(第三个 jal 的返回地址)。
最常见的解决方案和有效的解决方案是将两个功能完全分开。 将NextDigit
的所有代码完全放在main
的所有代码之后。
(或者,您可以围绕嵌入式 function 进行main
跳转。)
标签不执行; 它们被汇编器删除并且不会出现在机器代码中,因此处理器永远不会看到它们。 处理器看到的唯一东西是机器代码指令,它会直接跳过源代码中的标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.