Machine Language Instruction Execution
The CPU's normal behavior is a sequential fetch-and-execute process.
Why the Ordinary Path Comes First
Interrupts are a way of handling events that do not fit neatly into the CPU's ordinary instruction sequence. To understand what an interrupt disrupts, you first need a clear picture of that ordinary sequence. This article describes it. The CPU's normal behavior is a sequential fetch-and-execute process: it repeatedly obtains an instruction and carries it out, one after another.
Before reading on, make a prediction. If a program is stored on a hard disk, does the CPU run it straight from the disk? Think about what you would expect the CPU to need in order to reach each instruction, and then check the answer in the next section.
Instructions as Binary Patterns
A machine language instruction is a binary pattern, meaning a sequence of 0s and 1s. It is not a human-level explanation of what should happen. An instruction does not say, in words, 'add these two numbers' or 'wait for a key press.' It is simply a pattern that the CPU recognizes as an instruction to carry out.
This matters for how you think about programs. A program is a collection of these patterns. The CPU has no built-in understanding of what the program is for. It processes the patterns in the order they appear and performs the operation each pattern represents. The source material is explicit that the CPU does not understand events or decide intelligently what to do; it follows the sequence it is given.
The diagram shows the idea of a sequence. Each slot holds one binary pattern. The highlighted slot is the one the CPU is about to handle, and the arrows show that the ordinary flow moves forward through the slots in sequence. The specific patterns are invented for this illustration. The point is that the instructions are stored as patterns and processed in an ordered way.
The Fetch-Execute Cycle
The CPU's basic cycle has two repeated phases. In the fetch phase, the CPU obtains the next instruction from main memory. In the execute phase, the CPU carries out the operation that instruction describes. After execution, the cycle moves to the following instruction in the sequence and begins again. Because this loop repeats continuously, the whole program is carried out one instruction at a time.
The loop is the heart of ordinary CPU behavior. Notice that it has no built-in exit in this description: the CPU keeps fetching and executing. Interrupts later become interesting precisely because they interfere with this regular rhythm.
Where Instructions Come From
The CPU's instruction fetching takes place through main memory. A program stored on a hard disk or solid state drive cannot be executed directly according to the source's model. It must first be loaded into main memory, and only then can the CPU fetch its instructions.
Temporary Workspace Versus Permanent Storage
Main memory is the temporary execution workspace. It is comparatively limited and volatile, which means the contents do not persist without power. Storage devices, such as hard disks and solid state drives, provide permanent space for larger amounts of information. The two roles are different, and the difference explains why a program must be copied into main memory before it runs.
| Feature | Main memory | Storage device |
|---|---|---|
| Role in execution | Temporary execution workspace | Permanent storage |
| Size relative to need | Comparatively limited | Provides space for larger amounts of information |
| Behavior without power | Volatile: contents are not kept | Permanent: information is kept |
| Direct CPU instruction source in this model | Yes, the CPU fetches instructions through it | No, a program must first be loaded into main memory |
Main memory and storage devices, as described in the source model
Tracing One Pass Through the Cycle
Following a short program through the cycle
A program with three instructions has already been loaded from storage into main memory. Trace how the CPU handles the first two instructions, using the fetch-execute loop.
- 1Starting point
The program is in main memory, because it was loaded there from storage. The CPU is ready to begin the ordinary sequence at the first instruction.
- 2First fetch
The CPU obtains the first instruction, a binary pattern, from main memory.
- 3First execute
The CPU carries out the operation the first pattern describes.
- 4Move forward
The cycle moves to the following instruction in the sequence, the second one.
- 5Second fetch and execute
The CPU fetches the second instruction from main memory and executes it. The third instruction would follow the same pattern if the cycle continued.
Each instruction is fetched from main memory and executed in sequence, and the cycle then moves on. The CPU does not need to understand the purpose of the program; it only repeats the fetch-execute steps.
What do you think happens?
Can the CPU fetch a program's instructions directly from a hard disk?
Reveal answer
No, the program must first be loaded into main memory.
According to the source's model, a program stored on a hard disk or solid state drive cannot be executed directly. It is loaded into main memory first, and the CPU fetches instructions through main memory.
Mistakes Beginners Make About Execution
Common mistake Thinking instructions are readable English-like commands
What it looks likeAssuming the CPU sees a line saying 'add these numbers' and understands it.
Why it failsInstructions are binary patterns, not human-level explanations.
Do this insteadThink of each instruction as a pattern the CPU recognizes and carries out.
Common mistake Assuming the CPU runs programs straight from the hard disk
What it looks likeBelieving a saved program starts executing from its place on the drive.
Why it failsA program on a hard disk or solid state drive must first be loaded into main memory, because the CPU fetches instructions through main memory.
Do this insteadRemember the path: storage, then main memory, then the CPU.
Common mistake Treating main memory and storage as interchangeable
Why it failsMain memory is a temporary, comparatively limited, volatile workspace, while storage provides permanent space for larger amounts of information.
Do this insteadUse main memory for what is being worked on now and storage for what must be kept.
Common mistake Believing the CPU decides intelligently what each event means
Why it failsThe source's model does not assume the CPU understands events or decides what to do intelligently. It follows the ordinary sequence.
Why This Model Prepares You for Interrupts
An interrupt is studied as a way of handling events that do not fit neatly into the CPU's ordinary instruction sequence. The sequence that an interrupt disrupts is the fetch-execute loop described above. Without a clear picture of the CPU repeatedly fetching instructions from main memory and executing them in order, the effect of an event that breaks into that order cannot be properly understood.
Practice Tracing the Cycle
Without looking back at the text, write the four stages of the path a program takes from a hard disk to the CPU, and describe in one sentence what the CPU does in each fetch-execute repetition. Then explain in two sentences why the program must be loaded into main memory before it can run.
Hints
- Start with the storage device and end with the CPU.
- Recall that the cycle has a fetch phase and an execute phase, and then moves to the next instruction.
- Think about what main memory is described as being, and what happens to it when power is removed.
Key Takeaways
- Machine language instructions are binary patterns that the CPU recognizes and carries out, not human-level explanations.
- The CPU's normal behavior is a sequential fetch-and-execute cycle: it fetches an instruction from main memory, executes it, and moves to the next one.
- A program on a hard disk or solid state drive must first be loaded into main memory, which is a temporary, comparatively limited, volatile workspace, while storage devices provide permanent space.
- Understanding this ordinary CPU-and-memory path is necessary background for studying how interrupts disrupt the normal instruction sequence.