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

Module 3

This document provides an overview of macros and macro processors, explaining their definitions, advantages, and differences from subroutines. It details the structure of macro definitions, the process of macro expansion, and the implementation of a two-pass macro processor. Additionally, it outlines the specifications for data bases and algorithms used in the macro processing system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views36 pages

Module 3

This document provides an overview of macros and macro processors, explaining their definitions, advantages, and differences from subroutines. It details the structure of macro definitions, the process of macro expansion, and the implementation of a two-pass macro processor. Additionally, it outlines the specifications for data bases and algorithms used in the macro processing system.
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/ 36

Module 3: Macros and Macro

Processor
Introduction
Macro
 Macro instructions (often called macro) are single-line
abbreviations for groups of instructions.
 For every occurrence of this one-line macro instruction within a
program, the instruction must be replaced by the entire block.
 The advantages of using macro are as follows:
 Simplify and reduce the amount of repetitive coding.
 Reduce the possibility of errors caused by repetitive coding.
 Make an assembly program more readable.

Notes by Prof. Archana Naware 2


Introduction
Macro Processors
 A processor is any program that processes its input data and
produce output.
 This o/p can be used as an input to another program.

The outputs of the macro processors are assembly programs.
 Thes ALPs are inputs to the assembler.
 The macro processor may exist independently and be called
during the assembling process.
 It can be a part of the assembler implementation itself.

Notes by Prof. Archana Naware 3


Difference between Macro and Subroutine
Macro Subroutine
Macro name in the mnemonic field leads to Subroutine name in a call statement in the
expansion only. program leads to execution.

Macros are completely handled by the Subroutines are completely handled by the
assembler during assembly time. hardware at runtime.
Macro definition and macro expansion areexecuted by Hardware executes the subroutine call instruction. So, it
the assembler. So, the assembler has to know all the has to know how to save the return address and how to
features, options, and exceptions associated with branch to the subroutine. The assembler knows nothing
them.The hardware knows nothing about macros. about subroutines.

The macro processor generates a new copy The subroutine call instruction is assembled in the usual
of the macro and places it in the program. way and treated by the assembler as any other instruction

Macro processing increases the size of the Use of subroutines does not result into bulk
resulting code but results in faster object codes but has substantial overheads
execution of program for expanded of control transfer during execution.
programs.

Notes by Prof. Archana Naware 4


Macro Definition and Call

 A macro consists of a name, a set of formal parameters, and a body of codes.


A macro can be defined by enclosing a set of statements between a macro header and a macro end
statement..


The formal structure of a macro includes the following features:

 Macro prototype statement: Specifies the name of the macro and name and type of formal parameters.

 Model statements: Specify the statements in the body of the macro from which assembly language
statements are to be generated during expansion.

 Macro preprocessor statement: Specifies the statement used for performing

auxiliary function during macro expansion.

Notes by Prof. Archana Naware 5


Macro Definition and Call
A macro prototype statement can be written as follows:

<name_of_macro> [<formal parameter spec> [,...]]
 where [<formal parameter spec> [,...]] defines the
parameter name and its kind, which are of the following form:
 &<name_of_parameter> [<parameter_type>]

A macro can be called by writing the name of the macro in the
mnemonic field of the assembly language.

Notes by Prof. Archana Naware 6


Macro Definition and Call
The syntax of a typical macro call can be of the following form:

<name_of_macro> [<actual_parameter_spec> [,…]]

The MACRO directive in the mnemonic field specifies the start of the
macro definition and it should compulsorily have the macro name in the
label field.

The MEND directive specifies the end of the macro definition.

The statements between MACRO and MEND directives define the body
(model statements) of the macro and can appear in the expanded code.

Notes by Prof. Archana Naware 7


Macro Definition and Call
Macro Definition
MACRO
INCR &MEM_VAL, &INC_VAL, &REG
MOVER &REG &MEM_VAL
ADD &REG &INC_VAL
MOVEM &REG &MEM_VAL
MEND

Macro Call

INCR A, B

Notes by Prof. Archana Naware 8


Macro Expansion
 A macro call in a program leads to macro expansion.


To expand a macro, the name of the macro is placed in the operation
field, and no special directives are necessary.

During macro expansion, the macro name statement in the program is
replaced by the sequence of assembly statements.

