Processor Fundamentals and Assembly Language: Question 9

Syllabus 4.2

Structured AS 8 marks

A processor has an Accumulator (ACC). The table below shows the meaning of each opcode used in the program in this question.

Opcode Operand Meaning
LDM #n Immediate addressing. Load the number n to ACC.
DEC <register> Subtract 1 from the contents of the register (e.g. ACC).
CMP #n Compare the contents of ACC with the number n.
JPN <n> Relative addressing. If the previous compare was False, jump to the instruction at address (address of this JPN instruction + n), where n may be negative. If the previous compare was True, execution continues with the next instruction as normal.
END Return control to the operating system.

The program stored in memory is:

20  LDM #3
21  DEC ACC
22  CMP #0
23  JPN -2
24  END

(a) Identify the addressing mode used by LDM #3, and identify the addressing mode used by JPN -2. [2]

(b) The instruction JPN -2 at address 23 is executed for the first time. State the address that execution jumps to, showing how this address is calculated from the operand -2. [2]

(c) Complete a trace of this program, stating the value of ACC after each instruction executes, and state how many times in total the instruction DEC ACC executes before the program ends. [4]

Show worked solution Hide worked solution

Worked solution

Part (a): Addressing modes

  • LDM #3 uses immediate addressing. The value 3 is given directly in the instruction (shown by the #), and is loaded straight into ACC.
  • JPN -2 uses relative addressing. The operand -2 is not an address itself, but a signed offset that is added to the address of the JPN instruction to calculate the actual target address.

[2 marks]: 1 for LDM #3 = immediate addressing, 1 for JPN -2 = relative addressing.

Part (b): Calculating the jump target

JPN -2 is stored at address 23. Because it uses relative addressing, the target address is calculated by adding the operand to the address of the JPN instruction itself, not to the address of the following instruction:

Target address = address of JPN + operand
               = 23 + (-2)
               = 21

So the first time JPN -2 is executed, execution jumps to address 21.

[2 marks]: 1 for the correct method (address of JPN plus the operand), 1 for the correct target address 21.

Part (c): Tracing the program

CMP #0 compares ACC with 0: while ACC ≠ 0 the compare is False, so JPN -2 is taken and the loop repeats; once ACC = 0 the compare is True, so JPN -2 is not taken and execution falls through to END.

Instruction executedACCCompare result (at CMP)JPN -2 taken?
LDM #33--
DEC ACC2--
CMP #02False (2 ≠ 0)-
JPN -22-Yes -> jump to 21
DEC ACC1--
CMP #01False (1 ≠ 0)-
JPN -21-Yes -> jump to 21
DEC ACC0--
CMP #00True (0 = 0)-
JPN -20-No -> continue to 24
END0--

DEC ACC executes three times in total (reducing ACC from 3 to 2, then 2 to 1, then 1 to 0), and JPN -2 is taken on the first two passes and not taken on the third, when the program falls through to END with ACC = 0.

[4 marks]: marks awarded for correctly tracing ACC decreasing 3 -> 2 -> 1 -> 0, correctly identifying the compare is False (jump taken) on the first two passes, correctly identifying the compare is True (jump not taken) on the third pass, and the correct count of 3 executions of DEC ACC.

Final answers

  • (a) LDM #3 = immediate addressing; JPN -2 = relative addressing.
  • (b) Target address = 23 + (-2) = 21.
  • (c) Final ACC = 0; DEC ACC executes 3 times in total.