Concepts / How Computers Execute Instructions

How Computers Execute Instructions

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

Programs are usually written in a high-level language so that people can create and read them more practically. A compiler changes an entire high-level program into machine language in one complete pass. It then saves the translated result in an executable file that can be run later.

The Compiler Translation Pipeline

The compilation workflow begins with source code stored in a text file. You provide that file to a compiler. The compiler reads the entire file, checks it for errors, and translates every statement into machine instructions. After the translation is complete, it creates a new file containing machine language. That file is saved so the operating system can run it later.

Source codehigh-level programCompilerreads and translates theentire fileMachine languagelow-level byte sequencesExecutable filesaved for later executionprovide filetranslatesave
How does an entire high-level program move through a compiler and become a machine-language executable saved for later execution?
Worked example

A compiled program's journey

Imagine that you have written a program in C and want to run it on your computer.

  1. 1
    Write

    Create the program as high-level source code in a text editor and save it as a file.

  2. 2
    Compile

    Run the compiler on the file. It reads the entire file, checks for errors, and translates every statement into machine instructions.

  3. 3
    Output

    The compiler produces a new file containing machine language.

  4. 4
    Execute

    The operating system can run the resulting executable file directly.

Result

The source file remains the human-created input, while the new executable file contains the machine-language result saved for later execution.

What an Executable Contains

After compilation, the output file contains machine language. Machine language is made of low-level byte sequences intended for CPU execution, not human reading. If you open an executable in a text editor, you may see strange symbols, control characters, and gibberish. That appearance is expected: the file is not written for human interpretation.

Source codehigh-level textExecutable filemachine languageHuman readercan read the programCPUexecutes instructionshuman-readable inputdirect execution
What changes when source code is transformed into a saved executable, and how does the computer later use that executable?

Executable Files by Operating System

Operating systems use different conventions to identify runnable files. Windows identifies executable files with suffixes such as .exe or .dll. Linux and macOS use file permissions instead of relying on a suffix to mark a file as runnable. The identifying convention differs, but the compiled content is machine language instructions that the CPU can execute.

Windows.exe or .dll suffixMachine languageCPU-executable instructionsLinuxfile permissionsmacOSfile permissionsidentifies runnable filesidentifies runnable filesidentifies runnable files
How do executable files differ across operating systems, and what characteristics distinguish one format from another?
Operating systemHow runnable files are identifiedCompiled content
Windows.exe or .dll suffixesMachine language instructions
LinuxFile permissionsMachine language instructions
macOSFile permissionsMachine language instructions

The naming or permission convention differs, while the compiled result is machine language.

Compilation Compared with Interpretation

When code is given to a compiler, the compiler processes the entire source file before the resulting executable is run. When code is given to an interpreter, the interpreter reads and translates Python statements into machine language while the program is running. This means compilation produces a saved executable before execution, while interpretation combines translation with execution as the program proceeds.

Source programentire filePython programstatementsCompilercomplete translationPython interpretertranslates while runningSaved executablerun laterRunning programexecution proceedsprovide entire filesave translated resultprovide statementstranslate and execute
What happens differently when code is given to a compiler versus an interpreter, and when does the computer execute the instructions?
QuestionCompilerInterpreter
What is provided?An entire source filePython source statements
When does translation happen?Before the program is executedWhile the program is running
What is produced?A saved executable containing machine languageExecution of the program through the interpreter

This table contrasts the workflow described in the source material.

The Python Interpreter Has Its Own History

Python is interpreted, but the Python interpreter is itself a compiled program. The interpreter is written in C, a compiled language. Python developers used a C compiler to compile that interpreter into machine-language executable code. When you type python in a terminal or click the Python application, you launch that compiled C program. It then reads your Python script and interprets the Python statements while the script runs.

C source codePython interpreterimplementationPython scriptPython statementsC compilercompiles interpreterPython executionstatements interpretedwhile runningPython interpretercompiled machine-languageprogramcompilecreatesreadsinterpreted by
How can the Python interpreter be a program written in C that was itself compiled into machine code?
Key point

There are two different translation events to keep separate: the Python interpreter was compiled earlier from C, while your Python program is interpreted by that already-compiled interpreter.

Mistakes Beginners Make

  • Common mistake

    Assuming every executable must end with .exe.

    What it looks like

    A file on Linux or macOS may have no special executable suffix.

    Why it fails

    Linux and macOS use file permissions to mark files as runnable instead of relying on a suffix.

    Do this instead

    Treat .exe and .dll as Windows conventions, not universal requirements.

  • Common mistake

    Thinking that an executable should look readable in a text editor.

    What it looks like

    Opening a compiled file produces strange symbols and control characters.

    Why it fails

    The file contains low-level byte sequences intended for CPU execution.

    Do this instead

    Recognize that unreadable text can be normal for machine-language content.

  • Common mistake

    Thinking that Python is completely independent of compilation.

    What it looks like

    Saying that nothing in Python's execution process was compiled.

    Why it fails

    The Python interpreter is written in C and was compiled into a machine-language program.

    Do this instead

    Distinguish between the compiled Python interpreter and the Python code it interprets.

  • Common mistake

    Treating a compilation error and a runtime error as the same event.

    What it looks like

    Expecting a compiled program to run even though its source did not translate successfully.

    Why it fails

    A compilation error stops translation, while a runtime error occurs after an interpreted program has started running.

    Do this instead

    First identify whether the problem happened during translation or during execution.

Check Your Mental Model

MEDIUM

A learner says: “Python is interpreted, so the program involved in running Python cannot have been compiled.” Explain what is wrong with this statement. Then describe the roles of C source code, the C compiler, the Python interpreter, and the Python script.

Hints
  • Separate the program that interprets Python from the Python code being interpreted.
  • Ask what language the Python interpreter is written in.
  • Identify which compiler creates the interpreter.
EASY

Compare these two workflows in your own words: giving an entire C source file to a compiler, and giving a Python script to the Python interpreter. Include when translation happens, whether a saved executable is produced, and when execution occurs.

Hints
  • For compilation, start with the complete source file and end with a saved machine-language file.
  • For interpretation, focus on translation occurring while the program runs.

The Complete Execution Picture

  1. 1
    A compiler reads an entire high-level source file, checks it for errors, translates it into machine language, and saves the result as an executable.
  2. 2
    Machine language consists of low-level byte sequences designed for CPU execution, so executable files are not intended to be human-readable.
  3. 3
    Windows commonly identifies executable files with .exe or .dll suffixes, while Linux and macOS use file permissions.
  4. 4
    An interpreter translates code while the program runs instead of first saving the complete translation as an executable.
  5. 5
    The Python interpreter is a compiled C program that reads and interprets Python code.

Key Takeaways

  • Compilation translates an entire program before execution and saves machine language in an executable file.
  • Operating systems use different ways to identify executables: Windows uses suffixes such as .exe and .dll, while Linux and macOS use file permissions.
  • Executable contents are designed for CPU execution, so they are generally unreadable to humans.
  • Interpretation translates statements while a program runs.
  • The Python interpreter is itself a compiled program written in C.

Related concepts

More concepts starting with H