MIPS Registers

The register files

The MIPS architecture has 32 general-purpose registers, $HI and $LO registers for multiplication and division, and two coprocessors with registers of their own: coprocessor 1 holds the 32 floating point registers, coprocessor 0 the registers an exception writes. The registers panel of the editor shows each of the three as its own tab.

$zero

$0

Always contains the value 0. Any write attempt to this register is silently ignored.

$at

$1

Assembly temporary, reserved for assembler internal use. It is used by macro instructions like li or la to break them into multiple real instructions. You can take control of it using the .set noat directive, but after that, macro instructions that rely on it will stop working.

$v0 - $v1

$2 - $3

Used to return non-floating point values from a subroutine. If the return value fits in 32 bits, only $v0 is used; for 64-bit values, the high word goes in $v1. $v0 also holds the system call number before a syscall instruction.

$a0 - $a3

$4 - $7

Arguments, used to pass the first four non-floating point arguments to a subroutine. Additional arguments are passed on the stack.

$t0 - $t7

$8 - $15

Temporary registers, their values are not preserved across subroutine calls. The caller is responsible for saving them if needed.

$s0 - $s7

$16 - $23

Saved registers, subroutines must preserve their values across calls, either by not using them or by saving and restoring them on the stack.

$t8 - $t9

$24 - $25

Additional temporary registers, same conventions as $t0-$t7. Note that $t9 has a special role in PIC code: by convention it holds the address of the called function, allowing the callee to compute $gp.

$k0 - $k1

$26 - $27

Reserved for OS/interrupt handler use. They can be overwritten at any time by an interrupt or trap handler, so user code should never rely on their values.

$gp

$28

Global pointer, used for two distinct purposes. In PIC code (Linux shared libraries), it points to the GOT (Global Offset Table), a table of pointers that the dynamic loader fills at runtime with the real addresses of external symbols. In non-PIC code (embedded systems), it points to the center of a compact region of small global/static variables, allowing them to be accessed with a single instruction using a signed 16-bit offset (covering ±32KB, 64KB total).

$sp

$29

Stack pointer, points to the top of the stack. It is explicitly adjusted by the callee on subroutine entry and exit.

$fp

$30

Frame pointer, also known as $s8. Used by a subroutine to track the stack frame when the stack pointer cannot be used directly, for example when the stack size is not known at compile time (e.g. when using alloca()).

$ra

$31

Return address, automatically written by jal with the address of the instruction following the call. The subroutine returns by executing jr $ra. Functions that themselves call other subroutines must save $ra on the stack first, since jal would otherwise overwrite it.

FPU (coprocessor 1)

Floating point numbers live in a coprocessor with registers and instructions of its own: add.s adds two single precision values the way add adds two integers, and no arithmetic instruction reads a register from each file. Crossing between them is a job of its own. mtc1 and mfc1 name one general register and one floating point register and move the bits between them without converting anything, while cvt.s.w and cvt.w.s convert between an integer and a float once the value is inside the coprocessor.

This simulator has no li.s or li.d pseudo-instruction. A constant is either written in the data section as a .float or a .double and loaded with l.s or l.d, or assembled as a bit pattern in a general register and moved across with mtc1.

$f0 - $f31

32 bits each

The floating point registers. Each one holds a single precision value, which is what the .s instructions read and write. By convention $f12 and $f14 pass the first two floating point arguments to a subroutine and $f0 returns the result, and the syscalls that read and print floats use the same registers.

Double precision pairs

$f0, $f2, $f4 … $f30

A double precision value is 64 bits, so it occupies a pair of registers: the even one holds the low word and the odd one above it holds the high word. Only the even register is ever named, so l.d $f4, value fills both $f4 and $f5, and add.d $f0, $f2, $f4 reads and writes three pairs. Naming an odd register in a .d instruction is an error. This is also why the double format of the registers panel leaves the odd rows blank.

Condition flags

0 - 7

A floating point comparison writes no register. It writes one of eight condition flags, and bc1t and bc1f branch on one of them, which is how a float comparison reaches a branch. The two operand form c.lt.s $f0, $f2 writes flag 0, the flag that bc1t label reads when it is given no number; the three operand form c.lt.s 3, $f0, $f2 writes flag 3, and bc1t 3, label is the branch that reads it. Eight flags mean eight comparisons can be kept apart at once. The registers panel shows them in the row above the registers of the file.

CP0 (coprocessor 0)

Coprocessor 0 holds the state used for exceptions and interrupts: their cause, the saved program address, interrupt enables and the timer. The processor records this state before jumping to the handler at 0x80000180. Place the handler there with .ktext 0x80000180. Without a handler, an exception or enabled interrupt stops the program with an error.

Use mfc0 $k0, $13 to read Cause, or mtc0 $k0, $14 to write EPC. The processor executes the handler. CP0 records and controls its entry and return. Leave EPC unchanged after an interrupt, or when retrying an instruction after repairing an exception. Add 4 only when deliberately skipping the failed instruction.

This simulator implements the six registers below: MARS's four plus the MIPS32 Count and Compare timer. See Interrupts and the timer for complete programs.

$8 (vaddr)

The memory address that caused an address-error exception, such as the address a lw or sw could not access. Other exceptions leave this register unchanged. Check the exception code in Cause before using it.

$9 (count)

The timer counter. It increases by one every two instructions, starting at 0 before the first instruction. It measures executed instructions rather than elapsed time. The same instruction sequence produces the same Count values on each run. In C, startup code has already increased Count before main. Write it with mtc0 to set a new value. Counting continues from there. This is a MIPS32 register, absent from MARS 4.5.

$11 (compare)

Set this ahead of Count, for example to Count + 1000. When Count reaches it, the timer sets Cause bit 15 to request an interrupt. Writing Compare clears that request. The timer stays off until the first write.

Setting Compare equal to the current Count is not a reliable immediate request: the match point occurs at the end of an even-numbered instruction. If that point has passed, the timer waits until Count wraps around.

$12 (status)

The interrupt enable bits and source mask. Initially 0x0000FF11: user mode, interrupts enabled by bit 0, and all eight source mask bits, 8 to 15, set. The processor sets bit 1, EXL (exception level), on entry to a handler, giving 0x0000FF13. No further interrupt is taken while EXL is set. eret clears EXL. To block interrupts, use mtc0 to clear bit 0 or the required source mask bit.

$13 (cause)

The exception code is in bits 2 to 6. Code 0 means an interrupt. Code 4, an address error on a load, appears as 0x10 after shifting left by two. Pending interrupt sources appear in bits 8 to 15: bit 15 the timer, bit 8 the keyboard and bit 9 the display. mtc0 cannot set these bits. Writing Compare clears the timer bit. The display bit stays set in the handler until the next exception, or until mtc0 writes it as 0. A shared handler checks the exception code first, then the source bits for an interrupt.

$14 (epc)

The return address used by eret. After an interrupt, it points to the next instruction, which has not run: leave EPC unchanged. After an exception, it points to the failed instruction. Leave it unchanged to retry after repairing the cause, or add 4 to deliberately skip that instruction.