# How a CPU runs an instruction

> The CPU fetches bits from memory, decodes them, runs them in the ALU, then writes results and advances the PC.

Source: https://fullbytedeveloper.com/blog/how-a-cpu-runs-an-instruction
Published here: 2026-12-03, updated 2026-12-03

Your program becomes a file of machine instructions. The CPU's job is a loop: fetch one instruction from memory, decode it, execute it, store the result, then move on [1]. Part 2 of this lane is [pipelining and why CPUs speculate](/blog/cpu-pipelining-speculate).

## Quick start (under a minute)

You do not need silicon in your hands to see instruction bytes. Compile a tiny C function and disassemble it:

```bash
echo 'int add_two(int a){ return a+2; }' > /tmp/t.c
cc -c -O0 -o /tmp/t.o /tmp/t.c
objdump -d /tmp/t.o | head -n 20
```

You should see mnemonic lines (`mov`, `add`, `ret` on x86-64, or `add`/`ret` on AArch64) each tied to hex opcodes [2]. Those lines are what the CPU eventually fetches after the OS loads the executable into RAM [1].

## Load the executable into memory first

OpenStax: high-level code is translated into instructions stored as 1s and 0s in an executable. When you run a program, the OS loads that executable into main memory so the CPU can fetch from RAM instead of waiting on disk for every byte [1]. The loader also sets up the initial program counter and stack pointer per the ABI. Related memory picture: [stack vs heap memory](/blog/stack-vs-heap-memory).

## The single-cycle instruction loop (teaching picture)

Teaching texts collapse real hardware into a loop you can draw on a napkin [1]:

<figure aria-label="CPU instruction pipeline fetch decode execute writeback with program counter advance">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 360" role="img" aria-hidden="true">
  <defs>
    <marker id="cpu-arr" markerWidth="8" markerHeight="8" refX="4" refY="7" orient="auto">
      <path d="M0,0 L4,8 L8,0 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="320" height="360" rx="12" fill="#0b1f3a"/>
  <rect x="48" y="12" width="224" height="52" rx="6" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="160" y="34" text-anchor="middle" font-size="14" fill="#e6edf7" font-family="system-ui,sans-serif">fetch [1]</text>
  <text x="160" y="52" text-anchor="middle" font-size="13" fill="#e6edf7" font-family="system-ui,sans-serif">PC to IR</text>
  <line x1="160" y1="64" x2="160" y2="84" stroke="#14b8a6" stroke-width="2" marker-end="url(#cpu-arr)"/>
  <rect x="48" y="84" width="224" height="52" rx="6" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="160" y="106" text-anchor="middle" font-size="14" fill="#e6edf7" font-family="system-ui,sans-serif">decode</text>
  <text x="160" y="124" text-anchor="middle" font-size="13" fill="#e6edf7" font-family="system-ui,sans-serif">opcode fields</text>
  <line x1="160" y1="136" x2="160" y2="156" stroke="#14b8a6" stroke-width="2" marker-end="url(#cpu-arr)"/>
  <rect x="48" y="156" width="224" height="52" rx="6" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="160" y="178" text-anchor="middle" font-size="14" fill="#e6edf7" font-family="system-ui,sans-serif">execute</text>
  <text x="160" y="196" text-anchor="middle" font-size="13" fill="#e6edf7" font-family="system-ui,sans-serif">ALU [1]</text>
  <line x1="160" y1="208" x2="160" y2="228" stroke="#14b8a6" stroke-width="2" marker-end="url(#cpu-arr)"/>
  <rect x="48" y="228" width="224" height="52" rx="6" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="160" y="250" text-anchor="middle" font-size="14" fill="#e6edf7" font-family="system-ui,sans-serif">write-back</text>
  <text x="160" y="268" text-anchor="middle" font-size="13" fill="#e6edf7" font-family="system-ui,sans-serif">regs / memory</text>
  <path d="M72 254 C20 254 20 38 72 38" fill="none" stroke="#f59e0b" stroke-width="2" marker-end="url(#cpu-arr)"/>
  <text x="28" y="150" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif" transform="rotate(-90 28 150)">next PC</text>
</svg>
</figure>

Real cores add caches, TLBs, and multiple execution units. The loop still names the work each retired instruction does [1] [3].

## Walk the five beats in plain words

OpenStax's hardware story orders the beats like this [1]:

1. **Fetch:** bring the next instruction from the address in the program counter into an instruction register.
2. **Decode:** interpret the bit pattern (for example "add register x and y, store in z").
3. **Execute:** the ALU (and specialized units) perform the operation.
4. **Write back:** save the result in a register or memory location.
5. **Advance:** update the program counter so execution continues in program order unless control flow changes.

Branches and calls rewrite step 5: the "next" address is not always PC plus the instruction length [2]. A conditional branch compares flags from the ALU and may select a taken target encoded in the instruction.

## Meet the program counter and PC updates

OpenStax 5.4: the program counter (PC) keeps the address of the next instruction and is updated after each instruction so execution continues in sequence unless control flow changes [2]. On x86, instruction lengths vary; the CPU adds the decoded length, not a fixed word size. That detail matters when you debug corruption: a wrong return address is a wrong PC.

## Worked example: disassembly output you can compare

