The StringSplit function

The StringSplit function splits a string into tokens using delimiters and stores them in a buffer prepared by the user. Each token in the buffer is null-terminated.

Parameters: DS:AX - pointer to the string; ES:BX - pointer to the delimiter string; DS:CX - pointer to the buffer for the tokens.

Return value: CX - number of tokens stored in the buffer.

Flag state: The function does not modify the flags.

Program source code

An example program for the Intel 8086 assembler demonstrating the use of the StringSplit function to split a string into tokens by delimiters.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    string db "Spring wind, caressing leaves, whispers secrets, giving aroma of gardens.\0"
    buffer db 80 dup(0)
    delims db " ,.\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     cx, ds:[buffer]  ; Load the address of the variable buffer into the ds:cx register
        lea     bx, es:[delims]  ; Load the address of the variable delims into the es:bx register
        lea     ax, ds:[string]  ; Load the address of the variable string into the ds:ax register
        call    StringSplit      ; Call the StringSplit procedure (ds:ax, es:bx, ds:cx) := CX

        lea     si, ds:[buffer]  ; Load the address of the variable buffer into the ds:si register

    MainLoop:
        lodsb                    ; Load a byte from the source register si into the al register

        test    al, al           ; Logically compare the value in the al register with 0
        jz      Skip             ; If ZF=1, jump to the Skip label

        call    PutChar          ; Call the PutChar procedure (al)
        jmp     MainLoop         ; Jump unconditionally to the MainLoop label

    Skip:
        call    PrintNewLine     ; Call the PrintNewLine procedure
        loop    MainLoop         ; Loop while CX is not equal to 0

        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

Spring
wind
caressing
leaves
whispers
secrets
giving
aroma
of
gardens

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