Macro Processor-notes
Macro Processor-notes
MACRO PREPROCESSOR
Macro
A macro (or macro instruction)
– It is simply a notational convenience for the programmer.
– It allows the programmer to write shorthand version of a program
– It represents a commonly used group of statements in the source program.
For example:
Suppose a program needs to add two numbers frequently. This requires a sequence
of instructions. We can define and use a macro called SUM, to
represent this sequence of instructions.
The fundamental functions common to all macro processors are: ( Code to remember - DIE )
Page 1
Unit
4
Macro Definition
Macro definitions are typically located at the start of a program.
A macro definition is enclosed between a macro header statement(MACRO) and a
macro end statement(MEND)
Format of macro definition
macroname MACRO parameters
:
body
:
MEND
Body of macro consists of statements that will generated as the expansion of macro.
Consider the following macro definition:
SUM MACRO &X,&Y
LDA &X
MOV B
LDA &Y
ADD B
MEND
Page 2
Unit
4
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.
The arguments and parameters are associated with one another according to their
positions. The first argument in the macro invocation corresponds to the first
parameter in the macro prototype, etc.
Comment lines within the macro body have been deleted, but comments on individual
statements have been retained. Macro invocation statement itself has been included
as a comment line.
Consider the example for macro expansion on next page:
In this example, the macro named SUM is defined at the start of the program. This
macro is invoked with the macro call SUM P,Q and the macro is expanded as
LDA &P
MOV B
LDA &Q
ADD B
MEND
Again the same macro is invoked with the macro call SUM M,N and the macro is expanded
as
LDA &M
MOV B
LDA &N
ADD B
MEND
Page 3
Unit
4
Page 5
Unit
4
Definition table
(DEFTAB)
All the macro definitions in the program are stored in DEFTAB, which includes
macro prototype and macro body statements.
Comment lines from macro definition are not entered into DEFTAB because they will
not be a part of macro expansion.
References to the macro instruction parameters are converted to a positional notation
for efficiency in substituting arguments.
4500
ADD
B
………….
SUM
END
P,Qmacro definition for SUM is encountered, the macro name SUM along with
When the
LDA X and Y are entered into DEFTAB. Then the statements in the body of
its parameters
macro 3000
is also entered into DEFTAB. The positional notation is used for the
parameters. The parameter &X has been converted to ?1, &Y has been converted to
?2.
The macro name SUM is entered into NAMTAB and the beginning and end pointers
are also marked.
On processing the input code, opcode in each statement is compared with the
NAMTAB, to check whether it is a macro call. When the macro call SUM P,Q is
recognized, the arguments P and Q will entered into ARGTAB. The macro is
expanded by taking the statements from DEFTAB using the beginning and end
pointers of NAMTAB.
When the ?n notation is recognized in a line from DEFTAB, the corresponding
argument is taken from ARGTAB.
Page 7
Unit
4
Page 8
Unit
4
Page 9
Unit
4
Explanation of algorithm
The algorithm uses 5 procedures
o MACROPROCESSOR (main function)
o DEFINE
o EXPAND
o PROCESSLINE
o GETLINE
Page 10
Unit
4
Page 11
Unit
4
To deal with Nested macro definitions DEFINE procedure maintains a counter named
LEVEL.
o When the assembler directive MACRO is read, the value of LEVEL is
incremented by 1
o When MEND directive is read, the value of LEVEL is decremented by 1
o That is, whenever a new macro definition is encountered within the current
definition, the value of LEVEL will be incremented and the while loop which
is used to process the macro definition will terminate only after the value of
LEVEL =0. With this we can ensure the nested macro definitions are properly
handled.
EXPAND
Page 12
Unit
4
Page 13
Unit
4
GETLINE
This procedure is used to get the next line.
If EXPANDING = TRUE, the next line is fetched from DEFTAB. (It means we are
expanding the macro call)
If EXPANDING = False, the next line is read from input file.
Page 14
Unit
4
For example, suppose that the parameter to such a macro instruction is named &ID.
The body of the macro definition contain a statement like LDA X&ID1, which means
&ID is concatenated after the string “X” and before the string “1”
TOTAL MACRO &ID
LDA X&ID1
ADD X&ID2
STA X&ID3
MEND
Page 15
Unit
4
Page 16
Unit
4
Most of the macro processors deal with this problem by providing a special
concatenation operator to specify the end of parameter.
Thus LDA X&ID1 can be rewritten as . So that end of the parameter
&ID is clearly defined.
Example:
The example given above shows a macro definition that uses the concatenation
operator as previously described. The statement TOTAL A and TOTAL BETA shows
the invocation statements and the corresponding macro expansion.
Themacroprocessor deletes all occurrences of the operator
concatenation immediately after performing parameter substitution, so will not
the symbol appear in the macro expansion.
Page 17
Unit
4
Page 18
Unit
4
Page 19
Unit
4
Page 20
Unit
4
In the example, for the first invocation of COPY X,Y the label generated is $AALOOP and
for the second invocation COPY P,Q the label generated is $ABLOOP. Thus for each
invocation of macro unique label is generated.
Page 23
Unit
4
P
LDA 3
...................
ADD 4
STA Q
.........................
END
Implementation of Macro-time
looping(or Expansion time
looping) structure: WHILE-
ENDW
WHILE statement specifies that the following lines until the next ENDW statement,
are to be generated repeatedly as long as a particular condition is true. The testing
of this condition, and the looping are done during the macro under expansion.
When a WHILE statement is encountered during the expansion of a macro, the
specified Boolean expression is evaluated.
If expression is TRUE, the macro processor continues to process lines from
DEFTAB until it encounters the next ENDW statement.When ENDW is
encountered, the macro processor returns to the preceding WHILE, re-evaluates the
Boolean expression, and takes action based on the new value.
If the expression is FALSE, the macro processor skips ahead in DEFTAB
until it finds the next ENDW statement and then resumes normal macro expansion.
The example given below shows the usage of Macro-Time Looping
statement. In this example, the variable &LIMIT is a macro time variable.
Page 24
Unit
4
a) Positional Parameters
Parameters and arguments are associated according to their positions in the macro
prototype and invocation. The programmer must specify the arguments in proper
order.
Syntax : In macro definition,
macroname MACRO ¶meter1, ¶meter2,……
In macro invocation,
macroname argument1, argument2,….
Example: In macro definition,
EVAL MACRO &X, &Y
In macro invocation,
EVAL P,Q
Here &X recieves the value of P and &Y recieves the value of Q
If an argument is to be omitted, a null argument should be used to maintain the
proper order in macro invocation statement.
For example: Suppose a macro named EVAL has 5 possible parameters, but in a
particular invocation of the macro only the 1st and 4th parameters are to be
Page 25
Unit
4
Page 26
Unit
4
Page 27
Unit
4
Page 28
Unit
4
Then the macro call INCR X=A, Y=B will take the values A for parameter X, B for
parameter Y and R for the parameter Z.
The macro call INCR X=A, Y=B, Z=C will take the values A for parameter X, B for
parameter Y and C for the parameter Z.
Page 29
Unit
4
Page 31
Unit
4
Page 33
Unit
4
Advantages
Programmers do not need to learn many macro languages, so much of the time and
expense involved in training are eliminated.
Although its development costs are somewhat greater than those for a specific language
macro processor, this expense does not need to be repeated for each language, thus
save substantial overall cost.
Disadvantages
In spite of the advantages noted, there are still relatively few general purpose macro
processors. The reasons are:
Large number of details must be dealt with in a real programming language.
There are several situations in which normal macro parameter substitution or normal
macro expansion should not occur.
o For example, comments are usually ignored by the macro processor. But each
programming languages uses its own method for specifying comments. So a
general purpose macro processor should be designed with the capability for
identifying the comments in any programming languages.
Another problem is with the facilities for grouping together terms, expressions, or
statements.
o Some languages use keywords such as begin and end for grouping statements
while some other languages uses special characters like { }. A general purpose
macro processor may need to take these groupings into account in scanning the
source statements.
Another problem is with the identification of tokens of the programming languages. The
tokens means the identifiers, constants, operators and keywords in the programming
language. Different languages uses different rules for the formation of tokens. So the
design of a general purpose macro processor must take this into consideration.
Another problem is with the syntax used for macro definitions and macro invocation
Page 34
Unit
4
Page 35
Unit
4
Page 36
Unit
4
Page 37
Unit
4
Page 39
Unit
4
2. Nested Macros
Nested macros are macros in which definition of one macro contains definition of
other macros.Consider the macro definition example given below, which is used to
swap two numbers. The macro named SWAP defines another macro named STORE
inside it. These type of macro are called nested macros.
SWAP &X,&Y
MACRO
&
LDA
X &Y
LDX
&X,&Y
STORE
MACRO
STA &Y outer macro
STX &X Inner macro
MEND
MEND
3. Recursive Macro
Invocation of one macro by another macro is called recursive macro.Example
for recursive macro:
SUM MACRO &X,&Y
STA &X
ADD &Y
MEND
INPUT MACRO &A,&B
SUM MEND &A,&B .i n v o k i n g the macro
SUM
Page 40
Unit
4
Page 41