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

Macroprocessors Donovan

The document discusses the concept of macro instructions in assembly language programming, which allow programmers to define single-line abbreviations for blocks of code to enhance coding efficiency. It explains features of macro facilities, such as the use of arguments for flexibility, conditional macro expansion, and the ability to call macros within other macros. Overall, macros provide a higher-level programming capability while retaining the advantages of assembly language.

Uploaded by

archanavce121
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)
9 views25 pages

Macroprocessors Donovan

The document discusses the concept of macro instructions in assembly language programming, which allow programmers to define single-line abbreviations for blocks of code to enhance coding efficiency. It explains features of macro facilities, such as the use of arguments for flexibility, conditional macro expansion, and the ability to call macros within other macros. Overall, macros provide a higher-level programming capability while retaining the advantages of assembly language.

Uploaded by

archanavce121
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

4

macro language
and the macro processor

The assembly language programmer often finds it necessary to repeat some


blocks of coáe many times in the course of a program. The block may consist of
code to save or exchange sets of registers, for example, or code to set up linkages
or perform a series of arithmetic operations. In this situation the programmer
will find a macro instruction facility useful. Macro instructions (often called

macros) are
for groups of instructions. In employing a
single-line abbreviations
macro, the programmer essentially defines a single "instruction" to representa
block of code. For every occurrence of this one-line macro instruction in his pro-
gram, the macro processing assembler will substitute the entire block.
By defining the appropriate macro instructions, an assembly language pro
at no
grammer can tailor his own higher level facility in a convenient manner,
cOst in control over the structure of his program. He can achieve the conciseness
and ease in coding of high level languages without losing the basic advantage of
assembly language programming. Integral macro-operations simplity debugging
and program modification and they facilitate standardization. Many computer
the
to automate of ""tailored"
writing
anulacturers use macro instructions
operating systems in a process called systems generation.
of the basic assembler
Macro instructions are usually considered an extension
extension of the basic assem-
arguage, and the macro processor is viewed as an

however, macro instruction


r algorithm. As a form of programming language,
differ significantly from assembly languages and compiled algebraie
ages be found in some high level languages and
are to
guages. Important analogs
txt editing systems.
We will treat the use
will discuss an assembler macro facility.
"his section we the implementation of macros within
acro instructions in programming andcharacteristic of most macro processors
Sembler. Certain features that are
will be
introduced and developed. 111
114 FEATURES OF A MACRO
FACILIt
4.2 FEATURES OF A MACRO FACILITY

4.2.1 Macro Instruction Arguments


The macro facility presented thus far is capable of inserting blocks of instruo
tions in place of macro calls. All of the calls to any given macro will be replaced
by identical blocks. This macro facility lacks flexibility: there is no way fora
specific macro call to modify the coding that replaces it. An important extension
of this facility consists of providing for arguments, or parameters, in macro calt.
Corresponding macro dummy arguments will appear in macro definitions. Con
sider the following program.

Example 2:

1, DATA 1
A 2, DATA 1
3,DATA1

1, DATA 2
2, DATA 2
3, DATA 2

DATA 1 DC F'5
DATA 2 DC F'10'

In this case the instruction sequences are very Sumlar. but not identiçal. The
Tirst sequence performs an operation using DATAI as operand; the second, usig
DATA2. They can be considered to perform the saune operation with a vurjable
parameter, or argument. Such a parameter is called a macro instruction argt
ment, or "dummy argument" it is specificd on the macro name line and dive
tinguished (as a macro language symbol rather than an assembly language svm-

bol) by the ampersand (&),


which is always its first character. The pte odi
could be wrilten as:
program
MACIHO LANGUAGR AND THE MACRO PROCESBOR 116

Souree xpandod source


MACRO Macro INCH has
0ne argurment
INCR &ARG
A 1,8ARO
2,ARG
3,8ARG
MEND

INCR DATA Use DATA1 ns 1,0ATA1


operand 2,DATAI
3,DATA1

INCR DATA2 Use DATA2 as 1,0ATA2


0purand 2,DATA2
3,DATA2

DATA1 DC F'' DATA1


DATA2D F'S'
DATA2 DC F'10 F10

It is possible to supply more than one argument in a macro call. Each argu-
ment nust correspond to a delinition ("dumny") argument on the macro name
Iare of the macro delinition. When a macro eall is processed, the arguments sup-
plied are substituted for the respective dumny arguments m the macro
definition.
Consider the followlng program:
Example 3:

LOOP1 1, DATA1
2, DATA2
A 3, DATAJ

LOOP2 1,DATAS
2, DATA2
3, DATA1

DATAI DC
DATA2 DC F'10'
DATAJ DC P16
116 FEATURESs OF A MACRO

In this case the operands in the common sequence are different, as are
on the first cards of the sequences. This program could be written as:

sOURCE EXPANDED SOUP

MACRO
&LAB INCR &ARG1,&ARG2,&ARG3
&LAB A 1,&ARG1
A 2,&ARG2
A 3,&ARG3
MEND

LOOP1 INCR DATA1,DATA2,DATA3 LOOP1 A 1,DA


A 2,DA
A 3,DAT

LOOP2 INCR DATA3,DATA2,DATA1 | LOOP2 A 1,DATA


A 2,DATA
A 3,DATA

DATA1 DC F'5 DATA1 DC F'5


DATA2 DC F'10 DATA2 DC F'10
DATA3 DC F'15' DATA3 DC F'15

Here we have specified four arguments, including a lubel argument. Label


arguments are treated just like those written as "operands" of a macro instruc
tion; the argument appearing in the label Iieid on the macro name line and in

calls to the macro differs from other arguments only in its location. Arguments
used as labels within the macro need not appear in the label field of a call, and
in the label field of a call may be used as operands within
arguments appearing
Label arguments are convenient but nonessential
the macro instruction code.
similarity between macro instructions and ordinary
features that enhance the
&ssembly language
instructions. Following is
another encoding of
amplo.3
Ma HOLANGUAGE AND THE MAGRO PROCRBon

