Notes on MIPS Assembly Registers

July 20, 2023

Assembly MIPS Assembly

These are notes that I’m taking as I’m learning. It’s highly likely that some things in these notes are either fully wrong or lacking important nuance.

Jump To

Processor Memory

A register is a piece of memory in the processor. It’s the fastest memory, but also it makes up the smallest share of the memory in the memory hierarchy.

I’ve been trying to understand exactly what that means (besides the obvious that higher quality manufacturing costs more). I’m frankly having trouble finding reputable citations, but these are the factors that I’ve seen described.

1) It’s faster to get to information if the ‘table’ is small. If the information can only be in one of 32 spots that are physically very close together, it will always be faster to retrieve than if it’s in one of a thousand spots spread across a relatively large space. See the discussion here.

2) Building very fast memory requires more transistors than slow memory. I think the problem is that you can only improve your materials so much, especially when you have to do wide scale manufacturing. Therefore, to eke out more speed, you need more transistors to be able to run processors in parallel. This not only costs more because you’re adding more transistors, but also because that increases the size and complexity of each die. In Computer Organization and Design, the authors talk about how increasing chip size increases cost both because you get less chips per silicon wafer and also because every defective chip represents a higher percentage of the batch.

3) The more speed you need, the more power you have to spend. This is a problem both for the efficiency of the computer but also because you have to keep the chips cool. The CPU already requires some sort of heat sink to keep it cool and if I understand correctly, I think a PC’s fan helps cool the CPU too.

What is a Register?

I realized pretty quickly that I didn’t understand what a register is physically. It’s clearly not a single transistor, because you can store more information than that in a register. A register seems to be a piece of very fast memory that holds a certain number of bits, such as 8, 32, or 64.

What is a Coprocessor?

A coprocessor is a processor that handles specific functions for a processor. Coprocessors are inherently specialized–they essentially act as a place to hand off specific, data-intensive processors so that the main processor doesn’t have to waste time on them, such as floating-point arithmetic.

MIPS Processor Registers

MIPS specifically has 32 registers (numbered 0 through 31):

Number Name Reserved Notes
0 $zero x Constant value of 0
1 $at x ‘assembler temporary’, used as part of handling pseudo-instructions[1], used internally rather than being invoked by the programmer?
2-3 $v0 – $v1 ‘value’, return value from function
4-7 $a0 – $a3 ‘argument, function argument
8-15 $t0 – $t7 ‘temporary’, value might be changed after function returns[2]
16-23 $s0 – $s7 ‘saved temporary’, should be same after function returns (even if changed temporarily during function call)[2]
24-25 $t8 – $t9 ‘temporary’, value might be changed after function returns
26-27 $k0 – $k1 x ‘kernel’, reserved for use by OS kernel, generally as part of exception or interrput handling[3]
28 $gp x ‘global pointer’, points to a space in memory that saves global variables
29 $sp x ‘stack pointer’, points to the place where data will be stored during function calls, points to the end of the current record[4]
30 $fp x ‘frame pointer’, points to the place where data will be stored during function calls, points to the start of the current record[5]
31 $ra x ‘return address’, indicates the address that the program should return to when completed[6]

[1] Pseudo-instructions allow you to write MIPS statements that are legal, but can’t be translated as-is into machine language. They actually act as macros, getting converted into MIPS statements that do have direct machine language equivalents. See here and here for more information.

[2] As far as I understand, the non-reserved registers all technically work the same, but the names indicate a standard convention that makes MIPS Assembly code easier to read and maintain. This means there’s nothing in MIPS that actually prevents you from changing an $s register within a function–it’s just against the convention and will make your code hard for others to read. See more information here.

[3] While the actual processor doesn’t care what registers are used for what, from what I understand, MIPS won’t let you compile code that tries to use $k0 or $k1. More information here and here.

[4] This part gets a little dense without more practical knowledge, but there’s information here and here. As best as I can tell, this helps handle nested functions. If Function A calls Function B, information about Function A must be saved before Function B can start, and that information has to be retrieved when returning from Function B back into Function A. I think that information gets added to and removed from the stack.

[5] Stack and frame pointers seem to work together–this PowerPoint explains it in more detail. I think frames are what’s actually getting put on the stack. The stack pointer points to the place the next frame gets inserted, but the frame pointer points to the start of the current frame (the frame that’s on top of the stack).

[6] I think the return pointer is also used when jumping into and out of functions–I think it saves the address where you return to from a function.

MIPS Coprocessor Registers

MIPS also has a coprocessor that handles floats and doubles. MIPS uses 32-bit registers, but doubles can be 64 bits, so although the coprocessor has 32 registers, you can only refer to the even-numbered ones (since the odds are reserved as a potential second half of a 64-bit double).

The MIPS float coprocessor has 32 registers:

Number Name Reserved Notes
0-3 $f0$f2 return value from subprogram
4-11 $f4 – $f10 temporary, value might be changed after function returns
12-15 $f12$f14 argument, function argument
16-19 $f16$f18 temporary, value might be changed after function returns
20-31 $f20 – $f30 saved, should be same after function returns (even if changed temporarily during function call)

That’s it for this set of notes! I always find doing the research to type these up forces me to understand everything much more thoroughly.

Posted In: Education, Notes