Skip to content

9618 · Unit 26 · Paper 3: Advanced theory

Low-level programming and addressing.

9618 · Unit 26 · Low-level programming and addressing
Revisionbyexamplicity

Instructions and symbolic addresses

§20.1 · Low-level programming

Underneath every program, the processor takes tiny steps: load a number, add, store, jump. Low-level programming writes those steps directly, close to the processor’s instruction set. Each instruction specifies an opcode and, where needed, an operand; the accumulator (ACC) holds working values.

Tavren’s four hire counters hold 7, 3, 6 and 2 spare lanterns. A stock-total loop must add all four counts. Its assembled program begins at address 200; every instruction and data word occupies one location.

A label names a location, not its contents

Each label stands for an address. The location at that address holds the contents.

labeladdresscontentsLOOP204LDX STOCK⋮STOCK2157SECOND2163THIRD2176FOURTH2182TOTAL2190COUNT2200
SymbolAddressWhat is stored there initially
LOOP204LDX STOCK: the loop’s first instruction
STOCK2157: the first stock count
SECOND
THIRD
FOURTH
216
217
218
3
6
2
TOTAL2190: the running total
COUNT2200: counts already added

How the assembler replaces each label

The processor never sees a label. The assembler swaps each one for an address before the program runs.

Source instructionResolved instructionEffect
STO TOTALSTO 219Copy ACC into location 219.
LDX STOCKLDX 215Read from base address 215 plus IX.
JPN LOOPJPN 204After a comparison, jump to 204 if unequal.

Symbolic addressing writes a name where an address is needed. It does not specify an extra way to fetch the value. For example, LDD TOTAL is still direct addressing: the assembler replaces TOTAL with 219.

Trace the first three instructions

1
LDM #0 puts zero in ACC.
2
STO TOTAL stores that zero at address 219; ACC remains zero.
3
LDD TOTAL loads the contents of 219; it does not load the address 219.

Assembly depends on the processor. Use the instruction meanings supplied with a task, including the exact branch condition; a familiar-looking mnemonic is not enough.

Your turn

1 Which address does the label TOTAL stand for?

2 Does LDD TOTAL put 219 or the value stored at 219 into the ACC?

3 The same program is assembled to start at 300 instead of 200. Which address does STOCK get?

  1. 1
    219
  2. 2
    the value stored at 219 (0 at the start), not 219 itself
  3. 3
    315: every location moves up by 100
Tick what you can do now
Practise with s26/33 Q11(a), (b)
1 / 5
9618 · Unit 26 · Low-level programming and addressing
Revisionbyexamplicity

Immediate and direct addressing

§20.1 · Low-level programming

An operand is only a number, so the processor must be told what it means: the value to use, or where to find it. With immediate addressing, the operand supplies the value itself. With direct addressing, the operand supplies the address at which the value is stored.

Both instructions carry the same digits, 215, yet they leave different numbers in the ACC. Find the box where each route gets its number.

Immediate: the operand is the value

LDM #215no memory read: 215 is the valuevalue 215ACC215

Direct: the operand is an address

LDD 215address 215Memory[215]7data 7ACC7

Same digits: value or address?

Memory at 215 contains 7; at 216 it contains 3. The # prefix marks a denary literal in this instruction set.

InstructionFollow the operandACC afterwards
LDM #215Use the literal 215.215
LDD 215Read Memory[215].7
LDM #7Use the literal 7.7
LDD 216Read Memory[216].3

Real programs mix the two: a direct load fetches a count, then an immediate add adjusts it by a fixed amount.

A constant adjustment to the first count
LDD 215
ADD #2
STO 219
END
ACC ← Memory[215] = 7The load reads the stored count.ACC ← 7 + 2 = 9The immediate operand 2 is the amount to add.Memory[219] ← 9The store changes TOTAL; the original count remains 7.

ADD in each mode

Mix the two up and the answer is wrong, but it still looks like a sensible number.

Starting ACC = 7Operand usedResult
ADD #216216223
ADD 216Memory[216] = 310

Use an immediate operand for a fixed constant such as initial zero or the number of counters. Use a direct address to read a count or a running total that can change. A store needs a destination location, not an immediate data value.

Your turn

1 Memory at 215 holds 7 and at 216 holds 3. Give the ACC after LDM #216.

2 Give the ACC after LDD 216.

3 The ACC holds 7 (from LDD 215). Give the ACC after ADD #217, and after ADD 217 instead.

  1. 1
    216: the operand is the value
  2. 2
    3: the value stored at 216
  3. 3
    224 (7 + 217), and 13 (7 + the 6 stored at 217)
Tick what you can do now
Practise with s26/33 Q11(a), (b)
2 / 5
9618 · Unit 26 · Low-level programming and addressing
Revisionbyexamplicity

Indirect and indexed addressing

