SlideShare a Scribd company logo
UNIT – IVUNIT – IV
MACRO PROCESSORS
INTRODUCTIONINTRODUCTION
A macro instruction (Macro) is a notational
convenience for the programmer
◦ Allows the programmer to write short hand
programs (modular programming).
The macro processor replaces each macro
instruction with its equivalent block of
instructions.
The macro processor is not concerned with
the meaning of the involved statements during
expansion.
The design of the macro processor is
generally machine independent.
BASIC MACRO PROCESSOR FUNCTIONSBASIC MACRO PROCESSOR FUNCTIONS
Directives used during usage of Macro:
◦ Macro: Indicates begin of Macro
◦ MEND: indicates end of Macro
Prototype for Macro:
◦ Each argument starts with Name and macro
◦ Parameter list
 .
 .
 MEND
BODY: The statement will be generated as the
expansion of Macro
MACRO EXPANSIONMACRO EXPANSION
BASIC MACRO PROCESSORBASIC MACRO PROCESSOR
FUNCTIONSFUNCTIONS
MACRO INVOCATIONMACRO INVOCATION
A macro invocation statement (a macro call) gives
the name of the macro instruction being invoked
and the arguments to be used in expanding the
macro.
◦ macro_name p1, p2, …
Difference between macro call and procedure call
◦ Macro call: statements of the macro body are
expanded each time the macro is invoked.
◦ Procedure call: statements of the subroutine
appear only one, regardless of how many times
the subroutine is called.
MACRO INVOCATIONMACRO INVOCATION
Question
◦ How does a programmer decide to use
macro calls or procedure calls?
 From the viewpoint of a programmer
 From the viewpoint of the CPU
EXCHANGE THE VALUES OF TWOEXCHANGE THE VALUES OF TWO
VARIABLESVARIABLES
void exchange(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
main() {
int i=1, j=3;
printf("BEFORE - %d %dn", i, j);
exchange(i, j);
printf("AFTER - %d %dn", i, j);
}
What’s the result?
12 LINES OF ASSEMBLY CODE12 LINES OF ASSEMBLY CODE
SWAP TWO VARIABLES BY MACROSWAP TWO VARIABLES BY MACRO
#define swap(i,j) { int temp; temp=i; i=j;
j=temp; }
main() {
int i=1, j=3;
printf("BEFORE - %d %dn", i, j);
swap(i,j);
printf("AFTER - %d %dn", i, j);
}
BASIC MACRO PROCESSORBASIC MACRO PROCESSOR
FUNCTIONSFUNCTIONS
MAIN LDA #1
STA I
LDA #3
STA J
. Invoke a macro
LDA I
STA TEMP
LDA J
STA I
LDA TEMP
STA J
I RESW 1
J RESW 1
TEMP RESW 1
END MAIN
MACRO EXPANSIONMACRO EXPANSION
Each macro invocation statement will be
expanded into the statements that form the
body of the macro.
 Arguments from the macro invocation
are substituted for the parameters in the
macro prototype (according to their
positions).
◦ In the definition of macro: parameter
◦ In the macro invocation: argument
MACRO EXPANSIONMACRO EXPANSION
Comment lines within the macro body will
be deleted.
Macro invocation statement itself has been
included as a comment line.
The label on the macro invocation statement
has been retained as a label on the first
statement generated in the macro expansion.
◦ We can use a macro instruction in exactly
the same
◦ way as an assembler language
mnemonic.
MACRO INVOCATION: A PROGRAMMACRO INVOCATION: A PROGRAM
MACRO EXPANSION: A PROGRAMMACRO EXPANSION: A PROGRAM
MACRO EXPANSION: A PROGRAMMACRO EXPANSION: A PROGRAM
MACRO EXPANSION: A PROGRAMMACRO EXPANSION: A PROGRAM
NO LABEL IN MACRO BODYNO LABEL IN MACRO BODY
Problem of the label in the body of macro:
◦ If the same macro is expanded multiple
times at different places in the program …
◦ There will be duplicate labels, which will
be treated as errors by the assembler.
Solutions:
◦ Do not use labels in the body of macro.
◦ Explicitly use PC-relative addressing
instead.
 Ex, in RDBUFF and WRBUFF macros,
TWO-PASS MACRO PROCESSORTWO-PASS MACRO PROCESSOR
You may design a two-pass macro
processor
◦ Pass 1:
 Process all macro definitions
◦ Pass 2:
 Expand all macro invocation
statements
TWO-PASS MACRO PROCESSORTWO-PASS MACRO PROCESSOR
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 other macros.
EXAMPLE OF RECURSIVE MACROEXAMPLE OF RECURSIVE MACRO
DEFINITIONDEFINITION
MACROS (for SIC)
◦ Contains the definitions of RDBUFF
and WRBUFF written in SIC
instructions.
RECURSIVE MACRO DEFINITIONRECURSIVE MACRO DEFINITION
MACROX (for SIC/XE)
◦ Contains the definitions of RDBUFF
and WRBUFF written in SIC/XE
instructions.
MACRO DEFINITION: AN EXAMPLEMACRO DEFINITION: AN EXAMPLE
 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 PROCESSORONE-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.
DATA STRUCTURE FOR ONE-PASSDATA STRUCTURE FOR ONE-PASS
MACRO PROCESSORMACRO PROCESSOR
 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.
DATA STRUCTURE FOR ONE-PASSDATA STRUCTURE FOR ONE-PASS
MACRO PROCESSORMACRO PROCESSOR
NAMTAB
◦ Stores macro names
◦ Serves as an index to DEFTAB
◦ Pointers to the beginning and the end of
the
◦ macro definition (DEFTAB)
 ARGTAB
