.struct/.endstruct

.struct name

These directives can be used to define a structure - a consecutive group of variables with a fixed format. Here's an example:

; Structure variables generate local labels:
.local

; Define the structure:
.struct Point2D
    .var db, X
    .var db, Y
.endstruct

; Declare a structure variable:
.var Point2D, Me

; Sample code that accesses the structure:
    ld a,10
    ld (Me.X),a
    
    ld a,32
    ld (Me.Y),a
    
    ld hl,10+32*256
    ld (Me),hl

The directive works (in this case) by creating three labels and a new module. One label is Me, pointing to the start of the variable. The module created is added as a child to the current one, with the name Me and has two child labels, X (with a value of Me+0) and Y (with a value of Me+1).

As the structure variable produces a nested module with local labels, you must use the .local directive to be able to access the structure.

You may only use .var and conditional directives inside a structure's definition. You can nest structures - for example:

.local

.struct Point2D
    .var db, X
    .var db, Y
.endstruct

.struct Point3D
    .var Point2D, P
    .var db,      Z
.endstruct

.var Point3D, You

    ld a,(You.P.X)

You may not, however, nest a structure onto itself. You might feel tempted, for example, to do the following:

.struct TreeNode
    .var word, Value
    .var TreeNode, RightBranch
    .var TreeNode, LeftBranch
.endstruct

The problem is that that particular structure has an infinite size! If you wish to implement that sort of data structure, you'd use pointers to the branch nodes.