Learn
A1.1.5 | FETCH, DECODE AND EXECUTE
01 | HOW A PROGRAM BECOMES PROCESSOR ACTIONS
A program may tell a computer to add a score, compare two values or save a result. The CPU does not perform the whole program as one indivisible action. It processes machine instructions, obtaining an instruction, interpreting it and carrying out the required operation.
These stages are called fetch, decode and execute. Fetch obtains the next instruction. Decode determines what it means. Execute carries out its operation. The CPU then proceeds to another instruction, following the program’s control flow.
A useful way to understand the cycle is to follow the information: which address is selected, which value is transferred and which component uses it. Knowing the names of the registers is helpful, but understanding their changing contents is the real goal.
This lesson uses a deliberately simple processor model. It shows a clear logical sequence rather than the exact clock-by-clock behaviour of every modern CPU. Pipelining and overlapping work are considered in the next HL lesson.
02 | THREE STAGES, THREE DIFFERENT QUESTIONS
1 | FETCH
Which instruction is next?
Use its address to obtain the instruction from memory and retain it in IR.
2 | DECODE
What does this instruction mean?
The CU interprets its operation and any operand information.
3 | EXECUTE
What action is required?
Carry out the operation, which may include calculation or a memory transfer.
Then repeat: fetch the next instruction indicated by PC. A branch can change that address; a halt or other control condition can stop or alter execution.
The same broad stages apply to different instructions, but the execute stage is not always the same. An addition calculates a value; a load obtains a value; a store writes a value to memory.
Do not confuse fetching an instruction with reading the data it will use. An instruction may first be fetched from one address, then direct a later read from a different address during execution.
03 | THE COMPONENTS YOU WILL FOLLOW
| Component | Role during this lesson |
|---|---|
| PC: program counter | Holds the address of the next instruction to fetch. |
| MAR: memory address register | Holds the address selected for a memory access. |
| MDR: memory data register | Holds a value being transferred to or from memory. It can contain an instruction or ordinary data. |
| IR: instruction register | Retains the instruction currently being processed. |
| AC: accumulator | Holds a working data value or operation result. |
| CU: control unit | Decodes the instruction and coordinates transfers and operations with control signals. |
| ALU: arithmetic logic unit | Performs the arithmetic or logical operation when required. |
Main memory stores instructions and data for this example. Cache details are omitted so the register transfers remain visible. A real processor can obtain instruction or data values through its memory hierarchy.
This page uses IR and AC, matching the IB terminology. You may see CIR and ACC in other teaching resources for the corresponding instruction-register and accumulator roles.
04 | ADDRESS AND CONTENTS: THE DISTINCTION THAT MATTERS
A memory address identifies a location. Its contents are what is stored there. If memory[20] = 7, the address is 20 and the stored value is 7. The notation memory[20] means “the contents at address 20”.
MAR selects the location; MDR carries its transferred contents. Copying 20 into MAR does not put 7 into MAR. A read must obtain 7 and place that value in MDR.
Similarly, PC = 100 means that the next instruction is stored at address 100. It does not mean that 100 is the instruction itself or the value to add.
| Address | Initial contents | Meaning |
|---|---|---|
| 100 | LOAD 20 | Load the value stored at address 20 into AC. |
| 101 | ADD 21 | Add the value stored at address 21 to AC. |
| 102 | STORE 22 | Write AC’s value to address 22. |
| 20 | 7 | First score value. |
| 21 | 5 | Second score value. |
| 22 | 0 | Destination for the calculated result. |
LOAD, ADD and STORE are readable names for this teaching instruction set. Actual machine instructions are binary encodings. The opcode specifies an operation; an operand supplies information such as an address or value, depending on the instruction’s format. Here, all three operands are memory addresses.
05 | FETCH: OBTAIN THE INSTRUCTION, NOT ITS SCORE VALUE
Start with PC = 100. The CPU needs the instruction at address 100. The fetch stage first copies PC into MAR so the memory system can select that address.
The address bus carries 100 from CPU towards memory. The CU issues a read control signal. The instruction stored at address 100 returns along the data bus into MDR. In our readable notation, MDR now contains LOAD 20.
The instruction is copied from MDR into IR, where the CU can interpret it. PC advances to the following instruction address. Our model stores one instruction per numbered location, so PC becomes 101.
FETCH | LOGICAL REGISTER TRANSFERS
MAR ← PC MDR ← memory[MAR] IR ← MDR PC ← next sequential instruction address
The arrow means “copy the value on the right into the location on the left”. Copying does not automatically erase the source. After IR receives LOAD 20, MDR can still contain it until another transfer overwrites MDR.
The PC update is shown at a convenient point in this logical fetch sequence. Actual processors vary in timing and instruction size; “next instruction address” does not mean every physical CPU always adds one byte.
06 | DECODE: THE CU INTERPRETS THE INSTRUCTION
IR now holds LOAD 20. The control unit interprets the opcode to determine the required operation and interprets the operand according to that instruction’s rules.
For this instruction, LOAD means copy a value into AC. The operand 20 identifies the memory location containing that value. The CU therefore prepares the control actions for a read from location 20 and a transfer into AC.
Decode does not itself mean “do the addition”. Nor does the CU need to fetch the same instruction again from RAM just to determine its meaning: IR already retains it.
Keeping the instruction in IR matters because MDR will soon be reused for the value 7. The CPU must still know which instruction it is carrying out while other transfers occur.
07 | EXECUTE LOAD: A SECOND READ WITH A DIFFERENT PURPOSE
The CPU has fetched the instruction from location 100. Now it performs that instruction by reading the data from location 20. These are two different reads in different parts of the cycle.
MAR receives the operand address 20. The address bus selects that location and a read signal requests its contents. The data bus returns 7 to MDR. AC then receives 7 from MDR.
At this point PC = 101, IR = LOAD 20, MAR = 20, MDR = 7 and AC = 7. PC already identifies the next instruction while IR still retains the one just executed. No score has yet been added to another score.
EXECUTE LOAD 20 | THIS MODEL
MAR ← 20 MDR ← memory[20] # the stored value is 7 AC ← MDR
The execute stage here copies a value. It is not an ALU addition. Different instructions require different actions, so do not describe every execute stage as “the CPU does a calculation”.
08 | NEXT CYCLE: ADD THE SECOND VALUE
The next fetch starts from PC = 101. The CPU selects address 101, reads ADD 21 into MDR, copies it into IR and advances PC to 102. The CU then decodes the new instruction.
During execution, MAR receives 21 and a memory read brings the stored value 5 into MDR. The ALU adds it to the current accumulator value: 7 + 5 = 12. AC receives the result.
This instruction uses the contents of address 21, not the literal number 21. If you obtained 28, you added the address instead of the stored score. If you obtained 5, you loaded the second value without adding it to the first.
EXECUTE ADD 21 | THIS MODEL
MAR ← 21 MDR ← memory[21] # the stored value is 5 AC ← AC + MDR # the ALU computes 7 + 5 = 12
The CU coordinates the operation; the ALU performs the addition. AC now contains 12, but memory[22] is still 0. Computing a result and writing it to a memory location are separate actions.
09 | NEXT CYCLE: STORE THE RESULT
The third fetch starts from PC = 102. STORE 22 is read through MDR into IR and PC advances to 103. The CU interprets STORE as a write operation whose destination is address 22.
MAR receives destination address 22. MDR receives the value 12 from AC. The address bus selects 22; the data bus carries 12 towards memory; a write control signal causes memory[22] to receive it.
The operation does not add 22 to AC and does not read the old value 0 as its required result. The operand identifies where the accumulator value should be stored.
EXECUTE STORE 22 | THIS MODEL
MAR ← 22 MDR ← AC # supply the value 12 memory[22] ← MDR # write 12 into the destination
At the end, AC remains 12 and memory[22] is now 12. This is a main-memory write in the model, not a saved file on non-volatile storage. Our trace stops after the third instruction; it does not define the instruction at address 103.
10 | TRACE ALL THREE INSTRUCTIONS
Use Next step to follow the transfers. Register boxes turn pink when that step changes their contents. The memory table highlights the address currently held in MAR; the bus panel explains the actual activity at the current step.
Read the explanation before advancing. During the first fetch, MDR contains an instruction. During execution of LOAD, it contains 7. IR preserves the instruction as MDR is reused.
This is a teaching model with one instruction per address, read-only source scores and a single destination write. It does not model caches, binary instruction fields, clock timing, interrupts or overlapping execution.
| Address | Contents | Purpose |
|---|
Previous step replays earlier model state; it does not run a real CPU backwards. Reset restores the original registers and memory.
11 | WHICH BUS IS DOING WHAT?
| Activity | Address bus | Data bus | Control bus |
|---|---|---|---|
| Fetch LOAD 20 from address 100 | CPU supplies 100. | Instruction travels from memory to CPU. | CU requests a read. |
| Read the first score | CPU supplies 20. | Value 7 travels from memory to CPU. | CU requests a read. |
| Read the second score | CPU supplies 21. | Value 5 travels from memory to CPU. | CU requests a read. |
| Write the result | CPU supplies 22. | Value 12 travels from CPU to memory. | CU requests a write. |
The data bus carries instruction values as well as ordinary data. The address bus identifies the location involved in the access. The control bus coordinates what operation should occur and can also carry status signals.
A register-to-register copy inside the CPU does not mean the value must travel out to main memory and back. Likewise, an ALU addition is an internal processing action. Separate internal transfers from the external memory interactions shown by the three-bus model.
12 | A COMPACT END-OF-STAGE TRACE
| Instruction | After fetch | After decode | After execute |
|---|---|---|---|
| LOAD 20 | IR = LOAD 20; PC = 101. | CU identifies load from address 20. | MAR = 20; MDR = 7; AC = 7. |
| ADD 21 | IR = ADD 21; PC = 102. | CU identifies add from address 21. | MAR = 21; MDR = 5; AC = 12. |
| STORE 22 | IR = STORE 22; PC = 103. | CU identifies store to address 22. | MAR = 22; MDR = 12; AC = 12; memory[22] = 12. |
Notice how the same MDR has different contents at different points. A correct answer must say when a value is being described. “MDR holds 5” is correct after the second score read, but not after the final store preparation.
In our example, the PC advances sequentially. In a program with a branch, execution can replace the next instruction address so the following fetch starts elsewhere. The cycle still needs to obtain and interpret the instruction at that selected address.
13 | AVOID THE USUAL TRACE ERRORS
| Mistake | What to check |
|---|---|
| MAR and MDR contain the same thing. | MAR selects where; MDR carries the transferred contents. |
| Fetching LOAD 20 immediately puts 7 in AC. | Fetch obtains the instruction. Its execute stage performs the data read and load. |
| Decode performs every calculation. | CU interprets the instruction; execute carries out its operation. |
| PC holds the current instruction. | IR holds the instruction; PC holds the next instruction address. |
| ADD 21 adds the literal number 21. | In this instruction set, it adds memory[21], which contains 5. |
| The result is automatically saved when AC changes. | A store operation is needed to write the result to the chosen memory location. |
| Copying a register clears the original. | A copy preserves the source unless a later operation changes it. |
| Every CPU always increments PC by one byte. | The next address depends on the instruction format and control flow. |
14 | DESCRIBE THE SEQUENCE CLEARLY
For describe the fetch, decode and execute cycle, give an ordered account with named components. For fetch, connect PC, MAR, memory, MDR and IR. For decode, name the CU and the instruction held in IR. For execute, explain the operation required by that instruction.
A useful fetch description is: “The PC supplies the next instruction address to MAR. A memory read transfers the instruction via the data bus into MDR, then into IR. The PC is advanced for the next sequential instruction.”
Then continue: “The CU decodes the instruction in IR and coordinates its execution. This may involve reading an operand from memory, performing an ALU operation or writing a result, depending on the instruction.”
Practise with different source scores, such as 9 and 4. Keep their addresses the same and predict the new register and destination values before using the Questions tab. Changing the contents is different from changing which location an instruction names.