The CMP instruction

Comparison.

The CMP instruction format

InstructionDescription
CMP AL, imm8Compare imm8 with AL.
CMP AX, imm16Compare imm16 with AX.
CMP reg/mem8, imm8Compare imm8 with reg/mem8.
CMP reg/mem16, imm16Compare imm16 with reg/mem16.
CMP reg/mem16, imm8Compare imm8 with reg/mem16.
CMP reg/mem8, reg8Compare reg8 with reg/mem8.
CMP reg/mem16, reg16Compare reg16 with reg/mem16.
CMP reg8, reg/mem8Compare reg/mem8 with reg8.
CMP reg16, reg/mem16Compare reg/mem16 with reg16.

Compares the first source operand with the second source operand and sets the status flags in the FLAGS register according to the results. The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same way as in the sub instruction. When an immediate value is used as an operand, it is sign-extended to the length of the first operand.

The condition codes used by the Jcc instructions are based on the results of the cmp instruction.

Usage example

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

code segment public         ; Start of the code segment
    assume cs:code

    main proc               ; Start of the main procedure
        mov     al, 0AAh    ; Copy the value 170h into the al register
        mov     bl, 0AAh    ; Copy the value 170h into the bl register
        cmp     al, bl      ; Compare al with bl

        mov     ax, 0DDFFh  ; Copy the value 56831 into the ax register
        mov     bx, 0DBFFh  ; Copy the value 56319 into the bx register
        cmp     ax, bx      ; Compare ax with bx

        mov     ax, 4C00h   ; Copy the process termination function number into ah and the value into al
        int     21h         ; Call the interrupt
    main endp               ; End of the main procedure

code ends                   ; End of the code segment
end main                    ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Processor status flags

CF
Yes
PF
Yes
AF
Yes
ZF
Yes
SF
Yes
TF
No
IF
No
DF
No
OF
Yes