0% found this document useful (0 votes)
190 views53 pages

Macro Macro Processor PDF

1) A macro processor allows shorthand versions of program code known as macros. It replaces macro instructions with the corresponding program statements during expansion. 2) It has two passes - in Pass I it stores macro definitions in tables, in Pass II it identifies macro calls and replaces them with the stored definitions after substituting arguments. 3) The key databases used are the Macro Definition Table to store macro bodies, the Macro Name Table to map names to definitions, and the Argument List Array to substitute arguments during expansion.

Uploaded by

Mariyam Arsiwala
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)
190 views53 pages

Macro Macro Processor PDF

1) A macro processor allows shorthand versions of program code known as macros. It replaces macro instructions with the corresponding program statements during expansion. 2) It has two passes - in Pass I it stores macro definitions in tables, in Pass II it identifies macro calls and replaces them with the stored definitions after substituting arguments. 3) The key databases used are the Macro Definition Table to store macro bodies, the Macro Name Table to map names to definitions, and the Argument List Array to substitute arguments during expansion.

Uploaded by

Mariyam Arsiwala
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/ 53

Macro & Macro Processor

Nirmala Shinde
Content
• Introduction
• Features of a macro facility
• Implementation : Two pass macro processor
Introduction
• A macro instruction is a notational convenience for the programmer
• It allows the programmer to write shorthand version of a program (module
programming)
• The macro processor replaces each macro instruction with the
corresponding group of source language statements(expanding).
• Normally, it performs no analysis of the text it handles.
• It doesn’t concern the meaning of the involved statements during macro
expansion.
• The design of a macro processor generally is machine independent!
Macro Example

macro
Basic Functions of Macro Processor
• Macro is a Single Line abbreviation for set of instructions.
• Two new assembler directives are used in macro definition
• MACRO : identify the beginning of a macro definition
• MEND : identify the end of a macro definition
Example
Macro definition – Once the macro has Macro Expansion – Replacement of macro
been defined, the use of the macro name call by corresponding sequence of
as an operation mnemonic is an assembly instructions is called as macro expansion.
called as macro call.
• Source Program • Expanded source
Features of macro facility
• Macro instruction arguments
• Conditional macro expansion
• Macro calls within macros
• Macro instructions defining macros
Macro Instruction Arguments
• 1st sequence with DATA1 & 2nd with DATA2.
• To perform same operation with a variable
parameter/argument.
• Parameter is called as macro instruction
argument or dummy argument.
• They are denoted by leading ampersand (&)
Cont…
Source Expanded Source
Example: more than one arguments
• The operands in the common
sequence are different.
• As well as labels are on the 1st cards
of the sequences.
• Macro can be written as
Cont…
Cont…
• Specifying arguments to a macro call
• Positional argument
• Arguments are matched with dummy arguments according to the order in which they
appear.
• E.g. In a macro call “INCR A,B,C” replaces the 1st (A), 2nd (B), 3rd (C) dummy argument.

• Keyword argument
• Allows dummy arguments by name as well as by position.
• E.g. we may write “INCR &ARG1=A , &ARG2=B, &ARG3=C”
OR
“INCR &ARG1=A, &ARG2=&ARG3=C”
Conditional Assembly
• Conditional assembly, in which the expansion of a macro can lead to a number of
distinct code sequences.
• Conditional assembly permits the testing of attributes such as data format,
data value, or field length, and to use the results of such testing to generate
source code that is specific to the case in question.

• Conditional assembly instructions:


• AGO – an unconditional branch
• AIF – a conditional branch, i.e. ASK IF
• ANOP – a NOP that can be the branch target for either AGO/AIF.
• MNOTE – print a programmer defined message at assembly time
• MEXIT – exit the macro definition.
Attributes for use by conditional assembly
• The assembler can generate code specified by certain attributes of
the arguments to the macro definition at the time it is expanded.
• Attributes are:
• L (Length) – The length of the symbolic parameter.
• I (Integer) – The integer attribute of a fixed-point, floating-point or packed
decimal number
• T (Type) – The type of the parameter, as specified by the DC/DS declaration
with which it is defined.
• A(Address), C(Character), H(Halfword), F(Fullword), I(Instructuion), X(Hexdecimal) etc.
AIF Instruction
• AIF permits conditional reordering of the sequence of macro
expansion. This allows conditional selection of the machine
instruction that appear in expansions of a macro call.
• Has two parts:
1. A logical expression in parentheses, and
2. A sequence symbol immediately following, which serves as the branch
target.
• A sequence symbol begins with a period (.) followed by one to seven letters/digits, the
first of which must be a letter.
• E.g. .SYSNDX
• It performs arithmetic test & branch only if condition is true.
Cont…
• The AIF logical expression may use the following relation operators,
• EQ – Equal to
• NE – Not equal to
• LT – Less than
• GE – Greater than or equal to
• GT – Greater than
• LE – Less than or equal to

