mips instruction encoding
Summary
R-type
- register addressing
mips
arith rd, rs, rt
shift rd, rt, shamt
note
rs,rtandrdare all only 5 bits since there are only 32 registers
I-type
- immediate addressing
- base addressing(load/store)
- PC-relative addressing(branching)
mips
op rt, rs, imm
load/store rt, imm(rs)
branch rs, rt, label
Branching
- branch not taken
PC = PC + 4, increment to next word/instruction
- branch taken
PC = (PC + 4) + (imm * 4), skip overimminstructions
- 16 bits can specify 18 bits, allows branching to
words or bytes
J-type
- pseudo-direct addressing
Jumping
- 26 bits can specify 28 bits, allows branching to
words or bytes- ≈256MB
- 4 most significant bits are taken from
PC + 4 - doesn’t cover all of the 32-bit memory addresses
Concept
R-type
| Field | Purpose |
|---|---|
| opcode | specifies the instruction, 0 for all R-type instructions |
| rs | source register, 1st operand |
| rt | target register, 2nd operand |
| rd | destination register |
| shamt | amount that a shift instruction will shift by |
| funct | specifies the exact instruction alongside opcode, usually the variant |
I-type
| Field | Purpose |
|---|---|
| opcode | specifies the instruction, 0 for all R-type instructions |
| rs | source register, 1st operand |
| rt | target register, 2nd operand |
| imm | immediate value, treated as a 16-bit signed int or unsigned for bitwise operations |
Program counter
- special register that stores the address of the next instruction to be executed
- labels specify a target address, we can take the relative distance between the target and the PC to signify a “jump”
- next we can assume instructions are word aligned, so we can ignore the last two binary places
Encoding registers
- use the register numbers
Application
Add instruction(R-type)
mips
add $t0, $s0, $s1
Shift instruction(R-type)
mips
sll $s4, $s4, 4
Add-immediate instruction(I-type)
mips
addi $t8, $s7, -50
immis sign extended, since its interpreted as a signed int
Load word instruction(I-type)
mips
lw $t2 12($s3)
Branch instruction(I-type)
mips
beq $s0, $s1, Equal
... # <- current value of PC
...
...
Equal: # <- target address = PC + 3
Jump instruction
mips
Loop: ... # addr: 8
...
...
j Loop # addr: 20