a n d d seuroe

MACRO
ING NARGTAARG2NARaJ,NLAD|
&LAB A ARGT
,NARG?
3,ARG
MEND

1,DATAT1
INCR DATAI,DATAP,DATAB,LOOP1LOOPT ,DATAS
3,DATA

INCH DATAJ,DATA2,DATA1,L.0OP,LOoP?

: .DARA3
DATA
3,DATA

to a naerO call. The


There are generally two ways of spevilying arguments
is dennonstrated in the precoding exaples. Argu
fiut, oaitionalarxunent,
order in whih they
mcnts are imatched with
dumy aguments according to tlie

in maero eall UNCR A,B.C" A, D,


and C roplave ttho list, second,
appear. Thus, a
tacility, Kerwnd argt
and thiud dumny arguments. A nuore general argument by posltion.
ments, allows relerence to dumny
arguments by name as well as
arguments appearlng in the dotinition of INCR, we
Relening to the dumumy
might waite

&ARG1-A&AR03-C.&ARO2-0' or INCR &ARG1-A,&ARG2-8ARGJ-C


INCH

exanple-that are
This can be useful when a macro hus argunents-labols, for
not suppliod are presumod blank by the
Hot always needed. Any argnents
macto processor

4.2.2 Conditional Macro Expanslon


and AGo, permit conditional
Important macro processor pseudo-ops, All
w Thls allows conditional seloetion
TCordering of the sequence of macro expansion. a macro call.
N the maclhine instructions that appear in expansions of

onsider tlio following program:


118 FEATURES OF AMACRO FAC
ILIT
Example 4: ACRO LANGUAGE AND THE MACRO PRO
Labels starting with a period (), such
LOOP1 A 1, DATA1
in the output of the macro
appear
2, DATA2 proces
3, DATA3 DEINI directs the macro processor to
the parameter corresponding to &COUN
is to continue with the statement follow n
LOOP2 1, DATA3 AIF is a conditional branch pseudo-q
A 2, DATA2 branches only if the tested condition
branch pseudo-op or 'go to' statement.
LOOP3 A 1, DATA1 other statement in the macro instructi
tinues sequential processing of instructi
DATA1 DC statements are directives to the macro
F'5'
DATA2 DC F'10' expansions. Just as conditional and
DATA3 DC F'15 machine language program direct the or
trol the sequence in which the macro pr
In thisexample, the operands, labels, and the number of instructions instructions. This makes it possible to
change in each sequence. This program could be written as generated sequences to their various contexts.If
follows: used, they will waste execution time;a
waste memory space. Branches and tes
mit the use of highly general macrosth
MACRO parameters to each call, omit code no
3ARGO VARY bly of highly general macros is one ol
&ARGO &COUNT,&ARG1,&ARG2,&ARG3
1,&ARG1
AIF (&COUNT EQ 1).FINI programming tools.
A Test if & COUNT 1
2,&ARG2
AIF (&COUNT EQ 2).FINI
A Test if& coUNT 2
FINI MEND
3,8ARG3 4.2.3 Macro Calls Within Macros
Expanded source
Since macro calls are "abbreviations'
able that such "abbreviations" shou
LOOP1 VARY
3,DATA1,DATA2,DATA3 LOOP1 1,DATA1
tions. For example,
2,DATAZ
3,DATA3
Example 5:
LOOP2 VARY
2,DATA3,DATA2 MACRO
LOOP2 1,DATA3
2,DATA2 ADD1 &ARG
A
L 1,&ARG
LOOP3 VARY A 1,F1
1,DATA1 1,DAT1
LOOP3 ST 1,&ARG
MEND
DATA1 DC MACRO &ARG1, &ARG2
DATA2 F'5' ADDS
DC
DATA3 F'10' &ARG1
DC ADD1
F'15' &ARG2
ADD1
ADD1 &ARG3

MEND
mac
Within the lefinition of the
119
MACRO LANGUAGE AND THE MACRO PRocEsSOR

Labels starting with a period (), such as labels and do not


.FINI, are macro
statement AIF (&COUNT EQ
appear in the output of the macro processor. The
1)FINI directs the macro processor to skip to the statement labelled .FINI if
the macro processor
the parameter corresponding to &COUNT is a 1; otherwise,
is to continue with the statement following the AIF pseudo-op.
arithmetic test and
AIF is a conditional branch pseudo-op; it performs an
AGO is an unconditional
branches only if the tested condition is true. The
label appearing on some
branch pseudo-op or go to' statement. It specifies a
statement in the macro instruction definition;
the macro processor con-
other These
instructions with the indicated statement.
tinues sequential processing of in macro
are directives to the macro processor and do not appear
statements
and unconditional branch instructions ina
as conditional
expansions. Just AGO con-
program direct the
order of program flow, AIF and
machine language
the macro processor expands
the statements in macro
trol the sequence in which
of instructions
to tailor specific occurrences
instructions. This makes it possible tests are
their various contexts. If machine language
branches and
to
sequences
execution time; also, the tests
and unused instructions will
will waste
used, they macro instruction language per
Branches and tests in the
waste memory space. the
macros that assemble
selectively, and by testing
mit the use of highly general for selcctive assem-
omit code not needed. facility
This
parameters to each call, most powerful
is one of the systems programmer's
tly of highly general
macros

programming tools.

Macros
4.2.3 Macro Calls Within
it seems reason-
"abbreviations" of instruction sequences,
Since macro calls are
within other macro defini-
"abbreviations" should be available
abie that such
tions. For example,
Example 5:

MACRO
&ARG
ADD1
1,&ARG
L
A 1,F"1
1, &ARG
ST
MEND
MACRO &ARG1, &ARG2, &ARG3
ADDS
&ARG1
ADD1
&ARG2
ADD1
&ARG3
ADD1
MEND three separate calls to
a pre-
"ADDS' are
Within the definition of the macro
GE AND THE MACRO
120 FEATURES OF A MCARO FACILIY MACRO LANGU,

