2
$\begingroup$

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.

Please help me understand !!

Thanks.

$\endgroup$
2
$\begingroup$

A compiled entity (say, a library) has a sequence of instructions. Each instruction has a particular location in sequence. This location is a value which can be considered simply as an offset from a base address. Loader loads the library at a particular address in the virtual address space of process and this address becomes the base address for the library and subsequently each instruction's location added to this base address becomes the actual address of the instruction in virtual address space of process.

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – D.W. Mar 5 '17 at 11:58
  • $\begingroup$ @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. $\endgroup$ – Ahemone Mar 5 '17 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.