0% found this document useful (0 votes)
13 views25 pages

Macro Module 3

Uploaded by

aj950015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

Macro Module 3

Uploaded by

aj950015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter

5 Macros and Macro


Processors
This chapter discusses macros and macro processors. The chapter focuses on the
design aspects of a macro processor in detail. The features of the macros are also
addressed in this chapter.

5.1 DEFINITION OF A MACRO


Macro is a name or abbreviation for a sequence of instructions required to perform
an operation. A macro represents a commonly used group of statements in the
source program.
Often an assembly language programmer requires repeating some blocks of
code many times in the same program. Instead of repeatedly writing the same set of
instructions, the programmer can use the facility of a macro. While using a macro,

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

Name of the macro

Body of the 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’
———

be used in place of a mnemonic. When the assembler processes such a statement,


it replaces the statement with the text lines associated with that macro and then

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.

5.3 FEATURES OF A MACRO FACILITY


The following is a list of the basic features provided by a macro:
1. Macro instructions with parameters
2. Conditional macro expansion
5.4 An Introduction to System Software
3. Macro calls within macros

5.3.1 Macro Instructions with Parameters


Generally, wherever a macro is called, it is replaced with identical blocks of

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

actual parameters. When a macro is expanded, each formal parameter appearing


in the macro body is replaced by the corresponding actual parameter. The actual

For example, consider the following program segment:


———
———
ADD REG1, MEM1 Add contents of memory word MEM1 to Register1
ADD REG2, MEM1 Add contents of MEM1 to Register 2
ADD REG3, MEM1 Add contents of MEM1 to Register3
———
———
ADD REG1, MEM2 Add contents of MEM2 to Register1
ADD REG2, MEM2 Add contents of MEM2 to Register2
ADD REG3, MEM2 Add contents of MEM2 to Register3
———
———
MEM1 DC ‘5’
MEM2 DC ‘10’
———
———

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’

Here the macro call INCR MEM1 will be expanded as:


ADD REG1, MEM1
ADD REG2, MEM1
ADD REG3, MEM1
While INCR MEM2 will be expanded as:
ADD REG1, MEM2
ADD REG2, MEM2
ADD REG3, MEM2
It is also possible to pass more than one argument in a macro call, where each
argument must correspond to a “dummy argument” on the macro name line. The
arguments supplied at the time of a macro call, are substituted for the respective

Consider the following segment in which the number of arguments is same


in the instruction sequence, but they differ in the order of their use:
L1 ADD REG1, MEM1
ADD REG2, MEM2
ADD REG 3, MEM3
———
———
5.6 An Introduction to System Software
L2 ADD REG1, MEM3
ADD REG2, MEM2
ADD REG3, MEM1
———
———
MEM1 DC ‘5’
MEM2 DC ‘10’
MEM3 DC ‘15’
This program could be written as follows:
———
———
MACRO
&LAB INCR &X, &Y, &Z
ADD REG1, &X
ADD REG2, &Y
ADD REG3, &Z
MEND
———
———
L1 INCR MEM1, MEM2, MEM3
———
———
L2 INCR MEM3, MEM2, MEM1
———
———
MEM1 DC ‘5’
MEM2 DC ‘10’
MEM3 DC ‘15’
The macro call L1 INCR MEM1, MEM2, MEM3 will be expanded as:
L1 ADD REG1, MEM1
ADD REG 2, MEM2
ADD REG3, MEM3
And the second macro call L2 INCR MEM3, MEM2, MEM1 will be
expanded as:
Macros and Macro Processors 5.7
L2 ADD REG1, MEM3
ADD REG2, MEM2
ADD REG3, MEM1
There are generally two methods for specifying arguments to a macro call:
1. Positional arguments
2. Keyword arguments
Macro Parameters:

In case of positional arguments, the arguments replace the


dummy arguments according to the order of their appearance. E.g. INCR A, B, C,

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

might look like:


VARY, D1, , , , , , N
keyword parameters

&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.

5.3.2 Conditional Macro Expansion


In all of our previous examples of macro instructions, each invocation of a particular
macro was expanded into the same sequence of statements. These statements could
5.8 An Introduction to System Software
be varied by the substitution of parameters, but the form of statements and the
order in which they appeared were unchanged. There are many occasions when
it is desirable to be able to vary the output depending on some internal conditions
at expansion time. Thus, we may wish to omit certain statements or to choose one
of the several alternative statements or even to iterate a collection of statements
until some condition e.g., completion of scanning of the parameter list is complete.
The ability to alter the expansion of a macro by using control statements
makes macro instructions a much more powerful and useful tool for the
programmer. The types of conditions used are listed below:
EQ Equal
NE Not Equal
LT Less Than
GT Greater Than
LE Less than or Equal
GE Greater than or Equal

