Chapter 4 discusses the concept of macro processors, which automate the substitution of code snippets to increase efficiency in programming. It contrasts macros with subroutines, explaining their defining characteristics and various functionalities. The chapter also explores design options for macro processors, including one-pass and two-pass systems, keyword parameters, and recursive expansion.
Explores the basic functions and design options of macro processors, highlighting machine-independent features.
Defines macros, explaining their role as code shortcuts; compares macros to subroutines, emphasizing macro expansion.
Details how macros are defined, including syntax elements like MACRO and MEND, and macro parameters.
Describes macro expansion, invocation, and the handling of labels during macro expansion.
Addresses challenges with multiple macro expansions, presenting solutions like the two-pass macro processor design.
Focuses on definitions of macros for specific instruction sets: SIC and SIC/XE.
Discusses the design of a one-pass macro processor and its data structures like DEFTAB and NAMTAB.
Examines strategies for implementing nested macros and introducing counters to avoid definition conflicts.Presents algorithms for one-pass macro processors, comparing them to two-pass methods.
Discusses advanced features of macro processors: parameter concatenation and unique label generation.
Explains conditional macro expansion, including control structures and macro-time variables for dynamic generation.
Outlines macro processor options, emphasizing recursive macros, general-purpose processors, and integration with language translators.
Overview[1]
Definition of Macro
•Webster defines the word macro (derived from the Greek µακρoσ) as meaning
long, great, excessive or large.
• The word is used as a prefix in many compound technical terms, e.g.,
Macroeconomics, and Macrograph.
• We will see that a single macro directive can result in many source lines being
generated, which justifies the use of the word macro in assemblers.
3.
Overview[2]
Macro Vs Subroutine
Macro
•Section of code that the programmer
writes (defines) once, and then can use
many times.
• Completely handled by the
assembler/macro processor, at
assembly/macro processing time.
• Duplicated as many times as necessary.
Subroutine
• Section of the program that is written
once, and can be used many times by
simply calling it from any point in the
program.
• Completely handled by the hardware, at
run time.
• Stored in memory once (just one copy)
4.
Overview[3]
• In assemblylanguage programming it is often that some set or block of
statements get repeated every now.
• In this context the programmer uses the concept of macro instructions (often
called as macro) where a single line abbreviation is used for a set of line.
• For every occurrence of that single line the whole block of statements gets
expanded in the main source code.
• This gives a high level feature to assembly language that makes it more
convenient for the user to write code easily.
5.
Overview[4]
• A macroinstruction (macro) is simply a notational convenience for the programmer.
It allows the programmer to write shorthand version of a program
(module programming).
• A macro represents a commonly used group of statements in the source program.
• The macro processor replaces each macro instruction with the corresponding group of
source statements.
• This operation is called “expanding the macro”
• Using macros allows a programmer to write a shorthand version of a program.
• For example, before calling a subroutine, the contents of all registers may need to be
stored. This routine work can be done using a macro.
6.
Overview[5]
• The functionsof a macro processor essentially involve the substitution of one
group of lines for another. Normally, the processor performs no analysis of the text
it handles.
• The meaning of these statements are of no concern during macro expansion.
Therefore, the design of a macro processor generally is machine independent.
• Macros mostly are used in assembler language programming. However, it can also be
used in high-level programming languages such as C or C++.
7.
Overview[6]
• The macrodefinition consists of the following parts:
1. Macro name [ ]
2. Start of definition MACRO
3. Sequence of statements - - - - - - -
- - - - - - -
4. End of definition MEND
• Once the macro is defined then the name of macro instruction now acts as a
mnemonic in assembly language that is equivalent to sequence of the statements.
Basic Functions[1]
• Macrodefinition
• The two directive MACRO and MEND are used in macro definition.
• The macro’s name appears before the MACRO directive.
• The macro’s parameters appear after the MACRO directive.
• Each parameter begins with ‘&’
• Between MACRO and MEND is the body of the macro.
• These are the statements that will be generated as the expansion of the macro
definition.
10.
Basic Functions[2]
• Macroexpansion (or invocation)
• Give the name of the macro to be expanded and the arguments to be used in
expanding the macro.
• Each macro invocation statement will be expanded into the statements that form the
body of the macro, with arguments from the macro invocation substituted for the
parameters in the macro prototype.
• The arguments and parameters are associated with one another according to their
positions.
• The first argument corresponds to the first parameter, and so on.
11.
Retain Labels
• Thelabel on the macro invocation statement has been retained as a label on
the first statement generated in the macro expansion.
• This allows the programmer to use a macro instruction in exactly the same
way as an assembler language mnemonic.
Labels in MacroBody
Problem
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.
Solution
Do not use labels in the body of macro.
Explicitly use PC-relative addressing:
• Ex, In RDBUFF and WRBUFF macros, many program-counter
relative addressing instructions are used to avoid the uses of
labels in a macro.
• JEQ * + 11
• JLT * - 14
• It is inconvenient and error-prone.
Later on, we will present a method which allows a
programmer to use labels in a macro definition.
19.
Two-Pass Macro Processor
•Like an assembler or a loader, we can design a two-pass macro processor in
which:
• First pass: process all macro definitions, and
• Second pass: expand all macro invocation statements.
• However, such a macro processor cannot allow the body of one macro
instruction to contain definitions of other macros.
• Because all macros would have to be defined during the first pass before
any macro invocations were expanded.
Macro Containing MacroExample
• MACROS contains the definitions of RDBUFF and WRBUFF which are written in SIC
instructions.
• MACROX contains the definitions of RDBUFF and WRBUFF which are written in
SIC/XE instructions.
• 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.
• Defining MACROS or MACROX does not define RDBUFF and WRBUFF. These
definitions are processed only when an invocation of MACROS or MACROX is expanded.
23.
One-Pass Macro Processor
•A one-pass macro processor that alternate between macro definition and
macro expansion is able to handle “macro in macro”.
• However, because of the one-pass structure, the definition of a macro must
appear in the source program before any statements that invoke that macro.
• This restriction is reasonable (does not create any real inconvenience).
24.
Data Structures-- GlobalVariables
•Three main data structures involved in an one-pass macro processor:
• DEFTAB
• 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.
• NAMTAB
• Store macro names, which serves an index to DEFTAB contain pointers to the beginning and end of the definition
• ARGTAB
• Used during the expansion of macro invocations.
• When a macro invocation statement is encountered, the arguments are stored in this table according to their position
in the argument list.
25.
Data Structures
• Themacro names are entered into
NAMTAB, NAMTAB contains two
pointers to the beginning and the end
of the definition in DEFTAB.
• The third data structure is an argument
table ARGTAB, which is used during
the expansion of macro invocations.
• The arguments are stored in ARGTAB
according to their position in the
argument list.
26.
Algorithm
• Procedure DEFINE
•Called when the beginning of a macro definition is recognized. Make
appropriate entries in DEFTAB and NAMTAB.
• Procedure EXPAND
• Called to set up the argument values in ARGTAB and expand a macro
invocation statement
• Procedure GETLINE
• Get the next line to be processed
27.
Handle Macro inMacro(Nested Macro)
• When a macro definition is being entered into DEFTAB, the normal
approach is to continue until an MEND directive is reached.
• This will not work for “macro in macro” because the MEND first encountered
(for the inner macro) will terminate the whole macro definition process.
• 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 and decrease LEVEL by 1 each time a MEND
directive is read.
• A MEND terminates the whole macro definition process when LEVEL reaches 0.
• This is very much like matching left and right parentheses when scanning an arithmetic expression.
28.
Nested Macro DefinitionExample
TEST START 2000h
MACROS MACRO
CELTOFER MACRO &CEL &FER
LDA &CEL
MULT NINE
DIV FIVE
ADD THIRTYTWO
STA &FER
MEND
MEND
MACROF MACRO
CELTOFER MACRO &CEL &FER
LDAF &CEL
MULTF NINE
DIVF FIVE
ADDF THIRTYTWO
STAF &FER
MEND
MEND
Two-Pass Vs One-PassMacro Processor
Two Pass Macro Processor
• Passes:
• Pass1: Recognize macro definitions
• Pass2: Recognize macro calls
• Nested macro definitions are not
allowed.
One Pass Macro Processor
• 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
33.
Machine Independent Features
Extensionsto the basic macro processor functions
Concatenation of Macro Parameters
Generation of Unique Labels
Conditional Macro Expansion
Keyword Macro Parameters
34.
Concatenation of MacroParameters[1]
• Most macro processors allow parameters to be concatenated with other
character stings.
• A program contains one series of variables named by the symbols XA1, XA2,
XA3, …, another series named by XB1, XB2, XB3, …, etc.
• The body of the macro definition might contain a statement like:
35.
Concatenation of MacroParameters[2]
• Used when a program contains a set of series of variables.
• Suppose the parameter is named &ID, the macro body may contain a
statement:
• LDA X&ID1
• &ID is concatenated after the string “X” and before the string “1”.
LDA XA1 (&ID=A)
LDA XB1 (&ID=B)
36.
Concatenation of MacroParameters[3]
• Example
• Problem
• Ambiguous Situation
• The problem is that the end of the parameter is not marked. Thus X&ID1 may mean “X” + ID + “1” or “X” + ID1.
• Solution
• To avoid this ambiguity, a special concatenation operator -> is used to specify the end of the parameter.
• The new form becomes X&ID->1.
• Of course, -> will not appear in the macro expansion.
Generation of UniqueLabels[1]
• It is in general not possible for the body of a macro instruction to contain
labels of the usual kind.
• Leading to the use of relative addressing at the source statement level
• Only be acceptable for short jumps
• Solution:
• Allowing the creation of special types of labels within macro instructions
• Previously we see that, without special processing, if labels are used in macro
definition, we may encounter the “duplicate labels” problem if a macro is
invocated multiple time.
39.
Generation of UniqueLabels[2]
• To generate unique labels for each macro invocation, when writing macro
definition, we must begin a label with $.
• 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 will start from AA, AB, AC,…..
Conditional Macro Expansion[1]
•Most macro processors can modify the sequence of statements generated for
a macro expansion, depending on the arguments supplied in the macro
invocation.
• Part I is expanded if condition part is true, otherwise part II is expanded
• Compare operator: NE, EQ, LE, GT
43.
Conditional Macro Expansion[2]
•So far, when a macro instruction is invoked, the same sequence of
statements are used to expand the macro.
• Here, we allow conditional assembly to be used.
• Depending on the arguments supplied in the macro invocation, the sequence of
statements generated for a macro expansion can be modified.
• Conditional macro expansion can be very useful.
• It can generate code that is suitable for a particular application.
44.
Conditional Macro Expansion[3]
•In the following example, the values of &EOR and &MAXLTH parameters are used to
determine which parts of a macro definition need to be generated.
• There are some macro-time control structures introduced for doing conditional macro expansion:
• IF- ELSE-ENDIF
• WHILE-ENDW
• Macro-time variables(also called a set symbol) can also be used to store values that are used by
these macro-time control structures.
• Used to store the Boolean expression evaluation result
• A variable that starts with & but not defined in the parameter list is treated as a macro-time variable.
45.
Conditional Macro Expansion[4]
Macro-timeVariables[4.1]
• Macro-time conditional statements
• Macro processor directives:
• IF-ELSE-ENDIF
• SET
• Macro-time variables (also called a set symbol)
• Begins with “&” but is not a macro instruction parameter any symbol that begins with the
character & and is not a macro parameter
• 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 0
• Be changed with their values using SET directives
• &EORCK SET 1
Conditional Macro Expansion[7]
Macro-timeLooping Statements[4.1]
• Macro processor function
• %NITEMS: is a macro processor function that returns as its value the number of
members in an argument list.
• The execution of testing of IF/WHILE, SET,
• %NITEMS() occurs at macro expansion time
Conditional Macro Expansion[10]
ConditionalMacro Expansion Vs Conditional Jump Instructions
• The testing of Boolean expression in IF statements occurs at the time
macros are expanded.
• By the time the program is assembled, all such decisions have been
made.
• There is only one sequence of source statements during program
execution.
• In contrast, the COMPR instruction test data values during program
execution. The sequence of statements that are executed during
program execution may be different in different program executions.
52.
Conditional Macro Expansion[11]
Implementation[11.1]
•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.
• When an IF statement is encountered during the expansion of a macro, the specified
Boolean expression is evaluated.
• If the value of this expression is TRUE, the macro processor continues to process until it encounters the
next ELSE or ENDIF.
• If ELSE is encountered, then skips to ENDIF
• Otherwise, the assembler skips to ELSE and continues to process until it reaches ENDIF.
53.
Conditional Macro Expansion[12]
Implementation[11.2]
•When a WHILE statement is encountered during the expansion of a macro, the
specified Boolean expression is evaluated.
• If the value of this expression is 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.
• Otherwise:
• The macro processor skips ahead in DEFTAB until it finds the next ENDW statement and
then resumes normal macro expansion.
54.
Keyword Macro Parameters[1]
•So far, all macro instructions use positional parameters.
• If an argument is to be omitted, the macro invocation statement must contain a null argument to maintain the
correct argument positions.
• E.g.,
• If keyword parameters are used, each argument value is written with a keyword that names
the corresponding parameters.
• Arguments thus can appear in any order.
• Null arguments no longer need to be used.
•
• Keyword parameter method can make a program easier to read than the positional method.
55.
Keyword Macro Parameters[2]
•Keyword parameters
• Each argument value is written with a keyword that names the corresponding
parameter.
• Arguments may appear in any order.
• Null arguments no longer need to be used.
• E.g. GENER TYPE=DIRECT, CHANNEL=3
• It is easier to read and much less error-prone than the positional method. E.g. Fig. 4.10
Macro Processor DesignOptions
Recursive macro expansion
General-purpose macro processors
Macro processing within language translators
60.
Recursive Macro Expansion[1]
•If we want to allow a macro to be invoked in a macro definition, the already
presented macro processor implementation cannot be used.
• This is because the EXPAND routine is recursively called but the variable
used by it (e.g., EXPANDING) is not saved across these calls.
• It is easy to solve this problem if we use a programming language that
support recursive functions. (e.g., C or C++).
Recursive Macro Expansion[3]
•For easy implementation, we require that RDCHAR macro be defined before it is used in RDBUFF macro.
• This requirement is very reasonable.
63.
Recursive Macro Expansion[4]
Solutions
•Write the macro processor in a programming language that allows recursive calls. Thus, local variables will be retained.
• Most high-level language have been supported recursive calls
• The compiler would be sure that previous values of any variables declared within a procedure were saved when the procedure was
called recursively
• If you are writing in a language without recursion support, Use a stack to take care of pushing and popping local variables and
return addresses
64.
General-Purpose Macro Processors[1]
•Macro processors that do not dependent on any particular programming
language, but can be used with a variety of different languages.
Not tied to any particular language
Can be used with a variety of different languages.
65.
General-Purpose Macro Processors[2]
•Advantages
• Programmers do not need to learn many macro languages.
• Overall saving in software development cost and software maintenance effort
• Difficulties:
• Large number of details must be dealt with in a real programming language
Comment identifications ( //, /* */, …)
Grouping together terms, expressions, statements (begin_end, { }, …)
Tokens (keywords, operators)
Syntax had better be consistent with the source programming language
66.
Macro Processing withinLanguage
Translators
• The macro processors we discussed are called “Preprocessors”.
• Process macro definitions
• Expand macro invocations
• Produce an expanded version of the source program, which is then used as input to an assembler
or compiler.
• You may also combine the macro processing functions with the language translator:
• Line-by-line macro processor
• Integrated macro processor
67.
Line-by-Line Macro Processor
•Used as a sort of input routine for the assembler or compiler
• Read source program
• Process macro definitions and expand macro invocations
• Pass output lines to the assembler or compiler
• Benefits
• It avoids making an extra pass over the source program.
• Data structures required by the macro processor and the language translator can be combined
• E.g., OPTAB and NAMTAB)
• Utility subroutines can be used by both macro processor and the language translator.
• Scanning input lines Searching tables Data format conversion
• It is easier to give diagnostic messages related to the source statements (i.e., the source statement error can be
quickly identified without need to backtrack the source)
68.
Integrated Macro Processor
•An integrated macro processor can potentially make use of any information
about the source program that is extracted by the language translator.
• Benefits:
• An integrated macro processor can support macro instructions that depend upon the
context in which they occur.
• Since the Macro Processor may recognize the meaning of source language
69.
Drawbacks of Line-by-line/Integrated
•They must be specially designed and written
• To work with a particular implementation of an assembler or compiler.
• The costs of macro processor development is added to the costs of the
language translator
• Which results in a more expensive software.
• The assembler or compiler will be considerably larger and more complex.