A syscall is a request to the operating system (OS) to have it perform a service for us. In this class we will mainly use it to ask the OS to print to the console.
Before we do the syscall, we need to set certain registers that the OS will read so it knows what we want it to do.
We set the $v0 register to indicate what operation we want to do.
We set the $a0 register to the argument for the syscall. (Think of passing an argument into a function.)
| Value to set $v0 | Operation | Argument |
|---|---|---|
| 1 | Print int | int |
| 4 | print string | char* |
| 11 | print char | char |
| 10 | exit() | n/a |
we set the $a0 register to the parameter value for the syscall (think of arguments when calling a function)
.data
HELLO: .asciiz "Hello, World!\\n"
VALUE_TEXT: .asciiz "Value: "
.text
addi $v0, $zero, 4 # set $v0 to 4. Which makes the syscall print the string pointed to by $a0
la $a0, HELLO # set a0 to the address of the string
syscall # prints "Hello, World!\\n"
addi $v0, $zero, 4 # same thing...
la $a0, VALUE_TEXT # same thing...
syscall # prints "Value: "
addi $v0, $zero, 1 # set $v0 to 1. Which makes the syscall print the int value stored in $a0
addi $a0, $zero, 1234 # set $a0 to 1234, the value we want to print.
syscall # prints 1234
# final console output:
# Hello, World!
# Value: 1234