Implementing Absolute Loader in C++

A loader is a system program responsible for placing a program into memory so it can be executed. An absolute loader is the simplest type: it loads a program at a fixed, pre-determined memory address (the starting address is embedded in the object file itself and cannot be changed). This contrasts with a relocating loader, which can place the program anywhere in memory and patches all address-dependent instructions accordingly.

This C++ program simulates an absolute loader. You provide a starting address and the values to be placed at consecutive bytes, and the program then lets you query what the value at any relocation address (offset from the starting address) would be — mimicking the act of loading and then reading back memory.

C++ Code

#include <iostream.h>
#include <conio.h>

void main() {
    clrscr();

    int data[4];    /* 4 bytes of program data to load  */
    int stadd;      /* starting (absolute) load address */
    int readd;      /* relocation address entered by user */
    int i;
    char ch;        /* user's Y/N choice */

    /* Step 1: Accept the absolute starting address */
    cout << "Enter starting addressn";
    cin  >> stadd;

    /* Step 2: Accept 4 byte-values (simulating the object file content) */
    for (i = 0; i < 4; i++) {
        cout << "Enter value for byte " << i << " : ";
        cin  >> data[i];
    }

    /* Step 3: Repeatedly display the loaded bytes at a given base address */
    do {
        cout << "\nEnter relocation addressn";
        cin  >> readd;

        /* Show the value at each byte offset from the relocation address */
        for (i = 0; i < 4; i++) {
            cout << "\nValue for " << (readd + i) << " = " << data[i];
        }

        cout << "\nDo you want to continue relocating (Y/N) : ";
        cin  >> ch;

    } while (ch == 'Y');

    getch();
}

How the Code Works

  1. Starting address input: The user enters stadd, which represents the absolute address at which the program would be loaded. In a real system this value comes from the header record of the object file.
  2. Data (byte) input: Four integer values are read into data[0]data[3]. These simulate the content of the object module — the machine-code bytes that the loader places into memory starting at stadd.
  3. Relocation query loop: The user enters a readd (relocation address). The program displays the value stored at each byte offset (readd + 0, readd + 1, …) as if those bytes had been loaded at that base. The loop repeats until the user enters N.
  4. Absolute nature: Notice that the data[] values never change regardless of the relocation address — this is the defining property of an absolute loader. A relocating loader would adjust address-dependent bytes when the load address differs from the compiled address.

Sample Input / Output

Enter starting address
2224
Enter value for byte 0 : 1000
Enter value for byte 1 : 1001
Enter value for byte 2 : 1010
Enter value for byte 3 : 1110

Enter relocation address
5231

Value for 5231 = 1000
Value for 5232 = 1001
Value for 5233 = 1010
Value for 5234 = 1110
Do you want to continue relocating (Y/N) : Y

Enter relocation address
5777

Value for 5777 = 1000
Value for 5778 = 1001
Value for 5779 = 1010
Value for 5780 = 1110
Do you want to continue relocating (Y/N) : N

Output Explanation

  1. Starting address 2224: This is where the object module was assembled to run. The four data bytes (1000, 1001, 1010, 1110) represent the program content at bytes 2224, 2225, 2226, and 2227.
  2. First relocation query – base 5231: The loader is asked to show the content as if it were placed at address 5231. The four bytes appear at 5231, 5232, 5233, and 5234 with the same values. This confirms absolute loading: data values are unchanged.
  3. Second relocation query – base 5777: Another base address is tried; the bytes now appear at 5777–5780, again with identical values. Regardless of where you “load” them, the content stays fixed.
  4. User enters N: The do–while loop terminates and the program exits.

See Also

Conclusion

This simulation highlights the core limitation of absolute loading: the program is tied to one specific address and cannot be safely moved without recompilation. Despite this, absolute loaders are still used in embedded systems and microcontrollers where memory layout is fixed and simplicity is paramount. Understanding this model also sets the stage for appreciating how a multi-pass assembler generates the location-counter values that the loader reads, and why more sophisticated systems evolved relocating and linking loaders.

One thought on “Implementing Absolute Loader in C++”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.