2017-04-14 3 views
1

私の教授が書いたこのコードを実行しようとしています。私がコンパイルしたコードを実行すると、残念ながら結果:x86アセンブリヒープメモリ割り当て

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とハンドラとProtoが一緒に動作するかについても興味があります。ヒープは動的メモリ割り当てのために用意されたメモリであり、スタックとは異なり、メモリの割り当て方法や割り当て解除方法の設定パターンはありません。いつでもランダムに割り当ておよび割り当てを解除し、割り当てられたメモリをいつでも解放することができます。また、スタックとは異なり、ヒープメモリを手動で破棄してメモリを保護する必要があります。

答えて

2

実行ファイルをどのように構築していますか?

HeapFreeのような機能を使用するには、kernel32とリンクする必要があります。これを行う方法は、使用しているリンカーによって異なる場合があります。 MASMでは、これは書き込みを意味する可能性があります。

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

残念ながら、私はその提案を試み、動作しませんでした。ありがとうございました。 –