Home / GCSE · Edexcel / Computer Science / Computers

CPX11Programming Languages and Translators

Edexcel 1CP2 · 3.3.2

Why programs must be translated

  • A CPU can only execute machine code: instructions in binary.
  • Every type of processor has its own set of machine code instructions. Machine code written for one type of processor won’t run on a different type.
  • Programmers almost never write machine code. They write in a high-level language (such as Python) or in assembly language.
  • So a program must be translated into machine code before the processor can execute it.
  • The program the programmer writes is the source code. The software that translates it is a translator.
  • There are three translators: an assembler (for assembly language), and a compiler or an interpreter (for high-level languages).

Every route ends in machine code, the only thing the CPU can execute.

Watch outTranslation always goes towards machine code. A translator doesn’t turn machine code into English, and it doesn’t “let the computer understand” in some vague way: it produces (or carries out) machine code instructions.
Machine code
Binary instructions that the CPU executes directly. Each type of processor has its own set.
Source code
A program as the programmer wrote it, in a high-level or assembly language.
Translator
Software that turns source code into machine code: an assembler, compiler or interpreter.
Edexcel 1CP2 · 3.3.1

Low-level languages: machine code and assembly

Machine code and assembly language are the two low-level programming languages.

  • Machine code is written in binary and is executed directly by the CPU. No translation is needed.
  • Assembly language uses mnemonics: short, easy-to-remember codes such as ADD or SEND that stand for machine code instructions.
  • One assembly language instruction maps to one machine code instruction (one CPU operation).
  • Low-level languages are machine specific: code is written for one type of processor (one architecture) and won’t work on others. They aren’t portable.
  • Assembly language must be translated into machine code by an assembler before it can be executed.
  • You don’t need to be able to write machine code or assembly programs for the exam.
Mnemonic (made up)What it does
READ 7Read a value from input port 7
SAVE 40Store the value at memory address 40
ADD 41Add the value at memory address 41
SEND 3Send the result to output port 3
Exam tip“Binary is a low-level language” is wrong: machine code is the language, and it happens to be written in binary. And when you name the language that uses mnemonics, write assembly language (and spell “mnemonics” correctly).
Low-level language
A language close to the hardware: machine code or assembly language.
Assembly language
A low-level language that uses mnemonics; each instruction maps to one machine code instruction.
Mnemonic
A short, easy-to-remember code that represents a machine code instruction.
Edexcel 1CP2 · 3.3.1

What low-level code is good for

  • Machine code lets a programmer directly manipulate hardware features, such as memory locations and the CPU’s registers.
  • The programmer chooses every instruction, so the code can be made to run very quickly and to take up very little memory.
  • That suits embedded systems with limited processing power and memory, such as the control software in a washing machine, a toy or a car’s sensors.
  • It also suits device drivers and code that must control a particular chip or respond instantly (real time).
  • But low-level code is slow to write, hard to read and debug, has no ready-made libraries, and must be rewritten for a different processor.
Exam tipA characteristic is a feature (uses mnemonics; machine specific; one instruction per CPU operation; direct access to memory and registers). A benefit or drawback (“hard to learn”, “runs fast”) answers a different question. Read which one you are asked for.
Machine specific
Written for one type of processor (architecture); it won’t run on a different type.
Embedded system
A computer built into a larger device to do a dedicated task; often programmed in a low-level language.
Edexcel 1CP2 · 3.3.1

High-level languages

Most programs are written in high-level languages, such as Python. They are designed to be easy for people to read and write.

  • They use English-like keywords (if, while, print) and familiar maths symbols, so code is quicker to write, read and debug.
  • They have ready-made subprograms and components in libraries (such as Python’s math and random), so programmers don’t have to write everything themselves.
  • One high-level statement is translated into many machine code instructions.
  • They are portable: the same source code can be translated for different types of processor.
  • The programmer doesn’t need to know how the hardware works, but has less direct control over it.
  • High-level code must always be translated, by a compiler or an interpreter.
High-level languageLow-level language
Looks likeEnglish-like keywordsMnemonics (assembly) or binary (machine code)
Portable?Yes: translate it for each processorNo: machine specific
Libraries of ready-made subprogramsYesNo
Control of hardwareLittle direct controlDirect control of memory and registers
Time to write and debugQuickerSlower
Speed and size of the finished codeUsually larger and a little slowerCan be very fast and small
Worked exampleHow does this Python program show features of a high-level language?
  1. # ------------------------------------------------------------
    # Import libraries
    # ------------------------------------------------------------
    import math
    
    # ------------------------------------------------------------
    # Main program
    # ------------------------------------------------------------
    side = float (input ("Length of one side of the tile in cm: "))
    diagonal = math.sqrt (2) * side
    print ("Diagonal: " + str (round (diagonal, 1)) + " cm")
    A short program in Pearson’s style.
  2. import math and math.sqrt (2)A ready-made subprogram from a library: no need to write a square root routine.
  3. input, print, float, roundEnglish-like built-in subprograms.
  4. diagonal = math.sqrt (2) * sideOne statement; the translator turns it into many machine code instructions.
