Title Heap Test By Kip Irvine, July 2003 (heaptest.asm) INCLUDE Irvine32.inc COMMENT @ Here's a short program that uses dynamic memory allocation to set aside an array containing 1000 bytes. It then uses a loop to fill the array, verifying that all locations are accessible. There are only three functions to learn: GetProcessHeap, HeapAlloc, and HeapFree. You can allocate as many blocks of memory as you want, of difference sizes. When you free a block, remember that you must know its size. (Extracted from Microsoft MSDN online documentation) GetProcessHeap obtains a handle to the heap of the calling process. This handle can then be used in subsequent calls to the heap functions. Allows you to allocate memory from the process heap without having to first create a heap with the HeapCreate function. The HeapAlloc function allocates a block of memory from a heap. The allocated memory is not movable. If the function succeeds, the return value is a pointer to the allocated memory block. If the function fails, the return value is NULL. The HeapFree function frees a memory block allocated from a heap. If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. An application can call GetLastError for extended error information. @ .data ARRAY_SIZE = 1000 NULL = 0 pArray DWORD ? ; pointer to block of memory hHeap DWORD ? ; handle to the process heap dwFlags DWORD HEAP_ZERO_MEMORY ; set memory bytes to all zeros str1 BYTE "Cannot allocate heap memory!",0dh,0ah,0 str2 BYTE "Writing data into the array...",0dh,0ah,0 .code main PROC INVOKE GetProcessHeap ; get handle to this program's heap area mov hHeap,eax ; allocate the array's memory INVOKE HeapAlloc, hHeap, dwFlags, ARRAY_SIZE .IF eax == NULL mov edx,OFFSET str1 ; "Cannot allocate..." call WriteString jmp quit .ELSE mov pArray,eax ; save the pointer .ENDIF ; Fill the array with all "FFh" mov edx,OFFSET str2 ; "Writing data into..." call WriteString mov ecx,ARRAY_SIZE mov esi,pArray ; point to the array L1: mov BYTE PTR [esi],0FFh ; insert a byte in the array inc esi ; next location loop L1 ; free the array INVOKE HeapFree, hHeap, dwFlags, pArray quit: exit main ENDP END main