Sunteți pe pagina 1din 46

Macroprocessor 3-1

Chapter
3

MACROPROCESSOR

Key Topics 3.1 Introduction


3.1 Introduction
3.2 Macro Definition Macros are single-line abbreviations for a certain group of
3.3 Macro Expansion
3.4 Nested Macro Calls instructions. Once the macro is defined, these groups of
3.5 Features of Macro Facility
3.5.1 Macro Instruction Arguments
instructions can be used anywhere in a program. This unit
3.5.2 Conditional Macro Expansion discusses the concept of macros and also how to define and
3.5.3 Macro Instructions Defining
Macros expand it. It also describes nested macro facility and use of macro
3.6 Design of a Macro Pre-processor
3.6.1 Implementation of Two-Pass instructions in programming. This unit also focuses on the design
Algorithm
3.6.1.1 First Pass of a macro pre-processor.
3.6.1.2 Second Pass
3.6.1.3 Two-Pass Algorithm 3.2 Macro Definition
3.6.1.3.1 Pass 1 Macro
Definition
3.6.1.3.2 Pass 2 Macro It is sometimes necessary for an assembly language
Calls and programmer to repeat some blocks of code in the course of a
Expansion
3.6.2 Implementation of Single-Pass program. The programmer needs to define a single machine
Algorithm
3.7 Macro Processor Algorithm and Data instruction to represent a block of code for employing a macro
Structure
3.7.1 Comparison of Macro Processor in the program. The macro proves to be useful when instead of
Design
3.8 Machine-independent Macro-Processor
writing the entire block again and again, you can simply write
Features the macro that you have already defined.
3.8.1 Concatenation of unique labels:
3.8.2 Generation of Unique Labels An assembly language macro is an instruction that represents
3.8.3 Conditional Macro Expansion
3.8.4 Keyword Macro Parameters several other machine language instructions at once. In other
3.9 Macro Processor Design Options
3.9.1 Recursive Macro Expansion words, a macro is an abbreviation for a sequence of operations.
3.9.2 General-Purpose Macro
Processors Let us consider an example, which shows the use of a pseudo-op
3.9.3 Macro Processing within named Define Constant (DC).
Language Translators
3.9.4 Line-by-Line Macro Processor
3.9.5 Integrated Macro Processor
3-2 Macroprocessor

:
:
A 1,DATA Add contents of DATA to register 1
A 2,DATA Add contents of DATA to register 2
A 3,DATA Add contents of DATA to register 3
:
:
A 1,DATA Add contents of DATA to register 1
A 2,DATA Add contents of DATA to register 2
A 3,DATA Add contents of DATA to register 3
:
:
DATA DC F'2'
:
:

In this program, the following sequence occurs twice.


A 1,DATA Add contents of DATA to register 1
A 2,DATA Add contents of DATA to register 2
A 3,DATA Add contents of DATA to register 3
A macro facility permits you to attach a name to the sequence that is occurring several times
in a program and then you can easily use this name when that sequence is encountered. All you
need to do is to attach a name to a sequence with the help of the macro instruction definition. The
following structure shows how to define a macro in a program:
Start of definition-------------------------------------------------------------  Macro
Macro name--------------------------------------------------------------------  [ ]
Sequence to be abbreviated ---------
---------
---------
End of definition--------------------------------------------------------------  MEND
This structure describes the macro definition in which the first line of the definition is the
MACRO pseudo-op. Following line is the name line for macro, which identifies the macro instruction
name. The line following the macro name includes the sequence of instructions that are being
abbreviated. Each instruction comprises of the actual macro instruction. The last statement in the
macro definition is MEND pseudo-op. This pseudo-op denotes the end of the macro definition and
terminates the definition of macro instruction.
Macroprocessor 3-3

3.3 Macro Expansion


Once a macro is being created, the interpreter or compiler automatically replaces the pattern,
described in the macro, when it is encountered. The macro expansion always happens at the compile-
time in compiled languages. The tool that performs the macro expansion is known as macro expander.
Once a macro is defined, the macro name can be used instead of using the entire instruction sequence
again and again.
As you need not write the entire program repeatedly while expanding macros, the overhead
associated with macros is very less. This can be explained with the help of the following example.
In this example, the name INC has been assigned to the repeated sequence. This is noticeable in the
example that INC is the name of the macro that corresponds to a particular sequence of instructions.
When this sequence of instructions is required in the program, the name of the macro that has been
already defined can be replaced instead of writing the entire sequence of instructions repeatedly. In
the following code, you will notice that the same set of instructions is written corresponding to the
macro name INC. You can notice the source and the corresponding expanded source in the following
code:

Source Expanded Source

MACRo

INC

A 1, DATA

A 2, DATA

A 3, DATA

MEND

INC A 1, DATA

A 2, DATA

A 3, DATA

INC
A 1, DATA

A 2, DATA

A 3, DATA

DATA DC F’2’ DC DATA


3-4 Macroprocessor
The macro processor replaces each macro call with the following lines:
A 1,DATA
A 2,DATA
A 3,DATA
The process of such a replacement is known as expanding the macro. The macro definition
itself does not appear in the expanded source code. This is because the macro processor saves the
definition of the macro. In addition, the occurrence of the macro name in the source program refers
to a macro call. When the macro is called in the program, the sequence of instructions corresponding
to that macro name gets replaced in the expanded source.

3.4 Nested Macro Calls


Nested macro calls refer to the macro calls within the macros. A macro is available within
other macro definitions also. In the scenario where a macro call occurs, which contains another
macro call, the macro processor generates the nested macro definition as text and places it on the
input stack. The definition of the macro is then scanned and the macro processor compiles it. This
is important to note that the macro call is nested and not the macro definition. If you nest the macro
definition, the macro processor compiles the same macro repeatedly, whenever the section of the
outer macro is executed. The following example can make you understand the nested macro calls:

MACRO
SUB 1 &PAR
L 1, & PAR
A 1, = F'2'
ST 1, &PAR
MEND
MACRO
SUBST &PAR1, &PAR2, &PAR3
SUB1 &PAR1
SUB1 &PAR2
SUB1 &PAR3
2MEND
You can easily notice from this example that the definition of the macro 'SUBST' contains
three separate calls to a previously defined macro 'SUB1'. The definition of the macro SUB1 has
shortened the length of the definition of the macro 'SUBST'. Although this technique makes the
program easier to understand, at the same time, it is considered as an inefficient technique. This
technique uses several macros that result in macro expansions on multiple levels. The following
code describes how to implement a nested macro call:
Macroprocessor 3-5
This is clear from the example that a macro call, SUBST, in the source is expanded in the
expanded source (Level 1) with the help of SUB1, which is further expanded in the expanded
source (Level 2).

Expanded Source Expanded Source


Source
(Level 1) (Level 2)

MACRO
SUB1 &PAR
L 1, &PAR
A 2, = f’2’
ST 1, &PAR
MEND
MACRO
SUBS &PAR1, &PAR2, &PAR2
SUB1 &PAR1
SUB1 &PAR2
SUB1 &PAR3 Expansion of SUBST Expansion of SUB1
MEND

SUBST DATA1, DATA2, DATA3 L 1, DATA1


A 2, = f’2’
SUB1 DATA1 ST 1, DATA1

SUB1 DATA2
L 1, DATA2
SUB1 DATA3 A 2, = f’2’
ST 1, DATA2

L 1, DATA3
A 2, = f’2’
ST 1, DATA3

DATA1, DC F’5’ DATA1, DC F’5’


DATA2, DC F’10’ DATA2, DC F’10’
DATA3, DC F’15’ DATA3, DC F’15’

Macro calls within macros can involve several levels. This means a macro can include within
itself any number of macros, which can further include macros. There is no limit while using
macros in a program. In the example discussed, the macro SUB1 might be called within the definition
of another macro. The conditional macro facilities such as AIF and AGO makes it possible for a
macro to call itself. The concept of conditional macro expansion will be discussed later in this unit.
The use of nested macro calls is beneficial until it causes an infinite loop. Apart from the benefits
3-6 Macroprocessor

provided by the macro calls, there are certain shortcomings with this technique. Therefore, it is
always recommended to define macros separately in a separate file, which makes them easier to
maintain.

3.5 Features of Macro Facility


We have already come across the need and importance of macros. In this section, we are
concentrating on some of the features of macros. We have already discussed one of the primary
features of macro, which are nested macros. The features of the macro facility are as follows:
• Macro instruction arguments
• Conditional macro expansion
• Macro instructions defining macros
Let us discuss these features in detail one by one.

3.5.1 Macro Instruction Arguments


The macro facility presented so far inserts block of instructions in place of macro calls. This
facility is not at all flexible, in terms that you cannot modify the coding of the macro name for a
specific macro call. An important extension of this facility consists of providing the arguments or
parameters in the macro calls. Consider the following program.

