Macro Definition and Invocation 

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.
The directives = and EQU can be used for constant substitution during assembly time. For example, we can write

   CLASS_SIZE = 90
Both 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
Here 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.

 Position of Macro Definition

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.

Example: Define a macro to move a word into a word.
          PUSH WORD2
          POP  WORD1

Example: Invoke the macro MOVW to move B to A, where A and B are word variables.
 MOVW   A, B

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


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.

Example: Define a macro to shift left an operand to the left by an immediate value that can be greater than 1.
 SHLI   MACRO operand, count
          MOV  CL, count
          SHL  operand, CL

Example: Invoke the macro SHLI to shift left regiter AX by 6 bits.
 SHLI   AX, 6

This macro invocation will be expanded into the following code:

	MOV CL, 6