StringStartsWithEx function

The StringStartsWithEx function checks whether the first string starts with the specified substring. The string pointers are passed through the stack. The function automatically aligns the stack to 4 bytes.

Parameters (stack): 1. Pointer to the first string. 2. Pointer to the second string (substring).

Return value: ZF = 1 if it starts with; ZF = 0 if not.

Flag state: The function affects all flags.

Program source code

Intel 8086 assembly program example demonstrating the use of the StringStartsWithEx function to check the beginning of a string.

title Author: Evgeny Sergeevich Dovgopolov

.286C
.model small
.stack 100h

data segment public
    string    db "Evgeny Dovgopolov\0"
    mName     db "Evgen\0"
    msg_true  db "String '%s' starts with '%s'\0"
    msg_false db "String '%s' does not start with '%s'\0"
data ends

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

    main proc
        call    SetDataSegment      ; Call the SetDataSegment procedure

        pushs   es:[mName]          ; 2nd: Push the address of variable mName onto the stack
        pushs   ds:[string]         ; 1st: Push the address of variable string onto the stack
        call    StringStartsWithEx  ; Call procedure StringStartsWithEx(string, mName)

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

        pushs   ds:[mName]          ; 3rd: Push the address of variable mName onto the stack
        pushs   ds:[string]         ; 2nd: Push the address of variable string onto the stack
        leaax   ds:[msg_true]       ; 1st: Load the address of the msg_true format string into ds:ax
        jmp     @Done               ; Unconditionally jump to the @Done label

    @False:
        pushs   ds:[mName]          ; 3rd: Push the address of variable mName onto the stack
        pushs   ds:[string]         ; 2nd: Push the address of variable string onto the stack
        leaax   ds:[msg_false]      ; 1st: Load the address of the msg_false format string into ds:ax

    @Done:
        call    PrintFormatString   ; Call the PrintFormatString procedure
        add     sp, 4               ; Align the stack

        push    0                   ; Push the byte return code onto the top of the stack
        call    ExitProcessEx       ; Call the ExitProcessEx(0) procedure
    main endp

code ends
end main

String 'Evgeny Dovgopolov' starts with 'Evgen'

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