A program executes ADD R1, R2 followed by JMP 200. Order what the CPU does, from the start of the first instruction to the moment the jump takes effect.
- The PC's address is used to read the ADD instruction from memory.
- The PC advances to point at the JMP instruction.
- The JMP is fetched, and the PC advances again to the instruction after it.
- ADD is decoded and the ALU adds the two registers.
- JMP executes and writes 200 into the PC, discarding that advance.
Hints
- The PC advances during fetch, before the instruction is understood.
- The last step is what makes a jump a jump.
Show the answer
- The PC's address is used to read the ADD instruction from memory.
- The PC advances to point at the JMP instruction.
- ADD is decoded and the ALU adds the two registers.
- The JMP is fetched, and the PC advances again to the instruction after it.
- JMP executes and writes 200 into the PC, discarding that advance.
The sequence shows why incrementing the PC during fetch costs nothing. After the ADD is fetched, the PC advances to the JMP - which is exactly right, and no extra work was needed to arrange it.
The JMP is then fetched and the PC advances again, to whatever follows it. That advance is immediately discarded when the jump executes and writes 200 into the PC.
So a jump is not special sequencing hardware; it is one register write that happens to overwrite a value the fetch phase had speculatively set. Everything the CPU does about control flow reduces to what number ends up in the PC when the cycle comes round again - which is also why a corrupted PC is so catastrophic, and why buffer overflows that overwrite a return address are able to redirect execution anywhere.
Practise The Instruction Execution Cycle
The app has 8 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.
More questions on The Instruction Execution Cycle
- A jump instruction sits at address 40 and jumps to address 80. Instructions are 4 bytes long. Order what…
- The program counter is incremented during the FETCH phase, before the instruction has even been decoded. Why…
- A buffer overflow that overwrites a return address can make a program execute the attacker's code. Which…