Robust programs · GCSE · OCR J277 2.5.1, AQA 8525 3.4.4, Edexcel 1CP2 3.3.1 · about 20 min
High and low level, machine code and assembly; compilers, interpreters and assemblers.
[1 mark]Why must a Python program be translated before it runs?
[1 mark]Which translator turns assembly language into machine code?
[1 mark]Which translator produces a standalone file that runs without it?
[1 mark]Why is an interpreter useful while developing a program?
[1 mark]Which are features of low-level languages?
Tick every answer that is true.
[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?
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 + 1The hint students can ask for: Keep the register in a variable. Each instruction reads the word and acts on it. The jump instruction is the only one that changes where the program counter goes next, so make sure it does not then get moved on again.
from bugbot import *
connect()
program = ["LOAD 4", "FWD 20", "TURN 90", "BEEP 660", "DEC", "JNZ 1", "HALT"]
pc = 0
r = 0
while True:
op, *args = program[pc].split()
if op == "LOAD":
r = int(args[0])
elif op == "FWD":
forward(60, distance=int(args[0]))
elif op == "TURN":
turn_right(30, angle=int(args[0]))
elif op == "BEEP":
tone(int(args[0]), 0.2)
elif op == "DEC":
r = r - 1
elif op == "JNZ":
if r != 0:
pc = int(args[0])
continue
elif op == "HALT":
break
pc = pc + 1
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.