You are on page 1of 32

One Pass Macro Processors and

difference between macro and


function

SUBMITED BYYesbir singh


Dhwani shah
Harsh baria

Introduction

Concept
A macro instruction is a notational convenience for the
programmer
It allows the programmer to write shorthand version of a
program (module programming)
The macro processor replaces each macro invocation with
the corresponding sequence of statements (expanding)

Macro Processor

Recognize macro definitions


Save the macro definition
Recognize macro calls
Expand macro calls

Source
Code
(with macro)

Macro
Processor

Expanded
Code

Compiler or
Assembler

obj

One-Pass Macro Processor

Prerequisite
every macro must be defined before it is called

Sub-procedures
macro definition: DEFINE
macro invocation: EXPAND
NAMTAB
MACRO

DEFINE

DEFTAB

CALL

EXPAND

ARGTAB

PROCESSLINE

Data Structures -- Global


Variables

DEFTAB
NAMTAB
ARGTAB

EXPANDING

DEFTAB (definition table)- it is a definition table that is used to


store the macro definition including macro prototype and macro
body. Comment lines are not included here, and references to the
parameters use positional notation for efficiency in substituting
arguments.

NAMTAB(name table)- this table is used for storing macro


names.it serves as an index to DEFTAB and maintains pointers that
point to the beginning and end of the macro definition in DEFTAB.

ARGTAB(argument table)- it maintains arguments according to


their positions in the argument list . During expansion, the
arguments from this table are substituted for the corresponding
parameters in the macro body.
7

Nested Macros Definition

Macro definition within macros


process macro definition during expansion time

Example 4.3

Figure 4.3 (b)

One-Pass Macro Processor That


Allows Nested Macro Definition

Sub-procedures
macro definition: DEFINE
macro invocation: EXPAND

EXPAND may invoke DEFINE when encounter macro definition

NAMTAB
DEFTAB
ARGTAB
Expanding

MACRO

DEFINE

CALL

EXPAND

PROCESSLINE

MACRO Definition
10

1-Pass Macro Processor


D E F IN E

M AC RO
PR O C ESSO R

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
P R O C E S S L IN E

EXPAND

E X P A N D IN G = T R U E

G E T L IN E
P R O C E S S L IN E

G E T L IN E

E X P A N D IN G

FA LSE

TRU E

READ FR O M
DEFTAB

READ FRO M
IN P U T

13

Comparison of Macro Processors


Design

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

14

Concatenation of Macro
Parameters

Pre-concatenation
LDA

Post-concatenation
LDA

X&ID1
X&ID1

Example: Figure 4.6

15

Generation of Unique Labels

Example
JEQ
*-3
inconvenient, error-prone, difficult to read

Example Figure 4.7


$LOOP

TD

=X&INDEV

TD

=XF1

TD

=XF1

1st call:
$AALOOP

2nd call:
$ABLOOP

16

RDBUFF

F1, BUFFER, LENGTH

Conditional Macro Expansion

Macro-time conditional statements


Example: Figure 4.8
IF-ELSE-ENDIF

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

19

RDBUFF

F3, BUF, RECL, 04, 2048

RDBUFF

0E, BUFFER, LENGTH, , 80

RDBUFF

F1, BUFF, RLENG, 04

Conditional Macro Expansion


(Cont.)

Macro-time looping statement


Example: Figure 4.9
WHILE-ENDW

Macro processor function


%NITEMS: THE NUMBER OF MEMBERS IN AN
ARGUMENT LIST

23

Nested Macro Invocations

Macro invocations within macros


process macro invocation during expansion time

Recursive macro expansion


Example: Figure 4.11
Problems:
ARGTAB
EXPANDING

Solution
Recursive call
While loop with stack

24

ARGTAB
DEFTAB

MACRO Definition

NAMTAB

DEFINE
GETLINE

PROCESSLINE

Macro Invocation

EXPAND

ARGTAB

25

1-Pass Macro Processor


D E F IN E

M AC RO
PR O C ESSO R

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
P R O C E S S L IN E

EXPAND

E X P A N D IN G = T R U E

G E T L IN E
P R O C E S S L IN E

G E T L IN E

E X P A N D IN G

FA LSE

TRU E

READ FR O M
DEFTAB

READ FRO M
IN P U T

26

Allowing Nested Macro


Invocation
D E F IN E (f)

M AC R O
PR O C ESSO R

G E T L IN E (f)

P R O C E S S L IN E (f)
G E T L IN E (0 )
P R O C E S S L IN E (0 )

EXPAND

G E T L IN E (1 )
P R O C E S S L IN E (1 )

G E T L IN E (f)

FA LSE

TRU E

READ FR O M
DEFTAB

READ FRO M
IN P U T

27

Difference between macro and


function

Macro consumes less time:


When a function is called, arguments have to be passed to it, those
arguments are accepted by corresponding dummy variables in the
function. Then they are processed, and finally the function returns a value
that is assigned to a variable (except for a void function). If a function is
invoked number of times, the times add up, and compilation is delayed.
On the other hand, the macro expansion had already taken place and
replaced each occurrence of the macro in the source code before the
source code starts compiling, so it requires no additional time to execute.

2.

28

Function consumes less memory:


Prior to compilation, all the macro-presences are
replaced by their corresponding macro expansions,
which consumes considerable memory. On the other
hand, even if a function is invoked 100 times, it still
occupies the same space. Hence function consumes
less memory.

29

Example for Macro:


The following line defines the macro SUM, having two parameters
a and b and the replacement tokens (a + b):
#define SUM(a,b) (a + b)
This definition would cause the preprocessor to change the
following statements (if the statements appear after the previous
definition):
c = SUM(x,y);
c = d * SUM(x,y);
In the output of the preprocessor, these statements would appear
as:
c = (x + y);
c = d * (x + y);
30

Example for function:


In calling program function call would appear as,
c=SUM(x,y);
Where sum is the name of the function. And x, y are the arguments passed
from calling program to called program.
This statement will invoke the following function SUM. Hence x, y are copied
to dummy variables a and b. And then function computes the sum and stores
the value in c, which will be returned back to the calling program.
int sum(int a, int b)
{
int c;
c=a+b;
return c;
}

31

THANK YOU!

32