Memory Models

Technical Notes

Overview
What Is a Memory Model?
Memory Model Characteristics
Library Characteristics
Choosing a Memory Model
Memory Model Defines

Overview

This unit consists of symbolic constants which may be used to control conditional compilation based on a selected memory model.

What Is a Memory Model?

A memory model is a segment naming and grouping convention which classifies and organizes program code and data. Six standard memory models are supported: TINY, SMALL, MEDIUM, COMPACT, LARGE, and HUGE. These models define segment naming and ordering schemes for programs with a single segment as well as programs with thousands of segments.

Memory Model Characteristics

The six standard memory models have the following characteristics:
TINY
All code and static data must fit in a single segment or group and is accessed relative to the beginning of that segment or group. Code and data space is limited to 64K. A program written using the TINY model must be converted to a .COM file.
SMALL
All code must fit in a single segment or group and is accessed relative to the beginning of that segment or group. Code space is limited to 64K. Also, all data must fit in a single segment or group and is accessed relative to the beginning of that segment or group. Data space is limited to 64K.
MEDIUM
Code may be placed in multiple segments or groups and is referenced from the beginning of the segment or group in which it resides. There is no limitation on the number of code segments. Data must fit in a single segment or group and is accessed relative to the beginning of that segment or group. Data space is limited to 64K.
COMPACT
All code must fit in a single segment or group and is accessed relative to the beginning of that segment or group. Code space is limited to 64K. Data may be placed in multiple segments or groups and is referenced from the beginning of the segment or group in which it resides. There is no limitation on the number of data segments. Individual data structures are limited to a maximum size of 64K each and are accessed using offsets relative to the segment or group in which they reside.
LARGE
Both code and static data may be placed in multiple segments or groups and is referenced from the beginning of the segment or group in which it resides. There is no limitation on the number of code or data segments. Individual data structures are limited to a maximum size of 64K each and are accessed using offsets relative to the segment or group in which they reside.
HUGE
Both code and static data may be placed in multiple segments or groups and is referenced from the beginning of the segment or group in which it resides. There is no limitation on the number of code or data segments. Individual data structures can be larger than 64K and are accessed using normalized segment:offset pointers.
Models which support only one code segment (TINY, SMALL, and COMPACT) are referred to as small code models. Models with multiple code segments (MEDIUM, LARGE, and HUGE) are large code models. Similarly, models which support only one data segment (TINY, SMALL, and MEDIUM) are small data models, while models which allow many data segments (COMPACT, LARGE, and HUGE) are large data models. These classifications (large code, small data, etc.) are referred to as the characteristics of the model. This terminology is used throughout the manual.

The characteristics (number of code and data segments) of the six standard memory models may be summarized as follows:

Model      Data     Code    Description

TINY       Small    Small   One segment, code and data combined
SMALL      Small    Small   One data segment, one code segment
MEDIUM     Small    Large   One data segment, multiple code segments
COMPACT    Large    Small   Multiple data segments, one code segment
LARGE      Large    Large   Multiple code and data segments
HUGE       Large    Large   Special case of LARGE (64K+ data structures)
The characteristics of each model are fixed and cannot be changed.

Note that the TINY model is a degenerate case of SMALL, since the code segment and the data segment are the same. Likewise, HUGE is a special case of the LARGE model which is used for LARGE model programs which deal with data structures greater than 64K in size. Spontaneous Assembly includes separate libraries for the TINY, SMALL, MEDIUM, COMPACT, and LARGE models. HUGE model programs may be created using the LARGE model library, but no direct support is provided for data structures greater than 64K in size. The TINY model is used to create .COM programs. All other models are used to create .EXE programs.

Library Characteristics

In the TINY or SMALL models, all Spontaneous Assembly library functions are accessed using near calls. The TINY and SMALL models produce the tightest, fastest code.

In the COMPACT model, Spontaneous Assembly library functions are also accessed using near calls. This slightly increases the size of functions which access internal data, but it allows these functions to support multiple data segments.

In the MEDIUM model, Spontaneous Assembly library functions are accessed using far calls. This makes the code slightly larger and slower.

In the LARGE model, the Spontaneous Assembly library functions are accessed using far calls. This model has the largest overall code size and slowest execution speed.

Choosing a Memory Model

The following general rules should simplify the process of choosing a memory model for a specific application:
  1. TINY must be used if a .COM file is to be created.
  2. SMALL is usually the best choice for small to medium sized applications.
  3. MEDIUM is usually best for medium sized applications with more than 64K of code and less than 64K of data.
  4. COMPACT is usually best for medium sized applications with more than 64K of data and less than 64K of code.
  5. LARGE should be used for the largest and most complex applications.
  6. If an application is likely to grow substantially in the future, model independent programming techniques should be considered. In this case, start with the smallest model the applicationcan currently support. As the program grows in size, the model may be changed and the application may be reassembled and relinked with little, if any modification.

Memory Model Defines

These numeric defines indicate the selected memory model and the characteristics of the selected memory model. They equal -1 if the indicated condition is true; otherwise, they equal 0. These defines are defined in SA_DEF.H (automatically included from most other header files) and are intended for use with conditional compilation.
__TINY__
Indicates whether or not the selected memory model is TINY.
__SMALL__
Indicates whether or not the selected memory model is SMALL.
__MEDIUM__
Indicates whether or not the selected memory model is MEDIUM.
__COMPACT__
Indicates whether or not the selected memory model is COMPACT.
__LARGE__
Indicates whether or not the selected memory model is LARGE.
__HUGE__
Indicates whether or not the selected memory model is HUGE.
__SMALL_CODE__
Indicates whether or not the selected memory model is a small code model.
__LARGE_CODE__
Indicates whether or not the selected memory model is a large code model.
__SMALL_DATA__
Indicates whether or not the selected memory model is small data model.
__LARGE_DATA__
Indicates whether or not the selected memory model is a large data model.
The following example demonstrates the use of memory model defines for conditional compilation:

#include 
#include 

void _puts(char *str);
{
           ...
#if __LARGE_DATA__
           lds   si,str                   /* DS:SI -> str */
#else
           mov   si,str                   /* SI = offset of str */
#endif
           put_str();
           ...
}