x86 程序集堆内存分配

标签 x86 heap-memory masm irvine32

我正在尝试运行我的教授编写的这段代码。不幸的是,当我编译并运行代码时,结果:

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 

结果如下:

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

有人能解释一下为什么吗?谢谢。我也很好奇堆内存分配以及 Invoke 和 Handlers 以及 Proto 如何一起工作。据我所知,堆是为动态内存分配预留的内存,与堆栈不同,没有固定的模式来分配或释放内存。您可以随时随机分配和取消分配,并随时释放那些分配的内存。另外,与堆栈不同,堆内存必须手动销毁以防止内存占用。

最佳答案

您如何构建可执行文件?

为了使用像HeapFree这样的函数,您需要链接到kernel32。如何执行此操作可能因您使用的链接器而异。在 MASM 中,这可能意味着编写

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

关于x86 程序集堆内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43414989/

相关文章:

linux - 如何为汇编语言程序制作 Makefile?

assembly - 为什么使用 NASM 汇编时 `lea esi, dword [edx + ecx * 4]` 会产生错误?

JMP 中的 Java 堆大小

c++ - 动态分配内存存储说明

assembly - x86 寄存器标志缩写

c - 为什么使用 %ebx 寄存器会导致我的汇编代码出现段错误

assembly - 中断服务程序的奇怪行为

c++ - vector::reserve和经典内存分配之间的内存分配差异

winapi - 在汇编器中处理图形计算器中按钮的事件

assembly - 汇编中的矩阵乘法