:
:
:
A 1,DATA1
A 2, DATA1
A 3, DATA1
:
:
:
A 1,DATA2
A 2,DATA2
A 3,DATA2
:
:
:
A 1,DATA3
A 2,DATA3
A 3,DATA3
DATA1 DC F'5'
DATA2 DC F'10'
DATA3 DC F'15'
Macroprocessor 3-7
In this example, the instruction sequences are very much similar but these sequences are not
identical. It is important to note that the first sequence performs an operation on an operand DATA1.
On the other hand, in the second sequence the operation is being performed on operand DATA2.
The third sequence performs operations on DATA3. They can be considered to perform the same
operation with a variable parameter or argument. This parameter is known as a macro instruction
argument or dummy argument. The program, previously discussed, can be rewritten as follows:

Source Expanded source

MACRO Macro INC has one argument

INC &PAR
A 1, &PAR
A 2, &PAR
A 3, &ARG
MEND
:
:
:
INC DATA1 Use DATA1 as operand A 1,DATA1
: A 2,DATA1
: A 3,DATA1
:

INC DATA2 Use DATA1 as operand A 1,DATA2


: A 2,DATA2
: A 3,DATA3
:
INC DATA3 Use DATA1 as operand A 1,DATA3
: A 2,DATA3
: A 3,DATA3
DATA1 DC F’5’ DATA1 DC F’5’
DATA2 DC F’10’ DATA2 DC F’10’
DATA3 DC F’15’ DATA3 DC F’15’
:
:
Notice that in this program, a dummy argument is specified on the macro name line and is
distinguished by inserting an ampersand (&) symbol at the beginning of the name. There is no
limitation on supplying arguments in a macro call. The important thing to understand about the
macro instruction argument is that each argument must correspond to a definition or dummy argument
on the macro name line of the macro definition. The supplied arguments are substituted for the
respective dummy arguments in the macro definition whenever a macro call is processed.

3.5.2 Conditional Macro Expansion


We have already mentioned the conditional macro expansion in the nested macro calls. In this
section, we will discuss about the important macro processor pseudo-ops such as AIF and AGO.
These macro expansions permit conditional reordering of the sequence of macro expansion. They
are responsible for the selection of the instructions that appear in the expansions of a macro call.
3-8 Macroprocessor

These selections are based on the conditions specified in a program. Branches and tests in the
macro instructions permit the use of macros that can be used for assembling the instructions. The
facility for selective assembly of these macros is considered as the most powerful programming
tool for the system software. The use of the conditional macro expansion can be explained with the
help of an example. Consider the following set of instructions:

LOOP 1 A 1,DATA1
A 2,DATA2
A 3,DATA3
:
:
LOOP 2 A 1,DATA3
A 2,DATA2
:
:
DATA1 DC F'5'
DATA2 DC F'10'
DATA3 DC F'15'

In this example, the operands, labels and number of instructions generated are different in
each sequence. Rewriting the set of instructions in a program might look like:

:
:
MACRO
&PAR0 VARY &COUNT, &PAR1, &PAR2, &PAR3,
A 1, &PAR1
AIF (&COUNT EQ 1). FINI Test if & COUNT = 1
A 2, &PAR2
AIF (&COUNT EQ 2). FINI Test if & COUNT = 2
ADD 3, &PAR3
.FINI MEND
: Expanded source
:
LOOP1 VARY 3,DATA1, DATA2, DATA3 LOOP1 A 1,DATA1
A 2,DATA2
: A 3,DATA3
:
LOOP2 VARY 2,DATA3, DATA2 LOOP2 A 1,DATA3
: A 2,DATA2
:
DATA1, DC F’5’
DATA2, DC F’10’
DATA3, DC F’15’
Macroprocessor 3-9
The labels starting with a period (.) such as .FINI are macro labels. These macro labels do not
appear in the output of the macro processor. The statement AIF (&COUNT EQ 1).FINI directs
the macro processor to skip to the statement labelled .FINI, if the parameter corresponding to &
COUNT is one. Otherwise, the macro processor continues with the statement that follows the AIF
pseudo-op.
AIF pseudo-op performs an arithmetic test and since it is a conditional branch pseudo-op, it
branches only if the tested condition is true. Another pseudo-op used in this program is AGO,
which is an unconditional branch pseudo-op and works as a GOTO statement. This is the label in
the macro instruction definition that specifies the sequential processing of instructions from the
location where it appears in the instruction. These statements are indications or directives to the
macro processor that do not appear in the macro expansions.
We can conclude that AIF and AGO pseudo-ops control the sequence of the statements in
macro instructions in the same way as the conditional and unconditional branch instructions direct
the order of program flow in a machine language program.

3.5.3 Macro Instructions Defining Macros


Here, we are focusing your attention on those macro instructions that defines macros. A single
macro instruction can also simplify the process of defining a group of similar macros. The
considerable idea while using macro instructions defining macros is that the inner macro definition
should not be defined until the outer macro has been called once. Consider a macro instruction
INSTRUCT in which another subroutine &ADD is also defined. This is explained in the following
macro instruction.

MACRO
INSTRUCT &ADD Macro name : INSTRUCT
MACRO
&ADD &Z Dummy macro name
De?nition CNOP 0,4 Align boundary
De?nition of
of macro BAL 2,& + 8 Set register 2 to parameter list pointer
macro &ADD
INSRUCT DC A(&Z) Parameter list pointer
L 15,=Y(&ADD) Address of subroutine
BALR 14,15
MEND
MEND

In this code, first the macro INSTRUCT has been defined and then within INSTRUCT, a new
macro &ADD is being defined. Macro definitions within macros are also known as "macro definitions
within macro definitions".

3.6 Design of a Macro Pre-processor


A macro pre-processor effectively constitutes a separate language processor with its own
language. A macro pre-processor is not really a macro processor, but is considered as a macro
translator. The approach of using macro pre-processor simplifies the design and implementation of
3-10 Macroprocessor
macro pre-processor. Moreover, this approach can also use the features of macros such as macro
calls within macros and recursive macros. Macro pre-processor recognises only the macro definitions
that are provided within macros. The macro calls are not considered here because the macro pre-
processor does not perform any macro expansion.
The macro preprocessor generally works in two modes: passive and active. The passive mode
looks for the macro definitions in the input and copies macro definitions found in the input to the
output. By default, the macro pre-processor works in the passive mode. The macro pre-processor
switches over to the active mode whenever it finds a macro definition in the input. In this mode, the
macro pre-processor is responsible for storing the macro definitions in the internal data structures.
When the macro definition is completed and the macros get translated, then the macro pre-processor
switches back to the passive mode.
As it is already described in Unit 1, an assembler involves different steps such as statement of
the problem, specification of the data structures and so on. The four basic tasks that are required
while specifying the problem in the macro pre-processor are as follows:
1. Recognising macro definitions
A macro pre-processor must recognise macro definitions that are identified by the MACRO
and MEND pseudo-ops. The macro definitions can be easily recognised, but this task is complicated
in cases where the macro definitions appear within macros. In such situations, the macro pre-
processor must recognise the nesting and correctly matches the last MEND with the first MACRO.
2. Saving the definitions
The pre-processor must save the macro instructions definitions that can be later required for
expanding macro calls.
3. Recognising macro calls
The pre-processor must recognise macro calls along with the macro definitions. The macro
calls appear as operation mnemonics in a program.
4. Replacing macro definitions with macro calls
The pre-processor needs to expand macro calls and substitute arguments when any macro call
is encountered. The pre-processor must substitute macro definition arguments within a macro call.
It is quite essential for a pre-processor designer to decide on certain issues such as whether or
not dummy arguments appear in the macro definition. It is important to note that dummy arguments
can appear anywhere in a macro definition.
The implementation of macro pre-processor can be performed by specifying the databases
used macro pre-processor. You can implement macro-preprocessor using:
• Implementation of two-pass algorithm
• Implementation of single-pass algorithm
Macroprocessor 3-11

3.6.1 Implementation of Two-Pass Algorithm


The two-pass algorithm to design macro pre-processor processes input data into two passes.
In first pass, algorithm handles the definition of the macro and in second pass, it handles various
calls for macro. Both the passes of two-pass algorithm in detail are:

3.6.1.1 First Pass


