Unit 3 Session 5: How Computers Use Memory | IT 231 Slides

--:-- --
↓ Scroll for more

Unit 3 · Session 5

How Computers Use Memory

IT 231: IT and Application (BBM)

Learning Objectives 🎯

By the end of this session, 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.
  • ✅ Explain the flow of data from secondary storage to RAM to the CPU.

The Big Picture: From Click to Action

How does a computer run a program like a web browser or a game?

1. Storage: The program waits on your slow, long-term storage (SSD/HDD).

2. Memory (RAM): When you open it, it's copied into fast, temporary RAM.

3. CPU: The CPU then takes instructions from RAM to actually run the program.

Think of it as moving ingredients from the pantry (storage) to the kitchen counter (RAM) so the chef (CPU) can cook.

The Data Flow 📊

Data moves through a hierarchy based on speed and proximity to the CPU.

1. Secondary Storage

(SSD, HDD)

Slowest

Long-term, permanent storage.

→

Loads into

→

2. Primary Memory (RAM)

(DDR4, DDR5)

Fast

Workspace for active programs.

↓

3. CPU (Cache & Registers)

Fastest

Immediate processing area.

⏱️ If One Nanosecond Were One Second

Approximate time to fetch data, scaled up so that a 1 ns cache read takes 1 second:

  • CPU cache (about 1 ns) becomes 1 second
  • RAM (about 100 ns) becomes under 2 minutes
  • SSD (about 100 µs) becomes about 1 day
  • HDD (about 10 ms) becomes about 4 months

This is why programs are copied into RAM first: the CPU can't wait "months" for the disk on every instruction.

🧮 Worked Example: Why Cache Is Needed

A cache read takes about 1 ns; a RAM read takes about 100 ns. Suppose the data is already in cache 95% of the time (a cache hit).

Without cache: every read costs 100 ns

With cache: 0.95 × 1 + 0.05 × 100 = 0.95 + 5 ≈ 6 ns on average

About 17 times faster, from a cache far smaller than RAM.

Exam answer: cache is a small, fast memory (SRAM) between the CPU and RAM that keeps recently and frequently used data, so the CPU waits less. Unit 3.3 covers it in detail.

The CPU's Heartbeat ⚡

Once a program is in RAM, the CPU takes over. It runs a continuous loop to process instructions.

This process is called the Fetch-Decode-Execute Cycle. It's the fundamental operation of the CPU, repeated billions of times per second.

💓 How Fast Is the Heartbeat? Clock Speed

A CPU's clock speed is how many cycles it runs per second. 1 GHz = 1 billion cycles per second.

  • A 3 GHz CPU ticks 3 billion times a second, so one tick lasts about 0.33 nanoseconds.
  • In that time, light travels only about 10 cm.
  • GHz isn't everything: some instructions take several cycles, and modern CPUs have several cores working on many instructions at once.

The Fetch-Decode-Execute Cycle

1. Fetch 🔍

The CPU's Control Unit fetches the next instruction from its memory address in RAM.

"What is the next thing I need to do?"

2. Decode 🧠

The Control Unit decodes the instruction, figuring out what operation it needs to perform (e.g., add, subtract, move data).

"What does this instruction mean?"

3. Execute ⚡

The Arithmetic Logic Unit (ALU) performs the operation, like a calculation or moving data to a register.

"Okay, I'm doing it now!"

This cycle repeats continuously, allowing your computer to function.

🎬 Watch: One Cycle, Register by Register

Inside the Cycle: The CPU's Special Registers

RegisterFull nameJob in the cycle
PCProgram CounterHolds the address of the next instruction; goes up by 1 after each fetch
MARMemory Address RegisterHolds the RAM address being read from or written to
MDRMemory Data RegisterHolds the instruction or data just read from RAM (or about to be written)
CIRCurrent Instruction RegisterHolds the instruction being decoded and executed
ACCAccumulatorHolds the ALU's working result

