AS Level · Topic 4.2
A-Level Computer Science 9618 / Topic 4 / AS

One line at a time.

Tracing questions give you a short assembly program and the starting contents of memory, and ask you to complete a trace table. There is no shortcut: you act as the processor, carrying out each instruction exactly. Learn a method that keeps every loop, compare and jump under control.

The key idea

To trace a program, carry out one instruction at a time and record each change in a trace table, with a column for the ACC, any memory addresses that change, the IX if it is used, and the output. A CMP or CMI only sets the result of the comparison; the next JPE (jump if True) or JPN (jump if False) uses it. JMP always jumps. OUT outputs the character whose ASCII value is in the ACC.

a loop: JPN jumps back until the counter is 0 100 LDD 200101 ADD 201102 STO 200 103 LDD 202104 DEC ACC105 STO 202 106 CMP #0107 JPN 100108 LDD 200 109 END 200 | 0 total 201 | 6 number 202 | 3 counter each pass adds 6 to the total and takes 1 off the counter a trace table records every change, one instruction at a time
Fig. 1: a program that multiplies 6 by 3 by repeated addition
Section 01

A loop, traced.

Run the "Loop with OUT" and "Indexed addressing: total" programs in the lab and watch the trace table fill up row by row. Then follow the program in Fig. 1 below.

100  LDD 200
101  ADD 201
102  STO 200
103  LDD 202
104  DEC ACC
105  STO 202
106  CMP #0
107  JPN 100
108  LDD 200
109  END

Memory at the start: address 200 holds 0, address 201 holds 6, address 202 holds 3.

Instruction addressACC200202Note
03start
1000
1016
1026
1033
1042
1052
106, 1072 ≠ 0: False, so JPN jumps to 100
1006
10112
10212
1032
1041
1051
106, 1071 ≠ 0: False, so JPN jumps to 100
10012
10118
10218
1031
1040
1050
106, 1070 = 0: True, so JPN does not jump
10818
109END
Examiner trap

JPN jumps when the compare was False. Reading it as "jump if not zero" works here only because the compare is with #0; always check what was actually compared.

Section 02

Characters in, characters out.

IN and OUT work with ASCII codes, not with numbers as you would type them. Keep an ASCII reminder beside you: 48 is the character 0, 65 is A and 97 is a.

100  LDR #0
101  LDX 200
102  CMP #0
103  JPE 107
104  OUT
105  INC IX
106  JMP 101
107  END

Memory: 200 holds 72, 201 holds 73, 202 holds 33, 203 holds 0. (ASCII: 72 is H, 73 is I, 33 is !)

Worked example Output a stored string
Trace the program above and state the output.
  1. LDR #0 sets IX to 0. LDX 200 loads the contents of 200 + 0, so ACC = 72.
  2. CMP #0 is False, so JPE does not jump. OUT outputs H. INC IX makes IX = 1 and JMP returns to 101.
  3. LDX 200 now uses 201, so ACC = 73: OUT outputs I and IX = 2. Next, 202 gives ACC = 33: OUT outputs ! and IX = 3.
  4. LDX 200 uses 203, so ACC = 0. CMP #0 is True, so JPE jumps to 107 and the program ends.
Answer: HI! (and IX finishes at 3)
Your turn
If address 202 held 0 instead of 33, what would the output be, and what would IX hold at the end?
Examiner trap

OUT does not print the number in the ACC. With 50 in the ACC, OUT outputs the character 2, not 50.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What does STO 200 change?

Quick check+10 XP

In the lab program "Loop with OUT", the ACC holds 51 when OUT runs. What is output?

Quick check+10 XP

After CMP #0 with ACC = 4, does JPN 100 jump?

Quick check+10 XP

In the Fig. 1 program, how many times is ADD 201 executed?

Section 03

A tracing method.

Examiners award marks for each correct column of values in the right order. These habits avoid the common slips.

Your turn
Memory holds 300: 5 and 301: 2. Trace: LDD 300, SUB 301, STO 302, SUB #1, CMP 301, JPE 108 (where 108 is END). Give the final ACC and the contents of 302, and state whether the jump is taken.
Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Memory: 200 holds 4. Trace LDD 200, INC ACC, STO 200, ADD 200. What is the final ACC?

Exam style+20 XP

Address 204 holds 25. Trace LDM #4, MOV IX, LDX 200. What is in the ACC?

Exam style+20 XP

The ACC holds 65. Which instruction pair outputs B?

Exam style+20 XP

A loop counter starts at 5, is decreased by 1 each pass and the loop ends with CMP #0 and JPN. How many passes are made?

Skill unlocked

Tracing assembly language programs, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Tracing 9618 assembly programs with loops, compares, jumps, indexed addressing, IN and OUT.