With an input of 10 it prints Diagonal: 14.1 cm. The same source code runs on any computer that has Python.
Exam tip“Easier to understand” earns little unless you say why: “quicker to write and debug because it uses English-like keywords”, or “less code to write because there are libraries of ready-made subprograms”.
High-level language
A programming language with English-like keywords and ready-made subprograms; portable, and must be translated.
Portable
Able to run on different types of processor once translated for each.
Library
A collection of ready-made subprograms that a program can use.
Edexcel 1CP2 · 3.3.1

Choosing a level for the task

Many products use both: a high-level language for the parts people use, and low-level code where the hardware must be controlled directly.

  • Choose low-level when the code must control specific hardware, run on a chip with very little memory or processing power, or respond as fast as possible.
  • Choose high-level when the program is large, must be written quickly by a team, must run on several types of device, or needs ready-made components such as graphics or networking.
  • Always link your choice to the situation in the question.
TaskSensible levelReason
Code for the chip that reads a kettle’s temperature sensorLow-levelTiny memory and slow chip; direct control of the sensor
A quiz app for phones and tabletsHigh-levelLibraries for the screen and touch; portable across devices
A driver for a new graphics tabletLow-levelMust control that hardware directly and respond instantly
A school’s timetabling programHigh-levelLarge program, quicker to write and maintain
Exam tipIn a 6-mark Discuss, don’t just list “high level is easier, low level is faster”. Give the reason, and apply it to the device or app in the question. Use a paragraph for each part.
Edexcel 1CP2 · 3.3.1

Assemblers

  • An assembler translates assembly language into machine code.
  • It translates each line of assembly language into a single machine code instruction (one-to-one).
  • Each mnemonic is replaced by its binary code (opcode), and each address or value by its binary form.
  • The result is machine code for that one type of processor, which can then be executed.

An assembler turns each line of assembly into exactly one machine code instruction (made-up instruction set).

Exam tipAn assembler doesn’t translate high-level languages. Compilers and interpreters do that.
Assembler
A translator that turns each line of assembly language into one machine code instruction.
Edexcel 1CP2 · 3.3.2

Compilers

  • A compiler translates the whole high-level program in one go into machine code.
  • It produces an executable file, which can be run again and again without the compiler or the source code.
  • Errors are reported at the end of translation, as a list. No executable is made until the errors are fixed.
  • The executable is architecture specific: it only runs on the type of processor it was compiled for. To run on another type, the source code must be compiled again for that processor.
  • After any change to the source code, the program must be recompiled before the change can be run.
  • Compiled programs run quickly, because no translating happens while they run.

A compiler translates everything first; an interpreter translates and runs one line at a time.

Exam tipIf you are asked for characteristics of a compiler, describe what it does (whole program at once, errors at the end, architecture-specific executable). Advantages such as “it runs faster” answer a different question.
Compiler
Translates the whole high-level program in one go into machine code, producing an executable; errors are reported at the end.
Executable
A file of machine code that runs without the translator or the source code.
Edexcel 1CP2 · 3.3.2

Interpreters

  • An interpreter reads and translates the source code one line at a time.
  • Each line is executed immediately after it is translated, before the next line is translated.
  • Errors are reported as soon as they are found, and translation and execution stop at the error. Lines before it have already run.
  • No executable file is produced. The interpreter and the source code are needed every time the program is run.
  • Lines are translated again every time they run (even inside a loop), so interpreted programs run more slowly than compiled ones.
  • The same source code runs on any type of computer that has an interpreter for that language.
Exam tipTo describe how an interpreter translates, say: one line at a time; each line executed straight after it is translated; errors reported when found; stops at an error. Don’t give benefits, drawbacks or comparisons with a compiler unless asked, and don’t say it outputs machine code files.
Did you know?Real Python checks the whole file for syntax errors before it runs any of it, so a missing bracket stops the program before line 1. In the exam, describe interpreters the way the spec does: line by line, stopping at the first error.
Interpreter
Translates and executes high-level code one line at a time, reporting an error and stopping as soon as one is found.
Edexcel 1CP2 · 3.3.2

Compiler or interpreter? Benefits and drawbacks

Programmers often use both: an interpreter while writing and testing, and a compiler for the version users get.

  • During development, an interpreter lets programmers run code straight away and see exactly which line has the error, which makes testing small changes quick.
  • A compiler reports every error it can find in one list, but a large program takes time to recompile after each change.
  • For distribution, a compiler gives users a fast executable with no translator needed, and the company’s source code stays private.
  • But a compiled executable must be built separately for each type of processor; interpreted source code runs on any computer with the interpreter.
CompilerInterpreter
TranslatesWhole program in one goOne line at a time, then runs it
ErrorsListed at the end of translationReported as found; stops at the first
OutputExecutable fileNo executable; runs the source code
Speed when runningFast: already machine codeSlower: translates as it runs
What users needJust the executableThe interpreter and the source code
Source codeKept privateUsers can see and copy it
Different processorsRecompile for each typeRuns anywhere with the interpreter
Exam tipMatch the translator to the stage and the users: “an interpreter while testing, because it stops at the line with the error”; “a compiler for the release, because schools won’t need a translator installed”.