Macro Module 3
Macro Module 3
this one-line macro instruction appears in the program, the macro processor will
substitute the entire block. This is called macro expansion. Thus it is actually a text
replacing capability. A macro is just a notational convenience for the programmer.
Thus the macro instruction allows the programmer to write shorthand version of
a program and leave the mechanical details to be handled by the macro processor.
For example, suppose that it is necessary to save the contents of all registers before
calling a subprogram. This operation would require a sequence of instructions.
Using a macro instruction, the programmer could simply write one statement like
SAVEREGS. This macro instruction would be expanded into several assembly
language instructions needed to save the register contents. A similar macro instruction
could be used to reload the register contents after returning from the subprogram.
Advantages of Macros:
Simplify and reduces the amount of repetitive code.
Reduces errors caused by repetitive coding.
Make an assembly language program more readable.
Since macros can have ‘short’ names but expand to several or indeed many
lines of code, they can be used to make assembly language programs appear
to be much shorter.
5.2 An Introduction to System Software
5.2 MACRO INSTRUCTIONS
Macro instructions are usually considered as an extension of the basic assembly
language. The macro can be used anywhere in the program. Its occurrence is called
a macro call. Macro call is also called macro invocation. But there are some
Macro calls are made during the assembly process. The procedure calls are
made during program execution. A macro call leads to its expansion whereas the use
of a procedure call leads to its execution. Whenever there is a macro call, the body
of the macro is inserted into the object program. But for the procedure call only
control is transferred. There is no need of any return instruction in case of macros.
But there must be a return statement at the end of the execution of a procedure.
Consider the following program segment:
____
ADD REG1, MEM Add contents of memory word MEM to Register1
ADD REG2, MEM Add contents of MEM to Register2
ADD REG3, MEM Add contents of MEM to Register3
____
____
____
ADD REG1, MEM
ADD REG2, MEM
ADD REG3, MEM
____
MEM DC ‘5’
In this program, the above set of instructions is repeated twice. Using a macro
facility, we can give a name to this sequence of instructions by means of a macro
MACRO
MEND
MACRO
MEND
Macros and Macro Processors 5.3
an assembly program. The above example program can now be rewritten as follows:
MACRO
INCREMENT
ADD REG1, MEM
ADD REG2, MEM
ADD REG3, MEM
MEND
———
———
INCREMENT Macro Call
———
———
INCREMENT Macro Call
———
———
MEM DC ‘5’
———
In the above program, the instruction sequence has been given a name
‘INCREMENT’. So, wherever the word ‘INCREMENT’ appears in the program,
the macro processor replaces it with the following lines:
ADD REG1, MEM
ADD REG2, MEM
ADD REG3, MEM
This process is called macro expansion. Wherever, the name of the macro is
used as an operation mnemonic to be expanded, it is called a macro call.
the provision for modifying the coding that replaces a macro name. Macros come
with a facility to provide for parameters in the macro calls. Frequently a program
contains several sequences of instructions that are almost but not quite identical.
Macro processors handle the case of nearly identical sequences by allowing macro
instructions uses MEM1 as operand while the second uses MEM2.They can be
used to perform the same operation with a different argument which is called a
“macro instruction argument” or a “dummy argument”. So, the above example
code can be written as:
Macros and Macro Processors 5.5
}
MACRO
INCR &Arg
ADD REG1, &Arg
ADD REG2, &Arg
ADD REG3, &Arg
MEND
———
———
INCR MEM1 } MEM1 Macro Call
———
———
INCR MEM2 } MEM2 Macro Call
———
———
MEM1 DC ‘5’
MEM2 DC ‘10’
and C respectively.
Positional parameters are quite suitable for most macro instructions. However,
if a macro has a large number of parameters and only a few of them are given values,
Keyword argument
allows reference to dummy arguments by name as well as by position. For instance,
suppose that a certain macro instruction VARY has ten possible parameters but
in a particular invocation of the macro, only the third and ninth parameters are to
&T &N
VARY T = D1, N = 3
This statement is obviously much easier to read and much less error prone
than the positional version. If arguments are not supplied in a macro call, they are
presumed to be blank by the macro processor.
internally, lines of the macro body including the conditional statements themselves.
When these labels are attached to lines of code which are not themselves conditional
not passed to the output stream. Such labels are distinguished by a starting period
called sequencing symbols. It should be emphasized that these labels are purely
local and never appear in the code generated by the macro call.
There are two very simple branching instructions with operation mnemonics
AGO and AIF.
AGO is an unconditional branch and has format:
Optional sequence symbol AGO sequence symbol
For example, L1 AGO .L2
or
AGO .NEXT
Both with obvious meanings.
The operation AIF has the format:
Optional sequence symbol AIF (logical expression) sequence symbol
Macros and Macro Processors 5.9
For example, AIF (‘&ANS’ EQ “ “) .DONE
This statement tests for a null parameter. The quotation marks imply that the
actual parameter corresponding to &ANS is to be compared literally with the null
string and if they are identical then a branch is to be made to the statement with
the sequence symbol .DONE. Otherwise macro expansion continues sequentially.
The logical expression may be quite complicated during logical connectives AND,
OR and NOT and the relational operators EQ, NE, LT, LE, GT, GE. The logical
expression must evaluate to either true/false at macro expansion time.
A simple example is as follows:
LOOP1 ADD REG1, DATA1
ADD REG2, DATA2
ADD REG3, DATA3
———
———
LOOP2 ADD REG1, DATA3
ADD REG2, DATA2
———
———
LOOP3 ADD REG1, DATA1
———
DATA1 DC ‘5’
DATA2 DC ‘10’
DATA3 DC ‘15’
In this program, the instruction sequences differ in operands and number
of instructions generated. Using conditional macro expansion facility, the above
program can be written as follows:
MACRO
& ARG0 VARY &COUNT, &ARG1, &ARG2, &ARG3
ADD REG1, & ARG1
AIF (& COUNT EQ 1) .FINISH
ADD REG2, & ARG2
AIF (& COUNT EQ 2) .FINISH
ADD REG3, & ARG3
FINISH MEND
5.10 An Introduction to System Software
LOOP1 VARY 3, DATA1, DATA2, DATA3
———
———
LOOP2 VARY 2, DATA3, DATA2
———
———
LOOP3 VARY 1, DATA1
———
———
DATA1 DC ‘5’
DATA2 DC ‘10’
DATA3 DC ‘15’
LOOP1 VARY 3, DATA1, DATA2,
DATA3 will be:
LOOP1 VARY
ADD REG1, DATA1
ADD REG2, DATA2
ADD REG3, DATA3
The macro call LOOP2 VARY 2, DATA3, DATA2 will be expanded as:
LOOP2 VARY
ADD REG1, DATA3
ADD REG2, DATA2
Likewise, the expanded code for third macro call will be as follows:
LOOP3 VARY ADD REG1, DATA1
In this example, the number of arguments supplied to the macro; is passed
AIF pseudo-op is used to check the number of arguments supplied in the macro-
call. The macro is expanded accordingly. If the number of argument supplied to the
to the statement labeled. FINISH otherwise, the second line of the macro is also
expanded. The count of arguments is checked to decide whether the macro should
be expanded further or not, thus allowing the degree of optimization. Arguments
are separated by commas. The macro processor substitutes parameters of the macro
as nested macro calls. The macro containing the nested call is called the outer
Macros and Macro Processors 5.11
macro and the called macro is referred as inner macro. In such a macro structure,
For example,
MACRO
ADDS &ARG
LOAD REG1, &ARG
ADD REG1, = ‘5’
STORE REG1, &ARG
MEND
MACRO
ADDITION & ARG1, & ARG2, & ARG3
ADDS & ARG1
ADDS & ARG2
ADDS & ARG3
MEND
the macro “ADDS”. Using the facility of nested macro calls, the length of the outer
macro can be reduced.
Macro calls within macros may have multiple levels. Like in the above
macro. As in high level languages, the macros can be recursive. If any macro calls
itself then such macros are called recursive macros. As in recursive procedures
there must be some terminating conditions in the recursive macros so as to stop
In the above example, a call to the outer macro “ADDITION” will result in
the expansion of the inner macro “ADDS” thrice. E.g., a call ADDITION D1, D2,
D3 will be expanded as
ADDS D1
ADDS D2
ADDS D3
ADDS D1
[ LOAD REG1, D1
ADD REG1, = ‘5’
STORE REG1, D1
ADDS D2
[ LOAD REG1, D2
ADD REG1, = ‘5’
STORE REG1, D2
ADDS D3
[ LOAD REG1, D3
ADD REG1, = ‘5’
STORE REG1, D3
the parameter table of the calling macro is searched and it is replaced with
the appropriate real parameter.
all the macro calls have been replaced with the corresponding macro bodies. The
output of the macro processor will be an assembly language program containing no
macros. The program form generated by the macro processor can now be handed
over to the assembler to get the target form of the program.
Just like assemblers, there are two ways to design a macro processor:
Two-pass macro processor
Single-pass macro processor
In the next sections, we will outline a method for implementing a macro
processor using these two design options.
MACRO and MEND. This task can be some what complicated in case of
must recognize the nesting and correctly match the last or outer MEND
(b) Save the Macro Definitions: The macro processor must save the
A two-pass macro processor will make two systematic scans or passes over
accordingly.
The following are the databases used by the
two passes of a macro processor:
Pass-I Databases:
DT)
in a , which contains the macro prototype
and the statements that makeup the body of the macro.
2. Macro Name Table (MNT): The macro names are entered into MNT, which
: The
Counter is used to indicate the next available entry in MDT i.e., where the
4. Macro Name Table Counter (MNTC): The Macro Name Table Counter
(MNTC) is used to indicate where the next entry can be made in MNT.
5. Argument List Array (ALA)
references to the macro instruction parameters are converted to a positional
the MDT, except the MACRO line. The MEND is kept to mark the end of
Table 5.1
Index Contents
20 INCREMENT &ARG1, &ARG2, &ARG3
21 ADD REG1, #1
22 ADD REG2, #2
23 ADD REG3, #3
24 MEND
(b) Macro Name Table: The Macro Name Table consists of the names of
the macros along with a pointer or index to the entry in the MDT which
Table 5.2
Table 5.3
Index Argument
0 M1
1 D1
2 D2
3 D3
MDT, it is incremented by one. This will continue until the end of the
macro is encountered.
(e) Macro Name Table Counter, MNTC is used to specify where in the
MNT, a new macro name line can be added. Every time an entry is made
in MNT, this counter is incremented by one, so that we can know where
a new entry can be made.
(f) is used during expansion of
the macro by pass-II. It is initialized to MDT index from MNT entry. As
each line of the macro is expanded, this pointer is incremented to indicate
which line of MDT is to be expanded for the macro.
Processor
and saves them. The second pass recognizes the macro calls and expands them.
Following is a brief description of how these functions are implemented by pass-I
and pass-II of a two-pass macro processor.
Macros and Macro Processors 5.17
MDTC. The name of the macro is entered in the Macro Name Table (MNT), along
An ALA is prepared to substitute index markers for the dummy arguments while
in the MDT one by one until the MEND statement is encountered. When END
pseudo-op is found, the control is transferred to pass-II of the macro processor to
process the macro calls.
and scanning continues from the input program. When the END of the program
is encountered, the expanded source program is transferred to the assembler for
further processing.
The single pass macro processor discussed in this section provides the additional
The data structures used by a single pass macro processor are same as those
used by a two pass macro processor. But there are two additional variables used in
Level Counter (MDLC) acts as a counter to keep track of the number of MACRO
calculates the difference between the number of MACRO and number of MEND
processor.
Macro Calls
}
SP2 S(SP+2) 0th argument
Argument
SP3 S (SP +3) Ist argument List Array
for Macro
Call
SP+N-1 S(SP +N+1) (N-1)th argument
processor which is capable of handling macro calls within macros as well as macro
Macros and Macro Processors 5.23
Fig. 5.6 Single-pass Macro Processor Capable of Handling Nested Macro Calls
5.24 An Introduction to System Software
Fig. 5.7 Detail of Read Function for Nested Macro Call Expansion
version of the source program. This expanded program is then used as input to the
assembler.
The use of a macro preprocessor followed by a conventional assembler is
an expensive way of handling macros since the number of passes over the source
program is large and many functions get duplicated. Analysis of a source statement
Macros and Macro Processors 5.25
and assembler can be merged if macros are handled by a macro assembler which
performs macro expansion and program assembly simultaneously. This may reduce
the number of passes.
Pass Structure: To design pass structure of a macro assembler we identify
the following functions of a macro preprocessor and the conventional assembler
which can be merged to advantage. After merging, the functions can be structured
into passes of the macro assembler.
Pass-I
Macro Expansion
Memory Allocation, LC processing and SYMTAB construction.
IC generation.
Pass-II
Target Code Generation
Advantages:
1. It avoids making an extra pass over the source program.
2. Some of the functions do not have to be implemented twice.
3. Since the functions of a macro processor and assembler are combined, so there
Disadvantages:
implementation of an assembler.
2. It results in a more expensive piece of software as the cost of macro processor
development must be added to the cost of assembler.
3. In addition, the assembler will be considerably larger and more complex than
it would be if a macro processor were used.
4. The size may be a problem if assembler is run on a computer with limited
memory. Additional complexity will add to the overhead of language
translation.