# how the instructions are stored in relocatable format on disk

I understand how the instructions are kept in relocatable format in RAM. It is done so by keeping a base register and a limit register. Physical address = base register value + logical address. And logical address = the logical distance from the starting location of process's address space, just like an array.

But what I don't understand how are they kept in relocatable format on the disk, is there some base value there too. Plus how are they linked to a computer program say C Program or Java program using dynamic loading of shared libraries.

Thanks.

## Instructions

To answer your question it's worth diving briefly into the instruction set of the architecture. Most instruction sets will contain 2 classes of jump instructions; the indirect jump and the direct jump. This includes call instructions and branch instructions as they generally all feature some kind of jump. The direct type will jump to a set location, i.e. instruction pointer = value. Indirect jumps will perform the operation instruction pointer = instruction pointer + value.

## The compiler

Since you want to be able to write in a higher level language you will likely be using a compiler, this saves you having to work out all these offsets. An option in the compiler is to have it assemble programs using indirect jumps exclusively. This mode is likely default because, as you're aware, there is an operating system to contend with and so you never know where your program is actually located. By avoiding direct jumps completely you no longer need to know where you are, just how far away you are.

## Why have direct jumps in the first place?

They're faster, there's one less operation involved and often more. An indirect jump will need to look at the instruction pointer and add a number to it. Direct jumps then find themselves used in time critical situations as you'd often find in embedded applications such as microcontrollers or very low level OS and BIOS work.

• Nice answer! Minor comment: What you call an indirect jump is, in my experience, typically called "PC-relative addressing". I guess we could count this as an indirect jump. However, I have the impression that it is more usual to reserve the term "indirect jump" for only instructions that take their target address from a register or from a location in memory. – D.W. Mar 5 '17 at 11:58
• @D.W. your point is valid, I used indirect because I felt it a better blanket term. Depending on the architecture you may have to predict the branch by loading PC then offsetting manually before the indirect. In any case it's not using a hard coded value, but some kind of manipulation of PC. – Ahemone Mar 5 '17 at 22:19