• Welcome to Jose's Read Only Forum 2023.
 

Fast word count algo

Started by Steve Hutchesson, December 21, 2009, 11:37:21 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Steve Hutchesson

This algo does much the same thing as the standard TALLY$ function in PB which is a fast and reliable internal function. This one has a minor speed advantage over the internal version on the quad I develop on but the real reason for the algo is as a base to write extra capacity into.

Here is the algo.


' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION word_count(src$,ptn$) as DWORD

    #REGISTER NONE

    LOCAL psrc as DWORD
    LOCAL ptxt as DWORD
    LOCAL wcnt as DWORD

    psrc = StrPtr(src$)
    ptxt = StrPtr(ptn$)

    ! mov esi, psrc                     ' source in ESI
    ! mov edx, [esi-4]                  ' get src$ length
    ! mov edi, ptxt                     ' pattern in EDI
    ! sub edx, [edi-4]                  ' sub patn length from src length

    ! add edx, esi                      ' add src to exit position
    ! add edx, 1                        ' correct to get last word
    ! sub esi, 1

    ! push ebp
    ! or ebp, -1

  pre:
    ! add ebp, 1
  ppre:
    ! movzx eax, BYTE PTR [edi]

  wcst:
    ! add esi, 1
    ! cmp edx, esi                      ' test for exit condition
    ! jle wcout
    ! movzx ebx, BYTE PTR [esi]         ' load byte at ESI
    ! cmp eax, ebx                      ' test for 1st character in patn
    ! je ptw

    ! add esi, 1
    ! cmp edx, esi                      ' test for exit condition
    ! jle wcout
    ! movzx ebx, BYTE PTR [esi]         ' load byte at ESI
    ! cmp eax, ebx                      ' test for 1st character in patn
    ! jne wcst

  ptw:
    ! xor ecx, ecx
  test_word:
    ! add ecx, 1
    ! movzx eax, BYTE PTR [edi+ecx]
    ! test eax, eax
    ! je pre                            ' jump back and increment counter
    ! movzx ebx, BYTE PTR [esi+ecx]     ' load byte at ESI
    ! cmp eax, ebx
    ! jne ppre

    ! add ecx, 1
    ! movzx eax, BYTE PTR [edi+ecx]
    ! test eax, eax
    ! je pre                            ' jump back and increment counter
    ! movzx ebx, BYTE PTR [esi+ecx]     ' load byte at ESI
    ! cmp eax, ebx
    ! je test_word

    ! jmp ppre

  wcout:
    ! mov eax, ebp
    ! pop ebp

    ! mov FUNCTION, eax

END FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