PROCESSO
makes pertectly good sense. The pra
viously defined macro 'ADD1'. The use of the macro ADD1' has shortenedt gain
within macros will be discussed in the section
again-it

length of the definition of 'ADDS' and thus has made it more easily understogd
further in Figure 4,
treated
Such use of macros results in macro expansions on multiple "levels'. Thu mpleis

Macro
Instructions Defining Macros
4.2.4

Expanded source Expanded source viewed macros as generalizod abbrev


Source
(Level 1) (Level 2) Vehave seems reasonable topermit any
that it
noting
notin
macro definitions. In this
including
sequence,
used to simplify the process of de
be
might to realize that the inner ma
important
It is macro has be
MACRO
until fter the outer
ADD1 &ARG callable) implemente.
detinitions are
which
1,&ARG method by macros for subroutine
A 1,-F'1
define group of
a
ST 1,&ARG te
The following
example defines a
MEND sequence.
subroutine name
def+nes
with a
MACRO
when called generated
ADDS &ARG1,&ARG2, The individual
macros
subroutine.
&ARG3
their associated subrouti-
of
&SUB)
ADD1 &ARG1 argument
ADD1 &ARG2
Expansion of ADDS Expansion of ADD1
ADD1 &ARG3
MACRO
MEND DEFINE &SUB

