Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 67 ยท Big Picture

The Von Neumann Model and the Fetch-Decode-Execute Cycle

Understand the stored-program idea, the CPU, memory and I/O, the Harvard variant, and the instruction cycle, with a tiny Python machine you can step through.

Early computers were rewired by hand for each new task. In the 1940s a simple but radical idea changed that: keep the program in memory, as numbers, right next to the data. The machine then reads its own instructions one by one. This is the stored-program concept, usually credited to the Von Neumann model, and nearly every computer you use follows it.

In this lesson you will meet the three main parts of the model, see how they connect, learn the loop every processor repeats billions of times per second, and run a tiny machine of your own that steps through a program.

The stored-program idea

In the Von Neumann model, instructions and data live in the same memory, both encoded as binary numbers. Memory is a long row of numbered cells. The number of a cell is its address.

Why is that powerful?

  • Changing the program means changing numbers in memory, not rebuilding hardware.
  • A program can be loaded from disk, created by a compiler, or even modified by another program.
  • One machine can run a word processor now and a game a second later.

The cost is a design tension: the processor and memory share one path, so fetching an instruction and fetching data compete. This is the famous Von Neumann bottleneck, and many tricks in later lessons (caches, pipelines) exist to ease it.

The parts of the model

flowchart LR
  subgraph CPU
    CU["Control unit"]
    ALU["ALU"]
    R["Registers<br/>PC, IR, ACC"]
  end
  M["Main memory<br/>instructions + data"]
  IO["Input / Output<br/>devices"]
  CPU <-->|"address, data, control buses"| M
  CPU <--> IO

The CPU, memory and I/O are connected by buses.

  • Control unit (CU) reads instructions and tells everything else what to do.
  • Arithmetic logic unit (ALU) does arithmetic (add, subtract) and logic (AND, compare). You will build one in Building an ALU.
  • Registers are a few tiny, very fast storage cells inside the CPU.
  • Main memory holds the program and its data.
  • Input/Output connects the keyboard, screen, disk and network.
  • Buses are bundles of wires. The address bus carries "which cell?", the data bus carries the value, and the control bus carries signals such as "read" or "write". Details come in Components of a Computer.

Two registers matter in every design:

Register Name Job
PC Program counter Holds the address of the next instruction
IR Instruction register Holds the instruction currently being worked on

Many simple designs also have an accumulator (ACC), a register that holds the running result of the ALU.

The Harvard variant

In a Harvard architecture, instructions and data have separate memories and separate buses. The CPU can fetch an instruction and read data in the same moment. Microcontrollers in appliances and cars often use it (the program lives in flash, the variables in RAM, and both can be accessed at once), and so do digital signal processors. The Von Neumann design, by contrast, is the model of personal computers, laptops, phones, tablets and game consoles, where one flexible memory holds whatever program is loaded. Modern processors use both ideas, in disguise: the main memory is Von Neumann-style, but the first-level cache is split into an instruction cache and a data cache. The programmer sees one memory; the hardware uses two paths where it helps.

Von Neumann Harvard
Memories One, shared Separate for code and data
Buses One set Two sets
Fetch code and data together? No Yes
Self-modifying or loaded code Easy Harder
Typical use General computers Microcontrollers, split caches

The instruction cycle

Every instruction goes through the same loop, again and again:

  1. Fetch: use the PC to read the instruction from memory into the IR, then advance the PC.
  2. Decode: the control unit works out what the instruction means (which operation, which operands).
  3. Execute: the ALU or memory does the work, such as adding two numbers or reading a cell.
  4. Write back (when needed): the result is stored in a register or memory.
flowchart LR
  F["Fetch<br/>IR = memory[PC]<br/>PC = PC + 1"] --> D["Decode<br/>what is this?"]
  D --> E["Execute<br/>ALU or memory"]
  E --> W["Write back<br/>store result"]
  W --> F

The cycle repeats until the machine halts.

Jumps and branches change the PC to something other than "next", which is how loops and if-statements work. A processor that runs at 3 GHz can complete this loop (in an overlapped form, see Instruction Pipelining) billions of times per second.

A tiny machine in Python

Let us build the smallest useful Von Neumann machine. It has 16 memory cells, an accumulator, a program counter and five instructions. Each instruction is one number: the hundreds digit is the operation code and the last two digits are the address it works on.

Code Mnemonic Meaning
1xx LOAD xx ACC = memory[xx]
2xx ADD xx ACC = ACC + memory[xx]
3xx STORE xx memory[xx] = ACC
4xx JNZ xx jump to xx if ACC is not zero
000 HALT stop

The program below adds the values in cells 10 and 11, stores the sum in cell 12, and halts. The program and the data share the same memory, just like the model says.

Python Step through a stored program

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output has one line per cycle. Read the ACC column: it is 25 after the LOAD, 42 after the ADD, and the STORE copies it to memory. Notice that the instruction 110 is an ordinary number in the same list as the data 25. The machine only treats it as an instruction because the PC points at it.

A loop with a branch

Branching is what turns a calculator into a computer. The next program counts down from 3 to 0 by adding the value -1 repeatedly. It uses JNZ to jump back while the accumulator is not zero. Because we store the program as numbers in memory, we can write a small helper to assemble readable mnemonics into those numbers.

Python Countdown loop with JNZ

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The loop body runs three times (counter 2, 1, 0) before JNZ falls through to HALT. Every pass through fetch, decode and execute costs time, which is why the number of instructions and the cycles per instruction both matter for speed, the topic of Measuring Performance.

Real instructions are not decimal numbers. They are binary patterns with fields for the operation and operands. Instruction Formats and Encoding shows how those fields are laid out.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the key idea of the stored-program concept?

    Placing instructions in the same memory as data lets one machine run any program by loading different numbers.
  2. 2. Which register holds the address of the next instruction to fetch?

    The program counter (PC) points to the next instruction. The IR holds the instruction currently being decoded.
  3. 3. What is the Von Neumann bottleneck?

    With one memory path, fetching an instruction and moving data cannot happen at the same time, which limits throughput.
  4. 4. Which statement about Harvard architecture is true?

    Separate memories and buses for instructions and data allow both to be accessed at the same time.

Summary

  • The stored-program model keeps instructions and data in the same memory as binary numbers.
  • The CPU (control unit, ALU, registers), memory and I/O are connected by address, data and control buses.
  • The PC points to the next instruction; the IR holds the current one.
  • Every instruction goes through fetch, decode, execute and write back, repeated until the machine halts.
  • Harvard designs split code and data paths; modern CPUs mix both ideas with split L1 caches.
  • A dozen lines of Python are enough to model a working machine.

Continue with Components of a Computer.