The first pass processes the definition of the macro by checking each operation code of the
macro. In first pass, each operation code is saved in a table called Macro Definition Table (MDT).
Another table is also maintained in first pass called Macro Name Table (MNT). First pass uses
various other databases such as Macro Name Table Counter (MNTC) and Macro Name Table
Counter (MDTC). The various databases used by first pass are:
1. The input macro source deck.
2. The output macro source deck copy that can be used by pass 2.
3. The Macro Definition Table (MDT), which can be used to store the body of the macro
definitions. MDT contains text lines and every line of each macro definition, except the
MACRO line gets stored in this table. For example, consider the code described in macro
expansion section where macro INC used the macro definition of INC in MDT. Table 3.1
shows the MDT entry for INC macro:
Table 3.1: MDT

Macro De?nition Table (MDT)

&LAB INC &ARG1, &ARG2, &ARG3


#0 A 1, #1

A 2, #2

A 3, #3

MEND

4. The Macro Name Table (MNT), which can be used to store the names of defined macros.
Each MNT entry consists of a character string such as the macro name and a pointer such
as index to the entry in MDT that corresponds to the beginning of the macro definition.
Table 2.2 shows the MNT entry for INCR macro:
3-12 Macroprocessor
Table 3.2: MNT

Macro De?nition Table (MDT)

&LAB INC &ARG1, &ARG2, &ARG3


#0 A 1, #1

A 2, #2

A 3, #3

MEND

5. The Macro Definition Table Counter (MDTC) that indicates the next available entry in the
MDT.
6. The Macro Name Table Counter (MNTC) that indicates the next available entry in the
MNT.
7. The Argument List Array (ALA) that can be used to substitute index markers for dummy
arguments prior to store a macro definition. ALA is used during both the passes of the
macro pre-processor. During Pass 1, dummy arguments in the macro definition are replaced
with positional indicators when the macro definition is stored. These positional indicators
are used to refer to the memory address in the macro expansion. It is done in order to
simplify the later argument replacement during macro expansion. The ith dummy argument
on the macro name card is represented in the body of the macro by the index marker symbol
#. The # symbol is a symbol reserved for the use of macro pre-processor.
Table 3.3: ALA

Macro De?nition Table (MDT)

&LAB INC &ARG1, &ARG2, &ARG3


#0 A 1, #1

A 2, #2

A 3, #3

MEND
Macroprocessor 3-13
3.6.1.2 Second Pass
Second pass of two-pass algorithm examine each operation mnemonic such that it replaces
macro name with the macro definition. The various data-bases used by second pass are:
1. The copy of the input macro source deck.
2. The output expanded source deck that can be used as an input to the assembler.
3. The MDT that was created by pass 1.
4. The MNT that was created by pass 1.
5. The MDTP for indicating the next line of text that is to be used during macro expansion.
6. The ALA that is used to substitute macro calls arguments for the index markers in the
stored macro definition.

3.6.1.3 Two-Pass Algorithm


In two-pass macro-preprocessor, you have two algorithms to implement, first pass and second
pass. Both the algorithms examines line by line over the input data available. Two algorithms to
implement two-pass macro-preprocessor are:
• Pass 1 Macro Definition
• Pass 2 Macro Calls and Expansion

3.6.1.3.1 Pass 1 Macro Definition


Pass 1 algorithm examines each line of the input data for macro pseudo opcode. Following are
the steps that are performed during Pass 1 algorithm:
1. Intialize MDTC and MNTC with value one, so that previous value of MDTC and MNTC is
set to value one.
2. Read the first input data.
3. If this data contains MACRO pseudo opcode then
A. Read the next data input.
B. Enter the name of the macro and current value of MDTC in MNT.
C. Increase the counter value of MNT by value one.
D. Prepare that argument list array respective to the macro found.
E. Enter the macro definition into MDT. Increase the counter of MDT by value one.
F. Read next line of the input data.
G. Substitute the index notations for dummy arguments passed in macro.
H. Increase the counter of the MDT by value one.
I. If mend pseudo opcode is encountered then next source of input data is read.
J. Else expands data input.
4. If macro pseudo opcode is not encountered in data input then
A. A copy of input data is created.
B. If end pseudo opcode is found then go to Pass 2.
C. Otherwise read next source of input data.
3-14 Macroprocessor

Pass 1

MDTC = 1
MNTC = 1

Read Input from


Source card

Is Is END
NO Create a copy of
MACRO pseudo
Pseudo-op source card Opcode is NO
encountered found

YES YES
Read next Input from Go to PASS 2
Source card

Enter the name of the


macro and current value
of MDTC in MNT

MNTC = MNTC + 1
Substitute arguments with
index notation

Prepare ALA

Enter line into MDT

Enter the name of the


macro card into MDT
MDTC = MDTC + 1

MDTC = MDTC + 1

Is
Read next Input from MEND NO
Source card pseudo
opcode
encountered

YES

Figure 3.1: Flow Chart for First Pass


Macroprocessor 3-15
3.6.1.3.2 Pass 2 Macro Calls and Expansion
Pass two algorithm examines the operation code of every input line to check whether it exist
in MNT or not. Following are the steps that are performed during second pass algorithm:
1. Read the input data received from Pass 1.
2. Examine each operation code for finding respective entry in the MNT.
3. If name of the macro is encountered then
A. A Pointer is set to the MNT entry where name of the macro is found. This pointer is
called Macro Definition Table Pointer (MDTP).
B. Prepare argument list array containing a table of dummy arguments.
C. Increase the value of MDTP by value one.
D. Read next line from MDT.
E. Substitute the values from the arguments list of the macro for dummy arguments.
F. If mend pseudo opcode is found then next source of input data is read.
G. Else expands data input.
4. When macro name is not found then create expanded data file.
5. If end pseudo opcode is encountered then feed the expanded source file to assembler for
processing.
6. Else read next source of data input.

3.6.2 Implementation of Single-Pass Algorithm


The single-pass algorithm allows you to define macro within the macro but not supports macro
calls within the macro. In the single-pass algorithm two additional variables are used, Macro
Definition Input (MDI) indicator and Macro Definition Level Counter (MDLC). Following is the
usage of MDI and MDLC in single-pass algorithm:
• MDI indicator : Allows you to keep track of macro calls and macro definitions. During
expansion of macro call, MDI indicator has value ON and retains value OFF otherwise. If
MDI indicator is on, then input data lines are read from MDT until mend pseudo opcode is not
encountered. When MDI is off, then data input is read from data source instead of MDI.
• MDLC indicator : MDLC ensures you that macro definition is stored in MDT. MDLC is a
counter that keeps track of the numbers of macro1 and mend pseudo opcodes found.
Single-pass algorithm combines both the algorithms defined above to implement two-pass
macro pre-processor. Following are the steps that are followed during single-pass algorithm:
1. Initialize MDTC and MNTC to value one and MDLC to zero.
2. Set MDI to value OFF.
3. Performs read operation.
4. Examine MNT to get the match with operation code.
5. If macro name is found then
3-16 Macroprocessor
Figure 3.2 shows the above steps followed to implement second pass algorithm in dia-
grammatical structure:
Pass 2

Read Input from


Source card

Search MNT for match


with Op-code

NO Write into Is END


Is
expanded source pseudo
MACRO name
card ?le Opcode is
found
found
NO

YES
YES
MDTP = MDT index
from MNT
Feed expanded source ?le
to assembler processing

Prepare ALA

DTP = MDTP + 1

Get line from MDT

Substitute arguments from


macro call

Is
MEND NO Write expanded
pseudo source card
opcode
encountered

YES

Figure 3.2: Flow Chart for Second Pass


Macroprocessor 3-17
A. MDI is set to ON.
B. Prepare argument list array containing a table of dummy arguments.
C. Performs read operation.
6. Else it examines that macro pseudo opcode is encountered. If macro pseudo opcode is
found then
A. Enter the name of the macro and current value of MDTC in MNT at entry number
MNTC.
B. Increment the MNTC to value one.
C. Prepare argument list array containing a table of dummy arguments..
D. Enter the macro card into MDT.
E. Increment the MDTC to value one.
F. Increment the MDLC to value one.
G. Performs read operation.
H. Substitute the index notations for the arguments list of the macro for dummy arguments.
I. Enter data input line into MDT.
J. Increment the MDTC to value one.
K. If macro pseudo opcode is found then increments the MDLC to value one and performs
read operation.
L. Else it checks for mend pseudo opcode if not found then performs read operation.
M. If mend pseudo opcode is found then decrement the MDLC to value one.
N. If MDLC is equal to zero then it goes to step 2. Otherwise, it performs read operation.
7. In case macro pseudo opcode is not found, then write it into expanded source card file.
If end pseudo opcode is found, then it feeds expanded source file to assembler for processing,
otherwise performs read operation at step 2.

3.7 Macro Processor Algorithm and Data Structure


