Operating instructions

~
PCL
f-
(PC+1)
INC COUNT
BNE
= + 5
INC COUNT
+ 1
Another
way
of
thinking
about
macros is
to
view them as small
subroutines wh.ich are inserted
directly
into a program instead
of
being called.
When a short sequence
of
instructions
is
commonly
repeated,
it
may
be
cheaper to make a macro
out
of
it
than
to make
it
into
a subroutine. Part
of
the
reason for
this
is
that
it
costs
extra in-
structions
to
pass parameters to
a,
subroutine, especially on a micro such
as the 6502, which has a
limited
number
of
registers. In
this
example, particular·
ly, the difference is
significant.
In order
to convert the INCO macro
into
a
subroutine, we would need
to
figure
out
a way
to
pass the address
of
the
first
byte to be incremented. For example:
LOA
"Low
byte
of
address
of
COUNT"
LOX
"High
byte
of
address
of
COUNT"
J S R INCO
INC
0 S T A CL Page Zero Loc
S T X
CH
Next Page Zero Loc
LOY
#00
Assuming
Y
available-
otherwise
TAY-PHA first.
INC
(CL),Y
B N E
=
+6
INY
INC
(CL),Y
RTS
may form interpretive code which can,
via the macro capability, be interspersed
with
ordinary
machine code. By using
macros to generate the interpretive
code, the programmer is freed from the
odious
task
of
hand assembly - a task
which could discourage him from using
the interpretive code in the first place.
Sweet·16
The 6502 microprocessor provides
no direct
capability
for handling
16
bit
quantities. In particular, the machine
has no internal
16
bit registers, save for
the
PC.
Thus, when
it
becomes neces-
sary to do
16
bit arithmetic,
or
to
manipulate
pointers or
16
bit addresses,
the programmer is forced to
write
in-
struction
sequences to simulate the
re-
quired operations. The APPLE
II
firm-
ware
contains
a subroutine known as
the SWEET-16
"dream
machine," which
does
just
that.
It
operates in an inter-
pretive mode, taking the sequence
of
bytes
following
the
instruction
which
calls
it
as virtual
or
interpretive code.
Here's how
it
works.
When a JSR (Jump to SubRoutine)
instruction
is executed by the 6502 pro-
cessor, the value
of
the program
counter, which in
that
case
will
be
the
address
of
the last byte
of
the JSR in-
struction,
is saved on the 6502
stack
as
two
consecutive bytes. When
aRTS
(ReTurn from Subroutine)
instruction
is
executed within the called subroutine,
that
address increased by one will be
restored from the
stack
to the
PC,
to
enable the 6502 to
continue
executing
instructions
following the JSR instruc-
tion. (See Figure
1.)
The
fact
that the
"return"
address is saved on the
stack
means that the called subroutine can,
in
fact, find out where
it
was called from.
More than that,
it
can use the return ad-
dress and the indirect addressing mode
of
the 6502 to
actually
retrieve the se-
quence
of
bytes
following
the calling in·
struction.
That is precisely what the
Sweet-16 subroutine does.
The Sweet-16 interpreter takes ad-
vantage
of
the
fact
that the return ad-
dress is at the top
of
the 6502 stack. It
pops the two bytes from the
stack
and
transfers them to a pair
of
page zero
locations
which
it
then uses as
an
in-
direct address
to
locate the sequence
of
interpretive
instructions
following
the
JSR which called it.
Thus the return address
of
the
Sweet-16 subroutine becomes the ad-
dress
of
the first
instruction
to
be
ex-
ecuted by the Sweet-16 machine. As the
Sweet-16 machine executes instruc-
tions,
it
updates
this
address to
point
to
the next virtual
instruction
to
be
ex-
ecuted. When the Sweet-16 interpreter
finds
an
interpretive
instruction
This is
surprisingly
more
complicated
than the macro, which is why you pro·
bably never
thought
of
making
it
into
a
subroutine
before. In general,
if
a
subroutine
is
short
and
if
it
involves
manipulating
addresses
of
parameters,
then
it
may be worth converting
to
a
macro.
Assemblers vary widely in
the
richness
of
features supported. One
of
the more desirable features to use in
conjunction
with
the macro
capability
is
that
of
conditional
assembly. This
enables a program
to
define
instruction
sequences and, in particular, macros,
with
much
more
flexibility.
We shall see
this
in action when we
discuss
the
Sweet-16
macros
later.
Conditional
assembly directives
allow
the program-
mer to
control
the
actions
taken by the
assembler.
Macros can be used
to
generate ar-
bitrary bit patterns
into
the
stream
of
ob-
ject
code produced
by
the
assembly
of
a
program. There
may be
subtle
reasons
for
wishing
to do this. One
of
those
reasons
forms
the meat
of
our
principal
example: the
bit
patterns so generated
PC
H
~-
(PC+2)
JSR
~
ADD\
ubroutine
body
ADD~
(
PC)-~
~
RTS
PUSH~-
(PC)
(PC)f-POP
~
0
Stack
after
GALLO
Stack
before
(RETURNO
Figure
1:
6502
Subroutine Call and Return
20:26
MICRO
--
The 6502 Journal
January, 1980