Low-level Programming
Writing low-level programs with the 9618 instruction set: labelled data, the five addressing modes, loops built from a compare and jumps, totalling and counting, character codes for input and output, and finding the larger of two numbers.
Writing a program: plan the data first
Plan your data before your code. Give every value a label, and put its data line after END.
Plan the data before the code.
- Give every variable and constant a label: a name ending in a colon, such as
TOTAL:. The assembler turns it into an address. - Put the data lines after
END, away from the code. Then the processor never fetches them as instructions.
This program adds a delivery charge of 5 to the price stored at address 150. It stores the answer in TOTAL.
| Instruction | What it does |
|---|---|
LDM #5 | loads the number 5 itself |
STO CHARGE | keeps it as a labelled constant |
LDD 150 | loads the value stored at address 150, not the number 150 |
ADD CHARGE | adds the contents of CHARGE |
Example:With 36 at address 150, TOTAL ends as 41.
; TOTAL ← the price at 150 + 5 LDM #5 ; keep 5 as the constant CHARGE STO CHARGE ; the price stored at address 150 LDD 150 ADD CHARGE STO TOTAL END CHARGE: 0 TOTAL: 0 150 36
Low-level: the five addressing modes
An addressing mode tells the processor what the operand is: the value itself, an address, or a starting point.
An addressing mode says how an instruction's operand is used.
| Mode | The operand is... | Example |
|---|---|---|
| Immediate | the value itself | LDM #200 loads 200 |
| Direct | the address of the value | LDD 200 loads what is stored at address 200 |
| Indirect | the address of a location that holds the address of the value | LDI 200 |
| Indexed | an address; the contents of the index register, IX, are added to it | LDX 200 |
| Relative | an offset: the address is worked out from the address of the current instruction | no Cambridge instruction |
Remember:Adding 1 to IX each time round a loop steps through an array.
The box below follows each mode to its value, with IX = 3.
IX = 3
LDM #200 immediate
ACC ← 200
LDD 200 direct
ACC ← [200] = 205
LDI 200 indirect
ACC ← [[200]] = [205] = 63
LDX 200 indexed
ACC ← [200 + [IX]] = [203] = 42
relative (no Cambridge instruction),
from 196, offset 7:
ACC ← [196 + 7] = [203] = 42Indirect addressing: following an address
With indirect addressing, the operand holds an address. The processor follows that address to find the value.
With indirect addressing, the operand names a location that holds an address, not the value itself. It is like a note that says "the price is in box 151".
- Here
CHOICEholds 151, the address of the chosen price. LDI CHOICEreadsCHOICEand finds 151.- It then loads the value stored at address 151, which is 48.
- Adding
CHARGEmakesTOTAL53.
Watch out:LDD CHOICE would load 151, the address itself.
Change the number in CHOICE, and the same code uses a different price. So CHOICE works as a pointer: a variable that holds an address.
To move a pointer along a list, add 1 to it each time round a loop: LDD CHOICE, INC ACC, STO CHOICE.
; TOTAL ← [[CHOICE]] + [CHARGE] LDI CHOICE ADD CHARGE STO TOTAL END ; CHOICE holds an address, not a price CHOICE: 151 CHARGE: 5 TOTAL: 0 ; three prices, from address 150 150 36 48 52
Loops: a counter, a compare and two jumps
A low-level loop is a label, a compare and two jumps. Its counter must hit the stop value exactly.
A loop is built from a label and jumps. This program outputs N stars.
- At
LOOP, it loads the counterNand compares it with 0. JPE DONEleaves the loop when the compare is True.- Otherwise,
DEC ACCandSTO Ntake 1 off the counter. LDM #42andOUTshow a star (42 is the ASCII code of*).JMP LOOPgoes back to test again.
The test comes first, so N = 0 outputs nothing.
Watch out:CMP only tests whether two values are equal. So the counter must reach 0 exactly. A counter that went down by 2 from 5 would go from 1 to −1, and the loop would never stop.
; output N stars LOOP: LDD N CMP #0 ; no stars left: leave the loop JPE DONE DEC ACC STO N ; 42 is the code of * LDM #42 OUT JMP LOOP DONE: END N: 5
Writing a low-level program
You build a low-level loop by hand: load, work, store, change a counter, compare, and jump back.
A low-level loop is built by hand, in six steps:
- load
- work
- store
- change a counter
- compare
- jump back.
This program adds up five numbers stored from the label list.
IXis the position in the list.countis how many numbers are still to add.CMPcompares the accumulator with a value.JPNjumps if that comparison was false. So the loop goes round untilcountreaches 0.
Example:It stops with total = 50 after 47 instructions.
Trace it one instruction at a time, and watch ACC, IX and memory change.
; IX ← 0: the start of the list LDR #0 ; ACC ← [list + IX] loop: LDX list ; ACC ← ACC + [total] ADD total ; [total] ← ACC STO total ; move to the next item INC IX LDD count ; one fewer to add DEC ACC STO count ; was that the last one? CMP #0 ; if not, go round again JPN loop END list: 7 12 5 20 6 total: 0 count: 5
Counting the items that match
To count matches, start a counter at 0 and add 1 each time the condition is True.
To count, keep a counter in memory. It starts at 0, and you add 1 to it each time the condition is True.
This program counts how many items in LIST are equal to TARGET.
IXsteps along the list.- The item 0 marks the end of the list.
- After
CMP #0,CMP TARGETcan test the same item. - When the item does not match,
JPN NEXTjumps over the 3 instructions that add 1 toCOUNT.
Remember:A compare never changes the ACC. So after CMP #0 the ACC still holds the item.
Example:With the items 7, 3, 7 and 9 and TARGET 7, COUNT ends as 2.
; COUNT ← how many items equal TARGET LDR #0 LOOP: LDX LIST ; 0 marks the end of the list CMP #0 JPE DONE ; the ACC still holds the item CMP TARGET JPN NEXT LDD COUNT INC ACC STO COUNT NEXT: INC IX JMP LOOP DONE: END TARGET: 7 COUNT: 0 LIST: 7 3 7 9 0
Input and output: character codes
IN and OUT use character codes, not digit values. Subtract 48 after IN; add 48 before OUT.
Keys and the screen use ASCII codes: a number for each character. They do not use the value of a digit.
IN | OUT | |
|---|---|---|
| What it does | waits for a key and stores its ASCII value in the ACC | shows the character whose code is in the ACC |
| Example | the key 3 gives 51 | the code 55 shows the digit 7 |
| Convert | subtract 48, the code of 0, to get the number 3 | add 48 to a digit's value before OUT |
Example:This program reads two digit keys and adds their values. The keys 3 and 4 give 7, which OUT needs as the code 55.
Watch out:It works only while the answer is a single digit.
; output the sum of two digit keys IN ; the key 3 is code 51: take 48 away SUB #48 STO FIRST IN SUB #48 ADD FIRST ; back to a character code: add 48 ADD #48 OUT END FIRST: 0
Finding the larger of two numbers
With only an "equal" test, find the larger number by counting up from 0 until you meet one of them.
The 9618 instruction set has no greater-than test. CMP only tests whether two values are equal, and JPE and JPN act on that.
One way works for two whole numbers that are 0 or more: count up from 0. Whichever of A and B the count reaches first is not the larger. So MAX is the other one.
Cholds the count. Each pass compares it withA, then withB.- If
CreachesBfirst,ABIGloadsA. - If
CreachesAfirst,BBIGloadsB. - Both paths end at
SAVE, which stores the loaded value inMAX.
Example:With A = 4 and B = 2, the count reaches 2 first, so MAX is 4.
; MAX ← the larger of A and B LDM #0 STO C ; C counts up: 0, 1, 2, ... LOOP: LDD C ; C reached A first? then MAX is B CMP A JPE BBIG ; C reached B first? then MAX is A CMP B JPE ABIG INC ACC STO C JMP LOOP ABIG: LDD A JMP SAVE BBIG: LDD B SAVE: STO MAX END A: 4 B: 2 C: 0 MAX: 0
KEY TERMS
CHECK YOURSELF
1.Address 200 holds 205 and address 205 holds 63. Which value does LDI 200 load into the ACC?
2.A task says: store the number 30 as a constant labelled LIMIT. Which two instructions do it?
3.PTR holds 300, and address 300 holds 7. What does LDI PTR load into the ACC?
4.A loop ends with CMP #0 and then JPN LOOP. When does the program jump back to LOOP?
5.The key 7 is pressed and IN runs. What does the ACC hold?
6.Which test can the 9618 instruction set make with one compare?
7.Why are a program's data lines written after END?