1.8 Compiling, Linking and Loading a Program

     Programming in C    


Compiling, Linking, and Loading a Program

A program written in a source language is translated by a compiler to produce a program in a target language. The source language is usually a high-level language. The target language may or may not be machine language. In most cases, the target language is assembly language, which is further translated by an assembler into an object program. The object program is then linked with other object programs and library code to produce an executable program. When execution is required, the executable program is loaded into main memory. The entire process is managed and controlled by the operating system.


Compilation Process

Conceptually, the compilation process is divided into several phases, each handled by a separate module of the compiler.

1. Lexical Analysis

In this phase, the source program is scanned to identify lexical units called tokens, such as identifiers, operators, and delimiters. These tokens are classified according to their types.
A symbol table is created to store information about user-defined names along with their attributes. This table is used by other phases of the compiler.


2. Syntax Analysis

In this phase, tokens are combined into syntactic units such as expressions and statements. The syntax analyzer checks whether the program conforms to the syntax rules of the programming language.
As a result of parsing, a parse tree is generated.


3. Semantic Analysis

The semantics of a programming language define the meaning of statements during execution. In this phase, the compiler verifies that the operators and operands follow the language rules.
Semantic analysis ensures that statements are meaningful and logically correct.


4. Intermediate Code Generation and Optimization

Many compilers generate an intermediate code to make the target program smaller or faster or both. This code lies between assembly language and machine language.
Optimization techniques are applied to improve program efficiency without changing its meaning.


5. Code Generation

This is the final phase of compilation. The optimized intermediate code is converted into target code using predefined templates.
The generated code depends on the processor architecture, instruction set, addressing modes, and registers.

The output of the compilation process is an object program.


Linking 

After the compilation process, the compiler produces an object program, which is not directly executable. If the program uses pre-written system libraries or separately compiled user-defined subroutines, the object program must be combined with these components. This process is known as linking.

The linker performs the following important functions:

  • Combines multiple object files produced from different source programs.

  • Links library routines required by the program, such as input/output functions and mathematical operations.

  • Resolves external references, i.e., references to functions or variables that are defined in other modules.

  • Assigns absolute memory addresses to program instructions and data.

During linking, all symbolic addresses are replaced by actual memory addresses, and a single executable file is generated. The executable program is stored in a specified location in secondary storage.

Thus, linking ensures that all parts of a program are properly connected and ready for execution.


Loading 

The loading process begins when the executable program is selected for execution. The executable file resides in secondary memory and must be transferred to main memory before it can run.

The loader, which is a part of the operating system, performs the following tasks:

  • Allocates memory required for the program.

  • Loads program instructions and data from secondary storage into main memory.

  • Adjusts addresses if required, based on the allocated memory locations.

  • Initializes registers and prepares the execution environment.

  • Transfers control to the starting address of the program.

Once loading is completed, the program starts execution under the supervision of the operating system. The operating system manages the execution and provides necessary services such as memory management and I/O operations.


Importance of Linking and Loading

  • Linking enables the reuse of library code, reducing program size and development effort.

  • Loading allows the program to be executed efficiently by placing it into main memory.

  • Both processes are essential to transform object code into a runnable program.






Definition: 


Compiling and Linking

A computer cannot execute a program written in a high-level language such as C directly. The source program must be converted into machine language before execution. This conversion is carried out in two major stages known as compiling and linking.

Compiling

Compiling is the process of translating a complete source program written in a high-level language into an equivalent machine language program. This task is performed by a compiler. The compiler reads the entire source code and checks it for syntactical correctness. If any syntax errors are found, they are reported and the program must be corrected before recompilation.

When the source program is free from errors, the compiler generates an object code. This object code contains machine instructions, but it is not directly executable because it may contain references to external functions or library routines that are not yet linked.

Linking

Linking is the process that follows compilation. The linker combines one or more object files generated by the compiler with the necessary library files. It resolves external references, such as calls to standard library functions, and assigns appropriate memory addresses to all program instructions and data.

After completing the linking process, the linker produces a fully executable program, which can be loaded into memory and executed by the computer.

Importance of Compiling and Linking

Compiling ensures that the program is free from syntax errors and converts it into object code. Linking ensures that all required functions and libraries are properly connected, resulting in a complete and executable program.


Difference between Compiling and Linking

CompilingLinking
Converts source program into object codeCombines object files with libraries
Done by compilerDone by linker
Detects syntax errorsResolves external references
Output is object fileOutput is executable file




Loading  - Very Important 

In some digital devices such as controllers of small appliances, handheld devices, and videogame consoles, the operating system is simple and small and is stored in ROM (Read-Only Memory). In such systems, the operating system gains immediate control of the processor as soon as the device is turned on. Industrial controllers and petrol-filling equipment also use operating systems stored in ROM.

In a personal computer, the operating system is usually stored on the hard disk. Since the operating system is large in size, it cannot be placed entirely in RAM. The kernel, which is the core part of the operating system, is loaded into RAM during start-up and remains in memory at all times. Other parts of the operating system are loaded into RAM as and when required. A new computer does not contain an operating system by default. The operating system is generally supplied on CD or DVD and must be installed on the hard disk by expanding compressed files and initializing the system.

Booting is the process followed by a computer from the moment it is switched on until the operating system is fully loaded and ready for use.

The Basic Input Output System (BIOS) is a small set of instructions stored in PROM. When the computer is powered on, the ROM circuitry begins the boot process. An address is automatically loaded into the Program Counter (PC), pointing to the first instruction of the BIOS. The BIOS performs the POST (Power On Self Test) to verify that devices such as memory, monitor, keyboard, and I/O devices are functioning correctly.

During POST, the BIOS compares the detected system configuration with the information stored in the CMOS memory chip on the motherboard. The CMOS stores updated information about system components and parameters such as disk organization.

The BIOS then loads the Master Boot Record (MBR) from the first sector of the bootable device into main memory. The MBR is 512 bytes in size and contains a bootstrap loader. The bootstrap loader starts loading the operating system and transfers control to it. The operating system then completes the loading process.






📖 Reference

The content for this subject is prepared by referring to the standard textbook “Computer Fundamentals and Programming in C” by Pradip Dey and Manas Ghosh, Second Edition, Oxford University Press (2018). The explanations are simplified and exam-focused while aligning with the syllabus and concepts presented in the reference book.


Aivette-coi

Aivette-coi is created with the intention of helping college students learn smartly, revise quickly, and approach exams with confidence.

With love and care,
by Aivette 💙






Comments

Popular posts from this blog

22IMC7Z2 - CONSTITUTION OF INDIA

1.1 Historical Background of the Indian Constitution: The Company Rule & The Crown Rule

1.3 Preamble and Salient Features of the Indian Constitution