Assembly code snippets

Back to the snippets overview


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


Function: strtok
Description: Tokenizes a string
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.
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"

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 and search for strtok.


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

    mov     esi, lpszBuffer
    mov     edi, lpTokenBuffer


        xor     ebx, ebx
        xor     ecx, ecx


        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

        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


        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


        inc     ecx
        jmp     __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



strtok ENDP