Anda di halaman 1dari 5

SPIM Instruction Set

This document gives an overview of the more common instructions used in the SPIM simulator.

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.

Instructions and PseudoInstructions


The following is an abbreviated list of MIPS instructions and SPIM pseudoinstructions.
This list is not complete. Notably missing are all Floating Point and coprocessor
instructions.
- Indicates an actual MIPS instruction. Others are SPIM pseudoinstructions.
Instruction
add
Rd, Rs, Rt
addu Rd, Rs, Rt
addi
Rd, Rs, Imm
sub
Rd, Rs, Rt
subu Rd, Rs, Rt
div
Rs, Rt
divu
Rs, Rt
div
Rd, Rs, Rt
divu
Rd, Rs, Rt
rem
Rd, Rs, Rt
remu Rd, Rs, Rt
mul
Rd, Rs, Rt
mult
Rs, Rt
multu Rd, Rs
bits)

Function
Rd = Rs + Rt (signed)
Rd = Rs + Rt (unsigned)
Rd = Rs + Imm (signed)
Rd = Rs - Rt (signed)
Rd = Rs - Rt (unsigned)
lo = Rs/Rt, hi = Rs mod Rt (integer division, signed)
lo = Rs/Rt, hi = Rs mod Rt (integer division, unsigned)
Rd = Rs/Rt (integer division, signed)
Rd = Rs/Rt (integer division, unsigned)
Rd = Rs mod Rt (signed)
Rd = Rs mod Rt (unsigned)
Rd = Rs * Rt (signed)
hi, lo = Rs * Rt (signed, hi = high 32 bits, lo = low 32 bits)
hi, lo = Rs * Rt (unsigned, hi = high 32 bits, lo = low 32

and
andi
neg
nor
not
or
ori
xor
xori

Rd = Rs Rt
Rd = Rs Imm
Rd = -(Rs)
Rd = (Rs + Rt)
Rd = (Rs)
Rd = Rs + Rt
Rd = Rs + Imm
Rd = Rs Rt
Rd = Rs Imm

Rd, Rs, Rt
Rd, Rs, Imm
Rd, Rs
Rd, Rs, Rt
Rd, Rs
Rd, Rs, Rt
Rd, Rs, Imm
Rd, Rs, Rt
Rd, Rs, Imm

sll
sllv
srl
srlv

Rd, Rt, Sa
Rd, Rs, Rt
Rd, Rs, Sa
Rd, Rs, Rt

Rd = Rt left shifted by Sa bits


Rd = Rt left shifted by Rs bits
Rd = Rt right shifted by Sa bits
Rd = Rt right shifted by Rs bits

move
mfhi
mflo
li
lui

Rd, Rs
Rd
Rd
Rd, Imm
Rt, Imm

Rd = Rs
Rd = hi
Rd = lo
Rd = Imm
Rt[31:16] = Imm, Rt[15:0] = 0

Rt, Address(Rs)
Rt, Address(Rs)
Rt, Address(Rs)
Rt, Address(Rs)

Rt = byte at M[Address + Rs] (sign extended)


Byte at M[Address + Rs] = Rt (sign extended)
Rt = word at M[Address + Rs]
Word at M[Address + Rs] = Rt

slt
slti
sltu

Rd, Rs, Rt
Rd, Rs, Imm
Rd, Rs, Rt

Rd = 1 if Rs < Rt, Rd = 0 if Rs Rt (signed)


Rd = 1 if Rs < Imm, Rd = 0 if Rs Imm (signed)
Rd = 1 if Rs < Rt, Rd = 0 if Rs Rt (unsigned)

beq
beqz
bge
bgez
bgezal
bgt
bgtu
bgtz
ble
bleu
blez
bgezal
bltzal
blt
bltu
bltz
bne
bnez

Rs, Rt, Label


Rs, Label
Rs, Rt, Label
Rs, Label
Rs, Label
Rs, Rt, Label
Rs, Rt, Label
Rs, Label
Rs, Rt, Label
Rs, Rt, Label
Rs, Label
Rs, Label
Rs, Label
Rs, Rt, Label
Rs, Rt, Label
Rs, Label
Rs, Rt, Label
Rs, Label

Branch to Label if Rs == Rt
Branch to Label if Rs == 0
Branch to Label if Rs Rt (signed)
Branch to Label if Rs 0 (signed)
Branch to Label and Link if Rs Rt (signed)
Branch to Label if Rs > Rt (signed)
Branch to Label if Rs > Rt (unsigned)
Branch to Label if Rs > 0 (signed)
Branch to Label if Rs Rt (signed)
Branch to Label if Rs Rt (unsigned)
Branch to Label if Rs 0 (signed)
Branch to Label and Link if Rs 0 (signed)
Branch to Label and Link if Rs < 0 (signed)
Branch to Label if Rs < Rt (signed)
Branch to Label if Rs < Rt (unsigned)
Branch to Label if Rs < 0 (signed)
Branch to Label if Rs Rt
Branch to Label if Rs 0

Label
Label
Rs
Label

Jump to Label unconditionally


Jump to Label and link unconditionally
Jump to location in Rs unconditionally
Jump to location in Rs and link unconditionally

lb
sb
lw
sw

j
jal
jr
jalr

Registers
By convention, many MIPS registers have special purpose uses. To help clarify this,
SPIM defines aliases for each register that represent its purpose. The following table lists
these aliases and the commonly accepted uses for the registers.
Register
zero
at
v0
v1
a0
a1
a2
a3
t0
t1
t2
t3
t4
t5
t6
t7
s0

Number
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

s1
s2
s3
s4
s5
s6
s7
t8
t9
k0
k1
gp
sp
fp
ra

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

Usage
Constant 0
Reserved for assembler
Used for return values from function calls.
Used to pass arguments to procedures and functions.

Temporary (Caller-saved, need not be saved by called procedure)

Saved temporary (Callee-saved, called procedure must save and


restore)

Temporary (Caller-saved, need not be saved by called procedure)


Reserved for OS kernel
Pointer to global area
Stack pointer
Frame pointer
Return address for function calls.

System Calls
In order to perform I/O with the console, SPIM provides a small library of system calls.
In general, system calls are set up by placing a system call in register $v0, and any

arguments in register $a0 and $a1. Returned values are placed in register $v0. See the
table and the example program below for usage.

Example Program
# This program takes input from the user and echoes it back

Service
Print_int
Print_float
Print_double
Print_string
Read_int
Read_float
Read_double
Read_string
Sbrk
exit

System Call Code


1
2
3
4
5
6
7
8
9
10

Arguments
$a0 = integer
$f12 = float
$f12 = double
$a0 = string

$a0 = buffer, $a1 = length


$a0 = amount

Result

Integer (in $v0)


Float (in $f0)
Double (in $f0)
Address (in $v0)

.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
la
$a0,promptInt
syscall

# code for print_string


# point $a0 to prompt string
# print the prompt

# get an integer from the user


li
$v0,5
syscall
move
$t0,$v0

# code for read_int


#get int from user --> returned in $v0
# move the resulting int to $t0

# compute the next integer


addi
$t0, $t0, 1

# t0 <-- t0 + 1

# print out text for the result


li
$v0,4
la
$a0,resultInt
syscall

#code for print_string


# point $a0 to result string
# print the result string

# print out the result


li
$v0,1
move
$a0,$t0
syscall

# code for print_int


# put result in $a0
# print out the result

# print out a line feed


li
$v0,4
la
$a0,linefeed
syscall

# code for print_string


# point $a0 to linefeed string
# print linefeed

# wait for the enter key to be pressed to end program


li
$v0,4
# code for print_string
la
$a0,enterkey
# point $a0 to enterkey string
syscall
# print enterkey
# 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
# All done, thank you!
li
$v0,10
syscall

# code for exit


# exit program

Anda mungkin juga menyukai