What happens when you run a program
From a text file to instructions a CPU can run: memory, the fetch-decode-execute loop, and what compilers, interpreters, bytecode, and JIT actually do.
You type a command, press Enter, and a moment later something happens. Between those two moments, a plain text file becomes a sequence of tiny instructions that a chip can carry out. This note follows that path, because knowing it makes a lot of everyday things make sense: why some programs start instantly and others don’t, why some errors show up before the program runs and others only halfway through, and why deploying a Go binary feels so different from deploying a Node.js app.
The short version
- Your source code is just text in a file on disk.
- Some tool turns that text into instructions the machine can run, either ahead of time or while the program is running.
- The operating system loads those instructions and the data they need into memory.
- The CPU runs them, one small step at a time, in a loop that never stops until the machine shuts down.
The rest of this note unpacks each step.
Two parts that matter: the CPU and memory
A computer has many parts, but for running a program two of them do most of the work:
- The CPU executes instructions: add these two numbers, compare these values, jump to that place, copy this from there.
- Memory (RAM) holds the instructions being run and the data they work on. RAM is fast, but as Wikipedia puts it, it is “associated with volatile types of memory where stored information is lost if power is removed.”
That is why a program on disk has to be loaded first. Disk storage keeps files when the power is off; RAM is where the work happens.
The idea that a program’s instructions and its data can live in the same memory is usually traced to John von Neumann’s 1945 First Draft of a Report on the EDVAC. A stored-program computer “uses the same underlying mechanism to encode both program instructions and data.” (Historians point out that von Neumann was not the only one behind the idea, which is worth knowing before you repeat the name.) Almost every computer you’ll use today works this way.
The CPU’s loop: fetch, decode, execute
The CPU does not “read” your program the way you do. It repeats one cycle, over and over, billions of times per second. The instruction cycle is “the cycle that the central processing unit (CPU) follows from boot-up until the computer has shut down”:
| Stage | What happens |
|---|---|
| Fetch | Read the next instruction from memory |
| Decode | Work out what the instruction means |
| Execute | Do it: calculate, compare, move data, or jump |
Then it repeats with the next instruction. Everything a program does, from showing a web page to saving a file, is built out of these small steps.
There is a catch: the CPU only understands machine code, its own numeric instruction set. It cannot run for price in prices: or fmt.Println("hello") directly. Something has to translate.
From text to instructions: compilers and interpreters
There are two classic ways to bridge that gap.
- A compiler is “software that translates computer code written in one programming language (the source language) into another language (the target language).” Usually the target is machine code, and the translation happens before the program runs.
- An interpreter is “software that executes source code without first compiling it to machine code.” It reads the program and carries it out as it goes.
In practice, most modern languages sit somewhere in between. Three you are likely to meet:
Go: compiled ahead of time
The Go FAQ says it plainly: “Go programs are compiled ahead of time to native machine code.” You build once and get a file the operating system can run directly.
$ go build -o hello .
$ ./hello
hello
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, ...
ELF is the executable format Linux uses. The program needs no Go installation on the machine that runs it, only the same operating system and CPU type it was built for.
Python: compiled to bytecode, then interpreted
Python looks interpreted, but it does a compile step too. From the Python glossary: “Python source code is compiled into bytecode, the internal representation of a Python program in the CPython interpreter.” That bytecode then runs on a virtual machine, and it is “cached in .pyc files so that executing the same file is faster the second time.”
You can see the bytecode yourself with the standard dis module, which “supports the analysis of CPython bytecode by disassembling it.” Given this function:
def total(prices):
result = 0
for p in prices:
result = result + p
return result
Python 3.13 turns it into this (other versions differ slightly):
2 LOAD_CONST 1 (0)
STORE_FAST 1 (result)
3 LOAD_FAST 0 (prices)
GET_ITER
L1: FOR_ITER 7 (to L2)
STORE_FAST 2 (p)
4 LOAD_FAST_LOAD_FAST 18 (result, p)
BINARY_OP 0 (+)
STORE_FAST 1 (result)
JUMP_BACKWARD 9 (to L1)
...
5 LOAD_FAST 1 (result)
RETURN_VALUE
It is not machine code, but it is much closer to what a machine does: load a value, add, store, jump back to the top of the loop. After you import a module, a file like __pycache__/total.cpython-313.pyc appears next to it. That is the cached bytecode.
JavaScript: interpreted first, compiled when it gets hot
V8, the JavaScript engine in Chrome and Node.js, mixes both approaches. Since 2017 its pipeline has been “built upon Ignition, V8’s interpreter, and TurboFan, V8’s newest optimizing compiler.” Code starts running quickly as Ignition bytecode, and parts that run often get compiled to optimized machine code while the program is running. Compiling during execution like this is called just-in-time (JIT) compilation.
Side by side
| Go | Python (CPython) | JavaScript (V8) | |
|---|---|---|---|
| When translation happens | Before running | When loading, to bytecode | While running |
| What runs | Native machine code | Bytecode on a virtual machine | Bytecode, plus machine code for hot paths |
| Needs a runtime installed? | No | Yes, Python | Yes, Node.js or a browser |
Why this matters day to day
This is not just trivia. It explains things you will run into constantly:
- When errors appear. A compiler can reject a typo or a type mismatch before the program ever runs. In more dynamic languages, the same mistake may only surface when that exact line is reached, sometimes in production.
- How you deploy. A compiled Go program ships as a single file. A Node.js or Python app needs the right runtime and dependencies on the server. That difference shapes your whole deploy setup, from Dockerfiles to server images.
- Start-up and speed. Interpreters start fast but may run slower; ahead-of-time compilers pay the cost up front; JIT engines try to get the best of both by optimizing whatever turns out to be busy.
- What “it works on my machine” really means. A binary built for one operating system and CPU will not run on another without being rebuilt for it.
Try it yourself
- Save the
totalfunction above astotal.py, then runpython3 -c "import dis, total; dis.dis(total.total)". Change the loop tosum(prices)and compare the bytecode. - Run
python3 -c "import total", then look inside the new__pycache__folder. - If you have Go installed, build the hello program and run
fileon the result. Then tryGOOS=windows go buildand runfileagain.
Summary
- Source code is text; the CPU only runs machine code.
- Programs are loaded from disk into memory (RAM) before they run.
- The CPU repeats fetch, decode, execute, one instruction at a time.
- Compilers translate before running; interpreters execute as they go; most languages mix the two.
- Go compiles to native code, Python compiles to bytecode for a virtual machine, and V8 interprets first and compiles hot code just in time.
- These choices decide when you see errors, how you deploy, and how fast things start and run.
References
- Von Neumann architecture, Wikipedia (on the 1945 First Draft of a Report on the EDVAC).
- Instruction cycle, Wikipedia.
- Random-access memory, Wikipedia.
- Compiler and Interpreter (computing), Wikipedia.
- Go FAQ: “Does Go have a runtime?”, The Go Programming Language.
- Python glossary: bytecode and dis — Disassembler for Python bytecode, Python documentation.
- Launching Ignition and TurboFan, V8 blog, May 2017.