8.5 Using dos memory under the Go32 extender

Because Free Pascal for dos is a 32 bit compiler, and uses a dos extender, accessing DOS memory isn’t trivial. What follows is an attempt to an explanation of how to access and use dos or real mode memory3.

In Proteced Mode, memory is accessed through Selectors and Offsets. You can think of Selectors as the protected mode equivalents of segments.

In Free Pascal, a pointer is an offset into the DS selector, which points to the Data of your program.

To access the (real mode) dos memory, somehow you need a selector that points to the dos memory. The go32 unit provides you with such a selector: The DosMemSelector variable, as it is conveniently called.

You can also allocate memory in dos’s memory space, using the global_dos_alloc function of the go32 unit. This function will allocate memory in a place where dos sees it.

As an example, here is a function that returns memory in real mode dos and returns a selector:offset pair for it.

procedure dosalloc(var selector : word;  
                   var segment : word;  
                   size : longint);  
 
var result : longint;  
 
begin  
     result := global_dos_alloc(size);  
     selector := word(result);  
     segment := word(result shr 16);  
end;

(You need to free this memory using the global_dos_free function.)

You can access any place in memory using a selector. You can get a selector using the function:

function allocate_ldt_descriptors(count : word) : word;

and then let this selector point to the physical memory you want using the function

function set_segment_base_address(d : word;s : longint) : boolean;

Its length can be set using the function:

function set_segment_limit(d : word;s : longint) : boolean;

You can manipulate the memory pointed to by the selector using the functions of the GO32 unit. For instance with the seg_fillchar function. After using the selector, you must free it again using the function:

function free_ldt_descriptor(d : word) : boolean;

More information on all this can be found in the Unit Reference, the chapter on the go32 unit.

3Thanks for the explanation to Thomas Schatzl (E-mail: tom_at_work@geocities.com)