Search code examples
x86heap-memorymasmirvine32

x86 Assembly Heap Memory Allocation


I am trying to run this code written by my professor. Unfortunately, when I compile and run the code, the results:

INCLUDE Irvine32.inc
.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

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 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 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 someone explain why. Thanks. I am also curious to how Heap memory Allocation and how Invoke and Handlers and Proto works together. I understand that heap is memory set aside for dynamic memory allocation and unlike stack, there's no set pattern to how memory is allocated or deallocated. You can allocate and deallocate at any time randomly and free those allocated memory any time. Also, unlike stack, heap memory must be manually destroyed to prevent memory.


Solution

  • How are you building the executable?

    In order to use functions like HeapFree, you need to link with kernel32. How to do this can vary by which linker you're using. In MASM this may mean writing

    include     \masm32\include\kernel32.inc
    includelib  \masm32\lib\kernel32.lib