分配的内存有问题

时间:2018-12-28 20:16:18

标签: assembly x86 computer-science 32-bit irvine32

我是新来的集会成员,而且一直都在学习更多。我有一个问题,我曾经尝试写回去,但是我遇到了一些问题。我应该编写一个程序,以动态分配内存并从操作系统维护的堆内存中释放内存。该程序有效,但是存在问题。在主过程中,分配了内存,但调用free不太正确。

我想知道如何添加推入过程的clear参数。所以分配的内存被释放了?

这用于使用Irvine的x86 32b程序集,并且程序正在运行。

INCLUDE Irvine32.inc


HANDLE      TEXTEQU <DWORD>


H_ZERO_MEM  EQU    8


GetProcessHeap  PROTO

HeapAlloc       PROTO,  

                hHeap:HANDLE, 

                dwflags: DWORD, 

                dwbytes: DWORD

HeapFree        PROTO,  

                hHeap:HANDLE, 

                dwFlags:DWORD,

                lpMem:DWORD

.data

MSGAlloc    BYTE           " Successfully allocated ",0 

MSGFree     BYTE           " Successfully freed",0      

notAllocMSG BYTE           " Error in allocating. Please try again! ",0

notFreeMSG  BYTE           " There was an error freeing the heap memory",0 

sizeH       DWORD          ?


.code


Alloc       Proc

            push        ebp

            mov         ebp,esp

            INVOKE      GetProcessHeap

            cmp         eax, 0

            je          doNotAlloc

            INVOKE      HeapAlloc, eax, H_ZERO_MEM, [ebp +8] 

doNotAlloc:

            pop         ebp

            ret         4


Alloc       Endp

FREE        proc 

            push            ebp

            mov             ebp,esp

            INVOKE          GetProcessheap

            cmp             eax, 0

            je              doNotFree

            INVOKE          HeapFree , eax, 0, [ebp + 8]

doNotFree:

            pop             ebp

            ret              4

FREE        Endp

main        proc    

            mov              sizeH  , 1024

            mov              eax, sizeH

           push              eax

           call              ALLOC

           cmp               eax, 0

           je                notAlloc

           push              eax

           mov               eax, sizeH 

           call              writedec

           mov               edx, offset MSGAlloc

          call               writestring

          call               crlf  

          call               FREE

          cmp                eax, 0


          je                notHeapFreed

          mov               eax, sizeH 

          call              writedec

          mov               edx, offset MSGFree

          call              writestring

          jmp               endMSG


notAlloc:

          mov               edx, offset notAllocMSG

          call              writestring

          jmp               endMSG

notHeapFreed:

          mov               edx, offset notFreeMSG

          call              writestring

endMSG:
          Exit

main      ENDP

          END               main

0 个答案:

没有答案