Presentation is loading. Please wait.

Presentation is loading. Please wait.

MACRO Processors CSCI/CMPE 3334 David Egle.

Similar presentations


Presentation on theme: "MACRO Processors CSCI/CMPE 3334 David Egle."— Presentation transcript:

1 MACRO Processors CSCI/CMPE 3334 David Egle

2 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

3 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

4 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 )

5 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

6 Macro example MASM To use the macro swap [arr+esi],[arr+4+esi]
Swap MACRO A, B push EAX mov EAX, A xchg B, EAX mov A, EAX pop EAX ENDM To use the macro swap [arr+esi],[arr+4+esi] Note that this is not legal using usual mnemonics!

7 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

8 Macro example – revisited
The macro call swap [arr+esi],[arr+4+esi] is replaced by the body of the macro with appropriate substitutions ; swap [arr+esi],[arr+4+esi] push EAX mov EAX, [arr+esi] xchg [arr+4+esi], EAX mov [arr+esi], EAX pop EAX

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

10 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)

11 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

12 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

13 Example from text

14 Algorithm EXPANDING = FALSE while OPCODE <> ‘END’ GETLINE
PROCESSLINE

15 Procedure GETLINE if EXPANDING then else
get next line of macro definition from DEFTAB substitute arguments from ARGTAB for positional notation else read next line from input file

16 Procedure PROCESSLINE
search NAMTAB for OPCODE if found then EXPAND else if OPCODE = ‘MACRO’ then DEFINE else write source line to expanded file

17 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

18 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 definition

19 Optional Macro Processor Features
Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro Parameters

20 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

21 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

22 Example Macro Definition Macro invocation and expansion SUM MACRO &ID
LDA X&ID\1 ADD X&ID\2 ADD X&ID\3 STA X&ID\S MEND Macro invocation and expansion . SUM BETA LDA XBETA1 ADD XBETA2 ADD XBETA3 STA XBETAS

23 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)

24 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

25 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

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

27 Implementation Examples
MASM Macro Processor ANSI C Macro Language ELENA Macro Processor (research tool only)

28 Exercises 4.1 #6, 7 4.2 #4 4.3 #6


Download ppt "MACRO Processors CSCI/CMPE 3334 David Egle."

Similar presentations


Ads by Google