4.1.8 · Coding › Computer Architecture (Deep)
ADD R1, X jaisi instruction tab tak bekaar hai jab tak CPU ko pata na ho ki operand X kahan milega . Ek addressing mode woh rule hai jo CPU use karta hai actual location (yaani "effective address") compute karne ke liye — instruction ke bits se. Alag-alag modes flexibility , speed , aur instruction size ke beech tradeoff karte hain.
CPU ke paas bahut kam hardware locations (registers) hote hain, lekin programs lakho memory cells, arrays, structs, aur constants ko touch karte hain. Har memory cell ko apna opcode dena possible nahi hai. Isliye hum instruction mein ek chhota-sa "recipe" encode karte hain, aur hardware ko baar-baar compute karne dete hain. Isse milta hai:
Constants bina kisi memory access ke (immediate).
Fast scratch values (register).
Pointers / linked structures (indirect).
Arrays / loops bina instruction rewrite kiye (indexed).
Har mode jo central cheez produce karta hai woh hai Effective Address (EA) : woh final memory address jahan se operand fetch hota hai (ya write hota hai).
Operand = Memory [ EA ] (except when no memory access is needed)
Definition Immediate mode
Operand data khud hi hai , instruction ke andar rakha hua. Koi memory ya register lookup nahi.
Operand = Value field of instruction
Use hota hai: constants ke liye (MOV R1, #5).
Instruction ek register ka naam leta hai; operand usi register mein hota hai.
Operand = R [ i ] ( EA is a register number, not a memory address )
Use hota hai: fast temporaries ke liye. Koi memory access nahi ⇒ immediate ke baad sabse fast.
Definition Direct (absolute) mode
Instruction mein actual memory address hota hai.
EA = A Operand = M [ A ]
Use hota hai: global variables ke liye fixed addresses par.
Instruction mein ek aisa address hota hai jo batata hai ki real address kahan hai — yaani ek pointer.
EA = M [ A ] Operand = M [ M [ A ] ]
(Register-indirect: EA = R [ i ] , operand = M [ R [ i ]] .) Use hota hai: pointers , linked lists ke liye.
EA = ek base address A plus ek index register R [ i ] ki contents.
EA = A + R [ i ] Operand = M [ A + R [ i ]]
Use hota hai: arrays ke liye. Loop mein R [ i ] increment karo aur elements walk karo.
Maano: M[100]=200, M[200]=350, M[204]=999, register R2=4, A=100.
Worked example Immediate:
MOV R1, #100
Decode karta hai mode = immediate. Kyun? Koi memory ki zaroorat nahi; bits 100 khud hi data hain.
R1 ← 100. Ho gaya. 0 memory accesses.
LOAD R1, [100]
EA = 100 directly. Kyun? Direct = address literal hai.
R1 ← M[100] = 200. 1 access.
LOAD R1, [[100]]
Pehla read: M[100] = 200. Kyun? 100 data ka nahi balki address ka pointer hai.
EA = 200. Doosra read: M[200] = 350.
R1 ← 350. 2 accesses — isliye indirect slow hota hai.
LOAD R1, [100 + R2] with R2=4
EA = 100 + 4 = 104. R2 kyun add karo? Ye 100 par based array ka element R2/elemSize select karta hai.
R1 ← M[104] = 999. 1 access.
Next element ke liye, R2 ← R2 + 4 karo aur same instruction reuse karo. Ye kyun matter karta hai: arrays par loops ko har element ke liye naya code nahi chahiye.
ADD R1, R2
Dono operands register contents hain. Kyun fastest? Registers CPU ke andar hote hain; koi bus trip nahi.
R1 ← R1 + R2. Operands ke liye 0 memory accesses.
Worked example EA predict karo
Diya hai A = 50, M[50] = 80, M[80] = 12, R3 = 7.
Pehle har ek ke liye operand value forecast karo, phir check karo:
Immediate #50 → operand = 50 ✅ (0 accesses)
Direct [50] → operand = M[50] = 80 ✅
Indirect [[50]] → operand = M[M[50]] = M[80] = 12 ✅
Indexed [50 + R3] → operand = M[57] (jo bhi wahan stored hai) ✅
Agar tumhara forecast indirect par chuka, toh tumne ek $M[\cdot]$ kam count kiya.
Common mistake "Indirect aur indexed basically same hain."
Kyun sahi lagta hai: dono mein "ek address plus kuch lookup" jaisa lagta hai, aur dono direct se slow hain.
Fix: Indirect = M [ A ] (pointer fetch karne ke liye ek extra memory read ). Indexed = A + R [ i ] (ek addition , koi extra memory read nahi). Memory access counts alag hain: indirect = 2, indexed = 1. Indexed arrays walk karta hai; indirect pointers follow karta hai.
Common mistake "Immediate operand ko memory mein store karta hai aur wahan se read karta hai."
Kyun sahi lagta hai: baaki sabhi modes eventually memory touch karte hain.
Fix: Immediate ka operand instruction word ke andar hi hota hai jo instruction fetch ke dauran already fetch ho chuka hota hai. 0 additional operand accesses. Yahi iska poora fayda hai.
Common mistake "Direct mode sabse flexible hai kyunki ye exact address name karta hai."
Kyun sahi lagta hai: "exact address" powerful lagta hai.
Fix: Ek fixed address name karna sabse kam flexible hai — instruction sirf usi ek cell ko touch kar sakta hai. Indexed/indirect runtime par targets change karte hain, jo loops aur pointers ko chahiye.
Common mistake "Indexed EA = A plus A par jo value hai."
Kyun sahi lagta hai: indirect ke saath confuse ho jaate hain.
Fix: Indexed mein index register ki content R [ i ] add hoti hai, koi memory value nahi : EA = A + R [ i ] .
Recall Feynman: 12-saal ke bacche ko explain karo
Socho ek treasure hunt hai. Ek addressing mode har card par clue ka type hota hai.
Immediate: card likhta hai "tera khazana number 5 hai" — prize card par hi likha hai.
Register: "apni pocket #2 mein dekh" — jaldi, woh tumhare paas hi hai.
Direct: "ghar number 100 par jao" — address seedha likha hai.
Indirect: "ghar 100 par jao, andar note padho, woh asli ghar batayega" — do trips.
Indexed: "ghar 100 se shuru karo, phir utne gharon aage chalo jitna tumhara counter kahe" — poori street ek-ek karke check karne ke liye perfect (ek array!).
Mnemonic Modes aur unka memory-access cost yaad rakhne ke liye
"I Realize Doctors Inspect X-rays" → I mmediate, R egister, D irect, I ndirect, inde X — yeh sirf naming order hai, koi speed ranking NAHI.
Memory-access count (operand paane ke liye kitne memory reads) ke liye digits 0 0 1 2 1 yaad rakho (Immediate, Register, Direct, Indirect, Indexed ke liye). Slow wala hai Indirect (2 reads) kyunki woh pointer chase karta hai; baaki sabko zyada se zyada ek read chahiye.
Effective address (EA) kya hota hai? Ek operand ka final memory address, jo CPU instruction bits se addressing mode ke according compute karta hai.
Immediate mode — operand kahan hota hai aur kitne memory accesses lagte hain? Instruction word ke andar hi; 0 operand memory accesses.
Register mode mein operand kahan hota hai? CPU register R [ i ] mein; 0 memory accesses (memory bus ke liye fastest).
Direct mode EA formula? EA = A (instruction mein literal address); operand = M [ A ] , 1 access.
Indirect mode EA aur operand? EA = M [ A ] , operand = M [ M [ A ]] ; 2 memory accesses; pointers ke liye use hota hai.
Indexed mode EA formula? EA = A + R [ i ] (base + index register); operand = M [ A + R [ i ]] , 1 access.
Arrays ke liye best mode kaun sa hai aur kyun? Indexed — loop mein index register increment karo aur har element ke liye same instruction reuse karo.
Pointers / linked lists ke liye best mode kaun sa hai? Indirect — instruction us address ka address hold karta hai.
Indirect aur indexed mein key difference? Indirect ek extra MEMORY read karta hai (M [ A ] , 2 accesses); indexed ADDITION karta hai (A + R [ i ] , 1 access).
Har mode ke liye operand fetch karne ke liye memory access count (Imm, Reg, Direct, Indirect, Indexed)? 0, 0, 1, 2, 1.
Immediate mode kyun use karte hain? Constants supply karne ke liye bina kisi extra memory access ke (operand instruction ke saath hi pre-fetched hota hai).
operand in instruction, 0 access
Memory access to fetch operand