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 32 registers hain (Registers and the Register File), aur b=⌈log232⌉=5. Paanch bits exactly 25=32 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.
b=⌈log2200⌉.
Neighbours check karo: 27=128<200 (bahut kam), 28=256≥200 (fit hota hai). Toh b=8 bits, future instructions ke liye 256−200=56 spare patterns baar rakhte hain.
Ceil kyun:log2200≈7.64; 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):
rd=8=01000,rs1=9=01001,rs2=10=01010.
Order funct7 rs2 rs1 funct3 rd opcode mein assemble karo:
funct70100000rs201010rs101001funct3000rd01000opcode0110011
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 b-bit two's-complement number (Two's Complement and Sign Extension) span karta hai
−2b−1se2b−1−1.b=12 ke saath: −211=−2048 se 211−1=+2047 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.
Woh upper 12 bits 12-bit immediate ban jaate hain. I-type layout:
12imm[11:0]5rs13funct35rd7opcodeExactly 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.
7imm[11:5]5rs25rs13funct35imm[4:0]7opcodeSplit 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 32−16=16 bits bachte hain. Ek single R-type ko sirf teen registers ke liye 5+5+5=15 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.
Har register: ⌈log264⌉=6 bits. Teen ke liye = 18 bits.
Total used: 9+18=27 bits.
Word hai 32 → spare = 32−27=5 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 9 + rd6 + rs16=21 bits.
Immediate ko baaki milta hai: 32−21=11 bits.
Two's-complement value (Two's Complement and Sign Extension) ke roop mein, 11 bits span karte hain
−210se210−1=−1024se+1023.Signed kyun: "add −8" 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.
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:
111111111000invert000000000111=7+18.
Toh value hai −8. Decode ise sign-extend karta hai (top 1 ko baayi taraf copy karta hai) 32-bit −8 tak taaki ALU ek true −8 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:
imm[11:5]∥imm[4:0]=0000010∥00100=000001000100.
Binary mein padho: 0000010001002. Set bits positions 6 aur 2 par hain (0 se count karke), toh value =26+22=64+4=68.
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 ⌈log2N⌉ 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.