SPIM - MIPS processor simulator

SPIM is a MIPS processor simulator, designed to run assembly language code for RISC architecture.

"Hello World" in SPIM

This is a simple program to print Hello World. A comment starts with a # till the end of the line.

.data         # start putting stuff in the data segment
greet: .asciiz "Hello world\n" # declare greet to be the string
.text         #start putting stuff in the text segment

main:         # main is a label here. Names a function
li $v0, 4     # system call code for print_str(sect1.5)
la $a0, greet # address of string to print
syscall       # print the string
jr $ra        # return from main

li is load immediate into an integer register
la is load computed address into an integer register
jr is standard return from function call - $ra contains return address

# this program adds the numbers 1 to 10

move $t0, $zero   # initialize sum (t0) to 0
move $t1, $zero   # initialize counter (t1) to 0

addi $t1, $t1, 1  # increment counter by 1
add $t0, $t0, $t1 # add counter to sum
blt $t1, 10, loop # if counter < 10, goto loop

# this is outside the loop
# code to print the sum
li $v0, 1         # system call for print_int
move $a0, $t0     # the sum to print
syscall           # print the sum

SPIM Quick Reference