Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 40 ยท First Steps

How a CPU Runs Instructions

Understand the fetch-decode-execute cycle, the program counter, registers versus RAM and instruction encoding, then run a tiny CPU loop in JavaScript.

A processor looks complicated from the outside: billions of transistors, many cores, deep pipelines, several levels of cache. But the idea at its center is small enough to fit on one page. The CPU repeats a single loop forever: get the next instruction, work out what it means, do it. Every program you have ever run is just that loop applied to a very long list of instructions.

In this lesson you will meet the parts that the loop touches, see how an instruction can be stored as a number, and then write a working miniature CPU in JavaScript that you can run right here. Everything later in the tutorial, from loops to function calls, builds on this loop.

The parts of the machine

A simple computer, reduced to what matters for running code, has three kinds of storage and one controller:

  • Memory (RAM) is a huge array of numbered byte slots. It holds both the program's instructions and its data. Each slot has an address.
  • Registers are a few tiny storage cells inside the CPU itself. The CPU can compute only with values that are in registers (or, on some architectures like x86, directly from memory in one instruction).
  • The program counter (PC) is a special register that holds the address of the next instruction to run. On x86-64 it is called rip, on ARM64 it is pc.
  • The control unit and ALU read instructions, decide what to do, and perform arithmetic and logic.
flowchart LR
    subgraph CPU
        PC["Program counter<br/>address of next instruction"]
        REG["Registers<br/>fast working values"]
        ALU["ALU and control<br/>decode and compute"]
    end
    RAM["Memory (RAM)<br/>instructions and data"]
    PC -->|"address"| RAM
    RAM -->|"instruction word"| ALU
    ALU <--> REG
    REG <-->|"load and store"| RAM

The CPU asks memory for the instruction the program counter points at, executes it using registers, and moves on.

Registers and RAM differ mainly in speed and size. A register is read in a fraction of a clock cycle, and a CPU has only a few dozen of them. Main memory holds gigabytes but takes on the order of a hundred cycles or more to reach when the data is not in a cache. That is why compilers work hard to keep values in registers, and why the next lessons spend so much time on them.

The fetch-decode-execute cycle

Every instruction goes through the same three steps:

  1. Fetch. Read the instruction stored at the address in the program counter, then advance the program counter to point at the following instruction.
  2. Decode. Split the instruction into its parts: which operation it is (the opcode) and which registers, constants or addresses it uses (the operands).
  3. Execute. Carry out the operation: add two registers, read memory, compare values, or change the program counter.
stateDiagram-v2
    [*] --> Fetch
    Fetch --> Decode: instruction word read, PC advanced
    Decode --> Execute: opcode and operands known
    Execute --> Fetch: next cycle
    Execute --> [*]: halt

The cycle repeats until a halt instruction (or a fault) stops it.

There is one subtle point worth memorizing now. The program counter is advanced during fetch, before execute. So while an instruction runs, the PC already points at the next instruction. A jump instruction does not "jump" in any magical way: it simply writes a new address into the program counter, and the next fetch happens from there. Loops, if statements and function calls are all built from this single trick.

Real processors overlap the three steps for many instructions at once (a pipeline) and may even run them out of order, but they promise to give the same results as this simple one-at-a-time loop. For learning, the simple model is exactly right.

Instructions are numbers

Memory only stores numbers, so an instruction must be stored as a number as well. Designers split the bits of an instruction word into fields. In our tiny machine below, an instruction is a 16-bit word: the high 8 bits are the opcode and the low 8 bits are an operand. For example, "load the value 3 into the accumulator" has opcode 1 and operand 3, so the word is 0x0103.

JS Encode and decode instruction words

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

No output.

Real instruction sets do the same with more fields: ARM64 uses fixed 32-bit words with several register fields, x86-64 uses a prefix, opcode and optional address bytes. You saw both in the previous lesson. The idea does not change: decoding is slicing bits.

A complete tiny CPU

