A macro is defined by a MACRO assembler directive and the definition is terminated by an ENDM directive. The macro can take parameters, in the form of a comma-delimited list, after the name of the macro and the directive. These formal parameters will be substituted by the actual parameters during macro expansion. Unlike procedures, the actual text passed as parameters is substituted when encountered in the body of the macro.
Macros can be defined anywhere and used anywhere in the program body. The only restriction is that the macro must be defined before it is used.
Macros can be nested. One macro can have another macro call within its body. A macro can also be defined within the body of another macro.
Unique labels and data definitions can be quite tricky within a macro body since the text is eventually duplicated in the source. This normally leads to duplicate definition errors. To solve this problem, there is a local directive which can be placed just after the MACRO statement to list the local variables, in a comma-delimited list. These variables are then given unique names each time the procedure is called.
DOSSEG
.MODEL SMALL
.STACK 4096
DisplayMsg MACRO MessageAddr ; define macro to display message
lea dx,MessageAddr ; standard message display code
mov ah,9
int 21h
ENDM
.DATA
Msg1 db 'Sample Program',13,10,'$'
Msg2 db 'Hello World',13,10,'$'
.CODE
ProgramStart:
mov ax,SEG _DATA ; set up data segment
mov ds,ax
DisplayMsg Msg1 ; display first message
DisplayMsg Msg2 ; display second message
mov ah,4ch ; terminate program
int 21h
END ProgramStart
DOSSEG
.MODEL SMALL
.STACK 4096
.DATA
Msg1 db 'Monochrome monitor',13,10,'$'
Msg2 db 'Colour monitor',13,10,'$'
DisplayMsg MACRO MessageAddr ; define macro to display message
lea dx,MessageAddr ; standard message display code
mov ah,9
int 21h
ENDM
CheckVideo MACRO ; define macro to check video mode
local colour, end ; define local labels
mov ax,0040h ; set segment of BDA
mov es,ax
mov ax,es:[0010h] ; get equipment list
and ax,0030h ; check for monochrome startup display mode
cmp ax,0030h
jne colour
DisplayMsg Msg1 ; mono output part
jmp end
colour:
DisplayMsg Msg2 ; colour output part
end:
ENDM
.CODE
ProgramStart:
mov ax,SEG _DATA ; set up data segment
mov ds,ax
CheckVideo ; check up video mode
CheckVideo ; check up video mode
mov ah,4ch ; terminate program
int 21h
END ProgramStart