Introduction
A macro instruction (abbr. macro) is a notational convenience for the programmer. A macro represents a commonly used group of statements in the source programming language. The macro processor replaces each macro instruction with the corresponding group of source language statements.
• This is called expanding the macros.
2
1
Introduction (cont’d)
The mostly common use of macro processors is in assembler language programming. The design of a macro processor is not directly related to the architecture of the computer on which it is to run.
3
Macro definition
Two new assembler directives are used in macro definition. macro_name MACRO ¶1, ¶2 ...... MEND
• •
The macro name and parameters define a pattern or prototype for the macro instructions used by the programmer. Following the MACRO directive are the statements that make up the body of the macro definition.
4
2
Macro invocation
A macro invocation statement gives the name of the macro instruction being invoked and the arguments to be used in expanding the macro. The process of macro invocation and subroutine call are quite different.
• •
The statements that form the expansion of a macro are generated (and assembled) each time the macro in invoked. Statements in a subroutine appear only once, regardless of how many times the subroutine is called.
5
A macro definition
Use of Macro
Macro invocation
3
Macro expansion
The macro instruction definitions are deleted since they are no longer needed after the macros are expanded. Each macro invocation statement is expanded into the statements that form the body of the macro, with the arguments from the macro invocation substituted for the parameters in the macro prototype.
•
Can a label appear in the body of a macro? Yes
7
Macro expansion (cont’d)
After macro processing, the expanded file can be used as input to the assembler.
Macro