4.1.7Computer Architecture (Deep)

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

2,022 words9 min readdifficulty · medium

WHAT are we slicing? The 32 bits

Every MIPS instruction = exactly 32 bits. The first 6 bits (the opcode) tell the decoder which format to expect. That's the master key.

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

HOW the bits are laid out (and WHY each width)

R-type

opcode6  rs5  rt5  rd5  shamt5  funct6\underbrace{\text{opcode}}_{6} \;\underbrace{\text{rs}}_{5}\;\underbrace{\text{rt}}_{5}\;\underbrace{\text{rd}}_{5}\;\underbrace{\text{shamt}}_{5}\;\underbrace{\text{funct}}_{6}

rs, rt are source registers; rd is the destination.

I-type

opcode6  rs5  rt5  immediate16\underbrace{\text{opcode}}_{6}\;\underbrace{\text{rs}}_{5}\;\underbrace{\text{rt}}_{5}\;\underbrace{\text{immediate}}_{16}

J-type

opcode6  address26\underbrace{\text{opcode}}_{6}\;\underbrace{\text{address}}_{26}


Worked examples


Common mistakes


Recall Feynman: explain to a 12-year-old

Imagine a 32-light switchboard. The first 6 switches spell a code word that tells you what kind of message this is. If it's a "do math with three lockers" message, the next switches name locker A, locker B, locker C and a tiny extra code for which math (add? subtract?). If it's a "do something with a small number" message, fewer switches name lockers and the rest spell out the number. If it's a "jump far away" message, almost all switches spell the place to jump. Same 32 switches, three different rulebooks — and the first 6 switches always say which rulebook to use.


Flashcards

How many bits is a MIPS register field and why?
5 bits, because 25=322^5=32 registers exist, needing log232=5\log_2 32 = 5 bits.
Total width of every MIPS instruction?
32 bits, fixed, for uniform decoding.
What field tells the decoder which format an instruction is?
The 6-bit opcode (opcode 0 ⇒ R-type, look at funct).
R-type bit-field order from MSB?
opcode(6), rs(5), rt(5), rd(5), shamt(6→5), funct(6).
Why do all R-type ops share opcode 0?
The 6-bit funct field distinguishes them (26=642^6=64 ops), saving opcode space.
In R-type, which register is the destination?
rd (the third register field), even though it's written first in assembly.
I-type bit-field order?
opcode(6), rs(5), rt(5), immediate(16).
In lw $t0,32($s3), what is rs, rt, imm?
rs=s3(base),rt=s3 (base), rt=t0 (destination), imm=32 (offset).
Branch target formula for beq?
target = (PC+4) + (signed_imm × 4).
Why multiply branch/jump offsets by 4?
Instructions are word-aligned (multiples of 4); storing word counts extends reach for free.
J-type fields and target reconstruction?
opcode(6)+address(26); target = PC[31:28] ‖ address ‖ 00.
Effective branch reach?
±128 KB (16 signed bits + 2 implied = 18-bit signed byte range).
Why can't j reach all of memory?
Top 4 bits come from current PC ⇒ confined to a 256 MB region; use jr for full 32-bit jumps.
What is shamt and how wide?
Shift amount, 5 bits (0–31 positions for a 32-bit word).

Connections

  • MIPS Register File and Conventions — why 32 registers ⇒ 5-bit fields
  • Single-Cycle Datapath — how fixed positions let rs/rt always feed the register file
  • Control Unit and Decoding — opcode/funct drive control signals
  • Sign Extension and Immediates — how 16-bit imm becomes 32-bit
  • PC-relative vs Absolute Addressing — branch vs jump target math
  • RISC-V Formats (R, I, S, B, U, J) — how RISC-V splits immediates for hardware simplicity

Concept Map

first 6 bits

selects

selects

selects

uses

opcode 0 needs

holds

reused by

PC-relative offset

holds

enforces

32-bit instruction

opcode 6 bits

R-type

I-type

J-type

funct field 6 bits

three registers rs rt rd

16-bit immediate

26-bit address

beq branch

RISC regularity

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, CPU sirf ek 32-bit number dekhta hai — usko khud se nahi pata ki yeh "add" hai ya "load" hai. Isliye hum 32 bits ko fixed tarike se kaat-te hain, jisko instruction format kehte hain. MIPS mein sirf 3 format hote hain: R-type (teen register, jaise add), I-type (do register + ek 16-bit constant, jaise addi, lw, beq), aur J-type (bada 26-bit address, jaise j). Sabse pehle 6 bits = opcode, yeh master key hai jo batati hai konsa rulebook lagega.

Register field 5 bits ka kyun? Kyunki MIPS mein 32 register hote hain aur log232=5\log_2 32 = 5. R-type mein opcode 0 hota hai aur asli operation funct field mein chhupa hota hai — isse 64 R-type ops bin extra opcode ke ban jaate hain. Ek important baat: assembly mein destination pehle likhte ho (add $rd,$rs,$rt) par binary mein order hota hai rs, rt, rd — yani rd beech mein! Yeh galti sabse common hai, dhyan rakhna.

Branch aur jump mein ek smart trick hai. beq ka offset bytes mein nahi, words mein store hota hai, aur hardware usko 4 se multiply karta hai (kyunki har instruction 4 byte ka hota hai, low 2 bits hamesha 0). Isse reach badh jaati hai muft mein. Jump ke liye target banta hai: PC ke top 4 bits + 26-bit address + neeche 00. Isi wajah se j sirf apne 256 MB region mein hi jump kar sakta hai — poori memory mein jaane ke liye jr (register wala jump) chahiye.

Yeh sab matter kyun karta hai? Kyunki yeh "regularity" hi RISC ka dil hai — fixed positions ki wajah se decoder simple rehta hai, same wires register file ko feed karte hain, aur PC har baar +4 hota hai. Simple hardware = fast aur reliable chip.

Go deeper — visual, from zero

Test yourself — Computer Architecture (Deep)

Connections