Syllabus coverage
- Explain what is meant by a high-level language and a low-level language, including the advantages and disadvantages of each
- Understand the advantages and disadvantages in terms of ease of reading and writing code, ease of debugging, machine independence and direct manipulation of the hardware
- Understand that assembly language is a form of low-level language that uses mnemonics, and that an assembler is needed to translate an assembly language program into machine code
- Describe the operation of a compiler and an interpreter, including how high-level language is translated by each and how errors are reported
- Explain the advantages and disadvantages of a compiler and an interpreter
- Explain the role of an IDE in writing program code and the common functions IDEs provide
Part of the O Level Computer Science syllabus 2210.
High-level languages
A high-level language is written in a form close to English, using meaningful names and structures such as loops, conditions and procedures. One statement may do the work of many machine code instructions, and the programmer does not have to think about which register holds which value or where in memory a variable lives.
The advantages follow from that distance from the hardware. High-level code is easy to read and write, because it reflects the problem being solved rather than the design of the processor. It is easy to debug, because the code is easier to follow and the translator can report an error together with the line it is on. It is machine independent: the same source code can be translated to run on different types of processor, because it does not refer to any particular processor's instructions.
The disadvantage is the mirror image. The programmer has no direct manipulation of the hardware, so a specific processor feature cannot be used, and the translated code may not make the most efficient use of memory or processing time. High-level languages are the normal choice for applications, websites and most software written today.
Low-level languages
A low-level language is written in a form close to the hardware. It uses the processor's own instructions and memory addresses, so the programmer works at the level of the machine rather than the level of the problem. Assembly language, covered below, is the readable form of low-level language, and machine code is the binary form the processor actually executes.
The advantage is direct manipulation of the hardware. The programmer can choose exactly which instruction is used, which register holds a value and which memory address is read, so code can be written to run very fast or to fit into a very small amount of memory. That is why low-level languages survive in device drivers, in the routines of an operating system where speed and control matter, and in embedded systems with tiny amounts of memory.
The disadvantages are that low-level code is hard to read and write, because it looks nothing like the problem, and hard to debug, because mistakes are easy to make and difficult to trace. It is also machine dependent: a program written for one type of processor will not run on another, so it has to be rewritten for each family of processor.
Comparing high-level and low-level languages
The four headings the syllabus names are ease of reading and writing code, ease of debugging, machine independence and direct manipulation of the hardware. Learn one sentence for each heading and you can answer any version of the question.
Notice how the advantages and disadvantages are two sides of the same property. A high-level language is easy to read because it is far from the hardware, and that same distance is why it cannot control the hardware directly. A low-level language gives control because it is close to the hardware, and that closeness is why it is hard to read.
| High-level language | Low-level language | |
|---|---|---|
| Reading and writing code | Easy: close to English, uses meaningful names | Hard: close to the hardware, uses mnemonics or binary |
| Debugging | Easier: code is readable and errors can be reported with a line number | Harder: mistakes are easy to make and difficult to trace |
| Machine independence | Machine independent: can be translated to run on different processors | Machine dependent: written for one type of processor |
| Direct manipulation of the hardware | Not possible: the programmer cannot use specific processor instructions | Possible: the programmer works directly with the processor's instructions, registers and addresses |
| Where you meet it | Applications, websites, most software written today | Device drivers, operating system routines, embedded systems where speed or size matters |
Machine code, assembly language and mnemonics
The processor executes only machine code, which is binary instructions made up of an opcode saying what to do and an operand saying what to do it to. Writing in binary is slow, tiring and full of errors, and a single wrong bit changes the instruction completely.
Assembly language was created to make low-level programming bearable. It is still a low-level language, but it replaces binary opcodes with mnemonics: short letter codes such as `LDA` for load, `ADD` for add and `STO` for store. It also replaces memory addresses with names, so the programmer can write `LDA total` instead of a binary address.
Each mnemonic corresponds to exactly one machine code instruction. There is no one-to-many translation and no hidden work: what the programmer writes is what the processor runs, instruction for instruction. That is why assembly language still counts as low-level, even though it contains letters rather than only 1s and 0s. The programmer still has to think about registers, memory addresses and the order in which the processor does things.
| Assembly language | Meaning | Machine code equivalent |
|---|---|---|
| LDA total | Load the value at 'total' into the accumulator | One binary instruction with the load opcode |
| ADD price | Add the value at 'price' to the accumulator | One binary instruction with the add opcode |
| STO total | Store the accumulator back into 'total' | One binary instruction with the store opcode |
The assembler
The processor does not understand mnemonics, so an assembly language program cannot run as it is written. An assembler is the translator that converts it into machine code, replacing each mnemonic with its binary opcode and each named address with the actual address.
The translation is close to one assembly language instruction for one machine code instruction, so an assembler is much simpler than a compiler. It does not restructure the program or optimise it; it substitutes codes and addresses and produces a machine code program the processor can execute.
The three translators are easy to keep apart if you match each to its source: an assembler translates assembly language into machine code, a compiler translates a high-level language into machine code, and an interpreter translates a high-level language line by line as it runs.
How a compiler works
A compiler translates the whole program at once, before any of it is executed, and produces an executable file. The programmer runs the compiler, gets the executable, and can then run that executable as many times as they like without the compiler being involved again.
If the compiler finds errors it produces an error report for the whole program, listing the errors it has found and the line each one is on. No executable file is produced until those errors are fixed. Because the report covers the whole program, the programmer can see several errors at once and fix them together.
The advantages follow from translating everything in advance. Execution is fast, because the translation is already done by the time the program runs. The executable can be given to users who do not have the compiler or the source code, which also means the source code can be kept private. The disadvantages are that the compiler must be run again after every change, which slows development down, that translating a large program can take a long time, and that the executable is machine dependent, so a different version is needed for a different type of processor.
How an interpreter works
An interpreter translates and executes the code line by line. No separate executable file is produced: the interpreter reads a line, translates it, runs it, then moves on to the next line, and it must be present every time the program is run.
If it finds an error it stops execution at that point and reports the error. That has two consequences the examiner looks for: the lines before the error have already run, and any errors further down the program have not been found yet.
The advantages are the mirror image of the compiler's. An interpreter is useful while a program is being written, because the programmer can run part of the program and see the result immediately without waiting for the whole thing to be translated. An error stops the program at the line that caused it, which makes that error easier to locate. Its disadvantages are that the same line is translated again every time it is executed, so the program runs more slowly, and that the interpreter must be installed on the computer for the program to run at all.
Compiler or interpreter: the comparison
The comparison is best learned as pairs, because every point about one has a matching point about the other. A compiler translates the whole program at once, before execution; an interpreter translates and executes line by line. A compiler produces an executable file; an interpreter produces none and must be present to run the program. A compiler reports errors for the whole program; an interpreter stops at the first error it meets.
The speeds also run in opposite directions. A compiled program runs fast but takes longer to develop, because the whole program must be recompiled after every change. An interpreted program runs slowly but develops quickly, because part of it can be run at once.
| Compiler | Interpreter | |
|---|---|---|
| How it translates | Translates the whole program at once, before it is executed | Translates and executes the code line by line |
| Executable file | Produces an executable file that can be run on its own | Produces no executable file; the interpreter must be present to run the program |
| How errors are reported | Gives an error report for the whole program, listing the errors found | Stops execution when an error is found and reports that error |
| Speed when the program runs | Fast, because the translation is already done | Slower, because each line is translated every time it is executed |
| Speed of development | Slower, because the whole program must be recompiled after every change | Faster, because part of the program can be run immediately |
| Typical use | Translating the final program | Developing and testing a program |
Which translator is used when
In practice both are used on the same project, at different times. An interpreter is mostly used when a program is being developed, because it gives feedback line by line while the code is being written and stops at the exact line where something goes wrong. A compiler is used to translate the final program, because the finished version should run as fast as possible and be ready to hand to users who have no development software installed.
- Developing: interpreter, for immediate feedback and an error at the line that caused it.
- Finished program: compiler, for speed and for an executable that runs without the translator.
- Teaching or trying a few lines: interpreter, because no separate build step is needed.
- Software for sale: compiler, because the executable runs on its own and the source code can stay private.
Integrated development environments (IDEs)
An integrated development environment (IDE) is a single piece of application software that provides everything a programmer needs to write, test and translate a program. Its role is to bring the separate tools of program development together in one place, so the programmer does not have to keep switching between a text editor, a translator and a testing tool.
The code editor is where the code is typed. It is more than a plain text editor: it numbers the lines, colours the different parts of the code so that keywords and strings stand out, and indents automatically. Error diagnostics means the IDE reports errors as the programmer works, marking the line where the problem is and explaining what the translator objected to, which turns debugging from guesswork into reading a message.
Auto-completion means the IDE suggests how a word or line might finish as it is typed and offers a list of the available options, so the programmer types less and makes fewer typing mistakes. Auto-correction means the IDE corrects or completes obvious mistakes, such as closing a bracket that was left open or fixing the spelling of a keyword. Prettyprint means the IDE lays the code out neatly, with consistent indentation and spacing, so that the structure of loops and conditions can be seen at a glance.
The run-time environment means the IDE can run the program and show the output, and often step through it line by line to watch the values in variables change. The translators built into the IDE, which may include a compiler, an interpreter and an assembler, mean the program can be translated without leaving the IDE.
| Function of an IDE | What it gives the programmer |
|---|---|
| Code editor | A place to type the code, with line numbers, colour-coded keywords and automatic indentation |
| Run-time environment | The ability to run the program and see its output without leaving the IDE, and often to step through it line by line |
| Translators | A compiler, interpreter or assembler built in, so the program can be translated from inside the IDE |
| Error diagnostics | Reports of errors as the programmer works, showing the line the error is on and what is wrong |
| Auto-completion | Suggestions that finish a word or line as it is typed, and lists of available options |
| Auto-correction | Automatic correction of obvious mistakes, such as a missing closing bracket or a misspelled keyword |
| Prettyprint | Automatic layout of the code with consistent indentation and spacing, so its structure is easy to see |
Key terms you must be able to define
- High-level language
- A programming language written in a form close to English that is easy to read and debug and is machine independent, but gives the programmer no direct manipulation of the hardware.
- Low-level language
- A programming language written in a form close to the hardware that allows direct manipulation of the hardware and can be fast and compact, but is hard to read and debug and is machine dependent.
- Machine code
- The binary instructions that the processor executes, each made up of an opcode and an operand. It is the only language the hardware understands.
- Opcode
- The part of a machine code instruction that says which operation to carry out.
- Operand
- The part of a machine code instruction that says what the operation is to be carried out on, such as a value or an address.
- Assembly language
- A low-level language that uses mnemonics in place of binary opcodes and names in place of memory addresses, with one mnemonic for each machine code instruction.
- Mnemonic
- A short letter code used in assembly language in place of a binary opcode, such as LDA for load, ADD for add and STO for store.
- Assembler
- The translator that converts an assembly language program into machine code.
- Compiler
- A translator that translates the whole high-level program at once before it is executed, producing an executable file and giving an error report for the whole program if errors are found.
- Interpreter
- A translator that translates and executes a high-level program line by line, stopping and reporting an error when one is found.
- Executable file
- The machine code file produced by a compiler, which can be run on its own without the compiler or the original source code.
- Integrated development environment (IDE)
- A single piece of software that provides the tools needed to write, test and translate a program, including a code editor, a run-time environment, translators, error diagnostics, auto-completion, auto-correction and prettyprint.
- Prettyprint
- An IDE function that lays out code with consistent indentation and spacing so that its structure is easy to read.
In context
2210 sets its questions inside systems a student already uses - a school network, a phone, a supermarket till, a vending machine, a website. These show how the theory is applied to a context you have not seen before, then you answer for the system you are given.
A programmer writes the code in an IDE, using auto-completion to type less and error diagnostics to spot mistakes as she goes. While she is developing, she runs the program with an interpreter so she can see each line work and find the line that fails. When the program is finished, she compiles it, because the compiler produces a fast executable file that can be sent to users who have no programming software installed at all.
The finished game is compiled, because a compiled executable runs far faster than a program being translated line by line, and speed is the whole point of a game. The compiler also keeps the source code out of the hands of anyone who buys the game, since only the executable is shipped.
The software that drives a network card is written in assembly language, because it has to place values in particular registers and read particular memory addresses. Each mnemonic maps onto one machine code instruction, so the programmer keeps complete control, but the driver has to be rewritten for a different type of processor.
A student learning to program writes a short program and runs it with an interpreter. She gets an error message naming the line, fixes it, and runs the program again within seconds. If she had to compile the whole program each time she would wait longer for each attempt and might not connect the error message with the line that caused it.
The team keeps one copy of the source code in a high-level language. Machines with different processors run the same source, because a compiler exists for each type of processor. This is machine independence in practice: the code is written once and translated for each target.
The controller in a washing machine has a few kilobytes of memory. Its program is written in assembly language so that it fits and runs quickly, and an assembler turns it into the machine code that is burnt into the controller's ROM.
Flashcards
Click a card to flip it. Mark honestly — the tracker uses this.
Exam-style questions
7 questions, 25 marks. Write your answer and press Mark my answer — it is marked against the scheme and tells you which points you made and what would gain the next mark. You can still reveal the scheme and mark it yourself instead.
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Exam technique for this topic
- For compiler versus interpreter, the two phrases that earn marks are 'translates the whole program at once before executing it' and 'translates and executes line by line'. Add what happens when an error is found, because that is a separate mark.
- Keep the three translators matched to their sources: assembler from assembly language, compiler from high-level language to an executable, interpreter from high-level language line by line. Mixing them up loses marks in every part of the question.
- For an advantages and disadvantages question, always attach a reason. 'It is faster' is not a mark, but 'the programmer can use specific processor instructions, so the code runs faster' is.
- When asked about an IDE, give its role in one sentence and then name at least three functions with a sentence each: code editor, error diagnostics, auto-completion, auto-correction, prettyprint, run-time environment, translators.
- Remember the machine-independence point. High-level code is machine independent, a compiled executable is machine dependent, and assembly language is machine dependent. This one distinction is examined repeatedly.
Why students lose marks here
Where this fits in O Level Computer Science 2210
This page is one topic of Cambridge O Level Computer Science 2210. O Level Computer Science 2210 is the course home: every area and every topic in Cambridge's own order, the papers you sit and what each one tests, and the exam-practice tools. The full topic list, with each topic linked to its revision page, is on the O Level Computer Science 2210 syllabus page.