1.1.10 · HinglishHow Computers Work

The CPU — ALU, control unit, registers

1,765 words8 min readRead in English

1.1.10 · Coding › How Computers Work


WHAT is the CPU made of?

Figure — The CPU — ALU, control unit, registers

HOW a CPU runs a program: the Fetch–Decode–Execute cycle

The cycle, pehle principles se derived (ek instruction run karne ke liye kya hona chahiye?):

  1. Tumhe pata hona chahiye ki agle instruction kaun sa hai. → uski address ek special register mein rakho, Program Counter (PC).
  2. FETCH: Control Unit address PC par jo instruction hai usse memory se Instruction Register (IR) mein copy karta hai.
  3. Tumhe baad ke liye agle instruction ki taraf point karna hoga.PC ← PC + 1 (agli instruction tak advance karo).
  4. DECODE: Control Unit IR ko padhta hai aur figure out karta hai ki kaunsa operation hai aur yeh kaunse registers ko touch karta hai.
  5. EXECUTE: Control Unit ALU (ya memory) ko kaam karne ka signal deta hai; result ek register mein land karta hai.
  6. Repeat.

HOW the ALU actually adds (deriving binary addition)

Truth table for :

carry sum
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0

Worked Example 1 — running ADD R1, R2 (R1 = R1 + R2)

Maano , , aur instruction address 10 par hai.

Step Kya hota hai Yeh step kyun?
Fetch IR ← memory[10] = ADD R1,R2 CU ko act karne se pehle instruction milna chahiye
PC+1 PC ← 11 taaki agle loop mein agli instruction fetch ho
Decode CU dekhta hai: op=ADD, dest=R1, srcs=R1,R2 CU ko pata hona chahiye kaunse wires on karne hain
Execute ALU compute karta hai ; result → R1 sirf ALU hi add kar sakta hai

Result: . CU ne kabhi arithmetic nahi ki — usne sirf orchestrate kiya.

Worked Example 2 — a loop via the Program Counter

addr 0: SET R1, 0        ; counter = 0
addr 1: ADD R1, 1        ; counter += 1
addr 2: CMP R1, 3        ; compare to 3 (sets a flag)
addr 3: JLT 1            ; if R1 < 3, jump back to addr 1
addr 4: ...              ; done
  • CMP ALU kyun use karta hai? Comparing = subtract karna aur result ka sign/zero flag check karna.
  • Loop kaam kyun karta hai? JLT PC ko overwrite kar deta hai 1 se, usse 4 banne ki jagah. Jab finally 3 tak pahunchta hai, flag kehta hai "less than nahi," PC apni natural value 4 rakhta hai, loop khatam.


Recall Feynman: ek 12-saal ke bachche ko explain karo

Ek computer ek bachche jaisa hai jo worksheet kar raha hai. Registers woh kuch numbers hain jo bachcha apne dimag mein rakhe. ALU bachche ka actual adding-aur-comparing brain hai. Control Unit teacher hai jo worksheet ki har line zor se padhta hai: "ab yeh dono add karo, ab check karo kya yeh bada hai." Program Counter teacher ki ungli hai jo point karti hai ki hum kaunsi line par hain. Loop banane ke liye, teacher apni ungli wapas upar le jaata hai page par. Bas itna hi hai — poora computer ek list padhna aur ungli point karna hai.


Flashcards

CPU ke teen core components kya hain?
ALU (arithmetic/logic), Control Unit (decode + control), aur registers (fast on-chip storage).
ALU kya karta hai?
Arithmetic (add, subtract) aur logic (AND, OR, NOT, compare) operations perform karta hai — sirf yahi part compute karta hai.
Control Unit kya karta hai?
Instructions fetch aur decode karta hai aur control signals bhejta hai jo baaki parts ko batate hain kya karna hai aur kab; yeh calculate NAHI karta.
Registers kya hain?
CPU ke andar kuch extremely fast storage slots, har ek mein ek word hota hai (jaise 64 bits).
Program Counter (PC) mein kya hota hai?
Fetch karne wali NEXT instruction ka memory address.
Instruction cycle ke teen stages list karo.
Fetch, Decode, Execute (loop mein repeat hote hain).
Instruction fetch karne ke baad PC normally kaise change hota hai?
Yeh instruction length L se advance karta hai: PC ← PC + L.
CPU level par loops aur if-statements kaise kaam karte hain?
Ek jump instruction PC ko normally advance hone dene ki bajaye ek target address se overwrite kar deta hai.
Half-adder outputs derive karo.
sum = a XOR b; carry = a AND b.
Full-adder ka sum bit kya hai?
sum = a XOR b XOR carry_in.
CMP (compare) ALU kyun use karta hai?
Kyunki comparing subtract karke aur result flags (zero / sign) check karke ki jaati hai.
"ADD R1, R2" ka result kahan jaata hai, aur kaun compute karta hai?
ALU sum compute karta hai aur use destination register R1 mein wapas store karta hai.

Connections

Concept Map

contains

contains

contains

includes

includes

feeds instructions

fetch from PC

decode

advance

signals to execute

result to

jump overwrites

CPU

ALU calculator

Control Unit boss

Registers scratchpads

Program Counter

Instruction Register

Main memory RAM