A number of system services, mainly for input and output, are available for use by your MIPS program. They are described in the table below.

MIPS register contents are not affected by a system call, except for result registers as specified in the table below.

How to use SYSCALL system services

Step 1. Load the service number in register $v0.
Step 2. Load argument values, if any, in $a0, $a1, $a2, or $f12 as specified.
Step 3. Issue the SYSCALL instruction.
Step 4. Retrieve return values, if any, from result registers as specified.
Example: display the value stored in $t0 on the console

li  $v0, 1           # service 1 is print integer
add $a0, $t0, $zero  # load desired value into argument register $a0, using pseudo-op
syscall

Available Services

$v0ServiceArgumentsResult
1print integer$a0 = integer to print
2print float$f12 = float to print
3print double$f12 = double to print
4print string$a0 = address of null-terminated string to print
5read integer$v0 contains integer read
6read float$f0 contains float read
7read double$f0 contains double read
8read string$a0 = address of input buffer
$a1 = maximum number of characters to read
See note below table
9sbrk (allocate heap mem.)$a0 = number of bytes to allocate$v0 contains address of allocated memory
10exit (terminate exe.)
11print character$a0 = character to printSee note below table
12read character$v0 contains character read
13open file$a0 = address of null-terminated string containing filename
$a1 = flags
$a2 = mode
$v0 contains file descriptor (negative if error). See note below table
14read from file$a0 = file descriptor
$a1 = address of input buffer
$a2 = maximum number of characters to read
$v0 contains number of characters read (0 if end-of-file, negative if error). See note below table
15write to file$a0 = file descriptor
$a1 = address of output buffer
$a2 = number of characters to write
$v0 contains number of characters written (negative if error). See note below table
16close file$a0 = file descriptor
17exit2 (terminate with value)$a0 = termination resultSee note below table
30time (system time)$a0 = low order 32 bits of system time
$a1 = high order 32 bits of system time. See note below table
31MIDI out$a0 = pitch (0-127)
$a1 = duration in milliseconds
$a2 = instrument (0-127)
$a3 = volume (0-127)
Generate tone and return immediately. See note below table
32sleep$a0 = the length of time to sleep in milliseconds.Causes the MARS Java thread to sleep for (at least) the specified number of milliseconds. This timing will not be precise, as the Java implementation will add some overhead.
33MIDI out sync.$a0 = pitch (0-127)
$a1 = duration in milliseconds
$a2 = instrument (0-127)
$a3 = volume (0-127)
Generate tone and return upon tone completion. See note below table
34print integer in hexadecimal$a0 = integer to printDisplayed value is 8 hexadecimal digits, left-padding with zeroes if necessary.
35print int. in binary$a0 = integer to printDisplayed value is 32 bits, left-padding with zeroes if necessary.
36print int. as unsigned$a0 = integer to printDisplayed as unsigned decimal value.
37-39(not used)
40set seed$a0 = i.d. of pseudorandom number generator (any int).
$a1 = seed for corresponding pseudorandom number generator.
No values are returned. Sets the seed of the corresponding underlying Java pseudorandom number generator (java.util.Random). See note below table
41random int$a0 = i.d. of pseudorandom number generator (any int).$a0 contains the next pseudorandom, uniformly distributed int value from this random number generator’s sequence. See note below table
42random int range$a0 = i.d. of pseudorandom number generator (any int).
$a1 = upper bound of range of returned values.
$a0 contains pseudorandom, uniformly distributed int value in the range 0 = [int] [upper bound], drawn from this random number generator’s sequence. See note below table
43random float$a0 = i.d. of pseudorandom number generator (any int).$f0 contains the next pseudorandom, uniformly distributed float value in the range 0.0 = f 1.0 from this random number generator’s sequence. See note below table
44random double$a0 = i.d. of pseudorandom number generator (any int).$f0 contains the next pseudorandom, uniformly distributed double value in the range 0.0 = f 1.0 from this random number generator’s sequence. See note below table
45-49(not used)
50ConfirmDialog$a0 = address of null-terminated string that is the message to user$a0 contains value of user-chosen option
0: Yes
1: No
2: Cancel
51InputDialogInt$a0 = address of null-terminated string that is the message to user$a0 contains int read
$a1 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
52InputDialogFloat$a0 = address of null-terminated string that is the message to user$f0 contains float read
$a1 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
53InputDialogDouble$a0 = address of null-terminated string that is the message to user$f0 contains double read
$a1 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
54InputDialogString$a0 = address of null-terminated string that is the message to user
$a1 = address of input buffer
$a2 = maximum number of characters to read
See Service 8 note below table
$a1 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.
55MessageDialog$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)
N/A
56MessageDialogInt$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
N/A
57MessageDialogFloat$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
N/A
58MessageDialogDouble$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
N/A
59MessageDialogString$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
N/A
58MessageDialogDouble$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
N/A
59MessageDialogString$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
N/A

Services 1 through 17 are compatible with the SPIM simulator, other than Open File (13) as described in the Notes below the table. Services 30 and higher are exclusive to MARS.

NOTES: Services numbered 30 and higher are not provided by SPIM
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.
Service 11 - Prints ASCII character corresponding to contents of low-order byte.
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. The underlying file I/O implementation uses java.io.FileInputStream.read() to read and java.io.FileOutputStream.write() to write. 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).
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,.
Service 17 - If the MIPS program is run under control of the MARS graphical interface (GUI), the exit code in $a0 is ignored.
Service 30 - System time comes from java.util.Date.getTime() as milliseconds since 1 January 1970.
Services 31,33 - Simulate MIDI output through sound card. Details below.
Services 40-44 use underlying Java pseudorandom number generators provided by the java.util.Random class. 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.

Example of File I/O

The sample MIPS program below will open a new file for writing, write text to it from a memory buffer, then close it. The file will be created in the directory in which MARS was run.

# Sample MIPS program that writes to a new file.
# by Kenneth Vollmar and Pete Sanderson
 
.data
fout:   .asciiz "testout.txt"      # filename for output
buffer: .asciiz "The quick brown fox jumps over the lazy dog."
.text
###############################################################
# Open (for writing) a file that does not exist
li   $v0, 13       # system call for open file
la   $a0, fout     # output file name
li   $a1, 1        # Open for writing (flags are 0: read, 1: write)
li   $a2, 0        # mode is ignored
syscall            # open a file (file descriptor returned in $v0)
move $s6, $v0      # save the file descriptor 
###############################################################
# Write to file just opened
li   $v0, 15       # system call for write to file
move $a0, $s6      # file descriptor 
la   $a1, buffer   # address of buffer from which to write
li   $a2, 44       # hardcoded buffer length
syscall            # write to file
###############################################################
# Close the file 
li   $v0, 16       # system call for close file
move $a0, $s6      # file descriptor to close
syscall            # close file
###############################################################