Assembly Language Programming
Machine code and assembly language: opcodes and operands, the two-pass assembler, absolute and symbolic addresses, the instruction groups, tracing a program, addressing modes, and logical, arithmetic and cyclic shifts.
Machine code: an opcode and an operand
A processor runs only binary machine code. Assembly language writes the same instructions in a form people can read.
A processor can only run machine code: instructions written in binary (0s and 1s). An instruction has up to two parts:
| Part | What it says | Example |
|---|---|---|
| Opcode | which operation to carry out, such as load, add or jump | LDD |
| Operand | what that operation works on: a number, the address of a memory location, or a register | 6, or ACC in INC ACC |
Every instruction has an opcode. Most instructions also have an operand.
Example:In the figure an instruction is 8 bits. The first 4 are the opcode and the last 4 the operand. So 0010 0110 means LDD 6.
Assembly language writes the same instruction as a mnemonic (a short code word) and an operand: LDD 6. People can read it.
An assembler translates each assembly language instruction into one machine code instruction.
machine code 0010 0110
opcode operand
assembly LDD 6Assembly language, machine code and the two-pass assembler
An assembler reads the program twice. Pass 1 finds every label's address; pass 2 writes the machine code.
Machine code is the binary the processor runs. Each instruction is an opcode (what to do) and an operand (a value or an address).
Assembly language writes each machine code instruction with a mnemonic and labels (names that stand for addresses). An assembler translates it, in two passes:
| Pass 1 | Pass 2 |
|---|---|
| reads the program line by line | replaces each mnemonic with its opcode |
| removes comments | replaces each label with its address |
| counts each line's address | |
| puts every label in a symbol table (a list of labels and their addresses) |
A forward reference is a label used before its own line. Below, JPE DONE uses DONE before the line DONE: END. In pass 1 the label goes into the symbol table first without an address. It gets one when its line is reached.
Remember:Forward references are why two passes are needed.
; ACC ← the number stored at NUM LDD NUM ; is ACC the code of '0'? LOOP: CMP #48 ; if it is, stop JPE DONE ; show the character in ACC OUT ; the code of the digit one lower DEC ACC JMP LOOP DONE: END ; the code of '3' NUM: 51 ; the program prints 321
Absolute and symbolic addresses
An address can be a number (absolute) or a label (symbolic). The assembler turns every label into a number.
An operand's address can be written in two ways:
- An absolute address is the number of a memory location, such as 220 in the next note's program.
- A symbolic address is a label, such as
LIST, that stands for a location.
Pass 1 gives each label its address in the symbol table. Pass 2 swaps each label for that number. No labels are left in the machine code.
| Absolute | Symbolic | After pass 2 |
|---|---|---|
LDX 220 | LDX LIST | LDX 220 |
JPE 207 | JPE DONE | JPE 207 |
JMP 201 | JMP LOOP | JMP 201 |
So, written with labels, the next note's program assembles to the same machine code. Labels also help when you edit: insert a line before LOOP, and pass 2 gives LOOP and DONE their new addresses.
Watch out:Relative addressing, below, is an addressing mode, not a way to write an address.
Instruction groups, and tracing a program
Instructions come in five groups. A trace follows them in the order they run, not the order they are written.
The syllabus sorts the instructions into groups (full list below):
| Group | Instructions |
|---|---|
| data movement | LDM, LDD, LDI, LDX, LDR, MOV, STO |
| input and output | IN, OUT |
| arithmetic | ADD, SUB, INC, DEC |
| compare | CMP, CMI |
| jumps: unconditional or conditional | JMP; JPE, JPN |
An ASCII code is the number that stands for a character.
INstores the ASCII code of a key in the ACC.OUTshows the character whose code is in the ACC. So 72 in the ACC shows H.CMPdoes not change the ACC.JPEjumps only if the last compare was True.JPNjumps only if it was False.
To trace a program, write one row each time an instruction runs: its address, then the values that change.
Watch out:A jump changes which address runs next. So the trace does not simply go down the listing.
a = an address, n = a number
DATA MOVEMENT
LDM #n ACC ← n immediate
LDD a ACC ← [a] direct
LDI a ACC ← [[a]] indirect
LDX a ACC ← [a + [IX]] indexed
LDR #n IX ← n
MOV IX IX ← [ACC]
STO a [a] ← [ACC]
INPUT AND OUTPUT
IN ACC ← the ASCII code of a key
OUT show the character whose
code is in ACC
ARITHMETIC
ADD a, ADD #n add [a], or n, to ACC
SUB a, SUB #n subtract [a], or n,
from ACC
INC r, DEC r add or subtract 1
(r is ACC or IX)
COMPARE
CMP a, CMP #n compare [ACC] with [a],
or with n
CMI a compare [ACC] with [[a]]
UNCONDITIONAL AND CONDITIONAL JUMPS
JMP a jump to a
JPE a, JPN a jump if the compare was
True, False
END return control to the
operating system
LSL #n, LSR #n: shifts, below
AND, OR, XOR: bit masks, Chapter 7Tracing a program that reads keys
In a key-reading trace, IN gives a key's ASCII value, and CMP with an address compares with what is stored there.
Some exam traces read keys and keep a value in memory. Here is what this program does:
INstores the ASCII value of each key in the ACC.CMP 250compares the ACC with the contents of address 250. Address 250 holds 35, the code of #.- While the key is not #, the count in address 251 goes up by 1.
- At the end,
ADD #48turns the count 3 into 51. 51 is the code of the character 3. OUTshows the character whose ASCII value is in the ACC. So it shows 3.
Watch out:CMP 250 does not compare with the number 250. It compares with what is stored at address 250.
Remember:In the trace, the column for address 251 gets a value only when STO changes it.
Addressing modes
The addressing mode tells the processor how to use the operand to find the value.
| Mode | The operand is | Example |
|---|---|---|
| Immediate | the value itself | LDM #20 |
| Direct | the address of the value | LDD 20 |
| Indirect | the address of a location that holds the address of the value, so memory is read twice | LDI 20 |
| Indexed | added to the contents of the index register (IX); the sum is the address used | LDX 20 |
| Relative | an offset, added to the address of the current instruction | no Cambridge instruction |
Two uses to remember:
- Indexed: add 1 to IX each time round a loop, and you step through an array.
- Relative: the code still works wherever it is loaded in memory.
Exam:# means immediate: the number itself, in denary.
memory 20:23 21:7 22:41
23:90 24:12 25:5
IX = 2
LDM #20 immediate
ACC ← 20
LDD 20 direct
ACC ← [20] = 23
LDI 20 indirect
ACC ← [[20]] = [23] = 90
LDX 20 indexed
ACC ← [20 + [IX]] = [22] = 41
relative (no Cambridge instruction),
from 19, offset 5:
ACC ← [19 + 5] = [24] = 12Shifts: logical, arithmetic and cyclic
A shift slides every bit left or right. The three kinds differ in what falls off and what comes in.
A shift moves every bit left or right by one or more places.
| Shift | A bit that falls off | What comes in |
|---|---|---|
| Logical | is lost | 0s |
| Arithmetic (right) | is lost | a copy of the sign bit, in the empty leftmost place |
| Cyclic (rotate) | comes back at the other end | the bit that fell off |
Logical shift on an unsigned number:
- Each place left doubles it, if no 1 falls off.
- Each place right halves it, dropping any remainder.
An arithmetic shift keeps the sign of a two's complement (signed) number. So −106 becomes −53.
In a cyclic shift, no bit is lost.
Remember:LSL #n and LSR #n are logical shifts.
start 1001 0110
150, or −106 signed
LSL #1 logical left 0010 1100
a 1 fell off: not × 2
LSR #1 logical right 0100 1011
150 → 75
arithmetic right 1 1100 1011
−106 → −53
cyclic left 1 0010 1101
the lost 1 comes backShifts in a program: multiplying by 10
Each place of left shift multiplies by 2. Add two shifted copies to multiply by a number like 10.
LSL #n shifts the bits in the ACC n places to the left. 0s come in on the right.
On an unsigned whole number, each place multiplies it by 2. This works as long as no 1 falls off the left end.
| Instruction | Multiplies by |
|---|---|
LSL #1 | 2 |
LSL #2 | 4 |
LSL #3 | 8 |
A shift can be joined with ADD to multiply by a number that is not a power of 2. 10 is not one. So this program works out NUM × 10 as NUM × 8 + NUM × 2.
Example: NUM is 13.
LSL #1turns 13 into 26, whichSTOkeeps inTWICE.LSL #2then gives 104.ADD TWICEmakes 104 + 26 = 130.
Remember:Check it: 13 × 10 = 130.
LDD NUM ; ACC × 2 LSL #1 STO TWICE ; × 4 more: ACC is NUM × 8 LSL #2 ; NUM × 8 + NUM × 2 = NUM × 10 ADD TWICE STO RESULT END NUM: 13 TWICE: 0 RESULT: 0
KEY TERMS
CHECK YOURSELF
1.In LDM #20, what does the processor load?
2.Why does an assembler need two passes?
3.What does an arithmetic right shift do with the empty leftmost place?
4.Which task is done in BOTH passes of a two-pass assembler?
5.Which instruction group does INC IX belong to?
6.The ACC holds 65. What does it hold after CMP #66?
7.In the instruction LDD 107, which part is the opcode?
8.Which instruction uses a symbolic address?
9.An unsigned number in the ACC has 0 in its two leftmost bits. Which instruction multiplies it by 4?