在超过4个参数的递归函数中使用负偏移量进行堆栈分配

时间:2014-12-06 02:32:01

标签: assembly recursion stack mips offset

好的,所以我们有一个从main传递5个参数的函数。

$ a0- $ a3和$ t0

# Prologue: set up stack and frame poiters for search 
    addiu   $sp, $sp, -28           # allocate stack space -- 28 needed here
    sw      $fp, 0($sp)             # save caller's frame pointer 
    sw      $ra, 4($sp)             # save return address
    addi    $fp, $sp, 24            # setup the search frame pointer

# Preserve $a register arguments
    sw      $a0, 0($sp)     # addr of element in array
    sw      $a1, 4($sp)     # searchValue
    sw      $a2, 8($sp)     # indexLeft
    sw      $a3, 12($sp)    # indexRight

    # Get the 5th argument
    lw      $s5, 44($sp)    # level = 28 + 16

我知道这是如何工作的。但是,main并没有将$ t0存储在16($ sp)中。而是将$ t0存储在-4($ sp)中。

这是什么意思?为了将存储在$ t0中的值加载到我的$ s5寄存器中,我应该设置我的偏移量?我倾向于24($ sp)(28 +( - 4)),但我不确定。

这是一个递归函数,所以我想确保每次函数调用自身时每个寄存器加载正确的值,并从我的堆栈指针中分配另外28个字节。

就实际的递归调用而言,我不明白如何使用$ v0?如果有人能解释它是如何使用的,我将不胜感激

0 个答案:

没有答案