a little more MIPS

MIPS general purpose register usages in functions

namenumbersusedpreserve?
$00Constant 0constant
$at1Only for the assemblerdon’t use
$v0 to $v12-3For return valuesno
$a0 to $a34-7For argumentsyes
$t0 to $t78-15For temporariesno
$s0 to $s716-23For saved valueyes
$t8 to $t924-25For temporariesno
$k0 to $k126-27For kerneldon’t use
$gp28For global pointeryes
$sp29For stack pointeryes
$fp30For frame pointeryes
$ra31For return addressyes

The really special ones

The assembler temporary, $at, is used by the assembler for psueo-instructions, such as blt. For example, the instruction
    blt   $t0,$t1,locX
is tranformed into
    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).

Linkage registers

The argument registers $a0 to $a3 are used to pass values. The return registers $v0 and $v1 are used to pass values.

The temporary and saved registers

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?