HeapTest Programs

The following example (Heaptest1.asm) uses dynamic memory allocation to create and fill a
1000-byte array:

Title Heap Test #1 (Heaptest1.asm)
INCLUDE Irvine32.inc
; This program uses dynamic memory allocation to allocate and
; fill an array of bytes.
.data
ARRAY_SIZE = 1000
FILL_VAL EQU 0FFh
hHeap HANDLE ? ; handle to the process heap
pArray DWORD ? ; pointer to block of memory
newHeap DWORD ? ; handle to new heap
str1 BYTE "Heap size is: ",0
.code
main PROC
INVOKE GetProcessHeap ; get handle prog's heap
.IF eax == NULL ; if failed, display message
call WriteWindowsMsg
jmp quit
.ELSE
mov hHeap,eax ; success
.ENDIF
call allocate_array
jnc arrayOk ; failed (CF = 1)?
call WriteWindowsMsg
call Crlf
jmp quit
arrayOk: ; ok to fill the array
call fill_array
call display_array
call Crlf
; free the array
INVOKE HeapFree, hHeap, 0, pArray
quit:
exit
main ENDP
;--------------------------------------------------------
allocate_array PROC USES eax
;
; Dynamically allocates space for the array.
; Receives: EAX = handle to the program heap
; Returns: CF = 0 if the memory allocation succeeds.
;--------------------------------------------------------
INVOKE HeapAlloc, hHeap, HEAP_ZERO_MEMORY, ARRAY_SIZE
.IF eax == NULL
stc ; return with CF = 1
.ELSE
mov pArray,eax ; save the pointer
clc ; return with CF = 0
.ENDIF
ret
allocate_array ENDP
;--------------------------------------------------------
fill_array PROC USES ecx edx esi
;
; Fills all array positions with a single character.
; Receives: nothing
; Returns: nothing
;--------------------------------------------------------
mov ecx,ARRAY_SIZE ; loop counter
mov esi,pArray ; point to the array
L1: mov BYTE PTR [esi],FILL_VAL ; fill each byte
inc esi ; next location
loop L1
ret
fill_array ENDP
;--------------------------------------------------------
display_array PROC USES eax ebx ecx esi
;
; Displays the array
; Receives: nothing
; Returns: nothing
;--------------------------------------------------------
mov ecx,ARRAY_SIZE ; loop counter
mov esi,pArray ; point to the array
L1: mov al,[esi] ; get a byte
mov ebx,TYPE BYTE
call WriteHexB ; display it
inc esi ; next location
loop L1
ret
display_array ENDP
END main

The following example (Heaptest2.asm) uses dynamic memory allocation to repeatedly allocate
large blocks of memory until the heap size is exceeded.

Title Heap Test #2 (Heaptest2.asm)
INCLUDE Irvine32.inc
.data
HEAP_START = 2000000 ; 2 MB
HEAP_MAX = 400000000 ; 400 MB
BLOCK_SIZE = 500000 ; .5 MB
hHeap HANDLE ? ; handle to the heap
pData DWORD ? ; pointer to block
str1 BYTE 0dh,0ah,"Memory allocation failed",0dh,0ah,0
.code
main PROC
INVOKE HeapCreate, 0,HEAP_START, HEAP_MAX
.IF eax == NULL ; failed?
call WriteWindowsMsg
call Crlf
jmp quit
.ELSE
mov hHeap,eax ; success
.ENDIF
mov ecx,2000 ; loop counter
L1: call allocate_block ; allocate a block
.IF Carry? ; failed?
mov edx,OFFSET str1 ; display message
call WriteString
jmp quit
.ELSE ; no: print a dot to
mov al,'.' ; show progress
call WriteChar
.ENDIF
;call free_block ; enable/disable this line
loop L1
quit:
INVOKE HeapDestroy, hHeap ; destroy the heap
.IF eax == NULL ; failed?
call WriteWindowsMsg ; yes: error message
call Crlf
.ENDIF
exit
main ENDP
allocate_block PROC USES ecx
; allocate a block and fill with all zeros.
INVOKE HeapAlloc, hHeap, HEAP_ZERO_MEMORY, BLOCK_SIZE
.IF eax == NULL
stc ; return with CF = 1
.ELSE
mov pData,eax ; save the pointer
clc ; return with CF = 0
.ENDIF
ret
allocate_block ENDP
free_block PROC USES ecx
INVOKE HeapFree, hHeap, 0, pData
ret
free_block ENDP
END main

 

posted @ 2016-11-02 15:43  dreamafar  阅读(272)  评论(0编辑  收藏  举报