Input And Output
Standard input and output are performed using system calls. There is no built in i/o formatting. In a high level language, this is handled by library functions. Output Example. Consider the following example which displays a string of text followed by an integer.
.data
str: .asciiz "the answer = "
.text
main:
li $v0, 4 # code to print a string.
la $a0, str # load addr of string.
syscall # print the string.
li $v0, 1 # code to print an int.
li $a0, 5 # load immediate into $a0.
syscall # print the int.
li $v0, 10 # code to exit.
syscall # print the int.
Input Example. System calls are also used to input data. Consider the following code segment
int x, y, z;
read x;
read y;
z = x + y;
print z;
This segment is implemented in the MIPS langauge as follows
.data
x: .space 4
y: .space 4
.text
main:
li $v0, 5 # code to read an int
syscall
sw $v0, x # value was stored in $v0
li $v0, 5
syscall
sw $v0, y
lw $s0, x
lw $s1, y
add $s2, $s0, $s1
move $a0, $s2
li $v0, 1 # code to print an int
syscall
exit:
li $v0, 10
syscall
System Calls. The following table provides a list of the system calls used by the SPIM simulator.
|