Learning Objectives
By the end of this chapter, you will be able to:
- Trace how a program moves from storage to registers.
- Explain the fetch decode execute cycle with Program Counter and Instruction Register.
- Distinguish registers, cache, RAM, and virtual memory by speed and role.
- Diagnose when adding RAM fixes slowness and when it does not.
Detail: CPU and buses, memory hierarchy, inside the cabinet.
How memory runs a program: the path
A program on disk cannot run where it sits. It must be copied into memory the CPU can reach. The operating system does that copy. Then the CPU loops.
flowchart LR
SSD["Secondary Storage<br/>(SSD/HDD)"] --> RAM["RAM: working set"]
RAM --> Cache["Cache L1 L2 L3"]
Cache --> Reg["Registers"]
Reg -->|Save| SSD
- You launch an app. The OS finds files on secondary storage.
- Code and needed data are loaded into RAM.
- The CPU pulls lines from RAM into cache, then words into registers.
- You type. New data sits in RAM. Save writes it back to storage. Without that, power loss erases it.
Group the path by urgency.
| Layer | Where | What it holds now | Volatility |
|---|---|---|---|
| Registers | Inside CPU | Operands and current instruction, bytes | Yes |
| Cache | On or next to CPU | Hot blocks from RAM, a few MB | Yes |
| RAM | Motherboard slots | Working set of OS and apps, 8 to 32 GB | Yes |
| Storage | Drive bays | All files, 256 GB to TB | No |
Fast costs more per byte, so the machine keeps a little very fast near the CPU and a lot of slower further away. That trade is the whole reason for hierarchy. See memory hierarchy for Hot Warm Cold.
The cycle that never stops
The CPU lives in a loop. Three steps, plus a fourth that some books name.
- Fetch. Control Unit asks RAM for the instruction at the address held in the Program Counter (PC). Bits travel on address and data buses.
- Decode. Control Unit reads the bits in the Instruction Register (IR) and decides: add, compare, load, store.
- Execute. ALU calculates or moves data between cache and registers.
- Store. Result is written to a register or back to RAM and the PC advances to the next address.
flowchart LR
F[Fetch at PC] --> D[Decode in CU]
D --> E[Execute in ALU]
E --> S[Store result]
S --> F
The loop repeats billions of times per second. A 3.0 GHz clock ticks about 3 billion times. Each tick can advance one simple step, which is why work feels instant even though each Total = Price + VAT needs several passes: fetch load Price, decode, execute into register, fetch add VAT, decode, execute add, store to Total.
Worked trace. To do C = A + B where A is 20 and B is 15:
- Fetch instruction
LOAD A. Decode as load from A. Execute: copy 20 into register R1. PC moves. - Fetch
ADD B. Decode as add B. Execute: ALU adds R1 plus value at B. Result 35 sits in register. - Fetch
STORE C. Decode as store to C. Store 35 to RAM address C.
Three fetches. Three decodes. The program counter made order. The registers held what mattered this cycle.
Why cache and why virtual memory
Cache avoids waiting for RAM. When the CPU needs data it checks L1 first. A hit means found and fast. A miss means fetch from next level and pay time. Temporal locality and spatial locality explain why hits are common. Recent data and its neighbors are likely next.
Virtual memory is the OS using part of SSD or HDD as overflow RAM, called page file or swap. When RAM is full, least used pages move to disk. More programs can open, but speed collapses because disk is far slower than RAM. Constant swapping is thrashing. It looks like CPU load but the disk light stays solid.
Diagnosis by count:
- Too little RAM. Switching tabs and apps stalls, disk activity high, adding RAM removes most delay. This was the office PC with 4 GB running Tally plus 15 Chrome tabs plus video call.
- Enough RAM. Adding more does little. A faster SSD or faster CPU or better cache may help.
- Thrashing. Adding RAM is the cure. Adding GHz without RAM just waits faster.
Self-check
Nepal worked example: 4 GB that cost more than 16 GB (NPR, BS 2081)
A bookkeeping firm in Chitwan bills clients on Tally Prime. On 2081-08-18 three staff share one PC with 4 GB RAM, 256 GB HDD, and a 2.4 GHz CPU. Day end closing takes 34 minutes. A new PC is quoted at NPR 68,000. An 8 GB DDR4 stick is NPR 4,200. A 512 GB SATA SSD is NPR 5,800. Labour NPR 700.
Option A: new PC = NPR 68,000.
Option B: add 8 GB to reach 12 GB and clone HDD to SSD = 4,200 + 5,800 + 700 = NPR 10,700. Saving versus A = 68,000 - 10,700 = NPR 57,300.
Why this targets the right layer: monitoring shows RAM at 96 percent and disk queue above 80 percent while CPU sits near 45 percent. The CPU was not the bottleneck. Virtual memory on HDD was. After Option B, closing falls to 9 minutes. The fetch decode execute cycle did not change. The speed of feeding it did.
Example 1: In a cooperative, the path decides audit windows. Ask: on 2081-08-20 can interest for 8,000 members stay in RAM to finish before the Branch closes, or does swap push postings into disk and miss the cut? Add Warm RAM before faster silicon.
Example 2: In a store, the cycle and cache decide till response. Ask: when five bills scan at once, does the hot price list fit in L2 and L3 so each fetch hits cache, or does every lookup miss to RAM? If misses dominate, more cache or less bloat beats GHz.
Summary
- Programs move from storage to RAM to cache to registers, then back on save.
- The CPU repeats fetch at PC, decode in CU via IR, execute in ALU, store.
- Registers and cache are tiny and fast. RAM is working capacity. Storage is retention.
- Virtual memory extends RAM on disk at a steep speed cost. Thrashing looks like a slow CPU but is a short RAM.
Key Takeaways
- Volatility divides fast working layers from durable storage.
- The Program Counter holds where, the Instruction Register holds what.
- A cache hit is speed. A miss is waiting for RAM.
- If disk activity is high and RAM is full, adding RAM beats adding GHz.
Discussion Questions
- Where are the words you typed before you press Save, and what happens if power fails there?
- Why is the fetch decode execute pattern called a cycle?
- When would extra RAM give no visible gain?
Exercises
Short answer
- List the four steps of the machine cycle and name the two special registers.
- Why is cache faster than RAM, and what is a hit versus a miss?
- What is virtual memory and what is thrashing?
Long answer
- Trace
Total = Price + VATfrom SSD to registers through fetch decode execute with PC and IR, using a diagram. - A Chitwan firm on 2081-08-18 runs Tally on 4 GB plus HDD with 96 percent RAM and high disk queue. Recommend between a NPR 68,000 new PC and a NPR 10,700 RAM plus SSD upgrade, with thrashing reasoning and NPR math.


