Teachers' Portal  /  A-Level Computer Science 9618  /  Processor fundamentals  /  Assembly language and the two-pass assembler
Lesson pack · A-Level Computer Science 9618 · 4.2 · AS Level

Assembly language and the two-pass assembler

Mnemonics to machine code. Machine code is just binary, which is hard for people to write and read.

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 5 of 8 in Processor fundamentals; follows Interrupts; leads into Addressing modes
Central model
Fig. 1: pass 1 builds the symbol table; pass 2 uses it to generate the object code
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
  • One-to- one: Load a program in the lab and run it: each line is one instruction, made of an opcode and (usually) an operand.
  • The instruction set: These are the instructions you will meet in 9618 assembly questions, as printed in the syllabus.
  • Two passes: A label can be used before the line where it is defined (a forward reference).
Key points to leave on the board
  • Groups: data movement, input and output, arithmetic, unconditional and conditional jumps, compare.
  • One-to-one: one assembly instruction becomes one machine code instruction.
  • Pass 1: allocate addresses, build the symbol table (including forward references).
  • Pass 2: look up opcodes, replace labels with addresses, generate object code.
The core visual model

One picture every idea returns to

Machine code is the binary instructions a processor executes directly. Assembly language is a low-level language that uses mnemonics (such as LDD and ADD) for opcodes and labels for addresses; each assembly instruction usually translates into one machine code instruction, and both are specific to one processor's instruction set. An assembler translates it. A two-pass assembler needs two passes because of forward references: pass 1 reads the code, allocates addresses and builds a symbol table of labels and their addresses; pass 2 uses the symbol table and the opcode table to generate the object code.

the two-pass assembler sourceassemblylanguage pass 1build symbol table pass 2generate object code objectmachinecode LOOP 102COUNT 112symbol table
Fig. 1: pass 1 builds the symbol table; pass 2 uses it to generate the object code
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Mnemonics to machine code.. Machine code is just binary, which is hard for people to write and read. 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: One-to- oneLoad a program in the lab and run it: each line is one instruction, made of an opcode and (usually) an operand. Project tab 5 of the lab (Assembly language with the 9618 instruction set) and let a student drive it while the class predicts.Whole class, then pairs
13 to 21 minTeach and check: The instruction setThese are the instructions you will meet in 9618 assembly questions, as printed in the syllabus. Your turn: Classify each instruction into its group: CMI 210, OUT, LDR #0, JPN 104, DEC IX.Pairs
21 to 30 minTeach and check: Two passesA label can be used before the line where it is defined (a forward reference). Model the worked example Build the symbol table on the board, one line at a time. Your turn: What value is in TOTAL when the program above reaches END?Think-Pair-Share
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, 16 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 Two passes, 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

Think-Pair-Share

Pose the question, give thirty seconds of silent thinking, then a minute to compare with a partner before a few pairs share with the class. The silent minute is the part that cannot be cut.

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
Assembly language is not machine code.It must still be translated, by an assembler, before the processor can execute it.
CMP only compares: it does not change the accumulator.It sets the result that the next JPE or JPN uses.
Do not say pass 1 "translates" the program.Pass 1 builds the symbol table; the object code is generated in pass 2.
Worksheet

Assembly language and the two-pass assembler: worksheet

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

NameClassDate
Q1 · explain[2 marks]

Describe the relationship between assembly language and machine code.

Q2 · recall[4 marks]

Describe what happens during each pass of a two-pass assembler.

Q3 · apply[3 marks]
200         LDD FIRST
201         CMP SECOND
202         JPE SAME
203         LDM #78
204         JMP SHOW
205  SAME:   LDM #89
206  SHOW:   OUT
207         END
208  FIRST:  12
209  SECOND: 12

Complete the symbol table produced by pass 1 for this program, which starts at address 200.

Q4 · trace[2 marks]

For the program in the previous question, state the character that is output, and explain why. (ASCII: 78 is N, 89 is Y.)

Q5 · recall[3 marks]

Give the group to which each instruction belongs: (a) STO 300, (b) CMP #10, (c) IN.

Q6 · analysis[2 marks]

Explain why an assembler that reads the program only once could not translate the instruction JMP SHOW at address 204.

Answer key and marking notes
Q1 · 2 marks
  • Assembly language uses mnemonics (and labels) in place of binary opcodes (and addresses).
  • Each assembly language instruction translates into one machine code instruction (one-to-one).
