Skip to content

Registers

Before we go into the instruction, we have seen the importance of registers. So it is good to spend a few moment to talk about registers in MIPS.

Why Register?

As we have seen, registers are very close to the processor. This fact alone means that the speed to access the register is very fast. In comparison, memory access is very slow. In fact, the access time for memory can be five or more times slower than register access.

Why Not All Register?

Although register is very fast, we cannot put a lot inside. Part of the reason is the size of the processor. As the size of the processor increases, the distance of the register from the processor increases. This alone is enough to limit the number of registers.

So a typical architecture has 16 to 32 registers. More modern architecture may have 64 registers now. Due to this limitation, we will have to map variables to registers. This is all fine if we have fewer variables than registers. But that's not always the case.

So transfer between memory and registers will be inevitable. This is the job of the compiler to associate variables in program with registers. Modern compiler is so good at optimising this based on the lifetime of a variable.

What Are the Values?

The values in the registers are simply binaries! There are no data type associated to registers. This is very much unlike program variables1.

Assembly language and its corresponding machine code assumes that the data stored in the register is of the correct type. You will also learn later that it is the instruction itself that assumes the data type of the registers.

What Are the Registers in MIPS?

MIPS have 32 registers. Some of these registers are called general purpose registers. They can be referred to by their numbers or their names. The general purpose registers are shown below:

Name Number Conventional Usage
$zero 0 Constant value 0, cannot be written
$v0-$v1 2-3 Values for results and expression evaluation
$a0-$a3 4-7 Arguments
$t0-$t7 8-15 Temporaries
$s0-$s7 16-23 Program variables
$t8-$t9 24-25 More temporaries
$gp 28 Global pointer
$sp 29 Stack pointer
$fp 30 Frame pointer
$ra 31 Return address

Some of the registers are not available. For instance:

  • $at (register 1) is reserved for the assembler.
  • $k0-$k1 (register 26-27) is reserved for the operating system.

Convention

The convetional usage are basically just that, convention. When writing your own MIPS program, there is literally no restrictions preventing you from using it as anything. However, if you are not following the convetion, you are likely to simply confuse yourselves.

Special Registers

Recap that the special nature of von Neumann architecture is that both data and program are stored in the same memory unit. Hence, instructions are also stored in memory and that they have addresses. The control flow instructions use these addresses to branch/jump to the correct instruction.

You will learn later that instructions are 32-bit long and so is the address. Therefore, both the instruction and its addresses fit into the registers, which are also 32-bit long. The word for this is "word-aligned".

There is a special register called the Program Counter often abbreviated $PC or simply PC to store the address of the instruction being executed in the processor. This register is updated automatically by the processor. Some instructions, especially instructions for decision making, affect the next value of $PC.


  1. Of course, we are talking about C variables. Both Python and JavaScript do not have type associated with variables. Instead types are associated with values.