Mips 函数参数和 return 值

Mips function arguments and return values

我正在尝试使用 mips 程序集制作散列 table。我的 InsertKey 功能由于某种原因无法正常工作 correctly.The 当我尝试使用它时,控制台只是冻结且没有错误消息。

InsertK:
    li $v0, 4
    la $a0, enterIntPrompt
    syscall
    li $v0, 5
    syscall
    move $a0, $v0  # key in a0
    li   $t0, 0
    la   $a1, Hash # table address in a1
    bgt  $a0, $t0, JInsertKey
    li   $v0, 4
    la   $a0, notInsertKeyMes
    syscall
    j whileloop
    JInsertKey:
        jal InsertKey
        j   whileloop

我怀疑我在某处弄乱了我的传递值,但我找不到任何东西。教科书说,使用 $v0-$v2 寄存器作为函数调用的 return 值,使用 $a0-$a3 作为函数参数是一种很好的做法。 这是调用 InsertKey 函数的代码:

InsertKey:
    jal FindKey
    move $t0, $v0 # position in t0
    li   $t1, -1
    bne  $t0, $t1, prints
    bgt  $s1, $s0, dostuff
    li   $v0, 4
    la   $a0, hashTableFullMes
    syscall
    jr $ra
    dostuff:
    jal   HashFunction
    move  $t0, $v0
    li    $t3, 4
    multu $t0, $t3
    mflo  $t3
    la    $a1, Hash
    add   $a1, $a1, $t3 # a1 adress hash[position]
    sw    $a0, ($a1)
    addi  $s0,1
    jr    $ra
    prints:
        li $v0, 4
        la $a0, keyAlreadyInTableMes
        syscall
        jr $ra

FindKeyHashFunction 工作正常。有人可以识别这段代码的问题吗?我真的没脑子了..

编辑Findkey returns 找到其他键的位置 -1

InsertKey:
    jal FindKey

以上导致InsertKey$raInsertKey+8的地址覆盖,所以当InsertKeyreturns时,[= =20=]s 给它自己,而不是它的调用者。

在调用任何其他函数之前,您需要为 InsertKey 保存 return 地址和参数。