SYSCALL functions available in MARS

Introduction

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, and are compatible with those provided by the SPIM simulator.

How to use SYSCALL system services

As with most things assembler, this is a multistep process.
  1. load the service number in register $v0.
  2. load argument values, if any, in $a0, $a1, $a2, or $f12 as specified.
  3. issue the SYSCALL instruction
  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
    move $a0, $t0  # load desired value into argument register $a0, using pseudo-op
    syscall

Table of Available Services

Service Code in $v0 Arguments Result
print integer 1 $a0 = integer to print  
print float 2 $f12 = float to print  
print double 3 $f12 = double to print  
print string 4 $a0 = address of null-terminated string to print  
read integer 5   $v0 contains integer read
read float 6   $f0 contains float read
read double 7   $f0 contains double read
read string 8 $a0 = address of input buffer
$a1 = maximum number of characters to read
 
sbrk (allocate heap memory) 9 $a0 = number of bytes to allocate $v0 contains address of allocated memory
exit (terminate execution) 10    
print character 11 $a0 = character to print  
read character 12   $a0 contains character read
open file 13 $a0 = address of null-terminated string containing filename
$a1 = flags
$a2 = mode
$a0 contains file descriptor
read from file 14 $a0 = file descriptor
$a1 = address of input buffer
$a2 = maximum number of characters to read
$a0 contains number of characters read
write to file 15 $a0 = file descriptor
$a1 = address of output buffer
$a2 = number of characters to write
$a0 contains number of characters written
close file 16 $a0 = file descriptor  
exit2 (terminate with value) 17 $a0 = termination result  

NOTES:
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.
Service 11 - Prints ASCII character corresponding to contents of low-order byte.
Service 13 - MARS implements two modes: 0 for read and 1 for write, and ignores flags. The returned file descriptor will be -1 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 rather than using OS.
Service 17 - If the MIPS program is run under control of the MARS graphical interface (GUI), the exit code in $a0 is ignored.

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, 0        # flags
  li $a2, 1        # Open for writing (mode is 0: read, 1: write)
  syscall          # open a file (file descriptor returned in $a0)
  ###############################################################
  # Write to file just opened
  move $s6, $a0    # save the fd (syscall below will overwrite $a0)
  li $v0, 15       # system call for write to file
  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    # Restore fd
  syscall          # close file
  ###############################################################