5.1.2 · HinglishInstruction Set Architecture (ISA)

Instruction formats and encoding

1,985 words9 min readRead in English

5.1.2 · Hardware › Instruction Set Architecture (ISA)


Instruction format KYA hota hai?

Har instruction = [opcode][operand fields...]. CPU ka decode stage word ko fixed bit boundaries par slice karta hai aur har slice ko sahi jagah route karta hai (register file, ALU, sign-extender, etc.).


Humein multiple formats KI ZAROORAT KYUN hai?

Yeh hai 80/20: samjho kyun fields idhar-udhar jaate hain jabki opcode wahi rehta hai, aur har real ISA format padhna aasaan ho jaata hai.


Encoding KAISE kaam karta hai — scratch se ek field layout derive karo

Chaliye RISC-V R-type (register-register) format ko requirements se derive karte hain, ratta lagaane ki jagah.

Requirement 1 — opcode. RISC-V ek 7-bit primary opcode use karta hai. 7 kyun? Kyunki major operation classes kaafi hain, aur 7 se 32 bits mein ek clean remainder milta hai.

Requirement 2 — teen registers. RISC-V mein 32 registers hain, toh har register number ke liye bits chahiye. Teenon milakar bits.

Requirement 3 — similar ops mein distinguish karna. add aur sub ek hi opcode class share karte hain, toh unhe alag karne ke liye extra bits chahiye: ek 3-bit funct3 aur ek 7-bit funct7.

Tally: bits. Yeh exactly fit hota hai — yahi toh point hai.

Figure — Instruction formats and encoding

Fixed-length vs. variable-length encoding


RISC-V ke main format families

Format Kis liye use hota hai Special feature
R reg-reg (add, sub) 3 registers, funct7+funct3
I immediates aur loads (addi, lw) 12-bit immediate, rs2+funct7 ki jagah
S stores (sw) immediate do fields mein split
B branches (beq) immediate split + cheap sign-extend ke liye reordered
U upper immediate (lui) 20-bit immediate
J jumps (jal) 20-bit immediate

Worked examples


Common mistakes (steel-manned)


Recall Feynman: ek 12-saal ke bachche ko samjhao

Socho ek robot hai jo sirf punched holes waale cards samajhta hai. Har card same size ka hota hai. Pehle kuch holes robot ko bataate hain ki kya kaam karna hai (add, jump, load). Agle holes bataate hain ki kaunse boxes (registers) ya kaunsa number use karna hai. Kyunki "kya kaam" waale holes hamesha same jagah hote hain, robot bahut fast padh sakta hai. Kabhi-kabhi ek kaam ko bada number chahiye aur zyaada boxes nahi, toh hum holes thoda alag pattern mein punch karte hain — lekin "kya kaam" waale holes kabhi nahi hilte, toh robot kabhi confuse nahi hota.


Active recall

Instruction format kya hota hai?
Machine instruction word ke andar fields (opcode + operands) ka defined bit-layout.
Opcode formats ke across ek fixed bit position par kyun rehta hai?
Taaki decoder hamesha pehle use dhundh aur padh sake, fast/pipelined decoding enable ho.
N items index karne ke liye bits ka formula?
.
32 registers waale register field ke liye kitne bits?
5 bits, kyunki .
Fixed vs variable length: kaunsa faster decode hota hai aur kyun?
Fixed — har instruction same width ka hota hai aur opcode hamesha same jagah hota hai, toh length-finding step nahi hota.
Fixed vs variable length: kaunsa denser code deta hai?
Variable (jaise x86) — chhote ops kam bytes use karte hain.
S/B-type formats mein immediates split KYUN hote hain?
rs1/rs2 ko fixed positions par rakhne ke liye taaki register-read wiring har format ke liye identical ho; immediate bache hue holes fill karta hai.
12-bit signed (two's-complement) immediate ka range?
se .
32-bit ALU use karne se pehle 12-bit immediate ke saath kya hona chahiye?
32 bits tak sign-extension (aur branches/jumps ke liye scaling).
Fixed-width ISA ke liye field width sum rule?
Saare field widths ka sum exactly word size ke barabar hona chahiye (jaise 32 bits).
R-type mein funct3 aur funct7 kya karte hain?
Same primary opcode share karne wale operations mein sub-select karte hain (jaise add vs sub mein distinguish karna).

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