The StringCompare function

The StringCompare function performs a lexicographic comparison of two null-terminated (\0) strings.

Parameters: DS:AX - pointer to the first string; ES:BX - pointer to the second string.

Return value: AX = 0 and ZF=1 if the strings are equal. AX = 1 or -1 and ZF=0 if the strings are not equal.

Flag state: The function affects all flags, including ZF.

Program source code

An example program for the Intel 8086 assembler demonstrating the use of the StringCompare function for lexicographic comparison of strings.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    string1  db "Evgeny\0"
    string2  db "Dovgopolov\0"
    equal    db "The strings are equal!\0"
    notEqual db "The strings are not equal!\0"
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        lea     bx, es:[string2]   ; Load the address of the variable string2 into the es:bx register
        lea     ax, ds:[string1]   ; Load the address of the variable string1 into the ds:ax register
        call    StringCompare      ; Call the StringCompare procedure (ds:ax, es:bx) := AX and ZF

        jne     StringsNotEqual    ; If ZF=0, jump to the label StringsNotEqual

        lea     ax, ds:[equal]     ; Load the address of the variable equal into the ds:ax register
        jmp     Done               ; Perform an unconditional jump to the label Done

    StringsNotEqual:
        lea     ax, ds:[notEqual]  ; Load the address of the variable notEqual into the ds:ax register

    Done:
        call    WriteString        ; Call the WriteString procedure (ax)

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    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

The strings are not equal!

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