CS  ·  Computer Systems

Translators

Lesson CS8 of 10 Approx 60 min Compiler, interpreter and assembler
Learning intentions
  • Understand why programs must be translated before the processor can run them
  • Describe the purpose of compilers, interpreters and assemblers
  • Choose a suitable translator for a programming situation and justify the choice
Success criteria
  • I can state that machine code is the binary instruction code executed by the CPU
  • I can compare compiled and interpreted programs using speed, error reporting and distribution
  • I can identify when an assembler is needed
  • I can explain my answer using scenario evidence rather than one-word labels
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.

1. Which option best describes machine code?

2. Why do programmers usually write in high-level languages such as Python or Java?

3. What must happen before source code can be executed by the processor?

Key vocabulary

Source code
Program instructions written by a programmer in a high-level language or assembly language.
Machine code
Binary instructions that can be executed directly by a processor.
Translator
Software that converts program code from one language into another form, usually machine code.
Compiler
Translates the whole source program before execution, often producing object code or an executable file.
Interpreter
Translates and executes source code one instruction at a time while the program runs.
Assembler
Translates assembly language mnemonics into machine code.
Object code
The translated machine-code output produced by a compiler or assembler before linking or execution.
Syntax error
An error caused by breaking the grammar rules of a programming language.

Translators

Why translation is needed

Programmers usually write programs in languages that are practical for humans. Python, Java and similar high-level languages use words, symbols and structures that are easier to read than long strings of binary. The processor, however, does not understand high-level source code directly. A CPU executes machine code: binary instructions designed for that processor. A translator is therefore needed to convert source code into a form the computer can run.

Translation is part of the software development process. It allows a programmer to write clear source code, then have specialist software convert it into lower-level instructions. Different translators are used in different situations. At National 5, the three important types are compiler, interpreter and assembler.

Compilers

A compiler translates the whole source program before the program is executed. If the compiler finds syntax errors, the program will not compile successfully until the errors are fixed. When compilation succeeds, the output may be object code or an executable program that can be run without translating the original source code again each time.

Compiled programs are usually fast to run because the translation work has already happened. They are also convenient for distribution because users can be given an executable file rather than the original source code. This can help protect the source code. A disadvantage is that compilation can take time, and error messages may be produced after the compiler checks the program rather than exactly at the moment an instruction is reached during execution.

Interpreters

An interpreter translates and executes a program one instruction at a time while the program is running. It does not normally create a separate executable file. This is useful during development because the programmer can run code quickly, test small sections and see where an error occurs. Languages commonly used in classrooms, such as Python, are often described as interpreted because pupils write source code and run it through an interpreter.

The main disadvantage is speed. Because translation happens during execution, interpreted programs can run more slowly than compiled programs. The interpreter program must also be available on the computer that runs the source code. This makes interpreters excellent for learning, experimenting and debugging, but not always the best choice where maximum execution speed or easy distribution is the main requirement.

Assemblers

Assembly language is a low-level language that uses short mnemonic codes such as LDA, ADD or STA instead of pure binary. These mnemonics are easier for humans to read than machine code, but they are still closely linked to the processor's instruction set. An assembler translates assembly language into machine code.

Assembly language is used when a programmer needs very close control of hardware or processor instructions. It is less portable and harder to write than a high-level language, but it can be useful in embedded systems, device drivers and performance-critical routines. In an exam, if the source code is assembly language, the correct translator is an assembler.

Choosing the right translator

Exam questions often ask for a translator to match a scenario. A development situation with frequent testing and debugging may point towards an interpreter. A finished application that should run quickly and be distributed to users may point towards a compiler. Assembly language always points towards an assembler. Strong answers explain the reason, not just the name. For example: "A compiler is suitable because the finished game should run quickly and users do not need to see the source code."

TranslatorInputHow it worksUseful when...
CompilerHigh-level source codeTranslates the whole program before executionA finished program needs fast execution or distribution
InterpreterHigh-level source codeTranslates and executes one instruction at a timeA program is being tested, debugged or developed interactively
AssemblerAssembly languageTranslates assembly mnemonics into machine codeLow-level processor instructions or hardware control are needed

Worked examples

Example 1 — Recommending a compiler

A company has finished a desktop game. The game should run quickly and customers should not need the source code. Which translator is suitable?

