How instructions work
Part of the Fundamentals section of Coddy's Assembly journey — lesson 4 of 45.
You know that the CPU executes instructions one by one. But how does the CPU know which instruction to run next? Where are the instructions stored? And what does an instruction actually look like?
Instructions are numbers.
This is a fundamental truth about computers. Everything is a number. The data you work with (like the number 42) is a number. The address where data lives (like 1000) is a number. And the instructions themselves (like mov, add, cmp) are also numbers.
When you write assembly code like mov rax, 5, the assembler converts this human-readable text into a number that the CPU understands. This number is called an instruction code.
Where are instructions stored?
Instructions are stored in memory, just like data. There is no special place for instructions. Memory holds both:
- Data (numbers, characters, arrays)
- Instructions (the program itself)
The CPU does not know the difference. It simply reads whatever is at the memory address pointed to by the Program Counter. You learned about the PC briefly in Lesson 0.1. Now it is time to understand exactly how it works.
The Program Counter (also called the Instruction Pointer) is a special register inside the CPU. Its only job is to hold the memory address of the next instruction to execute.
The Fetch-Execute Cycle:
The CPU repeats these three steps forever:
| Step | What happens |
|---|---|
| 1. FETCH | Read the instruction at the address in PC |
| 2. EXECUTE | Perform that instruction |
| 3. ADVANCE | Move PC to the next instruction |
Simple example:
If PC = 1000, the CPU:
- Fetches the instruction at address 1000
- Executes it
- Moves PC to the next address (1000 + instruction size)
Then it repeats with the new PC value.
What an instruction looks like:
Every instruction has two parts:
| Part | What it does | Example |
|---|---|---|
| Opcode | What to DO | mov, add, cmp |
| Operands | What to do it TO | rax, 5, [result] |
Some instructions have one operand (like inc rax to add 1). Some have two (like mov rax, 5). Some have three (like add rax, rbx, rcx in some architectures).
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
1The Machine
What is CPUMemoryRegisters vs MemoryHow instructions workTwo-File PatternThe x86-64 Architecture