SPIM Instr
SPIM Instr
This document gives an overview of the more common instructions used in the SPIM simulator.
See Appendix A of Computer Organization and Design by Hennessy and Patterson for more
details.
Overview
The SPIM simulator implements the full MIPS instruction set, as well as a large number
of pseudoinstructions that correspond to one or more equivalent MIPS instructions.
There are also a small number of system call commands used to interface with the
console window of the SPIM simulator. Finally, SPIM renames registers according to
commonly used conventions in order to facilitate the readability of programs.
Instruction Function
• add Rd, Rs, Rt Rd = Rs + Rt (signed)
• addu Rd, Rs, Rt Rd = Rs + Rt (unsigned)
• addi Rd, Rs, Imm Rd = Rs + Imm (signed)
• sub Rd, Rs, Rt Rd = Rs - Rt (signed)
• subu Rd, Rs, Rt Rd = Rs - Rt (unsigned)
• div Rs, Rt lo = Rs/Rt, hi = Rs mod Rt (integer division, signed)
• divu Rs, Rt lo = Rs/Rt, hi = Rs mod Rt (integer division, unsigned)
div Rd, Rs, Rt Rd = Rs/Rt (integer division, signed)
divu Rd, Rs, Rt Rd = Rs/Rt (integer division, unsigned)
rem Rd, Rs, Rt Rd = Rs mod Rt (signed)
remu Rd, Rs, Rt Rd = Rs mod Rt (unsigned)
mul Rd, Rs, Rt Rd = Rs * Rt (signed)
• mult Rs, Rt hi, lo = Rs * Rt (signed, hi = high 32 bits, lo = low 32 bits)
• multu Rd, Rs hi, lo = Rs * Rt (unsigned, hi = high 32 bits, lo = low 32
bits)
move Rd, Rs Rd = Rs
• mfhi Rd Rd = hi
• mflo Rd Rd = lo
li Rd, Imm Rd = Imm
• lui Rt, Imm Rt[31:16] = Imm, Rt[15:0] = 0
Example Program
# This program takes input from the user and echoes it back
.data
# Constant strings to be output to the terminal
promptInt: .asciiz "Please input an integer: "
resultInt: .asciiz "Next integer is: "
linefeed: .asciiz "\n"
enterkey: .asciiz "Press any key to end program."
.text
main:
# prompt for an integer
li $v0,4 # code for print_string
la $a0,promptInt # point $a0 to prompt string
syscall # print the prompt
# wait for input by getting an integer from the user (integer is ignored)
li $v0,5 # code for read_int
syscall #get int from user --> returned in $v0