The answersDownload the PDF
Worksheet

A10.7 Programming languages and translators

Operating systems, software and translators · A level · OCR H446 1.2.2, AQA 7517 4.6.2.1, Eduqas A500QS 1.8 · about 45 min

BugBotLab
NameClassDate

What this lesson is about

Machine code, assembly and imperative high-level languages, the Little Man Computer, and choosing an assembler, compiler, interpreter or bytecode.

Questions 6 marks in all

  1. [1 mark]Which describes assembly language?

    1. AA low-level language of mnemonics, where each instruction translates to one machine code instruction
    2. BA high-level language translated by a compiler
    3. CBinary codes executed directly by the processor
    4. DA language that runs on any processor without translation
  2. [1 mark]Which are advantages of writing in assembly language rather than a high-level language?

    Tick every answer that is true.

    1. ADirect control of the processor's registers and memory
    2. BThe code can be hand-tuned to be very small and fast
    3. CThe program can run on any processor
    4. DThe code is quicker to write and easier to read
  3. [1 mark]In the Little Man Computer, what does the instruction BRZ 12 do?

    1. ABranches to address 12 if the accumulator is zero
    2. BBranches to address 12 always
    3. CStores zero in address 12
    4. DBranches to address 12 if the accumulator is positive or zero
  4. [1 mark]Why do most assemblers make two passes over the source code?

    1. AA label can be used before the line that defines it, so all addresses are found first
    2. BThe first pass removes comments and the second checks spelling
    3. CEach pass translates half of the program
    4. DThe second pass optimises the code the first pass produced
  5. [1 mark]A company is releasing a game to the public and does not want players to see how it works. Which translator should it use, and why?

    1. AA compiler, because it produces standalone object code, so the source code is not distributed
    2. BAn interpreter, because it stops at the first error
    3. CAn assembler, because games are written in assembly
    4. DAn interpreter, because it runs faster
  6. [1 mark]Why do some compilers produce bytecode rather than machine code?

    1. AThe bytecode can run on any computer with the right virtual machine, so the program is portable
    2. BBytecode runs faster than machine code
    3. CBytecode does not need to be translated or interpreted
    4. DBytecode is easier for people to read than source code

The task: an LMC assembler

Write a two-pass assembler for the LMC program in source, a list of strings, one line per address starting at address 0. Each line is one of: a mnemonic alone ("OUT"); a mnemonic and an operand ("BRZ end"); a label and a mnemonic ("end HLT"); or a label, a mnemonic and an operand ("one DAT 1"). A word is a label if it is not a key in OPCODES. An operand is either a label or a whole number. - Pass 1: for each line that has a label, store the label and its address in a dictionary, and print <label> = <address>. - Pass 2: for each line, the machine code is the opcode from OPCODES plus the operand's value (the label's address, or the number, or 0 if there is no operand). Print <address>: <code>, with the code as three digits, so 0 prints as 000 and 1 as 001. The robot stays still.

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

OPCODES = {"ADD": 100, "SUB": 200, "STA": 300, "LDA": 500, "BRA": 600, "BRZ": 700,
           "BRP": 800, "INP": 901, "OUT": 902, "HLT": 0, "DAT": 0}

source = [
    "INP",
    "STA count",
    "loop LDA count",
    "OUT",
    "BRZ end",
    "SUB one",
    "STA count",
    "BRA loop",
    "end HLT",
    "count DAT",
    "one DAT 1",
]

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

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

Challenges

  1. What does the source program do? Put your machine code into the Little Man Computer above, with an input of 3, and check.
  2. Make your assembler report an error, instead of crashing, for an unknown mnemonic or a label that is never defined.
  3. Write an LMC program that inputs two numbers and outputs the larger, using SUB and BRP, and assemble it.