CH4 - Macroprocessors
CH4 - Macroprocessors
MACROPROCESSORS
body
MEND
o Body: the statements that will be generated as the expansion of the macro.
The figure shows the MACRO expansion. The left block shows the MACRO
definition and the right block shows the expanded macro replacing the MACRO call
with its block of executable instruction.
Fig 4.1
The statement M1 DATA1, DATA2 is a macro invocation statements that gives the
name of the macro instruction being invoked and the arguments (M1 and M2) to be
used in expanding. A macro invocation is referred as a Macro Call or Invocation.
Macro Expansion:
The program with macros is supplied to the macro processor. Each macro
invocation statement will be 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. During the expansion, the macro definition statements are
deleted since they are no longer needed.
The arguments and the parameters are associated with one another according to
their positions. The first argument in the macro matches with the first parameter in the
macro prototype and so on.
After macro processing the expanded file can become the input for the
Assembler. The Macro Invocation statement is considered as comments and the
statement generated from expansion is treated exactly as though they had been written
directly by the programmer.
The following program shows the concept of Macro Invocation and Macro
Expansion.
Fig 4.3(a)
Fig 4.3(b)
SYSTEM PROGRAMMING, CHAPTER - 4, MACROPROCESSORS Page 6
A program that is to be run on SIC system could invoke MACROS whereas a
program to be run on SIC/XE can invoke MACROX.
However, defining MACROS or MACROX does not define RDBUFF and
WRBUFF.
These definitions are processed only when an invocation of MACROS or
MACROX is expanded.
A one-pass macro processor that alternate between macro definition and macro
expansion in a recursive way is able to handle recursive macro definition.
Restriction
o The definition of a macro must appear in the source program before any
statements that invoke that macro.
o This restriction does not create any real inconvenience.
The design considered is for one-pass assembler. The data structures required are:
The above figure shows the portion of the contents of the table during the processing
of the program in page no. 3. In fig 4.4(a) definition of RDBUFF is stored in
DEFTAB, with an entry in NAMTAB having the pointers to the beginning and the end
of the definition. The arguments referred by the instructions are denoted by their
positional notations. For example,
TD =X’?1’
The above instruction is to test the availability of the device whose number is given by
the parameter &INDEV. In the instruction this is replaced by its positional value? 1.
Figure 4.4(b) shows the ARTAB as it would appear during expansion of the RDBUFF
statement as given below:
For the invocation of the macro RDBUFF, the first parameter is F1 (input device
code), second is BUFFER (indicating the address where the characters read are stored),
and the third is LENGTH (which indicates total length of the record to be read). When
the ?n notation is encountered in a line fro DEFTAB, a simple indexing operation
supplies the proper argument from ARGTAB.
The algorithm of the Macro processor is given below. This has the procedure DEFINE
to make the entry of macro name in the NAMTAB, Macro Prototype in DEFTAB.
EXPAND is called to set up the argument values in ARGTAB and expand a Macro
Invocation statement. Procedure GETLINE is called to get the next line to be
processed either from the DEFTAB or from the file itself.
Most macro processors allow thr definitions of the commonly used instructions
to appear in a standard system library, rather than in the source program. This makes
the use of macros convenient; definitions are retrieved from the library as they are
needed during macro processing.
One-pass algorithm
o Every macro must be defined before it is called
o One-pass processor can alternate between macro definition and macro
expansion
o Nested macro definitions are allowed but nested calls are not allowed.
Two-pass algorithm
o Pass1: Recognize macro definitions
o Pass2: Recognize macro calls
o Nested macro definitions are not allowed
Suppose that the parameter to such a macro instruction is named &ID. The body
of the macro definition might contain a statement like
LDA X&ID1
Fig 4.7
& is the starting character of the macro instruction; but the end of the parameter is not
marked. So in the case of &ID1, the macro processor could deduce the meaning that
Most of the macro processors deal with this problem by providing a special
concatenation operator. In the SIC macro language, this operator is the character .
Thus the statement LDA X&ID1 can be written as
LDA X&ID1
Fig 4.8
The above figure shows a macro definition that uses the concatenation operator
as previously described. The statement SUM A and SUM BETA shows the
invocation statements and the corresponding macro expansion.
As discussed it is not possible to use labels for the instructions in the macro
definition, since every expansion of macro would include the label repeatedly which is
not allowed by the assembler. This in turn forces us to use relative addressing in the
jump instructions. Instead we can use the technique of generating unique labels for
every macro invocation and expansion. During macro expansion each $ will be
replaced with $XX, where xx is a two-character alphanumeric counter of the number
of macro instructions expansion.
For example,
The following program shows the macro definition with labels to the instruction.
The following figure shows the macro invocation and expansion first time.
There are applications of macro processors that are not related to assemblers or
assembler programming.
MACRO &COND
……..
IF (&COND NE ‘’)
part I
ELSE
part II
ENDIF
………
ENDM
Macro-Time Variables:
Fig 4.9(a)
Figure 4.5(a) gives the definition of the macro RDBUFF with the parameters
&INDEV, &BUFADR, &RECLTH, &EOR, &MAXLTH. According to the above
program if &EOR has any value, then &EORCK is set to 1 by using the directive SET,
otherwise it retains its default value 0.
The macro processor continues to process lines from the DEFTAB until it
encounters the ELSE or ENDIF statement.
If an ELSE is found, macro processor skips lines in DEFTAB until the next
ENDIF.
Once it reaches ENDIF, it resumes expanding the macro in the usual way.
If the value of the expression is FALSE,
The macro processor skips ahead in DEFTAB until it encounters next ELSE or
ENDIF statement.
The macro processor then resumes normal macro expansion.
WHILE-ENDW structure
All the macro instruction definitions used positional parameters. Parameters and
arguments are matched according to their positions in the macro prototype and the
macro invocation statement.
Positional parameters are suitable for the macro invocation. But if the macro
invocation has large number of parameters, and if only few of the values need to be
used in a typical invocation, a different type of parameter specification is required (for
example, in many cases most of the parameters may have default values, and the
invocation may mention only the changes from the default values).
Ex:
If positional parameters were used, the macro invocation statement might look
like
GENER , , DIRECT, , , , , 3 ,
If the third parameter is named as &TYPE and the ninth parameter is named
&CHANNEL, then the macro invocation statement would be
This statement is much easier to read and less error prone than the positional
version.
Arguments any appear in any order.