Unit Iv
Unit Iv
UNIT-IV
Basic Macro Processor functions:
Macro Definition and Expansion
Macro Processor Algorithm and data structures.
Machine-independent Macro Processor features:
Concatenation of Macro Parameters
Generation of Unique Labels
Conditional Macro Expansion
Keyword Macro Parameters
Macro Processor Design Options
Recursive Macro Expansion
General Purpose Macro Processors
Macro Processing within Language Translators
Implementation examples:
MASM Macro Processor – ANSI C Macro language.
Macro Processor
• Recognize macro definitions
• Save the macro definition
• Recognize macro calls
• Expand macro calls
Source
Macro Expanded Compiler or obj
Code
(with macro)
Processor Code Assembler
Copy code -- Example
Source Expanded source
STRG MACRO .
STA DATA1 .
STB DATA2 .
{
STX DATA3 STA DATA1
MEND STB DATA2
. STX DATA3
STRG .
{
. STA DATA1
STRG STB DATA2
. STX DATA3
. .
Macro vs. Subroutine
• Macro
– the statement of expansion are generated each
time the macro are invoked
• Subroutine
– the statement in a subroutine appears only once
Parameter Substitution --
Example
Source Expanded souce
STRG MACRO &a1, &a2, &a3 .
STA &a1 .
STB &a2 .
{
STX &a3 STA DATA1
MEND STB DATA2
. STX DATA3
STRG DATA1, DATA2, DATA3 .
{
. STA DATA4
STRG DATA4, DATA5, DATA6 STB DATA5
. STX DATA6
. .
Macro Instruction
• A macro instruction (macro) is simply a notational convenience
for the programmer.
• 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.
Machine Independent
• 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.
• Macro mostly are used un assembler language
programming. However, it can also be used in high-level
programming languages such as C or C++.
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.
Macro Program Example
Macro definition
G E T L IN E
E X P A N D IN G = F A L S E
P R O C E S S L IN E
G E T L IN E EXPAND
P R O C E S S L IN E
E X P A N D IN G = T R U E
G E T L IN E
G E T L IN E
P R O C E S S L IN E
E X P A N D IN G FA LSE
TRUE
• Positional parameters
– Parameters and arguments were associated
with each other according to their positions in
the macro prototype and the macro
invocation statements.
– A certain macro instruction GENER has 10
possible parameters.
GENER MACRO &1, &2, &type, …, &channel, &10
GENER , , DIRECT, , , , , , 3
4.2.4 Keyword Macro Parameters
• Keyword parameters
– Each argument value is written with a
keyword that names the corresponding
parameter.
– Arguments may appear in any order.
GENER MACRO &1, &2, &type, …, &channel, &10