instruction encoding


Summary

R-type

  • register addressing
mips
arith rd, rs, rt

shift rd, rt, shamt

note rs, rt and rd are 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 over imm instructions
  • 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

FieldPurpose
opcodespecifies the instruction, 0 for all R-type instructions
rssource register, 1st operand
rttarget register, 2nd operand
rddestination register
shamtamount that a shift instruction will shift by
functspecifies the exact instruction alongside opcode, usually the variant

I-type

FieldPurpose
opcodespecifies the instruction, 0 for all R-type instructions
rssource register, 1st operand
rttarget register, 2nd operand
immimmediate 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

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

imm is 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