Search code examples
assemblyx86-64low-level

How can I jump to a relative address, using an offset stored in a register?


Intel/AMD says that this:

mov rax, 0xabc
jmp rax

is not equivalent to this:

jmp 0xabc

Since the first assumes absolute jumps because of the register, and the second assumes relative jumps. My question is, what if I wanted to do a relative jump where the offset is stored in a register such as rdi?

I searched other answers in SO, however they weren't so enlightening.


Solution

  • You would have to add the base address to the offset to form an absolute address in the register and then jump to that.

    For example

        ; jump table contains offsets relative to jump_base
        ; index into jump table is in rax
        lea rcx, [rel jump_table]
        mov ecx, [rcx+rax*4]
        lea rax, [rel jump_base]
        add rax, rcx
        jmp rax
    jump_base: