MIPS Reference


Input And Output

<< Memory Access | Table of Contents | Branching And Logic >>
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.

Service Code Arguments Result
print int 1 $a0 = integer
print float 2 $f12 = float
print string 4 $a0 = address of string
read int 5 int in $v0
read float 6 float in $f0
read string 8
exit 10


<< Memory Access | Table of Contents | Branching And Logic >>


MIPS Processor and Assembly Language Reference

Print - Changes - Search
Last modified: September 19, 2008, at 01:54 PM.