MIPS Example: Add Two Numbers
# # Program to read two numbers and add them, this one with proper register # discipline. # .text .globl main main: subu $sp, $sp, 8 # Make additional stack space. sw $ra, 4($sp) # Save the return address sw $s0, 0($sp) # Save $s0 since we change it. # Ask the OS to read a number. li $v0, 5 # Code for read int. syscall # Ask the system for service. move $s0, $v0 # Copy to safer location, one that # the next syscall preserves. # Ask for another number. li $v0, 5 # Code for read int. syscall # Ask the system for service. # Add the previous value to the one we just read. add $a0, $s0, $v0 # Ask the system to print it. li $v0, 1 # Code for print int. syscall # Restore the values from the stack, and release the stack space. lw $ra, 4($sp) # Save the return address lw $s0, 0($sp) # Save $s0 since we change it. addu $sp, $sp, 8 # Make additional stack space. # Return -- go to the address left by the caller. jr $ra