1
Look for scenario clues: finished, run quickly and customers.
2
A compiler translates the whole program before it runs and can produce an executable file.
3
A strong answer is: Use a compiler because the finished game can run quickly and be distributed as an executable without giving customers the source code.
Example 2 — Recommending an interpreter

A programmer is learning Python and wants to test small changes often while fixing syntax errors.

1
The clues are learning, test small changes and fixing errors.
2
An interpreter translates and executes code one instruction at a time while the program runs.
3
A strong answer is: Use an interpreter because it supports quick testing during development and can help locate errors as the code is executed.
Example 3 — Identifying an assembler

A program is written using processor mnemonics such as LDA, ADD and STA. Which translator is required?

1
The clue is the use of processor mnemonics.
2
Mnemonic instructions are assembly language, not a high-level language.
3
The required translator is an assembler, because it translates assembly language into machine code.
Now you try

A small team is building a program. During development they want to run the program after every few lines of code. Later, they want to release the final version to users so it runs quickly and users do not need the source code.

Answer the following:

  1. Which translator is most useful during development?
  2. Which translator is most useful for the final released version?
  3. Give one reason for each choice.
  1. An interpreter is useful during development.
  2. A compiler is useful for the final released version.
  3. The interpreter helps with quick testing and debugging. The compiler can produce a faster executable and means users do not need the source code.
Common mistakes
Mixing up compiler and interpreter. Compiler: whole program before execution. Interpreter: one instruction at a time while running.
Saying a translator turns code into "English". The final target for the processor is machine code, not ordinary English.
Using assembler for every low-level task. An assembler is specifically for assembly language. High-level languages need a compiler or interpreter.
Giving a one-word recommendation. In an explain or justify question, connect the translator to the scenario, such as speed, debugging or distribution.
Exam tip

For translator questions, underline the clue words. Whole program, executable, fast running, protect source code usually points to a compiler. Line by line, testing, debugging, immediate feedback usually points to an interpreter. Assembly language or mnemonics points to an assembler. A two-mark answer normally needs the translator and a reason linked to the scenario.

Task Set

Questions 1-5 are auto-checked. Questions 6-9 are self-marked — write your answer, then reveal the model answer to check your work.

1. Which translator converts the whole source program before it is executed? TYPE 1

2. Which translator is most closely linked to line-by-line execution? TYPE 1

3. Which translator is required for assembly language? TYPE 1

4. Which is a likely advantage of a compiled program? TYPE 1

5. Which situation best matches an interpreter? TYPE 1

6. Describe two differences between a compiler and an interpreter. TYPE 2

A compiler translates the whole source program before execution, while an interpreter translates and executes one instruction at a time while the program runs. A compiler can produce object code or an executable file and the final program is usually faster to run. An interpreter is often useful for testing and debugging because code can be run and checked during development.

7. Explain why a programmer might prefer an interpreter while developing a new program. TYPE 2

An interpreter is useful during development because it lets the programmer run and test code quickly without compiling the whole program first. It translates and executes instructions as the program runs, so errors can be found while testing small sections of code.

8. A company wants to distribute a finished program to many users. The program should run quickly and users should not need access to the source code. Recommend a translator and justify your answer. TYPE 3

A compiler would be suitable. It translates the whole program before execution and can produce an executable file for users. The translated program is usually faster to run, and users do not need the original source code to execute it.

9. A developer writes a short routine using assembly language to control hardware directly. Identify the translator required and explain its purpose. TYPE 3

An assembler is required. Assembly language uses mnemonic instructions that are close to the processor's machine code. The assembler translates these assembly instructions into machine code so the processor can execute them.
Teacher notes — Shift+T to hide

Suggested timing: 60 minutes. Warm up 6 min; vocabulary 6 min; notes 20 min; worked examples 10 min; now you try 5 min; task set 13 min.

Key misconception to address: Pupils often memorise one-word definitions but struggle to match the translator to a scenario. Keep asking: is the clue speed/distribution, debugging/testing, or assembly language?

Live demo suggestion: Show a tiny Python syntax error and run it to discuss interpreter feedback, then contrast this with the idea of compiling a finished program into an executable.

Extension question: Some languages use more than one translation stage, such as compiling to bytecode and then running on a virtual machine. Why might that improve portability?

SQA command words covered: "state", "identify", "describe", "explain", "recommend", "justify".