Computer System Fundamentals (COA Core #1)
A processor is a state machine: registers, a program counter, and a few hundred instructions that move bytes and do arithmetic. Everything above that — compilers, operating systems, caches — is built on the contract that the next instruction runs after the current one, except when it does not.
CPU, memory, and I/O organization
A computer has three kinds of components connected by buses:
- CPU: control unit (decodes and sequences), ALU (arithmetic and logic), registers (fastest storage), and caches.
- Memory: RAM holding instructions and data, addressed by number.
- I/O: disks, network, display, and the controllers that connect them.
Three buses connect them: the address bus (which location), the data bus (the bytes), and the control bus (read/write, interrupt, clock).
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=4pt, minimum width=2.4cm, minimum height=0.9cm, font=\scriptsize, align=center},
a/.style={->, thick}]
\node[b, fill=blue!12] (cpu) at (0,0) {CPU\\control + ALU\\registers, caches};
\node[b, fill=green!15] (mem) at (4.4,0) {main memory\\RAM};
\node[b, fill=orange!15] (io) at (8.8,0) {I/O controllers\\disk, net, display};
\draw[a] (cpu) -- node[above, font=\tiny] {address} (mem);
\draw[a] (mem) -- node[above, font=\tiny] {address} (io);
\draw[a, dashed] (cpu) to[bend left=22] node[below, font=\tiny] {data + control} (io);
\node[font=\scriptsize, anchor=north] at (4.4,-0.8) {the three buses are shared};
\end{tikzpicture}
In the von Neumann model, instructions and data live in the same memory and share the same bus, which is why a program can treat code as data. The Harvard model has separate instruction and data memories; microcontrollers use it, and so do CPU caches in practice (separate L1I and L1D).
Memory-mapped I/O places device registers in the same address space as RAM, so a
load or store to a particular address talks to the device. Port-mapped I/O (x86
in/out) uses a separate address space.
ASolution
Only one transfer happens at a time, and instruction fetches compete with data accesses. That is the von Neumann bottleneck; caches reduce it by keeping recent code and data close to the CPU.
Instruction Set Architecture
The ISA is the contract between hardware and software. It defines:
- the instruction set and their encodings,
- the registers and their widths,
- data types and their representation,
- addressing modes,
- the memory model (alignment, ordering),
- privileged instructions and exceptions.
The ISA is not the microarchitecture. x86-64 is an ISA; Skylake and Zen are two different implementations of it. The same binary runs on both, with different performance.
Common ISAs:
| ISA | Style | Instruction length | Registers |
|---|---|---|---|
| x86-64 | CISC | 1–15 bytes, variable | 16 general-purpose |
| ARMv8-A | RISC | 4 bytes, fixed | 31 general-purpose |
| RISC-V | RISC | 4 bytes (compressed: 2) | 32 general-purpose |
binary?
ASolution
Yes. The ISA fixes the instruction meaning, not how many cycles each takes, how deep the pipeline is, or how big the caches are. Microarchitecture decides performance.
Machine instructions
An instruction has an opcode (what to do) and operands (what to do it to). Operands can be registers, immediate constants, or memory addresses.
x86-64 instructions are variable-length and complex:
mov rax, 42 ; opcode + immediate
add rax, rbx ; opcode + two register operands
mov [rsp+8], rax ; store to memory
RISC-V uses fixed 32-bit formats. An R-type instruction splits the word into fields:
\[ \underbrace{7}_{\text{opcode}} \;\; \underbrace{5}_{rd} \;\; \underbrace{3}_{funct3} \;\; \underbrace{5}_{rs1} \;\; \underbrace{5}_{rs2} \;\; \underbrace{7}_{funct7} \]
That regularity is what makes decoding cheap and pipelining easy.
A program is a sequence of such instructions plus data. The compiler’s job is to choose instructions; the CPU’s job is to execute them.
ASolution
The next instruction’s address is known immediately (PC + 4), so fetch and decode can proceed without first decoding the current instruction’s length.
Registers
Registers are the fastest storage, inside the CPU, accessed in a single cycle. They are few (16–32 general-purpose) because they are expensive in silicon and because instruction encoding has to name them in a few bits.
x86-64 registers:
| Register | Conventional use |
|---|---|
rax |
accumulator, return value |
rbx, r12–r15 |
callee-saved general purpose |
rcx, rdx, rsi, rdi |
arguments, counters (platform-dependent) |
rsp |
stack pointer |
rbp |
frame pointer |
rip |
program counter (instruction pointer) |
rflags |
condition codes (zero, sign, carry, overflow) |
The ABI decides which registers hold arguments and which survive a call. The hardware does not care; it just executes.
ASolution
Each register needs a name in the instruction encoding and dedicated silicon with fast access. More registers mean longer instructions and more area; caches cover the capacity gap.
Program counter and the instruction cycle
The program counter (rip on x86-64, pc elsewhere) holds the address of the
next instruction. The instruction cycle:
- Fetch: read the instruction at
PCinto the instruction register; updatePCto the next instruction. - Decode: determine the opcode, operands, and control signals.
- Execute: run the ALU operation or compute an address.
- Memory: load or store data if the instruction needs it.
- Writeback: write the result to a register.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=1.7cm, minimum height=0.62cm, font=\scriptsize},
a/.style={->, thick}]
\node[b, fill=blue!12] (f) at (0,0) {fetch};
\node[b, fill=blue!12] (d) at (2.1,0) {decode};
\node[b, fill=orange!18] (e) at (4.2,0) {execute};
\node[b, fill=green!15] (m) at (6.3,0) {memory};
\node[b, fill=green!15] (w) at (8.4,0) {writeback};
\foreach \x/\y in {f/d,d/e,e/m,m/w} \draw[a] (\x) -- (\y);
\draw[a, dashed] (w) to[bend left=30] node[below, font=\tiny] {next instruction} (f);
\end{tikzpicture}
Each stage may take one or more clock cycles. A 3.5 GHz core has a cycle time of about 0.29 ns; a load from DRAM takes 60–100 ns, which is 200–350 cycles. That gap is why caches and pipelining exist (Ch. 3–4).
ASolution
The address of the next instruction to execute. call pushes the return address
(the instruction after the call) onto the stack and jumps; ret pops it back
into the PC.
RISC versus CISC
| RISC | CISC | |
|---|---|---|
| Instructions | few, simple | many, complex |
| Length | fixed | variable |
| Addressing | load/store only | memory operands allowed |
| Registers | many | fewer |
| Pipelining | designed for it | harder, needs decoding |
| Code size | larger | smaller |
| Power | lower | higher |
| Examples | ARM, RISC-V, MIPS | x86, VAX |
The old argument was “RISC is faster”. The modern answer is that the boundary blurred: x86 CPUs decode complex instructions into RISC-like micro-ops, then execute those in a RISC-style out-of-order core. ARM has added complex instructions, and x86 has added fast simple paths.
What remains true: RISC makes the decoder simple, which helps power and area; CISC makes the instruction stream compact, which helps instruction-cache density.
ASolution
Because the ISA defines the visible behaviour: instruction encodings, registers, memory model, and compatibility. Micro-ops are an implementation detail that can change between generations without breaking binaries.
Assembly language basics
Assembly is a human-readable form of machine code. One line is one instruction (usually), with labels for branch targets.
mov rax, 0 ; sum
mov rcx, 10 ; counter
loop:
add rax, rcx
dec rcx
jnz loop ; jump if zero flag is clear
ret ; sum 1..10 = 55 in rax
Common instructions:
| Instruction | Meaning |
|---|---|
mov dst, src |
copy |
add, sub, imul |
arithmetic |
and, or, xor |
bitwise |
cmp a, b |
set flags from a - b |
jmp, jz, jnz |
control flow |
call, ret |
function call and return |
push, pop |
stack operations |
Two syntaxes for x86: Intel (mov rax, 42) and AT&T (movq $42, %rax). GCC
defaults to AT&T; -masm=intel switches.
A C++ loop compiles to the same pattern:
int sum(const int* a, int n) {
int s = 0;
for (int i = 0; i < n; ++i) s += a[i];
return s;
}
On x86-64 with optimisation, the body becomes a load, an add, and a pointer increment — about four instructions per element.
ASolution
No. Assembly can execute any instruction the ISA allows, but privileged operations (I/O, page-table changes) still trap to the kernel, and system calls are still syscalls.
Addressing modes
An addressing mode says how to compute an operand’s address. The common ones:
| Mode | Example (x86-64) | Effective address |
|---|---|---|
| Immediate | mov rax, 42 |
the constant 42 |
| Register | mov rax, rbx |
the value in rbx |
| Direct | mov rax, [0x1000] |
address 0x1000 |
| Register indirect | mov rax, [rbx] |
value of rbx |
| Base + displacement | mov rax, [rbx+8] |
rbx + 8 |
| Indexed | mov rax, [rbx+rcx*4] |
rbx + 4*rcx |
| Full | mov rax, [rbx+rcx*4+16] |
rbx + 4*rcx + 16 |
| PC-relative | lea rax, [rip+label] |
rip + offset |
lea (load effective address) computes an address without touching memory, which
makes it a cheap arithmetic instruction: lea rax, [rbx+rcx*4+16] is
rax = rbx + 4*rcx + 16 in one instruction.
Array indexing maps directly onto the full mode: for int a[], a[i] is
[base + i*4]. That is why array access is one instruction, while a linked list
needs a load to get the next pointer before it can load the value.
[rbx + rcx*4] limited to scales 1, 2, 4, and 8?ASolution
Those are the sizes of the common element types (byte, halfword, word, doubleword) and fit in two bits of the encoding. Arbitrary scales would need more bits and more hardware.
Bugs and edge cases
- Confusing the ISA with the implementation. Cycle counts and cache sizes are not in the ISA.
- Assuming assembly is faster. Hand-written assembly often loses to a good compiler; it is useful for understanding, not for speed by default.
- Forgetting the ABI. Registers have calling-convention roles; clobbering callee-saved registers breaks the caller.
- Ignoring flags.
cmpsets flags; the branch reads them. An instruction between them can overwrite the flags. - Stack alignment. x86-64 requires 16-byte alignment at a call boundary; a misaligned stack crashes in SSE code.
- Variable-length decoding. x86 instruction boundaries are not obvious from a hex dump; disassemble instead of guessing.
cause?
ASolution
The stack was not 16-byte aligned at the call. SSE instructions that spill 16-byte values to the stack fault on misalignment.
Next: Data Representation (COA Core #2) — two’s complement, IEEE 754, and why
0.1 + 0.2 != 0.3.