Author Topic: Fast word count algo  (Read 2411 times)

0 Members and 1 Guest are viewing this topic.

Offline Steve Hutchesson

  • Moderator
  • Jr. Member
  • *****
  • Posts: 83
    • The MASM Forum
Fast word count algo
« on: December 21, 2009, 11:37:21 AM »
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.

Code: [Select]
' §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§

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

' §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§