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


=、EQU、TEXTEQU 功能差不多, 它们定义的符号都将在编译前辈替换成它们指定的常量值.

相比之下 EQU 更好用些.

= 举例

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

    

.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 例二

; Test13_3.asm

.386

.model flat, stdcall



include    windows.inc

include    kernel32.inc

includelib kernel32.lib

include    user32.inc

includelib user32.lib



; 用 equ 给函数重命名

Msg  equ MessageBox

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

.model flat, 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


 
   

用 TEXTEQU 重做 EQU 的第一个例子(Test13_2.asm), 有区别:

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

    

.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


 
   

你可能感兴趣的:(Win32)