Sample MIPS Program and Output
Let us examine the following program for simple
input.
.text
.globl main
main:
li $v0,4 # output msg1
la $a0, msg1
syscall
li $v0,5 # input A and save
syscall
move $t0,$v0
li $v0,4 # output msg2
la $a0, msg2
syscall
li $v0,5 # input B and save
syscall
move $t1,$v0
add $t0, $t0, $t1 # A = A + B
li $v0, 4 # output msg3
la $a0, msg3
syscall
li $v0,1 # output sum
move $a0, $t0
syscall
li $v0,4 # output lf
la $a0, cflf
syscall
li $v0,10 # exit
syscall
.data
msg1: .asciiz "\nEnter A: "
msg2: .asciiz "\nEnter B: "
msg3: .asciiz "\nA + B = "
cflf: .asciiz "\n"
Output
Enter A: 10
Enter B: 5
A + B = 15
Comments on the program:
li $v0,5 # input A and save
Load the system register,$v0, with the input code.
syscall
System call to accept an integer from the keyboard and store
it in register $v0.
move $t0,$v0
Macro to transfer the contents of system register $v0 to temporary
register $t0.
add $t0, $t0, $t1 # A = A + B
Add the contents of $t0 to the contents of $y1 and place the
sum in $t0.
li $v0,1 # output sum
Load the system register $v0 with the output integer code.
Click here to write your own code
in an Applet!