Q2 · 4 marks
  • Pass 1: reads each line, removes comments and allocates an address to each instruction.
  • Pass 1: adds labels and their addresses to the symbol table (including forward references).
  • Pass 2: reads each line again, looks up the opcodes in the opcode table.
  • Pass 2: replaces symbolic addresses with absolute addresses from the symbol table and generates the object code.
Q3 · 3 marks
  • SAME 205 and SHOW 206.
  • FIRST 208.
  • SECOND 209.
Q4 · 2 marks
  • FIRST and SECOND both hold 12, so the compare is True and JPE jumps to SAME.
  • LDM #89 loads 89, so OUT outputs Y.
Q5 · 3 marks
  • (a) Data movement.
  • (b) Compare.
  • (c) Input and output.
Q6 · 2 marks
  • SHOW is a forward reference: it is defined later, at address 206.
  • When line 204 is first read, the address of SHOW is not yet known, so the operand cannot be replaced by an absolute address.
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

Which statement describes the relationship between assembly language and machine code?

  • AEach assembly language instruction usually translates into one machine code instruction
  • BEach assembly language instruction translates into several machine code instructions
  • CAssembly language is executed by the processor directly, so it is not translated
  • DAssembly language works on any processor, but machine code works on only one
Q21 mark

Which instruction belongs to the compare group of the 9618 instruction set?

  • ACMI 210
  • BJPE 210
  • CLDI 210
  • DSUB 210
Q31 mark

Pass 1 of a two-pass assembler processes this program, which starts at address 300.

300  START:  IN
301          CMP STOP
302          JPE FINISH
303          OUT
304          JMP START
305  FINISH: END
306  STOP:   35
Which symbol table does pass 1 produce?

  • ASTART 300, FINISH 305, STOP 306
  • BSTART 300, FINISH 302, STOP 301
  • CSTART 301, FINISH 306, STOP 307
  • DSTART 304, FINISH 302, STOP 301
Q41 mark

Which task is carried out in pass 2 of a two-pass assembler?

  • AEach symbolic address is replaced by its absolute address from the symbol table
  • BEach label is added to the symbol table with its address
  • CAn address is allocated to each line using a location counter
  • DForward references are recorded so their addresses can be filled in later
Q51 mark

In this program, which labels are forward references?

300  START:  IN
301          CMP STOP
302          JPE FINISH
303          OUT
304          JMP START
305  FINISH: END
306  STOP:   35

  • AFINISH and STOP only
  • BSTART only
  • CSTART, FINISH and STOP
  • DFINISH only
Q61 mark

The program below runs and the user types the characters A, B and then # (ASCII: A is 65, B is 66, # is 35). What is output?

300  START:  IN
301          CMP STOP
302          JPE FINISH
303          OUT
304          JMP START
305  FINISH: END
306  STOP:   35

  • AAB
  • BAB#
  • C6566
  • DBA
Quiz key
Q1: A

Assembly language replaces binary opcodes with mnemonics and addresses with labels, so the relationship is one-to-one. One statement becoming many machine code instructions describes a high-level language. Assembly language must still be translated by an assembler, and it is just as processor-specific as machine code, because it is written for one instruction set.

Q2: A

CMI compares the ACC with the contents of the address held at 210 (indirect), so it is a compare instruction. JPE uses the result of a compare but is a conditional jump. LDI is data movement (an indirect load), and SUB is arithmetic.

Q3: A

Each label takes the address of the line it labels: START 300, FINISH 305 and STOP 306. Using 302 and 301 gives the lines where FINISH and STOP are used, not defined. Starting the count at 301 is an off-by-one slip, and START 304 records the JMP that refers back to it.

Q4: A

In pass 2 the assembler looks up opcodes in the opcode table, swaps each label for its address from the finished symbol table and generates the object code. Adding labels to the symbol table, allocating addresses with a location counter and recording forward references all happen in pass 1.

Q5: A

FINISH is used at 302 but defined at 305, and STOP is used at 301 but defined at 306, so both are forward references. START is used at 304, after its definition at 300, so its address is already known: it is a backward reference. Leaving out STOP forgets that a data label used by CMP counts too.

Q6: A

A (65) is not equal to 35, so the compare is False, JPE does not jump and OUT outputs A; B is handled the same way. When # is typed the ACC holds 35, the compare is True and JPE jumps to FINISH before OUT, so # is never output. OUT outputs characters, not their codes, so 6566 is wrong, and each character is output as soon as it is typed, so the order is not reversed.

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 lessonInterrupts