Last modified: Wed Dec 10 22:40:08 UTC+0100 2025 © A. Tarpai
SIGNED/UNSIGNED CMP followed by Jcc
CMP (Compare) subtracts the source from the destination. The operands are unchanged, but the flags are updated. CMP updates CF, OF, SF, ZF (and AF, PF).
CMP dst, src = SUB dst, src = dst - src
Mnemonics are made easy to understand the condition for the jump. F. ex. JG makes the jump if dst greater (signed larger) than src. As if CMP is coded with the condition:
CMP dst > src CMP dst >= src CMP dst >= src JG jump.. JGE jump.. JAE jump.. signed signed unsigned
Mnemonics, opcodes and <, >, <=, >= condition tests:
UNSIGNED CMP SIGNED CMP
+-------------------+-------------------+ +-------------------+-------------------+
| | | | | |
| CMP dst < src | CMP dst > src | | CMP dst < src | CMP dst > src |
| JB | JA | | JL | JG |
| JNAE | JNBE | | JNGE | JNLE |
| | | | | |
+-------------------+-------------------+ +-------------------+-------------------+
| | | | | |
| CMP dst <= src | CMP dst >= src | | CMP dst <= src | CMP dst >= src |
| JBE | JAE | | JLE | JGE |
| JNA | JNB | | JNG | JNL |
| | | | | |
+-------------------+-------------------+ +-------------------+-------------------+
UNSIGNED POS COND UNSIGNED NEG COND OPCODE FLAGS CONDITION
B Below NAE Neither above nor equal 0010 CF = 1
AE Above or equal NB Not below 0011 CF = 0
BE Below or equal NA Not above 0110 CF or ZF = 1
A Above NBE Neither below nor equal 0111 CF or ZF = 0
SIGNED POS COND SIGNED NEG COND OPCODE FLAGS CONDITION
L Less NGE Neither greater nor equal 1100 LE = 1
GE Greater or equal NL Not less 1101 LE = 0
LE Less or equal NG Not greater 1110 LE or ZF = 1
G Greater NLE Neither less nor equal 1111 LE or ZF = 0
Where LE is the signed LESS condition: SF xor OF following subtraction.
Note for signed/unsigned CMP performs the same operation because the ALU operation is the same. It is the Jcc instructions that test for different FLAG bits combinations. Therefore different set of opcodes.
- The terms "above" and "below" refer to the relation between two unsigned values: CF/ZF is tested
- The terms "greater" and "less" refer to the relation between two signed values: SF/OF/ZF is tested.
(Greater i.e. more positive; Less i.e. less positive (more negative) signed values)
There are also opposite condition mnemonics for the same opcode if it makes more sense for the programmer.
CMP: understanding flag conditions
There is only one ALU, one CMP operation, a subtraction, which sets the FLAGS. The comparison reflected in the flags is that of the destination to the source.
It is the Jcc instr that tests different flags and flag-combinations to determine if the condition is true or false.
UNSIGNED CMP
The subtraction either sets CY or not.
Unsigned Below < and Above or equal >=
Tests Carry only. The subtraction dst - src sets Carry when src > dst.
CMP dst < src CF = 1 JB CMP dst >= src CF = 0 JAE
Above > and Below or equal <=
Tests Carry and Zero Flag. The OR-table indeed gives correct condition result:
CMP dst > src CF OR ZF = 0 => CF=0 AND ZF=0 (boolean)
CMP dst <= src CF OR ZF = 1
CF CF
0 1 0 1
+-----+ +-----+ +-----+ +-----+
0 | 0 | | 1 | 0 | > | | < |
ZF +-----+ | | ZF +-----+ +-----+
+-------+ | +-----+
1 | 1 1 | 1 | = | N/A
+-------------+ +-----+
OR-table
CF ZF CF or ZF A - B
0 0 0 Neither CF or ZF set: A is Above B (Neither below nor equal)
0 1 1 Equal A = B
1 0 1 Below A < B
1 1 1 cannot occur after ALU subtraction
* a zero subtraction result does not produce carry
SIGNED CMP
CMP dst < src FLAG? = 1
IF we had a flag.. similar to CF, which is set, when dst is less, than src, but for a signed-comparison. Then other signed conditions could be derived from it very similar to unsigned comparison.
I've written a whole book about this why and how (SF xor OF) or ZF = 1 works:
- First, we have to understand two's complement interpretation.
- Then, we have to understand the signed Overflow Flag (OF) of the ALU adder.
- Then, how OF with SF set after subtraction.
- Then, understand how LESS is correct.