MIPS Syscalls
Making a syscall
MIPS syscalls are used to make requests to the operating system. They are not instructions that are executed by the CPU, but rather instructions that are used by the simulator to make requests to the operating system.
Each syscall has a unique code that is used to identify it. You must put the syscall code inside the $v0 register before calling the syscall instruction. As an example:
# Load the integer 42 into register $a0, which is
# the register that will be printed by the syscall
li $a0, 42
# Set the syscall code for printing an integer
li $v0, 1
syscall
Print integer
$v0 = 1- $a0
- Integer to print
Print float
$v0 = 2- $f12
- Float to print
Print double
$v0 = 3- $f12
- Double to print
Print string
$v0 = 4- $a0
- Address of null-terminated string to print
Read integer
$v0 = 5- $v0 after
- Contains integer read
Read float
$v0 = 6- $f0 after
- Contains float read
Read double
$v0 = 7- $f0 after
- Contains double read
Read string
$v0 = 8- $a0
- Address of input buffer
- $a1
- Maximum number of characters to read
- Note
- Service 8 - Follows semantics of UNIX 'fgets'. For specified length n, string can be no longer than n-1. If less than that, adds newline to end. In either case, then pads with null byte If n = 1, input is ignored and null byte placed at buffer address. If n < 1, input is ignored and nothing is written to the buffer.
Sbrk (allocate heap memory)
$v0 = 9- $a0
- Number of bytes to allocate
- $v0 after
- Contains address of allocated memory
Exit (terminate execution)
$v0 = 10Print character
$v0 = 11- $a0
- Character to print
- Note
- Service 11 - Prints ASCII character corresponding to contents of low-order byte.
Read character
$v0 = 12- $v0 after
- Contains character read
Open file
$v0 = 13- $a0
- Address of null-terminated string containing filename
- $a1
- Flags
- $a2
- Mode
- $v0 after
- Contains file descriptor (negative if error)
- Note
- Service 13 - MARS implements three flag values: 0 for read-only, 1 for write-only with create, and 9 for write-only with create and append. It ignores mode. The returned file descriptor will be negative if the operation failed. MARS maintains file descriptors internally and allocates them starting with 3. File descriptors 0, 1 and 2 are always open for: reading from standard input, writing to standard output, and writing to standard error, respectively (new in release 4.3).
Read from file
$v0 = 14- $a0
- File descriptor
- $a1
- Address of input buffer
- $a2
- Maximum number of characters to read
- $v0 after
- Contains number of characters read (0 if end-of-file, negative if error)
- Note
- Services 13,14,15 - In MARS 3.7, the result register was changed to $v0 for SPIM compatability. It was previously $a0 as erroneously printed in Appendix B of Computer Organization and Design,.
Write to file
$v0 = 15- $a0
- File descriptor
- $a1
- Address of output buffer
- $a2
- Number of characters to write
- $v0 after
- Contains number of characters written (negative if error)
- Note
- Services 13,14,15 - In MARS 3.7, the result register was changed to $v0 for SPIM compatability. It was previously $a0 as erroneously printed in Appendix B of Computer Organization and Design,.
Close file
$v0 = 16- $a0
- File descriptor
Exit2 (terminate with value)
$v0 = 17- $a0
- Termination result
- Note
- Service 17 - If the MIPS program is run under control of the MARS graphical interface (GUI), the exit code in $a0 is ignored.
Time (program time)
$v0 = 30- $a0 after
- Low order 32 bits of the program time
- $a1 after
- High order 32 bits of the program time
- Note
- Service 30 - Milliseconds since the run started, rather than since 1 January 1970 as in MARS: it is the time the program can observe passing, and in a testcase it comes from a virtual clock that starts at zero and only advances through the waits of service 32.
Sleep
$v0 = 32- $a0
- The length of time to sleep in milliseconds
- Note
- Service 32 - Lets that much program time pass before the next instruction. The editor stays responsive while it waits and the wait costs no instructions, so a program idling on the keyboard never reaches the execution limit; in a testcase it completes at once and advances the virtual clock instead.
Print integer in hexadecimal
$v0 = 34- $a0
- Integer to print
- Note
- Displayed value is 8 hexadecimal digits, left-padding with zeroes if necessary.
Print integer in binary
$v0 = 35- $a0
- Integer to print
- Note
- Displayed value is 32 bits, left-padding with zeroes if necessary.
Print integer as unsigned
$v0 = 36- $a0
- Integer to print
- Note
- Displayed as unsigned decimal value.
Random int
$v0 = 41- $a0
- I.d. of pseudorandom number generator (any int)
- $a0 after
- Contains the next pseudorandom, uniformly distributed int value from this random number generator's sequence
- Note
- Each stream (identified by $a0 contents) is modeled by a different Random object. There are no default seed values, so use the Set Seed service (40) if replicated random sequences are desired.
Random int range
$v0 = 42- $a0
- I.d. of pseudorandom number generator (any int)
- $a1
- Upper bound of range of returned values
- $a0 after
- Contains pseudorandom, uniformly distributed int value in the range 0 <= [int] < [upper bound], drawn from this random number generator's sequence
- Note
- Each stream (identified by $a0 contents) is modeled by a different Random object. There are no default seed values, so use the Set Seed service (40) if replicated random sequences are desired.
Random float
$v0 = 43- $a0
- I.d. of pseudorandom number generator (any int)
- $f0 after
- Contains the next pseudorandom, uniformly distributed float value in the range 0.0 <= f < 1.0 from this random number generator's sequence
- Note
- Each stream (identified by $a0 contents) is modeled by a different Random object. There are no default seed values, so use the Set Seed service (40) if replicated random sequences are desired.
Random double
$v0 = 44- $a0
- I.d. of pseudorandom number generator (any int)
- $f0 after
- Contains the next pseudorandom, uniformly distributed double value in the range 0.0 <= f < 1.0 from this random number generator's sequence
- Note
- Each stream (identified by $a0 contents) is modeled by a different Random object. There are no default seed values, so use the Set Seed service (40) if replicated random sequences are desired.
ConfirmDialog
$v0 = 50- $a0
- Address of null-terminated string that is the message to user
- $a0 after
- Contains value of user-chosen option 0: Yes 1: No 2: Cancel
InputDialogInt
$v0 = 51- $a0
- Address of null-terminated string that is the message to user
- $a0 after
- Contains int read
- $a1 after
- Contains status value 0: OK status -1: input data cannot be correctly parsed -2: Cancel was chosen -3: OK was chosen but no data had been input into field
InputDialogFloat
$v0 = 52- $a0
- Address of null-terminated string that is the message to user
- $f0 after
- Contains float read
- $a1 after
- Contains status value 0: OK status -1: input data cannot be correctly parsed -2: Cancel was chosen -3: OK was chosen but no data had been input into field
InputDialogDouble
$v0 = 53- $a0
- Address of null-terminated string that is the message to user
- $f0 after
- Contains double read
- $a1 after
- Contains status value 0: OK status -1: input data cannot be correctly parsed -2: Cancel was chosen -3: OK was chosen but no data had been input into field
InputDialogString
$v0 = 54- $a0
- Address of null-terminated string that is the message to user
- $a1
- Address of input buffer
- $a2
- Maximum number of characters to read
- $a1 after
- Contains status value 0: OK status. Buffer contains the input string. -2: Cancel was chosen. No change to buffer. -3: OK was chosen but no data had been input into field. No change to buffer. -4: length of the input string exceeded the specified maximum. Buffer contains the maximum allowable input string plus a terminating null.
- Note
- See Service 8 note below table
MessageDialog
$v0 = 55- $a0
- Address of null-terminated string that is the message to user
- $a1
- The type of message to be displayed: 0: error message, indicated by Error icon 1: information message, indicated by Information icon 2: warning message, indicated by Warning icon 3: question message, indicated by Question icon other: plain message (no icon displayed)
MessageDialogInt
$v0 = 56- $a0
- Address of null-terminated string that is an information-type message to user
- $a1
- Int value to display in string form after the first string
MessageDialogFloat
$v0 = 57- $a0
- Address of null-terminated string that is an information-type message to user
- $f12
- Float value to display in string form after the first string
MessageDialogDouble
$v0 = 58- $a0
- Address of null-terminated string that is an information-type message to user
- $f12
- Double value to display in string form after the first string
MessageDialogString
$v0 = 59- $a0
- Address of null-terminated string that is an information-type message to user
- $a1
- Address of null-terminated string to display after the first string