Menu

Translators

Translators

A translator is a program that converts source code into machine code so that a processor can carry out the instructions. You write in a language that suits a person: meaningful names such as pupil_total, and English-like words such as if and while. A processor, however, only carries out machine code, which is a pattern of binary instructions. Code that has not been translated cannot run at all, which is why every program you write must be translated before it can run.

The three translators

TranslatorWhat it takes inWhat it produces
CompilerHigh-level source code, translated as a whole program in one goMachine code, saved as an object code file that can be run later
InterpreterHigh-level source code, taken one line at a timeNo saved file: each line is translated and then run straight away
AssemblerAssembly language, a low-level language written one instruction per lineMachine code

A compiler and an interpreter both start from the same thing, high-level source code, and both produce machine code instructions for the processor. The difference is the order in which they work. A compiler translates the entire program first, and only then is the finished program run. An interpreter translates the first line, runs it, translates the second line, runs it, and carries on like that to the end.

Picture a program of 40 lines. A compiler works through all 40 lines, produces one complete machine code file, and the program starts afterwards. An interpreter turns line 1 into machine code and carries it out at once, then does the same for line 2, so the program is already doing something while the later lines are still untranslated.

The third kind of translator is the assembler, and it is different again because of what it starts with. Assembly language sits close to the processor: one line, such as ADD 12, matches a single machine code instruction, so the assembler swaps each line for the matching binary instruction without any restructuring.

A compiler translates all 40 lines into one machine code file and then runs it, while an interpreter translates and runs line 1, then line 2, and so on to the end. An assembler swaps an assembly line such as ADD 12 for one machine code instruction.

Practise this