5.2 Structures and Unions

A structure is a group of possibly dissimilar data types and variable declarations that can be accessed as a unit or by any of its components. The fields within the structure can have different sizes and data types.

Unions are identical to structures, except that the fields of a union overlap in memory, which allows you to define different data formats for the same memory space. Unions can store different types of data depending on the situation. They can also store data as one data type and retrieve it as another data type.

Whereas each field in a structure has an offset relative to the first byte of the structure, all the fields in a union start at the same offset. The size of a structure is the sum of its components, while the size of a union is the length of the longest field.

A MASM structure is similar to a struct in the C language, a STRUCTURE in FORTRAN, and a RECORD in Pascal. Unions in MASM are similar to unions in C and FORTRAN, and to variant records in Pascal.

Follow these steps when using structures and unions:

1.Declare a structure (or union) type.

2.Define one or more variables having that type.

3.Reference the fields directly or indirectly with the field (dot) operator.

You can use the entire structure or union variable or just the individual fields as operands in assembler statements. This section explains the allocating, initializing, and nesting of structures and unions.

MASM 6.0 extends the functionality of structures and also makes some changes to MASM 5.1 behavior. You can still retain MASM 5.1 behavior if you prefer by specifying OPTION OLDSTRUCTS in your program. See Section 1.3.2 for information about the OPTION directive, and Section 5.2.3 for information about referencing structures and unions.