Macros in system programing

24
Macro Macro & & Macro Processors Macro Processors

Transcript of Macros in system programing

Macro Macro & &

Macro ProcessorsMacro Processors

INDEXINDEX

1.1. Macro expansionMacro expansion2.2. Handling Nested Macro CallsHandling Nested Macro Calls3.3. Design of Macro AssemblerDesign of Macro Assembler

Macro ExpansioNMacro ExpansioN

Macro ExpansioNMacro ExpansioNMacro call leads to Macro Expansion.Macro call leads to Macro Expansion.

In Macro Expansion , Macro Call In Macro Expansion , Macro Call statement is replaced by a statement is replaced by a Sequence Sequence of Assembly statementsof Assembly statements..

2 key notations used,,2 key notations used,,Expansion time control flowExpansion time control flowLexical SubstitutionLexical Substitution

• AlgorithmAlgorithm::– Initialize the Initialize the MEC (Macro Expansion counter)MEC (Macro Expansion counter)..– Check the statement which is pointed by MEC is Check the statement which is pointed by MEC is

not not MENDMEND..– If the statement is model statement.If the statement is model statement.

– Exit.Exit.

If( Statement = model Statement) then If( Statement = model Statement) then Expand the statementExpand the statementIncrement MECIncrement MEC

ElseElseMEC:=new value specified in statementMEC:=new value specified in statement

Macro ExpansioNMacro ExpansioN

• Tasks involvedTasks involved,,,,– Identify Identify Macro CallMacro Call– Identify the Value Of Identify the Value Of Formal ParameterFormal Parameter..– Maintain Maintain Expansion time variablesExpansion time variables..– Organize Organize Expansion time control flowExpansion time control flow..– Determining the values of Determining the values of sequencing sequencing

SymbolsSymbols..– Expansion of Expansion of model statementmodel statement..

Macro ExpansioNMacro ExpansioN

MACROMACROCLRMEMCLRMEM &P,&Q,&R1=AREG&P,&Q,&R1=AREGLCCLCC &A&A

&A&A SETSET 00MOVERMOVER &R1,=‘0’&R1,=‘0’

.MORE.MORE MOVEMMOVEM &R1,&P+&A&R1,&P+&A&A&A SETSET &A+1&A+1

AIFAIF (&A NE Q).MORE(&A NE Q).MOREMENDMEND

1 CLRMEMCLRMEM A,BA,B

2

3

4

5

6

Macro ExpansioNMacro ExpansioN

• Macro Definition Macro Definition ParameterParameter• Macro Invocation Macro Invocation ArgumentArgument

• Body of Macro is Defined between Body of Macro is Defined between MACROMACRO & & MENDMEND directive statements. directive statements.

• After macro processing , the expanded After macro processing , the expanded file can be used as I/P to the assembler.file can be used as I/P to the assembler.

Macro ExpansioNMacro ExpansioN

Lexical SubstitutioNLexical SubstitutioNConsist Consist 3 types3 types of string,, of string,,

11.Ordinary string..Ordinary string.22.Name of Formal parameter preceded .Name of Formal parameter preceded

by “by “&&”.”.33.Name of Preprocessor Variables..Name of Preprocessor Variables.

Take Take string as it string as it is as I/P.is as I/P.

Take Take Actual Actual

parametparameter as er as I/P.I/P.

Take Take Actual Actual

parametparameter as er as I/P.I/P.

Positional ParameterSPositional ParameterS• Parameters & arguments associated Parameters & arguments associated

with each other according to their with each other according to their PositioNPositioN in macro call & macro in macro call & macro definition.definition.

• Syntax ,,Syntax ,,

• Ex,, Ex,, &&ROLLNOROLLNO

& < parameter name >

MACROMACROINCRINCR &MEM_VAL,&INCR_VAL,&REG&MEM_VAL,&INCR_VAL,&REGMOVERMOVER &REG,&MEM_VAL&REG,&MEM_VALADDADD &REG,&INCR_VAL&REG,&INCR_VALMOVEMMOVEM &REG,&MEM_VAL&REG,&MEM_VALMENDMEND

INCR A,B,AREGINCR A,B,AREG

Formal ParameterFormal Parameter ValueValueMEM_VALMEM_VAL AAINCR_VALINCR_VAL BB

REGREG AREGAREG

