I want to create push and pop methods in mips assembly. Here's the code in java:
static int pop ()
{
if (i == 0) {
System.out.println ("Invalid Postfix");
System.exit(1);
}
i--;
return (p[i]);
}
and
static void push (int result)
{
if (i == MAX) {
System.out.println ("Too many tokens");
System.exit(1);
}
p[i] = result;
i++;
}
So far, I've created the push method:
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
However I don't know how to translate the return (p[i])
statement in assembly. Are return values stored in $v0
? Will I have to move the contents of $v0
to another registry? Any google searches on the subject have only confused me. Any help?
If my code in mips is confusing here's a cheat sheet:
$s0 = MAX
$s1 = stack pointer
$t7 = where I will store the numbers
$t2 = the number(result) that will be stored.
Taking the MIPS ABI as reference:
Are return values stored in $v0?
Yes.
Will I have to move the contents of $v0 to another registry?
No, that's not necessary. $vX registers are like the $tX registers, you don't need to back them up. It's the caller not the callee that needs to take care of backing up those registers if it cares about them.
PS: note that you can in fact use any other register or even memory for the return value if you want.