Study Guides
IB DP Computer Science (DP Computer Science): Translation (HL)
HL study guide to translation in IB DP Computer Science A.1: compilers, interpreters, assemblers, bytecode and just-in-time compilation, with worked examples.
- Subject
- Computer Science
- Level
- IB
- Topic
- A.1 – Computer fundamentals
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to International Baccalaureate IB Diploma Programme Computer Science (DP Computer Science), First assessment 2027. Official subject brief .
Syllabus page (what it covers and how it is assessed): IB Diploma Programme Computer Science.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for IB Diploma Programme Computer Science (DP Computer Science (2014) / DP Computer Science (2027)).
This unit covers A1.4 Translation, the part of A.1 Computer fundamentals that the guide marks HL only. The A.1 Computer fundamentals study guide explains how the CPU fetches and executes machine-code instructions. Translation answers the next question: how does a program written by a person in a high-level language become instructions the CPU can execute?
Scope of this page. The section number and its HL status come from the full DP Computer Science guide (first assessment 2027). Its individual statements were not available when this page was written, so check your school’s copy for the exact outcomes and the depth required.
Why translation is needed
A CPU can only execute machine code: binary instructions from its own instruction set. Writing machine code directly is slow and error-prone, and a program written for one processor family will not run on another.
High-level languages (such as Python and Java, the two languages used in the course) use English-like keywords, variables and structures such as loops and functions. They are easier to write, read and maintain, and are largely independent of the hardware. A translator converts high-level (or assembly) code into a form the machine can execute.
| Level | Example | Translated by |
|---|---|---|
| High-level language | total = price * quantity |
Compiler or interpreter |
| Assembly language | LDA price (mnemonics) |
Assembler |
| Machine code | 10110100 ... |
Executed directly |
The three kinds of translator
Compiler
A compiler translates the whole source program into machine code (an executable or object code) before it is run. The translation happens once; the executable can then be run many times without the compiler.
- Speed: the compiled program runs fast, because no translation happens while it runs.
- Errors: the compiler reports all the syntax errors it finds after analysing the whole program; no executable is produced until they are fixed.
- Distribution: the developer can share the executable without the source code, which protects it from copying.
- Portability: the executable is specific to one processor family and operating system; it must be recompiled for each platform.
Interpreter
An interpreter translates and executes the program one statement at a time, every time the program is run. No separate executable is produced.
- Speed: slower to run, because each statement is translated as it is reached, and statements inside a loop are processed again on every iteration.
- Errors: execution stops at the first error, showing the line, which makes it easy to locate. However, an error in a part of the program that is never reached may go unnoticed.
- Development: a change can be tested immediately without waiting for compilation.
- Distribution: the user needs the interpreter installed, and the source code is usually shared.
Assembler
An assembler translates assembly language into machine code. Each assembly mnemonic usually corresponds to one machine-code instruction, so the translation is almost one-to-one.
Compiler and interpreter compared
| Compiler | Interpreter | |
|---|---|---|
| Translates | Whole program before running | One statement at a time while running |
| Output | Executable file | None (program is run directly) |
| Execution speed | Fast | Slower |
| Error reporting | All errors listed after translation | Stops at the first error encountered |
| Testing during development | Must recompile after changes | Changes can be run immediately |
| Needed to run the program | Only the executable | The interpreter and the source |
| Source code protection | Good: only the executable is shared | Weak: source usually shared |
Bytecode, virtual machines and just-in-time compilation
Modern languages often combine the two approaches.
Bytecode. The source program is compiled into an intermediate code, bytecode, which is not the machine code of any real processor. A virtual machine then runs the bytecode on the actual hardware. Java source code is compiled to bytecode, which runs on the Java Virtual Machine (JVM). Because a JVM exists for many platforms, the same bytecode runs on all of them: “compile once, run on any machine with a JVM”. The standard Python implementation also compiles source code to bytecode behind the scenes before interpreting it.
Just-in-time (JIT) compilation. While the program runs, the virtual machine identifies parts that execute often, such as the body of a busy loop, and compiles those parts into native machine code. Later executions use the fast native code. JIT gives some of the speed of compilation while keeping the portability of bytecode, at the cost of a slower start and extra memory use.
Worked example: why a loop exposes the difference
A program contains a loop that runs 1,000,000 times with four statements in its body.
- Interpreter: each statement in the loop is analysed and translated each time it is reached: 4 × 1,000,000 = 4,000,000 translations during one run.
- Compiler: the four statements are translated once, before the run. During the run, the CPU executes machine code with no translation at all.
- JIT: the virtual machine notices that the loop is “hot” after some iterations and compiles it; the remaining iterations run as native code.
This is why a compute-heavy simulation is usually compiled or JIT-compiled, while a short script that a teacher changes every lesson is comfortably interpreted.
Worked example: an annotated model answer
Question. A games company is developing a new game. Explain why the programmers might use an interpreter during development but distribute the finished game as a compiled program. [4]
During development, programmers change code constantly, and an interpreter lets them run each change immediately without waiting for the whole program to compile [1: development speed]. It also stops at the first error and shows the line, which helps them find bugs [1: debugging]. The finished game must run as fast as possible, and compiled machine code runs faster because no translation happens during play [1: execution speed linked to a game]. Distributing an executable also means players do not need an interpreter and cannot easily see or copy the source code [1: distribution and protection].
Each point names a feature of the translator and connects it to the stage of the project.
A typical compiler’s stages
You may meet the stages a compiler goes through. In outline:
- Lexical analysis: removes spaces and comments and splits the source into tokens (keywords, identifiers, operators).
- Syntax analysis: checks that the tokens follow the grammar of the language; syntax errors are reported here.
- Semantic analysis: checks meaning, such as whether variables are declared and types match.
- Code generation: produces machine code (or bytecode).
- Optimisation: improves the code so that it runs faster or uses less memory.
Check your school’s copy of the guide for whether these stages are required in detail.
Common mistakes
- “An interpreter produces an executable.” It does not; it executes the program directly.
- “Compiled programs have no errors.” Compilation finds syntax errors; logic and runtime errors can still occur.
- “Interpreted code is always slow.” JIT compilation can make interpreted or bytecode languages run close to compiled speed.
- Confusing bytecode with machine code. Bytecode needs a virtual machine to run.
- Mixing up an assembler with a compiler. Assemblers translate assembly language, almost one instruction at a time.
Exam technique for the command words
- Distinguish or Compare: use paired statements about the same feature (“A compiler … ; an interpreter …”).
- Explain a choice: name the translator’s feature, then why it matters in the scenario (development, distribution, speed, portability).
- Evaluate a language or translator for a case study: weigh execution speed, development speed, portability and protection of source code, then conclude.
Practise and revise
Source
International Baccalaureate Organization, Diploma Programme Subject Brief, Sciences: Computer science, first assessment 2027 (© 2024). The complete subject guide is available through the IB Programme Resource Centre.
Related resources
-
Practice questions
IB DP Computer Science (DP Computer Science): Translation (HL) – Practice Questions
Twelve original HL questions on translation for IB DP Computer Science A.1: compilers, interpreters, assemblers, bytecode and JIT, with mark-scheme answers.
Computer Science · International Baccalaureate · IB
Try questions -
Revision notes
IB DP Computer Science (DP Computer Science): Translation (HL) – Revision Notes
Condensed HL notes on translation for IB DP Computer Science A.1: compilers, interpreters, assemblers, bytecode, JIT, exam traps and a self-test.
Computer Science · International Baccalaureate · IB
Revise topic -
Study guides
IB DP Computer Science A.1: Computer Fundamentals
Study guide to A.1 Computer fundamentals in IB DP Computer Science (first assessment 2027): the CPU, the fetch-execute cycle, storage, binary data and the OS.
Computer Science · International Baccalaureate · IB
Read guide
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.