The StringEndsWith function

The StringEndsWith function checks whether the first string ends with the specified second substring.

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

Return value: ZF = 1 if the first string ends with the substring; ZF = 0 otherwise.

Flag state: The function affects all flags.

Program source code

An example program for the Intel 8086 assembler demonstrating the use of the StringEndsWith function to check the ending of a string.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    string    db "Evgeny Dovgopolov\0"
    mName     db "Evge\0"
    true_msg  db "String '%s' ends with '%s'\0"
    false_msg db "String '%s' does not end with '%s'\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:[mName]      ; Load the address of the variable mName into the es:bx register
        lea     ax, ds:[string]     ; Load the address of the variable string into the ds:ax register
        call    StringEndsWith      ; Call the StringEndsWith procedure (ds:ax, es:bx)

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

    True:
        lea     ax, ds:[mName]      ; Load the address of the variable mName into the ds:ax register
        push    ax                  ; 3rd: Save the ax register value on the stack
        lea     ax, ds:[string]     ; Load the address of the variable string into the ds:ax register
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[true_msg]   ; 1st: Load the address of the true_msg format string into the ds:ax register
        jmp     Done                ; Jump unconditionally to the Done label

    False:
        lea     ax, ds:[mName]      ; Load the address of the variable mName into the ds:ax register
        push    ax                  ; 3rd: Save the ax register value on the stack
        lea     ax, ds:[string]     ; Load the address of the variable string into the ds:ax register
        push    ax                  ; 2nd: save the ax register value on the stack
        lea     ax, ds:[false_msg]  ; 1st: Load the address of the false_msg format string into the ds:ax register

    Done:
        call    PrintFormatString   ; Call the PrintFormatString procedure (ax)
        add     sp, 4               ; Align the stack

        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

String 'Evgeny Dovgopolov' does not end with 'Evge'

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