结构和联合分别用 struct、union 定义, 都是 ends 结束定义.
它们使用方法相同, 并可以互相嵌套; 主要区别是后者的各成员共用同一个地址.
结构示例:
; Test31_1.asm
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
;定义结构
MyPoint struct
X dd ?
Y dd ?
MyPoint ends
.data
;声明结构, 使用 <>、{} 均可
pt1 MyPoint <>
pt2 MyPoint {}
pt3 MyPoint <33,44>
pt4 MyPoint {55,66}
pt5 MyPoint {77,}
pt6 MyPoint {,88}
pt7 MyPoint {,}
.code
main proc
mov pt1.X, 11
mov pt1.Y, 22
inc pt1.X
PrintDec pt1.X ;12
PrintDec pt1.Y ;22
;
PrintDec pt2.X ;0
PrintDec pt2.Y ;0
;
PrintDec pt3.X ;33
PrintDec pt3.Y ;44
;
PrintDec pt4.X ;55
PrintDec pt4.Y ;66
;
PrintDec pt5.X ;77
PrintDec pt5.Y ;0
;
PrintDec pt6.X ;0
PrintDec pt6.Y ;88
;
PrintDec pt7.X ;0
PrintDec pt7.Y ;0
ret
main endp
end main
联合示例:
; Test31_2.asm
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
;定义联合
MyUnion union
D dd ?
W dw ?
B db ?
MyUnion ends
.data
u1 MyUnion <>
u2 MyUnion {1122h} ;只能有一个初始值
.code
main proc
mov u1.D, 12345678h
PrintHex u1.D ;12345678
PrintHex u1.W ;5678
PrintHex u1.B ;78
;
PrintHex u2.D ;00001122
PrintHex u2.W ;1122
PrintHex u2.B ;22
ret
main endp
end main
用寄存器引用结构变量:
; Test31_3.asm
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
MyPoint struct
X dd ?
Y dd ?
MyPoint ends
.data
pt1 MyPoint <11,22>
.code
main proc
lea ebx, pt1
PrintDec (MyPoint ptr [ebx]).X ;11
PrintDec (MyPoint ptr [ebx]).Y ;22
ret
main endp
end main
定义时的数据初始化:
; Test31_4.asm
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
MyStruct struct
FName db 12 dup(0)
FAge db 66
MyStruct ends
.data
u1 MyStruct <>
u2 MyStruct <'张三', 88>
.code
main proc
PrintString u1.FName ;
PrintDec u1.FAge ;66
PrintString u2.FName ;张三
PrintDec u2.FAge ;88
ret
main endp
end main
使用 SYSTEMTIME 结构获取系统时间的例子:
; Test31_5.asm
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
;SYSTEMTIME 结构定义在 windows.inc
;GetLocalTime 函数声明在 kernel32.inc
.data
sysTime SYSTEMTIME <>
.code
main proc
invoke GetLocalTime, addr sysTime
PrintDec sysTime.wYear ;2010
PrintDec sysTime.wMonth ;4
PrintDec sysTime.wDay ;22
ret
main endp
end main
结构嵌套:
; Test31_6.asm
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
Pt struct
X dd ?
Y dd ?
Pt ends
Rect struct
Left Pt <>
Right Pt <>
Rect ends
.data
r1 Rect <>
r2 Rect <<55,66>, <77,88>>
.code
main proc
mov r1.Left.X, 11
mov r1.Left.Y, 22
mov r1.Right.X, 33
mov r1.Right.Y, 44
PrintDec r1.Left.X ;11
PrintDec r1.Left.Y ;22
PrintDec r1.Right.X ;33
PrintDec r1.Right.Y ;44
PrintDec r2.Left.X ;55
PrintDec r2.Left.Y ;66
PrintDec r2.Right.X ;77
PrintDec r2.Right.Y ;88
ret
main endp
end main