1.1.10How Computers Work

The CPU — ALU, control unit, registers

1,925 words9 min readdifficulty · medium

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, derived from first principles (what must happen to run an instruction?):

  1. You need to know which instruction is next. → keep its address in a special register, the Program Counter (PC).
  2. FETCH: Control Unit copies the instruction at address PC from memory into the Instruction Register (IR).
  3. You must point at the next one for later.PC ← PC + 1 (advance to the following instruction).
  4. DECODE: Control Unit reads the IR and figures out what operation it is and which registers it touches.
  5. EXECUTE: Control Unit signals the ALU (or memory) to do the work; result lands in a register.
  6. Repeat.

HOW the ALU actually adds (deriving binary addition)

Truth table for a+ba+b:

aa bb 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 R1=5R1 = 5, R2=3R2 = 3, 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 5+3=85+3=8; result → R1 the ALU is the only part that can add

Result: R1=8R1 = 8. 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 CMP use the ALU? Comparing = subtracting and checking the result's sign/zero flag.
  • Why does the loop work? JLT overwrites the PC with 1 instead of letting it become 4. When R1R1 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?
The ALU (arithmetic/logic), the Control Unit (decode + control), and the registers (fast on-chip storage).
What does the ALU do?
Performs arithmetic (add, subtract) and logic (AND, OR, NOT, compare) operations — the only part that computes.
What does the Control Unit do?
Fetches and decodes instructions and sends control signals telling other parts what to do and when; it does NOT calculate.
What are registers?
A small number of extremely fast storage slots inside the CPU, each holding one word (e.g. 64 bits).
What does the Program Counter (PC) hold?
The memory address of the NEXT instruction to fetch.
List the three stages of the instruction cycle.
Fetch, Decode, Execute (repeated in a loop).
After fetching an instruction, how does the PC normally change?
It advances by the instruction length L: PC ← PC + L.
How do loops and if-statements work at the CPU level?
A jump instruction overwrites the PC with a target address instead of letting it advance normally.
Derive the half-adder outputs.
sum = a XOR b; carry = a AND b.
What is the full-adder sum bit?
sum = a XOR b XOR carry_in.
Why does CMP (compare) use the ALU?
Because comparing is done by subtracting and checking the result flags (zero / sign).
Where does the result of "ADD R1, R2" go, and who computes it?
The ALU computes the sum and stores it back into the destination register R1.

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

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.

Test yourself — How Computers Work

Connections