Module 02 · How Computers Work · The machine runs a program · about 15 min to read
Continue where you left off ×
How Code Becomes Execution (compilers, stack & heap)
How Code Becomes Execution · Open board
The road from the words you type to the bytes the chip runs, and the two work areas a running program keeps while it’s on that road: one that grows and shrinks with every function call, one that holds anything that has to outlive the call that made it.
Before you start. The CPU (fetch-decode-execute, the ISA, assembly) left you with a chip that does exactly one thing, forever: fetch an instruction, decode it, run it - it has no idea what a word, a loop or a function is. The Memory Hierarchy (cache, locality, the memory wall) left you with the other half: while that chip runs, everything the program is working on sits in memory, in copies at different speeds.
The idea in 30 seconds
Code you can read isn’t something the chip can run. A compiler translates all of it first, an interpreter translates one line at a time as it goes.
Interpreting costs real time: hundreds of times slower per step than translating ahead.
A running program keeps two work areas: a stack for calls that haven’t finished, and a heap for anything that has to outlive the call that made it.
The stack only ever changes from the top, one entry per call, and each entry holds the address to resume at.
A pointer is an address you can hold and follow. The resume address is one too, so writing past the end of a stack entry can overwrite it and hijack the program.
Every language, however it gets there, ends at the same machine code the chip actually runs.
Read the story
Lesson 8 of 41 in How Computers Work
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson 1952: “Nobody believed that” The fix: translate it all first, or one line at a time While it runs, where does any of this live? A call, traced: the ticket that says where to come back When a card overflows Down to the bytes Terms it explains: compiler, interpreter, stack, heap, pointer, buffer overflow, use-after-free, memory leak, AST, bytecode, CALL/RET, dereference (*), GC, JIT, linker, segfault, stack frame, stack overflow, use-after-free / dangling pointer.
Review How Computers Work
Mark as read Saved in this browser, and to your account when you sign in.