Image goes here
SPIM I/O
CptS 260 - Intro to Computer Architecture
Washington State University

SPIM I/O

Housekeeping

  • Exam coming up on Friday, 9/26
  • Will have review on Wed., 9/24
The MIPS stack frame conventions are a mess: there have been many conventions over the years used by different compilers. Larus seems to have mixed up several different conventions in appendix A. Further, the reasons behind some of the conventions seem to be lost to history. I've linked an old email on the class resources page that may help understand(!) the situation.

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 output

I/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 syscall after loading some specific with information about what kind of I/O is to be done. Note that syscall preserves registers not mentioned here as being changed.

ServiceCode in $v0Argument(s)Results
Print Integer1$a0 = number to print
Print Float2$f12 = number to print
Print Double3$f12 = number to print
Print String4$a0 = address of string to print
Read Integer5number returned in $v0
Read Float6number returned in $f0
Read Double7number returned in $f0
Read String8$a0 = buffer address
$a1 = buffer length
Exit10
# 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"
	
(c) 2004-2006 Carl H. Hauser           E-mail questions or comments to Prof. Carl Hauser