We often hear the terms compilers, assemblers, linkers, and loaders. What are they and how are they different from each other? In this article, we will discuss that in detail.
A compiler is a computer program that translates computer code written in a source language into a target language. Most often compilers are used to translate source code written in a higher-level language into code written in a lower-level language. The lower-level language can be assembly language, object code, or machine code (What is the difference between assembly code, object code, and machine code?). For example, using a GCC compiler, we can translate a source code written in C programing language into machine code.
There are different types of compilers. For example, a source-to-source compiler takes a high-level language as input and translates it into another high-level language. And, a cross compiler generates executable code for a platform other than the one on which it is running.
An assembler is a program that takes assembly language as input and generates machine code. These machine code consists of strings of 0’s and 1’s and can be interpreted by a computer CPU. A machine code instruction instructs a CPU to perform certain tasks like loading data into registers, storing data to a memory location, performing some arithmetic or logical operation, etc.
When we run a compiler, the high-level language first gets translated into assembly language first. Then, the assembly language is translated into machine code. But, the machine code thus generated may not be ready to run on a CPU. If the high-level language uses headers, libraries, etc., then those libraries and headers need to be linked with the generated machine code. A linker does the job. A linker takes the generated machine code as input, links it with libraries, headers, etc., and generates another machine code that can be loaded into system memory.
A loader, on the other hand, takes the linked machine code and loads it into system memory. It also initializes the CPU registers, creates stacks, and prepares the machine code for execution.






0 Comments