Compare Strings

Encoding

EncodingOperand
zoNone

Description

In legacy modes, the CMPS set of instructions subtracts the byte, word, doubleword, or quadword at DS:eSI with the byte, word, doubleword, or quadword ES:eDI. In Long Mode, the same operation is performed, but without the segment register offset. The flags are updated and the result discarded. Afterwards, rSI and rDI are incremented (if EFLAGS.DF is cleared) or decremented (if EFLAGS.DF is set) by the operand size.

For legacy modes, the implied DS segment may be changed with a segment prefix. The ES segment, however, may not be changed.

This set of instructions can be prefixed by a REP prefix for block comparisons.

Operation

// The implied `DS` segment may be changed with an explicit segment override prefix byte

public void CMPSB()
{
    if (MODE == 64)
        _ = Mem8[rSI] - Mem8[rDI];
    else
        _ = Mem8[DS:eSI] - Mem8[ES:eDI]

    rSI += EFLAGS.DF ? -1 : 1;
    rDI += EFLAGS.DF ? -1 : 1;
}

public void CMPSW()
{
    if (MODE == 64)
        _ = Mem16[rSI] - Mem16[rDI];
    else
        _ = Mem16[DS:eSI] - Mem16[ES:eDI]

    rSI += EFLAGS.DF ? -2 : 2;
    rDI += EFLAGS.DF ? -2 : 2;
}

public void CMPSD()
{
    if (MODE == 64)
        _ = Mem32[rSI] - Mem32[rDI];
    else
        _ = Mem32[DS:eSI] - Mem32[ES:eDI]

    rSI += EFLAGS.DF ? -4 : 4;
    rDI += EFLAGS.DF ? -4 : 4;
}

public void CMPSQ()
{
    _ = Mem64[rSI] - Mem64[rDI];

    rSI += EFLAGS.DF ? -8 : 8;
    rDI += EFLAGS.DF ? -8 : 8;
}

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.