◦ Stores the arguments of macro invocation
◦ according to their positions in the
argument list
◦ As the macro is expanded, arguments from
◦ ARGTAB are substituted for the
corresponding
◦ parameters in the macro body.
DATA STRUCTUREDATA STRUCTURE
NEXT – AFTER A BREAKNEXT – AFTER A BREAK
Algorithms
Nested macros
Comparison of different macro design
Machine-Independent macro features
ALGORITHMALGORITHM
ALGORITHMALGORITHM
ALGORITHMALGORITHM
ALGORITHMALGORITHM
HANDLING NESTED MACROHANDLING NESTED MACRO
In DEFINE procedure
◦ When a macro definition is being entered
into
◦ DEFTAB, the normal approach is to
continue
◦ until an MEND directive is reached.
◦ This would not work for nested macro
definition because the first MEND
encountered in the inner macro will
terminate the whole macro definition
process.
HANDLING NESTED MACROHANDLING NESTED MACRO
To solve this problem, a counter LEVEL is
used to keep track of the level of macro
definitions.
◦ Increase LEVEL by 1 each time a
MACRO directive is read.
◦ Decrease LEVEL by 1 each time a MEND
directive is read.
◦ A MEND terminates the whole macro
definition process when LEVEL reaches 0.
◦ This process is very much like matching
left and right parentheses when scanning an
arithmetic expression.
COMPARISON OF MACROCOMPARISON OF MACRO
PROCESSOR DESIGNPROCESSOR 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
COMPARISON OF MACROCOMPARISON OF MACRO
PROCESSOR DESIGNPROCESSOR DESIGN
Two-pass algorithm
◦ Pass1: Recognize macro definitions
◦ Pass2: Recognize macro calls
◦ Nested macro definitions are not
allowed
MACHINE-INDEPENDENT MACROMACHINE-INDEPENDENT MACRO
PROCESSOR FEATURESPROCESSOR FEATURES
CONCATENATION OF MACROCONCATENATION OF MACRO
PARAMETERSPARAMETERS
Concatenation parameters with other
character strings.
◦ Used when a program consists a set of
series of variables.
COMPARISON OF MACROCOMPARISON OF MACRO
PROCESSOR DESIGNPROCESSOR DESIGN
 Ambiguity problem
◦ If &ID and &ID1 are parameters
Solution to this ambiguity problem
◦ Use a special concatenation operator “-
>” to specify the end of the parameter
COMPARISON OF MACROCOMPARISON OF MACRO
PROCESSOR DESIGNPROCESSOR DESIGN
GENERATING UNIQUE LABELSGENERATING UNIQUE LABELS
 Labels in the macro body may cause
“duplicate labels” problem if the macro is
invocated and expanded multiple times. Ex:
•Use of relative addressing at the source statement level is
very inconvenient, error-prone, and difficult to read.
GENERATING UNIQUE LABELSGENERATING UNIQUE LABELS
 Let the macro processor generate unique
 labels for each macro invocation and
 expansion.
◦ During macro expansion, the $ will be replaced
with
◦ $xx, where xx is a two-character alphanumeric
counter
◦ of the number of macro instructions expanded.
◦ xx=AA,AB,AC,…..
 This allows 1296 macro expansions in a
single program.
GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
Conditional assembly depends on
parameters provided
Part I is expanded if condition part is true,
otherwise part II is expanded
 Compare operator: NE, EQ, LE, GT
GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
Begins with “&” but is not a macro instruction
 parameter
Can be used to store working values during
 the macro expansion
◦ -Store the evaluation result of Boolean
expression
 -Control the macro-time conditional structures
Be initialized to a value of 0
Be set by a macro processor directive, SET
 Ex: &EORCK SET 1
 &EORCTR SET &EORCTR + 1
GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
MACRO-TIME LOOPINGMACRO-TIME LOOPING
WHILE ( cond )
……
ENDW
 Macro processor function
◦ %NITEMS: The number of members in an
argument list
 The execution of testing of IF/WHILE,
SET,
◦ - %NITEMS() occurs at macro expansion
time
USE OF MACRO-TIME LOOPINGUSE OF MACRO-TIME LOOPING
STATEMENTSTATEMENT
USE OF MACRO-TIME LOOPINGUSE OF MACRO-TIME LOOPING
STATEMENTSTATEMENT
IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL
MACRO EXPANSIONMACRO EXPANSION
IF-ELSE-ENDIF structure
◦ The macro processor must maintain a
symbol table
 -This table contains the values of all
macro-time variables used.
 - Entries in this table are made or
modified when SET statements are
processed.
 - This table is used to look up the current
value of a macro-time variable whenever
it is required.
IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL
MACRO EXPANSIONMACRO EXPANSION
When an IF 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 ELSE or ENDIF statement.
◦If ELSE is encountered, then skips to END
FALSE
◦The macro processor skips ahead in
DEFTAB until it finds the next ELSE or
ENDIF statement.
IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL
MACRO EXPANSIONMACRO EXPANSION
When an IF 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.
IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL
MACRO EXPANSIONMACRO EXPANSION
FALSE
◦ -- The macro processor skips ahead
in DEFTAB until it finds the next
ENDW statement and then resumes
normal macro expansion.
SUMMARYSUMMARY
Algorithms
Nested macros
Comparison of different macro design
Machine-Independent macro features
Implementation of conditional macros
NEXT – AFTER A BREAKNEXT – AFTER A BREAK
Keyword macro parameters
Recursive Macros
Line-by-Line Macros
Integrated Macros
USE OF MACRO-TIME LOOPINGUSE OF MACRO-TIME LOOPING
STATEMENTSTATEMENT
IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL
MACRO EXPANSIONMACRO EXPANSION
IF-ELSE-ENDIF structure
◦ The macro processor must maintain a
symbol
◦ table
 Entries in this table are made or modified
