Code Bytes · "How Computers Work" · Episode 7 · Software layers
Machine code vs high-level code
A CPU only understands machine code, instructions written as patterns of ones and zeros, each doing one tiny job. People write high-level code like Python instead, which is close to human language and far from the hardware. A translator program turns that code into machine steps the CPU can run.
What the CPU understands
Your CPU (central processing unit, the "brain" of the computer) can only carry out machine code: instructions written as patterns of ones and zeros, like 10110000. Each one is a bit pattern (bits are the ones and zeros from Code Bytes EP26).
Every machine code instruction does one tiny job, like "add two numbers" or "copy this number into memory". The CPU runs them one after another with the fetch-decode-execute cycle (Code Bytes EP30).
For people, machine code is almost impossible to read. A long program is just a wall of ones and zeros.
High-level code
So we write high-level code instead, in languages like Python. High-level means close to human language and far from the hardware: you write words and symbols such as total, =, + and print, and you don't have to think about what is happening inside the chip.
Machine code is the opposite: low-level, close to the hardware and far from human language.
The same idea in Python
``python total = 2 + 3 print(total) ``
`` 5 ``
The first line adds 2 and 3 and stores the answer in a variable called total. The second line prints it.
Who translates?
The CPU can't read that Python directly. A translator program turns it into machine steps, something like:
- load 2
- add 3
- store total
Each of those steps is one machine instruction, a pattern of ones and zeros. So you write one easy line, and the CPU runs several tiny ones. That is the whole point of high-level languages: you think in ideas, and the translator handles the tiny steps.
Translators come in two main kinds, compilers and interpreters. What a compiler does is the next lesson.
Try it yourself
Open any free online Python editor (search "online Python") and run:
``python price = 4 count = 3 print(price * count) ``
You should see:
`` 12 ``
Three easy lines for you; for the CPU, many tiny steps: load the numbers, multiply, store, and print.
Good to know
- Every snippet on this page was run with Python 3 (3.13) and printed exactly what is shown.
- Each CPU family (for example the chips in most laptops versus most phones) has its own machine code, so the same machine code does not run on every computer. High-level code like Python can run on all of them, because each has its own translator.
- "Load 2, add 3, store" is an illustration of the kind of steps a CPU does, not the real output. Python actually works out
2 + 3 = 5before the program even runs. You can see this with Python'sdismodule:import disthendis.dis("total = 2 + 3")showsLOAD_CONST 5followed bySTORE_NAME total. - The binary rows in the video are made-up examples, not real instructions.
- The usual Python first compiles your code to bytecode, and an interpreter (itself a program in machine code) carries that bytecode out. So the CPU runs machine code in the end, but your Python line is not turned into a stand-alone machine code file.
- The toy CPU in the comment question is invented. Real instruction lengths vary by CPU: some CPUs give every instruction the same length, others use anywhere from 1 to 15 bytes.
Answer to the comment question
Our toy CPU uses 8 bits per instruction. A program has 4 instructions. How many bits?
32 bits.
Each of the 4 instructions is 8 bits long: 4 × 8 = 32. You can check it in Python 3:
``python print(8 * 4) ``
`` 32 ``
Test your knowledge
Quick quiz: Machine code vs high-level code
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Britannica, "machine language": machine code is the set of binary instructions a CPU carries out directly, each doing one simple operation
- Wikipedia, "High-level programming language": high-level languages are close to human language, hide hardware details, and are translated before the CPU can run them
- Python Software Foundation, "Glossary: bytecode": Python code is compiled to bytecode that the interpreter carries out
- Python Software Foundation, "dis: Disassembler for Python bytecode": used to check the steps Python makes from the two-line program
Video transcript
How does your Python turn into something the CPU can actually run?
Your CPU only understands machine code: instructions written as patterns of ones and zeros. Each one is a tiny job, like add two numbers. For people, it's almost impossible to read.
So we write high-level code instead, like Python. High-level means close to human language, far from the hardware.
In Python: total equals two plus three, then print total. It prints five.
The CPU can't read that, so a translator program turns it into machine steps: load two, add three, store. You write one easy line. The CPU runs several tiny ones.
Our toy CPU uses eight bits per instruction. A program has four instructions. How many bits? Comment your answer below!
That's your byte.
More from "How Computers Work"
- "How Computers Work" EP2How text is stored
- "How Computers Work" EP3The CPU
- "How Computers Work" EP4RAM vs storage
- "How Computers Work" EP5The fetch-decode-execute cycle