Slide 2
Quick Recap of Lecture 4
5 minutes · homework check
Remember
Homework check
- = puts a value in; == asks a question
- && needs both true; || needs one true
- Range: x >= 1 && x <= 10 (not 1 <= x <= 10)
- De Morgan: !(A || B) = !A && !B
- && is stronger than ||
- // Homework 4 and 5 check
- bool ok = t >= 10 && t <= 40; // HW4
- bool isTriangle = a > 0 && b > 0
- && c > 0
- && a + b + c == 180; // HW5
Recap
Programming Fundamentals · Lecture 5
Slide 3
Today's Plan
50 minutes · 8 parts
Recap & homework check
5 min
Learning outcomes & a question
3 min
Parts of a Von Neumann computer
12 min
The stored program & fetch–decode–execute
10 min
Variables live in memory: bytes & addresses
7 min
Class activity: Be the CPU
8 min
Common mistakes & recap quiz
4 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 5
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Name the parts of the Von Neumann architecture (CPU, memory, input/output, buses) and say what each does
SLO 5.1 → CLO4 · C1
Explain the stored-program idea and the fetch–decode–execute cycle
SLO 5.2 → CLO4 · C2
SLO 5.3 → CLO4 · C2
Relate a C++ variable to memory: bytes, addresses and sizeof
CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 5
Slide 5
What Really Happens When You Press Run?
A computer works like a busy dhaba (roadside restaurant) kitchen
Order slip
→
Input
keyboard, mouse, scanner
Chef
→
CPU (processor)
does the work, one step at a time
Shelf of numbered jars
→
Memory (RAM)
holds the recipe AND the ingredients
Waiter
→
Bus
carries things between chef and shelf
Plate served
→
Output
screen, printer, speaker
Module 1 · Parts of the Computer
Programming Fundamentals · Lecture 5
Slide 6
John von Neumann's Big Idea (1945)
The stored-program concept
- Before (ENIAC, 1945)
- To change the program, engineers re-plugged hundreds of cables by hand.
- A new program took days.
- After (stored program)
- The program is saved in memory as numbers, next to the data.
- A new program = load a new file. Seconds!
- Stored-program concept (in one line)
- Instructions and data are both kept in the SAME memory, and the CPU reads instructions from memory one by one.
- Real-world: your phone runs WhatsApp, then a game, then the calculator — the same hardware, just a different program loaded into memory.
Module 1 · Parts of the Computer
Programming Fundamentals · Lecture 5
Slide 7
The Von Neumann Architecture
Five main parts joined by buses
CPU (Central Processing Unit)
- Control Unit
- (manager)
- ALU
- (calculator)
- INPUT
- keyboard, mouse
- OUTPUT
- screen, printer
- Registers (PC, IR, accumulator …)
- tiny, super-fast storage
- Key point
- Program AND data share one memory and one path (bus) to the CPU.
system bus
- MAIN MEMORY (RAM)
- program instructions + data
Module 1 · Parts of the Computer
Programming Fundamentals · Lecture 5
Slide 8
Inside the CPU
Three parts, each with one job
Lecture 3 & 4 link: every +, *, % and every comparison like marks >= 50 that you wrote is finally done by the ALU.
Module 1 · Parts of the Computer
Programming Fundamentals · Lecture 5
Slide 9
Main Memory: Numbered Boxes
Each box holds 1 byte (8 bits) and has its own address
'A'
1000
1001
1002
1003
1004
1005
1006
1007
int marks = 85; → 4 boxes (1000–1003)
char grade
- RAM (main memory)
- Fast; the CPU works with it directly.
- Volatile: loses everything when the power goes.
- Example: 8 GB RAM in a lab PC.
- Storage (SSD / hard disk)
- Slower, but permanent (keeps data with power off).
- Your .cpp and .exe files live here.
- Example: 256 GB SSD.
Module 1 · Parts of the Computer
Programming Fundamentals · Lecture 5
Slide 10
Buses: The Roads Inside the Computer
A bus is a set of wires that carries signals between parts
Address bus
Data bus
Control bus
Carries the ADDRESS of the memory box the CPU wants
Carries the DATA (or instruction) itself, both ways
Carries commands such as READ or WRITE, and the clock
Real-world: Address written on a courier parcel
Real-world: The parcel itself
Real-world: "Deliver" or "collect" written on the slip
To read marks: CPU puts 1000 on the address bus + READ on the control bus → memory sends 85 back on the data bus.
Module 1 · Parts of the Computer
Programming Fundamentals · Lecture 5
Slide 11
The Fetch–Decode–Execute Cycle
The CPU repeats these three steps billions of times a second
1 FETCH
2 DECODE
3 EXECUTE
Copy the next instruction from memory (address in the PC) into the IR. Add 1 to the PC.
The Control Unit works out what the instruction means: add? store? jump?
Do it: the ALU calculates, or data is moved to/from memory or output.
repeat for the next instruction
Real-world: a student reading a recipe: read the next line (fetch), understand it (decode), do it (execute), move the finger to the next line (PC + 1).
Module 2 · Stored Program & the Cycle
Programming Fundamentals · Lecture 5
Slide 12
Watching the Cycle Run a C++ Program
Each C++ line becomes machine instructions; the CPU runs them one by one
cycle.cpp
- int a = 5; // 1
- int b = 7; // 2
- int sum = a + b; // 3
- cout << sum; // 4
Output
Module 2 · Stored Program & the Cycle
Programming Fundamentals · Lecture 5
Slide 13
How Fast Is "Fast"?
Clock speed is measured in gigahertz (GHz)
3 GHz
- Real-world
- A fast bowler's ball takes about 0.5 s to reach the batsman. In that time a 3 GHz CPU ticks about 1.5 billion times!
= about 3,000,000,000 clock ticks every second
- Cores
- A quad-core CPU has 4 cores = 4 'chefs', each running its own fetch–decode–execute cycle at the same time. Your C++ programs in this course use one core.
Module 2 · Stored Program & the Cycle
Programming Fundamentals · Lecture 5
Slide 14
The Von Neumann Bottleneck
One road for both instructions and data can become a traffic jam
- The problem
- The CPU is much faster than RAM.
- Instructions and data share one bus → the CPU waits.
- Like Canal Road in Faisalabad at 5 pm: one road, too many cars.
- The fix: cache memory
- A small, very fast memory inside/near the CPU keeps recently used instructions and data.
- Like keeping salt and mirch on the counter, not in the store room.
Module 2 · Stored Program & the Cycle
Programming Fundamentals · Lecture 5
Slide 15
A Variable Is a Named Group of Memory Boxes
sizeof tells how many bytes a type or variable uses
memory.cpp
Output
- int marks = 85;
- double cgpa = 3.45;
- char grade = 'A';
- bool passed = true;
- cout << "int uses " << sizeof(marks) << " bytes" << endl;
- cout << "double uses " << sizeof(cgpa) << " bytes" << endl;
- cout << "char uses " << sizeof(grade) << " byte" << endl;
- cout << "bool uses " << sizeof(passed) << " byte" << endl;
- int uses 4 bytes
- double uses 8 bytes
- char uses 1 byte
- bool uses 1 byte
- 1 byte = 8 bits
- A bit is a single 0 or 1.
- int marks needs 4 × 8 = 32 bits.
Module 3 · Variables Live in Memory
Programming Fundamentals · Lecture 5
Slide 16
Every Variable Has an Address
The & operator gives the memory address of a variable
address.cpp
- Name vs value vs address
- marks → the name
- 85 → the value in the box
- &marks → where the box is
- int marks = 85;
- double cgpa = 3.45;
- cout << "marks = " << marks << " is stored at " << &marks << endl;
- cout << "cgpa = " << cgpa << " is stored at " << &cgpa << endl;
- Your output will differ
- Addresses change on every run. That is normal!
Output (one run)
- marks = 85 is stored at 0x7ffff074b9ec
- cgpa = 3.45 is stored at 0x7ffff074b9f0
Real-world: your name is 'Ali', your CNIC card is the value, and your house address tells where to find you.
Module 3 · Variables Live in Memory
Programming Fundamentals · Lecture 5
Slide 17
CLASS ACTIVITY · 8 MIN · IN PAIRS
Be the CPU
Memory holds this program. One partner is the CPU, the other is Memory. Run the cycle and fill the table in your notebook.
Program in memory (addresses 100–104)
For each address write
- 100: LOAD x (x is 6)
- 101: LOAD y (y is 4)
- 102: MUL (ALU: x * y)
- 103: STORE area
- 104: PRINT area
- • PC before and after fetch
- • What the CU decodes
- • Which part executes it (CU, ALU, memory, output)
- • The final value of area
- Bonus: which line is the ALU's job?
Slide 18
Common Mistakes
Clear these up now — they appear in exams
✗
"RAM and hard disk are the same."
✓ RAM is temporary working memory; the disk keeps files permanently.
✗
"The CPU stores my files."
✓ The CPU only holds a few values in registers; data lives in memory/storage.
"sizeof(marks) gives the number of digits."
✓ sizeof gives BYTES: sizeof(int) is 4 even for marks = 7.
✗
"&marks prints 85."
✓ &marks prints the ADDRESS of the box; marks prints 85.
✗
Programming Fundamentals · Lecture 5
Slide 19
Quick Recap
Answer aloud
Q1
Name the three parts of the CPU and the job of each.
Q2
What is the stored-program concept?
Q3
List the three steps of the instruction cycle in order.
Q4
int n = 7; — what do n, sizeof(n) and &n give?
Programming Fundamentals · Lecture 5
Slide 20
Homework
Draw and label the Von Neumann block diagram (CPU with CU, ALU, registers; memory; input; output; buses).
In your own words, explain the fetch–decode–execute cycle using any daily-life example (not the recipe).
Write a program that prints sizeof for int, double, char, bool, float and long long. Note each answer.
A phone has 8 GB RAM and 128 GB storage. Which one keeps your photos when the phone is off, and why?
Next lecture: From source code to running program — preprocessor, compiler, linker and loader