Positional ParameterSPositional ParameterS

Keyword ParameterSKeyword ParameterS• For keyword parameters,,For keyword parameters,,<Parameter name><Parameter name> Ordinary string Ordinary string<Parameter kind> <Parameter kind> String “ String “==””<formal Parameter name><formal Parameter name>< <

Ordinary Ordinary string >string >

exex,, ,, XYZ = ABCXYZ = ABCSUMUP SUMUP X,Y,A=10,B=nX,Y,A=10,B=n

Positional parameters: Positional parameters: X , YX , YKeyword parameters: Keyword parameters: A , BA , B

Handling Nested Macro Handling Nested Macro CallSCallS

Nested Macro CallSNested Macro CallS• Nested macroNested macro Macro may call Macro may call

another macro.another macro.• Called macroCalled macro Inner macro. Inner macro.

MOVERMOVER BREG,TMPBREG,TMPMOVERMOVER BREG,PBREG,PADD ADD BREG,QBREG,QMOVEMMOVEM BREG,PBREG,PMOVERMOVER BREG,TMPBREG,TMP

MACROMACROCOMPUTECOMPUTE &F,&S&F,&SMOVER MOVER BREG,TMPBREG,TMPINCR_D INCR_D &F,&S,REG=BREG&F,&S,REG=BREGMOVER MOVER BREG,TMPBREG,TMPMENDMEND

Nested Macro CallSNested Macro CallS

MOVER BREG,TMPMOVER BREG,TMPMOVERMOVER BREG,PBREG,P

COMPUTE P,Q COMPUTE P,Q INCR P,QINCR P,Q ADD ADD BREG,QBREG,QMOVEMMOVEM BREG,PBREG,P

MOVERMOVER BREG,TMP BREG,TMP

Recursive MacroSRecursive MacroS• Special caseSpecial case nested macros. nested macros.• Recursive macroRecursive macro Macro Macro expands expands

itself.itself.• Not good but very useful.Not good but very useful.• ProblemProblem: recursively expands so : recursively expands so

quickly overflow the quickly overflow the MESMES..• SolutionSolution: mechanism required to stop : mechanism required to stop

recursion. recursion. (conditional assembly(conditional assembly) )

• Recursively generate INST1.Recursively generate INST1.• No stop point.No stop point.

NOGOODNOGOOD MACROMACROINST1INST1NOGOODNOGOODINST2INST2ENDMENDM

Recursive MacroSRecursive MacroS

Design of Macro Design of Macro AssembleRAssembleR

Design of Macro Design of Macro AssembleRAssembleR

• 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.

pass 3:pass 3:– Target code Generation.Target code Generation.

– Macro processorMacro processor can be added as a can be added as a processor to an assembler.processor to an assembler.

– It is implemented in It is implemented in pass 1pass 1 of the of the assembler.assembler.

Design of Macro Design of Macro AssembleRAssembleR

Advantages:Advantages:– Many functions don’t have to be Many functions don’t have to be

implemented twice.implemented twice.– Less overheadLess overhead during processing. (by during processing. (by

removing intermediate code)removing intermediate code)– More flexibilityMore flexibility is available to the is available to the

programmer.programmer.

Design of Macro Design of Macro AssembleRAssembleR

Disadvantages:Disadvantages:• Combined pass 1 of the assembler & Combined pass 1 of the assembler &

macro processor may be too macro processor may be too large large for a programfor a program to fit into core of to fit into core of some machine.some machine.

• ComplexityComplexity of such program may be of such program may be overwhelming.overwhelming.

Design of Macro Design of Macro AssembleRAssembleR

PASS 1PASS 1

RR

SearchSearch pseudo-op Tablepseudo-op Table

SearchSearch Macro name TableMacro name Table

SearchSearch machine-op Tablemachine-op Table

Go to Go to PASS 2PASS 2Type?Type?

ProcessProcess Macro definitionMacro definition

ProcessProcessPseudo-opsPseudo-ops

ProcessProcessMachine instructionMachine instruction

Setup macro Setup macro stack frame ,etc.stack frame ,etc.

READREAD

RRRR

RRRRFound, Found, macro callsmacro calls

FoundFound

Not Not

Not Not

End End pseudo-pseudo-opop

OtherOthers s

Macro Macro pseudo-pseudo-opop

@ @ Thank Thank

U! U!