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
01       .data
02# String constants for messages
03pr1:   .asciiz   "Please enter a string: "
04pr2:   .asciiz   "You entered: "
05msg:   .asciiz   "Congratulations, SPIMmer!\n"
06  
07# Space for the input string
08buf:   .space    200
09 
10##################### Program Code ######################
11 
12       .text
13       .globl  main
14main:
15# Request a string.
16       # Issue a prompt and read a string.
17       li      $v0, 4                  # Print a string.
18       la      $a0, pr1                # Prompt 1
19       syscall
20       li      $v0, 8                  # Read a string.
21       la      $a0, buf                # Buffer
22       li      $a1, 200                # Length of buffer
23       syscall
24 
25# Print the entered string and a message.
26       li      $v0, 4                  # Print a string.
27       la      $a0, pr2                # Prompt 2
28       syscall
29       li      $v0, 4                  # Print a string.
30       la      $a0, buf                # Buffer
31       syscall
32       li      $v0, 4                  # Print a string.
33       la      $a0, msg                # Message
34       syscall
35 
36# Exit the program.
37       li      $v0, 10                 # Exit.
38       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.