1,DATA1 MACRO
&Y
1,-F'1' &SUB
ADD1 DATA1 A 1,DATA1 Definition CNOP 0,4
1,DATA2
Definition BAL 1,+8
of macro of macr A&Y)
A 1,-F'1 &SUB DC
ADD1 DATA2 DEFINE 15,-V(&SU
ADDS DATA1,DATA2, ST 1,DATA2 L

DATA3 1,DATA3 BALR 14,1


L
ADD1 DATA3
1,-F'1 MEND

ST 1,DATA3 MEND

witn
this macro
DATA1 DC F5' ne user might then call
F'5' DATA2 DC F'10
DATA1 DC
DATA2 DC F'10 DATA3 DC F'15
F'15 DEFINE COS
DATA3 DC

COS: the
named
defining ew macro
subsequently

example, the
macro
definition. The user might
several levels. For
involve
calls within m a c r o s
can
n of another macro. In fact,
condi
ndi.
cond
Macro
within the definition O another tc AR
be called possible for a macro COS
ADDS might AlF and Au) make it
as at some
as
facilities (such ths
tional macro
an
intinite loop-so long
this does not cause nth tume, decides not to call itself and the wil
generate

long as macro processor

call itself, So
the
called lor
macro,
having been
point the
MACRO LANGUAGE AND THE MACRO PROCEssOR 121

aain-it makes perfectly good sense. The problems inherent to macro call:
agai.
vithin macros will be diseussed in the section on recursion; the preceding ex

ample is treated further in Figure 4.5.

4.24 Macro Instructions Defining Macros


We have viewed macros as generalized abbreviations for instruction sequences,
noting that it seems reasonable to permit any valid statements in the abbreviated
sequence, inchluding macro definitions. In this manner a single macro instruction
might be used to simplify the process of defining a group of similar macros.
It is important to realize that the inner macro definition is not defined (i.e.,
after the outer macro has been called. This is because of the
callable) until
method by which definitions are implemented. For example, a user might wish
to define a group of macros for subroutine calls with some standardized calling
which
sequence. The following example defines a macro instruction DEFINE,
when called with a subroutine name defines a macro with the same name as the
subroutine. The individual macros generated bear the names (given through the

argument&SUB) of their associated subroutines.

MACRO
DEFINE &SUB Macro name: DEFINE
MACRO
&SUB &Y Dummy macro name
Definition Definition CNOP 0,4 Align boundary
of macro of macrob BAL 1,+8 Set register 1 to parameter list pointer
DEFINE &sUB DC A(&Y) Parameter list pointer
L 15,-V(&SUB) Address of subroutine
BALR 14,15 Transfer control to subroutine
MEND
MEND

with the statement


he user might then call this macro

DEFINE COS

dctining named COS: the statement expands into a new


a new macro
macro
Einition. The user might subsequently call the COS macro as followS:

COS AR

the macro processor will generate the calling sequence:


AND THE MACRO
LANGUAGE
PROCcE
122 MACRO
IMPLEMENTATION define the syntax of permissible argum
BAL 1, +8 Iso
DC AAR) address of AR matters in
matte
detail, we present some typical
L 15,-V(COs) V denotes address of external symbol uments may appear any where in a macr
BALR 14,15 We want to be able to
as op-codes.
ppear
For example, we might war=
Macro definitions within macros are sometimes called fixed strings.
"macro definitions within mean A concatenated with the argument
macro definitions."
(See Section 4.3.2 for further treatment of this example) It is harder
to have arguments inside a stri=
enclosed in single quotes. Thus, we
to be
with the argument replacing AB, and A'&»
4.3 IMPLEMENTATION the argument replacing &A, and B. Any
(A&B)) may be used as an argument.
Thus far have described a macro instruction scheme for
we
extending the basic In order for AIF and AGO to work, sor
assembly language. In the remainder of this chapter we will outline a method for
implementing such a scheme. be evaluated. A symbol defining capabilit-
variables may be assigned and reassigned va
STATEMENT OF PROBLEM
There are four basic tasks that any instruction procesor must perform
macro
4.3.1 Implementation of a Restricted Fao
Recognize macro definitions A macro instruction
1.
nize macro definitions identified processor must recog We begin by making some simplifying
by the MACRO and MEND pseudo-ops. This
task can be complicated when macro
definitions appear within macros. When
macro processor is functionally independ
MACROS and MENDs are nested, as in the Put text from the macro processor will
macro processor must
example of the previous section, the will not permit macro calls or definitic
recognize the nesting and correctly match the last or outer
MEND with the first MACRO. All of the these features introduce
intervening text, including nested complications,
MACROs and MENDs, defines a single macro instruction. alter we have
presented
the basic niacro p
A macro processor, like
2.Save the definitions The processor must store an assembier,
the macro instruction detr
nitions, which it will need for expanding macro calls. assembly language, lines are interrelate
another by its address or name,
3. Recognize calls The processor must whie
recognize macro calls that assembler.
lines, pon Moreover,
as
operation mnemonics. This suggests that macro names be handled appear of the address assigne
op-code.
as a type their addresses, and possibly
sidermacro definitions to constitute int
4. Expand
calls and substitute arguments The of our
processor must substitute for macr language are not so closely
dummy or macro definition arguments the corresponding
árguments fromn a nothing outsid themselves, and macra
macro call; the resulting symbolie (in this case,
assembly language) text is then
Substituted for the macro call. This text, OI course, may contain
(Remember
not valuee
our
values, ur
t for restrictions. Remember
additional parameters. Suppose, fom
macro definitions or calls.
In summary: the macro processor must recogn1ze ana process macro definitions
preceded by a
Preceded
stitutesy
Cessee
Or
statement defining Y-e.
stitutes Y', anot '10' for the macro de
and macro cails. finiti Cesses the
Our EQU statement at all, except
With regard. to arguments, designer
the or the macro
processor must make the macro processor algorithm will m=
several decisions. He must dummy arguments may
determine where appear in a Just input
macro definition for example, whether they may appear as op-codes. He msst st asas ourtext, searching first for macra
tion so the assembler
ma cannot proce
rocessor cannot e
AND THE MACRO PROCESSODR 123
MACRO LANGUAGE

also define the syntax


ofpermissible arguments. Although we do not treat these
we present some typical (and reasonable) answers. Dummy
matters in detail,
f o r example, they may
arguments may appear anywhere n a macro definition
arguments with
op-codes. We want to be able to concatenate
macro
appear as
we take to
we might want to write A&A, which
fixed strings. For example, to
This is easy
mean A
concatenated with the argument replacing &A. arrange.

to have arguments inside a string. For


this we permit the argument
Itisharder concatenated
Thus, we take A&AB to mean A
to be enclosed in single quotes.
concatenation of A,
with the argument replacing AB, and A'&A'B to méan the
and B. Any string enclosed in parentheses (e.g.,
the argument replacing &A,
argument.
(A&B)) may be used as an will have to
to work, some arithmetic expressions
In order for AIF and AGO which
capability, similar to the EQU facility by
be evaluated. A symbol defining
variables may be assigned and reassigned
values, is often useful.

Restricted Facility: A Two-Pass Algorithm


4.3.1 Implementation of a
We will assume that our

We begin by making simplifying assumptions.


some
the out-
independent of the assembler and that
macro processor is functionally
be fed into the assembler. Initialy
we
put text from the macro processor will Because
macro definitions.
will not permit macro calls or definitions within
we postpone
discussion of them until
these features introduce complications,
niacro processor algorithm.
alter we have presented the basic lines of a text. In
scans and processes
A macro processor, like an
assembler,
a line can
refer to
interrelated by addressing:
aSETmbly language, lines are
to the
be available ("known")
name, which
must
another by its address or preceding
address assigned to each line depends upon
45sembler. Moreover, the their contents as well.
If we con
and possibly upon
ines, upon their addresses, we may say that
the lines
constitute integral entities,
SIder macro definitions to refer to
interrelated. Macro definitions
are not so closely
O u r macro language macro definitions.
and calls refer only to
macro
Lhing outside themselves, Remember also that a macro call substitutes text,
Y is
Kemember our restrictions.
for example, that a macro call INCR
Suppose, macro processor sub
OValues, for parameters. Y EQU 10. The
defining Y-e.g., it never pro
eded by a statement definition argument-indeed,
ouutes "Y', not '10' for the
macro
complete line of text.)
statement at all, except as a or passes, over
t n e EQU make two systematic scans,
algorithm will macro calls.
for
ur macro processor
for macro
definitions and then
the nput text, searching first symbol before
its defini-
to a
a reference
Just as
4s our
Our assembler cannot process
call beforehaving
found
a macro
cannot expand
SO the processor
macro
dus. Ine
examining every operation code, will save all macro definitions in lirst a p
Definition Table (MDT) and save a copy of the input text, minus
macro de
tions, on secondary storage (e.g., magnetic tape) for use in the second pas
fust pass will also
prepare a Macro Name Table (MNT) along with the MDT.
second pass will then examine every operation mnemonic and
replace
macro name with the
appropriate text from the macro definitions. (A sepa=
MNT isnot essential, but it facilitates
searching for macro names.)
SPECIFICATION OF DATA BASES
The following data bases are used by the two passes of the macro proveso
Pass 1 data bases:
. The input macro source deck
2. The output macro source deck
copy for use by pass 2
3. The Macro Definition Table
(MDT), used to store the body of the ma
definitions
4. The Macro Name Table
(MNT), used to store the names of defined macro=
5. The Macro Definition Table Counter
avalable entry in the MDT
(MDTC), used
the to indicate nex
6. The Macro Name Table Counter
(MNTC), used to indicate the next aviu
able entry in the MNT
7. The Argument List Array (ALA), used to substitute index markers fr
dummy arguments before storing a macro definition
Pass 2 data bases:
1. The copy of the input macro source deck
2, The output expanded source deck to be used as input to the assembler
3. The Macro Definition Table (MDT), created by pass
4. The Macro Name Table (MNT), created by pass 1
5. The Macro Definition Table Pointer (MDTP), used to indicate the next line
of text to be used during macro expansion
6. The Argument List Array (ALA), used to substitute macro call arguments
the index markers in the stored macro definition
for

SPECIFICATION OF DATA BASE FORMAT


