A Sample MIPS Program (Echo.asm)


The following program prompts a string that asks the user to enter a string, and prints text with the entered string.

Echo.asm
       .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

##################### Program Code ######################

       .text
       .globl  main
main:
# Request a string.
       # Issue a prompt and read a string.
       li      $v0, 4                  # Print a string.
       la      $a0, pr1                # Prompt 1
       syscall
       li      $v0, 8                  # Read a string.
       la      $a0, buf                # Buffer
       li      $a1, 200                # Length of buffer
       syscall

# Print the entered string and a message.
       li      $v0, 4                  # Print a string.
       la      $a0, pr2                # Prompt 2
       syscall
       li      $v0, 4                  # Print a string.
       la      $a0, buf                # Buffer
       syscall
       li      $v0, 4                  # Print a string.
       la      $a0, msg                # Message
       syscall

# Exit the program.
       li      $v0, 10                 # Exit.
       syscall
An Execution Example
Please enter a string: Hello, World! 
You entered: Hello, World!
Congratulations, SPIMmer!

The white italic text with a navy background color is entered by users.




      His diet went out the window (disappear) during the holidays.