Notes: op, funct, rd, rs, rt, imm, address, shamt refer to fields in the instruction format
PC is assumed to point to the next instruction, Mem is the byte addressed main memory
Common MIPS instructions
Assembly
Instruction
Instr
Format
op
op/funct
Meaning
Comments
add $rd, $rs, $rt
sub $rd, $rs, $rt
addi $rt, $rs, imm
R
R
I
addu $rd, $rs, $rt R
subu $rd, $rs, $rt
R
addiu $rt, $rs, imm I
mfc0 $rt, $rd
mult $rs, $rt
multu $rs, $rt
div $rs, $rt
divu $rs, $rt
mfhi $rd
mflo $rd
and $rd, $rs, $rt
or $rd, $rs, $rt
andi $rt, $rs, imm
ori $rt, $rs, imm
R
R
R
R
R
R
R
R
R
I
I
sll $rd, $rs, shamt R
srl $rd, $rs, shamt R
lw $rt, imm($rs)
sw $rt, imm($rs)
lbu $rt, imm($rs)
sb $rt, imm($rs)
lui $rt, imm
beq $rs, $rt, imm
bne $rs, $rt, imm
slt $rd, $rs, $rt
slti $rt, $rs, imm
sltu $rd, $rs, $rt
sltiu $rt, $rs, imm
j destination
jal destination
jr $rs
I
I
I
I
I
I
I
R
I
Advertisement
R
I
J
J
R
0/32
0/34
8
0/33
0/35
9
16
0/24
0/25
0/26
0/27
0/16
0/18
0/36
0/37
12
13
0/0
0/2
35
43
37
41
15
4
5
0/42
10
0/43
11
2
3
0/8
$rd = $rs + $rt
Add contents of two registers
$rd = $rs - $rt
Subtract contents of two registers
$rt = $rs + imm
Add signed constant
$rd = $rs + $rt
Unsigned, no overflow
$rd = $rs - $rt
Unsigned, no overflow
$rt = $rs + imm
Unsigned, no overflow
$rt = $rd
rd = coprocessor register (e.g. epc, cause, status)
Hi, Lo = $rs * $rt
64 bit signed product in Hi and Lo
Hi, Lo = $rs * $rt
64 bit unsigned product in Hi and Lo
Lo = $rs / $rt, Hi = $rs mod $rt
Lo = $rs / $rt, Hi = $rs mod $rt (unsigned)
$rd = Hi
$rd = Lo
$rd = $rs & $rt
$rd = $rs | $rt
Get value of Hi
Get value of Lo
Logical AND
Logical OR
$rt = $rs & imm
Logical AND, unsigned constant
Advertisement
$rt = $rs | imm
Logical OR, unsigned constant
$rd = $rs << shamt
Shift left logical (shift in zeros)
$rd = $rs >> shamt
Shift right logical (shift in zeros)
$rt = Mem[$rs + imm]
Load word from memory
Mem[$rs + imm] = $rt
Store word in memory
$rt = Mem[$rs + imm]
Load a single byte, set bits 8-31 of $rt to zero
Mem[$rs + imm] = $rt
$rt = imm * 216
Store byte (bits 0-7 of $rt) in memory
Load constant in bits 16-31 of register $rt
if($rs==$rt) PC = PC + imm (PC always points to next instruction)
if($rs!=$rt) PC = PC + imm (PC always points to next instruction)
if($rs<$rt) $rd = 1; else $rd = 0
if($rs<imm) $rt = 1; else $rt = 0
if($rs<$rt) $rd = 1; else $rd = 0 (unsigned numbers)
if($rs<$rt) $rd = 1; else $rd = 0 (unsigned numbers)
PC = address*4
Jump to destination, address = destination/4
$ra = PC; PC = address*4 (Jump and link, address = destination/4)
PC = $rs
Jump to address stored in register $rs
beqz $rs, label
Pseudo
If($rs==0) then goto label See also: bnez, bgez, bgtz, blez, bltz
bge $rs, $rt, label Pseudo
If($rs≥$rt) then goto label See also: bgt, ble, blt (add u for unsigned,eg bgeu)
la $rd, label
Pseudo
$rd = label
Assign the address of the label to register $rd
MIPS Instruction formats
Format
Bits 31-26
Bits 25-21
Bits 20-16
Bits 15-11
Bits 10-6
R
I
J
op
op
op
rs
rs
rt
rt
rd
address
shamt
imm
Bits 5-0
funct
register
-name
-nr
MIPS registers
usage
0
1
stores the value 0, do not write to it!
reserved for assembler
Advertisement
stores function results, more than 2 results (cid:198) stack
function arguments, if more than 4 arguments are needed (cid:198) stack
$zero
$at
$v0 - $v1 2 - 3
$a0 - $a3 4 - 7
$t0 - $t9 8 -15, 24-25 temporary variables
long-living variables
$s0 - $s7 16 - 23
reserved for kernel
$k0 - $k1 26 - 27
points to middle of a 64K block in the data segm.
$gp
stack pointer (top of stack)
$sp
frame pointer (beginning of current frame)
$fp
return address
$ra
28
29
30
31
writable caller- callee-
saved
yes
no
yes
yes
yes
yes
no
no
yes
yes
yes
-
-
x
x
x
-
-
-
-
-
-
-
-
-
-
-
x
-
-
-
x
x
internal, not directly accessible, registers
Hi, Lo
PC
status
cause
epc
stores the result of mult/div operations (use mflo, mfhi to access these registers)
contains the address of the next instruction to be fetched
register 12 in coprocessor 0, stores interrupt mask and enable bits (use mfc0)
register 13 in coprocessor 0, stores exception type and pending interrupt bits (use mfc0)
Advertisement
register 14 in coprocessor 0, stores address of instruction causing exception (use mfc0)
function
code in $v0
arguments
result
operating system functions
print_int
print_float
print_double
print_string
read_int
read_float
read_double
read_string
sbrk
exit
1
2
3
4
5
6
7
8
9
10
$a0
$f12
$f12/13
$a0 contains the start address of the string
$v0
$f0
$f0/1
$a0 contains the destination address of the
string, $a1 ist maximum length
$a0 contains needed size
string starting at $a0
$v0 contains start address of the memory area
MIPS Assembler Syntax
items:
.data
.word 1, 2
servus:
hello:
.ascii “servus!!”
.asciiz “hello”
.text
.globl main
main:
la $t0, servus
addi $t0, $zero, ‘a’