Fetch in register terms: PC → MAR → RAM → MDR → CIR, then PC + 1.

✏️ Drawing the Cycle for the Exam

1. FETCH
Copy the next instruction from RAM (address in the PC) into the CPU; PC + 1

➡️

2. DECODE
The Control Unit works out the operation and the data it needs

➡️

3. EXECUTE
The ALU calculates or data is moved; the result goes to a register or RAM

🔁 Back to FETCH for the next instruction, until HALT

Example to write under the diagram: a calculator app adding 7 + 5. Fetch the ADD instruction, decode it as "add two numbers", then the ALU executes 7 + 5 = 12 and stores the result.

Show the three labelled steps, arrows that loop back, one line per step, and a worked example.

⚡ Quick Quiz: Fetch, Decode or Execute?

  1. The ALU adds two numbers. → Execute
  2. The instruction at address 205 is copied from RAM into the CPU. → Fetch
  3. The Control Unit works out that "MUL" means multiply. → Decode
  4. The Program Counter goes up from 205 to 206. → Fetch (it happens as part of fetching)
  5. A result is written back to RAM. → Execute

🎭 Activity: Be the CPU

Program in RAM

100LOAD 7
101ADD 5
102STORE to address 200
103HALT

Four volunteers play the Program Counter, Control Unit, ALU and RAM.

The Program Counter starts at 100. LOAD and ADD work on the ALU's single register. Run fetch, decode, execute for each line.

Result: The register holds 7, then 12. Address 200 now holds 12, and the CPU stops at address 103 after four cycles.

🧺 Pipelining: An Assembly Line Inside the CPU

Modern CPUs overlap the steps: while one instruction executes, the next is decoded and a third is fetched.

Clock tick12345
Instruction AFDE
Instruction BFDE
Instruction CFDE

Count: how many ticks do 3 instructions take one after another with no overlap? And with the pipeline?

No overlap: 3 × 3 = 9 ticks. Pipeline: 5 ticks, and each extra instruction adds only 1 more.

Analogy: Making Chiya (Tea)

Let's compare the cycle to a simple, everyday task.

  • Fetch: You get the recipe book (RAM) and find the next step: "Add sugar".
  • Decode: Your brain understands this means grabbing the sugar jar and a spoon.
  • Execute: You physically take the sugar and add it to the pot.
  • Then you repeat the cycle for the next instruction: "Stir".

Fetch the ingredients, follow the recipe

A tea seller in Old Delhi pouring and stirring a pot of milk tea at his stall

Practical Application in Nepal

How does this apply to using a digital wallet like eSewa or Khalti?

eSewa Logo
  1. Launch: You tap the eSewa app. The OS loads it from your phone's storage into RAM.
  2. Fetch: CPU fetches the instruction for "Send Money".
  3. Decode: It understands it needs to access the amount field, recipient number, and the 'confirm' button logic.
  4. Execute: When you hit confirm, the ALU performs the calculations and sends the data to complete the transaction.

What Happens When You Save?

The data flow also works in reverse.

When you are typing a document, where are the words being stored before you hit "Save"?

Answer: In RAM! 💡

Crucial Point: RAM is volatile (temporary). If the power goes out, all your unsaved work is lost. Hitting "Save" writes the data from RAM back to your non-volatile secondary storage (SSD/HDD) for safekeeping.

Discussion Point: Out of RAM?

What happens if you try to run too many programs at once and you run out of RAM?

  • Your computer slows down dramatically.
  • The operating system starts using a part of your secondary storage (SSD/HDD) as "virtual memory," which is much slower than actual RAM.
  • This process is called "swapping" or "paging."
  • Programs may become unresponsive or even crash.

🎬 Watch: Filling 16 GB with Tabs

🎬 Watch: Why Swapping Is Slow

🧮 Worked Example: Is 8 GB Enough?

