Skip to content
Cambridge AS & A Level9618

Assembly Language

Computer Science 9618 Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Assembly Language notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Assembly Language notes as text: skim, search, and jump between subtopics.

~19 min read

1. Assembly language and machine code

A program written in assembly language is converted into machine code by an assembler. A two-pass assembler generates the object code in its second pass.

Each assembly language instruction has an opcode and, where the instruction needs one, an operand: `STO 201` has both, `END` has an opcode only. An `<address>` operand can be an absolute address, such as 201, or a symbolic address, a label such as `Twenty`.

`#` denotes a denary number, `B` a binary number and `&` a hexadecimal number, so `ADD #10`, `ADD B00001010` and `ADD &0A` add the same number.

Worked example 13 marks

The instruction `STO 201` is written in assembly language. State its opcode, state its operand and state what the instruction does.

  1. 1

    opcode: STO [1]

  2. 2

    operand: 201 (an address) [1]

  3. 3

    Store the contents of ACC at the given address [1]

2. The two-pass assembler

A two-pass assembler works through the assembly language program twice.

In the first pass it reads the source code one line at a time, removes white space and adds labels to the symbol table. The purpose of the first pass is to create a symbol table.

In the second pass it reads the source code one line at a time again and generates object code. So reads the source code one line at a time belongs to both passes.

Examiner insight

“Many candidates did not join the action to both passes.” “Many candidates need to improve their understanding of the purpose of each pass of a two-pass compiler.”

Worked example 13 marks

A program is written in assembly language.

The program is converted into machine code by a two-pass assembler.

Draw one or more lines to identify the pass or passes in which each action takes place.

ActionPass
generates object code
reads the source code one line at a time
removes white space
adds labels to the symbol table
  1. 1

    generates object code: second pass [1]

  2. 2

    reads the source code one line at a time: both passes (first and second) [1]

  3. 3

    removes white space and adds labels to the symbol table: first pass [1]

3. Instruction groups

Instructions are grouped according to their function.

Data movement: `LDM`, `LDD`, `LDI`, `LDX`, `LDR`, `MOV`, `STO`. Input and output of data: `IN` takes an input from the user and `OUT` outputs a character. Arithmetic operations perform addition and subtraction: `ADD`, `SUB`, `INC`, `DEC`.

Unconditional and conditional instructions move to another instruction: `JMP`, `JPE`, `JPN`. Compare instructions compare the result to another value: `CMP`, `CMI`. Name the group by its function: copying the explanation from the instruction table does not name a group.

Examiner insight

“The opcode most likely to be incorrectly placed was JPE; identified in the compare group rather than the unconditional and conditional group.” “Some candidates should understand that just copying the explanation of the opcode from the table does not identify the instruction group for the command.”

Worked example 14 marks

The instructions in the processor's instruction set can be grouped according to their function.

Identify the instruction group for each of the following opcodes.

OpcodeExplanation in the instruction tableGroup
INKey in a character and store its ASCII value in ACC
ADDAdd the denary number n to the ACC
JPEFollowing a compare instruction, jump to <address> if the compare was True
CMIIndirect addressing. The address to be used is at the given address. Compare the contents of ACC with the contents of this second address
  1. 1

    IN: Input and output of data [1]

  2. 2

    ADD: Arithmetic operations [1]

  3. 3

    JPE: Unconditional and conditional instructions [1]

  4. 4

    CMI: Compare instructions [1]

4. Tracing a program

To trace a program, execute it one instruction at a time. Make one row of the trace table for each instruction that runs, in the order it runs. A jump changes which instruction address comes next, so some addresses are skipped.

The columns are the instruction address, the ACC (and IX if used), each memory address the program reads or writes, and the Output. Fill a cell only when the instruction changes it.

`STO` copies the ACC to memory, and the ACC keeps its value. `CMP` makes the compare True or False, and the following `JPE` or `JPN` jumps or not. The Output column is for `OUT` only, never for the result of a compare, and it is written without quotation marks.

Examiner insight

“Some candidates need to improve their understanding of the difference between the CMP <address> instruction given in this table and the CMP #n instruction.” “Some candidates also need to understand that storing a value in a memory location copies the value from the accumulator and does not reset the register to zero.”

Worked example 13 marks