Design can be done as two-pass or a one-pass macro. In case of two-pass assembler.
Two-pass macro processor
• You may design a two-pass macro processor
– Pass 1:
• Process all macro definitions
– Pass 2:
• Expand all macro invocation statements
• However, one-pass may be enough
– Because all macros would have to be defined during the first pass before any macro
invocations were expanded.
• The definition of a macro must appear before any statements that invoke that
macro.
• Moreover, the body of one macro can contain definitions of the other macro
• Consider the example of a Macro defining another Macro.
3-18 Macroprocessor
Figure 3.3 represents above steps in the pictorial representation using flow chart.

One-pass
macro Is
processor MACRo Write into expanded
Psuedo opcode source card ?le
found ?
Yes,
MDTC – 1 macro de?nition Is
MNTC – 1 Macro END
MDI – ‘OFF’ READ * x
MDLC – 0 name Pseudo opcode
line found ?

Enter macro name


and current value
READ * x Supply expanded
of MDTC in MNT
source ?le to
entry number MNTC
assembler
processing
Search MNT for MNTC – MNTC + 1
match with
opcode
Prepare macro
de?nition
NO argument list array
Is Macro
name
found ? Enter macro name
card into MDT
Yes, macro call

MDI – ‘ON’ MDTC – MDTC + 1

MDTP – MDT index MDLC – MDLC + 1


from MNT entry

READ*
Prepare ALA

Substitute index
notation for arguments
in de?nition

Enter line into MDT

MDTC – MDTC + 1

Yes
MDLC – MDLC + 1

No Yes
MDLC – MDLC – 1

No
x MDLC – 0
?

Figure 3.3: Single Pass


Macroprocessor 3-19
• In the example below, the body of the first Macro (MACROS) contains statement
that define RDBUFF, WRBUFF and other macro instructions for SIC machine.
• The body of the second Macro (MACROX) defines these same macros for SIC/
XE machine.
• A proper invocation would make the same program to perform macro invocation
to run on either SIC or SIC/XEmachine.
MACROS for SIC machine

1 MACROS MACRO {De?nes SIC standard version macros}


2 RDBUFF MACRO &INDEV, &BUFADR, &RECLTH
.
. {SIC standard version}
.
3 MEND {End of RDBUFF}
4 WRBUFF MACRO &OUTDEV, &BUFADR, &RECLTH
.
. {SIC standard version}
5 MEND {End of WRBUFF}
.
.
.
6 MEND {End of MACROS}

Figure: 3.4(a)
MACROX for SIC/XE Machine
1 MACROX MACRO {De?nes SIC/XE macros}
2 RDBUFF MACRO &INDEV, &BUFADR, &RECLTH
.
. {SIC/XE version}
.
3 MEND {End of RDBUFF}
4 WRBUFF MACRO &OUTDEV, &BUFADR, &RECLTH
.
. {SIC/XE version}
5 MEND {End of WRBUFF}
.
.
.
6 MEND {End of MACROX}

Figure 3.4(b)
3-20 Macroprocessor
• A program that is to be run on SIC system could invoke MACROS whereas a program to
be run on SIC/XE can invoke MACROX.
• However, defining MACROS or MACROX does not define RDBUFF and WRBUFF.
• These definitions are processed only when an invocation of MACROS or MACROX is
expanded.
One-Pass Macro Processor:
• A one-pass macro processor that alternate between macro definition and macro expansion
in a recursive way is able to handle recursive macro definition.
• Restriction
– The definition of a macro must appear in the source program before any statements
that invoke that macro.
– This restriction does not create any real inconvenience.
The design considered is for one-pass assembler. The data structures required are:
• DEFTAB (Definition Table)
– Stores the macro definition including macro prototype and macro body
– Comment lines are omitted.
– References to the macro instruction parameters are converted to a positional notation
for efficiency in substituting arguments.
• NAMTAB (Name Table)
– Stores macro names
– Serves as an index to DEFTAB
• Pointers to the beginning and the end of the macro definition (DEFTAB)
• ARGTAB (Argument Table)
– Stores the arguments according to their positions in the argument list.
– As the macro is expanded the arguments from the Argument table are substituted for
the corresponding parameters in the macro body.
– The figure below shows the different data structures described and their relationship.
The above figure shows the portion of the contents of the table during the processing of the
program. In fig 3.4(a) definition of RDBUFF is stored in DEFTAB, with an entry in NAMTAB
having the pointers to the beginning and the end of the definition. The arguments referred by the
instructions are denoted by the their positional notations. For example,
TD = X'?1'
The above instruction is to test the availability of the device whose number is given by the
parameter &INDEV. In the instruction this is replaced by its positional value? 1. Figure 3.4(b)
shows the ARTAB as it would appear during expansion of the RDBUFF statement as given below:
CLOOP RDBUFF F1, BUFFER, LENGTH
Macroprocessor 3-21
NAMTAB DEFTAB

RDBUFF

ARGTAB
1 F1
2 BUFFER
3 LENGTH

(a)

(b)
Figure: 3.5
For the invocation of the macro RDBUFF, the first parameter is F1 (input device code), second
is BUFFER (indicating the address where the characters read are stored), and the third is LENGTH
(which indicates total length of the record to be read). When the ?n notation is encountered in a line
from DEFTAB, a simple indexing operation supplies the proper argument from ARGTAB.
The algorithm of the Macro processor is given below. This has the procedure DEFINE to
make the entry of macro name in the NAMTAB, Macro Prototype in DEFTAB. EXPAND is called
to set up the argument values in ARGTAB and expand a Macro Invocation statement. Procedure
GETLINE is called to get the next line to be processed either from the DEFTAB or from the file
itself.
When a macro definition is encountered it is entered in the DEFTAB. The normal approach is
to continue entering till MEND is encountered. If there is a program having a Macro defined within
another Macro. While defining in the DEFTAB the very first MEND is taken as the end of the
Macro definition. This does not complete the definition as there is another outer Macro which
completes the definition of Macro as a whole. Therefore the DEFINE procedure keeps a counter
variable LEVEL. Every time a Macro directive is encountered this counter is incremented by 1.
The moment the innermost Macro ends indicated by the directive MEND it starts decreasing the
value of the counter variable by one. The last MEND should make the counter value set to zero. So
when LEVEL becomes zero, the MEND corresponds to the original MACRO directive.
Most macro processors allow the definitions of the commonly used instructions to appear in a
standard system library, rather than in the source program. This makes the use of macros convenient;
definitions are retrieved from the library as they are needed during macro processing.
3-22 Macroprocessor

Procedure GETLINE
If EXPANDING then
get the next line to be processed from DEFTAB
Else

read next line from input ?le


MAIN program
Iterations of
GETLINE
PROCESSLINE Procedure PROCESSLINE
DEFINE
EXPAND
Ooutput source line

Procedure EXPAND
Procedure DEFINE
Set up the argument values in ARGTAB
Make appropriate entries in
Expand a macro invocation statement (like in
DEFTAB and NAMTAB
MAIN procedure)
Iterations of
GETLINE
PROCESSLINE

Figure: 3.6
Algorithms
begin {macro processor}
EXPANDINF : = FALSE
while OPCODE = ‘END’ do
begin
GETLINE
PROCESSLINE
end {While}
end {macro processor}
Procedure PROCESSLINE
begin
sarch MAMTAB for OPCODE
if found then
EXPAND
else if OPCODE = ‘MACRO’ then
DEFINE
else write soruce line to expanded file
end {PRCOESSOR}
Macroprocessor 3-23
Procedure DEFINE
begin
enter macro name into NAMTAB
enter macro prototype into DEFTAB
LEVEL :- 1
While LEVEL > do
begin
GETLINE
if this is not a comment line then
begin
substitute positional notation for parameters
enter line into DEFTAB
if OPCODE = ‘MACRO’ then
LEVEL := LEVEL + 1
else if OPCODE = ‘MEND’ then
LEVEL := LEVEL – 1
end {if not comment}
end {while}
store in NAMTAB Pointers to beginning and end of definition

Procedure EXPAND
begin
EXPANDING := TRUE
get first line of macro definition {prototype} from DEFTAB
set up arguments from macro invocation in ARGTAB
while macro invocation to expanded file as a comment
while not end of macro definition do
begin
GETLINE
PROCESSLINE
end {while}
EXPANDING : = FALSE
end {EXPAND}
Procedure GETLINE
begin
if EXPANDING then
3-24 Macroprocessor
begin
get next line of macro definition from DEFTAB
substitute arguments from ARGTAB for positional notation
end {if}
else
read next line from input file
end {GETLINE}
Figure: 3.7

3.7.1 Comparison of Macro Processor Design


• One-pass algorithm
– Every macro must be defined before it is called
– One-pass processor can alternate between macro definition and macro expansion
– Nested macro definitions are allowed but nested calls are not allowed.
• Two-pass algorithm
– Pass1: Recognize macro definitions
– Pass2: Recognize macro calls
– Nested macro definitions are not allowed

