Assembly Process (Cambridge (CIE) A Level Computer Science) : Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Two-pass assembler

What is a two-pass assembler?

  • A two-pass assembler translates assembly language into machine code in two stages

  • It produces an object program that can be stored, loaded, and run later

  • To execute this program, another utility called a loader is needed

  • The assembler scans the source code twice:

    • In the first pass, it identifies all labels and records their memory addresses

    • In the second pass, it replaces those labels with the correct memory addresses in the machine code

  • This ensures the final machine code is accurate and ready to be executed

Example

  • The following program loads the value of A (5), adds the value of B (3), stores the result (8) in C, then halts:

        LOAD A
        ADD B
        STORE C
        HALT
A:      DATA 5
B:      DATA 3
C:      DATA 0
  • Pass 1:

    • The assembler reads each line and assigns memory addresses to labels (e.g. A, B, C), but does not translate the instructions yet

Address

Instruction

Label

Add to symbol table

00

LOAD A

01

ADD B

02

STORE C

03

HALT

04

DATA 5

A

A = 04

05

DATA 3

B

B = 05

06

DATA 0

C

C = 06

Symbol table created:

Label

Address

A

04

B

05

C

06

  • Pass 2:

    • The assembler replaces labels with their corresponding memory addresses and outputs machine code

    • In this example we assume the following opcodes for simplicity:

Mnemonic

Opcode

LOAD

01

ADD

02

STORE

03

HALT

00

DATA

-

  • Now generate the object (machine) code:

Address

Assembly

Machine code

Explanation

00

LOAD A

01 04

LOAD from address 04

01

ADD B

02 05

ADD value at address 05

02

STORE C

03 06

STORE into address 06

03

HALT

00 00

Stop program

04

DATA 5

00 05

Data value 5

05

DATA 3

00 03

Data value 3

06

DATA 0

00 00

Data value 0 (placeholder)

Final object program:

01 04
02 05
03 06
00 00
00 05
00 03
00 00

Program tracing

What is a trace table?

  • A trace table is a table used to manually track the values of variables as a program runs

  • It helps to follow the flow of a program line by line, checking whether the logic works as expected

  • Trace tables are used to:

    • Understand how a program works

    • Debug errors in the logic

    • Predict outputs before running the program

    • Check variable values at each step

Example

  • An assembly program that loads the value from A (4) into the accumulator, adds the value from B (2), stores the result (6) into C, and halts

        LOAD A
        ADD B
        STORE C
        HALT
A:      DATA 4
B:      DATA 2
C:      DATA 0
  • To trace the program, assumptions must be made:

    • Accumulator (ACC): Used for calculations

    • Memory is addressed from 00 onwards

    • Simple instructions:

      • LOAD X → ACC = memory[X]

      • ADD X → ACC = ACC + memory[X]

      • STORE X → memory[X] = ACC

      • HALT → Stop

Trace table

Step

Instruction

ACC (Accumulator)

Memory[A]

Memory[B]

Memory[C]

0

(Start)

0

4

2

0

1

LOAD A

4

4

2

0

2

ADD B

6

4

2

0

3

STORE C

6

4

2

6

4

HALT

6

4

2

6

Final outcome:

  • ACC = 6

  • C = 6

  • The program has successfully added A and B, then stored the result in C

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.