ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

学 Win32 汇编[31] - 结构与联合

2021-04-30 12:08:00  阅读:350  来源: 互联网

标签:汇编 main lib PrintDec 31 includelib Win32 include inc


学 Win32 汇编[31] - 结构与联合


结构和联合分别用 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
posted on 2010-04-22 15:26  万一  阅读(2254)  评论(0)  编辑  收藏

标签:汇编,main,lib,PrintDec,31,includelib,Win32,include,inc
来源: https://blog.51cto.com/u_14617575/2745486

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有