Machine Language
A CPU directly executes machine language, not a high-level program.
Why a Processor Cannot Read Your Program Directly
Suppose you write a program in a language that is easy for people to read, such as Python. The program describes what you want done in terms that suit human thinking. A computer's CPU, however, does not work that way. It directly executes machine language, which is a set of very simple instructions that the CPU is built to understand. Your readable program is not in that form, so the CPU cannot simply run it as written.
What do you think happens?
If you save a Python file on a computer and double-click it, does the CPU run the Python text line by line exactly as written?
Reveal answer
No, something must translate the program into machine language or carry out its instructions through machine-language commands
The CPU directly executes only machine language. A high-level program must first be translated, either all at once by a compiler or piece by piece by an interpreter, before its instructions can be carried out.
A Program as an Ordered List of Instructions
A program is a list of unambiguous instructions for mechanical execution. Every program begins as a set of instructions. For a computer to carry them out mechanically, those instructions must be expressed in a language the computer is built to understand. That language is machine language.
The word unambiguous matters. A computer does not guess what you meant. Each instruction must have exactly one meaning, so the machine can perform it without interpretation by a human. Program execution is the process of carrying out the instructions in a program, one after another. The decisive question for any program is whether its instructions are already written in the machine language of the computer that will run them.
Machine language is the language a particular type of computer is built to carry out. It is not a single universal language. Each type of computer has its own machine language, and direct execution requires that the program be expressed in that computer's machine language.
Tracing Relationships Between Program, Instructions, and Execution
Four ideas are linked in a chain. A program is made of instructions. Those instructions must be in machine language before the CPU can execute them directly. Execution is the act of carrying them out. When the program starts in a high-level language, a translation step sits between the program and execution. The diagram below shows these connections.
Translating a High-Level Program
A high-level program is written for human convenience. Before it can be executed, something must translate it. The central difference between the two main approaches is when that translation happens. A compiler translates the entire program before execution. An interpreter translates instructions as they are needed during execution.
Imagine a high-level program as a complete document written in one language, and machine language as the language understood directly by the CPU. A compiler produces a translated machine-language program from the whole document. Once that translation is complete, the result can be run repeatedly, as long as it is used with the type of computer whose machine language it contains.
- This text is readable by people but is not machine language.
- Before the CPU can carry out these steps, a compiler or interpreter must translate them.
Following the Compiled and Interpreted Paths
The flowchart below traces both paths for the same high-level program. Follow the compiled path from the top: the whole program is translated first, and only then does the CPU run the result. Follow the interpreted path from the bottom: each instruction is read, understood, and carried out before the next one is read.
The key difference lies in the point where translation occurs. In the compiled path, translation is a separate step that finishes before execution begins. In the interpreted path, translation is interleaved with execution, one instruction at a time. In both cases, the CPU ultimately carries out machine-language commands, and in both cases the high-level program must be turned into machine language before it can be executed.
Why Compiled Output Belongs to One Type of Computer
Machine language is not a universal instruction set shared by every computer. Different types of computers have different machine languages. A compiler's output is therefore connected to the computer type for which it was produced. If the same high-level program must run on another type of computer, it has to be translated again, using a compiler that produces the appropriate machine language.
Mistakes Learners Make About Machine Language
Common mistake Believing the CPU can run any high-level program directly
Why it failsThe CPU directly executes only machine language, so a high-level program must first be translated or carried out through machine-language commands.
Do this insteadAsk whether the program is already in the machine language of the computer. If not, a translation step must come first.
Common mistake Assuming there is one universal machine language
Why it failsEach type of computer has its own machine language.
Do this insteadRemember that a compiled executable is tied to the computer type it was produced for.
Common mistake Thinking an interpreter creates a complete machine-language program file
Why it failsAn interpreter does not first create a complete executable. It reads and performs one instruction at a time.
Do this insteadDescribe interpretation as reading, deciding, and performing machine-language commands repeatedly during execution.
Common mistake Confusing a program's readable form with its executable form
Practice: Trace a Program's Path
A teacher gives you a high-level program and says it will be compiled on a computer of type A, then the resulting executable will be copied to a computer of type B and run there. Trace each step. Where does translation happen, what is produced, and what must be done before the program can run on computer B?
Hints
- Identify which step is the whole-program translation and which computer's machine language it targets.
- Consider whether an executable built for type A's machine language matches type B's machine language.
- If translation is needed again, which tool must produce the new output?
Key Takeaways
- A CPU directly executes machine language, a set of very simple instructions it is built to understand, not a high-level program.
- A program is a list of unambiguous instructions, and direct execution requires those instructions to be written in the computer's own machine language.
- A compiler translates the whole program before execution and produces an executable; an interpreter reads and performs one instruction at a time during execution.
- Each type of computer has its own machine language, so compiled output is tied to the computer type it was produced for.