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.

Source programhigh-level statementsCompilerreads the entire fileMachine instructionslow-level byte sequencesExecutable filesaved for later executionCPUexecutes instructionssubmittedtranslatessaved asrun
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

Worked example

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.

  1. 1
    Write

    The programmer writes the source code in a text editor and saves it as a file.

  2. 2
    Submit

    The programmer runs the compiler on that source file.

  3. 3
    Check

    The compiler reads the entire file and checks it for errors.

  4. 4
    Translate

    The compiler translates every statement into machine instructions.

  5. 5
    Save

    The compiler creates a new file containing machine language.

  6. 6
    Run

    The operating system can run the resulting executable file.

Result

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.

Program constructloop or function callByte sequencemachine instruction or dataCPU executionlow-level operationtranslated intoprocessed by
How are high-level instructions represented as binary machine instructions that a processor can execute?
Edge case

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

The operating system uses different conventions to identify files that can be run.

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

Entire source filePython codeComplete translationInterpretationExecutable fileProgram executionsubmitsave resultprovide while runningexecute on the fly
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 central difference is when translation occurs and whether a saved executable is produced.

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.

C source codePython interpreterimplementationC compilerPython executablecompiled programPython codeRunning Pythonprogramcompiled bycreatesreadsinterpreted into execution
How does C source code become the Python interpreter executable that receives and runs Python code?
Worked example

Launching Python

Identify the two translation stages involved when a learner launches Python and runs a Python script.

  1. 1
    Stage one

    Python developers write the interpreter in C and compile that C program into a machine-language executable.

  2. 2
    Stage two

    The learner launches that compiled executable, which reads Python code and interprets its statements while the program runs.

Result

Python is interpreted at use time by a translator that was compiled earlier.

Mistakes Beginners Make

  • Common mistake

    Assuming that Python's interpreter is written in Python.

    What it looks like

    Thinking that the program launched by the Python command is simply another Python script.

    Why it fails

    The source states that the Python interpreter is a compiled program written in C.

    Do this instead

    Separate the two ideas: Python code is interpreted, while the interpreter performing that work was compiled from C.

  • Common mistake

    Expecting an executable to look like readable source code.

    What it looks like

    Opening an executable in a text editor and treating the strange symbols as evidence that the file is damaged.

    Why it fails

    Executable contents consist of low-level byte sequences designed for CPU execution, not human reading.

    Do this instead

    Expect machine-language data to appear unreadable in an ordinary text editor.

  • Common mistake

    Assuming every operating system requires an executable suffix.

    What it looks like

    Concluding that a Linux or macOS file cannot run because its name does not end in .exe.

    Why it fails

    Linux and macOS use file permissions rather than a required suffix to mark files as executable.

    Do this instead

    Use the operating system's convention when identifying runnable files.

  • Common mistake

    Treating compilation and interpretation as the same workflow.

    What it looks like

    Assuming a compiler waits until execution to translate each statement.

    Why it fails

    A compiler translates the entire program before execution, while the interpreter processes Python statements as the program runs.

    Do this instead

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

At a glance

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

More concepts starting with M