Notes by Prof. Archana Naware 9


Macro Expansion
Macro Definition START 100
MACRO A DS 1
INCR &MEM_VAL, &INC_VAL, &REG
MOVER &REG &MEM_VAL B DS 1
ADD &REG &INC_VAL MOVER REG A
MOVEM &REG &MEM_VAL ADD REG B
MEND MOVEM REG A

Macro Call PRINT A

INCR A, B STOP

END
Notes by Prof. Archana Naware 10
Features of Macro facility

There are four features.



Macro instruction argument

Conditional macro expansion

Macro call within macros
 Macro instructions defining macros.

Notes by Prof. Archana Naware 11


Macro Instruction Argument

 When a Macro is called in a program, it is replaced by identical blocks.


 There is no way for a specific Macro call to modify the coding.
 Solution is: we can provide arguments or parameters in Macro calls.

Corresponding Dummy arguments will appear in Macro definition.
 In the following example, instruction sequence are similar but not
identical.

Notes by Prof. Archana Naware 12


Macro Instruction Argument

 So we can perform same operation with dummy argument.


 It is specified on macro name line by ‘&’.

The example can be written as follow.

Notes by Prof. Archana Naware 13


Macro Instruction Argument

Notes by Prof. Archana Naware 14


Macro Instruction Argument

Notes by Prof. Archana Naware 15


Macro Instruction Argument

Notes by Prof. Archana Naware 16


Macro Instruction Argument

There are two ways to specify arguments to a macro call.



Positional Arguments:
 Arguments are matched with dummy arguments according to the order in which
they appear.
e.g. INCR A,B,C
A,B and C replace the first, second and third argument.
 Keyword arguments:
 allows reference to dummy argument by name as well as by position.

Notes by Prof. Archana Naware 17


Conditional Macro Expansion

• Conditional macros are the one which will not get expanded to the
same group of statements all the time but rather it depends on the kind
of condition being put.

• This allows conditional solution of machine instruction that appear in


expansion of a macro call.

• So the group of statements being replaced may vary in length &


sequence, depending on the condition in the macro call.

Notes by Prof. Archana Naware 18


Conditional macro expansion

Two pseudo opcodes used are AIF and AGO


1.AIF
1. it’s a conditional branch pseudo opcode.
2. It performs arithmetic test and branches only if tested condition is true.

2.AGO
1. It is an unconditional branch pseudo opcode or ‘goto statement’.
2. It specifies a label appearing on some other statement in the macro instruction definition.
3. These statements are directive to the macroprocessor and do not appear in macro expansion.

Notes by Prof. Archana Naware 19


Macro Calls within Macro

Macros can be available within other


Macro Calls within Macro
macro definition.

Example: MACRO
MACRO
ADDS &ARG1,&ARG2,&ARG3
ADD1 &ARG
L 1,&ARG ADD1 &ARG1
A 1,=F’1’ ADD1 &ARG2
ST 1,&ARG
MEND MEND

Notes by Prof. Archana Naware 20


Macro Instructions Defining Macro

Notes by Prof. Archana Naware 21


Two pass macro processor

• In a two-pass macro processor all macro definitions are processed during


the first pass and all macro invocation statements are expanded during
second pass.
 a two pass macro processor cannot handle nested macro definitions.
 Consider the macro definition example given below, which is used to
swap two numbers.
• The macro named SWAP defines another macro named STORE inside it.
These type of macro are called nested macros.
Notes by Prof. Archana Naware 22
Two pass macro processor

Consider the macro definition example given below, which is used to swap two numbers.
The macro named SWAP defines another macro named STORE inside it. These type of macro are called
nested macros.

Notes by Prof. Archana Naware 23


IMPLEMENTATION

-> There are two tasks associated with Macro.


-> The input macro source deck definition and macro call.
STATEMENT OF PROBLEM:
There are four basic task that any macro processor is associated with.
1. Recognizing macro definition:
Macro definition is recognized in a program by the statements that appear in
between MACRO and corresponding MEND.
The definition of macro includes these two pseudo codes.
Notes by Prof. Archana Naware 24
IMPLEMENTATION
2. Save the definition:
The definitions of all the macro are saved onto a table so that they can be expanded when ever in
future they are invoked.
3. Recognize the calls:
A macro call is similar to a machine instruction whose work is predefined
i.e. the definition is stored in some table.
4. Expand calls and substitute arguments:
Each time a macro call is encountered a table consisting of macro name is searched and all the
code in the macro definition except the MACRO, MEND and the macro name card is expanded in
the main program. The actual source program looks like as it was intended by the programmer.