The processor has one register: the Accumulator (ACC). `LDD <address>` loads the contents of the location at the given address to ACC. `ADD <address>` adds the contents of the given address to the ACC. `STO <address>` stores the contents of ACC at the given address. `CMP <address>` compares the contents of ACC with the contents of `<address>`. `JPE <address>`: following a compare instruction, jump to `<address>` if the compare was True. `INC ACC` adds 1 to the ACC. `JMP <address>` jumps to the given address. `END` returns control to the operating system.

The current contents of memory addresses 10, 11, 12, 13 and 14 are 12, 11, 10, 22 and 22. The program in memory is:

``` 100 LDD 10 101 ADD 12 102 STO 11 103 CMP 14 104 JPE 107 105 INC ACC 106 JMP 101 107 STO 10 108 END ```

Trace the program currently in memory using the following trace table.

Instruction addressACC1011121314
1211102222
  1. 1

    100 `LDD 10`: ACC is 12. 101 `ADD 12`: ACC is 12 + 10 = 22 [1]

  2. 2

    102 `STO 11`: address 11 now holds 22, and the ACC is still 22 [1]

  3. 3

    103 `CMP 14`: 22 = 22, so the compare is True. 104 `JPE 107` jumps to 107. 107 `STO 10`: address 10 now holds 22 [1]

5. The addressing modes

The mode of addressing says how the operand is used to find the data.

Immediate: the operand is the data to be used, so `LDM #5` loads the number 5. Direct: the operand is an address where the data is stored, so `LDD 20` loads the contents of address 20.

Indirect: the operand is an address that stores the address where the data is stored, so `LDI 25` goes to address 25, reads an address from it, and loads the contents of that second address. Indexed addressing is in the next section.

Examiner insight

“This question required exact use of terminology; some candidates found this challenging.”

Worked example 14 marks

A program is written in assembly language.

Complete the table by writing the name of each mode of addressing.

Mode of addressingDescription
The operand is an address where the data is stored.
The operand is an address that stores the address where the data is stored.
The operand is an address. The address where the data is stored is calculated by adding the operand to the content of the index register.
The operand is the data to be used.
  1. 1

    Direct: the operand is an address where the data is stored [1]

  2. 2

    Indirect: the operand is an address that stores the address where the data is stored [1]

  3. 3

    Indexed: the address is calculated by adding the operand to the content of the index register [1]

  4. 4

    Immediate: the operand is the data to be used [1]

6. Indexed addressing

The index register IX holds a value that is added to an address. `LDR #n` loads the number n to IX. `INC IX` adds 1 to it.

`LDX <address>` forms the address from `<address>` plus the contents of IX, then copies the contents of that calculated address to the ACC. Direct addressing accesses the address given by the operand. Indexed addressing adds the contents of IX to the operand and accesses the data at that calculated address.

So with IX holding 1, `LDX 22` loads the contents of address 23.

address used = operand + [IX]

ACC ← [ address used ]

Examiner insight

“A small number of candidates loaded the zero into the accumulator instead of the index register.” “A significant number of candidates were able to achieve the first two marks, but then loaded an incorrect value to the accumulator on the execution of the indexed addressing in the second iteration.”

Worked example 14 marks

`LDI <address>` is indirect addressing, `LDD <address>` is direct addressing, `LDX <address>` is indexed addressing, `LDM #n` loads the number n to ACC and `LDR #n` loads the number n to IX. `ADD <address>` adds the contents of the address to the ACC. `ADD #n` and `SUB #n` add and subtract the number n. `SUB <address>` subtracts the contents of the address from the ACC.

The current contents of memory are: address 10 holds 1, 11 holds 3, 12 holds 5, 13 holds 11, 14 holds 10, 15 holds 16, 16 holds 12. The current contents of the ACC and IX are: ACC 10, IX 0.

Complete the table by writing the content of the ACC after each program has run.

Program numberCodeACC content
1`LDI 15`, `SUB #1`
2`LDD 14`, `ADD 11`
3`LDM #11`, `ADD #3`, `SUB 16`
4`LDR #2`, `LDX 14`, `ADD #2`
  1. 1

    1: `LDI 15`. Address 15 holds 16, so load the contents of address 16, which is 12. `SUB #1`: 12 - 1 = 11. ACC = 11 [1]

  2. 2

    2: `LDD 14` loads 10. `ADD 11`: 10 + 3 = 13. ACC = 13 [1]

  3. 3

    3: `LDM #11` loads 11. `ADD #3`: 14. `SUB 16`: 14 - 12 = 2. ACC = 2 [1]

  4. 4

    4: IX becomes 2. `LDX 14` uses address 14 + 2 = 16, which holds 12. `ADD #2`: 14. ACC = 14 [1]