Rough figures for an 8 GB laptop (real numbers vary):

  • Windows itself: about 3 GB
  • A Zoom call: about 1 GB
  • Microsoft Word: about 0.5 GB
  • A browser with 20 tabs at about 0.2 GB each: 4 GB

Total: 3 + 1 + 0.5 + 4 = 8.5 GB. That's more than 8 GB, so the OS starts swapping.

🧮 Is 8 GB Enough? (cont.)

Phones act differently: Android and iOS close background apps when memory runs low, which is why an app sometimes restarts when you switch back to it.

🗣️ Think-Pair-Share: Which Upgrade?

A friend's laptop has 8 GB of RAM and an HDD. They complain about two things:

  1. Apps take ages to open. Answer: opening copies the program from storage into RAM, so replace the HDD with an SSD.
  2. It crawls with many tabs open. Answer: RAM is full and it's swapping, so add more RAM.

The same complaint ("it's slow") can come from two different bottlenecks in the data flow.

Multitasking: How One CPU Runs Many Programs

  • A CPU core follows one stream of instructions at a time, yet you can play music, download a file and type all at once.
  • The operating system gives each program a tiny time slice (milliseconds), then switches to the next: context switching.
  • The switches are so fast that the programs seem to run together. With several cores, some really do.

Analogy: one momo cook at a busy stall: fold a few momos, check the steamer, take an order, back to folding. Every customer feels served.

💡 Why "Have You Tried Restarting It?" Works

  • Some programs have a memory leak: they keep claiming RAM and never give it back.
  • Over days, free RAM shrinks, the OS starts swapping to storage, and the PC slows down.
  • A restart empties RAM completely (it is volatile), so every program starts fresh from storage.

Save your work first: anything that is only in RAM is lost on a restart, just as in a power cut.

📝 Exam Answer Plan: From Storage to CPU

"Explain how a computer uses memory to run a program." Build the answer in five points:

  1. Stored: the program sits on secondary storage (SSD/HDD), which is non-volatile but slow.
  2. Loaded: when you open it, the OS copies it into RAM, the fast working memory.
  3. Cached: the instructions and data in use are copied into cache, then into registers.
  4. Processed: the CPU runs the fetch-decode-execute cycle billions of times a second.
  5. Saved: results go from RAM back to storage when you save; if RAM fills up, the OS swaps to virtual memory.

Add a simple diagram (Storage → RAM → Cache → Registers/CPU, with an arrow back for Save) to strengthen the answer.

Summary & Key Takeaways

Let's review the core concepts of CPU and memory interaction.

  • Programs must be loaded from secondary storage into faster RAM to be executed by the CPU.
  • The CPU continuously performs the Fetch-Decode-Execute cycle to process program instructions.
  • Data flows from storage ➡️ RAM ➡️ CPU Cache/Registers for processing, and back to storage when you save.
  • This coordinated system allows computers to perform complex tasks efficiently.

Exam Practice: Short Answer

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

Exam Practice: 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.

Revise the chapter note before writing these answers.

Image & Video Credits

Images

  • e-Sewa logo: public domain, via Wikimedia Commons
  • Masala chai seller, Old Delhi: Jakub Hałun, CC BY-SA 4.0, via Wikimedia Commons

Videos (YouTube)

  • Computer Science with Mr. E, "How Does a CPU Work? | Fetch–Decode–Execute Cycle Explained with Animation" (4:08–5:12), https://www.youtube.com/watch?v=4775ikl_bLY
  • Albert Unbox, "How Many Tabs Does It Take to Kill a 16GB Laptop?" (5:35–6:37), https://www.youtube.com/watch?v=eufI64wKKdc
  • Goodsauce Tech, "What happens when you run out of RAM? What if you don't have enough RAM memory?" (0:36–1:57), https://www.youtube.com/watch?v=STsZB2TWisM

Teaching content © Arjan KC. Third-party media remain the property of their owners.

Thank You!

Any questions?


📖 Read first (chapter notes)