next up previous
Next: Description of the Machine Up: SPIM S20: A MIPS Previous: Assembler Syntax

System Calls

   table199
Table 1: System services.

SPIM provides a small set of operating-system-like services through the system call (syscall) instruction. To request a service, a program loads the type code (see Table 1) into register $v0 and the arguments into registers $a0 tex2html_wrap_inline1653 $a3 (or $f12 for floating point values).gif System calls that return values put their result in register $v0 (or $f0 for floating point results). For example, to print ``the answer = 5'', use the commands:

        .data
  str:  .asciiz "the answer = "
        .text
        li $v0, 4        # print_str
        la $a0, str
        syscall
        li $v0, 1        # print_int
        li $a0, 5
        syscall

print_int is passed and integer and prints it on the console. print_float prints a single floating point number. print_double prints a double precision number. print_string is passed a pointer to a null-terminated string, which it writes to the console.

read_int, read_float, and read_double read an entire line of input up to and including the newline. Characters following the number are ignored. read_string has the same semantics as the Unix library routine fgets. It reads upto n-1 characters in a buffer and terminates the string with a null byte. If there are fewer characters on the current line, it reads through the newline and again null-terminates the string.

sbrk returns a pointer to a block of memory containing n additional bytes. exit stops a program from running.



Mitch Roth
Fri Sep 6 23:25:26 ADT 1996