Function of eight saved registers (MIPS)
I came across this statement in my Computer Organization and Design (Patterson & Hennessy) book:
$s0-$s7: eight saved 开发者_JAVA百科registers that must be preserved on a procedure call (if used, the callee saves and restores them)
I don't understand what that "preserved on a procedure call" means.
It means that if you're implementing a subroutine (e.g. the callee), you need to preserve the contents of those registers.
Which means: you either don't use them in your subroutine, or if you do, you save them first and restore them before you return to the caller.
Usually we store local variables on those registers. Let's say you are computing factorialof a number with the following algorithm:
int f(int a){ int t = a-1; return a*f(t); }
In this case, you see there are local variables a
and t
here. Since a
is an argument, it will be stored in $a0-$a3
during this procedure. However most probably you will store those arguments in $s0-$s7
or $t0-$t7
. This procedure calls itself recursively therefore you should save values of local variables into a stack. (read about "activation record" or "call stack") You should push these values to the stack $sp
then pop them upon return on stop case.
Basicly you save values on this stack in a procedure or a main program. This is just a convention, you can place your values into $v
or $t
or $a
registers, they'll work too, but things may be messed up after complexity of the program.
If your procedure is using $s
registers , before calling this procedure you must save these registers to a stack to "preserve" them if your procedure runs out of $t
registers.
If you are taking the couse about that, do not worry it about right now, you'll learn much more later.
register s0-s7 (soft registers) (aka $16-$20 physical registers) are the temp register, these are preserved when using C/C++.
Unless you are writing assembly then you should'nt have to worry about it at all as this is normal abi (application binary interface)
Some bookmarks from my collection might help if you need to do some reading... or can't sleep :-)
[Wiki Entry on MIPS][1]
[ABI Notes][2]
精彩评论