when SET statements are processed.
 This table is used to look up the current
value of a macro-time variable whenever
it is required.
 This table contains the values of all
macro-time variables used.
IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL
MACRO EXPANSIONMACRO EXPANSION
When an IF 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 ELSE or ENDIF statement.
 If ELSE is encountered, then skips to ENDIF
◦ FALSE
 The macro processor skips ahead in DEFTAB
until it finds the next
 ELSE or ENDIF statement.
IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL
MACRO EXPANSIONMACRO EXPANSION
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.
IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL
MACRO EXPANSIONMACRO EXPANSION
FALSE
◦ -- The macro processor skips ahead
in DEFTAB until it finds the next
ENDW statement and then resumes
normal macro expansion.
KEYWORD MACRO PARAMETERSKEYWORD MACRO PARAMETERS
Keyword macro parameters
◦ Positional parameters and arguments are
associated according to their positions in
the macro prototype and invocation.
◦ If an argument is to be omitted, a null
argument should be used to maintain the
proper order in macro invocation:
◦ Ex: XXX MACRO &P1, &P2, …., &P20,
….
XXX A1, A2,,,,,,,,,,…,,A20,…..
KEYWORD MACRO PARAMETERSKEYWORD MACRO PARAMETERS
It is not suitable if a macro has a large
number of parameters, and only a few of
these are given values in a typical
invocation.
KEYWORD MACRO PARAMETERSKEYWORD MACRO PARAMETERS
Keyword Parameters
◦ Each argument is written with the
keyword that names the corresponding
parameter.
◦ Argument may appear any order.
◦ Null arguments are no longer required to
be used.
◦ It is easier, less error prone and readable
form to have keyword parameter than
positional parameter.
KEYWORD MACRO PARAMETERSKEYWORD MACRO PARAMETERS
Keyword Parameters
◦ Each argument is written with the
keyword that names the corresponding
parameter.
◦ Argument may appear any order.
◦ Null arguments are no longer required to
be used.
◦ It is easier, less error prone and readable
form to have keyword parameter than
positional parameter.
 Ex P1=A1, P2=A2, … P20=A20
USE OF KEYWORD MACROUSE OF KEYWORD MACRO
PARAMETERSPARAMETERS
USE OF KEYWORD PARAMETERS INUSE OF KEYWORD PARAMETERS IN
MACROMACRO
USE OF KEYWORD PARAMETERS INUSE OF KEYWORD PARAMETERS IN
MACROMACRO
RECURSIVE MACRO EXPANSIONRECURSIVE MACRO EXPANSION
RECURSIVE MACRO EXPANSIONRECURSIVE MACRO EXPANSION
PROBLEM OF RECURSIVE MACROPROBLEM OF RECURSIVE MACRO
PARAMETERSPARAMETERS
Previous Macro Design Doesn’t take care
 of recursive macro expansion.
◦ Problems:
 When procedure EXPAND is called
recursively the invocation argument in
the ARGTAB will be overwritten.
 The boolean variable Expand would set
to false when the inner macro expansion
is completed without having an idea that
the it is part of another outer macro
whose expansion is still not completed.
PROBLEM OF RECURSIVE MACROPROBLEM OF RECURSIVE MACRO
PARAMETERSPARAMETERS
Previous Macro Design Doesn’t take care
 of recursive macro expansion.
◦ Solutions:
 Write the macro processor in a programming
language which automatically takes care of
the recursive calls thus retaining the local
variables.
 If written in a language without recursion
support, use a stack to take care of pushing
and popping the local variables and return
addresses thus retaining their values.
GENERAL PURPOSE MACROGENERAL PURPOSE MACRO
PROCESSORSPROCESSORS
◦ Macro processor that do not depend on any
particular language, can be used with
variety of languages.
◦ Pros
 Programmers do not need not learn any
macro language.
 Although its development costs is little
high than those for the language specific
macro processor, but these macros does
not need to be repeated for every
language.
GENERAL PURPOSE MACROGENERAL PURPOSE MACRO
PROCESSORSPROCESSORS
◦ Macro processor that do not depend on any
particular language, can be used with variety of
languages.
◦ Cons
 Large number of details must be dealt within a
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 must be consistent with the programming
language.
MACRO PROCESSING WITHINMACRO PROCESSING WITHIN
LANGUAGE TRANSLATORSLANGUAGE TRANSLATORS
◦ Macro processor discussed so far are
preprocessors that
 Process macro definitions
 Expand macro invocation
 Produces an expanded version of the
macro at the place of the call and then use
it by the assembler or the compiler.
◦ Macro processing functions can be
combined with the language translators.
 Line-by-line macro processors
 Integrated macro processors
LINE-BY-LINE MACROLINE-BY-LINE MACRO
PROCESSORSPROCESSORS
◦ Used as sort of input routine the assembler or compiler.
 Read source program.
 Handle macro definition and expand the invocation.
 Pass output lines to the assembler or compiler.
◦ Benefits
 Avoid making an extra pass over the source program.
 Data structures required by the translators and the macro
processor can be kept same.
 Utility subroutines by the translators and the macros can
be kept same.
 Scanning input lines, Searching tables.
