• Keine Ergebnisse gefunden

Creating an instance of a structure or union

Im Dokument . TUrbo Assembler" (Seite 160-164)

To create an instance of a structure or a union data type, use the structure or union name as a data allocation directive. For example, assume you've defined the following:

ASTRUC STRUC B DB IxyZ"

C DW 1 D DD 2 ASTRUC ENDS BUNION UNION X DW?

Y DD?

Z DB?

BUNION ends

Then the statements ATEST

BTEST

ASTRUC BUNION

would create instances of the structure astruc (defining the variable atest) and the union bunion (defining the variable btest). Since the example contained the? uninitialized data value, no initial data will be emitted to the current segment.

Initializing union or structure instances

Initialized structure instances are more complex than uninitialized instances. When you define a structure, you have to specify an initial default value for each of the structure members. (You can use the?

keyword as the initial value, which indicates that no specific initial value should be saved.) When you create an instance of the structure, you can create it using the default values or overriding values. The simplest initialized instance of a structure contains just the initial data specified in the definition. For example,

ASTRUC {}

is equivalent to DB IxyZ"

DW 1 DD 2

The braces ({ }) represent a null initializer value for the structure. The initializer value determines what members (if any) have initial values that should be overridden, and by what new values, as you allocate data for the structure instance. The syntax of the brace initializer follows:

{ [member_name = value [,member_name = value ... ll }

member _name is the name of a member of the structure or union. value is the value that you want the member to have in this instance. Specify a null value to tell Turbo Assembler to use the initial value of the member from the structure or union definition. A ? value indicates that the member should be uninitialized. Turbo Assembler sets any member that doesn't appear in the initializer to the initial value of the member from the structure or union definition. For example,

ASTRUC {C=2,D=?}

is equivalent to DB IxyZ"

DW 2 DD ?

You can use the brace initializer to specify the value of any structure or union member, even in a nested structure or union.

Unions differ from structures because elements in a union overlap one another. Be careful when you initialize a union instance since if several union members overlap, Turbo Assembler only lets one of those members have an initialized value in an instance. For example,

BUNION {}

is valid because all three members of the union are uninitialized in the union definition. This statement is equivalent to

DB 4 DUP (?)

In this example, four bytes are reserved because the size of the union is the size of its largest member (in this case a DWORD). If the initialized member of the union is not the largest member of the union, Turbo Assembler makes up the difference by reserving space but not emitting data. For example,

BUNION {Z=l}

is equivalent to DB 1

DB 3 DUP (?)

Finally, multiple initialized members in a union produce an error. For example, this is illegal:

BUNION {X=l,Z=2}

Note that if two or more fields of the union have initial values in the union definition, then using the simple brace initializer ({ }) will also produce an error. The initializer must set all but one value to ? for a legal instance to be generated.

An alternative method of initializing structure and union instances is to use the bracket « » initializer. The values in the initializer are unnamed but are laid out in the same order as the corresponding members in the structure or union definition. Use this syntax for the bracket initializer:

< [value [,value ... JJ >

value represents the desired value of the corresponding member in the structure or union definition. A blank value indicates that you'll use the initial value of the member from the structure or union definition. A ? keyword indicates that the member should be uninitialized. For example,

ASTRUC <"abc", ,?>

is equivalent to DB "abc"

DW 1 DD ?

If you specify fewer values than there are members, Turbo Assembler finishes the instance by using the initial values from the structure or union definition for the remaining members.

ASTRUC <"abc"> iSarne as ASTRUC <"abc",,>

When you use the bracket initializer, give special consideration to nested structures and unions. The bracket initializer expects an additional matching pair of angle brackets for every level of nesting, so that Turbo Assembler will treat the nested structure or union initializer as a single entity (to match the value in the instance). Alternatively, you can skip an entire level of nesting by leaving the corresponding entry blank (for the default value of the nested structure or union), or by specifying the?

keyword (for an uninitialized nested structure or union). For example, examine the following nested structure and union:

CUNION STRUC CTYPE DB?

UNION iStart of union ilf CTYPE=O, use this ...

STRUC

ENDS

CTOPARl DW 1 CTOPAR2 DB 2 ilf CTYPE=l, use this ...

STRUC

ENDS ENDS ENDS

CTIPARl DB 3 CTIPAR2 DD 4 iEnd of union

iEnd of structure data type

The bracket initializer for this complex structure/union has two levels of nesting. This nesting must appear as matched angle brackets within the initializer, like

CUNION <O,«2,>,?»

This directive is equivalent to DB 0

DW 2 DB 2 DB 2 DUP (?)

Im Dokument . TUrbo Assembler" (Seite 160-164)