Three levels
- Machine code: binary instructions the CPU carries out directly. Each processor family has its own.
- Assembly language: the same instructions written as short mnemonics (LDA, ADD, STA) with addresses. One assembly line = one machine code instruction.
- High-level languages (Python, C#, Java): close to English, so they are quicker to write, read and debug, and they run on different processors once translated. One line can become many machine code instructions.
Low-level code is used where speed, size or direct control of the hardware matters most: device drivers, embedded systems, parts of operating systems.
A tiny instruction set
In this course's made-up CPU, each instruction is 8 bits: a 4-bit opcode then a 4-bit address.
| Opcode | Mnemonic | Meaning |
|---|---|---|
| 0001 | LDA | load the value at the address into the accumulator |
| 0010 | ADD | add the value at the address |
| 0011 | SUB | subtract the value at the address |
| 0100 | STA | store the accumulator at the address |
| 0000 | HLT | stop |
- 0010 1001: opcode 0010 is ADD, address 1001 is 9, so ADD 9.
- x = a + b - c with a, b, c, x at addresses 8, 9, 10, 12 becomes LDA 8, ADD 9, SUB 10, STA 12.
Watch it run, one register at a time:
Decode an instruction
Split the 8 bits into the opcode and the address.