A-Level Computer Science / Unit 4: CPU Operation and Low-Level Processing

4.2.5 Tracing Assembly Programs

🔒 Lesson slides are available to signed-in users. Sign in

4.2.5 Tracing Assembly Programs

Tracing means following a program one executed instruction at a time and recording the resulting changes. A careful trace shows not only the final answer, but also how the accumulator, index register, selected memory locations, comparison result, output and Program Counter develop during execution.

By the end of this section, you should be able to:

  • Prepare an appropriate initial state before tracing begins.
  • Follow a program in the order determined by the Program Counter.
  • Record changes to ACC, IX, memory, the equality result and output.
  • Trace programs containing direct, indirect and indexed addressing.
  • Decide whether a conditional jump is taken.
  • Trace a loop until its stopping condition is reached.
  • Interpret IN and OUT using character codes.
  • Explain the overall purpose and result of a short assembly program.

What This Page Covers

The previous page explained the meaning of individual instructions. This page combines those instructions into complete programs and follows the changing processor state.

Content Location in the syllabus-based sequence
Meaning of each mnemonic 4.2.4 Understanding the Assembly Instruction Set
Tracing complete programs 4.2.5 Tracing Assembly Programs
Register transfers in the fetch–execute cycle 4.1.6 The Fetch–Decode–Execute Cycle
Carry, signed overflow and binary arithmetic Earlier number-representation and CPU-register sections
Structural change: The old page combined program tracing with register transfer notation and signed-overflow examples. Those ideas are useful, but they do not define the tracing requirement in 4.2. This page therefore concentrates on executing and recording complete assembly programs.

A Reliable Six-Step Trace Method

1 Read the initial state

Record starting register values, memory contents and input data.

2 Locate the next instruction

Use PC or the stated starting address.

3 Interpret the operand

Apply immediate, direct, indirect, indexed or relative addressing.

4 Execute one instruction

Change only the affected register, memory cell, flag or output.

5 Determine the next PC

Continue sequentially or follow a jump target.

6 Record the new state

Add one trace row, then repeat until END.

Dry run: a manual execution of a program in which changes to selected values are recorded step by step.

Choosing Useful Trace-Table Columns

Include a column only when it helps to show the program's behaviour. State clearly whether the PC column contains the address before or after the instruction.

Column Include it when... Typical entries
PC before Execution order matters, especially with jumps or loops. 500, 501, 502...
Instruction You want each row to show exactly what was executed. LDI POINTER
ACC The program loads, calculates, compares or outputs values. 77, 66, 3...
IX The program uses LDR, MOV IX, INC IX, DEC IX or LDX. 0, 1, 2...
Selected memory The program stores or repeatedly reads data locations. COUNT = 2
Equal flag A compare is followed by JPE or JPN. 0 or 1
Output OUT is executed. B, BM
PC after You need to show whether a jump was taken. 602 or 607
Consistency matters: The worked tables on this page record the complete selected state after every executed instruction. An assessment may instead ask you to enter only changed values, so follow the headings and instructions provided.

Worked Example 1: A Straight-Line Program

The program receives two characters. The first character is saved. The second character's code is increased by one and output, after which the original first character is output. The input queue is M, then A.

AddressInstructionPurpose
500INRead M; ACC becomes 77.
501STO FIRSTSave 77 at address 730.
502INRead A; ACC becomes 65.
503ADD #1ACC becomes 66.
504OUTDisplay character B.
505LDD FIRSTReload the saved value 77.
506OUTDisplay character M.
507ENDReturn control to the operating system.
730FIRST: 0Reserved data location.

Completed trace

Step PC before Instruction ACC after FIRST after Output so far PC after
1500IN770501
2501STO FIRST7777502
3502IN6577503
4503ADD #16677504
5504OUT6677B505
6505LDD FIRST7777B506
7506OUT7777BM507
8507END7777BM
Program purpose: output the character after the second input, followed by the unchanged first input. For inputs M and A, the output is BM.

Worked Example 2: Tracing a Loop

This program counts from 0 to 3. The comparison determines whether the loop repeats. When the count reaches 3, the value is converted to the character code for 3 and displayed.

AddressInstructionRole
600LDM #0Initialise ACC.
601STO COUNTInitialise COUNT.
602LOOP: LDD COUNTLoad the current count.
603INC ACCIncrease the count.
604STO COUNTSave the new count.
605CMP #3Set Equal flag according to ACC = 3.
606JPN LOOPRepeat while the comparison is false.
607LDD COUNTReload 3.
608ADD #48Form the ASCII code for character 3.
609OUTDisplay 3.
610ENDStop.
740COUNT: 0Data location.

