SS Macro
SS Macro
SS Macro
MACRO PROCESSORS
INTRODUCTION
⚫ A macro instruction (Macro) is a
notational convenience for the
programmer
◦ Allows the programmer to write
short hand programs (modular
programming).
⚫ The macro processor replaces each
macro instruction with its equivalent
block of instructions.
⚫ The macro processor is not concerned
with the meaning of the involved
statements during expansion.
⚫ The design of the macro processor is
generally machine independent.
BASIC MACRO PROCESSOR FUNCTIONS
⚫ Directives used during usage of Macro:
◦ Macro: Indicates begin of Macro
◦ MEND: indicates end of Macro
⚫ Prototype for Macro:
◦ Each argument starts with Name and macro
◦ Parameter list
● .
● .
● MEND
⚫ Question
◦ How does a programmer decide
to use macro calls or procedure
calls?
● From the viewpoint of a
programmer
● From the viewpoint of the CPU
EXCHANGE THE VALUES OF TWO
VARIABLES
void exchange(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
main() {
int i=1, j=3;
printf("BEFORE - %d %d\n", i, j);
exchange(i, j);
printf("AFTER - %d %d\n", i, j);
}
What’s the result?
12 LINES OF ASSEMBLY CODE
SWAP TWO VARIABLES BY MACRO
main() {
int i=1, j=3;
printf("BEFORE - %d %d\n", i, j);
swap(i,j);
printf("AFTER - %d %d\n", i, j);
}
BASIC MACRO PROCESSOR
FUNCTIONS
MAIN LDA #1
STA I
LDA #3
STA J
. Invoke a macro
LDA I
STA TEMP
LDA J
STA I
LDA TEMP
STA J
I RESW 1
J RESW 1
TEMP RESW 1
END MAIN
MACRO EXPANSION
⚫ Algorithms
⚫ Nested macros
⚫ Comparison of different macro
design
⚫ Machine-Independent macro
features
ALGORITHM
ALGORITHM
ALGORITHM
ALGORITHM
HANDLING NESTED MACRO
⚫ In DEFINE procedure
◦ When a macro definition is being
entered into
◦ DEFTAB, the normal approach is to
continue
◦ until an MEND directive is reached.
◦ This would not work for nested
macro definition because the first
MEND encountered in the inner
macro will terminate the whole
macro definition process.
HANDLING NESTED MACRO
⚫ 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.
◦ Decrease LEVEL by 1 each time a
MEND directive is read.
◦ A MEND terminates the whole
macro definition process when
LEVEL reaches 0.
◦ This process is very much like
matching left and right parentheses
when scanning an arithmetic
expression.
COMPARISON OF MACRO
PROCESSOR DESIGN
⚫ One-pass algorithm
⚫ 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
COMPARISON OF MACRO
PROCESSOR DESIGN
⚫ Two-pass algorithm
◦ Pass1: Recognize macro
definitions
◦ Pass2: Recognize macro calls
◦ Nested macro definitions are not
allowed
MACHINE-INDEPENDENT MACRO
PROCESSOR FEATURES
CONCATENATION OF MACRO
PARAMETERS
⚫ Concatenation parameters with
other character strings.
◦ Used when a program consists a
set of series of variables.
COMPARISON OF MACRO
PROCESSOR DESIGN
⚫ Ambiguity problem
◦ If &ID and &ID1 are parameters
⚫ Algorithms
⚫ Nested macros
⚫ Comparison of different macro
design
⚫ Machine-Independent macro
features
⚫ Implementation of conditional
macros
NEXT – AFTER A BREAK
⚫ Keyword Parameters
◦ Each argument is written with
the keyword that names the
corresponding parameter.
◦ Argument may appear any order.
◦ Null arguments are no longer
required to be used.
◦ It is easier, less error prone and
readable form to have keyword
parameter than positional
parameter.
KEYWORD MACRO PARAMETERS
⚫ Keyword Parameters
◦ Each argument is written with
the keyword that names the
corresponding parameter.
◦ Argument may appear any order.
◦ Null arguments are no longer
required to be used.
◦ It is easier, less error prone and
readable form to have keyword
parameter than positional
parameter.
● Ex P1=A1, P2=A2, … P20=A20
USE OF KEYWORD MACRO
PARAMETERS
USE OF KEYWORD PARAMETERS
IN MACRO
USE OF KEYWORD PARAMETERS
IN MACRO
RECURSIVE MACRO EXPANSION
RECURSIVE MACRO EXPANSION
PROBLEM OF RECURSIVE MACRO
PARAMETERS
⚫ Previous Macro Design Doesn’t take
care
⚫ of recursive macro expansion.
◦ Problems:
● When procedure EXPAND is called
recursively the invocation
argument in the ARGTAB will be
overwritten.
● The boolean variable Expand
would set to false when the inner
macro expansion is completed
without having an idea that the it
is part of another outer macro
whose expansion is still not
PROBLEM OF RECURSIVE MACRO
PARAMETERS
⚫ Previous Macro Design Doesn’t take care
⚫ of recursive macro expansion.
◦ Solutions:
● Write the macro processor in a
programming language which
automatically takes care of the
recursive calls thus retaining the local
variables.
● If written in a language without
recursion support, use a stack to take
care of pushing and popping the local
variables and return addresses thus
retaining their values.
GENERAL PURPOSE MACRO
PROCESSORS
◦ Macro processor that do not depend
on any particular language, can be
used with variety of languages.
◦ Pros
● Programmers do not need not
learn any macro language.
● Although its development costs is
little high than those for the
language specific macro processor,
but these macros does not need to
be repeated for every language.
GENERAL PURPOSE MACRO
PROCESSORS
◦ Macro processor that do not depend on any
particular language, can be used with
variety of languages.
◦ Cons
● Large number of details must be dealt
within a programming language.
● Situations in which normal macro
parameter substitution should not occur,
e.g., comments.
● Facilities for grouping together terms,
expressions, or statements
● Tokens, e.g., identifiers, constants,
operators, keywords.
● Syntax must be consistent with the
programming language.
MACRO PROCESSING WITHIN
LANGUAGE TRANSLATORS
◦ Macro processor discussed so far
are preprocessors that
● Process macro definitions
● Expand macro invocation
● Produces an expanded version of
the macro at the place of the call
and then use it by the assembler
or the compiler.
◦ Macro processing functions can be
combined with the language
translators.
● Line-by-line macro processors
● Integrated macro processors
LINE-BY-LINE MACRO PROCESSORS
◦ Used as sort of input routine the assembler or
compiler.
● Read source program.
● Handle macro definition and expand the
invocation.
● Pass output lines to the assembler or compiler.
◦ Benefits
● Avoid making an extra pass over the source
program.
● Data structures required by the translators and
the macro processor can be kept same.
● Utility subroutines by the translators and the
macros can be kept same.
● Scanning input lines, Searching tables.
INTEGRATED MACRO PROCESSORS
⚫ Example
◦ DISPLAY( I* J+ 1) ==> printf(“
EXPR = %d\ n”, I* J+ 1)
INTEGRATED MACRO PROCESSORS
◦ Recursive macro definitions or
invocations
After a macro is expanded, the macro
processor rescans the text that has been
generated, looking for more macro
definitions or invocations.
Macro cannot invoke or define itself
recursively.
Example
DISPLAY( ABSDIFF( 3, 8))
SCANS
printf(“ ABSDIFF( 3, 8)” “= %d\ n”,
ABSDIFF( 3, 8))
RESCANS
printf(“ ABSDIFF( 3, 8)” “= %d\ n”, (
(3)>( 8) ? (3) -
(8) : (8)-( 3) ))
ANSI C MACRO LANGUAGE
Conditional compilation statements
Example 1
#ifndef BUFFER_ SIZE
#define BUFFER_ SIZE 1024
#endif
Example 2
#define DEBUG 1
:
#if DEBUG == 1
printf(…) /* debugging outout */
#endif
MACRO MACRO PROCESSOR
ABSDIF J,K
MOV AX,J
SUB AX, K
JNS ??0000
NEG AX
??0000:
MACRO MACRO PROCESSOR
ABSDIF MACRO OP1, OP2, SIZE
LOCAL EXIT
IFNB <SIZE>
IFDIF <SIZE> <E>
;ERR
EXITM
ENDIF
ENDIF
MOV SIZEE&AX, OP1
SUB SIZE&AX, OP2
JNS EXIT
NEG SIZE&AX
EXIT:
ENDM
SUMMARY
⚫ Algorithms
⚫ Nested macros
⚫ Comparison of different macro
design
⚫ Machine-Independent macro
features
⚫ Implementation of conditional
macros