5.2.4 Nested Structures and Unions

Structures and unions in MASM 6.0 can be nested in several ways. This section explains how to refer to the fields in a nested structure or union. The example below illustrates the four techniques for nesting and how to reference the fields. Note the syntax for nested structures. The discussion of these techniques follows the example.

ITEMS STRUCT

Inum WORD ?

Iname BYTE 'Item Name'

ITEMS ENDS

INVENTORY STRUCT

UpDate WORD ?

oldItem ITEMS { \

?,

'AF8' \ ; Named variable of

} ; existing structure

ITEMS { ?, '94C' } ; Unnamed variable of

; existing type

STRUCT ups ; Named nested structure

source WORD ?

shipmode BYTE ?

ENDS

STRUCT ; Unnamed nested structure

f1 WORD ?

f2 WORD ?

ENDS

INVENTORY ENDS

.DATA

yearly INVENTORY { }

; Referencing each type of data in the yearly structure:

mov ax, yearly.oldItem.Inum

mov yearly.ups.shipmode, 'A'

mov yearly.Inum, 'C'

mov ax, yearly.f1

To nest structures and unions, you can use any of these techniques:

The field of a structure or union can be a named variable of an existing structure or union type, as in the oldItem field. The field names in oldItem are not unique, so the full field names must be used when referencing those fields in the statement

mov ax, yearly.oldItem.Inum

To declare a named structure or union inside another structure or union, give the STRUCT or UNION keyword first and then define a label for it. Fields of the nested structure or union must always be qualified, as shown in this example:

mov yearly.ups.shipmode, 'A'

As shown in the Items field of Inventory, you can also use unnamed variables of existing structures or unions inside another structure or union. In this case you can reference its fields directly, as shown in this example:

mov yearly.Inum, 'C'

mov ax, yearly.f1

Offsets of nested structures are relative to the nested structure, not the root structure. In the example above, the offset of yearly.ups.shipmode is (current address of yearly) + 8 + 2. It is relative to the ups structure, not the yearly structure.