📄 macro.inc
字号:
;==============================================================================
; 文 件 名: macro.inc
; 版 本: Beta
; 说 明: nasm汇编语言宏定义文件
; 编 译: NASM 0.98 以上编译器
; 编译命令: 该文件不能被直接编译, 需要使用此文件的宏请用%include "macro.inc"
; Bats Studio 2004-7。
;==============================================================================
%ifndef _MACRO_INC_
%define _MACRO_INC_
;==============================================================================
;.ERROR 错误处理
;参数2-3个
;2个参数为调试模式下的提示信息, 如果3个参数,则无论何种模式均会提示错误.
;==============================================================================
%macro .ERROR 2-3
%if %0=3
%ifdef _CHINESE
%error %2
%else
%error %1
%endif
%else
%ifdef DEBUG
%ifdef _CHINESE
%error %2
%else
%error %1
%endif
%endif
%endif
%endmacro
;========================================================================
;.struc
;.endst
; 定义一个结构体
;如:
; .struc MyStru
; char str,30 ;定义一个字符数组,长度为30
; unsigned num1 ;定义一个整形无符号变量(2字节)
; long num2 ;定义一个长整形变量(4字节)
; sint num3 ;定义一个整形变量,带符号(2字节)
; ulong num4 ;定义一个长整形无符号变量(4字节)
; .endst MyStru
;当你需要引用这个结构体的时候可以用下面方法:
; MyStru TEST,30 ;引用结构体30次。当然,你也可以不加后面的数字
;相当于 TASM中的:
; TEST MyStru 30 DUP (<>)
;当你需要从这个结构体的变量中取值或者付值时,可以使用:
; mov ax,[TEST .num1] ;取 TEST.num1的值
; mov si,TEST ;取 TEST 的地址
; mov di,TEST .num4 ;取 TEST.num4的地址
; mov eax,[TEST .num4] ;取 TEST.num4的值
; mov cx,size_MyStru ;取结构体的的字节数
;注意:如果取结构体成员的值或地址,必须在结构体名和结构体成员之间加上空格
;========================================================================
%macro .struc 1
%ifndef STRUC_FLAGE
%ifndef INUSE_%1
%define INUSE_%1 INUSE
%define STRU_%1 0
%define STRUC_NAME %1
%define STRUC_FLAGE
%define .%1 __DUP__ %1,
%endif
%else
.ERROR "ERROR: You mast use .endst","错误:在上一个结构体未结束以前,你必须先使用.endst",TRUE
%endif
%endmacro
%macro __DUP__ 3
%ifidn INUSE_%1,INUSE
%if %3=1
@%2 EQU $-0
times size_%1 db 0
size_%2 EQU $-@%2
%else
%assign j 0
@%2 EQU $-0
%rep %3
_TIME_DUP_ %1,%2,j
%assign j j+1
%endrep
size_%2 EQU $-@%2
%endif
%else
.ERROR "ERROR: redefine symbol!","错误:对象被重新定义",TRUE
%endif
%endmacro
%macro _TIME_DUP_ 3
%2.%3 EQU $-0
times size_%1 db 0
%endmacro
%macro .endst 1
%ifidn STRUC_NAME,%1
%undef STRUC_FLAGE
%undef STRUC_NAME
size_%1 EQU STRU_%1
%undef STRU_%1
%else
.ERROR "ERROR: .endstruc be for .struc","错误:.endstruc 之前必须有 .struc",TRUE
%endif
%endmacro
;===================================================================================
;下面的宏包括:
;char 定义一个字符串或者字符变量
; 如:
; char a,255 定义一个字符数组
; char a,"This is a test program" 定义一个字符串
; char a,'T' 定义一个字符变量
;
;sint 定义一个带符号整形变量
; 如:
; sint b 定义不初始化的整形变量
; sint b,5 与C语言的 int b=5 类似
; @是取地址符,.是取这个变量的值
; mov si,@b 取变量B的地址
; mov ax,.b 将会被展开成 mov ax,word [@b]
;以下各宏使用方式雷同
;unsigned 定义一个无符号整形变量
;long 定义一个带符号长整形变量
;ulong 定义一个无符号长整形变量
;S_BYTE 这个宏请不要直接使用,它用于 .struc
;.LABEL.GIVE. 也不能直接使用
;===================================================================================
%macro S_BYTE 1-*
where_%2 EQU STRU_%1
%define STRU_%1 where_%2+%3
%define .%2 +where_%2
size_%2 EQU %3
%endmacro
%macro char 1-2
%ifdef STRUC_FLAGE
%if %0=2
%ifnum %2
S_BYTE STRUC_NAME,%1,%2
%else
.ERROR "You mast use 'char label,number'","错误:你必须使用'char 标号,数值",TRUE
%endif
%else
S_BYTE STRUC_NAME,%1,1
%endif
%else
%ifndef INUSE_%1
%define INUSE_%1
%ifstr %2
.LABEL.GIVE. @%1
db %2,0
size_%1 EQU $-@%1-1
%define .%1 byte [@%1]
%else
%ifnum %2
dw @%1
.LABEL.GIVE. @%1
times %2+1 db 0
size_%1 EQU %2
%define .%1 word [@%1-2]
%else
.LABEL.GIVE. @%1
db 0,0
%define .%1 byte [@%1]
%endif
%endif
%else
.ERROR "ERROR: symbol redefine","错误:重复定义标号",TRUE
%endif
%endif
%endmacro
%macro .LABEL.GIVE. 1
%ifdef .MZFILE.
%1 EQU $-$$
%else
%ifdef Mode_Small
%1 EQU $+0x100
%else
%1 EQU $-0
%endif
%endif
%endmacro
%macro sint 1-3
%ifdef STRUC_FLAGE
S_BYTE STRUC_NAME,%1,2
%else
%ifndef INUSE_%1
%define INUSE_%1
.LABEL.GIVE. @%1
%ifnum %2
dw %2
%else
dw 0
%endif
%define .%1 word [@%1]
%else
.ERROR "ERROR: symbol redefine","错误:重复定义标号",TRUE
%endif
%endif
%endmacro
%macro unsigned 1-3
%ifdef STRUC_FLAGE
S_BYTE STRUC_NAME,%1,2
%else
%ifndef INUSE_%1
%define INUSE_%1
.LABEL.GIVE. @%1
%ifnum %2
dw %2
%else
dw 0
%endif
%define .%1 word [@%1]
%else
.ERROR "ERROR: symbol redefine","错误:重复定义标号",TRUE
%endif
%endif
%endmacro
%macro long 1-2
%ifdef STRUC_FLAGE
S_BYTE STRUC_NAME,%1,4
%else
%ifndef INUSE_%1
%define INUSE_%1
.LABEL.GIVE. @%1
%ifnum %2
dw %2
%else
dw 0
%endif
%define .%1 dword [@%1]
%else
.ERROR "ERROR: symbol redefine11111","错误:重复定义标号",TRUE
%endif
%endif
%endmacro
%macro ulong 1-2
%ifdef STRUC_FLAGE
S_BYTE STRUC_NAME,%1,4
%else
%ifndef INUSE_%1
%define INUSE_%1
.LABEL.GIVE. @%1
%ifnum %2
dw %2
%else
dw 0
%endif
%define .%1 dword [@%1]
%else
.ERROR "ERROR: symbol redefine","错误:重复定义标号",TRUE
%endif
%endif
%endmacro
;==================================================
;xpush,xpop
;使用方法:
; xpush ax,bx,cx,dx
; xpop ax,bx,cx,dx
; xpop 语句将自动按照反方向弹出寄存器
;==================================================
%macro xpush 1-*
%rep %0
push %1
%rotate 1
%endrep
%endmacro
%macro xpop 1-*
%rep %0
%rotate -1
pop %1
%endrep
%endmacro
%macro SETADD 1
times %1-($-$$) db 0
%endmacro
%endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -