How a Programming Language Becomes Voltages in a CPU

If you know basic electrical engineering — wires, voltage levels (0V / 3.3V), logic gates, multiplexers, and clocked flip-flops — there is zero magic in how computers run code. Follow one live program all the way from readable source code down to control wires and full adders.

Stage 1 · Software
Compiler
Text → Tokens → Tree (AST) → Assembly
Stage 2 · Encoding
Assembler
Assembly → 16-bit parallel voltage words
Stage 3 · Architecture
Control Unit & Datapath
Opcode bits drive MUXes, ALU & Registers
Stage 4 · Silicon
Gates & MOSFETs
Full adders & CMOS switches do the math
Stage 1 · Language → Assembly

The Compiler: Translating Human Structure into Flat CPU Steps

A CPU only understands tiny one-step operations on a few fast registers (R0–R7). A compiler reads your structured code in three mechanical passes: Lexing (chopping text into tokens), Parsing (building an Abstract Syntax Tree), and Code Generation (walking the tree to emit assembly).

EE Mental Model: Think of variables like a, b, c as human nicknames for specific hardware register banks (groups of D-flip-flops labeled R1, R2, R3). The compiler assigns each variable a register and flattens nested expressions or if/while blocks into sequential instructions.
Load Example:
Source Code (Editable) Input
1. Lexer (Tokens) Chars → Words
2. Parser (AST) Grammar Tree
3. Codegen (Assembly) Click a line!
Stage 2 · Assembly → Machine Code

The Assembler: Packing Text into 16 Parallel Wires

Here is the key bridge: the CPU never reads text like ADD R3, R1, R2. The assembler simply converts each mnemonic into a fixed 16-bit binary number. Inside the chip, those 16 bits are literally 16 parallel copper traces carrying 0V (0) or 3.3V (1).

Why fixed bit positions matter: Because bits 15..12 are always the opcode, those 4 physical wires can be soldered straight into the Control Unit's logic decoder. Bits 11..9 go straight to the Register File's write-address decoder. Click any bit below to flip its voltage!
Inspecting Instruction @00 (0x0000)
[15:12] Opcode (4b) [11:9] Rd (3b) [8:6] Rs1 (3b) [5:3] Rs2 / [5:0] Imm
MOVI R1, 5
Control Unit Combinational Outputs (driven directly by Opcode bits [15:12]):
16-Bit Instruction Set (ISA) Lookup Click row to load
OpcodeMnemonicFormatHardware Action
Stage 3 · Inside the CPU

How the CPU "Knows" What to Do: Clock, Decoder & Datapath

A CPU doesn't "understand" instructions — it is a clocked finite state machine. On each instruction cycle, the opcode wires set up multiplexers (MUXes) and ALU select lines, combinational logic computes the answer, and the next rising clock edge latches the result into flip-flops.

1. FETCH
2. DECODE
3. EXECUTE
4. WRITEBACK & PC
CLK CYCLE: 0 PC Flip-Flop 00 INSTR MEMORY 16-bit ROM Array 0x0000 — IR (16 WIRES) 0000 000 000 000000 OP | Rd | Rs1 | Rs2/Imm CONTROL UNIT Combinational Logic Decoder RegWrite: 0 ALUSrc: 0 ALUOp: NOP REGISTER FILE R0(=0) .. R7 Rs1: — Rs2: — Rd: — MUX 0 1 ALU — — FLAGS & BRANCH Z=0 N=0 OP[15:12] Rd/Rs Imm[5:0] A B Writeback Bus (latched on CLK ↑ if RegWrite=1) Next PC = PC + 1 (or Branch Target)
Ready to Execute
Click Step Phase to watch a single instruction move wire-by-wire through Fetch → Decode → Execute → Writeback, or click Auto Run.
REGISTER FILE (8 × 16-bit Flip-Flop Banks) READY
INSTRUCTION MEMORY (ROM)
Stage 4 · Inside the ALU

Down to the Silicon: How Logic Gates Actually Add Numbers

When the Control Unit sets ALUOp = ADD, the ALU doesn't "know" arithmetic — it routes your two numbers into a chain of Full Adders built from XOR, AND, and OR gates. Toggle the bits below (or pull live values from the CPU above) to watch voltages and carry bits ripple across the hardware.

= 5
+
= 3
= 01000 (8)
The Bottom Layer: CMOS Inverter (NOT Gate)

How a Gate Is Just Two MOSFET Switches

Every XOR, AND, OR, and Flip-Flop above is built from tiny complementary MOSFETs (PMOS + NMOS). Click the input button to apply 0V or 3.3V to the gates and watch which transistor conducts.

VDD (3.3V) GND (0V) PMOS ON NMOS OFF Vin 0V (0) Vout 3.3V (1)
Stage 5 · Summary

The Complete EE ↔ Software Translation Table

Keep this mental map whenever you look at code or a microcontroller datasheet — every software abstraction maps directly to a physical circuit.

Software Concept Assembly / Machine Level Electrical / Hardware Reality
Variable (let a = 5) Register R1 (or RAM address) A bank of 16 D-Flip-Flops holding high/low voltages until overwritten on a clock edge.
Statement (c = a + b) ADD R3, R1, R2 → 0x1648 16 parallel voltage wires read from ROM into the Instruction Register.
"CPU knows what to do" Opcode bits [15:12] = 0001 Combinational decoder (AND/OR gates or PLA) asserting RegWrite=1, ALUSrc=0, ALUOp=ADD.
if / while CMP + BGT / JMP ALU subtracts two values; its Zero/Negative flag wires control a MUX that overwrites the Program Counter (PC).
Arithmetic (+, -) ADD, SUB, ADDI Ripple-carry / carry-lookahead Full Adders made of XOR, AND, OR gates (subtraction uses two's complement: invert B + Cin=1).
CPU Speed (3 GHz) Instructions per second 3 billion rising clock edges per second synchronizing all flip-flops so combinational logic has time to settle.