Interpreters and Compilers

In the early days of computers the programs had to be written in machine code. The machine code instructions are the instructions provided as the instruction set of the machine and these codes are represented by a binary pattern in the computer. To write a program the programmer had to write the instructions in binary.

This was a very complex task and even writing very simple programs took a long time. It was very difficult to produce correct programs as detecting errors in the programs and correcting these errors (Debugging the program) was very difficult. A program that produces the binary instruction for a given assembly language program is called an Assembler.

Assembly Instruction Maps

Each assembly instruction maps to a single machine instruction so it is very easy to translate a program written in assembly language to machine code. Writing programs in assemble language is very easy but is still a tedious work and took a long time.

The program that translates a high-level language program into machine language is called a compiler. Once a program has been translated into machine code it can be loaded into the main memory and executed by the CPU. The high-level language version of the program is usually called the source code and the resulting machine code program is called the object code. The relationship between the source code and the object code is shown in figure below:


Figure of Compiler

A more appropriate definition of a compiler is that it is a program that takes as input a high-level language program and generates an object program. This object program may or may not be the absolute machine code.

Another useful translator is an interpreter. An interpreter takes as input a high-level language program and performs the following actions.

  • It repeatedly reads instructions (one at a time) and translates it to machine code.
  • It then executes the instruction

One difference between a compiler and an interpreter is that a compiler converts each instruction only once but an interpreter may translate an instruction several times. Clearly if an instruction has some error an interpreter can easily identify it.

Also an interpreted program runs slower than a compiled program as once a program is compiled it does not need any further translation but the original program has to be translated every time it is executed by an interpreter. And some instructions will be translated several times.