• E.g. If the &AMT is less than 2, go to .B23PACK


AIF (&AMT LT 2) .B23PACK
Conditional Macro Expansion
• Here the operands, labels and
the number of instructions
generated change in each
sequence.
Source
Expanded Source
AGO
• AGO unconditionally alters normal sequential statement processing
• Assembler breaks normal sequential statement processing
• Resumes at statement marked with the specified sequence symbol
• It is like goto statement
.
• E.g. .
L 2, &C
AGO .SKIP
.STORE ST 3, &D
.SKIP A 3, &M
.
.
Macro-calls within macro
• Macros are abbreviations of
instruction sequence.
• Such abbreviations are also
present within other macro
definition
Macro-instructions defining macros
• Macro-definition, which itself
is a sequence of instruction,
can be abbreviated by using
macro.
• It is important to note that
inner macro is not defined
until the outer macro is
called.
Cont…
• The user might call the above macro with the statement
DEFINE COS
• When this call is executed then the macro named COS will be defined.
• So after the above call user can call the COS macro with the
statement
COS AR
• The macro processor will generate the sequence
BAL 1,*+8
DC A(AR)
L 15,=V(COS)
BALR 14,15
Design of macro-processor
• There are four basic task that a macro-processor must perform
• Recognize macro definition
• A macro instruction processor must recognize macro definition identified by MACRO &
MEND pseudo-op.
• Save the definition
• The processor must store the macro-definitions which will be needed at the time of
expansion of calls.
• Recognize the macro call
• The processor must recognize macro calls that appear as operation mnemonics.
• Expand calls & substitute arguments
• The macro call is replaced by the macro definition.
• The dummy arguments are replaced by the actual data.
Two Pass Macro-Processor
• General Design Steps:
1. Specification of problem
2. Specification of databases
3. Specification of database formats
4. Algorithm
Specify the problem
• Pass I
• The macro definitions are searched and stored in the macro definition table &
the entry is made in macro name table.

• Pass II
• The macro calls are identified and the arguments are placed in the
appropriate place and the macro calls are replaced by macro definitions.
Pass I Specification of Data Bases
• Input – macro source program
• Output – macro source program used by pass II.
• Macro definition table (MDT) – store the body of macro definition
• Macro definition table counter (MDTC) – to mark next available entry
MDT.
• Macro name table (MNT) – used to store names of macros.
• Macro name table counter (MNTC) – used to indicate the next
available entry in MNT.
• Argument list array (ALA) – used to substitute index markers for
dummy arguments before storing a macro definition.
Pass II Specification of Databases
• The copy of source program
• Output expanded program from pass II
• MDT created by pass I
• MNT created by pass I
• Macro definition table pointer (MDTP) - indicate the next line of text
to be used during macro-expansion.
• ALA created by pass I.
Specification of database format
• Macro Definition Table
• Macro Name Table
• Argument List Array
Example: more than one arguments
• The operands in the common
sequence are different.
• As well as labels are on the 1st cards
of the sequences.
• Macro can be written as
MDT
• MDT is a table of text lines.
• Every line of each macro definition
except the MACRO line, is stored in
the MDT (MACRO line is useless
during macro-expansion)
• MEND is kept to indicate the end of
the defn.
• The macro-name line is retained to
facilitate keyword argument
replacement.
MNT
• It serves a fn similar to that
of assembler’s MOT & POT
• Each MNT entry consists of
• A character string (the macro
name)
• A pointer (index) to the entry
in MDT that corresponds to
the beginning of the macro-
definition (MDT index).
ALA
• ALA is used during both Pass1 & Pas2 but for some what reverse functions.
• During Pass 1, in order to simplify later argument replacement during macro
expansion, dummy arguments are replaced with positional indicators when
defn is stored.
• The ith dummy argument on the macro-name is represented in the body by #i.
Ex. # 1, # 2, # 3 etc.
• These symbols are used in conjunction with the argument list prepared before
expansion of a macro-call.
• Symbolic dummy argument are retained on macro-name card to enable the
macro processor to handle argument replacement by name rather by
position.
During Pass I
During Pass II
• During Pass II it is necessary to
substitute macro call arguments for
the index markers stored in the
macro definition.
• Thus upon encountering the call
LOOP INCR DATA1, DATA2, DATA3
• The macro call expander would
prepare an argument list array:
• The list would be used only while
expanding this particular call.
Pass 1
Pass 1 – processing macro definitions