Notes by Prof. Archana Naware 25


IMPLEMENTATION TWO PASS ALGORITHM

♣ Two pass algorithm will make two systematic scans, or passes over the inputs text.
♣ It searches first for macro definition and saves them and then searches macro calls
because macros cannot be expanded until they are defined unlike symbols in
assemblers.
♣ Hence the first pass handles the definition and the second pass handles the macro
calls or expansions.
♣ The macro definitions are stored in MDT( macro definition table) and the names
of the macros are stored in MNT( macro name table).

Notes by Prof. Archana Naware 26


SPECIFICATION OF DATA BASES

Pass 1 data bases:


1. The input macro source deck.
2. The output macro source deck copy for use by pass 2.
3. The macro definition table (MBT), 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 the MDT.
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.
Notes by Prof. Archana Naware 27
SPECIFICATION OF DATA BASES
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 in pass1.

4. The macro name table (MNT), created in pass1.

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.
Notes by Prof. Archana Naware 28
SPECIFICATION OF DATA BASE FORMAT

ARGUMENT LIST ARRAY (ALA) :

• Used during both pass1 and pass2 but some the functions are just the reverse.
• During pass1, ALA stores the arguments in the macro definition with positional
indicator when the definition is stored in MDT .
• i.e. the ith argument is stored in the ALA as #i.
• This arrangement is in according to the order of argument in which they appear in
the MDT.

Notes by Prof. Archana Naware 29


SPECIFICATION OF DATA BASE FORMAT
ARGUMENT LIST ARRAY (ALA) continued

• In the pass2, when there is macro expansion, ALA fills the arguments of the
corresponding index with its appropriate argument in the call.
• Example: when LOOP INCR DATA1, DATA2, DATA3 is executed, the ALA fills the
argument fields of the corresponding index #0, #1, #2, #3 with LOOP, DATA1, DATA2 and
DATA3.

Notes by Prof. Archana Naware 30


SPECIFICATION OF DATA BASE FORMAT
MACRO DEFINTION TABLE
– It is table of text lines.
– It consists of two fields, index that keep track of line numbers of the macro definition and
the card that is 80 bytes of size and is responsible for storing the macro definition.
– Everything except the pseudo code MACRO is inserted into the MDT.
– MEND pseudo code marks the end of the macro definition.

Notes by Prof. Archana Naware 31


SPECIFICATION OF DATA BASE FORMAT
MACRO NAME TABLE:
– It is similar to MOT and POT in assembler.
– It has got three field,
• Index field that keep track of various macro that are defined,
• Name field that keep track of names of the macros
• MDT index is a pointer to the entry in MDT.

Notes by Prof. Archana Naware 32


Flowchart: Pass1 of Two Pass Macroprocessor

Notes by Prof. Archana Naware 33


Pass 1: ALGORITHM

✜ Initializes MDTC and MNTC to 1


✜ Reads next source card.
✜ If MACRO pseudo code then
a. Read from next source card
b. Enter macro name and current value of MDTC in MNT entry number MNTC.
c. Increment MNTC.
d. Prepare ALA
e. Enter macro name card into MDT.
f. Increment MDTC.
g. Read next source card.
h. Substitute index notation for the arguments.
i. Enter line into MDT
j. Increment MDTC
k. If MEND GOTO 2. else GOTO g.
Else write copy of source card.
✜ If END then GOTO pass2 else GOTO 2.
Pass 2: ALGORITHM

1. Read next source card( copied by pass1).


2. Search MNT for match with operation code.
3. If Macro name found then
a. MDTP<-MDT index form MNT entry.
b. Setup ALA
c. Increment MDTP.
d. Get line form MDT.
e. Substitute arguments from macro call.
f. If MEND then GOTO 1 else GOTO 3.c.
4. Else write into expanded source card file
5. If END then, supply expanded source file to assembler processing, else GOTO 1.
Flowchart: Pass2 of Two Pass Macroprocessor

Notes by Prof. Archana Naware 36

You might also like