zoukankan      html  css  js  c++  java
  • HeapTest Programs

    The following example (Heaptest1.asm) uses dynamic memory allocation to create and fill a
    1000-byte array:

    Title Heap Test #1 (Heaptest1.asm)
    INCLUDE Irvine32.inc
    ; This program uses dynamic memory allocation to allocate and
    ; fill an array of bytes.
    .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
    .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: ; ok to fill the array
    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 example (Heaptest2.asm) uses dynamic memory allocation to repeatedly allocate
    large blocks of memory until the heap size is exceeded.

    Title Heap Test #2 (Heaptest2.asm)
    INCLUDE Irvine32.inc
    .data
    HEAP_START = 2000000 ; 2 MB
    HEAP_MAX = 400000000 ; 400 MB
    BLOCK_SIZE = 500000 ; .5 MB
    hHeap HANDLE ? ; handle to the heap
    pData DWORD ? ; pointer to block
    str1 BYTE 0dh,0ah,"Memory allocation failed",0dh,0ah,0
    .code
    main PROC
    INVOKE HeapCreate, 0,HEAP_START, HEAP_MAX
    .IF eax == NULL ; failed?
    call WriteWindowsMsg
    call Crlf
    jmp quit
    .ELSE
    mov hHeap,eax ; success
    .ENDIF
    mov ecx,2000 ; loop counter
    L1: call allocate_block ; allocate a block
    .IF Carry? ; failed?
    mov edx,OFFSET str1 ; display message
    call WriteString
    jmp quit
    .ELSE ; no: print a dot to
    mov al,'.' ; show progress
    call WriteChar
    .ENDIF
    ;call free_block ; enable/disable this line
    loop L1
    quit:
    INVOKE HeapDestroy, hHeap ; destroy the heap
    .IF eax == NULL ; failed?
    call WriteWindowsMsg ; yes: error message
    call Crlf
    .ENDIF
    exit
    main ENDP
    allocate_block PROC USES ecx
    ; allocate a block and fill with all zeros.
    INVOKE HeapAlloc, hHeap, HEAP_ZERO_MEMORY, BLOCK_SIZE
    .IF eax == NULL
    stc ; return with CF = 1
    .ELSE
    mov pData,eax ; save the pointer
    clc ; return with CF = 0
    .ENDIF
    ret
    allocate_block ENDP
    free_block PROC USES ecx
    INVOKE HeapFree, hHeap, 0, pData
    ret
    free_block ENDP
    END main
  • 相关阅读:
    Python集合
    excel数据的处理
    史上最全的web前端系统学习教程!
    H5游戏开发:贪吃蛇
    javascript+HTMl5游戏下载,开发一个都能月薪上万!舅服你
    2020年必看的web前端开发学习路线!
    花了一天准备好的web前端电子书籍,全部可以免费下载!
    2019年大牛最新整理的Python技术入门路线
    从零开始,学习web前端之HTML5开发
    经验分享:如何系统学习 Web 前端技术?
  • 原文地址:https://www.cnblogs.com/dreamafar/p/6023145.html
Copyright © 2011-2022 走看看