Machine Language Instructions
A program is stored as a sequence of machine language instructions in main memory.
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.
The Fetch-and-Execute Cycle
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.
Tracing a Three-Instruction Program
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.
- 1Step 1: Fetch the first instruction
The CPU obtains Instruction A from the stored program, since it is first in the sequence.
- 2Step 2: Execute the first instruction
The CPU carries out Instruction A. The instruction must be fetched before it is carried out.
- 3Step 3: Fetch the next instruction
The cycle moves on, and the CPU obtains Instruction B, the next instruction in the sequence.
- 4Step 4: Execute the next instruction
The CPU carries out Instruction B.
- 5Step 5: Repeat for the last instruction
The CPU fetches Instruction C and then executes it, completing the cycle for each stored instruction.
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?
Reveal 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 failsAt the machine-language level the CPU handles a program as a sequence of instructions, not a single step.
Do this insteadDescribe 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 failsThe cycle obtains one instruction, carries it out, and then obtains another.
Do this insteadPair 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 failsFetching obtains the instruction from main memory, while executing carries it out.
Do this insteadKeep 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 failsThe labels only make the sequence visible. The important idea is the order of operations.
Do this insteadFocus 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
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.