Concepts / Machine Language Instructions

Machine Language Instructions

A program is stored as a sequence of machine language instructions in main memory.

  • Programming

A Program as a Stored Sequence

When you run a program, it can seem like one mysterious action happens all at once. At the machine-language level, that is not what happens. The CPU handles a program as a sequence of instructions, one piece at a time. This article follows that sequence step by step so you can see exactly what the processor does with each piece.

Where the Program Lives

A program is stored as a sequence of machine language instructions in main memory. The instructions sit in main memory in an order, and the CPU works through that order. The diagram below shows a three-instruction program as an ordered list. The labels Instruction A, Instruction B, and Instruction C are only placeholders that make the sequence visible; the source does not describe what any particular instruction contains.

Instruction Afirst in sequenceInstruction Bsecond in sequenceInstruction Cthird in sequencefollowed byfollowed by
The learner question this answers: how does the program's instruction order map onto the storage that holds it? Each stored instruction occupies a position in order, and the CPU works through the positions in that order.

The Fetch-and-Execute Cycle

Definition

The fetch-and-execute cycle is the repeating process in which the CPU obtains one instruction from main memory, carries it out, and then obtains another instruction.

The cycle has two stages. In the fetch stage, the CPU obtains an instruction from the program stored in main memory. In the execute stage, the CPU carries out that instruction. The source states that an instruction must be fetched before the CPU carries it out, and that after execution the cycle moves on to another instruction. The cycle then repeats, so the same two stages happen again and again for as long as the program runs.

Fetch instructionobtain from main memoryExecute instructioncarry it outNext instructioncycle moves onthenthenrepeat
The learner question this answers: after the CPU finishes executing one instruction, how does the cycle return to fetch the next one? The arrow back to Fetch shows the cycle repeating.

Tracing a Three-Instruction Program

Worked example

Running a program with three stored instructions

A program is stored in main memory as three instructions in order: Instruction A, Instruction B, Instruction C. Trace the fetch and execute stages the CPU goes through.

  1. 1
    Step 1: Fetch the first instruction

    The CPU obtains Instruction A from the stored program, since it is first in the sequence.

  2. 2
    Step 2: Execute the first instruction

    The CPU carries out Instruction A. The instruction must be fetched before it is carried out.

  3. 3
    Step 3: Fetch the next instruction

    The cycle moves on, and the CPU obtains Instruction B, the next instruction in the sequence.

  4. 4
    Step 4: Execute the next instruction

    The CPU carries out Instruction B.

  5. 5
    Step 5: Repeat for the last instruction

    The CPU fetches Instruction C and then executes it, completing the cycle for each stored instruction.

Result

The CPU obtained three instructions and carried out three instructions, always fetching each one before executing it.

Notice the pattern. The fetch-and-execute pair repeats for every instruction. The CPU does not fetch all three instructions first and then execute them all. It fetches one, carries it out, and then goes on to the next.

Predicting the Order of Operations

What do you think happens?

Given three stored instructions A, B, and C, which sequence correctly shows the CPU's order of operations?

  • Fetch A, fetch B, fetch C, then execute A, B, C

  • Fetch A, execute A, fetch B, execute B, fetch C, execute C

  • Execute A, fetch A, execute B, fetch B, execute C, fetch C

Reveal answer
Answer:

Fetch A, execute A, fetch B, execute B, fetch C, execute C

The source states that an instruction must be fetched before the CPU carries it out, and that the cycle then moves on to another instruction. The CPU therefore repeats a fetch followed by an execute for each instruction in turn.

Common Mistakes About the Cycle

  • Common mistake

    Thinking a program runs as one indivisible action

    Why it fails

    At the machine-language level the CPU handles a program as a sequence of instructions, not a single step.

    Do this instead

    Describe the program as an ordered sequence that the CPU works through one instruction at a time.

  • Common mistake

    Believing the CPU fetches every instruction before executing any of them

    Why it fails

    The cycle obtains one instruction, carries it out, and then obtains another.

    Do this instead

    Pair each fetch with the execute that follows it, and repeat that pair for each instruction.

  • Common mistake

    Treating fetch and execute as the same job

    Why it fails

    Fetching obtains the instruction from main memory, while executing carries it out.

    Do this instead

    Keep the two stages separate when you trace the cycle.

  • Common mistake

    Assuming the labels Instruction A, B, and C are real instruction contents

    Why it fails

    The labels only make the sequence visible. The important idea is the order of operations.

    Do this instead

    Focus on the order of fetch and execute rather than on what any particular label contains.

Several Cores Working at Once

A single CPU works through instructions one after another in the cycle described above. A processor with multiple CPU cores changes this picture: the source states that multiple cores can execute several instructions simultaneously. This means the cycle can be running in more than one place at the same time.

Practice Tracing the Cycle

EASY

A program is stored in main memory as four instructions in order: W, X, Y, Z. Write out the full sequence of fetch and execute stages the CPU goes through on a single core, one stage per line.

Hints
  • Start with the fetch of the first instruction, W.
  • After each execute, the cycle moves on to the next instruction in the sequence.
  • There should be eight stages in total, alternating fetch and execute.

Key Takeaways

  • A program is stored as a sequence of machine language instructions in main memory.
  • The fetch-and-execute cycle is the repeating process in which the CPU obtains one instruction and then carries it out.
  • Fetching and executing are distinct stages, and each instruction must be fetched before it is carried out.
  • Multiple CPU cores can execute several instructions simultaneously, while each core still works through the cycle.