Find a course

Module 3: The ProcessorLesson 7 of 1113 min

Machine Code and Performance

Learn what machine code and instruction sets are, how pipelining, caches and multiple cores speed things up, and why GPUs excel at parallel work.

Step 1 of 13Learn

Speaking the processor's language

A CPU understands only machine code: instructions encoded as binary numbers. One instruction might be 32 bits long, with some bits saying add, some naming the destination register, and some naming the two source registers. The exact format is defined by the processor's instruction set architecture (ISA).

Two ISA families dominate. x86-64 powers most Windows PCs and many servers. ARM powers nearly every phone and tablet, many laptops and a growing share of servers. A third, RISC-V, is an open standard anyone can use without licensing fees.

A program compiled for one ISA won't run directly on another, because the numbers mean different things. That's why software sometimes comes in separate versions for different chips, or uses a translation layer.

Nobody writes large programs in raw binary. Assembly language gives each instruction a short name, like ADD R3, R1, R2, and an assembler turns it into machine code. Most software is written in higher-level languages such as Python or C, which other programs translate down to machine code, as you'll see later in this course.

Step 2 of 13Compare

Two instruction set philosophies

AspectRISC (reduced instruction set)CISC (complex instruction set)
InstructionsFewer, simpler, usually the same lengthMany, some doing multi-step work in one instruction
Memory accessOnly special load and store instructions touch memoryMany instructions can read or write memory directly
ExamplesARM, RISC-Vx86-64
TodayPopular for efficiency, common in phones and laptopsModern x86 chips internally split complex instructions into simpler RISC-like steps

Step 3 of 13Practice · True or false

An app compiled into machine code for an x86-64 processor can run unchanged on an ARM processor, because both understand binary.

Step 4 of 13Tap to reveal

What one instruction looks like in bits

  1. The opcode

    A group of bits at a fixed position names the operation, such as add, load or branch. The control unit reads these bits first to decide what the rest of the instruction means.

  2. The registers

    Further bit fields name the registers involved. With 32 registers, each field needs 5 bits, because 25 = 32. An add instruction names two source registers and one destination.

  3. An immediate value

    Some instructions carry a small constant directly inside them, such as add 4 to this register. Because the instruction has a fixed length, the constant is limited in size, and larger numbers must be loaded from memory or built in several steps.

Step 5 of 13Learn

Pipelining: an assembly line

Doing laundry with one washer and one dryer, you don't wait for the first load to dry before starting the second wash. While load 1 dries, load 2 washes. That's a pipeline.

CPUs do the same with instructions. While one instruction executes, the next is being decoded and the one after that is being fetched. With a 5-stage pipeline, up to five instructions are in flight at once, each at a different stage. Each instruction still takes five cycles from start to finish, but once the pipeline is full, one instruction finishes every cycle.

Pipelines have a weak spot: branches. When the CPU reaches if x > 0 then jump, it doesn't yet know which way to go, yet it must keep fetching. Modern CPUs use branch prediction, guessing based on past behavior, and they guess right well over 90% of the time on typical code. A wrong guess means throwing away partly finished work, which costs a dozen or more cycles.

High-end cores go further with superscalar execution, running several instructions side by side in each cycle, and out-of-order execution, rearranging independent instructions to keep every unit busy.

Step 6 of 13Practice · Work it out

A simple 5-stage pipeline starts empty. If no stalls occur, how many clock cycles does it take to completely finish 10 instructions?

Step 7 of 13Chart

Finishing 10 instructions on a 5-stage processor

Finishing 10 instructions on a 5-stage processorBar chart with 2 categories: No pipeline: 50; 5-stage pipeline: 14.01020304050CyclesNo pipeline5-stage pipeline5014
No pipeline: 10 × 5. Pipelined: 5 cycles to fill, then one finishes every cycle (5 + 9).
Finishing 10 instructions on a 5-stage processor
ItemClock cycles
No pipeline50
5-stage pipeline14

Step 8 of 13Learn

More cores, and their limits

Since clock speeds stalled, chipmakers have added more cores. A phone may have 6 to 8, a laptop 8 to 16, a server processor many dozens. Each core runs its own stream of instructions, so four cores can, in principle, do four times the work.

In practice, the gain depends on the software. A task must be split into pieces that can run at the same time, and many tasks contain parts that must happen in order. Amdahl's law captures this: if 25% of a program can't be parallelized, then even with infinite cores it can never run more than 4 times faster, because that 25% still takes the same time.

This is why some apps, like video exporting or 3D rendering, scale beautifully across cores, while others, like many games' main logic, depend more on single-core speed.

Graphics processing units (GPUs) take parallelism to the extreme: thousands of small, simple cores that apply the same operation to huge amounts of data at once, such as shading millions of pixels or multiplying the giant number grids used to train AI models.

Step 9 of 13Note

Step 10 of 13Explore

Amdahl's law: speedup from more cores

x is the number of cores. p is the share of the program that can run in parallel. Notice how the curve flattens.

05101520 204060
Speedup with 8 cores
2.91 x
Maximum speedup (infinite cores)
4 x

Step 11 of 13Note

Step 12 of 13Practice · Select all that apply

Which of these genuinely let a CPU get more work done per second? Select all that apply.

Select all that apply.

Step 13 of 13Practice · Choose one

Training an AI model involves repeating the same multiply-and-add operation on billions of numbers. Why are GPUs usually preferred over CPUs for this?