MDTC<-1
MNTC<-1

Read next
source card

MACRO No
Write copy of
pseudo-
source card
op?
Yes
END
pseudo- No
Read next
op?
source card
Yes

Go to Pass 2
Enter macro name and
Read next
current value of MDTC in
source card
MNT entry number MNTC

Substitute
MNTC <- MNTC + 1 index notation
for arguments

Prepare argument list array


Enter line into MDT

Enter macro name card


into MDT MDTC <- MDTC + 1

MDTC <- MDTC + 1


MEND
pseudo-
Yes op? No
Pass 2
Pass 2 – processing macro calls and expansion

Read next source


card (copied by
pass 1)

Search MNT for match with


operation code

MACRO No
Write into
name
expanded source
found?
card file
Yes
END No
MDTP <- MDT index from MNT pseudo-op?
entry
Yes

Supply expanded source


Set up argument list array
file to assembler
processing

MDTP <- MDTP + 1

Get line from MDT

Substitute arguments from


macro call

MEND Write expanded


pseudo-op? source card
Yes
No
Algorithm Pass - 1
• Pass1 of macro processor makes a line-by-line scan over its input.
• Set MDTC = 1 as well as MNTC = 1.
• Read next line from input program.
• If it is a MACRO pseudo-op, the entire macro definition except this
(MACRO) line is stored in MDT.
• The name is entered into Macro Name Table along with a pointer to
the first location of MDT entry of the definition.
• When the END pseudo-op is encountered all the macro-defns have
been processed, so control is transferred to pass2.
Algorithm For Pass - 2
• This algorithm reads one line of i/p prog. at a time.
• for each Line it checks if op-code of that line matches any of the MNT entry.
• When match is found (i.e. when call is pointer called MDTF to corresponding macro
defns stored in MDT.
• The initial value of MDTP is obtained from MDT index field of MNT entry.
• The macro expander prepares the ALA consisting of a table of dummy argument
indices & corresponding arguments to the call.
• Reading proceeds from the MDT, as each successive line is read, The values form the
argument list one substituted for dummy arguments indices in the macro defn.
• Reading MEND line in MDT terminates expansion of macro & scanning continues
from the input file.
• When END pseudo-op encountered , the expanded source program is given to the
assembler.
Exercise
Line no. Instructions Call to the macro as follows
1 MACRO
Line no.
2 INCR &a, &b
11 PRG START
3 A 1, &a
12 USING *, 15
4 L 2, &b
13 INCR DATA1, DATA2
5 MEND
14 INCR1 DATA3, DATA4
Line no. Instructions 15 FOUR DC F’4’
6 MACRO 16 FIVE DC F’5’
7 INCR1 &ARG1, &ARG2 17 TEMP DS ‘1’F’
8 L 3, &ARG1 18 END
9 ST 4, &ARG2
10 MEND
MNT Created by pass - 1
MACRO NAME TABLE for INCR
Line no. Instructions
1 MACRO INDEX MACRO NAME MDT INDEX
2 INCR &a, &b 1 INCR 1
3 A 1, &a 2
4 L 2, &b 3
5 MEND 4
5
Line no. Instructions
6 MACRO
7 INCR1 &ARG1, &ARG2
8 L 3, &ARG1
9 ST 4, &ARG2
10 MEND
ALA Created by pass - 1
ARGUMENT LIST TABLE FOR INCR
Line no. Instructions
1 MACRO INDEX ARGUMENT
2 INCR &a, &b 0 &a
3 A 1, &a 1 &b
4 L 2, &b 2
5 MEND

Line no. Instructions


6 MACRO
7 INCR1 &ARG1, &ARG2
8 L 3, &ARG1
9 ST 4, &ARG2
10 MEND
MDT Created by pass – 1
MACRO DEFINITION TABLE
Line no. Instructions
1 MACRO INDEX DEFINITION(CARD)
2 INCR &a, &b 1 INCR &a, &b
3 A 1, &a 2 A 1, #0
4 L 2, &b 3 L 2, #1
5 MEND 4 MEND

Line no. Instructions


6 MACRO
7 INCR1 &ARG1, &ARG2 5 INCR1 &ARG1, &ARG2
8 L 3, &ARG1 6 L 3, #2
9 ST 4, &ARG2 7 ST 4, #3
10 MEND 8 MEND
MNT Created by pass - 1
MACRO NAME TABLE for INCR & INCR1
Line no. Instructions
1 MACRO INDEX MACRO NAME MDT INDEX
2 INCR &a, &b 1 INCR 1
3 A 1, &a 2 INCR1 5
4 L 2, &b 3
5 MEND 4
5
Line no. Instructions
6 MACRO
7 INCR1 &ARG1, &ARG2
8 L 3, &ARG1
9 ST 4, &ARG2
10 MEND
MDT Created by pass – 1
MACRO DEFINITION TABLE INCR & INCR1
Line no. Instructions
1 MACRO INDEX DEFINITION(CARD)
2 INCR &a, &b 1 INCR &a, &b
3 A 1, &a 2 A 1, #0
4 L 2, &b 3 L 2, #1
5 MEND 4 MEND
5 INCR1 &ARG1, &ARG2
Line no. Instructions
6 L 3, #2
6 MACRO
7 ST 4, #3
7 INCR1 &ARG1, &ARG2
8 MEND
8 L 3, &ARG1
9 ST 4, &ARG2
10 MEND
ALA Created by pass - 1
ARGUMENT LIST TABLE FOR INCR & INCR1
Line no. Instructions
1 MACRO INDEX ARGUMENT
2 INCR &a, &b 0 &a
3 A 1, &a 1 &b
4 L 2, &b 2 &ARG1
5 MEND 3 &ARG2

Line no. Instructions


6 MACRO
7 INCR1 &ARG1, &ARG2
8 L 3, &ARG1
9 ST 4, &ARG2
10 MEND
ALA Created by pass - 2
ARGUMENT LIST TABLE FOR INCR & INCR1
Call to the macro as follows
INDEX ARGUMENT
Line no.
0 DATA1
11 PRG START
1 DATA2
12 USING *, 15
2 DATA3
13 INCR DATA1, DATA2
3 DATA4
14 INCR1 DATA3, DATA4
15 FOUR DC F’4’
16 FIVE DC F’5’
17 TEMP DS ‘1’F’
18 END
Pass 2
• Expanded source code
Line no. Instruction
11 PRG START
12 USING *, 15
INCR DATA1, DATA2
13 A 1, DATA1
14 L 2, DATA2

15 L 3, DATA3 INCR1 DATA3, DATA4

16 ST 4, DATA4
17 FOUR DC F’4’
18 FIVE DC F’5’
19 TEMP DS ‘1’F’
20 END
Macro Functions (or Subroutines)

Definition Macros are single-line abbreviations of a Functions are blocks of code that are to
small sequence of instructions. be executed repeatedly by the program

Lines of Code Macros are used when number of Functions can be used for any length of
repeating instructions is very small (3 to code that is to be executed repeatedly.
5 instructions)

Type Checking No type checking for macro arguments Function argument type checking is
performed, with prototype & definition

Time of processing Macros are processed at compilation time Functions are processed during execution
time
Processing the call Macro calls are replaced by corresponding When a function is called, its corresponding
sequence of instructions; these instructions are instructions are actually executed.
not actually executed.
Housekeeping No housekeeping operations (pushing and The return address is pushed onto stack, when a
operations popping on the stack) are required. function is called and is popped back when the
function returns.
Need of a Macro calls are not recognized by the Functions (or Subroutines) are executed by the
Preprocessor assembler. So, a separate Macro Processor is Assembler itself (as Procedures)
needed to expand them.
Return value Macros cannot return a value Functions can return a value

Size of the program Macro calls are expanded inline. So, they Functions do not increase the size of the program
increase the size of the program

Speed of execution Since there are no housekeeping operations Housekeeping operations make programs using
involved, programs using macros execute functions execute at lesser speed.
slightly faster than the ones using functions
Reference
• http://www.edwardbosworth.com/MY3121_LectureSlides_HTML/Ma
crosAndStacks_Part2.htm
• http://www-
01.ibm.com/support/knowledgecenter/SSLTBW_1.13.0/com.ibm.zos.
r13.asma400/ago.htm%23ago
• ftp://ftp.software.ibm.com/software/websphere/awdtools/hlasm/s8
167b.pdf
• http://www.edwardbosworth.com/MY3121_LectureSlides_HTML/Ma
crosAndStacks_Part2.htm

You might also like