System Software: Dr. Manish Khare Macro Processors
System Software: Dr. Manish Khare Macro Processors
Slide 2
➢ This involves saving a lot of context information (such as values of arguments
passed, local variables, CPU registers, return address, etc.) into the stack.
➢ The context is to be restored when control returns after executing the
subprogram.
➢ Microprocessors work by simple code substitution. Moreover, the substitution
takes place at the time of compiling/assembling the program.
➢ Each call to the macro is replaced by the body of the macro directly with the
parameters substituted by the arguments passed.
➢ Thus, once the expansion has taken place, there is no necessity to do context
switching during execution.
➢ This saves a lot of time, particularly if the macro is called quite often.
Slide 3
➢ To decide upon whether a code fragment be realized as a subprogram or as a
macro, the following points are to be noted.
➢ 1. Size: The size of the code fragment is an important parameter. In the case of
a macro, each call is replaced by the code fragment. Thus, if the size is
significantly high, with all these substitutions, the compiled/assembled
program may be quite big, which may result in slower execution of the
program. On the other hand, a small fragment may better be implemented as
macro, since substitution of it will not increase the code size significantly. At
the same time, the saving in context switching will result in faster execution of
the program.
Slide 4
➢ 2. Number of parameters: A large number of parameters in a subprogram will
necessitate creating all of them in the stack at the time of its invocation. This will
require large stack-area and may slow down execution.
➢ 3. Debugging: Debugging may be easier with a subprogram since the execution may
be suspended at the entry to the subprogram. But, in the case of macros, the code
substitution will change the source line numbers and also setting breakpoints at each of
the substituted code fragments become cumbersome.
➢ 4. Recursion: It cannot be implemented with a macro because the recursive calls are to
be executed at the run-time and this is not possible at compile-time.
➢ 5. Parameter passing: Since in the macro expansion, each reference to a parameter
within the body is substituted by the argument passed from the corresponding macro
call, the only type of parameter passing strategy that can be supported is call-by-name.
On the other hand, subprograms in most of the programming languages support call-
by-value and/or call-by-reference strategies.
Slide 5
Macro
➢ A macro (or macro instruction)
◼ It is simply a notational convenience for the programmer.
◼ It allows the programmer to write shorthand version of a program
◼ It represents a commonly used group of statements in the source program.
➢ For example:
◼ Suppose a program needs to add two numbers frequently. This requires a sequence
of instructions. We can define and use a macro called SUM, to represent this
sequence of instructions.
◼ SUM MACRO &X,&Y
◼ LDA &X
◼ MOV B
◼ LDA &Y
◼ ADD B
◼ MEND
Slide 6
Macro Preprocessor
➢ The macro pre-processor(or macro processor) is a system software which replaces each
macro instruction with the corresponding group of source language statements. This
operation is called expanding the macro.
➢ It does not concern the meaning of the involved statements during macro expansion.
➢ The design of a macro processor generally is machine independent.
Slide 7
➢ Three examples of actual macro processors:
◼ A macro processor designed for use by assembler language programmers
◼ Used with a high-level programming language
◼ General-purpose macro processor, which is not tied to any particular language
Slide 8
➢ C uses a macro preprocessor to support language extensions, such as named constants,
expressions, and file inclusion.
Slide 9
Classification of MACROS
➢ Lexical expansion
◼ Lexical expansion implies replacement of a character string by another
character string during program generation.
◼ Lexical expansion is to replace occurrences of formal parameters by
corresponding actual parameters.
➢ Semantic expansion
◼ Semantic expansion implies generation of instructions tailored to the
requirements of a specific usage.
◼ Semantic expansion is characterized by the fact that different uses of a
macro can lead to codes which differ in the number, sequence and
opcodes of instructions.
◼ Eg: Generation of type specific instructions for manipulation of byte
and word operands.
Slide 10
Basic Macro Processor Functions
➢ The fundamental functions common to all macro processors are: ( Code to remember -
DIE )
◼ Macro Definition
◼ Macro Invocation
◼ Macro Expansion
Slide 11
Macro Definition
➢ Macro definitions are typically located at the start of a program.
Slide 12
➢ A macro prototype statement declares the name of a macro and its parameters. It has
following format:
macroname MACRO parameters
➢ where macroname indicates the name of macro, MACRO indicates the beginning of
macro definition and parameters indicates the list of formal parameters. parameters is
of the form ¶meter1, ¶meter2,…Each parameter begins with ‘&’. Whenever
we use the term macro prototype it simply means the macro name along with its
parameters.
Slide 13
➢ Body of macro consist of statements that will generated as the expansion of macro.
➢ Consider the following macro definition:
SUM MACRO &X,&Y
LDA &X
MOV B
LDA &Y
ADD B
MEND
➢ Here, the macro named SUM is used to find the sum of two variables passed to it.
Slide 14
Macro Invocation(or Macro Call)
➢ A macro invocation statement (a macro call) gives the name of the macro instruction
being invoked and the arguments to be used in expanding the macro.
➢ The format of macro invocation
macroname p1, p2,...pn
Slide 15
Macro Expansion
➢ Each macro invocation statement will be expanded into the statements that form the
body of the macro.
➢ Arguments from the macro invocation are substituted for the parameters in the macro
prototype.
➢ The arguments and parameters are associated with one another according to their
positions. The first argument in the macro invocation corresponds to the first parameter
in the macro prototype, etc.
➢ Comment lines within the macro body have been deleted, but comments on individual
statements have been retained. Macro invocation statement itself has been included as
a comment line.
Slide 16
➢ Consider the example for macro expansion on next page:
➢ In this example, the macro named SUM is defined at the start of the program. This
macro is invoked with the macro call SUM P,Q and the macro is expanded as
LDA &P
MOV B
LDA &Q
ADD B
MEND
➢ Again the same macro is invoked with the macro call SUM M,N and the macro is
expanded as
LDA &M
MOV B
LDA &N
ADD B
MEND
Slide 17
Slide 18
Slide 19
Slide 20
Difference between Macro and Subroutine
Macro Subroutine
Macro name in the mnemonic field leads to Subroutine name in a call statement in the
expansion only. program leads to execution.
So there is difference in size and execution efficiency.
Statements of the macro body are expanded Statements of the subroutine appear only
each time the macro is invoked once, regardless of how many times the
subroutine is called.
Macros are completely handled by the Subroutines are completely handled by the
assembler during assembly time. hardware at runtime.
Macro definition and macro expansion are Hardware executes the subroutine call
executed by the assembler. So, the instruction. So, it has to know how to save
assembler has to know all the features, the return address and how to branch to the
options, and exceptions associated with subroutine. The assembler knows nothing
them. about subroutines.
The hardware knows nothing about macros.
Slide 21
Design of Macro Preprocessor
➢ Macro preprocessors are vital for processing all programs that contain macro
definitions and/or calls. Language translators such as assemblers and
compilers cannot directly generate the target code from the programs
containing definitions and calls for macros.
➢ Therefore, most language processing activities by assemblers and compilers
preprocess these programs through macro processors.
Slide 22
➢ The general design semantics of a macro preprocessor is shown as below
Slide 23
➢ The design of a macro preprocessor is influenced by the provisions for
performing the following tasks involved in macro expansion:
Slide 24
➢Recognize Macro Calls
◼ A table is maintained to store names of all macros defined in a program.
◼ Such a table is called Macro Name Table (MNT) in which an entry is made for
every macro definition being processed.
◼ During processing program statements, a match is done to compare strings in the
mnemonic field with entries in the MNT.
◼ A successful match in the MNT indicates that the statement is a macro call.
Slide 25
➢Determine the values of formal parameters
◼ A table called Actual Parameter Table (APT) holds the values of formal
parameters during the expansion of a macro call.
◼ The entry into this table will be in pair of the form (, ).
◼ A table called Parameter Default Table (PDT) contains information about default
parameters stored as pairs of the form (, ) for each macro defined in the program.
◼ If the programmer does not specify value for any or some parameters, its
corresponding default value is copied from PDT to APT.
Slide 26
➢ Maintain the values of expansion time variables declared in a macro
◼ A table called Expansion time Variable Table (EVT) maintains information about
expansion variables in the form (, ).
◼ It is used when a preprocessor statement or a model statement during expansion
refers to an EV.
Slide 28
➢ Perform expansion of a model statement
◼ The expansion task has the following steps:
◼ MEC points to the entry in the MDT table with the model statements.
◼ APT and EVT provide the values of the formal parameters and EVs,
respectively.
◼ SST enables identifying the model statement and defining sequencing.
Slide 29
Functions of Macro Processor
The design and operation of a macro processor greatly influence the activities
performed by it.
Slide 30
Macro Processor Algorithm and Data Structures
Slide 31
Two pass Macro Processor
➢ It is easy to design a two-pass macro processor in which all macro definitions are
processed during the first pass and all macro invocation statements are expanded
during second pass.
➢ Such a two pass macro processor cannot handle nested macro definitions. Nested
macros are macros in which definition of one macro contains definition of other
macros.
➢ Consider the macro definition example given below, which is used to swap two
numbers. The macro named SWAP defines another macro named STORE inside it.
These type of macro are called nested macros.
Slide 32
One pass Macro Processor
➢ A one-pass macro processor uses only one pass for processing macro definitions and
macro expansions.
➢ To implement one pass macro processor, the definition of a macro must appear in the
source program before any statements that invoke that macro.
Slide 33