System Services

 

The MARS simulator supports I/O through a set of system services. The code for the desired service is loaded into register $v0. The requirements for using the services and the results are summarized in the following table.

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 = n, the maximum number of characters to read
string read in can be no longer than n-1 chars; if less than that, adds newline to end, then pads with null byte
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 prints ASCII character for value in low order byte
read character 12   $v0 contains character read
open file 13 $a0 = address of null-terminated string containing filename $a1 = flags $a2 = mode $v0 contains file descriptor (negative if error). See note below table
read from file 14 $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
write to file 15 $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
close file 16 $a0 = file descriptor  

Examples

Print an Integer

    # put the number to be printed into reg $a0
    li    $v0, 1             # system call code for print int
    syscall                  # print the integer in $a0

Print a String

strout: .asciiz "Here is the string to print"

    li    $v0, 4             # system call code for print string
    la    $a0, strout        # load addr of string to print in $a0
    syscall                  # print strout

Read an Integer

    li    $v0, 5             # system call code for read int
    syscall                  # read integer into $v0
    # the value just read is in reg $v0

Read a String

strin: .space 40
$a0 = address of input buffer $a1 = maximum number of characters to read

    li    $v0, 8             # system call code for read string
    la    $a0, strin         # load addr of string to print in $a0
    li    $a1, 40            # read at most 39 characters
    syscall                  # read into strin

Exit from Program

    li    $v0, 10            # system call code to exit program
    syscall	                 # return control to system


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2009