Machine Language and Binary Representation
A compiler translates an entire program from high-level source code to machine language in one complete pass, then saves the result to an executable file for later execution.
From Human Instructions to CPU Instructions
When you write a program in a high-level language, you express instructions in a form intended for people to create and understand. A processor cannot execute those high-level statements directly. A compiler translates the entire program into machine language in one complete pass and saves the result in an executable file. That file can later be run by the operating system.
The Complete Compilation Pass
A C Program Becomes an Executable
Trace what happens after a programmer writes a program in C and submits its source file to a compiler.
Write: The programmer writes the source code in a text editor and saves it as a file.
Submit: The programmer runs the compiler on that source file.
Check: The compiler reads the entire file and checks it for errors.
Translate: The compiler translates every statement into machine instructions.
Save: The compiler creates a new file containing machine language.
Run: The operating system can run the resulting executable file.
The high-level source file has been converted into a saved executable containing machine language.
The important point is that compilation happens before the program is run. The compiler works on the complete source file rather than translating only one statement at a time while the program is executing. If the compiler reports an error, the program has not successfully completed translation, so the error must be fixed before the executable can be run.
Binary Content Inside Executables
Machine language is represented inside an executable as low-level byte sequences. Each byte or sequence of bytes represents a machine instruction or a data value. These sequences are designed for CPU execution rather than human interpretation, so opening an executable in a text editor usually produces strange symbols, control characters, and other unreadable output.
Unreadable executable content is not necessarily evidence of file corruption. The strange display can be the expected result of viewing machine language with a tool designed for human-readable text.
Executable Files by Operating System
| Operating system | How executable files are identified | Typical characteristic |
|---|---|---|
| Windows | File suffix | .exe or .dll |
| Linux | File permissions | No special suffix is required |
| macOS | File permissions | No special suffix is required |
On Windows, executable files are identified by suffixes such as .exe or .dll. Linux and macOS use file permissions instead of relying on a special suffix to mark a file as runnable. The visible naming convention differs, but the purpose of the executable is the same: it contains machine-language instructions that the operating system can run directly.
Compiler and Interpreter Workflows
With a compiler, you provide the source file to a translation step that processes the entire program and produces an executable for later execution. With an interpreter, the program is processed as it runs. The source pack describes Python as an interpreted language: the Python interpreter reads Python code and translates each Python statement into machine language on the fly.
| Question | Compiler workflow | Interpreter workflow |
|---|---|---|
| When is translation performed? | Before execution, for the complete program | As the program runs |
| What receives the source? | A compiler | An interpreter |
| What does the process produce or perform? | A saved executable file | Execution while statements are interpreted |
| Typical error timing described in the source | Compilation error before the program runs | Runtime error after execution has started |
The Compiled Program Behind Python
The Python interpreter is itself a compiled program written in C. Python code is therefore interpreted by a compiled translator. The Python developers wrote the interpreter's C source code and used a C compiler to create the machine-language executable that runs when you launch Python.
Launching Python
Identify the two translation stages involved when a learner launches Python and runs a Python script.
Stage one: Python developers write the interpreter in C and compile that C program into a machine-language executable.
Stage two: The learner launches that compiled executable, which reads Python code and interprets its statements while the program runs.
Python is interpreted at use time by a translator that was compiled earlier.
Mistakes Beginners Make
Assuming that Python's interpreter is written in Python.
The source states that the Python interpreter is a compiled program written in C.
Fix:
Separate the two ideas: Python code is interpreted, while the interpreter performing that work was compiled from C.Expecting an executable to look like readable source code.
Executable contents consist of low-level byte sequences designed for CPU execution, not human reading.
Fix:
Expect machine-language data to appear unreadable in an ordinary text editor.Assuming every operating system requires an executable suffix.
Linux and macOS use file permissions rather than a required suffix to mark files as executable.
Fix:
Use the operating system's convention when identifying runnable files.Treating compilation and interpretation as the same workflow.
A compiler translates the entire program before execution, while the interpreter processes Python statements as the program runs.
Fix:
Ask whether translation produces a saved executable before execution or happens during execution.
Check Your Understanding
A learner writes a program in a compiled language, submits the complete source file to a compiler, and receives a new file. Explain what the compiler did, what the new file contains, and how the answer would differ if the learner were running Python code through the Python interpreter.
Hints
- Mention whether the compiler processes the entire source file or only one statement.
- Describe the relationship between the output file and machine language.
- Remember that the Python interpreter is itself a compiled C program.
What do you think happens?
You open a compiled executable in a text editor. What are you most likely to see?
Reveal answer
Answer: Strange symbols and control characters
The executable contains low-level byte sequences designed for CPU execution rather than human interpretation.
Key Takeaways
- A compiler reads an entire high-level program, checks it, translates it into machine instructions, and saves the result as an executable file.
- Machine language is stored as low-level byte sequences intended for CPU execution, so executable contents are not normally readable as text.
- Windows commonly identifies executable files with .exe or .dll suffixes, while Linux and macOS use file permissions rather than a required suffix.
- The Python interpreter is a compiled C program that reads Python code and interprets its statements while they run.
- Compilation errors occur before a compiled program can run, while the source describes runtime errors in an interpreted program as occurring after execution has started.
Key Takeaways
- Compilers translate complete high-level programs into machine language before execution.
- The translated machine language is saved in an executable file containing low-level byte sequences.
- Windows uses executable suffixes such as .exe and .dll, while Linux and macOS use file permissions.
- Python code is interpreted by a compiled C program: the Python interpreter.
- Compiler workflows produce an executable before running, whereas interpreter workflows process code as it runs.