with nontrivial format are the Mac:o Definition Table
The only data bases and the Argument List Array (ALA).
(MDT), the Macro Name Table (MNT),
in detail.
The others we will not present

LIST ARRAY: The


Argument List Array (ALA) is used during
reverse functions. During pass i
ARGUMENT
2 but for somewhat
both pass 1 and pass during macro expansion. dummy
argument replacement
order to simplify later
AND THE MACRO PROCESSOr 125
NACRO LANGUAGE
indicators when
arguments in the macro
definition are replaced with positional name card is
is stored. The itn dunimy argument on the macro
the definition
the index marker symbol # where
represented in the body of the macro by (i.e., not available to
reserved for the use of the macro processor
#i is a symbol with the argument list
These symbols are used in conjunction
the programmers). arguments
of a macro call. The symbolic dummy
before expansion
prepared enable the macro processor to han dle
on the macro name card
to
are retained
replacement by name rather than by position.
argument Example 3. The stored
consider the macro INCR used in
As an example,
definition would be:
macro

MDT
Macro Definition Table

&ARG1,&ARG2,&ARG3

&LAB INCR
1, #1
0 A
A 2, #2
A 3, # 3
MEND

the index
arguments for
substitute m a c r o call
is necessary to the call
During pass 2 it encountering
definition. Thus upon
markers stored in
the macro

DATA1,DATA2,DATA3

LOOP INCR

would prepare an argument list array:


the macro call expander

Argument List Array

8 bytes per entry


Argument
Index
"LOOP1bbb"

"DATA 1bbb"
DATA2bbb"

"DATA3bbb"

