The CMPSB and CMPSW instructions

Compare memory with memory.

The CMPSB/CMPSW instruction format

InstructionDescription
CMPSBCompare the byte at DS:SI with the byte at ES:DI.
CMPSWCompare the word at DS:SI with the word at ES:DI.

Compares the contents of the memory location addressed by the si register, with the contents of the memory location addressed by the di register. The comparison is performed by subtracting the contents of the memory location addressed by the di register from the contents of the memory location addressed by the si register, and using the result to set the flags. Neither of the memory locations used in the subtraction is affected. The registers si and di are incremented/decremented depending on the value of the DF flag. An 8- or 16-bit operation can be specified.

The CF, OF, SF, ZF, AF and PF flags are set according to the temporary result of the comparison.

Usage example: finding the first matching element in two arrays

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    aArray  dw  6, 4, 7, 4, 5, 4, 5, 9, 8
    aArrlen equ ($-aArray) / 2
    bArray  dw  4, 3, 4, 2, 3, 3, 5, 5, 6
    bArrlen equ aArrlen
data ends

code segment public              ; Start of the code segment
    assume ds:data, cs:code

    main proc                    ; Start of the main procedure
        mov     ax, seg data     ; Get the data segment address and store it in the ax register
        mov     ds, ax           ; Pass the data segment address from ax to ds
        mov     es, ax           ; Pass the data segment address from ax to es

        mov     cx, aArrlen      ; Set the counter equal to the length of the aArray array
        lea     di, es:[bArray]  ; Load the address of the bArray variable into es:di
        lea     si, ds:[aArray]  ; Load the address of the aArray variable into ds:si
        mov     ax, cx           ; Save the length of the aArray array in the ax register

  repnz cmpsw                    ; Compare the element at ds:si with es:di while CX≠0 or the ZF flag≠0
        jnz     NotEqual         ; If ZF=0, jump to the NotEqual label

        sub     ax, cx           ; Subtract the state of the cx register from the array length
        dec     ax               ; Decrease ax by 1 so that it points to the found element
        jmp     Done             ; Jump unconditionally to the Done label

    NotEqual:
        mov     ax, 0FFFFh       ; Copy the value -1 into the ax register

    Done:
        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

Note: the DF flag is not changed by the CMPSB/CMPSW instructions, but it affects the direction of processing (increment/decrement of the indices).