SlideShare ist ein Scribd-Unternehmen logo
1 von 62
Subject-System Programming
Sub. Code-2150708
Unit- 4 (Macro and Macro pre-
processor)
By- Prof. Deepmala Sharma
MBICT CVM group, Anand
(V.V.Nagar)
Contents
• What is Macro?
• Advantages and disadvantages of Macro
• Applications of Macro
• Difference between Macro and Subroutines
• Macro Definition and Call
• Macro Expansion
• Handling Macro calls and Macro expansion
• Features of Macro facility
– Lexical expansion and parameter substitution
– Nested Macro Calls
– Advanced Macro Facilities
• Design Of a Macro Pre-processor
– Functions of a Macro Processor
– Basic Tasks of a Macro Processor
– Design Issues of Macro Processors,
– Features,
– Macro Processor Design Options,
– Two-Pass Macro Processors, and One-Pass Macro Processors
• Design of a Macro Assembler
Macro and Macro processor
• Macro:-
– Macro instructions are single line abbreviations for group
of instructions.
– Using a macro, programmer can define a single
“instruction” to represent block of code.
– Ex: #define in C and MACRO <Macro Name> in Assembly
language
• Macro processor-
– Macro instructions are an extension of the basic assembly
language that can simplify debugging and program
modification during translation.
– To process macro instructions , most assembler use pre-
processors known as Macro processors
Advantages and Disadvantages
• 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
• The disadvantage of the macro is the size of the
program. The reason is, the pre-processor will
replace all the macros in the program by its real
definition prior to the compilation process of the
program.
Applications of Macro
• when assembly language programming was
commonly used to write programs for digital
computers
• the use of macro instructions was initiated for
two main purposes:
• to reduce the amount of program coding that
had to be written by generating several
assembly language statements from one
macro instruction
MACROS PROCEDURE
1
The corresponding machine code
is written every time a macro is
called in a program.
1 The Corresponding m/c code is
written only once in memory
2
Program takes up more memory
space.
2 Program takes up comparatively
less memory space.
3
No transfer of program counter. 3 Transferring of program counter is
required.
4
No overhead of using stack for
transferring control.
4 Overhead of using stack for
transferring control.
5
Execution is fast 5 Execution is comparatively slow.
6
Assembly time is more. 6 Assembly time is comparatively
less.
7
More advantageous to the
programs when repeated group of
instruction is too short.
7 More advantageous to the
programs when repeated group of
instructions is quite large.
Difference between MACRO and PROCEDURE
Difference between MACRO and Subroutine
Design of Macro pre-processor
Macro definition and call
• A macro is a unit of specification for program generation
through expansion.
• A macro consists of a name, a set of formal parameters and
a body of code.
• The use of macro name with a set of actual parameters is
replaced by code generated from its body.
• The formal structure of a macro includes the following
features:
– Macro prototype statement: Specifies the name of the macro
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
Macro Statements
• A macro prototype statement
• where [<formal parameter spec> [,...]] defines
the parameter name and its kind, which are of
the following form:
• A macro can be called by writing the name of the
macro in the mnemonic field of the assembly
language called macro call
• macro call Statement:
Example of macro definition and call
• Macro Definition
• Macro Call
Name of macro
Start of definition
Sequence of instructions
End of definition
Macro Expansion
• Replacement of macro call by corresponding sequence
of instructions is called as macro expansion
• To expand a macro, the name of the macro is placed in
the operation field, and no special directives are
necessary.
• The assembly code sequence INCR A, B, AREG
is an example of the macro call
• the assembler recognizes INCR as the name of the
macro, expands the macro, and places a copy of the
macro definition (along with the parameter
substitutions)
• ‘+’ sign in the preceding label field denote the
expanded code
Example
MACRO
INCR &A, &B, AREG
MOVER REG &A
ADD REG & B
MOVEM REG & A
MEND
Macro definitionMacro call
 A and B are an actual
parameters
 &A and &B are formal
