繁体   English   中英

如何使该程序在MIPS中运行?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM