This document discusses macro processors and macro expansion. It defines macro expansion as replacing a macro call with the sequence of assembly statements defined by the macro. It describes the tasks of identifying macro calls, formal parameters, maintaining expansion variables, and organizing control flow. It also discusses nested macro calls, recursive macros, and the design of macro assemblers as having three passes - definition processing, expansion, and target code generation.
4. Macro ExpansioNMacro ExpansioN
Macro call leads to Macro Expansion.Macro call leads to Macro Expansion.
In Macro Expansion , Macro CallIn Macro Expansion , Macro Call
statement is replaced by astatement is replaced by a Sequence ofSequence of
Assembly statementsAssembly statements..
2 key notations used,,2 key notations used,,
Expansion time control flowExpansion time control flow
Lexical SubstitutionLexical Substitution
5. • AlgorithmAlgorithm::
– Initialize theInitialize the MEC (Macro Expansion counter)MEC (Macro Expansion counter)..
– Check the statement which is pointed by MEC isCheck the statement which is pointed by MEC is
notnot MENDMEND..
– If the statement is model statement.If the statement is model statement.
– Exit.Exit.
If( Statement = model Statement) thenIf( Statement = model Statement) then
Expand the statementExpand the statement
Increment MECIncrement MEC
ElseElse
MEC:=new value specified in statementMEC:=new value specified in statement
If( Statement = model Statement) thenIf( Statement = model Statement) then
Expand the statementExpand the statement
Increment MECIncrement MEC
ElseElse
MEC:=new value specified in statementMEC:=new value specified in statement
Macro ExpansioNMacro ExpansioN
6. • Tasks involvedTasks involved,,,,
– IdentifyIdentify Macro CallMacro Call
– Identify the Value OfIdentify the Value Of Formal ParameterFormal Parameter..
– MaintainMaintain Expansion time variablesExpansion time variables..
– OrganizeOrganize Expansion time control flowExpansion time control flow..
– Determining the values ofDetermining the values of sequencingsequencing
SymbolsSymbols..
– Expansion ofExpansion of model statementmodel statement..
Macro ExpansioNMacro ExpansioN
8. • Macro DefinitionMacro Definition ParameterParameter
• Macro InvocationMacro Invocation ArgumentArgument
• Body of Macro is Defined betweenBody of Macro is Defined between
MACROMACRO && MENDMEND directive statements.directive statements.
• After macro processing , the expandedAfter macro processing , the expanded
file can be used as I/P to thefile can be used as I/P to the
assembler.assembler.
Macro ExpansioNMacro ExpansioN
9. Lexical SubstitutioNLexical SubstitutioN
ConsistConsist 3 types3 types of string,,of string,,
11.Ordinary string..Ordinary string.
22.Name of Formal parameter preceded by.Name of Formal parameter preceded by
““&&”.”.
33.Name of Preprocessor Variables..Name of Preprocessor Variables.
Take stringTake string
as it is asas it is as
I/P.I/P.
Take stringTake string
as it is asas it is as
I/P.I/P.
TakeTake
ActualActual
parameterparameter
as I/P.as I/P.
TakeTake
ActualActual
parameterparameter
as I/P.as I/P.
TakeTake
ActualActual
parameterparameter
as I/P.as I/P.
TakeTake
ActualActual
parameterparameter
as I/P.as I/P.
10. Positional ParameterSPositional ParameterS
• Parameters & arguments associatedParameters & arguments associated
with each other according to theirwith each other according to their
PositioNPositioN in macro call & macroin macro call & macro
definition.definition.
• Syntax ,,Syntax ,,
• Ex,,Ex,, &&ROLLNOROLLNO
& < parameter name >& < parameter name >
16. Recursive MacroSRecursive MacroS
• Special caseSpecial case nested macros.nested macros.
• Recursive macroRecursive macro MacroMacro expandsexpands
itself.itself.
• Not good but very useful.Not good but very useful.
• ProblemProblem: recursively expands so quickly: recursively expands so quickly
overflow theoverflow the MESMES..
• SolutionSolution: mechanism required to stop: mechanism required to stop
recursion.recursion. (conditional assembly(conditional assembly))
19. Design of Macro AssembleRDesign of Macro AssembleR
• 3 passes in macro Assembler.3 passes in macro Assembler.
Pass 1:Pass 1:
– Macro definition processing.Macro definition processing.
– SYMTAB construction.SYMTAB construction.
Pass 2Pass 2::
– Macro expansion.Macro expansion.
– Memory allocation & LC processing.Memory allocation & LC processing.
– Processing of Literals.Processing of Literals.
– Intermediate Code Generation.Intermediate Code Generation.
20. pass 3:pass 3:
– Target code Generation.Target code Generation.
– Macro processorMacro processor can be added as acan be added as a
processor to an assembler.processor to an assembler.
– It is implemented inIt is implemented in pass 1pass 1 of theof the
assembler.assembler.
Design of Macro AssembleRDesign of Macro AssembleR
21. Advantages:Advantages:
– Many functions don’t have to beMany functions don’t have to be
implemented twice.implemented twice.
– Less overheadLess overhead during processing. (byduring processing. (by
removing intermediate code)removing intermediate code)
– More flexibilityMore flexibility is available to theis available to the
programmer.programmer.
Design of Macro AssembleRDesign of Macro AssembleR
22. Disadvantages:Disadvantages:
• Combined pass 1 of the assembler &Combined pass 1 of the assembler &
macro processor may be toomacro processor may be too large for alarge for a
programprogram to fit into core of someto fit into core of some
machine.machine.
• ComplexityComplexity of such program may beof such program may be
overwhelming.overwhelming.
Design of Macro AssembleRDesign of Macro AssembleR