IB COMPUTER SCIENCE | A1.1.5 Fetch, Decode and Execute Cycle

Lesson objective

Describe fetch, decode and execute using the CPU units, registers and three buses, distinguishing instruction reads, operand reads and result writes.

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

ONE INSTRUCTION AT A TIME IN THIS TEACHING MODEL

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

ComponentRole during this lesson
PC: program counterHolds the address of the next instruction to fetch.
MAR: memory address registerHolds the address selected for a memory access.
MDR: memory data registerHolds a value being transferred to or from memory. It can contain an instruction or ordinary data.
IR: instruction registerRetains the instruction currently being processed.
AC: accumulatorHolds a working data value or operation result.
CU: control unitDecodes the instruction and coordinates transfers and operations with control signals.
ALU: arithmetic logic unitPerforms 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.

AddressInitial contentsMeaning
100LOAD 20Load the value stored at address 20 into AC.
101ADD 21Add the value stored at address 21 to AC.
102STORE 22Write AC’s value to address 22.
207First score value.
215Second score value.
220Destination 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.

FETCHDECODEEXECUTE
Instruction and data memory in the model
AddressContentsPurpose

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?

ActivityAddress busData busControl bus
Fetch LOAD 20 from address 100CPU supplies 100.Instruction travels from memory to CPU.CU requests a read.
Read the first scoreCPU supplies 20.Value 7 travels from memory to CPU.CU requests a read.
Read the second scoreCPU supplies 21.Value 5 travels from memory to CPU.CU requests a read.
Write the resultCPU 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

InstructionAfter fetchAfter decodeAfter execute
LOAD 20IR = LOAD 20; PC = 101.CU identifies load from address 20.MAR = 20; MDR = 7; AC = 7.
ADD 21IR = ADD 21; PC = 102.CU identifies add from address 21.MAR = 21; MDR = 5; AC = 12.
STORE 22IR = 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

MistakeWhat 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.

MATCH THE CYCLE TERM TO ITS ROLE

Terminology

Terminology

Fetch

Obtain the next instruction from the memory system into the CPU’s instruction register.

Decode

Interpret the current instruction to determine its required operation and operand meaning.

Execute

Carry out the operation specified by the instruction.

Instruction

An encoded operation for the processor to carry out.

Opcode

The part of an instruction specifying its operation.

Operand

Information associated with an instruction, such as a value or address according to its format.

PC

Program counter: the address of the next instruction to fetch.

MAR

Memory address register: the location selected for a memory access.

MDR

Memory data register: a value being transferred to or from memory.

IR

Instruction register: retains the instruction currently being processed.

AC

Accumulator: a working value or intermediate result in this processor model.

CU

Control unit: interprets instructions and coordinates processing operations.

ALU

Arithmetic logic unit: performs arithmetic and logical operations.

Address bus

Carries the location selected for a memory access in this CPU–memory model.

Data bus

Carries transferred instructions and data values between CPU and memory.

Control bus

Carries coordination and status signals, including memory read and write controls.

Memory address

An identifier selecting a memory location.

Memory contents

The value stored at a selected location, distinct from its address.

Read operation

Obtain a value from a selected memory location.

Write operation

Place a supplied value into a selected memory location.

LOAD

In this instruction set, copy the contents of the named memory address into AC.

ADD

In this instruction set, add the contents of the named memory address to AC.

STORE

In this instruction set, write AC’s value to the named memory address.

Sequential execution

Proceed through instructions in their normal sequence unless control flow changes.

Branch

An instruction that can change the address used for the next instruction fetch.

Register transfer

Copying a value between registers or between a register and another specified location.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which sequence describes the broad instruction cycle?
2. Which transfers belong to instruction fetch in this model?
3. PC = 100 and memory[100] = LOAD 20. What is fetched?
4. Which component interprets the instruction held in IR?
5. For execute LOAD 20 with memory[20] = 7, which are correct?
6. Why is IR useful while MDR receives an operand?
7. AC = 7 and memory[21] = 5. What does ADD 21 do in this model?
8. Which describe STORE 22 after AC becomes 12?
9. Which statements about PC are correct?
10. At the end of the three-instruction example, which are correct?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Describe instruction fetch using the named registers.

2. Describe the decode stage.

3. Describe execute LOAD 20 when memory[20] contains 7.

4. Describe how fetching LOAD 20 differs from reading its operand.

5. Describe execute ADD 21 with AC = 7 and memory[21] = 5.

6. Describe how STORE 22 writes the result.

7. Describe why IR and MDR need different roles.

8. Describe the buses used during an instruction read.

9. Describe the effect of a branch on the next fetch.

10. Describe the final state and what has not been demonstrated.

TRACE IT | CHANGE THE SCORES

Keep the three instructions and addresses unchanged, but set memory[20] = 9 and memory[21] = 4. Draw a trace with PC, IR, MAR, MDR and AC after each instruction. Describe the read and write bus activity and show why memory[22] ends at 13. Do not substitute the addresses for the values stored there.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for A1.1.5 Fetch, Decode and Execute Cycle is coming soon. Complete the matching task and written questions in your exercise book.