A-Level Computer Science / Unit 5: System Software and Program Translation

5.2.1 Assemblers, Compilers and Interpreters

🔒 Lesson slides are available to signed-in users. Sign in

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.
Language translator: system software that converts instructions written in one programming language or representation into another form that can be processed or executed by a computer.

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
Exam tip: Do not write only that translation makes code “understandable to the computer”. State that the processor executes machine-code instructions, while the source program is written in another language.

Source Code, Machine Code and Translation Output

Source code: the instructions written by a programmer before translation. Source code may be written in a high-level language or in assembly language.
Machine code: binary-coded processor instructions that a particular processor architecture can execute directly.

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
Common mistake: “Source code” does not mean only high-level code. Assembly-language programs are also source code before they are assembled.

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.

Assembler: translation software that converts an assembly-language source program into machine-readable object code.
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.

Common mistake: An assembler does not translate a high-level language. Its input is assembly-language source code.

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.

Compiler: translation software that processes a high-level source program and produces object code for the target system.
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.
Common misconception: A compiler translates the program; it does not automatically mean the object code is already a complete executable. Linking may still be required.
Exam tip: Keep translation and execution separate. A compiler processes the source program before the resulting program is executed.

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.

Interpreter: software that translates and executes a high-level source program during execution, rather than first producing a separate complete machine-code program.
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.

Common mistake: “An interpreter translates one line at a time” is only a simplified description. Selection and loops mean that execution follows the program’s control flow, so some statements may repeat or may not be reached.

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
Exam tip: First identify the source language. Assembly language requires an assembler; a high-level program may be compiled or interpreted.

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.

Object code: translated machine-readable code produced for a source module, which may still require linking.
Executable code: a complete linked program in a form that the operating system can load for execution on its intended platform.
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.
Common mistake: Object code and executable code are often related but should not automatically be treated as identical.

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.
Common misconception: Translation does not automatically make a program usable on every processor. The translated code or interpreter must match the target system.

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.

1 Source code

High-level program file is available.

2 Interpreter

Translates and carries out statements as the program runs.

3 Execution

The selected statement is executed if it can be translated successfully.

Main role

Translate and execute a high-level program during a run.

Error handling

Errors are reported when the affected statement is reached.

What is needed

The interpreter and source program are required for execution.

Translation result

No separate complete executable is produced in the basic model.

Step 1 of 4

Start with source code

The programmer has written a high-level language program.

Assembler pathway: assembly source → assembler → object code → linking/loading where required → processor execution.

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

  1. Explain why a processor cannot directly execute a high-level source program.
  2. State the translator required for assembly language and describe its purpose.
  3. Describe the basic route from a high-level source program through a compiler to execution.
  4. Describe how translation and execution are combined by an interpreter.
  5. Distinguish between source code, object code and executable code.
  6. A program contains mnemonic opcodes and symbolic labels. Identify the translator required and explain two tasks it performs.
  7. A translator reports an error only when a particular branch is selected during a run. Identify the translator type and justify your answer.
  8. Explain why object code produced for one processor architecture may not run directly on a different architecture.
  9. Correct this statement: “A compiler reads each instruction only when it is executed, while an interpreter creates a permanent executable first.”
  10. Construct three labelled pathways showing the input, translator and outcome for an assembler, compiler and interpreter.
Exam tip: Use the source language as your first clue. Then explain whether translation happens before execution or during execution.

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.
Final check: Can you explain all three translators without discussing which one is “better”? Benefits, drawbacks and justified choices are the focus of Section 5.2.2.