Polling Loops and Interrupts
The CPU's normal behavior is a sequential fetch-and-execute process.
Why the Ordinary Path Comes First
Polling loops and interrupts are both ways of dealing with events that do not fit neatly into the ordinary flow of a program. An interrupt, in particular, is a way of handling an event that arrives outside the CPU's regular instruction sequence. But you cannot understand an interruption until you have a clear picture of the sequence being interrupted. This article builds that picture. It covers what the CPU normally does, where the instructions it runs actually live, and why the ordinary path is the foundation for everything that follows.
What do you think happens?
A program is saved on a hard disk. The CPU is told to run it. Does the CPU read the instructions straight from the disk, one at a time?
Reveal answer
No, the program must first be loaded into main memory
According to the model in the source material, a program stored on a hard disk or solid state drive cannot be executed directly. It must first be loaded into main memory, and the CPU's instruction-fetching activity takes place through main memory. Keep this in mind as we trace the cycle.
Instructions as Binary Patterns
An instruction, in the sense used here, is a binary pattern. It is a sequence of 0s and 1s that the CPU interprets as a machine language instruction. The source material is explicit on this point: instructions are binary patterns, not human-level explanations. A machine does not read the sentence add these two numbers. It reads a pattern of bits that the hardware is built to recognize.
Because the CPU works with patterns rather than explanations, a single pattern has to carry enough structure for the CPU to tell what it should do. A useful way to picture this is to think of the pattern as being split into fields. One field indicates the operation, and other fields supply the data the operation works on. The source material refers to these parts as an operation code and operands. The diagram below shows the idea using an invented pattern. It is not a real instruction set, and the split shown is only there to make the concept concrete.
Where Programs Live: Memory and Storage
Before the CPU can run anything, the instructions have to be somewhere it can reach. The source material separates two roles. Main memory is the temporary execution workspace, the place where a running program's instructions are held while the CPU works through them. Storage devices, such as hard disks and solid state drives, provide permanent storage. They keep information even when the machine is switched off, and they offer room for much larger amounts of information than main memory does.
| Property | Main memory | Storage device |
|---|---|---|
| Role | Temporary execution workspace | Permanent storage |
| Capacity | Comparatively limited | Provides space for larger amounts of information |
| Persistence | Volatile | Permanent |
| Can the CPU run from it directly | Yes, this is where instruction fetching happens | Not directly, according to the source model |
Main memory and storage play different roles in the execution model.
The practical consequence is that a program moves through a fixed route. It is stored on storage, loaded into main memory, and only then executed. The ordinary path always runs through main memory, which is why the source material treats main memory as central to the CPU's behavior.
Tracing the Fetch-Execute Cycle
The CPU's normal behavior is a sequential fetch-and-execute process. Think of it as a repeating loop with two stages. In the fetch stage, the CPU retrieves an instruction from main memory. In the execute stage, it carries out the operation that the instruction describes. Once that instruction has been executed, the cycle moves on to the next instruction in the sequence and repeats. The diagram below shows this loop. Notice that it is a loop in which each step leads to the next, not a set of separate actions.
- 1The program's instructions are already in main memory, having been loaded from storage.
- 2The CPU fetches the instruction at the current position from main memory.
- 3The CPU executes the instruction that was fetched.
- 4The CPU moves to the next instruction in the sequence and fetches it.
- 5The cycle repeats until the program's instructions have been carried out.
The defining feature of the ordinary path is that it is sequential. The CPU does not jump around deciding what matters. It keeps fetching and executing in order, and this steady rhythm is exactly what an interrupt will later disturb.
Worked Trace of a Short Instruction Sequence
Following Instructions Through Memory
Suppose a short program has been loaded into main memory as four consecutive binary patterns, labeled by their position. The CPU starts at position 0. Trace what happens during the first two passes of the cycle. The patterns are invented for this illustration.
- 1Starting state
The program is in main memory at positions 0 to 3. The CPU's current position is 0. Nothing has been fetched yet.
- 2First fetch
The CPU fetches the binary pattern at position 0 from main memory.
- 3First execute
The CPU executes the pattern it fetched. The operation is carried out using the operand field of that pattern.
- 4Second fetch
The sequence moves on, so the CPU fetches the pattern at position 1 from main memory.
- 5Second execute
The CPU executes the pattern at position 1 and then continues the cycle through positions 2 and 3 in the same way.
The CPU never looks ahead to choose an order of its own. It works through main memory position by position, fetching and executing one instruction at a time.
Mistakes About Where Programs Run
Common mistake Assuming the CPU reads instructions directly from the hard disk or solid state drive
What it looks likeA learner says the CPU runs each line of a saved program straight off the disk as it goes.
Why it failsThe source model states that a program stored on a hard disk or solid state drive cannot be executed directly. It must be loaded into main memory first, and fetching happens through main memory.
Do this insteadThink of storage as the permanent home of the program and main memory as the workspace where the CPU actually fetches and runs it.
Common mistake Treating main memory and storage as interchangeable
What it looks likeA learner assumes that because both hold data, either one can serve as the program's working space.
Why it failsMain memory is the temporary execution workspace and is comparatively limited and volatile. Storage provides permanent space for larger amounts of information.
Do this insteadAsk which role a piece of information plays. Running instructions belongs in main memory. Keeping them safely over time belongs in storage.
Common mistake Reading a binary instruction as if it were a sentence
Why This Model Is Background for Interrupts
An interrupt is studied as a way of handling events that do not fit into the CPU's ordinary instruction sequence. To see why it counts as a departure, you need the ordinary sequence firmly in mind. The fetch-execute cycle is the steady path, and main memory is where that path runs. An event that arrives from outside this path has to be understood relative to it.
Checking Your Trace
A program is stored on a solid state drive. Describe, in order, the route its instructions take before the CPU executes the first one, and explain why main memory is needed even though the drive can hold the whole program.
Hints
- Start with where the program is stored permanently.
- Think about which component the instructions must pass through before the CPU fetches them.
- Consider the difference between a permanent role and a temporary workspace.
Key Takeaways
- Instructions are binary patterns that the CPU interprets as machine language, not human-level explanations.
- The CPU's normal behavior is a sequential fetch-and-execute cycle that repeatedly fetches an instruction from main memory, executes it, and moves to the next one.
- Main memory is the temporary execution workspace, while storage devices provide permanent storage. A program must be loaded into main memory before it can be executed.
- This ordinary sequential path is the necessary baseline for studying how interrupts handle events outside the normal instruction sequence.