7. Relative addressing

In relative addressing the address to be used is an offset number of locations away, relative to the address of the current instruction.

The address is calculated using its distance from a base address.

The purpose is to allow for re-locatable code: all target addresses can be specified by the base address + offset.

address = base address + offset

Examiner insight

“This question was not answered well. Many candidates need to improve their understanding of relative addressing.”

Worked example 12 marks

Explain the purpose of relative addressing in an assembly language program.

  1. 1

    To allow for re-locatable code [1]

  2. 2

    ...because all (target) addresses can be specified by the base address + offset [1]

Recap

  • Assembly language is converted into machine code by an assembler. The second pass generates the object code.
  • First pass: reads one line at a time, removes white space, adds labels to the symbol table. Second pass: reads one line at a time, generates object code.
  • Groups: data movement; input and output of data; arithmetic operations; unconditional and conditional instructions; compare instructions.
  • Trace one instruction per row. `STO` copies the ACC to memory. `CMP` sets True or False. Output has no quotation marks.
  • Immediate: the operand is the data. Direct: the operand is the address of the data. Indirect: the operand is the address of the address of the data.
  • Indexed: address used = operand + the contents of IX.
  • Relative: the address is an offset from a base address, which allows re-locatable code.

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. The instruction `LDM #7` is written in assembly language. State its opcode and its operand.2 marks
    Answer
    1. Opcode: `LDM`.
    2. Operand: `#7`.

    Trap: The `#` is part of the operand: it marks a denary number, so the operand is #7, not 7.

  2. State the purpose of the first pass of a two-pass assembler. State one action that takes place only in the second pass.2 marks
    Answer
    1. The first pass creates a symbol table.
    2. The second pass generates the object code.

    Trap: Generating object code is a second-pass action, not the purpose of the first pass.

  3. Identify the instruction group of each opcode: `STO`, `CMP`, `JPN`.3 marks
    Answer
    1. `STO`: data movement.
    2. `CMP`: compare instructions.
    3. `JPN`: unconditional and conditional instructions.

    Trap: `JPN` uses the result of a compare, but it is a jump, so it is not in the compare group.

  4. `LDM #n` loads the number n to the ACC. `LDD <address>` loads the contents of the address to the ACC. Address 30 holds 45 and address 45 holds 12. State the content of the ACC after each instruction is run on its own. `LDM #30` `LDD 30` `LDD 45`3 marks
    Answer
    1. `LDM #30`: 30.
    2. `LDD 30`: 45.
    3. `LDD 45`: 12.

    Trap: `LDM` loads the number in the operand, but `LDD` loads the contents of the address.

  5. `LDI <address>` is indirect addressing: the address to be used is at the given address, and the contents of this second address are loaded to the ACC. `LDD <address>` is direct addressing. The current contents of memory are in the table. State the content of the ACC after each instruction is run on its own. `LDD 61` `LDI 61` `LDI 60` | Address | Contents | | --- | --- | | 60 | 62 | | 61 | 63 | | 62 | 7 | | 63 | 9 |3 marks
    Answer
    1. `LDD 61`: 63.
    2. `LDI 61`: address 61 holds 63, so load the contents of address 63, which is 9.
    3. `LDI 60`: address 60 holds 62, so load the contents of address 62, which is 7.

    Trap: `LDI` does not load the address it finds: it goes on to load the contents of that second address.

  6. `LDD <address>` loads the contents of the address to the ACC. `ADD <address>` adds the contents of the address to the ACC. `STO <address>` stores the contents of the ACC at the address. `CMP <address>` compares the contents of the ACC with the contents of `<address>`. `JPE <address>` jumps if the compare was True. `INC ACC` adds 1 to the ACC. `END` returns control to the operating system. The program is: ``` 200 LDD 50 201 ADD 51 202 STO 52 203 CMP 10 204 JPE 207 205 INC ACC 206 STO 52 207 END ``` The current contents of memory are in the table. Give the final content of the ACC and of address 52, with the working. | Address | Contents | | --- | --- | | 10 | 99 | | 50 | 6 | | 51 | 4 | | 52 | 0 |4 marks
    Answer
    1. 200 and 201: ACC is 6 + 4 = 10.
    2. 202: address 52 holds 10.
    3. 203: compare 10 with the contents of address 10, which is 99. The compare is False, so 204 does not jump.
    4. 205: ACC is 11. 206: address 52 holds 11.
    5. Final: ACC is 11 and address 52 holds 11.

    Trap: `CMP 10` compares with the contents of address 10, not with the number 10. Treating it as `CMP #10` takes the jump and gives 10.

  7. `LDR #n` loads the number n to IX. `LDX <address>` is indexed addressing: the address is `<address>` plus the contents of IX, and the contents of that address are loaded to the ACC. `INC IX` adds 1 to IX. `ADD <address>` adds the contents of the address to the ACC. The current contents of memory are in the table. This program is run: ``` LDR #3 LDX 40 INC IX LDX 40 ADD 41 ``` Give the final contents of the ACC and of IX, with the working. | Address | Contents | | --- | --- | | 40 | 5 | | 41 | 6 | | 42 | 7 | | 43 | 8 | | 44 | 9 |4 marks
    Answer
    1. `LDR #3`: IX is 3.
    2. `LDX 40`: address 40 + 3 = 43 holds 8, so ACC is 8.
    3. `INC IX`: IX is 4.
    4. `LDX 40`: address 40 + 4 = 44 holds 9, so ACC is 9.
    5. `ADD 41`: ACC is 9 + 6 = 15. Final: ACC is 15 and IX is 4.

    Trap: `LDX 40` is not `LDD 40`: the contents of IX at that moment must be added to 40 each time.

  8. `IN` keys in a character and stores its ASCII value in the ACC. `CMP <address>` compares the contents of the ACC with the contents of `<address>`. `JPN <address>` jumps to `<address>` if the compare was False. `INC ACC` adds 1 to the ACC. `OUT` outputs the character whose ASCII value is in the ACC. The ASCII codes are A 65, B 66, C 67, D 68. Address 90 holds 68. `END` returns control to the operating system. The program is: ``` 10 IN 11 CMP 90 12 JPN 15 13 OUT 14 END 15 INC ACC 16 OUT 17 END ``` The character C is keyed in. Give the order of instruction addresses run, and the output.4 marks
    Answer
    1. 10: ACC is 67.
    2. 11: compare 67 with the contents of address 90, which is 68. The compare is False.
    3. 12: `JPN` jumps, because the compare was False, to 15.
    4. 15: ACC is 68. 16: `OUT` outputs D. 17: `END`.
    5. Addresses run: 10, 11, 12, 15, 16, 17. Output: D.

    Trap: The output is D, written without quotation marks, and `JPN` jumps when the compare is False.

  9. `LDR #n` loads the number n to IX. `LDX <address>` is indexed addressing and loads the contents of `<address>` plus IX to the ACC. `CMP #n` compares the ACC with the number n. `JPE <address>` jumps if the compare was True. `ADD <address>` adds the contents of the address to the ACC. `STO <address>` stores the ACC at the address. `INC IX` adds 1 to IX. `JMP <address>` jumps to the address. `END` returns control to the operating system. The program is: ``` 20 LDR #0 21 LDX 100 22 CMP #0 23 JPE 28 24 ADD 110 25 STO 110 26 INC IX 27 JMP 21 28 END ``` The current contents of memory are in the table. Give the final ACC, IX and the contents of address 110, with the working. | Address | Contents | | --- | --- | | 100 | 4 | | 101 | 5 | | 102 | 0 | | 110 | 0 |5 marks
    Answer
    1. First time round, IX is 0: `LDX 100` loads 4. The compare with 0 is False. `ADD 110`: 4 + 0 = 4. Address 110 holds 4. IX is 1.
    2. Second time, `LDX 100` uses address 101 and loads 5. The compare is False. `ADD 110`: 5 + 4 = 9. Address 110 holds 9. IX is 2.
    3. Third time, `LDX 100` uses address 102 and loads 0. The compare with 0 is True, so `JPE` jumps to 28 and the program ends.
    4. Final: ACC is 0, IX is 2 and address 110 holds 9.

    Trap: `LDX` replaces the ACC each time, so the running total must be kept in address 110, and `ADD 110` adds the contents of address 110, not the number 110.

  10. A program counts how many times a letter appears in a stored word. The word is held from address 120 onwards and ends with 0. Address 90 holds the address of the letter to look for. `LDR #n` loads the number n to IX. `LDX <address>` loads the contents of `<address>` plus IX to the ACC. `LDD <address>` loads the contents of the address to the ACC. `CMP #n` compares the ACC with the number n. `CMI <address>` is indirect addressing: the address to be used is at the given address, and the ACC is compared with the contents of this second address. `JPE <address>` jumps if the compare was True and `JPN <address>` jumps if it was False. `INC <register>` adds 1 to the ACC or IX. `STO <address>` stores the ACC at the address. `JMP <address>` jumps to the address. `ADD #n` adds the number n to the ACC. `OUT` outputs the character whose ASCII value is in the ACC. `END` returns control to the operating system. The program is: ``` 50 LDR #0 51 LDX 120 52 CMP #0 53 JPE 61 54 CMI 90 55 JPN 59 56 LDD 100 57 INC ACC 58 STO 100 59 INC IX 60 JMP 51 61 LDD 100 62 ADD #48 63 OUT 64 END ``` The current contents of memory are in the table. The ASCII codes are 48 for 0, 49 for 1, 50 for 2, 65 for A and 66 for B. Give the final contents of address 100 and of IX, and the output, with the working. | Address | Contents | | --- | --- | | 90 | 95 | | 95 | 66 | | 100 | 0 | | 120 | 66 | | 121 | 65 | | 122 | 66 | | 123 | 0 |6 marks
    Answer
    1. `CMI 90`: address 90 holds 95, so every compare is with the contents of address 95, which is 66 (B).
    2. IX is 0: `LDX 120` loads 66. Not 0. 66 = 66 is True, so no jump: address 100 becomes 1. IX is 1.
    3. IX is 1: `LDX 120` loads the contents of address 121, 65. Not 0. 65 = 66 is False, so `JPN` jumps to 59. IX is 2.
    4. IX is 2: loads 66. True, so address 100 becomes 2. IX is 3.
    5. IX is 3: loads 0. `CMP #0` is True, so `JPE` jumps to 61. `LDD 100` gives 2, `ADD #48` gives 50, and `OUT` outputs 2.
    6. Final: address 100 holds 2, IX is 3, and the output is 2, written without quotation marks.

    Trap: `CMI 90` compares with the contents of address 95, not with the number 90 or the number 95, and `ADD #48` turns the count 2 into ASCII 50, so the output is the character 2, not 50.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
