本文介绍了具有四个以上参数的MIPS函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,只要我在MIPS中拥有一个具有四个以上参数的函数,就应该利用堆栈.但是在下面的代码中,将第五个参数保存在sw $t0, 4($sp)并执行jal sad之后,然后在sad函数的开头,我再次调整堆栈指针以保存由$sx使用的$sx寄存器.呼叫者.我在这里做错什么了吗?

I understand that whenever I have a function that has more than four arguments in MIPS I should utilize the stack. However in my code below after saving the fifth argument at sw $t0, 4($sp) and do a jal sad, then right at the beginning of the sad function I adjust the stack pointer again to save the $sx registers that is used by the caller. Am I doing something wrong here?

vbsme:  subu    $sp, $sp, 8     # create space on the stack pointer
    sw  $ra, 0($sp)     # save return address

    li  $v0, 0          # reset $v0
    li  $v1, 0          # reset $v1
    li  $s0, 1          # i(row) = 1
    li  $s1, 1          # j(col) = 1
    lw  $s2, 0($a0)     # row size
    lw  $s3, 4($a0)     # col size
    mul     $s4, $s2, $s3       # row * col
    li  $s5, 0          # element = 0
loop:   bgeq    $s5, $s4, exit      # if element >= row * col then exit

    subi    $a3, $s0, 1     # 4th parameter: i-1
    subi    $t0, $s1, 1
    sw  $t0, 4($sp)     # 5th parameter: j-1

    jal     sad         # calculate the sum of absolute difference using the frame starting from row a3 and col 4($sp)

    add $s6, $s0, $s1
    andi $s7, $s6, 1
if: bneq $s7, $zero, else
inif:   bge $s1, $s2, inelse
    addi $s1, $s1, 1
    j inif1
inelse: addi $s0, $s0, 2
inif1:  subi $s7, $s0, 1
    beq $s7, $zero, loop_back
    subi $s0, $s0, 1
    j loop_back
else:   bge $s0, $s2, inelse1
    addi $s0, $s0, 1
    j inif2
inelse1:addi $s1, $s1, 2
inif2:  subi $s7, $s1, 1
    beq $s7, $zero, loop_back
    subi $s1, $s1, 1
    j loop_back
loop_back: addi $s5, $s5, 1
       j loop
exit:   lw  $ra, 0($sp)     # restore return address
    addi    $sp, $sp, 8     # restore stack pointer
    jr $ra              # return

.globl  sad
sad:    subu $sp, $sp, 32       # allocate stack space for largest function
    sw $s7, 28($sp)         # save $s7 value
    sw $s6, 24($sp)         # save $s6 value
    sw $s5, 20($sp)         # save $s5 value
    sw $s4, 16($sp)         # save $s4 value
    sw $s3, 12($sp)         # save $s3 value
    sw $s2, 8($sp)          # save $s2 value
    sw $s1, 4($sp)          # save $s1 value
    sw $s0, 0($sp)          # save $s0 value


    #some code to be filled later



    lw $s7, 28($sp)         # restore original value of $s7 for caller
    lw $s6, 24($sp)         # restore original value of $s6 for caller
    lw $s5, 20($sp)         # restore original value of $s5 for caller
    lw $s4, 16($sp)         # restore original value of $s4 for caller
    lw $s3, 12($sp)         # restore original value of $s3 for caller
    lw $s2, 8($sp)          # restore original value of $s2 for caller
    lw $s1, 4($sp)          # restore original value of $s1 for caller
    lw $s0, 0($sp)          # restore original value of $s0 for caller
    addiu $sp, $sp, 32      # restore the caller's stack pointer
    jr $ra              # return to caller's code

推荐答案

这是gcc的工作方式.有关更多信息,您(可以)应阅读Mips ABI.有些事情可能有所不同.

This is how its done by gcc. For more information, you (could) should readthe Mips ABI. Some things may differ.

http://math-atlas.sourceforge.net/devel/assembly/mipsabi32.pdf

按照惯例,第五个参数应放在堆栈的第五个字上.

By convention, the fifth argument should go on the fifth word of the stack.

所以您应该

sad:
    sub $sp,$sp,24 #24 byte stack frame
    ... some code ...
    #Convention indicates to store $a0..$a3 in A..D (see below)
    sw $a0,0(sp)
    sw $a1,4(sp)
    sw $a2,8(sp)
    sw $a3,12(sp)

    #Get the 5th argument
    lw $t0,40($sp) #40 : 24 + 16

要将第5个参数存储在堆栈中,您应该了解以下信息:

To store the 5th argument in the stack, you should know this:

如果vbsme将要调用另一个函数,则应保存堆栈的底部4个字,以便被调用方在此处存储参数值.如果传递了四个以上的参数,则应为每个参数保存一个额外的单词.

If vbsme is going to call another function, then the bottom 4 words of the stack should be saved for the callee to store argument values there. If more than 4 arguments are passed, then an additional word should be saved for each argument.

vbsme's stack frame bottom part (Argument building area)

    |    ...       |
    ---------------
    |   5th arg    |  <---- sw      $t5,16($sp)
    ---------------
    |     D        |
    ---------------
    |     C        |
    ---------------
    |     B        |
    ---------------
    |     A        |
    ---------------  <--sp (of vbsme stack frame)

此外,$ ra寄存器应该保存在堆栈的顶部,因为它的寄存器是31.

Also, the $ra register should be saved at the top of the stack, since its register 31.

vbsme:
     subu    $sp, $sp, 20+N # 20: space for 5 arguments,
                            #N space for other stuff (ra,$tx, etc)


     #Set arguments (assumes 5th parameter value is in register $t5)
     subi    $a3, $s0, 1     # 4th parameter: i-1
     sw      $t5,16($sp)     #

    ...
 .end

响应

Why is it that you do:
lw $t0,40($sp)
to get the 5th argument, why did you add 24 to 16? when you do
sub $sp,$sp,24
don't you already move
the sp 24 place?

是的,$ sp + 24指向调用方堆栈的底部.但是,那不是我提出第五个论点的地方.第五个参数放在调用程序堆栈的第五个单词上,这就是为什么我要添加16.

Yes, $sp + 24 points to the base of the caller´s stack. However, thats not where I placed the fifth argument. The fifth argument is placed on the fifth word of the callers stack, thats why I add 16.

这篇关于具有四个以上参数的MIPS函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 08:31