Macro and Macro Assembler in Assembly Language Programming

A macro is a sequence of instructions to which a name is assigned. Together with the name, certain parameters are associated i.e. it is similar to functions used in high level programming languages. These parameters, also known as formal or dummy parameters, are strings of characters which appear in various fields of instructions comprising the macro. Programmer can call macro anywhere in the program using the macro name and its parameters.
The macro assembler is a program that replaces the macro call by the sequence of instructions that defines the macro by replacing the formal parameters used in the macro with the actual parameters used in the macro call. There are various kinds of macro assemblers but Microsoft Macro Assembler (MASM) and Netwide Assembler (NASM) are widely used.

Example:
Suppose a programmer needs to add two numbers repeatedly in the assembly program he can create a macro as follows:
MACRO
ADDTWO X, Y         ; Macro name and formal parameter
LDA X                        ; Get first operand
ADD Y                        ; Add second operand
MOV B, A                  ;  Copy result in register B
ENDM

This macro can be used by programmer anywhere in the program by calling macro name and giving actual parameters:

ADDTWO FIRST, SECOND
Here, FIRST and SECOND are the actual parameters.
Now the macro assembler inserts the sequence of ADDTWO macro in the main program by replacing formal parameters (X and Y) with actual parameters as follows:

LDA FIRST             ; Get first operand
ADD SECOND       ;  Add second operand
MOV B, A               ;  Copy result in register B

You may also like...