Learning Objectives

By the end of this chapter, you will be able to:

  • Describe how a computer uses memory to run a program.
  • Explain the three steps of the CPU’s fetch-decode-execute cycle.
  • Understand the flow of data from secondary storage to RAM to the CPU.
  • Explain the roles of registers, cache, and virtual memory.
  • Explain the effect of RAM size on performance.

How Computers Use Memory to Run Programs

For a computer to run a program (like a web browser or a word processor), the program’s instructions and data must be loaded from the slow secondary storage (like an SSD or HDD) into the much faster primary memory (RAM). The CPU can then access the instructions and data from RAM to execute the program.

This process is managed by the operating system and happens in a continuous loop known as the fetch-decode-execute cycle.

The Fetch-Decode-Execute Cycle

The CPU continuously performs three basic steps:

  1. Fetch: The CPU fetches the next instruction from its location in RAM.

  2. Decode: The CPU’s control unit decodes the instruction to understand what operation needs to be performed (e.g., add two numbers, move data).

  3. Execute: The CPU’s Arithmetic Logic Unit (ALU) executes the instruction. This might involve performing a calculation or moving data between registers, cache, and RAM.

This cycle repeats millions or billions of times per second, which is what allows the computer to perform complex tasks.

flowchart LR
  F[Fetch instruction from memory] --> D[Decode in control unit]
  D --> E[Execute in ALU]
  E --> S[Store result]
  S --> F

Some textbooks add a fourth step, store (write the result back to a register or memory). Two special registers help: the Program Counter (PC) holds the address of the next instruction, and the Instruction Register (IR) holds the instruction being decoded.

Worked example: to calculate Total = Price + VAT, the CPU fetches the instruction “load Price”, decodes and executes it (Price goes into a register), then fetches “add VAT”, and finally “store into Total.” Each simple step is one pass of the cycle. A 3 GHz CPU completes about 3 billion clock ticks per second, which is why such steps feel instant.

The Flow of Data

  1. When you launch an application, the operating system finds the program files on your secondary storage (e.g., your SSD).

  2. A copy of the program’s instructions and any necessary data is loaded into RAM.

  3. The CPU begins the fetch-decode-execute cycle, pulling instructions and data from RAM into its cache and then into its registers for immediate processing.

  4. As you work, any new data you create or changes you make are held in RAM. When you save your work, that data is written back to secondary storage for long-term preservation.

Why this layered memory?

Memory Location Speed Size (typical) Volatile?
Registers Inside the CPU Fastest A few bytes each Yes
Cache (L1–L3) Inside/near the CPU Very fast A few MB Yes
RAM Motherboard slots Fast 8–32 GB Yes
SSD / HDD Drive bays Much slower 256 GB–several TB No

Fast memory is expensive, so computers keep a little very fast memory near the CPU and a lot of cheaper, slower storage further away. Cache stores recently and frequently used data so the CPU does not wait for RAM each time — a cache hit is fast, a cache miss means fetching from slower RAM.

flowchart LR
  SSD["Secondary Storage<br/>(SSD/HDD)"] --> RAM["Primary Memory<br/>(RAM)"]
  RAM --> Cache["Cache"]
  Cache --> Reg["CPU Registers"]
  Reg -->|Save work| SSD

Figure 1: Data Flow from Storage to CPU

Virtual Memory

When RAM is full, the operating system uses part of the SSD or HDD as virtual memory (a page file or swap space). Less-used data is moved out of RAM to make room. This lets you open more programs than RAM alone could hold, but the computer slows down because storage is much slower than RAM. Constant swapping between RAM and disk is called thrashing.

Effect of RAM on Performance

  • With too little RAM, the computer relies heavily on virtual memory, so switching between apps and browser tabs becomes slow.
  • More RAM lets more programs and larger files stay in fast memory.
  • Beyond what your work needs, extra RAM brings little benefit; a faster SSD or CPU may matter more.

Example: an office PC with 4 GB RAM struggles when an accountant runs accounting software, a browser with 15 tabs, and a video call at once. Upgrading to 8 or 16 GB removes most of the slowdown, often a cheaper fix than buying a new computer.

Summary

Computers use a hierarchical memory system to run programs efficiently. Programs are stored long-term on slower secondary storage and are loaded into faster RAM to be run. The CPU then pulls instructions and data from RAM through cache into registers, executing them in a continuous fetch-decode-execute cycle guided by the program counter. When RAM is full, virtual memory on the disk extends it at the cost of speed. This coordinated flow of data between storage, RAM, and the CPU is what allows a computer to function.

Key Takeaways

  • Programs must be loaded from secondary storage into RAM to be executed.
  • The CPU continuously performs the fetch-decode-execute cycle.
  • Data flows from storage to RAM to cache and registers for processing.
  • Virtual memory uses disk space when RAM is full, but is much slower.
  • Adequate RAM is key to smooth multitasking.

Discussion Questions

  1. What happens if you try to run too many programs at once and you run out of RAM?
  2. Why is the fetch-decode-execute cycle called a “cycle”?
  3. When you are typing a document, where are the words being stored before you hit “Save”?

Exam-Style Questions

Short answer

  1. List the steps of the machine cycle.
  2. What is cache memory? Why is it needed?
  3. What is virtual memory?

Long answer

  1. Explain how a computer uses memory to run a program, from storage to the CPU.
  2. Describe the fetch-decode-execute cycle with a diagram and an example.