Trace a given simple assembly language program

4.2 Assembly Language – Tracing a Simple Program

Assembly language is like a recipe for the computer’s CPU. Each line tells the CPU exactly what to do, step by step. In this lesson we’ll trace a tiny program to see how the CPU changes its registers and memory as it runs.

Key Concepts

  • Registers – tiny storage spots inside the CPU (e.g., R1, R2).
  • Memory – a big list of addresses where data can be stored.
  • Instruction Pointer (IP) – tells the CPU which line to execute next.
  • Opcode – the operation to perform (e.g., ADD, SUB, LOAD, STORE).
  • Operands – the data the opcode works on (registers, immediate values, or memory addresses).

Example Program

Line Instruction
1 LOAD R1, 10
2 LOAD R2, 20
3 ADD R1, R2
4 STORE R1, 100
5 HALT

Tracing the Program

  1. Initial State:
    • IP = 1
    • R1 = 0, R2 = 0
    • Memory[100] = 0 (uninitialised)
  2. Line 1 – LOAD R1, 10
    • R1 now holds the value 10.
    • IP increments to 2.
  3. Line 2 – LOAD R2, 20
    • R2 now holds 20.
    • IP increments to 3.
  4. Line 3 – ADD R1, R2
    • R1 = R1 + R2 = 10 + 20 = 30.
    • IP increments to 4.
  5. Line 4 – STORE R1, 100
    • Memory[100] now contains 30.
    • IP increments to 5.
  6. Line 5 – HALT
    • Program stops.
  7. Final State:
    • R1 = 30, R2 = 20
    • Memory[100] = 30
    • IP = 5 (HALT)

Analogy: Think of LOAD as “pick up a number from the shelf”, ADD as “mix two numbers together”, and STORE as “put the result back on the shelf”. The HALT instruction is like a stop sign that tells the kitchen to close for the day.

Exam Tips

Remember the order of execution: The CPU always follows the IP, so trace line by line.
Keep a table: Write down register values and memory contents after each instruction – it’s the safest way to avoid mistakes.
Watch for side effects: Some instructions change the IP (e.g., jumps). If you see a JMP or CALL, note the new IP.
Use emojis for memory: 🗂️ for memory, 🧠 for registers, ⏱️ for IP – they help you visualise the flow.

Common Mistakes to Avoid

  • Confusing LOAD R1, 10 with LOAD R1, [10] – the first loads an immediate value, the second loads from memory address 10.
  • Forgetting that ADD R1, R2 does not change R2 – only R1 changes.
  • Assuming the program ends automatically after the last line – you must see a HALT or similar stop instruction.

🎓 Practice Tip: Write your own 3–5 line program that uses SUB and MUL, then trace it. The more you practice, the easier it becomes to spot errors in exam questions.

Revision

Log in to practice.

1 views 0 suggestions