A-Level Computer Science / Unit 4: CPU Operation and Low-Level Processing

4.2.4 Tracing Assembly-Language Programs

πŸ”’ Lesson slides are available to signed-in users. Sign in

Tracing Assembly-Language Programs

A trace table records how a program changes the processor's state as its instructions execute. Follow one instruction at a time, update only the values that change, and use the actual next instruction after every jump.

By the end of this page, you should be able to:

  • trace changes to ACC, IX and memory;
  • explain why a store instruction does not clear ACC;
  • evaluate comparisons and follow conditional or unconditional jumps;
  • record the effects of IN and OUT;
  • show the program state clearly in a trace table.

1. Trace one instruction at a time

Begin with the given values of the registers and memory. Read the instruction at the current program address, work out its effect, and then record the state after it executes.

  1. Write down the starting values for ACC, IX and any memory locations used.
  2. Process the instruction at the current address.
  3. Update only the register, memory location or output that changes.
  4. Follow the normal next instruction or the jump target.
  5. Continue until the program ends or the question asks you to stop.
Trace-table rule: when an instruction does not change a register or memory location, that value stays as it was.

2. Track registers and memory separately

A store instruction copies the value in ACC to the specified memory location. It does not remove the value from ACC. Later instructions can change ACC while the stored memory value remains unchanged.

Instruction or actionWhat to recordWhat stays the same
STO address The contents of that memory location become the value in ACC. ACC keeps its value; IX is unchanged.
INC IX or DEC IX IX increases or decreases by one. ACC and memory stay unchanged.
ADD or SUB ACC changes according to the arithmetic operation. IX and unrelated memory locations stay unchanged.

STO does not clear ACC

Copying a value into memory is not the same as moving it out of ACC. After STO, record the copied value in memory and keep the same value in ACC unless a later instruction changes it.

3. Evaluate comparisons before following a jump

CMP compares ACC with an immediate value or with the contents at a memory address. CMI follows an address stored in memory before comparing ACC with the data found there. Record whether the comparison is true or false before deciding what runs next.

Jump instructionWhen the jump is taken
JPEWhen the comparison result is true.
JPNWhen the comparison result is false.
JMPEvery time it executes; it does not check a comparison result.

If a conditional jump is not taken, continue with the next instruction in sequence. If it is taken, go to its target. An unconditional JMP always transfers control to its target.

Good tracing habit: write the comparison result first, then decide whether the branch is taken. This makes it easier to follow the correct path through the program.

4. Trace input and output

IN reads a character and places its ASCII value in ACC. OUT displays the character represented by the ASCII value currently in ACC. The output is a character, not the denary code itself.

InstructionEffect to record
INUpdate ACC to the ASCII value of the input character.
OUTRecord the character represented by the ASCII value in ACC.
Keep code and character distinct: the ACC holds an ASCII value; the output column records the character that value represents.

Interactive: step through a short program

This original example is set in a theatre's ticket-checking kiosk. Choose an input character, then use Next instruction to update the trace. You can move backward to review a step. The program stores the input, compares it, follows a branch, updates a register and memory, and outputs a character.

ASSEMBLY TRACE WALKTHROUGH

Choose the input character

Ready

Input for IN:

Program

  • 820IN
  • 821STO 640
  • 822INC IX
  • 823CMP #77
  • 824JPE EQUAL_TAG
  • 825LDM #68
  • 826OUT
  • 827JMP ENDPOINT
  • 828EQUAL_TAG: ADD #13
  • 829STO 642
  • 830OUT
  • 831JMP ENDPOINT
  • 832ENDPOINT: END

Current state

ACC22
IX24
Memory [640]29
Memory [642]37
Compareβ€”
Outputβ€”

Next instruction: 820

Trace instructions in order

Choose M or Q to set the input. Then step through the program one instruction at a time.

PCInstructionACCIXM[640]M[642]CompareOutput
Trace rows will appear here.

Starting state: ACC = 22, IX = 24, memory[640] = 29, memory[642] = 37. The two inputs follow different paths.

5. Use this checklist for a trace question

  • Copy the given starting values accurately.
  • Record the effect of each instruction before moving to the next one.
  • Keep ACC, IX, memory and output in separate columns.
  • After CMP or CMI, decide whether the result is true or false.
  • Follow the jump target when a branch is taken; skip instructions that are not executed.
  • Do not change values that the current instruction does not affect.

Do not trace skipped instructions

A taken jump changes the next instruction. Values do not change while skipped lines are bypassed. Resume the trace at the target instruction.

6. Check your understanding

Try each question before opening the suggested response.

  1. ACC contains 31. What happens to ACC when STO 645 executes?
    Suggested response

    ACC remains 31. The instruction copies 31 into memory location 645.

  2. ACC contains 56. A compare instruction checks ACC against 61. Is the comparison true or false?
    Suggested response

    False, because 56 is not equal to 61.

  3. After that comparison, what does JPN do?
    Suggested response

    It takes the jump because the comparison result is false.

  4. An input character has ASCII value 84. What is in ACC after IN?
    Suggested response

    ACC contains 84, the ASCII value of the input character.

  5. ACC contains the ASCII value for the character T. What does OUT display?
    Suggested response

    It displays the character T, not the denary code.

Final check

Show the state after each relevant instruction. Include the comparison result and the address of the next instruction after a jump. Keep stored memory values and register contents distinct.