Module 5
Macro Processor
Overview
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.
Overview
Macro Vs Subroutine
Macro Subroutine
• Section of code that the programmer • Section of the program that is written
writes (defines) once, and then can use once, and can be used many times by
many times.
simply calling it from any point in the
program.
• Completely handled by the
• Completely handled by the hardware, at
assembler/macro processor, at
assembly/macro processing time. run time.
• Duplicated as many times as necessary. • Stored in memory once (just one copy)
Overview
• In assembly language programming it is often that some set or block of
statements get repeated every now.
• In this context the programmer uses the concept of macroinstructions (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.
Overview
• A macro instruction (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 groupof statements in the source program.
• The macro processor replaces each macro instruction with the corresponding group of
source statements.
• This operation is called “expandingthe 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.
Overview
• The functions of 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++.
Overview
• The macro definition 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 tosequence of the statements.
Overview
Source Code Macro Expanded Compiler or Obj
(with macro) Processor Code Assembler
Basic Functions
• Macro definition
• 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.
Basic Functions
• Macro expansion (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.
Retain Labels
• The label 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.
Macro Program Example
Macro Program Example
Macro Program Example
Expanded Macro Example
Expanded Macro Example
Expanded Macro Example
Labels in Macro Body
Problem Solution
▪ If the same macro is expanded multiple times at ▪ Do not use labels in the body of macro.
different places in the program. ▪ Explicitly use PC-relative addressing:
•
▪ There will be duplicate labels, which will be Ex, In RDBUFF and WRBUFF macros, many program-counter
relative addressing instructions are used to avoid the uses of
labels in a macro.
treated as errors by the assembler.
• JEQ * + 11
• JLT * - 14
• It is inconvenient anderror-prone.
▪ Later on, we will present a method which allows a
programmer to use labels in a macro definition.
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
• Secondpass: 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.
Macros(for SIC)
Contains the definitions of RDBUFF and WRBUFF written in SIC instructions.
Macrox(for SIC/XE)
Contains the definitions of RDBUFF and WRBUFF written in SIC/XE instructions.
Macro Containing Macro Example
• 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.
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).
Data Structures-- Global Variables
• Three main data structures involved in an one-pass macro processor:
• DEFTAB
• Stores the macro definition including macroprototype and macrobody.
• 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.
Data Structures
• The macro 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.
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
Handle Macro in Macro(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.
Nested Macro Definition Example
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
Algorithm for One Pass Macro Processor
Algorithm for One Pass Macro Processor
Algorithm for One Pass Macro Processor
Two-Pass Vs One-Pass Macro Processor
Two Pass MacroProcessor One Pass MacroProcessor
• Passes: • Every macro must be defined before it
is called
• Pass1: Recognize macro definitions
• One-pass processor can alternate
• Pass2: Recognize macro calls between macro definition and macro
expansion
• Nested macro definitions are not • Nested macro definitions are allowed
allowed. but nested calls are not