2.2.4 Creating Data Segments

Programs can contain both near and far data. In general, you should place important and frequently used data in the near data area, where data access is faster. This area can get crowded, however, because (in 16-bit operating systems) the total amount of all near data in all modules cannot exceed 64K. Therefore, you may want to place infrequently used or particularly large data items in a far data segment.

The .DATA, .DATA?, .CONST, .FARDATA, and .FARDATA? directives create data segments. You can access the various segments within DGROUP without reloading segment registers (see Section 2.3.4, “Defining Segment Groups”). These four directives also prevent instructions from appearing in data segments by assuming CS to ERROR. (See Section 2.3.3 for information about ASSUME.)

Near Data Segments

The .DATA directive creates a near data segment. This segment contains the frequently used data for your program. It can occupy up to 64K in DOS or 512 megabytes under flat model in OS/2 2.0. It is placed in a special group identified as DGROUP, which is also limited to 64K.

Summary: Near data pointers always point to DGROUP.

When you use .MODEL, the assembler automatically defines DGROUP for your near data segment. The segments in DGROUP form near data, which can normally be accessed directly through DS or SS.

You can also define the .DATA? and .CONST segments that go into DGROUP unless you are using flat model. Although all of these segments (along with the stack) are eventually grouped together and handled as data segments, .DATA? and .CONST enhance compatibility with Microsoft high-level languages. In Microsoft languages, .CONST is used for defining constant data such as strings and floating-point numbers that must be stored in memory. The .DATA? segment is used for storing uninitialized variables. You can follow this convention if you wish. If you use C start-up code, .DATA? is initialized to 0.

You can use @data to determine the group of the data segment and @DataSize to determine the size of the memory model set by the .MODEL directive. The predefined symbols @WordSize and @CurSeg return the size attribute and name of the current segment, respectively. See Section 1.2.3, “Predefined Symbols.”

Far Data Segments

The compact, large, and huge memory models use far data addresses by default. With these memory models, however, you can still use .DATA, .DATA?, and .CONST to create data segments. The effect of these directives does not change from one memory model to the next. They always contribute segments to the default data area, DGROUP, which has a total limit of 64K.

When you use .FARDATA or .FARDATA? in the small and medium memory models, the assembler creates far data segments FAR_DATA and FAR_BSS, respectively. You can access variables with:

mov ax, SEG farvar2

mov ds, ax

See Section 3.1.2 for more information on far data.