A Adam Learning
Back to notes
9618-AS-04-02Chapter 4Section 4.2comprehensive

4.2 Assembly Language 汇编语言

Review the exact wording students need for this knowledge point, then open a filtered question list when ready to practise.

Find questions for 9618-AS-04-02

Syllabus learning goals 考纲学习目标

完成本节后,学生需要能够:

  • 解释 assembly language 与 machine code 的关系。
  • 描述 two-pass assembler 的两个 stages。
  • 把 two-pass process 应用于带 labels 的简单程序。
  • 使用给定 instruction set trace assembly program。
  • 把 instructions 分为 data movement、I/O、arithmetic、jump 和 compare groups。
  • 使用 immediate、direct、indirect、indexed 和 relative addressing。
Assembly questions 总会给出本题 instruction set。不要只凭记忆猜 opcode;先阅读表中的 exact operation、operand format 和 addressing mode。

1 Assembly language and machine code

Machine code 是 CPU 可直接执行的 binary instructions。每条 instruction 通常分为:

  • Opcode:指定 operation,例如 load、add、jump。
  • Operand:指定 data、register、address 或 offset。

Assembly language 使用 mnemonic opcode 和人类较容易阅读的 operands/labels,例如:

LDM #5
ADD #3
STO Total

Assembler 把 assembly source code 转换为 machine/object code:

  • Mnemonic opcode 对应 processor instruction set 中的 binary opcode。
  • Numeric operand 被编码为 binary。
  • Symbolic label 由 assembler 替换为实际 address。

Assembly language 是 low-level、processor-specific language。通常一条 assembly instruction 对应一条 machine instruction,但 source 中的 labels、comments 和 formatting 不会成为可执行 instructions。

2 Two-pass assembler

使用 two passes 的核心原因:程序可能在 label 定义出现之前就引用该 label。First pass 先确定所有 symbolic addresses,second pass 才能正确生成 object code。

First pass

  • Reads source code one line at a time。
  • Removes/ignores whitespace and comments。
  • Checks that opcodes are valid and detects relevant syntax errors。
  • Maintains a location counter to determine each instruction/data address。
  • Adds each label and its address to the symbol table
The main purpose of the first pass is to create/build the symbol table.

Second pass

  • Reads the source code again。
  • Looks up symbolic labels in the symbol table。
  • Replaces labels with their addresses。
  • Translates mnemonic opcodes and operands into binary machine code。
  • Generates the object code and reports unresolved labels/errors。

Actions by pass

ActionFirst passSecond pass
Read source code line by lineYesYes
Remove/ignore comments and whitespaceYesNo
Check/identify opcodesYesUsed for translation
Build symbol tableYesNo
Resolve label addressesNoYes
Generate object codeNoYes

Applied example

        LDM #5
        STO Total
        END
Total:  0

First pass:

  • Location counter assigns an address to each line。
  • Total and its address are entered into the symbol table。
  • STO Total can be recognised even though the address is not yet substituted。

Second pass:

  • LDMSTOEND are translated to their binary opcodes。
  • #5 is encoded as an immediate value。
  • Total is replaced with the address stored in the symbol table。
  • Object code is produced。

3 Instruction groups

GroupPurposeTypical examples
Data movementMove/load/store data between memory and registersLDM #5, LDD 100, LDI 100, LDX 100, LDR #2, MOV IX, STO 100
Input and outputReceive input or send outputIN, OUT
Arithmetic operationsPerform arithmetic or change register valuesADD 100, ADD #5, SUB #2, INC ACC, DEC IX
Unconditional and conditional instructionsChange normal instruction sequenceJMP Loop, JPE Equal, JPN NotEqual
Compare instructionsCompare ACC with a value and set a condition/result used by a jumpCMP #5, CMP 100, CMI 100

Exam wording:

  • Loading data into ACC → data movement
  • Incrementing IX → arithmetic operations
  • Branching to another address → conditional/unconditional jump instructions
  • IN/OUTinput and output of data

给 instruction example 时必须包含 suitable operand,例如写 ADD 100,而不是只写 ADD

4 Operand notation

题目常用:

  • #n:denary literal/immediate value,例如 #127
  • Bn:binary value,例如 B01001101
  • &n:hexadecimal value,例如 &4A
  • <address>:absolute numeric address 或 symbolic label。

同一个 opcode 的 operand form 可能改变 addressing mode。必须连同 #B& 或 address 一起阅读。

5 Addressing modes

假设 memory 使用 M[address] 表示,IX 是 index register。

Immediate addressing

Operand 本身就是 data value,不访问该数值对应的 memory address。

LDM #98  -> ACC = 98
ADD #4   -> ACC = ACC + 4

Mark-secure definition:the actual value/data is contained in the operand

Direct addressing

Operand 是存放 data 的 memory address。

