seg - memory segment


extern char Bseg;


short i;

short seg()

char *makeaddr(offset)
short offset;

short useaddr(addr)
char *addr;


Designed for the Intel 8088, Microsoft BASIC generally uses integers and addresses interchangeably as both are 16-bit quantities.  However, this processor is capable of addressing more memory through the use of an address extension known as a segment.  Segments are loosely associated with ordinary addresses.  Since Microsoft BASIC stipulates that all data is stored in one 64 kilobyte address space, 16-bit addresses are adequate for referencing program data.  Also, the user is able to address other areas of memory by directly setting the current (data) memory segment thus realizing additional 64 kilobyte address spaces.  The current memory segment is set with the DEF SEG statement. 

Basmark BASIC, on the other hand, is designed for many systems and many addressing methods: 16-bit addresses, 32-bit addresses and the Intel 16-bit address plus 16-bit segment (effectively).  Systems that use 16-bit addresses can be dealt with easily: the segmentation system is ignored.  The Intel system, of course, is no problem at all.  32-bit systems, however, can not be regarded as having 16-bit integers and addresses alike.  To compensate (while maintaining as much compatibility with the Microsoft system as possible), the memory segmentation scheme is expanded.  The segment is defined as the high-order 16-bits of the address.  Thus a DEF SEG statement and an integer-as-address are completely sufficient to state a 32-bit address.  In general, 32-bit systems may have more than a 64 kilobyte address space so the ability to refer to other “address spaces” in order to address variables in the program data space is a necessity.  In particular, the need exists to reference the “segment” (the high-order 16-bits) of an address.  This can not be known a priori as it must in the Microsoft system.  Therefore a special read-only variable, SEG, is added to the language. 

This scheme does not have repercussions on the Intel system or the 16-bit address system.  In fact, any program which has less than 64 kilobyte of data may ignore the scheme all together.  Also, any program designed for a given system will have a particular portability to any system with an address space of the same size or smaller.  (The address space of the Intel system is regarded as having an intermediate size.) Notwithstanding, such a program has no portability unless addresses are used in a very controlled manner. 

Hereinafter, the full machine address will be referred to as the address; the low-order 16-bits of the address, as the offset; and the high-order 16-bits of the address (if relevant), as the segment.  The segment is a value which works invisibly with the functions that use addresses.  Functions that would return addresses return the offset and invisibly set the segment.  Functions that are provided addresses are provided offsets and invisibly read the segment value. 

Bseg is the address of the beginning of the program data area.  No value or space is associated with this location.  Only its address is relevant. 

Initsg sets the current memory segment to be the beginning of the data area.  This function is called when the program starts-up.  This function also implements a version of the DEF SEG statement. 

Defseg sets the current segment to i.  This function implements a version of the DEF SEG statement. 

Seg returns the current segment value.  This function implements the BASIC SEG variable. 

Makeaddr returns the address given by offset offset and the current segment value.  This function supports functions which require addresses. 

Useaddr returns the offset and sets the current segment value given by the address addr.  This function supports functions which return addresses. 


bsaveload(mach), filptr(mach), peekpoke(mach), varptr(mach), varptS(mach)
def seg(stmt), seg(vrbl)


Operations involving absolute memory addresses ought not to be used. 

from The Basmark QuickBASIC Programmer’s Manual by Lawrence Leinweber