character)
(b denotes the blank

call. Suppose that


this particular
while expanding
The list would be used only
a succeeding call were:
&ARG1-DATA3,&ARG2-DATA2,&ARG3-DATA1
INCR
126
IMPLEMENTATION MACRO ANGUAGE AND HE MALRD PROCESSORa

The macro processor would find that '&ARGI', '&ARG2', and '&ARG3' occuw
ccupy
argument positions 1, 2, and 3 on the macro name card. The resulting argumeat
ALGORITHM

4.1 and .2 are flowcharts of the macro definitin


list array would be: Figures
rithms.
Each of the algorithms makes a line-by-line s
Argument List Array boxes efer to the fetching of successivenpu
READ'
Index Argument into a workspace.
storage
"bbbbbbbb" (all blank) DEFINITION: The
"DATA3bbb" PASS 1-MACRO algorithm for pass
line. If it is a MACRO 2Seudo-op, the entire macmc
2
3
"DATA2bbb"
"DATA 1bbb"
input available locations in the Macro D
is in the next
saved
of the definition is the macro name line. T
MACRO DEFINITION TABLE: The Macro Definition Table (MDT) is a table The first line
Table (MNT), along with a pointer to
of text lines; if input is from 80-column cards, the MDT can be a table with 80 the Macro Name
def+nition. When the END pseudo
byte strings as entries. Every line of each macro definition, except the MACRO MDT entry of the
macro def+nitions
have been processed so contol
line, is stored in the MDT. (The MACRO line is useless during macro expansion.) the
calls.
The MEND is kept to indicate the end of the definition; and the macro name to process macro
AND EXPANSION: The a
lineis reained to facilitate keyword argument replacement. Thus, for example, PASS 2-MACRO CALLS
the INCR macro discussed might be stored as follows: tests the operation mnemonic of each input lin
4.2)
the MNT. When a call is found, the call processor=
Macro Definition Table
to the correspa
Definition Table Pointer (MDTP),
80 bytes per entry of the MDTP is
stored in the MDT. The initial value
Index Card
macro expander pr
index" field of the MNT entry. The
table of dummy argumen
Array (ALA) consisting of a
15 &LAB ng argunments to the call. This
list is simply a suceess
INCR &ARG1,&ARG2,&ARG3 I
16 #o 1, # 1 d t c h the dummy arguments on
the name card (tne
17 2, #2 of zero). Argum
18 3, T#3 wnich is considered to have an index
19 MEND call are considered blank, and superfluous argumenis
scheme compl is
nent reference by position, this locates the
references by name, the macro
processor
the beginnings
MACRO NAME TABLE: The Macro Name Table (MNT) serves a function very macro name line (which is available at
index.
similar to that of the assembler's Machine-Op Table (MOT) and Pseudo-Op Table MDT) in order to determine the properi
each successi
MDT; as
POT). Each MNT entry consists of a character string (the macro name) and a ading proceeds from the argu
for dummy
pointer(index) to the entry in the to
MDTthat corresponds the
beginning of the from the argument list are substituted
e MDT te
macro definition. The MNT entry for the INCR macro discussed might be: definis MEND line
the .MDI
in the
in
definition.Reading of the file. W
from the input
macro, and scanning continues
transfèrret

4 bytes deck is

Index
8 bytes
Name MDT index
Cncountered,
Cncount
the
ther processing.
xpanded
source

15 4.3.2 A
"INCRbbbb"
Single-Pass Algorithm for macro
definitions

uppose we wanted to provide defined


only as
problem here is that the
re inner
macro
is
ofthe
inner
called; in order to provide for any
use
RO LANGUAGE AND THE MALRo PROCESSoR 127

DORITHM
res 4.1 and 4.2 are flowcharts of the macro definition and expansion algo
The
ms. Each of the algorithms makes a line-by-line scan over its input.
from secondary
AD' boxes refer to the fetching of successive input lines
ageinto a workspace.
for pass 1 (Fig. 4.1) tests each
SS 1-MACRO DEFINITION: The algorithm
ut line. If it is a MACRO pseudo-op,
the entire macro definition that follows
Macro Definition Table (MDT).
saved in the next available locations in the line. The name is entered into
e first line of the
definition is the macro name
to the first location of the
Macro Name Table (MNT), along with a pointer
is encountered, all of
entry of the definition. When the END pseudo-op
T to pass 2 in order
e macro definitions have been processed so control transfers
process macro calls.

CALLS AND EXPANSION: The pass 2 (Fig.


algorithm for
ASS 2-MACRO
each input line to see if it is a name in
2) tests the operation mnemonic of sets a pointer, the Macro
MNT. When a call is found, the call processor
ne
corresponding macro definition
efinition Table Pointer (MDTP), to theMDTP is obtained from the "MDT
the
tored in the MDT. The initial value of the Argument List
field of the MNT entry. The macro expander prepares
ndex"
of a table of dummy argument indices and correspand
array (ALA) consisting succession of symbols ordered to
to the call. This list is simply a
ng arguments
first is the label argument,
on the name card (the
match the dummy arguments
not represented in a
is considered to have an index of zero). Arguments
which case of
are considered blank, and superfluous
arguments are ignored. In the
call scheme is completely straightforward.
For
this
argument reference by position, locates the dummy argument on the
references by name, the macro processorthe in the
available at beginning of the definition
macro name line (which is
MDT) in order to determine the proper index.
each successive line is read, the values
MDT; as
Reading proceeds from the for dummy argument indices
in the macro
Irom the argument list
substituted
are
terminates expansion of the
line in the MDT
definition. Reading of the MEND
from the
input file. When the END pseudo-op is
macro, and scanning continues transferred to the assembler for fur-
source deck is
Cncountered, the expanded
ther processing

4.3.2 A Single-Pass Algorithm


definitions within macros. The basic
uppose we wanted to provide for macro
the outer one has been
macro is defined only after
Problem here is that the inner we would have to re-
for any use of the inner macro,
called; in order to provide
1.0
MPLEMENTAT MACRO LANGUAGE AND THE MACRG PR0

Pass 1
PASS 2

MDTC Read next ource


card (copied by
MNTC1 pass 1

Read next Search MNT for match


source card with operation code

MACRO No Write copy Macro


name found
pseudo-op of source card

Yes END
No
pseudo-op
MDTP MOT index
Read next from MNT entry
SOurce card , Yes

Go to
PASS 2 Set up argument
Enter macro name list array
and current value
of MDTC in MNT
entry number MNTC Read next
source card MDTP MOTP +1

MNTC MNTC +1 Substitute index


notation for
Get line from
arguments
Prepare argurnent MDT
list orray

Enter line into


Enter macro narne MDT Substitute arguments
from macro call
card into MDT

MDTC MDTC+ 1
MDTC MDTC +1 MEND
Yes pseudo op

MEND
Yes pseudo-op
No
cal
? macro

GURE 4.2 Pass 2-proce

FiCURE A.1 P e 1..nraoneiaa .


MACR LANGUAGE AND THE MACRO PROCESSOR 129

PASS 2

Read next source


card (copied by
pass 1)

Search MNT for match


with operation code

Macro
name found No Write into expanded
sOurce card file

Yes

MDTP MDT index END


pseudo-op Noi
from MNT entry

Yes
Set up argument
list arrayY Supply expanded
source file to
assembler processing

MDTP MDTP +1

Get line from


MDT

Substitute arguments
from macro call

Yes MEND Write expanded


No
pseudo-op sOurce card

GURE 4.2 Pass 2-processi ng macro calls and expansion


MPLEMENTAT
macro-call passes. However, (.
peat both the macro-definition and the
Simpler solution that has the added advantage ol reduCing all macro p r

that we drew to an assembler


prokte
to a single pass. Consider again the analogy
calls because
macros must be
defn
definitions must be processed before
calls to them. However,
if we stipnle
the processor before it can expand
is called, we removeth
defined before it
restriction that every macro be
the same would bet
(Notice that
tial obstacle to a single-pass process. for
similar requirement symbols
symbols, except that
a
assemblers and the of macro langue
In case
restrictive to the program flow.)
prove unduly all macro
definitions precede thei
reasonable to require that
is perfectly macro use. It
does not even pree
restriction on
This imposes no significant the definitig
not during
for the call is expanded
macro from calling itself,
the macro but during a later call. the algorithms of Figures
4.3 and 4.4 combine
The flowcharts in Figures
macro
definitions within macros (butn
and permit
and 4.2 into a single pass considerable similarity
between th
You should notice
calls within macros). earlier two-pass
macro procesor dep
flowcharts and those
illustrating the a Man
new
variables introduced
in the one-pass design:
additional
There are two
indicator and a Macro
Definition Level Cou
Definition Input (MDI) used to keep traka
switches (counters)
The MDI and MDLC
are
(MDLC).
definitions.
calls and macro
macro
indicator has the value
"ON" during expansion of a macro c
The MDI of macro calls B*
other times. The actual expansion
value "OFF" at all
thhe second flowchart (FIB
the READ box, which is detailed in the
formed in read from the a
the switch MDI. If it is "ON", lines are
READ tests indicates the enu
The reading of a MEND line
Definition Table (MDT).
of a call; MDI is reset to *"OFF"
ad I
macro and
terminates expansion ned

from the regular input stream.


Note that lines re
line is obtained
Ould

expanded macro code come


READ may include macro definitions;
deli

therefore include mac


READ looking just like any other code and may
tions.
incremented by 1 when a Dseudo?

The Macro Definition Level Counter is


decremented by 1 when a MEND
pseudo-op is encountered and
occurs. t i o n , inclul

The MDLC is used to insure that the entire macro definitio k


MACROs and MENDs, gets stored in the MDT. This is analogou
statements with nested parentheses-eg., "(2"(A+B)+C)". If a pro
the entire expression, it must not stop at the first right parenthesis,
secv

must keep a count of left and right parentheses and recognize oun Counter

parenthesis is the true end of the expression. The MDLC actsu n e


the difference between the numbers of MACROs and MENDS e
aCRO
M
LANGUAGE AND THE MACRO PROCESSOR 131

One pass
macro
MACRO No Write into expanded
processor
pseudo o ource card file

Yes,
macro definition

MDTC
MNTC Macro END
peudo opP
MDI"OFF*" READ name

MDLC
Lline
Yes

Enter macro name Supply expanded


and current valuee source file to
READ of MDTC in MNT assembler
entry number MNTC processin9

Search MNT for


match wth
MNTC MNTC
operation code
Prepare m cro
definitiun
argument list array
Macro
name
NO
Enter macro namee
found card into MDT
Yes, macro call

MDI ON" MDTC MDTC 1

MDTP MDT index MDLC MDLC1


from MNT entry

Set up macro call READ


argument list array

Substitute index
notation for orgument
in definition

Enter line into MDT

MDTC MDTC +1

MACRO
MDLC MDLC 1
pseudoop
?
No

MEND
No
peudo op MOLC MDLC-
No
(a MDLC 0

IgURE 4.3 Simple one-pass macro processor


132
IMPLEMENTATION MACRO
LANGUAGE
AND THE MACRO Pa

MDT: index
READ
subroutine DEFINE
MACRO
3 #1
CNOP
Increment MDT BAL
DC
MDI "OFF) No, pointor to Got next card L
noxt ontry from MDDT BALR
Y6s
within
frmacro call
MDTPMDTP +1 9 MEND
10 MEND
Head next
Sourcs Card Substitute
from input MDI-"OFF" arguments MNT: Index Name
from macro
fils DEFINE
call

Y9s, after processing of statement DEFINE C


torninat6 Yes MEND
rmacro cal pseudo-op
MDT:

MDLC o No Line

macro dofn
RETURN within macro
No, AIP
TO MAIN call COS
or 11
PROCESING No 12 CNOP
AGO
13 BAL
L Yos 14 DC
15 L
Process AIF or AG0. 16 BALR
3et new value to 17 MEND
MDTP

FIGURE 4A Detail of READ functlon used for macro


MNT: index Name
oxpansion
DEFINE
tclls how many more MACROs than MENDs have beon 2 COS
read,
Notice that since it is possiblo to be
performing tho "macro definition uno
So far no output has been generated by
sirgultaneously with the "macro expansion pass, as is thho case pass"
a nacro definition with an inner mácro
when expanding upon
detinition,
Argunent List Arrays. One ALA is used tor macro
there must be two
separate pansionof the
actions of statement COS =

dofinitions. The other the macro processor by follow


6nploryed for macro call expansion. is
ceding example.
Consider now example 6 from section 4.2.4. Altor definition of the macro
EFINE
4.3.3
Implementation of Macro CallsWit
The basic
problem in implemeuting macr-
MACRO LANGUAGE AND THE MACRO PROCESSOR 133

MDT: Index

DEFINE &SUB
MACRO
#1 &Y
CNOP 0,4
BAL 1,+8
DC A(&Y)
L 15,-V(#1)
BALR 14,15
MEND
10 MEND

MDT
MNT: Index Name Index
DEFINE 1

after processing of statement DEFINE COS:

MDT

Lines 1-10 same as above

COS &Y
12 CNOP 0,4
13 BAL 1,+8
14 DC A(#1)
15 L 15,-VICOS)
BALR 14,15
MEND

MDT
MNT: Index Name Index

DEFINE 1
COS 11

d0 far no output has been generated by the macro: output lines will be created
on expansion of the statement COS AR. The reader should "simulate" the
s of the macro processor by following through the flowchart for the pre
ceding example.

S3Implementation of Macro Calls Within Macros


he
basic problem in implemcnting macro calls within macros is that of recur
ANGUAGE AND THE MACRO PRoc
MACRO

134 IMPLEMENTATION OFF", and reading will proceed fro


set to
sion. If a macro call is encountered during the expansion of a macro, the macto This of c o u r s e is wrong: there are unex
processor will have to expand the included macro call and then finish expanding Three errors have occurre The switch
the enclosing macro. The second cal might be expanded by a second macro pro to read.from the input stream
processor

cessor, which would lovk up the macro definition in the MDT and return the MDTP before it was reset to process the
expanded code to the first macro processor. Having many macro processors is argument
list for the call to ADDS has bee
neither efficient nor general; however, if a single macro processor is to handle handle macro calls within macros,
To
work recursively-that is,
to process one m
such nested macro calls, it must in some way save its status when it encounters
to continue with the previous, or 'ou.
nested calls. then
means of a stack, a storage se
Example 5 illustrates this problem. It two macros, ADDS and ADDI.
defines operate by
variables associated with e
The macro definitions in the MDT are shown in Figure 4.5. area for the
or 'stack frame' is
separate storage space
is
Macro Definition Table MDT status of unfinished computations pre
that is treated in a last-
Index Contents bounded array
ADD1 &ARG stored is the first one removed. A Stack
L , #1 frame at theof the stack. (Pointe
'top'
A 1, F"1' which is an address or index. Intuitively
ST 1, #1
MEND in core.)
ADDS &ARG1,8ARG2,8ARG3 In our example the pointer MDTP and
ADD1 #1 each call belong in the stack. The swit-
8 ADD1 #2
ADD1 FF3
Pointer (SP), which indicates the beginn.
9
10 MEND in place of MDI; in each succeeding frar

MDT after macro definition of example 5


value of SP to prevent it from being
FIGURE 4.5
Macro Definition Level Counter (MDLC
ions was easy because the inner delinm
Consider the action of the macro processor of Figure 4.3 and 4.4 when it en-
See the exercises at the end of this chaj
counters the macro call
we consider the stack
presentation,
DATA1, DATA2, DATA3 Strings; S(SP) refers to the beginning
ADDS
Position indexed by SP.
he organization of each stack fráme
macro call argument list array and set a pointer,
Our algorithm will prepare a ne previous value of SP; for the first
MDT; switch MDI is set to "ON". The READ function
MDTP, to line 6 of the
increments MDTP, gets
its next line from the MDT (line 7) and substitutes the On SP 1, like MDI=0 in the nonrecu
that it is ot within
a macro call expar
the line
argument, yielding of
MDTP, the MDT pointer; S(SP+2).
ADD1
DATA1 N-1)th) character strings of the current
gures 4.7 and 4.8 depict the macrd
another macro call. ApDI is
calls within well
is in trouble: it has
encountered
definitions as
Now our algorithm
will prepare a new argument ist array, set MDTP
tions. Simplemacro
"housekeeping" steps, su=
a macro name,
so the piocessor

the MDT, and set


MDI to *"ON", its
current value. The ADDI m a c r o Counter (MNTC) and the Macro
Defir
to line 1 of
at the end
MDTP will point to MDT Iine 5. MDI will be re ting a
new entry, have been omittec
will expand correctly;
aCRO LANGUAGE AND THE MACRO
PROCESSOR 135

set to "OFF", and reading will proceed from the input stream.
This of course is wrong: there are unexpanded lines left in the ADDS macro.
Three errors have occurred. The switch MDI has been turned off, causing the
nrocessor to read from the input stream instead of from the MDT; the value of
MDTP before it was reset to process the ADD1 macro has been lost; and the
argument list for the call to ADDS has been lost.
To handle macro calls within macros, the macro processor must be able to
work recursively-that is, to process one macro before it is finished with another,
then to continue with the previous, or 'outer,' one. Recursive
procedures usually
operate by means of a stack, a storage scheme that allocates a separate storage
area for the variables associated with each call to the procedure. Becausea
separate storage space or 'stackk frame' is associated with each recursive call, the
status of unfinished computations is preserved. Conceptually, a stack is an un-
bounded array that is treated in a last-in, first-out manner: the last element
stored is the first one removed. A Stack Pointer (SP) indicates the position or
frame at the 'top' of the stack. (Pointer is a term that refers to a data item
which is an address or index. Intuitively, a
pointer points
to information stored
in core.)
In our example
the pointer MDTP and the argument list ALA
asso ciated with
each call belong in the stack. The switch MDI must also be saved. The Stack
Pointer (SP), which indicates the beginning of the
in place of MDI; in each
current stack frame, can serve
suceeding frame, however, we must save the previous
value of SP to prevent it from being lost.
(Compare this with our use of the
Macro Definition Level Counter
(MDLC). 'Recursive' handling of macro defini-
tions was easy because the inner definitions do not have
any associated data-
see the exercises at the end of
this chapter). Ignoring the actual method of data
Tepresentation, we consider the stack to be an array of pointers and character
Strings; S(SP) refers to the beginning of the current (top) stack frame, the
position indexed by SP.
The organization of each stack frame is depicted in Figure 4.6. S(SP) contains
ne previous value of SP; for the first frame, where SP=1,S(SP)=-1. The condi-
1on
SP=1, like MDI=0 in the nonrecursive algorithm, tells the macro processor
ldt it is not withinexpansion. S(SP+1) contains the current value
a macro call
MDTP, the MDT pointer;S(SP+2)...S(SP+N+1) con tain the N (Oth through
1th) character strings of the current argument ist.
ures 4.7 and 4.8 depict the macro processor algorithm that handles macro
s Within macro definitions as well as macro definitions within macro detini
tions. Simple "h
"housekeeping" steps, such as incrementing the Macro Name Table
Counter (MNTC)
unter (MNTC) and
an the Macro Definition Table Counter (MDTC) after inser.
ting
4new entry, have been omitted to simplify the flowcharts. The reauer
136 IMPLEMENTATI

Index
Contents Explanation
in stack
1

Previous framels)

to start of
Old SPnter
previous stack frame
SP
S(SP)
MDT pointer (MDTP) for thisfrane
S(SP+1)
SP+1
Oth (label) argument
One SP+2
S(SP+2)
stack S(SP+3) 1st argument Macro call
frame SP+3
argument
list array
(ALA)

S(SP+1+N)
(N-1)th argument
(SP+2)+IN-1)

Available for next frame

macro name
line
N: number of arguments on

value of stack pointer


SP: current
stack
contents of
mth position on the
SIm):

