mov bx, MemSize ;amount of memory requested, in paragraphs
mov ah, 48h ;Allocate Memory
int 21h
jc error_handler ;carry set means error
mov SegmentMem, ax ;segment address of allocated memory
Allocate Memory (Function 48h) allocates the requested amount of memory and returns the segment address of the allocated memory block.
MemSize
Specifies the amount of memory to be allocated, in paragraphs (16 bytes).
If the function is successful, the carry flag is clear and the AX register contains the segment address of the first byte (offset 0) of the allocated memory block. Otherwise, the carry flag is set and the AX register contains an error value, which may be one of the following:
Value | Name |
0007h | ERROR_ARENA_TRASHED |
0008h | ERROR_NOT_ENOUGH_MEMORY |
If Allocate Memory returns 0008h (ERROR_NOT_ENOUGH_MEMORY), the BX register contains the number of paragraphs in the largest available memory block.
The contents of the allocated memory are not defined.
MS-DOS allocates all available memory to a .COM program; most .EXE programs request all available memory when they load. If a program is to subsequently use the Allocate Memory function to dynamically allocate memory, it should use Set Memory Block Size (Function 4Ah) to free as much memory as possible.
The default memory-management strategy is to allocate the first available block that contains the requested number of bytes. A program can use Set Allocation Strategy (Function 5801h) to change the way MS-DOS chooses memory blocks for allocation.
Function 49h Free Allocated Memory
Function 4Ah Set Memory Block Size
Function 5800h Get Allocation Strategy
Function 5801h Set Allocation Strategy