程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 學 Win32 匯編[31] - 結構與聯合

學 Win32 匯編[31] - 結構與聯合

編輯:Delphi

結構和聯合分別用 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 




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved