Question

I believe the standard way of implementing jumps in x86 is to do your comparison with something like:

cmpl %ebx, %eax

Which stores the result in the eflags register, and then the following jump command uses that eflags register:

je some_label

But what if I want to save the result of the comparison to do the jump later? I thought about doing something like:

    cmpl %eax, %ebx
    je storetrue
storefalse:
    pushl $0
    jmp done
storetrue:
    pushl $1
done:
    ...

Do I need to do it this way or is there a more direct way that doesn't require the extra jumps?

Was it helpful?

Solution

A few methods come to mind.

  1. As has already been mentioned in the question comments, you can use the conditional move instruction.
  2. You can use the SETxx instruction to set one of the 8-bit registers to 0 or 1.
  3. You can use the PUSHF instruction to save a copy of the EFLAGS register onto the stack. It can be restored using the POPF instruction.
  4. You can craft your code so that it does not use instructions that modify the EFLAGS register for the next time you wish to use the results of that comparison.
  5. Depending upon your use cases, you may be able to craft a simple arithmetic expression that will result in either 0 or 1 or some other desired value (personally I prefer the combination of 0 or -1 as it then becomes trivial to convert that to any X or Y pair using an AND followed by an ADD).

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top