Chapter 2 of 3
What Happens When a Computer Runs a Program
Trace a simple instruction through input, memory, processor and output. Distinguish hardware from software, see how stored instructions control physical circuits, and build a basic model of the repeated fetch, interpret and execute cycle.
1. A Program Is Instructions Plus Hardware
Two Different Things
A program is stored instructions and data. Hardware is the physical machinery that reads those bits and changes electrical states.
Stored Instructions Control Circuits
Instruction bits select operations inside the processor: where values travel, which calculation happens, and what state is updated next.
2. Input Becomes Data
From Key to Event
A key press begins as an electrical change. Keyboard hardware reports it; system software turns it into an input event for the application.
Text Is Not a Picture
The digit `7` can be stored as encoded numeric data. Later, text-rendering software chooses a glyph from a font to make it visible.
3. Memory Holds the Working State
RAM Is the Workbench
The operating system loads active code and data into RAM. RAM holds instructions, variables, temporary results, and input while the program runs.
Addresses, Not Variable Names
Processors use numeric memory addresses. Names such as `total` help people; compilers and runtimes map them to machine-level locations.
4. The Processor Keeps Its Place
The CPU's Place Marker
The program counter stores the address of the next instruction. Registers also hold values, addresses, and status information.
Choices Change the Path
After a comparison, a CPU can branch to a different address. Branches create `if` decisions, loops, function calls, and returns.
5. The Repeating Fetch, Decode, Execute Cycle
Four Questions per Instruction
What instruction comes next? What does it mean? What physical operation does it trigger? Where does execution continue?
A Useful Simplification
Modern CPUs pipeline and overlap work, but the fetch-decode-execute model still explains the logical flow of a running program.
6. Trace a Tiny Calculation
From readable code to machine actions
Consider this simple intent:
7. Follow the Result to Output
Thought exercise: pause at each boundary
Trace `show(total)` and name what changes:
- Software requests that the value `12` be displayed.
- The CPU executes instructions that place data in memory or registers and communicate with operating-system graphics services.
- Display software converts the value into text characters, layout information, and drawing commands.
- Graphics hardware updates display memory; the screen controller turns that data into timed electrical signals for pixels.
- Pixels emit or modulate light, and you see `12`.
If the program sends the value across a network instead, more layers participate. TCP presents applications with a reliable byte stream and transmits data in segments; IPv6 carries packets with source and destination addresses through a network. Those protocols are software rules implemented by programs and network hardware, not properties of the value itself.
Reflect: At which step did information become light? At which step did it remain only stored or moving electrical data?
8. Check Your Model
Choose the best description of what the CPU does after an instruction changes a value.
In the simplified execution cycle, what normally determines which instruction the CPU fetches next?
- The program counter, possibly changed by a branch or jump instruction
- The font used to draw the program's text
- The most recently pressed keyboard key
Show Answer
Answer: A) The program counter, possibly changed by a branch or jump instruction
The program counter holds the next instruction address. Most instructions advance it, while branches, calls, returns, and jumps can replace it with another address. Fonts affect display appearance, and keyboard input becomes data that a program may or may not use.
Key Terms
- RAM
- Main working memory that holds currently active instructions and data.
- glyph
- A visible shape used to render one or more characters.
- hardware
- The physical components of a computer, including processors, memory chips, storage devices, and input/output devices.
- register
- A small, high-speed storage location inside a processor.
- software
- Programs and related data that direct hardware to perform tasks.
- code point
- A numeric value assigned by Unicode to an encoded character.
- instruction
- An encoded command that tells a processor to perform a defined operation.
- program counter
- A register containing the address of the next instruction to fetch.
- fetch-decode-execute cycle
- The repeating process of retrieving an instruction, determining its meaning, performing it, and selecting the next instruction.