How to implement a conditional jump in asm *without* label?

3

In Visual C's __asm, I'd like to do a jump to a location that's stored in a register, but it doesn't seem to work for conditional jumps, e.g. JAE. Ordinarily this works fine (if you use a label).

lea ecx, 0x0000001f[edx]
        ;jmp ecx        ;ok
        ;jae EXIT_LOOP  ;ok 
        jae ecx     ;   not ok  "improper operand type"

Is there any way to do a jae with a register (or stack) variable with Visual C __asm? Maybe there's a different way to approach this problem (conditionally jump somewhere using a number, not label, known at compile time)?

visual-c++
assembly
asked on Stack Overflow Dec 22, 2012 by T. Webster • edited Dec 27, 2012 by T. Webster

1 Answer

5

Perhaps that's because there is no such opcode on the x86: you can't use a conditional jump except to a label. You probably want to:

...
jb  skip
jmp ecx
skip:...

[Editing to add the label-less version]

This is ugly and sort of defeats one goal of using ASM (namely performance): note that JB (jump below) is equivalent to JC (jump on carry). Let A be the address to jump to if AE and B be the address if B:

...
sbb  eax,eax,0    // propagate carry flag into register; eax == 0 or -1
and  eax,B-A      // eax = 0 or B-A
add  eax,A        // eax = A or B
jmp  eax

You still need to figure out the addresses of where you want to go...

More involved tricks if you want to use other flags. You need to use the lahf or pushf instructions to get the flags into a processable position.

Ugh.

answered on Stack Overflow Dec 22, 2012 by MJZ • edited Dec 29, 2012 by MJZ

User contributions licensed under CC BY-SA 3.0