Registers
| # | Name | Usage | Saved by |
|---|---|---|---|
| 0 | $zero | Constant 0 (writes are ignored) | n/a |
| 1 | $at | Reserved for the assembler (used when expanding pseudo-instructions) | n/a |
| 2 | $v0 | Procedure return value 1; syscall service code and syscall result | caller |
| 3 | $v1 | Procedure return value 2 | caller |
| 4 | $a0 | Procedure argument 1; syscall argument | caller |
| 5 | $a1 | Procedure argument 2; syscall argument | caller |
| 6 | $a2 | Procedure argument 3 | caller |
| 7 | $a3 | Procedure argument 4 | caller |
| 8 | $t0 | Temporary value | caller |
| 9 | $t1 | Temporary value | caller |
| 10 | $t2 | Temporary value | caller |
| 11 | $t3 | Temporary value | caller |
| 12 | $t4 | Temporary value | caller |
| 13 | $t5 | Temporary value | caller |
| 14 | $t6 | Temporary value | caller |
| 15 | $t7 | Temporary value | caller |
| 16 | $s0 | Saved value (preserved across calls) | callee |
| 17 | $s1 | Saved value (preserved across calls) | callee |
| 18 | $s2 | Saved value (preserved across calls) | callee |
| 19 | $s3 | Saved value (preserved across calls) | callee |
| 20 | $s4 | Saved value (preserved across calls) | callee |
| 21 | $s5 | Saved value (preserved across calls) | callee |
| 22 | $s6 | Saved value (preserved across calls) | callee |
| 23 | $s7 | Saved value (preserved across calls) | callee |
| 24 | $t8 | Temporary value | caller |
| 25 | $t9 | Temporary value | caller |
| 26 | $k0 | Reserved for the operating system kernel | n/a |
| 27 | $k1 | Reserved for the operating system kernel | n/a |
| 28 | $gp | Global pointer (static data segment) | n/a |
| 29 | $sp | Stack pointer (top of stack; grows downward) | callee |
| 30 | $fp | Frame pointer (not used in this course) | callee |
| 31 | $ra | Return address written by jal | caller |
Memory layout
| Segment | Start | Note |
|---|---|---|
| Reserved | 0x00000000 | Not usable by programs |
| Text | 0x00400000 | Program instructions; PC starts here |
| Static data | 0x10000000 | MARS places .data at 0x10010000; $gp = 0x10008000 |
| Heap (dynamic data) | 0x10040000 | Grows upward (sbrk, syscall 9) |
| Stack | 0x7FFFFFFF | Grows downward; $sp initialised near the top |
| Kernel | 0x80000000 | Reserved 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)
| Code | Service | Arguments | Result | In course |
|---|---|---|---|---|
| 1 | print_int | $a0 = integer to print | none | yes (L7) |
| 2 | print_float | $f12 = float to print | none | no |
| 3 | print_double | $f12 = double to print | none | no |
| 4 | print_string | $a0 = address of null-terminated string | none | yes (L7) |
| 5 | read_int | none | $v0 = integer read | yes (L7) |
| 6 | read_float | none | $f0 = float read | no |
| 7 | read_double | none | $f0 = double read | no |
| 8 | read_string | $a0 = address of input buffer; $a1 = maximum number of characters to read (including the null terminator) | buffer filled; input is null-terminated | yes (L7) |
| 9 | sbrk | $a0 = number of bytes to allocate | $v0 = address of allocated memory | no |
| 10 | exit | none | program terminates | yes (L7) |
| 11 | print_char | $a0 = character to print (low byte) | none | yes (L7) |
| 12 | read_char | none | $v0 = character read | yes (L7) |
| 13 | open_file | $a0 = address of filename; $a1 = flags; $a2 = mode | $v0 = file descriptor (negative on error) | no |
| 14 | read_file | $a0 = file descriptor; $a1 = buffer address; $a2 = max chars | $v0 = chars read (0 = EOF, negative = error) | no |
| 15 | write_file | $a0 = file descriptor; $a1 = buffer address; $a2 = chars to write | $v0 = chars written | no |
| 16 | close_file | $a0 = file descriptor | none | no |
| 17 | exit2 | $a0 = termination result code | program terminates with code | no |
| 30 | time | none | $a0 = low 32 bits, $a1 = high 32 bits of system time (ms) | no |
| 32 | sleep | $a0 = milliseconds | none | no |
| 34 | print_int_hex | $a0 = integer | prints as 8 hex digits | no |
| 35 | print_int_bin | $a0 = integer | prints as 32 binary digits | no |
| 36 | print_int_unsigned | $a0 = integer | prints as unsigned decimal | no |
| 41 | random_int | $a0 = generator id | $a0 = random int | no |
| 42 | random_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