name | numbers | used | preserve? |
---|---|---|---|
$0 | 0 | Constant 0 | constant |
$at | 1 | Only for the assembler | don’t use |
$v0 to $v1 | 2-3 | For return values | no |
$a0 to $a3 | 4-7 | For arguments | yes |
$t0 to $t7 | 8-15 | For temporaries | no |
$s0 to $s7 | 16-23 | For saved value | yes |
$t8 to $t9 | 24-25 | For temporaries | no |
$k0 to $k1 | 26-27 | For kernel | don’t use |
$gp | 28 | For global pointer | yes |
$sp | 29 | For stack pointer | yes |
$fp | 30 | For frame pointer | yes |
$ra | 31 | For return address | yes |
$at
, is used by the
assembler for psueo-instructions, such as blt
.
For example, the instruction
blt $t0,$t1,locX
slt $at,$t0,$t1
bne $at,$0,locX
The kernel registers, $k0
and $k1
, should
only be used in the operating systems for saving and restoring registers
after interrupts.
The registers concerned with stack management,
$gp
, $sp
, $fp
and $ra
,
should only be used in the as permitted by the calling conventions
(see Wikipedia or Microsoft
or University of Washingon).
The argument registers
$a0
to $a3
are used to pass values.
The return registers
$v0
and $v1
are used to pass values.
Assembly language programmers and optimizing compilers
should try to use the ten temporary registers,
$t0
to
$t9
, for their code.
For leaf functions these ten
registers should be adequate.
After all how many leaf functions do you write that use more than
ten variables?
For example, it wouldn’t be difficult to generate assembly code
with sum
and i
stored in temporary registers.
sum = 0 ; for (i=0; i<1000; ++i) { sum = sum + A[i] ; }
Creators of non-leaf functions must assume that values stored in temporary registers will be lost when any function call is made. Consider this modification of the earlier code.
sum = 0 ; for (i=0; i<1000; ++i) { sum = sum + fOfMystery(A[i]) ; }
In this case,
if sum
and i
were stored in temporary
registers, they would have to be saved and reloaded with
every call of fOfMystery
.
It would be better to store them in one of the
eight saved registers, $s0
to $s7
,
which would need to be saved and restored only once in the
calling function rather than once for every interation of the loop.
There are inconsistent rules for the argument registers,
$a0
to $a3
. Can they be modified?