OCR H446, section 1.2.2 (d)
A processor can only run machine code. Programs written in any other language must be translated first. A translator is a program that does this.
Source code and object code
- Source code is the program as the programmer wrote it, in a high-level language or assembly language. People can read it.
- Object code (machine code) is what the translator produces. The processor can run it directly.
The three types of translator
Assembler
An assembler translates assembly language into machine code. There is a one-to-one match between assembly instructions and machine code instructions, so each mnemonic is replaced by its binary opcode, and labels are replaced by addresses.
Compiler
A compiler translates the whole of a high-level program into machine code (or another low-level form) in one go, before it is run. The result is a separate executable file.
- If the source has errors, the compiler gives a list of them and no program is produced.
- The compiled program runs fast, as it is already in machine code.
- The user needs only the executable, so the source code stays private.
- The executable runs only on the type of processor and operating system it was compiled for.
Interpreter
An interpreter translates and runs the program one statement at a time, as it goes. It doesn't produce a separate executable.
- Quick to start and easy to test: change a line and run again at once.
- It stops at the first error, and reports the line, which is good for debugging.
- Slower to run, as translation happens every time the program runs.
- The user needs the source code and the interpreter.
- The same source can run on any computer with an interpreter, which gives portability.
Comparison
| Compiler | Interpreter | Assembler | |
|---|---|---|---|
| Translates | A high-level language | A high-level language | Assembly language |
| How | The whole program first | One statement at a time | The whole program (one-to-one) |
| Output | An executable file | None (runs the program directly) | Machine code |
| Speed of running | Fast | Slower | Fast |
| Error reporting | All errors found before running | Stops at the first error during running | Reports errors in the assembly |
| Needed to run the program | Only the executable | The source and the interpreter | Only the machine code |
| Source code protected? | Yes | No | Yes (after assembly) |
| Good for | Finished software that is distributed | Learning, testing and development, and scripts | Low-level, hardware-specific programs |
Intermediate code (bytecode)
Some languages use both. The compiler translates the source into bytecode, a simple intermediate language that isn't tied to one processor. A virtual machine then interprets the bytecode (or compiles it as it runs). Java and Python work this way (see 1.2.1 (h)).
This gives portability (the same bytecode runs anywhere there is a virtual machine) with better speed than interpreting source directly.
Choosing a translator
| Situation | Translator | Reason |
|---|---|---|
| A game that will be sold to customers | Compiler | It must run fast, and the source code is kept secret |
| A student learning to program | Interpreter | Changes can be tried at once, and errors are shown line by line |
| A program for a tiny device with very little memory, written in assembly | Assembler | Gives exact control over the machine code |
| A web page script that must work on any user's browser | Interpreter (in the browser) | It runs on any type of computer |
Worked example
Task: explain why a company would compile its finished program before selling it, instead of giving customers the source code and an interpreter.
- The compiled version runs faster, because it doesn't need translating each time.
- The company keeps its source code secret, which protects its intellectual property and prevents others copying or altering it.
- Customers don't need to install an interpreter.
- The disadvantage is that the company must compile a separate version for each type of processor and operating system.
Exam tip: compare translators on how they translate (whole or line by line), speed, error reporting, whether an executable is produced and protection of the source code. Always link your choice to a situation.
Check yourself
What is the difference between a compiler and an interpreter?
A compiler translates the whole program into machine code before it is run and produces an executable. An interpreter translates and runs the program one statement at a time and produces no executable.
Which translator is used for assembly language?
An assembler.
Give one advantage of an interpreter over a compiler during development
Changes can be tested immediately without recompiling, and it stops at the first error and shows where it is, which makes debugging easier.
Why do some compilers produce bytecode?
Bytecode is portable: the same bytecode can run on any computer with a suitable virtual machine, so the program doesn't have to be recompiled for each type of processor.