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
INandOUTusing 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 |
A Reliable Six-Step Trace Method
Record starting register values, memory contents and input data.
Use PC or the stated starting address.
Apply immediate, direct, indirect, indexed or relative addressing.
Change only the affected register, memory cell, flag or output.
Continue sequentially or follow a jump target.
Add one trace row, then repeat until END.
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 |
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.
| Address | Instruction | Purpose |
|---|---|---|
| 500 | IN | Read M; ACC becomes 77. |
| 501 | STO FIRST | Save 77 at address 730. |
| 502 | IN | Read A; ACC becomes 65. |
| 503 | ADD #1 | ACC becomes 66. |
| 504 | OUT | Display character B. |
| 505 | LDD FIRST | Reload the saved value 77. |
| 506 | OUT | Display character M. |
| 507 | END | Return control to the operating system. |
| 730 | FIRST: 0 | Reserved data location. |
Completed trace
| Step | PC before | Instruction | ACC after | FIRST after | Output so far | PC after |
|---|---|---|---|---|---|---|
| 1 | 500 | IN | 77 | 0 | — | 501 |
| 2 | 501 | STO FIRST | 77 | 77 | — | 502 |
| 3 | 502 | IN | 65 | 77 | — | 503 |
| 4 | 503 | ADD #1 | 66 | 77 | — | 504 |
| 5 | 504 | OUT | 66 | 77 | B | 505 |
| 6 | 505 | LDD FIRST | 77 | 77 | B | 506 |
| 7 | 506 | OUT | 77 | 77 | BM | 507 |
| 8 | 507 | END | 77 | 77 | BM | — |
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.
| Address | Instruction | Role |
|---|---|---|
| 600 | LDM #0 | Initialise ACC. |
| 601 | STO COUNT | Initialise COUNT. |
| 602 | LOOP: LDD COUNT | Load the current count. |
| 603 | INC ACC | Increase the count. |
| 604 | STO COUNT | Save the new count. |
| 605 | CMP #3 | Set Equal flag according to ACC = 3. |
| 606 | JPN LOOP | Repeat while the comparison is false. |
| 607 | LDD COUNT | Reload 3. |
| 608 | ADD #48 | Form the ASCII code for character 3. |
| 609 | OUT | Display 3. |
| 610 | END | Stop. |
| 740 | COUNT: 0 | Data location. |
The decision points
| COUNT after increment | CMP #3 |
Equal flag | JPN LOOP |
Next PC |
|---|---|---|---|---|
| 1 | 1 = 3 is false | 0 | Taken | 602 |
| 2 | 2 = 3 is false | 0 | Taken | 602 |
| 3 | 3 = 3 is true | 1 | Not taken | 607 |
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:
| Address | Content |
|---|---|
| 820 | 17 |
| 821 | 824 |
| 822 | 9 |
| 823 | 42 |
| 824 | 31 |
| 825 | 68 |
| 826 | 55 |
| 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 |
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 |
|---|---|---|
| 65 | A | ASCII code 65 represents A. |
| 51 | 3 | ASCII code 51 represents character 3. |
| 10 | Not the text “10” | OUT interprets one character code, not a multi-digit denary value. |
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.
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
JPEorJPNas 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:
| Address | Content |
|---|---|
| 950 | 954 |
| 951 | 14 |
| 952 | 33 |
| 953 | 27 |
| 954 | 61 |
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. |