Macro Processors
1
Introduction
Concept
» A macro instruction is a notational convenience for the programmer
» It allows the programmer to write shorthand version of a program
(module programming)
» The macro processor replaces each macro invocation with the
corresponding sequence of statements (expanding)
2
Introduction
» The MACRO statement identifies the beginning of a macro definition
» •The symbol in the label field is the name of the instruction
» •The entries in the operand field identify the parameter of the macro instruction
» •Each parameter begins with the character &
» •The MEND assembler directive marks the end of the macro definition
» •A macro invocation statement gives the name of the macro instruction being
invoked and the arguments to be used in expanding the macro
4
Basic macro processor
functions :Macro Definition &
Expansion
* Two new assembler directives are used in macro definition-
» MACRO:identify the beginning of a macro definition-MEND:identify the
end of a macro definition
» * Prototype for the macro
– Each parameter begins with ‘&’
Name MACRO Parameters
:
Body
:
MEND
– Body: the statements that will be generated as the
expansion of themacro.
5
Macro expansion
Each macro invocation statement will be expanded
into the statements that form the body of the macro.
Arguments from the macro invocation are substituted
for the parameters in the macro prototype (according
to their positions).
» In the definition of macro: parameter
» In the macro invocation: argument
6
Macro vs. Subroutine
Macro
» the statement of expansion are generated each time the
macro are invoked
Subroutine
» the statement in a subroutine appears only once
8
Macro Processor
Recognize macro definitions
Save the macro definition
Recognize macro calls
Expand macro calls
Source
Macro Expanded Compiler or obj
Code
(with macro) Processor Code Assembler
9
Expanded program
14
15
Macro Definition
copy code
parameter substitution
conditional macro expansion
macro instruction defining macros
16
Copy code -- Example
Source Expanded source
STRG MACRO .
STA DATA1 .
STB DATA2 .
{
STX DATA3 STA DATA1
MEND STB DATA2
. STX DATA3
STRG .
{
. STA DATA1
STRG STB DATA2
. STX DATA3
. .
17
Parameter Substitution --
Example
Source Expanded souce
STRG MACRO &a1, &a2, &a3 .
STA &a1 .
STB &a2 .
{
STX &a3 STA DATA1
MEND STB DATA2
. STX DATA3
STRG DATA1, DATA2, DATA3 .
{
. STA DATA4
STRG DATA4, DATA5, DATA6 STB DATA5
. STX DATA6
. .
18
Parameter Substitution
» Positional argument
STRG DATA1, DATA2, DATA3
GENER ,,DIRECT,,,,,,3
» Keyword argument
STRG &a3=DATA1, &a2=DATA2, &a1=DATA3
GENER TYPE=DIRECT, CHANNEL=3
19
Conditional macro expansion
20
21
22
23
Nested macro calls
24
Nested macro definition
25
One-Pass Macro Processor
Prerequisite
» every macro must be defined before it is called
Sub-procedures
» macro definition: DEFINE
» macro invocation: EXPAND
NAMTAB
MACRO DEFINE DEFTAB
PROCESSLINE
CALL
EXPAND ARGTAB
26
28
29
Data Structures -- Global
Variables
DEFTAB
NAMTAB
ARGTAB
EXPANDING
30