Search code examples
assemblyx86masm

Checking if the result is 0 without CMP? Assembly x86 MASM


I have the following code:

or edx,edx          (0 Or 0 would equal 0)
jz InvalidDivisor

If i change the code to this:

AND edx,edx            (0 and 0 would equal 0)
jz InvalidDivisor

Wouldnt both ways work for checking to see if the product is 0?


Solution

  • These both work, but test edx, edx does the same and is more idiomatic than either one of them.

    It may also be more efficient because I would guess that either or or and would appear to the CPU as though they possibly modify the value of the register, since or edx, XXX would do so in general, and the CPU probably doesn't check for this special case. This could mean that later instructions using the value of edx have to stall until the or/and finishes executing, whereas with test those later instructions can go ahead and execute out-of-order without waiting for test to finish.