Using DOSmemory under the Go32 extender
Because Free Pascalfor DOSis a 32 bit compiler, and uses a DOSextender, accessing DOS memory isn’t trivial. What follows is an attempt to an explanation of how to access and use DOSor real mode memory8.
In Protected 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) DOSmemory, somehow you need a selector that points to the DOSmemory. 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 DOSsees it.
As an example, here is a function that returns memory in real mode DOSand 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.
An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