‘indexed addressing uses the index register, direct addressing does not’Direct accesses the address given by the operand, whereas indexed adds the contents of IX to the operand and accesses the data at that calculated address.
Copying the explanation of `JPE` from the instruction table.`JPE` is in the unconditional and conditional instructions group.
Output column: ‘A’, with quotation marks.Output column: A, with no quotation marks.
`LDX 110` loads 110 into the accumulator.`LDX 110` loads the contents of address 110 + [IX] into the accumulator.

Frequently asked questions

What is the difference between direct and indirect addressing?
Direct addressing: the operand is an address where the data is stored, so LDD 20 loads the contents of address 20. Indirect addressing: the operand is an address that stores the address where the data is stored, so LDI 25 goes to address 25, reads an address from it and loads the contents of that second address.
What does the first pass of a two-pass assembler do?
In the first pass the assembler reads the source code one line at a time, removes white space and adds labels to the symbol table. The purpose of the first pass is to create a symbol table. Generating object code is a second-pass action, so it is not the purpose of the first pass.
How does indexed addressing work in assembly language?
The address used is the operand plus the contents of the index register IX, so address used = operand + [IX]. For example, with IX holding 1, LDX 22 loads the contents of address 23 into the ACC. Indexed addressing adds the contents of IX to the operand before the data is accessed.
How do I trace a program using a trace table?
Execute the program one instruction at a time and make one row of the trace table for each instruction that runs, in the order it runs. A jump changes which address comes next, so some addresses are skipped. Fill a cell only when the instruction changes it, and use the Output column for OUT only.
What does relative addressing do, and why is it useful?
In relative addressing the address to be used is an offset number of locations away from a base address, so address = base address + offset. Its purpose is to allow re-locatable code, because all target addresses can be specified by the base address plus the offset.
Does LDX 110 load the number 110 into the accumulator?
No. LDX 110 is indexed addressing, so it loads the contents of address 110 + [IX] into the accumulator. A common incorrect result of the first LDX 110 instruction was to load 110 into the accumulator, which then gave an incorrect value stored in memory address 101.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters