Teachers' Portal  /  A-Level Computer Science 9618  /  Processor fundamentals  /  Tracing an assembly language program
Lesson pack · A-Level Computer Science 9618 · 4.2 · AS Level

Tracing an assembly language program

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.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Processor fundamentals
Syllabus reference
Cambridge International AS and A Level Computer Science 9618, 4.2
Level
AS Level
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 7 of 8 in Processor fundamentals; follows Addressing modes; leads into Shifts, masks and bit manipulation
Central model
Fig. 1: a program that multiplies 6 by 3 by repeated addition
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • 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.
  • Characters in, characters out: IN and OUT work with ASCII codes, not with numbers as you would type them.
  • A tracing method: Examiners award marks for each correct column of values in the right order.
Key points to leave on the board
  • Start row: copy every starting value first.
  • Record changes only: LDD, ADD, SUB, INC, DEC change the ACC; STO changes memory.
  • Compare then jump: JPE jumps if True, JPN if False, JMP always.
  • ASCII: OUT and IN use character codes (48 = 0, 65 = A).
The core visual model

One picture every idea returns to

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
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, 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. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: A loop, tracedRun the "Loop with OUT" and "Indexed addressing: total" programs in the lab and watch the trace table fill up row by row. Project tab 5 of the lab (Trace an assembly language program) and let a student drive it while the class predicts.Whole class, then pairs
13 to 21 minTeach and check: Characters in, characters outIN and OUT work with ASCII codes, not with numbers as you would type them. Model the worked example Output a stored string on the board, one line at a time. Your turn: If address 202 held 0 instead of 33, what would the output be, and what would IX hold at the end?Pairs
21 to 30 minTeach and check: A tracing methodExaminers award marks for each correct column of values in the right order. 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.Round Table
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 19 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in A tracing method, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Round Table

One sheet travels round the team. Each student adds one line of working or one idea and passes it on, so the solution is built four hands at a time and every contribution is visible.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
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.
OUT does not print the number in the ACC.With 50 in the ACC, OUT outputs the character 2, not 50.
Worksheet

Tracing an assembly language program: worksheet

6 questions, 19 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · trace[5 marks]
100  LDR #0
101  LDX 240
102  ADD 250
103  STO 250
104  INC IX
105  LDD 251
106  DEC ACC
107  STO 251
108  CMP #0
109  JPN 101
110  END

Memory: 240 holds 4, 241 holds 9, 242 holds 2, 250 holds 0, 251 holds 3. Trace the program and give the final contents of addresses 250 and 251, the IX and the ACC.

Q2 · trace[4 marks]
100  LDM #0
101  STO 300
102  IN
103  CMP #46
104  JPE 109
105  LDD 300
106  INC ACC
107  STO 300
108  JMP 102
109  LDD 300
110  ADD #48
111  OUT
112  END

The user types the characters A, B and then a full stop. (ASCII: A is 65, B is 66, the full stop is 46, the character 0 is 48.) State the final contents of address 300 and the output, and describe what the program does.

Q3 · explain[2 marks]

Explain the difference between the instructions JPE and JPN.

Q4 · trace[3 marks]

Memory: 150 holds 152, 151 holds 7, 152 holds 20. Trace LDI 150, SUB 151, STO 151, CMI 150. Give the contents of the ACC and address 151, and state whether the compare is True or False.

Q5 · write[3 marks]

Write assembly language instructions, using the 9618 instruction set, to input a character and output it twice.

Q6 · analysis[2 marks]

A loop is meant to run 4 times. The counter is stored at address 90 and starts at 4. The loop ends with LDD 90, DEC ACC, STO 90, CMP #1, JPN to the start of the loop. Explain why the loop does not run 4 times, and correct it.

Answer key and marking notes
Q1 · 5 marks
  • First pass: ACC = 4, 250 = 4, IX = 1, 251 = 2.
  • Second pass: ACC = 9 + 4 = 13, 250 = 13, IX = 2, 251 = 1.
  • Third pass: ACC = 2 + 13 = 15, 250 = 15, IX = 3, 251 = 0.
  • CMP #0 is True, so JPN does not jump; the program ends.
  • Final: 250 = 15, 251 = 0, IX = 3, ACC = 0.
Q2 · 4 marks
  • 300 holds 2 (incremented for A and for B).
  • When 46 is input, JPE jumps to 109; ACC = 2 + 48 = 50.
  • OUT outputs the character 2.
  • The program counts the characters typed before a full stop and outputs the count (as a digit).
