5.
MACROS
INTRODUCTION
TO MACROS
Why
Use
Macros?
A macro
is
e'>sentially a facility for replacing
one
set
of
parameters with
another.
In
developing your program,
you will
frequently
find
that
[T,any
instruction sequences
ale
repedted several times with only certain
parameters
changed.
As
an eXdmple, suppose
that
you code a
routine
that
moves five bytes
of
ddta from one
memory
locdtion to
another.
A little later, you find yourself coding
dnother
routine to move four
bytes
from a
different
source
field
to
a different
destination
field.
If
the
two routine,>
u'>e
the same coding techniques, you will find
that
they
Me
identic.al
except
for three pMameters: the characler
count,
the source field '>tarting addre'>s, and
the
destination
field stdlting addre'>S. Certainly
it
would be handy
it
there were some way
to
regenerate
that
origindl
routine
substituting
the new parameters rather than rewrite
that
code
your,>elf.
The
macro facility provides this
capability and
offers
,>everal
other
advantages over writing code repetit iously:
• The
tedium
of
frequent
rewrite (and the probdbility
of
error)
is
reduced.
• Symbols used
in
macros can be restricted so thdt they have medning only within the macro
i[<;elf.
Therefore, as
you
code
your program, you need not worry thdt you will ace dentally duplicate a
symbol
u'>ed
in
a mdcro. Also, a macro can
be
u'>ed
dny
number
of
times
in
the
same program
without
duplicdt:ng
any
of
it<,
own symbol'>.
•
An
erl"Or
c.letected
in
a macro need be
corrected
unly
once
regdrdless
uf
huw mdny times the macro
appedl,
in
the
progrdm.
Thi,>
reduce,> debugging time.
• Duplicatiun
of
effurt
between programmer'> can be reduced. Useful
functium
can be collected
in
a
library tu alluw
macru,>
to
be cupied
intu
different
programs.
In
addition,
mallo> can be
u',eci
tu improve program readability and to credte
structured
programs.
U,>ing
macros
to segment code blocks provic.les clear progr,lIn
notation
dnd simplitie'> trdcing the flow
of
the
progrdm.
What
Is
A
Macro?
A mdcro can be de,>cribed as a
routine
defined
in
a formal sequence
of
prototype
instructions
thdt, when culled
within a program, re'>ults
in
the replacement
of
each
,>uch
c;1I1
with d cude
expansion
consi<;ling
of
the
dctu.!1
instructions
represented.
5·1