News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

First word algorithm in test piece.

Started by hutch--, September 17, 2014, 12:23:04 PM

Previous topic - Next topic

hutch--

This is a useful algo when parsing lines of text where you need to know what the first word is to determine if you will process the line or not. You could probably tweak a few nanoseconds out of it but it runs OK and appears to be fast enough.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    firstword PROTO ptxt:DWORD,pbuf:DWORD

    .data?
      buffer db 1024 dup (?)    ; 1024 byte buffer
    .data
      buff dd buffer            ; pointer to buffer
      tst1 db "     Hay man, this is a test",0
      tst2 db 0
      tst3 db "       111111111111111111111111111111111111111111111        ",0

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL word1 :DWORD

    print ADDR tst1,13,10               ; display original string
    fn firstword,ADDR tst1,buff         ; get its first word
    mov word1, eax
    print word1,13,10                   ; display the 1st word
    print str$(len(word1)),13,10        ; display its length

    print ADDR tst2,13,10               ; display original string
    fn firstword,ADDR tst2,buff         ; get its first word
    mov word1, eax
    print word1,13,10                   ; display the 1st word
    print str$(len(word1)),13,10        ; display its length

    print ADDR tst3,13,10               ; display original string
    fn firstword,ADDR tst3,buff         ; get its first word
    mov word1, eax
    print word1,13,10                   ; display the 1st word
    print str$(len(word1)),13,10        ; display its length

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

firstword proc ptxt:DWORD,pbuf:DWORD

  ; -----------------------------------
  ; copy first word in text to a buffer
  ; ptxt must be valid address
  ; can be ascii zero
  ; pbuf must point to a large enough
  ; buffer to hold result
  ; -----------------------------------

    push esi

    mov esi, [esp+4][4]     ; ptxt
    sub esi, 1

    mov edx, [esp+8][4]     ; pbuf
    xor ecx, ecx            ; ECX as index

  ; --------------------------------------
  ; trim spaces and tabs from front if any
  ; --------------------------------------
  ftrim:
    add esi, 1
    cmp BYTE PTR [esi], 32
    je ftrim
    cmp BYTE PTR [esi], 9
    je ftrim
    cmp BYTE PTR [esi], 13
    je bye
    cmp BYTE PTR [esi], 0
    je bye

  ; ----------------------------------------
  ; copy characters to buffer until word end
  ; ----------------------------------------
  wcopy:
    mov al, BYTE PTR [esi+ecx]
    mov BYTE PTR [edx+ecx], al
    add ecx, 1
    cmp BYTE PTR [esi+ecx], 33
    jge wcopy

  bye:
    mov BYTE PTR [edx+ecx], 0
    mov eax, [esp+8][4]     ; pbuf

    pop esi

    ret 8

firstword endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start