Instructions and symbolic addresses
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.
| Symbol | Address | What is stored there initially |
|---|---|---|
| LOOP | 204 | LDX STOCK: the loop’s first instruction |
| STOCK | 215 | 7: the first stock count |
| SECOND THIRD FOURTH | 216 217 218 | 3 6 2 |
| TOTAL | 219 | 0: the running total |
| COUNT | 220 | 0: 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 instruction | Resolved instruction | Effect |
|---|---|---|
STO TOTAL | STO 219 | Copy ACC into location 219. |
LDX STOCK | LDX 215 | Read from base address 215 plus IX. |
JPN LOOP | JPN 204 | After 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
LDM #0 puts zero in ACC.STO TOTAL stores that zero at address 219; ACC remains zero.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.
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?
- 1219
- 2the value stored at 219 (0 at the start), not 219 itself
- 3315: every location moves up by 100
Immediate and direct addressing
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
Direct: the operand is an address
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.
| Instruction | Follow the operand | ACC afterwards |
|---|---|---|
LDM #215 | Use the literal 215. | 215 |
LDD 215 | Read Memory[215]. | 7 |
LDM #7 | Use the literal 7. | 7 |
LDD 216 | Read Memory[216]. | 3 |
Real programs mix the two: a direct load fetches a count, then an immediate add adjusts it by a fixed amount.
LDD 215 ADD #2 STO 219 END
ADD in each mode
Mix the two up and the answer is wrong, but it still looks like a sensible number.
| Starting ACC = 7 | Operand used | Result |
|---|---|---|
ADD #216 | 216 | 223 |
ADD 216 | Memory[216] = 3 | 10 |
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.
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.
- 1216: the operand is the value
- 23: the value stored at 216
- 3224 (7 + 217), and 13 (7 + the 6 stored at 217)
Indirect and indexed addressing
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
Indexed: add the index
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.
| Instruction | Effective data address | ACC |
|---|---|---|
LDD 300 | 300 | 217 |
LDI 300 | Memory[300] = 217 | 6 |
LDX 215, IX = 2 | 215 + 2 = 217 | 6 |
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.
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.
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?
- 13: 300 holds 216, and 216 holds 3
- 22: 215 + 3 = 218, which holds 2
- 3
LDI 300: once for the pointer, once for the data
Relative addressing
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.
Move the program from 200 to 400: the base changes, the +4 does not.
Offset to address, and back
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.
| Operation | Absolute form · BR = 200 | Declared relative form |
|---|---|---|
| Load a stock count | LDD 215 | LDD [BR]+15 |
| Store a stock count | STO 215 | STO [BR]+15 |
| Branch if unequal | JPN 204 | JPN [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.
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?
- 1615
- 28: 708 − 700
- 3the offset is a distance inside the program, and the distance is the same wherever it is loaded
Writing and checking a low-level loop
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.
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 COUNTputs the count into the ACC soCMPcan test it; the running sum stays safe in TOTAL.- For counts 1, 2 and 3 the compare with 4 is False, so
JPNreturns to LOOP. - When COUNT is 4 the compare is True: the program falls through to
LDD TOTAL, thenEND. IX = 4 is never used byLDX.
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.
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.
| Address | Statement that runs | ACC | IX | TOTAL | COUNT | Writes | What happens |
|---|---|---|---|---|---|---|---|
| 204 | LDX STOCK | 7 | ACC 7 | read STOCK + IX = 215 + 0 = 215: 7 | |||
| read STOCK + IX = 215 + 0 = 215: 7 | |||||||
| 205 | ADD TOTAL | 7 | ACC 7 | 7 + 0 (TOTAL) = 7 | |||
| 7 + 0 (TOTAL) = 7 | |||||||
| 206 | STO TOTAL | 7 | TOTAL 7 | copy the ACC (7) into TOTAL | |||
| copy the ACC (7) into TOTAL | |||||||
| 207 | INC IX | 1 | IX 1 | 0 + 1 = 1 | |||
| 0 + 1 = 1 | |||||||
| 208 | LDD COUNT | 0 | ACC 0 | load the value stored in COUNT: 0 | |||
| load the value stored in COUNT: 0 | |||||||
| 209 | INC ACC | 1 | ACC 1 | 0 + 1 = 1 | |||
| 0 + 1 = 1 | |||||||
| 210 | STO COUNT | 1 | COUNT 1 | copy the ACC (1) into COUNT | |||
| copy the ACC (1) into COUNT | |||||||
| 211 | CMP #4 | test is 1 equal to 4? False | |||||
| test is 1 equal to 4? False | |||||||
| 212 | JPN LOOP | jump 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.
| ACC | IX | TOTAL | COUNT | What decided it | |
|---|---|---|---|---|---|
| set up | 0 | 0 | 0 | 0 | TOTAL, COUNT and IX all start at 0 |
| TOTAL, COUNT and IX all start at 0 | |||||
| pass 1 | 7701 | 1 | 7 | 1 | adds 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 2 | 31012 | 2 | 10 | 2 | adds 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 3 | 61623 | 3 | 16 | 3 | adds 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 4 | 2183418 | 4 | 18 | 4 | adds 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 | |||||
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 load | Value | TOTAL after add |
|---|---|---|
| 0 | 7 | 7 |
| 1 | 3 | 10 |
| 2 | 6 | 16 |
| 3 | 2 | 18 |
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.
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?
- 119: 5 + 1 + 4 + 9
- 23: after counts 1, 2 and 3; at 4 it falls through
- 316: the loop stops when COUNT is 3, so only 7 + 3 + 6 are added