The instruction set
An instruction set is the list of commands a CPU understands, and the exact pattern of bits that stands for each one. A program for this CPU can only use these commands. Meet TINY-8, the course's teaching CPU.
TINY-8 keeps its working numbers in four registers, r0 to r3: tiny 8-bit stores inside the CPU. The program counter, PC, holds the address of the next instruction. Memory holds 256 bytes, addresses 0 to 255.
Every TINY-8 instruction is exactly 2 bytes. Byte 0 packs three fields: a 4-bit opcode (which command), d (the destination register) and s (the source register), 2 bits each. Byte 1, imm8, holds an 8-bit number or memory address.
Four opcode bits make 2 × 2 × 2 × 2 = 16 patterns, 0000 to 1111, so TINY-8 has exactly 16 instructions. They come in four groups: moving data, arithmetic and logic, jumps (plus HALT), and input and output.
Assembly to machine code
Bits are hard for people to read, so programs are written in assembly: one line per instruction, named by a short word, a mnemonic. LDI r0, 2 means load the number 2 into r0. This program adds 2 + 3, outputs 5, then counts down.
An assembler is a program that turns each line into bytes. LDI is opcode 0001, d = 00 for r0, s is unused so 00, and imm8 = 2. LDI r0, 2 becomes 0x10 0x02 (hexadecimal) at addresses 0 and 1.
ADD r0, r1 means r0 = r0 + r1. Opcode 0101, d = 00 (r0), s = 01 (r1). It needs no number, so imm8 is 0. The two bytes are 0x51 0x00, at addresses 4 and 5.
The label loop: is a name for address 12; it takes no bytes. JNZ loop (jump back if the last result was not zero) becomes opcode 1101 with imm8 = 12: the bytes 0xD0 0x0C.
The 9 instructions fill 18 bytes, addresses 0 to 17. This row of numbers is the machine code, the only thing the CPU really reads. The words on the card were for people; the assembler threw them away.
Running, and real CPUs
Running is a loop: fetch the 2 bytes at the PC, decode them into opcode, d, s and imm8, do the command, and move the PC on by 2 unless it jumped. After 13 instructions TINY-8 halts, having output 5.
Real CPUs work the same way, with bigger sets. x86-64, in most PCs, has instructions 1 to 15 bytes long: a CISC design. ARM, in most phones, and RISC-V, an open standard, use mainly 32-bit instructions: RISC, simpler commands.
What happens
;; step 1/11 · isa
instruction set the commands a CPU understands
+ the bit pattern (encoding) of each one
TINY-8 16 commands, 2 bytes each
move LDI LD ST MOV
math ADD SUB AND OR XOR ADDI
jump JMP JZ JNZ HALT
I/O IN OUT
TINY-8 is a teaching CPU made for this course, with every instruction exactly 2 bytes. Real 8-bit CPUs were less tidy: the MOS 6502, for example, used instructions 1 to 3 bytes long. 0x marks a hexadecimal (base-16) number. Each hex digit stands for 4 bits, so one byte is two digits, from 0x00 to 0xFF (0 to 255).