Professional Documents
Culture Documents
C64 ASSEMBLER
Version 2.04 / 14-11-83
BIN/HEX/DEC TUTO
Version 2.02 / 14-11-83
Commodore 64
Commodore 64
Assembly Language
Programming
Derek Bush and Peter Holmes
All programs in this book and the accompanying software have been
written expressly to illustrate specific teaching points. They are not
warranted as being suitable for any particular application. Every care
has been taken in the writing and presentation of this book but no
responsibility is assumed by the author or publisher for any errors
or omissions contained herein.
84
85
86
87
88
89
90
91
PRINTING
92 YEAR
CONTENTS
INTRODUCTION
CHAPTER 1
Getting Started
instructions.
Jumping, Branching, Flags
Unconditional jumps, calculating addresses.
The program counter. Conditional jumps
(branches). The 6510 flags.
CHAPTER 3
CHAPTER 4
CHAPTER 6
Built-in Subroutines
Solutions to Exercises
APPENDIX 1
APPENDIX 2
Tables
Table
Table
Table
Table
Table
APPENDIX 3
1.
2.
3.
4.
5.
INTRODUCTION
This book is a self-paced course on machine-code/assembly language program
ming based on the 6510 microprocessor. Getting going in assembly language
is said to be a major problem for the beginner. However, it's a problem that
we trust will disappear when the beginner's hands get on this book. By plot
ting a careful route through the instruction set, we have produced a text that
will take the reader stage by stage through 6510 assembly language maintain
ing interest and understanding at all times. In order to overcome the usual
difficulty associated with assembly languages, the Mnemonics used are total
ly uniform throughout the book and the instruction set used is the one employed
by the assembler and was written especially to accompany this text. Most of
the examples are designed to be loaded via this assembler and all are designed
to run on the Commodore 64.
In order to ensure that the vital first few machine code programs run, the open
ing chapter goes in great detail through the stages of entering, assembling,
listing and running these programs. Those who bring to the book pre-existing
skill are asked to bear with the text in these early stages. Notwithstanding the
gentle pace of the opening, the reader should find himself/herself accelerating
along with the text. By the end of chapter eight he/she will have covered the
whole 6510 instruction set.
Just to test your understanding at each stage the text is interspersed with
numerous exercises. In all cases, the solutions to these are provided and, in
many cases, detailed explanations are given to aid your understanding. As
you've paid for these exercises, don't waste your moneyhave a go at them.
These too are carefully graded to expand your understanding of 6510.
As a further aid, the software supplied with this book contains a Binary BCD
Hexadecimal Tutor. Appendix 1 provides a full explanation of how to use this
program and should ensure that you understand binary, binary-coded decimal
and hexadecimal really well. A further feature of this program - the exercises
- will enable you to test just how well you do understand. If you're shy, just
try them when no one's looking!
Well that's all the propoganda - we've done what we can to make learning
assembler fun - the rest is up to you. Enjoy yourself.
CHAPTER 1
Getting Started
Having driven a
computer before
you
are
probably
well
aware
that
that
understands.
the
As
an
micro-processor
example
take
chip
simple
in
your
addition
sum
computer
-
adding
and 2.
10
LET A = 1
20
LET B = 2
30
C = A + B
40
PRINT C
01
8E
A9
6D
8D
60
84 03
02
84 03
85 03
isn't it?
Well,
that's why we
STX
900
'1'
LDAIM
ADC
900
Add
to
the
accumulator
the
contents
of
STA
901
Store
contents
of
accumulator
in
memory
location 901
RTS
It's
much
easier
to
read
that
than
the
machine code,
isn't
does
machine-code.
is
to
change
the
assembly
language
it changes this
into an 'A2* and puts this into memory in the right place.
it?
language
All the
into
command
PAGE 1-1
the accumulator
(A)
6510 instructions allow you to write directly into this store using
Mode".
LDAIM
(i.e.
any
specified
memory
location.
STA
If
this
memory
location
is
Note
that
thought
the
of
TRANSFER
as
COPY
commands
commands
as
you
will
they
take
meet
a
should
copy
of
really
the
data
be
and
will
create
accumulator
screen,
A
i.e.
couple
program
to
be
and
program
then
which
transfer
it
will
to
put
the
number
top
left
when
you
start
where
you
want
into
position
the
on
the
location 1024.
of
points
the
about
assembler
placed
in
the
needs
computer
assembler:
to
be
told
memory.
The Commodore
64
to write
the
has
program
plenty
of
cassette
convenient
stored
So
in
buffer,
192
there
our
which
bytes.
are
early
extends
say
unlikely to
programs we
second
point
about
the
the
the
program
Thus,
the
PAGE 1-2
provides
which
are
use
be
told
first
program
is
that
Later
on
when we
and
EHD
1019,
programs
of
the
cassette
buffer
are:-
....
to
make
assembler
assembler must
program.
as
get mixed
Lastly,
828
will
from
convenient
last
initially we will
we
shall
have
lines
see
finished
of
the
how
only
other
entering
assembly
N.B.
The
underlining
intended
to
computer.
and
This
should
first
machine
will
and
it
type
the bit
last
code
that
this
is
easier
Remember,
computer
The
START ADDRESS?
convention
make
examples.
this is
of
indicate
used
for
if
it
this;
if
is
lines
shown
They
above
program
typed
throughout
to
isn't
this
book
the
then
the
then
in yourself.
nothing
provide
is
the
underlined,
to type
have
by
interpret
underlined,
it
simply
the
been
you
program.
in
has
to
do
with
information
When you
have
put
in your
machine-code
program
you
may
the
to
run
the
it
by
using the RUN facility provided by the assembler (or by using a SYS
call
to
case).
the
start
Either
machine-code
of
way
to
the machine
you
BASIC
must
or
tell
monitor.
the
The
program
command
to
return
that
does
from
this
is
to put
that
into
1.
2.
^LoaD
number
Mode.
The
'0'
into
Mnemonic
for
accumulator
this
is
.A
using
LDAIM
jtamediate
followed
by
the
jSTore
in
Accumulator,
specific
the
address
Mnemonic
is
the
STA.
contents
After
this
of
the
we
must
JITore
in
another
Accumulator
this
specific
STA
55296.
address
will
the
contents
explain
why
of
the
we
do
later.
5.
to BASIC RTS
6.
or
PROGRAM 1.1
STA 1024
STA 55296
RTS
END
PAGE 1-3
a)
b)
c)
d)
Select
'E'
e)
Type
of course)
and
press
the
<return>
key.
After
the
assembler
entered the code, it will type a "?" then:
in
"828"
(without
the
quotes
"
has
f)
g)
h)
i)
j)
Type in "END"
At this
2 LDAIM 0
2 STA 1024
2 STA 55296
2 RTS
2 END
k)
If it
to MENU.
of program start.
l)
Type in "828"
first
clears
program
left-hand
m)
PAGE 1-4
<return>.
the
which
After
screen and
will
this
then
runs
black
input
the
"@"
the
program
machine-code
in
the
top
Now select
'L1
ASSEMBLY
MACHINE
ADDRESS
CODE
PROGRAM
CODE
DEC HEX
828 O33C
A9 00
LDAIM 0
830 O33E
833 0341
8D 00 04
8D 00 D8
STA
STA
836 0344
60
RTS
etc
etc.
etc.
1024
55296
Your
and returns to
BASIC.
What the above shows is, taking the first line of program 1.1
..Memory location
of first byte of
command i.e.
"LDAIM"
the
Reading
the
this
Instruction
in machine code
and value to be
entered
listing
gives you
LDAIM 0
A? 00^^
828 O33C
CODE
PROGRAM
CODE
DEC HEX
Thus
ASSEMBLY
MACHINE
ADDRESS
is
both
check
full machine-code
off
on
what you
program or
entered
the
object
and
also
program.
listing
this
is:-
A9
8D
00
00
04
8D
00
D8
60
This
object
would yield
code
the
could
same
be
results
entered
as
directly
into
memory
typed
in.
and
The
PAGE 1-5
There
will
is
one
deal
store
the
point
with
zero
in
Commodore
that
before
55296
64
might
puts
have
introduce
The
colour
been
puzzling you
anything
answer
on
to
the
else.
which
Why
did
I
we
this
lies
in
the
screen.
If
we
store
way
a
value
directly into the screen (as we did when we stored the
zero in 1024), then we also need to tell the computer the colour
to
be
used
likely
to
for
be
background,
the
display.
displayed
and
If we
in
light
the
grey
don't
same
then
colour
character
on
the
as
result
the
is
screen
light
grey
or not.
Storing a zero in the first byte of the colour RAM (i.e. 55296)
ensured
that
the
character
stored
in
the
first
byte
of
the
from 0 to
computer
the
can
colour
screen
display.
RAM
is
One
the
background,
of
value
number
course.
we
don't
representing
In
the
want
the
case
of
to
store
colour
the
15
that
of
in
the
in
the
assembler,
15.
Now
let
program.
us
As
look
at
stated
another
earlier,
instruction
the
and
accumulator
use
is
the
this
in
repository
of most "answers" and the new instruction ADC "does a sum" and
loads the answer into
ADC
the accumulator.
ADd.with arry
location to
contents
of specified memory
the accumulator.
Don't
worry
what
they mean
for
now
just
tyne
'em in!
and
set.
In this book, the term COMMAND is used to refer to the
mnemonic part of an instruction when this precision is required.
For instance, in the instruction LDAIM 0, the LDAIM part may be
referred to as the command.
Let's look at the stages:-
PAGE 1-6
PROGRAM 1.2
Gives
address
for
beginning
of
program.
CLD
CLC
Gets
LDAIM 1
LoaD
"1"
into
the
Accumulator
in
IMmediate mode.
STA
1024
STore
STA
55296
STore
the
contents
of
Accumulator
of
Accumulator
(1) in 1024.
the
contents
(1) in 55296
LoaD
LDAIM 2
"2"
into
the
Accumulator
in
IMmediate mode.
ADC
1024
STA
1026
STore
STA
55298
STore
RTS
ReTurn
End
However,
using
if
of
the
the
contents
of
the
from
machine-code
Subroutine.
assembly.
If you make
mistake
contents
END
Right then,
the
mistake
the
you
before
cursor
press
pressing
keys
return
with
before
return,
the
you
you
may
INST/DEL
key
notice
the
correct
as
the
normal.
mistake
just
1.
2.
already running.
3.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Type in "CLC"
Type in "LDAIM 1"
Type in "STA 1024"
Type in "STA 55296"
Type in "LDAIM 2"
Type in "ADC 1024"
Type in "STA 1026"
Type in "STA 55298"
Type in "RTS"
Type in "END"
Select "R" to run program and then give start address 828.
Screen displays a white "A" in 1024 and a red "C" in
4.
1026.
Press any key to
return
to menu.
If you wish to list, select "L" and then give the location "828".
PAGE 1-7
the
three
lines
LDAIM 1
STA 1024.
STA 55296
is as a way of putting a
"1"
The 6510 has two index registers in addition to its accumulator and
these are referred
one 8-bit
it,
number.
The
arrangement
of
these
or,
as
the
jargon has
in Fig.
1.1.
Data
Bus
Accum
ulator
ALU
Fig.
In
this
figure
although
index
can
they
the
do
registers.
increment
and
differ
The
registers
slightly.
real
(increase
1.1
advantage
by
1)
or
are
shown
Nevertheless,
of
index
decrement
identically,
they
are
registers
is
(decrease
by
both
that
1)
we
the
value which they contain and, in addition, we can use them to 'step
through1 memory, so they are very powerful as we shall see later.
To the right of the figure is the 'ALU' or Arithmetic and Logic
Unit which is used by the 6510 for all arithmetic and logical
operations which it needs to carry out.
The ALU has two inputs for
the
data
that
it manipulates
and
one
output which
feeds
the result
feature
along
of
the
the
6510.
'Data Bus*
is extended
the
Data
which
flows
is
For talking to
between
pathway
devices
beyond
various
for
the
registers
communication
chip
this data
these
registers
remainder
of
this
chapter,
we
PAGE 1-8
the
common
will
look
at
the X-register -
so
to
load
LDX
JLoajD
index
register
specified address,
LDX 900
with
data
from
the
i.e.
LDX differs from the earlier "LDAIM11 (apart from one loading the
Accumulator and one X register) in that the LDAIM command is an
Immediate
what's
Mode
command.
immediately
When
following
6510 looks
data.
for what
the
the
6510
sees
instruction
this
and
it
loads
looks
that
for
as
follows and
this specifies
the
6510
loads
goes
into
to
the
memory
location
X-register.
register instructions)
is
This
900
to
find
instruction
really a COPY as
the
(as
the
data
are
which
all
it
the
the data
STX
SITore
the
contents
of
register
X.
in
the
specified address.
STX 1024
means
STore
contents
of
3C
register
into
Load
STA 1024
Store
STA 55296
contents
of
accumulator
in
contents
of
accumulator
in
1024
Store
the
55296
to
give
the
screen
STX 1026
Load
into
X-register,
display
contents
a
of
contents
(i.e. 1)
of
X-register
in
1026
STX 55298
RTS
END
screen white
End assembly
PAGE 1-9
At Menu select
The
screen
'R'
should
display
"A",
"space",
"A"
now
you
exercise
should
be
able
to
write
(both
simple
A's
programs
coloured
so,
as
an
Exercise 1.1
Load
the
Accumulator
'A1) in 1024.
Remember
to
directly with
put
"1"
into
55296
' 1 ',
to
give
display
the
this
display
(a
screen
the
colour
white.
pressing the RUN/STOP key together with the RESTORE key may return
the computer to
BASIC.
If pressing
RESTORE keys
together does not restore you to BASIC then the crash is not
recoverable and it will be necessary to switch the computer off and
on again, re-load the assembler and start again.
Exercise 1 .2
Write
your
name
in
the
top
left
hand
corner
of
the
screen.
One
Put
an
'X1
in each of the
four corners
of
the
screen.
Answer
on
page 9-2.
You
will
occupies
memory
need
the
to
1000
locations
characters.
the screen,
know
that
memory
is
the
screen
locations
needed
As we have seen,
to
of
from
provided
1024 is
the
1024
the
computer
to
25
2023.
rows
normally
The
each
of
1000
40
***
The
Load
and
Store
complemented by the
LDY
instructions
LoaD
register
address.
PAGE 1-10
that
we
have
met
so
far
with
data
at
specified
are
LDYIM
LoaD
register
Y_
with
data
specified
in
Immediate !fode.
STY
STore
the
data
^-register
in
the
the
diagrams:-
at
specified address.
These
in
1025
PAGE 1-11
or be able to interpret
LDA
LDX
LDY
LDAIM
LDXIM
LDYIM
STA
STX
STY
the following;-
RTS
ADC
For
many
operations,
treated
BUT
NOT
interchangeably;
ALL,
for
the
and
instance
registers
program
1.3
can
could
be
be
written:-
PROGRAM 1.3
START ADD? 828
LDAIM
1024
STA 55296
STA
LDX 1024
1026
STX
STX 55298
RTS
END
or
PROGRAM 1 .3a
START ADD? 828
LDAIM 1
1024
STA
STA 55296
1024
1026
LDY
STY
STY 55298
RTS
END
Because
of
rapidly
between
instructions
this
interchangeability
registers
exist
to
do
during
this
and
the
the
run
need
of
automatically.
to
swap
program
They
are
data
several
typified
by:Transfer
TAX
the
contents
of
the
Accumulator
Using this command in program 1.3 (to produce program 1.4), makes
the
program
result.
PAGE 1-12
little
shorter
but
manages
to
achieve
the
same
PROGRAM 1.4
START ADD? 828
LDAIM 1
STA 1024
STA 55296
TAX
STX 1026
STX 55298
RTS
END
When this program is run the screen should display two white "A"s,
one in 1024 and one in 1026.
Descriptions given of the codes so far have been spelled out in
detail.
However, as you are getting more used to the jargon, it is
reasonable to begin to abbreviate.
Prom now on, instead of "the
TAX
Thus, a summary of
is:-
Transfer A. into X.
TAY
TXA
transfer A. into .
Transfer Y into A.
Exercise 1.4
into
"A"
Exercise 1.5
Write
program
that:
Loads
a diamond
into
the
accumulator,
an
further
into X
screen
one in
Hint:
(1024 * 39).
the
bottom
locations
PAGE 1-14
left
for
the
hand
corner
colours.
and
the
corresponding ;memory-
CHAPTER 2
Jumping, Branching, Flags
Few real life programs proceed along a smooth uninterrupted path
without jumping or branching at some stage.
This chapter looks at
those commands and their uses and then examines the flags that
enable
the branches
to be controlled.
Unconditional Jumps
These
tell
the
program to
jump willy-nilly - no
JMP
For instance,
Put
conditions.
Only
LDAIM
look like
this:-
| JMP 834
[ STA 1024 |
STA
[ JMP
55296 |
833 |
Fig.
2.1
Such a jump routine doesn't really achieve a lot but it could, for
instance, be
figure
2.1,
used
for
In
STA
PAGE 2-1
Now this
can be
typed
in
PROGRAM 2.1
START ADDRESS?
LDAIM 1
JMP 834
RTS
STA
1024
STA
55296
JMP 833
END
Once again, it can be run by selecting 'R' on the menu and then
starting the program at 828.
When run, it should give a white "A"
in the top left hand corner of the screen.
When
the
program
jumps
are
exactly
used
where
in
to
this
jump
way
to,
it's
i.e.
to
necessary
give
an
to
tell
ADDRESS,
the
hence
JMP 834.
Calculating these addresses is quite straightforward as
long as -it is done systematically.
For instance, all the commands
or
"SOURCE-CODES":
memory,
834,
thus,
to
RTS,
jump
LDAIM,
over
RTS
JMP,
in
etc.,
Fig.
take
2.2a
we
up
one
jump
from
byte
of
830
to
-PSEUDO
OPERATION
LDAIM
828
830
JMP
833
834
837
RTS
840
JMP
829
831/2
STA
835/6
STA
838/9
841/2
834
1024
55296
833
PSEUDO
Fig.
The
part
following
the
OP.
2.2a
source code
is
known
as
the
OPERAND
and
2 .
find a complete list of all the source codes available on the 6510
microprocessor.
find the entry
BYTES OPER.
zero.
Thus,
It
PAGE 2-2
came originally.
On page
which takes a single byte
values up to 255, a single
in the table for LDAIM is a
to
confirm
this.
Other
instructions
met
require
2.2a
shows
the
location
of
various
instructions
and
LDAIM 1
number up to 255
JMP 834
RTS
STA 1024
This
TXA,
etc.
STA 55296
This takes
JMP 833
This can also be seen by using the "LIST" command on the assembler
MENU.
Return to MENU and type "L" for LIST,
then tell the
assembler where to start listing, i.e. type in "828"
The screen will display:828 O33C
A9 01
830 O33E
4C 42 03
833 0341
834 0342
837 0345
840 0348
LDAIM 1
JMP 834
60
RTS
8D 00 04
STA 1024
STA 55296
JMP 833
8D 00 D8
4C 41 03
Fig.
2.2b
60 8D 00 04 8D 00 D8 4C 41 03.
PAGE 2-3
JSR
This
jJump to &ub-Routine.
is
another
jump
command
which
is
used
along
with
RTS
and
of
the
We
ASSEMBLER
10
GOSUB 200
830
JSR 834
200
REM***SUB-ROUTINE
834
STA 1024
300
RETURN
840
RTS
use
this
can
modify
program
2.1
to
instruction
instead
Fig.
2.3
PROGRAM 2.2
START ADDRESS? 828
Pseudo-code
Load
LDAIM 1
Accumulator
in
immediate
mode
with a V
Jump to the subroutine at 834
JSR 834
RTS
Return
BASIC)
from
subroutine
(i.e.
back
to
STA 1024
STA 55296
RTS
END
Pseudo-code
The advantage of RTS over JMP is shown in this program, as with RTS
it is not necessary to calculate the address for the jump which
organises
PAGE 2-4
the
return
to
the
main
line
of
the
program.
In program
2.1 we had to put in the JMP 853 to have the same effect as RTS in
this program.
The 6510 does this trick by use of the
LDAIM 1
JSR
summary of
given in Fig.
834
the
PC
counter
contents
during
execution of
this
is
2.3PC BEFORE
PC AFTER
EXECUTION
EXECUTION
828
LDAIM 2
828
830
JSR
830
834
PROGRAM
START 828
834
Fig.
2.3
This figure illustrates how the PC steps through the program until
it comes to the JSR command.
It then takes the jump command and
sets
bytes
can
thus
only store
i.e. 834-
one
address,
As it is only two
the
PC
uses
some
This area
Exercise 2.1
Write
program
to
put
in
the
accumulator.
The
program
is
to
PAGE 2-5
Conditional Jumps
We have
needs
already looked at
to
test
for
unconditional
conditions
needs
jumps
but
CONDITIONAL
any
program
JUMPS.
In
that
BASIC,
10
In this
line
the
values X and Y,
in memory,
are compared.
The 6510 carries out this operation in several different ways - one
of
these
is
by
REGISTER (SR),
SR
is
an
using
special
register
known
as
the
STATUS
eight
it
register
contained
like
eight
the
accumulator
treated
as
signals
or flags.
individual
or
or
the status
and
The
Y
register, but it is used quite differently from these.
Whereas the
other registers are used to store and manipulate bytes, the SR is
if
bit
one
and
used as
status
flag
(as they are known) at a time, either setting the bit value to '0'
f 1 ',
testing
flag
One
example
of
the
status
flags
to
is
the
flag
or
the
ZERO
flag.
this way: if the flag is SET (to '1') then the condition (or
status) is TRUE, if the flag is CLEAR (to '0') then the status is
FALSE.
that
different
it is
true
that
instructions
can
the condition is
set
this
flag,
so
zero.
one
of
these
being:-
DEX
in use.
PROGRAM 2.3
(In Part)
START ADDRESS? 828
LDXIM 100
DEX
It
loads
This
with
facility
for
'100'
and
then
indexing both
flag is set
control
PAGE 2-6
the
to
1.
program
decrements -this
down
to
we
must
use
99
accounts for
If we wish to use
then
one
the
an
instruction
that
tests
the flag and brings about a branch dependent upon whether or not it
is set.
BEQ
(i.e.
branches as specified.
The operand in this case in only one byte
long, so only 0 to 255 can be accommodated.
As these 256 numbers
are needed for branching in both directions 0 to 127 are assigned
to
forward
jumps,
for
example
f60f
giving a forward
say,
jump of 60
In the case of
PROGRAM 2.3
LDXIM 100
DEX
BEQ 3
JMP 830
STX 1024
STX 55296
RTS
END
with
many
X-register
instructions,
DEX
has
corresponding
register instruction:-
DEY
Exercise 2.2
Write a program to
carry out
BNE
This does the reverse of the BEQ command and branches if the Z flag
is
NOT
Notice
set.
Program
how the
2.4
original
(page 2-8)
program
is
is
a modification
shortened
considerably
of 2.3.
by
the
PAGE 2-7
PROGRAM 2.4
100
DEX
BNE.253
STX 1024
STX 55296
RTS
END
index
identical
in
commands.
As
you
might
expect,
they
effect
to
also
be
indexed
upwards.
INcrement
the
contents
of
by
one.
INY
INcrement
the
contents
of Y by
one.
this.
CPX
ComPar.e
instructions
to be
examined
is:-
register.
thus
give
positive,
the
negative
or
zero
value.
Thus,
the
following:-
1.
2.
Subtract
3-
NOTE:
Neither
the
contents
register are
At
this
value
instruction
for
we
comparison
PAGE 2-8
of
the
memory
interested
can
in
2.5.
set
in
the
memory
the
location
nor
the
operation.
zero
register
somewhere.
condition.
at
The
zero
flow
To utilise
and
store
diagram
for
Load 90
into A
Store A
in 890
Load "0"
into Xregister
Jfc
Jump back
Increment
Compare
X register
890 with
by 1
X register
to
increment
YES
Return
Store X
to
in 1024
BASIC
Fig.
2.5
Load
LDAIM 90
90
(a
diamond)
Accumulator.
Store contents
STA 890
of
into
Accumulator
in 890,
LDXIM 0
Load
INX
Increment X register
'0'
into X register
Compare
CPX 890
value
in
register
BEQ 3
forward
three
bytes
if
CPX answer=0 .
Jump to memory location 835
JMP 835
STX 1024
LDAIM 1
STA 55296
RTS
Return
BASIC,
from
machine"code
to
END
Just
to make
that clearer,
various
stages,
the
program
steps
first
second
time
time,
that
and so
the
where stage 1
through
INX,
represents
stage
the
on.
PAGE 2-9
with
Accumulator
Loop
Number
'0f,
this
X-register
contents
contents
is
immediately
Z-flag
90
90
90
88
90
88
89
90
89
90
90
etc.
90
Fig.
2.6
At stage 90, BEQ is activated and program jumps 3 bytes to STX 1024
and then RTS.
Now type in the program and run it.
1024.
The compare instruction CPX has a corresponding instruction for the
Y register:
CPY
Compare
the
contents
of
specified
location
with
Re-write
CMP
This
is particularly useful as
the
results
of all arithmetic
operations are deposited in the Accumulator and CMP allows a direct
PAGE 2-10
PROGRAM 2.6
START ADDRESS? 828
LDXIM 0
Load a
LDAIM 83
INX
Load an '83'
'0'
into X.
Increment X
STX 900
Store X
in 900
CMP 900
Compare
A with 900
(a heart) into A
BNE 247
Branch
STX 1024
LDAIM 1
Store X in 1024
Load a '1' into the Accumulator
if Not
Equal
STA 55296
Store
display
Return
RTS
from Subroutine
END
Let
CPX
us
have
to
another
put
sledgehammer
'1 '
to
crack
readily set.
passed
from
registers
process.
look
In fact,
memory
or
when
Thus,
at
into
the
rather
2.5
flag
small
It
was
the
nut
zero
Accumulator,
answer
program
2.5
is
turns
a
as
to
a
program
the
the
that
like
Z
using
using
flag
is
very
to
the
obtained
could
out
bit
have
X
to
been
or
an
to
the
arithmetic
written
omitting
To demonstrate
and
DEY
and
this,
testing
the
command
BNE,
as
shown
in
figure
2.7
below:-
Load
42
into
.A
Load
As z\
100
into
Decrement
\=1 /
\ /
Y
.No
Fig.
Yes
Print
out
42
2.7
PAGE 2-11
And
the
program:-
PROGRAM 2.7
START ADDRESS? 828
LDAIM 42
LDYIM 100
DEY
BNE 253
STA 1024
LDAIM
STA 55296
RTS
END
The
instruction
BNE
253
gives
branch
of
256-253.
from
branch is
the
forward
beginning
of
the
next
DEY
This
is a
as the step is
instruction
whether
the
or backward.
have a go,
it should put a
Exercise 2.4
Now write
Start)
'Are
Load
Load
"0"
"80"
Increment
Register
Compare
into
into
X with Y
X & Y
^equal
No
Print
out
Yes
value
of Y
Fig.
If
you
run
flow-diagram
into
on
page
difficulty
9-5
should
though.
PAGE 2-12
2.8
with
help.
this,
the
more
PLEASE don't
look
detailed
at
this
Flag
Fig.
2.8
It
Negative
results
flag.
Set
in a negative
when
an
result.
arithmetic
The
flag
is
operation
controlled
PLA,
PLP,
Overflow
ROL,
flag.
ROR,
Set
TAX,
when
TAY,
an
TXA,
CPX,
LSR,
TYA.
arithmetic
and
operation
tells that
RTI,
Break
The
CLV,
SBC.
flag.
Set
when
programmed
interrupt
is
Decimal
be
flag.
carried
Set
out
when
Interrupt
arithmetic
in decimal.
flag.
PLP,
The
RTI,
operations
flag
is
Zero flag.
in a zero
Set
sequence
ADC,
ASL,
BIT,
CMP,
CPY,
DEX,
DEY,
EOR,
INC,
INX,
INY,
LDA,
LDX,
LDY,
PLA,
PLP,
ROL,
ROR,
RTI,
SBC,
TAX,
TXA,
TYA.
The
Carry flag.
or a 'borrow'
Indicates
the
presence
is
in
instructions
answer.
instructions
to
by
SED.
operation.
The flag is controlled by the
BRK, CLI, PLP, RTI, SEI.
are
controlled
of
results
by the
CPX,
DEC,
LSR,
ORA,
'carry'
Also set
PAGE 2-13
loss of a bit.
The flag is
CLC, CMP, CPX, CPY, LSR, PLP,
controlled by ADC,
ROL, ROR, RTI, SBC,
ASL,
SEC.
TheN-Flag
The N-flag, which is bit 7 on Fig. 2.8, is the Negative flag that is
set to '11 when the answer to an operation is negative.
It can be
tested by two instructions,
I BMI
An
ISranch on Mlinus.
instruction
such
as
BMI
27
would
test
the
flag
and
if
set
consequently
ceases
to branch.
PROGRAM 2.8
START ADDRESS? 828
LDAIM 90
Load 90 into A
STA 900
Store
Accumulator
contents
in 900,
LDYIM 0
INY
Increment Y
CPY 900
Compare
contents
contents
Branch on minus,
BMI 250
of 900 with
of Y,
i.e.
test N
flag
STY 1024
LDAIM 1
STA 55296
Store
in
colour
RAM
to
get
white display
Return from subroutine
RTS
END
On running,
In addition
to
the
BMI
command,
the
flag
is
also
tested
by
the
command:-
BPL
IJranch on PLus.
A command such as BPL 12 would test the N flag and if not set
branch the program forward 12 bytes.
Program 2.9 uses BPL to test
the
flag
and
to
branch
when
not
set.
In
this
program
memory
PAGE 2-14
the CPY test yields a minus at 90 - 91 and the Branch on PLus (BPL)
test fails allowing the program to
run through
to
the end.
PROGRAM 2.9
LDAIM 91
STA 900
LDYIM 100
DEY
CPY 900
BPL 250
STY 1024
LDAIM 7
STA 55296
RTS
END
from 90,
then print
out
the
current
value
of X.
PAGE 2-15
CHAPTER 3
of
the
advantages
operation
and
Animation,
for
of
machine
this
naturally
instance,
can be
code
programs
is
facilitates
achieved
their
screen
by means
of
speed
of
displays.
commands
such
as:-
STAX
SITore
specified
address
indexed
with
the
jC
register.
Thus,
if X contains
100 and
the
instruction
STAX 1024
will
put
increment
be
diamond
into
instruction,
indexed.
(1024+100).
this
Program 3*1
enables
When
the
demonstrates
used
location
along
on
the
with
screen
the
to
this.
PROGRAM 3.1
START ADDRESS? 828
LDXIM 100
Load
LDAIM 90
STAX 1023
100 into X
LDAIM -1
Load
STAX 55295
Ensure
into A
colour is white
DEX
Decrement X value
BNE 243
Branch on not
equal
RTS
END
When run,
the
screen.
Once
again,
the
'X1
in the first
command
has
100 locations on
corresponding
'Y'
command:
STAY
STore
the
contents
specified
address
indexed
with
the
Y_
register.
Exercise 31
Modify
program
register,
3-1
to
use
the
register
rather
than
the
of
the
an
decrement
have
used
increment
with
100 in order to
set
would
the
zero
then
have
flag to a
the
had
'1*.
to
program
do
could
compare
Try this
as an
exercise!
PAGE 3-1
Exercise 3.2
an
command
asterisk
to
in
the
first
100
screen
locations
using
an
INX
increment.
In
exercise
3*2
the
branch
instruction
from 255,
It
be used
can
set
thus
to
the
to
255 and
clocks back to
branch without
appropriate
function to 3.1
216
it
to
However,
value.
was
zero and
a
compare
Program
3.2
out an asterisk.
39
loops
Once at 255
activated
through,
by
zero
if the X or Y register is
plus
the 8 bit
resets
if it
the
is
performs
Z flag.
initially
similar
It increments X from
the next loop,
register
is
prints
full
of
Ts
and the addition of one more '1' ripples through each bit of the
store,
resetting them all to
'O's.
The 6510 does notice this
program
sets
offers
particular context
no
flag
to
advantage
remember
over
3*1
on
the
event
(discussed
its
in a
be advantageous.
PROGRAM 3.2
START ADD? 828
LDXIM 216
LDAIM 42
STAX 808
LDAIM 1
STAX 55080
INX
BNE 243
RTS
END
As
with
BASIC
programs
by filling the
space behind it.
PAGE 3-2
character
screen with
the
can
be
moved
across
the
screen
PROGRAM 3-3
START ADDRESS? 828
LDXIM 0
LDYIM 32
STY 900
LDAIM 90
STA 901
STAX
1024
LDAIM 1
STAX 55296
TYA
STAX 1023
LDA 901
INX
BNE 238
RTS
END
When
run,
1279-
the
You
animation;
program
may
be
runs
quite
if so please
through this
at
white
home
diamond
with
this
across
the
screen
to
technique
of
general
If not,
stage by stage.
Command
900
901
LDXIM 0
LDYIM 32
32
STY 900
32
32
LDAIM 90
90
32
32
STA 901
90
32
32
90
STAX 1024
90
32
32
90
90
32
32
90
32
32
90
1024
1024
1024
1024
1023
LDAIM
STAX 55296
TYA
32
32
32
90
STAX 1023
32
32
32
90
LDA 901
90
32
32
90
INX
90
90
32
32
90
BNE 238
32
32
STAX 1024
90
32
TYA
32
32
32
STAX 1023
32
32
etc.
STAX 1024
LDAIM 1
STAX 55296
90
90
90
32
32
90
90
1023
1023
1023
1025
1025
90
32
90
1024
32
32
32
90
90
32
32
90
90
0
0
90
32
32
32
90
until
90
1
1
TYA
32
STAX 1023
32
255
255
255
255
255
255
32
32
90
32
32
90
1279
1279
1279
1279
32
32
90
1278
90
90
32
32
32
90
1278
32
32
32
90
90
1278
1278
32
32
32
32
32
32
90
1278
32
LDA 901
90
INX
BNE 238
90
90
RTS
90
Fig.
3.1
PAGE 3-3
As
written,
job
and
best;
program
as you
no
3-3
nevertheless
required.
Later
acceptable
first
does
in
of
by
no
means
observed,
it
on
version
look at
is
doubt
the
is
flash
this
the
it
the
but
way
diamond
chapter
program,
only way
long
we
in
doing
being
to
will
order
of
from
where
develop
to
do
the
the
it's
more
this we must
pages
ago
it was
one
of
the
an advantage!
problems
SPEED.
runs
Thus
code
- a
in BASIC
few
it's not
which in
operation
number
of
of
the
various
cycles
EXECUTED.
Some
that
of
retrieve
takes
data
Clearly,
executed
is
operation
to
important
the
will
for
operations
memory.
The
be
them
take
of
as
program
the
time
it
and
is
be
place
taken
for
by
out,
that
allows
to
carried
entirely within
instruction
this
also
referred
to
knowledge
of
takes
from
two cycles
instructions
it
these
to
of machine
Whereas
TAX,
for
the
or
the
required
instance,
six.
the
instructions
determines
use
to
be
the
to
speed
made
of
be
of
the
Looking
the
back
screen
at
program 3*3,
prior
to
the
"moving
on"
and
time
can
that
be
the next
character
calculated.
few stages are
below:-
Command
Execution time
(cycles)
Elapsed
STAX 1024
LDAIM 1
STAX 55296
TYA
STAX 1023
14
LDA 901
18
INX
20
BNE 243
22
STAX 1024
27
PAGE 3-4
Time
(cycles)
remains
Thus,
on
the
tabulated
29
STAX 55296
34
TYA
36
STAX 1023
41
LDAIM
So,
41
cycles
written
or
in
20.5
about
micro-seconds.
5248
Overall,
micro-seconds
or
Indeed,
the
5-2
256
diamonds
milli-seconds,
are
much
be programmed
Such
simple
when
device
is
implementing
in
in
actual
principle
but
application.
may
need
Program
3.4
some
care
below
shows
PROGRAM 3.4
START ADDRESS? 828
LDXIM 250
2 cycles
DEX
2 cycles
BNE 253
3 cycles
RTS
This
gives
(ignoring LDXIM's
loop,
or 250x5=1250
through, a delay of
must be augmented
program 3*5 shows.
cycles)
a delay of 5
nesting
this
loop
cycles
per
within
another
one
as
PROGRAM 3.5
START ADDRESS? 828
LDYIM 200
2 cycles
LDXIM 250
2 cycles
r-^DEX
2 cycles
DEY
3 cycles
2 cycles
3 cycles
1BNE 253
1
BNE 248
RTS
A complete run of this program would run the DEX subroutine 200
times, i.e. achieving an overall delay of 200 x 625 micro-seconds,
or '/8 of a second.
PAGE 3-5
If we
wish
to
clearly not
must
step
account
use
the
computer
ignore
the
odd
very
carefully
for all
parts
of
for
through
the
when
instruction
it
the
branch
takes
only
the
timing,
here
program
program.
precision
two micro-seconds
then
and
to
we
there
ensure
can
and we
that
we
instance,
the BNE in program 3-5
i.e.
when
the
branch
succeeds.
fails
and
the
two
cycles.
to
the
program
Under
runs
other
past
.the
conditions,
if
the branch takes the program into another section of memory (i.e.
another "page", see page 3-9 for discussion), the instruction takes
an extra two cycles!
Another
chip
problem
which
What with
peculiar
controls
its
the
Sprites
screen
and
all,
Commodore 64
display
the VIC
to
can
is
that
interfere
ohip has
with
the
VIC
timing.
so much work to do
reinstate
especially
the
in the
display
afterwards
not
very
difficult
to
do,
Assembler.
3.6
illustrated
uses
3-3
as
basis
and
inserts
the
the
PROGRAM 3.6
START ADDRESS? 828
LDYIM
LDAIM
90
STA
900
Set up a diamond
LDAIM
STA
901
LDAIM
32
STA
902
LDXIM
250
LDA
900
STAY
1024
diamond
LDA
901
Load
STAY
55296
Set up a blank
colour white
Delay
DEX
253
LDA
902
loop
Load blank into accumulator
STAY
1024
Display blank
230
BNE
INY
BNE
RTS
PAGE 3-6
delay
loop
appearance
enough.
The
second
television
(European
roughly
PAL)
screen
or
white
thirtieth
diamond
of
that
is
for
only
time,
is
i/60th
16-20 milliseconds
little
running it.
Disappointing isn't
it?
the
only
refreshed
second
the
on
(USA
screen
the
chances
to
screen
are
be
so
i/50th
it
takes
scanned.
for
that
every
NTSC)
If
our
approximately
one
only one
in thirty of
If you
look
carefully,
detect
that
the
and
have
situation is a bit
line.
My
something
case,
it
engineering
fifteen
and
we
look
better
than
that,
tell
me
you
will
roughly three
that
this
is
or four
because
of
In any
3*6
is
too short.
the delay?
are
carefully,
times
increase
of 255
to
friends
called 'interlace'
appears
that
the
therefore about
maximum
you
already using
count
of
250,
so
there
isn't much scope for increasing the number of times around the loop
unless
we
extent
use
the
double
loop
illustrated
each
the
BNE
loop,
could
branch
at
900 instruction
the
loop
to
the
it
23.
we
make
end
would
This
it
of
the
more?
delay
the
mean
16-20
the
program
900.
milli-second
so
that
When you
run
scan
the
it
BNE
to
of
But
the
loop as well as
the
answer
went
number of
that
see
The
loop
each
the
program 3.5.
use
increase
would
in
delay
the
loop
yes.
If
the
LDA
consumed by
diamonds
enough
diamond.
see
to
cycles
of
still not
every
is
back
Try
goes back
roughly one
would
time
for
modifying
to
in
the LDA
three of
the diamonds.
We
need
to
increase
original program
One
possible
The
problem
the
character
solution
is
that
of
register
retrieve
it
by
factor
is
to
use
the
screen
the
double
uses
and
the
we
It
memory
after
is
always
prior
the
loop.
to
possible
entering
When
used
of
fifteen
in
the
of
program
Y register for
cannot
course.
in
delay
program 3-6
across
the
loop,
to
use
or can we?
save
the
in
therefore
the
delay
this
value
loop
way,
3.5.
indexing
the
the Y
Well we
in
and
the
then
lack
of
more than two registers in the 6510 can be overcome quite readily at
the expense of a
PAGE 3-7
PROGRAM 3 .6A
START ADDRESS? 828
LDYIM
LDAIM
90
STA
900
Set
up a diamond
LDAIM
STA
901
LDAIM
32
902
Set up a space
LDA
STAY
900
Load a diamond
1024
Display it
LDA
901
Load
STAY
55296
Store
STY
LDYIM
903
15
Set
outer loop
LDXIM
250
Set
inner loop
253
STA
on the screen
colour white
in colour RAM
DEX
BNE
DEY
BNE
248
Count down 15
LDY
903
LDA
STAY
902
Load blank
Display blank
1024
INY
219
BNE
done
RTS
END
Enter
this
program
demonstrates
slowed
each
41
and
run
it.
Brilliant
isn't
it!
It . really
the
machine
useful
code
work
down
cycles
by
adding
and
you
code
imagine
on
how
to
much
Modes of Addressing
When
moving
instructions
character
STAX
and
own and
this
is
STAY
across
as
the
these
screen
are
able
we
have
to
index
used
the
along
with
relative
of STAX,
STAY,
etc.
The
fact,
mode
of
command,
PAGE 3-8
the
STA
instruction
addressing
designed
used.
has
Thus,
to modify its
seven
the
varieties
address
is
depending
on
the
a modifier
of
the
The
address
either
essentially
directly
determined
by
or
the
points
the
6510
indirectly,
the
particular mode
of
to
way
location
it
does
addressing
in
memory
this
being
used.
Addressing
is uniform throughout the 64K of possible memory except for the 256
locations from 0 to 255is
needed
whereas
of memory is
all
To address
other
these locations,
locations
given a special
name
need
ZERO
two.
PAGE
Hence,
this
area
of addressing.
In fact, the whole of the memory that the 6510 can
address is divided up into pages of 256 bytes and an instruction
that
causes
extra cycle
In
this
book,
use of a
a
'Z'
convention
that
operation
over
the
boundary
between
pages
takes
an
to be executed.
really
Zero
page
memory
on the instruction,
addresses
in
e.g.
STAZ.
chosen
here
and
matters
is
that
is
by
the
no
means
zero
are
indicated
However,
a universal
page
STA
by
the
this is only
one.
command
All
has
the
2).
Implied Addressing
This mode,
is probably
i.e.
group
reference
where
an
external
TYA,
transfer Y to A and
is
necessary,
e.g.
the
RTS
other
return
from subroutine.
The
the
first
INX,
members
INY,
TAX,
of
TAY,
TXA,
TYA,
group
while
already
CLC,
CLD,
CLI,
CLV,
NOP,
SEC,
SED,
considered
those yet
SEI.
Those
to
in
are:
DEX,
DEY,
be discussed are:
are RTS (already used) and BRK, PHA, PHP, PLA, PLP and RTI.
Absolute Addressing
Instructions using this mode are among the easiest
to understand as
their operand
is
a
two
byte number that' defines
the
address
absolutely.
Thus in program 3-6, page 3-6, the instruction STA 901
tells the X register exactly where to store its contents.
Later in
the same program LDA follows the same pattern.
Instructions
already met;
STY,
while
that
ADC,
utilise
CMP,
those yet
CPX,
this
CPY,
form
JMP,
of
JSR,
to be discussed are:
addressing
are
STA,
those
LDA,
LDX,
LDY,
AND,
EOR,
STX,
PAGE 3-9
Zero-Page Addressing
This
form of addressing is
but
is
restricted
advantage
compared
book
'Z1.
mode
with
the
four
addresses,
is,
few
of
used
get
of
page
the
zero
not
these
that
using
faster
sub-set
its
zero
it
to
executes
for
the
zero-page
execution
in
255
absolute
modes
times
The
only
modes.
are
locations
code
way
are
programmer,
around
for machine
the
not
used
In
using
code
to
page
zero
interpreter
and
254.
interpreter
this
with
programs.
by BASIC
notably 251
BASIC
cycles
suffixed
when
major
three
page
your
of absolute addressing
operand
readily available
by the machine
know
of
required
instructions
thus,
to
is
this
Because
and
size
of
those
really a
in
may be
When you
then
you
will
even
when
location because
will
often
BASIC.
find
you
cannot
it would
it
write
to
interfere with
useful
to
read
the
particular
the
BASIC
zero
page
interpreter,
information
stored
you
there
by
Three of the locations in page zero that are useful are 160
to 162, which contain the jiffie clock that increments every 1/60th
of
byte
second.
Program
of
into
this
3*7
the
is
very
accumulator
simple
and
program
then
that
prints
it
loads
one
onto
the
screen.
PROGRAM 3-7
START ADDRESS? 828
LDAZ 160
Load
STA 1024
accumulator
LDAIM 1
STA 55296
RTS
Return
from
in
Zero-page
machine
code
subroutine
Because
of
their
limited
to date
in
range
this
of
use,
book.
no
zero-page
Those
that
instructions
use are:
ADCZ, ANDZ, ASLZ, BITZ, CMPZ, CPXZ, CPYZ,
INCZ, LDAZ, LDXZ, LDYZ, LSRZ, ORAZ, ROLZ, RORZ, SBCZ,
DECZ,
STAZ,
EORZ,
STXZ,
STYZ.
Immediate Addressing
This
mode
i.e.
directly as
as
a means
of
addressing allows
specified,
of comparison.
a number
into
All
'IM1
the
to
immediate
loaded
to
be
immediately,
used directly
commands
PAGE 3-10
be
register or
the
in
this book
This format is
format:
Normal-instruction #operand
instead
of
LDAIM 90
LDA
Many
examples
for
example
of
Accumulator
same
CPYIM
directly
many
resulting
the use
addresses
page
register and
However,
mode,
demonstrates
mode
36,
loaded
the X
way.
immediate
program
was
programs both
the
immediate
in
#90
using
been
In
LDAIM
and
in
been
can
already,
program,
32,
instructions
neater
seen
this
other
in
have
3-6.
be
programs.
the
other
loaded
used
in
Program
in
the
38
of:-
PROGRAM 3.8
START ADDRESS? 828
LDYIM 0
Load Y with
TYA
Transfer Y
INY
Increment Y
STAY
1023
LDAIM
Store
Load
'0f
to
contents
A with
STAY 55295
Store
CPYIM
Compare Y with
100
of A
in
1023+Y
in 55295+Y
BNE 242
Branch
RTS
Return
if Z
100
flag not
from
set
machine
code
subroutine
On
running,
the
character set
program
in the
first
prints
the
first
100
characters
of
the
Indexed Addressing
In
this
mode,
register added
an
address
is
calculated
to a specified address.
using
the
contents
of
frequently
thus
the command
STAX 1024
would
be written:
STAX
When
using
indexed
1024,X
addressing,
the
and
registers
behave
i.e.
PAGE 3-11
ANDX,
RORX,
Those
ANDY,
indexed
ORAY,
SBCY,
with
are:
ADCY,
ASLX, CMPX,
SBCX, STAX.
CMPY,
EORY,
DECX,
EORX,
LDAY,
LDXY,
STAY.
N.B.
Exceptions
to
the
generality
are
that
STY
indexed
with
DEC,
LSR,
ROL,
ROR CANNOT be
Y.
The source codes that can be used in zero-page mode have the suffix
'Z'
added
to
this
to
identify the
register used.
Thus
the
instruction
ADC
becomes
ADCZX.
The
total
list
of
zero-page,
EORZX,
STAZX,
STYZX.
register
index
Relative Addressing
Many programs used so far have utilised
relative addressing
in
which a branch has been defined relative to the current position of
the program, i.e. the operand expresses the desired displacement.
In program 3.8 the instruction BNE 242 was used to test the setting
of the Z flag and to branch when it was not set.
All the branch
instructions used so far utilise relative addressing and it is this
group as a whole that uses this mode.
The group consists of:BCC,
BCS,
BEQ,
BMI,
BNE,
BPL,
BVC,
BVS.
Indirect Addressing
This
that
pointer
the
operand
that
contains
directs
It gets the
is
a pointer and
the
6510
not an
towards
the
address.
memory
It is
location
this
that
the address.
However,
once
considerably
again,
in
addressing.
the
operation
All
the
and
and
give
instructions
indexing
rise
to
that
utilise
an
'IY1
or an
locations
PAGE 3-12
in
the
mechanisms
different
addressing are
'IX1
of all
differ
sub-modes
this
kind
of
of
operand.
zero-page
Because
of
this
indirect
indexed
with
addressing
(i.e.
using
added
to)
the
the
register,
contents
of
the
operand
is
register
to
the
produce a pointer.
This location and the one after it are then
examined and their contents provide addresses for the required data
in the order least significant byte, most significant byte.
just
to
demonstrate
one
instruction
in
use,
program
3.9
uses:-
This
88.
is
used to retrieve four bytes stored in page zero from 84 This is an area of RAM used for numeric storage in program
3-9.
PROGRAM 3.9
START ADDRESS? 828
When
run,
screen
upon
LDXIM 0
LDAIX 84
STAX 1024
LDAIM 1
Store A in
STAX 55296
Store
1024+X
in colour RAM
INX
Increment X
CPXIM 4
BNE 241
RTS
Branch if not
the
locations.
what
Load A with
l0l
BASIC
What
had
four characters
been
doing
in
the
differ every
earlier.
When
"41
equal
first four
time
used
in
depending
program
This
character 1
Address
character 2
Address
most
character 3
Address 2
significant byte
least significant byte
MSB
LSB
character 4
Address 2
MSB
perhaps
latter
more
name
clearly
implies,
as
PRE-INDEXED
the
addressing
INDIRECT
is
ADDRESSING.
pre-indexed
as
the
As
or
the
X value
Instructions that use this type of addressing are:CMPIX, EORIX, LDAIX, ORAIX, SBCIX, STAIX.
ADCIX,
ANDIX,
PAGE 3-13
addressing
differently,
zero-page
byte
of
as
memory
the
this
the
operand
location.
address,
significant
to
using
the
byte.
the
This
next
Finally,
address
to
surprisingly,
this
POST-INDEXED
INDIRECT
form
the
of
the
location
index
operates
points
contains
memory
the
form
register
instruction
least
contents
final
indexed
is
as
the
to
the
most
are
added
address.
referred
indexing
significant
containing
register
addressing
ADDRESSING
somewhat
directly
is
Not
to
as
carried
out
after
64
you
have
wish
to
become
look
at
Commands
EORIY,
experienced
way
these
that
LDAIY,
an
the
use
the
Assembler
BASIC
instructions
this
ORAIY,
type
SBCIY,
of
user
interpreter
you
will
works
and
no
doubt
you
will
are.
addressing
are:-
ADCIY,
CMPIY,
STAIY.
of addressing is used
JMPIA
It
is
an
address
is
In
absolute
and
instruction
can
thus
in
that,
the address
the
but
Dr
some
instruction
only:
indirect
finds
by one
Watson
other
address
at
that
(LSB first
assembler
assemblers
it
may
in
any
that
location
then MSB)
is
the
location
operand
in
and
the
recognised
this
by
two
byte
However,
subsequent
for the
represent
is
memory.
one
it
it
jump instruction.
the
with
mnemonic
JMP
JMPIA
followed
by
the operand in brackets, e.g. JMP (844), which is Jump to 844 to find
LSB and 845
to
HEX
(81 iO
(3iO)
849 0351
STA 1024
This
then
program,
to
845
035116(849)
PAGE 3-H
on meeting
to
This
JMPIA 844,
retrieve
is
loaded
03
jumps
and
into
the
to 844
to
re-assembles
program
pick up. 51
the
counter
and
address
and
the
program
jumps
to
849
to
execute
the
program
stored
there.
As
represent valid
them.
then transferring
843 NOP
844 NOP
845 NOP
846 NOP
847 NOP
848 NOP
The
executes
not
JMPIA
the
then
routine
a subroutine
as
retrieves
stored
it was
these
there.
and
This,
entered
with
jumps
to
however,
a
JMPIA
$0351
where
it
is a routine and
and
not
JSR,
so
Addressing Generally
The whole subject
be
approached
carefully
are
not
assist
in
of addressing is
only
Appendix
absolutely
in
with
weeding
care.
2. before
certain.
out
To
basic
using
some
instructions
rule
any
addressing
extent,
that
must
don't
the
be
of
one
to
which
assembler
exist
but
it
to
check
you
will
can't
PAGE 3-15
CHAPTER 4
Mathematical, Logical Operators
On page
1-1,
as
early as
program
1.1,
numbers
and
that
arises
their
from
answer
the
gave
flag.
This
is
that
fact
no
that
they
carries.
The "simplicity" of
were
When
only single
larger
brings
about
number - clear,
carry
eh?
between
If not,
the
digits
numbers
two
bytes
carried
of
are
carry
two
out
byte
do
it
number
use
now!
as
of
Naturally
there
two
are
bytes
it
bytes
would
free;
which
is
be
possible
however,
rather
we
to
will
fancily
hold
as
large
now consider
described
as
the
DOUBLE
PRECISION operation.
to be successfully utilised
by
some
mechanism.
This
I
This
BCC
tests
for
observed
when
the
is
the
function
that
represent a
to
the second
the
carry flag
executes
that
to
the
branch
if
carry
this
flag
is
being
so.
clear,
One
i.e.
precaution
set
is
to
0,
always
and
best
That
is to ensure
the operation that
task is:-
This sets the carry flag to 'clear1 or '0' and is used prior to the
process that may reset this in program 4.1:
PROGRAM 4.1
NOTE:
As you are well aware by
CLC
now,
LDAIM 0
ADCIM 1
i.e."START ADDRESS?
BCC 252
From this
STA 1024
LDAIM 1
chapter onwards
this first line is omitted
in the listing but must
STA 55296
be added in.
RTS
PAGE 4-1
When
run,
this program progressively increases
the
accumulator
contents by 1 until at 255 the ADCIM 1 instruction flips the eight
Ts
over to eight 'O's and sets the carry flag to 1.
Thus when
the
accumulator
is
displayed
The
6510
second
has
with
the
instruction
test
instruction
for
the
STA
1024,
carry
it
flag,
is
this
being:-
IBCS
if
the
test
illustrates this
is
positive,
'set1,
executes
i.e.
containing a '1',
branch.
Program
I
i
4.2
instruction in use:
PROGRAM 4.2
CLC
LDAIM 0
ADCIM 1
BCS
JMP
STA
831
1024
LDAIM 1
STA
55296
RTS
Once
again,
this
accumulator with
sets
the
carry
accumulator
program progressively
fills
'1's and'finally,
flipping
flag
and
contains
on
terminates
all
'O's
the
and
all
eight
these
program.
therefore
bits
the
to
the
end
the
white
*@*
is
At
of
over
'O's
Let's have a go
we'll take 1257.
to
do
this,
of
at
adding
First we
course,
hexadecimal format.
have
to
convert
INT (1257/4096)
=
INT (1257/256)
=
INT (1257-4x256)/16)=
(1257-4x256-8x16)
==
0
4
8
5
therefore
therefore
therefore
therefore
= 048516
PAGE 4-2
number
Right-most character
2nd character
3rd character
4th character
Thus:
Hex 125710
the
Thus:-
=
=
0
4
8
5
to
To add together two 125710's we must first add the LSB's, check if
there is a carry and then add the MSB's taking into account the
plus carry
OA
+ OA
16,10= carry
04
+04
08
Next add
in the
carry
08 + carry = 09
In
the
explanation
operation that
to a
The
'1'
next
the
have
glibly
said
operation
and
it
is
this
that
is
carried
out
"0"
With C
04 + 04
08
04 + 04
answer
then
takes
account
of
this
With C flag =
the
"+carry"
Thus
we
C-flag does
to
the
example
"1"
flag =
is,
09
in
Hex
090A,
or
in
decimal
9x256+10 = 23141ONow let's try doing that the long way - using the computer!
We can rely on the 6510 handling the
knowing
when
to
execute
carry!
double
precision
work
is
operation
done.
You
may
that
remember
addressing commands
the
is
carry
significant
it
that
stores
arises
byte
when
the
LSB
and
(MSB)
the
of
it
is
then stored up
addition when
6510
the
is
using
address
first
this
is
indirect
and
the
'numbers'
In order
to
ensure
that
the
the
the
either way
by a carry,
PAGE 4-3
First,
we
must
work
out
the
value
of
MSB
and
LSB
in
8 x 16 + 5
and
for MSB it
to
write
as
to
is
0 x 16 + 4
Now
decimal,
discussed
the
410
program
but
before
we
do
that,
let's
just
No OPeration
NOP
When
the
6510
meets
it,
it
does
nothing
for
two
cycles
of
its
operation.
for now,
PROGRAM 4.3
CLC
CLD
Explained
LDAIM 133
ADCIM 133
STA
1026
LDXIM 1
Load
STX
NOP
Store it
later
55298
Do nothing
LDAIM 4
ADCIM 4
STA
STX
1024
55296
Store
RTS
in colour RAM
Return
from
subroutine
machine
code
END
Having done
it
screen.
i.e.
PAGE 4-4
I or 9^q
9OA16
a&d J or 10-jq
231410
Accumulator
X Reg
1024
C Flag
START 828
CLC
133
133
133
133
133
133
4
9
9
9
9
CLD
LDAIM 133
ADCIM 133
STA 1026
LDXIM
STX 55298
NOP
LDAIM 4
ADCIM 4
STA 1024
STX 55296
RTS
the
above
chart
10
10
10
10
10
10
10
9
9
9
10
1
Fig.
As
1026
shows,
at
the
10
0
0
4.1
ADCIM
133,
carry
is
generated
ADC.
notice
The
other
thing
to
which
to
check
would
clear
this
the
the
you
flag
is
that
C flag is
could
after
at
the
replace
it
ADCIM
therefore
has
the
NOP
been
further
cleared to
command
set
and
'0f.
with
notice
'incorrect1.
This can be done by POKEing into 842 the code for CLC,
i.e.
24:
PROGRAM 4-4
POKE 842,24
Now,
when run,
H J
In this run,
266 overflowed, 256 was carried, the carry bit set and 10
stored in the accumulator.
However, this 256 was lost when
was
the
conversions into and out of hexadecimal which were carried out when
However,
PAGE 4-5
in hexadecimal,
providing it is preceded by a
'$'
sign.
Utilising
this becomes:
PROGRAM 4-3a
CLC
OLD
LDAIM $85
ADCIM $85
STA
1026
LDXIM 1
STX
55298
NOP
LDAIM $04
ADCIM $04
STA
STX
1024
55296
RTS
END
When
program
4.3a
program 4-3,
that
the
i-e.
is
run,
J.
assembler has
POKEd this
it
gives
Select
the
converted
exactly
list
the
$85
the
option
into
same
and
result
you
will
as
see
into memory.
Exercise 4*1
Using
hex
inputs,
add
together
10.
set
by
occurred
mistake
operation
then
1807^
and
2AFA15
it stores
the fact
this
carry
it
Verify
your
will
or
be
not.
added
For
that an addition
If the
on
the
C flag is
to
carry
the
to
left
next
be
ADC
passed
from the LSB to the MSB you have to arrange the ADC's to be done in
the appropriate order.
The
6510,
of
course,
SBC
e.g.
means
has
this
an
instruction
that
carries
out
is:-
the
data
SBC 891
look in memory location 891
and subtract
However,
in
the
same way as
it
was
to '0f.
PAGE 4-6
to
set
it,
rather
than
necessary to prepare
the
carry
it
to
'1'
rather
to
is
than
SEC
Let's
look at
that
T.
in a simple program
the immediate mode
to
to
take
load
from 4.
For a
the data.
PROGRAM 4.5
SEC
LDAIM 4
SBCIM 2
STA
1024
LDAIM
STA 55296
RTS
END
subtraction
the
on
your own so
have a
go
at
exercises
is explained
in
in fair detail
below
to
Exercise 4*2
Exercise 4.3
Write a program to subtract 50010 from the sum of 300^0 and 400^0-
Multiplication
The* arithmetic
and
instructions
subtractions
to
be
available
carried
out
for
but
the
no
6510
allow
provision
additions
is
made
for
multiplication.
This has to be carried out, therefore, by a series
of repeated additions.
For example, the sum 2x3 can be expressed
as
2+2+2,
is
thus
the
and
three
through
is
thus
relatively
one of adding 2
to
be
this.
before we do
set
Of
up
to
in
course,
simple
to
evaluate.
loop
the
to
define
accumulator
The
times and
the
number
should
process
requires
of
passes
contain
zero
the adding.
PAGE 4-7
4.2 below,
where Y = number of
passes through the loop (in this case 3), and A = current sum.
4.2
ADCIM 2
DEY
BNE
251
1024
STA
LDAIM 1
55296
STA
RTS
When run,
an
fFv
(a Commodore 64
f6f)
in 1024.
PROGRAM 4.5a
ADCIM 2
DEY
-BNE 251
this
multiplication.
algorithm
way
of
can
handle
double
precision
carrying
out
this
incrementing
is
by
the MSB.
means
of
the
instruction:-
INC
INCrement
the
contents
of
the
specified
memory
location.
Program
number
4.6
of
these.
PAGE 4-8
shows
carries
the
algorithm
generated
and
4.5a
to
elaborated
increment
the
to
MSB
record
to
the
record
PROGRAM 4.6
LDYIM 0
905
LDYIM 17
STY
LDAIM 0
/""it n
ADCIM
16
Add 16 to A once
BCC
INC
3
905
Increment
BNE
245
Check if Y decremented
STA
1026
If no
LDXIM 1
Load
carry generated
carry
then skip...
record
55298
LDA
905
1024
Display MSB
55296
zero
STX
STA
STX
to
in white
RTS
When run,
or
16x17.
times.
If
the
sum
2x100
was
attempted,
100 twice
tackled
one
hundred
subroutine
times.
that
ensures
This
that
could
the
be
multiplier
however,
then
it
by
is
writing
provided
short
with
the
smallest of the two values, and thus the algorithm is cycled the
least possible number of times.
However, on page 4-19 another
method of multiplication will be introduced - binary multiplication
- and this keeps the number or iterations down to
the minimum.
Division
Division using the 6510 is a process of repeated subtraction in the
same
way
that
multiplication
is
one
of
repeated
addition.
It
is
The
PAGE 4-9
PROGRAM 4.6a
LDYIM 0
LDXIM 2
STX
900
LDAIM 30
r-SEC
SBCIM 2
INY
CMP 900
BCS 247
STY
1024
LDXIM
STX 55296
STA 1026
STX 55298
RTS
letter 0)
in
addition
notation
to
also exists.
1,
cases
6510
chip
BCD
forms
greatly
can
the
represented
notation,
BCD
format of this
output.
arithmetic
in
binary
binary coded
and
decimal
decimal
or BCD,
is described
a bridge between
facilitates
handle
handling mode by
SED
being
or mixed
page A1-7-
many
numbers
hybrid
the
two
Fortunately
and
is
in Appendix
notations and
turned
on
in
for
us,
the
to
the
BCD
instruction:-
This
instruction sets
the
D
thereafter, arithmetic is done
operation
is
instruction:CLD
no
longer
flag automatically to a
'1 '
and
in BCD.
When the decimal mode of
required
it
is
cleared
with
the
You may
on
a
number
instructions.
remember
of
occasions,
prior
to
It should be clear to you
using
the
ADC
and
SBC
now that we were ensuring
simple
program
program 4.7.
PAGE 4-10
to
add
together
and
using
BCD
is
given
in
be
good
practise
to
clear
in 1024.
the
It is usually considered
decimal
flag
arithmetic,
just before
the RTS.
The
example
given
in
program
4-7
is
identical
in
effect
the CLD
to
the
in
is
If
POKE 831,6
POKE 836,6
PROGRAM 4.8
PROGRAM 4.7
SED
SED
CLC
CLC
LDAIM 2
LDAIM 6
STA
900
STA
ADC
900
ADC
900
STA
1024
STA
1024
LDXIM 1
STX
When
run,
LDXIM 1
55296
STX
CLD
CLD
RTS
RTS
saying 12!
900
LDAIM 6
LDAIM 1
a white
R in
5529
1024,
which
is
its
way
of
1810
However,
000100102
storing
two nybbles
3=110
Fig.
-210
4.3
or 12^q.
The above example emphasises the problems that arise when thinking
in decimal and working in binary.
As the 6510 itself works in
binary and won't easily be persuaded to change, it is necessary to
master the techniques of binary number processing.
PAGE 4-11
If
we
look
consisted
at
of
the
answer
to
program
4.8,
in
byte,
which
we
see
the
answer
one
of, the
problems
of bit manipulation.
What was needed in this case was a
technique for modifying individual bits within a byte.
In order to
extract the lower order nybble from the binary number it is only
necessary to erase the higher order nybble, i.e. to fill it with
'O's.
This can be done with an instruction:Perform a logical AND between the accumulator
AND
An Electronic AND
Fig.
4.4
Figure 4.4 shows an AND gate with two inputs A and B, and an output
C.
Its function is such that if both inputs, A AND B, are set at
T, then its output, C, is a '1'.
However, if either or both of
is
normally expressed
for the A,
B,
in what
is
known as
a TRUTH TABLE,
that
4.5.
Fig.
To use the
table,
appropriate inputs
a B input of '0'
4.5
of A and B.
Thus,
the output C is 0.
taking an A input of
'0'
and
output
(c)
Exercise 4*4
Using
the
truth
table,
AND B=0
AND B1
PAGE 4-12
fig.
4*5,
following inputs:-
work
out
the
logic
the
accumulator
simultaneously.
Thus
if
255
is
ANDed
with
then:
2551O
111111112
00000001.
10
i.e.
Own*r
^ Equals^
Fig.
The
result of the
operation
is
4.6
that
all
the
'0'
first bit.
Exercise 4.5
truth table.
the
strip
above
BITS
BCD 12
shows,
number
four
the
and
in one byte.
into
exercise
from
AND
could
This
be
BCD
instruction
used
to
can
convert
'12'
was stored as
read
out
be
used
part
of
to
the
two nybbles
zeros
then
the
byte
would
directly as
the
value
can be
done by using an AND command, as any '1' in the ANDing number will
leave Ts in the number ANDed. Also f0fs in the ANDing number
will switch any '1's in the ANDed number to 'O's.
Let's try that with the BCD 12.
BCD
12
BINARY 15
AND
BINARY 2
Fig.
4.7
significant
bits
have
been
erased
(binary 15io)
and
the
number
the four
converted
PAGE 4-13
PROGRAM 4.9
LDXIM 15
STX
900
Store X in 900
LDAIM 18
Load A with
AND
900
STA
1024
LDXIM 1
Store A in 1024
Load 1 for white display
STX
Store
55296
'18'
in colour RAM
RTS
When run,
ANDIM,
'B'
in 1024.
below:
PROGRAM 4.9a
LDAIM 18
ANDIM 15
STA
1024
LDXIM 1
STX
55296
RTS
When defining
somewhat
because
the
truth
differently
the
accumulator,
result
i.e.
of
table
from
of
the
that
the
AND
AND
used
instruction
in
operation
is
it
electronics.
deposited
is written
This
back
in
is
the
the output.
Accumulator*
contents
4.8
The 6510 chip also uses two other logical operators, one of these
being an OR function.
With the Mnemonics used in this book it is
referred
to as ORA.
ORA( accumulator)
PAGE 4-14
Perform
logical
inclusive
In
electronic
circuitry,
the
OR
is
OR
between
the
specified.
depicted
as
shown
in
Pig.
4.9.
An Electronic OR
Fig.
4.9
\. D
AN.
Truth table
Pig.
In action,
for OR
4.10
Binary of 14910
Binary of
5210
Binary of 18110
Pig.
4.11
PROGRAM 4.10
LDAIM 149
ORAIM 52
STA
1024
LDXIM 1
STX
55296
RTS
PAGE 4-15
(a white reverse-field 5)
in 1024.
As
with
AND,
ORA
has
several
modes
of
addressing
to
suit
different applications.
Third among the
logical operators
Perform
EOR
is:-
logical
accumulator and
Exclusive
the
data
OR
between
the
specified.
fig.
4.10.
4.12
of
the
function is
inputs
is
that
'1 '
Using
'1 '
this
Binary of
5210
Binary of 1611O
Fig.
The program to demonstrate that
4.13
is below:'
PROGRAM 4.11
LDAIM 149
EORIM 52
STA
1024
LDXIM 1
STX
55296
RTS
PAGE 4-16
EOR,
to
like
facilitate its
use
has
in programs.
Exercise 4.6
Calculate
(i)
the
result
of
the
following
logical
operations:-
(ii)
(iii)
(iv)
Write
a program to
651Q
instructions
within a byte
and
as
exist
a group
that
these
enable
lead
to
one
to
manipulate
bits
to
In
the
was
earlier
able
to
though,
become
to
example
isolate
extract
possible,
using
the
the
LSN
MSN
BCD
arithmetic,
the
from
the
byte.
using
the
available
however,
using
one
of
It
AND
was
instruction
not
logic.
the
bit
possible,
This
does
manipulation
commands:LSR
logical
Shift
of
the
specified
contents
one
When this
is done,
with the right-most bit falling off into the carry and a '0' being
put
into
the
left-most
bit.
Thus
the
LSR
command
operating
on
14910
M0 I 0 I 1 I0I 1 I 0 I 1 I
Fig.
4.14
As with most .6510 commands, LSR has several addressing modes and
the particular address used informs the 6510 where the data which
is to be shifted resides.
Thus:
LSRA
LSR 900
"
"
PAGE 4-17
LDAIM 149
LSRA
STA
1024
LDXIM
STX
55296
RTS
using
four
such
right
shifts
the
MSN
nybble
in
byte
can
be
moved into the place of the least significant nybble and the left
most four bits filled with O's.
This enables one to isolate the
MSN in a BCD calculation.
This
is
demonstrated
in
program 4.13,
this
time
absolute mode.
PROGRAM 4.13
LDYIM 18
STY
\Load
900
LDYIM 4
Set
LSR
900
DEY
places
BNE
250
LDA
900
STA
1024
to
right,
LDXIM 1
STX
loop counter
of 900
in white
55296
RTS
Exercise 4*7
Suppose that the answer to a problem in BCD is 86-jq.
decode
this
answer
left;
ASL
it is:-
Arithmetic
Shift JLeft:
PAGE 4-18
to
Shift
the left.
the
specified
The left-most bit is shifted into the carry bit and bit 0 is loaded
with a 'O1,
An ASL instruction
ASL of 149-j0
H910
CARRY
4.15
ASLA
ASL 900
e.g.
1024
LDXIM 1
STX
55296
RTS
Binary MultipUcation
We have seen from programs 4.5 and 4.6 that multiplication can be
carried out using a repetitive, or RE-ITERATIVE, process but we
have also seen that this is a lengthy process
and at times very
tricky.
However,
we
view of conventional
tackled
the
from a point of
binary has
its
own
First,
let*8
examine
our
way
of
doing
decimal
(conventional)
arithmetic.
Take the sum 13x14.
We define 13 as the MULTIPLICAND
and 14 as the MULTIPLIER and lay the multiplication out as below:13
X 14
Multiplicand
Multiplier
52
130
182
Fig. 4.16
PAGE 4-19
PAGE 4-19
In
this
the
conventional
lowest
partial
digit
product,
format,
of
the
i.e.
we
first
multiplier
4x13=52.
Next,
multiply
and
we
the multiplicand
store
this
multiply
i.e.
1x13,
the
and
as
the
by
first
multiplicand
then multiply
multiplier
and
happened to be
then
multiplied
10).
the
result
by
the
BASE
(which
partial products.
It
is
quite
using numbers
For example,
possible
to
perform
the
same
multiplication
process
in binary format.
to multiply 5 x 7 in binary,
510
0101
and
0111
710
(working
only
to
4 bits)
0111
i.e.
x 0101
0111
Current
total
0111
Partial Product 2
00000
Current
total 2 =
0111
Partial Product 3
011100
Partial Product 4
0000000
Partial Product
ANS
100011
=32+2+1
+ 351O
Fig. 4.17
With all digits in the multiplier equal to one,
bits
to
8-Bit MultipUcation
The
this
process
is
given
Answer-ANS,
D=Multiplicand,
R=Multiplier,
LSB=least significant bit of multiplier.
PAGE 4-20
in
figure 4.18,
N=current
bit
where
number,
Start
to 0
Set ANS
No
N 8
A
|n =
N -
Shift
1
<
Add
D by
Shifted
(N-1)
D onto
Bits
ANS
to
LEFT
No
Remove
LSB
Yes
Final
Answer
= ANS
Fig.
4.18
Working through this flow diagram for the example of 2-jq x 2-jq
D = R = 000000102
Successive passes
very
simple
past
example
A are
with
no
referred
carries,
suffice
to
as
A1,
only four
as
A2,
etc.
passes
only the
two
For this
through
the
right-most
N = 0
START
ANS
A1
N = 1.
1st LSB = 0
A2
N = 2
2nd LSB 1
N = 8
1) to left,
i.e. 00102^01002
add R to ANS,
i.e. ANS=0+00102=00102
N = 8
A3
A4
- 3
N - 4
N - 8
N = 8
3rd LSB 0
4th LSB - 0
for
the
remaining
four
PAGE 4-21
PROGRAM 4.15
LDXIM 2
LDYIM 8
STX
STX
Initialisation
901
902
LDAIM 0
LSR
|
BCC
CLC
ADC
901
-ASL
901
Multiply1
if necessary
DEY
BNE
checked
240
STA
1024
LDXIM 1
STX
55296
RTS
It may be
checked
different
by
changing
the
LDXIM
instruction
to
input
Re-write
program
4.15
so
as
to
multiply
two
different
numbers
together.
the
multiplies
by the
the
eighth shift
left-hand
end
of
multiplicand
the
by
the
base
eight
the
register.
after
seven
In
fact,
with
the
times.
fallen off
number 210
would
be
lost
shifts.
However,
this
did
not
affect the overall result because after two LSR's of the 00000010,
all the "1"s had been cleared and all subsequent partial sums were
equal
Were
to
zero.
program 4.15
answer involved
PAGE 4-22
to
be used with
carry
(was
greater
than
255),
then
the
this
final
loss
of
left-most
bits
would be wrong.
register
caught
and
during
in
the
an
ASL
carry.
transferring it
by means
of the
ROL
In
would
it
this
significant
to
when the
does
Thus
not
the
and
the
answer
left-most bit
just
problem
fall
is
into
one
of
This
obtained
falls out of a
space
but
retrieving
is
this
can be achieved
command:-
ROtate JLeft
operation,
rotated
bit and
be
Fortunately,
the
all
the
bits
of
the
specified
address
are
left, with the carry bit being loaded into the right-most
the left-most bit being transferred to the carry.
Fig.
As
the
rotation
rotation
and
involves
provides
4.19
the
carry
bit,
means
of
picking
again.
Using ROL enables one to write
accommodating the carry but produces
in chapter 5
it
is
the
known
carry
as
bit
9-bit
back
Such a program is
listed
clearer to follow,
Both
these
forms
ROtate Right
instructions
the contents
can
be
up
(page 5-7)
as,
to make
it
colleague:of the
addressed
in
specified address.
several
ways
and
take
as:-
ROLA
ROtate ^eft
RORA
ROtate Right
the
contents
of the Accumulator.
the contents of
the Accumulator.
BIT
in the
It also
BITZ.
PAGE 4-23
While
that
BIT
it
performs
leaves
however,
the
both
same
logical
accumulator
function as
and
memory
as
AND,
they
flag
is
affected
as
it
follows:-
bit
of
differs
are.
in
does,
following way:
is
the
It
zero
location
(and
being
being
the
whether
flag).
the
This
contents
negative without
of
is
very
convenient
particular
way
location
of
are
testing
positive - or
into one
of the
registers.
Status
register.
The
BIT
instruction also
copies
bit
special.
However,
programming
system,
you
used
will
if you
in
look
the
occasionally
at
some
BASIC
find
of
the
clever machine
interpreter
some
very
and
neat
operating
uses
of
the
these
binary
instructions
multiplication can be
process
analogous
to
binary
carried out.
process
needing
is
only
analogous
to
re-iterations
the
to
binary
handle
multiplication
an
8-bit
routine,
number.
It
is
illustrated in program 4.15A, where the dividend (in this case 31)
is stored in location 900 and the divisor (2), in 901.
The Y
register
made
is
used
through
instruction,
PAGE 4-24
as
the
the
the
loop
algorithm.
counter
By
to
means
remainder is built up
in
ensure
of
an
that
ASL
passes
and
the accumulator.
are
ROLA
PROGRAM 4.15A
LDXIM 51
900
LDXIM 2
STX
901
STX
LDYIM 8
LDAIM 0
ASL
900
ROLA
CMP
901
SBC
901
INC
900
BNE
238
LDX
900
STX
1024
LDYIM 1
STA
55296
1026
STY
55298
STY
RTS
When run,
the remainder 1
(as an A) in 1026.
15
(as a 0)
in
1024
and
PAGE 4-25
CHAPTER 5
Advanced Functions of the Assembler
Labels
The
use
of
labels
enables
instruction without
addresses.
itself
the
program
necessity
of
is
symbolic
of
location
to
be
directed
calculating
is SYMBOLIC
in
memory.
to
branches
LABEL as
For
named
or
jump
the
label
instance,
the
instruction
BNE L00P1
instructs
the
assembler
to
create
the
machine
code
(*).
label
and
This
writing
rule
this
not
is
whose address is
In order to
an
tell
instruction,
only a
particular
that
Thus,
tells
L00P1
the
STAX
it
is
preceded
by
an
asterisk
assembler.
Thus,
the
beginning
of
L00P1
would be entered:
*L00P1
Further
conventions
particularly those
must
be
concerning
STAX 1024
observed
spaces.
The
as
long
as
required
when
using
asterisk,
for
labels,
instance,
it
is
It
There
simply that
the
assembler looks for a space in order to work out how long the label
is.
For this
normal
reason the
instruction.
label must be
These
rules
may
sound
little
then
formidable
(i)
(ii)
LABELS
(iii)
(iv)
The
label
must
be
followed
by
gap
prior
to
the
instruction.
PAGE 5-1
PROGRAM 5.2
LDXIM 160
*L00P1
LDYIM
JMP
L00P2
LDAIM 83
STAX
1183
TYA
STAX
55455
DEX
BNE
L00P1
JMP
END
*L00P2 LDAIM 90
STAX
1023
TYA
STAX
55295
DEX
BNE
L00P2
LDXIM
120
DEY
JMP
*END
L00P1
RTS
END
Although this
easy
to
loop 2
follow.
and
It
starts
on completion
by
initialising
it
X
is
and
still
Y,
relatively
then
jumps
to
on
its
own
(without
the
asterisk)
is
the
pseudo-code
that
PROGRAM 5.2
In Assembly
In disassembled
Language
Assembly Language
LDXIM 160
LDXIM 160
LDYIM 1
LDYIM 1
JMP
*L00P1
PAGE 5-2
L00P2
JMP
850
LDAIM 83
LDAIM 83
STAX
TYA
STAX
TYA
1183
1183
STAX 55455
STAX 55455
DEX
DEX
BNE
L00P1
BNE
244
JMP
END
JMP
868
list
the
*L00P2 LDAIM 90
STAX
LDAIM 90
1023
STAX
TYA
STAX 55293
STAX 55295
DEX
DEX
BNE
L00P2
BNE
244
LDXIM 120
LDXIM 120
DEY
DEY
JMP
*END
1023
TYA
L00P1
JMP
RTS
835
RTS
END
be
but
and
to
changes
be
possible
as
to
variables,
the
into
assembly
immediately after
the
written
Moreover,
i.e.
back
out
re-label
currently
assemblers.
this
carried
label
this
is
points
not
by
so,
language.
assembly
editing
in
Were
then
the
common
once
the
BASIC
program has
LABELS
and
LABEL
REFERENCES,
it
assembler
with
been
this
would
other
re-run,
will
have
the
been
lost.
Re-creating the assembly program from machine code is known
as DISASSEMBLY,
i.e.
the
'list'
command could be re-titled as
diamonds and
Exercise 5*1
Memory Labels
In addition to labelling instructions, the assembler also allows
memory locations to be given labels.
Once again, the assembler
needs to be told what to expect and the presence of a memory label
is indicated by an "@" at the beginning of line.
It is followed
immediately by the name assigned to that location and then, after a
@LSB 900
in the rest of
PAGE 5-3
Program
5-3
illustrates
the
use
of
memory
labels
in
precision addition - it adds together two 16 bit numbers:-
Number 1
Number 2
made up of LSB1
in ANSLSB1
and MSB1,
=
=
double
276010
94810
and ANSMSB2.
PROGRAM 5.3
LSB1
900
MSB1
901
LSB2 902
for MSB1
MSB2 903
ANSLSB 904
ANSMSB 905
LDAIM
10
STA
MSB1
LDAIM
200
STA
LSB1
LDAIM
STA
MSB2
LDAIM
180
STA
LSB2
'
memory
Store LSB1
in
memory
Store MSB2 in
memory
memory
ADC
LSB1
STA
ANSLSB ,
STA
1025
LDA
MSB1
ADC
MSB2
STA
ANSMSB
STA
1024
LDXIM
STX
55296
RTS
in
\ Store LSB2 in
.r
CLC
STX
Store MSB1
LSB
"
store answer
on
screen
.
Load MSB1
t MSB2,
print
55297 J
and
in red
END
When run, program 5-3 will display a red N (14) in 1024 and a red [
(124) in 1025.
as
PAGE 5-4
Multiplier
MPD
RESLSB
Multiplicand =
255
Temporary location to store carry bit
Result - Least significant bit
RESMSB
Result
ALGO
NOCARRY
Jump to point
TEMP
- Most
255
significant
if no
bit
carry arises
PROGRAM 5.4
900
MPD
Define
255
multiplier,
STA
MPB
multiplier
LDAIM
255
multiplicand,
STA
MPD
multiplicand
904
TEMP
902
RESLSB
RESMSB
906
*ALGO
Define
Define
location
load
907
and
for
store
memory location
load and
for
store
temporary store,
and LSB/MSB
of result
LDAIM
STA
TEMP
Initialise
(by loading in
zero)
the temporary
and
STA
RESLSB
result store
STA
RESMSB
LDYIM
Set
LSR
MPR
Check if
BCC
NOCARRY
multiplier=0;
LDA
RESLAB
Calculate
MPD
product
and
add
current partial sum
CLC
ADC
STA
NOCARRY
memory
LDAIM
loop counter to 8
right-most
branch
current
bit of
if so
partial
in
to
RESLSB
LDA
RESMSB
Add
ADC
TEMP
sum
STA
RESMSB
ASL
MPD
Current
ROL
DEY
TEMP
next
BNE
ALGO
LDA
RESLSB
STA
1025
Display
LDXIM
MSB
STX
55297
LDA
RESMSB
STA
1024
STX
55296
current
PartialO,
set up
loop
result
of
LSB and
RTS
Fig.
5.1,
program,
page
once
5-10,
all
the
steps
through
registers
are
in
the
set
1024
first
up.
and
an
few stages
The
contents
in
of
1025,
this
of each
PAGE 5-5
NOCARRY
ALGO
NOCARRY
LSR MPR
ALGO
ACCUMULATOR
0000 0000
OOOO
0000 0110
0000 0001
ADC TEMP
DEY
ROL TEMP
ASL MPD
STA RESMSB
0000 0000
0000 0001
1111
LDA RESMSB
STA RESLSB
ADC MPD
CLC
LDA RESLSB
BCC NOCARRY
LSR MPR
BNE ALGO
DEY
ROL TEMP
ASL MPD
1111
0000 0000
ADC TEMP
STA RESMSB
0000 0000
LDA RESMSB
STA RESLSB
1111
1111
0000 0000
CLC
ADC MPD
0000 0111
1000
Y-REGISTER
LDA RESLSB
BCC NOCARRY
INSTRUCTION
LABEL
Fig.5.1
1111
1111
0111
0011
1111
1111
MPR MULTIPLIER
TEMP
0000 0011
0000 0001
0000 0000
1111
1111
1100
1110
1111
MPD
1111
1111
0000 0001
1111
0000 0001
0000 0000
RESMSB
0000 0000
RESLSB
0000 0000
Macro Instructions
One
further feature
MACRO
repeat
into
of your assembler is
instructions.
and
your
are
thus
program
automatically
similar to a
These
given
you
are
a
blocks
label.
simply
need
that
of
When
to
it
code
that
you wish
type
inserts
the
whole
macro.
subroutine except that the
in
the
to
you
wish
insert
label
to
these
and
this
Thus
a macro is very
assembler writes it in
calls
this
routine
times,
it
could be written
(i)
JSR
CLSBDR.
print menu
SUBROUTINE
JSR
CLSBDR
CLEAR SCREEN
print menu 2
PRINT BORDER
RTS -
JSR CLS
prin.t menu 3
r
(ii)
MACRO CLSBDR
MENU
MACRO CLSBDR
MENU 2
MACRO CLSBDR
MENU 3
Fig.
5.2
so
messy
organisation.
as
the
Where
picture
macros
suggests,
score
is
the
in
6510
doing
producing
much
of
program
the
that
PAGE 5-7
reads
more
logically
space
than
don't
use
up
processor
Just -whether
macro
particular case
To
and
is
subroutine,
time
or
is .left
identify a macro
easier
which
follow.
only
jumping
to
to
is
about,
subroutine
the
on this
They
written
they
is
up more
but
as they
quickly.
run more
more
preferable
later
'added'
into
it
is
the
which
that
particular line,
the macro
may
be
any
length
and
The end
preceded by
needs
to
be
should
The
be
the
first
of the macro
As
the macro's
only entry
on
occurrence
is
of
signalled by a
this
e.g.. "+MACR01" .
any
program).
in
individual programmer.
particular assembler
take
once,
entered
another
into
the
MACRO1
say,
program
at
just
the
the
line
appropriate
53
+MACRO1
LDAIM 90
First
LDXIM 40
inserted
STAX
this
1024
DEX
occurrance
point.
here at
BNE
into
of
MACR01
program
It is
at
defined
250
+END
macro
definition
+MACR01
Assembler
inserts
second
RTS
END
Fig 5-3
When using
in mind
labels,
that
the
label
references
assembler
needs
to
and
macros,
store
the
it
should
names and
be
borne
locations
into
account.
Thus,
and
references
repeating
and
macro
these must
with
also
label
be
and
If
the
used
numbers
is
going
of
labels,
to
exceed
references
the
or macros
numbers
necessary to
provided
likely to
for
re-dimension the
in
be
the
arrays
PAGE 5-8
are dimensioned
in
line
no, 75
of
the
assembler
program
contain:
stored
in K$(X),
label
in M$(X),
U%(x)
and
M1#(X)
enable
16
which
macros
are
to
be
includes
The
Assembler
for
the
Commodore
64
machine
code
provides
Language Monitor
chapter.
machine
The
the
(MLM)
first
facilities
which are
time
that
for
discussed
the
the assembler is
Machine
in the next
run,
this
is
risk
that
editing
the BASIC
section.
Assembler
program
(i)
assembler,
(v)
If
just in case,
the
you
test
are
fails,
check
confident
your
that
amendments
these
carefully.
have
been
If
entered
as
the computer
consequence
it
to
is
occur
with
the
reasonable
rule
of
thumb
is
that
each
new
label,
reference
or
10 more bytes.
PAGE 5-9
of
the
functions
options
- 0'.
which
If this
is
offered
is
on
selected,
provides
you
the
the
with
main
MENU
is
'Other
additional
functions
for
INSERT
function
is
accessed
by
MENU,
selecting
and
an
responding
'0'
at
'I'
the
when
main
asked
the
example
shown
below,
i)
six
byte
gap
is
opened
up
in
an
when asked:
ii)
Enter the
address
of the end
of the
current
program when
asked:
iii)
Enter
length
of
the
code
to
be
inserted,
in
bytes,
when
asked:
Length of Insertion? 6
iv)
for
the
screen
will say:
OK Space Inserted
v)
Press any key to return to the main MENU and then use the
normal ENTER procedure to enter the insert by selecting *E'.
PAGE 5-10
To access
the INSERT
Move.. M".
In
the
function,
example
into 828
but
"below,
to 842
is
selecting
machine
to be moved
'M1
code
to
the
when
followed as for
asked
program
"Insert..I
originally
written
SCOOO onwards.
i)
Enter
the
start
address
of
the
program
to
be
moved
when
asked:
ii)
iii)
Enter
the
moving,
address
where
the
program
is
to
start,
after
when asked:
iv)
When the block has been moved, the screen will display:
OK-Block Moved
v)
The
will
poke
BASIC
the
program
numbers
also
in
has
the
to
DATA
include
statement
simple
back
into
loop
the
DATA
will
that
Prom here,
load
and
make
use
of
created
option twice.
statements
program.
are
restriction
a 200 byte
the
are
machine
added
code
to
the
program
assembler
that
they
represent.
PAGE 5-11
In
the
example
shown below,
a machine
code
program
(150
bytes
created
long)
is
in the
to
be
'0'
has
been
selected
at
the
main
MENU
and
'Df
at
the
secondary
MENU.
i)
Enter
suitable
line
number
for
the
first
BASIC
DATA
statements
will
not
been
get
selected
mixed
up
to
ensure
with
the
the
DATA
assembler.
that
The
ii)
Enter
the
start
address
of
the
program
which
is
to
be
iii)
Enter the
address
of the
(sorry!).
end
In
to
of
this
have
the
program,
unless
it is
iv)
which
will
appear
briefly
the
on
program to
your
DATA
screen.
In
DATA
Statements
now
Entered
and you may press any key to return to the main MENU.
v)
on
the
main
MENU,
'D'
on
the
select the
secondary
f0*
MENU
and.
The start address and the end address this time will be 956
and 977, which are the bytes remaining to be converted.
enough,
if
you
have
one
of
the
BASIC
Aid/Toolkit
DELETE
-19999,
say.
The
but
DATA
excluding
statement
Simple
programs
or
program
can
be
saved
e.g.
and
later MERGED (another BASIC aid command?) with its BASIC program.
PAGE 5-12
However,
to
such useful
tools,
then here is
one easy method of saving the DATA statements. This technique works
well, provided all the DATA statements will fit on to the screen:
a)
b)
list
lost,
not
only the
on the
assembler,
but also
c)
the
screen
statement
key
again
Repeat
d)
has
and
press
been
and
the
re-entered
the
next
RETURN
into
DATA
key.
The
memory.
statement
first
Press
has
the
been
DATA
RETURN
recovered.
of
statements
the
DATA
only,
or
you
could
start
to
type
the
them.
'L*
listing
on
the
screen.
If
you
this
same
the
as
facility.
screen
The
listing
own
The
'P'
printed
and
printer
however,
you
will
will
be
will
printed
be
in
exactly
double
the
width
characters.
sure
course.
address
your
Now provide
when
listings.
will
that
be
hanging
asked
While
the
blanked.
up,
which
'0',
then select
printer
the
to
start
do
so,
printer
This
can
is
is
is
address
and
avoid
occasionally
the
listing,
to
'P'
connected
of
and
the
printer
the
the
switched
program
will
screen
chance
happen
if
of
of
you
and
on,
of
the
end
produce
your
the computer
the
are
printer
using
OK-Program Listed
PAGE 5-13
CHAPTER 6
far
all
the
via
the
the
assembler
The
assembler
code
machine
assember.
does
is,
Program 6.1
is
of
program into
code
However,
to
programs
as
make
course,
memory,
it
not
one
considered
discussed
easy
the
in
to
the
POKE
only way
other way is
have
been
last
chapter,
data
of
entered
into
all
memory.
putting a machine
to POKE
it
in directly.
PROGRAM 6.1
POKE 828,160
POKE 829,1
POKE 830,162
POKE 831,0
POKE 832,169
POKE 833,90
POKE 834,157
POKE 835,0
POKE 836,4
POKE 837,152
POKE 838,157
POKE 839,0
POKE 840,216
POKE 841,232
POKE 842,208
POKE 843,244
POKE 844,96
We
cannot
the
menu
to
direct
first
exit.
this
So
into
first
memory,
of
all,
of
type
in
simple
matter
put
828,162
the
to
from
it's
disassemble
not
the
1024
cursor back on
edit
the
digits.
When the program is in,
command and it will put 256 white
you
imagine
the
program
that
will
the
POKE
a HOME will
Fortunately,
course,
select
POKE commands.
and
<return>,
then
enter
assembler.
line,
and
is
we are
option
we
on
can
running
the
main
enter the
onward.
After
the P in POKE.
changing
the
each
It is
necessary
necessary
to
code
memory
from
while
X
imagine
for
it,
you
as
then select
the
and
'L'
assembler
display
it.
at the MENU
PAGE 6-1
The
assembler
will
then
reveal
the
source
program
for
6.1a
which
was:
PROGRAM 6.1a
LDYIM 1
LDXIM 0
LDAIM 90
.STAX
1024
TYA
55296
STAX
INX
244
BNE
RTS
As well as entering programs without the assembler it is also
possible
to
run
them
directly or
from
a BASIC
program.
For
instance,
to
run
the
program
that
currently
starts
at memory
location 828
counter.
demonstration
Next
it
is
only necessary
If program 6.1
exit
of
from
this
the
is
if
still
it
assembler
to
put
an
828
into
in memory this
is
not
still
and
get
back
in
to
the
program
memory
type
BASIC with
it
in.
the READY
cursor.
Now
type
in
SYS
828
and
the
program
will
run,
putting'
in
the
diamonds.
PROGRAM 6.2
Clear screen
Direct PC to 828
<return> and
Line 20000
Line 20010
the screen.
Line 20020
Running
program
directly
is
relatively
easy
but
the
direct
mode
a further,
more
easily entered
option
PAGE 6-2
is
offered
by the
PROGRAM 6.3
1 FOR X = 0 TO 16
2 READ A
3 POKE (828+X),A
4 NEXT X:RESTORE
5 DATA 160,1,162,0,169,90,157,0,4
6 DATA 152,157,0,216,232,208,244,96
7 END
GO TO
5.
6.
10:F0R X= 0 TO
16
Once this is done, the main assembler can be run and the program
RUN from the MENU - it's located at 828.
Of course, once the data
is loaded into memory, the program can also be run by a SYS 828
command.
The
Assembler
for the
Commodore
64 offers
a further
method
of
feature
memory.
MENU.
offers
a ready facility
for
examining
and
modifying
c*
PC
and
the
cursor
immediately
SR AC XR YR SP
.;E416
B1
will
be
following
the
FF 28 06 EA
flashing
last
merrily
dot
(".").
away
The
implementation of the
in
MLM
the
on
position
the
Dr.
Commodore Machine
be revealed below.
The cursor following the dot indicates that Monitor is waiting for
a single letter command from you to indicate which
required.
To investigate this type in the command:
facility
is
PAGE 6-3
Take
care
Monitor
6.1
when
will
doing
be
this
to
confused.
is still loaded,
put
This
in
all
command
the
spaces,
should,
otherwise
assuming
program
below:
.M O33C O34C
.:O33C AO 01
A2 00 A9 5A 90 00
.:O344 04 98 9D 00 D8 E8 DO F4
.:O34C 60 00 00 00 00 00 00 00
Fig.
6.1
(addresses in hex).
the
program loaded
contains
AO,
with 60.
O33D
The
by program 6.1
contains
values
01 ,
and
and
the
so
on
see
listing
to
O34C
displayed
in fact,
is
in O34D
loaded
to
0353
may not be 00 as shown in figure 6.1 since these may contain value
which were not chdnged when you loaded program 6.1.
You can list
any part of memory using the M command,
Look
at
the
block
listed
by
monitor
of course.
again.
Notice
that
each
line
monitor's commands:
useful
facilities
machine
instance,
code
offered
programs
while
program
by
as
6.1
the
if
is
monitor
they
were
loaded,
is
the
BASIC
go
into
ability
to
programs.
monitor
and
edit
For
list
O33C to O34C
Type:
.M O33C O34C<return>
the first T
of 01
*6'
using the
Monitor will show you the block again and you will be able to
confirm that the contents of O33D have indeed been changed to 06.
The : command to monitor has been obeyed.
Now repeat the process
to replace the 5A in 0341 with a 58 followed by a RETURN to action
PAGE '6-4
.M O33C O34C
.:O33C AO 02 A2 00 A9 58 9D 00
.:O344 04 98 9D 00-D8B8 DO F4
.:O34C 60 00 00 00 00 00 00 00
Fig.
6.2
You can of course make as many changes as you like in one line.
When you press the RETURN key, all the changes will be made.
You
could if you wished to build the change line yourself, type in the
colon
followed
However,
display
most
the
by
the
users
line
address
find
and
it
then
followed
much
use
by up
easier
the
to
to
use
command
the
command
to
set
The
Assembler
program may
and
LDAIM
instructions
LDAIM
90
and
LDAIM
88.
then
be
will
On
RUN
be
and
seen
running,
the
on
to
the MENU.
LIST ing
have
white
the
been
LDYIM
changed
diamonds
to
will
be
also
be
only
can
entered.
Try
programs
this
with
be
edited
the
in
monitor,
following
exercise,
they
using
can
the
monitor
i)
ii)
Enter monitor,
List memory contents
Type M O33C 034E return,
iii)
PROGRAM 6.5
.M O33C 0358
.:O33C AO 50 A9 4D 99 FF 03 99
:O344 9f 04 A9 4E 99 4F 04 88
.:O34C DO FO A9 09 AO FO 99 FF
. :O354 D7 88 DO FA 60 88 88 88
Note that the final "88"fs in this program are really irrelevant as
they occur after the 60 or RTS.
these locations.
The machine code program is now entered into memory and may be RUN
either
from the Assembler
of by SYS 828.
It
can also
be
dis-assembled
using
the
assembler
to
reveal
the
Assembly
language
version.
PAGE 6-5
(038416)
or by the
following procedure:
i)
ii)
iii)
iv)
v)
vi)
Enter MONITOR
List O33C to 0358 by
.M O33C 0358 <return>
Re-position cursor to the O33C display line
Replace the O33C by 0384 and press <return>
Repeat for the 0344, O34C and 0354 lines, as
below:
The procedure has made a copy of the O33C program in 0384 onwards.
be
told
where
the
program
.G 0384<return>
tape
and
LOADed
enable machine-code
back
from
this.
programs
However,
to be
one
SAVEd
slight
onto
problem
in
the
cassette
buffer
and
this
is
used
when
LOADing
and
SAVEing programs.
In order to protect
programs
they must
be
On
(0800^)
and
40959
programs
(9FFF^g).
are
normally
However,
stored
strings
between
generated
2048
by
program are written from the top of memory downwards. The computer
knows where to start storing this information, as when the machine
is turned on it checks what the highest available memory locations
are and stores this information in the zero-page locations 55 and
56.
If a machine-code program is stored at the top of memory,
then it will be written over when any BASIC program is run - such
as the assembler - when this generates strings.
However, before
PAGE 6-6
Immediately
after
switching
on the computer ,
Protect
100
9F9C1g).
bytes,
There
instance,
are
this
address
number
would
of
the
address 40960^0
and 56 - MSB.
ways
be
of
lowered
achieving
In order to
to
40860^q
this.
For
POKE 55,156(9C16)
POKE 56,159(9F16)
followed by,
CLR
get
it's line
the
strings
which
are
now
in
the
10 POKE 55,156
: POKE 56,159
: CLR
switch
on
time,
the
is
to
we
similarly,
calculate
the
address
values
which
In general,
from
the
will
be
found
later
on
may
current
values
stored
in 55\q
Machine
PAGE 6-7
not
really a waste
working
of time,
since
space
by
cartridges
this
is
not
Although
very
C000
which are
likely,
it
to
plugged
is
as
into the
well
to
computer.
know
how
to
Assembler
for
the
Commodore
64
does
use
few
hundred
of
area, but there is a great deal of space which you may safely use.
The address of the first byte in this area which is used by the
to
know
this.
As
ever,
the
format
is
low
byte
followed
by
high byte
copied
to C000 onwardte,
using Monitor's
used.
Save
For
starts
program
and
name.
to
where
Thus
be
it
SAVEd,
ends
the
and,
as
monitor
with
needs
other
to
know
programsf
where
it
it
needs
.S "program-name",01 ,start-address,end-address+1<return>
if saving to tape,
and
.S "program-name",08,start-address,end-address+1<return>
if saving to disk.
Notice the peculiarity that the end address is one larger than the
highest address of the program being saved.
Translating this to save the check, program to tape,
"...
it becomes:
.S "CHECK",01,C000,C01D <return>
respond
with
a "PRESS
RECORD
&
PLAY ON TAPE" (if you are saving to tape) and when this has been
done, an "OK". As you will expect, the screen will go blank during
the saving process.
computer will
respond
Finally,
with
and
the flashing
complete,
cursor
of
the
the
PAGE 6-8
Load
Provision
is
made
program from
which it was
on
the
monitor
address
on
program
back.
to
the
tape
As
or
this
for
re-loading
machine
code
disk
so
that
address
is
it
knows
known,
it
where
to
doesn't
load
need
the
to
be
load
from
tape,
and
.L "program-name",08<return>
from disk.
Thus
.L "CHECK",01<return>
will
command
is:
.L<return>
Following the return, the computer will respond with a "PRESS PLAY ON
will
go
blank
is
found
during
etc. When
the
the
the
loading
program
flashing
process,
is
clearing
loaded
cursor of
when
the
Monitor as
its
remember that
by Monitor when
it
is
I promised
first
entered.
the
shall
following:
*C
PC
SR AC XR YR SP
.;E146
31
10 00 06 F8
other
which
later.
way
case
namely:
The
it
that
it
would
next
Program
could
have
line
is
Counter
have
been
displayed
simply
(PC),
*B
entered
heading
Status
is
more
for
Register
by
of
BREAK,
BREAK
the
(SR),
in
entries
third
line,
Accumulator
PAGE 6-9
(AC),
X-Register
(XR),
Y-Register
(YR)
and
(SP).
In practice, these values are of academic interest only for
a CALL entry, they really come into their own if the entry is made
as a consequence of a BREAK.
One
of
the
6510
instructions
a hex value
that has
of 00^.
not
yet
If the
been
6510
dealt
with
encounters
is
BRK
'interrupts is
is given here).
If a
Machine Language Monitor has not been implemented, then the result
will simply be that the screen will be cleared and you will return
to the BASIC READY state.
If a MLM has been implemented, however,
then all sorts of nice things happen.
The values stored in the
Program Counter and the Registers have been saved during the BRK
interrupt
and
one
of
the
functions
of
the
Monitor
is
to
recover
Sometimes, the reason for the BREAK is simply that the program has
gone beserk and is executing data as if they were instructions 5
there are always a large number of zeros hanging around in a
computer's
memory
and
these
will
be
interpreted
as
BRK
instructions.
This is good, since you will (a) have regained
control of your runaway program and (b) know what part of memory it
was running away in.
However,
rogue
elephant
a way forward.
You
When
can
insert
(if)
the
BRK
BRK
instruction
instruction
is
into
your
executed
program
you
and
will
run
arrive
it.
in
hopefully,
the
clue
If .you don't
the
program
You can seed your program with as many BRK instructions as you
wish.
There is no problem of identification as Monitor tells you
the value of the Program Counter when it is entered, so you will be
try your
first
BRK
program,
you
will
thing.
The Program Counter which is shown
the BRK instruction but is one byte bigger.
PAGE 6-10
discover
curious
the
address
of
the
instruction
which
follows
the
BRK.
This
is
B
PC
SR AC XR YR SP
.;O35C
30 00 02 03 F4
(in
the same way as you edited the colon line resulting from a M
command) and typing a return will cause any changes which you have
made
to
be
remembered
by
Monitor.
If
you
re-enter
your
program
using the G command, then the program will restart with the new
values stored in its registers.
Naturally, if you change the value
of the PC, then you will re-enter the program at a different point
from the BRK exit.
There
is
with.
one
other
command
The R command
Monitor,
but
to
without
the
*C
or
*B
line.
The
main
use
of
this
command is when you are debugging and wish to re-enter the program,
bit
of
case,
nuisance,
you
could use
especially
the M and
if
:
the
program
commands
to
is
change
long.
In
this
the value of an
(a)
(b)
you BRKed,
and
here
is
to
make
note
of
the
hex
value
BEFORE
(the
you
change it to zero).
I am sure that you can see what a powerful tool this gives you for
debugging
your
machine
code
program*
You
will
be
surprised
how
PAGE 6-11
of
memory,
out
of
the
reach
of
BASIC,
it
switching
code
program,
of
course.
if
you
remains
It will
overwrite
Therefore,
it
in
memory
only be
can
it
with
be
after
destroyed by
used
machine
whenever
required.
To enter Monitor,
SYS 10
64
to
execute
Monitor.
am not
Why,
is
the
the opcode
BRK
pointers
to
command . stored
bottom
and
top
at
So,
10,
bingo,
we
enter
code programs?
convenient.
of
memory,
You will be
For example,
to
change
if I
surprised
to adjust the
colours
of
the
intended), and SYS 10 will simply take you back to the BASIC READY
state.
The trick is to PEEK at the two locations 790 and 791,
immediately after leaving assembler, and make a note of the values
that you find there.
These locations contain the link address to
Monitor for the BRK command.
You will find that they form the
M <lo-address>
<address>
<hi-address>
Display Memory
<byte1> <byte2>...
Change Memory
R
;
Display Registers
<prog-counter>
<status-reg>
etc
Change Registers
S <"filename">,<device>,<lo-add>,<hi-add + 1>
L <flfilename">,<device>
G <address>
X
PAGE 6-12
Go run
Exit from Monitor
colour
displays.
This
C-6M
facility
is
just
as
readily
used
6.6
shows
how
the
screen/border
combination
can
be
PROGRAM 6.6
LDYIM 15
LDXIM 15
.:
DEX
BPL BORDER
DEY
BPL SCREEN
RTS
Unfortunately,
when this
is
run as
it
stands
there
is
little
to see
previous
sometimes
chapters
nested
we
loops.
used
The
counting
next
loops
program
to
does
produce
things
the
the
delay,
easy way
Program 6.2
loads 246
into
for it
to become positive
BORDER STY
$D020
LDXIM 15
$D021
SCREEN STX
LDAIM 246
LDA
162
162
BMI
LOOP
STA
LOOP
DEX
BNE
SCREE
DEY
BNE
BORDE
RTS
PAGE 6-13
Individual
characters
on
the
screen
can
also
be
controlled
by use
of the screen colour codes (in fact we have been using this from
our earliest programs). If memory location 55296 (0800^) contains
a "2", then the top left memory location, i.e. 1024 (0400^5) will
print
out
The
complete
Colour
Black
White
Red
Cyan
4
5
6
Purple
Green
Blue
Yellow
Orange
Brown
9
10
Light Red
11
Dark Grey
12
Mid Grey
13
14
15
Light Green
Light Blue
Light Grey
Pig. 6.3
Program 6.8 shows how the colour of blocks of screen can be defined
LOOP
LDAIM
STAX
LDAIM
STAX
8
$D7FP
160
1023
DEX
BNE
LOOP
RTS
This process is taken a stage further J.n program 6.9 where the
sixteen different colours are cycled through, with a delay between
each.
PAGE 6-14
PROGRAM 6.9
LDAIM 0
STA
LOOP1
L00P2
LDXIM 200
LDAIM 160
STAX
STAX
1023
1019
$D7FF
DEX
BNE
L00P2
LDA
LDAIM 186
DELAY
1019
STA
162
I Delay of 60
LDA
BMI
162
f jiffies (1 sec)
DELAY J
LDA
1019
ADCIM 1
CMPIM 16
STA
1019
BNE
L00P1
RTS
PAGE 6-15
CHAPTER 7
Built-in Subroutines
As
on ROM (Read Only Memory), machine code routines that control the
6510.
These
enable
the computer
to
deal
with
the
BASIC
commands
that are put into programs, all input and output and all the
standard routines which are needed to keep the computer alive and
well.
The ROM which handles all the BASIC statements is located in
memory between $A000 and $BFFF.
The ROM which looks after all the
non-BASIC routines is called the Kernel by Commodore, and lies
between $E000 and $FFFF.
In addition to
use
of
the
RAM memory
in
the
bottom
four
pages
between
$0000
and
$03FF, the most frequently used locations being in the zero page
$00 to $FF.
Some of this usage is for the storage of transient
data such as, for instance, the jiffy clock ($A0 to $A2) that ticks
away
all
the
time
the
computer
is
turned
on.
Some
of
the
such as
program,
Some
these uses
RAM
is
in 43 to
56 ($2B to $38) which indicate the area of memory used by the BASIC
and
its
data
areas.
of
take
up only one,
two or three bytes, others use much more, such as the cassette tape
buffer which uses 192 bytes when transferring data between memory
and the cassette tape.
the
are
starting
very
well
points
of
defined.
the
Another
Kernel
helpful
routines
will
feature
be
developed
kept
and
is
that
the
same
this will
routines
to
64
accumulator
Kernel subroutine. We
by
using
STA
CHROUT
however
and
accumulator
position.
is
to
This
is
to
located
the
is
use
at
the
illustrated
e.g.
Kernel
65490
screen,
have
command
already
to
STA 1024.
subroutine
($FFD2).
starting
move
This
from
the
displayed
a
copy
of
the
the
A better/easier
which
will
is
called
output
current
the
cursor
in program 7-1 :
PAGE 7-1
PROGRAM 7.1
LDAIM 42
STA
(an asterisk)
1524
LDXIM 1
Load X with
STX
55796
JSR
$FFD2
RTS
END
When run,
at
1524
this
displays
white
two asterisks.
asterisk
which
the
screen
and
probably
we
In the middle
placed
prpbably in the
coloured
light
of the
directly.
blue..
This
screen
However,
we
corner of
extra
asterisk
Unfortunately, though,
this
program
suggests!
To
demonstrate
this,
try
replacing
although
in
the
hand
middle
the
screen,
the
nothing
expected white
appears
ASCII
character
screen set.
the
of
screen
However,
code.
set
The
code,
the
when
ASCII
so
ASCII
using
code
the
asterisk
program worked
code
that
subroutine
for
for
*Af
is
of
this
in
the
'A'
top
left
65490,
rather
to
be
properly
in
the
not
appears
top
happens
65,
the
Now,
as
it
than
the
the same as
first
is
run.
in screen
A2-25.
The
two
major
locates
advantages
the
cursor
automatically ,
each
time the
automatically store
the
in the
If you
the
colour RAM.
current
assembler
cursor
does
subroutine
automatically
colour
ran the
position
that,
called ;
the
have
and
second
appropriate
been
sets
first ,
increment
program from
would
screen1
in
are
will
subroutine is
current
'clear
and
it
this
it
will
position
1024,
because the
current colour to
the
If
we
wish
current
to,
we
can
set
the
current
code
cursor
program.
position
Setting the
and
the
current
the
cursor
could
of another built-
be
quite
in subroutine
PLOT
PAGE 7-2
will
either
read
or
set
complicated,
called
but
for
the
appropriately PLOT
current
position
of
the
set,
then PLOT
return
the
position
the X and Y registers, X containing the number of
and Y containing the column (0 to 79). If entered
then the values that we have stored in the X and
be used
will
current
of the cursor in
the row (0 to 24)
with carry clear,
Y registers will
place
a yellow asterisk at
the
PROGRAM 7.2.
CLC
LDXIM 9
LDYIM 0
JSR
$FFFO
LDAIM 7
646
LDAIM 42
JSR
$FFD2
STA
RTS
PROGRAM 7.2a
831
LDYIM 17
END
PROGRAM 7.3
CLC
LDXIM 9
LDYIM 17
JSR
$FFFO
LDAIM 7
STA
646
LDAIM 42
JSR
$FFD2
RTS
END
When run,
this program moves the cursor down 10 and across 18, and
PAGE 7-3
PROGRAM 7.3a
LDYIM 4
0843
*LOOP
JSR
$FFD2
DEY
BNE
LOOP
RTS
END
to yield:
PROGRAM 7.3b
CLC
LDXIM 9
LDYIM
17
JSR
$FFFO
LDAIM 7
STA
646
LDAIM 42
LDYIM 4
*LOOP
JSR
$FFD2
DEY
BNE
LOOP
RTS
END
When run, program 7.3b will print four yellow asterisks in line 10
in columns 18, 19, 20 and 21.
Notice that we didn't have to reload
the accumulator with 42 each time round the loop, so the CHROUT
subroutine did not alter the value of the accumulator.
It is also
clear that the Y register is not altered, otherwise the counting of
the four asterisks would not have worked.
In fact, CHROUT doesn't
alter either A, X or Y.
particular routine.
Not
so
it
is
of the
use
important
to
bear
is
the
in
one
of
built-in
mind
the
possibility
Many
BASIC
byte
input
to
the
carry
keyboard
If
out
queue
the
this
operation
called
GETIN
and
returns
is
it
empty,
as
an
GETIN
ASCII
does
LDAIM 0
PAGE 7-4
single
Kernel
which
value
not
in
wait,
PROGRAM 7.4
JSR
$FFE4
BEQ
JSR
LOOP
$FFD2
RTS
or more
is
queue
LOOP
one
use
of
that we choose to
or BASIC ROMs.
programs
into the
subroutines
from
This
all
the
but
When run,
*LOOP
JSR
$FFE4
BEQ
LOOP
loop
work
rather
with
than
ASCII
place
code,
it
there
rather
directly.
than
the
Both
GETIN
computer screen
and
CHROUT
code.
Direct
in
the
When
the BASIC
inputting
from
the
be
switched
(CHR$(13)) is typed.
on
keyboard,
the first
and
its
action
is
similar
to
will
remain
on
until
RETURN
($0200),
character
sequence
each
call
on
CHRIN.
There
is
no
need
to organise
the
This
in 7.5!
PROGRAM 7.5
JSR $FFCF
RTS
of
certain
terminating
character
which
need
not
character.
in to look for
PAGE 7-5
PROGRAM 7.6
LOOP
LDXIM 44
Store terminator in
STX
900
900
JSR
$FFE4
BEQ
LOOP
Wait
JSR
$FFD2
CMP
for input
900
LDAIM 13
Output a <return> to
JSR
screen to be tidy
BNE
LOOP
f branch back
$FFD2
RTS
This
program
comma
simulates
instead
change
the
of
operand
an
RETURN.
of
the
INPUT
To
first
value.
routine
use
that
is
terminator
instruction
Try this
so
terminated
other
that
than
900
by
comma,
will
be
for:
Exercise 7.1
Modify
space,
program
76
to
accept
an
to make
input
that
is
terminated
by
the change.
As
was
mentioned
earlierf
when
using
built-in
That
means
that
modify
the
SR
they
the
one
of
subroutines
put
Z,
things
and
the
is
into
other
major
that
A,
flags.
reasonable to assume
problems
they
too
and
Hence
encountered
use
the
and
they
when
6510.
also
returning
as it was left before the JSR, unless, of course we know (as with
the CHROUT routine) what is left undisturbed.
As an example of this,
look at the following program that is
designed
to
input
a four-character string from the keyboard.
First
it sets a loop counter in X at 4,
then it uses the GETIN
and CHROUT subroutines.
On return from these it decrements X and
checks for Z flag set - all very straightforward!
PROGRAM 7.8
LOOP
LDXIM 4
JSR
$FFE4
BEQ
LOOP
GETIN subroutine
JSR
$FFD2
CHROUT subroutine
LOOP
DEX
BNE
RTS
PAGE 7-6
However, when run, this program RTS's after only one character has
been input!
This suggests that one of the subroutines is using the
X
register.
To verify this the
the X register at various stages
program
program
of
7.9,
where
the
just
contents
can
to
the
register
are
examined
PROGRAM 7.9
LDXIM 4
LOOP
JSR
$FFE4
BEQ
LOOP
STX
LDYIM
STY
JSR
STX
LDYIM
1024
1
55296
$FFD2
1026
1
STY
DEX
55298
BNE
LOOP
RTS
When run,
also
output immediately after the exit from the GETIN routine (JSR
$PPE4) then it is clear that GETIN does modify the X register. As
we have already discovered,
end
the
the
program
changed to 1
caused
Since
program
to
leave
the
loop
on
the
the
first pass.
To
overcome
this
problem,
the
register
value
must
be
stored
to being decremented.
PROGRAM 7. 10
Assembled Version
LOOPCONST 900
STORE
GET
LDXIM 4
LDXIM 4
LOOPCONST
STX
STX 900
JSR 65508
BEQ 251
JSR 65490
JSR
$FPE4
BEQ
JSR
GET
LDX
DEX
LOOPCONST
BNE
STORE
$FFD2
BNE 239
RTS
RTS
When
run,
this
LDX 900
DEX
program
allows
four
entries
from
the
keyboard,
PAGE 7-7
is
from
used
511 ,
for
the
its
next
rapid
transfer
vacant
line
command).
the
last
stack
of
last
one
i.e.
from
which
was
displayed
When anything is
item
put
plates,
put
in
is
511
by
to be
top
one
However,
towards
data
256,
and
being
is
filled
recorded
by
downwards
a
STACK
the
so
in
response
retrieved
accessible.
only the
there.
of
location
The
to
the
being accessible
6510 stack is
plates
are
put
at
the
bottom
and
function of
jumps,;
which
the
it
stack
does
is
to
record
addresses
automatically.
When
during subroutine
the
instruction is
subroutine
has
been
so
that
it
executed
can
and
find
then
its
new
place
is
6510
sees
an
the
examined
"50000"
into
the
below with
the
PROGRAM 7.11
STEP 1.
STEP 2.
STEP 3.
INSTRUCTION
STEP 1.
828 ($O33C)
831 ($O33F)
834 ($0342>
i)
ii)
iv)
BEQ 251
Retrieve
i.e.
v)
vi)
address
$O33F.
for next
instruction
from PC,
:/
PAGE 7-8
(65508)
JSR $FFE4
STX 900
iii)
STEP 2.
($0384)
STX 900
$0342
509
$03(MSB
$03-
510
$42(LSB
$42-
511
STACK
vii)
Record
viii)
ix)
this
is
in
the
stack
encountered.
(i.e.
SP=5O9,
$0342)
and
example,
subroutines, and
would have been
location
xiii)
In
vacant
xii)
xiv)
next
x)
xi)
STEP 3.
the
the
first
each time
piled on
successively through
these
subroutine
a JSR
top.
could
have
met
nested
subroutines,
the
return
addresses
would
have been stripped off to steer the 6510 back to the original point
of departure
Fig. 7.2 illustrates this for subroutines nested 3
deep starting from a program with a JSR $0384 instruction in $O33D.
MAIN P ROG.
SUB
START
$0384
$0385
$O33C
$0386
$O33D
$0387
$03A2
$O3A3
$03A4
$O3A5
$O3A6
_
$03
510
$3E
511
$03
$87
$03
$3E
SUB 3
SUB 2
$03
$A4
$03
Jy
J>
508
$87
$03
$3E
509
510
511
$03E8
$O3E9
$03EA
$03EB
={>
506 '
507
508
509
510
511
$03
$A4
$03
$87
$03
$3E
STACK
STACK
STACK
STACK
SP=5O9
SP=5O7
SP=5O5
SP=5O4
$03
510
$03
$87
$03
$3E
511
$3E
V>1"*
511
STACK
STACK
STACK
SP=511
SP=5O9
SP=5O7
508
511
506
507
508
509
510
511
509
510
i.e.
stack is
empty.
Fig.
7.2
PAGE 7-9
In
the previous
would
all
takes
the
be
program,
RTS.
top
address
downwards at SUB 3,
i)
ii)
iii)
Thus
instructions
when
off
the 6510
the
stack,
at
$O3EB,
$O3A6
finds
an
RTS
which
is
$03A4.
and
at
$0388
$03EB,
it
Reading
Fortunately,
executing subroutines
is automatic and
the
However,
stack
does
so
the
programmer can
allow
not
automatically
store
register
These must
be handled
via
the
accumulator which
is stored using:-
PHA
I PLA
In both cases,
it continues
the
to point
to the next
that
PHA
JSR
$FFE4
BEQ
251
$FFD2
JSR
Jr
PLA
TAX
DEX
BNE
241
loop.
RTS
When
run,
the
program accepts
PAGE 7-10
four
character
input
and
prints
As well
to
reset
program,
this.
as affecting
the accumulator,
one or more
flags
in the SR.
a subroutine
On
is most likely
returning to the main
this problem,
built-in
provision
flags
on
the
I PLP
In program 7.12 the SR was not saved on the stack as, prior to
testing the Z flag with a BNE, the DEX instruction reset this.
However, under other circumstances it may have been necessary to
Exercise 7.2
accumulator,
X and Y
Save A-
Save SR
Save Y Save
Retrieve
Retrieve Y
Retrieve SR
Retrieve A
Fig. 7.3
PAGE 7-11
CHAPTER 8
Interrupting the 6510
at
certain
interruption.
times
the
to be prepared
able
to allow an
end of data."
"Thanks!".
SEI
Ironically,
the
first
action
that
usually needs
to
be
taken in an
so
flag
is
(or
simply
flag)
interested
may
be
in,
then
reset
to
'0'
or cleared
by the
instruction:-
CLI
If you
about
the
above,
you
will
realise
that
we
may
allow
interrupts to be interrupted.
That is perfectly true.
It is
rather like someone starting to peel the potatoes when the kettle
interrupts
by
coming
to
the
boil,
so
the
potato
cleaning
task is
PAGE 8-1
making
the
potatoes.
pot
of
The
trick is
tea,
at which
to
time we
Not
all
disable)
costs.
interrupts
flag,
Many
as
such
can
some
be
are
blocked
crucial
circumstances
by
and
may
setting
must
arise
during
for
the
two
type
different
input
pins,
one
for
each
the
get
(interrupt
through
the
at
control
immediate action.
two conditions, it
of
interrupt.
One
all
of
To
has
of
these is the NMI or Non Maskable Input pin which cannot be blocked,
and the other the IRQ or Interrupt ReQuest pin -which is masked off
by the I flag.
(telling
the
6510
where
to
return
to
when
it
on the stack
has
finished
saving
certainly,
of
A,
data
X
and
is
Y
really
would
only
be
half-measure
changed
during
the
as,
almost
interrupt
procedure.
Next the I flag is set to ' 1' to prevent further interrupts and
then the appropriate address for the appropriate interrupt routine
is loaded into the PC.
These addresses are found at 65530 and
65531 ($FPFA and $PPPB) for an NMI interrupt and 65534 and 65535
($PPFE and $FPPF) for an IRQ interrupt. These interrupt routines
IRTI
On meeting this,
|
1
(a) restores the SR so that the status flags are the same as when
the interrut occurred,
Unfortunately,
on the stack.
into A before being pushed onto the stack (by a PHA), and when
pulled off the stack (by a PLA) it will need to be restored to the
appropriate register by a TAX or TAY.
PAGE 8-2
BRKBReaK
~~"~'"
resets the
PC by indexing this by
one place (so that the PC points to the
byte following the BRK instruction), and stores this address on the
stack then it sets the Break Flag (B flag) which is bit 4 of the SR
and
stores
it
on
the
stack
also.
Following
this,
the
6510
then
does a normal IRQ interrupt using the IRQ VECTOR at $FFFE (LSB) and
$FFFF (MSB). The IRQ routine will check whether this interrupt was
caused
64,
by a
true
IRQ
or
BRK
instruction.
Normally,
on
the
C-
if the IRQ routine discovers that it was a BRK entry, then the
routine will jump into BASIC, the screen is cleared and READY will
be displayed.
As you will have discovered in chapter 6, however,
nothing so mundane and useless will occur when you are the owner of
a Dr. Watson assembler.
A BRK instruction on the
Commodore 64
will cause an entry to Monitor, great stuff for debugging!
To test this,
PROGRAM 8.1
LDAIM 90
STA
1024
LDAIM 1
STA 55296
BRK
When run, this will print a white diamond in 1024 and then go into
the interrupt routine, which on the Watsonised Commodore 64 enters the
Monitor.
find
in 65368
($FF58).
Before Watsonisation,
of the
790
called
point
(addresses
to whatever
pointing
routine
to
the
routines)
at
designers wish.
$FFFA and
On a
$FFFE
computer
can
like
PAGE 8-3
is
feature of
This use
enables us- to
of RAM
trap into
Signed Numbers
In
all
the
mathematical
exercises
done
so
far,
the
numbers
used
have
been
treated
as
simple
positive
numbers.
Thus,
any
arithmetical processes have dealt with these numbers as strings of
eight bits.
However,
if negative integers are
to be used
in
arithmetic,
byte
used
a
one
of
the
bits
must
be
used
to
indicate
that
given
it
is
magnitude
one
of
the
negative.
number
By
using
stored
in
one
the
bit
in
remaining
this
way,
seven
the
bits
is
number.
One
in theory,
of
the
problems
that
arises
from
this
usage
i.e.
is
that,
+zero and
-zero:
+0
00000000
-0
10000000
In order
representation,
To .convert
of all
it
or vice versa,
i.e.
of the
00100110
Next 1
positive
number
from
a 0
to
to its COMPLEMENT:
M 1011001
i.e.
11011001
+
1
11011010
-3810
To
understand
the
significance
110110102
of
this
representation,
i)
PAGE 8-4
=
=
=
11011010
00100110
00000000
i.e.
three
sums
ii)
43io - 3810
43io
001010112
-3810
110110102
i.e.
iii)
-38
11011010
+43
00101011
00000101
5_10
24!0 - 3810
2410
i.e.
24
=
-
000110002
00011000
-38 = +11011010
11110010
00001101
on 1
i.e.
00001101
+
00001110
-14m
Overflows
In signed number arithmetic the seven "magnitude" bits
(i.e.
0 to
i.e.
10010
011001002
+ 0001111 Oo
100000102
011001002
and
*100110112
then add 1
100110112
-#>100111002
PAGE 8-5
This flag
BVC
BVS
(V=0),
BVS tests
then a branch
is executed.
an
overflow
occurs
this
must
be
transferred
to
the
most
significant byte.
the
use
of BVC
to
test
for an overflow as
PROGRAM 8.2
CLC
LDAIM 0
ADCIM 1
BVC
252
STA
1024
LDXIM 1
STA
55296
RTS
END
When run,
until
the. right-most
seven bits
are
filled with
ones.
At
the next
The STA
with
other
flags,
provision
exists
for
the
control
of
the
be
set
so
the
CLV
Unlike
(as
on
the
other
the
flags,
carry
programmer wants
however,
flag).
to do,
in
It
the
the
isn't
overflow
really
normal way of
flag
cannot
something
things anyway,
PAGE 8-6
that
all
the
numerical
computer display
a table, it
as
numbers
examples
to
date,
code. While it
is
possible
only
256
one
three
character,
8.3
to
tackles
where
base
represent
this
can
thus
10
the
display O^q
if it
is
has
been
in
this using
displays
would
less
conversion
is effectively
to 25510 using
require
up
to
out
to
same value.
task
and
output
interpret
representation and
characters
Program
to
a base
screen
greater
than
first
sets
first digit is
100
i.e.
'2'
first
digit
display
value
11 '.
It then checks in a similar way for the tens and units and
uses the stack to store the remainder (i.e. so far unprocessed
portion) of the number while adding the conversion constant (48) to
the
accumulator
equivalent.
to
change
the
binary
- is loaded into
the accumulator at
value
to
the
PAGE 8-7
PROGRAM 8.3
LDXIM 1
LDAIM 152
CMPIM 200
PHA
LDAIM 50
STA
STX
1024
55296
TENS
Jump to tens
routine
Clear carry.
CMPIM 100
BCC
TENS
SBCIM 100
PHA
LDAIM 49
STA
STX
1024
55296
Load A with T
for 1
x 100.
Print A on screen
Store white in colour RAM
Retrieve A from stack
PLA
TENS CLC
Clear carry
LDYIM 0
CMPIM 9
BCC ZEROTENS
LOOP INY
Set Y to Zero
Compare A with 9
Branch if A less than 9
Increment X
SBCIM 10
Subtract 10 from A
CMPIM 9
BCS
LOOP
Compare A with 9
TYA
ADCIM 48
ZEROTENS PHA
to A
STA
1025
Print A on screen
STX
55297
PLA
ADCIM 48
Add
conversion constant
to A
STA
1026
Irint A on screen
STX
55298
RTS
PAGE 8-8
for 2 x 100
PLA
JMP
ONEHUND CLC
Print A on screen
Load white
152
CMPIM 200
BCC
15
into X.
than 200,
if so branch
SBCIM 200
PHA
LDAIM 50
STA
STX
'2'
for 200
1024
55296
PLA
JMP
868
CLC
CMPIM
100
BCC
12
SBCIM
100
if so
PHA
Subtract
LDAIM 49
STA
STX
than 100,
branch*
'1'
for 100
1024
55296
PLA
CLC
LDYIM 0
CMPIM 9
BCC
7
than or equal to 9;
if so,
branch
INY
SBCIM 10
CMPIM
9
BCS
PHA
249
TYA
ADCIM 48
STA
STX
1025
55297
PLA
ADCIM 48
STA
1026
STX
55298
RTS
When run, this program will print '152' in white at the top
left-hand corner of the screen.
In general terms it can be used as
a subroutine which prints out the contents of the accumulator.
PAGE 8-9
Floating-Point Numbers
With
the
integers
processes
in
used
carried
BASIC,
out
however,
so
has
the
far,
been
the
scope
somewhat
binary
of
the
restricted.
floating-point
arithmetical
When
constants
working
have
10
two
'accumulators'
are
provided
in memory
locations
store
displayed
number
in base
six bytes.
How
which
10
is
form,
this
could
the
running BASIC
programs,
very
small
are
notation.
Thus,
to
66-jg
contain
up
floating point
to
10
digits
when
accumulators use
only
trick managed?
When
numbers
61 -jg
expressed
4079.013
can
in
be
seen
that
very
exponential
form
or
expressed
as
large or
scientific
0.4079013E+4
or
the first
two
parts
iccumulators
(a)
in
are
the
in
binary
AFAC.
The
sign
'1'
of
on bit
signifies a
the
floating
point
the
mantissa
is
stored
in
the
sixth
byte
positive mantissa.
this
the
has
exponent
this
is
stored
yield
done
as
to
store
needs
97
,05
both
to
be
positive
converted
by adding 128.
128+20,
128-20,
FAC
AFAC
i.e.
98
106
i.e.
48,
Thus,
while
loading a
and
to
negative
an exponent
an
exponents,
positive
exponent
form
and
of 10 would be
of
-20
would
108.
99
107
100
101
102
108
109
110
Exponent-
When
in
(b)
stored
following manner:
Sign
Bit -
floating-point
representation and
thus its
number,
BASIC
normalises
left-most
(most
significant)
its
binary
digit
is
always
1.
Take,
for
instance,
the
number
+140010
(05781g).
Expressed in binary this is 0000 0101 0111 10002.
In this form,
PAGE 8-10
i.e.
0000 0101
0111
10002.
O*-Binary point or
RADIX
binary
point
moved
just
past
the
*211
been moved
11
places
value.
Finally this
is
in
its
least-significant
bytes
i.e.
Byte 2
Byte 3
Byte 4
Byte 5
1010 1111
0000 0000
0000 0000
0000 0000
i.e.
this byte
is added
to
the
first
Byte 1
1000 1011
Byte 2
1010 1111
Byte 3
0000 0000
Byte 4
0000 0000
Byte 5
0000 0000
1000 1011
Byte 2
1010 1111
Byte 3
0000 0000
Byte 4
0000 0000
Byte 5
Byte 6
0000 0000
0??? ????
floating-point
accumulator
(FAC)
by
means
of
the
subroutine
at
B39116 f45969).
PAGE 8-11
To accomplish this,
PROGRAM 8.4
LDY LSB
LDA MSB
JSR $B391
RTS
These two floating-point accumulators - FAC and AFAC - are used for
the manipulation of floating-point data and a means is provided for
transferring floating-point data into
Taking
AFAC,
for
instance,
the
them.
subroutine
for
loading
floating
point data from memory resides at $BA8C (47756) and it moves the
contents pointed to by the Accumulator (LSB) and the Y register
(MSB) into AFAC.
Thus, if the data is stored at $0350 onwards,
program 8.5 carries out this transfer:
PROGRAM 8.5
LDAIM 80
LDYIM 3
JSR
$BA8C
RTS
this
at $BBA2 (48034).
Unfortunately,
accumulators,
has
the
being
located
content
of
either
FAC
or
AFAC
been
for as in no case
visible.
To rectify
command
allows
machine-code program.
program
causes
the
the
transfer
of
For instance,
system
to
place
data
between
FAC
and
value
of
into
the
floating-point accumulator.
It then jumps to a machine code
routine whose address it finds at $0311 (785) LSB and $0312 (786)
MSB.
The presumption is that you have placed a machine code
routine into memory starting at that address and
$0311
set
the values of
use of FAC it will, of course be using the value that was in Q when
the USR function was called.
When the particular arithmetic
process has been carried out, your routine should leave the answer
out
the
result
of
the
machine
be
placed
by 2.
To
test
this,
accumulator
This
is
by
done
number
means
by
of
means
can
the
of
USR
the
into
function
two
the
and
programs
floating-point
then
printed
program
out.
8.6a
and
program 8.6b.
PROGRAM 8,6a
20 000 PRINT"<clear>11
60=$3C lAddress=$O33C
3=$O3 J
20 030 B=1400
20 040 Q=USR(B)
20 050 PRINT"Q=";Q
PROGRAM 8.6b
828 RTS
When line 20 040 is run, the argument B (1400) is loaded into the
floating-point accumulator.
Control is then handed over to the
machine code program at $O33C (828).
At this point, the machine
code routine doesn't actually modify the value of FAC, it simply
obeys the RTS which will return control to BASIC.
BASIC will place
the contents of FAC into Q.
Line 20 050 prints out the value
stored in Q, which has not been modified by the machine code
routine.
changes
use
its
the
machine-code,
BASIC
gets
102)
and
when
its
then
operating;
However,
if
hands
to
on
them
thus
the
should be amended
program 8.7.
FAC
contents.
set
again.
even
contents
To
they
do
can
these
on
the
screen.
be
this,
PEEK
are
command
examined
seen
program
to $66
This
is
in
before
8.6b
(97 to
done
in
PAGE 8-13
PROGRAM 8.7
LDXIM 6
LDYIM 1
LDAX
96
Load Y with 1
STAX
for white
1424
TYA
Transfer 1
STAX
DEX
55696
BNE
243
to A
Load white
Decrement
Branch
in colour RAM
loop counter
if more
to do
RTS
20 020 P0KE786,3
20 030 B=1400
20 040 A=USR(B)
20 050 PRINT"A=";A
20 060 F0RX=0T05
";
20 080 NEXT X
This
and
the answer.
program in is
that
the
the
One
locations
that
display
the
contents
l<home>1
in
line
20 000 indicates
that you
type the unshifted CLR/HOME key (easier for you to interpret than a
reversed heart in the listing).
When run,
this will
display:
A =
and
the contents:
139
When
1400
compared with
175
the calculated
47
contents
on page 8-11
it will be
seen that bytes 1-5 are identical, while byte 6 contains something
different.
In fact it is only the most-significant bit that is of
consequence
and
indicated.
Just
as
to
this
is
verify
set
this,
to, a
'0',
change
20
read
20 030
PAGE 8-14
B = -1400
positive
030
in
mantissa
is
program 8.8,
to
MSB$O5-5.
They are loaded into Y (LDYIM 120) and A (LDAIM 5).
PROGRAM 8.9
LDYIM 120
LDAIM 5
JSR
$B391
LDXIM 6
LDYIM 1
LDAX
96
STAX
1424
TYA
STAX
DEX
BNE
55696
243
RTS
CONVERT INTEGER TO FLOATING-POINT
Program 8.8 should then be modified so as not to put 1400 into FAC,
i.e.
20 030 B=1
The
suite
of programs
then be
executed
using
RUN
20000
and
or:
PAGE 8-15
PAC
97
98
99
100
105
106
107
108
139
250
0000 0000
0000 0000
AFAC
location
1000 1011
PAC
1111
1010
101
102
109
110
0000 0000
0000 0000
APAC
location
a more economical
This is known as
MPLPT (Memory PloaTing PoinT) format; the six byte format used in
FAC and AFAC is known as FLPT.
allowance
mantissa
and is used
'1'
made
when
this
this.
to store
the
for negative,
MPLPT
for
using
format.
by
This
adding
redundant
sign bit,
i.e.
to
the
therefore,
'0f
for positive
The
representation
of
left
bit,
+2000
in
of
a
giving us the
the
alternative
1000 1011
b)
1111
1010
0000 0000
0000 0000
0000 0000
1000 1011
139
PAGE 8-16
0111 1010
122
0000 0000
0
0000 0000
0
0000 0000
0
be
and
a)
the
can
0000 0000
be
loaded
into
memory,
and
this
is
most
easily
done
using
PROGRAM 8.10
POKE 853,139
POKE 854,122
POKE 855,0
POKE 856,0
POKE 857,0
this
subroutine
sets a pointer to
the data in 853in (O385ifi) onwards and then transfers it with a JSR
$BBA2.
Program 8.11
PROGRAM 8.11
LDAIM 85
LDYIM 3
$BBA2
JSR
LDXIM 6
LDYIM
LDAX
96
STAX
TYA
1424
STAX
55696
DEX
BNE
243
RTS
TRANSFER DATA FROM MEMORY TO FAC
for transferring
data,
AFAC
exists
this
the
copying
data
from
is
one
loaded
to
the
A similar subroutine
into
FAC
other,
copies
the
or
that
for
other way,
facility
copying
i.e.
AFAC
into
from AFAC
for
FAC
to FAC,
PAGE 8-17
EXERCISE 8.1
Write a suite
AFAC
from
contents
transfer
of programs
memory
and
of
this
from
AFAC into FAC
the contents,
to
load
then
2000
into
out
the
1424 onwards.
Then
and again print out
this time
Floating-Point Subroutines
Having
got
numbers
into
FAC
and
AFAC,
use
can
be
made
of
the
these
on
relocated
BASIC
for
the
PET,
V2
BASIC
on
V2
on
the VIC
the Commodore
64.
20
and
The
they
are
addresses
now
quoted
comprehensive
3..
In
list
addition
to
of
the
these
addresses
addresses
for
is
the
provided
C-64,
in
the
Appendix
equivalent
addresses are provided for the VIC 20, PET BASIC 2.0, and BASIC
4.0.
The equivalent addresses will be found most useful when
setting out
written
to
convert
code program
A further caveat must be given along with the advice to try these.
One must know where these subroutines pick up their data from and
where
they deposit
the
result,
if they are
to be used safely.
Many
the
subroutine
proper
begins
and
then
expects
retrieves
its
data
address
from
and it is
these JSR and JMP jaunts that you may find difficult to follow.
PAGE 8-18
EXERCISE 8.2
code
square-root
to
program.
of
the
input
the numbers
Multiply
sum.
these
out
together
this
answer
and
on
then
the
take
the
screen
from
looked
like
BASIC.
Second thoughts!!...
This
is
not
as
easy
as
it
seems;
the
original
plan
this:
20 040
Puts B into FAC
20 080 C=USR(D)-^
Puts D into FAC
in
fact,
constantly
changing
as
BASIC
runs
program.
Following line 20040 FAC contains B and after the JSR (FAC- AFAC)
both FAC and AFAC contain B.
However, in executing lines 20050 to
20080 the operating system utilises FAC and AFAC, and thus changes
the
contents
of
these.
Thus
when
JSR
(MULT)
is
called,
the
those
expected.
The
problem
returning
$BBC7.
to
can
only
BASIC.
be
This
overcome
is
done
by
by
saving AFAC
means
of
in memory while
the
subroutine
at
the five
PAGE 8-19
PROGRAM 8.15
LDAIM
132
STAZ
$49
'1
.f
LDAIM 3
STAZ
$4A
d|
- 132
$BBC7
JSR
RTS
can be
program:
(47756).
it is necessary to reload
the subroutine at $BA8C
a "reload AFAC" program to recover data from 900 onwards would look
like:
PROGRAM 8.16
LDAIM 132
LDYIM 3
JSR $BA8C
RTS
Perhaps
now is
suitable
time
to
try Exercise
8.2.
One
possible
lists
useful when
code.
altogether 17
dealing with
subroutines
that have
floating-point
numbers
been
from
found
machine
Addition
Using the subroutine at $B86A (47210),
in
FAC
and
AFAC
are
added
together
and
the
the
floating-point numbers
sum
loaded
into
FAC.
PAGE 8-20
PAGE 8-20
PROGRAM 8. 17
20 000 PRINT1 <CLR>"
20 010 POKE
1,60
INPUT B
20 030
A=USR(B)
20 040
20 080 INPUT D
C=USR(D)
20 090
100 PRINT"C=";C
20
PROGRAM 8. 18
828 LDAIM 132
STAZ
72
LDAIM 3
Store FAC
STAZ
74
JSR
$BBC7
RTS
847
in memory
$B86A
JSR
RTS
Retrieve data
from
memory,
in AFAC
store
m
Addition subroutine
Jr
When
the
prints
program at
out
the
20000
is
run,
it
requests
two
inputs
and
then
sum of these.
Subtraction
Programs 8.17 and 8.18 can be used
to demonstrate
this by inserting
can be
subtracts
the second
input
from the
will
out
the
answer.
It
first.
Division
Once again we may use programs 8.17 and 8.18 to demonstrate the use
before.
PAGE 8-21
Exponentiation
The exponentiation routine is at $BF7B (49019) - so replace 848 and
849 once again.
PROGRAM 8.21
(Part)
the first
Other routines need only one input and operate on this alone;
these
are:
Log
The subroutine is at $B9EA (47594) and computes the natural
logarithm or loge (log to the base e).
Program 8.22 demonstrates
this
subroutine
in use.
PROGRAM 8.22
831
RTS
PROGRAM 8.23
20 040 A=USR(B)
20 050 PRINT"A=";A
A
RUN
20000
input value.
PAGE 8-22
activates
both
routines
and
prints
out
loge
of
the
the
input
function is
Appendix
returned
find
which
the
develop more
is
exist
list
entered
in
radians
and
In each
the
computed
in FAC.
contains
subroutines
you
value
very
within
somewhat
experience,
comprehensive
the
C - 64
bewildering
however,
at
feel
list
ROM,
of
and
first
sure
the
sight.
that
many
suspect
you
that
As
will
you
find
the
assembler LIST
function.
Next
these
should
be
examined
for the first use of A, X and Y as once these are re-loaded in the
subroutine, any data originally in them will have been destroyed.
such as $22/$23
zero-page
locations
(73/74).
are
popular
Appendix
Similarly,
last
few
memory
instructions
locations
and
should
if
FAC
feature.
and
AFAC
The
end
are
of
involved,
the
their
subroutine
is
Finally,
or
if logic
bust1
attempted,
technique
left,
keep
records
good
i.e.
as
trying
constant
it.
reloading
If
'shoot
this
after
is
crashes
can be time-consuming.
Of course, after a crash the machine-code
program is not lost provided control can be regained by use of the
STOP/RESTORE keys.
computer off and
then on again
to regain
control
of
it , then
the
chances are that you will lose the machine code program in memory.
The moral is to use the T=TAPE option of the Dr. Watson Assembler
to save any lengthy routine before risking a run.
PAGE 8-23
PAGE 8-24
CHAPTER 9
Solutions to Exercises
Chapter 1
Exercise 1.1
(Page 1-10)
' 1 '
it
FRED
and
you
seems
reasonable
to
use
STA
1024
55296
STA
RTS
END
Exercise 1.2
Assuming
black,
that
(Page 1-10)
your
name
is
wish
to
this
in
STA
1024
LDAIM 0
STA
55296
LDAIM 18
STA
1025
LDAIM 0
STA
55297
LDAIM 5
STA
1026
LDAIM 0
STA
55298
LDAIM 4
STA
1027
LDAIM 0
STA
55299
RTS
END
This is a perfectly good solution and does what it sets out to do.
However, you may think that there is scope for improvement,
there is.
The LDAIM 0 occurs four times in the solution above,
and
and
PAGE 9-1
STA
1024
LDAIM
18
STA
1025
LDAIM 5
STA
1026
LDAIM 4
STA
1027
LDAIM 0
STA
55296
STA
55297
STA
55298
STA
55299
RTS
END
Exercise 1.3
the
two.
(Page 1-10)
STA
1024
STA
1063
STA
1984
STA
2023
LDAIM 2
STA
55296
STA
STA
55335
56256
STA
56295
RTS
END
PAGE 9-2
Exercise 1.4
(Page 1-13)
LDXIM 1
STX
900
TAX
LDA
900
STA
1024
STX
STA
2023
55296
STA
56295
RTS
END
third
Exercise 1.5
Immediate Mode
instruction.
(Page 1-13)
LDAIM 90
LDXIM 42
LDYIM 5
STX
900
TAX
TYA
LDY
900
STA
1024
STA
1063
STX
1984
STY
STA
2023
55296
STA
STA
55335
56256
STA
56295
RTS
END
Once again, in order to comply with the condition that only three
LD?IM instructions were to be used, this solution uses the 5 (fEf)
to provide the colour,
to be green.
PAGE 9-3
Chapter 2
Exercise 2.1
i)
(]>age 2-5)
START
ADD? 828
LDAIM 3
JSR
900
STA
1024
LDAIM 1
55296
STA
RTS
END
ii)
START
ADD? 900
STA
9^0
ADC
950
RTS
END
Exercise 2.2
(Page 2-7)
JMP
STY
STY
3
830
1024
55296
RTS
END
Exercise 2.3
(Page 2-10
LDAIM 83
STA
890
LDYIM 0
INY
CPY
890
BEQ
3
835
1034
JMP
STY
LDAIM 4
55396
RTS
STA
END
PAGE 9-4
Exercise 2.4
(Page 2-12)
890
INX
CPX
890
BNE
250
STX
1024
LDAIM 1
STA
55296
RTS
END
Exercise 2.5
(Page 2-15)
LDXIM 90
DEX
CPX
900
BPL
250
STX
STX
1024
55296
RTS
END
PAGE 9-5
Chapter 3
Exercise 3*1
(Page 3-1)
Table 1
From the
POKE 828,160
does
the trick.
POKE 832,153
POKE 837,153
Lastly,
136)
i.e.
Thus
POKE 840,136
the
final
program reads:-
LDYIM
100
LDAIM 90
1023
STAY
LDAIM
STAY
55295
DEY
BNE
243
RTS
END
Exercise 3.2
(Page 3-2)
LDXIM 100
Load X with
STX
Store X
900
Load
LDAIM 42
Load A
INX
900
BNE
242
END
CPX
RTS
f0f
with 42 (asterisk)
r\ **\ ^
LDAIM 1
STAX 55295
PAGE 9-6
X with
LDXIM 0
'100'
in 900
Chapter 4
Exercise 4.1
(Page 4-6)
18O716
2AFA16
i.e.
1 x 4096 + 8 x 256 + 0 x 16 + 7 x 1
4096 + 2048 + 07
2 x 4096 + 10 x 256 + 15 x 16 + 10 x 1
1100210
61511O + 1100210
1715310
Program to add
START ADD? 828
CLC
CLD
LDAIM $07
ADCIM $FA
STA
1026
LDXIM 1
STX
55298
J an instruction later on
LDAIM $18
ADCIM :$2A
STA
STX
1024
55296
RTS
END
6710 = 4 x 16 + 3 x 1
$43
0110 = 0 x 16 + 1 x 1
$01
- $4301
"4x4096+3+256+0x16+1
PAGE -9-7
Exercise 4.2
(Page 4-7)
1036
STA
LDXIM 1
STX
55308
LDAIM 3
SBCIM 2
1034
55306
STA
STX
RTS
END
Ans =
@ andD
0 and 200
Exercise 4.3
(Page 4-8)
Clear decimal to make sure in
CLD
binary mode
CLC
LDAIM $2C
ADCIM $90
STA
900
Load LSB 1
Add LSB 2 to A
LDAIM $01
Load MSB 1.
ADCIM $01
STA
901
Add MSB 2
SEC
Add LSB's
together
Sum of LSB's
Sum of MSB's
1 Add MSB's
, together incl.
incl.
carry
J (if there)
for subtraction<
LDA
900
SBCIM $F4
STA
1041
LDXIM 1
STX
55313
LDA
901
SBCIM $01
Subtract
LSB's
from LSB3.
and
display
in white
Subtract
MSB's
from MSB 3
and
STA
1040
display.
STX
55312
in white,
RTS
END
PAGE 9-8
(Page 4-12)
A = 1
B = 0
C (output) = 0
A = 0
A = 1
B = 1
B = 1
C = 0
C - 1
Exercise 4.5
(Page 4-13)
100101012
00110100p
1O
149 AND 52
16+4
20
Exercise 4.6
(Page 4-17)
128
100
87
75
99
57
94
27
10
64
32
16
ss
Thus:
i)
100
AND
87
010001002
0 + 64 + 0 + 0 + 0 + 4 + 0 +
68
10
LDAIM 100
ANDIM 87
STA
1024
LDXIM 1
STX
55296
RTS
END
PAGE 9-9
ii)
75
OR
27
O1O11O11
0 + 64+0
9110
16+
LDAIM 75
ORAIM 27
STA
1024
LDXIM 1
S.TX
55296
RTS
END
Lii)
99
EOR
57
01011010
0+64+0+16+8+0+2+0
LDAIM 99
EORIM 57
STA
1024
LDXIM 1
STX
55296
RTS
END
94
100
AND
87
EOR
00011010
0+0+0+16+8+0+2+0
PAGE 9-10
26 10
LDAIM
100
ANDIM 87
EORIM 94
STA
1024
LDXIM 1
STX
55296
RTS
END
Exercise 4.7
(Page 4-18)
861Q in BCD
10000110
which in binary =
1 34-jq
ANDIM 15
STA
1025
LDXIM 1
STX
55297
LDYIM 4
LDAIM 134
LSRA
DEY
BNE
252
STA
STX
1024
55296
RTS
END
PAGE 9-11
Exercise 4.8
(Page 4-22)
901
LDXIM 4
STX
902
LDYIM 8
902
BCC
CLC
ADC
901
ASL
901
DEY
BNE
240
STA
1024
LDXIM 1
STX
55296
RTS
END
Chapter 5
Exercise 5.1
(Page 5-3)
LDXIM 80
*L00P1
LDYIM 2
JMP
JMP
L00P3
865
LDAIM 83
LDAIM 83
STAX
TYA
1183
STAX
TYA
1183
STAX
55455
STAX
55455
BNE
JMP
DEX
L00P1
END
BNE
JMP
244
883
LDAIM 90
STAX
1023
TYA
STAX
55295
LDAIM 90
STAX
1023
TYA
STAX
55295
DEX
DEX
BNE
JMP
PAGE 9-12
LDXIM 80
LDYIM 2
DEX
L00P2
.22.
L00P2
L00P1
BNE
JMP
244
835
*L00P3
LDAIM 42
LDAIM 42
STAX
1223
STAX
55495
STAX
TYA
TYA
STAX
DEX
DEX
BNE
END
1223
LOOPS
BNE
244
LDXIM
120
LDXIM 120
DEY
DEY
JMP
JMP
L00P2
RTS
55495
850
RTS
END
Chapter 7
Exercise 7.1
(Page 7-6)
POKE 829,32
900
JSR
65508
251
65490
BEQ
JSR
CMP
BNE
900
243
LDAIM 13
65490
JSR
RTS
END
Exercise 7.2
(Page 7
LDXIM 4
TXA
PHA
PHP
JSR
$FFE4
BEQ 251
JSR $PFD2
PLP
PLA
TAX
DEX
BNE 239
RTS
END
In the above program, SR could have been loaded into stack before
the X
register
retrieved first,
(via
i.e.
the
accumulator),
LIFO operation.
as
long
as
it
was
then
PAGE 9-13
Chapter 8
Exercise 8.1
(Page 8-18)
is:
139
It
122
PROGRAM 8.12
POKE 900,139
POKE 901,122
POKE 902,0
POKE 903,0
POKE 904,0
The machine code
routi
is:
PROGRAM 8.13
LDAIM 132
Set up address
LDYIM 3
O5i6=O3io : 8416*13210
JSR
$DA8C
LDXIM 6
LDAX
96
STAX
1423
for subroutine
from memory-
LDAIM 1
STAX
DEX
BNE
55695
242
$DBFC
LDXIM 6
JSR
LDAX
96
STAX
1623
LDAIM 1
STAX
55895
DEX
BNE
RTS
END
PAGE 9-14
242
This
BASIC
program is:
PROGRAM 8.14
20 000 PRINT "<clear>"
20 010 POKE 785,60
20 020 POKE 786,3
20 030 B=1
20 040 A=USR(B)
(for example)
20 100 PRINT
20
128
139
250
122
READY
Exercise 8.2
(Page 8-19)
20 000 PRINT
"<clear>"
20 040
A=USR(B)
20
C=USR(D)
100 PRINT
HC=";C
PAGE 9-15
LDAIM 132
STAZ
73
LDAIM 3
STAZ
74
JSR
$ABC7
RTS
LDAIM 132
LDYIM 3
$AA8C
JSR
JSR
$A82B
JSR
$AF71
RTS
END
Appendix 1
Exercise A1.1
(Page A1-2)
i)
0000 00112
ii)
0000 01002
iii)
1000 00002
iv)
1000 0011?
v)
1011 01112
vi)
0111 00112
Exercise A1.2
7)
ii)
0+0+0+0+0+0+2+1
310
0+0+0+0+0+4+0+0
410
128+0+0+0+0+0+0+0
(Page A1-5)
' OOO9i6
0013-, 6
12810
128+0+0+0+0+0+2+1
128+0+32+16+0+4+2+1
0+64+32+16+0+0+2+1
0x409+0x256+0x16+9x1
a
0+0+0+9
910
0x4096+0x256+1x16+3x1
iii)
OOA5!6
iv)
OAAE16
v)
PAGE 9-16
OOOE16
0+0+16+3
1910
0+0+10x16+5x1
160+5
0+10x256+10x16+14x1
2560+160+14
0+0+0+14
1*10
vi)
OHA16
0+256+16+10
vii)
OOEA16
28210
0+0+14x16+10
=
viii)
FOA3!6
224+10
15x4096+0+10x16+3
61440+160+3
=
Exercise A1.3
(Page A1-7)
616O31O
TJ
410
01002(BCD)
ii)
1010
1x10+0
iii)
77! 0
iv)
97!0
v)
53iO
vi)
10210
vii)
953!0
viii)
2579!0
Exercise A1.4
7x10+7
0111 01112(BCD)
9x10+7
1001 01112(BCD)
5x10+3
0101
00112(BCD)
1x100+0x10+2x1
0001
0000 00102(BCD)
9x100+5x10+3x1
1001 0101
00112(BCD)
2x1000+5x100+7x10+9x1
0010 0101 0111
(Page A1-8)
T)
ii)
0000 10012(BCD) =
iii)
0001 01012(BCD) =
iv)
0010 00002
0100 10012(BCD) =
1io
0x10+9x1
910
1x10+5x1
2x10+0x1
v)
10012(BCD)
2010
4x10+9x1
PAGE 9-17
r)
1010 00112(BCD)
***
This
first
is
nybble,
not
10102
valid
vii)
1001
01112(BCD) =
0x10+7x1
97
viii)
1000 10002(BCD) =
8x10+8x1
PAGE 9-18
BCD
10iq
8810
number
i-e-
is
as
the
greater
APPENDIX 1
Binary, Binary-Coded Decimal,
Hexadecimal Notation
Counting
systems
in
general
use
throughout
the
world
use
the
right.
For instance,
times
This
the
is
second,
i.e.
extended
in
larger
numbers
where
of ten,
6 x 100%
digits
to
the
left
are
i.e.
^6 x 10
6 x 1000
6666
the
place
to
the
left
is
greater
by
being
multiplied
by
the
base.
The computer,
if it
is
told
to only recognise
two states,
on or off or
f0f
and
volts
(off)
and
system
must
cases,
the
number 101
some volts
resort
multiplying
(on).
To
place-value
factor
is
count
notation
the
base,
past
one,
and,
as
i.e.
2.
the
binary
with
other
Thus,
the
i.e.
when
to
x 4-4
0x2
*1
x 1
4+0+1=5.
Clearly the plethora of, bases presents a problem
representing numbers as in base 10,
f 101 ' represents one
a
is
Thus,
(base. 2)
convention
written to
'101'
represents 5.
To
exists when
representing
the right of the number,
PAGE A1-1
101io
1012
The
+4
+2
+1
Digit
Equivalent
64
128
16
32
Pig.
By
way
of
example,
let's
in
base 10
A1.1
take
one
more
conversion
say,
1010101112.
1
x 128
1 x 32
Thus 10100111
0 x 16
X 1
0x8
- 16710
Just to check your understanding, have a go at the following:
EXERCISE A1.1
ii)
iii)
iv)
v)
vi)
000000112
000001002
100000002
100000112
10t101112
011100112
If you
remain unclear on
this,
or
simply want
to
see
demonstrated, load and run the Binary/Hex tutor program which
included
"Decimal,
on
the
Binary
assembler
and
tape.
HexadecimalH.
At
the
Then,
menu
when
display
three
rows
of
boxes,
of
which
select
asked
'H*
"At
it
is
for
what
the
top
two
are
currently of interest.
These represent a decimal number (marked
HDEC") and a binary number (marked "BIN")/ At this stage, they
should contain the numbers 1jq and 12^
The decimal number has
three digits and thus has a capacity of 999iqi and the binary, with
PAGE A1-2
From this point, the program will simply count, every time that you
press
the space bar, both the decimal and the binary boxes will
index one.
Try pressing the space bar once and the boxes should
contain a 210 and a IO2.
If you carry on indexing then you will
see how binary counts.
When you get to the stage where the decimal
1111
B.
'
" ';
On adding the 1 (A) to the 1 (B) this gives f2f i.e. 0, carry 1.
This carry then produces another '01 plus another carry, and so on.
If you continued
to press
the
space
bar
long enough,
then
eventually the binary register would become full.
However, this
would take an awful lot of pressing, so we will take a short cut to
this state of affairs.
Instead of pushing the space bar, press the
RETURN key instead.
This will return you to the menu where you can
lost.
carry
to
make
notation,
In
order
you
may
sure
This
wish
that
to
you
try
will
really
some
a menu
of
understand
the
Select
the
exercises
*Ef
of exercises
at
binary
which
are
exercises,
delete
the
by the way,
last
entry
correction facility.
enough, pressing the
typing a space
that
you
made,
thus
menu.
While the O's and 1 *s are convenient for the computer, they are
much less so for the mere human so a compromise is sought.
Decimal
notation is of little
other correspondence.
That's apart
PAGE.AU3
256 digits!
A compromise system adopted
and
represents
represented
is
these
11112
or
15-iq and
1
0 1
this
requires,
along with
2 3 4 5 6 7 8 9 10 11
12 13 H 15
Decimal number
234 5 6789 A
Symbol
Fig.
one
for
the 0,
F
A1.2
the
most
significant
four
bits
and
one
for
the
byte
consists
of
two
nybbles,
most
significant
nybble
1 I 1 I 1 1-1 ) - 2551O,
LSN
Fig. A1.3
binary
number
can
be
again.
The third row of boxes, which we ignored last time round,
contains the Hex numbers.
While the counting is going on in the
for instance 11112, 15|q or F16: one index past this clocks the
binary ones to zeroes and adds a one to the left, i.e. to 100002 or
lOg
PAGE A1-4
16
0001
0000 0001
0001
00002
110
161O
0000 00002
1OO16
= 25610
1000-15
=4O961O
numbers.
After 15, Hex to decimal conversion becomes a little more
tricky, as the use of two numbers together, e.g. FF1g=255, once
again calls for place-value notation.
This time, as the base is 16
the ratio between any place and its neighbour is 16.
The values,
4
Fig.
Place Number
A1.4
Using" Fig. A1.4 the way that E92F16 makes up 59695.10 is explained
below in figure A1.5.
A1.5
EXERCISE A1.2
i) OOO916
ii) OO1316
iii) OOA516
iv) OAAE16
v) 000E16
vi) O11A16
vii)
viii)
PAGE A1-5
well
system
as
is
decimal,
used
in
binary
computing
and
-
hexadecimal
binary-coded
notations,
decimal.
one
As
its
other
name
BCD
factor
the
normal
of
10
decimal
times
its
represented in binary.
is
retained,
but
each
i.e.
one
place
individual
is
digit
is
8 7
1000
base
neighbour
base 10
0111
i.e.
BCD
1000
0111
(or
in
10000111)
eight
bits
Fig. A1.6
As
the
largest
digit
required
in
decimal
notation
is
9,
only four
can be
represented
by a nybble and
is 10012 and when one is added to this it clocks over to 00002 and
carries the
810
910
1010
0000 1000
i.e.
(base 2 BCD)
0000 1001
=
-
0001
0001
0000
0001
Fig. A1.7
It would probably be helpful at this point if you load and run the
PAGE A1-6
contain decimal,
bar as before,
binary and
to
index from
BCD are
rows
binary and
'1',
identical..
If you press
However,
as
you
index
10-jo keep an eye on the BCD box and you will see the 1
to
the
most
significant
nybble.
time
the space
apparent
1010
upwards
carried over
becomes
true hybrid representing the decimal number in a binary form.
From
from 9-jq
BCD
as 99^0 changes
to
100-jq*
Us
before,
typing
to
see
the
<return>
BCD generate
carry
When 99io
from
its
be lost.
times,
that,
although
all
COMMODORE
computers
have
had
BCD
into the 64's two input/output chips (6526*s) do make use of BCD.
Convert
the
following
decimal
4
10
77
97
v)
vi)
vii)
viii)
numbers
into
BCD:
i)
ii)
iii)
iv)
53
102
953
2579
PAGE A1-7
EXERCISE A1.4.
i)
ii)
iii)
OOOO 0001
0000 1001
0101
0001
iv)
v)
vi)
0010 0000
0100 1001
1010 0011
vii)
viii)
1001
0111
1000 1000
In
the
explanations
notation
given
of
simplification
the
was
value
of
places
in
place-value
adopted
in
order
to
make
these
However,
if
you
wish
to
explanation, please read on.
With
binary
value
in
numbers
multiples
it
of
see
a
slightly
more
mathematical
Otherwise - END OF APPENDIX 1.
was
2,
said
but
that
the
the
least
places
increase
significant
bit
their
of
the
binary number was equivalent to the same symbol base 10 (or for
that matter base 3, or whatever).
In actual fact the multiplying
factor is
zero at
the
base
the left.
raised
to
In binary:
Place
128
64
32
16
multiplication factor
27
26
25
24
23
22
21
20
Previously stated
Mathematically more
precise factor.
16
161
16
1622
16^
PAGE A1-8
256
4096
i.e.
(if you
The next
base 16:
APPENDIX 2
Tables
TABLE 1
used
in this
table:
X = X
register
S = Set (to 1)
C = Clear (to 0)
register
A = Accumulator
= Y
ADC
ADd
specified
answer
in
= Condition according to
contents
to
accumulator;
according
to
with
Carry:
negative,
BYTES
result.
CYCLES
ADDRESSING MODE
DEC
HEX
109
105
97
6D
69
Immediate
61
Indirect
with X
71
with Y
7D
5*
4*
4*
Indirect
ADCX
113
125
3
4
Zero-page
ADC
ADCIM
ADCIX
ADCIY
OPER.
ADCY
121
79
ADCZ
101
65
ADCZX
117
75
* Plus
cycle
store
overflow,
NO.
NO.
OP-CODE
MNEM.
accumulator
Condition
data.
Absolute
Indexed with X
Indexed with Y
Zero-page
indexed with X
operates
in
decimal
or
binary
mode
according
to
D-flag
setting.
.N
PAGE A2-1
AND
result.
OP-CODE
NO.
MNEM.
NO.
BYTES
DEC HEX
45
41
35
AND
ANDIM
ANDIX
49
ANDIY
61
57
ANDX
ANDY
57
55
ANDZ
ANDZX
Plus 1
CYCLES
ADDRESSING MODE
OPER.
2D
Absolute
29
Immediate
Indirect
5*
5D
59
25
55
4*
4*
5
Zero-page
21
51
with X
Indirect
with Y
Indexed with X
Indexed with Y
\D
N
?
B
-
ASL
A ^^
into carry,
'O1
into bit
zero;
OP-CODE
NO.
DEC HEX
BYTES
OPER.
MNEM.
ASL
14
ASLA
10
ASLX
50
6
22
ASLZ
ASLZX
N
?
05
ADDRESSING MODE
Absolute
Accumulator
1E
06
16
2
1
7
5
6
Indexed with X
Zero-page
Zero-page indexed with X
OA
PAGE A2-2
NO.
CYCLES
BCG
if clear (C=O)
branch relative.
OP-CODE
MNEM.
DEC HEX
144
BCC
NO.
NO.
BYTES
CYCLES
ADDRESSING MODE
OPER.
1
90
2*
Relative
FLAGS:
BCS
to different page.
No effect.
test
carry
flag,
if
set
(C=1),
branch relative.
NO.
OP-CODE
BYTES
MNEM.
176
ADDRESSING MODE
OPER.
DEC HEX
BCS
NO.
CYCLES
BO
2*
Relative
two cycles
FLAGS:
BEQ
if branch implemented
Branch
on
result
Egual
MNEM.
DEC HEX
BEQ
to different page.
no effect.
240
FO
NO.
NO.
BYTES
CYCLES
to
zero:
test
Z-flag,
if
set,
ADDRESSING MODE
OPER.
1
2*
Relative
no effect.
PAGE A2-3
BIT
AND
specified
unaltered;
BITs
set Z
with
(=1)
accumulator:
if bits match,
remains
transfer bits
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
BIT
BITZ
44
2C
Absolute
36
24
Zero-page
Bit
Bit
BMI
ADDRESSING MODE
OPER.
DEC HEX
IBranch
on
Z
?
result
c
-
Minus:
test
flag
if
set
branch relative.
OP-CODE
MNEM.
48
NO.
CYCLES
ADDRESSING MODE
OPER.
DEC HEX
BMI
NO.
BYTES
30
2*
Relative
BNE
no effect.
Branch on
to
zero:
test
flag if not
NO.
OP-CODE
BYTES
MNEM.
208
DO
2*
FLAGS:
PAGE A2-4
ADDRESSING MODE
OPER.
DEC HEX
BNE
NO.
CYCLES
if branch implemented
no effect.
Relative
to same page.
to different page.
BPL
OP-CODE
MNEM.
DEC HEX
16
BPL
test N flag,
NO.
NO.
BYTES
CYCLES
two
10
FLAGS:
BRK
cycles
no
ADDRESSING MODE
2*
Relative
to same page.
if branch implemented
to different page.
effect.
initiate
interrupt
sequence;
OP-CODE
MNEM.
DEC HEX
BRK
NO.
NO.
BYTES
CYCLES
ADDRESSING MODE
Implied
j^ranch
Save
stack;
Set I
OPER.
BVC
(N=0),
OPER.
if not set
relative.
on
overflow _Clear:
test
overflow
flag,
if
not
OP-CODE
MNEM.
DEC HEX
80
BVC
NO.
NO.
BYTES
CYCLES
50
2*
two
FLAGS:
cycles
ADDRESSING MODE
OPER.
if branch
implemented
Relative
to same page.
to different page.
no effect.
PAGE A2-5
BVS
Branch
on
oVerflow
Set:
OP-CODE
MNEM.
DEC HEX
112
BVS
test
overflow
flag,
if
NO.
ADDRESSING MODE
CYCLES
OPER.
70
2*
Relative
two cycles
FLAGS:
if branch implemented
CLC
NO.
OP-CODE
NO.
CYCLES
BYTES
MNEM.
DEC HEX
Implied
18
CLD
OP-CODE
NO.
DEC HEX
NO.
ADDRESSING MOE
Implied
PAGE A2-6
fO*
OPER.
216 D8
CLD
load
CYCLES
BYTES
MNEM.
ADDRESSING MODE
OPER.
24
CLC
to different page.
no effect.
set
CLI
CYCLES
BYTES
DEC HEX
CLI
88
58
load
f0'
into interrupt
NO.
NO.
OP-CODE
MNEM.
flag:
ADDRESSING MODE
OPER.
Implied
CLV
NO.
NO.
OP-CODE
CYCLES
BYTES
MNEM.
DEC HEX
184
CLV
ADDRESSING MODE
OPER.
Implied
B8
PAGE A2-7
CMP
CoMPare
specified
data
with
accumulator:
NO.
' NO.
CYCLES
ADDRESSING MODE
OPER.
DEC HEX
CMP
205
CD
Absolute
CMPIM
201
C9
Immediate
CMPIX
193
209
C1
Indirect with X
D1
Indirect with Y
CMPX
221
DD
CMPY
217
197
213
D9
C5
D5
5*
4*
4*
Zero-page
CMPIY
CMPZ
CMPZX
data
BYTES
OP-CODE
MNEM.
subtract
set Z if equal,
Indexed with X
Indexed with Y
Zero-page
indexed with X
CPX
CJomPare
from X
specified
register,
otherwise
DEC HEX
data
do
reset;
not
with
X.
store
register:
result;
NO.
NO.
CYCLES
ADDRESSING MODE
OPER.
CPX
236
EC
CPXIM
224
EO
Immediate
CPXZ
228
E4
Zero-page
Absolute
PAGE A2-8
subtract
set
if
data
equal,
BYTES
OP-CODE
MNEM.
CPY
ComPare
from
specified
Y-register,
otherwise
reset;
DEC
HEX
not
NO.
NO.
CYCLES
204
CC
CPYIM
192
CO
CPYX
196
C4
DEC
DECrement
Y-re^ister:
store
result;
subtract
set
if
data
equal,
ADDRESSING MODE
OPER.
CPY
with
BYTES
OP-CODE
MNEM.
data
do
Absolute
Immediate
Zero-page
specified
memory
contents
by
one,
store
condition negative
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
DEC HEX
ADDRESSING MODE
OPER.
DEC
206
CE
Absolute
DECX
222
DE
DECZ
198
Zero-page
DECZX
214
C6
D6
7
5
6
Indexed with X
Zero-page,
indexed with X
PAGE A2-9
DEX
DEcrement
contents
X-register;
to
NO.
NO.
BYTES
CYCLES
OP-CODE
DEC HEX
202
in
ADDRESSING MODE
Z
9
DEcrement
contents
of Y-re6istert
store
result
in
Y-register; condition negative and zero flags according
to
result.
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
DEC HEX
135
DEY
result
flags according
Implied
CA
store
OPER.
DEY
X-register:
result.
MNEM.
DEX
of
V
-
PAGE A2-10
ADDRESSING MODE
OPER.
Implied
88
EOR
Perform Exclusive OR
contents:
store
negative and
OP-CODE
MNEM.
DEC HEX
77
73
EOR
EORIM
between
result
in
accumulator and
specified
accumulator.
Condition
NO.
NO.
BYTES
CYCLES
result.
ADDRESSING MODE
OPER.
4D
Absolute
49
41
51
Immediate
Indirect with X
5*
4*
4*
Indirect with Y
Indexed with X
3
4
Zero-page
EORIX
65
EORIY
81
EORX
5D
EORY
93
89
59
EORZ
69
EORZX
85
45
55
Indexed with Y
Zero-page indexed with X
A
?
INC
INCrement
specified
contents
OP-CODE
MNEM.
NO.
one:
store
negative
and
result
zero
in
page
NO.
BYTES
DEC HEX
by
CYCLES
ADDRESSING MODE
OPER.
INC
238
EE
Absolute
INCX
254
FE
INCZ
230
Zero-page
INCZX
246
E6
F6
7
5
6
Indexed with X
Zero-page indexed with X
PAGE A2-11
INX
_INcrement
X- register
X-register;
to
OP-CODE
DEC HEX
N
?
NO.
E8
CYCLES
Implied
Y- register
NO.
one:
store
result
in
NO.
BYTES
DEC HEX
200
by
result.
OP-CODE
CYCLES
ADDRESSING MODE
OPER.
C8
Implied
c
-
OP-CODE
MNEM.
DEC HEX
JMP
JMPIA
FLAGS:
ADDRESSING MODE
Y-register;
MNEM.
JMP
in
JENcrement
to
INY
result
OPER.
INY
store
NO.
BYTES
232
one:
result.
MNEM.
INX
by
NO.
CYCLES
ADDRESSING MODE
OPER.
76
4C
108
6C
no effect.
PAGE A2-12
NO.
BYTES
3
5
Absolute
Indirect
JSR
ump
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
DEC HEX
32
JSR
FLAGS:
ADDRESSING MODE
OPER.
20
Absolute
no effect.
LDA
.Lc-aD
Accumulator
negative and
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
DEC HEX
with
specified
contents:
ADDRESSING MODE
OPER.
Absolute
AD
LDAIM
169
A9
Immediate
LDAIX
161
A1
Indirect with X
LDAIY
177
189
B1
BD
LDAY
185
LDAZ
165
B9
A5
B5
5*
4*
4*
Indirect with Y
LDAX
3
4
Zero-page
173
LDA
181
LDAZX
condition
data.
Indexed with X
Indexed with Y
Zero-page indexed with X
Z
?
c
-
PAGE A2-13
LDX
IioaJD
X-register
with
specified
contents:
condition
OP-CODE
MNEM.
NO.
NO.
CYCLES
BYTES
DEC HEX
ADDRESSING MODE
OPER.
174
162
AE
LDXIM
A2
Immediate
LDXY
190
BE
LDXZ
166
Zero-page
182
A6
B6
Indexed with Y
LDXZY
4*
3
LDX
Absolute
N
?
LDY
^oad
Y-register
c
-
with
specified
contents:
condition
OP-CODE
NO.
DEC HEX
BYTES
OPER.
MNEM.
172
LDY
LDYIM
AC
AO
160
LDYX
188
BC
LDYZ
164
LDYZX
180
A4
B4
N
?
PAGE A2-14
NO.
CYCLES
Z
9
ADDRESSING MODE
Absolute
Immediate
4*
Indexed with X
3
4
Zero-page
c
-
LSR
bit
0 into
carry bit
and
'0*
into bit 7;
load
condition
NO.
BYTES
CYCLES
OP-CODE
MNEM. .
DEC HEX
ADDRESSING MODE
OPER.
LSR
78
4E
Absolute
LSRA
4A
Accumulator
LSRX
74
94
5E
LSRZ
70
46
Zero-page
LSRZX
86
56
3
6
6
Indexed with X
NOP
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
DEC HEX
234
NOP
FLAGS:
ADDRESSING MODE
OPER.
EA
Implied
no effect.
ORA
Perform
logical
OR
between
Accumulator
and
contents:
store
result
in
accumulator;
negative and zero flags according to result.
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
DEC HEX
ADDRESSING MODE
OPER.
13
9
OD
Absolute
ORAIM
09
Immediate
ORAIX
01
Indirect with X
ORAIY
17
11
Indirect with Y
ORAX
1D
ORAZX
21
19
0515
ORAZ
29
25
5
5*
4*
4*
ORA
ORAY
specified
condition
Zero-page
3
4
1
1
Indexed with X
Indexed with Y
Zero-page indexed with X
TRUTH TABLE
N
Z
9
c
-
PAGE A2-15
PHA
usH
Accumulator
onto
stack:
update
stack
pointer;
remains unaltered.
OP-CODE
MNEM.
DEC HEX
PHA
72
FLAGS:
no
PHP
NO.
BYTES
NO.
CYCLES
48
Implied
effect.
P.USH .Processor
pointer;
status
NO.
HEX
08
PHP
onto
stack:
update
CYCLES
ADDRESSING MODE
OPER.
0
Implied
no effect.
PLA
PuLl .Accumulator
from stack:
update pointer;
OP-CODE
MNEM.
104
PLA
NO.
NO.
BYTES
DEC HEX
stack
NO.
BYTES
DEC
word
OP-CODE
MNEM.
FLAGS:
ADDRESSING MODE
OPER.
ADDRESSING MODE
OPER.
68
PAGE A2-16
CYCLES
Z
9
Implied
c
-
condition
PLP
j^Ii1
.Processor
pointer;
status
condition
word
ALL
from
stack:
flags
update
according
stack
to
PSW
retrieved.
OP-CODE
NO.
MNEM.
NO.
BYTES
PLP
DEC
HEX
40
28
CYCLES
Implied
ROL
ROtate
bit
into
JLeft
into
one
bit
carry
place,
of
flag;
according to
OP-CODE
MNEM.
condition
NO.
HEX
ROL
46
2E
ROLA
42
2A
ROLX
62
3E
ROLZ
38
ROLZX
54
26
36
7
5
6
data
contents:
and
negative
bit
and
load
7
of
zero
carrythese
flags
NO.
CYCLES
DEC
specified
specified
result.
BYTES
ROL is a 9-bit
ADDRESSING MODE
OPER.
ADDRESSING MODE
OPER.
Absolute
Accumulator
Indexed with X
Zero-page
Zero-page
indexed with X
rotation.
bit
PAGE A2-17
ROR
ROtate
load
of
Right
carry bit
these
into
into
carry
flags according to
OP-CODE
MNEM.
ROR
110
RORA
RORX
106
126
RORZ
102
RORZX
118
is a 9-bit
N
9
flag;
place,
of
specified
specified
condition
contents:
negative
and
zero
result.
CYCLES
ADDRESSING MODE
OPER.
6E
6A
Absolute
Accumulator
7E
66
76
7
5
6
Zero-page
Indexed with X
rotation.
RTI
NO.
BYTES
DEC HEX
ROR
NO.
one
bit
bit
0
from stack,
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
RTI
DEC
HEX
64
40
Implied
RTS
IteTurn
from
Subroutine:
increment by one,
OP-CODE
DEC HEX
96
RTS
FLAGS:
NO.
BYTES
MNEM.
60
no effect.
PAGE A2-18
ADDRESSING MODE
OPER.
retrieve
from
NO.
CYCLES
ADDRESSING MODE
OPER.
PC
update stack.pointer.
Implied
stack
and
SBC
StaBtract
with
accumulator:
negative,
Carry
store
specified
answer
overflow,
zero
in
and
contents
accumulator;
carry
flags
from
condition
according
to
result.
OP-CODE
NO.
MNEM.
NO.
BYTES
DEC HEX
SBC
237
ED
SBCIM
233
E9
SBCIX
SBCIY
225
241
SBCX
CYCLES
ADDRESSING MODE
OPER.
2
1
Absolute
Immediate
E1
F1
Indirect with X
Indirect with Y
253
FD
SBCY
249
F9
5*
4*
4*
SBCZ
229
245
E5
F5
Zero-page
Zero-page
SBCZX
Indexed with X
Indexed with Y
indexed with X
To
subtract
SBC
operates
without
in
carry,
decimal
set
or
carry
binary
flag
mode
(SEC)
before
according
to
SBC.
D-flag
setting.
SEC
OP-CODE
MNEM.
DEC HEX
56
SEC
C=1
NO.
NO.
BYTES
CYCLES
ADDRESSING MODE
OPER.
38
Implied
PAGE A2-19
SED
OP-CODE
MNEM.
DEC HEX
SED
248
D1
NO.
NO.
BYTES
CYCLES
F8
Implied
SEI
OP-CODE
MNEM.
DEC HEX
SEI
120
NO.
NO.
BYTES
CYCLES
76
ADDRESSING MODE
Implied
STore
1=1
OPER.
STA
ADDRESSING MODE
OPER.
Accumulator
contents
at
specified
address:
remains unaltered.
OP-CODE
MNEM.
NO.
BYTES
DEC HEX
NO.
CYCLES
ADDRESSING MODE
OPER.
STA
STAIX
141
129
8D
2
1
4
6
Absolute
81
STAIY
145
157
155
133
149
91
6-
Indirect with Y
9D
99
85
2
1
Indexed with X
Indexed with Y
95
5
5
3
4
STAX
STAY
STAZ
STAZX
FLAGS:
no effect.
PAGE A2-20
Indirect with X
Zero-page
Zero-page
indexed with X
STX
STore
contents
of
X-register
at
specified
address:
remains unaltered.
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
DEC HEX
ADDRESSING MODE
OPER.
STX
142
8E
Absolute
STXZ
134
86
Zero-page
STXZY
150
96
3
4
FLAGS:
no effect.
STY
STore
contents
of
Y-register
at
specified
address:
remains unaltered.
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
DEC HEX
ADDRESSING MODE
OPER.
STY
140
8C
Absolute
STYZ
132
148
3
4
Zero-page
STYZX
84
94
FLAGS:
no
TAX
effect.
Transfer
remains
contents
unaltered;
according to
DEC HEX
170
N
?
of
Accumulator
condition
to
negative
X-register:
and
zero
flags
data.
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
TAX
ADDRESSING MODE
OPER.
Implied
AA
PAGE A2-21
TAY
Transfer
remains
contents
of
unaltered;
Accumulator
condition
to
negative
Y-register:
and
zero
flags
according to data.
NO.
NO.
BYTES
CYCLES
OP-CODE
MNEM.
DEC HEX
168
TAY
N
?
Transfer
remains
contents
TSX
N
?
HEX
186
BA
X-register:
and
Implied
Z
?
contents
of
unaltered;
X-register
into
condition negative
NO.
NO.
BYTES
CYCLES
OP-CODE
DEC HEX
138
TXA
Acumulator:
and
ADDRESSING MODE
OPER.
Implied
8A
PAGE A2-22
SP
flags
ADDRESSING MODE
according to data.
MNEM.
zero
NO.
CYCLES
remains
to
negative
OPER.
Transfer
Stack-pointer
condition
NO.
TXA
of
data.
BYTES
DEC
unaltered;
OP-CODE
MNEM.
according to
Implied
A8
TSX
ADDRESSING MODE
OPER.
*>
c
-
zero
flags
TXS
Transfer
contents
of
X-register
into
Stack:
remains
unaltered.
OP-CODE
MNEM.
NO.
NO.
CYCLES
BYTES
DEC HEX
154
TXS
FLAGS:
ADDRESSING MODE
OPER.
Implied
contents
of
9A
no effect.
TYA
Transfer
"remains
unaltered;
JT-register
condition
into
negative
Accumulator:
and
zero
flags
according to data.
TYA
CYCLES
BYTES
DEC HEX
NO.
NO.
OP-CODE
MNEM.
152
ADDRESSING MODE
OPER.
Implied
98
PAGE A2-23
Character Set
TABLE 2
poke
set1
set2
poke
set1
set2
poke
65
33
34
35
67
36
68
37
69
70
66
38
&
39
40
41
71
72
73
42
74
75
10
43
11
44
12
45
13
46
47
79
48
80
49
81
50
82
51
83
52
84
53
85
14
15
16
17
18
19
20
21
22
23
24
25
26
27
76
77
54
86
55
87
56
88
89
57
29
30
31
poke
set1
97
98
C
H
a
DD
B
B
D
a
0
B
CD
90
58
59
set2
78
91
ffl
D
CD
<
92
61
93
62
>
94
63
64
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
im-i
60
28
32
set1
124
125
126
95
127
96 <space)
>
<space>
PAGE A2-24
set2
D
m
[B
a
B
B
a
Q
a
H
D
c
01
H
U
13
a
TABLE3
50
67
68
83
42
43
59
75
91
107
123
139
155
44
60
76
84
54
39
55
71
85
86
87
103
81
82
97
98
112
128
144
240
10
26
70
96
9
25
69
80
208
52
66
224
35
51
65
24
38
64
7
23
37
53
33
49
34
48
192
6
22
36
32
3
4
5
21
176
20
2
18
160
3
19
17
16
119
135
151
167
183
199
215
231
247
41
57
73
40
56
72
58
74
88
89 90
104 105 106
120
121
122
136
11
12
28
13
29
14
27
30
45
46
15
31
47
92
108
124
140
137 138
152 153 154
156
168 169 170 171 172
184 185 186 187 188
200 201 202 203 204
216 217 218 219 220
232 233 234 235 236
61
62
77
78
93
109
125
141
157
173
189
94
110
111
126
127
142
143
159
175
191
207
223
239
255
158
174
190
205 206
221
222
237 238
252 253 254
63
79
95
TABLE4
MSB
LSB
BIN
000
001
011
100
010
101
110
111
0000
NUL
DLE
SPACE
0001
SOH
DC1
0010
STX
DC2
3
4
0011
ETX
EOT
3
4
0100
DC3
DC4
5
6
0101
ENQ
NAK
0110
ACK
SYN
0111
BEL
ETB
1000
BS
CAN
1001
HT
EM
1010
LF
SUB
1011
VT
ESC
1100
FF
FS
1101
CR
GS
1110
1111
SO
RS
SI
US
#
$
&
7
8
<
>
n
-
DEL
PAGE A2-25
TABLES
6510 Flag Guide
This guide lists all 6510
flags with the
families of instructions
that set them and the branch instructions that test their condition.
N - NEGATIVE FLAG
Instruction to Condition
Instruction to Test
ADC
DEX
LDY
SBC
BMI
AND
DEY
LSR
TAX
BPL
ASL
EOR
ORA
TAY
BIT
INC
PLA
TXA
CMP
CPY
INX
PLP
TYA
INY
ROL
CPX
LDA
ROR
DEC
LDX
RTI
Instruction to Condition
Instruction to Test
ADC
CLV
RTI
BVC
BIT
PLP
SBC
BVS
Instruction to Condition
BRK
PLP
Instruction to Test
RTI
D - DECIMAL FLAG
Instruction to Condition
CLD
PLP
RTI
Instruction to Test
SED
I - INTERRUPT FLAG
Instruction to Condition
BRK
CLI
SEI
PAGE A2-26.
PLP
RTI
Instruction to Test
Z - ZERO FLAG
Instruction to Condition
Instruction to Test
ADC
DEX
LDY
SBC
BEQ
AND
DEY
LSR
TAX
BNE
ASL
EOR
ORA
TAY
BIT
INC
PLA
TXA
CMP
CPY
INX
PLP
TYA
INY
ROL
CPX
LDA
ROR
DEC
LDX
RTI
C - (3ARRY FLAG
Instruction to Condition
ADC
CPX
ROL
ASL
CPY
ROR
CLC
LSR
RTI
CMP
PLP
SBI
SEC
Instruction to Test
BCC
BCS
PAGE A2-27
APPENDIX 3
Maps and Vectors
The
serious
programs
machine
which,
code
of
programmer
necessity,
must
of
First ,
zero
use
many
page
of
the
they use,
Second ,
the
of the most
locations.
zero
page,
Considerable
it
is
will
and
expertise
there
wish
routines
knowledge
may
be
which
used
we
of
the
obtained
by
in
are
be
many
convert
Unfortunately,
been
the
routines
developed,
Many
of
the
particularly
the
ordinary
writing
folk
occasions
on
the
use
made
in ROM has
and
some
programs
the
for conversion.
where
64
40
of
RAM
the
as
the
knowledge
of
these
column
The VIC
and
for
screen
20
the
dreaded
use
of
ROM.
routines
to
invent
traveller
which
disposition
is
the
have
required.
computer
range,
machines, are
potential
targets
many
map
should
of
machines
changes
characteristics
without
follow
were
range.
PET
shares
the
programmer
which
Commodore
different
maps
these
programs
in
changed
written
is ample scope
intrepid
that
the
the computer
Commodore
for,
The
making
going
the
memory
extensive
locations
in
of
are
Commodore 64 and,provided
there
the
require use of
make
included
various
writing
with
will
to
was
that
is
conflicts.
benefits
routines
unlikely
better 'wheel1,
Last,
however,
considerable
instructions
and Kernel
is necessary to avoid
comprehensive
and
BASIC
C-64
peacefully
in FOM by Commodore.
powerful 6510
The
the
co-exist
is
go
likely
some
to
way
of
the
can be allowed
get
to
lost.
The
avoiding
the
In addition to providing
however,
they
also provide
references are
identified
possible,
have
Mike
(ICPUG).
Todd
of
the
Where all
as VIC,
B2 or B4
that no meaningful
used
the
respectively.
An entry
counterpart exists.
labels
which
are
used
by
Independent
else has
Commodore
failed,
Products
however,
Users
Group
I have invented my
own labels.
Certain
conventions
have
been
used
in
the
descriptions.
A,
X,
contents of
that reference.
Thus,
(TXTPTR) is the two byte
address in the usual low/high byte form, (A/Y) is the value or
address formed from the contents of the accumulator (low byte), and
the Y-register (high byte).
PAGE
3-1
Memory Maps
Label
VIC
B2/B4
Demi
Hex
Hex
64Hex
Description
D6510
0000
R6510
0001
0002
ADRAY1
0003-0004
0003
ADRAY2
0005-0006
0005
--
CHARAC
0007
0007
0003
ENDCHR
0008
0008
0004
INTEGR
0007-0008
0007
0003
TRMPOS
0009
0009
VERCK
000A
10
000A
009D
Flag: 0 = load,
COUNT
000B
11
000B
0005
DIMFLG
000C
12
000C
0006
VALTYP
OOOD
13
OOOD
0007
INTFLG
OOOE
14
OOOE
0008
GARBFL
OOOF
15
OOOF
0009
SUBFLG
0010
16
0010
000A
INPFLG
0011
17
0011
000B
TANSGN
0012
18
0012
000C
CHANNL
0013
19
0013
OOOE
LINNUM
0014-0015
20
0014
0011
Temporary:
TEMPPT
0016
22
0016
0013
integer.
collection
call.
READ.
Integer value.
LASTPT
0017-0018
23
0017
0014
TEMPST
0019-0021
25
0019
0016
INDEX
0022-0025
34
0022
001F
INDEX1
0022-0023
34
0022
001F
INDEX2
0024-0025
36
0024
0021
RESHO
0026-002A
38
0026
0023
TXTTAB
002B-002C
43
002B
0028
VARTAB
002D-002E
45
002D
002A
ARYTAB
002F-0030
47
002D
002C
STREND
0031-0032
49
0031
002E
FRETOP
0033-0034
51
0033
0030
FRESPC
0035-0036
53
0035
0032
tCMSIZ
0037-0038
55
0037
0034
CURLIN
0039-003A
57
0039
0036
OLDLIN
003B-003C
59
003B
0038
OLDTXT
003D-003E
61
003D
003A
DATLIN
003F-0040
63
003F
003C
DATPTR
0041-0042
65
0041
003E
INPPTR
0043-0044
67
0043
0040
divide.
address.
Label
-64
Hex
Demi
VIC
B2/B4
Hex
Hex
Description
VARNAM
0045-0046
69
0045
0042
VARPNT
0047-0048
71
0047
0044
FORPNT
0049-004A
73
0049
0046
Pointer:
VARTXT
004B-004C
75
004B
0048
OPMASK
004D
77
004D
004A
TEMPF3
004E-0052
78
004E
004B
F0UR6
0053
83
0053
0050
JMPER
0054-0056
84
0054
0051
TEMPF1
0057-005B
87
0057
0054
table.
descriptor if string.
collection.
005C-0060
92
005C
0059
FAC
0061-0066
97
0061
005E
FACEXP
0061
97
0061
005E
FAC exponent.
FACHO
0062-0065
98
0062
005F
FAC mantissa.
FACSGN
0066
102
0066
0063
FAC sign.
SGNFLG
0067
103
0067
0064
BITS
0068
104
0068
0065
AFAC
0069-006E
105
0069
0066
routine.
ARGEXP
0069
105
0069
0066
AFAC exponent.
ARGHO
006A-006D
106
006A
0067
AFAC mantissa.
ARGSGN
006E
110
006E
006A
AFAC sign.
ARISGN
006F
111
006F
006C
FACOV
0070
112
0070
006D
FBUFPT
0071-0072
113
0071
006E
CHRGET
0073-008A
115
0073
0070
conversion.
CHRGOT
0079
121
0079
0076
TXTPTR
007A-007B
122
007A
0079
RNDX
008B-008F
139
008B
0088
STATUS
0090
144
0090
0096
STKEY
0091
145
0091
009B
SVXT
0092
146
0092
009C
VERCKK
0093
147
0093
009D
C3P0
0094
148
0094
00A0
BSOUR
0095
149
0095
00A5
SYNO
0096
150
0096
OOAB
TEMPX
0097
151
0097
OOAD
TEMPY
0097
151
0097
OOAD
LDTND
0098
152
0098
OOAE
DFLTN
0099
DFLTO
009A
153
154
0099
009A
OOAF
00B0
fetch.
Label
64
Hex
Demi
VIC
B2/B4
Hex
Hex
Description
PRTY
009B
155
009B
00B1
DPSW
009C
156
009C
00B2
MSGFLG
009D
157
009D
FNMIDX
009E
158
009E
00B4
PTR1
009E
158
009E
OOCO
PTR2
009F
159
009F
00C1
TirC
00A0-00A2
160
00A0
008D
TSFCNT
00A3
163
00A3
00B7
TBTCNT
00A4
164
00A4
00B9
CNTDN
00A5
165
00A5
OOBA
BUFPNT
00A6
166
00A6
OOBB
tape write.
EOI
flag.
shift counter.
OOBC
INBIT
00A7
167
00A7
BITC1
00A8
168
00A8
OOBE
RINONE
00A9
169
Q0A9
OOBF
"
"
"
RIDATA
OOAA
170
OOAA
00C2
temporary (all).
RIPRTY
OOAB
171
OOAB
00C3
SAL
OOAC-OOAD
172
OOAC
00C7
EAL
OOAE-OOAF
174
OOAE
00C9
CM>0
OOBO-OOB1
176
OOBO
OOCB
TAPE!
OOB2-OOB3
178
00B2
00D6
Pointer:
BITTS
00B4
180
00B4
OOCE
NXTBIT
00B5
181
00B5
OOCF
RODATA
00B6
182
00B6
00D0
(all).
FNLEN
0087
183
00B7
0001
LA
00B8
184
00B8
00D2
SA
00B9
185
00B9
0003
FA
OOBA
186
OOBA
0004
FNADR
OQBB-OOBC
187
OOBB
0005
ROPRTY
OOBO
189
OOBO
OODD
FSBLK
OOBE
190
OOBE
OODE
OOBF
191
OOBF
OODF
CAS1
OOCO
192
OOCO
OOF9
STAL
00C1-00C2
193
00C1
OOFB
MEMUSS
00C3-Q0C4
195
00C3
64
Label
Hex
Demi
VIC
B2/B
Hex
Hex
Description
LSTX
00C5
197
00C5
0097
NDX
00C6
198
00C6
009E
RVS
00C7
199
00C7
009F
Flag: Reverse
I NDX
0X8
200
00C8
00A1
LXSP
00C9-00CA
201
00C9
00A3
SFDX
OOCB
203
KEYVAL
__
OOCB
00A6
BLNSW
oocc
204
OOCC
00A7
BLNCT
OOCD
205
OOCD
00A8
disabled.
GDBLN
OOCE
206
OOCE
00A9
BLNON
OOCF
207
OOCF
00 AA
CRSW
OODO
208
OODO
OOAC
PNT
00D1-00D2
209
00D1
00C4
PNTR
00D3
211
00D3
00C6
QTSW
00D4
212
00D4
OOCD
LNMX
00D5
213
00D5
00D5
TBLX
00D6
214
00D6
00D8
SCHAR
00D7
215
00D7
00D9
INSRT
00D8
216
00D8
OODC
$00.
character output.
LOTB1
00D9-00F2
217
00D9
OOEO
USER
00F3-00F4
243
OOF 3
KEYTAB
O0F5-OOF6
245
OOF 5
RIBUF
00F7-00F8
247
OOF 7
ROBUF
OOF 9-OOFA
249
00F9
FREK2P
OOFB-OOFE
251
OOFB
BASZPT
OOFF
255
OOFF
ASCWRK
Q0FF-10A
255
OOFF
OOFF
BAD
0100-013E
256
0100
0100
conversion.
STACK
0100-01FF
256
0100
0100
BSTACK
013F-01FF
319
013F
013F
BUF
0200-0258
512
0200
"
0200
"
0200-0250 - PET.
LAT
0239-0262
601
0259
0251
FAT
0263-026C
611
0263
025B
SAT
026D-0276
621
026D
0265
(device numbers).
addresses.
Label
64
Hex
Demi
VIC
B2/B4
Hex
Hex
Description
KEYD
0277-0280
631
0277
026F
MEMSTR
0281-0282
641
0281
MEMSIZ
0283-0284
643
0283
TIMOUT
0285
645
0285
system.
COLOR
0286
646
0286
GDCOL
0287
647
0287
HIBASE
0288
648
0288
XMAX
0289
649
0289
RPTFLG
028A
650
028A
KOUNT
028B
651
028B
DELAY
028C
652
028C
SHFLAG
028D
653
028D
0098
028E
654
028E
KEYLOG
028F-0290
655
028F
0291
657
0291
AUTODN
0292
658
0292
enabled.
M51CTR
0293
659
0293
M51CDR
0294
660
0294
M51AJB
0295-0296
661
0295
RSSTAT
0297
663
0297
BITNUM
0298
664
0298
BAUDOF
0299-029A
665
0299
RIDBE
029B
667
029B
RIDBS
029C
668
029C
RODBS
029D
669
029D
RODBE
029E
670
029E
IRQTMP
029F-02AO
671
029F
ENABL
02A1
673
02A1
RS232 enables.
operations.
TODSNS
02A2
674
TRDTMP
02A3
675
TD1IRQ
02A4
676
TLNIDX
02A5
677
TVSFLG
02A6
678
Label
64-
Hex
IERROR
Demi
VIC
B2/B4
Hex
Hex
Description
02A7-02FF
679
02A1
Unused.
0300-0301
768
0300
Vector:
Normal
0302-0303
770
0302
--
ICRNCH
0304-0305
772
0304
IQPLOP
0306-0307
774
0306
IGONE
0308-0309
776
0308
--
and decode.
routine.
dispatch routine.
(64).
IEVAL
030A-030B
778
030A
SAREG
030C
780
030C
SXREG
030D
781
030D
SYREG
030E
782
030E
SPREG
030F
783
030F
USRPOK
0310
784
0000
0000
USRADD
0311-0312
785
0001
0001
0313
787
0314-0315
788
evaluation.
CINV
0314
0090
0316-0317
790
0316
0092
NMINV
0318-0319
792
0318
0094
031A-031B
794
031A
ICLOSE
031C-031D
796
031C
ICHKIN
031E-031F
798
03IE
ICKOUT
0320-0321
800
0320
--
ICLRCH
0322-0323
802
0322
--
IBASIN
0324-0325
804
0324
IBSOUT
0326-0327
806
0326
ISTOP
0328-0329
808
0328
--
IGETIN
032A-032B
810
032A
routine.
routine.
routine.
routine.
routine.
routine.
routine.
routine.
routine.
Label
64
Hex
ICLALL
032C-032D
Demi
812
VIC
B2/B4
Hex
Hex
032C
Description
Vector:
routine.
USRCMD
032E-032F
814
032E
03FA
0330-0331
816
0330
Vector:
routine.
ISAVE
818
0332
0334-033B
820
0334
Unused.
routine.
TBUFFR
0332-0333
033C-03FB
828
033C
027A
03FC-03FF
1020
03FC
VICSCN
0400-07F7
1024
1E00
8000
SPNTRS
07F8-07FF
2040
0800-9FFF
2048
0400
0400
8000-9FFF
32768
A000
AOOO-BFFF
40960
C000
B000
COOO-CFFF
49152
DOOO-DFFF
53248
9000
E800
D000-D02E
53248
9000
E880
54272
9000
VIC-II (64),
D500-D7FF
54528
D800-DBFF
55296
9400
DCOO-DCOF
56320
9110
E810
DDOQ-DDOF
56576
9120
E820
E840
57344
E000
B000
E000-E4FF
57344
E000
E500-FFFF
58624
E500
E000
BASIC ROM
Label
64
VIC
B2
B4
Description
BCOLD
AOOO
COOO
BWARM
A002
C002
A004
C004
STMDSP
AOOC
COOC
COOO
BOOO
Authorship note:
"CBMBASIC"
FUNDSP
A052
C052
C046
B066
OPTAB
A 080
C080
C074
B094
RESLST
A09E
C09E
C092
B0B2
A129
C129
CUD
B13D
OPLIST
A140
C140
C134
B161
FUNLST
A14D
C14D
C141
B16E
ERRTAB
A19E
C19E
C192
B20D
Error messages.
ERRPTR
A328
C328
OKK
A364
C364
"ERROR",
"IN",
"READY",
"BREAK".
FNDFOR
A38A
C38A
C2AA
B322
BLTU
A3B8
C3B8
C2D8
B350
BLTUC
A3BF
C3BF
C2DF
B357
GETSTK
A3FB
C3FB
C31B
B393
REASON
A408
C408
C328
B3A0
OMERR
A435
C435
C355
B3CD
ERROR
B3CF
...
ending at (HIGHDS)-l.
error "OUT OF *MORY" if not.
space - if not
...
...
ERRFIN
A469
C469
C37A
B3F0
C474
READY
A474
C389
B3FF
MAIN
B406
...
MAINI
A49C
C49C
C3AB
B41F
INSLIN
A4A2
C4A2
C3B1
B470
command
A52A
C52A
C439
B4AD
LNKPRG
A533
C533
C442
B4B6
INLIN
A560
C560
C46F
B4E2
C481
RDCHR
Label
CRUNCH
64
VIC
B2
B4
B4FB
Description
A613
C613
C52C
B5A3
FNDLNC
A617
C617
C530
B5A7
SCRATH
A642
C642
C55B
B3D2
SCRTCH
A644
C644
C55D
B5D4
...
RUNC
A659
C659
C572
B5E9
CLEAR
A63E
C65E
C577
B5EE
CLEARC
A660
C660
C579
B5FC
LDCLR
A677
C677
C590
B6OB
STXPT
A68E
C68E
C5A7
B622
LIST
A69C
C69C
C5B5
B630
QPLOP
B6B5
then CLEARC.
start of program.
FOR
A742
C472
C658
B6DE
NEWSTT
A7AE
C7AE
C6C4
B74A
CKEOL
A7C4
C7C4
C6DA
B7F5
GONE
B77C
GONE 3
A7ED
C7ED
C700
B785
RESTOR
A81D
C81D
C730
B7B7
STOP
A82C
C82C
C73F
B7C6
END
A82F
C82F
C741
B7C8
...
from text.
next line parameters.
start of BASIC.
A834
C834
C744
B7CB
STPEND
A841
C841
C751
B7D8
CONT
A857
C857
C76B
B7EE
RUN
A871
C871
C785
B808
GOSUB
A883
C883
C790
B813
GOTO
A8AO
C8A0
C7AD
B830
GOTOC
A8A3
C8A3
C7BO
B833
Label
64
VIC
B2
B4
Description
RETURN
A8D2
C8D2
C7OA
B85D
RTC
A8D4
C8D4
C7DC
B85F
...
RTNOGS
A8E0
C8E0
C7E8
B86B
A8E3
C8E3
C7EA
B86E
DATA
A9F8
C8F8
C800
B883
DATAN
A906
C906
C80E
B891
REMN
A909
C909
C80E
B891
SERCHX
A90B
C90B
C811
B894
Search text for (X) or zero byte - exit with (Y) set
IF
A92B
C92B
C830
B8B3
REM
A93B
C93B
C843
B8C6
DOCOND
A940
C940
C848
B8CB
ONGOTO
A94B
C94B
C853
B8D6
LINGET
A96B
C96B
C873
B8F6
LET
A9A5
C9A5
C8AD
B930
variable space and set (FORPNT) to point at it evaluate expression then PUTINT, PTFLPT, PUTTIM or
GETSPT as appropriate.
PTFLPT
A9C4
C9C4
C8CC
B94F
PUTINT
A9D6
C9D6
C8DE
B961
PUTTIM
A9E3
C9E3
C8EB
B972
GETSPT
AA2C
CA2C
C937
B9BA
PRINTN
AA80
CA80
C98B
BA88
CHD
AA86
CA86
C991
BASE
STRDON
AA9A
CA9A
C9A5
BAA2
AAAO
CAAO
C9AB
BAA8
VAROP
AAB8
CAB8
C9C3
BACO
NUHDON
AABC
CABC
C9C7
BAC4
CROO
AAD7
CAD7
C9E2
BADF
(FORPNT).
Output CR/LF
output CR only.
COhPRT
AAE8
CAE8
C9EF
BAFO
Label
64
VIC
B2
B4
Description
STROUT
ABIE
CB1E
CA1C
BB1D
STRPRT
AB21
CB21
CA1F
BB20
OUTSTR
AB24
CB24
CA22
BB23
AB3B
CB3B
CA39
BB3A
PRTSPC
AB3F
CB3F
CA3D
BB3E
OUTSKP
AB42
CB42
CA40
BB41
OUTQST
AB45
CB45
CA43
BB44
OUTDO
AB47
CB47
CA45
BB46
Print (A).
TRMNOK
AB4D
CB4D
CA4F
BB4C
GET
AB7B
CB7B
CA7D
BB7A
INPUTN
ABA5
CBA5
CAA7
BBA4
identify GET,
do input
INPUT
ABBF
CBBF
CAC1
BBBE
BUFFUL
ABEA
CBAE
CAED
BBE8
QINLIN
ABF9
CBF9
CAFA
BBF5
READ
AC 06
CC06
CB07
BC02
INPCON
ACOD
CCOD
CBOE
BC09
INPCO1
AC OF
CCOF
CB10
BCOB
RDGET
AC 35
CC35
CB36
BC31
any, do input.
AC43
CC43
CB44
BC3F
DATLOP
ACB8
CCB8
CBB9
BCB4
ACFC
CCFC
CBFC
BCF7
RDGET.
NEXT
ADOC
CDOC
CCOD
BD07
AD IE
CD IE
CC20
BD19
AD 61
CD61
CC62
BD5B
FRMNUM
AD8A
CD8A
CC8B
BD84
enters
AD8D
CD8D
CC8E
BD87
CHKSTR
AD8F
CD8F
CC90
BD89
Label
FR*VL
64
AD9E
VIC
CO9E
B2
B4
CC9F
BD98
Description
result is
If
In addition,
if expression is a
Finally,
if an error is
AE83
CE83
CD84
BE 81
Identify
CEA8
COA3
BEAO
format -
3.1415965.
QDOT
AEAD
CEAD
CDAB
BE A 5
PARCHK
AEF1
CEF1
CDEC
BEE9
CHKCLS
AEF7
CEF7
CDF2
BEEF
CHKOPN
AEFA
CEFA
CDF 5
BEF2
CHKCOM
AEFD
CEFD
CDF8
BEF5
SYNCHR
AEFF
CEFF
CDFA
BEF7
expression.
right parenthesis.
left parenthesis.
comma.
If not -
"SYNTAX ERROR".
SYNERR
AF08
CF08
CEO3
BFOO
DOMIN
AFOD
CFOD
CE08
B505
RSVVAR
fiFlb
CF14
ISVAR
AF28
CF28
BASIC READY.
___
CEOF
BF04
Place numeric
AF48
CF48
CE2E
BFAD
ISFUN
AFA7
CFA7
CE89
C037
STRFUN
AFB1
CFB1
CE93
C051
NUtFUN
AFD1
CFD1
CEB3
C071
to string.
Label
64
VIC
B2
B4
Description
DROP
AFE6
CFE6
CEC8
C086
Perform OR.
ANOOP
AFE9
CFE9
CECB
C089
Perform AND.
DOREL
B016
D016
CEF8
C0B6
If numeric expression
B01B
DO1B
CEFD
C0B8
STRREL
B02E
DO2E
CF1O
COCE
DIM
B081
D081
CF63
C121
Perform DIM.
PTRGET
B08B
DO8B
CF6D
C12B
ORDVAR
B0E7
D0E7
CFC9
C187
ISLETC
B113
D113
CFF7
C1B6
NOTFNS
B11D
DUD
D001
C1CO
NOTEVL
B128
D128
DOOC
C1CB
FMAPTR
B194
D194
D078
C2C8
B1A5
D1A5
D089
C2D9
FACINX
B1AA
D1AA
INTIDX
B1B2
D1B2
D08D
C2DO
AYINT
B1BF
D1BF
D09A
C2EA
ISARY
B1D1
D1D1
DOAC
C2FC
FNDARY
B218
D218
D0F3
C343
BDSBSC
B245
D245
D020
C370
ontack.
ISARY.
ILLQNT
B248
D248
D023
C373
NOTFDD
B261
D261
D13C
C38C
INLPN2
B30E
D30E
D1EA
C439
UMULT
B34C
D34C D228
C477
FRE
B37D
D37D
D259
C4A8
GIVAYF
B391
D391
D26D
C4BC
POS
B39E
D39E
D27A
C4C9
SNGFT
B3A2
D3A2
D27C
C4CB
ERRDIR
B3A6
D3A6
D280
C4CF
starting at (VARPNT).
(CPOS) in FAC.
to 255.
(CURLIN) = $FF.
Label
64
VIC
B2
B4
Description
DEF
B3B3
D3B3
D28D
C40C
GETFNM
B3E1
D3E1
D2BB
C5OA
FNDOER
B3F4
D3F4
D2CE
C51D
SETFNV
B423
D423
D2FD
C54C
STRD
B465
D465
D33F
C58E
STRINI
B475
D475
D34F
C59E
(DSCPNT).
STRUT
B487
D487
D361
C5B0
String
PUTNW1
B4O5
D4O5
D3AF
GETSPA
B4F4
D4F4
D3CE
C61D
GARBA2
B526
D526
D400
C66A
DVARS
B5BD
D5BD
D497
GRBPAS
B606
D606
D40E
CAT
B63D
D63D
D517
C74F
MOVINS
B67A
D67A
D554
C78C
FRESTR
B6A3
D6A3
D57D
C7B5
FREFAC
B6A6
D6A6
D58O
pointer.
length is in (A).
space used by discarded strings.
to evaluate expression.
(STRNG1).
...
B6DB
D6D6
D5B5
C811
CHRD
B6EC
O6EC
D5C6
C822
LEFTD
B700
D700
D5DA
C836
RIGHTD
B72C
D72C
D600
C862
MIDD
B737
D737
D611
C86D
PREAM
B761
D761
D63B
C897
LEN
B77C
D77C
D636
C8B2
LEN1
B782
D782
D65C
C8B8
ASC
B78B
D78B
D665
C8C1
GTBYTC
B79B
D79B
D675
C8O1
format.
value in (X).
Return
Label
VAL
64
B7AD
VIC
D7AD
B2
B4
D687
C8E3
Description
STRVAL
B7B5
D7B5
D68F
C8F5
Confirm argument is
...
GETNUM
B7EB
D7EB
D6C6
C921
B7F7
D7F7
D6D2
C92D
PEEK
B80D
D80D
D6EB
C943
POKE
B824
0824
D707
C95A
WAIT
B82D
D82D
D710
C963
FADDH
B849
D849
D72C
C97F
FSUB
B850
D850
D733
C986
FSUBT
B853
D853
D736
C989
FADD5
B862
D862
D76E
C998
FADD
B867
D867
D773
C99D
FADDT
B86A
D86A
D776
C9A0
OVERR
B97E
D97E
D88A
CAB4
MULSHF
B983
D983
D88F
CAB9
Multiply by a byte.
B9BC
D9BC
D8C8
CAF2
B9C1
D9C1
D8CD
CAF7
LOG
B9EA
D9EA
D8F6
CB20
FMULT
BA28
DA28
D934
CB5E
FMULTT
BA3O
DA30
D934
CB5E
(FAC).
(FAC).
format.
functions.
Multiply
MLTPLY
BA59
DA 59
D965
CB8F
CONUPK
BA8C
DA8C
D998
CBC2
MULDIV
BAB7
DAB7
D9C3
CBED
MLDVEX
BAD4
DAD4
D9E0
CCOA
MUL10
BAE2
DAE2
D9EE
CC18
for underflow/overflow.
BAF9
DAF9
DA05
CC2F
Constant
DIV10
BAFE
DAFE
DAOA
CC34
FDIVF
BB07
DB07
DAD
CC3D
FDIV
BBOF
DBOF
DA1B
CC45
FDIVT
BB12
DB12
DA IE
CC48
10 in ffLPT format.
On entry (A) =
Label
64
MOVFM
BBA2
M0V2F
BBC7
MOV1F
MOVXF
VIC
B2
B4
Description
DBA2
OAAE
CCD8
DBC7
DAD3
CCFD
BBCA
OBCA
DAD6
COOO
BBDO
DBDO
DADC
CD06
MOVhF
BBD4
DBD4
DAEO
CO06
MOVFA
BBFC
DBFC
DB08
CD32
MOVAF
BCOC
DCOC
DB18
CO42
ROUND
BC1B
DC1B
DB27
CD 51
SIGN
BC2B
DC2B
DB37
CD 61
SGN
BC39
DC39
DB45
CD6F
ACTOFC
BC3C
DC3C
DB48
CD72
INTOFC
BC44
DC44
DB50
CD7A
ABS
BC58
DC 58
DB64
CD8E
FCOMP
BC5B
DC5B
DB67
CD91
BC9B
DC9B
DBA7
CDD1
INT
BCCC
DCCC
DBD8
CE02
FIN
BCF3
DCF3
DBFF
CE29
BDB3
DDB3
DCBF
CEE9
INPRT
BDC2
DDC2
DCCE
CF78
LINPRT
BDC9
DDC9
DCD5
CF7F
NUfPRT
BDCD
DDCD
DDC9
CF83
INTPRT
BDD1
DDD1
DDCD
CF87
FACOUT
BDD7
DDD7
DCE3
CF8D
FOUT
BDDD
DDDD
DCE9
CF93
to FLPT in FAC.
(CURLIN).
page location.
FYOUT
BDDF
DDDF
DCEB
CF95
FOUTIM
BE 68
DE68
DD74
DO1E
BF11
DF11
DE1D
D067
SQR
BF71
DF71
DE5E
D108
FPWRT
BF7B
DF7B
DE68
D112
STACK-1+(Y).
ending with null byte.
MFLPT constants used in ASCII conversion.
EXP
BFB4
DFB4
DEA1
D14B
BFBF
DFBF
DEAC
D156
BFED
DFED
DEDA
D184
(AFAC) to
Label
64
POLYX
E043
VIC
E040
B2
B4
DF2D
D1D7
Description
On entry (A/Y)
Routine
E08A
DF77
D221
DF7F
D229
RND
E097
E094
BIOERR
E0F9
E0F6
BCHOUT
E1OC
E109
BCHIN
E112
E1OF
BCKOUT
E118
ELL5
BCKIN
E11E
E11B
BGETIN
E124
E121
SYS
E12A
E127
FFDE
FFDE
SAVET
E156
E153
FFDB
FFDB
CHKOUT routine.
CHKIN routine.
routine.
SAVER
E15F
E15C
F6A7
F6E3
VERFYT
E165
E162
FFDB
FFDB
LOADT
E168
E165
FFD5
FFD5
LOADR
E175
E172
F322
F356
OPENT
E1BE
E1BB
FFCO
FFCO
OPENR
E1C1
E1BE
F524
F563
CLOSET
E1C7
E1C4
FFC3
FFC3
CLOSER
E1CA
E1C7
F2AC
F2E0
SLPARA
E1D4
E1D1
F43E
F47D
SAVE routine.
text before calling kernal routine.
COMBYT
E200
E1FD
F460
F49F
DEFLT
E206
E2O3
F5OE
F54D
CMMERR
E20E
E20B
F516
F555
Label
64
VIC
B2
B4
Description
OCPARA
E219
E216
F4CE
F5OD
COS
E264
E261
DFD8
D282
SIN
E26B
E268
DFDF
D289
TAN
E2B4
E2B1
E028
D2D2
PI2
E2E0
E2DD
E054
D2FE
TWOPI
E2E5
E2E2
E059
D3O3
FR4
E2EA
E2E7
EO5E
D308
E2EF
E2EC
E063
D30D
E30E
E3OB
E08C
D32C
E33E
E33B
EOBC
D35C
E37B
E467
ATN
BASSFT
E453
E45B
INIT
E394
E378
INITNV
E397
E37B
E116
D3B6
E3A2
E387
E0F9
D399
RNOSEO
E3BA
E39F
Elll
D3B1
INITCZ
E3BF
E3A4
E460
E429
E1B7
D44B
FREMES
E473
E436
E1C4
D438
BVTRS
E447
E44F
INITV
E453
E45B
set first
IOBASK
64
E500
VIC
B2
E500
B4
Description
Kernal routine.
SCRORG
E5O5
E5O5
E50A
E50A
E518
E518
E1DE
E60F
Initialise input/output.
E566
E581
E257
E05F
Home screen,
STUPT
E56C
E587
E25D
E06F
VPAN
E59A
E5B5
PANIC
E5A0
E5BB
INITV
E5A8
E5C3
KBGET
E5B4
E5CF
E285
EOA7
...
GETIN comes
here if DFLTN = 0.
KBINP
E5CA
E5E5
E29A
EOBC
KSINP comes
here if CRSW=O.
KSINP
E632
E64F
E2F4
E116
CHRIN
E63A
E657
E2FF
E121
CRSW = 3.
QTSWC
E684
E6B8
E33F
E16A
SCPUT
E691
E6C5
E34C
E177
SCNPNT
E716
E742
E3D8
E202
Used by SCNPNT.
CKDECL
E8A1
E8E8
CKINL
E8B3
E8FA
CKCOLR
E8CB
E912
Check colour.
COLTAB
E8DA
E921
SCROL
E8EA
E975
E53F
E3C8
CLRLN
E9FF
EA8D
DSPP
EA13
EAA1
KEY
EA31* EABF* E61B* E442* The main IRQ interrupt handling routine.
E396
[*CINV
EA87
EB1E
E68E
E4CD
This is the
EB79
EC46
E6F8
E6D1
Label
TALKK
VIC
B2/B4
Demi
Hex
Hex
F0B6
FQD2
64
Hex
E.D09
EE14
Description
EDOC
EE17
FOBA
F0D5
EDB9
EECO
accessed by SECOND.
TKSAK
EDC7
EECE
actioned by TKSA.
CIOUTK
EDDD
EEE4
F16F
F19E
UNTLKK
EDEF
EEF6
F17F
F1AE
The character
'hand shaking1
can be
UNTALK vector.
UNLSNK
EDFE
EF04
F183
F1B9
The
EE13
EF19
F18C
F1CO
A byte is
in A.
RSTRAB
EEBB
EFA3
RSTBGN
EF06
EFEE
RSRCVR
EF59
F036
'hand shaken'
KMSGTX
FOBD
F174
FOOO
FOOO
KMESSG
F12B
F1E2
F156
F185
NGETIN
F13E
F1F5
F1D1
F205
stored here.
return 0.
If no
This is the
F157
F20E
F1E1
F215
This is the
NBSOUT
F1CA
F27A
F232
F266
This is
F20E
F2C7
F7BC
F7FE
Kernal vector.
NCKOUT
F250
F309
F770
F71F
Kernal vector.
Label
NCLOSE
64
F291
VIC
F34A
B2
B4
F2AC
F2E0
Description
The details
SAT).
This is the
F32F
F3EF
F26E
F2A2
The number of
close
'output'
files tidily,
NCLRCH
F333
F3F3
F272
F2A6
This is
NOPEN
F34A
F40A
F524
F563
LOADSP
F49E
F542
F3CC
F40B
F5AF
F647
OUTFN
F5C1
F659
LOOING
F5D2
F66A
Subroutine to display
"LOADING" or "VERIFYING"
F5DD
F675
F6A4
F6E3
on device FA. If SA
F68F
F728
UDTIMK
F69B
F734
F729
F768
In addition it
Kernal routine.
RDTIMK
F6DO
F760
SETTMK
F6E4
F767
NSTOP
F6ED
F770
F343
Label
64
VIC
B2
B4
Description
KERROR
FflEB
F77E
F315
F349
FAH
F72C
F7AF
F5A6
F5E5
TCNTL
F80O
F88A
F806
F84B
They
READ
F92C
F98A
F855
F89A
RDI5
FA70
FABD
F931
F976
WRITE
FBA6
FBEA
FB93
FBD8
START
FCE2
FD22
FCD1
FD16
MEMCHK
FE91
AOINT
FD02
FD3F
RESTRK FD15
FD52
VECTRK FD1A
FD57
RAMTSK FD50
FD8D
FDF9
FE49
STLFSK FEOO
FE50
READSS FE07
FE57
FE66
STTMOX FE21
FEff
MEMTPK FE25
FE73
Label
64
VIC
B2
B4
Description
tCMBTK FE25
FE73
NMI
FEA9
FE43
BAUDTB FEC2
FF5C
T2NMI
FED6
PULS
FF48
FF72
E442
PCINT
FF5B
Patch to CINIT
RLSNUM FF80
KVCTRS FF81
FF8A
FFCO
FFCO
NMIVEC FFFA
FFFA
FFFA
FFFA
RSTVEC FFFC
FFFC
FFFC
FFFC
IRQVEC FFFE
FFFE
FFFE
FFFE
prudent
programmer
makes
as
much
use
as
possible
of
the
ROM
wheels
designed
to
then
there
for
this.
cater
approach.
What
happens
are
plenty
However,
to
your
of hobbies
there
clever
is
which
programs
seem
to be
you
computer with
change
three
times
The
to
jump
it
chosen as
part
All
the
these
the
the
VIC
20
the
so
place,
to
do
is
JSR
Using
the
the
to
the
Kernal
form
the
routines
to provide data
values
these
jump
table
ROM.
that
be
that
into
Kernal
the
one
machine
is
routine
address.
quite
to the
or more
the
the
in
the
address
requires,
providing
of use
you
this
with
on
any
Commodore
code
access
routines
to
have
39
been
to the programmer.
have
64
program
Commodore,
upgrades
Kernal
which
It
that
of
the
includes
was
20 Kernal,
expects.
of
If
Read a byte
straightforward.
routine,
the
the
written
will run
This
routine
Registers affected:
loading
returns
or more
any
of the
values,
then
registers.
A,
A.
X.
Preparatory routines:
TALK,
TKSA.
Associated routines:
TALK,
TKSA,
Error returns:
byte
if
$FFA5 (65445)
Communicating registers:
usually means
registers.
in one
Firstly,
Call address:
Reads
to
he/she
These
preserved
The
places
Commodore 64.
you need
in
it
simply
although
to different
on the
is
that
likely to be
any
to make
The Kernal
same
moved
system
will
problem and
same.
understanding
Kernal,
provides
routine
a Kernal
ones most
entries
this
transfer.
in
be
avoid
programmer has
to
operating
the
Kernal
may
remaining the
the
of
to
stays
jumps
Commodore 64 has
in
is
the
access
always
routines
is
in
which
gives
address
Kernal
of
machines.
which
The
the
table1
address
routine
new
of
to write
'jump
The
required.
purpose
easier
is
from
UNTLK.
See READST.
serial
device
(e.g.
disk)
with
full
handshaking.
The device must have previously been commanded to
TALK.
The byte is returned in A.
Beginners will find that using
SETLFS,
SETNAM,
OPEN and CHRIN provides an easier approach
co
reading data
PAGE
3-25
CHKIN
Call address:
$FFC6 (65478)
Communicating registers:
X.
Registers affected:
A, X.
Preparatory routines:
OPEN.
Associated
CLOSE,
routines:
CLALL.
Error returns:
A
logical
channel
file
that
SETNAM,
OPEN,
CHRIN,
CLRCHN,
3, 5 or 6 (see READST).
previously
in order
SETLFS,
opened
it may be
by
OPEN
read.
is
defined
as
an
input
number
of file.
CHKOUT
Call address:
$FFC9 (65481)
Communicating registers:
Registers affected:
A,
Preparatory routines:
Associated routines:
CLOSE CLALL.
Error returns:
A
logical
channel
file
that
OPEN.
SETLFS,
SETNAM, OPEN,
CHROUT,
CLRCHN,
0, 3, 5, 7 (see READST).
previously
in order
X.
X.
it
opened
may be
by
OPEN
written
is
to.
defined
Load
as
an
output
with
logical
CHKIN,
CLRCHN,
number of file.
CHRIN
Call address:
$FFCF (65487).
Communicating registers:
A.
Registers affected:
A, X.
Preparatory routines:
OPEN,
Associated
CLOSE,
routines:
CLALL.
Error returns:
Reads
byte
of
data
CHKIN.
SETLFS,
SETNAM,
OPEN,
(see READST).
from
the
channel
opened
for
input.
Byte
returned in A.
CHROUT
Call address:
$FFD2 (65490).
Communicating registers:
Registers affected:
Preparatory routines:
Associated routines:
CLOSE, CLALL.
Error returns:
A.
A.
OPEN,
SETLFS,
CHKOUT.
SETNAM,
A 3-26
CHKOUT,
0 (see READST).
PAGE
OPEN,
for output.
CLRCHN,
CINT
Call address:
$FF81
(65409).
Communicating registers:
none.
Registers affected:
A, X, Y.
Preparatory routines:
none.
Error returns:
This
none
to restore
status,
for instance,
CIOUT
Preparatory routines:
LISTEN, SECOND.
Associated routines:
LISTEN, SECOND, UNLSN.
Error returns:
(see READST).
CLALL
none.
Registers affected:
A, X.
Preparatory routines:
none.
Associated
routines:
CHKOUT, CHROUT,
Error returns:
SETLFS,
CLRCHN,
none.
SETNAM,
OPEN,
CHKIN,
CHRIN,
CLOSE.
Call address:
$FFC3 (65475).
Communicating registers:
Registers affected:
A,
Preparatory routines:
Associated
CHKOUT,
routines:
CHROUT,
Error returns:
CLRCHN,
A.
X,
Y.
none.
SETLFS,
SETNAM,
OPEN,
CHKIN,
CHRIN,
CLALL.
PAGE A 3^27
CLRCHN
Registers
affected:
Preparatory
Associated
CHKOUT,
A,
routines:
none.
routines:
CHROUT,
SETLFS,
CLOSE,
Error returns:
none
X.
the
current
input/output
channels (keyboard and screen).
Get a character from
Call address:
the keyboard
Communicating registers:
A,
Error returns:
a
the
byte
into
available,
then
IOBASE
keyboard
a
CHKIN,
CHRIN,
and
resets
default
or RS232 device.
A.
X,
Preparatory routines:
with
channels
$FFE4 (65508).
Registers affected:
Reads
OPEN,
none.
Closes
GETIN
SETNAM,
CLALL.
Y.
OPEN,
CHKIN.
(see READST).
from
or
the
an
zero
input
RS232
is
channel.
device.
returned
in
Should
If
A.
CHRIN
no
is
only
be
character
the
used
is
preferred
devices.
Call address:
$FFF3 (65523).
Communicating registers:
Registers affected:
X,
Preparatory routines:
Error returns:
X,
Y.
Y.
none.
none.
handle
obtained
via
the
this
CIA
registers
routine,
then
use
be
improved.
For
the COMMODORE
$DCOO, VIC 20 returns $9100.
IOINIT
Communicating registers:
Preparatory routines:
Error returns:
none.
be
used
condition.
as the 6581
PAGE
offset
64 ,
from
of
the address
the
address
programs
returned
will
is
May
an
interchangability
to
restore
all
A,
none.
X,
Y.
none.
input/output
SID.
3-28
devices
to
their
normal
LISTEN
Command
Call address:
Communicating
$FFB1
registers:
Registers affected:
This
routine
serial
bus
will
to
listen.
A.
SECOND,
CIOUT,
UNLSN.
(see READST).
command
to listen.
serial bus
none.
routines:
Error returns:
the
A.
Preparatory routines:
Associated
on
(65457).
the
device
specified
to
by
(A),
on
the
IEEE
device.
LOAD
Error returns:
SETLFS,
Y
SETNAM.
0, 4, 5, 8, 9 (see READST).
Loads (or verifies) RAM from the device defined by the SETLFS and
SETNAM routines.
(A) specifies the action: 0 = load, 1 = verify.
If
the secondary address specified in the corresponding SETLFS routine was
A secondary address of 1 or 2
causes the address stored in the header of the file to be used as a load
address.
MEMBOT
Call address:
$FF9C (65436).
Communicating registers:
X, Y.
Registers affected:
X, Y.
Preparatory routines:
none.
Error returns:
If carry is
memory
in
set
none.
on entry,
(X/Y).
If
routine
carry
is
returns
clear,
the address
the
(X/Y)
of the bottom of
is used
to
set
the
bottom of memory.
MEMTOP
Return or set
Call address:
top of memory.
$FF99 (65433).
Communicating registers:
Registers affected:
X,
Preparatory routines:
Error returns:
none.
Similar
in
action
to
X,
Y.
Y.
none.
MEMBOT,
but
used
to
return
or
set
the
top
of
memory.
OPEN
Call address:
$FFCO (65472).
Communicating registers:
Registers affected:
A,
none.
X,
Y.
Preparatory routines:
SETLFS,
Associated routines:
CHROUT, CLRCHN, CLOSE,
SETLFS,
CLALL.
Error returns:
SETNAM.
SETNAM,
1, 2, 4, 5, 6, 240
CHKIN,
CHRIN,
CHKOUT,
(see READST).
PAGE A 3-29
Used
and
to open the file which has previously been specified by the SETLFS
SETNAM
PLOT
routines.
Return or set
current
Call address:
cursor position.
$FFPO (65520).
Communicating registers:
Registers affected:
A,
Preparatory routines:
Error
returns:
A,
X,
X,
Y.
Y.
none.
none.
This routine may be used to obtain the current position (row, column)
of
the
cursor,
or
to
move
the
cursor
to
specified
position.
with carry set obtains the current row (X), and column (Y).
carry clear move
RAMTAS
the cursor to
Initialise
memory,
$03FF.
Call address:
the
A,
Preparatory routines:
Error returns:
A,
X,
X,
RAM
above
Y.
none.
none.
non-destructive
of
Y.
Clears and
test
$FF87 (65415).
Registers affected:
on.
including non-destructive
Communicating registers:
Normally used
Entry
Entry with
test
of
RAM
above
$0400,
resets
the
carries out
screen base
to
$0400.
RDTIM
Read
system clock.
Call address:
$FFDE (65502).
Communicating registers:
Registers affected:
Preparatory
A,
routines:
Error returns:
A,
X,
X,
Y.
Y.
none.
none.
PACE
Y (lsb).
The only
A 3-30
X,
READST
Call address:
$FFB7 (65463).
Communicating registers:
Registers affected:
A.
A.
Preparatory routines:
none.
none.
Error returns:
accumulator may be
Bit
position
result
in an error.
interpreted
Numeric
Tape
value
read
The bits
as shown in the
Serial
Short block
Long block.
in the
Tape
I/O
returned
table below:
load/verify
read
Short block
Long block.
Mismatch.
Checksum.
16
Read
5
6
32
Checksum.
End of file.
EOI
-128
End
Device not
64
error.
of tape.
read.
End
of tape
present.
RESTOR
Call address:
Communicating registers:
Registers affected:
A,
Preparatory routines:
This
routine
to BASIC and
SAVE
restores
none.
X,
VECTOR.
none.
the defaults
Save RAM to
Registers affected:
A,
Preparatory routines:
routines:
Error returns:
routine
saves
specified
by
SETLFS
start
the
end
Y.
As
and
there
are
$FFD8 (65496).
Communicating registers:
Associated
addresses
and
the device.
Call address:
The
Y.
none.
Associated routines:
Error returns:
interrupt vectors.
$FF8A (65418).
A,
X.
X,
Y.
Y.
SETLFS,
SETLFS,
SETNAM.
SETNAM.
5, 8, 9 (see READST).
section
and
of
SETNAM
of the RAM
only three
memory
to
routines.
to be
saved
registers,
is
and
The
device
specified
there
previously
addresses
are
in
A,
of
the
X and
four bytes of
specify
an
address
one
greater
than
the
last
byte
to
be
saved.
PAGE A3-31
SCNKEY
Scans
the
keyboard
and
returns
the
ASCII
value
of
any key
Call address:
$FF9F
Communicating
Registers affected:
Preparatory
(65439).
registers:
A,
routines:
Associated routines:
Error returns:
none.
X,
Y.
none.
STOP,
UDTIM.
none.
In the normal
trapping
this
the
case,
iftQ
the
and
not
allowing
user
may
call
either by a SEI,
normal
the
SCNKEY
perhaps by
scanning.
routine.
or,
keyboard
This
keyboard
In
should
using
Call address:
$FFED (65517).
Communicating registers:
Registers affected:
X,
Preparatory routines:
Error returns:
X,
Y.
Y.
none.
none.
It
the
Call address:
$FF93 (65427).
Communicating registers:
Registers affected:
Preparatory routines:
Associated routines:
LISTEN.
ACPTR.
Associated routines:
LISTEN,
Error returns:
This
bus
routine
following
is used
a
to
LISTEN
(see READST).
transmit
routine,
PAGE
A.
A.
A3-32
ACPTR,
UNLSN.
a secondary address
in
order
that
the
on
the serial
device which
is
SETLFS
Call address:
$FFBA (65466).
Communicating registers:
Registers affected:
Preparatory
called
routines:
in any order,
routine
is
an
X,
Y.
although
SETNAM
and
SETLFS
may be
Associated routines:
Error returns:
none.
This
Af
none.
SETNAM,
essential
OPEN,
SAVE,
preliminary
LOAD.
for
many
of
the
are
transmitted
to
the
routine
through
A,
an
respectively.
SETMSG
LDAIM 2 / LDXIM 4 /
Controls
of
the
format
the
system messages
output
by
the
operating system.
Call address:
$FF9O (65424).
Communicating registers:
Registers affected:
A.
Preparatory routines:
none.
Error returns:
none.
This little used routine controls the printing of error and control
messages.
Bits 7 and 6 of the value supplied in A determine the
printing.
If bit 7
is
set
then
SETNAM
error messages
are
printed,
bit
to be printed.
Call address:
$FFBD (65469).
Communicating registers:
Registers affected:
Preparatory
A,
X,
Y.
none.
routines:
although
SETNAM
and
SETLFS may be
Associated routines:
Error returns:
none.
SETLFS,
OPEN,
LOAD,
SAVE.
is desired
then
the
routine
is
If no
into A.
SETTIM
Preparatory routines:
Error returns:
A,
X,
Y.
none.
none.
none.
The three bytes of the jiffy clock are loaded with (A) msb, (X) and
PAGE A3-33
SETTMO
Set
IEEE bus
Call address:
timeout
$FFA2 (65442).
Communicating registers:
Registers affected:
This
routine
IEEE
bus.
normally
may
be
abandon
none.
none.
used
Commodore
milliseconds.
none.
Preparatory routines:
Error returns:
to
enable
micros
or
disable
controlling
transaction
This
flag.
if
timeout may be
no
timeout
response
defeated
is
not
required
affected
if
it
is
the PET
range
interface).
STOP
by
the
timeout
anticipated
of
IEEE
that
the
devices
bus
received
the
will
within
64
however,
program
(using
this
may
an
be
routine may be
used
to
appropriate
handle
hardware
Call address:
$FFE1
(65505).
Communicating registers:
Registers affected:
Associated
the
returns
STOP
was
with
interrupt
has
been
UDTIM,
SCNKEY.
none.
pressed
the
X
none.
routines:
Error returns:
A,
Preparatory routines:
If
flag,
is
IEEE
trapping on
parallel
flag
during
set.
disabled,
the
In
it
last
the
is
keyboard
event
necessary
that
to
scan,
the
issue
this
routine
normal
keyboard
to UDTIM
call
prior to STOP.
TALK
Call address:
$FFB4 (65460)
Communicating registers:
Registers affected:
The
accumulator
Send
TKSA,
ACPTR,
UNTLK
(see READST).
should
TKSA
none.
routines:
Error returns:
Preparatory routines:
Associated
to TALK.
contain
the
number
of
the
device
on
the
serial
to TALK.
secondary
address
to
the
device
which
is
commanded
to
TALK.
Call address:
$FF96 (65430).
Communicating registers:
Registers affected:
Preparatory routines:
Associated routines:
Error returns:
The
routine
transmits
TALK
TALK,
ACPTR,
(see READST).
secondary address
PAGE A 3-3**
UNTLK
on
the
to a
UDTIM
Call address:
$FFEA (65514).
Communicating registers:
Registers affected:
A,
Preparatory routines:
Associated routines:
Error returns:
The
name
updating
current
STOP
of
the
depressed,
any
clock
in
routine
even
is
it
STKEY.
to
when
none.
SCNKEY,
STOP.
none.
routine
key matrix
Kernal
disabled.
to
this
system
none.
X.
somewhat
also
As
the
whether
normal
IRQ
In
keyboard
consequence
determine
the
deceptive.
scans
it
is
the
stores
possible
STOP
interrupt
addition
and
to
the
for
the
key
has
been
routine
has
been
call
to
the
STOP
routine,
then
STOP
key
detection
will
take
place.
UNLSN
Call address:
$PFAE (65454).
Communicating registers:
Registers affected:
A
Preparatory routines:
Associated
routines:
Error returns:
This
routine
serial bus
UNTLK
commands
none.
none.
LISTEN,
SECOND,
(see READST).
all
devices
CIOUT.
currently
LISTENing
on
the
IEEE
to stop LISTENing.
Call address:
$FFAB (65451)
Communicating registers:
Registers affected:
Preparatory routines:
Associated
routines:
Error returns:
All devices
none.
A.
none.
TALK,
TKSA,
(see READST).
currently TALKing on
the
ACPTR.
IEEE serial
bus will
stop sending
VECTOR
Call address:
$FF8D (65421).
Communicating registers:
Registers affected:
Preparatory routines:
Associated routines:
Error returns:
Calling
the
this
by (X/Y).
X,
X,
Y.
Y.
none.
RESTOR.
none.
routine with
system vectors
A,
to be
carry set
stored
in
will
cause
the
current
contents of
is pointed
to
this list.
PAGE
A3-35
INDEX
= Explained
P = First Program
ADC
1-6E,
1-2E,
1-8
CPXIM 3-13P
CPXZ 3-10
CPY 2-10
1-7P
ADCIX 3-13
ADCIY 3-H
CPYIM 3-11E,
3-11P
CPYZ 3-10
ADCZ 3-10
Crashes
Addressing 3-8ff
Data Bus
8-20
1-10
1-8
Debugging 6-9
ALU 1-7
AND 4-12E, 4-14P
DECZ 3-10
ANDZ 3-10
DEX 2-6E,
2-6P
DEY 2-7E,
2-12P
ASLA 4-19P
ASLZ 3-10
Assembler, entering program 1-3
BCC 4-1E,
4-1P
Delays 3-4
Disassembly 5-2
Division 4-9,
EOR 4-16E,
BCS 4-2E,
EORIM 4-16P
4-2P
8-21
D flag 2-13E
EORIX 3-13
EORZ 3-10
4-16P
EORIY 3-13
Binary-coded decimal
Exponentiation 8-22
arithmetic 4-10
Binary notation A1-1ff
Flags 2-6ff
BITZ 3-10
BMI 2-14E,
BNE 2-7E,
BPL 2-14E,
2-14P
2-8P
2-14P
Floating point
subroutines 8-18
I flag 2-13E
Immediate addressing 3-10
Branches 2-6ff
INC
BRK 8-3E
INCZ 3-10
Indexed addressing 3-11
BVC 8-6E,
BVS 8-6E
8-6P
4-8E
C flag 2-13E
CLC 4-1
CLD 4-1OE, 4-11P
Interrupts 8-1
CLI 8-1E
INX 2-8E
CLV 8-16
INY 2-8E
2-11P
JMP 2-1E,
2-1P
CMPIX 3-13
JMPIA 3-HE,
CMPIY 3-H
CMPZ 3-10
Colour 6-13
Colour RAM 1-6
Conditional Jumps 2-6
CPX 2-8E,
PAGE
1-1
2-9P
Labels 5-1
LDAIM 1-2E,
LDAIY 3-12E
3-14P
1-3P
LDAZ 3-10
ROR 4-23E
RORA 4-23E
LDYIM 1-11E
RORZ 3-10
RTI 8-2E
LDYZ 3-10
SBC 4-6E
SBCIM 4-7P
SBCIX 3-13
SBCIY 3-14
LSR 4-17E,
SBCZ 3-10
4-18P
LSRA 4-18P
LSRZ 3-10
Machine code
1-1
Macros 5-11
Memory Labels 5-3
STA
Monitor 6-3
1-2E,
1-3P
Stack 7-8
Multiplication 6-7E
STAIX 3-13
N flag 2-13E
STAIY 3-14
Status register 2-6E
Status word 2-6E
Nybble A1-4
NOP 3-7, 4-4E, 4-4P
Numerical screen output 8-7
ORA 4-15E,
4-15P
STAY 3-1E,
STAY 3-1E,
ORAIM 4-15P
ORAIX 3-13
ORAIY 3-14
3-6E
3-1P
STAZ 3-10
STX 1-9E,
1-9P
STXZ 3-10
STY 1-11E
ORAZ 3-10
Overflows 8-5
STYZ 3-10
PHA 7-11
PHP 7-10
PLA 7-10
TAX 1-12E,
PLP 7-11
TAY 1-13E
Timing 3-4
TXA 1-13E
2-2
PSW 2-16
1-13P
PAGE
1-2