SPIM I/O
CptS 260 - Intro to Computer Architecture Washington State University |
|||||||||||||||||||||||||||||||||||||||||
SPIM I/OHousekeeping
Meanwhile, it will be sufficient for this class to save and restore only what is required to meet the register usage conventions for $s-, $t-, $a-, $v- type registers along with $sp, $ra. I/O - Input and outputI/O is the way that a program communicates with the outside world. Later in the semester we will look more at how this works at the hardware level. For now we look at the libary facilities in SPIM for doing simple I/O.
Like in a real operating system, I/O in SPIM is implemented using "system calls". System
calls are invoked by the special instruction
# factorial with a more useful (and correct) main function
.text
fact:
addiu $sp, $sp, -32
sw $ra, 20($sp)
sw $fp, 16($sp)
addiu $fp, $sp, 28
sw $a0, 0($fp)
bgtz $a0, L2
li $v0, 1
b suffix
L2:
addi $a0, $a0, -1 # or addiu ?
jal fact
# fact(n-1) now in $v0
lw $t0, 0($fp) # pick up n
mul $v0, $v0, $t0
suffix:
# note: return value must be already set in $v0
lw $fp, 16($sp)
lw $ra, 20($sp)
addiu $sp, $sp, 32
jr $ra
main:
addiu $sp, $sp, -4 # minimum prolog
sw $ra, 0($sp)
# print a prompt
li $v0, 4
la $a0, prompt
syscall
# read a number
li $v0, 5
syscall
move $a0, $v0
# compute its factorial
jal fact
# print the factorial
move $a0, $v0
li $v0, 1
syscall
lw $ra, 0($sp) # minimum epilog
addiu $sp, $sp, 4
jr $ra
.data
prompt:
.asciiz "Enter a small number\n"
outputLabel:
.asciiz "The factorial is "
newline:
.asciiz "\n"
|
|||||||||||||||||||||||||||||||||||||||||