Teachers' Portal  /  A-Level Computer Science 9618  /  System Software  /  Assemblers, compilers and interpreters
Lesson pack · A-Level Computer Science 9618 · 5.2 · AS Level

Assemblers, compilers and interpreters

Lost in translation? The processor only executes machine code, so every other program must be translated first.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
System Software
Syllabus reference
Cambridge International AS and A Level Computer Science 9618, 5.2
Level
AS Level
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 3 of 4 in System Software; follows Utility software and program libraries; leads into Features of an IDE
Central model
Fig. 1: an assembler, a compiler and an interpreter, and Java's compile-then-interpret route
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • 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.
Key points to leave on the board
  • 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.
The core visual model

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.

Three translators, and Java's two-stage route assembly code Assembler machine code high-level source Compiler object code /executable file high-level source Interpreter each statementexecuted at once Java (console mode) source → compiled to bytecode → interpreted by the Java Virtual Machine
Fig. 1: an assembler, a compiler and an interpreter, and Java's compile-then-interpret route
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen 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 minTeach and check: The assemblerAssembly 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 minTeach 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 minTeach and check: Partly compiled, partly interpretedSome 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 minCheckThe 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 minPlenaryThe 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 minExit and homeworkSet the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

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.

Running the cooperative task

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.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching 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.
Worksheet

Assemblers, compilers and interpreters: worksheet

6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · explain[2 marks]

Explain why an assembly language program needs to be translated by an assembler before it can be executed.

Q2 · explain[4 marks]

Describe how a compiler and an interpreter differ in the way they translate a high-level language program.

Q3 · explain[3 marks]

Give three benefits of using an interpreter while a program is being developed.

Q4 · apply[3 marks]

A software company is about to release a finished game to customers. Justify the use of a compiler for the release.

Q5 · explain[3 marks]

Java programs are partially compiled and partially interpreted. Describe how a Java program is translated and executed.

Q6 · analysis[2 marks]

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
Q1 · 2 marks
  • 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.
Q2 · 4 marks
  • 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.
Q3 · 3 marks
  • 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.
Q4 · 3 marks
  • 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.
Q5 · 3 marks
  • 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.
Q6 · 2 marks
  • 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.
Quiz

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.

Q11 mark

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
Q21 mark

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
Q31 mark

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
Q41 mark

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
Q51 mark

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
Q61 mark

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
Q1: A

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.

Q2: A

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.

Q3: A

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.

Q4: A

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.

Q5: A

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.

Q6: A

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.

Lab and demo

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

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.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonUtility software and program libraries