On this draft host (Ubuntu 24.04, GCC 13, x86-64, `-O0`, 2026-12-03), the add function disassembly begins like:

```text
/tmp/t.o:     file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <add_two>:
   0:	55                   	push   %rbp
   1:	48 89 e5             	mov    %rsp,%rbp
   4:	89 7d fc             	mov    %edi,-0x4(%rbp)
   7:	8b 45 fc             	mov    -0x4(%rbp),%eax
   a:	83 c0 02             	add    $0x2,%eax
   d:	5d                   	pop    %rbp
   e:	c3                   	ret
```

Reading left to right on one line: address, hex bytes, mnemonic [2]. At runtime the CPU fetches those bytes from the `.text` section in order until `ret` redirects control. Optimizing compilers reorder and elide instructions; always disassemble the build you profile.

If `objdump` is missing, install `binutils` per your distro docs. The pedagogy does not depend on one vendor ISA.

## Where memory fits in the same cycle

Loads and stores move data between registers and addresses computed from base plus offset [1]. Those addresses may hit L1 cache in a few cycles or DRAM in hundreds. Contiguous layouts keep more useful bytes per cache line than pointer chasing; see [arrays vs linked lists](/blog/arrays-vs-linked-lists).

## Why pipelines come next

OpenStax 5.6 splits the bulky single-cycle box into phases (fetch, decode, issue, execute, commit) so different instructions can occupy different phases at once [3]. Instruction *i+1* may decode while instruction *i* executes. Throughput rises toward one instruction per cycle per pipeline slot when the program is predictable.

When the next PC is unknown (taken branch, indirect call), the front end may guess so the pipe stays full. Wrong guesses waste work; details and safety fences live in [pipelining and why CPUs speculate](/blog/cpu-pipelining-speculate).

Endianness shows up at the fetch boundary: the same 32-bit word in memory may list bytes differently in a hexdump versus register display [2]. The CPU still decodes according to the ISA. Cross-endian bugs appear at I/O boundaries, not in everyday add instructions on one machine.

Interrupts and traps also enter through the same machine: an external device or a faulting load can redirect the PC into a handler after the current instruction retires or is aborted per the architecture rules [1]. From the programmer's seat that still looks like "the CPU ran instructions"; the difference is who chose the next address. Kernel entry paths, syscall stubs, and page-fault handlers are ordinary instruction streams once fetch resumes at the vector [1] [2].

## More reader questions

**What is the ALU?** The arithmetic logic unit performs integer math and bitwise ops on register operands named by the decoded instruction [1].

**Where do operands live during execute?** In registers when possible; load and store instructions move data between registers and memory addresses [1] [2].


Branches retarget the PC; a wrong return address is a wrong PC. That is why crash dumps lead with a faulting instruction pointer before anyone argues about high-level frameworks [2].

## Do not do these

Thinking the CPU "runs Python." CPython, Node, and the JVM translate or interpret to machine code or internal bytecode with runtime helpers [1]. The silicon still ends up in this fetch/decode/execute loop for the native pieces.

Inlining, loop unrolling, and SIMD widen the gap between your editor view and retired instructions [2].

Decode maps bits to control signals. Meaning lives in your source and debug symbols.

Ignoring the PC on crashes. Segfaults and illegal instructions often show a faulting PC in a core dump. That address is where fetch or execute failed.

Treating all instructions as equal cost. Multiply, divide, floating point, and memory misses stall different pipeline stages [3].

Trace one `add` in your head: where the bytes came from, which register changed, what memory might update. Pipelining and speculation deepen that picture in [CPU pipelining and why CPUs speculate](/blog/cpu-pipelining-speculate); how main memory feeds the core is [what is RAM (computer memory)](/blog/what-is-ram-computer-memory). For how managed runtimes reclaim heap after those allocations, see [how garbage collection works](/blog/how-garbage-collection-works).

When you read `objdump -d` output, match each mnemonic back to the fetch-decode-execute sketch above before you chase microarchitecture folklore.

Send this to the friend who writes JavaScript all day and has never pictured fetch and decode.

Image: Illustration of four linked platforms on cyan rails: complex fetch stage, orange decode disc, sliding execute cube, and a writeback stage of mixed shapes in a glass cube, no people and no readable UI text.
https://fullbytedeveloper.com/media/how-a-cpu-runs-an-instruction/how-a-cpu-runs-an-instruction-venice.webp
One instruction token walks fetch, decode, execute, then writeback.

## Sources

1. [OpenStax Intro CS 5.1 Computer systems organization, accessed 2026-12-03](https://openstax.org/books/introduction-computer-science/pages/5-1-computer-systems-organization)
2. [OpenStax Intro CS 5.4 Machine-level program representation, accessed 2026-12-03](https://openstax.org/books/introduction-computer-science/pages/5-4-machine-level-program-representation)
3. [OpenStax Intro CS 5.6 Processor architectures, accessed 2026-12-03](https://openstax.org/books/introduction-computer-science/pages/5-6-processor-architectures)
4. [Full Byte Developer: Pipelining and why CPUs speculate](https://fullbytedeveloper.com/blog/cpu-pipelining-speculate)
5. [Full Byte Developer: Stack vs heap memory](https://fullbytedeveloper.com/blog/stack-vs-heap-memory)
