如何在MIPS中将地址推入和弹出堆栈

4
我正在完成编译器课程的作业,我已经盯着我生成的MIPS(32位)代码看了几个小时,试图找出其中的问题,但无济于事。在生成MIPS代码后,我使用我们教练提供的MIPS VM上的gcc进行编译。我被告知要使用一些C标准库函数,例如printf,我相当确定代码的这部分是正确的,因为(正如我们被指示的那样),我大多数是从gcc输出的类似C代码中窃取的。
下面是应执行以下操作的MIPS代码:
  1. 在堆栈上为2个整数变量创建空间
  2. 将它们分别初始化为5和10(用于测试)
  3. 通过将它们的绝对地址推到堆栈上,然后弹出并访问它们来打印它们俩。
目前发生的情况是第二个printf似乎打印的是存储在第一个变量空间中的值而不是第二个变量的值。
(当使用常量int时,代码完全有效,因此我认为printf完全正确)
        .data
printf_string:    .asciiz  "%d\n"
scanf_string:    .asciiz  "%d"

    .text
    .globl main

main:
# make space for two ints on stack
    addiu   $sp, $sp, -8
# store return address in a saved register
# was going to push it onto the stack but until I figure out this issue I'm
# being lazy and just saving it to a saved register
    move    $s0, $ra
# make a copy of the stack pointer - likely not needed
    move    $s1, $sp

# typically here i loop and initialize the 2 ints on the stack but for now I'm
# doing it manually so I can figure out this issue with less possible things
# that could be wrong

# load some value into the register so I can store it
    li      $t7, 5
# store into first variable
    sw      $t7, 0($sp)
# different so I can tell if printing location works
    li      $t7, 10
# store into second variable
    sw      %t7, 4($sp)

instructions:

########################################
### CODE BELOW PRINTS FIRST VARIABLE ###
########################################

# appears to work...

# load standard library pointer and stuff (copied from gcc output)
# everything below works IF you did something like
# "WRITE 5" instead of "WRITE a"
    lui     $gp, %hi(__gnu_local_gp)
    addiu   $gp, %lo(__gnu_local_gp)
    lw      $t9, %call16(printf)($gp)
    .cprestore  16
    nop # needed after load word :-/

# load print_string address - works 
    la      $4, printf_string
# Here's where problems start
# make space for location of visited variable
    addiu   $sp, $sp, -4
# initialize $t0 to top of stack
    move    $t0, $s1
# add offset of variable to $t0
    addiu   $t0, $t0, 0
# store absolute memory address of variable to stack
    sw      $t0, 0($sp)

# load absolute memory address of variable from stack
    lw      $t0, 0($sp)
    nop # needed after lw
# undo stack allocation
    addiu   $sp, $sp, 4
# load MEM[$t0 + 0] into $5 (arg 2)
    lw      $5, 0($t0)
    nop
# finally call printf
    jalr    $t9
    nop

#########################################
### CODE BELOW PRINTS SECOND VARIABLE ###
#########################################

# appears to print the value stored in the first variable
# if I add "sw $s5, 4($sp)" here then it DOES work so I'm just very confused

# everything here should be basically the same as above but with a different
# offset/address pushed, popped, and accessed


    lui     $gp, %hi(__gnu_local_gp)
    addiu   $gp, %lo(__gnu_local_gp)
    lw      $t9, %call16(printf)($gp)
    .cprestore  16
    nop

    la      $4, printf_string
    addiu   $sp, $sp, -4
    move    $t0, $s1
    addiu   $t0, $t0, 4
    sw      $t0, 0($sp)

    lw      $t0, 0($sp)
    nop
    addiu   $sp, $sp, 4
    lw      $5, 0($t0)
    nop
    jalr    $t9
    nop


    addiu   $sp, $sp, 8
    move    $ra, $s0
    jr      $ra
    nop

如果有人能找到任何看起来不对的地方,我将非常感激!

1个回答

0

这只是一个建议,而不是确定的答案,你可能根本没有使用地址调用printf。我注意到在将参数的地址写入堆栈后,您会在调用printf之前恢复堆栈指针。因此,在两个示例中,当调用printf时,堆栈指针都指向原始的第一个参数,这可能解释了为什么在两种情况下都打印第一个参数。

sw      $t0, 0($sp)
lw      $t0, 0($sp)
nop
addiu   $sp, $sp, 4
...
# finally call printf
jalr    $t9

我的意图是将弹出堆栈的地址处的值加载到$t0中。这就是“lw $5, 0($t0)”这行代码的作用。我认为在增加堆栈指针之前,我已经将堆栈中的值加载到了$t0中。 - Michael Stewart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接