Concepts / Machine Language and Binary Representation

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.

  • Programming

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.

submittedtranslatessaved asrunSource programhigh-level statementsCompilerreads the entire fileMachine instructionslow-level byte sequencesExecutable filesaved for later executionCPUexecutes instructions
What happens to an entire high-level program as a compiler translates it into machine language and saves the result as an executable file?

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.

translated intoprocessed byProgram constructloop or function callByte sequencemachine instruction or dataCPU executionlow-level operation
How are high-level instructions represented as binary machine instructions that a processor can execute?

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 systemHow executable files are identifiedTypical characteristic
WindowsFile suffix.exe or .dll
LinuxFile permissionsNo special suffix is required
macOSFile permissionsNo 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

submitsave resultprovide while runningexecute on the flyEntire source filePython codeComplete translationInterpretationExecutable fileProgram execution
How does the workflow differ when source code is translated all at once by a compiler versus processed as it runs by an interpreter?

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.

QuestionCompiler workflowInterpreter workflow
When is translation performed?Before execution, for the complete programAs the program runs
What receives the source?A compilerAn interpreter
What does the process produce or perform?A saved executable fileExecution while statements are interpreted
Typical error timing described in the sourceCompilation error before the program runsRuntime 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.

compiled bycreatesreadsinterpreted into executionC source codePython interpreterimplementationC compilerPython executablecompiled programPython codeRunning Pythonprogram
How does C source code become the Python interpreter executable that receives and runs Python code?

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

MEDIUM

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?

  • The original high-level source code
  • Readable comments and variable names
  • Strange symbols and control characters
  • A list of compiler instructions written for humans
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

  1. A compiler reads an entire high-level program, checks it, translates it into machine instructions, and saves the result as an executable file.
  2. Machine language is stored as low-level byte sequences intended for CPU execution, so executable contents are not normally readable as text.
  3. Windows commonly identifies executable files with .exe or .dll suffixes, while Linux and macOS use file permissions rather than a required suffix.
  4. The Python interpreter is a compiled C program that reads Python code and interprets its statements while they run.
  5. 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.