Stack organization
FIGURE 4.6

new flowchars
described in these
that the algorithm Notice that
confim the fact 4.3 and 4.4).
should algorithm (Figs. in
similar to the previous number of elements u
indeed very the
frame depends upon
entries in a stack
arguments in
the macro deliniu
number of
number of dummy macro 4 9
operation the
of
list (i.e., the hand-simulate the
argument reader to
desirable for the source program.Figu in
It is expand a macro
various key poin
algorithm to
cessor by
using the and expanded
source at
Table (Mu
the stack
the state of
Definition d that
Macro
illustrates
The corresponding
Example 5. recommenur Sei s
processing
of 4.5. It is strongly
the presented
in Figure and compareres
example was expansion
for this of the macro

through all the steps


go
the reader 4.9b.
4.9a and
with Figures
MACRO LANGUAGE AND THE MACRO PROcESSOR
127

Macro processor

MDLC Initial ization)


z

MACROO No Write into


READ o Pseudo
op Expandedd
Source File
es, macro
definition
Macro END
Search MNT for READ name seudo-op
match with Lline
operation code
Enter macro name
Yes
and current value Supply expanded
of MOTC in MNT source file to
Macro assembler
name No entry number MNTC|
processi ng
found?
Yes
Prepare macro
SSPN+2) SPSave SP]| definition argument
list arrayY

- SP+N+2et new SP valuel


Enter macro name
card into MDT

SSP+1)MDT index
from MNT entry
MDLC-MDLC 1
Set up macro call
argument list array in
SISP+2)... S(SP+N+1)
where N total number
of arguments
READ

Substitute index notation for


arguments in detinitian

Enter line into MOT

MACRO YesMOLC uOLC


pseudo-oo
No

MENO Yes
MDLC MOLC -
'See detail of READ function in Fig 4.8) pseudo o

MDLC

IGURE 4.7 Onepass macro processor capabte oi handiing maco calls withan maro
definitions

You might also like