Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Functions of Compilers /

Interpreters, Linkers, Loaders, Cross


Compilers (*)
What is Compiler?
• A compiler is a computer program that transforms code written in a
high-level programming language into the machine code.
• It is a program which translates the human-readable code to a language a
computer processor understands (binary 1 and 0 bits).
• The computer processes the machine code to perform the corresponding
tasks.
• A compiler should comply with the syntax rule of that programming
language in which it is written.
• However, the compiler is only a program and cannot fix errors found in that
program. So, if you make a mistake, you need to make changes in the
syntax of your program. Otherwise, it will not compile.
Role of Compiler
• Compliers reads the source code, outputs executable code
• Translates software written in a higher-level language into
instructions that computer can understand. It converts the text that
a programmer writes into a format the CPU can understand.
• The process of compilation is relatively complicated. It spends a lot
of time analyzing and processing the program.
• The executable result is some form of machine-specific binary code.
What is Interpreter?
• An interpreter is a computer program, which coverts each high-level
program statement into the machine code.
• This includes source code, pre-compiled code, and scripts.
• Both compiler and interpreters do the same job which is converting
higher level programming language to machine code.
• However, a compiler will convert the code into machine code
(create an exe) before program run.
• Interpreters convert code into machine code when the program is
run.
Role of Interpreter
• The interpreter converts the source code line-by-line during RUN
Time.
• Interpret completely translates a program written in a high-level
language into machine level language.
• Interpreter allows evaluation and modification of the program while
it is executing.
• Relatively less time spent for analyzing and processing the program
• Program execution is relatively slow compared to compiler
KEY DIFFERENCE
• Compiler transforms code written in a high-level programming language
into the machine code, at once, before program runs, whereas an
Interpreter coverts each high-level program statement, one by one, into
the machine code, during program run.
• Compiled code runs faster while interpreted code runs slower.
• Compiler displays all errors after compilation, on the other hand, the
Interpreter displays errors of each line one by one.
• Compiler is based on translation linking-loading model, whereas Interpreter
is based on Interpretation Method.
• Compiler takes an entire program whereas the Interpreter takes a single
line of code.
Difference Between Compiler and
Interpreter
Linkers

A linker is a program that allows a user to link library programs or separate modules of code into
their own programs. It is used to combine different modules of object code into one single
executable code program. This may involve combining a program with library programs, or involve
recombining blocks of object code from the same program, or a mixture of both.
• A program may call library modules.
• Library modules will use data stored in ‘relative addresses’. See the section below on loaders for more about
relative addressing.
• The program itself is compiled.
• The library modules are compiled.
• The linker program is run.
• The compiled library modules are linked to (connected to) the compiled program.
• Base addresses used in the library modules are adjusted so that the calls in them to data work from within the
main program.
• One executable program is produced.
• Linkers are also used when a user has to compile a big program in sections, perhaps because there is
a shortage of RAM. The user's program is split up into sections and held on backing storage. Each
section is then brought into RAM, one section at a time, from backing storage and compiled into
object code. Each block of object code is then saved back to the backing storage. When the whole
program is compiled, the compiler can be removed from RAM and all of the blocks of object code
can then be brought into RAM because there is enough space without the compiler program. The
linker program is used to recombine the blocks of object code in RAM to get a working full program.
Loaders
• A loader is a piece of software that chooses exactly where to put object code in RAM, ready for it to be run. It also
adjusts the memory references in programs. These pieces of software are explained in more detail below.

• The job of a piece of software known as a loader is to take the object code generated by compilation and to find a
'good' place for it in RAM, where it can then be executed. Imagine that a software house has written a program to sell
to the public. They wrote the source code, compiled it so that they then had some object code, and then copied the
object code onto a CD, ready to sell. They wouldn't distribute the source code because they would want to keep the
actual program code secret from competitors - it is practically impossible to turn object code back into source code.
Besides, your customers may not have the necessary compilers on their machines to convert the source code into
object code. In addition, it would be a little inconvenient if your customers had to compile every program they
wanted to use before they actually used it!

• If you bought the CD and wanted to run the program, you might double click on the .exe file. The loader would then
copy the object code from the CD into your RAM and run it from there. But where in RAM would the loader put it?
You have other applications running in RAM, for example, the operating system and a virus checker. You may also be
multi-tasking, with various programs and data in RAM. If the loader is not careful, it will load a program in a place in
RAM that interferes with other programs. Your loader program, then, must decide where to put the object code in
RAM so that it doesn't interfere with other programs and data. This is the first main job of the loader program.

• The second main job involves adjusting references that are used within a program. Programs can be written by
programmers using either ‘absolute addressing’ or ‘relative addressing’. Relative addressing is more common
because then the loader can put the program anywhere in RAM - absolute addressing isn't flexible.
Cross Compilers
• Cross Compilers are compilers that execute on one computer and
generate object code that can execute on different platform.
• for example a cross compiler that is running on windows pc can
produce object code that run on MAC Os or Android Os.
Native Compiler Cross Compiler

Translates program for same hardware/platform/machine on Translates program for different hardware/platform/machine
it is running. other than the platform which it is running.

It is used to build programs for same system/machine & OS It is used to build programs for other system/machine like
it is installed. AVR/ARM.

It is dependent on System/machine and OS It is independent of System/machine and OS

It can generate executable file like .exe It can generate raw code .hex

TurboC or GCC is native Compiler. Keil is a cross compiler.

You might also like