LDD 50 -> ACC = M[50]
ADD 54 -> ACC = ACC + M[54]

Indirect addressing

Operand 指向一个 memory location;该 location 的内容才是 data 的实际 address,需要两次 memory lookup。

LDI 101 -> ACC = M[M[101]]

如果 M[101]=98M[98]=8,则 LDI 101ACC=8

Indexed addressing

Effective address = operand/base address + contents of IX。

LDX 100 -> ACC = M[100 + IX]

IX=2M[102]=32,则 ACC=32。Indexed addressing 常用于依次访问 array/list items;increment IX 即可访问下一 element。

Relative addressing

The operand is an offset added to a base value to form the effective address.

Base value 通常由 processor/PC/current address 提供。Relative addressing 适合 branch 到当前位置附近,也使 code 更容易 relocation。不要把 relative 与 indexed 混为一谈:indexed 明确使用 IX;relative 使用 offset + base/current address。

Comparison table

ModeEffective value/address
Immediateoperand value
DirectM[operand address]
IndirectM[M[operand address]]
IndexedM[operand address + IX]
RelativeM[base address + offset] or branch target base + offset

6 Worked addressing example

Given:

AddressData
988
9916
1003
10198
10232

IX = 2

InstructionReasoningACC
LDM #98Immediate value is 9898
LDI 101M[101]=98, then M[98]=88
LDX 100Effective address 100+2=102, M[102]=3232

这是区分三种 mode 的最稳妥方式:先写 expression,再查 memory。

7 Tracing an assembly program

Trace-table method

  • Copy initial values of ACC、IX 和 relevant memory locations。
  • Start at the first instruction address。
  • Read the exact opcode definition from the provided table。
  • Resolve the operand/addressing mode before changing any value。
  • Update only the register、memory cell、output or condition actually affected。
  • Unless a jump occurs, move to the next instruction address。
  • For CMP, do not change ACC;record whether comparison is true or false。
  • For JPE/JPN, use the most recent comparison result to decide the next address。
  • Stop only when END executes or the question's stopping condition is reached。

Worked arithmetic trace

Memory:M[50]=54, M[54]=100, M[56]=50, M[53]=52, M[52]=50

Program 1:

LDD 50     ; ACC = 54
ADD #4     ; ACC = 58
ADD 54     ; ACC = 58 + 100 = 158

Program 2:

LDI 53     ; M[53]=52, M[52]=50, ACC = 50
DEC ACC    ; ACC = 49
ADD 56     ; ACC = 49 + 50 = 99

Program 3:

LDM #55    ; ACC = 55
SUB #5     ; ACC = 50

Jumps and loops

  • JMP address always changes the next instruction address。
  • CMP value/address establishes a true/false comparison but does not normally alter ACC。
  • JPE address jumps if the comparison is true/equal。
  • JPN address jumps if the comparison is false/not equal。
  • A loop may execute the same instruction address multiple times;trace table 必须记录每一次 execution。

8 Common mistakes 常见失分点

  • 把 assembly language 写成 CPU 可直接执行;CPU 直接执行的是 machine code。
  • First pass/second pass 都写成 “translate code”,却没有 symbol table 和 object code 的区别。
  • 忘记 symbolic label 在 second pass 被 address 替换。
  • 忽略 operand 的 #,把 immediate value 当 memory address。
  • Direct 读取一次 memory,indirect 读取两次;两者混淆。
  • Indexed 忘记 address + IX 后还要读取该 effective address 的内容。
  • CMP 后错误修改 ACC。
  • Conditional jump 没有使用最近一次 comparison result。
  • STO 方向写反;它把 ACC 写入 memory,不是从 memory load ACC。
  • Trace loop 时跳过重复执行的 rows。

9 Exam-answer checklist 真题检查表

  • 是否写 assembly mnemonic/operand 由 assembler 转成 binary opcode/operand?
  • First pass 是否出现 location counter 和 symbol table?
  • Second pass 是否出现 label lookup 和 object code generation?
  • Instruction group 是否使用 syllabus 指定名称?
  • Example instruction 是否含 suitable operand?
  • 是否精确区分 immediate、direct、indirect、indexed、relative?
  • Trace 前是否先写出 effective address/value?
  • CMP 是否保持 ACC 不变?
  • Jump 后 next instruction address 是否正确?
  • 每次 memory/register change 是否记录在同一 trace row?

本材料依据 9618 syllabus 4.2 的六项要求,并综合本页所列 2021-2025 past-paper questions 及 mark schemes。由于部分 syllabus points 没有单独绑定题目,相关内容同时依据官方 instruction-set wording 组织。

Practice This Knowledge Point

Open the question bank with this knowledge code already filled in. The filtered list will show matching questions for this note.

Open filtered practice
Source questions used