The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: hutch-- on August 25, 2008, 11:39:05 AM

Title: Fixed array test piece
Post by: hutch-- on August 25, 2008, 11:39:05 AM
I have meant to write this one for a while, differing from the version in the masm32 library, this one only does one GlobalAlloc() call as it writes the pointer array at the beginning of the allocated memory with the data storage area above the pointer array. Its a standard "suicide" type algo, use it correctly or it will go BANG but its both small and fast and this is what I need it for with a future project.


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

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

    fixed_array PROTO :DWORD,:DWORD

    .code

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

    call main
    inkey
    exit

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

acnt equ <1024>

main proc

    LOCAL parr  :DWORD
    LOCAL cntr  :DWORD

    push ebx
    push esi
    push edi

    mov parr, rv(fixed_array,acnt,16)   ; allocate fixed array

    mov esi, parr
    mov edi, acnt
    mov cntr, 0
  @@:
    fn szCopy,str$(cntr),[esi]          ; write a numeric string to each member
    add esi, 4
    add cntr, 1
    sub edi, 1
    jnz @B

    mov esi, parr
    mov edi, acnt

  @@:
    print [esi],13,10                   ; read and display each member
    add esi, 4
    sub edi, 1
    jnz @B

    free parr                           ; free the array memory

    pop edi
    pop esi
    pop ebx

    ret

main endp

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

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

    align 16

fixed_array proc cnt:DWORD,blen:DWORD

    push esi

    add DWORD PTR [esp+8][4], 3     ; align blen up to next 4 byte boundary
    and DWORD PTR [esp+8][4], -4

    mov esi, [esp+4][4]
    add esi, esi                    ; mul cnt by 4
    add esi, esi                    ; store pointer array length in ESI
   
    mov ecx, [esp+4][4]             ; calculate length required
    mov eax, [esp+8][4]
    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][4]
  @@:
    mov [eax], ecx                  ; write each data storage member to pointer array
    add eax, 4                      ; next pointer
    add ecx, [esp+8][4]             ; add blen for next data storage
    sub esi, 1
    jnz @B

    mov eax, edx                    ; return the memory start address
    pop esi

    ret 8

fixed_array endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

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

end start