The answersDownload the PDF
Worksheet

F6.5 Languages and translators

Robust programs · GCSE · OCR J277 2.5.1, AQA 8525 3.4.4, Edexcel 1CP2 3.3.1 · about 20 min

BugBotLab
NameClassDate

What this lesson is about

High and low level, machine code and assembly; compilers, interpreters and assemblers.

Questions 6 marks in all

  1. [1 mark]Why must a Python program be translated before it runs?

    1. AA processor only understands its own machine code
    2. BPython is too slow
    3. CTo remove comments
    4. DTo check spelling
  2. [1 mark]Which translator turns assembly language into machine code?

    1. AAn assembler
    2. BA compiler
    3. CAn interpreter
    4. DAn IDE
  3. [1 mark]Which translator produces a standalone file that runs without it?

    1. AA compiler
    2. BAn interpreter
    3. CAn editor
    4. DA debugger
  4. [1 mark]Why is an interpreter useful while developing a program?

    1. AIt stops at the first error, on the line where it happened
    2. BIt makes the finished program run fastest
    3. CIt produces an executable file
    4. DIt hides the source code
  5. [1 mark]Which are features of low-level languages?

    Tick every answer that is true.

    1. AOne instruction for each processor instruction
    2. BWritten for a particular kind of processor
    3. CClose control of memory and hardware
    4. DEasy to read and move between computers
  6. [1 mark]The robot assembly program is LOAD 4, FWD 20, TURN 90, BEEP 660, DEC, JNZ 1, HALT. How many beeps does it play?

The task: robot assembly

Finish the interpreter so it runs the robot assembly program below, which uses LOAD, DEC and JNZ to drive a square with a beep at each corner. Store the value of R in a variable, and make JNZ change the program counter.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

program = ["LOAD 4", "FWD 20", "TURN 90", "BEEP 660", "DEC", "JNZ 1", "HALT"]

pc = 0
while True:
    op, *args = program[pc].split()
    if op == "FWD":
        forward(60, distance=int(args[0]))
    elif op == "TURN":
        turn_right(30, angle=int(args[0]))
    elif op == "HALT":
        break
    pc = pc + 1

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/f6-5-languages-and-translators/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Add a WAIT n instruction, and write an assembly program that flashes a beep three times.
  2. Add JMP a, an unconditional jump. What happens if a program jumps back with no way to stop?
  3. Write your own compiler of sorts: a function that turns the assembly program into a Python program as text, and print the text.