Unit 3 Macro1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Unit- III

Macros and Macro Preprocessor


Macro
 A macro is a unit of specification for
program generation through
expansion.
 A macro consists of
 a name,
 a set of formal parameters and
 a body of code.
Macro Definition
 A macro definition is enclosed between a
macro header statement and macro end
statement.
MACRO //Opcode for macro header
.
.
.
MEND //Opcode for macro end
Statements in Macro Definition
Three types of statement:
1. Macro Prototype
2. Model Statement
3. Macro Preprocessor
1. Macro Prototype Statement
 Declares name of the macro and names
and kind of its formal parameters.
 ‘&’ is prefixed to formal parameter names
to distinguish them from actual
parameters.
2. Model Statement
 A statement from which an assembly
language statement may be generated
during macro expansion.
3. Macro Preprocessor Statement
 Performs auxiliary functions during macro
expansion.
 For example, conditional statement like
AIF statement to control the flow of
program.
Macro Prototype
MACRO
<Macro name> [<formal parameter
specification>]

Model statement(s) / macro preprocessor


statements

MEND;
Macro: Example #1
 Write a macro to increment the value of
a variable.
 The following sequence of instructions is
used to increment the value in a
memory word by a constant.
1. Move the value from the memory word into a
machine-register.
2. Increment the value in the machine register.
3. Move the new value into the memory word.
Macro: Example #1
Macro definition to increment the value
of a variable.
MACRO
INCR &MEM_VAL, &INCR_VAL, &REG
MOV &REG, &MEM_VAL INCR is macro name;
MEM_VAL, INCR_VAL, REG
ADD &REG, &INCR_VAL are formal parameters.
MEM_VAL is the value to be
MOV &MEM_VAL, &REG incremented; INCR_VAL is
the incremental value and
MEND; REG is the register to be
used for holding temporary
values.
Macro Call
 The macro is called by writing macro
name followed by actual parameters if
any.
 The INCR macro can be called as
INCR A, B, AX
Macro Call Contd..
 Macro call leads to macro expansion.
 During macro expansion, the macro call
statement is replaced by a sequence of
assembly statements.
 Each expanded statement is marked
with a “+” preceding its label field to
distinguish it from original program
statements.
Macro Expansion

Macro Call Macro Prototype


INCR A, B, AX INCR &MEM_VAL, &INCR_VAL, &REG

Substitution Macro Statements


+MOV AX, A MOV &REG, &MEM_VAL
+ADD AX, B ADD &REG, &INCR_VAL
+MOV A, AX MOV &MEM_VAL, &REG
Macro Expansion
 The language preprocessor replaces a
macro call statement by a sequence of
assembly language statements that are
generated by using the body of code in
the macro definition.
 Two kinds of macro expansion are as:
1. Lexical Substitution
2. Semantic Expansion
1. Lexical Substitution
 It 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.
2. Semantic Expansion
 It is based on control flow.
 The control flow in the definition
determine the order in which model
statements would be visited for expansion
of macro call.
Expansion Time Control Flow
 The default flow of control during macro
expansion is sequential.
 In the absence of preprocessor statements,
the model statements of a macro are
visited sequentially starting with the
statement following the macro prototype
statement and ending with the statement
preceding the MEND statement.
Expansion Time Control Flow
Contd..
 A preprocessor statement can alter the
flow of control during expansion as:
 Conditional Expansion: some model statements are
either never visited during expansion, or
 Expansion Time Loops: are repeatedly visited during
expansion.
 The flow of control during macro
expansion is implemented using a macro
expansion counter (MEC)
Algorithm: Macro Expansion
1.MEC:=statement number of first statement following the
prototype stmt.
2. While statement pointed by MEC is not a MEND
statement.
a. If a model statement then
i. Expand the statement
ii. MEC:=MEC+1;
b. Else (i.e. a preprocessor statement)
i. MEC:= new value specified in the statement.
3. Exit from macro expansion.
ADVANCED MACRO
FACILITIES
 Advanced macro facilities are aimed to
support semantic expansion.
 Used for performing conditional
expansion of model statements and in
writing expansion time loops.
ADVANCED MACRO FACILITIES
 These facilities can be grouped into
following.
1. Facilities for alteration of flow of
control during expansion.
2. Expansion time variables.
1. ALTERATION OF FLOW OF CONTROL
DURING EXPANSION
 Two features are provided to facilitate
alteration of flow of control during
expansion.
1. Expansion time sequencing symbol
2. Expansion time statements
i. AIF,
ii. AGO and
iii. ANOP.
Sequencing Symbol
 A sequencing symbol (SS) has the syntax
.<ordinary string>
 As SS is defined by putting it in the
label field of statement in the macro
body.
 It is used as an operand in an AIF or AGO
