News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Simple line parser algorithm.

Started by hutch--, July 15, 2017, 12:46:33 AM

Previous topic - Next topic

hutch--

I needed a simple algorithm to parse command lines for multiple options/filenames etc .... ZIP file is attached. This is the source.


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

    include \masm32\include64\masm64rt.inc

    .data
      v1 db "   one",0
      v2 db "  one  two  ",0
      v3 db " one    two  three  four       five six ",0

      tptr dq v3

    .code

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

entry_point proc

    LOCAL aptr      :QWORD              ; array pointer
    LOCAL acnt      :QWORD              ; array count
    LOCAL .r14      :QWORD
    LOCAL .r15      :QWORD

    mov .r14, r14                       ; preserve non volatile registers
    mov .r15, r15

    mov aptr, alloc(4096*8)             ; <<<< This is [4096 x 8]

    mov acnt, rv(cltok,tptr,aptr)       ; tokenise text

    conout "array count = ",str$(acnt),lf,lf

  ; ----------------------------------
  ; use non volatile registers in loop
  ; ----------------------------------
    mov r15, aptr                       ; array address in r15
    mov r14, acnt                       ; array count in r14
    sub r15, 8                          ; prep address for following loop
  @@:
    add r15, 8                          ; add 8 to get next address
  ; *************************
    conout QWORD PTR [r15],lf           ; output text
  ; *************************
    sub r14, 1                          ; decrement the counter
    jnz @B                              ; loop back if not zero
  ; ----------------------------------

    mov r14, .r14                       ; restore non volatile registers
    mov r15, .r15

    conout lf

    waitkey                             ; stop to see results

    mfree aptr                          ; release the pointer array memory

    .exit

entry_point endp

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

cltok proc ptxt:QWORD,parr:QWORD
  ; -------------------------------------
  ; tokenise space delimiter single words
  ; -------------------------------------
  ; ptxt = address of words to tokenise
  ; parr = start address of QWORD pointer array
  ;
  ; return value = number of tokens in text
  ;
  ; NOTE : Do not use this algo with word pairs
  ;        that has spaces as it will treat each
  ;        word after each space as another token
  ; -------------------------------------
    invoke szMono,ptxt                  ; monospace text and trim both ends

    mov r11, ptxt                       ; load text address
    xor r10, r10                        ; zero the space counter
    mov r9,  parr                       ; load pointer array
    mov QWORD PTR [r9], r11             ; write first pointer
    add r9, 8                           ; increment to next pointer location

    sub r11, 1
  lbl0:
    add r11, 1                          ; increment byte after each inneration
  backin:
    movzx rax, BYTE PTR [r11]
    cmp rax, 32
    jne @F                              ; jump forward if not space
    add r10, 1                          ; count spaces
    mov BYTE PTR [r11], 0               ; replace it with a zero
    add r11, 1                          ; increment to next byte after last terminator
    mov QWORD PTR [r9], r11             ; write next pointer
    add r9, 8                           ; increment to next pointer location
    jmp backin
  @@:
    test rax, rax                       ; exit loop on zero
    jnz lbl0                            ; else loop back

    mov rax, r10                        ; write count to rax
    add rax, 1                          ; add one for last arg

    ret

cltok endp

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

    end