The CMP instruction

The CMP (Compare) instruction is used to compare the contents of the accumulator with the specified operand. It conceptually performs a subtraction but does not store the result; instead, it updates the flag bits depending on the result of the comparison.

The CMP instruction format

InstructionDescription
CMP reg8 This format is used to compare reg8 with the value in register A. Here reg8 can be any general-purpose register, for example A, B, C, D, E, H or L.
CMP mem8 This format is used to compare the value in the memory location pointed to by the contents of the HL register pair with the value in register A.

Usage example

; Author: Evgeny Sergeevich Dovgopolov

num1: db 45
num2: db 40

main:
    mvi     a, 14h   ; Load the hexadecimal value 14h into register A
    mvi     b, 15h   ; Load the hexadecimal value 15h into register B
    cmp     b        ; Compare the value in register B with the value in register A
    mvi     a, num1  ; Load the value of the variable num1 into register A
    lxi     h, num2  ; Load the address of the variable num2 into the HL register pair
    cmp     m        ; Compare the value in the memory location with the value in register A

    hlt              ; Stop program execution

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

Processor status flags

S
Yes
Z
Yes
AC
Yes
P
Yes
CY
Yes