How to jump to an address saved in a register in intel assembly?

2.2k Views Asked by At

If I calculate an address of a label and store it in the eax register, how can I conditionally jump (using JE) to eax?

jmp eax

compiles, but I didn't check if it works.

je eax

doesn't compile (invalid combination of opcode and operands). Why the difference? And how can I jump if equal to eax?

1

There are 1 best solutions below

0
On BEST ANSWER

There simply is no such form of je. What you can do is put a relative conditional jump based on the opposite condition, followed by an unconditional register-indirect jump:

jne skip
jmp eax
skip:

You could make a macro out of this to save you from writing the same thing over and over. For example, in NASM syntax that macro could look like this:

%macro je_reg 1 
    jne %%skip 
    jmp %1 
    %%skip: 
%endmacro

And could be used like this:

je_reg eax
je_reg ebx

The macro could be generalized to work with any condition code:

%macro jcc_reg 2 
    j%-1 %%skip   ; %-1 expands to the inverse of the condition in the first macro argument
    jmp %2 
    %%skip: 
%endmacro

; Example usage
jcc_reg e,eax
jcc_reg no,ebx