What Is a Program?
A program is a set of instructions that a computer carries out.
From Instructions to Action
A program is a set of instructions that a computer carries out. When you write Python, you express those instructions in a form designed for people to read and write. The CPU, however, does not understand Python directly. It understands machine language, represented by zeros and ones. Running a program therefore requires a bridge between the language used by the programmer and the language understood by the CPU.
Tracing a Python Run
Suppose you write a small Python program. The text is meaningful to you because Python uses a high-level language designed for human readability. The CPU's hardware has no direct understanding of that Python text. When you run the program, a translator reads the Python code and converts it into machine language for the particular CPU. That machine language is loaded into memory, and the CPU executes the resulting binary instructions.
Following x = 5 + 3
What changes as the CPU receives the operation expressed by x = 5 + 3?
- 1Human-readable instruction
The Python statement is concise and readable to a programmer. It expresses an assignment and an addition.
- 2Translation
A translator converts the statement into a longer sequence of machine-language instructions. The result specifies the low-level operations required by the CPU.
- 3CPU execution
The translated instructions are loaded into memory and executed by the CPU.
The same computation has been expressed first in Python and then in machine language. The Python statement is not itself a machine-language instruction; it is converted into instructions the CPU can execute.
Two Different Languages
| High-level language | Machine language |
|---|---|
| Designed to be readable and writable by people | Understood by the CPU |
| Can use readable syntax such as Python statements | Is represented by zeros and ones |
| Hides many CPU-specific details | Specifies detailed operations for a CPU architecture |
| Can be portable when a translator exists for the target machine | Is tied directly to particular CPU hardware |
High-level code and machine language express computation in fundamentally different ways.
The difference is not merely visual. A Python statement is a human-friendly way to describe a computation. The corresponding machine language is a different expression of that computation, one that speaks directly to the CPU's architecture. For the example x = 5 + 3, the machine-language form is not simply the Python text written with different symbols. It is a longer sequence that specifies details such as which CPU registers to use, which memory locations to access, and which low-level operations to perform.
The Translator's Role
A translator is a tool that converts high-level language code into machine language that a CPU can execute. It solves the basic communication problem between human programmers and CPU hardware.
Portability Through Re-translation
Machine language is hardware-specific. Different processor types, including Intel, ARM, and AMD processors, have different machine languages. Machine language written for one processor type will not run on a different processor type. High-level code gains portability because it can be translated again for the CPU in another computer.
Sharing a Python Program
Why can the same Python program run on two computers with different CPUs?
- 1Write once
The programmer writes the program in Python, a high-level language.
- 2Translate on the first computer
A translator converts the Python code into machine language suited to the first computer's CPU.
- 3Translate on the second computer
A translator on the other computer converts the same Python code into machine language suited to that computer's CPU.
The machine-language outputs can be different, but the high-level Python program can run on both computers as long as each machine has a Python translator installed.
Binary Does Not Mean Simple
Machine language may appear simple because it uses only two symbols: zero and one. That appearance is misleading. Its syntax is more intricate than Python, and writing it directly is impractical for programmers. A machine-language sequence must express detailed CPU-specific operations. For a computation such as x = 5 + 3, the sequence can specify registers, memory locations, and low-level operations. The small alphabet does not remove complexity; it makes the instructions harder for people to read and write.
Mistakes About Programs
Common mistake Assuming the CPU directly understands Python.
What it looks likeA learner writes a Python statement and imagines that the CPU executes that text exactly as written.
Why it failsThe CPU understands machine language, not high-level languages such as Python.
Do this insteadRemember the translation step: Python code is converted into machine language before the CPU executes it.
Common mistake Assuming machine language is easier because it uses only zeros and ones.
What it looks likeBinary appears simpler than Python because it has only two symbols.
Why it failsMachine language has intricate syntax and must specify detailed CPU operations.
Do this insteadSeparate the number of symbols from the difficulty of expressing instructions. Machine language is hardware-specific and impractical to write directly.
Common mistake Assuming the same machine-language program works on every CPU.
What it looks likeA machine-language program for an Intel processor is expected to run unchanged on an ARM processor.
Why it failsDifferent processor types have different machine languages.
Do this insteadUse high-level code and translate it for the target machine.
Common mistake Thinking portability means the same machine code is used everywhere.
What it looks likeA Python program runs on two computers, so the learner concludes that both CPUs must be executing identical machine-language instructions.
Why it failsEach computer's translator can produce machine language suited to its own CPU.
Do this insteadPortability comes from re-translation of the same high-level program on each target machine.
When tracing a program, keep three layers separate: the high-level instructions written by the programmer, the translated machine language, and the CPU's execution of that machine language. This separation makes it easier to understand both why translators are necessary and why high-level programs can be portable.
Check Your Understanding
What do you think happens?
A Python program is sent from one computer to a friend's computer, and the two computers use different CPU types. What must happen before the friend's CPU can execute the program?
Reveal answer
The program is translated into machine language for the friend's CPU.
The source program remains high-level Python, but a translator on the friend's machine converts it into machine language suited to that machine's CPU.
In your own words, explain why a high-level language can be portable even though machine language is hardware-specific.
Hints
- Identify which language is written by the programmer.
- Identify which component converts that language.
- Explain what changes when the target CPU changes.
Key Takeaways
- A program is a set of instructions, but the CPU can directly understand only machine language represented by zeros and ones.
- High-level languages such as Python are designed for human readability and are not directly understood by the CPU's hardware.
- Translators convert high-level code into machine language that the CPU can execute.
- Machine language is tied to particular CPU hardware, while high-level programs can be portable through re-translation on different machines.
- Although machine language uses only two symbols, its syntax and CPU-specific details make it more complex to write than Python.