Three translator types
Program Translators
All source code written in high-level or assembly languages must be converted into machine code before a CPU can execute it. This conversion is performed by a program called a translator. There are three types of translator used in computing: the assembler, the compiler, and the interpreter. Each works differently, produces a different outcome, and suits different situations.
Assembler and compiler both produce machine code directly. The interpreter does not produce a machine code file.
Each Translator in Detail
Assembler
An assembler translates assembly language source code into machine code. Because assembly language has a 1:1 correspondence with machine code, each assembly mnemonic (e.g. ADD, MOV, JMP) is assembled into exactly one binary machine code instruction. The assembler processes the entire source file and produces machine code that can then be loaded and executed directly by the CPU.
Key characteristics:
- Input: assembly language source code (mnemonics)
- Output: machine code directly - one instruction for each mnemonic
- Translation: one-to-one; no combining or abstracting of instructions
- Result: a machine code program specific to the target processor
When to use an assembler: when writing software for a specific, known processor where maximum hardware control is needed - such as embedded system firmware, device drivers, or operating system kernels. The assembled machine code is very efficient and gives precise control over the hardware.
Compiler
A compiler translates an entire high-level language program into machine code in a single translation step. The compiler reads the complete source file, checks it for errors, and produces an executable file containing machine code. Once compiled, the executable can be run directly by the CPU without the original source code or the compiler being present. A single line of high-level source code typically compiles into many machine code instructions.
Key characteristics:
- Input: complete high-level source program
- Output: a standalone executable file (machine code saved to disk)
- Translation: the whole program is translated once before execution
- Errors: detected during compilation - the program will not run until all errors are resolved
- Result: the executable runs at full speed with no translation overhead
When to use a compiler: when developing software that will be distributed and run many times on the same platform - such as desktop applications, games, and operating system components. Compilation is slower to set up but the resulting program runs very efficiently.
Interpreter
An interpreter translates and executes high-level source code line by line, each time the program runs. Crucially, an interpreter does not generate machine code directly. Instead, for each statement it encounters, it calls appropriate machine code subroutines that already exist within its own code to carry out the required operation. No standalone executable file is produced - the original source code and the interpreter must be present every time the program is run.
Key characteristics:
- Input: high-level source code (one statement at a time)
- Output: no machine code file produced - execution happens directly via built-in subroutines
- Translation: happens at runtime, line by line, every time the program runs
- Errors: detected during execution - a program runs until it hits an error, then stops
- Result: slower execution than compiled code (translation overhead every run)
The interpreter calls its own built-in machine code routines - it never writes machine code to a file.
When to use an interpreter: during development and testing (errors are shown immediately for the specific line that fails, making debugging faster), for scripting and automation tasks, and for languages like Python where code needs to run cross-platform without a compilation step.
Summary: Key Differences
| Feature | Assembler | Compiler | Interpreter |
|---|---|---|---|
| Input language | Assembly | High-level | High-level |
| Produces machine code? | Yes - directly | Yes - as a saved file | No - calls own MC subroutines |
| Translation timing | Whole program, before run | Whole program, before run | Line by line, at runtime |
| Executable file produced? | Yes | Yes | No |
| Source code needed to run? | No (after assembly) | No (after compilation) | Yes (every run) |
| Error detection | Before execution | Before execution | At point of error during execution |
| Execution speed | Very fast | Fast | Slower (overhead every run) |
Key Takeaways
- The three translator types are assembler, compiler, and interpreter. Each converts source code for the CPU, but in different ways.
- Assemblers and compilers both produce machine code directly. An assembler outputs one machine code instruction per assembly mnemonic; a compiler translates an entire high-level program into a standalone executable file.
- An interpreter does not produce machine code. It calls machine code subroutines within its own code to execute each source statement line by line at runtime.
- Compilers and assemblers produce an executable that can run without the original source code. Interpreted programs need the source code and interpreter every time they run.
- Use an assembler for embedded/hardware-specific code; a compiler for distributed applications needing fast execution; an interpreter for development, scripting, and cross-platform flexibility.