5.1.2 · D4 · HinglishInstruction Set Architecture (ISA)

ExercisesInstruction formats and encoding

2,766 words13 min read↑ Read in English

5.1.2 · D4 · Hardware › Instruction Set Architecture (ISA) › Instruction formats and encoding

Shuru karne se pehle, ek picture poore page par kaam aane wali vocabulary fix kar deti hai.

Figure — Instruction formats and encoding

Hum parent se field-width law bhi reuse karte hain:


Level 1 — Recognition

Recall Solution L1.1

Lowest 7 bits mein opcode hota hai. Wahan kyun: decoder ko yeh jaanna zaroori hai ki kis tarah ka instruction hai pehle — tab hi use pata chalega ki baaki fields kahan hain. Opcode ko ek fixed spot (har format mein bottom 7 bits) par pin karke, fetch/decode hardware use bina kuch aur parse kiye padh sakta hai. Ise box ke bahar lage label ki tarah samjho.

Recall Solution L1.2

Teen register fields: rd, rs1, rs2. Har ek 5 bits wide hota hai. 5 kyun: RISC-V mein registers hain (Registers and the Register File), aur . Paanch bits exactly patterns dete hain — perfect fit, koi waste nahi.

Recall Solution L1.3

Fixed-length. Har instruction same width ka hai (32 bits = 4 bytes), isliye opcode kabhi move nahi karta. Isi wajah se decoding sasta aur pipelineable hota hai. Variable-length (jaise x86) CPU ko pehle yeh discover karne par majboor kar deta ki har instruction kahan khatam hoti hai.


Level 2 — Application

Recall Solution L2.1

Neighbours check karo: (bahut kam), (fit hota hai). Toh bits, future instructions ke liye spare patterns baar rakhte hain. Ceil kyun: ; tum bit ka fraction nahi kharid sakte, isliye upar round karo.

Recall Solution L2.2

Registers ko 5-bit binary mein convert karo (har field 5 wide hai): Order funct7 rs2 rs1 funct3 rd opcode mein assemble karo: Ek 32-bit string ke roop mein grouped: 01000000101001001000010000110011. Yeh order kyun: decode opcode ko lowest bits se padhta hai aur baayi taraf kaam karta hai, isliye fields exactly wahin hone chahiye jahan decode unhe expect karta hai.

Recall Solution L2.3

Ek -bit two's-complement number (Two's Complement and Sign Extension) span karta hai ke saath: se tak. Ek extra negative kyun: pattern 1000...0 sabse negative value hai aur uska koi positive twin nahi hai, isliye negative side ek aage tak jaati hai.


Level 3 — Analysis

Recall Solution L3.1

Woh upper 12 bits 12-bit immediate ban jaate hain. I-type layout: Exactly 12 kyun: freed funct7(7) + rs2(5) = 12 contiguous bits upar baith jaate hain, isliye immediate ek clean block hai — 32 bits tak sign-extend karna aasaan hai. Dhyan do ki rs1, funct3, rd, opcode R-type ke same positions par rehte hain — yeh shared layout hi wajah hai ki register-read wiring har format mein identical hai.

Recall Solution L3.2

Ek store do registers padhta hai (rs1 = base address, rs2 = store kiya jaane wala data), isliye dono register fields apni usual spots par rehne chahiye. Isse sirf holes bachte hain — puraana rd slot (5 bits) aur puraana funct7 slot (7 bits) — immediate ke liye. Split kyun, shrink kyun nahi: hum rs2 nahi hata sakte, isliye 12-bit immediate ko ek 7-bit aur ek 5-bit piece mein kaatke do leftover holes mein thuns diya jaata hai. Hardware benefit: ==rs1 aur rs2 kabhi move nahi karte==, isliye register-read logic R, I, S, aur B formats ke liye ek fixed circuit hai. Sirf immediate-reassembly wiring alag hoti hai.

Recall Solution L3.3

16-bit opcode ke saath, baaki sab ke liye sirf bits bachte hain. Ek single R-type ko sirf teen registers ke liye bits chahiye, plus function selectors — yeh fit nahi hoga, aur ek I-type ke paas useful immediate ke liye almost koi jagah nahi hogi. Asli lesson: 32-bit word ek fixed budget hai. Opcode ko di gayi har bit operands se churi hoti hai. Real ISAs opcode chhota rakhte hain (7 bits) aur operations ko funct3/funct7 ke saath sideways expand karte hain. Yeh ek core RISC vs CISC design pressure hai.


Level 4 — Synthesis

Recall Solution L4.1
  • Opcode: . Kyunki , opcode = 9 bits.
  • Har register: bits. Teen ke liye = 18 bits.
  • Total used: bits.
  • Word hai 32 → spare = bits (funct field, flag, ya future growth ke liye usable). Kyun fit hota hai: humne bits sirf wahin kharch ki jahan requirements ne demand kiya aur running sum ko 32-bit budget ke against check kiya — exactly parent ka "total width = sum of field widths" rule.
Recall Solution L4.2

Fixed costs: opcode + rd + rs1 bits. Immediate ko baaki milta hai: bits. Two's-complement value (Two's Complement and Sign Extension) ke roop mein, 11 bits span karte hain Signed kyun: "add " jaisi instructions kaam karni chahiye, isliye top bit sign bit hai aur decode ALU ke touch karne se pehle 11 bits ko 32 tak sign-extend karta hai.


Level 5 — Mastery

Recall Solution L5.1

R-type map funct7 rs2 rs1 funct3 rd opcode use karke word slice karo:

  • funct7 = 0000000, funct3 = 000 → operation hai add.
  • rs2 = 00111 = 7 → source 2 hai x7.
  • rs1 = 00101 = 5 → source 1 hai x5.
  • rd = 00001 = 1 → destination hai x1. Assembly: add x1, x5, x7 (RISC-V rd, rs1, rs2 likhta hai). Text mein yeh order kyun: assembler destination pehle print karta hai, phir sources — bit layout ka ulta visual order, jo ek classic trip-up hai.
Recall Solution L5.2

12-bit field ka top bit 1 hai, isliye yeh ek negative two's-complement number hai. Ise padhne ke liye, invert karo aur ek add karo: Toh value hai . Decode ise sign-extend karta hai (top 1 ko baayi taraf copy karta hai) 32-bit tak taaki ALU ek true add kare, koi bada positive number nahi. Instruction hai addi rd, rs1, -8.

Recall Solution L5.3

Do pieces ko sahi order mein concatenate karo — high bits pehle, low bits baad mein: Binary mein padho: . Set bits positions 6 aur 2 par hain (0 se count karke), toh value . Top bit 0 hai → positive, sign-extension value nahi badlata. Reassemble kyun: store ka effective address hai rs1 + 68; split purely ek storage trick hai rs1/rs2 ko fixed rakhne ke liye — L3.2 mein S-type map dekho.


Recall Poore ladder ka ek-line summary

Opcode fixed hota hai aur pehle padha jaata hai; baaki fields se size hote hain aur word width tak sum hone chahiye; immediates signed, sign-extended, scaled, ya split ho sakti hain — raw bits hamesha value nahi hoti.

Active recall

Decode kaunsa field pehle padhta hai, aur kyun
Opcode — yeh ek fixed position par pinned hota hai taaki decode kisi doosre field ko locate karne se pehle format pick kar sake.
cheezein index karne ke liye Bits
, kyunki bits patterns dete hain aur tumhe chahiye.
S-type apna immediate split kyun karta hai
Taaki rs1 aur rs2 apne fixed positions par rahein; immediate do leftover holes mein jaati hai.
12-bit signed immediate ki range
se .