Now you have everything for a working machine. This one has a single working register called the accumulator (acc), a program counter and six instructions:

Opcode Name Meaning
1 LOAD n acc = n
2 ADD n acc = acc + n
3 SUB n acc = acc - n
4 JNZ a if acc is not zero, set the PC to address a
5 OUT print acc
6 HALT stop

The program in memory counts down from 3: load 3, print it, subtract 1, and go back to the print instruction while the result is not zero. The loop below is the entire fetch-decode-execute cycle, and it prints one line per cycle so you can watch the program counter and accumulator change.

JS The fetch-decode-execute loop

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

No output.

Study the trace. At cycle 4 the JNZ 1 instruction runs while acc is 2, so it writes 1 into pc, and cycle 5 fetches from address 1 again: that is a loop. At cycle 10 the accumulator is 0, the jump is not taken, pc stays at 4, and the HALT ends the run.

Things to try: change the LOAD word to start from 5, or add a second OUT instruction inside the loop. Remember that jump addresses are fixed numbers, so inserting an instruction shifts everything after it. Real assemblers solve that annoyance with labels, which you will use from the next lesson on.

Code and data are both just memory

In this machine, and in nearly every real computer, the instructions live in the same kind of memory as data. A program is only data that the CPU happens to interpret as instructions. Nothing in the hardware says which bytes are "code" except where the program counter points. This is called the stored-program idea, and it has two big consequences:

  • A compiler, an operating system loader or a JIT engine can create instructions in memory and then run them.
  • A bug or an attacker who can write into the wrong place can change what the CPU runs. Modern systems add protections (marking data memory non-executable) that you will meet in Security: Buffer Overflows and Defenses.

To see that code really is changeable data, the next example patches two instruction words and runs the same program again.

JS Code is just numbers in memory

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

No output.

The last line shows a classic assembly lesson: a loop whose exit condition can never become true runs until something outside stops it. The CPU has no idea that this is a mistake. Our limit plays the role of an operating system killing a runaway process.

What real CPUs add

A real CPU is this loop plus engineering to make it fast and useful:

  • More registers, with names and roles (the next chapters cover them).
  • Flags, small status bits set by arithmetic, so conditional jumps can ask "was the result zero?" or "was it negative?".
  • A stack in memory for function calls and temporary values.
  • Interrupts and system calls, ways for the hardware and the operating system to take control for a moment.
  • Pipelines, caches and out-of-order execution, which make the same loop run much faster without changing what it means.

The next lesson introduces the toy CPU that carries those ideas (registers, flags, memory and a stack) and runs real programs written in a readable assembly syntax.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the program counter hold?

    The program counter (rip on x86-64, pc on ARM64) always points at the next instruction. Fetching reads from that address and advances it.
  2. 2. In which order do the three steps of an instruction cycle run?

    The CPU fetches the instruction word, decodes it into opcode and operands, then executes it. Then the cycle repeats with the next instruction.
  3. 3. How does a jump instruction change the flow of a program?

    A jump only changes the program counter. The next fetch reads from the new address, which is how loops, if statements and calls are built.
  4. 4. Why do compilers try to keep values in registers instead of memory?

    Registers are inside the CPU and are read almost instantly, while a trip to main memory can cost on the order of a hundred cycles or more.

Summary

  • The CPU repeats one loop: fetch the instruction at the program counter, decode it, execute it.
  • The program counter is advanced during fetch, so while an instruction runs it already points at the next one.
  • A jump is just a write to the program counter; loops and branches are built from it.
  • Instructions are numbers split into fields (opcode and operands); decoding means slicing bits.
  • Registers are tiny and very fast; RAM is large and much slower.
  • Code and data share memory (the stored-program idea), which is powerful and a source of security problems.
  • A loop that never meets its exit condition runs forever; the CPU cannot tell it is a mistake.

Next: Meet the Toy CPU: Our Runnable Assembly, the readable instruction set you will use for the rest of the tutorial.