The FindSubString function

The FindSubString function searches for a substring in a null-terminated string. If the substring is found, a pointer to its first occurrence is returned. If it is not found, the function returns 8000h and sets the CF flag = 1.

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

Return value: AX: pointer to the substring (CF=0) or 8000h (CF=1).

Flag state: The function affects the CF flag.

Program source code (Example 1)

An example program for the Intel 8086 assembler demonstrating the use of the FindSubString function to search for a substring.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    string db "Hello, my world!\0"
    substr db "my\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:[substr]  ; Load the address of the substring variable into the es:bx register
        lea     ax, ds:[string]  ; Load the address of the string variable into the ds:ax register
        call    FindSubString    ; Call the FindSubString procedure (ax:string, bx:substr)
        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

my world!

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

Program source code (Example 2: all occurrences)

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    string db "Hello, my world, my paradise!\0"
    substr db "my\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     ax, ds:[substr]  ; Load the address of the substring variable into the ds:ax register
        call    StringLength     ; Call the StringLength procedure (ax:substr) := AX
        mov     cx, ax           ; Save the ax register value in the cx register

        lea     bx, es:[substr]  ; Load the address of the substring variable into the es:bx register
        lea     ax, ds:[string]  ; Load the address of the string variable into the ds:ax register

    MainLoop:
        call    FindSubString    ; Call the FindSubString procedure (ax:string, bx:substr) := CF
        jc      Done             ; If CF=1, jump to the label Done

        call    WriteString      ; Call the WriteString procedure (ax)
        call    PrintNewLine     ; Call the PrintNewLine procedure
        add     ax, cx           ; Increment the pointer by the length of the substring substr
        jmp     MainLoop         ; Perform an unconditional jump to the label MainLoop

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

my world, my paradise!
my paradise!

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