31 26 21 16 11 6 0 ______ _____ _____ _____ _____ ______ |______|_____|_____|_____|_____|______| OP RS RT RD SHAMT FUNC
op | rs | rt | rd | shamt | funct |
---|---|---|---|---|---|
6 bits | 5 bits | 5 bits | 5 bits | 5 bits | 6 bits |
Format | 6 bits | 5 bits | 5 bits | 5 bits | 5 bits | 6 bits | Comments |
---|---|---|---|---|---|---|---|
R | op | rs | rt | rd | shamt | funct | Arithmetic |
I | op | rs | rt | address/immediate | Transfer, branch,immediate | ||
J | op | target address | Jump |
Category | Instruction | Example | Meaning | Comments |
---|---|---|---|---|
Arithmetic | add | add a,b,c | a=b+c | Always 3 operands |
Arithmetic | subtract | sub a,b,c | a=b-c | Always 3 operands |
Note that each operand has exactly three operands.
Note the summary is not complete. Click here to see the full list
Instruction | Example | Meaning | Comments |
---|---|---|---|
add | add $1,$2,$3 | $1=$2+$3 | Always 3 operands |
subtract | sub $1,$2,$3 | $1=$2-$3 | Always 3 operands |
add immediate | addi $1,$2,10 | $1=$2+10 | add constant |
add unsigned | addu $1,$2,$3 | $1=$2+$3 | Always 3 operations |
subtract unsigned | subu $1,$2,$3 | $1=$2-$3 | Always 3 operations |
add immed.unsigned | addiu $1,$2,10 | $1=$2+10 | Always 3 operations |
Instruction | Example | Meaning | Comments |
---|---|---|---|
and | and $1,$2,$3 | $1=$2&$3 | 3 register operands |
or | or $1,$2,$3 | $1=$2|$3 | 3 register operands |
and immediate | andi $1,$2,10 | $1=$2&10 | AND constant |
or immediate | or $1,$2,10 | $1=$2|10 | OR constant |
shift left logical | sll $1,$2,10 | $1=$2<<10 | Shift left by constant |
shift right logical | srl $1,$2,10 | $1=$2>>10 | Shift right by constant |
Instruction | Example | Meaning | Comments |
---|---|---|---|
load word | lw $1,10($2) | $1=Memory[$2+10] | memory to register |
store word | sw $1,10($2) | Memory[$2+10]=$1 | register to memory |
load upper immed. | lui $1,10 | $1=10x2^16 | load constant into upper 16 bits |
Instruction | Example | Meaning | Comments |
---|---|---|---|
branch on equal | beq $1,$2,10 | if($1==$2)go to PC+4+10 | Equal test |
branch on not equal | bne $1,$2,10 | if($1!=$2)go to PC+4+10 | Not equal test |
set on less then | slt $1,$2,$3 | if($2<$3)$1=1;else $1=0 | Less than compare |
Instruction | Example | Meaning | Comments |
---|---|---|---|
jump | j 1000 | go to 1000 | Jump to target address |
jump register | jr $31 | go to $31 | For switch, procedure return |
jump and link | jal 1000 | $31=PC+4;go to 1000 | For procedure call |
# hello.asm # Simple routine to demo MIPS output. # Author: R.N. Ciminero # See Patterson & Hennessy pg. A-46 for system services. .text .globl main main: li $v0,4 # code for print_str la $a0, msg # point to string syscall li $v0,10 # code for exit syscall .data msg: .asciiz "Hello World!\n"
Hello world!
# simpleio.asm # Simple routine to demo SPIM input/output. # Author: R.N. Ciminero # Revision date: 10-05-93 Original def. # See Patterson & Hennessy pg. A-46 for system services. .text .globl main main: li $v0,4 # output msg1 la $a0, msg1 syscall li $v0,5 # input A and save syscall move $t0,$v0 li $v0,4 # output msg2 la $a0, msg2 syscall li $v0,5 # input B and save syscall move $t1,$v0 add $t0, $t0, $t1 # A = A + B li $v0, 4 # output msg3 la $a0, msg3 syscall li $v0,1 # output sum move $a0, $t0 syscall li $v0,4 # output lf la $a0, cflf syscall li $v0,10 # exit syscall .data msg1: .asciiz "\nEnter A: " msg2: .asciiz "\nEnter B: " msg3: .asciiz "\nA + B = " cflf: .asciiz "\n"
Enter A: 10 Enter B: 5 A + B = 15
# sumit.asm # Simple routine to sum N integers to demo a loop. # Author: R.N. Ciminero # Revision date: 10-06-93 Original def. # See Patterson & Hennessy pg. A-46 for system services. .text .globl main main: li $v0,4 # output msg1 la $a0, msg1 syscall li $v0,5 # input N and save syscall move $t0,$v0 li $t1, 0 # initialize counter (i) li $t2, 0 # initialize sum loop: addi $t1, $t1, 1 # i = i + 1 add $t2, $t2, $t1 # sum = sum + i beq $t0, $t1, exit # if i = N, continue j loop exit: li $v0, 4 # output msg2 la $a0, msg2 syscall li $v0,1 # output sum move $a0, $t2 syscall li $v0,4 # output lf la $a0, lf syscall li $v0,10 # exit syscall .data msg1: .asciiz "\nNumber of integers (N)? " msg2: .asciiz "\nSum = " lf: .asciiz "\n"
Number of integers (N)? 5 Sum = 15