3.8 Machine-independent Macro-Processor Features


The design of macro processor doesn't depend on the architecture of the machine. We will be
studying some extended feature for this macro processor. These features are:
• Concatenation of Macro Parameters
• Generation of unique labels
• Conditional Macro Expansion
• Keyword Macro Parameters

3.8.1 Concatenation of unique labels


Most macro processor allows parameters to be concatenated with other character strings.
Suppose that a program contains a series of variables named by the symbols XA1, XA2, XA3,…,
another series of variables named XB1, XB2, XB3,…, etc. If similar processing is to be performed
on each series of labels, the programmer might put this as a macro instruction. The parameter to
such a macro instruction could specify the series of variables to be operated on (A, B, etc.). The
macro processor would use this parameter to construct the symbols required in the macro expansion
(XA1, Xb1, etc.).
Suppose that the parameter to such a macro instruction is named &ID. The body of the macro
definition might contain a statement like.
Macroprocessor 3-25
LDA X&ID1

TOTAL MACRO &ID


LAD X&ID1 LAD XA1
ADD X&ID2 TOTAL A LAD XA2
STA X&ID3 STA XA3
MEND X&ID3
Figure: 3.8
& is the starting character of the macro instruction; but the end of the parameter is not marked.
So in the case of &ID1, the macro processor could deduce the meaning that was intended. If the
macro definition contains contain &ID and &ID1 as parameters, the situation would be unavoidably
ambiguous.
Most of the macro processors deal with this problem by providing a special concatenation
operator. In the SIC macro language, this operator is the character. Thus the statement
LDA X&ID1 can be written as
LDA X&ID

ID123 MACRO &ID


LAD X&ID1
ADD X&ID2
STA X&ID3
MEND
1 SUM MACRO &ID
2 LDA X&ID1
3 ADD X&ID2
4 ADD X&ID3
5 STA X&IDS
6 MEND

SUM A SUM BETA


 
LAD XA1 LDA XBEATA1
ADD XA2 ADD XBEATA2
ADD XA3 ADD XBEATA3
STA XAS STA XBEATAS

Figure:3.9
The above figure shows a macro definition that uses the concatenation operator as previously
described. The statement SUM A and SUM BETA shows the invocation statements and the
corresponding macro expansion.
3-26 Macroprocessor
3.8.2 Generation of Unique Labels
As discussed it is not possible to use labels for the instructions in the macro definition, since
every expansion of macro would include the label repeatedly which is not allowed by the assembler.
This in turn forces us to use relative addressing in the jump instructions. Instead we can use the
technique of generating unique labels for every macro invocation and expansion. During macro
expansion each $ will be replaced with $XX, where xx is a two-character alphanumeric counter of
the number of macro instructions expansion.
For example,
XX = AA, AB, AC…
This allows 1296 macro expansions in a single program.
The following program shows the macro definition with labels to the instruction.
25 RDBUFF MACRO &INDEV, &BUFADR, &RECLTH
30 CLEAR X
35 CLEAR A
40 CLEAR S
45 +LDT #4096 SET MAXIMUM RECORD LENGTH
50 $LOOP TD =X’&INDEV’ TEST INPUT DEVICE
55 JED $LOOP LOOP UNTIL READY
60 RD =X’&INDEV READ CHARACTER INTI REG A
65 COMPR A, S TEST FOR END OF RECORD
70 JEQ $EXIT EXIT LOOP IF EOR
75 STCH &BUFADR, X STORE CHARACTER IN BUFFER
80 TIXR $LOOP HAS BEEN REACHED
90 $EXIT STX &RECLTH SAVE RECORD LENGTH
MEND
The following figure shows the macro invocation and expansion first time.
RDBUFF F1, BUFFER, LENGTH
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 CLEAR S
50 +LDT #4096 SET MAXIMUM RECORD LENGTH
55 $AALOOP TD =X’F1’ TEST INPUT DEVICE
60 JED %AALOOP LOOP UNTIL READY
65 RD =X’F1 READ CHARACTER INTI REG A
70 COMPR A, S TEST FOR END OF RECORD
70 JEQ $AAEXIT EXIT LOOP IF EOR
75 STCH &BUFFER, X STORE CHARACTER IN BUFFER
85 TIXR LOOP HAS BEEN REACHED
90 $AAEXIT STX LENGTH SAVE RECORD LENGTH
If the macro is invoked second time the labels may be expanded as $ABLOOP $ABEXIT.
Macroprocessor 3-27
3.8.3 Conditional Macro Expansion
There are applications of macro processors that are not related to assemblers or assembler
programming.
Conditional assembly depends on parameters provides
MACRO &COND
……..
IF (&COND NE ‘’)
part I
ELSE
part II
ENDIF
………
ENDM
Part I is expanded if condition part is true, otherwise part II is expanded. Compare operators:
NE, EQ, LE, GT.
Macro-Time Variables : Macro-time variables (often called as SET Symbol) can be used
to store working values during the macro expansion. Any symbol that begins with symbol & and
not a macro instruction parameter is considered as macro-time variable. All such variables are
initialized to zero.
25 RDBUFF MACRO &INDEV, &BUFADR, &RECLTH, &FOR. &MAXI TH
26 IF (&FOR NE ‘ ’)
27 &EORCK SET 1
28 ENDIF
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
38 IF (&, EORCK EQ 1)
40 Macro-time LDCH =X’&EOR’ SET EOR COUNTER
42 Variable RMO A,S
43 ENDIF
44 IF (&MAXLTH EQ ‘ ’)
45 +LDT #4096 SET MAX LENGTH = 4096
46 ELSE
47 +LDT #&MAXLTH SET MAXIMUM RECORD LENGTH
48 ENDIF
50 $ LOOP TD =X’SINDEV’ TEST INPUT DEVICE
55 JEQ $ LOOP LOOP UNTIL READY
60 RD = X’ SINDEV’ READ CHARACTER INTI REG A
63 IF (& FORCK EQ 1)
65 COMRR A, S REST FOR END OF RECORD
70 JEQ $ EXIT EXIT LOOP IF EOR
73 ENDIF
75 STCH & BUFADR, X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUN LENGTH
85 JLT $ LOOP HAS BEEN REACHED
90 $EXIT STX &RECLTH SAVE RECORD LENGTH
95 MEND
Figure: 3.9 (a)
3-28 Macroprocessor
Figure 3.5(a) gives the definition of the macro RDBUFF with the parameters &INDEV,
&BUFADR, &RECLTH, &EOR, &MAXLTH. According to the above program if &EOR has any
value, then &EORCK is set to 1 by using the directive SET, otherwise it retains its default value 0.

RDBUFF F31 BUF, RECL, 04, 2048


30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 LDCH =X’04 SET EOR CHARACTER
42 RMO A,S
47 +LDT #2048 SET MAXIMUM RECORD LENGTH
50 $ AALOOP TD =X’F3’ TEST INPUT DEVICE
55 JEQ $AALOOP LOOP UNTIL READY
60 RD =X’F3’ READ CHARACTER INTI REG A
65 COMPR A,S TEST FOR END OF RECORD
70 JEQ $AAEXIT EXIT LOOP IF EOR
75 STCH BUF, X STORE CHARACTE IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $AALOOP HAS BEEN REACHED
90 $AAEXIT STX RECL SAVE RECORD LENGTH

Fig 3.9(b) Use of Macro-Time Variable with EOF being NOT NULL

RDBUFF OE, BUFFER, LENGTH, , 80


30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
47 +LDT #80 SET MAXIMUM RECORD LENGTH
50 $ABLOOP TD =X’OE’ TEST INPUT DEVICE
55 JEQ $ABLOOP LOOP UNTIL READY
60 RD =X’OE’ READ CHARACTER IN BUFFER
75 STCH BUFFER, X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
87 JLT $ABLOOP HAS BEEN REACHED
90 $ABEXIT STX LENGTH SAVE RECORD LENGTH

Fig 3.9(c) Use of Macro-Time conditional statement with EOF being NULL
Macroprocessor 3-29

RDBUFF F1. BUFF, ELENG, 04


