【问题标题】:How to push and pop from a stack in Assembly mips?如何在 Assembly mips 中从堆栈中推送和弹出?
【发布时间】:2016-04-04 13:12:33
【问题描述】:

我想在 mips 程序集中创建 push 和 pop 方法。这是java中的代码:

static int pop ()
{
    if (i == 0) {
        System.out.println ("Invalid Postfix");
        System.exit(1);
    }
    i--;
    return (p[i]);
} 

static void push (int result)
{
    if (i == MAX) {
        System.out.println ("Too many tokens");
        System.exit(1);
    }
    p[i] = result;
    i++;
}

到目前为止,我已经创建了 push 方法:

push:
    beq $s1, $s0, error_overflow
    sw $t2 , stack($t7)          # p[i] = result
    addi $t7, $t7, 4             # go to space for next int
    addi $s1, $s1, 1             # i++

    jr $ra

但是我不知道如何翻译汇编中的return (p[i]) 语句。返回值是否存储在$v0 中?我是否必须将$v0 的内容移动到另一个注册表?任何关于这个主题的谷歌搜索只会让我感到困惑。有什么帮助吗?

如果我在 mips 中的代码令人困惑,这里有一张备忘单:

$s0 = MAX
$s1 = stack pointer
$t7 = where I will store the numbers
$t2 = the number(result) that will be stored.

【问题讨论】:

  • 我不会使用stack($t7),而是使用指定为堆栈指针的寄存器(即$sp aka $r29)。如果需要设置$sp,请在开始时执行la $sp,stack_high。然后,使用sw $whatever,0($sp)

标签: assembly mips mips32


【解决方案1】:

MIPS ABI为参考:

返回值是否存储在 $v0 中?

是的。

我是否必须将 $v0 的内容移动到另一个注册表?

不,没有必要。 $vX 寄存器就像 $tX 寄存器,你不需要备份它们。如果它关心这些寄存器,则需要注意备份这些寄存器的是调用者而不是被调用者。

PS:请注意,如果您愿意,实际上您可以使用任何其他寄存器甚至内存作为返回值。

【讨论】:

  • 谢谢@m0skit0!这是否意味着我的代码看起来像这样? beq $s1, $zero, error_underflow lw $v0, stack($t7) addi $t7, $t7, -4 addi $s1, $s1, -1 # i--
猜你喜欢
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2020-12-19
  • 1970-01-01
  • 2014-12-19
相关资源
最近更新 更多