Macroprocessors Donovan
Macroprocessors Donovan
macro language
and the macro processor
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
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-
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:
MACRO
&LAB INCR &ARG1,&ARG2,&ARG3
&LAB A 1,&ARG1
A 2,&ARG2
A 3,&ARG3
MEND
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
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
MEND
mac
Within the lefinition of the
119
MACRO LANGUAGE AND THE MACRO PRocEsSOR
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
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
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
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
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
DEFINE COS
COS AR
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
"DATA 1bbb"
DATA2bbb"
"DATA3bbb"
character)
(b denotes the blank
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 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
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.
Pass 1
PASS 2
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
MDTC MDTC+ 1
MDTC MDTC +1 MEND
Yes pseudo op
MEND
Yes pseudo-op
No
cal
? macro
PASS 2
Macro
name found No Write into expanded
sOurce card file
Yes
Yes
Set up argument
list arrayY Supply expanded
source file to
assembler processing
MDTP MDTP +1
Substitute arguments
from macro call
must keep a count of left and right parentheses and recognize oun Counter
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
Substitute index
notation for orgument
in definition
MDTC MDTC +1
MACRO
MDLC MDLC 1
pseudoop
?
No
MEND
No
peudo op MOLC MDLC-
No
(a MDLC 0
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
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
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
MDT
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.
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
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)
macro name
line
N: number of arguments on
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
Macro processor
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
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