INTEGRATED MACRO PROCESSORSINTEGRATED MACRO PROCESSORS
◦ An integrated macro processor can make
use of any information about the source
program that is extracted by the
language translators
◦ An integrated macro processor can
support macro instructions that depend
upon the context in which they occur.
INTEGRATED MACRO PROCESSORSINTEGRATED MACRO PROCESSORS
◦ Definitions and invocations of macros are
handled by a preprocessor, which is
generally not integrated with the rest of the
compiler.
◦ Example
 #DEFINE NULL 0
 #DEFINE EOF (-1)
 #DEFINE EQ ==
 #DEFINE ABSDIF (x, y)
{X>Y? X-Y:Y-X}
INTEGRATED MACRO PROCESSORSINTEGRATED MACRO PROCESSORS
◦ Parameter substitutions are not
performed within quoted.
◦ #define DISPLAY( EXPR) printf(“
EXPR= %d n”, EXPR)
Example
◦ DISPLAY( I* J+ 1) ==> printf(“ EXPR
= %d n”, I* J+ 1)
INTEGRATED MACRO PROCESSORSINTEGRATED MACRO PROCESSORS
◦ Recursive macro definitions or invocations
After a macro is expanded, the macro processor
rescans the text that has been generated, looking
for more macro definitions or invocations.
Macro cannot invoke or define itself recursively.
Example
DISPLAY( ABSDIFF( 3, 8))
SCANS
printf(“ ABSDIFF( 3, 8)” “= %d n”,
ABSDIFF( 3, 8))
RESCANS
printf(“ ABSDIFF( 3, 8)” “= %d n”, ( (3)>( 8) ?
(3) -
(8) : (8)-( 3) ))
ANSI C MACRO LANGUAGEANSI C MACRO LANGUAGE
Conditional compilation statements
Example 1
#ifndef BUFFER_ SIZE
#define BUFFER_ SIZE 1024
#endif
Example 2
#define DEBUG 1
:
#if DEBUG == 1
printf(…) /* debugging outout */
#endif
MACRO MACRO PROCESSORMACRO MACRO PROCESSOR
ABSDIF J,K
MOV AX,J
SUB AX, K
JNS ??0000
NEG AX
??0000:
MACRO MACRO PROCESSORMACRO MACRO PROCESSOR
ABSDIF MACRO OP1, OP2, SIZE
LOCAL EXIT
IFNB <SIZE>
IFDIF <SIZE> <E>
;ERR
EXITM
ENDIF
ENDIF
MOV SIZEE&AX, OP1
SUB SIZE&AX, OP2
JNS EXIT
NEG SIZE&AX
EXIT:
ENDM
SUMMARYSUMMARY
Algorithms
Nested macros
Comparison of different macro design
Machine-Independent macro features
Implementation of conditional macros

More Related Content

What's hot

Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assembler
Bansari Shah
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
Malek Sumaiya
 
Single Pass Assembler
Single Pass AssemblerSingle Pass Assembler
Single Pass Assembler
Satyamevjayte Haxor
 
Macro
MacroMacro
Direct linking loaders
Direct linking loadersDirect linking loaders
Direct linking loaders
Satyamevjayte Haxor
 
structured programming
structured programmingstructured programming
structured programming
Ahmad54321
 
Assembler
AssemblerAssembler
Assembler
Temesgen Molla
 
Unit 3 sp assembler
Unit 3 sp assemblerUnit 3 sp assembler
Unit 3 sp assembler
Deepmala Sharma
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit IIManoj Patil
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
Syed Hassan Ali
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Macros in system programing
Macros in system programingMacros in system programing
Macros in system programing
Brijesh__patel
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
EVOLUTION OF SYSTEM
EVOLUTION OF SYSTEM EVOLUTION OF SYSTEM
EVOLUTION OF SYSTEM Sahil Garg
 
Modular programming
Modular programmingModular programming
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
Rajkumar R
 
Unit 3
Unit 3Unit 3
Unit 3
pm_ghate
 
Loaders
LoadersLoaders

What's hot (20)

Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assembler
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
Single Pass Assembler
Single Pass AssemblerSingle Pass Assembler
Single Pass Assembler
 
Macro
MacroMacro
Macro
 
Direct linking loaders
Direct linking loadersDirect linking loaders
Direct linking loaders
 
structured programming
structured programmingstructured programming
structured programming
 
Assembler
AssemblerAssembler
Assembler
 
Unit 3 sp assembler
Unit 3 sp assemblerUnit 3 sp assembler
Unit 3 sp assembler
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Macros in system programing
Macros in system programingMacros in system programing
Macros in system programing
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
EVOLUTION OF SYSTEM
EVOLUTION OF SYSTEM EVOLUTION OF SYSTEM
EVOLUTION OF SYSTEM
 
Modular programming
Modular programmingModular programming
Modular programming
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 
Unit 3
Unit 3Unit 3
Unit 3
 
Loaders
LoadersLoaders
Loaders
 
Loaders
LoadersLoaders
Loaders
 

Viewers also liked

Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
desta_gebre
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
Jothi Lakshmi
 
Loader and Its types
Loader and Its typesLoader and Its types
Loader and Its types
Parth Dodiya
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loadersTech_MX
 
Macro
MacroMacro
Macro
Google
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
Satyamevjayte Haxor
 

Viewers also liked (6)

Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Loader and Its types
Loader and Its typesLoader and Its types
Loader and Its types
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loaders
 
Macro
MacroMacro
Macro
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
 

Similar to Ss4

handout6.pdf
handout6.pdfhandout6.pdf
handout6.pdf
ssuser700533
 
Module 5.pdf
Module 5.pdfModule 5.pdf
Module 5.pdf
SE14Darshan
 
