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

Mnemonics to machine code.

Machine code is just binary, which is hard for people to write and read. Assembly language replaces each binary opcode with a short mnemonic and lets you name memory locations with labels. Learn the 9618 instruction set, its groups, and how a two-pass assembler turns it into machine code.

The key idea

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
Section 01

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. That is exactly one machine code instruction.

Examiner trap

Assembly language is not machine code. It must still be translated, by an assembler, before the processor can execute it.

Section 02

The instruction set.

These are the instructions you will meet in 9618 assembly questions, as printed in the syllabus. All questions assume one general purpose register, the accumulator (ACC); IX is the index register. Learn which group each instruction belongs to.

GroupOpcode and operandExplanation
Data movementLDM #nImmediate addressing. Load the number n to ACC
LDD <address>Direct addressing. Load the contents of the location at the given address to ACC
LDI <address>Indirect addressing. The address to be used is at the given address. Load the contents of this second address to ACC
LDX <address>Indexed addressing. Form the address from <address> + the contents of the index register. Copy the contents of this calculated address to ACC
LDR #nImmediate addressing. Load the number n to IX
MOV <register>Move the contents of the accumulator to the given register (IX)
STO <address>Store the contents of ACC at the given address
Input and outputINKey in a character and store its ASCII value in ACC
OUTOutput to the screen the character whose ASCII value is stored in ACC
ArithmeticADD <address>Add the contents of the given address to the ACC
ADD #n/Bn/&nAdd the number n to the ACC
SUB <address>Subtract the contents of the given address from the ACC
SUB #n/Bn/&nSubtract the number n from the ACC
INC <register>Add 1 to the contents of the register (ACC or IX)
DEC <register>Subtract 1 from the contents of the register (ACC or IX)
Unconditional jumpJMP <address>Jump to the given address
Conditional jumpJPE <address>Following a compare instruction, jump to <address> if the compare was True
JPN <address>Following a compare instruction, jump to <address> if the compare was False
CompareCMP <address>Compare the contents of ACC with the contents of <address>
CMP #nCompare the contents of ACC with number n
CMI <address>Indirect addressing. The address to be used is at the given address. Compare the contents of ACC with the contents of this second address
OtherENDReturn control to the operating system

A label is written before an instruction or a data item, followed by a colon, for example LOOP: LDD COUNT or COUNT: 4. <address> can be an absolute address (such as 112) or a symbolic address (a label such as COUNT). The bit manipulation instructions (AND, OR, XOR, LSL, LSR) are covered in the last lesson of this topic.

Your turn
Classify each instruction into its group: CMI 210, OUT, LDR #0, JPN 104, DEC IX.
Examiner trap

CMP only compares: it does not change the accumulator. It sets the result that the next JPE or JPN uses.

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 is the relationship between assembly language and machine code?

Quick check+10 XP

In the lab, which group does JPN belong to?

Quick check+10 XP

What does pass 1 of a two-pass assembler produce?

Quick check+10 XP

What does the instruction OUT do?

Section 03

Two passes.

A label can be used before the line where it is defined (a forward reference). On a single read, the assembler would not yet know that label's address, so it reads the program twice.

Pass 1Pass 2
Reads the source code one line at a time.
Removes comments and white space.
Allocates an address to each line, using a location counter.
Adds each label to the symbol table with its address; a label that is used before it is defined is added and its address filled in when the definition is reached.
Reads the source code one line at a time again.
Looks up each opcode in the opcode table to get its binary code.
Replaces each symbolic address with its absolute address from the symbol table.
Generates the object code (machine code) and reports any errors, such as a label that was never defined.
100  START: LDM #0
101         STO TOTAL
102  LOOP:  LDD TOTAL
103         ADD NUM
104         STO TOTAL
105         LDD COUNT
106         DEC ACC
107         STO COUNT
108         CMP #0
109         JPN LOOP
110         END
111  NUM:   5
112  COUNT: 4
113  TOTAL: 0
Worked example Build the symbol table
The program above is loaded starting at address 100. Complete the symbol table after pass 1, and give the object code (with absolute addresses) for the lines at 101 and 109.
  1. Pass 1 counts addresses from 100: START is at 100 and LOOP is at 102.
  2. At 101, STO TOTAL uses TOTAL before it is defined: a forward reference. TOTAL is added to the symbol table without an address.
  3. The data labels are reached later: NUM at 111, COUNT at 112 and TOTAL at 113, which fills in the missing address.
  4. Pass 2 replaces each label with its address: STO TOTAL becomes STO 113 and JPN LOOP becomes JPN 102.
Answer: START 100, LOOP 102, NUM 111, COUNT 112, TOTAL 113; STO 113 and JPN 102
Your turn
What value is in TOTAL when the program above reaches END?
Examiner trap

Do not say pass 1 "translates" the program. Pass 1 builds the symbol table; the object code is generated in pass 2.

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

Why does an assembler need two passes?

Exam style+20 XP

Which instruction is in the data movement group?

Exam style+20 XP

Which task is carried out during pass 2?

Exam style+20 XP

What does LDR #5 do?

Skill unlocked

Assembly language and the two-pass assembler, 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
Assembly language and machine code, the groups of the 9618 instruction set, and the two-pass assembler and its symbol table.