Compare

Encoding

EncodingOperand 1Operand 2
mrModRM.r/m[r]ModRM.reg[r]
rmModRM.reg[r]ModRM.r/m[r]
aiAL/AX/EAX/RAXimm8/16/32
miModRM.r/m[r]imm8/16/32

Description

The CMP instruction subtracts the second source operand from the first. The flags are updated and the result discarded.

This instruction internally operates the same as SUB, but without writing the result back. As such, the CMP instruction is the only one of the eight original ALU instructions that cannot be used with the LOCK prefix.

Operation

// `src2` is sign extended to the width of `src1`

public void CMP(ref U8 src1, U8 src2)
{
    _ = src1 - src2;
}
public void CMP(ref U16 src1, U16 src2)
{
    _ = src1 - src2;
}
public void CMP(ref U32 src1, U32 src2)
{
    _ = src1 - src2;
}
public void ADD(ref U64 src1, U64 src2)
{
    _ = src1 - src2;
}

Flags Affected

CF (carry flag)
Set according to the temporary result.
PF (parity flag)
Set according to the temporary result.
AF (auxiliary flag)
Set according to the temporary result.
ZF (zero flag)
Set according to the temporary result.
SF (sign flag)
Set according to the temporary result.
OF (overflow flag)
Set according to the temporary result.

Exceptions

Real-Address Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand (using a segment other than
  • SS
  • ) has an effective address that is outside the segment's limit.

Virtual-8086 Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand (using a segment other than
  • SS
  • ) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Protected Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If the destination is located in a non-writable segment.
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Compatibility Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If the destination is located in a non-writable segment.
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Long Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the SS segment is in non-canonical form.
  • If a memory operand using the SS segment has an effective address that is outside the SS segment's limit.
#GP(0)
  • If a memory operand (using a segment other than SS) is in non-canonical form.
  • If the destination is located in a non-writable segment.
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.