33443223 system-software-unit-iv
33443223 system-software-unit-iv33443223 system-software-unit-iv
33443223 system-software-unit-ivShaniya Fathimuthu
 
Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1venkatam
 
Macros...presentation
Macros...presentationMacros...presentation
Macros...presentation
fika sweety
 
Handout#04
Handout#04Handout#04
Handout#04
Sunita Milind Dol
 
N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)
Selomon birhane
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
acsmadurai
 
Handout#05
Handout#05Handout#05
Handout#05
Sunita Milind Dol
 
MACRO ASSEBLER
MACRO ASSEBLERMACRO ASSEBLER
MACRO ASSEBLER
Meghaj Mallick
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
SARASWATHI S
 
SAS Macro
SAS MacroSAS Macro
SAS Macro
Sonal Shrivastav
 
SAS Macros part 1
SAS Macros part 1SAS Macros part 1
SAS Macros part 1venkatam
 
Presentation on macros and macro processor
Presentation on macros and macro processorPresentation on macros and macro processor
Presentation on macros and macro processor
Kuldeep Pathak
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
ssuser1c8ca21
 
SAS Macros part 2
SAS Macros part 2SAS Macros part 2
SAS Macros part 2venkatam
 
Benchmarking on JVM
Benchmarking on JVMBenchmarking on JVM
Benchmarking on JVM
Vladimir Tsanev
 
Unit ii-111206004636-phpapp01
Unit ii-111206004636-phpapp01Unit ii-111206004636-phpapp01
Unit ii-111206004636-phpapp01riddhi viradiya
 
BAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureBAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 Lecture
Wake Tech BAS
 

Similar to Ss4 (20)

handout6.pdf
handout6.pdfhandout6.pdf
handout6.pdf
 
Module 5.pdf
Module 5.pdfModule 5.pdf
Module 5.pdf
 
33443223 system-software-unit-iv
33443223 system-software-unit-iv33443223 system-software-unit-iv
33443223 system-software-unit-iv
 
Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1
 
Macros...presentation
Macros...presentationMacros...presentation
Macros...presentation
 
Handout#04
Handout#04Handout#04
Handout#04
 
N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
 
Handout#05
Handout#05Handout#05
Handout#05
 
MACRO ASSEBLER
MACRO ASSEBLERMACRO ASSEBLER
MACRO ASSEBLER
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 
SAS Macro
SAS MacroSAS Macro
SAS Macro
 
SAS Macros part 1
SAS Macros part 1SAS Macros part 1
SAS Macros part 1
 
Presentation on macros and macro processor
Presentation on macros and macro processorPresentation on macros and macro processor
Presentation on macros and macro processor
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
 
SAS Macros part 2
SAS Macros part 2SAS Macros part 2
SAS Macros part 2
 
Benchmarking on JVM
Benchmarking on JVMBenchmarking on JVM
Benchmarking on JVM
 
Unit ii-111206004636-phpapp01
Unit ii-111206004636-phpapp01Unit ii-111206004636-phpapp01
Unit ii-111206004636-phpapp01
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
BAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureBAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 Lecture
 

Recently uploaded

DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 

Recently uploaded (20)

DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 

