Languages, Translators and IDEs
High-level and low-level languages, the translators that turn them into machine code, and the IDE you write code in.
WATCH IT EXPLAINED
EXPLORE IT IN 3D
The translation factory
One program at three altitudes, an assembler press, a compiler racing an interpreter, and an IDE to write it in. Feed it, stamp it, race it — then debug it.
The processor only understands machine code
A processor only runs machine code, so every other language must be translated first.
A processor can only execute machine code: instructions in binary, from its own instruction set (the list of instructions it knows).
A program written in any other language has to be translated into machine code before it can run.
Remember:One line of a high-level language usually becomes several machine-code instructions.
The figure runs the two lines of Python below on the Gatecraft processor from the game's Tier 5. Step through it one instruction at a time. The Python line lights up while its instructions run.
total = price + tax
print(total)High-level languages
High-level languages are easy for people to use, but they must be translated and give less control.
A high-level language, such as Python, Java, Visual Basic or C++, uses English-like words and maths symbols.
| Advantages | Disadvantages |
|---|---|
| easier to read and write | cannot directly manipulate the hardware |
| easier to debug | must be translated before it can run |
| machine independent: the same program can be translated for different types of processor | may be less efficient (slower or larger) than well-written low-level code |
| you do not need to know how the hardware works |
Low-level languages: machine code and assembly
Low-level languages, machine code and assembly, control the hardware directly but are hard for people.
There are two low-level languages:
- Machine code is binary. It is the only language the processor runs.
- Assembly language uses mnemonics: short codes such as
LDD(load),ADDandSTO(store). Each assembly instruction stands for one machine-code instruction.
| Advantages | Disadvantages |
|---|---|
| can directly manipulate the hardware, such as registers and memory addresses | hard to read and write |
| can be very efficient | hard to debug |
| machine dependent: written for one type of processor |
Exam:You need to understand assembly language, not write it.
LDD price // ACC <- price ADD tax // ACC <- ACC + tax STO total // total <- ACC OUT // output ACC END price: DB 12 tax: DB 3 total: DB 0
The assembler
An assembler translates assembly language into machine code, one instruction for one instruction.
An assembler is the translator for assembly language. It turns:
- each mnemonic into its opcode, the binary code for that instruction;
- each label into a memory address.
Every assembly instruction becomes one machine-code instruction. So assembly is still a low-level language: it is the processor's own instructions, written so that people can read them.
Watch out:An assembler translates assembly language only. Compilers and interpreters translate high-level languages.
In the figure, pick the preset "two mistakes". The assembler reports both mistakes at once, on lines 2 and 3.
A compiler
A compiler translates the whole program before any of it runs, and makes an executable file.
A compiler works in these steps:
- It translates the whole high-level program at once, before any of it is executed.
- Errors found? It produces an error report for the whole code, with every error and its line. It makes no executable file.
- No errors? It produces an executable file of machine code.
- The executable file runs on its own, as often as you like, without the compiler and without the source code.
Example:In the figure, press Run. The compiler reports the errors on lines 7 and 8, and nothing runs.
An interpreter
An interpreter translates and runs one line at a time, and stops at the first error.
An interpreter works in these steps:
- It translates line 1 and runs it, then translates line 2 and runs it, and so on.
- When it meets an error, it stops execution at that line and reports that one error. The lines after it are never reached.
- It makes no executable file.
- So the interpreter and the source code are needed every time the program runs. Every run translates the lines again.
Example:In the figure, fix line 7 and run it again. Now it stops at line 8.
Compiler or interpreter: which, and when
Use an interpreter while developing a program, and a compiler for the final program.
| Compiler | Interpreter | |
|---|---|---|
| Translates | all the code, then runs it | one line, then runs it |
| Makes | an executable file | no executable file |
| Errors | one report for all the code | stops at the first error |
| Runs | faster: nothing is translated while it runs | slower: its lines are translated every time it runs |
| Translator needed | only once | every run |
| Mostly used for | the final program | developing it |
- An interpreter finds errors one at a time as the code runs. When an error is corrected, the program can carry on from that point.
- A compiler's executable file can be given to users without the source code or the translator.
Watch out:After every change, a compiler must translate the whole code again.
An IDE: writing code
An IDE is one program for writing, translating, running and testing code, with tools that help you type.
An IDE (integrated development environment) is one program with everything needed to write, translate, run and test code.
Four of its functions help while you write:
| Function | What it does |
|---|---|
| code editor | where you type and edit the code |
| prettyprint | colours the keywords and indents the code automatically, so its structure is easy to see |
| auto-completion | suggests the rest of a keyword or a variable name as you type it |
| auto-correction | fixes common mistakes automatically, such as a misspelt keyword |
Try auto-completion: type OUT on a new line.
An IDE: running and finding mistakes
An IDE also translates and runs your code, and points to errors; know all seven functions.
Three more functions help you run code and find mistakes:
- Translator: a compiler or an interpreter built into the IDE. The code is translated at the press of a button.
- Run-time environment: runs the program inside the IDE, showing its output and taking its input.
- Error diagnostics: find errors and show where they are. They highlight the line, explain the problem and often suggest a fix.
All seven syllabus functions of an IDE:
| Function | Job |
|---|---|
| code editor | type and edit the code |
| prettyprint | colour keywords, indent the code |
| auto-completion | suggest the rest of a word |
| auto-correction | fix common mistakes as you type |
| error diagnostics | find errors, show where they are |
| translator | compile or interpret the code |
| run-time environment | run the program inside the IDE |
Exam:Many IDEs also have a debugger, with breakpoints and watched variables. It is useful, but in the exam name the seven syllabus functions first.
- Click the stages below in the order they happen.
KEY TERMS
CHECK YOURSELF
1.You are learning to program and want errors reported one at a time as you go. Which translator suits you?
2.What does a compiler produce that an interpreter does not?
3.Which is an advantage of a high-level language over assembly?
4.Which translator turns an assembly language program into machine code?
5.A program has an error on line 7 and another on line 8. What does a compiler do?
6.Which IDE function colours the keywords and indents the code so its structure is easy to see?