Module 5 Remaining Notes
Module 5 Remaining Notes
The design of macro processor doesn’t depend on the architecture of the machine. We will be
studying some extended feature for this macro processor. These features are:
● Most macro processor allows parameters to be concatenated with other character strings.
Suppose that a program contains a series of variables named by the symbols XA1, XA2,
XA3,…, another series of variables named XB1, XB2, XB3,…, etc. If similar processing
is to be performed on each series of labels, the programmer might put this as a macro
instruction.
● The parameter to such a macro instruction could specify the series of variables to be
operated on (A, B, etc.). The macro processor would use this parameter to construct the
symbols required in the macro expansion (XA1, XB1, etc.
● 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
● & 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 was
intended.
● If the macro definition contains &ID and &ID1 as parameters, the situation would be
unavoidably ambiguous.
● 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&ID→1
● 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.
● 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.
● 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.
● If the macro is invoked second time the labels may be expanded as $ABLOOP $ABEXIT.
● We can modify the sequence of statements generated for a macro expansion depending on
conditions.
Looping-WHILE
● In the macro invocation statement there is a list(00,03,04) corresponding to the parameter &EOR.
Any one of these characters is to be considered as end of record.
● The WHILE statement specifies that the following lines until the next ENDW are to be generated
repeatedly as long as the condition is true.
● The testing of these condition and the looping are done while the macro is being expanded.
The conditions do not contain any runtime values.
● %NITEMS is a macroprocessor function that returns as its value the number of members in an
argument list. Here it has the value 3. The value of &CTR is used as a subscript to select the proper
member of the list for each iteration of the loop. &EOR[&CTR] takes the values 00,03,04 .
● Implementation- When a WHILE statement is encountered during a macro expansion the specified
Boolean expression is evaluated , if the value is false the macroprocessor skips ahead in DEFTAB
until it finds the ENDW and then resumes normal macro expansion(not at run time).
● 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.
● Eg: Consider the macro GENER which has 10 parameters, but in a particular
invocation of a macro only the third and nineth parameters are to be specified. If
positional parameters are used the macro invocation will look like
GENER , , DIRECT, , , , , , 3,
● But using keyword parameters this problem can be solved. We can write
GENER TYPE=DIRECT, CHANNEL=3
Keyword parameters
● Each argument value is written with a keyword that names the corresponding
parameter.
● Arguments may appear in any order.
● Null arguments no longer need to be used.
● It is easier to read and much less error-prone than the positional method.
● We have seen an example of the definition of one macro instruction by another. But we have
not dealt with the invocation of one macro by another. The following example shows the
invocation of one macro by another macro.
Problem of Recursive Expansion
● Previous macro processor design cannot handle such kind of recursive macro
invocation and expansion
o The procedure EXPAND would be called recursively, thus the invocation
arguments in the ARGTAB will be overwritten.
o The Boolean variable EXPANDING would be set to FALSE when the “inner”
macro expansion is finished, i.e., the macro process would forget that it had
been in the middle of expanding an “outer” macro.
The procedure EXPAND would be called when the macro was recognized. The arguments
from the macro invocation would be entered into ARGTAB as follows:
Parame Value
ter
1 BUFFE
R
2 LENG
TH
3 F1
4 (unused
)
- -
The Boolean variable EXPANDING would be set to TRUE, and expansion of the macro
invocation statement would begin. The processing would proceed normally until statement
invoking RDCHAR is processed.
This time, ARGTAB would look like
Value
Paramet
er
1 F1
2 (Unuse
d)
-- --
● Solutions
o Write the macro processor in a programming language that allows recursive
calls, thus local variables will be retained.
o If you are writing in a language without recursion support, use a stack to take
care of pushing and popping local variables and return addresses.
● Disadvantages- They must be specially designed and written to work with a particular
implementation of an assembler or compiler.. Cost of development is high.