MC logo

String Echo

  MIPS Code Examples

echo.s
        .text
        .globl  main
main:
# Save registers.
        subu    $sp, $sp, 4             # Make room on the stack.
        sw      $ra, 0($sp)             # Save the return address.
        
# Request a string.
        # Issume a prompt and read a string.
        li      $v0, 4                  # Print prompt string.
        la      $a0, pr1
        syscall
        li      $v0, 8                  # Read string.
        la      $a0, buf                # Buffer
        li      $a1, 200                # Length of buffer
        syscall

# Print the messages and the string
        li      $v0, 4
        la      $a0, pr2
        syscall
        li      $v0, 4
        la      $a0, buf
        syscall
        li      $v0, 4
        la      $a0, msg
        syscall

        # Return
        lw      $ra, 0($sp)             # Restore the return address.
        addu    $sp, $sp, 4             # Pop the stack
        jr      $ra

        .data
# String constants for messages.
pr1:    .asciiz "Please enter a string: "
pr2:    .asciiz "You entered: "
msg:    .asciiz "Congratulations, SPIMmer!\n"

# Space for the input string.
buf:    .space  200