A macro is a block of text (code, data, etc.) that has been given a name (called a macro name ). When the assembler encounters that name in a program, the block of text associated with the macro name is subsituted. This process is referred to as macro expansion .
Assemblers provide three directives to support macro substitutions:
=, EQU, and MACRO.
CLASS_SIZE = 90 or CLASS_SIZE EQU 90Both statements cause 90 to be textually substituded for CLASS_SIZE.
The difference between = and EQU directives is that the = directive allows redifinition later on, while the constants defined by EQU cannot be redefined. However, both these directives are not useful for general text subsitution.
The syntax of macro definition is
macro_name MACRO [parameter1, parameter2, ...] macro body ENDMHere macro_name is the user-supplied name for the macro. The pseudo-ops MACRO and ENDM indicate the beginning and end of the macro definition. In the MACRO directive, the parameters are optional (as indicated by the square brackets ).
To invoke or call a macro, use the macro_name and supply the necessary parameter values. This causes a macro expansion, i.e. it copies the macro body into the program at the position of the invocation, just as if the user has typed them in. The format for invoking a macro is
macro_name [argument1, argument2, ...]During macro expansion, the assembler replaces each parameter in the macro body by its corresponding argument.
One use of macros is to create new instructions. For example, we know that the operands of a MOV instruction can't both be word variables, but we can get arond this restriction by defining a macro to move a word into a word.
To expand this macro, the assembler would copy the macro statement into the program at the position of the call, replacing the occurence of WORD1 with A and WORD2 by B. The result is
PUSH B POP A
The 8086 processor does not allow specification of shift count great than 1 as an immediate value. Next, we will write a shift left macro that allows that. We will call this macro SHLI.
This macro invocation will be expanded into the following code:
MOV CL, 6 SHL AX, CL