您的位置:首页 > 其它

Win32汇编学习[7]: 定义符号常量(=、EQU、TEXTEQU)

2011-12-04 14:14 525 查看
关于符号常量 =的例子

.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

.data

n = 1 ; = 伪指令只能定义整数或整数表达式

n = n + 1; 可重复定义

.code

main proc

PrintDec n ;2

ret

main endp

end
main

-------------------------------------------------------------------------------------------------------------------------------------------

Equ的一个例子

------------------------------------------------------------------------------------------------------------------------------------------

; Test13_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

.data

num1 equ 2 ; equ 可以定义整数和整数表达式

num2 equ 3 ; 它不能像 = 一样重复定义

num3 equ num1+num2 ; (我尝试了重复定义, 竟然有时也可以, 只是偶尔)

txt equ <'Asm'> ; 可用 equ 定义字符串, 应使用 <>

szTxt db txt, 0 ; 使用 equ 定义的常量

.code

main proc

PrintDec num3 ;5

PrintString szTxt ;Asm

ret

main endp

end
main

------------------------------------------------------------------------------------------------------------------------------------------

Equ函数宏定义

------------------------------------------------------------------------------------------------------------------------------------------

.386

.model flat,stdcall

include
windows.inc

include
kernel32.inc

includelib
kernel32.lib

include
user32.inc

includelib
user32.lib

; 用 equ 给函数重命名Msg equMessageBox

Exit equ <ExitProcess> ;尖括号可保证字符串的完整、并避免和关键字冲突

; 甚至代替整个表达式

ShowMsg equ <invoke MessageBox, NULL,addr
szMsg,addr szCaption, MB_OK>

.data

szMsg db 'Hello World!', 0

szCaption db 'Hi', 0

.code

main proc

invoke
Msg, NULL,addr szMsg,addr
szCaption, MB_OK

ShowMsg

invoke Exit, NULL

main endp

end
main

------------------------------------------------------------------------------------------------------------------------------------------

textequ

------------------------------------------------------------------------------------------------------------------------------------------

; Test13_4.asm

.386

.modelflat,stdcall

include
windows.inc

include
kernel32.inc

includelib
kernel32.lib

include
user32.inc

includelib
user32.lib

Msg textequ MessageBox

Exit textequ <ExitProcess>

ShowMsg textequ <invoke MessageBox, NULL,addr szMsg,addr szCaption, MB_OK>

.data

szMsg db 'Hello World!', 0

szCaption db 'Hi', 0

.code

main proc

invoke
Msg, NULL,addr szMsg,addr
szCaption, MB_OK

ShowMsg

invoke Exit, NULL

main endp

end
main

------------------------------------------------------------------------------------------------------------------------------------------

.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

.data

num1 equ 2 ; 这里的 equ 换做 textequ 不行

num2 textequ <3> ; 如果用 textequ 定义整数或表达式也要放在 <> 中

num3 textequ <num1+num2> ; 这曾是 num3 equ num1+num2

txt textequ <'Asm'> ;

txt textequ <'Delphi'> ; textequ 可重复定义

szTxt db txt, 0

.code

main proc

PrintDec num3 ;5

PrintString szTxt ;Delphi

ret

main endp

end
main

------------------------------------------------------------------------------------------------------

哈哈 强大的宏替换吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: