Concepts / What Is a Program?

What Is a Program?

The CPU understands only machine language, represented in zeros and ones. High-level languages like Python are invisible to the CPU's hardware.

  • Programming

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.

runproducesis executed byPython programhuman-readable instructionsTranslatorconverts the codeMachine languagezeros and onesCPUexecutes instructions
How does a high-level program become a sequence of actions performed by the computer?

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.

runconvertsloadsexecutesPython codereadable statementTranslator readsPython instructionsMachine languageCPU-specific binaryinstructionsMemorytranslated code loadedCPU executioninstructions processed
What happens between a high-level instruction and the CPU's executable instructions?
python

Following x = 5 + 3

What changes as the CPU receives the operation expressed by x = 5 + 3?

Human-readable instruction: The Python statement is concise and readable to a programmer. It expresses an assignment and an addition.

Translation: A translator converts the statement into a longer sequence of machine-language instructions. The result specifies the low-level operations required by the CPU.

CPU 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 languageMachine language
Designed to be readable and writable by peopleUnderstood by the CPU
Can use readable syntax such as Python statementsIs represented by zeros and ones
Hides many CPU-specific detailsSpecifies detailed operations for a CPU architecture
Can be portable when a translator exists for the target machineIs tied directly to particular CPU hardware
usestranslated intounderstood byPythonreadable statementProgrammerwrites and readsMachine languagezeros and onesCPUexecutes
What is the difference between code written for people and instructions represented as zeros and ones?

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.

inputconverts toexecutesHigh-level codePythonTranslatorconversion toolMachine languagezeros and onesCPUhardware
How does a translator connect a high-level instruction to CPU-executable instructions?

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.

translated ontranslated onproducesproducesPython programsame source codeTranslatormachine AMachine language Afor CPU ATranslatormachine BMachine language Bfor CPU B
How can the same Python program run on computers with different CPU architectures?

Sharing a Python Program

Why can the same Python program run on two computers with different CPUs?

Write once: The programmer writes the program in Python, a high-level language.

Translate on the first computer: A translator converts the Python code into machine language suited to the first computer's CPU.

Translate 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.

encodesencodesencodesZeros and onesbinary representationCPU registerswhich registers to useMemory locationswhich locations to accessLow-level operationswhat the CPU performs
How can a simple-looking binary representation stand for many detailed operations?

Mistakes About Programs

  • Assuming the CPU directly understands Python.

    The CPU understands machine language, not high-level languages such as Python.

    Fix: Remember the translation step: Python code is converted into machine language before the CPU executes it.

  • Assuming machine language is easier because it uses only zeros and ones.

    Machine language has intricate syntax and must specify detailed CPU operations.

    Fix: Separate the number of symbols from the difficulty of expressing instructions. Machine language is hardware-specific and impractical to write directly.

  • Assuming the same machine-language program works on every CPU.

    Different processor types have different machine languages.

    Fix: Use high-level code and translate it for the target machine.

  • Thinking portability means the same machine code is used everywhere.

    Each computer's translator can produce machine language suited to its own CPU.

    Fix: Portability 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?

  • The friend's CPU executes the Python text directly.
  • The program is translated into machine language for the friend's CPU.
  • The original computer's machine language is copied unchanged.
  • The Python code is replaced with zeros and ones by the programmer.
Reveal answer

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.

EASY

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.