OCR H446, section 1.2.4 (c)
Low-level languages
A low-level language is close to the hardware, and tied to a particular type of processor.
- Machine code is the binary instructions that the processor executes directly. It is very hard for people to read and write.
- Assembly language replaces the binary with short memorable codes (mnemonics), such as
ADD, and uses labels for addresses.
Machine code (binary) Assembly language
0001 0110 0100 ADD 100
- There is a one-to-one match: each assembly instruction becomes one machine code instruction.
- An assembler translates assembly into machine code.
- It gives full control of the hardware, and fast, compact programs, but it is hard to write, read and debug, and it isn't portable: it only works on one type of processor.
- Used in device drivers, embedded systems and where speed or size is critical.
The Little Man Computer (LMC)
The Little Man Computer is a simple model of a computer that is used to teach how assembly language works. It has:
- 100 memory locations ("mailboxes"), numbered 00 to 99, each holding a 3-digit value;
- an accumulator (ACC) for calculations;
- a program counter (PC);
- an input and an output.
It follows the fetch-decode-execute cycle (see 1.1.1 (b)). Instructions and data are held in the same memory, as in a von Neumann computer.
The LMC instruction set
In OCR questions the mnemonics in the left-hand column are used. The alternatives are also accepted in your answers.
| Mnemonic | Meaning | Alternatives | Action |
|---|---|---|---|
ADD |
Add | ACC = ACC + the value at the address | |
SUB |
Subtract | ACC = ACC − the value at the address | |
STA |
Store | STO |
Copy the ACC into the address |
LDA |
Load | LOAD |
Copy the value at the address into the ACC |
BRA |
Branch always | BR |
Jump to the address |
BRZ |
Branch if zero | BZ |
Jump if the ACC is 0 |
BRP |
Branch if positive | BP |
Jump if the ACC is 0 or positive |
INP |
Input | IN, INPUT |
Read a number into the ACC |
OUT |
Output | Output the ACC | |
HLT |
End program | COB, END |
Stop |
DAT |
Data location | Reserve a memory location (for a variable or constant) |
ADD, SUB, STA, LDA and the branches take an operand (an address or a label). INP, OUT and HLT don't.
Labels
Instead of writing numeric addresses, you use labels for instructions and data. The assembler turns them into addresses.
LDA first ; first is a label for a memory location
...
first DAT 5 ; 'first' holds the value 5
Example 1: add two numbers
INP ; read the first number
STA first ; store it
INP ; read the second number
ADD first ; ACC = second + first
OUT ; output the result
HLT
first DAT
For the inputs 7 and 5:
| Instruction | ACC | Memory first |
Output |
|---|---|---|---|
INP |
7 | ||
STA first |
7 | 7 | |
INP |
5 | 7 | |
ADD first |
12 | 7 | |
OUT |
12 | 7 | 12 |
Example 2: count down using a loop
LDA five ; ACC = 5
loop OUT ; output the ACC
SUB one ; ACC = ACC - 1
BRZ end ; if ACC is 0, stop
BRA loop ; otherwise go round again
end HLT
five DAT 5
one DAT 1
| Pass | ACC after LDA or SUB |
Output |
|---|---|---|
| 1 | 5 | 5 |
| 2 | 4 | 4 |
| 3 | 3 | 3 |
| 4 | 2 | 2 |
| 5 | 1 | 1 |
After SUB |
0 | BRZ jumps to end |
The output is 5, 4, 3, 2, 1. The BRA makes a loop, and the BRZ is the exit condition.
Example 3: output the larger of two numbers
INP
STA a
INP
STA b
SUB a ; ACC = b - a
BRP bbig ; if b - a is 0 or more, b is the larger
LDA a ; otherwise a is larger
OUT
HLT
bbig LDA b
OUT
HLT
a DAT
b DAT
For the inputs 3 and 8: b - a = 5, which is positive, so the branch is taken, and 8 is output. For the inputs 9 and 4: b - a = -5, so there is no branch, and 9 is output.
This uses selection (a conditional branch). Together, BRA, BRZ and BRP give you selection and iteration.
Writing LMC programs
- Work out the algorithm in words or pseudocode.
- Decide on the variables, and reserve each with a
DATat the end. - Use
INP,LDA,ADD,SUB,STAandOUTto do the calculations. - Use labels and branches for decisions and loops.
- Trace the program with a table of ACC, memory values and output.
- End with
HLTbefore the data.
Worked example
Task: write an LMC program that reads a number and outputs double its value.
INP
STA num
ADD num ; ACC = num + num
OUT
HLT
num DAT
For the input 6: INP gives ACC = 6, STA stores 6 in num, ADD num gives ACC = 12, and OUT outputs 12.
Exam tip: in a trace, record the ACC after every instruction and any output. Remember that BRP branches if the ACC is zero or positive, and put DAT locations after HLT, so that data isn't executed as instructions. Alternative mnemonics are accepted.
Check yourself
What does BRZ end do?
It jumps to the instruction labelled end if the accumulator is zero, and carries on with the next instruction otherwise.
What is the relationship between assembly language and machine code?
There is a one-to-one match: each assembly instruction is translated by an assembler into exactly one machine code instruction.
What is the output of this program? LDA x, ADD x, OUT, HLT, with x DAT 4
- The accumulator is loaded with 4, then 4 is added to give 8, which is output.
Give one advantage and one disadvantage of assembly language
Advantage: full control of the hardware and fast, compact programs. Disadvantage: hard to write, read and debug, and it only works on one type of processor.