The shape of the lesson
By the end of the lesson, learners can
- The assembler: Assembly language uses mnemonics that map closely to machine code instructions.
- Compiler or interpreter?: Both translate high-level language programs, but in very different ways.
- Partly compiled, partly interpreted: Some high-level languages use both methods.
- Assembler: assembly language to machine code, usually one instruction to one.
- Compiler: whole program before running; executable file; error list; fast execution; source code kept private.
- Interpreter: statement by statement while running; stops at first error; good for development; slower.
- Java: compiled to bytecode, interpreted by the JVM: portable.
One picture every idea returns to
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.
Forty-five minutes, phase by phase
| Time | Phase | What happens in the room | Grouping |
|---|---|---|---|
| 0 to 5 min | Hook: predict | Open with the question in the title, Lost in translation?. The processor only executes machine code, so every other program must be translated first. Put the figure on the board: it is the one picture every idea in the lesson returns to. | Whole class, then pairs |
| 5 to 13 min | Teach and check: The assembler | Assembly language uses mnemonics that map closely to machine code instructions. Project tab 5 of the lab (Assembly language with the 9618 instruction set) and let a student drive it while the class predicts. | Whole class, then pairs |
| 13 to 21 min | Teach and check: Compiler or interpreter? | Both translate high-level language programs, but in very different ways. Model the worked example Justify a choice on the board, one line at a time. | Pairs |
| 21 to 30 min | Teach and check: Partly compiled, partly interpreted | Some high-level languages use both methods. Your turn: Explain why a Java program compiled on a Windows laptop can run on a Linux server without being recompiled. | Corners |
| 30 to 36 min | Check | The four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong. | Individual, then whole class |
| 36 to 43 min | Plenary | The exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording. | Individual, random call |
| 43 to 45 min | Exit and homework | Set the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave. | Individual |
Protect the parts that carry the learning
The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.
Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.
Cut first if the build overruns: the last check in Partly compiled, partly interpreted, never the check or the plenary.
In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.
Corners
Put one answer or viewpoint in each corner of the room. Students move to the corner they agree with, justify the choice to a partner there, then hear one voice from each corner before the reveal.
The full structure, with its variations and the other twelve, is on the cooperative-learning page.
What to head off, and how
| Trap learners fall into | Teaching move that pre-empts it |
|---|---|
| An assembler translates assembly language only. | Do not say it translates high-level code: that is the job of a compiler or interpreter. |
| "A compiler is faster" is ambiguous. | Say that the compiled program executes faster; compiling a large program itself can take some time. |
Assemblers, compilers and interpreters: worksheet
6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.
Explain why an assembly language program needs to be translated by an assembler before it can be executed.
Describe how a compiler and an interpreter differ in the way they translate a high-level language program.
Give three benefits of using an interpreter while a program is being developed.
A software company is about to release a finished game to customers. Justify the use of a compiler for the release.
Java programs are partially compiled and partially interpreted. Describe how a Java program is translated and executed.
A loop body of 3 statements is executed 1000 times. Explain why this program would run much faster compiled than interpreted.
Answer key and marking notes
- The processor can only execute machine code (binary instructions).
- The assembler converts each mnemonic (and its operand / symbolic address) into the equivalent machine code instruction.
- A compiler translates the whole program before it is executed; an interpreter translates and executes one statement at a time.
- A compiler produces an executable file (object code); an interpreter does not, so the program is translated each time it is run.
- A compiler reports all errors at the end of compilation; an interpreter stops at the first error.
- Compiled code executes faster than interpreted code.
- The program can be run without translating the whole of it first, so it can be tested as it is written.
- Execution stops at the line with the error, so errors are easy to locate.
- The error can be corrected and the program continued / re-run immediately; no need to recompile the whole program.
- The compiler produces an executable file, so customers do not need a translator to run the game.
- The source code is not distributed, so it is protected from being copied or modified.
- The compiled game executes faster, which matters for a game.
- The source code is compiled into bytecode (intermediate code).
- The bytecode is given to / run on the Java Virtual Machine (JVM) on the target computer.
- The JVM interprets the bytecode into the machine code of that computer as it executes, so the same bytecode can run on different platforms.
- A compiler translates the 3 statements once, before the program runs.
- An interpreter translates the statements every time they are executed, so 3 × 1000 = 3000 translations are carried out during execution.
6 multiple-choice questions on this lesson
From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.
Why must a program written in assembly language be translated before it can be executed?
- AThe processor can only execute machine code, so each mnemonic must be converted into a binary instruction
- BAssembly language is a high-level language, so it must first be compiled into bytecode
- CThe operating system cannot store mnemonics, so they must be converted before the file is saved
- DAssembly language instructions can only be run inside a virtual machine
Which is a feature of a compiler but not of an interpreter?
- AIt produces an executable file that can be run again without the translator
- BIt translates and executes one statement at a time
- CIt stops at the first error it finds in the program
- DIt translates a statement in a loop again on every pass
A student is writing a program and wants to run each new section as soon as it is typed, fixing errors one at a time. Which translator is most suitable?
- AAn interpreter, because it runs the code straight away and stops at the line with the error
- BA compiler, because it reports every error at once before the program can run
- CAn assembler, because it translates each statement as soon as it is typed
- DA compiler, because the executable shows where each error is when it runs
What happens when a Java program is run on a user's computer?
- AThe Java Virtual Machine interprets the bytecode into the machine code of that computer
- BThe Java compiler translates the source code into machine code for that computer each time
- CAn assembler converts the bytecode into assembly language and then into machine code
- DThe processor executes the bytecode directly, because bytecode is a kind of machine code
A loop body contains 4 statements and is executed 250 times. How many times are the loop body statements translated by an interpreter, and by a compiler?
- AInterpreter 1000; compiler 4
- BInterpreter 4; compiler 1000
- CInterpreter 1000; compiler 1000
- DInterpreter 250; compiler 1
A company compiles a Java program once and wants customers with Windows, macOS and Linux computers to run it. Which statement explains why this works?
- AThe bytecode is not tied to any one processor, and each computer's own JVM translates it into that machine's code
- BThe compiler produces three executable files, one for each operating system, in a single run
- CThe source code is sent to each customer and compiled on their computer the first time it runs
- DEvery computer has the same instruction set, so a single executable runs on all of them
Quiz key
A CPU decodes binary machine code only, so an assembler replaces mnemonics such as LDD with opcodes and converts operands and labels to binary addresses. Assembly is a low-level language, not high-level, and it is not compiled to bytecode. Mnemonics can be stored in a text file like any source code. Assembly programs run directly on the processor once assembled, not in a virtual machine.
A compiler translates the whole program into object code, giving an executable that runs without the compiler. Translating and executing statement by statement, stopping at the first error and re-translating loop statements on each pass are all features of an interpreter.
An interpreter executes code without translating the whole program first, and stops at the first error, which suits testing as you go. A compiler produces no executable until all errors are fixed, which is slower for partly written code. An assembler only translates assembly language. A compiled executable is only produced once there are no syntax errors, so it does not show them.
Java source is compiled once into bytecode; on each computer the JVM interprets that bytecode into the local machine code as it runs. The source code is not recompiled on the user's computer. An assembler is not involved. Bytecode is not the machine code of any real processor, so the CPU cannot execute it directly.
An interpreter translates each statement every time it is executed: 4 × 250 = 1000 translations. A compiler translates each of the 4 statements once, before execution. Swapping the figures reverses the two translators. A compiler does not translate on every pass. 250 and 1 count passes and loops, not statements.
Bytecode is an intermediate code; a JVM on each platform interprets it into the local machine code, so one compilation serves every platform with a JVM. The compiler outputs bytecode, not three native executables. The source code does not need to be distributed. Different processors have different instruction sets, which is exactly why native executables are not portable.
What to project
The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.
Slides for this lesson
A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.