Author Topic: Fast fixed array algorithm.  (Read 941 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4813
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Fast fixed array algorithm.
« on: February 22, 2015, 12:47:37 PM »
This algo is designed to be easy to use and fast, it accepts an array count and a member length. It returns a pointer array that accesses each array member and when it is no longer required you must release the allocated memory with GlobalFree() or the macro "free".


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

    include \masm32\include\masm32rt.inc
    .686p
    .mmx
    .xmm

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

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

    fixed_alloc PROTO cnt:DWORD,blen:DWORD

    .code

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

    call main
    inkey
    exit

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

main proc

    LOCAL parr   :DWORD
    LOCAL acnt   :DWORD
    LOCAL blen   :DWORD
    LOCAL number :DWORD

    push esi
    push edi

    mov acnt, 8192          ; number of members in array
    mov blen, 128           ; member length of array

  ; -----------------------------------------------------------
  ; parr is the address of a pointer array to each array member
  ; index is zero based. 8192 = 0 to 8191
  ; -----------------------------------------------------------
    mov parr, rv(fixed_alloc,acnt,blen)

  ; ---------------------------
  ; load the array with strings
  ; ---------------------------
    mov esi, parr           ; pointer array in ESI
    mov edi, acnt           ; array count in EDI
    mov number, 0           ; set the start number string to 0

  lbl0:
    cst [esi], ustr$(number)
    add number, 1
    add esi, 4
    sub edi, 1
    jnz lbl0

  ; -------------------
  ; display the strings
  ; -------------------
    mov esi, parr           ; pointer array in ESI
    mov edi, acnt           ; array count in EDI

  lbl1:
    print [esi],13,10
    add esi, 4
    sub edi, 1
    jnz lbl1

    free parr               ; release the allocated memory when finished

    pop edi
    pop esi

    ret

main endp

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

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

    align 16

fixed_alloc proc cnt:DWORD,blen:DWORD

  ; --------------------------------------------------------------------
  ; algorithm produces an array of pointers to an array of members after
  ; the pointer array. On exit the allocated memory needs to be released
  ; uing either GlobalFree() or the macro "free"
  ; --------------------------------------------------------------------
    movd mm0, esi
    movd mm1, edi

    mov edi, [esp+8]

    add edi, 3                      ; align blen up to next 4 byte boundary
    and edi, -4

    mov esi, [esp+4]
    add esi, esi                    ; mul cnt by 4
    add esi, esi                    ; store pointer array length in ESI
   
    mov ecx, [esp+4]                ; calculate length required
    mov eax, edi
    imul ecx                        ; multiply cnt by blen to get data storage size
    add eax, esi                    ; add pointer array length

    push eax
    push GMEM_FIXED or GMEM_ZEROINIT    ; allocate fixed memory
    call GlobalAlloc

    mov edx, eax                    ; store memory address in EDX
    mov ecx, eax
    add ecx, esi                    ; add the length of the pointer array to ECX

    mov esi, [esp+4]
  align 4
  @@:
    mov [eax], ecx                  ; write each data storage member to pointer array
    add eax, 4                      ; next pointer
    add ecx, edi                    ; add blen for next data storage
    sub esi, 1
    jnz @B

    mov eax, edx                    ; return the memory start address

    movd esi, mm0
    movd edi, mm1

    ret 8

fixed_alloc endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

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

end start
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: