0% found this document useful (0 votes)
11 views25 pages

System Programming

The document discusses the role of loaders in system programming, detailing their functions such as memory allocation, loading, relocation, linking, symbol resolution, and initialization. It differentiates between loaders and linkers, explaining various types of loaders like absolute, relocating, and dynamic linking loaders, along with their advantages and disadvantages. Additionally, it covers the design and working of linking loaders and linkage editors, emphasizing the importance of dynamic linking for efficient memory usage and executable file size reduction.

Uploaded by

krishnadhir4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

System Programming

The document discusses the role of loaders in system programming, detailing their functions such as memory allocation, loading, relocation, linking, symbol resolution, and initialization. It differentiates between loaders and linkers, explaining various types of loaders like absolute, relocating, and dynamic linking loaders, along with their advantages and disadvantages. Additionally, it covers the design and working of linking loaders and linkage editors, emphasizing the importance of dynamic linking for efficient memory usage and executable file size reduction.

Uploaded by

krishnadhir4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

System Programming

Basic Loader Functions: In system programming, a loader is a part of the


operating system that loads a program into memory so that the CPU can
execute it. When we run a program (like a text editor or a game), the loader
performs several important tasks to make sure the program runs correctly. The
main functions of a loader are:

1. Allocation (Memory Reservation):


 Before a program run, it needs space in the computer’s memory (RAM).
 The loader then allocates (reserves) the necessary memory for the
program.
 It ensures that the program does not overlap with other running
programs.
Example: If you open a Word document, the loader reserves space in memory
for the Word application and your document.

2. Loading (Copying Program into Memory):


 The program is usually stored on a hard disk in the form of an executable
file (e.g., .exe files in Windows).
 The loader copies this executable file from the hard disk into RAM.
Example: When you double-click a game icon, the loader copies the game’s
program into memory so that the computer can run it.
3. Relocation (Adjusting Memory Addresses):
 Some programs are written with fixed memory addresses, but when
loaded, they may be placed in different memory locations.
 The loader adjusts these memory addresses so the program works
correctly.
Example: If a program expects to start at memory address 1000 but is loaded at
2000, the loader updates all addresses accordingly.

4. Linking (Connecting External Files or Libraries):


 Many programs use external files or libraries (like .dll files in Windows
or .so files in Linux).
 The loader connects these external files to the program, ensuring all
necessary functions are available.
Example: A video player program may need a codec library to play certain video
formats. The loader links this library so the program can use it.

5. Symbol Resolution (Finding Variable & Function Names):


 Programs often use function names or variable names that refer to
actual memory locations.
 The loader finds these memory locations and replaces the names with
actual addresses.
Example: If a program calls a function printMessage(), the loader finds where
printMessage() is stored in memory and updates the program to use the
correct address.
6. Initialization (Preparing the Program to Run):
Before handing control to the program, the loader initializes important
components like:
 Stack (for temporary variables)
 Heap (for dynamic memory allocation)
 Registers (to store CPU instructions)
Example: When you open a web browser, it first loads essential settings (like
your homepage and bookmarks) before displaying the window.

In this way, the loader is an essential part of an operating system that makes
sure programs are loaded, connected, and ready to run. Without a loader,
programs would not be able to execute properly on a computer.
Differentiate between Loaders and Linkers:

Loaders Linkers

1. Definition: A Loader loads a 1. Definition: A linker combines


program into memory so that the multiple object files generated
CPU can execute it. by a compiler into a single
executable file.
2. Main Function: It loads the
program into RAM and prepares it 2. Main Function: Links various
for execution. object files and produces an
executable file.
3. Execution Time: Works at
runtime, just before the program 3. Execution Time: Works before
starts executing. execution, usually at compile-
time or load-time.
4. Types Of Loaders:
4. Types:
 Absolute Loader.
 Relocating Loader.  Static Linker.
 Dynamic Loader.  Dynamic Linker.

5. Output: The program is placed in 5. Output: Produces a single


memory and made ready for executable file after linking
execution. object files.
6. Example: When compiling a C
Important:
Loaders Schemes: In system programming, a loader is a part of the operating
system that loads a program into memory so that the CPU can execute it.
When we run a program (like a text editor or a game), the loader performs
several important tasks to make sure the program runs correctly. Below are
some common loader schemes in system programming:

1. Absolute Loader:
 The simplest type of loader.
 responsible for loading an executable file into memory at a predefined
address without any modification.
 No relocation or linking is done.
Advantages:
 Simple and fast since no address modification is needed.

2. Relocating Loader (Relocatable Loader):


 Modifies program addresses before loading it into memory.
 It allows the program to be placed at any available memory location.
 This makes programs more flexible and efficient in memory
management.
Advantages:
 More flexible than an absolute loader.
 Allows dynamic memory allocation.
3. Direct Linking Loader (Linking Loader):
 Performs both linking and loading of object modules.
 Uses a linkage editor.
 Converts symbolic addresses to absolute addresses at load time.
Advantages:
 Supports modular programming and dynamic linking.

4. Bootstrap Loader:
 A special type of loader that loads the operating system (OS) into
memory when a computer starts.
 Stored in ROM (Read-Only Memory) or firmware.
 It initializes hardware components and loads the OS kernel into RAM.
Advantages:
 Essential for starting the system.
 Always available because it is stored in ROM.
5. Dynamic Linking Loader:
 Loads and links programs dynamically at runtime.
 Useful in shared libraries (DLLs in Windows, .so in Linux).
 Saves memory by sharing common code among multiple processes.

Advantages:
 Reduces memory consumption.
 Allows updating libraries

Comparison of Loader Schemes:

Loader Type Relocation Linking

Absolute Loader No No

Relocating Loader Yes No


Loader Type Relocation Linking

Direct Linking
Yes Yes
Loader

Bootstrap Loader No No

Dynamic Linking
Yes Yes
Loader

In this way, the loader is an essential part of an operating system that makes
sure programs are loaded, connected, and ready to run. Without a loader,
programs would not be able to execute properly on a computer.

Design of an Absolute Loader: An Absolute Loader is a simple and fundamental


type of loader used in operating systems. It is responsible for loading an
executable file into memory at a predefined address without any modification.
The absolute loader does not perform relocation or linking, as the program is
already translated to execute at a fixed location.
Components of an Absolute Loader:
1. Header Record:
 The first part of the program file.
 It contains metadata about the program, such as:
o Program Name.
o Start Address (where the program should be loaded in memory).
o Program Length.

2. Text Records:
 These contain the actual machine instructions and data that need to be
loaded into memory.
 Each instruction is associated with a specific memory address.
Example: If a text record contains
Address: 1000 → Instruction: A0B5
Address: 1002 → Instruction: C3D4
The loader will place A0B5 at memory location 1000 and C3D4 at 1002.

3. End Record:
 This marks the end of the program file.
 It may also specify the starting address for execution.

Working of an Absolute Loader:


1. Reads the Header Record:
 This contains basic information, such as where the program should start
loading in memory.
2. Reads the Text Records:
 These records contain the actual program instructions (machine code)
along with their memory addresses.
 The loader places each instruction in the correct memory location
without any changes
3. Reads the End Record:
 This marks the end of the loading process.
 It may also specify the starting address for execution.
4. Transfers Control to the Loaded Program:
 Once all instructions are placed in memory, the loader hands over
control to the program so it can start running.

Algorithm of an Absolute Loader:


Step 1: Start.
Step 2: Read the Header Record.
Step 3: Extract the starting address from the header.
Step 4: Read the Text Records.
Step 5: For each text record:
a) Extract address and instruction.
b) Store instruction at the specified address.
Step 6: Read the End Record.
Step 7: Transfer control to the execution start address.
Step 8: Stop.
Flowchart of an Absolute Loader:
[Start] → Read Header Record → Extract Start Address → Read Text Records →
Load Instructions into Memory → Read End Record → Transfer Control to
Execution Address → [End].
Advantages of an Absolute Loader
✔ Simple to implement
✔ Requires minimal processing time
Disadvantages of an Absolute Loader
✖ Lack of flexibility

Diagram of Absolute Loader:

Relocating Loaders: Relocating Loaders is a type of loaders in system