30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 LDCH =X’04’ SET EOR CHARACTER
42 RMO A, S
45 +LDT #4096 SET MAX LENGTH = 4096
50 $ACLOOP TD =X’F1’ TEST INPUT DEVICE
55 JEQ $ACLOOP LOOP UNTIL READY
60 RD =X’F1 READ CHARACTER INTI REG A
65 COMPR A.S TEST FOR END OF RECORD
70 JEQ $ACEXIT EXIT LOOP IF EOR
75 STCH BUFF,X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $ACLOOP HAS LOOP REACHED
$ACEXIT STX RLENG SAVE RECORD LENGTH
Fig 3.9(d) Use of Time-variable with EOF NOT NULL and MAXLENGTH being NULL
The above programs show the expansion of Macro invocation statements with different values
for the time variables. In figure 4.9(b) the &EOF value is NULL. When the macro invocation is
done, IF statement is executed, if it is true EORCK is set to 1, otherwise normal execution of the
other part of the program is continued.
The macro processor must maintain a symbol table that contains the value of all macro time
variables used. Entries in this table are modified when SET statements are processed. The table is
used to look up the current value of the macro-time variable whenever it is required.
When an IF statement is encountered during the expansion of a macro, the specified Boolean
expression is evaluated.
If the value of this expression TRUE,
• The macro processor continues to process lines from the DEFTAB until it encounters the
ELSE or ENDIF statement.
• If an ELSE is found, macro processor skips lines in DEFTAB until the next ENDIF.
• Once it reaches ENDIF, it resumes expanding the macro in the usual way.
If the value of the expression is FALSE,
• The macro processor skips ahead in DEFTAB until it encounters next ELSE or ENDIF
statement.
• The macro processor then resumes normal macro expansion.
The macro-time IF-ELSE-ENDIF structure provides a mechanism for either generating (once)
or skipping selected statements in the macro body. There is another construct WHILE statement
which specifies that the following line until the next ENDW statement, are to be generated repeatedly
3-30 Macroprocessor
as long as a particular condition is true. The testing of this condition, and the looping are done
during the macro is under expansion. The example shown below shows the usage of Macro-Time
Looping statement.
WHILE-ENDW structure
• When an WHILE statement is encountered during the expansion of a macro, the specified
Boolean expression is evaluated.
• TRUE
– The macro processor continues to process lines from DEFTAB until it encounters the next
ENDW statement.
– When ENDW is encountered, the macro processor returns to the preceding WHILE, re-
evaluates the Boolean expression, and takes action based on the new value.
• FALSE
– The macro processor skips ahead in DEFTAB until it finds the next ENDW statement and
then resumes normal macro expansion.

RDBUFF OE, BUFFER, LENGTH, , 80

25 RDBUFF MACRO &INDEV, &BUFADR, &RECLTH, &EOR


27 &EORCT SET %NITEMS (&EOR) Macro Processor function
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
45 +LDT #4096 SET MAX LENGTH = 4096
50 $LOOP TD = X’ &INDEV’ test INPUT DEVICE
55 JED $LOOP LOOP UNIT READY
60 RD =X’SINDEV’ READ CHARACTER INTO REG A
63 &CTR SET 1
64 WHILE (&CTR LE &EORCT)
65 COMPR =X’-0000& EOR [&CTR]’ List Index
70 JEQ &CTR+1
73 ENDW
75 STCH &BUFADR, X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $LOOP HAS BEEN REACHED
90 $EXIT STX &RECLTH SAVE RECORTH LENGTH
100 MEND
Macroprocessor 3-31

RDBUFF F2, BUFFER, LENGTH, 00,03, 04


List
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
45 +LDT #4096
50 $AALOOP TD =X’F2’ TEST INPUT DEVICE
55 JEQ $AALOOP LOOP UNTIL READY
60 RD =X’F2’ READ CHARACTER INTO REG A
65 COMP =X’0000000’
70 JEQ $AAEXIT
65 COMP =X’0000003’
70 JEQ $AAEXIT
65 COMP =X’000004’
70 JEQ $AAEXIT
75 STCH BUFFER, X STORE CHARACTER IN BUFFER
80 TIXT T LOOP UNLESS MAXIMUM LENGTH
85 JLT $AALOOP HAS BEEN REACHED
90 $AAEXIT STX LENGTH SAVE RECORD LENGTH

3.8.4 Keyword Macro Parameters


All the macro instruction definitions used positional parameters. Parameters and arguments
are matched according to their positions in the macro prototype and the macro invocation statement.
The programmer needs to be careful while specifying the arguments. If an argument is to be omitted
the macro invocation statement must contain a null argument mentioned with two commas.
Positional parameters are suitable for the macro invocation. But if the macro invocation has
large number of parameters, and if only few of the values need to be used in a typical invocation, a
different type of parameter specification is required (for example, in many cases most of the
parameters may have default values, and the invocation may mention only the changes from the
default values).
Ex: XXX MACRO &P1, &P2, …., &P20, ….
XXX A1, A2,,,,,,,,,,…,,A20,…..
Null arguments
Keyword parameters
• Each argument value is written with a keyword that names the corresponding parameter.
• Arguments may appear in any order.
• Null arguments no longer need to be used.
• Ex: XXX P1=A1, P2=A2, P20=A20.
• It is easier to read and much less error-prone than the positional method.
3-32 Macroprocessor

25 RDBUFF MACRO &UNDEV=F1, &BUFADR=, &RECLTH=, &EOR = 04,


&MAXLTH = 4096
26 IF (&EOR NE ‘ ’)
27 &EORCK SET 1
28 ENDIF Parameters with default value
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
38 IF (&,EORCK EQ 1)
40 LDCH =X’&EOR’ SET EOR CHARACTER
42 RMO A, S
43 ENDIF
47 +LDT #MAXLETH SET MAXIMUM RECORD LENGTH
50 $LOOP TD =X’&INDEV’ TEST INPUT DEVICE
55 JEQ $LOOP LOOP UNTIL READY
60 RD =X’&INDEV’ READ CHARACTER INTI REG A
63 IF (&EORCK EQ 1)
65 COMPR A, S TEST FOR END OF RECORD
70 JEQ $EXIT EXIT LOOP IF EOR
73 ENDIF
75 STCH $BUFADR,X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $LOOP HAS BEEN REACHED
90 $EXIT STX &RECLTH SAVE RECORD LENGHT
95 MEND

RDBUFF BUFFER=BUFFER, RECHTH-LENGTH

30 CLEAR X CLEAR LOOP COUNTER


35 CLEAR A
40 LDCH =X’04 SET EOR CHARACTER
42 RMO A,S
47 +LDT #4096 SET MAXIMUM RECORD LENGTH
50 $AALOOP TD =X’F1’ TEST INPUT DEVICE
55 JEQ $AALOOP LOOP UNTIL READY
60 RD =X’F1 READ CHARACTER INTI REG A
65 COMPR A, S TEST FOR END OF RECORD
70 JEQ $AAEXIT EXIT LOOP IF EOR
75 STCH BUFFER,X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $AALOOP HAS BEEN REACHED
90 $AAEXUT STX LENGTH SAVE RECORD LENGTH
Macroprocessor 3-33

1. RDBUFF RECLTH = LENGTH, BUFADR= BUFFER, EOR = INDEV = F3

30 CLEAR X CLEAR LOOP COUNTER


35 CLEAR A
47 +LDT #4096 SET MAXIMUM RECORD LENGTH
50 $AALOOP TD =X’F3’ TEST INPUT DEVICE
55 JEQ $AALOOP LOOP UNTIL READY
60 RD =X’F3’ READ CHARACTER INTI REG A
75 STCH BUFFER,X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $AALOOP HAS BEEN REACHED
90 $AAEXUT STX LENGTH SAVE RECORD LENGTH

Fig 3.10 Example showing the usage of Keyword Parameter

3.9 Macro Processor Design Options

3.9.1 Recursive Macro Expansion


We have seen an example of the definition of one macro instruction by another. But we have
not dealt with the invocation of one macro by another. The following example shows the invocation
of one macro by another macro.

10 RDBUFF MACRO &BUFADR, &RECLTH, &INDEV


15 -
20 - MACRO TO READ RECORD INTO BUFFER
25 -
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 CLEAR S
45 +LDT #4096 SET MAXIMUM RECORD LENGTH
50 $LOOP RDCHAR &INDEV READ CHARACTER INTO REG A
65 COMPR A,S TEST FOR END OF RECORD
70 JEQ &EXIT EXIT LOOP IF EOR
75 STCH BUFFER,X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $LOOP HAS BEEN REACHED
90 $EXUT STX &RECLTH SAVE RECORD LENGTH
95 MEND
3-34 Macroprocessor

5 RDCHAR MACRO &IN


