4.1.5 · HinglishComputer Architecture (Deep)

Registers — general purpose, special (PC, SP, LR, CPSR)

2,354 words11 min readRead in English

4.1.5 · Coding › Computer Architecture (Deep)


Registers exist HI KYUN karte hain?

WHAT hai ek register, precisely?

HOW many bits? bits wala register distinct values store kar sakta hai.

  • 32-bit register → values.
  • 64-bit register → values.

Dono families

1. General-Purpose Registers (GPRs)

WHY general? Flexibility — compiler decide karta hai ki har register kya hold kare, isliye woh aapki hottest variables ko registers mein rakh sakta hai (yahi register allocation hai).

2. Special-Purpose Registers

Inki hardware-defined meaning hoti hai. CPU khud inhe read/update karta hai.

Reg Name Kya hold karta hai Kyun special hai
PC Program Counter next instruction ka address jo fetch karna hai Hardware har fetch par auto-increment karta hai; branches ise overwrite karte hain
SP Stack Pointer stack ke top ka address Hardware (push/pop, call/ret) ise bump karta hai
LR Link Register function call ka return address BL ise likhta hai taaki callee jaane kahan wapas jaana hai
CPSR Current Program Status Register condition flags + mode bits ALU flags set karta hai; branches inhe read karte hain
Figure — Registers — general purpose, special (PC, SP, LR, CPSR)

PC — Program Counter (fetch loop derive karte hue)

First principles se increment derive karna. Maano instructions fixed width bytes ki hain (ARM: ). Address par instruction fetch karne ke baad, default next instruction yahan hogi: Hardware yeh current instruction execute hone se pehle karta hai, isliye jab tak instruction run hoti hai, PC agli wali ko point kar raha hota hai. Yahi wajah hai ki classic ARM par PC current + 8 read karta hai (pipeline: fetch do stages aage hoti hai) — ek famous gotcha.

Branch to target simply karta hai. Signed offset (instructions mein) wala relative branch compute karta hai:


SP — Stack Pointer (push/pop derive karte hue)

Zyaadatar CPUs full-descending stack use karte hain: stack lower addresses ki taraf grow karta hai, aur SP last pushed item (occupied top) ko point karta hai.

Value ka PUSH derive karna (full-descending, word = 4 bytes):

  1. Top ko niche le jaake jagah banao: . Why first? Taaki hum current top item ko overwrite na karein.
  2. Store karo: .

Register mein POP derive karna:

  1. Current top padho: .
  2. Slot free karo: . Why after? Data pehle read hona chahiye, tabhi hum slot free declare karein.

Function call BL target (Branch with Link) atomically do kaam karta hai: PC_next kyun save karte hain, PC nahi? Kyunki hum call ke baad wali instruction par resume karna chahte hain, call ko dobara run nahi karna (infinite loop!).

Return karne ke liye: BX LR karta hai .


CPSR — flags (conditional branches derive karte hue)

Flags exist HI KYUN karte hain? Branches ko poochna padta hai "kya result zero tha? negative?" bina dobara compute kiye. Compare instruction CMP a, b compute karta hai, result throw away karta hai, lekin flags rakhta hai. Phir BEQ (branch if equal) sirf test karta hai.

CMP a,b ke baad BEQ derive karna:

  • CMP compute karta hai. Agar toh .
  • BEQ branch karta hai iff → branch karta hai iff . ✔ first-principles se correct.

V (signed overflow) first principles se derive karna. -bit signed numbers ke saath addition ke liye, signed overflow tab hota hai jab dono operands ka sign same ho lekin result ka sign opposite ho: Kyun? Do positives legitimately negative mein sum nahi ho sakte range ke andar; agar bit pattern kehta hai unhone kiya, toh true sum representable range se overflow hua.


Recall Feynman: 12-saal ke bachche ko explain karo (click to reveal)

CPU ek super-fast cook hai. Registers woh kuch bowls hain jo chopping board ke bilkul paas hain — cook ingredients unse instantly uthata hai. RAM woh bada fridge hai kitchen ke us paar: bahut jagah, lekin wahan jaana slow hai. Kuch bowls special hain: ek bowl (PC) ek sticky note hold karta hai jisme likha hai "next step number." Doosra (SP) dirty plates ke stack ka top mark karta hai. Teesra (LR) ek note hai jisme likha hai "is kaam ke baad, yahan wapas aao." Aur CPSR ek chota scoreboard hai: kya last result zero tha? negative? kya woh edge se spill hua? Cook woh scoreboard check karta hai decide karne ke liye ki recipe mein aage skip karna hai ya nahi.


Common mistakes


Flashcards

Registers RAM se faster kyun hote hain?
Yeh physically CPU die par hote hain, seedha ALU se wired; koi off-chip memory bus latency nahi (~1 cycle vs ~100+ cycles).
Ek general-purpose register ko special se kya alag karta hai?
GPR ki koi hardware-imposed meaning nahi hoti (compiler decide karta hai iska use); ek special register (PC/SP/LR/CPSR) ka ek fixed role hota hai jise hardware khud read/update karta hai.
PC kya hold karta hai aur har cycle mein kaise change hota hai?
Next instruction ka address; hardware har fetch par (=instruction length) karta hai, aur branches ise target se overwrite karte hain.
Full-descending stack mein PUSH kya karta hai?
phir (pehle decrement, phir store).
Full-descending stack mein POP kya karta hai?
phir (pehle read, phir free).
BL target kaunse do actions perform karta hai?
(return address save karo) aur (jump karo).
Non-leaf function ko LR kyun save karna chahiye?
Ek nested BL LR overwrite karta hai, caller ka return address destroy karta hai; LR ko stack par save karne se unlimited nesting possible hoti hai.
Char CPSR condition flags kaunse hain?
N (negative, MSB=1), Z (zero result), C (carry/borrow, unsigned), V (signed overflow).
Addition par V (signed overflow) flag ka rule batao.
V=1 iff dono operands ka same sign ho lekin result ka opposite sign ho.
CMP a, b ke baad, BEQ kaun sa flag test karta hai aur kyun?
Z test karta hai; CMP compute karta hai, isliye exactly tab hota hai jab .
8-bit signed 0x7F + 0x01 ke liye N,Z,C,V kya hain?
N=1, Z=0, C=0, V=1 (127+1 signed range se overflow ho kar −128 ban jaata hai).
ARM par kaun si instructions flags update karti hain?
Sirf S-suffixed ops (ADDS, SUBS, …) aur CMP/CMN/TST/TEQ; plain ADD nahi karta.

Connections

  • Instruction Cycle (Fetch-Decode-Execute) — PC fetch stage drive karta hai.
  • The Stack and Function Calls (Calling Conventions) — SP & LR call/return frames implement karte hain.
  • Pipelining — classic ARM "PC = current + 8" offset explain karta hai.
  • Two's Complement Arithmetic — define karta hai ki N/C/V flags kaise compute hote hain.
  • Addressing Modes — registers as operands vs memory operands.
  • Cache Memory Hierarchy — registers L1 ke upar sabse fast tier hain.

Concept Map

motivates

width W stores

splits into

splits into

enables

keeps

includes

includes

includes

includes

auto-increment by L then

sets

read by

overwrite

set by BL for

Speed gap ALU vs RAM

Register tiny CPU cell

2 to the W values

General-Purpose Regs

Special-Purpose Regs

Register allocation

Hottest variables fast

PC next instr address

SP top of stack

LR return address

CPSR flags plus mode

Fetch loop

ALU operations

Branches

Function call return