12
2.3 Register Usage Convention
Within the register file different sets of registers have been given names to remind the
programmer of a convention, which all MIPS assembly language programmers are
expected to abide by. If all the members of a programming team do not adhere to the
same convention, the entire effort will result in disaster. To simulate this situation
everyone using this book is expected to adhere to the same convention. Programs should
run correctly, even if the class members randomly exchange their functions.
Programs of any complexity typically involve a main program that calls functions. Any
important variables in the main program that must be maintained across function calls
should be assigned to registers $s0 through $s7. As programs become more complex,
functions will call other functions. This is referred to as nested function calls. A function
that does not call another function is referred to as a “leaf function.” When writing a
function, the programmer may utilize registers $t0 through $t9 with the understanding
that no other code modules expect values in these registers will be maintained. If
additional registers are needed within the function, the programmer may use only
registers $s0 through $s7 if he/she first saves their current values on the stack and
restores their values before exiting the function. Registers $s0 through $s7 are referred to
as callee-saved registers. Registers $t0 through $t9 are referred to as caller-saved
registers. This means that if the code module requires that the contents of certain “t”
registers must be maintained upon return from a call to another function, then it is the
responsibility of the calling module to save these values on the stack and restore the
values upon returning from the function call. Registers $a0 through $a3 are used to pass
arguments to functions, and registers $v0 and $v1 are used to return values from
functions. These conventions are covered in more detail in Chapter 6.
2.4 The MIPS Instruction Set
When the MIPS architecture was defined, the designers decided that the machine would
have instructions to perform the operations listed in Appendix C. At that time, the
designers also decided on a binary operation code for each instruction, and the specific
instruction format in binary. Given the requirement of the different instructions it was
necessary to define three different formats. Some instructions are encoded in R format,
some in I format and a few in J format. The list of instructions in Appendix C is not a
complete list. The other instructions not in Appendix C involve floating point
instructions, coprocessor instructions, cache instructions, instructions to manage virtual
memory, and instructions concerned with the pipeline execution.
The designers of the MIPS assembler, the program that translates MIPS assembly
language code to MIPS binary machine language code, also made some decisions to
simplify the task of writing MIPS assembly language code. The MIPS assembler
provides a set of macro (also called synthetic or pseudo) instructions. Every time a
programmer specifies a macro instruction, the assembler replaces it with a set of actual
MIPS instructions to accomplish the task. Appendix D provides a list of macro