The CPU — ALU, control unit, registers
WHAT is the CPU made of?

HOW a CPU runs a program: the Fetch–Decode–Execute cycle
The cycle, derived from first principles (what must happen to run an instruction?):
- You need to know which instruction is next. → keep its address in a special register, the Program Counter (PC).
- FETCH: Control Unit copies the instruction at address
PCfrom memory into the Instruction Register (IR). - You must point at the next one for later. →
PC ← PC + 1(advance to the following instruction). - DECODE: Control Unit reads the IR and figures out what operation it is and which registers it touches.
- EXECUTE: Control Unit signals the ALU (or memory) to do the work; result lands in a register.
- 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)
Say , , and the instruction lives at address 10.
| Step | What happens | Why this step? |
|---|---|---|
| Fetch | IR ← memory[10] = ADD R1,R2 |
CU must have the instruction before acting |
| PC+1 | PC ← 11 | so the next loop fetches the following instruction |
| Decode | CU sees: op=ADD, dest=R1, srcs=R1,R2 | CU must know which wires to switch on |
| Execute | ALU computes ; result → R1 | the ALU is the only part that can add |
Result: . The CU never did arithmetic — it only orchestrated.
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
- Why does
CMPuse the ALU? Comparing = subtracting and checking the result's sign/zero flag. - Why does the loop work?
JLToverwrites the PC with 1 instead of letting it become 4. When finally reaches 3, the flag says "not less than," PC keeps its natural value 4, loop ends.
Recall Feynman: explain it to a 12-year-old
A computer is like a kid doing a worksheet. The registers are the few numbers the kid keeps in their head. The ALU is the kid's actual adding-and-comparing brain. The Control Unit is the teacher reading each line of the worksheet out loud: "now add these two, now check if it's bigger." The Program Counter is the teacher's finger pointing at which line we're on. To make a loop, the teacher just moves their finger back up the page. That's it — the whole computer is reading a list and pointing a finger.
Flashcards
What are the three core components of a CPU?
What does the ALU do?
What does the Control Unit do?
What are registers?
What does the Program Counter (PC) hold?
List the three stages of the instruction cycle.
After fetching an instruction, how does the PC normally change?
How do loops and if-statements work at the CPU level?
Derive the half-adder outputs.
What is the full-adder sum bit?
Why does CMP (compare) use the ALU?
Where does the result of "ADD R1, R2" go, and who computes it?
Connections
- How Computers Work
- Binary and number representation
- Logic gates — AND, OR, NOT, XOR
- RAM and the memory hierarchy
- Machine code and assembly
- The clock and clock speed
- Fetch–Decode–Execute cycle
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, CPU computer ka dimaag hai, aur uske teen main parts hote hain. Pehla hai ALU — yeh actual calculator hai jo add, subtract aur comparison (logic) karta hai. Dusra hai Control Unit — yeh boss hai jo har instruction ko padh kar baaki parts ko bolta hai "ab yeh karo, ab woh karo." Aur teesra hai registers — yeh CPU ke andar ke chhote-chhote, super-fast dabbe hain jisme ek-ek number rakha jaata hai. Yaad rakho: CU sirf command deta hai, calculation toh sirf ALU karta hai.
CPU kaam kaise karta hai? Ek loop ke through, jise Fetch–Decode–Execute cycle kehte hain. Fetch matlab memory se agla instruction utha lo (uska address Program Counter mein hota hai). Decode matlab Control Unit samajhta hai ki yeh kaunsa operation hai. Execute matlab ALU asli kaam kar deta hai aur result kisi register mein chala jaata hai. Phir PC apne aap +1 ho jaata hai taaki agla instruction aaye. Yeh poora loop baar-baar chalta rehta hai — yahi computer ka "heartbeat" hai.
Loops aur if-else kaise bante hain? Bahut simple — ek jump instruction Program Counter ke andar nayi address daal deta hai, normal +1 ki jagah. Toh PC wapas upar chala jaata hai aur same code phir chalta hai. Matlab pura program ka behaviour bas isi baat par depend karta hai ki PC kahan point kar raha hai. Yeh idea clear ho gaya toh tum samajh gaye ki computer "sochta" kaise hai — woh sochta nahi, bas list padhta hai aur ungli (PC) move karta hai.