如何在 Assembly mips 中从堆栈中推送和弹出?
How to push and pop from a stack in Assembly mips?
我想在 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])
语句。 return 值是否存储在 $v0
中?我是否必须将 $v0
的内容移动到另一个注册表?对这个主题的任何 google 搜索只会让我感到困惑。有帮助吗?
如果我的 mips 代码令人困惑,这里有一个作弊 sheet:
$s0 = MAX
$s1 = stack pointer
$t7 = where I will store the numbers
$t2 = the number(result) that will be stored.
以MIPS ABI为参考:
Are return values stored in $v0?
是的。
Will I have to move the contents of $v0 to another registry?
不,没有必要。 $vX 寄存器类似于 $tX 寄存器,您不需要备份它们。如果关心这些寄存器,则需要备份这些寄存器的是调用者而不是被调用者。
PS:请注意,如果需要,您实际上可以使用任何其他寄存器甚至内存作为 return 值。
我想在 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])
语句。 return 值是否存储在 $v0
中?我是否必须将 $v0
的内容移动到另一个注册表?对这个主题的任何 google 搜索只会让我感到困惑。有帮助吗?
如果我的 mips 代码令人困惑,这里有一个作弊 sheet:
$s0 = MAX
$s1 = stack pointer
$t7 = where I will store the numbers
$t2 = the number(result) that will be stored.
以MIPS ABI为参考:
Are return values stored in $v0?
是的。
Will I have to move the contents of $v0 to another registry?
不,没有必要。 $vX 寄存器类似于 $tX 寄存器,您不需要备份它们。如果关心这些寄存器,则需要备份这些寄存器的是调用者而不是被调用者。
PS:请注意,如果需要,您实际上可以使用任何其他寄存器甚至内存作为 return 值。