10 -
15 - MACROTO READ CHARACTER INTO REGISTER A
20 -
25 TD =X’&IN TEST INPUT DEVICE
30 JEQ *–3 LOOP UNTIL READY
35 RD =X’&IN’ READ CHARACTER
40 MEND
Problem of Recursive Expansion
• Previous macro processor design cannot handle such kind of recursive macro invocation and
expansion
– The procedure EXPAND would be called recursively, thus the invocation arguments in the
ARGTAB will be overwritten.
– The Boolean variable EXPANDING would be set to FALSE when the "inner" macro
expansion is finished, i.e., the macro process would forget that it had been in the middle of
expanding an "outer" macro.
• Solutions
– Write the macro processor in a programming language that allows recursive calls, thus
local variables will be retained.
– If you are writing in a language without recursion support, use a stack to take care of
pushing and popping local variables and return addresses.
The procedure EXPAND would be called when the macro was recognized. The arguments
from the macro invocation would be entered into ARGTAB as follows:
Parameter Value
1 BUFFER
2 LENGTH
3 F1
4 (unused)
- -
The Boolean variable EXPANDING would be set to TRUE, and expansion of the macro
invocation statement would begin. The processing would proceed normally until statement invoking
RDCHAR is processed. This time, ARGTAB would look like
Parameter Value
1 F1
2 (unused)
- -
Macroprocessor 3-35
At the expansion, when the end of RDCHAR is recognized, EXPANDING would be set to
FALSE. Thus the macro processor would 'forget' that it had been in the middle of expanding a
macro when it encountered the RDCHAR statement. In addition, the arguments from the original
macro invocation (RDBUFF) would be lost because the value in ARGTAB was overwritten with
the arguments from the invocation of RDCHAR.

3.9.2 General-Purpose Macro Processors


• Macro processors that do not dependent on any particular programming language, but can be
used with a variety of different languages
• Pros
– Programmers do not need to learn many macro languages.
– Although its development costs are somewhat greater than those for a language specific
macro processor, this expense does not need to be repeated for each language, thus save
substantial overall cost.
• Cons
– Large number of details must be dealt with in a real programming language
• Situations in which normal macro parameter substitution should not occur, e.g., comments.
• Facilities for grouping together terms, expressions, or statements
• Tokens, e.g., identifiers, constants, operators, keywords
• Syntax had better be consistent with the source programming language

3.9.3 Macro Processing within Language Translators


• The macro processors we discussed are called "Pre-processors".
– Process macro definitions
– Expand macro invocations
– Produce an expanded version of the source program, which is then used as input to an
assembler or compiler
• You may also combine the macro processing functions with the language translator:
– Line-by-line macro processor
– Integrated macro processor

3.9.4 Line-by-Line Macro Processor


• Used as a sort of input routine for the assembler or compiler
– Read source program
– Process macro definitions and expand macro invocations
– Pass output lines to the assembler or compiler
• Benefits
– Avoid making an extra pass over the source program.
3-36 Macroprocessor
– Data structures required by the macro processor and the language translator can be combined
(e.g., OPTAB and NAMTAB)
– Utility subroutines can be used by both macro processor and the language translator.
• Scanning input lines
• Searching tables
• Data format conversion
– It is easier to give diagnostic messages related to the source statements
3.9.5 Integrated Macro Processor
• An integrated macro processor can potentially make use of any information about the source
program that is extracted by the language translator.
– Ex (blanks are not significant in FORTRAN)
• DO 100 I = 1,20
– a DO statement
• DO 100 I = 1
– An assignment statement
– DO100I: variable (blanks are not significant in FORTRAN)
• An integrated macro processor can support macro instructions that depend upon the context in
which they occur.

Solved Question Answers


Q.1. What is the need of macro?
Ans. A macro instruction or macros are single line abbreviations for group of instructions. Macros
are special code fragments that are defined once in the program and are used repetitively by
calling them from various places within the program.
In the assembly language program the programmer often needs to use some block of statements
again and again in the program. So we need to repeat the whole code a number of times in the
program which increases complexity to reduce this complexity we need macros.
Q.2. What is input and output of a macro processor? How is it dependent on the assembler
source code format?
Ans. A macro processor is a program that copies a stream of text from one place to another, making
a systematic set of replacements as it does so. Macro processors are often embedded in other
programs, such as assemblers and compilers. Sometimes they are standalone programs that
can be used to process any kind of text.
Q.3. What is an interrupt?
Ans. An interrupt is a signal to the processor emitted by hardware or software indicating an event
that needs immediate attention. An interrupt alerts the processor to a high-priority condition
requiring the interruption of the current code the processor is executing. The processor responds
by suspending its current activities, saving its state, and executing a function called an interrupt
handler to deal with the event. This interruption is temporary, and, after the interrupt handler
Macroprocessor 3-37
finishes, the processor resumes normal activities. There are two types of interrupts: hardware
interrupts and software interrupts.
Q.4. What is instruction counter?
Ans. A program counter is a register in a computer processor that contains the address (location)
of the instruction being executed at the current time. As each instruction gets fetched, the
program counter increases its stored value by 1. After each instruction is fetched, the program
counter points to the next instruction in the sequence. When the computer restarts or is reset,
the program counter normally reverts to 0.
Q.5. What is the use of Macroinstructions?
Ans. A macro instruction is a group of programming instructions that have been compressed into
a simpler form and appear as a single instruction. When used, a macro expands from its
compressed form into its actual instruction details. Both the name of the macro definition
and other variable parameter attributes are included within the macro statement. Macros
save developers much time and effort, especially when dealing with a certain sequence of
commands that is repeated more than once within the program body. Macros also save space
and spare the programmer time spent on a long code block that may pertain to performing a
single function.
Q6. What are dummy arguments?
Ans. In general (not in Fortran) a dummy argument is an argument that will not be used by the
body of the function.
For example, in the following function, z is a dummy argument but x and y are not.
int f(int x, int y, int z) {
return (x + y);
}
Q.7. What is the use of LTORG operation?
Ans. The Literal Pool contains a collection of anonymous constant definitions, which are generated
by the assembler. The LTORG directive defines the start of a literal pool.
While some textbooks may imply that the LTORG directive is not necessary for use of literals,
your instructor's experience is different. It appears that an explicit LTORG directive is required
if the program uses literal arguments.
The classic form of the statement is as follows, where the "L" of "LTORG" is to be found in
column 10 of the listing.
Generally, this statement should be placed near the end of the listing, as in the next example
taken from an actual program.
240 * LITERAL POOL
241 *********************************
000308 242 LTORG *
000308 00000001 243 =F'1'
000000 244 END LAB1
3-38 Macroprocessor
Here, line 243 shows a literal that is inserted by the assembler. Note that lines 242 and 243
are listed at the same address (hexadecimal 000308). The directive generates no code.
Q.8. What is page fault?
Ans. An interrupt that occurs when a program requests data that is not currently in real memory.
The interrupt triggers the operating system to fetch the data from a virtual memory and load
it into RAM.
An invalid page fault or page fault error occurs when the operating system cannot find the
data in virtual memory. This usually happens when the virtual memory area, or the table that
maps virtual addresses to real addresses, becomes corrupt.
Q.9. What are Subroutines?
Ans. In computer programming, a subroutine is a sequence of program instructions that perform a
specific task, packaged as a unit. This unit can then be used in programs wherever that particular
task should be performed. Subprograms may be defined within programs, or separately in
libraries that can be used by multiple programs. In different programming languages, a
subroutine may be called a procedure, a function, a routine, a method or a subprogram. The
generic term callable unit is sometimes used.
Q.10. Is it true that call leads to control to control to macro definition?
Ans. Yes, macro calls lead to control to macro definition. This is because when Macro call is
implemented in assembly program, macro definition is substituted in place of macro call.
Macro definition cannot be placed without placing macro call in the assembly program.
Q.11. What are the various format of databases required while designing a two pass macro
processor?
Ans. Macro Definition Table 80 bytes per entry
Index Card
·
·
·
15 &LAB INCR & arg1, & arg2, & arg3
16 #0 A 1, # 1
17 A 2, # 2
18 A 3, # 3
19 MEND
· ·
· ·
· ·
1. MDT is a table of text lines.
2. Every line of each macro definition is except the MACRO line ,is stored in the MDT .
3. MEND is kept to indicate the end of the program.
4. The macro-name line is retained to facilitate keyword argument replacement
Macroprocessor 3-39

Macro Name Table


