30% off your first 3 months

Get the deal

How a computer runs your code

by DuckyHelperNov 12, 20265 min read
Computer science

TL;DR:

  • A processor repeats three steps: fetch the next instruction from memory, decode what it means, execute it.

  • The CPU does the work, RAM holds what is running right now, and storage keeps files when the power is off.

  • A compiled language is translated to machine code before it runs. An interpreted language is read and run by another program as it goes.

You write x = 2 + 3 and press run. Somewhere inside the machine, a huge number of tiny switches settle into a pattern that means 5. How does a line of text turn into that? The answer is a very small loop that a processor runs over and over, one tiny instruction at a time.

The idea in plain words

Three parts do most of the work.

  • The CPU (central processing unit) carries out instructions. It has a few very fast storage slots called registers.

  • Memory (RAM) holds the program and data that are being used now. It is fast but loses its contents when the power goes off.

  • Storage (a drive or SSD) keeps files permanently, but it is much slower than RAM. A program is loaded from storage into RAM before it runs.

A program, once translated, is a long list of simple instructions in memory. The CPU keeps a register called the program counter that holds the address of the next instruction.

Why it is a cycle

A CPU does not understand x = 2 + 3. It understands very small instructions such as 'put this number in a register' or 'add these two numbers'. To run a program it repeats the same three steps forever.

  1. Fetch. Read the instruction at the address in the program counter, then move the counter to the next address.

  2. Decode. Work out what the instruction means: which operation, which registers or addresses.

  3. Execute. Carry it out: do the arithmetic, move data, or jump to another address.

Worked example: the fetch, decode, execute cycle

This is a made-up, simplified processor with one working register, A. It is only for teaching; real chips have many more instructions and registers. The program computes 2 + 3 and saves the result at memory address 10.

Address

Instruction

Meaning

0

LOAD A, 2

Put the number 2 in register A

1

ADD A, 3

Add 3 to register A

2

STORE 10

Copy register A into memory address 10

3

HALT

Stop

Now trace each pass through the cycle. The program counter, PC, starts at 0.

Cycle

Fetch

Execute

A after

PC after

1

LOAD A, 2 from address 0

A = 2

2

1

2

ADD A, 3 from address 1

A = 2 + 3

5

2

3

STORE 10 from address 2

memory[10] = 5

5

3

4

HALT from address 3

stop

5

stop

After four cycles, address 10 holds the number 5, which is the value of x. Each step was trivial. What makes computers powerful is that they run an enormous number of such steps every second, and that a long sequence of simple instructions can do anything a program asks.

How a loop looks to the CPU

There is no 'repeat' instruction in our toy chip. A loop is a jump: an instruction that changes the program counter. Here is a countdown that repeats until A reaches zero. JNZ 1 means 'jump to address 1 if A is not zero'.

Address

Instruction

0

LOAD A, 3

1

SUB A, 1

2

JNZ 1

3

HALT

Trace it. Cycle 1: A = 3. Cycle 2: SUB makes A = 2. Cycle 3: A is not zero, so PC jumps back to 1. Cycle 4: A = 1. Cycle 5: jump again. Cycle 6: A = 0. Cycle 7: A is zero, so the jump is skipped and PC moves on to 3. Cycle 8: HALT. The loop body ran 3 times and the whole program took 8 cycles.

Every for and while loop you write ends up as a pattern like this, built from a comparison and a jump.

From your code to instructions

Nobody writes programs one instruction at a time. A compiler or interpreter translates readable code into instructions the CPU can run.

  • Compiled languages, such as C, are translated into machine code ahead of time. The result is a file the CPU runs directly.

  • Interpreted languages are read by another program, the interpreter, which carries out your code as it goes. The standard Python runs this way: it first compiles your code to a compact intermediate form, then a program executes that form.

  • Some systems mix the two, translating the busiest parts into machine code while the program runs.

Compiled programs tend to start faster and run faster. Interpreted languages are often easier to change and try out, since you can run a line right after writing it. Neither is better in every case.

How to check your answer

  • Can you list the three steps of the cycle in order?

  • Does your trace update the program counter after each fetch?

  • Does the final register and memory value match what the program should compute: 2 + 3 = 5?

  • Can you say where each thing lives: the program and data in RAM, files in storage, the working numbers in registers?

Common mistakes

  • Mixing up memory and storage. Both hold data, but RAM is temporary and fast.

  • Thinking the CPU understands Python. It runs machine instructions.

  • Forgetting that the program counter moves on, unless a jump changes it.

  • Thinking a faster CPU always makes every program faster. Waiting for storage or the network can dominate.

Where you will use it next

This picture explains why variables live in memory, why recursion uses a stack of calls, and why some code runs faster than other code. The objects you create in a program also live in memory, as in classes and objects in plain words. Next, practise writing your own named piece of code in writing your first function.

Frequently asked questions

What is a clock speed?

It is how fast the processor's internal timer ticks. Each instruction takes one or more ticks. A higher clock speed helps, but design matters as well.

Why does a program need to be loaded into RAM?

The CPU reads instructions from memory very quickly. Storage is far too slow to feed it directly.

Get started

DuckyHelper can
do more

It sees your screen and works in any app you use. Try it for 10 minutes, no card.

Get it for Mac, Windows or Linux

Share

Questions and jobs

Do anything

Ask it a question or hand it a job, in any app.

Doing jobs for you is in early access on Mac. Asking works on Mac, Windows and Linux.