The decision points

COUNT after increment CMP #3 Equal flag JPN LOOP Next PC
11 = 3 is false0Taken602
22 = 3 is false0Taken602
33 = 3 is true1Not taken607
Common mistake: JPN does not mean “jump when ACC is negative”. In this teaching instruction set it means jump when the previous equality comparison was false.

Tracing Different Addressing Modes

When an instruction uses memory, calculate the effective address before changing the trace table.

Assume IX = 4 and the following memory contents:

AddressContent
82017
821824
8229
82342
82431
82568
82655
Instruction Addressing path ACC after
LDD 823 Direct: read memory[823] 42
LDI 821 Indirect: memory[821] = 824, then memory[824] 31
LDX 822 Indexed: effective address = 822 + IX = 826 55
Show the path: For indirect and indexed instructions, write the intermediate address calculation beside your trace before entering the final ACC value.

Tracing Input, Output and Character Codes

In this instruction set, IN places the code of a typed character in ACC, while OUT treats the content of ACC as a character code.

ACC before OUT Displayed character Reason
65AASCII code 65 represents A.
513ASCII code 51 represents character 3.
10Not the text “10”OUT interprets one character code, not a multi-digit denary value.
Common misconception: If ACC contains 51, OUT displays the character 3, not the text 51.

Interactive: Trace Table Builder

Select a program and execute one instruction at a time. The widget records PC before and after, processor state, memory changes and output. Use Previous to revisit a decision and Reset to start the dry run again.

Program

    Remaining input M, A

    Current State

    PC500
    ACC
    IX0
    Equal flag
    Output
    ProgramRunning

    Watched memory

    Symbol / addressValue

    Step 0

    Ready

    Select Next instruction to begin the dry run.

    Generated trace table

    Step PC before Instruction ACC after IX after Equal Memory change Output PC after

    Common Mistakes and Misconceptions

    • Starting with assumed register or memory values instead of the values given.
    • Following the printed line order after a jump rather than following PC.
    • Changing ACC during CMP; the equality result changes instead.
    • Treating JPE or JPN as if it performs the comparison itself.
    • Using the address as the data value in direct addressing.
    • Stopping an indirect load after finding the pointer rather than following it.
    • Adding IX to the stored content instead of to the address operand.
    • Forgetting that a loop may execute the same instruction address several times.
    • Writing the denary content of ACC as output instead of converting it to a character.
    • Continuing after END.

    Exam Tips

    Annotate the program first

    Resolve labels, identify watched memory locations and mark where each conditional jump can lead before filling the trace table.

    One row means one executed instruction

    Do not add a row for a skipped instruction. A repeated loop instruction should appear again each time it is executed.

    Separate comparison from branching

    First record the result of CMP or CMI. On the next row, use that result to decide whether JPE or JPN changes PC.

    Finish with the program's purpose

    After tracing, describe the inputs, processing and outputs in plain language. This often reveals a trace error that a final number alone would not show.

    Practice

    Practice 1: Straight-line trace

    900  LDM #8
    901  STO VALUE
    902  LDM #5
    903  ADD VALUE
    904  STO RESULT
    905  END
    
    VALUE:  0
    RESULT: 0

    Construct a trace table containing PC before, ACC, VALUE, RESULT and PC after.

    Practice 2: Conditional route

    920  LDM #12
    921  CMP LIMIT
    922  JPE MATCH
    923  LDM #0
    924  JMP FINISH
    925  MATCH: LDM #1
    926  FINISH: STO ANSWER
    927  END
    
    LIMIT:  12
    ANSWER: 0

    Trace the program and explain why address 923 is or is not executed.

    Practice 3: Addressing challenge

    Assume IX = 2 and:

    AddressContent
    950954
    95114
    95233
    95327
    95461

    Find ACC after each independent instruction: LDD 953, LDI 950 and LDX 951.

    Review

    Question Strong answer should include
    What is a program trace? A step-by-step manual execution that records selected state changes.
    Why include PC? To show the actual execution order and whether a jump is taken.
    What changes after CMP? The equality result, while ACC remains unchanged.
    How do you trace indirect addressing? Read the pointer, then use that address to obtain the final value.
    When does tracing stop? When END executes or when the requested trace point is reached.
    Final check: Can another student reproduce the same execution order and final output from your table without rereading the whole program?