programming that modifies program addresses before loading it into memory
and allows the program to be placed at any available memory location. This
makes programs more flexible and efficient in memory management.
Working of Relocating Loaders:
1. Reads the Program → The loader takes the program code and checks where
it should be placed in memory.
2. Finds Free Space → The operating system decides where the program can fit
in memory.
3. Changes Memory Addresses →The loader adds the new starting address to
all memory references in the program.
4. Loads the Program → After modifying the addresses, the loader puts the
program into memory.
5. Starts Execution → The computer runs the program from the new memory
location.

Advantages of Relocating Loaders:


1. Efficient Memory Utilization: Programs don’t need a fixed memory location
to run. The operating system can place programs in any free memory space,
reducing wasted memory.
2. Allows Multiprogramming: Multiple programs can run simultaneously
without memory conflicts.

🔢 Example in Assembly-Like Code


Let's consider a simple program where LOAD and STORE instructions use
memory addresses.
Before Relocation (Original Object Code):
Instruction Memory Address

(Load data from memory address


LOAD A, 1050
1050)

(Add data from memory address


ADD B, 1060
1060)

(Store result at memory address


STORE C, 1070 1070)

 Assume the program was originally written to run from 1000.


 The memory addresses (1050, 1060, 1070) are absolute addresses.

After Relocation (New Memory Location: 5000):


The relocating loader updates all addresses by adding (5000 - 1000) = 4000 to
each.

Updated Memory
Instruction
Address

LOAD A, 5050 (1050 → 5050)

ADD B, 5060 (1060 → 5060)

STORE C, 5070 (1070 → 5070)

Now, the program can run correctly from 5000 without needing any changes to
the original code.

In this way, Relocating Loaders, modifies the program addresses before


loading it into memory and allows the program to be placed at any available
memory location. This makes programs more flexible and efficient in memory
management.
Design of a linking loader: A linking loader is a type of loader that performs
both linking and loading in one step. It takes multiple object files (produced by
compilers or assemblers), resolves external references (like function calls
between different files), and directly loads the linked program into memory for
execution. linkage editor is used in Linking Loader.
Functions of a Linking Loader
1. Allocation: Assigns memory space to the program.
2. Loading: Copies program instructions into memory.
3. Linking: Resolves external symbols and relocates addresses.
4. Relocation: Adjusts memory addresses to fit within the allocated space.
5. Execution: Transfers control to the loaded program.

Working of Linking Loaders:


Pass 1: Symbol Collection
 Reads the object file’s record to understand program size.
 Scans the definition records to collect symbols (variables, functions).
 Stores them in a symbol table for future reference.
Pass 2: Loading and Relocation
 Reads text records (actual program code) and loads them into memory.
 Applies changes using modification records.
 Loads the modified program into memory.
 Transfers control to the program’s starting address for execution.

Flowchart of Linking Loaders:

Start
|
V
Reads the object file’s record.
|
V
Allocate Memory.
|
V
Scans the definition records.
|
V
Build Symbol Table
|
V
Reads text records.
|
V
Applies changes using modification records.
|
V
Loads the modified program into memory.
|
V
Transfer Control to Program.
|
V
Execution Starts.
|
V
End.

Advantages of a Linking Loader


 Reduces memory usage since linking is done in memory.
 Faster program execution as linking occurs only once.
 Supports dynamic linking for external libraries.

Linkage Editor: A Linkage Editor is a system software component that takes or


processes object file modules and prepares them for execution. It is
responsible for linking multiple object file modules into a single executable
program. It plays a crucial role in the execution of program by linking different
object file modules together and resolving addresses before execution.
Functions of a Linkage Editor:
1. Linking Object Modules:
 It combines multiple object modules generated by the compiler or
assembler.
 Ensures that references between modules are correctly resolved.
2. Symbol Resolution:
 Resolves external and internal symbols to determine memory addresses.
 Matches function calls with their definitions.
3. Relocation:
 Adjusts memory addresses to ensure all modules work correctly in a
single memory space.
 Assigns final absolute addresses to variables, functions, and instructions.
4. Optimization & Modification:
 Optimizes code by eliminating unnecessary operations.
 It can also modify object code for better performance or compatibility.
5. Executable File Generation:
 Produces a final executable file (e.g., .exe, .out) which is ready for
execution by the operating system.

Types of Linkage Editors:


