5.2.1 Assemblers, Compilers and Interpreters
A processor executes instructions represented in machine code. Programs written in assembly language or a high-level language therefore need translation before the processor can carry out their instructions. The translator used depends on the language in which the source program was written.
By the end of this section, you should be able to:
- Explain why source code must be translated before processor execution.
- Match assembly language with an assembler.
- Describe how a compiler translates a high-level program.
- Describe how an interpreter translates and executes a high-level program.
- Distinguish source code, object code, executable code and machine code.
- Trace the basic route followed by each translator type.
Why Is Translation Needed?
High-level languages use meaningful identifiers, structured statements and abstractions that help programmers express solutions. Assembly language uses mnemonic opcodes and symbolic addresses. Neither form is the binary instruction pattern that the processor directly fetches and executes.
| Representation | Designed mainly for | Can the processor execute it directly? |
|---|---|---|
| High-level source code | Programmers solving problems with readable constructs | No |
| Assembly-language source code | Programmers working close to the processor instruction set | No |
| Machine code | The processor’s instruction decoder | Yes, when it matches the processor architecture |
Source Code, Machine Code and Translation Output
Translators do not all produce and use code in the same way. An assembler and a compiler normally create translated code that can be stored. An interpreter translates and carries out the source program while the interpreter itself is running.
| Source language | Required translator | Basic outcome |
|---|---|---|
| Assembly language | Assembler | Machine-readable object code corresponding to the assembly program |
| High-level language | Compiler | Object code for the translated program |
| High-level language | Interpreter | Statements are translated and executed as the program runs |
Assembler: Translating Assembly Language
An assembler translates assembly-language instructions into the machine-code instructions used by a particular processor. Mnemonic opcodes are replaced by binary opcodes, and symbolic labels or addresses are resolved into the values needed by the machine-code program.
| Assembly feature | What the assembler must do |
|---|---|
| Mnemonic opcode | Replace it with the corresponding binary operation code. |
| Symbolic label | Determine the address represented by the label. |
| Operand | Encode the value, register or address in the required instruction format. |
| Invalid instruction or unresolved symbol | Report a diagnostic so the source program can be corrected. |
Assembly instructions usually correspond closely to processor instructions, but labels, assembler directives and symbolic notation still need processing. The detailed two-pass assembler procedure belongs to Unit 4.2; this section focuses on why the assembler is needed.
Compiler: Translating a Complete High-Level Program
A compiler analyses a high-level source program and translates it into object code. Compilation occurs before the translated program is run. A successful build can therefore produce code that is stored and used again without running the compiler each time.
| Stage | Compiler activity |
|---|---|
| Read and analyse | Examine the structure and meaning of the source program. |
| Detect translation errors | Report problems such as invalid syntax or incompatible language constructs. |
| Generate translated code | Produce object code when the source has been translated successfully. |
| Pass output to later build stages | Allow object modules and required libraries to be linked into a runnable program. |
Interpreter: Translating While the Program Runs
An interpreter takes a high-level source program, translates the next statement or construct that needs to be executed, and immediately carries out its effect. Translation and execution are therefore combined within the same run.
| Stage | Interpreter activity |
|---|---|
| Obtain the next statement to execute | Follow the program’s control flow rather than merely reading every line in order. |
| Analyse and translate | Check the statement and convert it into a form the interpreter can carry out. |
| Execute | Perform the requested operation immediately. |
| Continue | Move to the next statement selected by sequence, selection or iteration. |
If an error exists in a branch that is never executed during a particular run, the interpreter may not encounter that error during that run.
Comparing the Three Translation Routes
| Feature | Assembler | Compiler | Interpreter |
|---|---|---|---|
| Input | Assembly-language source code | High-level source code | High-level source code |
| Main task | Map mnemonics, labels and operands to processor instructions | Translate the source program before execution | Translate and execute statements during the run |
| Stored translated output | Normally object code | Normally object code | No separate complete executable is required in the basic model |
| Execution | Occurs after assembling and any required linking/loading | Occurs after compiling and any required linking/loading | Occurs as the interpreter processes the source program |
Object Code Is Not Always an Executable
An assembler or compiler can produce an object module. That module may still refer to routines or other modules stored elsewhere. A linker resolves those references and combines the required parts into a form that can be loaded and executed.
| Term | Key distinction |
|---|---|
| Source code | Written by the programmer before translation. |
| Object code | Translated code for one module; external references may remain. |
| Executable code | Linked code prepared to be loaded and run. |
| Machine code | Binary processor instructions; object and executable files may contain machine code. |
The Translator and the Target System
Machine-code instructions depend on the processor architecture. A compiler or assembler must therefore generate code suitable for its target architecture. An interpreter must itself be available for the system on which the source program will run.
| Translator | System relationship |
|---|---|
| Assembler | Understands the assembly language and instruction set of the target processor. |
| Compiler | Produces object code for a specified target architecture or platform. |
| Interpreter | Must be implemented for the platform on which it executes the source program. |
Interactive: Translator Pathway Visualiser
The existing visualiser has been retained. Use the interpreter and compiler modes to review the two high-level-language pathways. The Java mode provides a short preview of mixed translation, which is developed fully in Section 5.2.2.
Common Mistakes and Misconceptions
- Wrong source language: an assembler translates assembly language; compilers and interpreters process high-level languages.
- Source code means high-level code only: assembly source is also source code.
- Compiler executes the program: compilation and execution are separate stages.
- Interpreter translates the whole program first: translation and execution occur together as statements are reached.
- Object code is always a finished executable: linking may still be required.
- Translation creates universal code: machine code is tied to a processor architecture or target platform.
- One source statement always equals one machine instruction: this is more characteristic of assembly than a high-level language, and even assembly contains labels and directives that require processing.
Practice
Try these original questions
- Explain why a processor cannot directly execute a high-level source program.
- State the translator required for assembly language and describe its purpose.
- Describe the basic route from a high-level source program through a compiler to execution.
- Describe how translation and execution are combined by an interpreter.
- Distinguish between source code, object code and executable code.
- A program contains mnemonic opcodes and symbolic labels. Identify the translator required and explain two tasks it performs.
- A translator reports an error only when a particular branch is selected during a run. Identify the translator type and justify your answer.
- Explain why object code produced for one processor architecture may not run directly on a different architecture.
- Correct this statement: “A compiler reads each instruction only when it is executed, while an interpreter creates a permanent executable first.”
- Construct three labelled pathways showing the input, translator and outcome for an assembler, compiler and interpreter.
Review
| Translator | Input | Essential description |
|---|---|---|
| Assembler | Assembly-language source code | Converts mnemonics, operands and symbols into processor-specific object code. |
| Compiler | High-level source code | Translates the source program before execution and produces object code. |
| Interpreter | High-level source code | Translates and executes statements as they are reached during a run. |