MIPS Instructions Reference

COSC 211 Machine Architecture (UBC Okanagan) - MIPS32 instruction set as used with the MARS simulator - v0.4.0

Registers

#NameUsageSaved by
0$zeroConstant 0 (writes are ignored)n/a
1$atReserved for the assembler (used when expanding pseudo-instructions)n/a
2$v0Procedure return value 1; syscall service code and syscall resultcaller
3$v1Procedure return value 2caller
4$a0Procedure argument 1; syscall argumentcaller
5$a1Procedure argument 2; syscall argumentcaller
6$a2Procedure argument 3caller
7$a3Procedure argument 4caller
8$t0Temporary valuecaller
9$t1Temporary valuecaller
10$t2Temporary valuecaller
11$t3Temporary valuecaller
12$t4Temporary valuecaller
13$t5Temporary valuecaller
14$t6Temporary valuecaller
15$t7Temporary valuecaller
16$s0Saved value (preserved across calls)callee
17$s1Saved value (preserved across calls)callee
18$s2Saved value (preserved across calls)callee
19$s3Saved value (preserved across calls)callee
20$s4Saved value (preserved across calls)callee
21$s5Saved value (preserved across calls)callee
22$s6Saved value (preserved across calls)callee
23$s7Saved value (preserved across calls)callee
24$t8Temporary valuecaller
25$t9Temporary valuecaller
26$k0Reserved for the operating system kerneln/a
27$k1Reserved for the operating system kerneln/a
28$gpGlobal pointer (static data segment)n/a
29$spStack pointer (top of stack; grows downward)callee
30$fpFrame pointer (not used in this course)callee
31$raReturn address written by jalcaller

Memory layout

SegmentStartNote
Reserved0x00000000Not usable by programs
Text0x00400000Program instructions; PC starts here
Static data0x10000000MARS places .data at 0x10010000; $gp = 0x10008000
Heap (dynamic data)0x10040000Grows upward (sbrk, syscall 9)
Stack0x7FFFFFFFGrows downward; $sp initialised near the top
Kernel0x80000000Reserved for the operating system

System calls

Load the service code into $v0 → Load the arguments into $a0..$a3 (and $f12 for floating point) → Execute the instruction syscall → Read any result from $v0 (or $f0 for floating point)

CodeServiceArgumentsResultIn course
1print_int$a0 = integer to printnoneyes (L7)
2print_float$f12 = float to printnoneno
3print_double$f12 = double to printnoneno
4print_string$a0 = address of null-terminated stringnoneyes (L7)
5read_intnone$v0 = integer readyes (L7)
6read_floatnone$f0 = float readno
7read_doublenone$f0 = double readno
8read_string$a0 = address of input buffer; $a1 = maximum number of characters to read (including the null terminator)buffer filled; input is null-terminatedyes (L7)
9sbrk$a0 = number of bytes to allocate$v0 = address of allocated memoryno
10exitnoneprogram terminatesyes (L7)
11print_char$a0 = character to print (low byte)noneyes (L7)
12read_charnone$v0 = character readyes (L7)
13open_file$a0 = address of filename; $a1 = flags; $a2 = mode$v0 = file descriptor (negative on error)no
14read_file$a0 = file descriptor; $a1 = buffer address; $a2 = max chars$v0 = chars read (0 = EOF, negative = error)no
15write_file$a0 = file descriptor; $a1 = buffer address; $a2 = chars to write$v0 = chars writtenno
16close_file$a0 = file descriptornoneno
17exit2$a0 = termination result codeprogram terminates with codeno
30timenone$a0 = low 32 bits, $a1 = high 32 bits of system time (ms)no
32sleep$a0 = millisecondsnoneno
34print_int_hex$a0 = integerprints as 8 hex digitsno
35print_int_bin$a0 = integerprints as 32 binary digitsno
36print_int_unsigned$a0 = integerprints as unsigned decimalno
41random_int$a0 = generator id$a0 = random intno
42random_int_range$a0 = generator id; $a1 = upper bound (exclusive)$a0 = random int in [0, $a1)no

Sources: COSC 211 lecture notes L4-L11 (Dr. Abdallah Mohamed, adapted from Patterson & Hennessy); Patterson & Hennessy, Computer Organization and Design, 5th ed., Chapter 2 and Appendix A; MARS help: supported instructions and system calls