1. Linker-Based Linkage Editor (Static Linking).
2. Dynamic Linkage Editor (Dynamic Linking).
1. Linker-Based Linkage Editor: The Linker- Based Linkage editor combines or
links all the object file modules together into a single executable file before the
program runs.
Advantages of Linker-Based Linkage Editor:
✔ The program runs faster since everything is pre-linked.
✔ No dependency on external files at runtime.
Example:
When you install a software that doesn’t require additional downloads, it’s
likely using static linking.

2. Dynamic Linkage Editor: This type of linkage editor links some parts of the
program at runtime instead of during compilation. It loads external libraries
only when needed.
Advantages of Dynamic Linkage Editor:
✔ Reduces the size of the executable file.
✔ Makes it easy to update or modify libraries.
Example: Most modern applications, like web browsers, use dynamic linking to
load external resources only when required.
In this way, a Linkage Editor is an important system software component that
takes or processes object file modules and prepares them for execution.

Dynamic Linking: Dynamic linking is a method used in system programming


that links libraries to a program at runtime rather than at compile time. It
allows programs to share common libraries, reducing memory usage and
executable file size.

Working of Dynamic Linking:


 When a program is compiled, it does not include the entire library code
inside the executable file.
 When the program runs, the operating system loads the required library
into memory and connects the program to it.
 Dynamic linking enables multiple programs to use the same library
without repetition.

Key Components of Dynamic Linking:


1. Shared Libraries (Dynamic Link Libraries)
 On Windows: (.dll) (Dynamic Link Library)
 On Linux: (.so) (Shared Object)
 On macOS: (.dylib) (Dynamic Library)
2. Dynamic Linker/Loader
 A system component that loads shared libraries into memory at runtime.
 Examples:
o Windows: LoadLibrary() function.
o Linux/macOS: ld.so.
3. Symbol Resolution:
 The linker resolves operations like function and variable dynamically
using symbol tables.
 If any symbol is missing in symbol table, the program may crash.

Advantages of Dynamic Linking:


 Reduces Memory Usage: Dynamic Linking reduces the memory usage
since multiple programs use the same library.
 Smaller Executable Size: The main program file remains small because it
does not contain all the library code.
 Easier Updates: If a library is updated, all programs using that library will
automatically get updated.
 Code Reusability: The same library functions can be used in multiple
programs, making software development easier.
Dynamic linking is an efficient technique that helps reduce memory usage and
executable file size. However, it also introduce security risks. Understanding
dynamic linking is essential for system programmers, as it plays a vital role in
modern operating systems.

Bootstrap Loader: In system programming, the Bootstrap Loader (or simply


Bootloader) is a fundamental component responsible for loading the operating
system (OS) into memory when a computer is powered on or restarted.
Basically, Bootstrap Loader is a small but essential
program in a computer system that is responsible for starting the operating
system (OS) when the computer is turned on or restarted.
When a computer is powered on, the main memory (RAM) is empty. The
computer needs a way to load the operating system into memory so that it can
begin functioning properly. This task is performed by the Bootstrap Loader,
which is stored in a non-volatile memory such as ROM (Read-Only Memory).
Working of the Bootstrap Loader
The process can be described in the following steps:
1. Power-On: When the computer is switched on, the CPU starts executing
instructions from a fixed memory address where the bootstrap code is
located.
2. Execution of Bootstrap Code: This code performs basic hardware checks
and then locates the bootable device (such as hard disk, SSD, or USB
drive).
3. Loading the Operating System: Then the Bootstrap Loader loads the
Operating System kernel into the RAM (main memory).
4. Transfer of Control: Once the OS is loaded, the bootstrap loader
transfers control to the operating system, which then takes over the
complete control of the computer.

Types of Bootstrap Loaders


There are generally two types of bootstrapping:
 Cold Booting: When the system is started after being completely
powered off.
 Warm Booting: When the system is restarted without turning off the
power.

Importance of Bootstrap Loader


 It is the first software that runs when a system is started.
 Without it, the computer would not know how to load the operating
system.
 It acts as a bridge between hardware and the operating system. n

Flowchart of Bootstrap Loaders:


In this way, the Bootstrap Loader (or simply
Bootloader) is a fundamental component responsible for loading the operating
system (OS) into memory when a computer is powered on or restarted.

You might also like