Macro Processors ◻ Another programmer convenience ◻ A macro (instruction) represents a commonly used group of statements in the source program ◻ A macro processor replaces (expands) each macro instruction with the corresponding group of source language statements ◻ After macro processing, the expanded file is used as input to the assembler Macro Processors ◻ Example SAVEREGS macro saves all registers before calling a subprogram LOADREGS macro reloads the registers after returning from the subprogram ◻ Essentially involve substitution of a large group of characters for a much smaller group string substitution Macro Definition ◻ The definition involves header: includes the directive plus the macro name and parameters body: the statements which make up the macro end: usually a directive indicating the end of the definition ◻ Note that macros are usually used in assembly languages, but can be found in several high level languages (such as C and C++ using #define ) Macro Definition and Usage ◻ In SIC two new assembler directives are used MACRO and MEND the label field is the name of the macro entries in the operand field identify the parameters ◻ each parameter begins with the char & ◻ a parameter is substituted during macro expansion
◻ A macro call (invocation) statement gives the name
of the macro and the arguments Macro example ◻ MASM To use the macro ◻
Swap MACRO A, B swap [arr+esi],[arr+4+esi]
push EAX mov EAX, A ◻ Note that this is not legal xchg B, EAX using usual mnemonics! mov A, EAX pop EAX ENDM Macro Expansion ◻ When a macro is used macro processor looks up macro definition the definition is inserted in the code, with the given parameters substituted for the formal parameters ◻ Note that only substitution is performed – no evaluation of statements is done acts like a text editor Macro example – revisited ◻ The macro call ; swap [arr+esi],[arr+4+esi] push EAX mov EAX, [arr+esi] swap [arr+esi],[arr+4+esi] xchg [arr+4+esi], EAX mov [arr+esi], EAX ◻ is replaced by the body of pop EAX the macro with appropriate substitutions Macros vs Functions (subprograms) ◻ Parameters for macros are substituted in – must be passed to functions ◻ Function values must be returned – in a macro they are already in the correct place ◻ Code for macros is inserted inline each time they are invoked – the code for a function appears only once (where it is defined) ◻ Which you should use depends on how it will be used. Macro design ◻ Two pass macro processor The first pass: macro definitions are processed The second pass: macro invocation statements are expanded doesn’t allow the body of a macro instruction to contain definitions of other macros ■ this is useful if you want certain macros to be defined one way sometimes, and a different way at other times (e.g., one set for SIC and another for SIC/XE) Macro design – 2 ◻ Usually one-pass macros are used alternates between macro definition and expansion as needed requires that the definition of a macro appears before the macro is used
◻ This is the form discussed in this chapter
Data Structures Required ◻ DEFTAB: a definition table that stores the macro definitions comment lines are not entered references to parameters are converted to a positional notation ◻ NAMTAB: is an index to DEFTAB that contains the macro names and pointers to the beginning and end of the definition in DEFTAB ◻ ARGTAB: stores the arguments when a macro invocation statement is encountered Example from text Algorithm EXPANDING = FALSE while OPCODE <> ‘END’ GETLINE PROCESSLINE Procedure GETLINE if EXPANDING then get next line of macro definition from DEFTAB substitute arguments from ARGTAB for positional notation else read next line from input file Procedure PROCESSLINE search NAMTAB for OPCODE if found then EXPAND else if OPCODE = ‘MACRO’ then DEFINE else write source line to expanded file Procedure EXPAND EXPANDING = TRUE get first line of macro definition from DEFTAB set up arguments from macro invocation in ARGTAB write macro invocation to expanded file as a comment while not end of macro definition do GETLINE PROCESSLINE EXPANDING := FALSE Procedure DEFINE enter macro name into NAMTAB enter macro prototype into DEFTAB LEVEL := 1 while LEVEL > 0 do GETLINE if this is not a comment line then substitute positional notation for parameters enter line into DEFTAB if OPCODE = ‘MACRO’ then LEVEL := LEVEL + 1 else if OPCODE = ‘MEND’ then LEVEL := LEVEL –1 store in NAMTAB pointers to beginning and end of Optional Macro Processor Features ◻ Concatenation of Macro Parameters ◻ Generation of Unique Labels ◻ Conditional Macro Expansion ◻ Keyword Macro Parameters Concatenation of parameters ◻ Allows parameters to be concatenated with other character strings ◻ Example: a program has similar processing to a series of variables ■ XA1, XA2, XA3, … ■ XB1, XB2, XB3, … a macro is defined for such processing ■ a macro parameter &ID is used to construct the symbols ■ a macro invocation specify the series of variables (A, B, etc.) ■ A statement in the macro definition body would look like LDA X&ID1 Concatenation of parameters – 2 ◻ The symbol ‘&’ identifies the beginning of a parameter ◻ Problem: the end of the parameter is not marked ambiguous resolution if the macro definition contains &ID and &ID1 as parameters ◻ Solution: use special concatenation operator LDA X&ID\1 Example ◻ Macro Definition ◻ Macro invocation and expansion SUM MACRO &ID LDA X&ID\1 . SUM BETA ADD X&ID\2 LDA XBETA1 ADD X&ID\3 ADD XBETA2 STA X&ID\S ADD XBETA3 MEND STA XBETAS Generation of Unique Labels ◻ The usual use of labels in the body of macro definition may lead to duplicate labels If a macro definition includes a label, and is invoked twice, then the label would be defined twice – not good Could write the macros using relative operands (e.g. JEQ *-3) such notation is inconvenient, error-prone, and difficult to read ◻ A technique for unique label generation within a macro expansion can be used require that labels begin with a special symbol, ‘$’ when a macro is expanded, each label is modified by replacing ‘$’ by ‘$xx’ xx is alphanumeric counter of the number of macro instructions expanded (AA, AB, AC, etc. : allowing 36 x 36 = 1296 macro expansions) Conditional Macro Expansion ◻ Conditional macro expansion allows the modification the sequence of statements generated for a macro expansion depending on the arguments ◻ Uses macro-time conditional structure IF-ELSE-ENDIF ◻ Macro- time looping statement WHILE- ENDW repeat statements as long as a condition is true the condition contains macro-time variables Macro Time Variables ◻ Another macro processor directive SET is used to assign value to a macro-time variable (set symbol) a set symbol begins with & and is not a parameter initialized to 0 Keyword Macro Parameters ◻ Positional Parameters: parameters and arguments are associated according to their positions not suitable when the macro has large number of parameters, most of them have default values; the macro invocation specifies only the changes. e.g. GENER ,,DIRECT,,,,,,3. ◻ Keyword Parameters: each argument value is written with a keyword that names the corresponding parameter. e.g. GENER TYPE=DIRECT, CHANNEL= 3. Implementation Examples ◻ MASM Macro Processor ◻ ANSI C Macro Language ◻ ELENA Macro Processor (research tool only) Exercises ◻ 4.1 #6, 7 ◻ 4.2 #4 ◻ 4.3 #6