;; GETMEM.ASM--program to take as input a requested size in bytes ;; and return as output a pointer to a block of that size. If ;; such a block is unavailable, return the null pointer. ;; ;; Calling Sequence: ;; EXTRN GetMem : NEAR ;; mov ax, numberOfBytesRequested ;; ;; The program returns with es:bx pointing to the allocated memory, ;; or null, All registers except ax, bx, and es are preserved. ;; ;; Library source text from "Assembly Language for the IBM PC Family" by ;; William B. Jones, (c) Copyright 1992, 1997, Scott/Jones Inc. ;; .MODEL SMALL HeapSeg SEGMENT MAXHEAP EQU 0FFFDh ; make the heap (nearly) one full segment NextHeap DW HeapStart ; offset of next block to allocate HeapStart DB MAXHEAP DUP (?) ; the heap HeapSeg ENDS .CODE PUBLIC GetMem GetMem PROC mov bx, HeapSeg mov es, bx ASSUME es : HeapSeg mov bx, MAXHEAP + 2; Is there enough room? sub bx, NextHeap ; bx = amt of heap available cmp bx, ax jae OK ; must be unsigned compare; amount of ; heap available will initially appear ; to be negative mov bx, 0 ; Indicate not enough memory mov es, bx ; (Pascal simply dies in this case) ret OK: mov bx, NextHeap ; es:bx now properly set add NextHeap, ax ; and NextHeap properly updated ret GetMem ENDP END