Ss4

  • 1. UNIT – IVUNIT – IV MACRO PROCESSORS
  • 2. INTRODUCTIONINTRODUCTION A macro instruction (Macro) is a notational convenience for the programmer ◦ Allows the programmer to write short hand programs (modular programming). The macro processor replaces each macro instruction with its equivalent block of instructions. The macro processor is not concerned with the meaning of the involved statements during expansion. The design of the macro processor is generally machine independent.
  • 3. BASIC MACRO PROCESSOR FUNCTIONSBASIC MACRO PROCESSOR FUNCTIONS Directives used during usage of Macro: ◦ Macro: Indicates begin of Macro ◦ MEND: indicates end of Macro Prototype for Macro: ◦ Each argument starts with Name and macro ◦ Parameter list  .  .  MEND BODY: The statement will be generated as the expansion of Macro
  • 5. BASIC MACRO PROCESSORBASIC MACRO PROCESSOR FUNCTIONSFUNCTIONS
  • 6. MACRO INVOCATIONMACRO INVOCATION A macro invocation statement (a macro call) gives the name of the macro instruction being invoked and the arguments to be used in expanding the macro. ◦ macro_name p1, p2, … Difference between macro call and procedure call ◦ Macro call: statements of the macro body are expanded each time the macro is invoked. ◦ Procedure call: statements of the subroutine appear only one, regardless of how many times the subroutine is called.
  • 7. MACRO INVOCATIONMACRO INVOCATION Question ◦ How does a programmer decide to use macro calls or procedure calls?  From the viewpoint of a programmer  From the viewpoint of the CPU
  • 8. EXCHANGE THE VALUES OF TWOEXCHANGE THE VALUES OF TWO VARIABLESVARIABLES void exchange(int a, int b) { int temp; temp = a; a = b; b = temp; } main() { int i=1, j=3; printf("BEFORE - %d %dn", i, j); exchange(i, j); printf("AFTER - %d %dn", i, j); } What’s the result?
  • 9. 12 LINES OF ASSEMBLY CODE12 LINES OF ASSEMBLY CODE
  • 10. SWAP TWO VARIABLES BY MACROSWAP TWO VARIABLES BY MACRO #define swap(i,j) { int temp; temp=i; i=j; j=temp; } main() { int i=1, j=3; printf("BEFORE - %d %dn", i, j); swap(i,j); printf("AFTER - %d %dn", i, j); }
  • 11. BASIC MACRO PROCESSORBASIC MACRO PROCESSOR FUNCTIONSFUNCTIONS MAIN LDA #1 STA I LDA #3 STA J . Invoke a macro LDA I STA TEMP LDA J STA I LDA TEMP STA J I RESW 1 J RESW 1 TEMP RESW 1 END MAIN
  • 12. MACRO EXPANSIONMACRO EXPANSION Each macro invocation statement will be expanded into the statements that form the body of the macro.  Arguments from the macro invocation are substituted for the parameters in the macro prototype (according to their positions). ◦ In the definition of macro: parameter ◦ In the macro invocation: argument
  • 13. MACRO EXPANSIONMACRO EXPANSION Comment lines within the macro body will be deleted. Macro invocation statement itself has been included as a comment line. The label on the macro invocation statement has been retained as a label on the first statement generated in the macro expansion. ◦ We can use a macro instruction in exactly the same ◦ way as an assembler language mnemonic.
  • 14. MACRO INVOCATION: A PROGRAMMACRO INVOCATION: A PROGRAM
  • 15. MACRO EXPANSION: A PROGRAMMACRO EXPANSION: A PROGRAM
  • 16. MACRO EXPANSION: A PROGRAMMACRO EXPANSION: A PROGRAM
  • 17. MACRO EXPANSION: A PROGRAMMACRO EXPANSION: A PROGRAM
  • 18. NO LABEL IN MACRO BODYNO LABEL IN MACRO BODY Problem of the label in the body of macro: ◦ If the same macro is expanded multiple times at different places in the program … ◦ There will be duplicate labels, which will be treated as errors by the assembler. Solutions: ◦ Do not use labels in the body of macro. ◦ Explicitly use PC-relative addressing instead.  Ex, in RDBUFF and WRBUFF macros,
  • 19. TWO-PASS MACRO PROCESSORTWO-PASS MACRO PROCESSOR You may design a two-pass macro processor ◦ Pass 1:  Process all macro definitions ◦ Pass 2:  Expand all macro invocation statements
  • 20. TWO-PASS MACRO PROCESSORTWO-PASS MACRO PROCESSOR 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 other macros.
  • 21. EXAMPLE OF RECURSIVE MACROEXAMPLE OF RECURSIVE MACRO DEFINITIONDEFINITION MACROS (for SIC) ◦ Contains the definitions of RDBUFF and WRBUFF written in SIC instructions.
  • 22. RECURSIVE MACRO DEFINITIONRECURSIVE MACRO DEFINITION MACROX (for SIC/XE) ◦ Contains the definitions of RDBUFF and WRBUFF written in SIC/XE instructions.
  • 23. MACRO DEFINITION: AN EXAMPLEMACRO DEFINITION: AN EXAMPLE  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.
  • 24. ONE-PASS MACRO PROCESSORONE-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.
  • 25. DATA STRUCTURE FOR ONE-PASSDATA STRUCTURE FOR ONE-PASS MACRO PROCESSORMACRO PROCESSOR  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.
  • 26. DATA STRUCTURE FOR ONE-PASSDATA STRUCTURE FOR ONE-PASS MACRO PROCESSORMACRO PROCESSOR NAMTAB ◦ Stores macro names ◦ Serves as an index to DEFTAB ◦ Pointers to the beginning and the end of the ◦ macro definition (DEFTAB)  ARGTAB ◦ Stores the arguments of macro invocation ◦ according to their positions in the argument list ◦ As the macro is expanded, arguments from ◦ ARGTAB are substituted for the corresponding ◦ parameters in the macro body.
  • 28. NEXT – AFTER A BREAKNEXT – AFTER A BREAK Algorithms Nested macros Comparison of different macro design Machine-Independent macro features
  • 33. HANDLING NESTED MACROHANDLING NESTED MACRO In DEFINE procedure ◦ When a macro definition is being entered into ◦ DEFTAB, the normal approach is to continue ◦ until an MEND directive is reached. ◦ This would not work for nested macro definition because the first MEND encountered in the inner macro will terminate the whole macro definition process.
  • 34. HANDLING NESTED MACROHANDLING NESTED MACRO To solve this problem, a counter LEVEL is used to keep track of the level of macro definitions. ◦ Increase LEVEL by 1 each time a MACRO directive is read. ◦ Decrease LEVEL by 1 each time a MEND directive is read. ◦ A MEND terminates the whole macro definition process when LEVEL reaches 0. ◦ This process is very much like matching left and right parentheses when scanning an arithmetic expression.
  • 35. COMPARISON OF MACROCOMPARISON OF MACRO PROCESSOR DESIGNPROCESSOR 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
  • 36. COMPARISON OF MACROCOMPARISON OF MACRO PROCESSOR DESIGNPROCESSOR DESIGN Two-pass algorithm ◦ Pass1: Recognize macro definitions ◦ Pass2: Recognize macro calls ◦ Nested macro definitions are not allowed
  • 38. CONCATENATION OF MACROCONCATENATION OF MACRO PARAMETERSPARAMETERS Concatenation parameters with other character strings. ◦ Used when a program consists a set of series of variables.
  • 39. COMPARISON OF MACROCOMPARISON OF MACRO PROCESSOR DESIGNPROCESSOR DESIGN  Ambiguity problem ◦ If &ID and &ID1 are parameters Solution to this ambiguity problem ◦ Use a special concatenation operator “- >” to specify the end of the parameter
  • 40. COMPARISON OF MACROCOMPARISON OF MACRO PROCESSOR DESIGNPROCESSOR DESIGN
  • 41. GENERATING UNIQUE LABELSGENERATING UNIQUE LABELS  Labels in the macro body may cause “duplicate labels” problem if the macro is invocated and expanded multiple times. Ex: •Use of relative addressing at the source statement level is very inconvenient, error-prone, and difficult to read.
  • 42. GENERATING UNIQUE LABELSGENERATING UNIQUE LABELS  Let the macro processor generate unique  labels for each macro invocation and  expansion. ◦ During macro expansion, the $ will be replaced with ◦ $xx, where xx is a two-character alphanumeric counter ◦ of the number of macro instructions expanded. ◦ xx=AA,AB,AC,…..  This allows 1296 macro expansions in a single program.
  • 43. GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
  • 44. GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
  • 45. GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS Conditional assembly depends on parameters provided Part I is expanded if condition part is true, otherwise part II is expanded  Compare operator: NE, EQ, LE, GT
  • 46. GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS Begins with “&” but is not a macro instruction  parameter Can be used to store working values during  the macro expansion ◦ -Store the evaluation result of Boolean expression  -Control the macro-time conditional structures Be initialized to a value of 0 Be set by a macro processor directive, SET  Ex: &EORCK SET 1  &EORCTR SET &EORCTR + 1
  • 47. GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
  • 48. GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
  • 49. GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
  • 50. GENERATION OF UNIQUE LABELSGENERATION OF UNIQUE LABELS
  • 51. MACRO-TIME LOOPINGMACRO-TIME LOOPING WHILE ( cond ) …… ENDW  Macro processor function ◦ %NITEMS: The number of members in an argument list  The execution of testing of IF/WHILE, SET, ◦ - %NITEMS() occurs at macro expansion time
  • 52. USE OF MACRO-TIME LOOPINGUSE OF MACRO-TIME LOOPING STATEMENTSTATEMENT
  • 53. USE OF MACRO-TIME LOOPINGUSE OF MACRO-TIME LOOPING STATEMENTSTATEMENT
  • 54. IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL MACRO EXPANSIONMACRO EXPANSION IF-ELSE-ENDIF structure ◦ The macro processor must maintain a symbol table  -This table contains the values of all macro-time variables used.  - Entries in this table are made or modified when SET statements are processed.  - This table is used to look up the current value of a macro-time variable whenever it is required.
  • 55. IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL MACRO EXPANSIONMACRO EXPANSION When an IF 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 ELSE or ENDIF statement. ◦If ELSE is encountered, then skips to END FALSE ◦The macro processor skips ahead in DEFTAB until it finds the next ELSE or ENDIF statement.
  • 56. IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL MACRO EXPANSIONMACRO EXPANSION When an IF 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.
  • 57. IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL MACRO EXPANSIONMACRO EXPANSION FALSE ◦ -- The macro processor skips ahead in DEFTAB until it finds the next ENDW statement and then resumes normal macro expansion.
  • 58. SUMMARYSUMMARY Algorithms Nested macros Comparison of different macro design Machine-Independent macro features Implementation of conditional macros
  • 59. NEXT – AFTER A BREAKNEXT – AFTER A BREAK Keyword macro parameters Recursive Macros Line-by-Line Macros Integrated Macros
  • 60. USE OF MACRO-TIME LOOPINGUSE OF MACRO-TIME LOOPING STATEMENTSTATEMENT
  • 61. IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL MACRO EXPANSIONMACRO EXPANSION IF-ELSE-ENDIF structure ◦ The macro processor must maintain a symbol ◦ table  Entries in this table are made or modified when SET statements are processed.  This table is used to look up the current value of a macro-time variable whenever it is required.  This table contains the values of all macro-time variables used.
  • 62. IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL MACRO EXPANSIONMACRO EXPANSION When an IF 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 ELSE or ENDIF statement.  If ELSE is encountered, then skips to ENDIF ◦ FALSE  The macro processor skips ahead in DEFTAB until it finds the next  ELSE or ENDIF statement.
  • 63. IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL MACRO EXPANSIONMACRO EXPANSION 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.
  • 64. IMPLEMENTATION-CONDITIONALIMPLEMENTATION-CONDITIONAL MACRO EXPANSIONMACRO EXPANSION FALSE ◦ -- The macro processor skips ahead in DEFTAB until it finds the next ENDW statement and then resumes normal macro expansion.
  • 65. KEYWORD MACRO PARAMETERSKEYWORD MACRO PARAMETERS Keyword macro parameters ◦ Positional parameters and arguments are associated according to their positions in the macro prototype and invocation. ◦ If an argument is to be omitted, a null argument should be used to maintain the proper order in macro invocation: ◦ Ex: XXX MACRO &P1, &P2, …., &P20, …. XXX A1, A2,,,,,,,,,,…,,A20,…..
  • 66. KEYWORD MACRO PARAMETERSKEYWORD MACRO PARAMETERS It is not suitable if a macro has a large number of parameters, and only a few of these are given values in a typical invocation.
  • 67. KEYWORD MACRO PARAMETERSKEYWORD MACRO PARAMETERS Keyword Parameters ◦ Each argument is written with the keyword that names the corresponding parameter. ◦ Argument may appear any order. ◦ Null arguments are no longer required to be used. ◦ It is easier, less error prone and readable form to have keyword parameter than positional parameter.
  • 68. KEYWORD MACRO PARAMETERSKEYWORD MACRO PARAMETERS Keyword Parameters ◦ Each argument is written with the keyword that names the corresponding parameter. ◦ Argument may appear any order. ◦ Null arguments are no longer required to be used. ◦ It is easier, less error prone and readable form to have keyword parameter than positional parameter.  Ex P1=A1, P2=A2, … P20=A20
  • 69. USE OF KEYWORD MACROUSE OF KEYWORD MACRO PARAMETERSPARAMETERS
  • 70. USE OF KEYWORD PARAMETERS INUSE OF KEYWORD PARAMETERS IN MACROMACRO
  • 71. USE OF KEYWORD PARAMETERS INUSE OF KEYWORD PARAMETERS IN MACROMACRO
  • 74. PROBLEM OF RECURSIVE MACROPROBLEM OF RECURSIVE MACRO PARAMETERSPARAMETERS Previous Macro Design Doesn’t take care  of recursive macro expansion. ◦ Problems:  When procedure EXPAND is called recursively the invocation argument in the ARGTAB will be overwritten.  The boolean variable Expand would set to false when the inner macro expansion is completed without having an idea that the it is part of another outer macro whose expansion is still not completed.
  • 75. PROBLEM OF RECURSIVE MACROPROBLEM OF RECURSIVE MACRO PARAMETERSPARAMETERS Previous Macro Design Doesn’t take care  of recursive macro expansion. ◦ Solutions:  Write the macro processor in a programming language which automatically takes care of the recursive calls thus retaining the local variables.  If written in a language without recursion support, use a stack to take care of pushing and popping the local variables and return addresses thus retaining their values.
  • 76. GENERAL PURPOSE MACROGENERAL PURPOSE MACRO PROCESSORSPROCESSORS ◦ Macro processor that do not depend on any particular language, can be used with variety of languages. ◦ Pros  Programmers do not need not learn any macro language.  Although its development costs is little high than those for the language specific macro processor, but these macros does not need to be repeated for every language.
  • 77. GENERAL PURPOSE MACROGENERAL PURPOSE MACRO PROCESSORSPROCESSORS ◦ Macro processor that do not depend on any particular language, can be used with variety of languages. ◦ Cons  Large number of details must be dealt within a 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 must be consistent with the programming language.
  • 78. MACRO PROCESSING WITHINMACRO PROCESSING WITHIN LANGUAGE TRANSLATORSLANGUAGE TRANSLATORS ◦ Macro processor discussed so far are preprocessors that  Process macro definitions  Expand macro invocation  Produces an expanded version of the macro at the place of the call and then use it by the assembler or the compiler. ◦ Macro processing functions can be combined with the language translators.  Line-by-line macro processors  Integrated macro processors
  • 79. LINE-BY-LINE MACROLINE-BY-LINE MACRO PROCESSORSPROCESSORS ◦ Used as sort of input routine the assembler or compiler.  Read source program.  Handle macro definition and expand the invocation.  Pass output lines to the assembler or compiler. ◦ Benefits  Avoid making an extra pass over the source program.  Data structures required by the translators and the macro processor can be kept same.  Utility subroutines by the translators and the macros can be kept same.  Scanning input lines, Searching tables.
  • 80. INTEGRATED MACRO PROCESSORSINTEGRATED MACRO PROCESSORS ◦ An integrated macro processor can make use of any information about the source program that is extracted by the language translators ◦ An integrated macro processor can support macro instructions that depend upon the context in which they occur.
  • 81. INTEGRATED MACRO PROCESSORSINTEGRATED MACRO PROCESSORS ◦ Definitions and invocations of macros are handled by a preprocessor, which is generally not integrated with the rest of the compiler. ◦ Example  #DEFINE NULL 0  #DEFINE EOF (-1)  #DEFINE EQ ==  #DEFINE ABSDIF (x, y) {X>Y? X-Y:Y-X}
  • 82. INTEGRATED MACRO PROCESSORSINTEGRATED MACRO PROCESSORS ◦ Parameter substitutions are not performed within quoted. ◦ #define DISPLAY( EXPR) printf(“ EXPR= %d n”, EXPR) Example ◦ DISPLAY( I* J+ 1) ==> printf(“ EXPR = %d n”, I* J+ 1)
  • 83. INTEGRATED MACRO PROCESSORSINTEGRATED MACRO PROCESSORS ◦ Recursive macro definitions or invocations After a macro is expanded, the macro processor rescans the text that has been generated, looking for more macro definitions or invocations. Macro cannot invoke or define itself recursively. Example DISPLAY( ABSDIFF( 3, 8)) SCANS printf(“ ABSDIFF( 3, 8)” “= %d n”, ABSDIFF( 3, 8)) RESCANS printf(“ ABSDIFF( 3, 8)” “= %d n”, ( (3)>( 8) ? (3) - (8) : (8)-( 3) ))
  • 84. ANSI C MACRO LANGUAGEANSI C MACRO LANGUAGE Conditional compilation statements Example 1 #ifndef BUFFER_ SIZE #define BUFFER_ SIZE 1024 #endif Example 2 #define DEBUG 1 : #if DEBUG == 1 printf(…) /* debugging outout */ #endif
  • 85. MACRO MACRO PROCESSORMACRO MACRO PROCESSOR ABSDIF J,K MOV AX,J SUB AX, K JNS ??0000 NEG AX ??0000:
  • 86. MACRO MACRO PROCESSORMACRO MACRO PROCESSOR ABSDIF MACRO OP1, OP2, SIZE LOCAL EXIT IFNB <SIZE> IFDIF <SIZE> <E> ;ERR EXITM ENDIF ENDIF MOV SIZEE&AX, OP1 SUB SIZE&AX, OP2 JNS EXIT NEG SIZE&AX EXIT: ENDM
  • 87. SUMMARYSUMMARY Algorithms Nested macros Comparison of different macro design Machine-Independent macro features Implementation of conditional macros