Structures And Unions
Structures
A structure is a named variable that contains other variables, called
fields The structure begins with the name you want,
for example, MyStruct, and the keyword STRUCT. A matching keyword
ENDS follows the last field in the structure. You attach
a copy of the structure's name and ENDS. For example, a structure
that contains three fields representing the date would be:
myDate STRUCT
day db 1 ; Day field -- default value = 1
month db ? ; Month field -- no default value
year dw 1991 ; Year field -- default value = 1991
myDate ENDS
You can insert fields of any type inside a structure, using the same
methods you use to declare plain variables. This example has three
fields: day, month, and year. The first two fields
are single bytes, with the first one initialized to 1. The second
byte field is uninitialized. The third field is a word, initialized to
1991. The indentation of each field is purely for show -- makes it
easier to read! When defining structures such as this, remember
these important points:
- A structure is not a variable. An structure is a schematic for
a variable, just like in C.
- A structure tells the assembler about the design of variables
that you plan to declare later on.
- Even though you use directives such as db and dw
to define the types of the structure's data field, the structure
does not reserve space in the data segment or cause any bytes to be
written to the data segment or cause any bytes to be
written to the finished program. This is just like the typedef
in C.
Declaring Structured Variable
To use a structure design, you must reserve space in memory for the structure's
fields. The result is a variable that has the design of the the structure.
Start each such variable declaration with a label, followed by the
structure name, and ending with a list of default values
in angle brackets, < and >. Leave the brackets empty to use
the default values (if any) defined earlier in the structure definition.
birthday MyDate <> ; 1-0-1991
A label birthday starts the variable declaration. Next comes the
structure name myDate at the same place you would normally use
simple directives like dw. The empty angle brackets cause this
date's field to assume the default values declared in the structure.
If you want to initialize on certain fields, you must make sure you
do not provide a value, but include the comma, so that the data and
fields line up correctly:
monthOfSundays MyDate <,8,> ; this only puts a value into the field month
Example
.model small
.stack 100h
.data
MyDate STRUCT
day db 1
month db ?
year dw 1991
Mydate ENDS ; This is our template, like typedef in C
today MyDate <16, 10, 2001> ;Notice how to give it is values
someday MyDate < , , 2005> ; Only specifying a different year
msg db 'Today is $'
.code
EXTRN PutDec : NEAR
strudemo PROC
include pcmac.inc
_begin
_PutStr msg
mov al, [today.day]
mov ah, 0
call PutDec
_PutCh '/'
mov al, [today.month]
mov ah, 0
call PutDec
_PutCh '/'
mov ax, [today.year]
call PutDec
_PutCh '.', 10, 13
_exit
strudemo ENDP
END strudemo
The output was:
Today is 16/10/2001.
Unions
Defined with a UNION directive, a union has the identical form
as a STRUCT structure. Like structures, unions contain named fields,
often of different data types. The difference between a union and a
structure is that unions overlay each other within in variable.
A union with three byte fields, in other words, actually
occupies only a single byte.
ByteWord UNION
aByte db ?
aWord dw ?
ByteWord ENDS
The ENDS directive ends the union. In this example, aByte
overlays the first byte of aWord. Because this is a union,
aByte and aWord are stored in the same
location in memory. Changing the value of aByte also changes
the LSB of aWord. You can think of the AL and AH registers
as in a union with AX, which in turn is in a union with EAX.
UMBC |
CSEE