3

Can someone explain in English what a relative jump is in assembler?

starblue
  • 55,348
  • 14
  • 97
  • 151
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415

4 Answers4

10

It is an OP code whose operand will cause execution to jump to an address relative to the current address. The value of the operand is an offset.

Suppose the relative jump instruction is held in address 0x0005 and has an operand of 3. Then execution will jump to address 0x0008. If the operand is -3, execution will jump to address 0x0002.

Assad Ebrahim
  • 6,234
  • 8
  • 42
  • 68
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    x86 jumps are relative to the *end* of the instruction. So if the 2-byte instruction `EB 03` *ends* at `0x0005`, then the jump target is `0x0008`. https://www.felixcloutier.com/x86/jmp – Peter Cordes Nov 06 '22 at 00:51
5

A relative jump differs from an absolute or long jump in that the instruction does not encode the entire target address to where execution will continue.

Rather, it encodes a portion of the address or an offset from the current instruction pointer, depending on the architecture. This saves program memory space and in non-pipelined architectures it will also execute slightly faster. The limitation is that you can only jump shorter distances.

For current instruction relative jumps, the range is often a signed offset of the storage size used to encode the address. For example, if an 8-bit opcode and 8-bit offset is used, then the jump range would be typically -126 to +129 from the jump instruction. The asymmetry is due to the fact that the offset is added to the instruction pointer, which at the time of the addition points to the next instruction instead of the current one.

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • Relative jumps don't *have* to be limited range. For example, 32-bit x86 can `jmp rel32` or `call rel32` anywhere in its 32-bit virtual address space. The other advantage of being relative is that code is position independent. x86's short jump, `jmp rel8`, does save code-size. [-128..+127] relative to the *end* of the instruction. So yes, [-126..+129] if you measure from the start unlike how the machine encoding works. (https://www.felixcloutier.com/x86/jmp). (All of these are x86 "near" jumps, not changing the code segment selector. All x86's far jumps are absolute.) – Peter Cordes Nov 05 '22 at 23:24
  • Re: x86 absolute jumps: [Call an absolute pointer in x86 machine code](https://stackoverflow.com/q/19552158) - there is no absolute direct near jump, usually you want `mov rax, 0x123456789a` / `jmp rax`. – Peter Cordes Nov 06 '22 at 00:50
1

Jump to address relative to (with respect to) the current address (address of this relative jump instruction).

You need to specify the "by how much?" part in the operand.

claws
  • 52,236
  • 58
  • 146
  • 195
0

Instead of executing the next instruction, it increments the code cursor by the value given to it. Then the execution continues

Ohnemichel
  • 324
  • 2
  • 9