The processor only executes machine code, so every other program must be translated first. Learn why we need assemblers, compilers and interpreters, weigh up compiling against interpreting for a given situation, and see how Java is partly compiled and partly interpreted.
A processor can only execute machine code. An assembler translates an assembly language program into machine code. A compiler translates a whole high-level language program into machine code (object code) before it is run, producing an executable file and a list of all errors. An interpreter translates and executes a high-level program one statement at a time, stopping at the first error. Some languages, such as Java, are partially compiled into bytecode and then interpreted by a virtual machine.
Fig. 1: an assembler, a compiler and an interpreter, and Java's compile-then-interpret route
Section 01
The assembler.
Assembly language uses mnemonics that map closely to machine code instructions. In the lab, step through an assembly program in the 9618 instruction set: before a real CPU could run it, an assembler must turn each line into a binary opcode and operand.
Assembly instruction
Meaning (9618 instruction set)
LDD 200
load the contents of address 200 into ACC
ADD #5
add the number 5 to ACC
STO 201
store the contents of ACC at address 201
END
return control to the operating system
Why needed: the CPU cannot execute mnemonics such as LDD; they must be converted into the binary machine code of that processor.
What it does: replaces each mnemonic with its opcode, converts operands (including #, B and & values) to binary, and replaces labels and symbolic addresses with actual addresses.
One to one: usually each assembly instruction becomes one machine code instruction, so the program is still specific to one processor's instruction set.
Examiner trap
An assembler translates assembly language only. Do not say it translates high-level code: that is the job of a compiler or interpreter.
Section 02
Compiler or interpreter?
Both translate high-level language programs, but in very different ways. Learn the comparison point by point, then use it to justify a choice.
Compiler
Interpreter
How it translates
the whole program, before it is run
one statement at a time, executing each before translating the next
Output
an executable file (object code) that can be run again without the compiler
no executable file; the source is translated again every time it is run
Errors
a list of all errors after compiling; no executable is produced until they are fixed
stops at the first error found, which can be fixed and the program run again from there
Execution speed
fast: already translated
slower: each statement is translated as it runs, including every pass of a loop
Distribution
executable can be given to users without the source code, protecting it; users do not need the compiler
users need the source code and the interpreter
Best for
a finished program to distribute and run often
developing and debugging: test parts of a program as they are written
Worked example Justify a choice
A team is writing a stock-control program. During development they test small sections often. When it is finished, it will be sold to many shops. Justify the use of an interpreter and of a compiler at each stage.
During development: an interpreter lets them run a section straight away without compiling the whole program.
It stops at the first error and shows where it is, so errors can be found and corrected one at a time, and partly finished programs can be tested.
For release: a compiler produces an executable file, so the shops do not need the source code or a translator.
The compiled program runs faster, and the source code is not distributed, so it cannot easily be copied or altered.
Answer: Interpreter while developing (quick testing, error by error); compiler for the finished product (executable, faster, source code protected).
Examiner trap
"A compiler is faster" is ambiguous. Say that the compiled program executes faster; compiling a large program itself can take some time.
Stage 1 · Learn
Check what the sim just showed you
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Quick check+10 XP
What does an assembler translate?
Quick check+10 XP
In the lab, which instruction stores the contents of the ACC at an address?
Quick check+10 XP
Which translator produces an executable file?
Quick check+10 XP
What does an interpreter do when it finds an error?
Section 03
Partly compiled, partly interpreted.
Some high-level languages use both methods. Java (console mode) is the example named in the syllabus.
Stage 1, compile: the Java compiler translates the source code into bytecode, an intermediate code that is not the machine code of any real processor. Syntax errors are reported at this stage.
Stage 2, interpret: the Java Virtual Machine (JVM) on the user's computer interprets the bytecode, translating it into the machine code of that particular processor as it runs (often with just-in-time compilation of frequently used parts).
Benefit: the same bytecode runs on any computer with a JVM, so the program is portable ("write once, run anywhere") without distributing the source code.
Benefit: bytecode is already checked and partly translated, so it runs faster than interpreting the original source code.
Your turn
Explain why a Java program compiled on a Windows laptop can run on a Linux server without being recompiled.
The compiler produces bytecode, not machine code for one processor. The Linux server has its own Java Virtual Machine, which interprets the same bytecode into that machine's code.
Stage 2 · Exam
Exam-style questions
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
Finish the four checks above to unlock the exam questions
Exam style+20 XP
Which is a benefit of a compiler over an interpreter?
Exam style+20 XP
Why is an interpreted program usually slower to execute?
Exam style+20 XP
What is produced when a Java program is compiled?
Exam style+20 XP
A student is writing and testing a program one section at a time. Which translator is most suitable and why?
Skill unlocked
Assemblers, compilers and interpreters, translated.
This skill is now recorded on your profile. Keep the chain going.