Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative vs Absolute jmp in Assembly

Tags:

x86

assembly

I was wondering about the following. I've noticed when writing assembly language that IA32 is designed in such a way as to promote the use of relative jumps i.e. jump a displacement amount of bytes vs the use of absolute jumps i.e. change eip to a specific address in memory. What is the logic behind this?

like image 487
themaestro Avatar asked Jan 15 '11 03:01

themaestro


People also ask

What does jmp mean in assembly?

In the x86 assembly language, the JMP instruction performs an unconditional jump. Such an instruction transfers the flow of execution by changing the program counter.

What is a relative jump?

A relative jump jumps to an address specified relative to the current instruction pointer. The $E9 opcode is a relative jump with a 32 bit offset. Note that there are different jump opcodes for jumps with differently sized offsets.

What is an absolute jump?

absolute jump get the exact address of the target, and it's used when the code have static address space.

What is the difference between near jump and far jump?

Short jump—A near jump where the jump range is limited to –128 to +127 from the current EIP value. Far jump—A jump to an instruction located in a different segment than the current code segment but at the same privilege level, sometimes referred to as an intersegment jump.


1 Answers

Most jumps are to targets not far away from the jump instruction. Since jump instructions are provided that take a signed 16-bit value, they can be fewer bytes than needed for an absolute jump (usually 4 bytes plus the instruction itself).

One small additional advantage of relative branches is that they don't need to be fixed up in the linker, or, for that matter, go through the extra indirection needed in PIC (position independent code).

like image 71
Raph Levien Avatar answered Sep 21 '22 15:09

Raph Levien