§20.1 · Low-level programming

To work along a list, a program needs an address that can change while the instruction stays the same. Indirect addressing reads a stored address before reading the data. Indexed addressing adds the index register (IX) to the operand to calculate the data’s address.

The stock counts occupy 215–218: 7, 3, 6, 2. A pointer at address 300 holds 217. For the indexed example, IX = 2.

Both routes end at address 217 and load 6. Watch where the 217 comes from: read out of memory, or worked out by adding.

Indirect: follow the pointer

LDI 300Memory[300]217address 217Memory[217]6data 6ACC6

Indexed: add the index

LDX 215 · IX = 2215 + 2 = 217address 217Memory[217]6data 6ACC6

Find the effective address before the value

Work out the address first, then read what is stored there. The first row is the trap: with no pointer step, the 217 stored at 300 arrives as data.

InstructionEffective data addressACC
LDD 300300217
LDI 300Memory[300] = 2176
LDX 215, IX = 2215 + 2 = 2176

The same final value 6 comes from different routes. Changing Memory[300] changes the indirect target; changing IX changes the indexed target. Neither operation changes the instruction’s operand.

Move the index to the fourth stock count
LDR #3
LDX 215
END

LDR #3 sets IX, not ACC. The load uses 215 + 3 = 218, so ACC becomes 2. Index 0 selects the first element; four elements use indexes 0–3.

Your turn

1 Memory[300] is changed to 216. Give the ACC after LDI 300.

2 IX holds 3. Give the ACC after LDX 215.

3 Which of LDI 300 and LDX 215 reads memory twice?

  1. 1
    3: 300 holds 216, and 216 holds 3
  2. 2
    2: 215 + 3 = 218, which holds 2
  3. 3
    LDI 300: once for the pointer, once for the data
Tick what you can do now
3 / 5
9618 · Unit 26 · Low-level programming and addressing
Revisionbyexamplicity

Relative addressing

§20.1 · Low-level programming

A program is not always loaded at the same place in memory, and fixed addresses would then point at the wrong things. Relative addressing represents a target as an offset from a specified base address. Moving the program preserves internal distances, so the same offsets still reach the corresponding targets.

In this address-reference model, a base register (BR) holds the program’s start address. The offset counts memory locations from that base.

target address = contents of BR + offset

Move the program from 200 to 400: the base changes, the +4 does not.

BR = 200Target: 204+4200 + 4 = 204BR = 400Target: 404+4400 + 4 = 404

Offset to address, and back

Offset = target − base = 204 − 200 = 4LOOP is four locations from the program start.STOCK: 200 + 15 = 215The same data offset after relocation gives 400 + 15 = 415.

Write a relative operand for the required operation

Each operation is written both ways: the absolute address while BR holds 200, and the offset that works wherever the program sits.

OperationAbsolute form · BR = 200Declared relative form
Load a stock countLDD 215LDD [BR]+15
Store a stock countSTO 215STO [BR]+15
Branch if unequalJPN 204JPN [BR]+4

Here [BR] means the contents of BR. At BR = 400 the stock forms reach 415 and the branch reaches 404, with unchanged offsets. Absolute operands must change to those new addresses; symbolic reassembly can recalculate them.

The bracketed form is an illustrative base-relative notation, not part of the standard instruction set. The executable stock loop uses absolute operands. Read the supplied machine’s notation before writing relative code.
Your turn

1 BR holds 600. Which address does LDD [BR]+15 read?

2 BR holds 700 and the target is at 708. What offset reaches it?

3 The program is moved to a new base. Why does a relative operand not need to change?

  1. 1
    615
  2. 2
    8: 708 − 700
  3. 3
    the offset is a distance inside the program, and the distance is the same wherever it is loaded
Tick what you can do now
4 / 5
9618 · Unit 26 · Low-level programming and addressing
Revisionbyexamplicity

Writing and checking a low-level loop

§20.1 · Low-level programming

Assembly has no FOR loop, so you build one from explicit state changes: initialise, load one count, update the total, advance, test and branch. The branch must stop before an index goes beyond the stock data.

Four counts 7, 3, 6 and 2 are stored consecutively at STOCK. The task is to leave their sum in TOTAL and in ACC. Here CMP tests equality; JPN jumps when that comparison is not equal.

Instructions, then stored data · origin 200
LDM #0
STO TOTAL
STO COUNT
LDR #0
LOOP: LDX STOCK
ADD TOTAL
STO TOTAL
INC IX
LDD COUNT
INC ACC
STO COUNT
CMP #4
JPN LOOP
LDD TOTAL
END

STOCK: 7
SECOND: 3
THIRD: 6
FOURTH: 2
TOTAL: 0
COUNT: 0

