4.1.7 · D3Computer Architecture (Deep)

Worked examples — Instruction formats — R-type, I-type, J-type (MIPS - RISC-V)

4,019 words18 min readBack to topic

This page is a workout. The parent note told you the rules. Here we hit those rules with every awkward case a machine can throw at you: negative offsets, backward branches, zero-register tricks, jumps that reach and jumps that cannot, and an exam-style "decode this hex" twist. If you can survive every cell of the matrix below, no instruction encoding can surprise you.


The scenario matrix

Each row is a class of situation this topic can produce. The last column names the worked example(s) that hit that cell. Every cell is covered.

# Case class What makes it tricky Covered by
A R-type, positive registers field reorder (rd sits in the middle) Ex 1
B R-type, shift with shamt rs unused ⇒ must be 0 Ex 2
C I-type, positive immediate offset sits in low 16 bits Ex 3
D I-type, negative immediate two's-complement sign extension Ex 4
E Branch, forward (positive offset) (PC+4)+imm×4 Ex 5
F Branch, backward (negative offset) signed word offset, loop back Ex 6
G J-type, target reachable (same region) top 4 bits of PC+4 == top 4 bits of target Ex 7
H J-type, target unreachable (degenerate) top 4 bits of PC+4 ≠ top 4 bits of target ⇒ j cannot reach Ex 8
I Zero / degenerate register ($zero) register number 0 as a real encoding Ex 9
J Exam twist: decode raw hex reverse direction, identify format Ex 10

Prerequisite vault topics we lean on: MIPS Register File and Conventions, Sign Extension and Immediates, PC-relative vs Absolute Addressing, Control Unit and Decoding.


Reference: the bit layouts we will fill in

Figure — Instruction formats — R-type, I-type, J-type (MIPS - RISC-V)

Figure s01 (above): the three MIPS layouts stacked. Top row R = opcode(6), rs(5), rt(5), rd(5), shamt(5), funct(6). Middle row I = opcode(6), rs(5), rt(5), immediate(16). Bottom row J = opcode(6), address(26). Bit 31 is on the left, bit 0 on the right; the leftmost box (opcode) is the "master key" the decoder reads first.

Keep this picture open. Every example just fills the coloured boxes with numbers, then converts to binary/hex. The register numbers we use (from MIPS Register File and Conventions):

Recall Register numbers used on this page

$zero=0, $t0=8, $t1=9, $t2=10, $s0=16, $s1=17, $s2=18, $s3=19.


Worked examples

Cell A — R-type, positive registers


Cell B — R-type shift, rs unused


Cell C — I-type, positive immediate


Cell D — I-type, NEGATIVE immediate (sign extension)


Cell E — Branch FORWARD (positive offset)


Cell F — Branch BACKWARD (negative offset, a loop)


Cell G — J-type, target REACHABLE (same region)


Cell H — J-type, target UNREACHABLE (degenerate)


Cell I — Zero / degenerate register


Cell J — Exam twist: decode raw hex


Recall One-line summary of each cell

Positive R-type reorders to rs,rt,rd ::: Ex 1: 0x02324022 Shift uses shamt, rs=0 ::: Ex 2: 0x00114100 Positive immediate pads with zeros ::: Ex 3: 0x22680064 Negative immediate uses two's complement + sign extension ::: Ex 4: 0x2268FFF8 Forward branch stores positive word count ::: Ex 5: 0x11090005, target PC+24 Backward branch stores negative word count ::: Ex 6: 0x1500FFFC, imm 0xFFFC = −4 Reachable jump (same region) stores bits ::: Ex 7: 0x08100009 Unreachable jump (top 4 bits differ) → use jr ::: Ex 8 $zero encodes as an honest 00000 ::: Ex 9: 0x02204020 Decode by reading opcode first ::: Ex 10: lw $t2, 4($t0)