Reputation: 370
I'm studying for a test in assembly and in the subject of "Position-Independent-Code" I find the difference between a relative jump and an absolute jump confusing. How can I tell what kind of jump it is?
I understand what a relative jump is (the offset from current line). But what does an absolute jump look like? When does it happen?
Upvotes: 6
Views: 10651
Reputation:
Depending on the architecture and on the assembler or mnemonics a relative jump may be not distinguishable from an absolute one.
Some architecture have different mnemonic (the name of the instruction encoded by some machine code) for each branch type, other have the same mnemonic.
Usually it is the assembler that take cares of writing the right jump instruction based on the distance of the target instruction.
Relative addressing is preferred because:
operation_field + 32bit_operand
in 32 bit!About the human factor, we usually are in the state "one way or another, it doesn't care" when programming so we let the assembler choose. Sometime when we write low level routines we may need to move them in memory and force the use of relative jumping. Sometime we want to jump to a fixed location (for example a reset vector at 0000h or 0ffff0h) wherever the code may end up in memory.
Some incomplete example of jumps
beq
,
bne
,
bgtz
,
bgez
,
bltz
,
blez
are all relative jumping
j
, jal
are kind of mixed, they are absolute but the high nibble of PC is kept.
jr
,
jalr
are absolute (being indirect, i.e. using the value of a register).
For more info, see here.
b
, bl
, blx
are relative.
bx
, blx
are absolute.
If you modify the PC directly it is an absolute jump.
Note how the instruction that take immediates are relative while the indirect one are not. This is very common in RISC.
For more info, see here.
jmp
this is either relative or absolute depending on the machine code used. More specifically jumps can be near or far. There is no near absolute direct jump. Absolute near jumps are always indirect (they use memory operand or register).
Far jump are always absolute and can be direct (the address is in the instruction operand) or indirect.
jmp label
is a near jump relative.
jmp [dest]
, jmp eax
are near absolute (indirect) jumps.
jmp 0ffff0:0000h
is far absolute direct.
jump FAR [dest]
is far absolute indirect.
For more info you can see here.
Upvotes: 7
Reputation: 64913
Anything that looks like just plain jmp label
is relative.
Absolute jumps look like
jmp register
jmp [address]
jmp segment:absoluteaddr
jmp far [address]
Any far jump is absolute, any indirect jump is absolute, the combination (far, indirect) is therefore also absolute. Far jump only happen when necessary (you have to change cs
and it's not a call
). Indirect jumps are used for function pointers, branch tables (used in some cases for switch
statements), dynamic dispatch (virtual methods) and maybe for imported functions (usually you call them, but maybe it's a tail call).
Upvotes: 10
Reputation: 5128
from what i recall, relative jump used when the code is positional independent (the code doesn't expect to be loaded into specific memory range. for instance dll library that is dynamically loaded). therefore, all branches in this code cannot assume they know the exact address to jump, but the relative offset between the branch IP and the target IP).
absolute jump get the exact address of the target, and it's used when the code have static address space.
hope it helps,
Upvotes: 0