What each pass does

  • COUNT and IX start at 0. Each pass adds one count, then both go up by 1.
  • LDD COUNT puts the count into the ACC so CMP can test it; the running sum stays safe in TOTAL.
  • For counts 1, 2 and 3 the compare with 4 is False, so JPN returns to LOOP.
  • When COUNT is 4 the compare is True: the program falls through to LDD TOTAL, then END. IX = 4 is never used by LDX.

Match the procedural algorithm

The high-level loop sets Total ← 0, then for Index 0 to 3 does Total ← Total + Stock[Index]. Assembly splits that one assignment into load, add and store, and makes the loop control explicit.

7 + 3 + 6 + 2 = 18The procedural loop independently gives the same total.Final ACC = TOTAL = 18Reload the total after the loop’s count comparison.

Follow one pass, line by line

Pass 1 in slow motion: nine instructions, most of them changing one register or one memory word. Watch IX and COUNT both reach 1.

Pass 1, one instruction at a time
Each instruction that runs is a row. Its value goes in the column it writes. Together they make the blue pass 1 row of the whole run.
AddressStatement that runsACCIXTOTALCOUNTWritesWhat happens
204LDX STOCK7ACC 7read STOCK + IX = 215 + 0 = 215: 7
read STOCK + IX = 215 + 0 = 215: 7
205ADD TOTAL7ACC 77 + 0 (TOTAL) = 7
7 + 0 (TOTAL) = 7
206STO TOTAL7TOTAL 7copy the ACC (7) into TOTAL
copy the ACC (7) into TOTAL
207INC IX1IX 10 + 1 = 1
0 + 1 = 1
208LDD COUNT0ACC 0load the value stored in COUNT: 0
load the value stored in COUNT: 0
209INC ACC1ACC 10 + 1 = 1
0 + 1 = 1
210STO COUNT1COUNT 1copy the ACC (1) into COUNT
copy the ACC (1) into COUNT
211CMP #4test is 1 equal to 4? False
test is 1 equal to 4? False
212JPN LOOPjump the compare was False, so jump to LOOP
jump the compare was False, so jump to LOOP

The whole run, pass by pass

One pass shows how the loop works; the whole run shows that it stops. Read down the COUNT column: the loop ends on the row where it reaches 4.

ACCIXTOTALCOUNTWhat decided it
set up0000TOTAL, COUNT and IX all start at 0
TOTAL, COUNT and IX all start at 0
pass 17701171adds 7; COUNT is 1, not 4, so JPN jumps back to LOOP
adds 7; COUNT is 1, not 4, so JPN jumps back to LOOP
pass 2310122102adds 3; COUNT is 2, not 4, so JPN jumps back to LOOP
adds 3; COUNT is 2, not 4, so JPN jumps back to LOOP
pass 3616233163adds 6; COUNT is 3, not 4, so JPN jumps back to LOOP
adds 6; COUNT is 3, not 4, so JPN jumps back to LOOP
pass 421834184184adds 2; COUNT is 4, so no jump: LDD TOTAL loads 18, then END
adds 2; COUNT is 4, so no jump: LDD TOTAL loads 18, then END
Instructions, then stored data · origin 200
LDM #0
STO TOTAL
STO COUNT
LDR #0
LOOP: LDX STOCK
ADD TOTAL
STO TOTAL
INC IX
LDD COUNT
INC ACC
STO COUNT
CMP #4
JPN LOOP
LDD TOTAL
END

STOCK: 7
SECOND: 3
THIRD: 6
FOURTH: 2
TOTAL: 0
COUNT: 0

One pass adds one element

IX at loadValueTOTAL after add
077
1310
2616
3218

COUNT and IX start at zero. After each addition both increase by one. LDD COUNT puts the count into ACC so CMP can test it; the running sum remains safely stored in TOTAL.

How the loop stops

For counts 1, 2 and 3, comparison with 4 is false, so JPN returns to LOOP. When COUNT is 4, equality is true: execution continues to LDD TOTAL, then END. IX = 4 is never used by LDX.

Match the procedural algorithm

The high-level loop sets Total ← 0, then for Index 0 to 3 performs Total ← Total + Stock[Index]. Assembly separates that one assignment into load, add and store, and makes the loop control explicit.

7 + 3 + 6 + 2 = 18The procedural loop independently gives the same total.Final ACC = TOTAL = 18Reload the total after the loop’s count comparison.
Your turn

1 The four counts are changed to 5, 1, 4 and 9. What is in TOTAL at the end?

2 How many times does JPN LOOP jump back to LOOP?

3 CMP #4 is changed to CMP #3. What is in TOTAL at the end, and why?

  1. 1
    19: 5 + 1 + 4 + 9
  2. 2
    3: after counts 1, 2 and 3; at 4 it falls through
  3. 3
    16: the loop stops when COUNT is 3, so only 7 + 3 + 6 are added
Tick what you can do now
5 / 5