Microsoft KB Archive/35562

From BetaArchive Wiki

Determining Available DOS Memory in Pascal with MASM PSS ID Number: Q35562 Article last modified on 03-28-1991 PSS database name: S_PasCal

3.3x 4.00



This article provides a code example for determining the number of bytes of available memory under DOS using Pascal versions 3.30 to 4.00, and a Macro Assembler routine.

More Information:

The program further below determines the amount of free space in both the long (or far) and near heaps under DOS.

The external assembly language routine FarMem is called to determine the number of 16-byte paragraphs that are available in the far heap above the default data segment. FarMem calls DOS interrupt 21 hex, function 4B hex, requesting FFFF hex paragraphs, or one Megabyte. Because that much memory never is available with DOS and this program loaded in memory, the memory allocation fails; however, the number of available paragraphs is put in the BX register. FarMem moves the contents of the BX register into AX prior to returning from the function call below for the return value of FarMem. The number of 16-byte paragraphs returned from FarMem is then multiplied by 16 to calculate the number of free bytes in the far heap.

Library function freect returns an estimate of the number of times “size” bytes can be allocated in near heap by the new procedure. Because of a two-byte allocation overhead for a pointer pointing above the allocated block of memory for subsequent near-heap allocations, freect of zero bytes will return the number of two-byte pointers that can be allocated in the near heap. Multiplying this return value by two will give the number of free bytes in the near heap.

Both the far and near heap are available for dynamic memory allocation by the Pascal library functions allmqq and getmqq. These functions are defined in the “Microsoft Pascal Compiler Reference Manual” on pages 228 and 238. There is an example of their use on pages 73-76 of the “Microsoft Pascal Compiler User’s Guide,” and an additional example that uses allmqq in the file ALLOCMEM.PAS is available in the software library or the Pascal Examples application note file ALLOCMEM.PAS.


program AvailableMemory(input,output); var farheapsize : integer4; {free bytes in far/long heap} nearheapsize : word; {free bytes in near heap }

{MASM routine in the file FARMEM.ASM to determine the number of free 16-byte paragraphs in the far heap.} function FarMem : word; extern;

{Pascal library function used to determine the number of free bytes in the near heap within the default data segment.} function freect(size : word) : word; extern;

begin {available number of bytes in far heap} farheapsize := FarMem * 16; writeln; writeln(‘Available bytes in far heap =’, farheapsize:6);

{available bytes in near heap within the default data segment} nearheapsize := freect(0)*2; writeln(‘Available bytes in near heap =’, nearheapsize:6);

writeln(‘Total available bytes =’, farheapsize + nearheapsize : 6);

writeln; writeln(‘Program completed. Good-bye.’) end.


This assembler procedure, “FARMEM”, calls DOS interrupt 21 hex with ; function 4B hex to determine the number of 16-byte paragraphs available ; in the far heap. It returns the number of paragraphs in the AX ; register for its return value to the function call from Pascal. ; No error checking is being performed. ; ; DOS function 4B hex is intended for allocating memory, but if ; the number of requested paragraphs specified in the BX register ; is not available, the number of actual paragraphs available is ; put into the BX register. FFFF hex paragraphs are requested, ; which is more than could possibly be available under DOS, ; consequently this memory request is sure to fail and the actual ; number of available paragraphs can be accessed in BX. BX is ; moved into AX for the return value of FARMEM. ; ; When this proc begins execution, it pushes the bp register to ; preserve it. Then bp is set to the current stack frame by ; moving sp, the Stack Pointer register, into it. The bp register ; points where its previous value was pushed onto the stack, and ; bp+2 and bp+4 point to the return address. The ret instruction ; is used to pop the return address off of the stack when returning.
    NAME    farmem
Formally declare this text segment, and the data group “DGROUP” ; created by Pascal.


Procedure to determine the number of 16-byte paragraphs available ; in the far heap by calling interrupt 21 hex, function 48 hex. ; Return value of number of paragraphs moved into ax.

FARMEM_TEXT SEGMENT PUBLIC FARMEM FARMEM PROC FAR push bp ; save bp register mov bp,sp ; set bp to new stack frame

    mov     bx,0ffffh ; ask for a Meg of memory
    mov     ah,48h    ; to call function 48h for available memory
    int     21h       ; call DOS

    mov     ax,bx     ; # paragraphs moved to ax for return value
    pop     bp        ; restore bp register
    ret               ; pop return address off of stack & return



Copyright Microsoft Corporation 1991.