Concepts / Machine Language Instruction Execution

Machine Language Instruction Execution

The CPU's normal behavior is a sequential fetch-and-execute process.

  • Programming

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.

Slot 00101 1100Slot 11010 0011Slot 20011 1110Slot 31100 0001nextnextnext
Which location does the CPU read next, and how does that location change from one instruction to the next? This illustration is generated for teaching purposes; the binary values are invented examples, not a real instruction set.

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.

Fetch instructionfrom main memoryExecute instructionMove to nextinstructioninstruction obtainedoperation donerepeat
After the CPU finishes one instruction, what happens next, and how does the cycle repeat?

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.

Storage devicepermanent storageMain memorytemporary workspaceCPUfetches and executesprogram loadedinstructions fetched
When a program runs, which components do instructions and data travel between, and in what direction?

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.

FeatureMain memoryStorage device
Role in executionTemporary execution workspacePermanent storage
Size relative to needComparatively limitedProvides space for larger amounts of information
Behavior without powerVolatile: contents are not keptPermanent: information is kept
Direct CPU instruction source in this modelYes, the CPU fetches instructions through itNo, 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

Worked example

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.

  1. 1
    Starting 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.

  2. 2
    First fetch

    The CPU obtains the first instruction, a binary pattern, from main memory.

  3. 3
    First execute

    The CPU carries out the operation the first pattern describes.

  4. 4
    Move forward

    The cycle moves to the following instruction in the sequence, the second one.

  5. 5
    Second 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.

Result

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?

  • Yes, directly from the disk

  • No, the program must first be loaded into main memory

Reveal answer
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 like

    Assuming the CPU sees a line saying 'add these numbers' and understands it.

    Why it fails

    Instructions are binary patterns, not human-level explanations.

    Do this instead

    Think 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 like

    Believing a saved program starts executing from its place on the drive.

    Why it fails

    A 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 instead

    Remember the path: storage, then main memory, then the CPU.

  • Common mistake

    Treating main memory and storage as interchangeable

    Why it fails

    Main memory is a temporary, comparatively limited, volatile workspace, while storage provides permanent space for larger amounts of information.

    Do this instead

    Use 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 fails

    The 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

EASY

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.