Trace a given simple assembly language program

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – 4.2 Assembly Language

4.2 Assembly Language

Learning Objective

By the end of this lesson you should be able to trace a simple assembly language program step‑by‑step, showing the contents of the program counter, registers and memory after each instruction.

What is Assembly Language?

Assembly language is a low‑level, human‑readable representation of machine code. Each instruction corresponds directly to an operation performed by the CPU. Typical components of an assembly language program are:

  • Opcode – the operation to be performed (e.g., LD, ADD, ST).
  • Operand(s) – the data or address the operation works on.
  • Labels – symbolic names for memory addresses, making programs easier to read.

Typical Registers Used in Simple Assembly

RegisterPurpose
PC (Program Counter)Holds the address of the next instruction to be fetched.
ACC (Accumulator)Used for arithmetic and logic operations.
IR (Instruction Register)Temporarily stores the fetched instruction.

Sample Assembly Program

The following program adds the contents of two memory locations (M[10] and M[11]) and stores the result in M[12].

; Simple addition program

LD 10 ; Load M[10] into ACC

ADD 11 ; Add M[11] to ACC

ST 12 ; Store ACC into M[12]

HLT ; Halt the processor

Initial Memory Contents

Assume the memory before execution contains the following values (all values are decimal):

AddressContent
107
115
120

Step‑by‑Step Trace

Each row of the table shows the state of the system after the instruction has been executed.

StepPCIR (Fetched Instruction)ACCMemory Change
000Initial state
11LD 107
22ADD 1112 (\$7+5\$)
33ST 1212M[12] ← 12
44HLT12Program stops

Explanation of Each Step

  1. Step 0 – Initialisation: All registers are cleared. PC points to the first instruction (address 0).
  2. Step 1 – LD 10: The instruction at address 0 is fetched into IR, PC is incremented to 1, and the value at memory address 10 (which is 7) is loaded into ACC.
  3. Step 2 – ADD 11: The next instruction is fetched, PC becomes 2. The value at address 11 (5) is added to the current ACC value (7), resulting in 12, which is stored back in ACC.
  4. Step 3 – ST 12: The store instruction copies the ACC value (12) into memory location 12.
  5. Step 4 – HLT: The halt instruction stops the fetch‑decode‑execute cycle. The final state of the system is ACC = 12 and M[12] = 12.

Common Mistakes When Tracing

  • Forgetting to increment the PC after fetching an instruction.
  • Confusing the source and destination of ST – the accumulator is the source, the memory address is the destination.
  • Not updating the memory table when a store operation occurs.

Suggested Diagram

Suggested diagram: Simple CPU datapath showing PC → Instruction Memory → IR → Decoder → ACC, with read/write paths to Data Memory.

Practice Exercise

Trace the following program. Assume the initial memory contents are M[20]=3, M[21]=4, M[22]=0.

LD 20

SUB 21

ST 22

HLT

Provide a trace table similar to the one above, showing the final value stored in M[22].