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.

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

A compiled program's journey

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

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

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

Output: The compiler produces a new file containing machine language.

Execute: The operating system can run the resulting executable file directly.

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.

human-readable inputdirect executionSource codehigh-level textExecutable filemachine languageHuman readercan read the programCPUexecutes instructions
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.

identifies runnable filesidentifies runnable filesidentifies runnable filesWindows.exe or .dll suffixMachine languageCPU-executable instructionsLinuxfile permissionsmacOSfile permissions
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

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.

provide entire filesave translated resultprovide statementstranslate and executeSource programentire filePython programstatementsCompilercomplete translationPython interpretertranslates while runningSaved executablerun laterRunning programexecution proceeds
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

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.

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

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

  • Assuming every executable must end with .exe.

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

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

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

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

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

  • Thinking that Python is completely independent of compilation.

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

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

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

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

    Fix: 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. 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. Machine language consists of low-level byte sequences designed for CPU execution, so executable files are not intended to be human-readable.
  3. Windows commonly identifies executable files with .exe or .dll suffixes, while Linux and macOS use file permissions.
  4. An interpreter translates code while the program runs instead of first saving the complete translation as an executable.
  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.