parameters
Macro call
Expanded code
First Macro Example:-
#include <stdio.h>
#define SquareOf(x) x*x // (Macro definition)
void main() {
int xin=3;
printf("nxin=%i",xin);
printf("nSquareOf(xin)=%i",SquareOf(xin)); // (macro call)
printf("nSquareOf(xin+4)=%i",SquareOf(xin+4));
printf("nSquareOf(xin+xin)=%i",SquareOf(xin+xin));
}
Lexical Expansion and Parameter
Substitution (Lexical Substitution)
• Two key points concerning macro expansion
are:
– Expansion time control flow: This determines the
order in which model statements (body) are
visited during macro expansion.
– Lexical Substitution: It is used to generate an
assembly statement from a model
statement(body).
Four ways of specifying arguments to
a macro call (Parameter Substitution)
• A) Positional argument or Parameter
Argument are matched with dummy arguments
according to order in which they appear or
compute the ordinal position of the formal
parameters.
• INCR A,B,C
• ‘A’ replaces first dummy argument
• ‘B’ replaces second dummy argument
• ‘C’ replaces third dummy argument
Example
• Macro definition
– MACRO
– MNAME &par1, &par2, &par3
– MOVER &par1, &par2
– ADD &par1, &par3
– MOVEM &par1, &par2
– MEND
• Macro call
– MNAME REG1, A, B
• Expanded code
– MOVER REG1, A
– ADD REG1, B
– MOVEM REG1, A
Formal Par Actual par
&par1 REG1
&par2 A
&par3 B
• B) keyword arguments or parameters
• This allows reference to dummy arguments by
name as well as by position.
• The parameter specification are useful when
macros use a long list of parameters
• Format: &<name_of_par>=<actual_par>
Example
• Macro definition
– MACRO
– MNAME &par1=, &par2=, &par3=
– MOVER &par1, &par2
– ADD &par1, &par3
– MOVEM &par1, &par2
– MEND
• Macro call
– 1. MNAME par1=REG1, par2=A, par3=B OR
– 2. MNAME par1=A, par2=REG1, par3=B
• 1. Expanded code
– MOVER REG1, A
– ADD REG1, B OR
– MOVEM REG1, A
2. Expanded code
MOVER A, REG1
ADD A, B
MOVEM A, REG1
• C) keyword Default Parameters
• If a parameter has the same value in most calls
on a macro, this value can be specified as its
default value in the macro definition itself.
• If a macro call does not explicitly specify the value
of the parameter, the pre-processor uses its
default value; otherwise, it uses the value
specified in the macro call.
• Par. spec. required when the parameter takes the
same value in most calls.
• Format :
Example
• Macro definition
– MACRO
– MNAME &par1= REG1, &par2=, &par3=
– MOVER &par1, &par2
– ADD &par1, &par3
– MOVEM &par1, &par2
– MEND
• Macro call
– 1. MNAME par2=A, par3=B OR
– 2. MNAME par1=REG2, par2=A, par3=B
• 1. Expanded code
– MOVER REG1, A
– ADD REG1, B OR
– MOVEM REG1, A
2. Expanded code
MOVER REG2, A
ADD REG2, B
MOVEM REG2, A
• d) Mixed Parameters
• A macro definition may use both positional and
keyword parameters. In such a case, all
• positional parameters must precede all keyword
parameters in a macro call
• This makes the search and association of the
actual parameters to the corresponding formal
parameter easier.
• The parameters can be used not only in the
operation field, but also in all the field of the
instruction
Example
• Macro definition
– MACRO
– MCALC &par1=, &par2=, &par3=MULT, &par4=
– &par4
– MOVER REG1, &par1
– ADD REG1, &par2
– MOVEM REG1, &par1
– MEND
• Macro call
– MCALC A, B, par4=loop
• 1. Expanded code
– +loop MOVER REG1, A
– MULT REG1, B
– MOVEM REG1, A
Flow of control during macro
expansion
• Flow of control during expansion.
– The default flow of control during macro expansion is
sequential. its start with statement following the
macro prototype statement and ending with the
statement preceding the MEND statement.
– A pre-processor statement can alter the flow of
control during expansion such that some model
statements are never visited during expansion is called
conditional expansion.
– Same statement are repeatedly visited during
expansion is called loops expansion.
Flow of control during macro
expansion
• The flow of control during macro expansion is
implemented using a macro expansion counter (MEC)
• Algorithm of macro expansion:
• 1. MEC:=statement number of first statement following
the prototype stmt.
• 2. While statement pointed by MEC is not a MEND
statement.
– a. If a model statement then
• i. Expand the statement
• ii. MEC:=MEC+1;
– b. Else (i.e. a pre-processor statement)
• i. MEC:= new value specified in the statement.
• 3. Exit from macro expansion.
Nested Macro Facility
• Nested Macro Definition:
• Nested macro definition are those cases where the definition of
one macro is contained within the definition of another
• Example :
• MACRO
• MAC_X &par1, &par2
• MOVER REG1, &par1
– MACRO
– MAC_Y &par2, REG=REG3
– ADD REG, &par2
– MOVEM REG, &par2
– MEND
• PRINT &par1
• MEND
Nested Macro Facility
• Nested Macro Expansion and Call:
• A model statement in a macro may constitute a
call on another macro.
• Such calls are known as nested macro calls
• Macro containing the nested call is the outer
macro and, Macro called is inner macro
• They follow LIFO rule.
• Thus, in structure of nested macro calls,
• expansion of latest macro call (i.e inner macro) is
• completed first.
Example
• MACRO
– MAC_Y &par2, REG=REG3
– ADD REG, &par2
– MOVEM REG, &par2
– MEND
• MACRO
– MAC_X &par1, &par2
– MOVER REG1, &par1
– MAC_Y &par2, REG=REG3
• PRINT &par1
• MEND
Nested Expansion
Macro call Expansion Nested expansion
MAC_X A, B + MOVER REG1, A + MOVER REG1, A
+MAC_Y B, REG2 ++ADD REG2, B
+PRINT A ++ MOVEM REG2, B
+PRINT A
Recursive macro call
• A recursive macro is a situation where the macro
expands itself.
• Note: Macro definition does not contain any statement
to stop any of the inner expansions and return to
complete the outer ones. This lead to infinite loop.
• The conditional statement is one mechanism to handle
recursive call
• MACRO
– MAC_Y &par=A
– MOVER REG1, &par
– MAC_Y &par=A
• MEND
ADVANCED MACRO FACILITIES
• Facilities for change of flow of control
• Expansion time variables
• Attributes of parameters
• Advance directives
– The Remove Directive
– The IRP Directive
– The REPEAT Directive
For change of flow of control:-
Two features are provided for this purpose
1) Expansion time Sequencing Symbol
2) Expansion time Statements ( AIF,AGO,ANOP)
Sequencing Symbol( SS)
SS is defined by putting it in the label field of a stmt in the macro
body.
Statements (syntax)
AIF-> AIF ( <expression>) <Sequencing Symbol (SS)>
Expression includes relational expression ( LT, NE etc.). If condition
is true then control is transferred to SS.
AGO-> AGO <SS>  unconditional transfer
ANOP-> <SS> ANOP  used to define SS
Example
MACRO
TEST &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &Y
AGO .LAST
.ONLY MOVER AREG, &Z
.LAST MEND
REPRESENTING SS
If condition is true ( Y= X) then control will be transferred
to .ONLY
Expansion time variables
EV’s are variables which can be only used during the expansion
of macro calls.
It may be of two types:-
1) Local EV (LCL) can be used only in one particular macro.
2) Global EV (GBL) works like global variables.
Syntax:-
LCL <EV specification>, ………
GBL <EV specification>, ……..
Values of EV can be changed using SET (preprocessor stmt).
<EV specification> SET <SET expression>
Example of EV
MACRO
TESTEV
LCL &A
&A SET 1
LCL &A declares local variable A
Attributes of parameters
An attribute is written using the syntax:-
<attribute name> ’ <formal parameter specification>
T– type
L – length
S – size
Example
MACRO
ME &A
AIF (L’&A EQ 1) .NEXT
------
-----
EXAMPLE
MACRO
TEST &X, &N, R=AREG
LCL &M
&M SET 0
.PQ MOVEM &R, &X+&M
&M SET &M + 1
AIF (&M NE N) .PQ
MEND
MACRO CALL
TEST S,10
REPRESENTS PARAMETER NAME
REPRESENTS EXPAN. VAR. NAME
REPRESENTS SEQUENCING SYMBOL
NAME
REPRESENTS ACTUAL PARAMETER NAME
Advance directives
• The Remove Directive: Remove Macro from MDT(Macro definition table).
• The IRP Directive: (Indefinite repeat): It is used by programmer to direct
the assembler to repeatedly duplicate and assemble the sequence a
number of times determined by a compound parameter.
• IRP example
– Macro
– MAC_X &P, &Q
– IRP &P
– ADD REG1, &P
– IRP
– MEND
• MACRO CALL
– MAX_X (A ,B, #3), H
• On execution
– ADD REG1, A
– ADD REG1, B
– ADD REG1, #3
The REPEAT (REPT)Directive
• REPEAT: ): This is another facility with assembler to duplicate and
assemble the sequence a number of times during macro expansion.
• Syntax: REPT <expression>
– MACRO
– DEF_R
– LCL &P
– &P SET 5
– REPT 5
– DC ‘&P’
– &P SET &P+1
– ENDM
– MEND
– On execution
– 5,6,7,8,9,10 will be output
Design issues of Macro preocessor
• Flexible data structures and databases
• Attributes of macro arguments/parameter
name
• Default arguments/ formal parameter value at
macro definition time
• Numeric values of arguments/ actual
parameter value at macro call
• Comments in macros
Features of macro processor
• Associating macro parameters with their
arguments
• Delimiting macro parameters
• Directives related to arguments
• Automatic label generation
• Machine independent features
Design of Macro Preprocessor
Functions of Macro preprocessor
• Macro processor will perform the following
tasks-
– Identifies macro definitions and calls in the
program.
– Determines formal parameters and their values.
– Keeps track of the values of expansion time
variables and sequencing symbols declared in a
macro.
– Handles expansion time control flow and performs
expansion of model statements.
Two pass Macro Processor
• General Design Steps
• Step 1: Specification of Problem:-
• Step 2 Specification of databases:-
• Step 3 Specification of database
formats
• Step 4 : Algorithm
Specify the problem
• In Pass-I the macro definitions are searched
and stored in the macro definition table and
the entry is made in macro name table
• In 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.
Specification of databases:-
Pass 1:-
• The input macro source program.
• The output macro source program to be used by Pass2.
• Macro-Definition Table (MDT), to store the body of macro
defns.
• 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-defns.
Specification of databases:-
• Pass 2:-
• The copy of the input from Pass1.
• The output expanded source to be given to assembler.
• MDT, created by Pass1.
• MNT, created by Pass1.
• Macro-Definition Table Pointer (MDTP), used to
indicate the next line of text to be used during macro-
expansion.
• Argument List Array (ALA), used to substitute macro-
call arguments for the index markers in the stored
macro-defns
Specification of Database format
Specification of Database format
EXAMPLE
MACRO
TEST &X, &N, R=AREG
LCL &M
&M SET 0
.PQ MOVEM &R, &X+&M
&M SET &M + 1
AIF (&M NE N) .PQ
MEND
MACRO CALL
TEST S,10
REPRESENTS PARAMETER NAME
REPRESENTS EXPAN. VAR. NAME
REPRESENTS SEQUENCING SYMBOL
NAME
REPRESENTS ACTUAL PARAMETER NAME
X
N
R
PNTAB
M
EVNTAB
R AREG
KPDTAB
PQ
SSNTAB
S
10
AREG
APTAB
0
EVTAB
27
SSTAB
Name #PP #KP #EV MDTP KPDTP SSTP
TEST 2 1 1 25 10 5
MNT
25 LCL (E,1)
26 (E,1) SET 0
27 MOVEM (P,3),(P,1)+(E,1)
28 (E,1) SET (E,1) +1
29 AIF (E,1) NE (P,2) (S,1)
30 MEND
MDT
Steps for MNT Entry
Name #PP #KP #EV MDTP KPDTP SSTP
TEST &X, &N, R=AREG
STRUCTURE OF MNT
For each positional parameter
(i) Enter Parameter name in PNTAB [PNTAB_ptr]
(ii)PNTAB_ptr := PNTAB_Ptr + 1;
(iii)# PP := # PP + 1 :
PNTAB
x
#pp=1
N
#pp=1+1=2
.PQ MOVEM &R, &X+&M
SSNTAB
a) If SS is present in SSNTAB then get index No.
Else
1 Enter SS in SSNTAB, then get index No. and
2. Enter MDT_Ptr in SSTAB
b) For a parameter , generate the specification ( p,#n).
c) For an expansion variable,generate the
specification ( E, # m).
.PQ
SSTAB
5
27
.PQ MOVEM &R, &X+&M
MOVEM (P,3) , (P,1)+(E,1) Enter this line into MDT
Steps for Model Stmt
Argument List Array (ALA):
• ALA is used during both Pass1 & Pas2 but for some what
reverse functions.
• During Pass1, in order to simplify later argument
replacement during macro expansion, dummy arguments
are replaced with positional indicators when defn is stored.
Ex. # 1, # 2, # 3 etc.
• The ith dummy argument on the macro-name is
represented in the body by #i.
• These symbols are used in conjunction with ALA prepared
before expansion of a macro-call.
• Symbolic dummy argument are retained on macro-name to
enable the macro processor to handle argument
replacement byname rather by position.
During pass-I
Algorithm
• 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
MDTC 1
MNTC 1
MDTC MDTC+1
MDTC MDTC+1
Enter Macro Name and Current
value of MDTC in MNT
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
MDTP  MDTP + 1
Pass structure of Macro assembler
Macro assembler flow chart

Weitere ähnliche Inhalte

Was ist angesagt?

Direct linking loader
Direct linking loaderDirect linking loader
Direct linking loaderbabyparul
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language ProcessingHemant Sharma
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1Manoj Patil
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code GeneratorDarshan sai Reddy
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loadersTech_MX
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03desta_gebre
 
System Programming- Unit I
System Programming- Unit ISystem Programming- Unit I
System Programming- Unit ISaranya1702
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Language processing activity
Language processing activityLanguage processing activity
Language processing activityDhruv Sabalpara
 

Was ist angesagt? (20)

Direct linking loader
Direct linking loaderDirect linking loader
Direct linking loader
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language Processing
 
Design of a two pass assembler
Design of a two pass assemblerDesign of a two pass assembler
Design of a two pass assembler
 
Unit 3
Unit 3Unit 3
Unit 3
 
Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
 
MACRO PROCESSOR
MACRO PROCESSORMACRO PROCESSOR
MACRO PROCESSOR
 
Linker and Loader
Linker and Loader Linker and Loader
Linker and Loader
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loaders
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
 
Loaders
LoadersLoaders
Loaders
 
loaders and linkers
 loaders and linkers loaders and linkers
loaders and linkers
 
System Programming- Unit I
System Programming- Unit ISystem Programming- Unit I
System Programming- Unit I
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Language processing activity
Language processing activityLanguage processing activity
Language processing activity
 
Macro Processor
Macro ProcessorMacro Processor
Macro Processor
 

Ähnlich wie Unit 4 sp macro (20)

Unit 2
Unit 2Unit 2
Unit 2
 
Module 5.pdf
Module 5.pdfModule 5.pdf
Module 5.pdf
 
Ss4
Ss4Ss4
Ss4
 
Handout#05
Handout#05Handout#05
Handout#05
 
33443223 system-software-unit-iv
33443223 system-software-unit-iv33443223 system-software-unit-iv
33443223 system-software-unit-iv
 
handout6.pdf
handout6.pdfhandout6.pdf
handout6.pdf
 
Pre processor directives in c
Pre processor directives in cPre processor directives in c
Pre processor directives in c
 
Handout#04
Handout#04Handout#04
Handout#04
 
MACRO ASSEBLER
MACRO ASSEBLERMACRO ASSEBLER
MACRO ASSEBLER
 
Chap6 procedures &amp; macros
Chap6 procedures &amp; macrosChap6 procedures &amp; macros
Chap6 procedures &amp; macros
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
 
Preprocessor.pptx
Preprocessor.pptxPreprocessor.pptx
Preprocessor.pptx
 
Modularisation techniques new
Modularisation techniques newModularisation techniques new
Modularisation techniques new
 
Macro assembler
 Macro assembler Macro assembler
Macro assembler
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
Macro Programming
Macro ProgrammingMacro Programming
Macro Programming
 
SAS Macro
SAS MacroSAS Macro
SAS Macro
 
Macros in system programing
Macros in system programingMacros in system programing
Macros in system programing
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 

Kürzlich hochgeladen

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 

Kürzlich hochgeladen (20)

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

Unit 4 sp macro

  • 1. Subject-System Programming Sub. Code-2150708 Unit- 4 (Macro and Macro pre- processor) By- Prof. Deepmala Sharma MBICT CVM group, Anand (V.V.Nagar)
  • 2. Contents • What is Macro? • Advantages and disadvantages of Macro • Applications of Macro • Difference between Macro and Subroutines • Macro Definition and Call • Macro Expansion • Handling Macro calls and Macro expansion • Features of Macro facility – Lexical expansion and parameter substitution – Nested Macro Calls – Advanced Macro Facilities • Design Of a Macro Pre-processor – Functions of a Macro Processor – Basic Tasks of a Macro Processor – Design Issues of Macro Processors, – Features, – Macro Processor Design Options, – Two-Pass Macro Processors, and One-Pass Macro Processors • Design of a Macro Assembler
  • 3. Macro and Macro processor • Macro:- – Macro instructions are single line abbreviations for group of instructions. – Using a macro, programmer can define a single “instruction” to represent block of code. – Ex: #define in C and MACRO <Macro Name> in Assembly language • Macro processor- – Macro instructions are an extension of the basic assembly language that can simplify debugging and program modification during translation. – To process macro instructions , most assembler use pre- processors known as Macro processors
  • 4. Advantages and Disadvantages • 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 • The disadvantage of the macro is the size of the program. The reason is, the pre-processor will replace all the macros in the program by its real definition prior to the compilation process of the program.
  • 5. Applications of Macro • when assembly language programming was commonly used to write programs for digital computers • the use of macro instructions was initiated for two main purposes: • to reduce the amount of program coding that had to be written by generating several assembly language statements from one macro instruction
  • 6. MACROS PROCEDURE 1 The corresponding machine code is written every time a macro is called in a program. 1 The Corresponding m/c code is written only once in memory 2 Program takes up more memory space. 2 Program takes up comparatively less memory space. 3 No transfer of program counter. 3 Transferring of program counter is required. 4 No overhead of using stack for transferring control. 4 Overhead of using stack for transferring control. 5 Execution is fast 5 Execution is comparatively slow. 6 Assembly time is more. 6 Assembly time is comparatively less. 7 More advantageous to the programs when repeated group of instruction is too short. 7 More advantageous to the programs when repeated group of instructions is quite large. Difference between MACRO and PROCEDURE
  • 7. Difference between MACRO and Subroutine
  • 8. Design of Macro pre-processor
  • 9. Macro definition and call • A macro is a unit of specification for program generation through expansion. • A macro consists of a name, a set of formal parameters and a body of code. • The use of macro name with a set of actual parameters is replaced by code generated from its body. • The formal structure of a macro includes the following features: – Macro prototype statement: Specifies the name of the macro 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
  • 10. Macro Statements • A macro prototype statement • where [<formal parameter spec> [,...]] defines the parameter name and its kind, which are of the following form: • A macro can be called by writing the name of the macro in the mnemonic field of the assembly language called macro call • macro call Statement:
  • 11. Example of macro definition and call • Macro Definition • Macro Call Name of macro Start of definition Sequence of instructions End of definition
  • 12. Macro Expansion • Replacement of macro call by corresponding sequence of instructions is called as macro expansion • To expand a macro, the name of the macro is placed in the operation field, and no special directives are necessary. • The assembly code sequence INCR A, B, AREG is an example of the macro call • the assembler recognizes INCR as the name of the macro, expands the macro, and places a copy of the macro definition (along with the parameter substitutions) • ‘+’ sign in the preceding label field denote the expanded code
  • 13. Example MACRO INCR &A, &B, AREG MOVER REG &A ADD REG & B MOVEM REG & A MEND Macro definitionMacro call  A and B are an actual parameters  &A and &B are formal parameters Macro call Expanded code
  • 14. First Macro Example:- #include <stdio.h> #define SquareOf(x) x*x // (Macro definition) void main() { int xin=3; printf("nxin=%i",xin); printf("nSquareOf(xin)=%i",SquareOf(xin)); // (macro call) printf("nSquareOf(xin+4)=%i",SquareOf(xin+4)); printf("nSquareOf(xin+xin)=%i",SquareOf(xin+xin)); }
  • 15. Lexical Expansion and Parameter Substitution (Lexical Substitution) • Two key points concerning macro expansion are: – Expansion time control flow: This determines the order in which model statements (body) are visited during macro expansion. – Lexical Substitution: It is used to generate an assembly statement from a model statement(body).
  • 16. Four ways of specifying arguments to a macro call (Parameter Substitution) • A) Positional argument or Parameter Argument are matched with dummy arguments according to order in which they appear or compute the ordinal position of the formal parameters. • INCR A,B,C • ‘A’ replaces first dummy argument • ‘B’ replaces second dummy argument • ‘C’ replaces third dummy argument
  • 17. Example • Macro definition – MACRO – MNAME &par1, &par2, &par3 – MOVER &par1, &par2 – ADD &par1, &par3 – MOVEM &par1, &par2 – MEND • Macro call – MNAME REG1, A, B • Expanded code – MOVER REG1, A – ADD REG1, B – MOVEM REG1, A Formal Par Actual par &par1 REG1 &par2 A &par3 B
  • 18. • B) keyword arguments or parameters • This allows reference to dummy arguments by name as well as by position. • The parameter specification are useful when macros use a long list of parameters • Format: &<name_of_par>=<actual_par>
  • 19. Example • Macro definition – MACRO – MNAME &par1=, &par2=, &par3= – MOVER &par1, &par2 – ADD &par1, &par3 – MOVEM &par1, &par2 – MEND • Macro call – 1. MNAME par1=REG1, par2=A, par3=B OR – 2. MNAME par1=A, par2=REG1, par3=B • 1. Expanded code – MOVER REG1, A – ADD REG1, B OR – MOVEM REG1, A 2. Expanded code MOVER A, REG1 ADD A, B MOVEM A, REG1
  • 20. • C) keyword Default Parameters • If a parameter has the same value in most calls on a macro, this value can be specified as its default value in the macro definition itself. • If a macro call does not explicitly specify the value of the parameter, the pre-processor uses its default value; otherwise, it uses the value specified in the macro call. • Par. spec. required when the parameter takes the same value in most calls. • Format :
  • 21. Example • Macro definition – MACRO – MNAME &par1= REG1, &par2=, &par3= – MOVER &par1, &par2 – ADD &par1, &par3 – MOVEM &par1, &par2 – MEND • Macro call – 1. MNAME par2=A, par3=B OR – 2. MNAME par1=REG2, par2=A, par3=B • 1. Expanded code – MOVER REG1, A – ADD REG1, B OR – MOVEM REG1, A 2. Expanded code MOVER REG2, A ADD REG2, B MOVEM REG2, A
  • 22. • d) Mixed Parameters • A macro definition may use both positional and keyword parameters. In such a case, all • positional parameters must precede all keyword parameters in a macro call • This makes the search and association of the actual parameters to the corresponding formal parameter easier. • The parameters can be used not only in the operation field, but also in all the field of the instruction
  • 23. Example • Macro definition – MACRO – MCALC &par1=, &par2=, &par3=MULT, &par4= – &par4 – MOVER REG1, &par1 – ADD REG1, &par2 – MOVEM REG1, &par1 – MEND • Macro call – MCALC A, B, par4=loop • 1. Expanded code – +loop MOVER REG1, A – MULT REG1, B – MOVEM REG1, A
  • 24. Flow of control during macro expansion • Flow of control during expansion. – The default flow of control during macro expansion is sequential. its start with statement following the macro prototype statement and ending with the statement preceding the MEND statement. – A pre-processor statement can alter the flow of control during expansion such that some model statements are never visited during expansion is called conditional expansion. – Same statement are repeatedly visited during expansion is called loops expansion.
  • 25. Flow of control during macro expansion • The flow of control during macro expansion is implemented using a macro expansion counter (MEC) • Algorithm of macro expansion: • 1. MEC:=statement number of first statement following the prototype stmt. • 2. While statement pointed by MEC is not a MEND statement. – a. If a model statement then • i. Expand the statement • ii. MEC:=MEC+1; – b. Else (i.e. a pre-processor statement) • i. MEC:= new value specified in the statement. • 3. Exit from macro expansion.
  • 26. Nested Macro Facility • Nested Macro Definition: • Nested macro definition are those cases where the definition of one macro is contained within the definition of another • Example : • MACRO • MAC_X &par1, &par2 • MOVER REG1, &par1 – MACRO – MAC_Y &par2, REG=REG3 – ADD REG, &par2 – MOVEM REG, &par2 – MEND • PRINT &par1 • MEND
  • 27. Nested Macro Facility • Nested Macro Expansion and Call: • A model statement in a macro may constitute a call on another macro. • Such calls are known as nested macro calls • Macro containing the nested call is the outer macro and, Macro called is inner macro • They follow LIFO rule. • Thus, in structure of nested macro calls, • expansion of latest macro call (i.e inner macro) is • completed first.
  • 28. Example • MACRO – MAC_Y &par2, REG=REG3 – ADD REG, &par2 – MOVEM REG, &par2 – MEND • MACRO – MAC_X &par1, &par2 – MOVER REG1, &par1 – MAC_Y &par2, REG=REG3 • PRINT &par1 • MEND
  • 29. Nested Expansion Macro call Expansion Nested expansion MAC_X A, B + MOVER REG1, A + MOVER REG1, A +MAC_Y B, REG2 ++ADD REG2, B +PRINT A ++ MOVEM REG2, B +PRINT A
  • 30. Recursive macro call • A recursive macro is a situation where the macro expands itself. • Note: Macro definition does not contain any statement to stop any of the inner expansions and return to complete the outer ones. This lead to infinite loop. • The conditional statement is one mechanism to handle recursive call • MACRO – MAC_Y &par=A – MOVER REG1, &par – MAC_Y &par=A • MEND
  • 31. ADVANCED MACRO FACILITIES • Facilities for change of flow of control • Expansion time variables • Attributes of parameters • Advance directives – The Remove Directive – The IRP Directive – The REPEAT Directive
  • 32. For change of flow of control:- Two features are provided for this purpose 1) Expansion time Sequencing Symbol 2) Expansion time Statements ( AIF,AGO,ANOP) Sequencing Symbol( SS) SS is defined by putting it in the label field of a stmt in the macro body. Statements (syntax) AIF-> AIF ( <expression>) <Sequencing Symbol (SS)> Expression includes relational expression ( LT, NE etc.). If condition is true then control is transferred to SS. AGO-> AGO <SS>  unconditional transfer ANOP-> <SS> ANOP  used to define SS
  • 33. Example MACRO TEST &X, &Y, &Z AIF (&Y EQ &X) .ONLY MOVER AREG, &Y AGO .LAST .ONLY MOVER AREG, &Z .LAST MEND REPRESENTING SS If condition is true ( Y= X) then control will be transferred to .ONLY
  • 34. Expansion time variables EV’s are variables which can be only used during the expansion of macro calls. It may be of two types:- 1) Local EV (LCL) can be used only in one particular macro. 2) Global EV (GBL) works like global variables. Syntax:- LCL <EV specification>, ……… GBL <EV specification>, …….. Values of EV can be changed using SET (preprocessor stmt). <EV specification> SET <SET expression>
  • 35. Example of EV MACRO TESTEV LCL &A &A SET 1 LCL &A declares local variable A
  • 36. Attributes of parameters An attribute is written using the syntax:- <attribute name> ’ <formal parameter specification> T– type L – length S – size Example MACRO ME &A AIF (L’&A EQ 1) .NEXT ------ -----
  • 37. EXAMPLE MACRO TEST &X, &N, R=AREG LCL &M &M SET 0 .PQ MOVEM &R, &X+&M &M SET &M + 1 AIF (&M NE N) .PQ MEND MACRO CALL TEST S,10 REPRESENTS PARAMETER NAME REPRESENTS EXPAN. VAR. NAME REPRESENTS SEQUENCING SYMBOL NAME REPRESENTS ACTUAL PARAMETER NAME
  • 38. Advance directives • The Remove Directive: Remove Macro from MDT(Macro definition table). • The IRP Directive: (Indefinite repeat): It is used by programmer to direct the assembler to repeatedly duplicate and assemble the sequence a number of times determined by a compound parameter. • IRP example – Macro – MAC_X &P, &Q – IRP &P – ADD REG1, &P – IRP – MEND • MACRO CALL – MAX_X (A ,B, #3), H • On execution – ADD REG1, A – ADD REG1, B – ADD REG1, #3
  • 39. The REPEAT (REPT)Directive • REPEAT: ): This is another facility with assembler to duplicate and assemble the sequence a number of times during macro expansion. • Syntax: REPT <expression> – MACRO – DEF_R – LCL &P – &P SET 5 – REPT 5 – DC ‘&P’ – &P SET &P+1 – ENDM – MEND – On execution – 5,6,7,8,9,10 will be output
  • 40. Design issues of Macro preocessor • Flexible data structures and databases • Attributes of macro arguments/parameter name • Default arguments/ formal parameter value at macro definition time • Numeric values of arguments/ actual parameter value at macro call • Comments in macros
  • 41. Features of macro processor • Associating macro parameters with their arguments • Delimiting macro parameters • Directives related to arguments • Automatic label generation • Machine independent features
  • 42. Design of Macro Preprocessor
  • 43. Functions of Macro preprocessor • Macro processor will perform the following tasks- – Identifies macro definitions and calls in the program. – Determines formal parameters and their values. – Keeps track of the values of expansion time variables and sequencing symbols declared in a macro. – Handles expansion time control flow and performs expansion of model statements.
  • 44. Two pass Macro Processor • General Design Steps • Step 1: Specification of Problem:- • Step 2 Specification of databases:- • Step 3 Specification of database formats • Step 4 : Algorithm
  • 45. Specify the problem • In Pass-I the macro definitions are searched and stored in the macro definition table and the entry is made in macro name table • In 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.
  • 46. Specification of databases:- Pass 1:- • The input macro source program. • The output macro source program to be used by Pass2. • Macro-Definition Table (MDT), to store the body of macro defns. • 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-defns.
  • 47. Specification of databases:- • Pass 2:- • The copy of the input from Pass1. • The output expanded source to be given to assembler. • MDT, created by Pass1. • MNT, created by Pass1. • Macro-Definition Table Pointer (MDTP), used to indicate the next line of text to be used during macro- expansion. • Argument List Array (ALA), used to substitute macro- call arguments for the index markers in the stored macro-defns
  • 50. EXAMPLE MACRO TEST &X, &N, R=AREG LCL &M &M SET 0 .PQ MOVEM &R, &X+&M &M SET &M + 1 AIF (&M NE N) .PQ MEND MACRO CALL TEST S,10 REPRESENTS PARAMETER NAME REPRESENTS EXPAN. VAR. NAME REPRESENTS SEQUENCING SYMBOL NAME REPRESENTS ACTUAL PARAMETER NAME
  • 52. Name #PP #KP #EV MDTP KPDTP SSTP TEST 2 1 1 25 10 5 MNT 25 LCL (E,1) 26 (E,1) SET 0 27 MOVEM (P,3),(P,1)+(E,1) 28 (E,1) SET (E,1) +1 29 AIF (E,1) NE (P,2) (S,1) 30 MEND MDT
  • 53. Steps for MNT Entry Name #PP #KP #EV MDTP KPDTP SSTP TEST &X, &N, R=AREG STRUCTURE OF MNT For each positional parameter (i) Enter Parameter name in PNTAB [PNTAB_ptr] (ii)PNTAB_ptr := PNTAB_Ptr + 1; (iii)# PP := # PP + 1 : PNTAB x #pp=1 N #pp=1+1=2
  • 54. .PQ MOVEM &R, &X+&M SSNTAB a) If SS is present in SSNTAB then get index No. Else 1 Enter SS in SSNTAB, then get index No. and 2. Enter MDT_Ptr in SSTAB b) For a parameter , generate the specification ( p,#n). c) For an expansion variable,generate the specification ( E, # m). .PQ SSTAB 5 27 .PQ MOVEM &R, &X+&M MOVEM (P,3) , (P,1)+(E,1) Enter this line into MDT Steps for Model Stmt
  • 55. Argument List Array (ALA): • ALA is used during both Pass1 & Pas2 but for some what reverse functions. • During Pass1, in order to simplify later argument replacement during macro expansion, dummy arguments are replaced with positional indicators when defn is stored. Ex. # 1, # 2, # 3 etc. • The ith dummy argument on the macro-name is represented in the body by #i. • These symbols are used in conjunction with ALA prepared before expansion of a macro-call. • Symbolic dummy argument are retained on macro-name to enable the macro processor to handle argument replacement byname rather by position.
  • 57. Algorithm • 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
  • 58. MDTC 1 MNTC 1 MDTC MDTC+1 MDTC MDTC+1 Enter Macro Name and Current value of MDTC in MNT
  • 59. 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
  • 61. Pass structure of Macro assembler