Registers, ALU and Control Unit: The Parts of a CPU
Learn what registers, the ALU and the control unit do inside a CPU, with a small C model of an 8-bit ALU and its status flags that you can read line by line.

A processor looks like a black box that runs your code, but its core is only three kinds of part. Registers hold the few values the CPU is working on right now. The arithmetic logic unit, the ALU, computes new values from them. The control unit reads an instruction and tells the other two what to do. Everything else on a modern chip, from caches to branch predictors, exists to keep those three parts fed.
This post explains each part in plain terms, then models an 8-bit ALU in C17 so you can see how the status flags that conditional jumps depend on come out of a single addition. The model is a teaching sketch, not a description of one real chip, and the real x86-64 register names appear where they help.
Registers: the CPU's own variables
A register is a small, fast storage cell inside the processor, as wide as the machine word. A 64-bit CPU such as x86-64 has general-purpose registers that are 64 bits wide, named rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp and r8 through r15. Arithmetic works on registers directly. To add two numbers that live in memory, the CPU first loads them into registers, because the ALU is wired to registers and not to RAM.
Some registers have a job beyond holding data:
| Register | What it holds |
|---|---|
Program counter (rip on x86-64) | The address of the next instruction |
Stack pointer (rsp) | The top of the current stack |
Flags (rflags) | One bit per condition the last operation produced |
| Instruction register (internal) | The instruction being decoded now |
A C compiler decides which variables live in registers and which spill to the stack. When you read optimized assembly, most of what you see is values being moved between a handful of registers, which is why the register count of an architecture shapes its compilers.
The ALU: arithmetic and logic
The ALU takes two inputs, an operation selector and produces a result plus a set of flags. It performs addition, subtraction, bitwise AND, OR, XOR, shifts and comparison, and it knows nothing about programs. It does not know what the numbers mean. That is why the same adder serves signed and unsigned arithmetic: the bits are identical, and only the interpretation of the flags differs.
The flags are the ALU's side channel. The four that matter in most architectures are:
Zero (Z): the result was zero.
Negative or sign (N): the top bit of the result is set.
Carry (C): an unsigned addition produced a carry out of the top bit, or an unsigned subtraction needed a borrow.
Overflow (V): a signed operation produced a result that does not fit.
A comparison such as if (a < b) becomes a subtraction whose result is thrown away and whose flags are kept. A conditional jump then reads one or two flag bits. Here is that idea as a small C model of an 8-bit ALU.
#include <stdint.h>
#include <stdio.h>
enum op { OP_ADD, OP_SUB, OP_AND, OP_OR, OP_XOR };
struct flags { unsigned z : 1, n : 1, c : 1, v : 1; };
static uint8_t alu(enum op op, uint8_t a, uint8_t b, struct flags *f) {
uint16_t wide = 0;
uint8_t r = 0;
f->c = 0;
f->v = 0;
switch (op) {
case OP_ADD:
wide = (uint16_t)a + b;
r = (uint8_t)wide;
f->c = wide > 0xFF;
f->v = ((a ^ r) & (b ^ r) & 0x80) != 0;
break;
case OP_SUB:
r = (uint8_t)(a - b);
f->c = a < b;
f->v = ((a ^ b) & (a ^ r) & 0x80) != 0;
break;
case OP_AND: r = a & b; break;
case OP_OR: r = a | b; break;
case OP_XOR: r = a ^ b; break;
}
f->z = r == 0;
f->n = (r & 0x80) != 0;
return r;
}
static void show(const char *label, uint8_t r, struct flags f) {
printf("%-14s r=%3u Z=%u N=%u C=%u V=%u\n", label, r, f.z, f.n, f.c, f.v);
}
int main(void) {
struct flags f;
uint8_t r;
r = alu(OP_ADD, 200, 100, &f); show("200 + 100", r, f);
r = alu(OP_ADD, 127, 1, &f); show("127 + 1", r, f);
r = alu(OP_SUB, 5, 5, &f); show("5 - 5", r, f);
r = alu(OP_SUB, 3, 5, &f); show("3 - 5", r, f);
r = alu(OP_AND, 0xF0, 0x0F, &f); show("0xF0 & 0x0F", r, f);
return 0;
}gcc -std=c17 -Wall -Wextra -O2 alu.c -o alu
./alu200 + 100 r= 44 Z=0 N=0 C=1 V=0
127 + 1 r=128 Z=0 N=1 C=0 V=1
5 - 5 r= 0 Z=1 N=0 C=0 V=0
3 - 5 r=254 Z=0 N=1 C=1 V=0
0xF0 & 0x0F r= 0 Z=1 N=0 C=0 V=0Read the table row by row, because it shows why the flags are separate:
200 + 100is 300, which does not fit in 8 bits. The stored result is 44, andC=1says an unsigned carry happened. As signed values, 200 is -56 and 100 is 100, whose sum 44 is correct, soV=0.127 + 1gives 128. Unsigned, that is fine andC=0. Signed, 128 does not exist in 8 bits, so the result flips to -128, andV=1says the signed result is wrong.5 - 5setsZ, which is exactly how==is implemented.3 - 5needs a borrow, soC=1, and the stored 254 is 3 - 5 modulo 256.
The same bits mean different things to signed and unsigned code, and the compiler picks different jump instructions for each: jb and ja test carry for unsigned comparisons, while jl and jg combine sign and overflow for signed ones.
The control unit: turning instructions into signals
The control unit is the part that makes the other two cooperate. It receives an instruction, which is just a number, and splits it into fields: an opcode that says what to do, and operands that say which registers or addresses to use. It then raises the right control signals: connect register 1 and register 2 to the ALU inputs, select the add operation, write the result into register 1, update the flags.
There are two broad designs. A hardwired control unit uses fixed logic gates to turn each opcode into its signals, which is fast and was the choice for simple RISC cores. A microcoded control unit stores a small program per instruction, which lets complex instructions be sequences of simpler steps, and it was the traditional approach for CISC designs such as x86. Modern x86-64 processors decode most instructions into simpler internal micro-operations, which are what the execution units run.
You can model the control unit's decode step in the same program. An instruction byte with the top three bits as the opcode and the low bits as two register numbers is enough:
#include <stdint.h>
#include <stdio.h>
int main(void) {
uint8_t ins = 0x29; /* 001 01 001 */
unsigned opcode = (ins >> 5) & 0x7; /* top three bits */
unsigned dst = (ins >> 3) & 0x3; /* next two bits */
unsigned src = ins & 0x7; /* low three bits */
printf("opcode=%u dst=r%u src=r%u\n", opcode, dst, src);
return 0;
}opcode=1 dst=r1 src=r1The byte 0x29 is 0010 1001. The top three bits 001 give opcode 1, the next two bits 01 give destination register 1, and the low three bits 001 give source register 1. A real control unit does this with wires instead of shifts, and the opcode value then selects which ALU operation the next clock tick performs.
How the three parts work together
One instruction passes through all of them. The control unit reads the instruction at the address in the program counter and decodes it. It routes the named source registers into the ALU and selects the operation. The ALU produces a result and flags, the control unit writes the result into the destination register and the flags into the flag register, and the program counter moves on, or jumps if the instruction was a branch. That loop repeats billions of times a second.
When this model is too simple
Real processors add pipelines, so several instructions are in different stages at once, and out-of-order execution, which runs instructions as soon as their inputs are ready. The three parts still exist, but there are many ALUs, hundreds of physical registers behind the few names a programmer sees, and a control unit that tracks dependencies. This post's model also leaves out memory access and the cache, which sit between the registers and RAM. Treat it as the shape of the idea, and read the architecture manual of your target chip for the actual register file and flag definitions.


