Relocatable Code in Compiler

In this class, We discuss Relocatable Code in Compiler.

For Complete YouTube Video: Click Here

The reader should have prior knowledge of the basics of the compiler. Click here.

The below diagram shows the program and its machine-level code in Random Access Memory.

We discussed the example in our last class.

Given, The RAM locations 500 to 512 to the program p5.

The remaining RAM is filled with other programs and named p1 and p2.

In our computer architecture, we discussed whenever an i/o operation has come.

The present program’s execution is stopped, and the i/o program will get executed.

Similarly, we discussed whenever a high priority program has come into the Operating system.

we shift the execution to high priority program

Assume our program p5 executed two lines.

A new high priority program has come. The operating system stopped executing the program p5.

The new high priority program has no place to be placed in RAM.

So operating system will take the program p5 and place the copy on the hard disc.

The place assigned to p5 is given to the new high priority program.

The high priority program will execute.

Again, program p5 got a chance to execute.

The program is on a hard disc, and we need to copy it to RAM.

Now, the operating system will assign new space to program p5.

Assume, That program p5 is assigned starting from location 200.

The data is present in 210 and 211 locations.

The instruction says the data is present in the 510 location.

The example mentioned above is a problem.

We need a relocatable code to avoid the problem.

How to convert the high-level language to relocatable code?

The below example explains to convert to relocatable code.

The compiler assumes during the conversion; the program will be placed from zero memory location in RAM.

The compiler assumes:

The memory location zero will be given to the first machine instruction.

The second instruction will be placed in the first memory location. And so on.

The below diagram shows the relocatable code.

The responsibility of the loader program is to convert the relocatable code.

The loader program will assign memory to the program p5.

Assume the loader wants to assign program p5 at starting location 500.

The address in the instruction is added with a value of 500.

The starting address is placed in a base register. And The starting address is added to the address in each instruction.

Suppose we want to place the program in 200 locations. We add 200 to the address in the instructions.

We can relocate the code to any location.