5.1.2Instruction Set Architecture (ISA)

Instruction formats and encoding

2,190 words10 min readdifficulty · medium5 backlinks

WHAT is an instruction format?

Every instruction = [opcode][operand fields...]. The CPU's decode stage slices the word at fixed bit boundaries and routes each slice to the right place (register file, ALU, sign-extender, etc.).


WHY do we need multiple formats?

This is the 80/20: master why fields move around while the opcode stays put, and every real ISA format becomes readable.


HOW encoding works — derive a field layout from scratch

Let's derive the RISC-V R-type (register-register) format from requirements, not memorize it.

Requirement 1 — opcode. RISC-V uses a 7-bit primary opcode. Why 7? Because 27=1282^7 = 128 major operation classes is plenty, and 7 leaves a clean remainder in 32 bits.

Requirement 2 — three registers. RISC-V has 32 registers, so each register number needs log232=5\lceil \log_2 32 \rceil = 5 bits. Three of them =15= 15 bits.

Requirement 3 — distinguish similar ops. add and sub share the same opcode class, so we need extra bits to tell them apart: a 3-bit funct3 and a 7-bit funct7.

Tally: 7+5+5+5+3+7=327 + 5 + 5 + 5 + 3 + 7 = 32 bits. It fits exactly — that's the point.

funct77  rs25  rs15  funct33  rd5  opcode7\underbrace{funct7}_{7}\;\underbrace{rs2}_{5}\;\underbrace{rs1}_{5}\;\underbrace{funct3}_{3}\;\underbrace{rd}_{5}\;\underbrace{opcode}_{7}

Figure — Instruction formats and encoding

Fixed-length vs. variable-length encoding


The main RISC-V format families

Format Used for Special feature
R reg-reg (add, sub) 3 registers, funct7+funct3
I immediates & loads (addi, lw) 12-bit immediate replaces rs2+funct7
S stores (sw) immediate split across two fields
B branches (beq) immediate split + reordered for cheap sign-extend
U upper immediate (lui) 20-bit immediate
J jumps (jal) 20-bit immediate

Worked examples


Common mistakes (steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine a robot that only understands cards with punched holes. Each card is the same size. The first few holes tell the robot what job to do (add, jump, load). The next holes tell it which boxes (registers) or what number to use. Because the "what job" holes are always in the same spot, the robot can read super fast. Sometimes a job needs a big number and not many boxes, so we punch the holes in a slightly different pattern — but the "what job" holes never move, so the robot never gets confused.


Active recall

What is an instruction format?
The defined bit-layout of fields (opcode + operands) within a machine instruction word.
Why does the opcode stay in a fixed bit position across formats?
So the decoder can always find and read it first, enabling fast/pipelined decoding.
Formula for bits needed to index N items?
b=log2Nb=\lceil\log_2 N\rceil.
How many bits for a register field with 32 registers?
5 bits, since log232=5\lceil\log_2 32\rceil = 5.
Fixed vs variable length: which decodes faster and why?
Fixed — every instruction is the same width and the opcode is always in the same place, so no length-finding step.
Fixed vs variable length: which gives denser code?
Variable (e.g. x86) — small ops use fewer bytes.
Why are immediates split in S/B-type formats?
To keep rs1/rs2 in fixed positions so register-read wiring is identical across formats; the immediate fills the leftover holes.
Range of a 12-bit signed (two's-complement) immediate?
2048-2048 to +2047+2047.
What must happen to a 12-bit immediate before the 32-bit ALU uses it?
Sign-extension to 32 bits (and scaling for branches/jumps).
Field width sum rule for a fixed-width ISA?
All field widths must sum exactly to the word size (e.g. 32 bits).
What do funct3 and funct7 do in R-type?
Sub-select among operations sharing the same primary opcode (e.g. distinguish add vs sub).

Connections

Concept Map

requires

defines layout of

most important

rest are

says

say

kept in

enables

slices word into

varied needs cause

reuse same

width sized by

sum fits

CPU reads only bits

Instruction format

Fields

Opcode

Operands

What to do

What to act on

Fixed position

Decode stage

Multiple formats

32-bit word

b = ceil log2 N

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, CPU sirf bits samajhta hai — 0 aur 1. To jab hum likhte hain add x5, x6, x7, us instruction ko ek fixed-width binary word mein pack karna padta hai. Is packing ke rules ko bolte hain instruction format. Sabse pehla aur sabse important field hota hai opcode — matlab "karna kya hai" (add karna hai, jump karna hai, load karna hai). Baaki fields batate hain "kispe karna hai" — registers ya koi number (immediate).

Ab sawaal: alag-alag instructions ki zaroorat alag hoti hai. Kisi ko 3 register chahiye, kisi ko 2 register + ek bada number. Isliye ek hi 32-bit word ko hum alag-alag tareeke se baant-te hain — yeh alag layouts hi R-type, I-type, S-type wagairah kehlate hain. Lekin trick yeh hai: opcode hamesha same jagah (last 7 bits) mein rehta hai, taaki decoder turant pehchan le ki kaun sa instruction hai aur fields kahaan hain. Yeh cheez decoding ko fast banati hai.

Field ka size nikalne ka simple formula: b=log2Nb = \lceil \log_2 N \rceil. 32 registers ke liye log232=5\log_2 32 = 5 bits chahiye. Ek chhota sawaal jaroor pehchano — immediate value jo instruction mein padi hai, wo ALU tak pahunchne se pehle sign-extend ya shift ho sakti hai, to raw bits aur effective value alag ho sakti hain. Yeh ek common galti hai jismein students fas jaate hain.

Last baat — fixed-length (RISC-V, ARM, MIPS = 32 bit) decode fast karta hai kyunki har instruction same size ka hai. Variable-length (x86, 1 se 15 bytes) code chhota banata hai, memory bachata hai, par decode slow hota hai. Yeh koi "better/worse" nahi, pura ek trade-off hai — yehi exam aur interview dono mein poocha jaata hai.

Go deeper — visual, from zero

Test yourself — Instruction Set Architecture (ISA)

Connections