?
Aus dem NASM-Docu:
4.8.5 STRUC and ENDSTRUC : Declaring Structure Data Types
The core of NASM contains no intrinsic means of defining data structures; instead, the preprocessor
is sufficiently powerful that data structures can be implemented as a set of macros. The macros
STRUC and ENDSTRUC are used to define a structure data type.
STRUC takes one parameter, which is the name of the data type. This name is defined as a symbol
with the value zero, and also has the suffix _size appended to it and is then defined as an EQU
giving the size of the structure. Once STRUC has been issued, you are defining the structure, and
should define fields using the RESB family of pseudo-instructions, and then invoke ENDSTRUC to
finish the definition.
For example, to define a structure called mytype containing a longword, a word, a byte and a
string of bytes, you might code
struc mytype
mt_long: resd 1
mt_word: resw 1
mt_byte: resb 1
mt_str: resb 32
endstruc
The above code defines six symbols: mt_long as 0 (the offset from the beginning of a mytype
structure to the longword field), mt_word as 4, mt_byte as 6, mt_str as 7, mytype_size as
39, and mytype itself as zero.
The reason why the structure type name is defined at zero is a side effect of allowing structures to
work with the local label mechanism: if your structure members tend to have the same names in
more than one structure, you can define the above structure like this:
struc mytype
.long: resd 1
.word: resw 1
.byte: resb 1
.str: resb 32
endstruc
Thisd efinest heo ffsetst ot hes tructuref ieldsa sm ytype.long ,m ytype.word ,m ytype.byte
and mytype.str .
59
NASM, since it has no intrinsic structure support, does not support any form of period notation to
refer to the elements of a structure once you have one (except the above local-label notation), so
code such as mov ax,[mystruc.mt_word] is not valid. mt_word is a constant just like any
other constant, so the correct syntax is mov ax,[mystruc+mt_word] or
mov ax,[mystruc+mytype.word] .
4.8.6 ISTRUC , AT and IEND : Declaring Instances of Structures
Having defined a structure type, the next thing you typically want to do is to declare instances of
that structure in your data segment. NASM provides an easy way to do this in the ISTRUC
mechanism. To declare a structure of type mytype in a program, you code something like this:
mystruc:
istruc mytype
at mt_long, dd 123456
at mt_word, dw 1024
at mt_byte, db ’x’
at mt_str, db ’hello, world’, 13, 10, 0
iend
The function of the AT macro is to make use of the TIMES prefix to advance the assembly position
to the correct point for the specified structure field, and then to declare the specified data. Therefore
the structure fields must be declared in the same order as they were specified in the structure
definition.
If the data to go in a structure field requires more than one source line to specify, the remaining
source lines can easily come after the AT line. For example:
at mt_str, db 123,134,145,156,167,178,189
db 190,100,0
Depending on personal taste, you can also omit the code part of the AT line completely, and start the
structure field on the next line:
at mt_str
db ’hello, world’
db 13,10,0