All subjects · Computer Science · WCP03 Advanced Principles of Computer Science

Tracing and writing assembly programs

Edexcel IAL Computer Science · WCP03 Advanced Principles of Computer Science

Exam questions give you a short assembly program and ask you to trace it in a table and say what it does, or to write a few lines yourself. The skill is careful, line-by-line bookkeeping.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

How to trace

1. Draw a column for each register and memory location used (and the comparison result if it helps).

2. Work through one instruction at a time, writing a new value only when it changes.

3. At a CMP, note the comparison; at a conditional branch, decide whether to jump.

4. Keep going until HALT.

5. Then state the purpose in plain words: 'multiplies R0 by R1 using repeated addition, result in R3'.

Worked trace: multiplication by repeated addition

      MOV R3, #0      ; R3 holds the running total
loop: CMP R1, #0      ; any more additions?
      BEQ done
      ADD R3, R3, R0  ; add R0 to the total
      SUB R1, R1, #1  ; one fewer to do
      B loop
done: STR R3, 100     ; store result in memory address 100
      HALT

With R0 = 6 and R1 = 4:

StepR0R1R3Memory 100
Start64
MOV0
Loop 136
Loop 2212
Loop 3118
Loop 4024
CMP R1, #0 equal: BEQ done24

Purpose: multiplies R0 by R1 by repeated addition and stores the product in address 100

Selection in assembly

There is no IF statement: a CMP followed by a conditional branch jumps over code.

      LDR R0, 100
      LDR R1, 101
      CMP R0, R1
      BGT first
      MOV R2, R1
      B end
first: MOV R2, R0
end:  STR R2, 102
      HALT

If R0 > R1 the program jumps to first and copies R0; otherwise it copies R1 and jumps over first. It stores the larger of the two values in address 102.

Writing loops

A count-controlled loop pattern:

      MOV R1, #10     ; counter
loop: ...             ; body
      SUB R1, R1, #1
      CMP R1, #0
      BNE loop
      HALT

Check that the loop runs the right number of times by tracing the first and last passes.

Worked example

Trace this program and state the final values of R0 and R1. ``` MOV R0, #0 MOV R1, #5 loop: ADD R0, R0, R1 SUB R1, R1, #1 CMP R1, #0 BGT loop HALT ```

  1. R0 = 0, R1 = 5
  2. Pass 1: R0 = 5, R1 = 4; 4 > 0 so branch.
  3. Pass 2: R0 = 9, R1 = 3. Pass 3: R0 = 12, R1 = 2. Pass 4: R0 = 14, R1 = 1.
  4. Pass 5: R0 = 15, R1 = 0; 0 is not greater than 0, so continue to HALT.

Answer: R0 = 15, R1 = 0: it adds 5 + 4 + 3 + 2 + 1.

Key idea: Trace one instruction at a time, recording only changes. CMP plus a conditional branch makes selection and loops. Always finish by stating the program's purpose.

Check yourself

  1. The multiplication program runs with R0 = 7 and R1 = 3. What value is stored in memory address 100?
    Show answer

    21. It adds 7 three times: 21.

  2. The multiplication program runs with R0 = 9 and R1 = 5. How many times is the ADD instruction executed?
    Show answer

    5. The loop body runs once for each unit in R1: 5 times.

  3. The summing program (MOV R0, #0; MOV R1, #5; loop adds R1 to R0 and decrements R1 while R1 > 0) is changed to start with MOV R1, #8. What is the final value of R0?
    Show answer

    36. 8 + 7 + ... + 1 = 36.

  4. The maximum program runs with 17 in address 100 and 42 in address 101. What value ends up in address 102?
    Show answer

    42. 42 is larger, so BGT does not branch; R2 = R1 = 42 is stored.

  5. The maximum program runs with 30 in both addresses 100 and 101. What value is stored in address 102?
    Show answer

    30. CMP finds them equal, so BGT does not branch; R2 = R1 = 30.

  6. What is the purpose of the program: LDR R0, 60 / ADD R0, R0, #1 / STR R0, 60 / HALT?
    • It stores 61 in R0
    • It increments the value in memory address 60 by 1
    • It copies address 60 to address 61
    • It compares R0 with 60
    Show answer

    It increments the value in memory address 60 by 1. Load, add 1, store back.

  7. A loop ends with SUB R1, R1, #1 / CMP R1, #0 / BNE loop. R1 starts at 4. How many times does the loop body run?
    • 3
    • 5
    • It never stops
    • 4
    Show answer

    4. R1 goes 4, 3, 2, 1, 0; the branch is taken while R1 is not 0, so the body runs 4 times.

  8. What does HALT do?
    • Pauses until a key is pressed
    • Clears all registers
    • Stops the execution of the program
    • Jumps to the start of the program
    Show answer

    Stops the execution of the program. HALT ends execution.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.