statement to designate the destination of
an expansion time control transfer.
AIF Statement
An AIF statement has the syntax
AIF (<expression>) <sequencing symbol>
◦ Where <expression> is a relational
expression involving ordinary strings,
formal parameters and their attributes
and expansion time variables.
 If the relational expression evaluates to
true, expansion time control is
transferred to the statement containing
<sequencing symbol> in its label field.
AN AGO STATEMENT
 An AGO statement has the syntax
AGO <sequencing symbol>
 Unconditionally transfers expansion time
control to the statement containing
<sequencing symbol> in its label field.
AN ANOP STATEMENT
 An ANOP statement is written as

<sequencing symbol> ANOP

 And simply has the effect of defining the


sequencing symbol.
Example: A macro to find the greater
number between two numbers.
MACRO
COMP_NUM &A, &B, &R
1. .NEXT ANOP
2. .OVER ANOP
3. AIF (&A GT &B) .NEXT
4. MOV &R, &B
5. AGO .OVER
6. .NEXT MOV &R, &A
7. .OVER
MEND;
Example: Macro to evaluate an
algebraic expression A-B+C
MACRO
EVAL &A, &B, &C, &R
MOV &R, &A
SUB &R, &B
ADD &R &C
MEND;
EVAL Macro- Efficient Version
 Modify the definition of EVAL to generate
efficient code as
 When first two parameters are identical,
EVAL should generate a single MOV
instruction to load the third parameter
into Register ‘R’.
MACRO 1. START
EVAL &A, &B, &C, &R 2.
3.
READ A,B,C
IF A=B THEN GOTO 5
.ONLY_MOV ANOP 4. T = A-B+C GOTO 6
5. T=C
.OVER ANOP 6. PRINT T
AIF (&A EQ &B) .ONLY_MOV 7. STOP

MOV &R, &A


SUB &R, &B
ADD &R &C
AGO .OVER
.ONLY_MOV MOV &R, &C
.OVER
MEND;
2. EXPANSION TIME VARIABLE
 Expansion time variables (EV’s) are
variables which can only be used during
the expansion of macro calls.
 A local EV is created for use only
during a particular macro call.
 A global EV exists across all macro calls
situated in a program and can be used in
any macro which has a declaration for it.
Local & Global EV’s
Local and global EV’s are created through
declaration statements with the following
syntax:
LCL<EV specification> [,<EVspecification>]

GBL<EV specification>[,<EV specification>]


 Example
LCL &A
GBL &B
EV Specification
 <EV specification> has the syntax

&<EV name>
where <EV name> is an ordinary string.

 Values of EV’s can be manipulated


through the preprocessor statement
SET.
SET Statement
 A SET statement is written as

<EV specification> SET <SET-expression>


where <EV specification> appears in the
label field and SET in the mnemonic field.
 Example:
&A SET 5
 Thevalue of an EV can be used in any field
of a model statement, and in the expression
of an AIF statement.
Example:
 Macro that moves 8 numbers from the
first eight position of an array specified as
the first operand into the first eight
position of an array specified as second
operand.
MACRO
ARR_CPY &A, &B
LCL &I
&I SET 0
.AGAIN ANOP
.AGAIN MOV &B+&I, &A+&I
&I SET &I+1
AIF(&I NE 8) .AGAIN
MEND;
Expansion Time Loops
 It is often necessary to generate many
similar statements during the expansion
of a macro.
 This can be achieved by writing similar
model statements in the macro.
 Alternatively, loops can be written using
EV’s and expansion time control transfer
statements AIF and AGO.
Expansion Time Loops
 An expansion time loop generates
sequential statements based on looping
condition and variable.
 An execution time loop leads to more
compact program but executes slower
than expansion time variable.
 Expansion time loops can be used for
execution efficiency.
Loop Example
 Write a macro that takes parameters as
A, B, C, D and calculate A*B +C*D in a
register.
 Note: Do not use multiplication
instruction instead use loop.
MACRO A*B +C*D
CAL &A, &B, &C, &D, &Reg1, &Reg2 T=A
.LOOP1 ANOP do
.LOOP2 ANOP T=T+A
B=B-1
MOV &Reg1, &A
while (B>1)
.LOOP1 ADD &Reg1, &A
SUB &B, #1
AIF (&B GT 1) .LOOP1 A=4; B=3
T = 4;
MOV &Reg2, &C do
.LOOP2 ADD &Reg2, &C T = 4+ 4 (8)
SUB &D, #1 B=3-1 (2)
AIF (&D GT 1) .LOOP2 while (B>1);
do
ADD &Reg1, &Reg2 T = 8+ 4 (12)
B=2-1 (1)
MEND; while (B>1);
Example: Macro to initialize ‘n’ consecutive
words starting from address ‘x’
MACRO
INIT_ZERO &X, &N, &R
.AGAIN ANOP
LCL &I
MOV &R, #0

&I SET 0

.AGAIN MOV &X+&I, &R


&I SET &I + 1
AIF (&I NE &N) .AGAIN

MEND;

You might also like