CIE AS & A Level Computer Science, section 20.1 (Programming paradigms)
Low-level languages
A low-level language is close to the hardware: machine code (binary) and assembly language (mnemonics such as ADD). Each instruction matches a single processor operation. Low-level programs give direct control of registers and memory, are fast and small, but are hard to write and read and run only on one type of processor.
Uses: device drivers, embedded systems and parts of an operating system.
Addressing modes
The address mode says how the operand of an instruction is used to find the data. In these examples ACC is the accumulator and memory holds the values shown.
| Mode | Meaning | Example |
|---|---|---|
| Immediate | the operand is the value | LDM #5 loads 5 |
| Direct | the operand is the address of the value | LDD 200 loads the contents of address 200 |
| Indirect | the operand is the address of a place holding the address of the value | LDI 200 |
| Indexed | the operand + the index register gives the address | LDX 200 loads from |
| Relative | the address is relative to the current instruction (used for jumps) | a jump forward 4 places |
Simulating the modes in Python
memory = {200: 300, 300: 42, 201: 7}
IX = 1
immediate = 200 # LDM #200
direct = memory[200] # LDD 200 -> 300
indirect = memory[memory[200]] # LDI 200 -> 42
indexed = memory[200 + IX] # LDX 200 -> 7
print(immediate, direct, indirect, indexed) # 200 300 42 7
Bit manipulation
Low-level programs often work with individual bits, using logical shifts and the logical instructions AND, OR and XOR.
| Operation | Effect | Python |
|---|---|---|
| Shift left by | multiplies by | x << n |
| Shift right by | divides by (whole number) | x >> n |
| AND with a mask | keeps chosen bits, clears the rest | x & 0b00001111 |
| OR with a mask | sets chosen bits | `x |
| XOR with a mask | toggles chosen bits | x ^ 0b00000001 |
x = 0b10110110
print(bin(x & 0b00001111)) # 0b110 (the low four bits)
print(bin(x | 0b00000001)) # 0b10110111
print(bin(x ^ 0b11111111)) # 0b1001001 (every bit flipped)
print(x >> 2, x << 1) # 45 364
Masks are used to check or set flags (such as a status register) and to pack data compactly.
Common mistake: mixing up direct and immediate. With immediate, the operand is the value; with direct, it is the address of the value. Also, shifting left multiplies by 2, but bits shifted off the end are lost.
Exam tip: for an addressing question, trace what the operand is used for: value, address, or address plus index. Show the result in the accumulator.
Check yourself
What does the immediate addressing mode use as the value?
The operand itself.
What is the effect of shifting a binary number left by 3 places?
It multiplies the number by .
Which operation with a mask can be used to set a particular bit to 1?
OR with a mask that has a 1 in that bit.