[英]How to get this program to work in MIPS?
以下程序不会停止。 如何使其停止?
.data
message: .asciiz "Calling the Procedure."
exitMessage: .asciiz "\nExiting properly"
.text
main:
jal DisplayMessage
jal exit
#End of the program.
#DisplayMessage - procedure
DisplayMessage:
jal DisplayActualMessage
jr $ra # return to the caller
#DisplayActualMessage - procedure
DisplayActualMessage:
li $v0, 4
la $a0, message
syscall
jr $ra # return to the caller
#Exit function
exit:
li $v0, 4 #Print code
la $a0, exitMessage #Load the message to be printed
syscall #Print command ends
li $v0, 10 #Exit code
syscall
是否可以创建一个通用功能来打印不同的短信?
jal
指令会修改$ra
寄存器,因此会发生以下情况:
jal DisplayMessage
将$ra
设置$ra
指向其后的指令。 DisplayMessage
使用jal DisplayActualMessage
调用DisplayActualMessage
,因此现在$ra
将被设置为指向DisplayMessage
jr $ra
。 DisplayActualMessage
返回,并在DisplayMessage
中的jr $ra
处恢复执行。 $ra
仍然指向相同的位置,因此最终会出现无限循环。 当您在MIPS程序集中有嵌套函数调用时,必须以某种方式保存和恢复$ra
寄存器。 您可以将堆栈用于此目的。 因此, DisplayMessage
将变为:
DisplayMessage:
addiu $sp, $sp, -4 # allocate space on the stack
sw $ra, ($sp) # save $ra on the stack
jal DisplayActualMessage
lw $ra, ($sp) # restore the old value of $ra
addiu $sp, $sp, 4 # restore the stack pointer
jr $ra # return to the caller
DisplayActualMessage
没有以同样的方式被改变,因为它不调用任何其它功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.