News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

JL, JG or JB, JA after a CMP ?

Started by Eddy, January 19, 2007, 12:13:34 PM

Previous topic - Next topic

Eddy

Hi All,

After a CMP (Compare) of two unsigned (32 bits) integers, should I use JL (Jump if Less) or JB (Jump if Below) to see which one is the largest of the two.
I know that the terms "above" and "below" are used for unsigned integers but since CMP does a subtraction, the (internal) result can be negative and therefore signed .. This has me confused because how does CMP 'know' that a certain 32 bits memory address is supposed to hold signed or unsigned integers ?

But perhaps it is as simple as:
- use JL, JG when comparing signed integers,
- use JA, JB when comparing unsigned integers,
because CMP actually does 2 compares: one for signed and one for unsigned integers.

I probably have answered my own question now but can someone confirm this?

Regards
Eddy
Eddy
www.devotechs.com -- HIME : Huge Integer Math and Encryption library--

Tedd

Yes :lol

Less/Greater is for signed, and Above/Below for unsigned.

There is only one comparison done (a virtual subtraction), but the result of a subtraction affects more than just the carry flag, and the condition is based on the states of the flags..

above: carry=0 and zero=0
below: carry=1

less: sign=not(overflow)
greater: zero=0 and sign=overflow
No snowflake in an avalanche feels responsible.

Wistrik

To answer your first question, here is some info from the MASM Reference book and a processor reference manual I happened to have open:

For unsigned comparisons:

JB/JNAE (CF = 1)                : Jump if below/not above or equal
JAE/JNB (CF = 0)                : Jump if above or equal/not below
JBE/JNA (CF = 1 or ZF = 1)  : Jump if below or equal/not above
JA/JNBE (CF = 0 and ZF = 0): Jump if above/not below or equal

For signed comparisons:

JL/JNGE (SF <> OF)              : Jump if less/not greater or equal
JGE/JNL (SF = OF)                : Jump if greater or equal/not less
JLE/JNG (ZF = 1 or SF <> OF): Jump if less or equal/not greater
JG/JNLE (ZF = 0 and SF = OF): Jump if greater/not less or equal

(There are several other conditional jump types but they don't apply to your question.)

Compare does a subtraction but doesn't store the result. Instead it merely sets flags according to the result. The flags affected are: CF (carry), SF (sign), ZF (zero), OF (overflow), AF (adjust), and PF (parity). Compare doesn't know if a value is signed or unsigned, and it doesn't care. The value of the high order bit (most significant bit) is always copied into the sign flag (SF) after an arithmetic operation. If you're doing signed arithmetic, you'll care what the sign flag says. Otherwise you won't.

The overflow flag is set whenever the result is too large a positive value or too small a negative value to fit into the destination operand.

Eddy

Thanks guys !    :U
That answers my question !

Kind regards
Eddy
www.devotechs.com -- HIME Huge Integer Math and Encryption library--
 
Eddy
www.devotechs.com -- HIME : Huge Integer Math and Encryption library--