x86 Assembly Heap Memory Allocation -
i trying run code written professor. unfortunately, when compile , run code, results:
include irvine32.inc .data array_size = 1000 fill_val equ 0ffh hheap handle ? ;handle process heap parray dword ? ;pointer block of memory newheap dword ? ;handle new heap str1 byte "heap size is: ",0 getprocessheap proto .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: call fill_array call display_array call crlf ;free array invoke heapfree, hheap, 0, parray quit: exit main endp ;------------------------------------------------------- allocate_array proc uses eax ; ;dynamically allocates space array ;receives: eax = handle program heap ;returns: cf = 0 if memory allocation succeeds ;------------------------------------------------------- invoke heapalloc, hheap, heap_zero_memory, array_size .if eax == null stc ;return cf = 1 .else mov parray, eax ;save pointer clc ;return cf = 0 .endif ret allocate_array endp ;-------------------------------------------------------- fill_array proc uses ecx edx esi ; ;fills array positions single character ;receives: nothing ;returns: nothing ;--------------------------------------------------------- mov ecx, array_size ;loop counter mov esi, parray ;point 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 array ; receives: nothing ; returns: nothing mov ecx, array_size ;loop counter mov esi, parray ;point array l1: mov al, [esi] ;get byte mov ebx, type byte call writehexb ;display inc esi ;next location loop l1 ret display_array endp end main
the following results:
bobnew.asm(41) : error a2006: undefined symbol : heapfree bobnew.asm(56) : error a2006: undefined symbol : heapalloc bobnew.asm(22) : error a2006: undefined symbol : writewindowsmsg bobnew.asm(30) : error a2006: undefined symbol : writewindowsmsg bobnew.asm(97) : error a2006: undefined symbol : writehexb
can explain why. thanks. curious how heap memory allocation , how invoke
, handlers , proto
works together. understand heap memory set aside dynamic memory allocation , unlike stack, there's no set pattern how memory allocated or deallocated. can allocate , deallocate @ time randomly , free allocated memory time. also, unlike stack, heap memory must manually destroyed prevent memory.
how building executable?
in order use functions heapfree
, need link kernel32
. how can vary linker you're using. in masm may mean writing
include \masm32\include\kernel32.inc includelib \masm32\lib\kernel32.lib
Comments
Post a Comment