macro is written. Conditional macro expansion helps in generating assembly code


suited to the arguments in a macro call.
To meet the requirement of conditional macro expansion, we introduce an
internal language which is independent of the language being generated which

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

5.3.3 Macro Calls Within Macros

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

resulting expanded code will be as follows:


5.12 An Introduction to System Software

Macro Call Expanded Code

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

In order to implement macro calls within macros:


A stack is used to keep the order of the macro calls
A parameter table is created at each macro call

the parameter table of the calling macro is searched and it is replaced with
the appropriate real parameter.

useful in assembly language programs. The general structure of such as macro


instruction is as follows:
MACRO X Start of macro X
———
———
MACRO Y Start of macro Y
———
———
MEND End of macro Y
———
———
MACRO Z Start of macro Z
Macros and Macro Processors 5.13
———
———
MEND End of macro Z
———
———
MEND End of macro X
When a call is made to MACRO X, then two macros embedded within it i.e.,

5.4 MACRO PROCESSORS


The macros used in a program are handled by a macro processor. A macro processor
can be considered as an extension of the basic assembler algorithm. Assemblers
must pass the name of the macros to the loader for loading them into the main
memory. Macro processors can also be used with high level programming languages,
operating system command languages. There are general purpose macro processors,
which are independent of the programming languages.
Design of a Macro Processor: In this section, the design concepts of macro
processors are discussed. The source program is read and is then transformed into

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.

Fig. 5.1 Schematic of a Macro Preprocessor

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.

1. Statement of the Problem: A macro processor is supposed to perform four


basic functions:
5.14 An Introduction to System Software
A macro processor must be able to

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

the macro calls.


(c) Recognize the Macro Calls: The macro processor must identify the
macro calls appearing as mnemonic opcodes in the program.
(d) Expansion of the Macro Calls: The macro processor must expand the
macro wherever a call has been made to it. While expanding a macro,
the macro processor must substitute the arguments used while calling 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

of a macro processor to substitute index markers for the dummy arguments


Macros and Macro Processors 5.15
Pass-II Databases:
, generated by pass-I of the macro
processor.
2. Macro Name Table (MNT),
3. , used to indicate the next line
of text to be used for the macro expansion.
4. Argument List Array (ALA), which is used during the expansion of
macro invocations. When a macro invocation statement is recognized, the
arguments are stored in ALA according to their positions in the argument
list. As the macro is expanded, arguments from ALA are substituted for
the corresponding parameters in the macro body.
3. Format of the Databases:
:

the MDT, except the MACRO line. The MEND is kept to mark the end of

in the previous example, can be stored in the MDT as follows:

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

Index Name MDT Index


1 20

index no. 20 in the MDT.


(c) Argument List Array (ALA): The Argument List Array is used by both
of the passes of the macro processor but for reverse functions. While
5.16 An Introduction to System Software

ith dummy argument on the macro name line. During


pass-II, again ALA is created so as to replace macro call arguments for
For example, during a
macro call –
M1 INCREMENT D1, D2, D3
The macro processor would prepare the following argument list array:

Table 5.3

Index Argument
0 M1
1 D1
2 D2
3 D3

D1, D2 and D3 respectively.


, MDTC is a variable which points

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

Fig. 5.2 Flowchart for Pass-I of a Macro Processor


5.18 An Introduction to System Software
Pass-I: The pass-I of the macro processor checks each input line, if it is a

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.

Fig. 5.3 Flowchart for Pass-II of a Macro Processor


Macros and Macro Processors 5.19
Pass-II:
checking whether the mnemonic operation used in an input line is the name of a

List Array is prepared (ALA), which is a table consisting of dummy arguments


indices and the corresponding actual arguments to the call. While expansion of the
macro, each successive line from MDT is read, the values from ALA are substituted

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.

expansions in a single pass. A single pass macro processor design imposes a

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

MACRO and MEND directives gets stored in MDT. MDLC is incremented by


one when a MACRO pseudo-op is encountered and decremented by one when
a MEND pseudo-op occurs. When the value of MDLC reaches zero, it indicates

calculates the difference between the number of MACRO and number of MEND

processor.

Macro Calls

nested macro call


5.20 An Introduction to System Software
Macros and Macro Processors 5.21
5.22 An Introduction to System Software

Fig. 5.4 Flow Chart for Single-pass Macro Processor

SP S (SP) Old Value of SP


SP1 S (SP +1) MDTP

}
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

Fig. 5.5 Stack Frame Organization

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

Fig. 5.4. The detail of *Read function is given in Fig. 5.7.

5.5 MACRO ASSEMBLER


The macro processors discussed so far might be called preprocessors i.e., they

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

input to the assembler.

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.

You might also like