Index 8bytes 4bytes
Name MDT index
3 “INCR” 15
· ·
· ·
· ·
Q.12. Macro expansion is very similar to subroutine calls during program execution. Explain
the similarities and difference between them?
Ans. Difference:
1. a macro call is an instruction to replace the macro name with its body, whereas subroutine
call is an instruction to transfer the program's control to the subroutine's definition with
all paraneters, if required.
2. A macro call results in macro expansion, whereas subroutine call results in execution.
3. Macro expansion increases the size of the program but subroutine execution doesn't
affect the size of the program.
4. Macro expansion doesn't affect the execution speed of the program much in comparison
to subroutines affect the execution speed of the program.
5. Macro doesn't have any return statement...but a subroutine can have execution time needed
for a macro is much lesser than subroutine memory requirement for a macro is generally
higher.
6. Subroutine can be classified as stated in previous answers. But no such classification is
available with macro although macro can have different types of nested form generally
no of instructions in macro are smaller than subroutine macro is always local to the
program that defines it.
7. Subroutine may or may not be local..
Q.13. Differentiate between: impure procedure/pure procedure, close subroutine/open
subroutine.
Ans. Impure procedure/pure procedure:-Procedures that modify themselves are called impure
procedure. Writing such procedures is poor programming practice. Other programmers find
them difficult to read, and moreover they cannot be shared by multiple processors, Each
processors executing an impure procedure modifies its contents. Another processor attempting
to execute the same procedure may encounter different instructions or data. Thus, impure
procedures are not readily reusable. A pure procedure does not modify itself. To ensure that
the instructions are the same each time a program is used, pure procedure is employed.
Close subroutine/Open subroutine:-In closed subroutine a subroutine stored outside the main
routine can be connected to it by linkages at one or more locations. Where as in open subroutine
is a set of computer instructions i.e. a subroutine that performs some particular program and
insert them directly each and every time that particular function is required.
3-40 Macroprocessor
Q.13. Explain the two pass macro-processor with help of flowchart?
Ans.
Pass 2

Pass 2 – Processing macro calls and ex


Read next source
card (copled by
pass 1)

Search MNT for match


with Operation code

Write into
NO
MACRO name expanded
found source
card ?le

YES END
NO
pseudo
MDTP = MDT index Opcode
from MNT entry

YES
Set up argument list
array Supply expanded
source ?le to
assembler
processing
MDTP = MDTP + 1

Get line from MDT

Substitute arguments
from macro call

MEND Write
pseudo expanded
opcode source card
YES NO
Macroprocessor 3-41
Q.15. What is the need for a macro?
Ans. By running a macro, users are able to trim down time normally consumed by repetitive tasks.
Some macros, like the ones in MS Excel, may also contain functions. Macros are used to
make a sequence of computing instructions available to the programmer as a single program
statement, making the programming task less tedious and less error-prone. (Thus, they are
called "macros" because a big block of code can be expanded from a small sequence of
characters.) Macros often allow positional or keyword parameters that dictate what the
conditional assembler program generates and have been used to create entire programs or
program suites according to such variables as operating system, platform or other factors.
The term derives from "macro instruction", and such expansions were originally used in
generating assembly language code. A stored macro can then be accessed from a menu list or
from the toolbar and run by simply clicking. You can also assign a hotkey to the macro for
even faster access. Since macros can be called automatically as soon as a digital document is
loaded, they have been employed by malicious individuals for creating macro viruses.
Q.16. What are Macro instruction arguments? Explain.
Ans. Macros provides a facility for specifying arguments or parameters in the macro calls. For all
the parameters passed in macro call, there should be dummy arguments in macro definition.
In macro prototype, these arguments are proceeded by an ampersand (&) sign.
Types of parameters or arguments:
1. Positional arguments : These are the parameters that are matched with dummy arguments
in macro prototype according to their position.
2. Keyword arguments : These are the parameters in which arguments in macro call are
matched with dummy arguments by the name as well as the position.
3. Default arguments : The parameters have default value in macro definition are called
Default parameters .These default parameters get their default value if its name does not
appear in macro call.
Q.17. With the help of an example, distinguish between a function and a macro.
Ans. The function is a program's source code that contains set of instruction units that are entitled
to do a specific job, whereas a macro is an open subroutine that replaces given text when a
macro definition is being called. The functions are mainly used in modular programming
whereas macro are found to be used in non-modular programming. The differences between
function and macros are as follows:-

Function Macro
1. The function is a program or source code 1. The macro is an open subroutine that
that contains set of instruction units that replaces given text when a macro
are entitled to do a specific job. definition being called for further
processing.
2. The function calls are made during run 2. The macro calls are made during
time state of a program. assembly process.
3-42 Macroprocessor

Function Macro
3. In case of function, when they are called, 3. In case of macros, the control is not
the control transfers to the definition of transferred; instead the macro definition
the function and after end of the function, replaces that call of macro in the
it returns back to main program. program.
4. The function requires stack to maintain 4. The macro doesn't require any stock,
call and return of function. since there is no call and returns in
macros.
5. The overhead are move in functions due 5. The overhead in case of macros are
to number of calls and returns. comparatively less as compared to
function.
6. The functions are also known as 6. Te macros are known only as open
procedures, subroutines and module. subroutines.
7. The functions are basically emphasized 7. The macros are not entitled to do so.
on called and returned function.
8. The functions are providing only 8. The macro represents commonly used
modular approach, were the problem can group of statements and used only for
be subdivided. smaller programs.
9. main() 9. main()
{ {
........................ ........................
........................ ........................
........................ ........................
function(); macro();
} }
function() macro()
{ {
........................ ........................
........................ ........................
........................ ........................
Print } ("macro"); print("macro");
}
Macroprocessor 3-43
Q.18. What are subroutine reference or extern pseudo-ops?
Ans. SUBROUTINE REFERENCE (EXTRN PSEUDO-OP)
Assembler symbols are either internal or external means that their value is not known to the
assembler but will be provided by the loader, the action of the loader will be discussed in the
following section.
EXTERN E1, E2, etc.
Defines E1, E2, etc. As external symbols to be used in address constants.
Example CALL BETA becomes:
EXTRN BETA
:
:
:
L 15, ABETA
BALP 14, 15
:
:
:
A BETA DC A(BETA)
Q.19. What do you meant by macro instructions?
Ans. The “macro” is an abbreviation for a sequence of operations. Consider the following program:
Example:-
A 1, DATA Add contents of DATA to register 1
A 2, DATA Add contents of DATA to register 2
A 3, DATA Add contents of DATA to register 3
:
A 1, DATA Add contents of DATA to register 1
A 2, DATA Add contents of DATA to register 2
A 3, DATA Add contents of DATA to register 3
:
DATA DC F'5'
:
In the above program the sequence occurs twice.
A 1, DATA
A 2, DATA
A 3, DATA
3-44 Macroprocessor
A macro facility allows us to attach a name to this sequence and to use this name in its place.
We can invent a macro language that allows us to specify the above as a macro definition and
allows us to the definition later. We have chosen to use a 360-type macro language.
Q.20. What is macro processor?
Ans. A macro processor effectively constitutes a separate language processor with its own language.
It has been formed as follows:
Start of definition................................................................................MACRO
Macro name.........................................................................................[]
Sequence to be abbreviated ................
................
................
End of definition.....................................................................................MEND
Q.21. What is the various macro instruction processor that it perform?
Ans. There are four basic tasks that any macro instruction processor must perform.
1. Recognize macro definitions : A macro instruction processor must recognize macro
definitions identified by the MACRO and MEND pseudo-ops. This task can be
complicated when macro definitions appear within macros. When MACROs and MENDs
are nested, as in the example of the previous section, the macro processor must recognize
the nesting and correctly match the last or outer MEND with the first MACRO. All of the
intervening text, including nested MACROs and MENDs, defines a single macro
instruction.
2. Save the definitions : The processor must store macro instruction definitions, which it
will need for expanding macro calls.
3. Recognize calls : The processor must recognize macros calls that appear as operation
mnemonics. This suggests that macro names be handled as a type of op-code.
4. Expand calls and substitute arguments : The processor must substitute for dummy or
macro definition arguments the corresponding arguments from a macro call the resulting
symbolic text is then substitute for the macro call I his text of course, may contain
additional macro definitions or calls.
Q.22. What are the data bases used in macro processor?
Ans. Specification of data base: Te following data bases are used by the two passes of the macro
processor:
Pass 1 data bases:
1. The input macro source deck.
2. The output macro source deck copy for use by pass2.
3. The macro definition table (MDT), used to store the body of the macro definitions.
4. The macro name table (MNT), used to store the names of defined macros.
5. The macro definition table counter (MDTC), used to indicate the next available entry in
t MDT.
Macroprocessor 3-45

6. The macro name table counter (MNTC), used to indicate the next available entry in the
MNT.
7. The argument list array (ALA), used to substitute index markers for 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 1.
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 for the index
markers in the stored macro definition.
7. Stop.
Q.23. What are storage classes? Explain.
Ans. Storage classes PL/I offers the programmer three classes of storage (1) static (2) automatic
and (3) controlled.
Static storage is permanent and is assigned at compile time .Storage available in many
programming language such as FORTRAN.
Automatic storage is allocated only when the procedure or block referencing is being executed.
For example if there is a procedure SUBR with an automatic variable A on the first call
SUBR will be assigned a location for A. When the procedure is exited, A's space will be
released for possible use by other procedure. It is possible that a different location will be
assigned to A for each call to SUBR A is automatic storage and is assigned automatically at
execution time.

S-ar putea să vă placă și