Relative leaps allow compilers to generate relocatable code, which means the code will work anywhere in memory; It is not tied to a fixed location. This is a critical concept of libraries: you can write the code once and compile it into a file of a movable object, which can be linked as is in any program. The compiler should assign absolute addresses only for functions that are externally accessible (so your own code can find them); all "internal" jumps are relative and should not change from one executable to the next.
It is worth your time to write code in a high-level language such as C, have a compiler generate the assembly code (check the -S option on gcc), and then read the assembly output. Pay particular attention to conditionals and loops such as if , for and while , and you will see that they all generate relative jumps.
Here's a far-fetched example using dummy build commands:
// Sample C code Address Assembly Code Comments if (x < 10) { 0000 CMP x,
Here, the compiler generates relative jumps ( JGE and JMP ) to skip the incomplete branch of the if-else block. These jumps will work regardless of where in the memory the linker places the code. If they were absolute leaps, the linker would have to recount the addresses each time it linked the code.
You will even find that many function calls will generate relative jumps, especially if functions are limited within a single file. This not only speeds up the binding process, but also makes working code smaller and more efficient. This is because relative addresses are usually limited to a much smaller range than absolute addresses, which means that they can be represented in fewer bytes.
Hope this helps!
Adam liss
source share