MIPS嵌套函数程序从

时间:2017-03-14 11:47:25

标签: mips mars-simulator

对不起,我不是MIPS的专家。我需要导入0到5之间的整数值(0 4我有一条错误消息errorDimensionMessage,我的程序重新启动函数jal dimension,直到键入正确的值。在此之后,如果我输入正确的值(例如3),程序进入jal exit退出。

(代码更新)

问题出在5(错误)之后 - > 3(好)程序从底部掉下来。

.data
    InsertDimension: .asciiz " Insert an integer (n) with value between 0 and 5 (0 < n < 5): "
    errorDimensionMessage: .asciiz  " Error. Matrix dimension is not valid (e.g., 0 < n < 5)\n"
    messageExit: .asciiz " Exit...\n"


.text
.globl main
    main:
        jal dimension
        jal exit

   exit: 
        la $a0, messageExit 
        li $v0, 4 # print string
        syscall
        li $v0, 10 # loads the service that exits
        syscall

    dimension:
        move $s0, $ra # save return address into s0
        la $a0, InsertDimension 
        li $v0, 4 # print string
        syscall 
        li $v0, 5 # read an integer from console and put it in $v0!
        syscall
        jal isValidDimension
        move $ra, $s0    #restore return address that was saved into s0
        jr $ra #return

    isValidDimension: 
        move $s1, $s0 # save return address into s0
        beqz $v0, errorDimension
        bgt $v0, 4, errorDimension
        move $s0, $s1 # save return address into s0
        jr $ra #return

    errorDimension:
        la $a0, errorDimensionMessage 
        li $v0, 4 # print string
        syscall
        j dimension # return to dimension

1 个答案:

答案 0 :(得分:2)

jal dimension # return to dimension这不是跳回dimension的正确方法。您希望使用常规的无条件跳转,即J指令(或B)。

现在发生的事情是jal$ra更改为指向jal之后的指令地址,即超出程序结束时的地址。因此,当执行jr $ra中的dimension时,您会跳过程序的结尾,这就是为什么你得到“程序从底层掉下来”