Q3 · 2 marks
  • JPE jumps to the given address if the previous compare was True.
  • JPN jumps to the given address if the previous compare was False (otherwise the next instruction is executed).
Q4 · 3 marks
  • LDI 150: 150 holds 152, which holds 20, so ACC = 20; SUB 151: ACC = 20 - 7 = 13.
  • STO 151: address 151 = 13.
  • CMI 150: compares the ACC (13) with the contents of 152 (20), so False.
Q5 · 3 marks
  • IN
  • OUT
  • OUT (followed by END); the ACC is not changed by OUT, so the second OUT outputs the same character.
Q6 · 2 marks
  • The counter goes 3, 2, 1: the compare is True when it reaches 1, so the loop runs only 3 times.
  • Compare with 0 instead: CMP #0.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

Address 200 holds 7 and address 201 holds 3. What is in the accumulator after these instructions?

LDD 200
SUB 201
STO 202
ADD 202

  • A8
  • B4
  • C11
  • D10
Q21 mark

What is output by these instructions? (ASCII: 65 is A, so 66 is B, and so on.)

LDM #70
ADD #2
OUT

  • AH
  • B72
  • CJ
  • DF
Q31 mark

How many times is the instruction at address 103 executed when this program runs?

100  LDM #0
101  STO 300
102  LDD 300
103  ADD #3
104  STO 300
105  CMP #12
106  JPN 102
107  END

  • A4
  • B3
  • C12
  • D5
Q41 mark

The ACC holds 5 and CMP #5 has just been executed. Which statement is correct?

  • AA following JPE would jump, but a JPN would not
  • BA following JPN would jump, but a JPE would not
  • CBoth JPE and JPN would jump
  • DThe CMP has set the ACC to 0, so neither would jump
Q51 mark

Memory: 240 holds 5, 241 holds 8, 242 holds 3, 243 holds 0 and 244 holds 6. Trace the program. What are the final contents of address 250 and the index register?

100  LDR #0
101  LDM #0
102  STO 250
103  LDX 240
104  CMP #0
105  JPE 110
106  ADD 250
107  STO 250
108  INC IX
109  JMP 103
110  END

  • A250 holds 16 and IX holds 3
  • B250 holds 22 and IX holds 4
  • C250 holds 16 and IX holds 4
  • D250 holds 13 and IX holds 2
Q61 mark

The user types C, A, T and then a full stop. What is output? (ASCII: A is 65, C is 67, T is 84, the full stop is 46.)

100  IN
101  CMP #46
102  JPE 106
103  INC ACC
104  OUT
105  JMP 100
106  END

  • ADBU
  • BCAT
  • CDBU/
  • DBZS
Quiz key
Q1: A

LDD 200 gives 7, SUB 201 gives 7 − 3 = 4, STO 202 stores 4 at 202 (the ACC is still 4), and ADD 202 gives 4 + 4 = 8. 4 stops before the final ADD. 11 adds the stored 4 to the original 7, as if STO had reset the ACC, and 10 adds the contents of 201 instead of subtracting them.

Q2: A

LDM #70 loads 70 and ADD #2 makes 72; 72 − 65 = 7 letters after A, which is H. OUT outputs a character, not the number 72. J would be 74 and F is 70, the value before the addition.

Q3: A

The ACC at the compare is 3, 6, 9 and then 12. The first three compares are False, so JPN jumps back; at 12 the compare is True and the program ends. That is 4 executions of ADD #3. 3 forgets the final pass that reaches 12, 12 confuses the target value with the number of passes, and 5 counts one pass too many.

Q4: A

5 equals 5, so the compare is True. JPE jumps when the compare was True, and JPN jumps only when it was False, so JPE jumps and JPN does not. Only one of the two can act on a given result, and CMP never changes the ACC: it only records the result of the comparison.

Q5: A

IX = 0 loads 5 (total 5), IX = 1 loads 8 (total 13), IX = 2 loads 3 (total 16), then IX = 3 loads 0 from 243: the compare is True and JPE jumps to END before INC IX, so IX stays 3. 22 adds the 6 at 244, which is never reached. IX 4 increments once more after the 0 was found, and 13 with IX 2 stops one pass early.

Q6: A

Each code is increased by 1 before OUT, so C (67) becomes D, A becomes B and T (84) becomes U. When the full stop (46) is typed the compare is True and JPE jumps to END, so it is never output (its successor would be /). CAT ignores INC ACC, and BZS subtracts 1 instead of adding it.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonAddressing modes