| UMBC CMSC 211 |
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:
birthday MyDate <> ; 1-0-1991
monthOfSundays MyDate <,8,> ; this only puts a value into the field month
.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:
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.