News:

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

Main Menu

New algo for counting lines.

Started by hutch--, July 15, 2022, 11:40:30 AM

Previous topic - Next topic

hutch--

Needed this for a test piece I am working on. Will probably do 2 different versions, one for ascii 10 and another for ascii 13.

; «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»

NOSTACKFRAME

get_line_count proc

    sub rcx, 1                      ; set up address for following loop
    xor rax, rax                    ; zero the line counter
  @@:
    add rcx, 1
    cmp BYTE PTR [rcx], 0
    je @F                           ; exit on zero
    cmp BYTE PTR [rcx], 13
    jne @B
    add rax, 1                      ; increment counter if line end
    jmp @B
  @@:
    ret                             ; return line count in rax

get_line_count endp

NOSTACKFRAME

; «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»

hutch--

This is the test piece. I had to track down a really weird problem, when I use szCopy to copy on buffer to another, it worked OK but any further calls to GlobalFree() (mfree macro) would instantly GP fault. Did not directly make sense so after wasting too much time on it, I changed the szCopy to bcopy and BINGO, it worked correctly.

The loadfile macro calls a library module that will load any file, binary or character, into a buffer of the correct size. Seems it must be used with a BYTE copy that specifies the length rather than relying on a terminating ascii zero.

; «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»

    include \masm32\include64\masm64rt.inc

    .code

; «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»

entry_point proc

    LOCAL pMem  :QWORD                                      ; source memory pointer
    LOCAL flen  :QWORD                                      ; file length of source
    LOCAL hMem  :QWORD                                      ; extra buffer pointer
    LOCAL lcnt  :QWORD                                      ; line count of source
    LOCAL nlen  :QWORD                                      ; new length of modified buffer

    mov pMem, loadfile("win64.inc")                         ; load source file
    mov flen, rcx                                           ; get the returned file length

    conout "  File Length = ",str$(flen),lf

    mov hMem, alloc(flen)                                   ; allocate a buffer the same size as pMem

    rcall bcopy,pMem,hMem,flen                              ; copy pMem to hMem at flen byte count
    conout "  Bytes copied to hMem = ",str$(flen),lf

    mov lcnt, rvcall(get_line_count,hMem)                   ; get the line count
    conout "  Line count in hMem = ",str$(lcnt),lf

    rcall szRemove,hMem,pMem,chr$(10)                       ; strip the ascii 10s
    mov nlen, len(pMem)                                     ; get the new length
    conout "  New length in pMem = ",str$(nlen),lf

    mfree pMem                                              ; free both memory blocks
    mfree hMem

    conout chr$(13,10)
    waitkey "  Thats all folks, press a key ...."

    .exit

entry_point endp

; «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»

NOSTACKFRAME

get_line_count proc

    sub rcx, 1                      ; set up address for following loop
    xor rax, rax                    ; zero the line counter
  @@:
    add rcx, 1
    cmp BYTE PTR [rcx], 0
    je @F                           ; exit on zero
    cmp BYTE PTR [rcx], 13
    jne @B
    add rax, 1                      ; increment counter if line end
    jmp @B
  @@:
    ret                             ; return line count in rax

get_line_count endp

NOSTACKFRAME

; «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»

    end