Worked solution from the System Software Paper 3 Advanced Theory booklet, Part 2 of 2.
Optimisation removes redundant code, simplifies expressions or reorders the code. [1]
The aim is to minimise storage size, memory use, power use or execution time (CPU time). [1]
Work in brackets first: (6 + 12) becomes 6 12 +. [1]
(16 – 10) becomes 16 10 –, then divide the two results: 16 10 – /. [1]
Multiply by 18: put 18 and * at the end. [1]
Write: 6 12 + 16 10 – / 18 *
Push each value. An operator takes the top two items, works out the answer and pushes it back. a = 4, b = 12, c = 24, d = 6.
| Step | Item read | Stack (bottom → top) |
|---|---|---|
| 1 | c | 24 |
| 2 | a | 24, 4 |
| 3 | - | 20 |
| 4 | b | 20, 12 |
| 5 | d | 20, 12, 6 |
| 6 | + | 20, 18 |
| 7 | * | 360 |
| 8 | b | 360, 12 |
| 9 | c | 360, 12, 24 |
| 10 | + | 360, 36 |
| 11 | / | 10 |
c, a pushed, then - gives 20: stack 24 → 24, 4 → 20 [1]
b, d pushed, then + gives 18: stack 20, 12 → 20, 12, 6 → 20, 18 [1]
* gives the interim total 360, then b and c pushed and + gives 36: stack 360 → 360, 12 → 360, 12, 24 → 360, 36 [1]
/ gives the final total 10 [1]
▶ Topic lesson
A lesson on this topic · by Craig'n'Dave. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.