------------------------------------------------------------------------------
MC logo
MIPS Example
[^] Chapter 5: Real-World Architectures
------------------------------------------------------------------------------
[Chapter 1: Introduction] [Chapter 2: Data Representation] [Chapter 3: Boolean Algebra and Digital Logic] [Chapter 4: MARIE: A Very Simple Architecture] [386 Assembler Language] [Chapter 5: Real-World Architectures] [Chapter 6: Memory Systems] [Chapter 7: I/O Systems] [Chapter 9: Alternative Architecture] [Chapter 11: Performance Measurement]
[Which Endian Is Up?] [MIPS Example]
add2b.s
#
# 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