Microsoft KB Archive/31308

= Basic Example Using SETMEM to Allocate Far Heap Memory; EXEMOD =

Article ID: 31308

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic for MS-DOS
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q31308



SUMMARY
To allocate a reserved location in memory that compiled Basic will not touch, you can deallocate some memory by using the SETMEM function (see the Basic language reference manual and the example for SETMEM below). Then, using either CALL INTERRUPT or CALL INT86OLD, execute a MS-DOS Interrupt 21 hex (33 decimal), with function 48 hex (72 decimal) to obtain the address of the memory freed in far memory. Call with AX equal to &H4800 and BX equal to the number of paragraphs of memory needed (the number of bytes of memory needed, divided by 16).

If the function succeeds, the flag is clear and AX returns the initial segment (paragraph address) of the allocated block.

If the function fails, the flag is set and AX either is 7 (if memory control blocks were destroyed) or 8 (if memory was insufficient, in which case BX gives the size of the largest available block).



MORE INFORMATION
Please note that QuickBasic for MS-DOS, versions 3.0 and earlier do not include the SETMEM function.

The following are two other methods of allocating areas of memory:
 * 1) A static array can be set aside as a block of memory that will not move. The VARPTR function returns the offset of the array.
 * 2) The EXEMOD.EXE utility provided with the Microsoft Macro Assembler allows you to modify the header of an .EXE to shorten the maximum upper load address of a program. By default, Basic .EXE programs assume that all of RAM is available. If you make the load address smaller, you must make sure that there is enough room for the Basic program's code and data. Microsoft does not encourage using EXEMOD with compiled Basic programs. The SETMEM function should be used instead.

Example
' To try this example in VBDOS.EXE: ' 1. From the File menu, choose New Project. ' 2. Copy the code example to the Code window. ' 3. Press F5 to run the program.

' To run this program in the environment, you must invoke the ' environment with the /L switch to load the default Quick library: '   VBDOS.EXE /L          for Visual Basic 1.0 for MS-DOS

' Use the following include file for Visual Basic 1.0 for MS-DOS: REM $INCLUDE: 'VBDOS.BI'

DEFINT A-Z DIM InRegs AS RegType, OutRegs AS RegType DIM InRegsX AS RegTypeX, OutRegsX AS RegTypeX

PRINT SETMEM(-1808)    ' Have Visual Basic free up some memory. InRegs.ax = &H4800 InRegs.bx = 113        ' Allocates a block of 113 paragraphs, or                        ' 113*16 bytes. CALL INTERRUPT(&H21, InRegs, OutRegs)

' Check results of the interrupt call. IF (OutRegs.flags AND 1) = 0 THEN PRINT &quot;Eureka! Memory allocated at &H&quot;; HEX$(OutRegs.ax) ELSEIF OutRegs.ax = 7 THEN PRINT &quot;ERROR! Memory control blocks destroyed!&quot; END ELSE PRINT &quot;Insufficient memory! Largest available block is: &quot; ;OutRegs.bx    END END IF

' Now deallocate the memory we just allocated. InRegsX.es = OutRegs.ax InRegsX.ax = &H4900 CALL INTERRUPTX(&H21, InRegsX, OutRegsX)

' Double check the outcome of the interrupt call. IF (OutRegsX.flags AND 1) = 0 THEN PRINT &quot;Memory successfully deallocated.&quot; ELSEIF OutRegsX.ax = 7 THEN PRINT &quot;ERROR! Memory Control Blocks destroyed!&quot; ELSE PRINT &quot;ERROR! Incorrect segment in ES!&quot; END IF

Additional query words: VBmsdos BasicCom

Keywords: KB31308

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.