Ooad Notes
Ooad Notes
STRUCTURE
FEATURES
10.IMPLEMENTATION EXAMPLE
A macro name is an abbreviation, which stands for some related lines of code. Macros
are useful for the following purposes:
to simplify and reduce the amount of repetitive coding
to reduce errors caused by repetitive coding
to make an assembly program more readable.
Source
Macro Expanded Compiler or obj
Code
(with macro) Processor Code Assembler
Macro expansion
Ex:
User program Macro Definition User program (after macro expansion)
Ex: displaying a message. (Assume that: MES2 DB ‗Enter the data as mm/dd/yy‘)
DATA STRUCTURE
ALGORITHMs
One-Pass Macro Processor
Prerequisite
» every macro must be defined before it is called
Sub-procedures
» macro definition: DEFINE
» macro invocation: EXPAND
NAMTAB
DEFINE
MACRO DEFTAB
PROCESSLINE
Sub-procedures
» macro definition: DEFINE
» macro invocation: EXPAND
EXPAND may invoke DEFINE when encounter macro definition
NAMTA DEFINE
MACR
DEFTA PROCESSLINE
CAL EXPAND
ARGTAB
Expandin MACRO
1- Pass Macro Processor
DEFINE
MACRO
PROCESSOR
GETLINE
EXPANDING=FALSE
PROCESSLINE
GETLINE EXPAND
PROCESSLINE
EXPANDING=TRUE
GETLINE GETLINE
PROCESSLINE
EXPANDING FALSE
TRUE
Single pass
» every macro must be defined before it is called
» one-pass processor can alternate between macro definition and macro
expansion
» nested macro definitions may be allowed but nested calls are not
Two pass algorithm
» Pass1: Recognize macro definitions
» Pass2: Recognize macro calls
» nested macro definitions are not allowed
Concatenation of parameter
Pre-concatenation
» LDA X&ID1
Post-concatenation
» LDA X&ID 1
Example
» JEQ *-3
» inconvenient, error-prone, difficult to read
Example Figure 4.7
– $LOOP TD =X’&INDEV’
» 1st call:
– $AALOOP TD =X’F1’
» 2nd call:
– $ABLOOP TD =X’F1’
Macro-time variables
» any symbol that begins with the character & and that is not a macro
parameter
» macro-time variables are initialized to 0
» macro-time variables can be changed with their values using SET
– &EORCK SET 1
Macro-time looping statement
» Example: Figure 4.9
» WHILE-ENDW
Macro processor function
» %NITEMS: THE NUMBER OF MEMBERS IN AN ARGUMENT LIST
Only part of the macro is copied out into the code. Which part is copied out will be under
the control of the parameters in the macro call.
Ex:Line
no. Assembly instructions
… …
8 CONMB (&C>2), 15
9
… …
15
… …
more
source N Assembler
code
Y
N Y
write line to the output file macro call call Macro Expander routine
N conditional Y
assembly call
LP = LP + 1 LP = LP + 1 LP = line number in
the
statement
Ex:
Parameter table
Dummy Real
parameter parameter
ARG1 A
ARG2 2
ARG3 C
Keyword Macro Parameters
Positional parameter: parameters and arguments were associated with each other
according to their positions in the macro prototype and the macro invocation
statement
Keyword parameters: each argument value is written with a keyword that named
the corresponding parameter
Each parameter name is followed by an equal sign, which identifies a keyword
parameter
The parameter is assumed to have the default value if its name does not appear in
the macro invocation statement
instruction
conditional macro call macro end
assembly call
current param.
write it to the output file N True Y
table pointer
Build a parameter table
LP = LP + 1 LP = LP + 1 LP = line number in
the statement
Push LP and the pointer
to the last parameter
table
onto the stack
Figure 4. Macro Expander Routine that allows macro calls within macros
ARGTAB
GETLINE
PROCESSLINE
Macro Invocation
EXPAND
ARGTAB
I- PASS macroprocessor
DEFINE
MACRO
PROCESSOR
GETLINE
EXPANDING=FALSE
PROCESSLINE
GETLINE EXPAND
PROCESSLINE
EXPANDING=TRUE
GETLINE
GETLINE
PROCESSLINE
EXPANDING FALSE
TRUE
GETLINE(f)
PROCESSLINE(f)
GETLINE(0)
PROCESSLINE(0) EXPAND
GETLINE(1)
PROCESSLINE(1)
GETLINE(f)
f FALSE
TRUE
DEFINE(f)
MACRO
PROCESSOR
GETLINE(f)
PROCESSLINE(f)
GETLINE(0)
PROCESSLINE(0) EXPAND
GETLINE(1)
PROCESSLINE(1)
GETLINE(f)
f FALSE
TRUE
ELENA
» Software: Practice and Experience
Macro definition
» header:
– a sequence of keywords and parameter markers (%)
– at least one of the first two tokens in a macro header must be a
keyword, not a parameter marker
» body:
– the character & identifies a local label
– macro time instruction (.SET, .IF .JUMP, .E)
– macro time variables or labels (.)
Macro invocation
» There is no single token that constitutes the macro ―name‖
» Constructing an index of all macro headers according to the keywords in
the first two tokens of the header
» Example
– DEFINITION:
ADD %1 TO %2
ADD %1 TO THE FIRST ELEMENT OF %2
– INVOCATION:
DISPLAY TABLE
MASM
The macro processor of MASM is integrated with Pass 1 of the assembler
MASM generates the unique names of local labels in the form ??n, where n is a
hexadecimal number in the range 0000 to FFFF
.ERR: signals to MASM that an error has been detected
EXITM: directs MASM to terminate the expansion of the macro
&: is a concatenation operator
;; is a macro comment, serves only as documentation for the macro definition
; is an ordinary assembler language comment, included as part of the macro
expansion
IRP: sets the macro-time variable to a sequence of values specified in <…>
The statements between the TRP and the matching ENDM are generated once for
each value of the variable
ANSI C Macro Language
In the ANSI C language, definitions and invocations of macros are handled by a
preprocessor, which is generally not integrated with the rest of the compiler
#define ABSDIFF(X,Y) X>Y ? X-Y : Y-X
#define DISPLAY(EXPR) printf(#EXPR‖=%d\n‖,EXPR)
Macro in ANSI C may contain definitions or invocations of other macros
DISPLAY(ABSDIFF(3,8)) ABSDIFF(3,8)=5
The ANSI C preprocessor also provides conditional compilation statements
#ifndef BUFFER_SIZE
#define BUFFER_SIZE 1024
#endif