The MASM Forum

Microsoft 64 bit MASM => Examples => Topic started by: hutch-- on April 30, 2018, 03:17:23 AM

Title: 64 bit version of append algo.
Post by: hutch-- on April 30, 2018, 03:17:23 AM
While I have worked on the text append algo, I thought I may as well tweak the one in the library by unrolling it. I will add it back into the existing library when I am more awake.

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

    include \masm32\include64\masm64rt.inc

    .code

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

entry_point proc

    LOCAL pMem :QWORD
    LOCAL aMem :QWORD
    LOCAL parr :QWORD
    LOCAL .r13 :QWORD
    LOCAL .r14 :QWORD
    LOCAL .r15 :QWORD
    LOCAL cloc :QWORD
    LOCAL plfd :QWORD
    LOCAL bwrt :QWORD

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

    mrm plfd, chr$(13,10,0)                         ; get address of CRLF pair

    mov aMem, alloc(1024*1024)                      ; allocate the output buffer
    mov pMem, loadfile("wordlist.txt")              ; load test file into memory
    mov r13,  rv(ltok,pMem,ADDR parr)               ; tokenise it into a pointer array

    mov cloc, 0                                     ; zero current location pointer

    mov r15, parr                                   ; load the pointer array
    sub r15, 8                                      ; set r15 up for loop
  lbl:
    add r15, 8
    conout QWORD PTR [r15],lf                       ; display the array text
    mov cloc, rv(append,aMem,QWORD PTR [r15],cloc)  ; append array text to output buffer
    mov cloc, rv(append,aMem,plfd,cloc)             ; append a CRLF pair
    sub r13, 1                                      ; decrement the line counter
    jnz lbl                                         ; loop again on non zero

    mov bwrt, savefile("output.txt",aMem,cloc)      ; write the output buffer to disk

    mfree pMem                                      ; release source memory
    mfree parr                                      ; release pointer array memory
    mfree aMem                                      ; release outout buffer memory

    waitkey                                         ; pause app so you can see the results

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

    .exit

entry_point endp

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

NOSTACKFRAME

append proc

  ; ----------------------------------------------------------
  ; rcx = buffer    the main buffer to append extra data to.
  ; rdx = addstr    the byte data to append to the main buffer
  ; r8  = location  current location pointer
  ; ----------------------------------------------------------
    mov r9, rcx             ; buffer
    mov rcx, rdx            ; addstr
    add r9, r8              ; add offset pointer to source address

    mov rax, -1
  ; -----------
  ; unroll by 2
  ; -----------
  @@:
    add rax, 1
    movzx rdx, BYTE PTR [rcx+rax]
    mov [r9+rax], dl
    test rdx, rdx
    jz @F                   ; exit on written terminator

    add rax, 1
    movzx rdx, BYTE PTR [rcx+rax]
    mov [r9+rax], dl
    test rdx, rdx
    jnz @B                  ; exit on written terminator

  @@:
    add rax, r8             ; return updated pointer offset
    ret

append endp

STACKFRAME

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

    end