1

I am trying to avoid register flag dependency in my loop and I consider is there any compare instruction for integer which set just CF or OF flag. And only that flags. The important fact is: Is one operand greater or not so the one bit is sufficient.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

0

No, there isn't. What exactly are you trying to avoid? All CPUs (except P4) rename different parts of EFLAGS separately, so inc doesn't have a false dependency on the old value of EFLAGS. Usually when you want to loop without modifying a flag, it's CF that you're trying to preserve. (For an adc loop, or shifting bits out of a register into CF one at a time.)

You can loop without affecting flags at all with lea and jrcxz, but that's not worth it for an adc loop on Intel SnB and later, where partial-flag merging is fast.


But if you need an actual compare, not just a count-down (like the slow loop instruction), you could do it like this:

### Don't actually do this.  This is just to illustrate how bad the best option is
# A in eax,  B in ebx
movd      xmm0, eax
movd      xmm1, ebx
pcmpgtqd  xmm0, xmm1    ; all-ones if eax>ebx (32bit signed compare), else all-0
movd      ecx
jrcxz   A_ngt_B
;  fall-through path: A > B,  ecx = -1

This sucks a lot, compared to other options. It's probably worse than simply saving/restoring flags with sahf/lahf, or the even-slower pushf/popf. Often you can "save" a flag with a setcc dl or something, and then some kind of test or sub dl, 1 to generate a carry or not.

To accomplish exactly what the question asked for (conditionally setting just CF without affecting others), use that branch on jrcxz to conditionally run stc or clc to set or clear CF.


See also the tag wiki.

Community
  • 1
  • 1
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847