2.2.1 Comparisons
Control structures decide what to do based on comparisons of data. In
assembly, the result of a comparison is stored in the FLAGS register to be
used later. The 80x86 provides the CMP instruction to perform comparisons.
The FLAGS register is set based on the dierence of the two operands of
the CMP instruction. The operands are subtracted and the FLAGS are set
based on the result, but the result is not stored anywhere. If you need the
result use the SUB instead of the CMP instruction.
For unsigned integers, there are two flags (bits in the FLAGS register)
that are important: the zero (ZF) and carry (CF) flags. The zero flag is
set (1) if the resulting dierence would be zero. The carry flag is used as a
borrow flag for subtraction. Consider a comparison like:
cmp vleft, vright
The difference of vleft - vright is computed and the flags are set accordingly.
If the difference of the of CMP is zero, vleft = vright, then ZF is set
(i.e. 1) and the CF is unset (i.e. 0). If vleft > vright, then ZF is unset
and CF is unset (no borrow). If vleft < vright, then ZF is unset and CF
is set (borrow).
For signed integers, there are three flags that are important: the zero
(ZF) flag, the overflow (OF) flag and the sign (SF) flag. The overflow flag is set if the result of an operation overflows (or underflows). The sign flag
is set if the result of an operation is negative. If vleft = vright, the ZF
is set (just as for unsigned integers). If vleft > vright, ZF is unset and
SF = OF. If vleft < vright, ZF is unset and SF 6= OF.
Do not forget that other instructions can also change the FLAGS register,
not just CMP.