Assembly code snippets

Back to the snippets overview

Details

TitleTokenizer
Authorstryker
Submitted by:stryker
Date added:2002-07-10 18:49:13
Date modified:2002-07-10 18:50:15

Comments

Function: strtok
Description: Tokenizes a string
Parameters:
hWnd - Handle to a window. You can remove this if you don't need it.
lpszBuffer - Pointer to a NULL terminated string
lpTokenBuffer - Pointer to the token buffer. The size of the token will depend on the size of this buffer.
Note:
You can customize the code below where the comments are located.

Exclusion Point - Add arguments for characters you want to ignore. E.G. to ignore the character A add this:

cmp dl, "A"
je __RESET_AND_PRINT

You can also ignore a range of characters. Just be careful when implementing one. The other customization is what are you going to do with the tokenized string. Down below are 2 comments for you to customize, at that point edi now contains the tokenized string.

For a working sample program. Go to win32asmcommunity.net and search for strtok.

Snippet

strtok PROC USES ebx esi edi hWnd:DWORD, lpszBuffer:DWORD, lpTokenBuffer:DWORD

    mov     esi, lpszBuffer
    mov     edi, lpTokenBuffer

    __START_TOKENIZER:

        xor     ebx, ebx
        xor     ecx, ecx

    __SCAN:

        mov     dl, BYTE PTR [esi+ecx]
        or      dl, dl
        jz      __EXIT_SCAN

        ;Add argument here. This is the exclusion point.
        ;On this example, this will exclude characters
        ;0Dh and 0Ah, in short, it will ignore the newline
        ;character.

        cmp     dl, 0Dh
        je      __RESET_AND_PRINT
        cmp     dl, 0Ah
        je      __RESET_AND_PRINT

        mov     BYTE PTR [edi+ebx], dl
        inc     ebx
        jmp     __SCAN_FINISHED

    __RESET_AND_PRINT:

        cmp     BYTE PTR [edi], 0
        je      __SCAN_FINISHED

        mov     BYTE PTR [edi+ebx], 0

        push    ecx

        ;Customize The Next 2 Lines.

        mov     eax, hWnd
        invoke  SendMessage, eax, LB_ADDSTRING, NULL, edi

        pop     ecx

        xor     ebx, ebx
        mov     BYTE PTR [edi], bl

    __SCAN_FINISHED:

        inc     ecx
        jmp     __SCAN

    __EXIT_SCAN:

        mov     BYTE PTR [edi+ebx], 0
        cmp     BYTE PTR [edi], 0
        je      __DISCARD_LAST

        ;Customize The Next 2 Lines.

        mov     eax, hWnd
        invoke  SendMessage, eax, LB_ADDSTRING, NULL, edi

    __DISCARD_LAST:

    ret

strtok ENDP