Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assembly Lines Complete Wagner

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

Assembly Lines:

The Complete Book

A Beginner’s Guide to 6502


Programming on the Apple ] [

by
Roger Wagner

edited by
Chris Torrence
©2
014Rog
erR.
Wag
ner

S
econdpr
int
ing
,Apr
il2
017
.

⇢isworkisma deavail
ableunderaCreati
veCommonsAt tr
ibut
ion-NonComme r
cial
-ShareAl
ik e2
.0li
cense.
You
ar
efre
etoshareanda daptthema t
eri
ali
na nyme di
um orformatunderthef
ollowingterms:(1)Attr
ibut
ion–You
mustgi
veappropria
tecredit
,provi
dealinktotheli
cens
e,andindicat
eifcha
nge swerema de;(
2 )NonComme rci
al
–Youma ynotus ethema ter
ialf
orcomme rci
alpur
poses
;(3)ShareAli
ke–Ifyour emix
, t
ransf
or m,orbuil
dupon
t
hema t
eri
al,
youmus tdis
tri
buteyourcontr
ibuti
onsunderthesamelic
enseastheorigi
nal.Forthecomplete
l
ice
nsese
ehttp://
creat
ivec
ommons .
org
/li
censes
/by-
nc-s
a/2.0/
.

As
sembl
yLine
s:TheCompl
et
eBooki
sani
nde
pende
ntpubl
ic
ati
ona
ndha
snotbe
ena
uthor
ize
d,s
pons
ore
d,or
ot
herwi
sea
pprov
edbyAppl
eInc
.

Appl
e,t
heApplelogo,andallAppl
eha
rdwa
rea
nds
o4wa
rebr
andna
mesa
ret
rade
mar
ksofAppl
eInc
.,r
egi
st
ere
d
i
ntheU.S.
andothercountr
ies.

⇢econt
ent
sofVol
ume1(chapte
rs1715
,appe
ndicesA7E)wer
eori
gi
nal
l
ypr
int
edi
nAs
semb
lyLi
nes
:TheBo
ok
(
Rog
erR.Wagne
r,S
o4a
lkPubl
ishi
ng,Nor
thHol
lywood,CA,1
982)
.

⇢econt
ent
sofVolume2(chapter
s1673
3)we
reor
igi
nal
l
ypr
int
edi
nSo
fta
lkma
gaz
ine(
So4a
lkPubl
is
hing
,Nor
th
Hol
l
ywood,
CA,Januar
y19
82 7June19
83).

⇢ec ov
e ri
ma g
esoftheAppleIIPlusandthegreenbarcompute
rpa pe
rwerecrea
tedbyChri
sTorr
ence
.⇢e
“Usa
geChartof650
2Inst
ruct
ions
”isadapt
edfrom Fi
g.2-
1inIns
idetheAppl
e//
e,byGaryB.
Lit
tl
e,a
ndisus
edby
per
miss
ion.Al
lothe
rimagesa
ndFg ure
sare©Rog e
rR. Wagne
r.

Whileever
ypreca
uti
onha sbe
entakeninthepre
para
tionoft hi
sbook,t
hepubl
is
her,
author
,andedi
toras
sumeno
r
esponsi
bil
ityf
orerr
orsoromiss
ions,
orfordamage
sre s
ult
ingfrom t
heuseoft
heinf
ormati
onconta
inedher
einor
f
rom theuseofpr
ogramsandsourcecodet
hatmayaccompa nyit
.

I
SBN9
78-
1-3
12-
089
40-
2
Tab
leo
fCo
nte
nts
Pr
efa
ce.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.xi
I
ntr
oduc
tion.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
xvi
i
1.
Appl
e’
sAr
chi
tec
tur
e..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.1
65
02Operat
ion 2
MemoryLocat
ions 2
Hexa
deci
malNot a
tion 4
I
t’
sCult
ure⇢a tCounts7
2.
⇢eMoni
tor
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
9
Ex
plori
ngt
heMoni
tor 9
Di
sass
embl
y1 0
3.
Ass
embl
ers
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
13
⇢eMi ni
-Asse
mbler1 3
Ass
e mbler
s1 5
Load/St
oreOpcodes1 8
Put
tingitAll
Tog e
the
r1 9
Conclusi
on 2 0
4.
Loopsa
ndCount
ers
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
21
Binar
yNumbe r
s2 2
⇢eS tatusReg
ist
er22
Incr
eme nti
ngandDec
reme
nti
ng 2
3
LoopingwithBNE 24
5.
Loops
,Br
anc
hes
,COUT,
andPa
ddl
es.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
27
LoopingwithBEQ 2 7
BranchOLs et
sandRe ver
seBra
nche
s28
Scr
e e
nOut putUsingCOUT 2 9
ReadingaGa mePa ddl
e3 2
PaddleProgra
m Problems 33
Transfe
rComma nds 34
ANot eaboutBRUNa ndCOUT 3 5
6.
I/OUs
ingMoni
tora
ndKe
yboa
rds
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
37
Compari
sons;Rea
dingtheKe yboa
rd 37
CompareComma ndsandCa r
ryFla
g3 8
Usi
ngMoni t
orProgramsforI/ORouti
nes4
1
Rea
dingDatafrom t
heKey board 42
7.
Addr
ess
ingMode
s..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
45
I
ndexedAddr
ess
ing 4
6
S
ome t
imesXandYAren’
tInt
erc
hang
eabl
e47
S
tor
ingPureDat
a4 8
8.
SoundGe
ner
ati
on.
.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.53
De
lays54
De
layVal
ueinMemor
y5 6
De
layf
rom t
heKe
yboa
rdorPa
ddl
es5
8
9.
⇢eSt
ack.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
61
S
tac
kLi
mit6
4
Ta
bleofCont
ent
s v
ii

10.
Addi
ti
ona
ndSubt
rac
tion.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.65
Bina
ryNumbe rs6 5
Addit
ionwithADC 6 6
Subt
racti
on 72
Posi
ti
veandNe ga
tiv
eNumbe
rs7
2
⇢eS ignBit7 3
⇢eS ignFla
g7 5
11.
DOSa
ndDi
skAc
ces
s..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.77
⇢eOv ervi
ew:DOS 7 7
Di
skett
eOr ga
nizat
ion 7 8
DOSModi Fcat
ions 85
Di
sk-VolumeModi Fca
tion 86
Cat
al
ogKe ypres
sModiFc a
tion 87
Be
llModiFcati
onandDr iveAcc
ess88
12.
Shi
5Ope
rat
orsa
ndLog
ica
lOpe
rat
ors
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.89
Shi
4Ope ra
tors8 9
Logi
calOpera
tor
s9 2
BIT 96
ORAa ndEOR 9 7
13.
I/ORout
ine
s..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.105
PrintRout
ine
s105
InputRout
ines1
08
14.
Rea
dinga
ndWr
iti
ngFi
lesonDi
sk.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
113
Re
adi
nga
ndWr
it
ingDat
aFi
les1
13
Re
adi
nga
ndWr
it
ingTe
xtFi
le
s120
15.
Spe
cia
lPr
ogr
ammi
ngTe
chni
que
s..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.127
Reloca
tablevers
usNon-r
e l
oca
tabl
eCode 1
27
JMPComma nds 128
De t
erminingCodeLocat
ion 13 1
JSRSimulati
ons 134
Self
-Modifyi
ngCode 1 37
Indir
ectJumps 1 3
9
16.
Pas
singDa
taf
rom Appl
eso5BASI
C..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.143
Si
mpl eI
nte
rfac
ing 144
⇢eI nte
rnalS
truct
ureofAppl
eso4 1
45
Pas
si
ngVa ri
ables1 47
17.
Mor
eAppl
eso5Da
taPa
ssi
ng.
.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.151
Appleso4Variable
s1 51
Me moryMa ps 1 52
Passi
ngVa r
iabl
e st
oAssembl
yLanguage156
Passi
ngDa t
afrom Asse
mblyLanguag
e1 6
1
ProgrammingTi p 164
Conclusi
on 1 65
18.
Appl
eso5Hi
-Re
sGr
aphi
cs.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.167
GroundS chool 1 6
7
Landma r
ksa ndEntryPoint
s168
ATe stFl
ight:Hi-ResDemo 16
9
AMi norDi ver
sion 172
Locat
ion 1 73
Motion 1 73
v
ii
i As
sembl
yLi
nes

19.
Cal
li
ngHi
-Re
sGr
aphi
csRout
ine
s..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.177
Taki
ngtheOpposit
eofaSi
gnedNumbe
r1 7
8
⇢eRe al⇢ing:
Hi -
Resi
nAs s
embl
y1 79
Tabl
e-Dri
venGraphi
cs1 83
Concl
usi
on 1 8
7
20.
Str
uct
ureoft
heHi
-Re
sDi
spl
aySc
ree
n..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.189
LoadingaHi-ResScre
en: t
he“Fil
l”ELe
ct1 8
9
AnotherProbl
em: Shi
4ingColors1 92
OtherProble
ms:Whe nI sWhiteNotWhit
e?1 9
5
SuperHi-ResGraphic
s1 95
21.
Hi-
ResPl
ott
ingi
nAs
sembl
y..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
197
NormalPointPlot
ting 19
7
Al
terna
tePlotti
ngMode s2 0
0
14
0-PointResolut
ionMode 201
56
0-PointResolut
ionMode 203
ADe monstra
tionProgr
am 2 06
22.
Eve
nBe
tte
rHi
-Re
sPl
ott
ing.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
207
Int
era
cti
onsbetwee
nAdjacentBy
tes2
08
Some“Ne wandImprove
d”Routi
nes209
PLOT.140+ 210
PLOT.560+ 212
PLOT.560-Whi
te2 1
3
AFinalDemoPr og
ram 2 1
7
Concl
usion 218
23.
Hi-
ResGr
aphi
csSCRNFunc
tion.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.219
AnOv e
rvi
ew 221
Sa
mpleProgr
am 223
Conc
lusi
on 224
24.
⇢eCol
li
si
onCount
er,
DRAW,
XDRAW.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
225
SomeExperi
me nt
s2 2
5
DRAW v er
susXDRAW 2 27
Pri
nci
ple
sofAni mat
ionandCol
li
si
on 2
28
⇢eS c
anner2 28
⇢ePos s
ibi
li
ti
es2 3
4
25.
Expl
osi
onsa
ndSpe
cia
lE?e
cts
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.235
Expl
osions,Rays
,andOther⇢ings⇢a
tGoBumpi
ntheNi
ght 2
35
ALittl
eMor eSophis
ti
cat
ion 2
3 9
Putt
ingitAllToget
her2 41
⇢eS hooterProgra
m 2 45
26.
Pas
singFl
oat
ing
-Poi
ntDa
ta.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.251
I
nte
rna
li
zat
ionofDa
ta:
Int
ege
rve
rsusRe
alVa
ria
ble
s25
2
Ta
bleofCont
ent
s i
x

⇢eFl oa
ting-
Poi ntAccumula
tor(FAC) 2 5
4
Pas
si
ngDa t
afrom Appleso4totheFAC 2 55
MovingtheFACt oaMe moryLoc a
tion 25
7
MovingMe mor yintot
heFAC 2 58
Pas
si
ngFACDa taBacktoAppleso4 2 5
9
Put
ti
ngitAl lTog e
ther2 60
Concl
usion 2 62
27.
Floa
ting
-Poi
ntMa
thRout
ine
s..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.263
MoreApples
o4I nt
ernal
s2 65
AnExample⇢a tDoe s
n’tWork 266
WhyitDoesn’
tWor k2 67
ALit
tl
eMor eFines
se2 69
Othe
rOpe r
ati
ons:Subt
ract
ion,Mul
ti
pli
cat
ion,
andS
oOn 2
70
Concl
usi
on 2 70
28.
⇢eBCD,
orBi
nar
yCode
dDe
cima
l..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.271
Limi
tat
ions 273
⇢eCa rryFl
ag2 73
CommonOpe r
ati
ons 27
4
Pri
nti
ngBCDVa l
ues276
Concl
usion 279
Spec
ial
Not e
:Counti
ngDown 2
80
29.
Int
erc
ept
ingOut
put
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.281
Output 2 8
1
Int
ercept
ingOut
put 2
83
OtherOutputDe
vic
es2 8
6
Conclusi
on 290
30.
Int
erc
ept
ingI
nput
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
291
⇢eI nputVector:KSW 2 9
1
OtherInputSources2 9
4
Int
ercepti
onRoutines2 95
SomethingMor eUsef
ul:Lower
cas
eInput 2
97
Conclusi
on 3 00
31.
Hi-
ResCha
rac
terGe
ner
ator
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
301
TextandHi-ResS
cre
enMa ppi
ng 3
01
⇢eCha ract
erGener
ator 304
AHi -
ResCha r
act
erSet309
Concl
usion 312
32.
Hi-
ResCha
rac
terEdi
tor
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
313
HowitWorks3 20
AndNowwi t
htheMagni
fyi
ngGl
ass3
21
Runni
ngtheEdi
tor 3
25
Misc
ell
ane
ousNote
s3 26
Concl
usi
on 326
33.
⇢e65C02.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
327
NewAddressi
ngMode s3 28
I
ndir
ectAddress
ing 328
I
ndexedAbsolut
eIndir
ect329
New“Sta
ndard”Addressi
ngModes3 30
AtLas
t,t
heRe al
S c
oop!NewIns
truc
ti
ons 3
31
Othe
rDiLerence
s3 35
x As
sembl
yLi
nes

Appe
ndi
xA:
Cont
est
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
339
Appe
ndi
xB:
Ass
embl
yComma
nds
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.344
Appe
ndi
xC:
6502I
nst
ruc
tionSe
t..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.394
65
02Mi croproce
ssorIns
truc
tions 3
94
Usag
eCha rtof6502Inst
ruct
ions 395
65
02I ns
tructi
onCode s3 98
65
C0 2Instr
ucti
onCode s4 02
HexOpe r
a t
ionCodes4 03
Appe
ndi
xD:
Moni
torSubr
out
ine
s..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.404
Out putSubrout i
ne s40 4
InputS ubr
out i
ne s4 05
Low- ResGr aphicsSubrouti
nes4 0
6
Hi- ResGraphicsS ubr
outines407
Floa t
ingPointAc cumulator 40
8
Ot herSubroutines4 10
Appe
ndi
xE:
ASCI
IandSc
ree
nCha
rts
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
411
YouGetWha tYouASCI
IFor
..
.41
1
Tex
tScre
enMe moryMap 416
Hi-
ResMe moryMap 417
Appe
ndi
xF:
Zer
o-Pa
geMe
mor
yUs
age
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
418
Spe
cia
lLoc
ati
ons 4
18
MemoryUsag
eTabl
e4 1
9
Appe
ndi
xG:
Beg
inne
r’sGui
det
oMe
rli
n..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.420
ControlMode s4 20
Gett
ingS tart
ed 421
Dele
tingLi nes423
Ins
erti
ngLi nes4 23
Edi
tingLi nes424
Asse
mbl ingtheCode 424
Sav
inga ndRunningYourProg
ram 4
25
Li
stofPr
ogr
ams
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.426
Di
rec
tor
yLi
sti
ngf
orPr
ogr
am Di
sks
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
427
I
nde
x..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.428
Qui
ckRe
fer
enc
e..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
432
Pr
efac
e

InOc tobe r1 98 0,int hes e


c ondi ssueofS ofta l
kma gazine ,ane ws eriesof
arti
clesma dei tsde but.Itst itl
ewa s“ Asse mbl yLi ne s”wi t
ht hes ubt itle“ Ev ery
-
one ’
sGui det oMa chineLa ng uag e.
”1⇢ea uthorwa sRog erWa g ne r,thepr esi
-
dentofS outhwe st
ernDa t aS ystems .Byt hen,Rog erha da lrea dye stablished
hims e
lfa sawe ll-res
pe cteds o4wa republ isherwhoc are
da boutt hee ndus e r
. As
AlTomme rvi
ks tat
es,“ Hispr ogr amsr eSe ctc onc ernt hattheus erg etmor et han
util
ity
–hes houl da l
sog aink nowl edge–f rom us eoft hes o4wa re.” 2

Be f
or etha tissueofS o ftalk,
af ewbr av esoul sha dl e
arne da sse mbl yl ang uage
al
lont he irown,us i
ngc l
ue sf rom t heROM l istingsint heAp p leIIRe ference
Ma nua l
.⇢e sei nc l
ude d de veloperss uc ha sJ ordan Me c hne r( crea t
orof
Ka rat
eka) ,S il
a sWa rner( Ca s
tleWo lf
e nstein),a nd Da n Br ick lina nd Bob
Frankston( VisiCa l
c).Butnowhe r
ewa sas e
riesofa rti
clestha tta ug htt her estof
ushowt opr og ramt he6 502 .
IFr stbe camea cqua i
nt edwi thAs semb lyLi ne swhe n, asane wAppl eI IPlus
owne r,Ir eceivedmyc ompl iment aryissueofS oftalkma gazi
nei nJ anua ry1 9 82.
⇢es erieswa sa l
readyonPa rt1 6;i tha dmov edbe y
ondt heba s i
csa ndwa s
beginningt oe x ploresounda ndhi -resolut i
ong raphi cs.Despitemi ssingt hec r
u-
ci
a lintroduc tor ya rt
icles,Ie ag erl
yl ook e df orwa rdtor eceivingS o ftalke ach
mont ht os eewha tRog erWa g
ne rha dt ot eachusa boutt he6 5 02a nda ssembl y
la
ng uage.
⇢e n, i
nMa rch1 98 2
, So4a l
ka nnounc edt ha titwa spubl ishingRog er’
sa rti
-
cl
e sinbookf or m. Assemb l
yLi ne s:TheBo okc ont ainedt heFr stF4e ena r t
icle
s
plusa na ppe ndi xof6 502a ssembl y-
lang ua gec omma nds,Moni tors ubr out ines,
anda ninde x.
Rog erc ont inuedt owr itehi smont hly“ As sembl yLi nes”a rticlesunt i
lJ une
1983 .
Pa rt3 3cont aineda ni nt r
oduc t
iont ot hene w6 5C02c hipa nde nde dwi tha
farewell
:
Iwanttot
hankthemanyrea
der
softhiscol
umnoverthelas
tse
v-
er
alyea
rsf
orthe
irent
husi
ast
ics
upportandval
uabl
esugge
sti
ons
.I

1Eve
ntua
ll
ycha
nge
dto“
Eve
ryone
’sGui
det
oAs
sembl
yLa
ngua
ge.

2Tomme
rvi
k,Al
l
an,“
Exe
cSDS
:Sout
hwe
ste
rn Dat
aS y
stems
,Ass
embl
ing Us
eful
Ut
il
it
ie
s,”S
ofta
lk,
Aug
ust1
981(
So4a
lkPubl
is
hingI
nc.
),pp.30
732.
x
ii As
sembl
yLi
nes

ha
vealwaysbel
ie
vedtha
tthehumane
lementt
othi
sindus
try
,and
i
nfac
ta nyende
avor
,ist
hetrul
yre
war
dingpar
t3
.
⇢ea
rti
cl
ethe
nende
dwi
thanot
efr
om t
heEdi
tor
,st
ati
ng:
⇢eFr sty
ear
’scol
umnsplusappendix
esandrev
isionshav
ebeen
ava
il
ablef
orsomet imeinAss
emb l
yLine
s:TheBo ok.Vol
ume2 ,
cove
ringt
heres
tofthecol
umns,wil
lbere
lea
sedshortl
ybySo4a
lk
Books.
De spitenume rousa nnounc eme ntsa boutVol ume2ov ert hene xty ear
,
whe nS o4a l
kPubl i
shingI nc. we ntba nk ruptinAug us t1984 ,As s
emb lyLines:The
Bo o
k, Vo l
ume2r emaine dinc ompl etea ndunpubl is
he d.
Itist here
foreag reatpr ivil
egea ndal ong -
overduehonort opr esenta l
lof
Rog erWa gner’
s“As s
e mbl yLi nes”a rticl
e sinonec ompl etev olume .⇢i svolume
containsa lloftheor i
gina lAs semblyLi nes:TheBo ok ,includingt hea ppendices,
plusthec ontentofther ema ininge ight eenS o ftal
ka r
ticl
e s
.
⇢ec ompl etevolumes houl da ppe al t
ol ong -t
imer e a
de rswhoma ynotha ve
accesst otheor igi
nalS oftal
ka rti
cles,e speciallythos ea r
ticlesfrom themi ss
ing
Volume2 . Ial
sohopet hatRog erWa g ner’scle arexplanationsa ndhi ssubtlebut
ever-presenthumorwi l
le nc ourag ene wr eade rstodi scov erthej oy sof6 502
assembl y-
languagepr og r
a mmi ngont heAppl eII.AsDa vidFinni gannot esin
TheNe wAp pl
eIIUs er’
sGui de ,“⇢e r ea restillsoma nypr og ramst obewr itte
n,
experime ntstobec onduc ted, anda dv entur e
st obeha d.”4

Wi thRog e
rWa gne rl eadingt hewa y,a ndwi t
ht r a
c t
or-feedpa pe rinone
handa ndMe r
li
nAs s
emb lera tours ide,whok nowswha ta ma zi
ngpr og ra
mswe
canc reate?

Chri
sTorrenc
e
Loui
svi
ll
e,Col
ora
do
Dece
mbe r1,2
014

3Wa
gne
r,Rog
er,“
Ass
embl
yLi
nes
,Pa
rt3
3,”S
ofta
lk,
June1
983(
So4a
lkPubl
is
hingI
nc.
),
pp.
199
720
4.
4Fi
nni
gan,
Dav
id,
TheNe
wAp
pleI
IUs
er’
sGui
de(
MacGUI
,Li
ncol
n,I
L),
p.x
i.
Pr
efa
ce x
ii
i

Cha
nge
sfr
om t
heOr
igi
nal
Int heor i
g i
na lAs semb lyLine s:TheBo ok,theFr stt woS oftalka r
ticles( Oc to-
bera ndNov embe r1 98 0)we rec ombi ne di ntoc ha pte rone ,“ Appl e’sAr chi tec-
ture.
”I nt hise diti
on, tha tcha pterha sbe enr e-spli
ti nt ot woc hapt ers
: theFr s ton
theAppl e’sarchitectur ea ndt hes econdont heAppl eMoni tor. Bydoi ngt his, al
l
ofthec ha pternumbe rsnowa gre ewitht heor iginalS o ftalka rti
clenumbe rs.
Al loft hea ssembl y-lang uag eprog r amsnowi ncludet heCHKps eudo- opc ode
atthebot tom.⇢eCHKi nstruct i
oni ns ertsas i
ng l
e -by tec he cksum a tthee nd.
Youc anus et hisc he c
k sum t ov eri
fyt ha tyouha v et ype di nt hepr og ramc or-
rectl
y. CHKi sav ail
ablei nt heMe r l
inMa croAs semb ler. Ify oura ssembl erdoe snot
suppor tthisps eudo- opc odey ouc a
ni gnor eitint hec ode .
⇢eor ig
ina lpr ogra msi nc ludedt heOBJps eudo- opc ode ,whi che s
tablis hes
thea ddr essatwhi cht hec odewi l
lbea ssemb l
e d.⇢eOBJdi r
e cti
v eisnotus ua l
ly
necess arya ndi sinc ompa ti
blewi thla t
e rversionsoft heMe rl
ina ss
embl e r( Me r-
l
inPr oa ndMe rlin8 /
1 6).Int hise ditiona l
loft heOBJ l i
ne sha vebe enc om-
me ntedout .
S pell
ing ,g r
a mma r,a ndot he rmi norc orrectionsha v ebe enqui e t
lyma de.
Mor es igniFc antc orrections( suc ha sc odinge r r
or s)a rema r
k edwi t
haf oot not e
andmyi nitial
s[ CT] .
InAppe ndixBa ndC, the6 5 C02i ns t
ructionsha vebe ena dde d.InAppe ndi x
C,t heI nstructionCo de stablenowc ont a
insac olumnwi t hthec lockc yc lesf or
eachi nstruc t
ion.⇢eUs a geCha rtinAppe ndixCwa sa da ptedf rom t hec ha rti n
Insi
det heAp p l
e/ /eb yGa r yB.Li ttlea ndi sus edwi thhi spe rmission.Ia lso
adde dane wAppe ndixF( Ze r
o -PageMe mo r
yUs a ge)a ndAppe ndixG ( Be gin-
ner’
sGui det oMe rl
in) .

Ac
knowl
edg
eme
nts
⇢i se dit
ionwa sc r
eatedonaMa cBookPr ous i
ngOpe nOWc e4 .
1.1
.⇢e
assemblyc odewa scr e
atedus ingt heMe rl
inMa c
roAs s
emb lerona nAppl e/ /e
comput era ndt heVi r
tual][e mulator( http:/
/www. virtuali
i.com) .⇢ei ma ges
weres ca
nne dus ingt heFlipPa lMobi leS canner( htt
p://Sip-pal.com) .⇢ec over
i
ma geoft heg re
e nba rcomput e rpaperwa sc reatedusingI DL8 .4.
⇢et e xtforVol ume1wa st akenf r
om t hee le
ctronicv ersi
ona vailabl
ef rom
theOpe nLi brar
y ,whichisapr ojectoftheI nternetAr chive.
ForVol ume2 ,Iwoul dl iketoa cknowl edget heg e
ne roushe lpofJ i
m
Salmons,Ti mlynnBa bit
sky,andPe t
erCa y l
oroft heS o4a lkAppl ePr oject
,who
providedPDFa ndOCRv ersionsoft heAs sembl yLine sarticl
e s.⇢e i
re nthus i
-
asma nde xperta dvi
cema det hi sprojectpossibl
e .
Speci
a lthank sisg i
vent omyf earle
sspr oof r
eader s
:JohnGr uve r,Ant oine
Vigna u,
andS hawnLe wis
. ⇢r ought heirde dic
a t
iona nda ttentiont ode tai
lt hey
x
iv As
sembl
yLi
nes

ca ughtma nyi ns
idiouserrors
.⇢a nksa lsotot heme mbe rsof6502.org and
comp.sys.apple2, whopr ovi
deds ug ge
stionsforc hapter33ont he65 C02.Addi-
tionaltha nkstofell
owAppl eIIe nthusi
astMa r
cGol ombe c
k ,
whopr ov i
dedval
u-
abl efe
e dbackont hesecondprinting.
Ic ouldnotha vecompl e
tedt hisbookwi thoutthehe lpa ndsupportofmy
wonde rfulfamily
:mywi feGigia ndmyda ughtersMi aa ndEl y
ssa.⇢a nksto
Gi giforbe ingag uineapigont heBe g
inne r’
sGui detoMe rl
inappendix,andto
Mi aandEl ys
saforr ea
dingpa gesofhe xc odesoutl oudwhi leIpr oofr
eadthe
pr ograms .
Finall
y,Iwoul dl i
ketotha nkRog erWa gnerforg ivi
nghi spermissi
onfor
thepr oj ect
,forans weri
nginnume rabl
ee ma i
lsaboutAs s
emb l
yLine sminuti
ae,
andf ora llofhiscontri
buti
onstot heAppl eI Icommuni tyovertheyears.
Volume 1
I
ntr
oduc
ti
on

Oneo4e ng etst hei mpr es si


ont hatpr ogrammi ngi na sse mbl yl a ngua gei s
somev erydi Wc ulta ndobs curet echni queus edonl ybyt ho sea dva nc edp r ogram-
me rs.Asi tha ppe ns ,a ssembl yl ang uagei sme relydi Le re nt ,andi fy ouha v es uc-
cessfull
yus e dI nt eg erorAppl eso4BAS I Ct odos omepr og rammi ng ,the re’
sno
reasonwhyy ouc an’ tus ea ssembl yl angua get oy oura dv a nt a
gei ny ourownpr o-
grams .
⇢i sbookwi llt a kear athe runor t
hodoxa ppr oa cht oe xpl aininga s s
e mbl y
prog rammi ng .Be c aus ey oua repr esuma blys ome wha tf a mi li
a rwi thBAS IC,we
willdr aw ma nypa ra l
lelsbe twe env ariousa ssembl y-la ng uag et ec hni que sa nd
theirBAS ICc ount er pa r
ts .Ani mpor t
antf actori nl earni nga ny thingne wi sa
familiarf rame wor ki nwhi c ht oFtt hene wi nforma tion.Yourk nowl edg eof
BAS ICwi l
l prov idet ha tfra me wor k .
Iwi lla l
sot ryt ode scribei nitiall
yonl yt hos et echni c alde tailsoft hemi c ro-
proce ss
orope rationst hata rene ededt oa ccompl ishouri mme dia t
eg oa l
s.⇢e
restwi l
lbeFl l
e di na swemov et omor einv olvedt e chni que s.
⇢i sbookdoe snota ttemptt oc overe ve rya spec tofa s sembl y-la ng uagepr o-
grammi ng. Itdoe s, howe v er,pr ovidet hene cessaryi nf or ma t
iona ndg uidanc et o
all
ow e venas ome wha ti ne xpe ri
e nce dpe rsont ol ea r
na ss embl yl a ng uagei na
mi nimum oft ime .Yous houl dFndt het exta nde xa mpl esqui ter ea da ble,wi th-
outbe i
ngov erwhe lme dbyt e chni caljargonort oomuc hma terialbe ingpr e-
senteda tonc e .
I ’
dl i
k et ot a ket hisoppor tuni tytobr ieSyme nt i
onaf ew ofmyownpr o-
grammi ngphi los ophi es.Wr i
tingpr og r
a mst odoag i
v e nt aski se sse ntial
lya n
exerciseinpr obl ems ol ving .Pr oble ms olvingi sinf actas ubj ectini t self.Noma t
-
terwha ty ourpr og r ammi ngg oa l i
s, i
twi llalwa ysi nvol ves olvings omepa rticular
aspec tthat,a tt ha tmome nt,y oudon’ tr eallyk now how t os ol ve.⇢emos t
impor tantpa r tist ha t,ify ouk eepa tit,youe ventua l
lywi ll g e
tt hes olut ion.
Oneoft hek eye leme ntsint hispr ocess, Ibe l
ie ve,a ndt hepa rticula rpoi ntt o
stres
snow, i
st ha ti tisi mpor tantt obeat ool use r
. Pr ogr ammi ngi na nyl ang ua ge
consistsofus ingt hev ariousc omma ndsa ndf unc t
ionsa v ail
a blet oy oui nt hat
l
a ngua gea ndofput tingt hema l
lt og et
he ri namor ec ompl exa ndf unc tioni ng
unit.Ify oua renotf ami liarwi tht heopt ionsy ouha vea ta nyg ivenmome nt –
thatis,yourt ool s –t hepr oblem- solvingpr oc essisi mme ns e l
ymor edi Wc ult.
x
vii
i As
sembl
yLi
nes

Myi ntentinthisbooki stopr es


entina nor ganize
dwa yt hev ari
ousope ra-
tionsa v
ail
ableinassemblyla ngua geandhowt heyc a
nbec ombi nedtoa ccom-
plishs impleobjec
tiv
e s
.⇢emor efamili
ary oua rewiththeseeleme nts
,theea si
er
itwill betosolv
eapa rti
cularpr og r
ammi ngpr oblem.
Youma ywi s
ht ok eepy ourownl i
stoft hea s
sembly-languagec omma nds
andt heirfuncti
onsa sweg oa long.A listoft hesecomma ndsi sincl
ude di n
Appe ndixC,butIt hinky ou’lla greethatbyt akingthetimet owr i
teeachone
downa syoulearnit
, al
ongwi t
hy ourownpe rsonalexpla
na t
ionofwha titdoe s
,
youwi llcr
eateamuc hs t
ronge rima geiny ourmi ndofthatpa rt
icularoperat
ion.
Youma ywishtos uppleme ntt hi
sbookwi thotherbook son6 502program-
mi ng. Recomme ndedbook sinc lude:
Ra
ndyHy
de,
Usi
ng6
502As
semb
lyLa
ngua
ge(
Nor
thr
idg
e,CA:
Dat
aMos
t,1
981
).
DonInma nandKurtI
nma
n,Ap
pleMa
chi
neLa
ngua
ge(
Res
ton,VA:Re
ston
Publ
is
hing
,19
81)
.
La
nceA.Le
vent
hal
,65
02As
semb
lyLa
ngua
ge(
Ber
kel
ey:
Osbor
ne/Mc
Gra
w-Hi
ll
,
1
979
).
Rodna
yZa
ks,
Pro
gra
mmi
ngt
he6
502(
Ber
kel
ey:
Sybe
x,1
981
).
⇢e rea r
eundoubt e
dl yot herst hata r
ea lsoa vailable,andy ous houl dc on-
sidery ourownt asteswhe ns electingwhi chone ss eem mos ta ppropr i
a tetoy our
ownl ear
ni ngs tyl
e .
Ana dditionalc onc ernf orabookl iket hisiswhi cha ssembl ert ous e.( An
assembl eri sa ne ditor -l
ikeut ilit
yf orc reatinga ssembl y-languag epr og r
ams .If
you’ r
ev ag ueont hisc heckc ha ptert hreeformor ei nforma t
ion.)Al thoug hI ’m
some wha tbi ased,myf avor itea ssembl erist heonea vail
a blefrom S out hweste rn
Da taS ystemsc a
lledMe rlin.I tnotonl yc ont ainsag ooda ssembl er,buta l
soa
numbe rofa dditiona lutilitiesa ndFl esofi nte r
e s
t.Me rl
ini snotr e
qui red,how-
ever ,asthee xa mplesg iv
e na r ewr itt
e ntobec ompa tiblewi thmos toft hea ssem-
blersc urr entl
ya vail
a ble.⇢e s einc ludet heAp pl
eDOSTo olKi t,TED I I,t he
S- CAs semb l
er,andma nyot her s.
Alsoa v a
ilabl
ef rom S out hwe sternDa taS y s
temsi saut ili
tyc all
edMunc h-A-
Bug( MAB)whi cha llowsape r
s ont oe as
ilytra cea ndde -
bugpr ograms , aproc ess
whi chc anbeoft r
e me ndoushe l
p,MABa l
s oi nclude sitsownmi ni-assembl er
whi chc anbeus edf ort hebe ginni ngl is
ti
ng spr ov i
de dint hisbook .
I
nt er msofha r dwa re,a nyAppl eIIorAppl eI IPl uss houldbemor et ha n
ade quatef ory ourne edsa ndnoa dditi
ona lha r
dwa r
ei sr equired.Di ska ccessi s
discussedi ns everalc ha pte rs,buti sot herwi senotac onc e
r nt hroug houtt he
rema inde roft hebook .
Onewa rningbe forey ous tartintot hes ubje c
tofa s s
embl y-lang uagepr o-
grammi ng .Aswi tha nynont riv i
ale ndea v
or ,ma nype oples el
lthe ms elvesshor t
I
ntr
oduc
ti
on x
ix

beca useofwha tIc allthei nstante xpe r tmy th.Howma nype oplehe ars ome one
playapi anowe l
l,a nds ay,“ My ,wha tabe a utifult hing .It hinkI ’
llg e tonea nd
learnhowt opl aymy self!”⇢e ythe ns pendas ubs tant i
ala mountofmone y,s i
t
down, a ndpr es saf e wk eys.S urpr ise!Tot he irg rea tdi sappoi ntme nt , t
heMoon-
li
ghtS ona tadoe snotma g i
c al
lySow f rom t heirFng ers!⇢e yus ua ll
yt he n
becomei mme diatelydi scour ageda ndne verpur suet hea reaf ur ther,t urni ng
some t
hi ngt ha tc oul dg iv ethe mt re me ndouspl e asur eint oa ne xpe ns iveme ansof
suppor tforaSowe rv ase .
I’
v es eent hi ss amee Lecti na l
mos te ve rya re aofhuma na c t
ivity. Ifwha ty ou
wa ntedwa st heMoonl ightS ona ta,ar ecor dwi llpr oduc et hes oundy oude sire.
Peopl ek nowt ha ti ttak est al
e nt( talent=9 9%pr ac t
ic e=9 9%t i
me )t opl a ywe l
l,
buta r
et hendi s appoi nte dwhe nt he yc an’ts itdowna ndpe rf
or ml ikea ne xpe r
t
imme di atel
y .
Oneoft heg r eats ecretst ol ear ni nga ny thingi st obes a ti
sFe dwi thmi nor
learnings teps .Pl ayingt heAppl ei si nma nywa ysmuc he asiertha nl ear ningt o
playapi ano,buty ous houl ds t
illnote xpe ctt os itdowna ndwr itet hewor ld’s
greates tda taba sei ny ourFr ste veni ng .
Sety our se lfsomes impl ea nda c hie vableg oa ls
. Ca ny oumov eoneby tef rom
oneme mor yl oc ationt oa nothe r?I fy ouc any ou’ rewe llony ourwa yt oma ste r
-
ingpr og rammi ng .Myf e e
lingi st ha tv irtua llya ny onec a nbe c
omebe t
t ert ha n
eightyt oni ne type rcentofhi sfellowc i
tizensi na nya reas impl ybe caus ee ighty
toni ne type rc entoft heot he rpe opl ea ren’twi ll
ingori nc li
ne dtos pe ndt hene c-
essaryt imet ol ea rnt hes kil
l.Re a chi ngt het opni ne ty-ninepe rce nti sc e rtainly
diWc ul t,butni ne ty-Fv epe rcenti ss ur pr ising lye asy.
⇢i sbooki swr it
te nwi tht hei nt e ntionofpr ov i
di ngt hos esimpl ea c hie vable
ste
ps .Ands ur pris i
ng lye noug h,byt het i
mey ouFni sht hisbooky ouwi llha ve
writtenas impl eda taba seofs orts,a longwi ths omes oundr outine s,s omepr o-
gramst ha tus epa ddl esa ndt hedi sk, andaf e wot herni 4iesa swe ll!
Soha ngi nt he rea nddon’ te xpe c tt obea ne xpe rtonpa geFv e .Iwi l
l g ua r
a n-
teet ha tbypa g eonehundr e dy ouma ye vens ur prisey ourse l
fa st ohow e asy
assembl y-lang ua g epr og rammi ngr ea l
lyi s.
OneFna lnot e.I’
dl iketot ha nkAl Tomme r vi
kf orhi stre me ndoushe lpa nd
suppor tint hi spr ojecta sbot he di tora ndf riend,a ndGr egVos swhopr ov i
de d
ma nyi ns ig
ht fuls ugge stionsi nt ra ns for mi ngt hemont hlys eriesi nt ot hebook .
AlsoEr icGoe zf orhi se ncour ageme ntt one v era cc eptl esstha nthebe st, andhi s
att
e ntive( i
fnote nthus iastic)listeningt omyv ariouspl ansov e rthey ear s
.
Las tbutnotl eastmyt ha nk sa nds i
nc e ret houg htsofa ppre ciationt ot he
ma nype oplet ha tha v es har e
di nmyowne xpe rienc esi nc omput ingov ert hel a s
t
fewy ears .Whe the rthe ywe rer ea de rsoft hec ol umn,us e r
sofmypr og ra ms ,or
thewe a lthofne wf r
ie ndst ha tha vee ntere dmyl ifev iat heAppl e,the yha ve
ma dea llmye Lor tsmor et ha nwor thwhi l
ea ndbr oug htr ewa rdsbe y onda nys im-
plee conomi cg a i
nsofa nor dina ryj ob.
x
x As
sembl
yLi
nes

Alasfora ny
onewhot hi
nk sthatcomputer
sleadtoalossofthehuma ni
sti
c
aspect
sofl ife
.⇢e yne edonl ylookt otheamazingcommuni tythathasbeen
drawnt ogetherfrom allpa r
tsoft hewor l
dbytheAppl etoseethatfr
iends
hip
andhuma nc reati
v i
tywillalwaysout shi
nethesimpletool
sweus etoexpres
s
oursel
ves.
Mywi s
hf ory ou,dearreader,isthatyourec
eiveasmuc henjoymentfr
om
theAppl eandpr ogrammi ngasIha v
e.

RogerWagner
Sant
ee,
Ca l
if
orni
a
December1,198
1
1
1
. Ap
ple
’sAr
chi
tec
tur
e
Oc
tob
er1
980

⇢eFr stare
atoconsideristheg e
ne r
alstruct
ureoftheAppleitsel
f.Tohelp
v
isua
li
z ewhat’
sgoi
ngoni nthere,whynott ak ealookinsi
de.⇢a t’sri
ght–rip
t
hecoveroLa ndse
ewha t’sinthere!Don’tb etimid–getyournos eri
ghtdown
i
nthereandseewhatyous hel
ledoutallthoseha r
d-ear
nedbuc k
sfor.
Provi
dingyouhaven’
tg ott
enc arr
iedawa yindismemb e
ringyourAppl e
,the
i
nnerworkingss
houldappe a
rsome wha tli
kethoseinthephotobelow.

⇢ema i
ni t
emsofi nter
esta r
ethe6 502mi cr
oproc es
s or(A)a ndthebank sof
me morychips(B).Ifyou’r
enota ne lec
troni
c swhiz,itreallydoesn’
tma tt
er.You
cantakeita sade vi
ceofma gicfora l
litma tt
ers.⇢eme mor ychi
psha vet he
capabil
it
yofs t
oringthousandsofi ndivi
dualnumbe rv alue sandthe6 5
02s uper-
vis
esthea c
ti
v i
ti
estherei
n. Alltherestofthee l
ect
roni cde bri
swi t
hinissupplied
onlytosupporttheme mor ya ndthe6 502.⇢ec ircuitsallowy outoseedisplays
ofthisdataont hescreen,andpe rmitthec omput ertowa tchthekeyboardf or
youracti
ons.

]
2 As
sembl
yLi
nes

⇢es cre
enandkeyboar
da r
erathe
rse
condaryt
othena t
ureofthec
omputer
andareprovi
dedonl
yt oma k
ey oubuythethi
ng.AsfarastheAppleisc
on-
cer
ned,i
tcouldt
al
ktoi t
sel
fperf
ect
lywel
lwit
houteit
herthescre
enorthekey-
boar
d.

6502Ope
rat
ion
Sohowdoe si twor k?⇢ehe artoft hes y s
temi st he6 502mi c r
opr ocessor.
⇢i sde viceope ratesbys canningt hroug hag ivenr a ng eofme mor ya ddresses.At
eachl ocation, itFndss omepa rt
icularv al
ue.De pendi ngonwha titFnds , i
tex e-
cutesag iv enope rati
on. ⇢i sopera t
ionc ouldbea ddi ngs omenumbe rs,stori
nga
numbe rs ome whe re,ora nyofav arietyofot he rta sks.⇢e seinterpret
edv a
lue s
areo4e nc al
ledo pcodes.
Int heol dda ys,prog ramme rswoul dplyt heirtr adebyl oadinge achopc ode ,
onea tat ime ,int osucce ss
iveme mor yl ocati
ons .A4e rawhi l
e,some onei nvente d
ane asierwa y, usingas o4wa redevicet ointerprets hor tabbr e
viatedwor dsc al
le d
mne mo ni cs
.A mne moni cisa nya bbr e
viatedc omma nd orc odewor dt ha t
soundss ome wha tlikethewor ditstandsf or,suc ha sSTXf orS ToreX. ⇢ec om-
puterwoul dt henFg ur eoutwhi c hv aluestous ea nds upe rvis
et hestor i
ngof
thes
ev aluesi nc ons ecutiveme mor yl ocati
ons .⇢i swonde riswha tisg enerally
cal
leda na ssembl er.Ita l
lowsust oi nteractwi ththec omput e
ri namor ena t
ur al
way. Inf act,BAS ICi t
se l
fcanbet houg htofa sa ne xtre mec aseofthea ssembl er.
Wej us tus ewor dslikePRINTa ndINPUTt ode scribeawhol esetoftheope rat
ions
neede dtoa ccompl ishourde sir
eda cti
on.
Ins omewa ys,assembl ylanguag eise vene asiert hanBAS IC. ⇢e rea reonl y
F4y-Fv ec omma ndst ol earn,asoppos edt omor et ha nonehundr edinBAS IC.
Ma chinec oder unsv er yfastandg enerall
yi smor ec ompa cti nthea mountof
me mor yne ede dt oc arryoutag i
v e
nope rati
on.⇢e sea tt
ributesope nupma ny
possibili
tiesforpr ogramst ha twoulde it
herr unt oos lowlyort akeupt oomuc h
room i nBAS IC.

Me
mor
yLoc
ati
ons
Probablythemostunfa
mi l
iarpar
tofde ali
ngwi t
ht heAppleinr e
gardto
machine-
leveloper
ati
onsisthewa ya ddress
esa nd numbersing e
ne r
ala r
e
t
reat
ed.Unl es
syouleadanunusual
lycha r
me dli
fe,a
ts omepointi
ny ourdeal-
i
ngswi t
hy ourAppleyouha
veha dita
br uptl
ystopwha titwa
sdoingands how
yousomethingli
kethi
s:
8BF2- A=03 X=9D Y=00 P=36 S=F2
1
.Appl
e’
sAr
chi
te
ctur
e 3

⇢i soc cur swhe ns omema c hi


ne -levelproc e s
ss udde nlyenc ount er
sabr eak
initsope ra tion, usuallyf rom a nunwa ntedmodi Fca t
ionofme mor y
. Beli
e veitor
not ,t heAppl ei sa ctua ll
yt ryingt ot el
luss ome thinghe r
e.Unf ortunately,it’
s
rathe rl i
kebe ingat our i
s tandha v i
ngs ome ones hout , “Alaet
eq uetbeideggen!”a t
you. 1I tdoe sn’ tme a
nmuc hunl e ssyouk nowt hel i
ng o, sotospe ak..
.
Wha tha sha ppe nedi sthatt heAppl eha se nc ounte r
edt hebr ea kweme n-
tione da nd, int hepr oc essofr ec overing, haspr ov i
de duswi t
hs omei nforma tion
ast owhe ret hebr eakoc curreda ndwha tt hes tatusoft hec omput erwa sa tthat
cruc ialmome nt.⇢eme ssagei sra t
he rl iket hel astc r
ypti
cwor dsf rom t he
recent lyde pa rted.
⇢el e4mos tpa rtoft heme ss
ag eisofg re atimpor ta
nce.⇢i si swhe ret he
br e
a ki nt heope r
ati
onoc curre d.J ustwha tdoweme anbyt hewor dwhe r?
e
Re me mbe ra lltha tconc erna boutwhe t
he ryouha vea1 6K,32 K,or4 8KAppl e
?
⇢ec onc e rn wa sa boutt henumbe rofus a bleme mor yl ocati
onsi ny our
ma c hine .⇢i side abe come sc l
e arerthroug ht heus eofame mo ryma p,sucha s
theones hownbe low.
Ins i
det heAppl ea rema nye lectronicuni tst ha tstorethenume ricalv a
lues
wee nt er.Bynumbe ringt he seuni ts,wea ssigne achoneauni quea ddress.⇢i s
wa ywec ans pe cifya nypa rti
cul aruni torme mor yl ocati
on,e it
hert oi nquire
abouti tsc ont ent sortoa lt
erthos econt entsbys t
or ingane wnumbe rt here.
Int heAppl et her ea reat otalof6 553 6oft he s
eme mor yl ocations,c al
led
by t
es .⇢ec ha rtg i
vesusawa yofg raphi call
yr epr esentinge a
c hpos sibl
es poti n
thec omput e r.
Whe nt hec omput ers howsusa na ddr ess,itdoe snotdoi tinawa ys imi l
ar
tot henumbe rsont hel e4oft heme mor yma p,butr at
herint hefashionoft he

1“
Wat
chwhe
rey
ou’
res
teppi
ngy
oune
rd!
”(i
nca
sey
ou’
renotf
ami
li
arwi
tht
hispa
rti
cu-
l
ardi
al
ect
.)

]
4 As
sembl
yLi
nes

onesontheri
ght
.Youma ywel
lre
markher
e:“
Idi
dn’
tknowBFFFwa
sanum-
ber
;its
oundsmorel
ikeawets
neak
er.
.
.”2

He
xade
cima
lNot
ati
on
Tounde rst
andt hisnot at
ion,let’
ss e
ehowt he6 5
0 2count s
.Ifwepl a
ceour
by t
ea ttheFr s
tavail
ableloc at
ion,itsaddressis$0.⇢edol lars i
gnisusedinthis
ca s
et os how thatwea renotc ountinginourf amili
arde ci
ma lnotati
on,but
ratherinhe x a
decima l(basesix t
een)not a
tion,usuall
yc al
le
dhe x,whichishow
thec omput erdis
playsa nda cc
e pt
sda taattheMoni t
orl e
vel.
A4e rby te$0,succe s
sivelocati
onsa relabel
edint heusua lpatt
ernupt o$9.
Att hispointthecomput erusesthec haracter
sAt hroughFf orthene xtsi
xloca-
tions.⇢el ocat
ionr ighta 4er$F is$10.⇢i sisnottobec onfusedwithten.It
representsthede cima lnumbe rsixteen.⇢epa tte
rnr epeatsitsel
fasi nusual
count i
ngwi th:
$10, $11, $12, $13... $19, $1A, $1B... $1E, $1F, $20

Trynott ol ett hiswa yofc ount ingups e ty ou. ⇢epa tte


rni nwhi chape rson
(orma chine )c ount sisr athera rbi trary,a nds houl dbej udg edonl yonwhe therit
ma ke sac compl ishingat aske asierornot . ⇢ebi ggestpr oblemf ormos tpe opleis
mor eama tterofha vingbe entr aine dt ous ena me slikeo nehundr e dwhe nt hey
seet henume r als1 00 .Howma nyi temst hisc orrespondst or eal
lyde pendsmor e
ont hec onv ent ionswea greet ous et ha nona nyc osmi cde cree.Toa i
di ny our
escapef rom y ourpos si
blyna rrowv iewofc ount ing ,youma ywi s
ht or eadt he
diver s
iona rys t orya tt hee ndoft hi sc hapt er. Ina nye v e
nt, i
twi l
lbes uWc i
e ntfor
ourpur pos est ounde rstandt hat$1Fi sa sle gitima teanumbe ra s3 1
.
⇢ehe xnumbe r$FF( 2 55)ist hel ar
g estv alueas inglebytec anhol d.Abl ock
of2 56by t
e s(f ori nstanc e$00t o$FF)i so4e nc all
edap ageofme mor y.Int heFg -
urea tright ,a llthea ddr essesfr om $00 t o$FFa res howni nbl ock( b).Fourof
thesebl ock st og ethe r,asin( c),ma k eup1 Kofme mor y.Asy ouc ans ee,the rea r
e
actua l
ly1 ,02 4by tesin1 K.⇢usa4 8Kma c hinea ctua l
lyha s49 ,
15 2byte sofRAM
(Random Ac ce ssMe mor y).
Block( d)s howst heAppl e’
se ntirer ang ea gain. Ifyoudonotha veaf ull48 K
ofme mor y,t he nt hemi ssingr a ng ewi llj usta ppe art ohol dac ons tantv alue
(usua ll
y$FF) , andy ouwi llnotbea blet os tor ea nypa rti
cularv al
uet here .
⇢er a ng ef rom $C000t o$FFFF,a na ddi tiona l1 6K,i sallreser
v edf orha rd-
war e.⇢i sme a nst ha tanyda tast ore di nt hisr angei sofape rma nentna t
ur ea nd
cannotbea lt
e re dbyt heus er.Somea rea sa rea ct
ua llyaphy sic
a lconne ctiont o
thi
ng sli
k et hes peak erorg ames wi tche s.Ot he r
s ,l
ik e$E000t o$FFFFa reFl ledin
byt hec hipsi nt hema chinec all
edROMs .

2[
JohnGr
uve
r]Or
,the
seda
ys,
may
be“
Bes
tFr
iendsForFor
eve
r..
.

1
.Appl
e’
sAr
chi
te
ctur
e 5

ROM s t
andsf orRe adOnl yMe mor y.⇢e sec hi
pshol dt hema chine-lan-
gua ger outi
nestha tma k
eupe ithe rAppl eso4BAS ICorI nt egerBAS IC, depe nd-
ingonwhe thery ouha vea nAppl eIIPl usort hes tanda rdmode l.Oneoft he
chipsi stheMoni tor, whichiswha tinit
ial
ize stheAppl ewhe niti sFrstt urne don
soy ouc a
ntalktoi t.
⇢eMoni t
orc a
nbet houg htofa sas impl es upe rvisorpr og r
a mt ha tk eeps
theAppl efuncti
oni nga tar at
he rpr imit
iv eleve lofi ntell
ige nce.I tha ndlesba si
c
inputa ndout putf ort hecomput e r,anda llowsaf ews impl ec omma ndsr e l
ating
tos uc ht hi
ngsa se nteri
ng,listi
ng ,ormov i
ngbl ocksofme mor ywi t
hi nt he
Appl e.Don’ tbef ool edthough.⇢ea mountofc oder equi redt odoj ustt hese
thing sisnottri
v i
a l
,a ndina dditionpr ovide suswi thar e
a dy -ma demi ni-library
ofr outinesthatwec anc al
lfrom ourownpr og rams ,aswi llbes hownl ateri n
thisbook .
Appl eprovidesa ne xcell
entdi s
cussionoft heMoni tora ndi tsc omma nds
andope r
ati
onwi t
hint heAp pleIIRe f
erenceMa nua ,c
l urre ntlys uppl i
e dwi tha l
l
newAppl es
.Youma ywi shtoc onsultthisify oua reuns ureoft heg e
ne ral wayi n
whi cht heMoni torisa ccess
eda ndus ed.Nowt ha tbr e
a kme ssag eshoul dha veat
l
e astal i
ttl
eme aning .
8BF2- A=03 X=9D Y=00 P=36 S=F2

]
6 As
sembl
yLi
nes

⇢e$8BF2i sana ddr e


ssinme mory
.⇢edi splayindic
ate
sthatt
hebreak
a
ctua
ll
yoc c
ur r
edatthea ddre
ssgiv
enminustwo($8BF272=$8BF0) .Forr
ea-
s
onsthataren’
tworthg oingintoher
e,theMoni
tora l
wa y
sprint
soutabreak
a
ddres
sinthisplus
-twof ashi
on.Whataboutt
herestoftheme s
sag
e?Consi
der
t
henextthr
eeitems
:
A=03 X=9D Y=00

⇢e6 5 02 ,ina dditiont obe i


nga blet oa ddr e
sst hev ar
iousme mor yloc a
tions
i
nt heAppl e ,ha sanumbe rofi nterna lreg i
ster.⇢e
s sea reuni t
sinsidet he6 50 2
i
tselftha tca ns t
or eag i
vennumbe rva lue,a ndthe ya reindividuall
ya ddr es
sa ble
i
nmuc ht hes amewa yme mor yis.⇢edi Lerencei sthatins t
eadofbe ingg i
vena
hexa dec i
ma l addr ess,theya rec a
ll
edt heAc cumul ator,t
heX- Regis
ter,andt heY-
Reg i
s t
er .Inoure rrorme ssage,wea rebe ingt ol
dt hes tat
usoft he s
et hreer egis-
ter
sa tt hebr e ak.3

⇢eFg ur ebe l
owi ll
ustrateswha twek nows of ar.⇢e6 502isami croproc es-
sorc hipt ha tha sthea bil
itytos canthr oug hag i
v enr a
ng eofme mory ,whi chwe
willg ene r
allys pec if
ybyus inghe xnot a ti
onf ort hea ddresses.Dependi ngont he
val
ue si tFndsi ne a chloca t
iona sitsca nst hrough, itwillpe r
formv ariousope ra-
ti
ons .Asa na dditionalf e
a t
uret oitsope rati
on, i
tha sanumbe rofinterna lregis-
ter
s ,spe c
iFc allyt heAc cumul a
tor,t heX- Register,theY- Regist
er.Me mor y-
rel
a t
e dope r ationsa rebe stdonebye nte ringt heMoni t
orl eveloftheAppl e(us u-
all
ywi thaCALL-151)a ndus ingthev a riousr outinesavailabletous.

3[
CT]⇢eFna
ltwoi
te
msa
ret
heS
tat
usRe
gis
ter“
P”a
ndt
heS
tac
kPoi
nte
r“S
”.
1
.Appl
e’
sAr
chi
te
ctur
e 7

I
t’
sCul
tur
e⇢ a
tCount
s
Ma nype opl eha ver ema rk edt hatourc hoi ceoft ena sanumbe rba sei s
rela
te dtot hef acttha tweha vete nFng ersonourha nds.Onec anonl yg ue sshow
adiLe rents etofc ircums tanceswoul dha vepr ofoundl ycha ngedourl ives .S pec u-
lat
ing ,forins t
a nce, onwhi cht woc omma ndme nt swoul dha vebe enomi tte dha d
weonl ye i
g htFng e rsi senoug ht ok eeponea wa k eatni ght.
Al ivinge xampl eoft hisa rbitraryna tur eofnumbe rba seswa sr ec ent l
y
broug htt ol i
ghtbyt hedi scove ryofal ong -l
os ttribel i
vingi nther emot ej ung les
ofS outhAme rica. Itwoul dse emt het ribeha dbe enisolatedfrom t her es toft he
wor ldfora tle ast10 ,00 0years. Ani nter esti
nga spe c
toft he i
rlif
ewa sahug epop-
ulat
ionofdog slivinga mongt hepe opl e.Inf act,dog ss oout numbe re
dt hepe o-
ple( sot os pea k)t ha tthepe opleha de v olvedac ountings yste
m ba sedont he
numbe rofl egsonadog ,asoppos edt oourmor er at
iona lbaseten.⇢e yc ount ed
inthee qui valentofba sefour.
Inc ount i
ng ,the ywoul dbehe ardt os ay,“ one ,two,t hree.
..
”S incet he yha d
neve rde velopedmor ethanf ours ymbol st oc ountwi th( 0,1,2,3)whe nt he yg ot
tothenumbe ra 4e rt hree,the ywr otei ta s1 0a ndc al
ledi tdoggy ,t
husc onFr m-
ingt hequa ntit
yi nt ermsofana turaluni tint he i
re nvironme nt.Cont inui ngt o
countt he ywoul ds ay ,“dogg y-one( 11 ),dog gy-two( 12),dog gy-t
hr e
e( 13 )...

Att hispoi ntt he ywoul dwr i
tet hene xtnumbe ra s20a ndc a l
litt wog gy .A
similarpr oc e
dur ewa susedf or3 0.
20–t wog g y 30–t r oggy
21–t wog g y-one 31–t r oggy -
one
22–t wog g y-two 32–t r oggy -
two
23–t wog g y-thre e 33–t r oggy -
thr ee
Now, uponr eac hing3 3 ,thene xtnumbe rmus tag ai
nf orceanot herpos i
tion
inthenumbe rdi s
pl ay .
You’ r
epr oba bl ywonde r
ingwha tthe yc all
e di t
. ⇢edi gi
tsareofc our se1 0 0.
Oh, thena me ?Why , ofcour se,it’
so neho undr ed.

]
2
2
. ⇠eMo
nit
or
No
vemb
er1
980

Ex
plor
ingt
heMoni
tor
I
tispossibl
etopr ogramthec omput ermanua l
lybyent er
ingnumbe rsoneat
atimei ntos ucces
siveme mor yl ocat
ions.A program oft hissor tiscall
eda
ma c
hinelanguageprog r
am becaus ethe6502candi re
ctl
yr unthec odedpr ogr
am
st
eps.Huma ns,howe ve
r,Fndt hi stypeofda t
adi Wcultt oreada nda remor e
li
kel
ytoma kemistakeswhilewor kingwithit
.
Amor ec onvenie
ntme thodofpr ogra
mmi ngistoa s
signs omek i
ndofc ode
wordtoe achv al
ue.⇢ec ompute rwilltr
ansl
atethiswordi nt
ot hec orr
ectnum-
bertostoreinme mor y.⇢i stranslat
ionisdonebya nassemb l
er,andpr ograms
ente
redordi spl
ayedinthisma nne rarecal
ledass
e mbly-
languagepr ograms.
Asa ne xample,let’
slooka ts omeda tawi t
hiny ourAppl e,Frsti nthe
machine-l
ang uageformata ndthe ni nthea s
sembl y
-la
ng uageforma t.Firs
twe
mustentertheMoni tor.Typein:
CALL -151

⇢i
sshoul
dgi
vey
oua
nas
ter
is
k(*)asapr
ompt
.Nowt
ypei
n:
F800.F825

⇢i
ste
ll
stheMoni
torwewa
ntt
oex
ami
net
her
ang
eofme
mor
yfr
om $F800t
o
$F825.
⇢eg
ene
ral
synt
axoft
hec
omma
ndi
s:
<start address>.<end address>

t
heperi
odbe
ingusedtosepa
ratet
hetwoval
ues
.
Uponhi
tt
ing<RETURN>youshoul
dge
tthefol
lowi
ngda
ta:
F800- 4A 08 20 47 F8 28 A9 0F
F808- 90 02 69 E0 85 2E B1 26
F810- 45 30 25 2E 51 26 91 26
F818- 60 20 00 F8 C4 2C B0 11
F820- C8 20 0E F8 90 F6
*

]
1
0 As
sembl
yLi
nes

⇢er a
ngeIhavepic
kedist
hev er
ybe gi
nningoft
heMonitorROM.⇢e
dat
aherec
anbedi
rect
lyre
adbythe6502,butisve
rydi
Wcul
tformosthuma
ns
tomak
emuc hs
ens
eof.⇢isisma
chinela
ng ua
ge.

Di
sas
sembl
y
Nowt
ypei
n:
F800L

⇢iste
ll
stheMoni t
ort
ogiveusadi
sas
sembl
yoft
hene
xtt
went
yins
truc
ti
ons
,
s
tar
ti
ngat$F800.
⇢es y
nta
xherei
s:
<start address>L

Todis
ass
embleme anstoreversetheproc
esswetal
kedaboutea
rli
er,
tak
ing
e
achnumberval
uea ndtra
nsla
tingitintot
heappropr
iat
ecodeword.
A4erhi
tt
ing<RETURN>yous houldget
:
F800- 4A LSR
F801- 08 PHP
F802- 20 47 F8 JSR $F847
F805- 28 PLP
F806- A9 0F LDA #$0F
F808- 90 02 BCC $F80C
F80A- 69 E0 ADC #$E0
F80C- 85 2E STA $2E
F80E- B1 26 LDA ($26),Y
F810- 45 30 EOR $30
F812- 25 2E AND $2E
F814- 51 26 EOR ($26),Y
F816- 91 26 STA ($26),Y
F818- 60 RTS
F819- 20 00 F8 JSR $F800
F81C- C4 2C CPY $2C
F81E- B0 11 BCS $F831
F820- C8 INY
F821- 20 0E F8 JSR $F80E
F824- 90 F6 BCC $F81C

⇢i sisadi sass
embl edl i
sting.Al thoughi tprobablydoesn’
tdoal otfory ou
rightnow, Ithinkit’
sobv ioustha titisa tl
eastmor edist
inct
ive.
Let’slooka tital i
ttl
emor ec l
os el
y.InBAS IC,li
nenumbe r
sa r
eus edt o
beg i
nea chs e
tofs ta
teme nts.⇢e y’repa rt
icular
lyha ndywheny ouwa nttodoa
GOTO o rGOSUBt os omeot herpa rtoft hepr ogram.I nass
embl ylanguage,the
addressesthems el
vestak ethepl aceoft helinenumbe r
s.Inourexample,thec ol
-
umnofnumbe r
sont hef arle 4a ret hea ddres
sesa twhiche ac
hope rati
oni s
found.Tot herightofe acha ddressa reonet othreehe xval
ue s
,whicha r
enum-
berv aluess tore
di ns uccessi
v ea ddr ess
es.⇢e sea retheopcodeswi t
ht hei
r
accompa ny i
ngope rands.
2
.⇢eMoni
tor 1
1

At$F802, fori ns tanc e, i


st heopc ode$20. Reme mbe r,thedol lars i
g ni suse d
tos howwea reus i
ngba ses ixt een.$20i st heopc odef ort hec omma ndJSR.Al l
mne moni csa r
ema deupoft hr eel ett
ers.I nt hisc ase,JSRs tandsf orJ umpt o
SubRout i
nea ndi srathe rl i
k eaGOSUBi nBAS I
C. ⇢ene x ttwonumbe rs,$47a nd
$F8, compr i
s et heope rand,t ha tis,thenumbe rt ha ttheopc odei st ous ei ni ts
ope ration. Tot her ightwes eet hatt he s
enumbe rsg i
ve$F847a st heobj ectoft he,
JSR1.Co nt i
nui ngwi thoura na l
og y,wha twoul dbeaGOSUB 1000 i nBAS IC
appe arsa saJSR $F847 i na sse mbl yl ang ua ge.⇢ec omma ndJSR $F847 wi l
l
jumpt ot hes ubr outinea t$F847a ndr eturnwhe ndone .
You’ v ej ustl earne dy ourFr stwor dofa ssembl yl ang uag e: JSR!L ook i
ng
throug ht hel is
ting ,y ouc ans ees eve raloft he se.⇢eFr stoneg oe st os omer ou-
tineout sidet hel i
sti
ng .Wha ta boutt heot he rtwoJSRc omma nds ?Yous houl d
bea bletos e
et ha tthe yr efer enc er outine swi thint hel is
ting .⇢es econde nte r
sa t
$F800, thet hirda t$F80E.
InBAS IC, aGOSUBe vent ua llye ndswi t
haRETURN. ⇢eJSRha sa na na logous
count erpar t
. Look i
nga tt hee ntrypoi nta t$F80Ea ndwha tf ollows , ca ny ouFnd
any thingt hatlook slik eitmi g htbet hee qui v a
lentofaRETURN?Ta k et het imet o
Fndi tify ouc anbe f
or er eadi ngon.
Ify oupi ck edt heRTS,y ou’ rer ight,RTSs tandsf orr etur nf rom s ubrout ine.
Aswi thaRETURN, whe nt hepr og ramr eache stheRTS, i
tr eturnst owhe rei torigi-
na l
lyc amef rom.Enc ount eringt heRTS a t$F818,pr ogr ame xe cut i
onwoul d
resumea t$F824, i
fe nt rywa sf rom t heJSR$F80Ea t$F821.
Youmi g htnot i
c et ha ta lmos ta llma chinec odebl oc k st ha ty ouma yha v e
us eda longwi t
hBAS I Cpr og ra ms ,suc ha st oner out ines ,usua llye ndwi tha$60
ast hel astby te.⇢i sist heopc odef orRTS. Ina l
mos tanya ssembl y-la ng uag epr o-
gra my ouwr ite, youmus te ndwi tha nRTS. ⇢i si sbe caus e,tot hec omput e rasa
whol e,yourpr og rami sat empor arys ubrout ineofi t
sov e rallope r
a tion.
Whe ny ourpr og rame nds ,theRTSl etst heAppl ere turnt oi t
sor ig i
na lope r-
ationsofs canni ngt hek eyboa rda nds uc h.Whe ny oudoaCALL 768 f r
om
BAS IC, fore x a
mpl e,y oua ree ss entiall
ydoi ngaJSRt ot ha tma chi ner outine .⇢e
76 8i sthede c i
ma lva luef ort hea ddr e s
soft hes tartoft her out ine ,e qui valentt o
$300i nhe xade cima l.Att hee ndoft ha tr outine ,theRTSr eturnsy out oy our
BAS ICpr ogra mt oleti tc ontinuewi tht hene xtstateme nt .
1Not
ic
etha
titt
ake
stwoby
test
ost
oret
hev
aluef
ora
naddr
ess
.Fore
xampl
e,f
ort
he
address$F847,t hev al
ue“ F8”i sstoredi noneby te,and“ 47”ina nother.Readinga n
addressi sgeneral
lyama t
terofme nta
llyc ombi ningt hetwoby tes
.⇢eby terepres
ent i
ng
t
hel e4-ha ndpor ti
onoft henumbe riso4e nc al
ledt hehigh-orderby t
e;theby t
er epre
-
s
e nti
ngt her ig
ht-ha ndporti
oni sc al
le
dt hel ow- orderby te
.Itisimportanttor e
ali
zet ha
t
t
het woby te
st hatma keupa na ddressar ea l
mos talwa ysrevers
edinr egar
dstowha tyou
mi g
htnor ma l
lye xpec.⇢a
t tistos ayt ha tina na ddressby t
e-pai
r,thelow- orderby t
e
al
wa ysc ome sFrst,imme diate
lyf oll
owe dbyt hehi gh-orderbyte.⇢i sme ansthatwhe n
examini ngr awme mory,youmus tme nta l
lyr ever
set heby tetodeterminet headdr es
s
s
tored. Fortuna t
elywhe nusingt he“L”c omma nd,thedi sa
ssemblerdoe st
hisforyou.

]
3
3
. As
semb
ler
s
De
cemb
er1
980

⇢eMi
ni-
Ass
embl
er
Ime ntione de a
rliertha ttheba s i
cpr i
nc ipl eoftheAppl eisi t
sa bil
itytos can
throug har ang eofme mor ya nde xec utedi Le rentope rat
ionsde pe ndingonwha t
nume ri
cv a luesi tFndsa tea chl oca ti
on,ora ddr e
ss.Inst
e adoft e dious l
yl oading
eachl ocationbyha ndwi thmunda nenumbe r stocr e
ateapr og ram, ana ssembl e
r
isus edt ot ra nslat
ea bbr eviatedc ode wor ds,c al
ledmne moni cs,int ot hepr oper
numbe rva lue stobes tore dinme mor y .
⇢et y pe sofa ss
e mbl ersa vailablea requi tedi ve
rse,a ndr ang ef rom t he
Mini -Assembl erpr esenti na nAppl ewi thI nte ge
rBAS I
C( ort heMunc h- A-Bug
pack age)tos ophi s
ticatede ditor/ass embl er slik eMe rln.
i
Fornow,we ’l
lus etheMi ni -
As sembl e rt ot r
yas hor tprog ram.I fy ouha ve
anAppl eI I,a nAppl eI IPl uswi thal ang ua gec ard,ora nAppl e/ /e,theMi ni
-
Asse mbl e
ri sa vail
ablepr ovide dt ha ty oue nt ert heMoni torf rom I nt e
g erBAS IC.
Ina nyc ase, you’llwa nttog etamor ec ompl etea ssemblert odoa nyr e
a lprogram
writing.
Sta
rtingwi thc hapte rfour ,I’lla ssumey ouha v
ea na ssembl er
,a ndha ve
l
e a
r neda tle aste nougha boutope ra ti
ngi ttoe nterapr ogram. S
inc et heonl ytwo
comma ndsweha vea tthispoi nta reJSRa ndRTS,ourr outinewi llbev e r
ys im-
ple.Int heMoni tora t$FBDDi sar out inet ha tbe epsthespe ake r
. Ourr out i
newi ll
doaJSRt ot ha tsubrout i
ne ,the nr etur nt oBAS ICv iaanRTSa tthee nd.
Toe nt ert hepr ogram us ingt heMi ni -As sembl er
,followt he ses teps:1From
Integ e
rBAS IC, entertheMoni torwi thaCALL-151. ⇢e nt ypei n:
F666G

F666istheaddres
swheretheMini-
Ass
embl
erprog
ramsta
rts
.Gt
el
lst
heMoni-
tortoexec
utetheprogr
amt he
re.
Youcant
hinkofGasg
o;it
sBASI
Cequi
val
ent
isRUN.
⇢eg e
neralsy
ntaxi
s:

1I
fyoudonotha
vet
heMi
ni-
Ass
embl
era
vai
la
ble
,youc
ane
nte
rthes
ameda
tai
nto
memor ybye
nte
ringtheMonitorandt y
pingin:300: 20 DD FB 60<RETURN>.
Rej
oinusatt
he300Lma rkonthene xtpage.
Ify
ouha vea
nAppl ewit
ha6 5
C0 2,theme moryaddr
esseshavechanged.Youcane
nte
r
t
heMi ni-
Ass
emblerbytypi
ng“!”,
a ndexitt
heasse
mblerbyhitti
ng<RETURN>.

]
1
4 As
sembl
yLi
nes

<start address>G

⇢epr ompts
houl
dc ha
ngetoa
ne xc
lamati
onma r
k(!).Tous
etheMini-
Assem-
bl
er,
youmustfol
lowaba si
cpa
tte
rnofinput.
Seepage49int
henewestAppl
eII
Re
fer
enceManualforathor
oug
hde s
cri
pti
onofthi
s.Fornow,t
houg
h,enter
:
!300: JSR FBDD<RETURN>

⇢eAppl
ewi
ll
imme
dia
tel
yre
wri
tet
hisa
s:
0300- 20 DD FB JSR $FBDD

⇢einputsynta
xistoentertheaddressa
twhichtos ta
r tt
hepr ogra
mf ol
-
l
owedbyacol
ona ndaspace
,thenenterthemnemonic
,a notherspace,a
ndthen
t
heoper
and,i
nthiscas
etheaddre
ssfortheJSRtoj
umpt o.Ne xtt
y pein:
! RTS<RETURN>

whi
chwi
ll
ber
ewr
it
tena
s:
0303- 60 RTS

Besuretoenterone<SPACE>bef
oretheRTS.Whatt
heass
emble
rhasdonei
s
t
ota
keourmne moni ci
nputandtra
nslat
eitint
othenumer
icopc
odesa
ndope r
-
a
ndsofa
ctualmachinel
anguage.
Nowt ypein:
!$FF59G

⇢iswil
lex
itt
heMini
-Asse
mbl e
r,gi
vingyoubackt
heas
ter
iskpr
ompt(
*)oft
he
Moni
tor
.Youcannowli
styourprog
ram byty
pingi
n:
300L

⇢eFr
stt
wol
ine
sofy
ourl
is
ti
ngs
houl
dbe
:
0300- 20 DD FB JSR $FBDD
0303- 60 RTS

Wha tfoll
owsa4er$303ismor eorlessra
ndom a nddoe Le
snota ctthecodewe
ha
vet ypedin.Whenr un,thisprogr
am willjumpt ot
hebeeprout
ineat$FBDD.
Atthee ndofthatrout
ineisa nRTSthatwillre
t urnustoourprogr
a mat$303.
⇢ eRTSt herewil
lthendoaFna lr
etur
nf rom theprogrambacktoe i
thert
he
MonitororBAS I
Cde pendi
ngonwhe rewec al
litfrom.
From theMonitortypein:
300G

⇢espea
kers
houldbeepandyouwi
llg
ett
hea
ste
ris
kpr
omptba
ck.
Nowg
oba
ck
i
ntoBASI
Cwitha<CTRL>B.
Typei
n:
CALL 768
3
.As
sembl
ers 1
5

⇢es
pea
kers
houl
dag
ainbe
epa
ndt
heng
ivey
out
heBAS
ICpr
omptba
ck.
CALL
768s
houl
dwor
kfr
om I
nte
gerorAppl
eso4.
Aslongastheprogramsar
enotveryinvol
ved,t
heMi ni
-Ass
embleri
shandy
f
orwri
ti
ngquickrouti
ne s
.Acomplet
etableofrout
inesi
ntheMoni t
orappea
rs
i
nAppendi
xD a tthee ndofthebook.Trytowr i
teyourownJSRst ooneor
mor
eofthe
serouti
nes.Youmighteve
nt r
ydoingsever
ali
nar owforf
un.

As
sembl
ers
Now l et’
sl ooka tt heope rati
onofamor et ypic
a lassembl er.⇢i se xample
assume syou’ r
eus inga na sse
mbl ersimi l
art otheone sme ntione dint heint r
o-
duc t
ion.Ify ouha veadi Lerenta s
se mblert hatgivesy oudi Le rentr e
sults
,y ou
ma yha vetoc onsulty ourope rati
ngma nua lforthepr ope rproc eduresfore nter
-
ingsourcel i
stings.
Be f
orepr esentingthel ist
ing,I’dliketoc lar
ifytwoc ommonl yus edt e
rmsi n
assembly-l
a ngua gepr ogrammi ng ,sourcecodea ndo bjectcode.S ourcec odeisthe
Eng l
ish-l
iket exty oue nte rintothea ssembl e
r.⇢i st ex thast hea dva nta
g eof
beinge asi
lyr eadable,andma yincludewhol es e
nt e
nc esorpa ragraphsofc om-
me ntsverys i
mi lartoREM- typestateme ntsfoundi nBAS IC. Sour cecodei s,
how-
ever,notdi rec
tlye xecutablebyt he6 502. I
tsimpl ydoe snotunde rst
a ndEng lis
h-
li
ket ext
.Asme nti
one de arli
er,t
he6 502’spreferred(andi nfa ctonlya cceptable
)
dietisone-t ot hree-bytec hunksofme mor yi nwhi chs implea nduna mbi guous
numbe r
sar efound.
⇢ea ssembl ertak e
sthi stex
ta ndpr oduc esthepur enume ricda ta,cal
ledthe
objectcode ,whichi sdirectlyexecutablebyt he6 502.
Nowt helisti
ng :

Obj
ectCode Sour
ceCode
1 ********************************
2 * AL03-SAMPLE PROGRAM *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 BELL EQU $FBDD
9 *
0300: 20 DD FB 10 START JSR BELL ; RING BELL
0303: 60 11 END RTS ; RETURN
0304: 66 12 CHK

Tother
ightsi
deoft
hel
is
ti
ngi
swhatisg
e ne
ral
lyc a
ll
edthesour
cecode
.
⇢i
sist
heprogra
m,code
dusi
ngmnemoni
csandv a
riousnamesorlabe
lsf
or

]
1
6 As
sembl
yLi
nes

diLerentpa rtsofy ourr outine .Ve ryf ewa ctuala ddr essesorv aluesa reus edi n
thesour c ecode .2

Tot hel e4i stheobj ectc ode .⇢i si swha ti sa ctua l


lyputi nme mor ya sthe
ma chinel ang ua gepr og ram.⇢eobj ectc odei swha tthec omput era c tuallyexe-
cutes;itisobv ious l
yr a therdi Wc ultt ounde rstand, atle a
stc ompa redt ot ryi
ngt o
unde rstandi twhe ny ouha vet hea dv ant ageoft hes our cecode . Beingmor eread-
il
ya blet ounde rstandt hec odi ngpl ac esg reat
e rimpor tanceonha vingt hes ource
li
sti
ngf orag iv enpr og ra ma nde xpla i
nswhyy ourAp p l
eI IRe fere nc eMa nua l
conta i
nsas our cel i
stingf ort heAppl eMoni t
or .S uc hl is
ting swe r ec ons idered
necessar yindoc ume nt ingas ys t
em whe ntheAppl ec ameout .
Howe ver,s our cel ist
ing sf orAppl eso4BAS IC, IntegerBAS I
C, andt heDi sk
Ope ratingS yst em( DOS )a remuc hha r dertoc omebya nda renotdi rectlydi s
-
tri
but edbyAppl eComput erCo. ,Inc .I ndepe nde ntlyc reateds ourc el isti
ng sfor
DOSa ndAppl eso4BAS ICha v ebe e npr eparedbyi ndi vi
dua lsnotdi r ectlya s
so-
ciat
edwi thAppl eComput erCo. ,I nc .a nda rec omme rci
allya vailable .⇢eDOS
3.3s ourc ecompi ledbyRa ndyHy dei sa vailablef rom La zerS ystems ,Inc .An
Appl eso4BAS ICs our c elistingi si nclude di nt heMe r
linAs s
emb l
erf rom S outh-
we s
te r
nDa t
aS y stems .
Mos ta s
se mbl ersdi splaybot ht heobj e c
tc odea ndt hes our cec odewhe nt he
ASM ( f
orAS se Mbl e)c omma ndi sus e d.Obj e cta nds ourcec odea re,howe ver,
usuallys avedt odi ska st wos epa ratea nddi st
inc tFl es .
Initiall
y ,l
et’sc ons iderjust
thesour c eli
sting .
⇢eFr stt hingt onot icei stha t,jus tlikeinBAS IC, wea gainha vel inenum-
bers.Ina sse
mbl yl ang ua ge,t houg h,thel i
nenumbe rsa res ol
e l
yf orus ewi t
ht he
progr a
me dit
or ,a nda r enotus eda ta lltor eferenc er out i
ne s.Ins ertingal ineis
donewi thas pe ciale ditorc omma nd,a nda l
lf ollowi ngl ine sa r
ea ut oma t
icall
y
renumbe redt oa ccommoda tet hene wl ine.
Ne x tnotic et hes ynt ax, orpr ope ror de r
ingoft hei nforma tion. Ge ne rallythe
syntaxc onsistsoft hr e eba sice l
e me nt s,or?e l
ds ,t oe a
c hl i
ne .⇢e s eFe l
dsa re
eit
he rde Fnedbyt he irpos itionont hel i
neor ,mor eo4e n,byde limi ter.A
s
deli
mi teri sac ha racterus edt os epa ra teoneFe l
df rom a not he r
.I nmos ta sse
m-
bler
s ,as pacei sus ed. Us ingt hi sconv ent i
on, youdon’ tha v etot abov e rtos ome
speciFcpos i
tionf ore a chFe ldont hel ine.Ins teady ouj us tma kesur ee achFe ld
issepara tedfrom t hea djacentonebyas pace.

2[
CT]Li
ne1
2cont
ainst
heCHKps
eudo-
opc
odet
hati
spr
ovi
dedbyt
heMe
rli
nMa
cro
Ass
emble
r.CHKinstr
uc t
st hea s
semblertoinser
tasing
lebyt
ec onta
ininga“ c
hecksum”
f
ortheenti
reprogram.I fyoua reusi
ngMe r
ln,y
i oucanusetheresult
ingchecks
um t o
ver
if
ythatyouhavetypedi ntheprogramcorrect
ly.
Forexa
mpl e,
forthispa
rti
cula
rpr o-
gra
my oushouldgetac he c
ksum of$66.Ifyoua r
enotusi
ngMe rlny
i oushouldignore
t
heCHKinstruc
ti
on.Inc asey ouarecuri
ous,thechecks
um iscomputedbype rf
orming
anexc
lus
iveORona l
loft heprogram by
tes.
3
.As
sembl
ers 1
7

⇢eFr stFe l
di sforal abe la ndi sopt i
ona l.Line s1 0a nd1 1,fore xampl e
,
eachha v eal abe ltha tappl ie stot ha tpoi nti nt her outine.Int hisc ase,t hela bel
STARTi ndica t
e swhe reweFr stbe gint hepr ogram;ENDi sthec leverlabe l usedf or
theFni sh.Youma ye v e
nr ec ogni zet hispr ograma st heoneweus edt obe e pt he
spea kere a r
lier.S omea ss embl e rsl i
mi tthenumbe rofc haractersus e di nt he
label.
Ast hepr og ram be come smor ec ompl ex,wec andot hee qui valentsofGOTO
andGOSUBbyus i
ngt hesel abe l
si ns teadofal i
nenumbe r.You’llnot i
cet hattodo
this,BELLh astobede Fne ds ome whe rei nt hel i
sting.S inceBELLdoe snotoc cur
asal abelwi thinourownpr ogr am( lines1 0a nd1 1),itisde Fne da tt hebe gin-
ningus ingt heEQU( EQUa ls)s t
ate me nt .⇢es tateme ntr eads:“BELLEQUa ls$FBDD. ”
⇢i swa y ,whe ne verweus et hel abe lBELL, thea ssembl e
rwi llautoma tica ll
ys etup
theJSRorwha teve rtothea ddr e ss$FBDD.
⇢es econdFe ldi st hec omma nd?e d,whi
l chi nc ludest heopc odea ndi t
s
ope rand.I nl ine1 0 ,theJSR i st heopc odea ndt heope randi sBELL.Nota l
l
opc ode swi l
l hav ea nope rand.
⇢et hirdFe ld, tother i
g ht, i
st hec omme nt?e d.
l Useoft hec omme ntFe l
di s
optiona la ndi sr es ervedf ora nyc omme nt sa boutt hel ist
ingy oumi g htwi s
ht o
ma k e( f
ore xampl e ,RINGBELL) .⇢es e mi coloni nt hes our c
ec odei sus eda st he
de l
imi terfort hec omme ntsFe ld. Comme ntsc ana lsobedonea tthev er ybe gin-
ningoft hel i
nebyus inga na st
e ri
ska st heREMa rkc ha ract
er.3 Asi nBAS IC,
every t
hi nga 4e rthea ste
r iski signor edbyt hea s
se mble r.
As sembl ersa lsoha vewha ta r es ome time sc al
ledps e
udoopc odesordi rec-
tives,l i
k eEQU.Al thoug hdi re
c tivesdonott rans l
a t
ei nto6 502c ode ,t he ya re
interpr etedbyt hea sse
mbl era cc ordingt oa s
signe dde Fni t
ionsa stheobj ec
tc ode
isassembl ed.
⇢e ya rec al
le ddi rectiv esbe c aus et he ydi rectthea ssemble rtope rf
or ma
spec i
Fcf unc tiona tthatpoi nts uc ha ss toreaby te,sa veaFl etodi sk,etc.
⇢es ampl epr og r
am us est wodi rectives,OBJ a ndORG,onl ines6a nd7of
thes our cel ist
ing .OBJs tandsf orOBJ e cta ndde Fne swhe ret heobj ectc odewi l
l
bea ssembl edinme mor y .4O RGs tandsf orORi Gina ndde Fne stheba sea ddresst o
beus edwhe nc reatingt heJSRs ,JMPs , andot herf unc ti
onst hatre f
e r
enc es pe ciFc
addr esseswi t
hi nt hepr og ram.Ge ne rallyOBJa ndORGa rethes ame ,a ndf ort he
timebe ingwe ’l
l l
e aveita tt ha t
. Cons ul ty oura s
se mble rma nua lformor es pe ciFc
informa ti
onont heus eoft he sec omma nds .

3[
CT]Aqui
ckt
ip:I
fyoua
reus
ingt
heMe
rlna
i s
sembl
er,y
ouc
ana
utoma
tic
all
yFl
lthe
l
inewithast
eri
sksbyhitti
ng<CTRL>P.Ifyoutypeaspaceandt henhit<CTRL>Pthen
Merlnwi
i ll
inse
rtanast
eris
ka tt
hebegi
nningandend.Se
eAppe ndixGf ordet
ail
s.
4[
CT]⇢eOBJdi r
ect
iveisnotusual
lynecess
ary
,anditisincompa t
ibl
ewithlaterve
r-
s
ionsoftheMerlna
i ssembler(Mer
li
nPr oandMe rl
in8/
16).Inthisbooka l
loftheOBJ
l
ineshav
ebeencomme ntedout.

]
1
8 As
sembl
yLi
nes

Reme
mber,onl
ythea c
tualpr
ogramisconv
ert
edint
otheobj
ectcode.⇢e
r
ema
rksa
ndtheEQU,OBJ,andORGsta
tement
sareonl
yusedi
nthesourcecode
a
ndar
enev
ert
ransf
err
edtotheobje
ctcode
.

Loa
d/St
oreOpc
ode
s
Oneoft hemos tfundame nta
loperat
ionsinma chinec odeistransf
erri
ngthe
numbe rv al
uesbetwe endi Le
r e
ntlocati
onswi t
hint hec omput er.You’l
lr e
call
tha
ti na dditi
ontot he6 4
K ofa ct
ualme mor ylocati
ons ,therewe reregi
ster
s
i
nsidethe6 502it
self
.⇢e sewe r
etheAc cumulator,theX- Regist
er,andt heY-
Regis
ter.⇢e r
ea r
eanumbe rofopc odesthatwillloade achoft heseregis
ter
s
wit
hapa r t
ic
ularvaluea nd,ofcourse,anothe
rs ettostor ethesev a
luessome-
whereint hecomput er.
⇢et a
blebelowsumma r
izesthes
e:

Ac
cumul
ator X-
Reg
ist
er Y-
Reg
ist
er
Load: LDA LDX LDY
St
ore: STA STX STY

⇢eFr stmne monic,LDA,standsforLoa D Ac cumulator.LDAisuse


dwhe never
youwi shtoputav al
uei ntotheAc cumul at
or.Conv ersel
y,tostor
ethatv a
lue
s
ome whe re,
youwoul de x
e cut
etheSTAc omma nd,whichs t
andsforSToreAccu-
mulator.⇢eopc odesfortheX- Regis
terandY- Regi
ste
ra resi
milarandperf
orm
t
heide nti
calf
uncti
onwi ththea s
soci
atedregisters
.
Nowt hequesti
oni s
,howdowec ontrolwha tnumbe rsgetputi
ntothereg-
i
ste
rwe ’
reconcer
nedwi th?⇢e rearebasi
c a
llytwoopt ions.⇢eFr s
tistoputa
s
peciFcnumbe rthere.⇢i sisusual
lyindic
a t
edi nthesourcelis
ti
ngbypre c
eding
t
henumbe rwewa nttobel oadedwitha“ #
”c haracte
r.
99 LDA #$05 ; LOAD ACC. WITH THE
100 ; VALUE ’$05’

Fori
nst
ance,i
nt hisexampl
e,weha veloadedtheAc c
umulat
orwiththeval
ue5.
Howdoy outhinkwewoul dloadtheX-Re g
ist
e rort
heY-Regi
st
erwiththeva
lue
0?
⇢eot hera l
terna
tiv
eist oloadther eg
isterwit
hthecontent
sofa not
her
memoryloca
tion.Todot hi
s,wejustl
eaveoLt he“#”cha
rac
ter
.
99 LDA $05 ; LOAD ACC. WITH THE
100 ; CONTENTS OF LOC. $05

I
nt hi
scase
,weareloadi
ngtheAc c
umul at
orwit
hwha t
everloc
ation$05ha ppens
t
obeholdingatthemome nt.
⇢esetwooptionsarecal
le
da ddres
si
ngmodes.⇢eFr stexample(#$05)we
c
allthei
mme di
atemo de,be
causei
ti snotne
ces
sar
ytog otoame morylocati
on
t
og ett
hedesi
redva l
ue⇢es
. econdc as
ewecall
theabsol
utemo de.I
nthismode ,
3
.As
sembl
ers 1
9

weputagi
venv
aluei
nther
egi
st
erbyFr
stg
oingt
oas
pec
iFe
dme
mor
yloc
ati
on
t
hathol
dst
heval
uewewant
.

Put
ti
ngi
tAl
lTog
ethe
r
Wenow ha vethea bil
ityt ot ransf
e rnumbe rsabouti nt hec omput e r
,t o
j
umpt oot hersubrout ineswi thintheAppl ev i
aaJSR, andtor e
t urns afelytot he
nor malwor l
dv iaanRTSwhe nwe ’r
edone .Ina dditi
on,weha v ea na s
sembl er
thatwillallowuse as
ilyt og enerateas our celis
tingforourpr og ram,whi chc an
alsobee asilymodi Fe d.Le t’
sputa llthist oget
he rtowr it
eas hortpr ogramt o
pr i
ntsomec haract
ersont hes creen.Appe ndixEc ontainstwoc ha rt
s( theAS CI I
table,
andt heTe x
tS c
r eenMe mor yMa p)t hatwillsupplythene cessaryi nforma -
tiontoa c
hi evethis
.
Whe nac haracte
ri spr i
nt edont hes cre e
n,wha tisreall
yha ppe ningi sthata
numbe rv alueisbe i
ngs t
or e
di nt hea reaofme mor yreservedfort hes creendi s-
play.Cha ng eav al
uet he reandac haracteront hes creenwillcha ng e.⇢eTe xt
Sc r
eenMe mor yMa pg iv
e sthev ariousa ddr e
ssesofe a
c hposit
ionont hes creen.
⇢euppe rl e4cornerc orre s
pondst olocation$400, thelowerrightt o$7F7.
⇢eAS CIItableshowswhi chnumbe rv a
luescreatewhichs creenc ha ract
e rs
.
Suppos ewewa nttopr intthewor dAPPLEi nnor ma lte
x t.
⇢et a
bl eindi catesthat
wes houldus ethefollowi ngv alues:
A: $C1
P: $D0
P: $D0
L: $CC
E: $C5

Ifwewa ntthewor dt oappe aront hes event


hl i
neoft hes cr
een,wes houldload
thes
ev al
ue sintolocati
ons$700 t o$704.Tot estthis,enterthefoll
owi ngpro-
gram us i
ngy oura ssembler.Ify oustilldon’
tha veone ,theAppleMi ni-Assem-
ble
rc a nbeus ed,alt
houg hwewi l
ls oonr ea
cht hepoi ntwhe reitwi l
lnotbe
suWc i
e ntforourne e
ds.Ify oua reusingt heAppl eMi ni-Ass
embler,enteronly
thepr ogrami tsel
f,ignori
ngt heOBJ a ndORGs tat
e ments.InplaceofJSRHOME
enterJSR$FC58.
Att hebe ginningoft hepr ogram,wede Fnewhe reitistobea ssemble
d.
⇢e nwede Fnear outi
nei nt heAppl ecall
edHOME,whi c hispartoftheAppl e
Moni torandi sat$FC58.Whe neverthisrouti
nei scalle
d,thes c
reeni scle
ared
andt hec ursorputi nt heuppe rle4c or
ner.⇢i se nsuresthatonlyt hewor d
APPLEwi llbepr i
ntedont hes creen.
1 ********************************
2 * AL03-TEST PROGRAM 1 *
3 ********************************
4 * OBJ $300
5 ORG $300

]
2
0 As
sembl
yLi
nes

6 HOME EQU $FC58


7 *
0300: 20 58 FC 8 START JSR HOME ; CLEAR SCREEN
0303: A9 C1 9 LDA #$C1 ; 'A'
0305: 8D 00 07 10 STA $700
0308: A9 D0 11 LDA #$D0 ; 'P'
030A: 8D 01 07 12 STA $701
030D: 8D 02 07 13 STA $702
0310: A9 CC 14 LDA #$CC ; 'L'
0312: 8D 03 07 15 STA $703
0315: A9 C5 16 LDA #$C5 ; 'E'
0317: 8D 04 07 17 STA $704
031A: 60 18 END RTS
031B: 72 19 CHK

⇢er outinewi l
lbe gi
nbydoi ngaJSR t othehomer out i
net oclearthe
scr
een.⇢e nt heAc cumulatorwi llbel oadedwitha nimme diate$C1,thev a
lue
f
ort helet
terA.⇢i swi l
lthenbes toreda tloca
tion$700ont hescree
n,whi ch
wil
lc a
usethel et
terAt obev i
si
bleont hescreen.⇢ene xtvalueloadedisforthe
l
ette
rP,a ndt hi
si sstoreda t$701 a nd$702.I tisnotne c
e ss
a r
yt orel
oadt he
Accumulator,s i
nces tori
ngt henumbe rdoe snota c
tual
lyr emov eitfrom the
Accumulator.⇢enumbe risjustdupl i
cate
da ttheindica
teds pot.⇢epr oc
ess
cont
inuesint hispatternunt i
lallFvel ett
ershavebeenprinted,andt he
na nRTS
ret
urnsustonor malope r
ation.
Oncey ouha vea ssembledt her outineat$300,trycall
ingi tbothfrom the
Moni t
orle
v elwith:
300G

a
ndf
rom BAS
IC(
eit
herone
)wi
th:
CALL 768

Yous
houldal
sochang
et heLDA/
STAtot
heX-Reg
ist
era
ndY-
Reg
ist
ere
qui
v-
a
lent
stov
eri
fytha
tthe
ywor kinasimil
armanne
r.

Conc
lus
ion
Younow ha vea ty ourdi s
posalatotalofe i
ghtopc odesandaf amiliar
ity
witha s
sembl e
rs.⇢e sef ewopc odesareprobablythemos to4e nused,a ndwi th
j
ustt hesealoney ouc andoqui t
eanumbe roft hings
.⇢eJSRa ll
owsy out o
ma keus eofa l
lther outinesa l
readyavai
labl
ei ntheMoni t
or.Ihig hl
yr ecom-
me nd ge t
ti
ngTheAp pleMo nitorPee
ledbyW. M.Doug herty
,a va
ilabl
ee xclu-
si
ve l
yfrom Appl e
, f
ormor einformati
ononus ingt hes
eroutines.Hisbookg ives
alotofde t
ailonwha tisa va
ilable
.
Int hene xtchapterwe ’
lllookatsomemor ea dvanceda ddr
e ss
ingt ech-
ni
que s,andhowt odoc ountersandloops.
4
4
. Lo
opsandCo
unt
ers
J
anua
ry1
981

Now weg e ti ntonotonl ymor emne moni cs,butt het echni que sofus ing
themt oa ccompl i
s hv a r
iousov erallope rat
ions .Inpa rticula r, we’lllooka tc oun-
tersa ndl oopsi na ssembl yl ang uag e.InBAS IC,t heFOR- NEXTl oopi soneoft he
mor ee ssent ialpa rtsofma nypr og rams,a ndt hisi snol esst r ueinma chi nepr o-
gr a
mmi ng .⇢eonl ydi Lerenc ei showt hel oop/ count erc ombi na ti
oni sa ctuall
y
ca r
riedout .
InBAS IC,t het estingofc ount ersisdonee itherbyIF- THENs t
ateme ntsor ,
automa tica l
ly ,int heNEXTs tat
e me ntoft heFOR- NEXTl oop. Ina ssembl ylang ua ge,
thet estingi sdonebye xami ningSa gsint heS tatusRe g i
st er. ⇢e seSa gsi ndi cat
e
thes tatusoft hev ariousr egistersa ndme mor yl oca ti
ons . ⇢eS t
a tusRe g i
s t
eri sa
four thr egiste roft he6 502,oneweha venotpr evious l
yme ntione d. Befor eg oing
onwi thl oopsa ndc ount ersitwi llbene cessa ryt obr ieSydi sc usstheS tatusRe gis
-
tera nd, ina ddition, bina r
ynumbe r
s.
Lik et heot he rthr eer egis
ter s–t heAc c umul ator, theX- Re gister,andt heY-
Re gister–t heS tatusRe g i
sterhol dsas ingleby te.You’ llr eca lltha teac hby tein
theAppl ec anha v eav aluefrom 0t o2 55( $00t o$FF) .
Asi tha ppe ns , t
he rea rema nywa ysofl ook i
nga ta ndi nt erpre t
ingnumbe rs
.
⇢eoneofc ommone x periencei stha tinwhi chwec ons ide ronl yt hema g nitude
oft henumbe r.Not icingt hat2 5 5isl ar
g ert ha n1 2 8g ivesusonl yav erys i
mpl e
form ofi nf or ma tion–whe t
he ranumbe rise ithe rlesst ha n,e qua lto,org reat
er
thana not he rnumbe r.
As econdwa yofl ookinga tnumbe rsisi nbi na ryf orm. Ba setwoa llowsust o
seemor einf or ma tioni nanumbe randhe nc eist ha tmuc hmor eus eful.Weha ve
alreadys ee nhowas ing l
eby tec anber epr esent ede ithe ra s0t o2 55ora s$00t o
$FF. I
nbi na r yt her ang eis00000000 t o11111111.Fori ns tanc e,1 33( ba seten)
wa sr epr ese nteda s$85.I nbi na ryi tha sthea ppe a ranc e1 00 0010 1.Int hisc ase,
ea c
h1or0r epr esent sthepr esenc eora bse nc eofag ivenc ondi t
ion.⇢us ,e i
ght
distinctpi ece sofi nfor mationa rec onve yed,a swe lla sa llt hev ariousc ombi na-
tionspos sible .
Be forey our uns hriekingf rom t her oom,r eme mbe rt ha tthi sisa lldonet o
ma k et hing se asier,notha r
de r.Be sides,lear ningba ses i
x tee n( he x)wa sn’tthat
ba dba cka tt hebe ginni ngoft hisbook ,wa si t?S ole t’
sta keamome nttos eewha t
thisbi tsa ndby te ss t
uLi sallabout .

]
2
2 As
sembl
yLi
nes

Bi
nar
yNumbe
rs
⇢eAppl ei sane l
ectr
onicdevic
ea nd,a ct
ua ll
y,inma nywa ys,as i
mpl eone
attha t.Inmos tpa rtsofitscir
cuit
ry,thec urre
nti se i
the roLoron. ⇢a t
’sit.No
in-be t
we en.Ha vingt wopos si
bleposit
ionsi spe rf
ectforba setwo.⇢ei de aofa
numbe rba sehast odowi thhowma nys y mbol s,oruni ts,y ouusef orcounting.
Wenor ma llyuset en. Weha veatota
l oft enpos sibl
es ymbol stowr it
einas ing l
e
positionbe foreweha v
et ostar
tdoubl ingupa ndus ingt wopos i
tionstor epre-
sentanumbe r.You’ llrecal
linhextha t,byus ing0t hroug h9a ndAt hroughF,
weha ds i
x teenpos sibil
iti
es;t
hus,wewe reinba ses i
x t
een. Wi t
htheon/ oLna ture
oftheAppl e,we’relimi t
edtotwopos sibili
t i
es:0or1 .
Howhi ghca nwec ountinonepos it
ion?Notv ery.Wes t
artat0 ,t
heng ot o
1,a ndt hat’sit.⇢e nweha vetoadda nothe rpos it
ion.⇢ene xtnumbe r,there-
fore, i
s1 0. Asbe fore,reme mberthat
, int hiscase ,10re presentswha tweus ua l
ly
calltwo.I fweus et hreepositi
ons,t
hel owe stnumbe ris1 0 0(repres
entingt he
qua ntit
yf ouri nba set e
n).
Forag ivennumbe rbase,t
herei
saf or mulaf orthehi g hestde c
ima l
numbe r
youc anre presentwi thag ive
nnumbe rofpos i
tions:
N=BP71
whereNi sthela
rgestde cima lnumber
, Bi
sthenumbe rba s
e,andPi sthenum-
berofposit
ionsavai
lable.
Byusingeig
htpos it
ions,wec angoupto1 11111
11,whi c
hj usthappenst o
equal255.Howha ndy!⇢i sisthesamema x
imum v al
ueofourby tes.
And, ifthe
tr
uthbek nown,it
’sactua ll
ytheotherwayaround.Weus ethenumbe rsthroug h
255becauseweareus inge ig
htbi t
stomakeupe achbyt
e.Whe thereachbitisa0
ora1de pendsonwhe therthepa r
toft
hecircui
tthati
sresponsibl
eforthatbitis
oLoron.

⇢ eSt
atusRe
gis
ter
He r
ea tlas
tisourr epres
entationofas ingl
eby te,ma deupofe ig
htbi t
s.In
part
icular,theby t
ewea relook inga tistheS t
atusRe giste
roft he6 502.⇢e
importantdiLerencebetweenthisr egi
sterandtheot hersisthatitisnotus edto
st
orenumbe rval
ues.Inst
e a
ditindi ca
tesvari
ousc onditi
ons .
⇢ebi t
softheS t
atusRe g
isterarenumbe redfrom righttole4, 077.Eachbit
inthisregist
erindica
testhes t
atusa nd/orresult
sofdi Lerentope rati
onsa ndis
cal
ledaSa g
.Itisbyus i
ngt hi
sr egist
erthatwec anc reat
ec ounte
r sandl oopsin
ourpr ograms.⇢eSa gwewi llbei mme diate
lyc oncernedwi t
hi sbi tone,the
zer
oCa g.Int e
rmsoft hecomma ndswea l
readyknow, t
hez eoSa
r gisa Lectedby
anLDA, LDX,orLDY.
4
.Loopsa
ndCount
ers 2
3

I
fthev a
luel oadedintotheAc cumul a
tor,X-Regis
ter
,orY-Re g
ist
erwere$00,
t
heSa gwoul dbes e
tto1 .I fitwe r
eanonz er
onumbe r
,theSa gwouldbe0 .
S
eemi nglyba c
k wardper haps,butr emembe r
,eachSa gisse
ttos howthepr es
-
e
nc eora bse
nc eofag ivenc ondit
ion,inthiscase
,$00.⇢es ett
ingorc l
eari
ngof
e
achS t
atusRe gist
erSagi sdonea utomati
call
ybyt he6502a4e reachprogra
m
s
tep,indica
tingther es
ultsofa nypa r
tic
ularopera
tion.

I
ncr
eme
nti
nga
ndDe
cre
ment
ing
Tocr
eateacount
erandt he
nal oop,wewi llusetheStatusRegi
stertotel
l
whenag i
venregi
st
erorme moryloca
tionreaches0.Wewi llals
one e
dawa yof
cha
ng i
ngtheval
ueofthecounte
ri nar e
gula
rf a
shion.Inthe6 502
,thisisdone
byincr
ement
ingordec
rementngbyonee
i achti
me ,asindi
cated.

Acc
umulator X-
Regis
ter Y-
Reg
ist
er Me
mor
yLoc
Inc
reme
ntby1: Notav
ail
able INX INY INC
Decr
ementby1
:Nota v
ail
able DEX DEY DEC

⇢et abl
ea boves howst hemne monic
sus e
dt oincre
me ntorde c
r e
me nta
par
ti
c ul
arreg
ist
e rorme mor ylocat
ion.
Not ethatdirect
lyi nc
reme nti
ngorde cr
eme nt
ingtheAc cumulat
ori snot
pos
sible.
⇢ei nc re
me nt/de
creme ntcommandsaLe c
tthezeroSag,dependingon
whet
he rther
esultoftheope rat
ionis0ornot.
⇢eus ua
l s
y nta
xf orusi
ngt hesecommandsinana s
semblylis
ti
ngis:
10 INX
11 INY
12 DEX
13 DEY
14 INC $0600
15 DEC $AA53

]
2
4 As
sembl
yLi
nes

Forther e
gist
erope r
a t
ions,thecommands ta
ndsalone
,wit
hnoneedofan
oper
and.Inthec a
seofINCa ndDEC, t
hememor yl
ocat
ionst
obeoper
ate
dona r
e
gi
ven,i
nhe xofcourse,usuall
ypre c
ededbythedoll
ars
ign.
Onet hi
ngt ome nt
ionhe r
eisthewrap-ar
oundnatureoft
heope
rat
ions
.To
under
sta
ndt hi
s,examinet hefoll
owingchar
t:

Or
igi
nal
cont
ent
s I
ncr
eme
nt De
cre
ment Z-
Fagset? Z
$05 $06 $04 no,no 0
,0
$0F $10 $0E no,no 0
,0
$01 $02 $00 no,ye
s 0
,1
$FF $00 $FE yes
,no 1
,0
$00 $01 $FF no,no 0
,0

⇢ee Lectsofi ncr eme ntinga ndde creme ntingdi Lerentv a


luesares hown,
al
ongwi ththee Lectsont hez eroSa ga 4e rtheope r
ations.⇢eFr stcaseiss i
m-
ple
, 5+1=6 , 571=4 . I
nbot hcase s,ther es
ulti snonz ero,sot hez e
roSa gisnot
set
.For$0F, t
hes amehol dstrue. Re me mbe rt hat,inhe x,thene x tnumbe ra 4er
$0Fi s$10.Int hecaseof$01,i ncre me ntingpr oduces$02.Whe nwede creme nt
$01,ther es
ultis$00;t hez eroSa gi sset.
He r
e ’
swhe reitg etsinter es
ting .Whe nt hes tart
ingv aluei s$FF, a
ddingone
wouldnor mallygive$100.Howe ve r,sinceas i
ng leby teonlyha sar angeof$00
to$FF, t
he1i signor ed, andt hev aluebe c
ome s$00. ⇢i ssetsthez er
oSa g.Int he
caseofde cr
eme nti
ng ,$FF71=$FE,s ot hez eroSa gisnots et
.
Ifwes tartwith$00,a lthoug hi nc reme nt i
ngpr oduce sthee x
pected$01,
decreme nti
ngwr apsa r oundi nt her e v
e rs
eoft hepr eviousc ase,givi
ng$FF. Bot h
res
ultsa renonz er
o,s oZ–s hor tfort hez eroSa g–i sc l
ear,thati s
,nots et,for
bothope rat
ions.

Loopi
ngwi
thBNE
⇢eonl ypr
oce
durer
ema i
ningtoenableyout
oc r
eat
eal
oopisawayoft
est
-
ingtheZ-
Sagandthe
nbeingablet
oge tbacktothet
opoft
heloopf
oranot
her
pass
. I
nBASIC,
asi
mplel
oopmi ghtl
ookl i
kethi
s:
10 HOME
20 X = 255
30 PRINT X
40 X=X-1
50 IF X <> 0 THEN GOTO 30
60 END

I
nt hi
sprog
ram,west
artwi
ththec
ounte
rXse tat2
55.⇢entheval
uei
s
pr
int
ed,dec
rement
ed,a
ndthepr
oce
ssre
peat
edunt
ilthec
ount
err
eache
s0.We
4
.Loopsa
ndCount
ers 2
5

canma ketheloope xecut


ea nynumbe roft i
me sbyproperlysett
ingthei
nit
ial
val
ueofX.
Inma chinecode,thete
sta ndGOTOi sdonewi t
habr a
nc hinst
ructi
on.I
nthis
cas
e ,theonewe ’
lluseFrsti sBNE.BNEs tandsforBranchNotEqua landisa
branchi nst
ructi
one xecut
edwhe nar egis
terisloadedwith“ anonzeroval
ue.”
⇢i sca nhappe nei
therdire
c t
lywi thsome t
hinglikeaLDA #$01ora stheres
ult
ofana rit
hme ti
coperati
on,sucha sanINX, DEC,orADC.Hereistheass
embly-
lan-
guagee qui
valentoftheBASICl is
ting:
1 ********************************
2* AL04-LOOP PROGRAM 1 *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7 HOME EQU $FC58
8*
9 START JSR HOME
10 LDX #$FF
11 LOOP STX $700
12 DEX
13 BNE LOOP
14 END RTS

Andhe
rei
sthewa
yAppl
e’
sdi
sas
sembl
erwoul
dshowi
t:
*300L
0300- 20 58 FC JSR $FC58
0303- A2 FF LDX #$FF
0305- 8E 00 07 STX $0700
0308- CA DEX
0309- D0 FA BNE $0305
030B- 60 RTS

Inthispr ogram, weFr stdoaJSRt othec lea


rs cree
nr outinei ntheMoni tor
thatweus edi nc hapt erthree .⇢e nweloa dtheX- Regi
sterwi t
has t
artingvalue
of$FF.Now wes ta rtthel oop.S t
ori
ngt heX- Re gis
tera t$700wi l
lma kethe
l
oop’sa ct
ionv i
sibl
ea sac har acteronthes cr
e enfore achpa ssthrought heloop.
Ne xt
,DEXs ubtract
sonef rom t hec urr
entv alueoft heX- Re gi
ster.⇢eBNEwi ll
thenc ontinuet hel oopba ckupt oLOOP unt i
lt heX- Registerr ea
che s$00,at
whichpoi ntt hetestwi l
lfaila ndpr ogr
ame xecuti
onwi l
lfallthrought otheRTS
atthee ndoft hepr ogram.Pe oplewil
lalsor efert othee xecutionofabr anch
i
ns t
ructi
onbys ayi
ngt hatthebr anchisignore dort akende pendingonwhe ther
program Sow f allst hrought hebr anchinstructi
onorg oestot hene wa ddr es
s
indic
atedbyt hebr a nchi nstruc ti
on.
Trye nt e
ringt hi snow,a nda lsonoticehow f as
tt hepr ogramr uns.You
probablywe ren’tablet os e
ev erymuc h,butall 255v al
ueswe reputt othes cr
een.
⇢ei nv er
seAt ha t
’sl e
4ont hes cre
enishowa$01a t$700a ppear s
.($00d oesn’
t

]
2
6 As
sembl
yLi
nes

getprint
ed–why ?)Tov eri
fyt hateachpassisbe ingexec
uted,repla
c etheSTX
$700inthes ourceli
sti
ngwi thaJSR$FBDD. Ifyoudon’ twanttohear2 55be e
ps,
tr
yc hangingtheiniti
alval
ueoft heX- Regi
st
e rinline10.Asbe f
ore,yous houl
d
beabletoc a
llthi
sprog r
amf rom theMonitorwi t
ha300G, orfrom BAS ICwi t
ha
CALL768.
Youma ya ls
owi shtotrythee qui
val
entve r
sionoftheprogram usingtheY-
Regi
s t
erorame mor ylocat
iona sthecounter.Iwoul dsugge
sttryi
ngt owr it
ea
program usi
ngINC, INX,orINYt odriv
ethecount erasapract
iceprogr
a m.
5
5
. Lo
ops
,Br
anc
hes
,COUT,
andPaddl
es
Fe
brua
ry1
981

Loopi
ngwi
thBEQ
Int hepr evi
ousc hapt e
rwes tar
tedintot hevari
ouste c
hniquesofc r
eat
ing
andus ingc ountersa ndl oopsi nassemblylanguage.Toaccompl i
shtheloop,we
usedthev a
lueinoneoft her egi
ste
rsasac ounterandthebranchinstr
ucti
onthat
tes
tsfort hepresenc eofanonz e
ronumbe rint heregi
st
ertoa ct
ual
lydotheloop-
ing.Reca l
lthatthise val
ua ti
onofz ero/nonzeroisdoneviathezerobit,
orSa g
,of
theStatusRe gis
teroft he6 5
0 2.
⇢ec omple me ntoft heBNE instr
uction issomethingc al
ledBEQ,which
obscurelye noughs tandsf orBr anchEQua l.Itopera
tesi
nj ustt
heoppos i
tefa
sh-
ion fr
om BNE;t hati s,itbr anchesonlywhe ntheregis
terorme morylocat
ion
reac
he sav alueof0 .
Fore xample ,cons i
de rthisBASICl i
sti
ng :
10 HOME
20 X = 255
30 PRINT X
40 X=X-1
50 IF X = 0 THEN 70
60 GOTO 30
70 END

Inthiscas
e,thel
oopc ont
inue
sa sl
ongasXisnotequa
lto0.Ifi
tis
,the
bra
nchinstr
ucti
oniscar
rie
douta ndtheprog
ramends
.Inasse
mblyla
nguage
,
thi
sprog
ram wouldbet
heequival
ent:
1 ********************************
2* AL05-LOOP PROGRAM 2 *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7 HOME EQU $FC58
8*
9 START JSR HOME
10 LDX #$FF
11 LOOP STX $700
12 DEX
13 BEQ END

]
2
8 As
sembl
yLi
nes

14 JMP LOOP
14 END RTS

Not i
cethatthispr
ogramrequirestheadditi
onofane winstr
ucti
ontoourreper-
toir
e:theJMPc omma nd.⇢isi sanalogoustoaGOTOinBAS I
C,andinthispro-
gram willcauseprog
rame xecuti
ontoj umpt otherouti
nestar
ti
ngatLOOPe a
c h
time.Onl ywhe nt
heX- Reg
isterre
a c
hes0doe stheBEQtakeeLecta
ndc auset
he
programt os k
iptotheRTSa tEND.He reisthewaythiswouldappearwhenput
i
nt ome morya ndthe
nlist
edwi tht
he“ L”comma ndfrom theMonit
or:
*300L
0300- 20 58 FC JSR $FC58
0303- A2 FF LDX #$FF
0305- 8E 00 07 STX $0700
0308- CA DEX
0309- F0 03 BEQ $030E
030B- 4C 05 03 JMP $0305
030E- 60 RTS

⇢ea ssembl
era ut
omati
cal
lytra
nsl
at
e stheposit
ionsofLOOP andENDinto
t
hea ppropri
ateaddres
sest
obeus e
dbyt heBEQa ndJMPwhe nitassembl
esthe
code
.
Re membe rt
hattothele
4a r
etheaddresse
sandt heval
uesforeachopcode
anditsaccompanyingoper
and.⇢emor ei nt
ell
ig
ibl
et r
ansl
ati
ont otheri
ghtis
Apple’
sinter
pret
ati
onofthi
sdata.

Br
anc
hO?s
etsa
ndRe
ver
seBr
anc
hes
Noticet hattheJMPsa ndJSRsareimme diat
el
yf oll
owedbyt hea ddre
sses
(r
ever
sed)tha tt
heyaretoj
umpt o,suchasintheFrstJSRas$300.
Howe ve r
,branchinst
ructi
onsarehandledalitt
lediLere
ntl
y.⇢e$03i sa n
oLse
tthattell
sthe6502tojumpt hr
eebytespastt
hene xti
nstr
ucti
on.
Sincethene xti
nst
ruct
ionisat$30B,t
he6 502willbra
nchto$30E,thusskip-
pi
ngtheJMPc omma ndandg oi
ngdi r
ect
lytotheRTS, whic
hterminate
st herou-
ti
ne.
Branche scanals
obedonei ntherever
sedirect
ion.Hereisar a
therineW-
ci
ent,
butillustr
ati
veexample:
1 ********************************
2* AL05-LOOP PROGRAM 2A *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7 HOME EQU $FC58
8*
9 START JSR HOME
10 JMP SETX
11 END RTS
5
.Loops
,Br
anc
hes
,COUT,
andPa
ddl
es 2
9

12 *
10 SETX LDX #$FF
11 LOOP STX $700
12 DEX
13 BEQ END
14 JMP LOOP

⇢eMoni
torl
is
ti
ngf
ort
hiswoul
dbe
:
*300L
0300- 20 58 FC JSR $FC58
0303- 4C 07 03 JMP $0307
0306- 60 RTS
0307- A2 FF LDX #$FF
0309- 8E 00 07 STX $0700
030C- CA DEX
030D- F0 F7 BEQ $0306
030F- 4C 09 03 JMP $0309

Int hi sex ampl e,thebr anch,ifta ken,wi llc auset hepr og ramt omov eba c k
upt hr oug ht hel i
sting .Toi ndicatethisbr anc hi nt heoppos it
edi r
ec t
ion, thehi gh
bitiss et. ⇢i si st
hes amet echniquet ha tiso4e nus edt oshowne gati
v enumbe rs
ina ssembl y-langua gepr og rams. Ple
a senot et ha titisnotj ustama t
terofs et
ting
thehi g hbi t.Ifthatwe rethec ase,thev aluef ollowi ngt heBEQc omma ndmi g
htbe
expec tedt obe$89. (⇢ea ddr essofthene xtins truction( $30C)mi nuswhe rewe
wa ntt og oto( $303) e qua l
s$09. ⇢e nwi t
ht hehi ghbi ton, weha ve$89. )
⇢i si sa lmos tc or r
e ct
.⇢ea ct
ua lv aluei sa rrive da tbys ubt ractingt he
branc hdi stanc efrom $100.⇢us$100mi nus$09e qua ls$F7.⇢i siss ot hatthe
destina tiona ddressc a ns t
illbea rri
ve da tt hroug ha ddi ti
on.Not i
c etha t$30C+
$F7 =$403.I tisthe nv erye as
yf ort he6 50 2t oc or rectt hisba ckonepa get o
$303.
Ifa l
lt hiss eemsabi tconfus i
ng,t rynott ol etitbot hery ou.I na ctua lpra c-
tic
e ,the rei snotmuc hr easont obec onc erneda boutt hewa yinwhi cht heoLs et
bytei sde t
e rmineds i
nc ey oura ssembl erwi l
l de t
e r
mi net hepr operv aluesf ory ou
whe na ssembl i
ngc ode ,a ndAppl e’
sdi sassembl er, aswe l
l asma nyothe rs,includ-
ingS our c eror,willgivet hede sti
na t
iona ddre sswhe nr eadingot hercode .
⇢i si sa lsoag oodt imet os tre
sst hei mpor t
a nceofwor kingt hroug he a
chof
thesee xampl esony ourown,s t
e pbys tep,toma kes ur ey ouunde rstande xactly
wha tha ppe nsa teac hs t
e p,andhowi tr elatest other e stoft hepr ogram. Ifyou’r e
nots ur e, goba ckov eritunt ilt
ha tprov erbiallightc ome son!

Sc
ree
nOut
putUs
ingCOUT
Ast
heX-
Reg
ist
eri
sinc
reme
nte
dint
hispr
ogr
am,we
’l
lsuLt
t hev
aluei
nto
$700s
owec
ans
ees
ome
thi
ngont
hes
cre
ena
sthec
ount
era
dva
nce
s.

]
3
0 As
sembl
yLi
nes

Nowy ouma yr ema rkf r om y oure xperie nceinc ha pterfourt ha ta lthoug h


thispr og ra
mi spl ea sa
nt l
ys impl einitslogic,i tisnotmuc hf unt owa tchont he
screenbe causei trunss oqui ck l
y .
Tos olvet his,wewi l
ls tar ttoma kemor ee xtens iveus eoft her out ines
alreadypr esenti nt heMoni tort odoc er
taint a sksandt husma keourpr og ram-
mi ngr equireme ntsal it
tles impl e
r.Re ferri
ngt otheMoni t
ors ubr outine si n
Appe ndixD,i tha ppe nst ha tt heFr strout inel ist
edi ss ome thingc all
edCOUT.
⇢i si st her outinet hata ctua ll
ys endsac ha racterwewa ntout putt owha tever
dev i
c e(s)ma yc ur rentlybei nus e.Mos toft het i
met hi sjus tgoesdi rec t
lyt ot he
nex trout i
nel is
te d, COUT1( cle verwi t
ht hena me s,ar
e n’tthe y?),whichs peciFc all
y
handl est hes cree nout put .Wha tthisme ansf orusi st hata ny t
imewewa ntt o
outputac harac ter,wedon’ tha vetowr it
eourownr out inest owor rya bouta l
l
thei n- depthde t ai
lsa boutt hes cre
en( cursorpos it
ion,s creens ize,whe theri t’
s
timet os croll)–wej ustloadt heAc cumul atorwi ththeAS CI Ivaluefort hec ha r
-
acterwewa nttopr inta ndt he ndoaJSR$FDED!
Now c ome ss omepr og rammi ngt echni que .Wewoul dl i
k et oha v et he
count erv alueint heAc cumul ators owec anpr intitviaCOUT, butunf or t
una tel
y
ouri nc reme nt/de creme ntc omma ndsonl ywor kfortheX- Re g
ister
, theY- Re gis
-
ter,andg ivenme mor yloc ations .Tos olv
et his ,we’l
l hav et oe xpandourl istinga
l
ittle.⇢i stime ,we ’l
lus eame mor ylocati
ona sthec ount er,andt he nl oa dt he
Ac cumul at
or ,one a chpa sst hr ough,topr intoutav i
s i
bl es i
gnoft hec ount er’
s
activity.Goodl oc ationst ous ef ore x
pe ri
me nt inga re$06t o$09.⇢e sea renot
usedbye itherI nt eger,Appl e o4,DOS
s ,ort heMoni tor .⇢i sisi mpor tantf or
avoidi ngc onSi ctswi t
ht heAppl e’
snor ma la cti
vit
ieswhi l
er unningy ourown
prog rams .
Andnowourr evi
se dlisting :
1 ********************************
2* AL05-LOOP PROGRAM 2B *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7 CTR EQU $06
8 HOME EQU $FC58
9 COUT EQU $FDED
10 *
11 START JSR HOME
12 LDA #$FF
13 STA CTR
14 LOOP LDA CTR
15 JSR COUT
16 DEC CTR
17 BEQ END
18 JMP LOOP
19 END RTS
5
.Loops
,Br
anc
hes
,COUT,
andPa
ddl
es 3
1

Appl
e’
s“L”c
omma
ndwi
ll
giv
ethi
sa4e
ryou’
vea
sse
mbl
edi
tinme
mor
y:
*300L
0300- 20 58 FC JSR $FC58
0303- A9 FF LDA #$FF
0305- 85 06 STA $06
0307- A5 06 LDA $06
0309- 20 ED FD JSR $FDED
030C- C6 06 DEC $06
030E- F0 03 BEQ $0313
0310- 4C 07 03 JMP $0307
0313- 60 RTS

Ac alltothisrouti
nev iaourus ual300Gf rom t heMoni t
or,oraCALL768
from BAS IC,shouldc l
earthes cree
n,t henpr inta l
lthea vail
ablecha r
acter
son
yourAppl eina l
lthreedispl
a ymode s(norma l,Sashing,andi nverse)
.⇢ebe ep
youhe aristhe<CTRL>G( bel
l)be i
ngp rint
e dtot hescreenv iaCOUT.⇢ei nvi
si
ble
cont rolcharact
ersaccountfort heblankr egionbe tweenthet woma insegments
ofout putcharact
ers.Youwi l
la ls
os eesomec haracte
rst hatarenotnor ma ll
y
ge neratedbyt heAppl e,sucha sunde rscore,r ev e
rses l
ash,andt hel e4squa r
e
br acket(_,\,[).
⇢ea lphabetisba ckwa r
d be causewes t
a r
teda tthehi ghestvaluea nd
wor k
e dourwa ydown. From c hapterfour,thoug h,you’l
lre membe rthatwhena
by teisincrementedbyonef rom $FF,t heres ultwr apsaroundba c
kt o$00.⇢i s
wi llproducea na c
tiont e
stablebyaBEQ.Us ingt hiswr a
p- ar
ounde Lectofthe
inc re
me ntc omma nd,wec a
nr ewri
tet hepr og ramt obeal i
ttl
emor econven-
tional li
keso:
1 ********************************
2* AL05-LOOP PROGRAM 3 *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7 CTR EQU $06
8 HOME EQU $FC58
9 COUT EQU $FDED
10 *
11 START JSR HOME
12 LDA #$00
13 STA CTR
14 LOOP LDA CTR
15 JSR COUT
16 INC CTR
17 BEQ END
18 JMP LOOP
19 END RTS

]
3
2 As
sembl
yLi
nes

Wi
tht
heAppl
eshowi
ng:
*300L
0300- 20 58 FC JSR $FC58
0303- A9 00 LDA #$00
0305- 85 06 STA $06
0307- A5 06 LDA $06
0309- 20 ED FD JSR $FDED
030C- E6 06 INC $06
030E- F0 03 BEQ $0313
0310- 4C 07 03 JMP $0307
0313- 60 RTS

Acallt
othisr
outi
neshoul
dnowprintoutt
hecha
ract
ersi
namor ef
amil
ia
r
manner
.Atlastourprog
ramsares
tar
ti
ngt odosomethi
ngint
ere
sti
ng!I
tget
s
be
tte
r!

Re
adi
ngaGa
mePa
ddl
e
Let
’stryreadi
ngag a mepaddl
ea ndusewha tweg etba c
ktopr i
ntsomething
tothescreen!Granted,I’
m notanymor esuret
ha nyoua rewha tgoodthi
smi ght
be,buti
t’sguarante
e dtobeane wprogrami nyourli
bra r
y!
⇢ePREADs ubrouti
nei nAppendixDi ndi
catesthatapa ddlecanber e
adby
l
oadingt heX-Re g
isterwiththeval
ueforthenumbe roft hepaddleyouwi s
ht o
rea
d, f
oll
owe dbyaJSR$FB1E. Whent herouti
ner e
turns,thevalueofthepaddle
wil
lbeint heY- Regist
er.Allwehavetodot henisgrabt hisval
ue,stuLitinthe
Accumul at
or,andthe ndoourJSRCOUT.
1 ********************************
2* AL05-PADDLE PROGRAM 1 *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7 TEMP EQU $06
8 PREAD EQU $FB1E
9 HOME EQU $FC58
10 COUT EQU $FDED
11 *
12 START JSR HOME
13 LDX #$00
14 LOOP JSR PREAD
15 STY TEMP
16 LDA TEMP
17 JSR COUT
18 JMP LOOP
19 * INFINITE LOOP

Yous
houl
dge
tthi
sinme
mor
y:
*300L
0300- 20 58 FC JSR $FC58
5
.Loops
,Br
anc
hes
,COUT,
andPa
ddl
es 3
3

0303- A2 00 LDX #$00


0305- 20 1E FB JSR $FB1E
0308- 84 06 STY $06
030A- A5 06 LDA $06
030C- 20 ED FD JSR $FDED
030F- 4C 05 03 JMP $0305

⇢i sr outinewhe nc alledwi llqui cklyFllupt hes creena ndt henc hang ethe
st
ream ofc ha ract
ersout puta syout urnpa ddle0 .S i
nc eweha venot e
stfora n
end,RESETi stheonl ywa yt os topthi sinFniteloop.
De pendi ngony ourpr ope nsityt owa r
dbe inghy pnot i
z e
d,y ouma ylose
t
ouc hwi t
ht hewor ldf ori nde Fnitepe riodsoft imewhi ler unni ngt hi
spr ogram.
Att hei nvers ea nd Sa s hing e nd,i t’
sa ls
or ema rkablyg ood a ts ti
mula t
ing
migrainehe adache sinr ec ordt ime .Byc ar
efull
yc ontrollingt hepa ddle,youc an
al
soobs e rves omei nt erestingbi tsofAS CIItrivia.Fore xampl e, a
4e rtheinv e
rse
andSa shingr ange,y ous houl dbea bletos t
opt heSowbymov i
ngi ntothec on-
t
rolc har ac
terr ang e.Wi ths uWc ientde xte
rity
,y ous houl dbea blet olockont o
t
hepe rsistentbe epoft hebe l
l (
<CTRL>G) .
Shor tl
ya 4e rthispoi nt ,thes cr
e enwi llz
ipi ntomot ionwhe nyouhi ttheline
f
e e
dc ha racter(<CTRL>J)a nd, ofc our se,al
soa t<CTRL>M( carriagere t
urn).Wha t
f
un,e h!Whe nnor ma lcha racterout putr eturnsa syoupa sstheha l
fwaypoi nt
,
youc ande lighti nv ariouspa tter
nsofs creenFl l
ing .
Why ,youma ye venwi s
ht o
t
rywr itingy ourna mebyde 4c ontrol ofthepaddl e–c hild’splay!

Pa
ddl
ePr
ogr
am Pr
obl
ems
Returningt or ea
lityhe r
e,itiswor t
hme ntioningtha tsomepr oble
msi n
a
ccurac
yc ana ris
efrom r epe at
edlyreadingthepa ddlesoqui ckly.⇢ea na l
ogc i
r-
c
uit
sdon’ thavetimet ore turnto0 ,andv ari
ouspr oblemsc reepin.
Als
o,weha vebee nabi tnegli
genti nlookingoutf orc onSicti
ngus eoft he
r
egi
ster
sbyt heva r
iousr out i
neswea r
ec al
ling.⇢e r
eiso4e nnoa ssurancethat
t
heregi
s t
ery ou’r
eus ingf ory ourownr outi
newon’ tbec l
obbe re
dbyt heMoni -
t
orroutiney ouus e.Int hec a
seoft hepa ddlea ndout putr outi
nes,you’l
lnot e
t
heydidme nti
onhowt heX- Regist
er,theY- Registe
r,andt heAc cumul at
orwe re
Le
a cte
dbye achoft herout ines.
Forther ecord,hereisar ea
sonablefacsimileofourpr ogr a
mi nAppl eso4:
10 HOME
20 T = PDL(0)
30 PRINT CHR$(T);
40 GOTO 20

I
tisal
soworthment
ioni
ngtha
touras
sembly-
lang
uagevers
iont
akesei
gh-
t
eenbyt
es,
whil
etheAppl
eso4onet
ake
sthi
rt
y-e
ight,
notcount
ingspa
ceuse
dby
t
hevar
iabl
eT.

]
3
4 As
sembl
yLi
nes

Ex e
c ut
ionspeedmayseemtobes
imi
la
r,butt
hisisbec
auseofthepr
int
ing
ofthec haract
erstothescre
en.I
nmostcas
es,machineexe
cuti
onwouldbe
order
sofma g
nitudef
ast
er.

Tr
ans
ferComma
nds
Inourpr ogram,weha vetogothr oughar at
herinele
g antwayoft ra
nsfer-
ringthev a
luef r
om t heY-Re gi
ste
rtot heAc cumulat
or,usingat e
mpor arystor-
ageby te.Fortunately,t
he reisane asierwa y.⇢e r
ea r
ef ourcomma ndsf or
transf
erri
ngc ontentsoftheX- Regis
terort heY-Regi
stertoa ndfrom theAcc u-
mul at
or.⇢e ya r
ea sfoll
ows :
TXA:TransferscontentsofX-Regis
te rtoAccumulat
or.
TYA:TransferscontentsofY-Regi
ste rtoAccumulat
or.
TAX:TransferscontentsofAccumul atortoX-Reg
ister
.
TAY:TransferscontentsofAccumul atortoY-Reg
ister
.
Ea chofthes
ea cti
onsc onditi
onsthezer oSa guponexecuti
on, soi
tispossibl
et o
testwhathasbe entransf
e r
red.⇢e rei
snoc omma ndtotransferdi
rect
lybetwee n
theX- Regi
ste
ra ndt heY-Re gi
st
er.
⇢i sgive
susa nevenshor t
erprogram:
1 ********************************
2* AL05-PADDLE PROGRAM 1A *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7 PREAD EQU $FB1E
8 HOME EQU $FC58
9 COUT EQU $FDED
10 *
11 START JSR HOME
12 LDX #$00
13 LOOP JSR PREAD
14 TYA
15 JSR COUT
16 JMP LOOP
17 * INFINITE LOOP

Nowi
t’
sonl
yF4e
enby
tesl
ong
!
*300L
0300- 20 58 FC JSR $FC58
0303- A2 00 LDX #$00
0305- 20 1E FB JSR $FB1E
0308- 98 TYA
0309- 20 ED FD JSR $FDED
030C- 4C 05 03 JMP $0305

Withtwent
ycommandsaty
ourdispos
a l
,younowknowjus
tov
erat
hir
dof
t
het
ota
lvoca
bula
ryoft
hela
nguage
.Soon,you’
llbeda
nger
ous
!
5
.Loops
,Br
anc
hes
,COUT,
andPa
ddl
es 3
5

ANot
eaboutBRUNa
ndCOUT
I
fyout rytoBRUNAL05. LOOP2B, ra
thertha nus eaCALL768or300G, s
trang e
thingswi llha ppe n.⇢i si sbe causeDOSi nte rf
ereswitha nybi narypr og ram
whi chus esinputorout putr outineswhe ns uchapr ogrami sBRUN,ra t
he rtha n
ca l
ledfrom t heMoni t
ororBAS IC.⇢i sisbe causeDOSi sal
wa yswa tchingCOUT
forDOSc omma nds,sucha sPRINT D$;"CATALOG".Whe ny ouBRUNaFl ey ou
aree ss
entia l
lyinaDOSs ubroutine,andfur t
he ruseofCOUTma kesDOSmor eor
lessforgetwhe r etor e
turntowhe ne ver
ythingi scompl e
ted.⇢e rea retwos olu-
tionstot hispr oblem.⇢eFr s
tist r
ivia
l–don’ tBRUNFl esthatus eCOUT. Instead,
BLOADt heFl ea ndt hencallt
her outineintheus ua lway.
I
f,howe ver,youi nsi
stonBRUNi ngaFl e,theot herchoiceist oex i
tv iathe
wa rm-re entryv ector$3D0.Aj umpt othisa ddressreplace
st heFna lRTSi na ny
pr ogra
my ouwi s
ht oBRUN.Fore xampl e
,r eplaci
ngl i
ne1 9inLOOPPROGRAM2B
wi thJMP$3D0wi llall
owy out oBRUNt heFl ewi thnopr oble
ms .Pleasek eept his
inmi ndwhe na t
tempt i
ngtoBRUNa nyothe rlis
tingsthroughoutthisbook .

]
6
6
. I
/OUs
ingMo
nit
orandKe
ybo
ards
Ma
rch1
981

Compa
ris
ons
;Re
adi
ngt
heKe
yboa
rd
Nowwe ’regett
ingtowhe rewec ana ctuall
ydos omeintere
sti
ngthingswit
h
wha twek now sof ar
.⇢eba s
icideasy ous houldbec omfort
ablewithatthis
pointarefai
rlysi
mpl e.⇢e6 502mi croprocessorisourma inoperat
ionalunit
.
⇢e rearethreema i
nr egi
st
e rs
:theAc c
umul at
or,theX-Regis
ter,a
ndtheY- Reg-
ist
er.Als
opr e
sentistheStatusRegis
ter,whi c
hhol dsanumbe rofone-
bitSagsto
indica
tevari
ousc ondit
ions.Sof a
r,theonl yonewe ’
veconsi
deredistheZ-Sag,
forindic
ati
ngwhe t
heraz e
r oornonz eronumbe rispre
s e
ntinoneoft heother
threereg
ist
ers.

Progr
amsa ree xecut
edbyt he6 502s canningthroughme mor y.Addres
sesin
me moryarea nal
og oustolinenumbe r
si nBAS I
C. AJSR$FC58i na ss
emblylan-
guageisjustasv al
ida saGOSUB1000i nBAS IC.Inus ingana s
s emble
r,wec an
giv
ena mestor outi
ne satgi
v e
na ddressesa ndma kethingsthatmuc hsimple
rby
sayi
ngJSRHOME, whe nHOMEha sbeende Fne das$FC58.
Inchapte
rFv e, weusedtesti
ngc omma ndslikeBEQa ndBNEt ocreat
esimple
l
oops .Weus edtheX- Regi
sterandt heY- Re g
ist
era scounter
sa ndincremented
ordec r
ementedbyonef oreachc y
cleoft heloop.
Now let’
se xpandourr e
pe r
toir
eofc omma ndsbya ddings omene w ones
and,intheproc ess
,a ddsomeSe xi
bili
tyt owha twec andowi thloopsa ndtes
ts
ingeneral
.

]
3
8 As
sembl
yLi
nes

Inourpre
viouspr
ogr
amswerel
iedonourc ount
ersre
achi
ng0andte
sti
ng
v
iatheZ-Sa
gtot ak
eappr
opr
iat
eact
ion.Suppos
e,howeve
r,tha
twewis
htotes
t
f
oraval
ueotherthan0
.⇢isi
sdoneusingtwonewidea
s.

Compa
reComma
ndsa
ndCa
rryFl
ag
⇢eFr stist
hecomparecommand,themnemonicforwhi
c hisCMP.⇢is
te
llsthecompute
rtocompa r
etheconte
ntsoft
heAc c
umulat
ora g
a i
nstsome
otherval
ue.⇢eotherval
uecanbespeci
Fedinava
rie
tyofways.As i
mpletes
t
agains
taspeci
Fcval
uewoul
dlookl
ikethi
s:
CMP #$A0

⇢i swoul
dber ead,“CompareAc c
umul at
orwit
hanimme di
ateA0.
”⇢i swould
te
llthe65
02toc ompa r
etheAccumulatortot
hespec
iFcva
lue$A0.Ontheother
hand,wema ywa nttocomparetheAc cumula
torwit
hthec onte
ntsofgive
n
me moryl
oca
tion.⇢i swouldbeindic
atedby:
CMP $A0

Inthiscase ,the6 502wouldg ot ol


oc at
ion$A0, s
eewha twa st
her e
, a
ndc ompare
thatt
ot heAc cumulator.Itisi
mpor t
anttounde rs
tandt ha tt
hec ontentsof$A0
ma ybea ny thingfrom $00t o$FF,a nditisaga
instthisv a
luetha ttheAc c
umula-
torwillbec ompa red.Ine achcase,the65 0
2doe sthec ompa ris
onbyi nter
nal
ly
subtr
acti
ngt hes peci
Fe dvaluefrom theAc c
umul at
or.⇢eAc cumul at
orrema i
ns
unchange d,howe ver,
andt heresul
toft hecompa r
isonisr eSec
tede ls
ewhe re
.
⇢es econdi mportantideaisthatoftheca r
ryCa g.⇢ec arrySage nabl
esus
todetermi net her e
sultofthec ompa ri
son.Rightnextt otheZ- Sagint heStat
us
Regis
terist hebi tcal
ledthec ar
ry.

⇢ec arryisus eddur i


ngaddi t
iona ndsubtra
c t
ionbythe6 50
2 .Inourcase
,
sincethecompa reope rat
ioninvolvessubtra
cti
on,t hecar
rySa gcanbeus edto
testtheres
ult.Youdot hiswitht wone wbranchc omma nds,BCC andBCS.BCC
standsforBranc hCa rryClear
.Ift heAc cumulat
ori sle
ssthanthev al
uec om-
pa r
eda gai
nst,BCC willbrancha ppropria
tel
y.BCSs ta
ndsforBr anchCa r
ryS e
t
andi stake
nwhe ne ve
rt heAc c
umul atorisequalt oorg r
eaterthanthev al
ue
used.⇢i sme anst ha
twec annownotonl ytestforspeci
Fcv a
luesbutalsotes
t
forranges
.Tryt hisexample.
6
.I/
OUs
ingMoni
tora
ndKe
yboa
rds 3
9

1 ********************************
2* AL06-PADDLE PROGRAM 2A *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 PREAD EQU $FB1E
9 HOME EQU $FC58
10 COUT EQU $FDED
11 *
12 START JSR HOME
13 LDX #$00
14 LOOP JSR PREAD
15 TYA
16 CMP #$C1 ; CMP TO ASCII FOR "A"
17 BCC LOOP ; TRY AGAIN IF LESS THAN
18 CMP #$DB ; CMP TO ASCII FOR "["="Z"+1
19 BCS LOOP
20 JSR COUT
21 JMP LOOP
22 * INFINITE LOOP

Whe
nas
sembl
eda
ndl
is
tedf
rom me
mor
y,i
tshoul
dlookl
iket
his
:
*300L
0300- 20 58 FC JSR $FC58
0303- A2 00 LDX #$00
0305- 20 1E FB JSR $FB1E
0308- 98 TYA
0309- C9 C1 CMP #$C1
030B- 90 F8 BCC $0305
030D- C9 DB CMP #$DB
030F- B0 F4 BCS $0305
0311- 20 ED FD JSR $FDED
0314- 4C 05 03 JMP $0305

Let’
sst
ept hroughthepr ogram. A4ertheJSRt othec l
earscreenr outi
ne,we
loadXwi th0i npr e
pa r
ationforreadingapa ddle.⇢e#$00wi lltelltheroutine
thatwewi shtor eadpa ddle0.A4e rther e
ad,thea nswerisreturne di ntheY-
Regist
er,whic
hwet ransfertotheAc cumulat
orwi thaTYA. I
tisa tthispointthat
weus eourtests ec
ti
on.I ftheAc cumulatorisles
st hantheAS CI Iv al
uefort he
let
terA,wea v oidthepr intoutbyg oingbackt oLOOP.Iha v
eus edt heAS CII
val
uef orA plus$80s ot hatweg etnor maloutputont hes cr
een.I fwet estfor
$41instea
d,Sas hi
ngc haract
erswillbeoutputtothes c
reen.

]
4
0 As
sembl
yLi
nes

⇢ene xtcompa ris


oni sfortheAS CIIvaluef orthec har
acter“[”.⇢i sc
om-
paris
ona ssure
stha ttheBCSwi l
lcatcha l
lval
ue shi gherthantheonef orZ.⇢e
Frsttabl
einAppe ndixEi suse f
ulinseeingwhe ret hesenumbe rscomefrom.1

Onlynumbe r
sf rom $C1t o$DAwi l
lma kei tthrought obepr i
ntedoutusing
COUT($FDED).
Ag a
intheloopi sinFnite
, s
oRESETi srequire dtoe x
it.
⇢eX- Regis
terandY- Re gis
tercana l
sobec ompa redinas i
mi l
arma nne
rby
codesCPXa ndCPY. Ca nyour ewrit
ethisprogramt ouseCPYi nste
adofCMP?
BEQ andBNEa rea ls
os ti
llusablea4erac ompa reope ra
tion.He r
e’sasum-
ma ry:

Comma nd Acti
on
CMP ComparesAccumula
tortosome
thi
ng
CPX ComparesX-Regi
st
er
CPY ComparesY-Regi
st
er
BCC Bra
nchifreg
ist
er<value
BEQ Bra
nchifreg
ist
er=value
BNE Bra
nchifreg
ist
er<>va l
ue
BCS Bra
nchifreg
ist
er>=va l
ue

1AS
CII(
forAme
ric
anS
tanda
rdCodef
orI
nfor
mat
ionI
nte
rcha
nge
)isac
odi
ngs
che
me
fortra ns mitt
ingt e
x t.Iti salsous edint heAppl ef ore nc odi ngt ex tinme mor y,s c
re en
display ,diskFles,printe rout put,andma nyot hera reas.Appe ndi xEg ivesac hartofa l
l
thec ha ra c
tersandt he i
rAS CI Ivalues.Onei mpor tantnot e :itispos sibletoe nc odea l
l
thea lpha beti
cc haracters( uppe randlowe rc
as e),nume rics,spec i
a lsymbol s,andc ontrol
code sus ingonl y12 8c ha rac
te rs
.⇢i sme a nst ha tAS CIIi sc ons i
de reda7 -bitc ode .⇢i s
me anst hatalltheinf orma t
ionr equiredt ode termi newhi chc hara cterha sbe ens e
nti s
conta inedi nbi t
s0 76oft heby te
.⇢us$8Ai sr eas onabl ye quiv a
le ntto$ 0Aa sf ara si t
s
AS CIIi nt er
pr e
tat
ioni sc onc erned.⇢ema t
teroft hehi ghbi tbe ings etorc learc anc re-
atecons iderabl
econf usionwhe nitisnotma dec l
e arwha tthec omput ere xpects .
Ge ne ral
lytheAppl eope ra te
sinternal
lywi t ht hehi g hbi tsetona l
lc haracters.⇢a tis
tos ay,c ha ra
c t
ersretriev edf rom thek eyboa rdv ia$C000a ndc ha racterss t
or edi nt he
screena reaofme mor ya ndondi s
ka l
lus uall
yha vet hehi ghbi tse t(i.
e .av al
uee qualt o
org rea terthan$80) . ⇢i sisa lsothewa yAppl eso4s t
or esda tawi thinpr og r
aml i
ne s.To
keepy ouony ourtoe s,thoug h,Applepr interc a rdsus uallydonots uppor tha vi
ngt he
highbi ts e
twhe ns endingout putt oapr inter,a nds tri
ng swi t
hinapr og r
a m( suc ha sA$
=”CAT”)a l
soha vet hehi ghbi tcle
a r
.Al so,whe nus ingCOUT( t
heMoni tortex tout put
routine ),thehi ghbi ts houldbes et(a l
wa ysl oa dt heAc cumul at
orwi thv al
ue sg reater
than$80)be forecall
ingCOUT.
Iwi shIc oulds ayi twa sa lleasi
ertha nt ha t,butt hena gaini fi twe rea l
lt ha teas y
,
youwoul dn’thavet oha v eboug htthisbook ,a ndt henwhe rewoul dIbe ?
6
.I/
OUs
ingMoni
tora
ndKe
yboa
rds 4
1

Us
ingMoni
torPr
ogr
amsf
orI
/ORout
ine
s
Asy ouma yha venot iced, Ie njoyus i
ngt hepa ddle sa sinputde vic
e s.⇢i si s
becaus et hey ’
reane as
ywa yofs endi ngv aluesf rom $00t o$FFi ntot hes ystemi n
av erys moot ha ndna tura lwa y.Wec a ng e ts i
mi l
arda taf rom t hek e yboar d,
thoug h. ⇢e rethea dv ant a
g ei stha twec anj umpf rom onev aluetoa not herwi th
not rans i
tionbe t
we ent het wov al
ue s.
Ag oodpa rtofma nyf or ma la s s
embl y-la nguagec our sesde a
lswi ths yste m
I/O–t ha tis,gett
ingda taina ndoutv iadi Le rentde vices. Wr it
ings ucht hing sa s
printerdr i
v er
s,diskort apea ccessr outine s,ha rdwarei nt erfa
ces o4wa re, et
c.,a re
thea reast ha tha rdcorepr og ra mme rss pendt heiry out hsma ster
ing .Us ingt he
Moni torr outinesont heAppl es impl iFest hi sforusg rea t
lybe causewedon’ t
havet odoal otofI /O de tails.You’ v
ea l
readys hownt hi sbyus i
ngt hepa ddl es
($FB1E)f ori nputa ndt hes c reen( $FDED)f orout putwi thoutha v i
ngt ok now
any t
hinga bouthow t hea ctua lope rati
oni sc arrie
dout .⇢ek eyboa rdi se ven
easie
r.
Ime ntionede arl
iert ha tthea ddr essr ang efrom $C000t o$FFFFi sde vote d
toha rdwa re–t heseme mor yr ang esc annotbea lt
eredbyr unni ngpr og rams .(I’m
ignoringt heRAM c ardsf ort het imebe i
ng .
)⇢er a
ng ef rom $D000t o$FFFFi s
usedbyROM r outinest ha twe ’
vebe enc all
ing . ⇢er ang ef rom $C000t o$CFFFi s
assig
ne dt oI /O de vi
ces .Ty pi call
yt hes econddi git(orma y
beIs houldc al
li ta
hexit)f rom t hele 4g ivesust hes lotnumbe roft hede v ice.Fori nstanc e,ify ou
haveapr interins lotone ,listingt hec odea t$C100wi llr evealthema chinel an-
guagec odeonROM oft hec a r
dt hatma ke si twork .At$C600y ou’llpr obabl y
Fndt hec odet hatma kest hedi skdr iveins lots ixboot .
$C000 t o$C0FFi sr e s
e rvednotf ors l
ot0 ,butf ordoi ngs pe c
ialthing swi th
theha rdwa repor ti
onsoft heAppl ei tsel
f.Ana tte
mptt odi sassembl efrom $C000
willnotpr oduc ear ecog niza blelisting,buti twi l
lpr oba blyc ausey ourAppl et o
actabi todd.⇢i sr ang eisma deupofanumbe rofme mor yloca t
ionsa ct
ua lly
wiredt ophy sicalpa rt
sofy ourAppl e .I
fy out y pein:
*C030

from theMoni tor


,ina dditi
ont og ett
ingsomer andom valuedisplaye
d,the
speakershouldc li
ck.Ifitdoesn’
tc l
icktheFr stti
me ,t
rya g
ain.Eacht i
mey ou
access$C030,thespeakerwil
lclic
ka sitmov e
sinr es
ponsetoyoura c
tion.
⇢ek eyboardisal
sot i
edintoas peciFcl
oc at
ion.Bylooki
nga ttheconte
nts
of$C000, youc antel
lifak e
yha sbeenpr ess
ed. I
nBAS IC,it
’sdonewi thaPEEK
-16384.(Seepa ge6oft he1981Ap pleIIRe fe
renceMa nual.
)Ina s
s embl
ylan-
guagey ouwoul dusual
lyloadar egi
ste
rwi ththecontent
sof$C000,sucha s
:
LDA $C000

]
4
2 As
sembl
yLi
nes

Re
adi
ngDa
taf
rom t
heKe
yboa
rd
Be causeitisdi Wc ultt or eadt hek e yboarda texac t
lytheins tants ome oneha s
pressedt heke y,thek ey boa rdi sde s
ig ne dtohol dthel astkeypr essedunt ilei
the r
anothe rk e
yi spr essedorunt ilyouc learthes trobe,asit’scal
le d, bya cc
e ssi
nga n
alt
erna teme mor yloc ation, $C010. ⇢es trobeiswi redt ocle
a ra nyc ha racte
rsoL
thek eyboa r
dt ha tma ybeha nginga roundf ora nynumbe rofv ari
ousr eas
ons .
Whe ny ouc hec kf orac ha racter,y oudon’ twa ntt opi ckoneupt hats ome one
inadvertentlye nter edpr iort oy oure nqui r
y( perhapsbyne rv ous l
ydr ummi ng
theirFng ersa c
r osst hek e yboa rdwhi l
ewa i
ti
ngf oroneofAppl e’slightning-lik
e
diska ccesses
!).I tisa lsoa lwa ysag oodi deat oc l
eart hekey boa r dwhe ny ou’ r
e
donewi thit,ot her wisey ouma ys imi larl
yha v ethek eypress edf ory ouri nput
sti
llha ng i
nga roundf orwha teverr e adst hek eyboa rdne xt,s uc ha sa ni nput
stat
eme ntinBAS IC. ⇢es t
r obei sclea redbye itherar eadorawr iteope rati
on. It
istheme r
ea cce ssto$C010i na nyma nne rthata ccompl i
she st hec lear.⇢usa
LDA$C010wo uldwor kj usta swe lla saSTA$C010. 2⇢el as
tpoi ntt obea wareof
isthatthek eyboa rdi ss e tupt ote l
ly ouwhe nak eyispr ess
edbyt hev al
uet ha tis
reada t$C000. Now,y oumi ghtt hinkt hatthelog ica
lwa ywoul dbet ok eepa$0
in$C000.Pe rha ps ,butt hat’snott hewa ytheydoi t
.Instead,t heya dd$80t o
wha tevertheAS CI Iv aluei soft hek eyy oupr essed.I fav a
luel essthan$80i sa t
$C000, itme ansak eyha snotbe enpr essed.
So,t oill
ust ratet his( a
ndIa dmi ti tg otalittl
einv olvedf ormyt astes)
,le t
’s
looka tsomes ampl epr og ramst or eadda t
af r
om t hek eyboard.
1 ********************************
2 * AL06-KEYBOARD PROGRAM 1A *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 KYBD EQU $C000
9 STROBE EQU $C010
10 COUT EQU $FDED

2Ha
vingnowj
usts
aidt
hatr
eada
ndwr
it
eope
rat
ionsa
ree
sse
nti
al
lye
qui
val
entf
orc
lea
r-
ingthestr
obe ,letmec overmy s
elfenoug ht os aythattherei sones l
ightdiLere nce.A
writ
eope ra
tiona ctual
lya ccesse
st hel oca t
iont wice
,whe reasar eadope rat
iononl y
acces
sesonce .Mos tofthet imet hisdoe sn’tma kea nydi Lerence.Sincemos tpe ople
can’
ttypeat1 0 0,
0 00chara c
terspers ec
ond,i t
’shardt ogetac haract
erinbe twe ent he
twoc l
earope rati
ons .Howe ver,therearenowa vail
ablefort heAppl ek e
yb oar
db uEe r
s
whichwills
tor eawhol estringofc haracte
r senteredbytheus er,
inst
eadoft heus ualone
normall
yuse df orthek eyboard.Ase achc haracterisreadin, i
tistakenoutoft hebuLe r
byc l
eari
ngt hes trobe.Youg uessedit!A wr i
teope rat
ion–s ucha saSTA$C010ora
POKE-16368,0–wi l
lcle
a rtwoc haracter
soutoft hebuLe r:theoney ouj ustrea da nd
thenextonei nl ine.⇢e r ef
ore,itisg e
ne rall
yg oodpr act
icet oc l
earthes tr
obewi tha
readoperat
ion, sucha saLDA$C010, X=PEEK-16368, ort hel i
ke.LikeIsaid,ifitwe re
tooeasy
..
.
6
.I/
OUs
ingMoni
tora
ndKe
yboa
rds 4
3

11 HOME EQU $FC58


12 *
13 START JSR HOME
14 LOOP LDA KYBD
15 CMP #$80
16 BCC LOOP
17 JSR COUT
18 JMP LOOP
19 * INFINITE LOOP

Onc
eent
ere
d,t
hiss
houl
ddi
sas
sembl
eas
:
*300L
0300- 20 58 FC JSR $FC58
0303- AD 00 C0 LDA $C000
0306- C9 80 CMP #$80
0308- 90 F9 BCC $0303
030A- 20 ED FD JSR $FDED
030D- 4C 03 03 JMP $0303

Tryi
ngthisprogr
a m,youshoul
dnot i
cethatt
heprog
ramrunson,pr
int
ingthe
samecharac
teruntily
oupressanot
herkey.⇢at’
sbec
aus
ewenevercl
ear
edthat
st
robeyout houghtIwa sra
mbl i
ngona bout
.Oncethekeypre
ssget
sont he
board,
it’
sneverc
learedunt
ili
tisre
pla
cedbyanewk e
y.
Abe tt
erprogramis:
1 ********************************
2 * AL06-KEYBOARD PROGRAM 1B *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 KYBD EQU $C000
9 STROBE EQU $C010
10 COUT EQU $FDED
11 HOME EQU $FC58
12 *
13 START JSR HOME
14 LOOP LDA KYBD
15 CMP #$80
16 BCC LOOP
17 STA STROBE
18 JSR COUT
19 JMP LOOP
20 * INFINITE LOOP

whi
chl
is
tsa
s:
*300L
0300- 20 58 FC JSR $FC58
0303- AD 00 C0 LDA $C000
0306- C9 80 CMP #$80
0308- 90 F9 BCC $0303

]
4
4 As
sembl
yLi
nes

030A- 8D 10 C0 STA $C010


030D- 20 ED FD JSR $FDED
0310- 4C 03 03 JMP $0303

⇢i ss houl dwor kbe tte


r.He rewec lea
rt hek ey boa rdwhe neve rwe ’veg ottena
chara cte
ra ndpr inte dit.Whynotc lea
ri tr ighta 4e rt her e a
donl ine1 5 ?Ifwe
didt ha t
,we ’
dbel uc kyt oc atchag l
imps eoft hec hara ctera t$C000a st heus e
r
pr e
ssedt hek e y.Asi tis,wec a npr obablyg eta wa ywi t
hi tbe caus eoft hes peedof
theloop.Buti fweha dt og oa waytoa not he rrout inef orawhi l
e,orot herwise
de l
ayg ettingba ckt ot heLDA$C000, we’dpr obablymi ssi t.
Yous houlda lsot ypeine noug htowr apa roundont ot hene x tli
ne ,anda l
so
trythea rrow k ey sa nd<RETURN>.Youma yt hinkt hisa llpe rformsa se xpected
(witht hee xcepti
onoft hemi ssingcursor ),butt hisa llshoul dnotbet akenf or
grante d.Wi t
houtt hes creenma nageme ntofCOUT,y ou’ dha v et odoqui teabi t
mor epr og rammi ngt ok eept hi ngsstra
ig ht.Onc emor e,thisi sthea dvantag eof
us i
ngt her outinesa lreadypr ese ntintheMoni torr athe rtha nwor ryinga boutt he
de t
ailsyour self
.
Al so,pl easenot i
cehowt heSTAwa sc hose nbe c ausewedi dn’ twa ntt olose
thec onte ntsoft heAc cumul at orindoingt hea ccess.⇢i si nfor ma t
ionc onc erns
techniquemor et hana ct
ua lcomma nds, buti swor thme ntioningi fy ou’reg oing
tog eta l
ongwi thy ourAppl es uc ce
ssf
ully.
Onpa ge1 30oft he1 9 81Ap pl
eIIRe ferenceMa nua ly ou’llFndal isti
ngof
thes o4-s witchesa ndot he rgoodi e
sat$C000t oC0FF. ⇢e seca nbev eryus efulin
ha vi
ngy ourAppl er elat
et otheout s
idewor ld.
Youma ywi sht oe xperime ntwiththe se .
Al sodon’ tfor geta bouta llther ou-
tineslistedi nAppe ndixD.⇢e s
ea r
ea l
s of unt oe xpe ri
me ntwi t
ha nda repr o-
videdt oe ncourag ey out owr iteshortpr og ramsj usttot estyourwi ngs.AsI ’
ve
me ntione dbe fore ,the y
’rea l
sous ef
ulins av i
ngy out het roubl eofwr iti
ngy our
ownI /Oa ndot hermor einv olv edrouti
ne s.
7
7
. Addr
ess
ingMo
des
Ap
ril
198
1

Let’
slooka tthev ari
ousaddre s
si
ngmode sus edina ss
embl y -
lang uagepro-
gra
mmi ng.⇢i sc onceptisrat
he rfundame ntalinpr ogrammi nga ndy ouma y
j
ustiFablywonde rwhyweha v
enotc overeditsoone r.We l
l,asi tha ppens,we
have;Ijustdidn’
tc al
litbynamea tthetime. I
nc hapteronewel a idoutt hebasic
st
ruc t
ureofs ix
ty-Fvethousandindividualme mor ylocati
ons.S incet hen,we’ve
work e
dmos tofourma gicbys i
mpl yma nipul
a t
ingthec ontentsoft hoseloca-
ti
ons .
Flexi
bil
it
yi nthewa ysinwhichy ouc anaddr es
sthe s
elocationsi sthek e
yt o
eveng r
eaterpowe rinyourownpr ogr
ams .
Cons i
derthischartoftheaddressi
ngmode sa vai
lableonthe6 502:

Addre s
singModes Ex
ampl
e He
xBy
tes
Imme diat
e LDA #$A0 A9 A0
Absolute LDA $7FA AD FA 07
ZeroPa g
e LDA $80 A5 80
Impli
c i
t/
Impli
ed TAY A8
Rela
tive BCC $3360 90 0F
Indexed LDA $200,X BD 00 02
Indi
rectInde
xed LDA ($80),Y B1 80
IndexedIndi
rec
t LDA ($80,X) A1 80

Inlookinga tthee xamples,yous houl dFnda llbutt helastthreev eryf amil


iar
.
Weha veus ede ac
hoft hemi nprev i
ouspr ograms .
I
mme diatemo dewa susedt ol oadar egis
terwi thas peciFcv alue.Inmos t
asse
mbl er
s,t hisi
si ndica
tedbyt heus eoft henumbe rsi
g n( #)pr ecedingthe
val
uet obel oa d.⇢i
de scontras
tswi ththea bsolutemo deinwhi cht hev al
ueis
ret
rie
vedf rom ag ivenme mor yloca t
ion.Inthismode ,theex actaddr e
ssy ou’
re
int
eres
tedi ni sg i
ven.Zerop agei sjustav ari
ationont hea bsolutemode .⇢e
ma i
ndiLe renceisthenumbe rofby t
esus edint hec odi
ng.Itt akesthre einthe
genera
lc a
s e;i
nz er
opa ge,onlytwoa r
er e
quired.
I
mp li
cit,orimp li
ed,iscerta
inlyt hemos tc ompa ctinstr
uc t
ioni nt hatonly
oneby t
ei sus ed.⇢eTAYc omma nd,Tr ansf
erAc c umulatortot heY- Regis
ter
,

]
4
6 As
sembl
yLi
nes

ne edsnoa ddit
ionaladdr es sbytesbe c
ausethes ourcea ndde sti
nat
ionoft hedata
arei mpl i
edbyt heveryi ns truct
ioni t
self
.
Relativeaddres
singi sdonei nr el
ati
ont owhe retheFr stbyt
eoft heinst
ruc-
tioni ts
e l
fisfound.Al thoug hthee x
ampl eint
er pr
etsita sabr ancht oas pe
ciFc
addr ess,you’ll
noticetha tthea ctualhexc odeisme relyapl usormi nusdispl
ac e
-
me ntfrom thebranchpoi nt .⇢ist oowa scoveredpre viously.
Wi ththeseaddressingmode s,wec ancreat
equi teav a r
iet
yofpr ogr
ams .⇢e
pr oblem wi ththesemode si st
ha tthepr ogra
msa rer atherinSexi
bl ewithda t
a
from t heout si
dewor l
d,s ucha sthos eininputr outine s,anda l
sowhe ndoi ng
thing sli
k eaccess
ingtable sa ndlargeblocksofda ta
.

I
nde
xedAddr
ess
ing
Toa cc esss uchda ta,wei ntroduc ethene wi de aofinde xeda ddr ess
ing.Int he
purefor m, thec ontentsoft heX- Re gi
sterorY- Regist
era rea ddedt ot headdr ess
giveni nthei ns tructi
ont odetermi net heFna la ddress.Int hee xampl eg iven,if
theX- Reg i
sterhol dsa$0,t heAc c umul at
orwi llbeloade dwi tht hec ontentsof
locat
ion$200.I f,instead,theX- Re gist
erhol dsa$04,t he nlocation$204wi l
lbe
access
e d.⇢eus e f
ulnessina ccessingt a
blesa ndt helikeshoul dbeobv ious.
⇢epr oble mt hata r
isesher eoc curswhe ny ouwa ntt oa c
c essat ablet hat
growsors hr ink sdy nami call
ya st heda tawi thini tcha nges.Anot herpr oblem
occurswhe nt het ableg rowsl a
rg erthan2 5 6by tes.Becaus ethema ximum oLs et
possi
bl eusingt heX- Re gist
erorY- Re gi
steris2 55,wewoul dnor ma ll
ybeoutof
luck.
⇢es olut iont ot heby telimiti stous et hei ndir
ecti ndexedmode .Indirect
indexedi sre allya ne l
eg antme t
hod.Fi rst
,the6 502goe st otheg ive nzero-pa ge
locat
ion( theba sea ddressmus tbeonpa gez ero) .I
nthee xample ,itwoul dg ot o
$80 a nd$81 t og e tthel ow-orde ra ndhi gh-or derby t
e soft hea ddresss t
or ed
there.⇢e ni ta ddst hev alueoftheY- Re g
istertot hataddr ess
.
7
.Addr
ess
ingMode
s 4
7

O4times,thesetwo- bytezero-pagea ddresspair


sa r
ec a
lle
dp ointe
rs,a ndyou
willhe arthemr efer
redt oinde al
ingwi thvari
ouspr ogramsont heAppl e.In
fa
c t,bylookinga tpa ges1 4
0t o1 41oft heAp ples
oftIIBAS I
CPr ogrammi ngRe f
-
er
e nc eMa nual,y ouwi llobservequi teanumbe roft hes
eby t
epa i
rsus e
dby
Appl eso4tok eept ra
c kofa l
lsortsofc ontinual
lychangingthings,l
ikewhe r
ethe
prog ramis,theloc at
ionsofs t
ringsandot hervari
abl
es,andma nyni4yitems .
Ifwewa ntedt os i
mul at
etheLDA$200,Xc omma ndwi t
ht heindire
c tmode,
wewoul dFr ststorea#$00i n$80a nda#$02i n$81,wi th00a nd02be ingthe
l
ow- orderandhi gh-orderby t
e soft hea ddress$200.⇢e nwe ’
dus et hec om-
ma ndLDA($80),Y.
Amuc hbe tt
er( butunfortuna t
elyr arel
yus e
d)t er
mi sp ost
-i
ndexing ,re
fer
-
ri
ngt othefactthatthei ndexisadde da rt
fte hebaseaddr e
ssisdetermined.

Some
time
sXa
ndYAr
en’
tInt
erc
hang
eabl
e
Youma yha venot i
cedt ha tIus edt heX- Re gis t
e rinonec a sea ndt heY- Re g-
ist
eri nt heot he r.Itt ur nsoutt hatt heX- Re giste ra ndt heY- Re gist
e rc annot
alwa ysbeus edi ntercha ng eabl y.⇢edi Le r enc es howsupde pe ndingonwhi c h
addr ess
ingmodea ndwha ta ct ualc omma ndy oua reus ing( LDA, STX, orot hers )
.
Asi tha ppe ns,i ndirec tinde x eda ddr essingc anonl ybedoneus ingt heY- Re gis-
ter.
Tok nowwhi cha ddr essingmode sc anbeus edwi thag ivenc omma nd,y ou
canr efertoe i
the roft woa ppe ndi cespr ov ideda ttheba ckoft hisbook .Appe ndi x
Bi sr at
herl ikeadi c
tiona ryofa llthepos sible6 5 02c omma ndsa ndde v otessev-
eralpa ragraphst oe achc omma nd.Appe ndi xC,ont heot he rha nd,i samor e
conde nsedf or m oft heFr sta ppe ndixa ndma yma k eite asie rt oc ompa rea vail-
ablemode sbe twe enav arietyofc omma nds .
Ihighl yr ecomme ndma k i
ngf reque ntus eofAppe ndi xBwhi l
ey oua re
learninga sse mbl y-lang uag epr og r ammi ng . I
ti se ssent i
allyy ourt ool boxofa vail-
ablec omma ndsf ors olvingapa rticularpr og rammi ngpr obl em.Whe ne very ou
tryt owr iteapa rtic
ul arr out inea nda re n’ts urej usthow t oa ppr oa c
hi t,s ki
m
throug ht hiss e cti
onofa l
lpos si
bl ecomma ndsa nds eei fa nypa r ti
cularc om-
ma ndi nspi resapos siblea ppr oa ch. Gra nt ed,t hisi slikelyt oha ppe nmor ewhe n
you’rewor kingonr athe rs impl eg oalss uc ha smov ingaby tef rom he r
et othe re,
bute vent hel ar gestpr ogra msa rema deupofj us ts uc hsimpl es tepsa sthat.
⇢el a sta ddr ess
ingmode ,inde xedi ndi r
e c
t ,ispr oba blyt hemos tunus ua l
.I n
thisc as
e,t hec ont e
nt soft heX- Re g i
ster( t heY- Re gisterc annotbeus edf orthi s
mode )area dde dt ot heba sea ddr essbe for eg oi ngt og ett hec ont ent s
.I nac a se
similartot heot herone ,ift heX- Registe rhe l
d$0,a nLDA($80,X)woul dg ot o
$80 a nd$81f ort het wo- bytea ddr essa ndt he nl oadt heAc c umul atorwi t
ht he
cont ent
soft hei ndica tedl oc a t
ion.I f,i ns t
ead,t heX- Re g isterhe lda$04,t he
me mor ya ddr e sswoul dbede termi ne dbyt hec ont ent sof$84a nd$85!

]
4
8 As
sembl
yLi
nes

Usual
ly
,then,theX-Regis
teri
sloadedwit
hmul t
ipl
esoftwotoa cce
ssa
s
eri
esofconti
nuouspoint
ersinpag
ez e
ro.⇢isisa
lsocal
ledpr
e-i
nde
xingsince
t
heindexi
saddedtothezer
o-pagel
ocat
ionbe
for
edeter
miningt
hebas
ea ddres
s.

St
ori
ngPur
eDa
ta
Beforewec anputa llthisne wi nforma tiontowor k,wenowne edtoa ns wer
onemor equesti
on.Howdoy ous torej ustpur edatawithi napr og ram?Al lthe
comma ndswe ’
vec overe
ds of ara rea ctualcomma ndsfort he650 2.⇢e rei sno
datacomma nda ssuch.Wha tarea vailable
, t
houg h,aretheAs sembl e
rdirec ti
v e
s
ofyourpa r
tic
ularassembler.⇢e s
ewi l
lv aryfrom onea ssembl ertoa nothe r,so
you’l
lha vetoconsultyourownma nua ltos e
ehowy ouras semblerope rat
es.
Ing enera
l,thetheoryi stode Fneabl oc kofoneormor eby te
sofda taa nd
thentos kipov e
rtha tbl
oc kwi thabr anc horj umpi nst
r uc ti
onwhe ne x
ec uting
yourpr ogram.Us uall
y,datac anbee nt erede i
therashe xby te
sora stheAS CII
charact
e r
sy ouwi s
ht ous e
.I nt hes econdc ase
,thea s
sembl e rwillautoma ticall
y
tra
nslatetheAS CIIcharactersintot hepr operhe xnumbe r s.
Mos tassemblershavehe xc omma ndf ordirect
lyenter i
ngt hehe xby t
esofa
datatable.⇢eAppl eDOSTo olKi tassembl erisonee xception. I
tdoe snotha ve
theHEXc omma nd( norma nyot hers)a ndy oumus tuset heDFB( “deFneby te”
)
comma nd.Usingit,li
ne2 0ofthef ol l
owi nglisti
ngshouldr ead:20DATADFB$C1,
$D0,$D0, $CC,$C5.As ampl epr ogra m us i
ngt heindexeda ddressmodei sg iven
here:
1 ********************************
2 * AL07-SAMPLE DATA PROGRAM *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 COUT EQU $FDED
9*
10 START LDX #$00
7
.Addr
ess
ingMode
s 4
9

11 LOOP LDA DATA,X


12 JSR COUT
13 INX
14 CPX #$05
15 BCC LOOP
16 LDA #$8D
17 JSR COUT
18 EXIT RTS
19 *
20 DATA HEX C1D0D0CCC5
21 *
22 * DATA = 'APPLE'

Whe
nlook
eda
tinme
mor
y,i
tshoul
dappe
arl
iket
his
:
*300L
0300- A2 00 LDX #$00
0302- BD 13 03 LDA $0313,X
0305- 20 ED FD JSR $FDED
0308- E8 INX
0309- E0 05 CPX #$05
030B- 90 F5 BCC $0302
030D- A9 8D LDA #$8D
030F- 20 ED FD JSR $FDED
0312- 60 RTS
0313- C1 D0 CMP ($D0,X)
0315- D0 CC BNE $02E3
0317- C5 00 CMP $00

⇢i spr og rami sani mpr ove dvers i


onoft heonewedi dea rl
iertopr intthewor d
APPLEo nt hes creen.Itus esthei nde xe
da ddr essmodet os cant hrought heda ta
tabletopr i
ntt hewor dAPPLE.Not i
cet hatda t
at ablesma ybewi ldlyinterpreted
tothes cr ee
nwhe ndi sassembl i
ng .⇢i sisbe c auset heAppl eha snowa yofk now-
ingwha tpa rtoft hel i
sti
ngi sda taa ndt riest olistda taasaus ua lasse
mbl y-
lan-
guag epr ogram.
Ba sical
ly,t hei deaoft hepr og rami st ol oopt hroug h,g etti
ngs uc ces
sive
ite
msf rom theda tatableus i
ngtheoLs e toft heX- Re g i
ster.Whe ntheX- Re gis
ter
reache s05( t
henumbe rofi te
msi nt het a ble)
,wea reFni s
he dpr inti
ng .A4e r
printing,wet ermi natewi t
hac ar ri
ager etur n.Re me mbe rtha tina ssembl ylan-
guag ewemus tusua ll
ydoe v eryt
hi ngour s
elv es.
⇢i sme answec annota ssumea n
automa ticc arr
ia g
er eturna tthee ndofapr i
nt eds t
r i
ng .
Not ethatthehe xvaluesint heda tat ablea retheAS CI Ivalue sforeachl et
ter
plus$80. ⇢i sse t
st hehighbi tofe a
c hnumbe r,whi chi swha tt heAppl ee xpects
inor dert oha vethel ett
erpr i
ntedoutpr oper l
ywhe nus ingCOUT.
⇢ei ndirectaddr es
singmode sareus edwhe ny ouwa nttoa cc e
ssme mor yi n
av eryc ompa cta nde Wcientwa y.Le t’sc ons i
de rt hepr oblem ofc l
earingthe
screen,fori nstance .Wewa nttoputas pa cec harac t
e rine veryme moryl ocati
on
inthes cr ee
nbl ock( $4007$7FF) . He reisonewa yofdoi ngt hi
s:

]
5
0 As
sembl
yLi
nes

1 ********************************
2 * AL07-SCREEN CLEAR PROGRAM 1A *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 PTR EQU $06
9*
10 ENTRY LDA #$04
11 STA PTR+1
12 LDY #$00
13 STY PTR
14 * SETS PTR (6,7) TO $400
15 START LDA #$A0
16 LOOP STA (PTR),Y
17 INY
18 BNE LOOP
19 NXT INC PTR+1
20 LDA PTR+1
21 CMP #$08
22 BCC START
23 EXIT RTS

Li
st
edf
rom t
heMoni
tor
,its
houl
dappe
arl
iket
his
:
*300L
0300- A9 04 LDA #$04
0302- 85 07 STA $07
0304- A0 00 LDY #$00
0306- 84 06 STY $06
0308- A9 A0 LDA #$A0
030A- 91 06 STA ($06),Y
030C- C8 INY
030D- D0 FB BNE $030A
030F- E6 07 INC $07
0311- A5 07 LDA $07
0313- C9 08 CMP #$08
0315- 90 F1 BCC $0308
0317- 60 RTS

Wes ta
rtoLbyi nit
iali
zinglocat
ions$06 and$07t oholdt heba seaddressof
$400,theFrstbyteofthes c
reenme morya r
ea. ⇢e nwee nteral
oopt hatrunsthe
Y-Re gi
st
erfrom $00t o$FF.S i
ncethisisadde dtot heba seaddressin$06,$07,
wet henstorea n$A0( aspace)ineverylocati
onf rom $400t o$4FF.Whe nYi s
incr
e ment
e dfrom $FF,itgoesbackto$00,andt hisisdetectedbytheBNEonl ine
18.At$00, i
tfall
sthrougha ndlocat
ion$07i sincreme ntedfrom $04to$05, gi
v-
ingane wba seaddressof$500.⇢i swholepr ocessisrepeateduntill
ocat
ion$07
reac
he savalueof$08( corres
pondingtoaba seaddressof$800), atwhichpoint
wer
etur
nfr
om t
her
out
ine
.
7
.Addr
ess
ingMode
s 5
1

Bychangi
ngt
hev al
ueoft
he#$A0tosomeot
hercha
racte
r,wecanc
leart
he
s
cre
entoa nyc
har
act
erwewish.I
nfact
,youca
ngett
hevaluefr
om t
hekeyboa
rd
a
swe ’
vedonei
nearl
ie
rprogr
ams.
1 ********************************
2 * AL07-SCREEN CLEAR PROGRAM 1B *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 PTR EQU $06
9 CHAR EQU $08
10 KYBD EQU $C000
11 STROBE EQU $C010
12 *
13 ENTRY LDA #$04
14 STA PTR+1
15 LDY #$00
16 STY PTR
17 * SETS PTR (6,7) TO $400
18 READ LDA KYBD
19 CMP #$80 ; KEYPRESS?
20 BCC READ ; NO, TRY AGAIN
21 STA STROBE ; CLEAR KYBD STROBE
22 STA CHAR
23 CLEAR LDY #$00
24 LDA CHAR
25 LOOP STA (PTR),Y
26 INY
27 BNE LOOP
28 NXT INC PTR+1
29 LDA PTR+1
30 CMP #$08
31 BCC CLEAR
32 AGAIN JMP ENTRY

I
tshoul
dappe
arl
iket
hisi
nli
st
edf
orm:
*300L
0300- A9 04 LDA #$04
0302- 85 07 STA $07
0304- A0 00 LDY #$00
0306- 84 06 STY $06
0308- AD 00 C0 LDA $C000
030B- C9 80 CMP #$80
030D- 90 F9 BCC $0308
030F- 8D 10 C0 STA $C010
0312- 85 08 STA $08
0314- A0 00 LDY #$00
0316- A5 08 LDA $08
0318- 91 06 STA ($06),Y
031A- C8 INY
031B- D0 FB BNE $0318
031D- E6 07 INC $07

]
5
2 As
sembl
yLi
nes

031F- A5 07 LDA $07


0321- C9 08 CMP #$08
0323- 90 EF BCC $0314
0325- 4C 00 03 JMP $0300

Ent erthisprog rama ndr unf rom BAS I


Cwi thaCALL768.Ea chpr e
sswi l
lclear
thes cr
e entoadi Le rentc ha racter
. ⇢es cree
ns houl dcleartothes amec haracte
r
ast hek eyy oupr ess,i ncludi ngthe<SPACE>ba ra nds pecialchara c
ters.Int his
prog ra
me spe ci
ally,y ouc ans e
ehow f as
tma chi nel a
ng uagei s
.Toc learthe
screenr equiresl oa dingmor et ha nonet hous anddi Lerentloc a
tionswi ththe
givenv a l
ue.InAppl e so4,t hispr ocesswouldbequi tes l
ow byc ompa rison.In
assembl ylang uage ,y ou’ l
lFndt ha tthescreenwi llclea
rt odi Lerentc ha r
a c
ter
s
justasfa s
tasy ouc ant y pethe m.
Ani nterest
ingv ar i
a t
ionont hi sistoentert heg raphicsmodebyt y pingin
GRb eforec a
lli
ngt her out ine .
⇢e nt hescre
enwi ll clea
rtov ariousc ol
orsa nddi f-
fer
e ntlinepa t
terns .
I
nt hisv ari
ationonpr ogram 1Awe ’
veuse dt hepr i
nc i
plesfrom c ha ptersi
x
whe rewer ea dthek ey boa rdunt ilweg otavalueg re at
ertha n$80, me aningak e
y
hasbe enpr e s
s e
d.⇢i sv aluei she l
dt emporaril
yi nthev ar
iableCHARs ot hatit
canber etr
ievede a cht imea 4e ri
nc rementi
ngt hePTRi ntheNXTs ecti
on.
Se
ewha tv ariationsy ouc anma keont his,ort r
yt hehi -r
ess creen( $2000
throug h$3FFF) .
8
8
. S
oundGe
ner
ati
on
Ma
y19
81

Soundg enerat
ioni nasse mbl yla
ng ua geissuc hal ar getopici ni ts
elfthata n
entir
ebookc ouldbedoneont hatsubjecta l
one .Howe ver ,si
mpl erout inesares o
easytha tthey ’
rewor tha tleastabr iefexa minationhe re .⇢e serouti neswi l
lnot
onlya ll
owy out oputt hec omma ndsy ou’vele a
r nedt of urtherus e,buta r
ea lso
justplai
nf un.
⇢eFr ste l
eme ntofas ound- generatingr outinei st hes peak e
ri ts
e l
f.Re cal
l
thatthes peak eri
spa rtoftheme moryr ang efr
om $C000t o$C0FFt ha tisde voted
entir
elyt oha rdwa r
ei temsoft heAppl eI I.Ine a
r li
erpr og rams ,wel ook edatt he
keyboa rdbye xa
mi ningme mor ylocation$C000.⇢es pe akerc anbes i
mi l
arly
accessedbyl ook i
nga tlocation$C030.⇢ee x ceptionhe rei sthatt hev aluea t
$C000( thek eyboard)v ari
eda ccordingt owha tk eywa spr essed,whe reaswi th
$C030( thes peaker)thereisnol ogica
l valuereturne d.
Ev erytimel ocati
on$C030i sa ccessed,thes peakerwi l
lc l
ickonc e.⇢i sis
easytode mons tr
a t
e.Simpl ye ntertheMoni torwi thaCALL-151. Ent erC030a nd
press<RETURN>. You’llha v
et ol i
stenc arefull
y,andy ouma yha vetot ryitsev e
ral
times.Ea cht ime,thes peake rwi l
lcli
c konc e.Youc anima ginet hat
,i fwec oul d
repeatedlya ccessthes pe a
k era taf aste noug hr a t
e,thes eriesofc lickswoul d
becomeas t
e adytone .InBAS ICt hisca nbedone ,alt
houg hpoor l
y,byas impl e
loops ucha sthis:
10 X = PEEK(-16336): GOTO 10

⇢epi t
chofthet onegener
ate
dde pendsontherat
eatwhi
chthespea
keris
a
cces
sed.Becaus
eI ntege
rBAS ICisfaste
rinitsexe
cuti
onthanAppl
eso4,the
t
onegenera
tedwil
l benoti
ceabl
yhigherinpi
tchint
heInte
gerv
ers
ion.
I
na ss
emblylanguage,t
heprogr
a m woul
dlookli
kethi
s:
0300- AD 30 C0 LDA $C030
0303- 4C 00 03 JMP $0300

Inthi
sc a
seI’
ms howingita stheApplewoulddirec
tl
ydisas
semblei
t,a
s
oppos
edtotheusualasse
mbly -
languagesourc
elis
ti
ng.⇢epr og
rami ss
oshort
tha
ttheeas
ies
twayt oenteriti
sbyt y
pinginthehexcodedi
rect
ly.Todothi
s,
ent
ertheMonit
or(CALL-151) a
ndt ype:
300: AD 30 C0 4C 00 03

]
5
4 As
sembl
yLi
nes

⇢e nrunthepr og r
am bytypi
ng300G.
Disappointed?⇢epr ogrami swor k
ing.⇢epr obl
emisthatther outi
neis
act
uall
ytoofa s
tf orthespeakertorespond.What’
slacki
nghereissomewa yof
cont
roll
ingther a t
eofe xec
utionoft heloop.⇢i sisus
ual
lyaccompl is
hedby
putt
ingade l
ayofs omek i
ndi ntheloop.Wes houldal
sobeabletos peci
fythe
l
engthofthede lay,eit
herbeforetheprogramisrunor,eve
nbe t
ter,duringthe
exec
uti
onoft hepr ogram.

De
lay
s
Wec a
ndot hisi na nyoft hr eewa ys:( 1)phy s
ica l
lya l
tert hel engthoft he
prog ramt oincrea set hee xecut iont i
meofe a chpa ssthr oug ht heloop; (
2 )stor ea
va l
ues ome whe rei nme mor ybe forer unni ngt hepr og r ama ndt he nus et hat
va l
uei nade layloop; or( 3)g ett hede la
yv alueonac ont inua lba si
sf rom t heout -
sidewor ld,sucha sf rom t hek ey boa rdorpa ddl e
s.
Fort heFr stme thod, youc anus eane wa nda dmi tte dlyc ompl exc omma nd.
⇢emne moni cfort hisi nstruc tioni sNOPa nds t
andsf orNoOPe ration.Whe n-
evert he6 5 02mi cropr ocessore ncount er
st his,itj us tc ont inuest ot hene xt
instruc ti
onwi t
houtdoi nga ny thi ng .⇢i scodei sus e
df orj ustwha twene edhe re
–at imede lay.
Itismor eo4e nus e d,thoug h, ase i
therat empor ar yFl lerwhe na ssembl i
nga
blockofc ode( suc ha sf orlaterda tat ables
)ort oc ancel oute xisti
ngope rationsi n
apr ev iouslywr it
te ns ec ti
onofc ode .Qui teo4e n,thisc omma nd( $EA, or2 34i n
de c
ima l)isus edint hisma nne rt opa t
chpa rtsoft heAppl eDOSt oc anc e lout
va r
iousf ea
turest ha ty ounol ong erwa nttoha veactive( sucha stheNOTDIRECT
comma nde rrort hatpr eventsy ouf rom doi ngaGOTOdi re ct
lyt oal inet ha tha sa
DOSc omma ndoni t).
Inours oundr out ine, anNOPwi llt
akeac e r
taina mountoft imee vent opa s
s
ov e
ra ndwi llthusr educ ethenumbe rofc yclespe rsec ondoft hetonef reque nc y
.
⇢ema inpr oblemi nwr iti
ngt hene wv e r
sionwi ll
bede termi ningt henumbe rof
NOPst ha twillha vetobei nserte d.
⇢ee as
iestwa yt og e
tal a rg ebl ockofme mor yc lea redt oas pe ciFcv aluei s
tous et hemov er out inea l
readypr esentint heMoni tor .Toc learthebl oc k ,loa d
theFr stme mor yl oca ti
oni nt her ang etobec learedwi tht hede s
iredv alue .⇢e n
typei nt hemov ec omma nd, mov i
nge ver
ythi ngf r
om t hebe ginningoft her ang e
tothee nduponeby te .Fori ns tanc e,toc l
eart her angef r om $300t o$3A0a ndFl l
itwith$EAs ,youwoul d, from t heMoni torofc ourse,ty pei n:
300: EA
301<300.3A0M

Not
etha
twea
rec
lea
ringe
ver
ythi
ngf
rom $300t
o$3A0t
ocont
aint
hev
alue
$EA.
8
.SoundGe
ner
ati
on 5
5

Nowt
ypei
n:
300: AD 30 C0
3A0: 4C 00 03

⇢ent
ypein300L,f
oll
owe
dwi
thLf
ore
acha
ddi
ti
ona
lli
sts
ect
ion,t
ovi
ewy
our
ne
wpr
ogra
m.
*300L
0300- AD 30 C0 LDA $C030
0303- EA NOP
0304- EA NOP
0305- EA NOP
0306- EA NOP
0307- EA NOP
0308- EA NOP
0309- EA NOP
* * *
* * *
* * *
0395- EA NOP
0396- EA NOP
0397- EA NOP
0398- EA NOP
0399- EA NOP
039A- EA NOP
039B- EA NOP
039C- EA NOP
039D- EA NOP
039E- EA NOP
039F- EA NOP
03A0- 4C 00 03 JMP $0300

Nowr unthi
swiththeusual300G.
⇢et oneproduc edshoul
dbeav e
rynice,puret one.⇢epi t
chofthetone
canbecontr
oll
edbymov i
ngtheJMP$300t opointsofv ar
yingdista
ncefrom t
he
LDA$C030.Granted,thi
sisav erycl
ums ywa yofc ontrol
lingthepitchandis
rat
herper
ma ne
ntonc ecreat
ed,buti
tdoesil
lust
rat
et heba s
icprinci
ple
.
ForadiLer
e nttone,
hitRESETtost
opthepr og
r a
m, t
hent y
pein:
350: 4C 00 03

Whe nthisisr un(300G),thet onewi l


lbenot ic
eablyhigher.⇢ede la
yt i
mei s
abouthalfofwha titwas,andt hust hefrequencyistwi
cet heorigi
nalvalue.Try
typi
ngint hethre
eby tesi
ns e
pa rater unsat$320a nd$310. At$310youma ynot
bea bl
etohe arthet one,becauset hepi t
chi snow ess
entiall
yint heultra
sonic
range.
It
hinky ou’l
lalsonoti
cetha tallthesetonesareofaverypur enatureand,i
n
genera
l,muc hnicerthanthoseg enera t
edbyaBAS ICprogram.

]
5
6 As
sembl
yLi
nes

De
layVa
luei
nMe
mor
y
Usual
lyt
hewayt
onepr
ogr
amswor ki
stopas
sapi
tc
hvaluef
rom BASICby
putti
ngtheva
luei
namemoryl
oca
tion.
⇢i spr
ogr
amisane
xampleoftha
ttec
h-
nique.
1 ********************************
2* AL08-SOUND ROUTINE 2 *
3 ********************************
4*
5*
6* OBJ $300
7 ORG $300
8*
9 PITCH EQU $06
10 SPKR EQU $C030
11 *
12 ENTRY LDY PITCH
13 LDA SPKR
14 LOOP DEY
15 BNE LOOP
16 JMP ENTRY
17 * INFINITE LOOP

Whe
nas
sembl
ed,
its
houl
dlookl
iket
his
:
*300L
0300- A4 06 LDY $06
0302- AD 30 C0 LDA $C030
0305- 88 DEY
0306- D0 FD BNE $0305
0308- 4C 00 03 JMP $0300

Int hispr og r
am,weg etav alueof$00 t o$FF from locati
on$06( labe l
ed
pit
ch)a ndputi tint heY- Re gi
ster.⇢es pe ake
risthenc l
ic
ke d.Attha tpoint, we
enterade l
ayl oopt hatcy cl
esnt i
me swher enisthenumbe rv al
ueforpi tchhe l
d
inloc at
ion$06.
Tor unthi sprog ra
m, Frstloadlocati
on$06wi t
hv al
uesofy ourc hoic
e( 0to
255de c
ima l
, $00t o$FFhe x)a ndthe nrunwi th300G.⇢i sismor ecompa cta nd
controllabl
et ha ntheFr stexampl e,butitstil
lsuLersf
rom beinga ninFni t
el oop.
Wha twene edt odoi sspec i
fyadur ati
onf orthetoneaswe l
l.Ag a
iny ouha vet he
optionofe ithe rma kingt hev aluepa r
toft heprogram orpa ss
ingitint hes ame
waya swe ’
rec urrentlydoingt hev al
ueforpi t
ch.He re
’sali
stingforthene wpr o-
gram:
1 ********************************
2* AL08-SOUND ROUTINE 3 *
3 ********************************
4*
5*
6* OBJ $300
8
.SoundGe
ner
ati
on 5
7

7 ORG $300
8*
9 PITCH EQU $06
10 DURATION EQU $07
11 SPKR EQU $C030
12 *
13 ENTRY LDX DURATION
14 LOOP LDY PITCH
15 LDA SPKR
16 DELAY DEY
17 BNE DELAY
18 DRTN DEX
19 BNE LOOP
20 EXIT RTS

Di
sas
sembl
ed,
itwi
ll
appe
arl
iket
his
:
*300L
0300- A6 07 LDX $07
0302- A4 06 LDY $06
0304- AD 30 C0 LDA $C030
0307- 88 DEY
0308- D0 FD BNE $0307
030A- CA DEX
030B- D0 F5 BNE $0302
030D- 60 RTS

⇢isrout i
neisusedbyl oa di
ng$06wi thav a
lueforthepitc
hy oudesir
e,$07
wit
hav a l
uef orhowl ongy ouwa ntthetonet olas
t,andthenr unningtherou-
t
inewiththe300G.
Exa
mi ningthisli
sti
ng ,
y ou’l
lseethatitisbasi
cal
lyane xt
ensionofrouti
ne2 .
Inthisrevis
edv ersi
on,i ns
te a
dofa l
wa ysjumpingba cktot heLDYoft hepl a
y
cyc
le,wede crementac ounter( t
heX- Regist
er).⇢iscountsthenumbe roftimes
we’
rea l
lowedt opassthrought heloop,andt hustheFnallengt
hoft hepla
y.
⇢isc a
nbeus e dbyBAS ICpr ogr
a ms,asill
ustr
atedbythissampleAppleso4
l
ist
ing:
10 PRINT CHR$ (4);"BLOAD AL08.SOUND3,A$300"
20 INPUT "PITCH, DURATION?";P,D
30 POKE 6,P: POKE 7,D
40 CALL 768
50 PRINT
60 GOTO 20

⇢i sma kesi
tveryeas
ytoe x
perimentwithdiLere
ntv al
uesforbothpitch
anddur at
ion.⇢ema inbugint
hisrouti
neist
hatevenforaFx e
dv al
uefordura-
tion,t
hele ngt
hofthenotewillvar
yde pe
ndingont hepitchspeci
Fed.⇢i sis
be c
auselessti
mespentinthedel
ayloopme ansle
s sover
allexe
cuti
ont i
mef or
therouti
nea sawhol
e.

]
5
8 As
sembl
yLi
nes

De
layf
rom t
heKe
yboa
rdorPa
ddl
es
⇢ene xtva
ria
tionistogett
hepit
chonacontinua
lba
sisfr
om anout
side
s
our
ce.I
nt hisc
ase,thesour
cewil
lbet
hekey
board.Typei
na ndas
sembl
ethis
s
our
celi
st
ing:
1 ********************************
2* AL08-SOUND ROUTINE 4 *
3 ********************************
4*
5*
6* OBJ $300
7 ORG $300
8*
9 KYBD EQU $C000
10 STROBE EQU $C010
11 SPKR EQU $C030
12 *
13 ENTRY LDA KYBD
14 STA STROBE
15 CMP #$80
16 BEQ EXIT
17 TAY
18 LOOP LDA SPKR
19 DELAY DEY
20 BNE DELAY
21 JMP ENTRY
22 EXIT RTS

I
nme
mor
y,i
tshoul
dlookl
iket
his
:
*300L
0300- AD 00 C0 LDA $C000
0303- 8D 10 C0 STA $C010
0306- C9 80 CMP #$80
0308- F0 0A BEQ $0314
030A- A8 TAY
030B- AD 30 C0 LDA $C030
030E- 88 DEY
030F- D0 FD BNE $030E
0311- 4C 00 03 JMP $0300
0314- 60 RTS

Runningthiswillg i
veyouar e
all
ye asywa yofpassingtonev al
uest othe
routi
ne.Characte
rswi thlowAS CIIv a
lueswi llproducehighertonesthanone s
withhigherval
ue s
.⇢i smeansthatthec ontrolc har
act
e r
swi l
lproduceunus u-
all
yhightones.Toe xi
tpress<CTRL><SHIFT>P( <CTRL>@).
Let
’srev
iewhowt herouti
nefunctions.
Attheentrypoint($300),t
hev er
yFr stthingdonei stog e
tav a
luefrom the
keyboard.⇢es t
robei sthenclear
ed,a nda ni mme dia
tec heckdonef or#$80.
Reme mbe rt
hat$80i saddedtotheAS CI Ivalueforeachk eypr e
sse
dwhe nread
at$C000.Av al
uelessthan$80me ansnok eyha sbeenpressed.Checki
ngs pec
iF-
8
.SoundGe
ner
ati
on 5
9

cal
lyfor$80, thecomput erl
ook stoseeifa<CTRL>@ha sbe e
npr e
ssed.⇢isisj
ust
anicetouc ht ogiveusawa yofe xi
ti
ngt hepr ogram.A4e rthecheck,wetrans
fer
theAc cumul atorv al
ue( equival
entt opitc hinthee arli
erprograms)totheY-
Regis
tera ndFni shwi t
ht hesamer outineus edinSoundRout i
ne2.
Ofc ourse,Iha v
etog i
vey oua tleastonepr ogra
m us ingthepaddles
.⇢i s
oneg i
ve susa noppor tunit
yt ous ethee xte
rnalroutinesintheMoni tor
,too.
Don’tforg etthatus i
ngt her outi
nesa lreadypresentint heMoni t
oristhereal
secr
ettoe a s
ya sse
mbl y-languageprog rammi ng.Itsav
e syouthet r
oubleofhav-
ingtowr iteyourownI /Oa ndot hersophi st
icat
edroutinesandletsyouconcen-
tra
teont hos easpectsuniquet oyourpr ogram.
Nowf orthepr ogra
m:
1 ********************************
2* AL08-SOUND ROUTINE 5 *
3 ********************************
4*
5*
6* OBJ $300
7 ORG $300
8*
9 PDL EQU $FB1E
10 SPKR EQU $C030
11 *
12 ENTRY LDX #$00
13 JSR PDL
14 LDA SPKR
15 LDX #$01
16 JSR PDL
17 LDA SPKR
18 JMP ENTRY
19 * INFINITE LOOP

⇢eMoni
torwi
ll
li
stt
hisa
s:
*300L
0300- A2 00 LDX #$00
0302- 20 1E FB JSR $FB1E
0305- AD 30 C0 LDA $C030
0308- A2 01 LDX #$01
030A- 20 1E FB JSR $FB1E
030D- AD 30 C0 LDA $C030
0310- 4C 00 03 JMP $0300

Runningthi
ss houldpr oduces omer ea
llyinteres
ti
ngr es
ult
s.⇢et he or
yof
t
hisr
outi
neiselegantl
ys i
mpl e.
Ittur nsoutt
ha tjustre
adingapaddletakesacer-
t
ainamountoftime, s
uWc ienttocrea t
eourne ededdelay
. ⇢eg r
eate
rthepa ddl
e
r
eadi
ng,thel
onge rthedelaytore a
di t.
Wha thappensi nthisroutinei sthatwea c
tuall
yha vet
wodi st
inctdelay
s
c
reat
ed,onebye achpa ddle.Reme mbe rthattor eadapa ddl
e,youFrstha v
eto
l
oadtheX-Regis
terwi t
hthenumbe roft hepaddl eyouwishtoreadandthe ndo

]
6
0 As
sembl
yLi
nes

theJSRt othepa ddlere


adr outi
ne .
⇢ev alueisr e
turnedi ntheY-Regis
ter
,butin
thi
scasewedon’ tneedtok nowwha tthev aluewa s.
⇢ec ombi nat
ionoft hetwodi Lerentpe riodsofde laycrea
testheeLectof
twotone satonc eandanumbe rofotherv eryuniques ounds.
⇢i shasbe enonlythemos tbasi
cdisc uss
ionofs oundg e
nerat
ioninass
em-
blyla
ng uage,butIt hi
nky ou’l
lFndt hatitill
ustrat
eswha tcanbedonewi thonly
afewc omma nds,andthatma chinelanguag eoLe r
sma nyadvant
agesinter
msof
me moryus ea ndexecut
ions peed.
9
9
. ⇠eS
tac
k
J
une1
981

Oneoft hemor eobs curepa r


tsoft heope rationoft heAppl ei sr elatedt o
some t
hingc all
edthes tack. ⇢i sisapa rtofme mor yr eser
v edforhol dingr et
ur n
addr essesf orGOSUBsa ndFOR- NEXTl oops ,a ndaf e wot herope rati
onsi ndi re c
t
ma chi necode .
Ifyouwa nttoi mpre ssy ourfriendswi thy ourk nowl edg eofa s
s embl yl an-
gua ge, j
ustt hrowt histerma roundi nac onFde ntma nne randt he y’
llFg ur ey ou
mus tbea ne xpert!
⇢es t
a ckc anbet houg htofl ikethos es pring -l
oa dedpl ateholderst he yha ve
inr estaurant s
.Platesarel oade dont ot het opofac yl
inderwi thas pr ing-loade d
platformi ni t
.Asmor epl atesarea dde d,t her estg etpus heddown.⇢epl a te
s
mus ta l
way sber emov edi nt heoppos iteor de rfrom t hati nwhi cht he ya reput
in.⇢ec atc hphr asefort hi sisLI FO,f orLa st-In,Fi rs
t-Out .⇢eFr stloc ation
loade dint he6 502s t
acki s$1FF.Ra t
he rt ha npus hinge ve r
ythingdownt owa rd
$100e acht imeane wv aluei sputont hes tac k,the6 5 02ha saS tackPoi nt ertha t
isa djusteda sne w datai sa dde d.S uc cess i
v ev alue sarea dde di nde scendi ng
orde r,witht heS t
ac kPoint erbe i
ngr esete a cht imet oindicatethepos itionoft he
nex tav ai
lablelocation.⇢ust hetableisc rea t
edi nr ev e
rseor der,buildingdown-
wa rd.
⇢et echni c
alde ta
ilsofi tsope rationa r enotr equiredt oma keg oodus eof
thes tack.Oneoft hemos tc onv enientt hing st hes tackc anbeus edfori stohol d
value stempor ari
lywhi l
ey ou’redoi ngs ome thinge l
se.Nor ma l
lyinapr og r
a m,
we’dha vet oa ssi
gnaz ero-pa geloc at
iont ohol dav alue.Fori nstanc e,cons ider
thi
spr ogram:
1 ********************************
2 * AL09-BYTE DISPLAY PROGRAM 1 *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 CHR EQU $06
9 PRBYTE EQU $FDDA
10 COUT EQU $FDED
11 PREAD EQU $FB1E
12 HOME EQU $FC58
13 *

]
6
2 As
sembl
yLi
nes

14 START JSR HOME


15 GETCHR LDX #$00
16 JSR PREAD
17 STY CHR
18 TYA
19 JSR PRBYTE
20 LDA #$A0 ; SPACE
21 JSR COUT
22 LDA CHR
23 JSR COUT
24 LDA #$8D ; RETURN
25 JSR COUT
26 JMP GETCHR

⇢i
swi
ll
bel
is
tedbyt
heMoni
tora
s:
*300L
0300- 20 58 FC JSR $FC58
0303- A2 00 LDX #$00
0305- 20 1E FB JSR $FB1E
0308- 84 06 STY $06
030A- 98 TYA
030B- 20 DA FD JSR $FDDA
030E- A9 A0 LDA #$A0
0310- 20 ED FD JSR $FDED
0313- A5 06 LDA $06
0315- 20 ED FD JSR $FDED
0318- A9 8D LDA #$8D
031A- 20 ED FD JSR $FDED
031D- 4C 03 03 JMP $0303

⇢i sprog r
amg etsav aluef rom $00 t o$FF f rompa ddle0 ,a nds toresi tin
location$06.⇢i si sne ede dbe causet heJSR t o$FDDA ( aha ndyr outinet hat
pr i
ntsthehe xnumbe rint heAc cumul ator)scra mbl e
st heAc cumul at
ora ndY-
Re gi
ster.Wewa ntt ok e
e pt hev a
luea tha ndbe caus etheAS CIIc ha ract
e rcorre-
spondi ngtoi tisthe npr i
nt edoutr i
ghta 4erthenumbe rusingCOUT.⇢ec y cl
e
thenr epeatsuntilyoupr essRESET.
Loc ati
on$06i sus edf oronl yamome nte achpa sstos toret hev a
luet empo-
raril
y. I
na dditi
on, itcommi tst hatz e
ro-pa gelocationt ous ea ndt husl i
mi tsour
choiceswhe nwene edot he rpl acestos tores ome t
hing .A be tters yst
e mi st o
ma keus eoft hestack. ⇢ec omma ndstodot hisa rePHAa ndPLA. PHAs tandsf or
“PusH Ac cumulatoront os ta ck.”Whe nt hisisus edinl ine1 7be low,t hev alue
currentlyint heAc cumul atori sputont ot hestack.⇢eAc cumul atoritsel
fg oes
una l
tered,andnoneoft hes tatusSa gs,suc hasthec ar
ryorz eroSa gs,arec ondi-
tioned. ⇢ev al
ueiss i
mpl yc opie da ndstoredforus .
La teron,whe nwewa ntt or etri
evet hev alue,thePLA( “Pul LAc cumul ator
from stack”)onl i
ne2 1pul lst hev aluebackoLt hes tackintot heAc cumul a
tor. A
PLAc omma nddoesc onditiont hez eroSa g,anda l
sot hes ignbi t,whi chha snot
be e
nc ov er
edy et.
9
.⇢eS
tac
k 6
3

I
mpor t
ant:ForeachPHAthe
remus
tbeaPLAe
xec
ute
dbe
for
eenc
ount
eri
ng
t
henextRTSinaprogram.
Here’
stherevi
sedprogr
am:
1 ********************************
2 * AL09-BYTE DISPLAY PROGRAM 2 *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 PRBYTE EQU $FDDA
9 COUT EQU $FDED
10 PREAD EQU $FB1E
11 HOME EQU $FC58
12 *
13 START JSR HOME
14 GETCHR LDX #$00
15 JSR PREAD
16 TYA
17 PHA
18 JSR PRBYTE
19 LDA #$A0 ; SPACE
20 JSR COUT
21 PLA
22 JSR COUT
23 LDA #$8D ; RETURN
24 JSR COUT
25 JMP GETCHR

⇢i
swi
ll
lis
tli
kes
o:
0300- 20 58 FC JSR $FC58
0303- A2 00 LDX #$00
0305- 20 1E FB JSR $FB1E
0308- 98 TYA
0309- 48 PHA
030A- 20 DA FD JSR $FDDA
030D- A9 A0 LDA #$A0
030F- 20 ED FD JSR $FDED
0312- 68 PLA
0313- 20 ED FD JSR $FDED
0316- A9 8D LDA #$8D
0318- 20 ED FD JSR $FDED
031B- 4C 03 03 JMP $0303

⇢es t
ackisa l
sous eda utomatic
all
ybyt he6 502f
ors tori
ngt here t
urn
addres
sf orea
chJSRa sit’se
nc ounter
ed.Eac
ht i
mey oudoaPHA, t
hisaddressis
burie
donel eveldeeper
.Youmus thavedonea nequi
val
entnumbe rofPLAsa t
somepoi ntint heroutinebe f
orer e
achi
ngt henextRTS t
oha v
et hi
ngswor k
properl
y.
Alsoremember,ifyouwa nttostoremorethanoneval
ue,y oumus tr
etri
e v
e
thev a
luesintheoppos iteorderi nwhichtheywe rest
ored.Onc eav aluei s

]
6
4 As
sembl
yLi
nes

re
mov e
df r
om t
hest
ackwit
hthePLA,
iti
ses
sent
ial
lyg
onef
ore
verf
rom t
hes
tac
k
unl
essyouputi
tba
ckdir
ect
ly.

St
ackLi
mit
⇢e r
ei salimittohowmuc hyouca nputi nthes tack
.⇢el i
mi tofs i
xteen
nestedGOSUBsa ndFOR-NEXTloopsi
nBAS ICisr el
at
e dt othi
s.Te chni c
all
yy ou
canput2 56one-byteval
uesor128RTSaddr e
ssesont hes t
ack,buttheAppl ea l
so
usesitforitsownope rat
ions,
andma nytimesy ouhav eBAS ICgoing ,too.
Ing eneral
,though,thestac
krarel
yFllsupunl essy ou’
reg e
tti
nge xtr
emei n
it
sus e,anda tthatpointthecodeprobabl
ywi llbesot angle
di nne ste
ds ubrou-
ti
nestha tyouma ywanttoconsi
derarewrit
ea nyway!
Tryus ingt hesta
cki nsomeofy ourownpr ogra ms;Ithinky ou’lFndi
l t
quit
eus eful.
1
0
1
0. Addi
ti
onandS
ubt
rac
ti
on
J
uly1
981

Nowl et’slooka tthes i


mpl ema t
hope rationsofa dditi
ona nds ubtra
cti
oni n
assemblylang uage.Toa ne xte
nt,we ’vea l
rea dydones omeofthis .⇢ei ncre
me nt
andde c
reme ntcomma nds( INC/DEC, ands oon)a dda ndsubt
ra ctforus.Unfor-
tunately,
theyonl ydos obyonee a chtime( VALUE+1o rVALUE-1) .
Ifyou’rer ea
llyambi ti
ousy ouc oul d,wi ththec omma ndsy ouha vealr
eady,
addors ubtractanynumbe rbyus ingal oopofr epe ti
ti
veope rat
ions,butthis
woul dbeabi tte
dious, nottome nti
ons l
ow. Fortunate l
yabetterme thode x
ist
s.
ButFr st
,aqui ckreviewofs omebi naryma thfa ct
s.
Inc hapte rfourwedi scussedt hei de abe hindbi narynumbe rsandwhy
they’r
es oimpor tantinc omput ers.Iwoul dl iketof urtherel
abor at
eont hetopic
nowa ndshowhowt heideaofbi na r
ynumbe r
sa pplie
st obasi
ca ri
thme ti
copera-
tionsinassembl y-l
angua geprogrammi ng .

Bi
nar
yNumbe
rs
Bynowy ou’rec ert
ainlycomf ortablewi t
ht hei deaofaby t
ebe i
nga ni ndi-
vidualme mor yloc ati
onwhi c
hc anhol dav al
uef rom $00t o$FF( 0to2 55).⇢i s
numbe rc ome sa bouta sadi rectresultoft hewa ythec omput erisconstructed
andt hewa yinwhi chyouc ounti nba setwo.
Ea chby t
ec anbet houg htofa sbe i
ngphy s
icallyma deupofe i
ghtindividual
switche sthatc anbei neithera nonoroLpos ition.Wec a ncountbya ssigni
ng
eachpos s
iblecombi nati
onofonsa ndoLsauni quenumb erv al
ue.
Int hef ol
lowi ngdiagra ms,ifaswi t
chi soL, i
twi l
lber epres
entedbya0i nits
parti
c ularpos i
tion.I fit’
son,a1wi llbes hown.Whe na lltheswitche
sa reoL,
we ’
llcallthat0 .
Inba setwo,e achoft hee ightpos i
t i
onsint heby teisc all
edabi t
,a ndt he
posit
ionsa renumbe redfrom righttol e4:[ 7 6 5 4 3 2 1 0 ].
⇢epa tternf orcount i
ngi ssimi l
art onorma ldeci
ma lorhe xnot a
tion.⇢e
valuei sincreasedbya ddingonee acht i
met othedi gi
tont hefarrig
ht,ca r
rying
asne cessary.Inba setenyou’dha v
et oc arr
ye veryt e
nthc ount,andinhe xe very
si
x t
eent h.Inba set wo,thec ar
rywi llbedonee v
e r
yo thertime !
So. .
.t
heFr stfe wnumbe rslookliket his
:

]
6
6 As
sembl
yLi
nes

He
x De
cima
l Bi
nar
y
$00 0 0000 0000
$01 1 0000 0001
$02 2 0000 0010
$03 3 0000 0011
$04 4 0000 0100

Noti
cethating oi
ngfrom t
hev al
ue1t otheval
ue2,weadda1tothe1
al
rea
dyattheFrstposi
ti
on(bit0
) .
⇢i sgene
rat
esthec
arr
ytoi
ncre
mentt
hesec
-
ondpos
it
ion(bit1).
He r
eistheendofthese
rie
s:

$FD 253 1111 1101


$FE 254 1111 1110
$FF 255 1111 1111

Nowt hemos timportantpart


.Observ
ewha thappe
nswhe ntheuppe
rli
mit
oft
hecounterisFna l
lyr e
ached.At$FF(255),al
lposi
tionsar
eful.Whe
l nthe
nex
tincr
eme ntisdone ,wes houl
dcarryaonet othenextposi
ti
ontothel
e4;
unf
ort
unatel
y,thatnextposit
iondoes
n’texi
st
!

Addi
ti
onwi
thADC
⇢i siswherethecar
rybitoftheStat
usRegist
erisusedag
ain.Bef
ore,
itwas
usedint hecompareoperat
ions(CMP,fori
nsta
nc e
),but,asi
thappens
,itisal
so
condit
ionedbyt henextcomma nd,ADC.⇢isstandsforADdwi thCarr
y.Whe n
the$FFisincre
me ntedus
inganADCc omma nd,t
hingswi l
ll
ookli
kethi
s:

Ca
rry
$100 256 0000 0000 1

⇢eby teha sr e
turne dt oav alueof0a ndt hecarrybitissettoaone .
Wedi scussedt hewr ap-aroundt o0e arl
ier
,witht heinc re
me nt/
decrement
comma nds,butwedi dn’ tme ntiont hec arr
y.⇢a t
’sbe ca
us etheINC/DECc om-
ma ndsdon’ taLe ctthec arrySag .
Howe ver,theADCc omma nddoe sconditi
ont hec ar
rySa g.⇢ec a
rrywillbe
setwhe nevert her esultoft hea dditi
oni sg re
a t
erthan$FF. 1 WithADC,youc an
ma key ourcount ersinc reme ntbyv aluesotherthanone –r a
therliket
heFORI=
1TO10STEP5s tateme ntinBAS IC. ButADCi susedmor eo4e nforgeneralmath
ope rat
ions,suc hasc al
c ulati
ngne wa ddres
s e
sors cr
ee npos i
ti
ons ,
amongawi de
varietyofothe rapplications.
Whe neverADCi sus ed,thev al
uei ndi
c at
edisa ddedt ot hecontent
soft he
Ac cumul at
or.⇢ev al
uec anbes tat
e de i
therdirect
lybyus eofa ni mme diat
e
valueorwi tha nindi re
c tvalue.

1[
CT]S
imi
la
rly
,thec
arr
ywi
ll
bec
lea
redwhe
nther
esul
tis$FForl
ess
.
1
0.Addi
ti
ona
ndS
ubt
rac
ti
on 6
7

Impor
tantNot
e:Al
thoughtheADCcondit
ionsthecar
rya
4eri
tisex
e c
ute
d,it
ca
nnotbeassumedtha
tthecarryi
sc onve
nientl
ys t
andi
ngi
naclearcondi
ti
on
whenthea
dditi
onrout
inei
sbegun.
Forex
ample,c
onsi
derthi
ssimpleprog
ram:
LDA #$05
ADC #$00
STA RESULT

Asitsta
nds,therearetwopos si
bleresul
ts
. I
fthecarr
yha ppenedtobec l
ear
whenthiswasexecuted,theval
uei nresul
twoul dbe$05.I f
,however,t
hecarry
hadbeenset(pe
rha psastheresultofsomeot heropera
tion),t
henthecarrybit
wouldbeincl
udeda ndtheresul
twoul dbe$06.
⇢epoi ntofallthi
sisthatthecarrySagmus tbecle
ar e
dbe f
oretheFrs
tADC
oper
ati
on.⇢ee xampl eaboveshouldha v
ebeenwr it
tenas:
CLC (CLear Carry)
LDA #$05
ADC #$00
STA RESULT

Inthi
scase
,res
ultwi
llal
waysendupholdingt
heval
ue$05.Herea
resome
s
ampl
eprogr
amsforusi
ngtheADC.
Notetheuseoft
heCLCbe
foreea
chADC.
1 ********************************
2 * AL10-ADC SAMPLE PROGRAM 1 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 N1 EQU $06
9 N2 EQU $08
10 RSLT EQU $0A
11 *
0300: A5 06 12 START LDA N1
0302: 18 13 CLC
0303: 65 08 14 ADC N2
0305: 85 0A 15 STA RSLT
0307: 60 16 END RTS

1 ********************************
2 * AL10-ADC SAMPLE PROGRAM 2 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 N1 EQU $06
9 RSLT EQU $0A
10 *
11 *

]
6
8 As
sembl
yLi
nes

0300: A5 06 12 START LDA N1


0302: 18 13 CLC
0303: 69 80 14 ADC #$80
0305: 85 0A 15 STA RSLT
0307: 60 16 END RTS

1 ********************************
2 * AL10-ADC SAMPLE PROGRAM 3 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 N1 EQU $06
9 INDX EQU $08
10 RSLT EQU $0A
11 TBL EQU $300
12 *
0300: A5 06 13 START LDA N1
0302: A6 08 14 LDX INDX
0304: 18 15 CLC
0305: 7D 00 03 16 ADC TBL,X
0308: 85 0A 17 STA RSLT
030A: 60 18 END RTS

1 ********************************
2 * AL10-ADC SAMPLE PROGRAM 4 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 N1 EQU $06
9 INDX EQU $08
10 RSLT EQU $0A
11 PTR EQU $1E
12 *
0300: A9 00 13 START LDA #$00
0302: 85 1E 14 STA PTR
0304: A9 03 15 LDA #$03
0306: 85 1F 16 STA PTR+1
0308: A5 06 17 LDA N1
030A: A4 08 18 LDY INDX
030C: 18 19 CLC
030D: 71 1E 20 ADC (PTR),Y
030F: 85 0A 21 STA RSLT
0311: 60 22 END RTS

IntheFrstprogra
m,t heval
uei nN1i saddedtothec ontent
sofN2.Int he
s
econd,N1i
sa ddedtothei mmediat
ev alue$80.NotetheCLCbe f
oretheADCt o
e
nsureanaccurater
esult
.⇢i sres
ultisthenr e
turne
dinl oca
tion$0A.⇢i srou-
t
inecoul
dbeus edasasubrouti
nefora notherass
embly-l
anguageprogra
m, orit
c
ouldbecal
le
df rom BASICa 4e
rpassingthev a
luest
olocati
ons$06a nd$08.
1
0.Addi
ti
ona
ndS
ubt
rac
ti
on 6
9

⇢el at
tertwopr ogramsa remor ee l
aborat
ee xampleswhe ret heindi
r ec
t
modesareus edtoFndt hev a
luefrom atablest
ar t
inga t$300.Inpr ogram3 ,an
i
ndexvalueispa s
sedtolocati
on$08. ⇢a tisusedasa noLs etviatheX- Regi
ster.
Inprogr
am4 ,thelow-orderandhi g
h-orderbytesf ortheaddr es
s$300a reFr st
puti
napa irofpointerbyte
s($1E,$1F) a
ndt heoLsetisputint heY-Re gis
ter
.
Inalloftheseprograms,howe ver
,wea reli
mi tedt oaddingtwos i
ngle
-by t
e
val
uesandf urt
herr e
stri
cte
dt oaone -byt
er es
ult.Notv e
rypr act
icalinther ea
l
worl
d.
⇢es ol
utionistous ethec ar
rySa gtoc re
ateat wo-byteadditionroutine.
Here’
sane xample:
1 ********************************
2 * AL10-ADC SAMPLE PROGRAM 5A *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 N1 EQU $06
9 N2 EQU $08
10 RSLT EQU $0A
11 *
0300: 18 12 START CLC
0301: A5 06 13 LDA N1
0303: 65 08 14 ADC N2
0305: 85 0A 15 STA RSLT
0307: A5 07 16 LDA N1+1
0309: 65 09 17 ADC N2+1
030B: 85 0B 18 STA RSLT+1
030D: 60 19 END RTS

Not i
cethatN1, N2,andRSLTa r
ea l
ltwo- byt
enumbe rs,wit
ht hes econdby t
e
ofe a
chpa i
rbeingus edf orthehi gh-orderby t
e.(Ifyou’reunsur eofthei deaof
l
ow-a ndhi gh-orderby tes,ref
ertoc haptertwo, footnoteone).⇢i sa l
lowsust o
usev al
ue sandr e
sultsfrom $00t o$FFFF( 0t o65 535).⇢isiss uWc i
entf orany
addressintheAppl eI I,alt
houg hbyus ingthr e
eormor eby t
es,
wec oulda ccom-
moda tenumbe rsmuc hlargerthan$FFFF.
Af e
w wor dsofe xplanati
ona boutthispr ogram.Fi rs
t,theCLC ha sbeen
mov edt othebe gi
nni ngoft her outine.Althoughi tne edonlypr ecedet heADC
comma nd,ithasnoe Le ctont heLDA, soitisputa tthebe gi
nningoft her outi
ne
foraestheti
cpurpos es
. Onc ethetwol ow-orde rbytesofN1a ndN2a r
ea dde dand
thepartialres
ultstored,thehi gh-orderby t
esa r
ea dded.Ifthere s
ultoft hisFrs
t
additi
oni sgreate
rt han2 55,thec arrywillbes e
ta nda ne x
trauni tadde dinthe
seconda dditi
on.Not etha tthec arryrema insuna Lectedduringt heLDAN1+1
operati
on.

]
7
0 As
sembl
yLi
nes

⇢eMonit
orli
st
ingi
sgi
venf
ort
hisones
otha
tyouc
anc
alli
tfr
om t
his
BAS
ICpr
ogr
am:2

0 REM AL10-ADC 5A ADDITION ROUTINE


10 HOME
15 FOR I = 0 TO 13: READ OP: POKE 768 + I,OP: NEXT I
20 INPUT "N1,N2?";N1,N2
30 N1 = ABS(N1):N2 = ABS(N2)
40 POKE 6, N1 - INT (N1 / 256) * 256: POKE 7, INT (N1 / 256)
50 POKE 8, N2 - INT (N2 / 256) * 256: POKE 9, INT (N2 / 256)
60 CALL 768
70 PRINT: PRINT "RESULT IS "; PEEK (10) + 256 * PEEK (11)
80 PRINT: GOTO 20
90 DATA 24,165,6,101,8,133,10,165,7,101,9,133,11,96

⇢eABS()s tate
mentsonli
ne30e l
imina
teval
uesl
essthan0 .Al
thoughthere
ar
ec onve
nti
onsforhandl
ingnega
tiv
enumbe rs
,thi
srouti
nei snott
ha ts
ophisti
-
ca
ted.
Ma nyti
me sthenumberbei
nga ddedtoabaseaddressisknowna l
way sto
be$FForl e
ss,soonlyonebyteisneededforN2.A t
wo/ oneadditi
onroutine
l
ookslik
ethi
s:
1 ********************************
2 * AL10-ADC SAMPLE PROGRAM 5B *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 N1 EQU $06
9 N2 EQU $08
10 RSLT EQU $0A
11 *
0300: 18 12 START CLC
0301: A5 06 13 LDA N1
0303: 65 08 14 ADC N2
0305: 85 0A 15 STA RSLT
0307: 90 06 16 BCC END
0309: A5 07 17 LDA N1+1
030B: 69 00 18 ADC #$00
030D: 85 0B 19 STA RSLT+1
030F: 60 20 END RTS

Forspeed,i
facarryisn’
tgene
ratedonl i
ne14,thepr
ogramski
psdi
rec
tl
yto
theend.If,however
,thec ar
ryiss
et,thevalueinN1+1get
sincr
ement
edbyone
eventhoug htheADCsaysa nimmediate$00.⇢e$00a ct
sasadummyv al
ueto
all
owt hecarr
ytodoitsjob.Ifs
pee
di snotac onc
ern,t
heBCCcanbel
e4outwi
th
noilleLect
.

2[
CT]⇢i
swa
scha
nge
dtoi
ncl
udet
hema
chi
nec
odewi
thi
ntheDATAs
tat
eme
nt.
1
0.Addi
ti
ona
ndS
ubt
rac
ti
on 7
1

⇢efol
lowingpr
ogra
ms howsana l
ter
nat
eme thodus
ingt
heINCc
omma
nd.
I
nthi
sca
se,theBCCi
sre
quir
edforproperope
rat
ion.
1 ********************************
2 * AL10-ADC SAMPLE PROGRAM 5C *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 N1 EQU $06
9 N2 EQU $08
10 RSLT EQU $0A
11 *
0300: 18 12 START CLC
0301: A5 06 13 LDA N1
0303: 65 08 14 ADC N2
0305: 85 0A 15 STA RSLT
0307: 90 06 16 BCC END
0309: A5 07 17 LDA N1+1
030B: 85 0B 18 STA RSLT+1
030D: E6 0B 19 INC RSLT+1
030F: 60 20 END RTS

⇢er eas
onf orbri
nginguplist
ing5Cisthatthemostcommonr e
asonfor
a
ddingonetoat wo-byt
enumbe ristoi
ncrementanaddres
spoint
erbyone.In
t
hatcas
e,t
her es
ultisus
ua l
lyputri
ghtbac
ki ntheori
gi
nalloc
ati
onrat
herthan
i
nas epar
ateRESULT.A routi
neforthi
sismorec ompac
ta ndwouldl
ookl i
ke
t
his
:
1 ********************************
2 * AL10-ADC SAMPLE PROGRAM 5D *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 N1 EQU $06
9 *
0300: 18 10 START CLC
0301: E6 06 11 INC N1
0303: D0 02 12 BNE END
0305: E6 07 13 INC N1+1
0307: 60 14 END RTS

]
7
2 As
sembl
yLi
nes

Subt
rac
tion
Subtracti
onisdonel i
k eaddi t
ione xceptt hatab or
rowi srequired.Rather
thanus i
ngas e
parat
eSa gforthisope rat
ion,t hec omputerrecognizesthecarr
y
Sagass ortofarever
seborrow.
⇢a ti s,astc
e arrySagwi l
lbet r
eatedbyt hes ubt
ractcomma nda saclea
r
borrw(
o noborrowtaken);
ac l
ea rcarryasas etb or
row(borrowuni ttaken)3
.
⇢ec omma ndforsubtrac
tioni sSBC,forS uBtractwit
hCa rry
. ⇢ebor r
owi s
cle
aredwi t
hthecomma ndSEC, forSEtCa rry.(Reme mber,thing
sa r
eba c
kwa r
d
here)
. As ubtr
acti
onequiva
lentofpr ogram5 Al ooksli
kethis
:
1 ********************************
2 * AL10-SBC SAMPLE PROGRAM 6 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 N1 EQU $06
9 N2 EQU $08
10 RSLT EQU $0A
11 *
0300: 38 12 START SEC
0301: A5 06 13 LDA N1
0303: E5 08 14 SBC N2
0305: 85 0A 15 STA RSLT
0307: A5 07 16 LDA N1+1
0309: E5 09 17 SBC N2+1
030B: 85 0B 18 STA RSLT+1
030D: 60 19 END RTS

⇢eprog
ramcanbec
all
edwi
tht
hes
ameBAS
ICpr
ogr
amt
hatweus
edf
or
t
hea
ddi
ti
onr
out
ine
.

Pos
iti
vea
ndNe
gat
iveNumbe
rs
Sofarweha vedis
cuss
edonlyhowt orepres
entwholenumbersgreate
rthan
orequaltozerousingoneortwoby t
es.Ar e
asonabl
equest
iont
he nis:“
Howdo
wer e
presentnegat
ivenumbers?

Negati
venumbe rscanbethoughtofasawa yofhandli
ngcertai
nc ommon
ari
thmeticposs
ibil
it
ies
,suchaswhensubtract
ingala
rgernumberfrom asma l
le
r
one,forinst
ance,375=72 ,orwhe na ddi
ngapos i
ti
venumbe rtoane g
ati
ve
numbe r,
forinst
ance,5+78=73 .

3[
CT]J
ustl
ikeADC,
SBCa
lsoc
ondi
ti
onst
hec
arr
ySa
g.I
fther
esul
tre
qui
resabor
rowt
hen
thecar
ryiscl
ear
ed(borr
owse,f
t ore
xample$507$80).I
fthere
sul
tdoe
snotr
equi
rea
borr
owt he
nthecar
ryiss
et(
borr
owcle
ar,
forexample$507$30)
.
1
0.Addi
ti
ona
ndS
ubt
rac
ti
on 7
3

Tobes ucc
ess
ful
,the
n,wemus tc
omeupwithas
yste
m usi
ngthee
ightbi
ts
i
neachbyt
ethatwil
lbecons
is
tentwit
hthes
igne
dari
thmet
ictha
twearecur-
r
ent
lyfa
mil
iarwit
h.

⇢eSi
gnBi
t
⇢emos ti
mme diates olutiont otheque s
tionofs i
gne dnumbe rsistousebit
7toi ndica
tewhe theranumbe rispos it
iveorne ga
tive.Ifthebitisc l
ear,the
numbe risposi
tiv
e .I
fthebi tiss e
t,thenumbe rwi l
lberegardedasneg at
ive.
⇢us+5woul dber epre s
e nted:00000101
While75woul dbes howna s:10000101
Notethatbys acriFcingbi t7t os howthes ign,we’renowl imitedtov al
ues
fr
om 71 27to+1 2 7.Whe nus ingt woby t
estor e
presentanumbe rsuc hasa n
address
,thisme anst hatwe ’
llbel i
mi te
dt ot her a
ng eof73 2767t o+3 2
7 67
.
Soundf amil
iar
?I fy ou’veha da nye xperi
encewi thIntegerBAS IC,theny ou’l
l
rec
og ni
zethisasthema ximum r ang eofnumbe rv al
ueswi t
hinthatlanguage.4

Alt
houg hthisne ws c hemei sv erypleas
ingi nt er
msofs implic
ity,itdoes
haveonemi nordr awba ck–i tdoe sn’twork.Ifwea t
temptt oaddapos iti
vea nd
negati
venumbe rus i
ngt hiss che meweg etdis
tur bi
ngresults:
5 00000101
+ -8 10001000
-3 10001101 = -13!

Althoughwes houl
dg e
t73a sther esult
,usingoursignedbits
ystem weget
713.Ts k,tsk.⇢e remus tbeabe tt
erwa y.We ll
,withthehelpofwha ti
se s
sen-
ti
al
lyal i
ttl
enume r i
cma gicwec ange tsome thi
ngwhi chworks,al
thoughsome
ofthec onceptuals
implici
tyg e
tslos
tint hepr ocess
.
Wha twe’l
linvokeistheideaofnumbe rc omplement
s.⇢es imples
tcomple-
menti swha tiscal
ledao ne’sc
omp le
me nt. ⇢eone ’scomplementofanumbe ris
obtai
ne dbyr eve
r s
ingeach1a nd0t hroug houttheorigi
nalbi
narynumbe r.
Fore xample,theone’scomplementt o5woul dbe:
00000101 = 5
11111010 = -5

For8
,itwoul
dbe
:
00001000 = 8
11110111 = -8

⇢ispr oc
essi
sesse
nti
all
yoneofde
?nit
ion:wesi
mplyde
clar
etot
heworl
d
t
hat11110111 wi
llnow r
epres
ent78wit
houtspec
iFca
ll
ytr
yingtoj
ust
if
yit.
4[
CT]Te
chni
cal
l
y,f
ort
wo’
scompl
eme
nt,t
hemi
nimum shoul
dbe7327
68.Howeve
r,
Appl
eso4andInteg
erBASICres
tri
ctt
hemini
mum i
nte
gerto732
767
.Seecha
pte
r1 7
f
orawa yt
ofoolApple
so4i
ntodi
spl
ayi
ng73
2 7
68.

]
7
4 As
sembl
yLi
nes

Undoubt edl
ythe r
ea relovelyma t
hema t
icalproofsofsuchthingstha
tpresent
marvelouswa y
sofs pendinga na4ernoonbut, forourpurpos
e,agener
alnot
ion
ofwha ttheter
m me a
nswi llbesuWc i
ent.Fortunatel
ycomputersar
everygood
atfoll
owinga rbit
rarynumbe ri
ngscheme swi t
houta sk
ing“butwhyi sitthat
way?”
Nowl et
’sseeifwe’r
ea nyclose
rtoawor kings ys
tem:
5 00000101
+ -8 11110111
-3 11111100 = -3 (00000011 = +3)

Hmmm.
.
.Se
emst
owor
kpr
ett
ywe
ll
.Le
t’
str
yanot
her
:
-5 11111010
+ 8 00001000
3 00000010 = 2 (plus carry)

We l
l,ourans werswillberighthalftheti
me..
.Itt
urnsouttherei
saFna l
sol
uti
on,andthatistousewha tiscal
le
dt hetwo’
sco
mp l
ements
y s
tem.
⇢eonl ydiLerencebetweenthisandt heone’
scomplementsys
temistha
t
a4erder
ivingthene gati
venumbe rbyr eve
rsi
ngeachbitofi
tsc or
res
pondi
ng
posi
ti
venumbe r,wea ddone.
Soundsmy ste
rious.Let’
sseehowitlooks:

For75
: For78
:
5 = 00000101 8 = 00001000
↓ one
’sc
ompl
eme
nt.
.
. ↓
11111010 11110111
↓ nowa
ddone
..
. ↓
-5 = 11111011 -8 = 11111000

Nowl
et
’st
ryt
het
woe
arl
ie
rope
rat
ions
:

5 00000101
+ -8 11111000
-3 11111101 = -3
-5 11111011
Does 11111101 equal -3? + 8 00001000
3 00000011 = 3
sample #: 00000011 = 3 (plus carry)
one’s comp: 11111100
two’s comp: 11111101 = -3

Atlas
t!Itwor k
sinbothc as
es.Itturnsoutt
hattwo’
scomplementmath
worksi
nallcas
es,wi
ththecar
rybeingig
nor e
d.
Nowt hatyou’
vema s
ter
edthat,I
’l
lletyouoLthehookabi
tbys a
yingt
hat
noneofthisknowledg
ewi l
lbes pecFc
i all
yr e
qui
redinanyprog
ramsi nt
his
1
0.Addi
ti
ona
ndS
ubt
rac
ti
on 7
5

book.Howev
er,itisagoodthi
ngtoknow a
bouta
ndisve
ryusefuli
nunder-
st
andingt
henextide
a,t
hatoft
hesi
gna
ndov e
rSowSag
sint
heSt
atusRegi
st
er.

⇢eSi
gnFl
ag
Sincebit7ofa nyby tec anrepresentwhe therthenumbe rispositi
veorne g-
ative
,aSa gi nt heS t
atusRe gi
sterispr ovi
dedf ore a
syt es
tingofthena tureofa
givenby te.Whe neveraby t
eisl oadedi ntoar eg
iste
r,orwhe nana ri
thme t
ic
ope r
ationisdone ,thes ignSa gwillbec onditione daccordingtowha tthestateof
bit7is.
Fore xampl e,LDA#$80wi llsett
hes i
gnSa gt o1 ,whereasLDA#$40woul dset
itto0 .⇢isi stest
edus ingt hecomma ndsBPLa ndBMI. BPLs t
andsforBr anchon
PLus ,andBMIs tandsf orBr anchonMI nus.
Re gar
dlessofwhe thery oua r
eus ings i
gne dnumbe rs,the
seinstructi
onsc an
bev eryusefulfortesti
ngbi t7ofaby t
e. Manyt i
me sbit7isus e
dinv a r
iouspa r
ts
oftheAppl et oindicatethes tat
usofs ome thi
ng .Forexampl e,t
hek e
y boardloca-
tion,$C000,g et
sthehi ghbi tsetwhe nev e
rak e yispressed.
Upunt ilnow we ’
v ea l
wa ysteste
dbyc ompa ri
ngt hev al
uer e
turne dfrom
$C000t o#$80, sucha sint hisl
isti
ng:
1 ********************************
2 * AL10-BPL KEYTEST PROGRAM 1 *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 *
9 KYBD EQU $C000
10 STROBE EQU $C010
11 *
0300: AD 00 C0 12 CHECK LDA KYBD
0303: C9 80 13 CMP #$80
0305: 90 F9 14 BCC CHECK ; NO KEYPRESS
15 *
0307: 8D 10 C0 16 CLR STA STROBE
030A: 60 17 END RTS

⇢i sprog
ram wi
llst
ayinaloopunt i
lakeyispress
ed.⇢ek eypr
essi
s
de
tect
edbytheval
uer
etur
nedfr
om $C000bei
ngequa
ltoorgre
ate
rtha
n#$80.A
moreel
egantmet
hodi
stouset
heBPLc ommand:
1 ********************************
2 * AL10-BPL KEYTEST PROGRAM 2 *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300

]
7
6 As
sembl
yLi
nes

8 *
9 KYBD EQU $C000
10 STROBE EQU $C010
11 *
0300: AD 00 C0 12 CHECK LDA KYBD
0303: 10 FB 13 BPL CHECK ; NO KEYPRESS
14 *
0305: 8D 10 C0 15 CLR STA STROBE
0308: 60 16 END RTS

Inthiscas
e,aslongasthehighbitst
ayscl
ear(
i.e
. nok e
ypres
s),
theBPLwil
l
beta
k e
na ndtheloopcont
inued.Assoona sakeyi
spre s
sed,bi
t7willbes
ett
o1
andtheBPLwillf
ail
.⇢es tr
obeisthencle
aredandthereturndone.
As imil
artec
hniqueisusedforde t
ect
ingwhetherapa ddl
epushbutt
onhas
bee
npr es
sed.
1 ********************************
2 * AL10-BPL BUTTON TEST *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 *
9 PB0 EQU $C061
10 *
11 *
0300: AD 61 C0 12 CHECK LDA PB0
0303: 10 FB 13 BPL CHECK ; NO BUTTON PUSH
14 *
0305: 60 15 END RTS
1
1
1
1. DOSandDi
skAc
ces
s
Aug
ust1
981

Oneoft hemor euseful appli


cat
ionsofa ssemblyla ngua geisina cce s
s i
ngt he
diskdi rect
lytos t
or eorr etriev
eda t
a.Youmi g htdot histomodi fyinf orma t
ion
alreadyont hedi sk,sucha swhe nyou’rema k i
ngc ustom modi Fcati
onst oDOS ,
ort ode alwithda tawi thinFl esont hedi sk,sucha swhe ny ou’repa tchingor
repairingdama g e
dori mpr operl
ywr i
tte
nFl es.
Toc overDOSwe llr e qui
resmor et hanaf ewc hapterss ucha st his .My
intenthe re,then,i stos uppl yyouwi the noug hi nf
or ma ti
ont oa l
low y out o
accessa nyportionofadi ska ndtohavee noug hbasicunde rs
tandingoft heov e r
-
alllayoutofDOSa nddi skst oma kesomes e
ns eofwha ty ouFndt here.1

He r
e’swha twe ’
llcove rinthischapter.First
,we ’
llpa intag enera lov erview
ofwha tDOSi sa ndhowt heda t
aont hedi s
ke t
teisa r
ra nged.⇢e ny ou’ l
llea r
na
gene ralacces
sut i
li
tywi thwhi c
hy oucanr eada ndwr itea nys i
ng l
ebl ockofda t
a
from adi sk.Wi t
ht hese
, you’ l
lhaveastarti
ngpoi ntfory ourowne xplora ti
onsof
thisaspe c
tofy ourAppl ec omput er.

⇢eOv
erv
iew:
DOS
AnAppl
ewi
thoutadi
skdr
iveha
snowa
yofunde
rst
andi
ngc
omma
ndsl
ike
CATALOGorREAD.⇢es
enewwordsmustent
eri
tsv
ocabul
aryf
rom s
ome
where
.
Whe nanAppl ewi
thadi
skdr
iveat
tac
hedisFr
stt
urnedonoraPR#6i
sdone
,
thi
sinformati
oni sloa dedint othecomput erbyapr oc
essknowna sboot ing.
Duringtheboot ingpr ocess,as ma l
la mountofma c
hine
-la
ng ua gecodeon
thedis
ki nter
facec ardr eadsi nda t
af rom as mallport
ionofthedi sk.⇢i sdata
contai
nsthecodene cessarytor eada nother10 Kofma c
hinelanguager ef
e r
redto
asDOS .⇢i sblockofr outi
ne sisresponsibleforal
ldis
k-rel
ate
dope rationsinthe
comput e
r.Itnorma llyr es
ide sintheuppe r10Kors oofme mory,from $9600t o
$BFFF.
A4erbooting,t heor g anizat
ionoft heme mor yusedbyDOSl ookss ome-
thi
ngliketheFg ureshownont hene xtpage.

1Forade
tai
le
dlooka
tDOS
,Ir
ecomme
ndt
hebookBe
nea
thAp
pl ,
eDOSbyDa
nWor
th
andPi
et
erLechne
r(Rese
da ,CA:Quali
tySo4ware
,1981
).
[
CT]Forhintsonimpleme nt
ingthecodeinProDOS,seeBe
nea
thAp
plePr
o ,by
DOS
DanWorthandPiet
erLechner(Qual
it
yS o4wa
re,19
84).

]
7
8 As
sembl
yLi
nes

⇢eFr s ta reac ont ainst het hr e


ebuLe rsse tasidef ortheSowofda tat oa nd
from t hedi sk .AbuLe risabl oc kofme mor yr es
er vedt oholdda tat empor aril
y
whi l
ei t’
sbe ingt ransferre d. ⇢eMAXFILESc omma nd( ale
g a
l DOSc omma nd; see
yourma nua lify ouha ven’ te nc ount e
r editbe fore)c ana lt
erthenumbe rofbuf -
fersr e
s e
rv eda ndt husc ha ng et hebe gi
nninga ddres sfrom $9600t oot herv a l
ue s.
Asi tha ppe ns ,thr eebuLe r sa rea l
mos tne verne ede ds o,inapi nc hf orme mor y,
youc anus ua ll
ys e tMAXFILESt o2 ,a
ndo4e nj ustto1 .
Fore xa mpl e,ify ouha dope nedat extFl ec al
le dTEXTFILE,t heda tabe ing
readorwr i
tte nwoul dbet ra nsfe rr
edv iabuLe r1 .If
, whilethisFl ewa sstillope n,
youdi dac a talog ,buLe r2woul dbeputi nus e. If
,ins tea
d, y
ouope nedt woot her
Fles,sa yTEXTFILE1a ndTEXTFILE2,a ndthe nt ri
edt odoaCATALOG,y ouwoul d
getaNOBUFFERSAVAILABLEe rr or(a s
sumi ngMAXFILESwa ss etatthr ee )
.BuLe r1
startsat$9AA6, buLe r2a t$9853, andbuLe r3a t$9600. I
fMAXFILESi ss eta t3a s
inanor ma ls ystem,i t’soc casiona ll
yus ef
ultous et hede ads pac eoft heunus ed
buLe r3f ory ourownr out ine s.
⇢ema inDOSr out ine ss tarti
nga t$9D00a ret heone srespons i
bl efort he
interpretingc omma ndss uc ha sCATALOGa nd,i ng e neral
,fora llowi ngDOSt o
talktoBAS I Cv ias t
ateme nt spr e Fxedwi th<CTRL>D.
⇢eFl ema na geri sas e tofr outinestha ta ct
ua llyexecutet hev ariousc om-
ma ndss entv iat hema inr out ine sandt hatma kess ur eFlesarestor edi nal og i
c a
l
(we l
l,almos t)ma nne ront hedi sk.IttakescareofFndi ngaFl ey ouna me ,che ck-
ingt os eewhe theri t
’sunl oc ke dbe foreawr ite,Fndi nge mpt ys pac eont hedi sk
forne wda ta, andc ount l
e ssot he rt a
s ksrequiredt os toreev e
nt hes impl es
tFl e .
Whe ntheFl ema na g erg etsr e
adyt or eadda taf rom orwr iteda tat ot hedi sk,
itma kesus eoft her ema ini ngr outines,call
edt heRWTSr outines.⇢i ss tandsf or
Re ad/Wr iteTr ac kS ector.Tounde rstandfullywha tt hissecti
ondoe s,thoug h,it
wi l
lbene cessa rynowt ol ooka ttheg eneralor ganizationoft hedi ski tself
.

Di
ske
tteOr
gani
zat
ion
Physi
cal
ly
,adis
ket
tei
sc oat
edwi t
hama ter
ialv
erysi
mil
art
ot ha
tonma g-
net
ic
-rec
ordi
ngtape
.Smal
lpor t
ionsofthesurf
acearei
ndi
vi
dual
lyma g
net
iz
e d
tos
toret
hedatai
nthef
orm ofonesandzeros
.
1
1.DOSa
ndDi
skAc
ces
s 7
9

Butthediske
tteismorea na
logoustoav inylrec
ordthant oac ont i
nuouss t
ri
pof
ta
pe.Arrangedinc oncentr
iccir
cles,therearethi
rtyFvei ndividualtr
acks,each
ofwhichisdiv
idedintosixt
eensegme ntscall
edsect
ors2
.
Tr
acksarenumbe redfrom 0to3 4( $00to$22),st
arti
ngwi thTrack0a tthe
oute
redg eofthediske
tteandtrack3 4ne ar
estthecent
er .
S ec
tor sarenumbe red
fr
om 0t o15($00t o$0F)anda reinterl
eavedforfast
estaccess.⇢i sme ansthat
sec
tor1isnotphy s
ical
lynextt
os ec
t or0ont hedisk
ette
. Rather
, theorderis:
0-D-B-9-7-5-3-1-E-C-A-8-6-4-2-F

Bythet i
meDOSha sreadi na ndproc esse
dones ec t
or, i
tdoe sn’tha vesuW-
ci
entt i
met or e
adt hene xtphy si
call
y-contiguouss ectorpr ope r
ly.Ift hes ect
ors
wer earr
angeds equentiall
y,DOSwoul dha vetowa i
tf ora nothere nt i
rer e
volu-
t
ionoft hediskettetor eadthene xtsector.Bye xa miningt hes eque nc eyouc an
se
et hata4erreadingsector0 ,DOSc anle tasma nya ssixot hersec t
or sg obya nd
st
illhavetimetos tartlookingf ors ect
or1 .⇢i sa l
te
rna ti
onofs e c
tor sissome-
t
ime scall
edthesk ewfactororj ustsect
ori nt
erleaving.
Lookingforag iv
e ns e
ctori sdonewi thtwoc ompone nts.⇢eFr s tisaphy si
-
calone,whe r
einther ead/writehe adispos it
ioneda tas pec i
Fcdi stanc efrom the
centerofthediskett
etoa ccessag i
ventra ck.⇢es ect
ori slocatedv i
as o4wa r
eby
l
ook ingforaspec i
Fcpa tte
rnofi denti
fyingby t
es. Inaddi t
iont ot he2 5 6bytesof
actualdatawit
hinas ector,eachs e
c t
orispr ece
de dbyag r
oupofi dent ifyi
nga nd

2⇢r
oug
houtt
hisdi
scus
sionwewi
lla
ssumey
oua
reus
ingDOS3
.3,whi
chus
ess
ixt
een
sec
torspert
rack
.DOS3 .
2hasonl
ythir
tee
ns e
ctor
spertrac
kbuti
sra
pidl
ybecoming
obsol
ete
.Ifyou’
reusi
ngDOS3 .
2,thecorr
ect
ionfrom s
ixt
eent
othi
rt
eenshoul
dbe
madeinthetopi
cst
hroug
hout
.

]
8
0 As
sembl
yLi
nes

error-che ckingby tes.⇢e seinc l


ude ,f
ore xampl e
, s
ome t
hi nglike$00$03$FEf or
track$00,s ector$03,v olume$FE.Byc ont inuouslyr eadingt hesei de ntiFc at
ion
bytesunt i
l ama tchwi ththede sir
edv a l
ue soc c
urs,ag ivens ect
orma ybel oca t
ed.
⇢i ss o4wa reme thodofs e ct
orl ocationi susua l
lyc all
eds oft-sectoring ,and
it’
ss ome wha tuni quet ot heAppl e.Mos tot hermi crocomput ersus eha rd-sector
-
ing.Ha rd-sector i
ngme anst hatha rdwa rel ocate
st hese ctoraswe lla sthet rack;
sectorsa ref oundbyme ansofi ndexinghol eslocat
e daroundt hec ent erhol eofa
disk. EvenAppl edisk ett
esha vethisce nterhol e,
a l
ongwi thonet os ixte eni ndex-
inghol esi nt heme diaits e
lf,butt he sea ren’tactuall
yus edbyt hedi skdr ive.
Bec auset heAppl edoe s
n’tne edt hesehol est oinde x,usingt hes econds ideofa
diski sjus tama tterofpr ope rl
ynot chingt hee dgeoft hedi skja ckett oc reat
e
anot herwr i
te-e nablenot ch. We ’l
l notg oint othepr osandc onsofus ingt hesec-
onds idebutwi l
lle avet hatt oy ou.I t’
soneoft hos et opicsg uara ntee dt obe
wor tht we ntyt ot hi
r tymi nut esofc onv ersati
ona ta nyg atheringoft woormor e
Appl eowne rs.
Eachs ectorhol ds2 56( $100)b yt
e sofda ta.⇢i sda tamus talwa ysbewr it
ten
orr eada sas ing l
ebl ock.La rgeFl esaret hereforealwa ysma deupofmul tiplesof
256by tes.⇢usa5 20 -byteFlet akesupt hreee nti
res ec
tor s,event houg hmos tof
thet hirds ectori swa steds pace:
1 2 3
******************************** ******************************** ********
******************************** ********************************
******************************** ********************************
******************************** ********************************
******************************** ********************************
******************************** ********************************
******************************** ********************************
******************************** ********************************

Ce rt
aintracksa nds ectorsarer es
ervedfors peciFcinf orma t
ion.Tr ack1 7
($11),forexampl e,c ontainsthedi rect
ory.⇢i sg iv e
se achFl eana me ,anda l
so
tell
show t oFndoutwhi c hs e
c t
orsont hedi skc ontai
nt heda t
af ore achFl e
.
Tr a
ck1 7,sector0c ontainst heVol umeTa bleofCont ents( VTOC) ,whi chisa
ma st
ert abl
eofwhi c hs ectorscurrentl
yholdda ta ,andwhi cha rea vail
ablefor
stori
ngne wda t
a.I fa l
loft r
ack1 7i sdamage d,itma ybene ar
lyimpos si
bleto
retr
ievea nyda t
af rom t hedi ske venthought heFl esthems el
vesmi ghtsti
llbe
intac
t.
⇢eot herma inr eserveda reaisontracks0t hr ough2 . ⇢e setrack sholdthe
DOSt hatwillbeloa dedwhe nt hediskisboote d.Ifa nyoft he s
etrack sareda m-
agedi twillnotbepos s
iblet oboott hediskette.
..orifthedi skdoe sboot ,DOS
ma ynotf uncti
onpr ope rly.
Asav ar
iati
onont histheme ,byma kingc e r
tainc ontrolledchang estoDOS
direct
lyont hedisky ouc anc reat
ey ourownc us tom v ersi
onofDOSt oe nhance
wha tAppl eor i
ginallyha di nmi nd.⇢e see nha nc ementswi l
lbe comepa rtof
1
1.DOSa
ndDi
skAc
ces
s 8
1

yours y
stem whene veryoubooty ourmodi Feddiskett
e.Somemodi Fcati
onsof
t
histypea redis
cussedbelow.
Tog ainacces
st oas ect
ortoma kethesechang e
s,howeve
r,wene edt obe
abl
et oint
e rf
acewiththerouti
nesa l
rea
dyi nDOSt odoourownope rat
ions.⇢is
i
smos teasil
ydonebyus ingtheRWTSr outi
nesme nti
onedear
li
er.Fortunate
ly,
Appleha sma dethemf airl
yeasytousef r
om theus er’
sasse
mbly-
languagepro-
gram.
Tous eRWTS,youmus tdothreegene r
alopera
tions:
1.S peci
fythetrackands ec
toryouwi shtoacces
s.
2
.S pec
if
ywhe
ret
heda
tai
stobel
oade
dtoorr
eadf
rom (
tha
tis
,gi
vet
hebuLe
r
a
ddr
ess
).
3. Fi nall
y, callRWTSt odot her ead/writeope ration.
Ift heope ra t
ioni st obear ead,thenwewoul dpr esuma bl ydos ome thing
wi t
ht heda tai nt hebuLe ra4e rther eadisc ompl e
te.I
fawr it
ei stobedone , then
thebuLe rs houl dbel oa dedbe f
orec all
ingRWTSwi ththea ppr opr i
a t
eda t
a.Us u-
ally,thewa ya ll t
hi swor k sistor eadinas ectorFr st
,thenma kemi norchang esto
thebuLe r,andt he nwr itethes ectorba ckoutt ot hediskette.
S teps1a nd2a rea ctuall
ydonei ne s
sent i
a l
lythes ameope ration,byse t
ting
upt heIOBt able( “Input /Out puta ndc ont r
olBl ock”)
.⇢i sisde scribedinde t
ail
(alongwi tht hes ec t
oror ganiz at
ion)i nt heAp pl
eDOSMa nua ,buthe
l re’
s
enoug hinforma tiont o“ ma key ouda ng erous ,
”a sthesayingg oe s.
⇢ eIOBt a blei sat abley ouma keupa ndpl aceatal oc a
tionofy ourchoi ce.
(Youc anals oma k eus eoft heonea lreadyinme morytha tisus edi nDOSope r
a-
tions.)Mos tpe opl eIk nows eemt opr efert oma keupt he i
rown,butmype r-
sona lpr e
ferenc ei st ous etheonei nDOS .S inc emos tpe opleIk nowa ren’tat
thisk eyboardr ig htnow, I’
l
l explainhowt ous et hetabl
ea lreadys etupi nDOS .
⇢et ablei sma deupofs event e
enby tesa nds tar
tsa t$B7E8.I t
’sorganized
l
ik ethis:

Loc
ati
on Code Purpose
$B7E8 $01 IOBtypeindicat
or,mustbe$01
B7E9 $60 Sl
otnumbe rti
me ss
ixt
een3
B7EA $01 Drivenumbe r
B7EB $00 Expecte
dv olumenumbe r
B7EC $12 Tracknumbe r
B7ED $06 Sect
ornumbe r
B7EE $FB Low-orderby t
eofdevi
cechara
cte
ris
ti
cta
ble(
DCT)
B7EF $B7 High-orderbyteofDCT

3Not
ic
etha
tthi
sca
lcul
at
ion,l
ikemul
ti
ply
ingbyt
eni
nde
cima
l,me
ansj
ustmov
ingt
he
he
xdi
gitt
othel
e4onepl
ace
.

]
8
2 As
sembl
yLi
nes

Loc
ati
on Code Purpose
B7F0 $00 Low-orderbyteofdat
abuLersta
rti
nga ddre
ss
B7F1 $20 High-orderbyt
eofdatabuLerst
art
inga ddr
ess
B7F2 $00 Unused
B7F3 $00 Unused
B7F4 $02 Comma ndcode;$02=writ
e
B7F5 $00 Err
orc ode(orla
stbyt
eofbuLerreadin)
B7F6 $00 Actualvol
umenumbe r
B7F7 $60 Pre
v i
ousslotnumberacces
sed
B7F8 $01 Pre
v i
ousdr i
venumberacces
sed

Becaus eDOSha sa l
re adys etthist ableupf ory ou,i tisn’
tne cess a
ryt oloa d
everyl ocationwi ththea ppr opriatev alues.Inf a ct,ify ou’rewi l
lingt ocont i
nue
usingt hel astaccess
eddi skdr ive,youne edonl ys pe cifythet rac
ka nds ector, se
t
thec omma ndc ode,a ndt he nc lea
rt hee r
r ora ndv ol umev alue
st o#$00.How-
ever,forc ompl e t
ea ccura cy ,thes lota nddr i
v ev alue ss houlda l
sobes ets oy ou
knowf ors urewha tthee nt ryc onditi
onsa re.
Onc et heIOBt ab leha sbe ens etup,t heY- Re gistera ndAc c umul a
tormus t
bel oade dwi ththelow-a ndhi g
h-or de rbyt e
soft heIOBt able,andt hent heJSR
toRWTSmus tbedone .Al thoug hRWTSa ct
ua l
lys tartsa t$B7B5, t
hec allisusua ll
y
donea sJSR$3D9whe nDOSFr s
tboot s.⇢ea dva nt ageofc a
lli
nghe rei sthatif
Appl ee verc hangesthel oc ationofRWTS,onl yt hev e ctora ddressa t$3D9wi llbe
chang eda ndac al
lto$3D9wi ll s
til
lwor k.
Av ectoristheg ene r
a lte rm use df orame mor yl oc ati
ont hathol dsthei nfor-
ma tionf oras econdme mor ya ddre s
s. Av ec t
ori sus eds ot hatajumpt oas ing l
e
placei nme mor ycanber out edt oanumbe rofot he rme mor yloc ati
ons ,usua ll
y
thebe ginni ngsofv a
riouss ubr out i
ne s.Av ectorisr a therl i
keat elephones witch-
boa rd:ev e
nt houg htheus e ra lwaysc allsthes amea ddr ess,theprog ram Sowc an
bedi rectedt oa nynumbe rofdi Le r
e ntpl acess impl ybyc hangingt woby te
sa t
thev ectorloc a
tion.
Fore xampl e,s
uppos ea tl ocati
on$3F5wewe ret oputt hesethr eeby tes:
3F5: 4C 00 03

Li
st
edf
rom t
heMoni
tor
,thi
swoul
ddi
sas
sembl
eas
:
03F5- 4C 00 03 JMP $0300

Nowwhe ne
veryoudoac al
lto$3F5,ei
therbyaCALL1013or3F5G,t
hepro-
gr
am wi
llendupc a
ll
ingaroutineat$300.Itwoul
dnowbeas impl
ema tt
erto
wri
teaswi
tchi
ngprogra
mt hatwouldrewrit
ethetwobyte
sa t$3F6and$3F7so
t
hataca
llt
o$3F5wouldg oanywherewewa nte
d.
1
1.DOSa
ndDi
skAc
ces
s 8
3

Asitha ppens, $3F5isusedinj ustsuc


haf as
hionbyt hea mpersa
nd( &)
funct
ionofAppl eo4.⇢eAp
s pl
esoftIIBASICProgrammingRe f
erenceMa nual
provi
desmor einforma ti
ononthisfeature
.
⇢ebe stwa yt oFni s
he x
plaini
nghow t ousetheIOB tabl
ea ndRWTSi sto
prese
ntthefollowingut i
li
tytoaccessag i
ventr
ackands ec
torusingRWTS.We ’
l
l
thenste
pthroug ht heprograma ndlearnwhytheva r
iousste
psa redonetous e
RWTSsucce
ssfully
.
1 ********************************
2 * *
3 * AL11-GENERAL PURPOSE RWTS *
4 * DOS UTILITY *
5 * *
6 ********************************
7 *
8 *
9 * OBJ $300
10 ORG $300
11 *
12 CTRK EQU $06
13 CSCT EQU $07
14 UDRIV EQU $08
15 USLOT EQU $09
16 BP EQU $0A ; BUFFER PTR.
17 UERR EQU $0C
18 UCMD EQU $E3
19 * USER SETS THIS TO HIS CMD
20 *
21 RWTS EQU $3D9
22 *
23 * BELOW ARE LOCS IN IOB
24 SLOT EQU $B7E9
25 DRIV EQU $B7EA
26 VOL EQU $B7EB
27 TRACK EQU $B7EC
28 SECTOR EQU $B7ED
29 BUFR EQU $B7F0
30 CMD EQU $B7F4
31 ERR EQU $B7F5
32 OSLOT EQU $B7F7
33 ODRIV EQU $B7F8
34 *
35 READ EQU $01
36 WRITE EQU $02
37 *
38 *
39 *
40 ********************************
41 * ENTRY CONDITIONS: SET *
42 * TRACK, SECTOR, SLOT, DRIVE, *
43 * BUFFER, AND COMMAND. *
44 ********************************
45 *
46 *

]
8
4 As
sembl
yLi
nes

47 *
0300: A9 00 48 CLEAR LDA #$00
0302: 8D EB B7 49 STA VOL
50 *
0305: A5 09 51 LDA USLOT
0307: 8D E9 B7 52 STA SLOT
53 *
030A: A5 08 54 LDA UDRIV
030C: 8D EA B7 55 STA DRIV
56 *
030F: A5 06 57 LDA CTRK
0311: 8D EC B7 58 STA TRACK
59 *
0314: A5 07 60 LDA CSCT
0316: 8D ED B7 61 STA SECTOR
62 *
0319: A5 E3 63 LDA UCMD
031B: 8D F4 B7 64 STA CMD
65 *
031E: A5 0A 66 LDA BP
0320: 8D F0 B7 67 STA BUFR
0323: A5 0B 68 LDA BP+1
0325: 8D F1 B7 69 STA BUFR+1
70 *
0328: A9 B7 71 LDA #$B7
032A: A0 E8 72 LDY #$E8
032C: 20 D9 03 73 JSR RWTS
032F: 90 05 74 BCC EXIT
75 *
0331: AD F5 B7 76 ERRHAND LDA ERR
0334: 85 0C 77 STA UERR
78 *
0336: 60 79 EXIT RTS
80 *
0337: CD 81 CHK

Whe nt hispr ogra mr uns ,ita s


sume st heus erha ssett
hede si
redva l
uesfor
thetracka nds ectorwa nted, whi chslotanddr iv
et ous e
,whe rethebuLe ris,
and
whethe rtor eadorwr it
e .
Startingwi tht heFr stfunc tionalline,line48 ,thebyteforthev olumenum-
berint heIOBt a ble( VOL)i ss tuLe dwi t
ha0 . Av alueof0her etel
lsRWTSa nyv ol
-
umenumbe risa cce ptabledur ingt hea c
c ess
.I fwewa ntedt oa ccessonlya
part
icula rv olumenumbe r,av aluef r
om $01 t o$FEwoul dbeus e
di nste
adof
$00.4

Int hene xtf ours etsofope rati


ons ,theus erv al
uesforthes lot
,dr i
ve,tr
ac k
,
ands ectornumbe r
sa reputi ntotheIOBt abl
e.Not icethat,toha vethiswor k
prope r
ly,y oumus ts etUSLOT( $09)t os ix
te entime sthevaluef orthes l
oty ou
wisht ous e.Fore x ampl e, t
oa ccessslot5y ouwoul dstorea#$50( 80decimal)in
loca
tion$09j us tbe for ec al
lingt hisrout i
ne.

4[
CT]$FFi
snotav
ali
dDOSv
olumenumbe
r.
1
1.DOSa
ndDi
skAc
ces
s 8
5

⇢ene xtpa irofstateme ntstak etheus ercomma ndUCMDa ndputt hatint he


table
.Ify ouwa ntt or e
a das ector,s e
tUCMD=$01.Awr itei sUCMD=$02.Af e w
otheropt i
onsa r ese l
dom us e d.⇢e sea r
ede scribedinmor ede taili
nt heDOS3 .3
ma nualint hes ectiononRWTS.
Nex t,thebuLe rpoi nteri ssettot heva lueg i
venbyt heus erinl ocati
ons$0A
and$0B.⇢er e quireds pac ei s25 6by t
es( $100)a ndc anbeputa ny wheret hat
won’ tconSi ctwi t
hda t aa lreadyi nt hec omput er
.Conv e nientpl acesa ret he
numbe rt hre eDOSFl ebuLe r( $9600) ,t
hei nputbuLe ritself($200) ,ora nar e
aof
me morybe low$9600pr otec tedbys e
tti
ngHIMEMt oa na ppr opriatev al
ue.5Int he
example st ha tfol l
ow,I ’l
lus et hea re
af r
om $1000 t o$10FFbe causenoBAS IC
program wi llber unninga nd$1000i sani cenumbe r.Int hisc ase,$0Aa nd$0B
willbeloa de dwi t
h#$00a nd#$10, respect
iv el
y.
Lastofa ll
, theY- Re gi
steri sloadedwi th#$E8a ndt heAc cumul atorisloade d
with#$B7, thel ow- ordera ndhi gh-orderby tesoftheIOBt ablea ddress.
A4e rt hec alltoRWTSv iat hev ectorat$3D9, t
hec arrySa gisc heckedf ora n
error.I
fthec arryi scl
ea r,t
he rewa snoe rrora ndther outiner eturnsv iatheRTS.
Ifane r
rori se nc ountered,t hec odewi l
lbet r
ansfer
redf rom t heIOBt abletot he
userlocation. ⇢epos siblee rrorc odesa r
e:

Code Condition
$10 Diskwrite-prot
ected,andc a
nnotbewr i
tte
nto.
$20 Volumemi smatche rr
or.Volumenumbe rfoundwa sdi
Lerentthan
speci
Fed.
$40 Driveerror.Ane r
r orothert
hanthethreedesc
ribedherei
sha ppening
(I/
Oe rr
or ,forexa
mpl e).
$80 Readerror .
RWTSwi llt
ryforty
-ei
ghtti
me stogettoagoodread;ifit
sti
ll
fai
ls,itwill
returnwi t
hthise
rrorcode.

DOSModi
Kca
tions
⇢eERRby t
eoftheIOBtablei
ssome wha tunusua l
inthatitdoe snotrema i
n
at0e veniftheread/wri
teoperat
ionwa ss uccess
ful.Ina c
tualope rat
ion,ifa n
err
ordoe snotoc c
ur ,t
heERRby tewillcontainthel as
tby teoft hesectorjust
acc
essed.
I
tisimpor t
antthere
foretoalwaysus ethec arr
ySa gt ode t
e c
twhe t
hera n
err
orha soccurr
ed.Infa
ct,asyourexperi
enc egrows,youwi llnoticet
ha tagreat
manys ubrouti
nesusethecarrySaga sani ndic
a t
oroft here s
ultsoftheope ra-
t
ion.Inthec as
eofRWTS,thecarr
ywillbeclearediftheac c
esswa ssuccessf
uland

5Not
e:⇢ei
nputbuLe
rca
nbeus
edonl
yte
mpor
ari
lydur
ingy
ourownr
out
ine
.Ify
ou
r
etur
ntoBASI
C,ordoanyi
nputorDOScomma
nds
,da
tai
nthi
sar
eawi
ll
bede
str
oye
d.
Othe
rtha
ntha
t,i
t’
saha
ndypla
cetous
e.

]
8
6 As
sembl
yLi
nes

seti
fane r
roroc curr
ed.Itisnotne c
essarytoconditi
ont hec ar
rybeforecall
ing
RWTS.
Oneoft hebe stwaystog ra
spthisroutinei
stousei ttomodi f
ytheDOSon
asampl ediska ndobser
v et
hedi Lere
nce s
.Be f
oreproceedingwiththee xamples
,
bootanAppl ema st
erdisk,thenINITabl ankdisk.
⇢i swi llbeourtestpiec
e,so
tospeak.Donott rythesee xper
imentsonadi s
ka l
readyc onta
iningimpor t
ant
data.I
fdonec orr
ectl
ythec hangeswon’thur t
,butifane r
r orweretooc c
ury ou
couldloseag oodde a
lofwor k!

Di
sk-
Vol
umeModi
Kca
tion
Fir
stins
tal
lthe s
ect
or-
acc
essrouti
ne at$300.Now i
nse
rtt
hes
ampl
e
di
sket
te.
Ente
rtheMoni
torwi
thCALL-151andtypein:
*06: 02 02 01 60 00 10
*E3: 01

⇢i
sas
sume
syourdi
ske
ttei
sindr
ive1
,sl
ot6
.Nowe
nte
r:
*300G

⇢edi
skdr
ivemot
ors
houl
dcomeon.
Whe
nits
topst
ypei
n:
*10AFL

Yous
houl
dge
tsome
thi
ngl
iket
his
:
10AF- A0 C5 LDY #$C5
10B1- CD D5 CC CMP $CCD5
10B4- CF ???
10B5- D6 A0 DEC $A0,X
10B7- CB ???
10B8- D3 ???
10B9- C9 C4 CMP #$C4

⇢i sappa r
entnonsenseistheAS CI
Idataforthewords“DISKVOLUME”
inreverseorder.⇢isisl
oa de
di nwhe nt
hedi s
kisboot
eda ndisus
edinal
lsub-
sequentcatal
ogoperat
ions.
⇢eda t
awa sret
riev
e dfr
om track2,se
ctor2,andputinabuLerst
art
ingat
$1000.⇢es equenc
ewe ’
reinter
este
dinstart
satbyt
e$AFint ha
tsec
tor
.Tomod-
if
ythat,typein:
*10AF: A0 D4 D3 C5 D4 A0 AD
*E3: 02
*300G

⇢eFrs
tli
nerewri
te
stheASCIIda
tat
her
e,t
heE3:02c
hang
est
hec
omma
nd
t
o“wri
te
,”a
ndthe300Gputsi
tba
ckonthedi
sk.
1
1.DOSa
ndDi
skAc
ces
s 8
7

Nowr e
bootthedi
skandt hentypeinCATALOG.Whe nt
hecatal
ogpri
ntsto
t
hescr
een,t
henewc har
act
ers“DISK - TEST 254”shoul
dappea
r.6Byus
ingthe
ASCI
Icharac
terc
harti
nAppe ndixE,youc a
nmodi fythi
spar
tofthedi
sket
teto
sa
yanyt
hingyouwishwit
hinthetwel
v e
-char
acterl
imit.

Ca
tal
ogKe
ypr
essModi
Kca
tion
Rei
nst
al
lthes
ect
ora
cce
ssut
il
it
y,putt
hes
ampl
edi
ski
nthedr
ivea
gai
n,a
nd
t
ypein:
*06: 01 0D 01 60 00 10
*E3: 01
*300G

⇢i
swi
ll
rea
dtr
ack1
,se
ctor$0D,
int
othebuLe
r.Ty
pei
n:
*1039L

⇢eFr
stl
inel
is
teds
houl
dbe
:
1039- 20 0C FD JSR $FD0C

Cha
nget
hist
o:
*1039: 4C DF BC (JMP $BCDF)

Andr
ewr
it
etot
hedi
sk:
*E3: 02
$300G

Nowr
eadi
nthes
ect
ionc
orr
espondi
ngt
o$BCDF(
tra
ck0
,se
ctor6
)byt
yp-
i
ng:
*06: 00 06
*E3: 01
*300G

Anda
lt
ert
hiss
ect
ionwi
t 7
h:
*10DF: 20 0C FD C9 8D D0 03 4C 2C AE 4C 3C AE
*E3: 02
*
*300G

6[
CT]⇢edi
skv
olumenumbe
r(2
54)i
sst
il
lpr
int
ed.
7[
CT]⇢eMoni
torl
is
ti
ngl
ook
sli
ket
his
:
10DF- 20 0C FD JSR $FD0C
10E2- C9 8D CMP #$8D
10E4- D0 03 BNE $10E9
10E6- 4C 2C AE JMP $AE2C
10E9- 4C 3C AE JMP $AE3C

]
8
8 As
sembl
yLi
nes

Asithappe ns,thispar tofthedis


ki s
n’tuse da ndpr ovidesanic eplacet oput
thisnewmodi Fcati
on.
Whe ny our eboota 4e rma ki
ngthisc hang e
,pl aceadi skwithal ongc ata
log
oni tinthedr i
vea ndt y
pei nCATALOG.Whe nt hel i
stingpa usesa 4ert heFr s
t
groupofna me s
,pr e
ss<RETURN>. ⇢el is
tings houlds top,leavi
ngt hena me sj
ust
shownont hes creen.I finste
a dofpre s
si
ng<RETURN>y oupr essanyot he rkey
,
thecatal
ogwi llcontinuejus tasitnorma l
lywoul d, goingont othene xtg roupof
name s.
Bothoft hesemodi Fcati
onswi l
lgointoe Lectwhe neveryouboott hes ample
disk.⇢e s
ef eaturesca nalsobepr opagate
dt oot he rdisksbyboot ingt hes ample
diskandusingt hene wDOSt oINITf r
e s
hdi sks.
Ma nymodi Fcationst ot heexis
tingDOSc anbema det hiswa y,a ndwe
hav e
n’tev
ens t
artedtot al
ka boutstori
ngbi naryda taing eneral.

Be
llModi
Kca
tiona
ndDr
iveAc
ces
s
(1)⇢eFr stti
mey ouc al
ltheacc essut i
li
tyf rom t heMoni tor,itwillreturn
wi t
hjustthea steri
skpr ompt. A4erthat,unl essyouhi tRESETordoaCATALOG, i
t
wi l
lret
urnwi tht hea st
eris
ka ndabe ep.⇢i sisbe causet hes ta
tusstorageby tefor
theMoni tor( $48)g e t
ssettoanonz erov a
luebyRWTS.I fthebe epa nnoysy ou,
modi f
ythea cces sutil
itytoset$48bac kto#$00be forer eturning.
(2)Ifyous etthes lot
/dri
v eval
uest os ome thingot he rthany oura cti
vedr ive
,
thea c
tiv
edr ivewi llsti
llbetheonea ccess edwhe ny oudo, fore xample,thene xt
CATALOG. ⇢i sisbe causeDOSdoe s
n’ta ctuall
yl ooka tthel ast-sl
ot/dri
ve-accessed
va l
ueswhe ndoi ngaCATALOG.I nste
ad,i tlook sa t$AA66f orthev olumenumbe r
(usuall
y#$00) ,a t$AA68f orthedrivenumbe r,anda t$AA6Af orthes l
otnumbe r
(timessixtee
n) .Ify ouha veBAS ICora s se
mbl y
- l
ang uag epr ogramswhe rey ou
wa nttoc hanget hea c
tiv
edr iveval
ue swi thoutha vingt odoaCATALOGorg i
ve
anotherc omma nd,t henjustPOKEorSTAt hede siredv a l
ue sinthe s
et hreeloca-
tions.
Ha vefun!
1
2
1
2.S
hi(Ope
rat
orsandLo
gic
alOpe
rat
ors
S
ept
emb
er1
981

Shi
5Ope
rat
ors
He reI ’
dliket oc overtwoma ing roupsofa s
sembly-l
a nguag ec omma nds:
shi
ftope r
a tosa
r ndlo gic
alo pe ra
tors.S hi4sa r
ee asi
ertounde r
stand,s owe ’
lldo
them Frst.
You’llrecallt
ha ttheAc c umula torhol dsas i
ngleeight-
bitv alue ,
a ndt hatin
previ
ouspr ogramsitha sbe enpos sibl etotestindivi
dualbitsbye x ami ningSa g s
intheS tatusRe gi
ster.Ane xampl eoft hiswa susedinte s
tingbi t7a 4era nLDA
operati
on.I ftheAc cumul atori sl
oa dedwi thav a
luefrom $00 t o$7F,bi t7i s
cle
ara ndonl yBPLt estswills ucce
e d,s incethes i
gnSa grema insc lear.If,how-
ever,av al
uef rom $80t o$FFi sloade d,BMIwi l
lsucc
eeds i
nc ebit7woul dbes e t
;
hencethes ignSa gwi l
l al
sobeaone .
⇢ es hi4c omma ndsg reatlyextendoura bil
it
ytotestindividua lbi t
sbyg iv-
ingust heopt i
onofs hi4inge achbi ti ntheAc c
umul a
toronepos itiont othel e4
orright.⇢e rea r
et wodi r
ec tshi4c omma nds,ASL(Ar it
hme ticS hi4Le 4)a nd
LSR(Log icalShi4Rig ht).

ASLLAr
ithme
ticShi
5Le
5

I
nt heca
seofASL,eac
hbitismovedtothele4onepositi
on,wit
hbit7goi
ng
i
ntothecar
ryandbit0be i
ngforc
edto0 .I
na ddit
iontothecarry
,thesi
gnand
z
eroSagsar
ea l
soaLec
ted.Someexample
sappe a
rinthefol
lowingtabl
e.

Va
lue Bi
nar
y Re
sul
t Bi
nar
y (
C)Ca
rry (
N)Si
gn (
Z)Ze
ro
$00 0000 0000 $00 0000 0000 0 0 1
$01 0000 0001 $02 0000 0010 0 0 0
$80 1000 0000 $00 0000 0000 1 0 1
$81 1000 0001 $02 0000 0010 1 0 0
$FF 1111 1111 $FE 1111 1110 1 1 0

]
9
0 As
sembl
yLi
nes

Int heFr s
tc ase,t he r
e’snone tc hanget otheAc cumul a
tor,alt
houg ht he
carrya nds i
g nSa gsa rec le
areda ndt hez eroSagiss e
t.⇢e0a teachbitpos i
tion
wa sr eplacedbya0t oit srig
ht.
Howe ver,i nt hec as
eof$01,t hev alueint heAc cumulatordoubl est o
bec ome$02a sthe1i nbi t0mov e
st ot hebit1pos iti
on.Inthisc a
se,allthree
Sag swi llbec l
ea re
d.
Whe nt hes t
arti
ngv al
ueis$80org re
a t
er,t
hec arr
ywi l
lbeset.Inthec aseof
$80i tself
, t
heAc cumul atorreturnsto0a 4ertheshi4,si
ncetheonly1i nt hepa t
-
tern, bit7,ispus hedouti ntot hecarry.
Not i
c ethati nthec aseof$FF, t
hes i
gnSa gg e
tssetasbit6intheAc c
umul a
-
tormov e
si ntopos it
ion7 .Re membe rt hatinsomes cheme s,
bit7isuse dtoindi-
cateane gativenumbe r.
ASLh asthee Le ctofdoubl i
ngt heby tebe i
ngope rat
edon.⇢i scanbeus ed
asa ne asywa ytomul tiplybyt wo. I
nf act,byusingmul ti
pleASLs,youc anmul ti
-
plybyt wo,f our ,ei
g ht,s i
xteen,ands oon,de pendingonhowma nyy ouus e.In
thedi scus s
ionofDOSa ndRWTSi ncha pter11,youmi ghtremembe rthattheIOB
tabler equiredt heslotnumbe rbyteint het a
bletobes i
xteenti
me sthetruev al
ue.
Ifyoudi dn’twa ntt odot hemul tipl
ica t
iona hea
doft ime,youcoulddoi tiny our
acce ssprog ram, asbe l
ow.
*
*
*
A5 09 LDA USLOT
0A ASL
0A ASL
0A ASL
0A ASL
8D E9 B7 STA SLOT
*
*
*

USLOTh oldst hevaluefrom onetos e


venthatyoupa sstotherouti
neand
SLOTi sthelocationint heIOBtableinwhichthevalueforUSLOT*1
6s houl
dbe
plac
e d.Ev e
nt houg hthef ourASLslookabitredundant,noti
cethattheyonly
tookf ourbytes.Infact,theLDA/STAste
psconsumedmor ebyte
s(Fve)thanthe
fourASLs.
Ing ener
al,then,ASLisusedfortheset
ypesofopera
tions:
1
. Mul
ti
plybyt
wo,
four
,ei
ght
,ands
oon.
2
.Se
torc
lea
rthec
arr
yfo
rfr
eewhi
les
hi4i
ngf
ors
omeot
herr
eas
on.
3. Testbit
sthroug
h6 .Not
e:⇢i sc
anbedone,butit
’sus
uall
ydonethiswa
y
onl
yforbit6;t
herea
re,
ingener
al,
bet
terwa
ysofte
sti
ngspec
iFcbit
s,whi
chwe’
ll
des
cri
beshortl
y.
1
2.S
hi4Ope
rat
orsa
ndLog
ica
lOpe
rat
ors 9
1

⇢ecomple
me ntoftheASLcommandisLSR.I
tbeha
vesi
dent
ic
all
yex
cept
t
hatt
hebi
tsa
lls
hi4totheri
ghtandbi
t7be
come sa0.

LSRLLog
ica
lShi
5Ri
ght

LSRc anbeus edt odi v


idebymul tipl
esoftwo.It’
sals
oani cewaytote s
t
whethe ranumbe rise venorodd.Ev ennumbe r
salwayshavebit0cl
ear.Odd
al
wa yshaveitset
. Bydoi nga nLSRfoll
owedbyBCCorBCS, youc a
ntes
tforthis.
Whe theranumbe risoddore venissometi
me scal
le
di t
spari
ty.Anevennum-
berha sapari
tyof0 ,
a nda noddnumbe rapar
ityof1.
LSRa l
soconditi
onst hesignandz e
roSags.
InbothLSRa ndASL,onee ndortheotheralway
sg et
sfor
cedtoa0.Some -
ti
me sthisisnotdesirable.⇢es olut
iontothi
sistherotaec
t omma nds
,ROLa nd
ROR(ROt ateLe4,ROt ateRight)
.

ROLLRot
ateOneBi
tLe
5 RORLRot
ateOneBi
tRi
ght

Inthe secomma nds,


thec arrynotonlyrecei
vesthepushedbi
t,butit
sprevi
-
ousc ontentsareusedtol
oa dthenowa vai
la
bleendposit
ion.
ROL a ndROR areusedr atherinfre
quentl
ybutdot urnupoc cas
ional
lyin
ma t
hf unc t
ionssuchasmul t
iplyanddi vi
derouti
nes.
Sofar ,al
ltheexampleshav eusedtheAc c
umulatorasthebyt
etobes hi
4ed.
Asi thappe ns,eit
hertheAc c
umul atororame morylocati
onma ybes hi
4ed.
Addr ess
ingmode sa l
soincludeZe roPa ge,
Xa ndAbs olut
e,X.⇢eY- Reg
ist
er
cannotbeus edasanindexina nyoftheshi4operat
ions.

]
9
2 As
sembl
yLi
nes

Log
ica
lOpe
rat
ors
Log i
c alope ratorsa re,totheuni nitiated,someoft hemor ee sotericoft he
assembl y-l
a nguag ec omma nds.Aswi t
he verythingwe ’vedonebe for e,thoug h,
withal it
tlee xplanationt hey’
llbe c
omequi teuse f
ul.
Le t
’ss tartwi thoneoft hemos tc ommonl yus edc omma nds , AND.Yo u’re
alreadyfa mi l
iarwi tht heba si
ci deaoft hisonef rom y ourda i
lys pe ech.Ift his
andt hata reac ertainwa y,t
henI ’l
l dos ome t
hing. ⇢i ssamewa yoft hinkingc an
bea ppliedt oyourc omput er.
Aswe ’ves een,e ac
hby t
eisma deupofe ightbits
. Let’
stakejustt hel e
4- mos t
bit,bit7,a nds eewha tk i
ndofi de asc anbepl ayedwi th.Nor maltex tout puton
theAppl ei sa lwa ysdonewi tht hehi ghbi tset.⇢a ti s
,allcharactersg oingout
throughCOUT ( $FDED)s hould bee qua lt oorg reaterthan#$80 ( 10 000 00 0
binary )
.Li kewise ,whe nwa tc
hingt hek eyboa rdforak eypress,wewa itunt il
$C000h asav aluee qual toorg reaterthan#$80.
S uppos eweha dapr ogram whe rei
nwewoul dprintcha r
acterstot hes creen
onlywhe nak eywa spre s
seda ndas t
anda r
dc ha r
acterwa sbeings e ntthroug h
thes yste
m.Wha twe ’
res ayi
ngi stopr intc haracte
rsont hesc reeno nlywhe n
botht hec ha r
actera ndt hek e
yboa rdbuLe rshowbi t7s etto1.
Wec andr a w as impl echa rtt hati ll
ustrate
sa llthepos si
bili
ties( andy ou
knowhowf ondc omput erpeoplea reofc harts)
.

Cha
rac
terBi
t7
0 1
Ke
yboa
rd0 0 0

Bi
t7 1 0 1

⇢ec ha r
tshowsf ourpos si
bil
it
ies.Ifthec ha r
acter
’sbit7i s0( anon-stan-
dardc hara cte
r)andt hek eyboardbitis0( nok eypr e
ss),the
nt hec ha r
acteri
snot
pri
nte d(a0r esul
t).Likewi s
e,ifonlyoneoft hec onditi
onsisbe ingme tbutnot
theothe r,t henther esultisstil
l0,a ndt hec haracterisst
illnotpr i
nted.Onl y
whe nbot hde s
ire
dc ondi t
ionse xi
stwi l
lwebea llowedt oprint,ass hownbyt he
onea sther e s
ult
.
Ta kent oitsextreme ,wha twee ndupwi thisane wma thema ticalf
unc ti
on,
AND.Int hec a
seofas inglebina r
ydi gi
t(orpe rha pswes houldc al
li tabigit
),the
possi
bi l
it
ie sarefew,andt hea nswersareg ive
na sas imple0or1 .
Wha ta boutlargernumbe rs
?Doe st
het erm 5AND3ha veme a ning?Itturns
outtha titdoe s,a
lthoug hthea nswerint hiscasewi l
lnotbe8 ,andi tisnowt hat
wemus tbec auti
ousnott oletourda il
yus eoft hewor d“ a
ddition”bec onfused
withourne wme aning.
1
2.S
hi4Ope
rat
orsa
ndLog
ica
lOpe
rat
ors 9
3

Aswel
ooka
tthe
senumbe
rsonabi
nar
yle
vel
,howt
oge
tther
esul
tof5AND
3wi
ll
bemor
eobv
ious
.
x = 5 0101
y = 3 0011
x AND y 0001=1

Ifwet akethec ha r
tc reatedearli
e randa pplyittoe ac
hs etofma tchingbi t
s
i
nxa ndy ,weobt aint her esul
tshown.S t
a rti
ngont hele4,two0 sg i
v e0a sa
res
ult. Fort henex ttwobi ts,onlyas ingle1i spresentinea c
hc ase,
sti
llgiving0
asar es ul
t.Onl yint hel astpos i
ti
ondoweg etthene ces
sary1 sinbit0ofb oth
numbe rstoy ie
lda1i nt her esult
.
⇢us5AND3doe sha veme ani
ng ,andt hea nswe ris1.(Trythatatpartie
s!)
Don’ tbedi scoura g
e dify oudon’ tseethei mme diat
eva l
ueinthisope ra
tion;
yous houldg ues
sbynowt ha tever
ythingi sg oodf orsome t
hing!
ANDi sus edforav a
rietyofpur pos es
.⇢e seinc l
ude:
(1)Tof orcezerosinc ertainbitpos i
tions .
(2)Asama skt oletthroug honlyone sinc ert
a i
npos i
ti
ons .
Whe na nANDope r
a t
ioni sdone ,thec ont e
ntsoft heAc cumulatora r
eAND’ d
wit
ha nothe rspeciFedv alue.⇢er esultoft hisope rat
ionisthenputba ckint he
Accumul ator.⇢eot herv aluema ybee itherg ivenbywa yoft hei mme diate
modeorhe ldiname mor yl ocati
on.⇢e sea resomepos si
blewa ysofusi
ngAND:
LDA #$80
AND #$7F
AND $06
AND $300,X
AND ($06),Y

Tounde rstandbe tte


rhowANDi sus e
d,wes houldc lari
fysomeot heride as
.
Oneoft hesei sthena tureofa ss
embl y-la
ng uagepr og ramsi ng eneral
.Ibe l
ieve
that
, a
ta nyg ivenpoi ntinapr ogra
m, oneoft wok indsofwor kwi l
lbeg oingon.
Onei stheo per at
ionalmo de ,wheres omes peciFcta s
k ,suc hasclic
kingas pe aker
orre a
dingapa ddle,istak ingplace.Att he semome nt s,dataa ssuchdoe snot
exis
t.Int heot herc as
e,t hep roce
ssingmo de,dataha sbe enobt ainedf r
om a n
operati
ona lmodea ndt hei nformationi sprocess
e da nd/ orpa ss
edtos omeot her
routi
neorl oc ati
oni nme mor y.
Ag ivenr outi
ner arel
yi sentir
e lyinj ustonemodeort heot her,buta ny
givenstepus ua l
lyfal
lsmor ei ntoonec ategorythant heot her
.
⇢e sei deasa r
eimpor ta ntbecaus e,
ing eneral
, al
l oft helogica
lope rat
or sare
useddur ingt hepr ocessi
ngpha sesofapr og r
am.Att hos etimes,somek indof
dataisbe i
ngc a rr
ieda l
ongi nar e
gisterorme mor yloc at
ion. Partofthepr oc e
ss-
ingthatoc cursiso4e ndonewi t
hthel ogicaloperators.

]
9
4 As
sembl
yLi
nes

Inthec as
eofthet womode sofuse,opera
tiona
landproc
ess
ing,wea r
e
r
eal
lyjustta
lki
ngabouttwodiLerentwaysofl
ookingatt
hesa
meope r
ati
on.To
i
ll
ust
ratethi
s,e
xaminethispa
rti
aldis
ass
emblyoftheMonit
ors
tar
ti
nga 1
t$FDED:
*FDEDL
FDED- 6C 36 00 JMP ($0036)
FDF0- C9 A0 CMP #$A0
FDF2- 90 02 BCC $FDF6
FDF4- 25 32 AND $32
FDF6- 84 35 STY $35
FDF8- 48 PHA
FDF9- 20 78 FB JSR $FB78
FDFC- 68 PLA
FDFD- A4 35 LDY $35
FDFF- 60 RTS

Fornor ma lte xtout putont heAppl e,t heAc cumul atori sl oadedwi tht he
AS CI Ivaluef ort hec ha ra ctert obepr inted, thehi ghbi tisse t
, andaJMPt oCOUT
($FDED)i sdone .Fr om l ook inga tthel i
s t
ing , youc ans eet hata t$FDEDt he reisa n
indirectjumpba sedont hec ont entsof$36, $37( cal
ledav ector )
.
Ifthiss ee msal itt
lev ag ue,the nc ons ide rf oramome ntwha tIc al
ltheCo w
ofc ontrolint hec omput e r.⇢i sme anst ha tt hec omput eri sa lwa ysexe cuti
nga
prog rams ome whe re.Ev enwhe nt here’snot hi ngbutaSa shingc ursoront he
screen,t hec omput eri ss tilli nal ooppr ogr amme dt og etac ha ra
cterf r
om t he
key board.Whe ny ouc ally ourownr out ine s,t hec omput eri sjustt empor ar i
ly
leavingi tsowna ctivit
iest odoy ourt asksunt ili thitst ha tlastRTS.I tthe ng oes
bac ktowha titwa sdoi ngbe for e;usua ll
y ,tha t
’swa it
ingf ory ourne xtcomma nd.
Whe nc ha rac tersa repr intedt ot hes cree n,di sk,pr inter,ora ny whe ree lse
,
ther e
’saSowofc ont rolt ha tc arriesalongt hec ha ract
e rt obepr i
nted. Forv irtu-
all
ye ve
r yc ha racterpr int ed, the6 5 02s canst hr oug ht hisr eg i
ona site xecutest he
codene cessa rytopr intt hec har a
c t
er .
Nor ma l
ly ,$36,$37 p oint st o$FDF0( atl ea stbe foreDOSi sboot ed).⇢i s
ma ys eem al itt
lea bs urdunt ilyour e al
izet ha tag reatde alofSe xibil
ityisc reated
byt hev ector .Fori nsta nce ,aPR#1,s uc ha sy oudowhe nt ur ningonapr i
nt er,
redirects$36,$37t opoi ntt ot hec ard,whi chi nt urn,a 4e rpr int i
ngac ha racter
,
usua ll
yr etur nst owhe re$36, $37u sedt opr int.
⇢ec a rdt husbor rowst heSowofc ont roll onge noug ht opr intthec harac-
ter,a4e rwhi chi tg ivesc ont rolba ckt ot hes cree npr intr out ine.Li kewise,whe n
DOSi sboot ed, $36,$37 g etsr edirectedf rom $FDF0 t o$9EBD,whi chi swhe re
phr asespr ec ededbya<CTRL>Da rede te cted.I fno<CTRL>Di sf ound,t heout put
isre t
urne dt o$FDF0.
Now,ba ckt owha tANDi sus edf or.Nor ma llywhe nt her outinee nt e
rsa t
$FDF0, theAc cumul a t
orwi l
lhol dav aluebe twe en$80a nd$DF2.⇢ec har a
cte r
s

1[
CT]⇢i
sisf
ort
heAppl
eIIPl
us.
Res
ult
sont
heAppl
eIIorAppl
e//
ewi
ll
bedi
Ler
ent
.
2[
CT]Be
twe
en$80a
nd$FFf
orc
omput
erswi
thl
owe
rca
ses
uppor
t.
1
2.S
hi4Ope
rat
orsa
ndLog
ica
lOpe
rat
ors 9
5

from $80t o$9Fa reallcont


rolcharact
ersandarepassedthr
oug hbytheBCCf ol-
lowi ngt heFr stCMP.Cha ract
erspassi
ngt hi
st e
stwillbetheus ualalphabeti
c,
nume ri
c ,andspec i
alcharac
tersshowninAppe ndixE.You’l
lnoticeatthispoint
anANDwi ththec ontent
sof$32i sdone.Locati
on$32i scal
ledINVFLGa ndus u-
allyhol dse i
ther$FF,$7F,or$3Fde pe
ndingonwhe t
herthec omputerisinthe
NORMAL, FLASHING,o rINVERSEtextmode .Let
’sass
umet hattheAc c
umul atoris
hol dingt hevalueforanor ma lA.Looka tthefoll
owingtabl
et oseewha tha p-
pe nswhe nanANDi sdonewi theachoftheseval
ues.

Example1: He
x Bi
nar
y ASCI
I
Accumulat
or: $C1 1100 0001 A
INVFLAG: $FF 1111 1111 −
Resul
t: $C1 1100 0001 A

Example2:
Accumulat
or: $C1 1100 0001 A
INVFLAG: $7F 0111 1111 −
Resul
t: $41 0100 0001 A (flashing)

Example3:
Accumulat
or: $C1 1100 0001 A
INVFLAG: $3F 0011 1111 −
Resul
t: $01 0000 0001 A (inverse)

I
nt heFr ste xampl e ,ANDingwi th$FFy iel
dsar e
sultidenti
calt otheor i
ginal
v
alue
.⇢er e
sulti sident icalbec ause,withe ac
hbi tsetto1 ,ther esult
ingbi twi l
l
a
lwaysc omeoutt hes amea sthec orr
espondingbi tint heAc cumul at
or.( Ca n
y
oug uesswha tt her esultofANDi ngwi t
h$00woul da lwaysyield? )⇢i sme a ns
t
hatt
hec haracterc ome souti ni tsorigi
nalform.
I
nt hes econdc a s
e ,ANDingwi th$7Fha sthee Lectofforcinga0i nbi t7of
t
heresult.Ex ami ningt hec ha rtinAppe ndixE,wec ans eethat$41c orresponds
t
oaSa shingA.
⇢eAppl eus e
st hel eadingt wobi t
stode t
ermi nehowt opr intt hechar act
e r
.
I
fthelea dingt wobi tsa reo E,the nt hecharacterwillbei ninver se.Ifbit7i s0
a
ndbi t6i s1 ,the nt hec hara ct
erwi l
lbepr i
ntedi nSa shi
ngmode .Ifbit7i ss et
,
t
henthec haracterwi llbedi splaye dinnor maltext.
Us i
ng t heAND ope r
a t
orf or cesa0i nt hede s
iredpos it
ionsa ndl etst he
r
emainingbi tpa tternt hroug h.
I
ng e neral,the n,t hewa yt ous eAND istos etame mor yl ocati
on( ort he
i
mme diatev alue)e qua ltoav aluewhos ebit
sa reallsetto1e xceptf orthos ethat
y
ouwi sht ofor ceto0i nt heAc c umul at
or.

]
9
6 As
sembl
yLi
nes

Youc ana l
sothinkofANDa sa ct
ingratherli
keas cr
eent
ha tlet
sonlyc ert
ain
partsoft heima g
et hrough.Whe nINVFLG iss etto$3F,t
hel e
adingbi t
swi l
l
alwaysbe0 ,regar
dlessofwhe t
he rtheywe resetatentry
;hence,theex pre
ssi
on
ma sk.
Some ti
mesFg uringexac
tl
ywha tvalueyous houldusef
ort hedesi
re dresul
t
istri
cky.Asag eneralfor
mul a
,Fr stdeci
dewha tbit
syouwantt oforcet o0and
thencalculat
ethenumbe rwit
ha llotherpositi
onssettoones.⇢i swillgivethe
propervaluetouseint hema s
k. Fore x
ampl e,t
ode ri
vethei
nv ers
edisplayma sk
value:
1. De terminewhi c
hbi t
stoforceto0 :
00xxxxxx

2
. Ca
lcul
at
ewi
tht
her
ema
ini
ngpos
it
ionss
ett
oone
s:
00111111 = $3F (63)

Trythiswiththede s
iredresul
tofforcingonl
ybit7to0a ndseeifyoug e
t
thepropervalueforINVFLGof$7F.
Ap p
leDOSTo olKitusersshouldnotethatwhenshi
4ingtheAc c
umulator,
Apple’sass
emblerrequire
st headdit
ionoftheAope r
and(Example
:LSR A).⇢is
appl
iest oASL,LSR,ROR,a ndROL.Mos totherass
embler
sdonotr equi
retheA
operand,andthatisthesyntaxusedinthi
sbook .

BI
T
⇢ec omma nds ome wha tr elatedt oANDi sBIT. ⇢i sisprovidedt oallowt he
usertode termi nee as
ilythes tatusofs pe ci
Fcbi t
si nag ivenby te.Whe nBITi s
execut
ed, quiteanumbe roft hing sha ppe n.Firstofa ll,bits6and7oft heme m-
orylocationa ret r
ansferreddi rec t
lyt ot hes igna ndov erSowbi tsoft heS tatus
Regist
er.S i
ncewe ’venotdi sc ussedt heov erSowSa g ,letmes aybr ieSyt ha tit
s
rel
atedc omma nds,BVCa ndBVS, ma ybeus edj ustasBPLa ndBMIa reuse dtot es
t
thesta
tusoft hes ignSa g.I
fV( t
heov erSowSa g)iscl
e ar,BVCwi llsucceed. I
fVi s
set
,BVSwi llwor k.
Mos ti mpor t
ant,though, ist hec ondi ti
oningoft hez eroSag .Ifoneormor e
bit
sint heme mor ylocati
onma tchbi tsse ti
nt heAc cumul at
or,thez eroSa gwi l
l
becleared( Z=0 )
.Ifnoma tchi sma de, Zwi l
l bes et(Z=1 ).
⇢i sisdonebyAND-
ingtheAc cumul atora ndt heme mor yl ocationa ndc onditi
oningZa ppropr i
-
ate
ly.⇢ec onf usingpa rtist ha tthisma ys ee ms ome wha tback ward.Al as,it’
s
unavoida ble;it
’sjustoneoft hos enot estos cribbleiny ourbooks oastor eme m-
berthequi r
ke a chtimey ouus eit.
Not et hatoneoft hema ina dvanta gesofBIT i st hattheAc cumul atoris
unaLectedbyt het est.
1
2.S
hi4Ope
rat
orsa
ndLog
ica
lOpe
rat
ors 9
7

Her
ear
eex
a mpl
esofhowBITmi
ghtbeus
ed:
Ex
ampl
e1:Tote
stforbi
ts0a
nd2,
set:
LDA #$05 ; 0000 0101
BIT MEM
BNE OK ; (1 OR MORE BITS MATCH)

Ex
ampl
e2:
Tot
estf
orbi
t7,
seti
nme
mor
y:
CHECK BIT $C000 ; (KEYBOARD)
BPL CHECK ; (BIT 7 CLR, NO KEY PRESSED)
BIT $C010 ; (ACCESS $C010 TO CLR STROBE)

Ifyouwanttote
stforal
lofas
pec
iFcs
etofbi
tsbe
ingon,
theANDc
omma
nd
mus
tbeuse
ddirect
ly.
Example3:Totes
tforbot
hbit
s6and7bei
ngon:
LDA CHAR
AND #$C0 ; 1100 0000
CMP #$C0
BEQ MATCH ; BOTH BITS “ON”

⇢isla
stexa
mpleiss
omewhatsubtl
e,i
nt ha
tther
esul
tint
heAcc
umula
tor
wi
llonl
yequalt
heval
uewit
hwhi c
hi twasAND’
difea
chbitse
tto1i
nthetes
t
va
lueha
sanequi
val
entbi
tontheAccumulat
or.

ORAa
ndEOR
⇢e sel
a s
ttwoc omma ndsbringupa nintere
stinger r
orofs ortsint heEng-
l
ishlang uage,andt ha tisthediLer e
nc ebe t
we ena ninclusiveORa ndt hee xc
lu-
sieOR.
v Wha tallthi sisaboutisthephe nome nont hatsayings omethingl i
ke“I’l
l
gotot hes t
oreifi tstopsr aini
ngo rifabusc ome sby ”hast wopos si
blei nte
rpre-
tat
ions.⇢eFr sti st ha tifeit
herev entha ppens,ande veni fbothe v
e ntsoccur,
thenther esul
twi llha ppen. ⇢isisc al
leda ninclusi
v eORs tatement.
⇢eot herpos sibil
ityisthatthec onditi
onst obeme tmus tbeoneort he
otherbutnotbot h. ⇢i smi ghtbec al
ledt hepur es
tf orm ofanORs tat
e me nt.I
tis
eit
herni ghtorda y, butne verboth.⇢i si scall
eda ne xc
lusiveORs tate
me nt.
Ina sse
mbl yla ng uage,theinclusiv
eORf uncti
oni sc al
ledORAf orORAc cu-
mul a
tor .⇢eot he ri sc al
ledEORf orEx cl
usiveOR.⇢et ablebelow s howst he
chart
sf orbothfunc tions.

ORA Ac
cumul
ator EOR Ac
cumul
ator
0 1 0 1
0 0 1 0 0 1
Me
mor
y Me
mor
y
1 1 1 1 1 0

]
9
8 As
sembl
yLi
nes

Firs
t,cons iderthetableforORA.I feit
herorbothc orres
pondingbitsinthe
Acc umul atora ndt hetes
tv al
uema tch,thentheres
ultwillbeaone .Onlywhe n
neithe rbi
ti s1doe sa0v alueresultforthatbit
.⇢ema inuseforORAistoforcea
onea tag ivenbi tposi
ti
on. Inthisma nner
,it
’ssomethingofthecompl e
me ntto
theus eoft heANDope r
atortoforcez eros.
⇢ef ollowingt ab
lepr es
entss omee xa
mpl e
soft hee Le
ctoft heORAc om-
ma nd.

Ex
ampl
e1: Ex
ampl
e2:
Ac
cumula
tor
: $80 1000 0000 $83 1000 0011
Val
ue: $03 0000 0011 $0A 0000 1010
Re
sult
: $83 1000 0011 $8B 1000 1011

Use ofORA c ondi t


ionst hes i
gna ndz e
r oSags,de pendi
ngont her esul
t,
whichisa utomati
callyputi ntotheAc cumul a
tor.
⇢eEORc omma ndi ss omewha tdiLerentinthatthebi t
sintheresultarese
t
to1onl yi foneort heot he rofthecorrespondingbitsint heAc c
umul atorand
tes
tv al
ueiss e
tto1 ,butnotbot h.
EORh asanumbe rofus es
.⇢emos tcommoni sine nc odi
ngda t
a.Ani nter-
est
inge Lectofthetableist hat
,foranyg iventestva
lue,theAc cumulat
orwi llSi
p
backa ndf ort
hbe twe ent heor ig
inalva l
uea ndther es
ul teac
ht i
met heEORis
done.Se etheexampl esint hetabl
ebe l
ow.

Ac
cumula
tor
: $80 1000 0000 $83 1000 0011
Val
ue: $03 0000 0011 $0A 0000 1010
Re
sult
: $83 1000 0011 $89 1000 1001

Ac
cumula
tor
: $83 1000 0011 $89 1000 1001
Val
ue: $03 0000 0011 $0A 0000 1010
Re
sult
: $80 1000 0000 $83 1000 0011

⇢isSippingphe nome nonisuse


de xt
ensi
vel
yinhi-re
sgraphicstoall
owone
imagetoov erl
aya notherwithoutdest
royi
ngtheima gebel
ow.EORa l
soc anbe
usedtor eve
rses peci
Fcbi ts
:S i
mplyplaceonesint heposi
tionsy ouwi shto
rev
e r
se.
Youmi ghtFndi tquiterewardi
ngtowr i
teyourowne xperimentalrouti
ne
tha
twi l
lEORc e
rtainrangesofme morywithgive
nv al
ues.⇢enma kethesecond
passtoveri
fythattheda t
aha sbeenres
tored.
⇢i sisespe
cia
ll
yi nt
erest
ingwhe n
donee i
theront hehi -r
ess cr
eenorblocksofAS CIIdatasucha sont het ex
t
scr
een.
1
2.S
hi4Ope
rat
orsa
ndLog
ica
lOpe
rat
ors 9
9

Itwoul dbeas ha meifyou’vesta


yedwiththi
schapterlongenoughtoread
t
hroug hallt
hisanddi dn’tgetaprogramforyoureLorts
,soIoLe rthedemon-
s
trat
ionpr ogramtha tfoll
ows.Itprovi
desawayofv is
uall
ye xpe
rimenti
ngwith
t
hedi Lere
nts hi
4sa ndl ogi
caloperat
ors
.Assembletheassembly-
langua
gepro-
g
raml ist
eda ndsa
vei ttodiskunderthenameAL12.OPERATOR.
1 ********************************
2 * AL12-BINARY FUNCTION DISPLAY *
3 * UTILITY *
4 ********************************
5 *
6 *
7 * OBJ $300
8 ORG $300
9 *
10 NUM EQU $06
11 MEM EQU $07
12 RSLT EQU $08
13 STAT EQU $09
14 *
15 YSAV1 EQU $35
16 COUT1 EQU $FDF0
17 CVID EQU $FDF9
18 COUT EQU $FDED
19 PRBYTE EQU $FDDA
20 *
21 *
0300: A9 00 22 OPERATOR LDA #$00
0302: 48 23 PHA
0303: 28 24 PLP
0304: A5 06 25 LDA NUM
0306: 25 07 26 AND MEM ; <= ALTER THIS
0308: 85 08 27 STA RSLT
030A: 08 28 PHP
030B: 68 29 PLA
030C: 85 09 30 STA STAT
030E: 60 31 RTS
32 *
030F: A9 A4 33 PRHEX LDA #$A4 ; ’$’
0311: 20 ED FD 34 JSR COUT
0314: A5 06 35 LDA NUM
0316: 4C DA FD 36 JMP PRBYTE
37 *
0319: A5 06 38 PRBIT LDA NUM
031B: A2 08 39 LDX #$08
031D: 0A 40 TEST ASL
031E: 90 0D 41 BCC PZ
0320: 48 42 P0 PHA
0321: A9 B1 43 LDA #$B1 ; '1'
0323: 20 ED FD 44 JSR COUT
0326: A9 A0 45 LDA #$A0 ; 'SPC'
0328: 20 ED FD 46 JSR COUT
032B: B0 0B 47 BCS NXT
48 *

]
1
00 As
sembl
yLi
nes

032D: 48 49 PZ PHA
032E: A9 B0 50 LDA #$B0 ; '0'
0330: 20 ED FD 51 JSR COUT
0333: A9 A0 52 LDA #$A0 ; 'SPC'
0335: 20 ED FD 53 JSR COUT
54 *
0338: 68 55 NXT PLA
0339: CA 56 DEX
033A: D0 E1 57 BNE TEST
58 *
033C: 60 59 EXIT RTS
60 *
033D: EA 61 NOP
033E: EA 62 NOP
033F: EA 63 NOP
64 *
0340: C9 80 65 CSHOW CMP #$80 ; STAND CHAR?
0342: 90 10 66 BCC CONT
0344: C9 8D 67 CMP #$8D ; <C/R>
0346: F0 0C 68 BEQ CONT
0348: C9 A0 69 CMP #$A0 ; 'SPC'
034A: B0 08 70 BCS CONT
71 *
034C: 48 72 PHA
034D: 84 35 73 STY YSAV1
034F: 29 7F 74 AND #$7F ; FORCE '0' IN BIT 7
0351: 4C F9 FD 75 JMP CVID
76 *
0354: 4C F0 FD 77 CONT JMP COUT1
78 *
0357: 00 79 EOF BRK
80 *
81 *
0358: 87 82 CHK

⇢e
nent
ert
hea
ccompa
nyi
ngAppl
epr
ogr
ama
nds
avei
tunde
rthena
me
3
AL12.OPERATOR.A.

1 IF PEEK (768) <> 169 THEN PRINT CHR$ (4);"BLOAD AL12.OPERATOR,A$300"


2 REM IF DOS 3.3 THEN SET UP CSW VECTOR
3 IF PEEK(1002) = 76 THEN POKE 54,64: POKE 55,3: CALL 1002: GOTO 10
4 REM IF PRODOS, SET UP OUTPUT LINK AT $BE30,31
5 POKE 48688,64: POKE 48689,3
10 REM LOGICAL OPERATOR PROGRAM
15 OP = 774: F = 768: PH = 783: PB = 793
20 TEXT: HOME: GOTO 1000
100 KEY = PEEK ( -16384): IF KEY > 127 THEN 1000
110 A = PDL(0):A = PDL(0)
120 M = PDL(1):M = PDL(1)
125 POKE 6,A: POKE 7,M
130 CALL F: REM EVALUATE FUNCTION

3[
CT]S
pac
esa
ndda
she
swe
rec
lea
nedupt
oma
ket
hes
cre
endi
spl
aymor
ere
ada
ble
.In
a
ddi
tion,f
orProDOSwema nua
ll
ychangetheout
putvectorat$BE30,$BE31t
opoi
nt
t
oCSHOW($340)
.Se
efoot
not
e1inchapt
er29formoredi
scussi
on.
1
2.S
hi4Ope
rat
orsa
ndLog
ica
lOpe
rat
ors 1
01

140 R = PEEK (8): S = PEEK (9)


200 VTAB 11: HTAB 1: PRINT "OPCODE:";: POKE 6,OC: GOSUB 500: VTAB 11: HTAB
32: PRINT "'";O$;"'"
210 VTAB 14: PRINT "ACC:";: POKE 6,A: GOSUB 500: HTAB 30: PRINT " ";: HTAB
30: PRINT CHR$ (A);: VTAB 14: HTAB 33: PRINT "(P0)": POKE 1742,A: IF
A = 13 OR A = 141 THEN VTAB 14: HTAB 30: INVERSE : PRINT "M": NORMAL
215 IF O1 = 7 THEN VTAB 16: PRINT "MEMORY:";: POKE 6,M: GOSUB 500: HTAB
30: PRINT " ";: HTAB 30: PRINT CHR$ (M);: VTAB 16: HTAB 33: PRINT
"(P1)": POKE 1998,M: IF M = 13 OR M = 141 THEN VTAB 16: HTAB 30:
INVERSE : PRINT "M": NORMAL
220 IF O$ < > "BIT" THEN VTAB 18: PRINT "RESULT:";: POKE 6,R: GOSUB 500:
HTAB 30: PRINT " ";: HTAB 30: PRINT CHR$ (R): POKE 1270,R: IF R = 13
OR R = 141 THEN VTAB 18: HTAB 30: INVERSE : PRINT "M": NORMAL
230 VTAB 20: PRINT "STATUS:";: POKE 6,S: GOSUB 500: PRINT
240 VTAB 22: HTAB 10: PRINT "N V - B D I Z C"
250 GOTO 100
499 END
500 REM PRINT BITS & HEX
510 HTAB 10: CALL PB: HTAB 26: CALL PH: RETURN
1000 REM SELECT FUNCTION
1010 T = PEEK(-16368):FC = FC + 1 -(KEY = 136) * 2: IF FC > 8 THEN FC = 1
1011 IF KEY = 193 THEN FC = 1: REM 'A'=AND
1012 IF KEY = 194 THEN FC = 3: REM 'B'=BIT
1013 IF KEY = 197 THEN FC = 4: REM 'E'=EOR
1014 IF KEY = 204 THEN FC = 5: REM 'L'=LSR
1015 IF KEY = 207 THEN FC = 6: REM 'O'=ORA
1016 IF KEY = 210 THEN FC = 7: REM 'R'=ROL
1019 IF FC < 1 THEN FC = 8
1020 FOR I = 1 TO FC: READ O$,OC,O1: NEXT I: RESTORE
1025 IF KEY = 155 THEN PRINT CHR$ (4);"PR#0": END : REM <ESC>
1030 POKE OP,OC: POKE OP + 1,O1: HOME
1050 ON FC GOSUB 1100,1200,1300,1400,1500,1600,1700,1800
1055 POKE 32,0
1060 A = -1: GOTO 100
1100 REM 'AND'
1110 POKE 32,9
1140 VTAB 2
1145 PRINT " AND 0 1 "
1150 PRINT " -------------"
1155 PRINT " 0 ! 0 ! 0 !"
1160 PRINT " -------------"
1165 PRINT " 1 ! 0 ! 1 !"
1170 PRINT " -------------"
1175 PRINT : HTAB 7: PRINT "'AND'"
1180 VTAB 23: PRINT "^ ^"
1185 RETURN
1200 REM 'ASL'
1220 VTAB 1: HTAB 9: PRINT "-------<==-------"
1225 HTAB 4: PRINT "----- 7 6 5 4 3 2 1 0 <-- '0'"
1230 HTAB 4: PRINT "! -----------------"
1235 HTAB 3: PRINT "---"
1240 HTAB 3: PRINT "!C!"
1245 HTAB 3: PRINT "---"
1250 VTAB 7:HTAB 16:PRINT "'ASL'": HTAB 8: PRINT "(ARITHMETIC SHIFT LEFT)"
1280 VTAB 23: HTAB 10: PRINT "^ ^ ^"
1285 RETURN

]
1
02 As
sembl
yLi
nes

1300 REM 'BIT'


1310 POKE 32,9
1340 VTAB 2
1345 PRINT "AND/BIT 0 1 "
1350 PRINT " -------------"
1355 PRINT " 0 ! 0 ! 0 !"
1360 PRINT " -------------"
1365 PRINT " 1 ! 0 ! 1 !"
1370 PRINT " -------------"
1375 PRINT : HTAB 7: PRINT "'BIT'"
1380 VTAB 23: PRINT "M M ^": PRINT "7 6";
1385 RETURN
1400 REM 'EOR'
1410 POKE 32,9
1440 VTAB 2
1445 PRINT " EOR 0 1 "
1450 PRINT " -------------"
1455 PRINT " 0 ! 0 ! 1 !"
1460 PRINT " -------------"
1465 PRINT " 1 ! 1 ! 0 !"
1470 PRINT " -------------"
1475 PRINT : HTAB 7: PRINT "'EOR'"
1480 VTAB 23: PRINT "^ ^"
1485 RETURN
1500 REM 'LSR'
1520 VTAB 1: HTAB 9: PRINT "-------==>-------"
1525 HTAB 2: PRINT "'0' --> 7 6 5 4 3 2 1 0 -----"
1530 VTAB 3: HTAB 9: PRINT "----------------- !"
1535 HTAB 29: PRINT "---"
1540 HTAB 29: PRINT "!C!"
1545 HTAB 29: PRINT "---"
1550 VTAB 7: HTAB 15: PRINT "'LSR'": HTAB 8: PRINT "(LOGICAL SHIFT RIGHT)"
1580 VTAB 23: HTAB 10: PRINT "0 ^ ^"
1585 RETURN
1600 REM 'ORA'
1610 POKE 32,9
1640 VTAB 2
1645 PRINT " ORA 0 1 "
1650 PRINT " -------------"
1655 PRINT " 0 ! 0 ! 1 !"
1660 PRINT " -------------"
1665 PRINT " 1 ! 1 ! 1 !"
1670 PRINT " -------------"
1675 PRINT : HTAB 7: PRINT "'ORA'"
1680 VTAB 23: PRINT "^ ^"
1685 RETURN
1700 REM 'ROL'
1720 VTAB 1: HTAB 9: PRINT "-------<==-------"
1725 HTAB 4: PRINT "<---- 7 6 5 4 3 2 1 0 <----"
1730 HTAB 4: PRINT "! ----------------- !"
1735 HTAB 4: PRINT "! --- !"
1740 HTAB 4: PRINT "----------->!C!------------"
1745 HTAB 16: PRINT "---"
1750 VTAB 8: HTAB 15: PRINT "'ROL'": HTAB 9: PRINT "(ROTATE ONE BIT LEFT)"
1780 VTAB 23: HTAB 10: PRINT "^ ^ ^"
1785 RETURN
1
2.S
hi4Ope
rat
orsa
ndLog
ica
lOpe
rat
ors 1
03

1800 REM 'ROR'


1820 VTAB 1: HTAB 9: PRINT "-------==>-------"
1825 HTAB 4: PRINT "----> 7 6 5 4 3 2 1 0 ---->"
1830 HTAB 4: PRINT "! ----------------- !"
1835 HTAB 4: PRINT "! --- !"
1840 HTAB 4: PRINT "------------ C <-----------"
1845 HTAB 16: PRINT "---"
1850 VTAB 8:HTAB 15: PRINT "'ROR'": HTAB 9: PRINT "(ROTATE ONE BIT RIGHT)"
1880 VTAB 23: HTAB 10: PRINT "^ ^ ^"
1885 RETURN
2000 DATA AND,37,7, ASL,10,234, BIT,36,7, EOR,69,7, LSR,74,234, ORA,5,7,
ROL,42,234, ROR,106,234
32000 REM COPYRIGHT (C) 1981
32010 REM ROGER R. WAGNER

⇢eba si
ct heoryofope rati
onf ort hepr og rami stor ewrit
el ocati
ons$306
and$307wi tht heappr opr i
atevaluestoc re a
tet hediLerentope r
ator s
.⇢e sev al-
uesarec ont ai
ne dint heda tastateme ntonl i
ne2 0 0
0oft heAppl e
s o4pr ogra m.
Inadditi
on,t herearer outinestopr intt hev alueinl ocati
on$06i nbot hbi na ry
andhe xf orma ts.Also,the r
ei sar outinet oshowc ont r
olc hara
c t
ersini nv erse .
Youma ywi sht oexa minee achoft heset ode terminet helogic,ifany ,behi nd
thei
rope ration.
⇢eAppl eso4pr og r
a mi ts
elfope ratesbyg et
ti
ngav aluefortheAc cumul a-
torandtheme mor ylocationfrompa ddl es0a nd1 .⇢edoubl ereadsi nlines1 1 0
and1 20mi nimizet heint e
racti
onbe twe ent het wopa ddl es
.Pr e
s si
nga nyk e y
advancest hedisplaytot hene xtfunction; thele4a rr
owba cksup.Pr ess
ingA, B, E,
L,O,orRwi l
l j
umpt othes ele
c t
edf unction.
⇢es c r
e e
ndi splays howst hehe xa ndbi na r
yv aluesfore a
c hnumbe ra nd
als
owha tc ha r
acterwoul dbepr i
nte dv iaaPRINTCHR$(X)s ta
teme nt( cont rol

EOR 0 1
-------------
0 ! 0 ! 1 !
-------------
1 ! 1 ! 0 !
-------------

'EOR'

OPCODE: 0 1 0 0 0 1 0 1 $45 'EOR'

ACC: 1 1 0 0 0 0 0 1 $C1 A (P0) A

MEMORY: 1 1 0 1 1 0 1 1 $DB [ (P1) [

RESULT: 0 0 0 1 1 0 1 0 $1A Z Z

STATUS: 0 0 1 0 0 0 0 0 $20

N V – B D I Z C
^ ^

]
1
04 As
sembl
yLi
nes

c
harac
tersareshownininver
se)
. Tothefarri
ghti
sthec
hara
cte
robt
aine
dwhen
t
heval
uei spokedint
othescr
eendispla
ypa r
tofmemory.
IsupposeifIwe reapuris
tt heenti
rethingwoul
dhavebeenwrit
teni
n
a
sse
mblyl anguage
.Ohwe l
l,
ma ybenextti
me .
1
3
1
3. I
/ORo
uti
nes
Oc
tob
er1
981

I
nc ha pter1 1Idi s
cuss
edhow t oaccessthediskus i
ngt heRWTSr outi
ne.
⇢e reisa notherwa ytorea
dt hedis
kt ha
tismor esi
mi l
artothepr ocedureus ed
inBAS IC.⇢ea dvant
ageofthissy
stemisthatwene ednotbec oncerneda bout
whattracka nds ect
orwe ’
reusing
,sinceDOSwi l
lhandletheFlesjustasi tdoes
inano rma lprog ra
m.⇢edi sadva
ntageisthatwemus tha vethee quivalentof
PRINTandINPUTs t
atement
stouseinourpr ogra
mstos endandr e
ceivetheda t
a.
So,bef
or egoinga nyfurt
her,l
et’
sdigre
sstoinput/
outputrouti
nes .

Pr
intRout
ine
s
Ihavet
wofavori
tewa y
sofsimul
ati
ngthePRINTs
tat
eme
nt.⇢eFr
stwa
s
de
scr
ibe
dinear
li
erchapt
ersandl
ooksl
iket
his
:
1 ********************************
2 * AL13-DATA-TYPE PRINT 1 *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 *
9 COUT EQU $FDED
10 *
0300: A2 00 11 ENTRY LDX #$00
0302: BD 0E 03 12 LOOP LDA DATA,X
0305: F0 06 13 BEQ DONE
0307: 20 ED FD 14 JSR COUT
030A: E8 15 INX
030B: D0 F5 16 BNE LOOP
17 * (ALWAYS UP TO 255 CHRS)
18 *
030D: 60 19 DONE RTS
20 *
030E: 84 21 DATA HEX 84
030F: C3 C1 D4 22 ASC "CATALOG"
0312: C1 CC CF C7
0316: 8D 00 23 HEX 8D00
24 *
0318: 00 25 EOF BRK

]
1
06 As
sembl
yLi
nes

⇢i st ypeofr outineus esade Fne dda tabloc kt ohol dt heAS CI Ivalue sf or


thec ha ra cte rswewi sht opr int.⇢epr i
nt i
ngi sa ccompl ishe dbyl oadingt heX-
Reg i
s terwi th$00a nds teppi ngt hroug ht heda tat ableunt ila$00i se ncount ere d.
Eac hby tel oadedi sputi ntot heAc cumul atora ndpr intedv iatheJSR t oCOUT
($FDED) .Whe nt he$00 i sFna ll
yr eache d,t heBEQonl ine1 3i st ak e
na ndwe
returnf r om t herout inev i
at heRTSa tDONE.
⇢ene wi t
em ofi nt ere
s tint hisl i
stingi stheus eoft he$84a st heFr s tc ha r-
acterpr i
nt e d.⇢i swi l
lbepr i
nteda sa<CTRL>D,a ndt hewor dCATALOGt ha tfol -
lowswi ll bee xecuteda saDOSc omma nd.
⇢ee ssenc eoft hisc ha pter’
sme ssage,a longwi tht her outine s,istha ta ny
DOSc omma ndc anb ee x ecutedf r om a ssembl yl a ng uag ee xa ct
lyt hes amewa y
it’
sdonef r om BAS IC. Onene edonl ypr ecedet hec omma ndwi t
ha<CTRL>Da nd
termi na tet hec omma ndwi thac a rriager eturn. Be caus eDOSl ooksa ta l
l cha rac -
tersbe ingout put ,i twi lls eet he<CTRL>D c ha ractera ndbe havea ccordi ng ly.
(READa ndWRITEa res ome t
hi ngofa ne xceptiont ot histe chni quebutc ans til
lbe
donewi thonl ymi nora djustme nts.)
You’ ll a l
sonot ic
et hene wa s sembl erdi rective: ASC. ⇢i sdi rectivea l
lowsy ou
toputa nAS CIIs tringdi re
c tl
yi ntoal is
tingwi thoutha vingt ous etheHEXc om-
ma nd, whi c hwoul dne cessitateal otofme ntalc onv ers i
ons .
Tr ye nt eri
ngt hispr og rama ndt henc alli
ngi twi the ithe ra300Gf rom t he
Moni tororaCALL768f rom BAS IC. Re me mbe r,ther out inec a nnotbeBRUN.
Whe nr unni ngt hi spr ogram,y ouma ynot i
c eadi Le renc ebe t
we enaCALL
768a ndt he300G. Whe nc all
e dfrom t heMoni torwi t
ht he300G, stra ngec ha ra c-
tersa repr int edouta 4e rtheCATALOGi sdone .I tisi mpor tantt onot ehe r et ha t
anyDOSc omma ndwi l
lov erwr itet hei nputbuLe r( $200+)wh ene xec ute d.
Bec auset heMoni tore x pectst olookf orc omma ndsa 4e ry our300G, itma int ains
ani nte rna lpoi nte rtowhi c
hc hara cterint hei nputbuLe ri tisc urrent l
ye v alua t-
ing.Fore x ampl e,i tnor ma llywoul dbepe rfectlyl eg altoe x ecutet hec omma nd:
300G 200.210.
⇢epr oblemi s
,itwoul dn’twor kwi ththi sprog ra
m.Le t’
ss eewhy .Whe n
youe nte
r300G<RETURN>, theinputbuLe rhol dsFv ec hara
c t
ers
:3-0-0-G-<C/R>.
Whe n$300i scall
ed,thec har
ac t
erpoi nterisa tthe<RETURN>c haracter.Whe n
theDOSc omma ndCATALOG,i sissued,thei nputbuLe risov e
r wri
tte
nwi t
ht he
cha r
a ct
ers^D-C-A-T-A-L-O-G-<C/R>,whe rea^Dr e prese
ntsthe<CTRL>Dc ha r
-
ac t
er.A4e rtheCATALOG,t heMoni torwi llresumei tsinterpret
ationoft hei nput
buLe ront heF4hc har
a c
ter,whichnowi nsteadoft hec arr
iager et
urn,ist hes e
c-
ondAoft hewor dCATALOG. ⇢us ,a 4ertheCATALOGc omma ndi sdonea ndc on-
trolreturnsfrom theroutineat$300, youg e
tt hesamer es
ultasifyouha dt yped
inALOG,whi c hwouldbet odisass
e mblet hec odes tart
inga tlocat
ion$0A( AL),
foll
owe dbyabe epforas yntaxerrorf orOG<C/R>.Toa voidthisproble m,r ou-
tinesthatinv olv
eDOSc omma ndss houldbec all
edonl yfrom ar unningBAS IC
1
3.I
/ORout
ine
s 1
07

program,ors houl de xitv i


aaJMP$3D0,a sme nti
onede arl
ierint hesect
ionon
theCOUTr outine .
⇢i sne xtpr i
ntr outineismor einvolv edbutdoe soLers omea dvanta g
es.
Onea dva ntag
ei sthattheHEXorASCda t
af orwha tyouwa ntt oprintcanimme -
diate
lyfollowt heJSRpr i
ntsta
tement,whic hpa ral
lel
sBAS ICal i
ttl
emor eclosely
anda voidscons t
ructi
onoft hevari
ousda tabl ocks.⇢edi sadv a
ntageisthatthe
overal
l c
odei sl ong e
rf orshortprogr
a mss ucha sthis.
⇢eg ene r
alruleofthumb
istouset heda ta-
typepr introuti
newhe ny ouha veonl ytopr i
ntonc eort wice
duringthepr og ram,a ndt ousethef oll
owi ngt ypeofr outinewhe npr inti
ng
ma nytime s.
⇢el ogi
cbe hindt heope r
ati
onoft hiss ec
ondme thodiss l
ightl
ymor ec om-
plexthant hepr eviousr outi
ne,butIthinky ou’llFnditquiteintere
sti
ng .
He r
e ’
sthene wme thod:
1 ********************************
2 * AL13-SPECIAL PRINT 2 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 PTR EQU $06
9 COUT EQU $FDED
10 *
0300: 20 0E 03 11 ENTRY JSR PRINT
0303: 84 12 E0 HEX 84
0304: C3 C1 D4 13 ASC "CATALOG"
0307: C1 CC CF C7
030B: 8D 00 14 HEX 8D00
030D: 60 15 DONE RTS
16 *
030E: 68 17 PRINT PLA
030F: 85 06 18 STA PTR
0311: 68 19 PLA
0312: 85 07 20 STA PTR+1
0314: A0 01 21 LDY #$01 ; PTR HOLDS E0-1 HERE
22 *
0316: B1 06 23 P0 LDA (PTR),Y
0318: F0 06 24 BEQ FNSH
031A: 20 ED FD 25 JSR COUT
031D: C8 26 INY
031E: D0 F6 27 BNE P0 ; (MOST ALWAYS)
28 *
0320: 18 29 FNSH CLC
0321: 98 30 TYA
0322: 65 06 31 ADC PTR
0324: 85 06 32 STA PTR
0326: A5 07 33 LDA PTR+1
0328: 69 00 34 ADC #$00
032A: 48 35 PHA
032B: A5 06 36 LDA PTR
032D: 48 37 PHA

]
1
08 As
sembl
yLi
nes

032E: 60 38 EXIT RTS


39 * WILL RTS TO DONE INSTEAD OF
40 * E0!
41 *

⇢i sonei sr atheri nt
ere st
ingi nt hatituse st hes t
acktode termi newhe reto
startrea dingt heda ta.You’ l
lr ecalltha twhe naJSRi sdone ,ther eturna ddres
s
mi nusonei sputont hestack .Upone ntr
yt ot hePRINTr outine, weus ethisfac
t
toputt ha ta ddres sinPTR,PTR+1.Byl oadingt heY- Registerwi t
h#$01 a nd
index i
ngPTRt of etcht heda ta,wec ans ca
nt hroug hthestri
ngt obepr int
edunt i
l
wee nc ount er$00, whi chindi c
a testhee ndoft hes t
ring.
Whe nt hee ndi sr eac
he d,theBEQFNSHwi llbet aken.A4e rtha thappens ,
the
Y-Re gister( thel engthoft hes tringpr i
nted)ist ransf
erredtot heAc cumul at
or
anda dde dt ot hea ddr es
si nPTR,PTR+1,a ndt her es
ultpus hedba ckont othe
stack.Re me mbe rtha ttheol dre turna ddresswa sE0-1unt ili
twa spulledoL.
Nowwhe nt heRTSi se ncount e
red,thepr og ram wil
lbef ool edintor et
urn-
ingt oDONEi nsteadoft oE0a sitot herwisewoul dha vedone .
Tos umma rize,the n:
1. AnyDOSc omma ndc anbee x
ec ute
df rom a ssemblylang uag ejustasitis
donei nBAS ICbydoi ngt hee quivalentofpr intinga<CTRL>Df oll
owe dbyt he
comma nda nda<RETURN>.
2
. Ada t
a-typepri
ntroutineusesASCIIcha
rac
tersinal a
beledblock,
whichis
t
hencall
edbyna meus ingt heX-
Re g
ist
erinadir
e c
ti ndex
eda ddres
si
ngmode .
⇢es t
ri
ngt obepri
nteds houldha
v et
hehighbi
tset(AS CI
Ivalue+$80),andthe
s
tri
ngmus tbeter
minatedbya0( atle
astwhenusi
ngt herouti
neg i
venhere)
.
3. AJSRtoaspe
cia
l pr
intr
out
inecanal
sobedone
.Int
hisc
asetheAS
CIIda
ta
shoul
dimmedi
ate
lyfoll
ow t
heJSR.Aga
in,hav
ethehig
hbitsetandendwi
th
$00.

I
nputRout
ine
s
⇢eot hersi
deofthec oi
nis,ofcourse,t
heINPUTrouti
ne.Youmightbesur-
pri
sedbythenumbe roft
imesIg etcall
sfrom peopl
esa
ying,
“Ifonl
ytheinputi
n
suc
h-and-s
uchpr ogra
m woul da cceptquotes,commas,etc
.”⇢es ol
utioni
s
act
uall
yquit
esimpleandisprese
nt e
dhe re.
Initss
imples
tform,therouti
nelook sli
kethis:
1 ********************************
2 * AL13-INPUT ROUTINE FOR BINARY*
3 ********************************
4 *
5 * STORES STRING AT PTR LOC
6 *
7 * OBJ $300
1
3.I
/ORout
ine
s 1
09

8 ORG $300
9 *
10 GETLN EQU $FD6F
11 BUFF EQU $200
12 PTR EQU $06
13 *
14 *
0300: A2 00 15 ENTRY LDX #$00
0302: 20 6F FD 16 JSR GETLN
17 *
0305: 8A 18 CLEAR TXA ; X=LEN OF STRING
0306: A8 19 TAY
0307: A9 00 20 LDA #$00
0309: 91 06 21 STA (PTR),Y ; PUT END-OF-STRING MARKER
030B: 88 22 DEY ; Y-1 FOR PROPER INDEXING
030C: B9 00 02 23 C2 LDA BUFF,Y
030F: 29 7F 24 AND #$7F ; CLEAR HIGH BIT
0311: 91 06 25 STA (PTR),Y ; PUT IN NEW LOC
0313: 88 26 DEY
0314: C0 FF 27 CPY #$FF
0316: D0 F4 28 BNE C2
29 *
0318: 60 30 DONE RTS

⇢ehe a rtoft hisr out ineisac al


ltot heMoni tor ’
sGETLNr outine, whi c
hg ets
al i
neoft e xtf rom t hek ey boar dorc urrenti nputde vicea ndput siti nt hek e y-
boa rdbuLe r( $2007$2FF) .
⇢i ss ave sourha vingt owr it
ear outineour selves.⇢ebe a utyoft hi s
me thodi sa l
sot ha ta l
lt he<ESCAPE>a ndl e4/righta rrow k eysar er ecogniz ed.
Whe nt her out iner e t
ur nsf r
om GETLN,t hee nteredl inei ss itti
nga t$200+.⇢e
leng t
hi she ldi nt heX- Re gister.
Att hispoi ntwepr esuma blyc ouldjus treturnf rom ourr outi
nea swe llbut ,
asi thappe ns , alltheda tanowi nthebuLe rha sthehi ghbi ts et–tha tis,#$80ha s
bee na dde dt ot heAS CI Iv a l
ueofe achc ha ra
cter.Be causeAppl e
so4i npa rticu-
lar,andma nyot he rr outine sing e
ne r
al,don’ texpe ctthis,thehi g
hbi tshoul dbe
clearedbe forer e tur ning .Al so$200+wi llhol donl yones tringa tat ime ,sot he re
shoul dbes omepr ov isi
onf orr elocat
ingthes t
ringt os omeFna ldestination.
Botha rea c compl i
she di nt heCLEARs e ct
ionoft hisrout ine.Fi r
stt hele ng th
oft hes tringi st rans ferredv iat heTXA,TAYt ot heY- Re gi
s t
e r.Mypr ef
e re
nc ei s
thent oma rkt hee ndoft hes tring.⇢es ubt l
epa rthe r
ei st hateve nt houg ht he
Y- Registerhol dst hel eng thv a l
ue,thisa ctuallypoi ntst ot hepos itioni mme di-
atelya 4e rt hel a stc ha racte re nter
e dintot hei nputbuLe r.Fore xampl e,ify ou
ente r
edt hewor dTEST,Xwoul dber etur neda s$04.Now t hec ha ractersTEST
occ upyby te s$2007$203.⇢uswhe nt hel eng t
h( $04)i sputi ntheY- Registe r
,
STA$200,Ywi llputa0i nt heF4hc harac t
e rpositi
on. ⇢usaDEYi st henne ede d
tog etrea dyf ort hec ontinua t
iont oC2.

]
1
10 As
sembl
yLi
nes

Ne x
t,C2b eginsal oopthatloadseachc ha rac teri ntothebuLe r
,doe sa nAND
with#$7F, andthe ns t
orestheresultatal ocationpoi ntedtobyPTR,PTR+1pl us
theY-Re gi
steroLset.
⇢eAND #$7Fha sthee Lec
tofc l
earingt hehi g hbi tbyf or
cingbit7t o0 .
⇢eY- Re gi
sterist hende cre
me nteda ndt hel oopr epeate
dunt i
lt heDEY
forc
esYt oa n$FF.⇢i swilli
ndicatetha tthel a stv a l
uewa s$00,a ndweha v e
thuscompl ete
ds canningthebuLe r.
⇢i sroutinewi llworkFnea slonga sy ou’r ewi l
lingtoma naget hes tr
ing
enti
relybyy ourselfonc ei
tg et
stot hePTR,PTR+1l oc at
ion.Asnobl ea sitmight
betowr iteprogra mse nti
relyi
na ssembl ylang ua ge ,Ius uall
yprefertowr i
tein
bothAppl eso4a nda ssemblylanguage.⇢i sisbe caus eunl es
sspeedi srequired,
Appleso4doe soLe rsomea dv
antagesint ermsofpr og r
amc lari
tya nde aseof
modiFc at
ion.A4e ra l
l,iftherewe renoa dva ntag et oAppl eso4,whywoul d
somebodyha vewr i
ttenitintheFrstplace?
So,tothatend, herearetwone wl i
stings,theFr sti nAppleso4:
5 PRINT CHR$(4);"BLOAD AL13.INPUTFP"
10 IN$ = "X"
20 PRINT "ENTER THE STRING: ";
30 CALL 768: IN$ = MID$(IN$,1)
40 IF IN$ = "END" THEN END
50 PRINT IN$: PRINT: GOTO 20

a
ndt
hes
econdi
nas
sembl
yla
ngua
ge:
1 ********************************
2 * AL13-INPUT ROUTINE FP BASIC *
3 ********************************
4 *
5 * IN$="" MUST BE FIRST VARIABLE
6 * DEFINED IN PROGRAM!
7 *
8 * OBJ $300
9 ORG $300
10 *
11 GETLN EQU $FD6F
12 VARTAB EQU $69
13 BUFF EQU $200
14 *
15 *
0300: A2 00 16 ENTRY LDX #$00
0302: 20 6F FD 17 JSR GETLN
0305: A0 02 18 LDY #$02
0307: 8A 19 TXA
0308: 91 69 20 STA (VARTAB),Y
21 * STORE ’X-REG = LEN OF IN$’
22 * IN LEN BYTE OF IN$
23 *
030A: C8 24 INY ;Y=3
030B: A9 00 25 LDA #$00
030D: 91 69 26 STA (VARTAB),Y
1
3.I
/ORout
ine
s 1
11

030F: C8 27 INY ;Y=4


0310: A9 02 28 LDA #$02
0312: 91 69 29 STA (VARTAB),Y
30 * SET LOCATION PTR OF IN$ TO
31 * $200 (INPUT BUFFER)
32 *
0314: 8A 33 XFER TXA
0315: A8 34 TAY ; Y-REG = LEN NOW
0316: B9 00 02 35 X2 LDA BUFF,Y
0319: 29 7F 36 AND #$7F
031B: 99 00 02 37 STA BUFF,Y
031E: 88 38 DEY
031F: C0 FF 39 CPY #$FF
0321: D0 F3 40 BNE X2
41 *
0323: 60 42 DONE RTS
0324: 62 43 CHK

⇢ei mportantdi Lerenc etonot i


cehereisthatIN$ha sbe ende Fnedasthe
Frstv a
riablei
nt heAppl es
o4pr ogram,andt hattheassembly-l
ang uagerouti
ne
usesthisfactt
otra nsferthestri
ngt oAppl e
so4.
⇢ewa ythisisdonebe gi
nsa tXFER.Whe na nAppl eso4s t
ringv ar
iabl
eis
stored,thename ,l ength,a ndl ocat
ionoft hestringareputi nat abl
ewhos e
be g
inningispointedt obyl ocations$69,$6A(VARTAB,VARTAB+1).
S i
nceIN$wa stheFr stv ar
iabledeFned,wek nowt hatitsnamea ndpoint
er
wi l
lstar
ta twherev e
rVARTABpoi nt
s.⇢ena mei she l
dinpos it
ions$00a nd$01,
thelengthin$02, andt heloc at
ionin$03a nd$04.

Byloadi
ngtheY- Regi
ste
rwi t
h#$02,wec ans t
orethelengt
hoft heenter
ed
st
ringintheproperplace
.⇢el ocationofIN$isthensetto$200byput ti
ngthe
appropri
atebyte
si ntoposit
ions$03 a nd$04.Now Appl eso4iste mporar
il
y
f
ooledintothi
nkingthatIN$isat$200–r i
ghtwhereourinputstr
ingisheld!
⇢er outneFni
i shesbycleari
ngt hehighbi
t,asbefore,andthenr e
turni
ng
withtheRTS.

]
1
12 As
sembl
yLi
nes

WhentheRTSisdone
,li
ne30oft
heAppl
eso4pr
ogrami
mme dia
tel
yass
igns
IN$t
oit
sel
finsuc
hawa yastof
orc
eAppl
eso4tomoveIN$f
rom wher
eitwasin
theinputbuLe rt oanewl ocat
ionupi nitsusualvari
ablestora
gea r
ea.⇢ene t
resul
tc a
nbeobt ai
nedinv ari
ousotherwa ysbesi
destheMID$s t
atement,
butthe
ways howni stheleas
tintrusi
veintermsofa Lect
ingothervaria
bles
.(Youcould
useA$=IN$:IN$=A$,butt henyou’dne e
das e
condv ari
ableinyourprogra
m–
nopr oble
m, justmorena mestok ee
ptrackof.)
Ma kesuret hei
nputr out
ineisloadedat$300b ef
orerunningtheAppleso4
program. Notethatyouc anentercomma s,quote
s,<CTRL>C’s
,etc
.Onlyenter
ing
ENDo rpress
ingRESETs houldbea bl
etoexitthi
srouti
ne.
1
4
1
4. Re
adi
ngandWr
it
ingFi
le
sonDi
sk
No
vemb
er1
981

Re
adi
nga
ndWr
iti
ngDa
taFi
les
⇢i sc hapte
rwi llchal
lengey ourde votiont ot hec auseofl e
arninga s
sem-
bly-l
anguag eprog r
ammi ng.Upunt i
lnow t hes ourcelistingshavebe env er
y
shortande asil
ytypedi nafewmi nutes’t
ime . Unfortunately,thel
isti
ngsforthi
s
chapterareabi tlongerthanus ual.Butchinup!⇢er esultwi l
lbewor t
hi t
!I’
ve
recei
vedqui teanumbe rofr e
que stsforinfor mationonhowt oreadandwr it
e
Flesont hedi s
k.⇢epr ogramsl i
stedwi l
lc ombi nema nyoft hetechniquesand
routi
nesy ou’vele
a r
ne dsofarintoas i
nglemi ni -
da t
abasepr ogram.1

⇢e Fr stprog r
a m savesa nd l oadst he da ta by me ansofa s i
mple
BSAVE/BLOADo perati
on. ⇢isisfastandv erys t
raightfor
wa rd. Here
’stheli
sti
ng:
1 ********************************
2 * AL14-NAME FILE DEMO PROGRAM *
3 ********************************
4 *
5 *
6 ORG $6000
7 *
8 HOME EQU $FC58
9 COUT EQU $FDED
10 RDKEY EQU $FD0C
11 GETLN EQU $FD75
12 BUFF EQU $200
13 VTAB EQU $FC22
14 CH EQU $24
15 CV EQU $25
16 CTR EQU $08
17 PTR EQU $06
18 REENTRY EQU $3D0
19 *
20 *
6000: A9 00 21 ENTRY LDA #$00
6002: 85 06 22 STA PTR

CT]⇢e
1[ set
wopr
ogr
amswi
llwor
konl
yinDOS
,notPr
oDOS
.Ac
cor
dingt
oBe
nea
th
AppleProDOS( Don Wor tha nd Piet
erLechner,Qua l
it
yS o4ware,1984,p.6-61)
,
<CTRL>D doesnotwor kwithProDOSc omma ndsfrom ass
emblyc ode.I
nst
ead,you
mustplac
et hecomma nds t
ringint nputbuLe
heGETLN i rat$200a ndthencal
lthe
BASICInter
pret
er(BI)at$BE03.⇢i sisl
e4asane xe
rcis
efortherea
de r
.

]
1
14 As
sembl
yLi
nes

6004: A9 10 23 LDA #$10


6006: 85 07 24 STA PTR+1
6008: A9 B1 25 LDA #$B1
600A: 85 08 26 STA CTR
27 *
600C: A0 00 28 CLR LDY #$00
600E: 91 06 29 STA (PTR),Y
6010: C8 30 INY
6011: A9 A0 31 LDA #$A0
6013: 91 06 32 STA (PTR),Y
6015: A9 00 33 LDA #$00
6017: C8 34 INY
6018: 91 06 35 STA (PTR),Y
601A: E6 07 36 INC PTR+1
601C: E6 08 37 INC CTR
601E: A5 08 38 LDA CTR
6020: C9 B6 39 CMP #$B6
6022: 90 E8 40 BCC CLR
41 *
42 * PUTS ’#1-5,SPC,00’ IN BUFFER
43 *
6024: 20 58 FC 44 MENU JSR HOME
6027: A9 02 45 P1 LDA #$02
6029: 85 25 46 STA CV ; VTAB 3
602B: 20 22 FC 47 JSR VTAB
602E: 20 C2 61 48 JSR PRINT
6031: B1 A9 A0 49 ASC "1) INPUT NAMES"
6034: C9 CE D0 D5 D4 A0 CE C1
603C: CD C5 D3
603F: 8D 00 50 HEX 8D00
51 *
6041: A9 04 52 P2 LDA #$04
6043: 85 25 53 STA CV
6045: 20 22 FC 54 JSR VTAB ; VTAB 5
6048: 20 C2 61 55 JSR PRINT
604B: B2 A9 A0 56 ASC "2) PRINT NAMES"
604E: D0 D2 C9 CE D4 A0 CE C1
6056: CD C5 D3
6059: 8D 00 57 HEX 8D00
58 *
605B: A9 06 59 P3 LDA #$06
605D: 85 25 60 STA CV
605F: 20 22 FC 61 JSR VTAB ; VTAB 7
6062: 20 C2 61 62 JSR PRINT
6065: B3 A9 A0 63 ASC "3) SAVE NAMES"
6068: D3 C1 D6 C5 A0 CE C1 CD
6070: C5 D3
6072: 8D 00 64 HEX 8D00
65 *
6074: A9 08 66 P4 LDA #$08
6076: 85 25 67 STA CV
6078: 20 22 FC 68 JSR VTAB ; VTAB 9
607B: 20 C2 61 69 JSR PRINT
607E: B4 A9 A0 70 ASC "4) LOAD NAMES"
6081: CC CF C1 C4 A0 CE C1 CD
6089: C5 D3
1
4.Re
adi
nga
ndWr
it
ingFi
le
sonDi
sk 1
15

608B: 8D 00 71 HEX 8D00


72 *
608D: A9 0A 73 P5 LDA #$0A
608F: 85 25 74 STA CV
6091: 20 22 FC 75 JSR VTAB ; VTAB 11
6094: 20 C2 61 76 JSR PRINT
6097: B5 A9 A0 77 ASC "5) END PROGRAM"
609A: C5 CE C4 A0 D0 D2 CF C7
60A2: D2 C1 CD
60A5: 8D 00 78 HEX 8D00
79 *
60A7: A9 0C 80 P6 LDA #$0C
60A9: 85 25 81 STA CV
60AB: 20 22 FC 82 JSR VTAB ; VTAB 13
60AE: 20 C2 61 83 JSR PRINT
60B1: D7 C8 C9 84 ASC "WHICH DO YOU WANT? "
60B4: C3 C8 A0 C4 CF A0 D9 CF
60BC: D5 A0 D7 C1 CE D4 BF A0
60C4: 00 85 HEX 00
86 *
60C5: 20 0C FD 87 M1 JSR RDKEY
60C8: C9 B1 88 CMP #$B1 ; '1'
60CA: D0 06 89 BNE M2
60CC: 20 FD 60 90 JSR INPUT
60CF: 4C 24 60 91 JMP MENU
60D2: C9 B2 92 M2 CMP #$B2 ; '2'
60D4: D0 09 93 BNE M3
60D6: 20 42 61 94 JSR DSPLY
60D9: 20 0C FD 95 JSR RDKEY
60DC: 4C 24 60 96 JMP MENU
60DF: C9 B3 97 M3 CMP #$B3 ; '3'
60E1: D0 06 98 BNE M4
60E3: 20 78 61 99 JSR SAVE
60E6: 4C 24 60 100 JMP MENU
60E9: C9 B4 101 M4 CMP #$B4 ; '4'
60EB: D0 06 102 BNE M5
60ED: 20 A0 61 103 JSR LOAD
60F0: 4C 24 60 104 JMP MENU
60F3: C9 B5 105 M5 CMP #$B5 ; '5'
60F5: D0 03 106 BNE M6
60F7: 4C D0 03 107 JMP REENTRY
60FA: 4C 24 60 108 M6 JMP MENU
109 *
110 *
60FD: 20 42 61 111 INPUT JSR DSPLY ; SHOW WHAT’S THERE
112 *
6100: A9 00 113 I0 LDA #$00
6102: 85 06 114 STA PTR
6104: A9 10 115 LDA #$10
6106: 85 07 116 STA PTR+1 ; SET PTR=$1000
117 *
6108: A9 00 118 LDA #$00
610A: 85 08 119 STA CTR
610C: 18 120 ILOOP CLC
610D: A5 08 121 LDA CTR
610F: 65 08 122 ADC CTR

]
1
16 As
sembl
yLi
nes

6111: 85 25 123 STA CV


6113: 20 22 FC 124 JSR VTAB
6116: A9 00 125 LDA #$00
6118: 85 24 126 STA CH
611A: A8 127 TAY
611B: 20 29 61 128 JSR IP
611E: E6 07 129 INC PTR+1
6120: E6 08 130 INC CTR
6122: A9 04 131 LDA #$04
6124: C5 08 132 CMP CTR
6126: B0 E4 133 BCS ILOOP ; GET 5 NAMES
134 *
6128: 60 135 IFIN RTS
136 *
6129: A2 00 137 IP LDX #$00
612B: 20 75 FD 138 JSR GETLN
612E: 8A 139 TXA
612F: F0 10 140 BEQ IPFIN ; EXIT IF <CR> ONLY
6131: A8 141 TAY
6132: A9 00 142 LDA #$00
6134: 99 00 02 143 STA BUFF,Y
6137: B9 00 02 144 IPLOOP LDA BUFF,Y
613A: 91 06 145 STA (PTR),Y ; MOVE DATA TO PTR
146 * ; BLOCK
613C: 88 147 DEY
613D: C0 FF 148 CPY #$FF
613F: D0 F6 149 BNE IPLOOP
6141: 60 150 IPFIN RTS
151 *
6142: 20 58 FC 152 DSPLY JSR HOME
6145: A9 00 153 LDA #$00
6147: 85 08 154 STA CTR
155 *
6149: 85 06 156 STA PTR
614B: A9 10 157 LDA #$10
614D: 85 07 158 STA PTR+1
614F: 18 159 D0 CLC
6150: A5 08 160 LDA CTR
6152: 65 08 161 ADC CTR
6154: 85 25 162 STA CV ; VTAB (2*CTR)+1
6156: 20 22 FC 163 JSR VTAB
6159: A9 00 164 LDA #$00
615B: 85 24 165 STA CH ; HTAB 1
615D: A8 166 TAY
167 *
615E: B1 06 168 D1 LDA (PTR),Y
6160: F0 06 169 BEQ D1FIN
6162: 20 ED FD 170 JSR COUT
6165: C8 171 INY
6166: D0 F6 172 BNE D1 ; (ALWAYS)
173 *
6168: A9 8D 174 D1FIN LDA #$8D
616A: 20 ED FD 175 JSR COUT ; END WITH <CR>
616D: E6 07 176 INC PTR+1
616F: E6 08 177 INC CTR
6171: A9 04 178 LDA #$04
1
4.Re
adi
nga
ndWr
it
ingFi
le
sonDi
sk 1
17

6173: C5 08 179 CMP CTR


6175: B0 D8 180 BCS D0 ; PRINT 5 NAMES
181 *
6177: 60 182 DSFIN RTS
183 *
184 *
6178: A9 8D 185 SAVE LDA #$8D
617A: 20 ED FD 186 JSR COUT ; CLEAR OUTPUT BUFFER
617D: 20 C2 61 187 OPEN JSR PRINT
6180: 84 188 HEX 84
6181: C2 D3 C1 189 ASC "BSAVE DEMOFILE,A$1000,L$500"
6184: D6 C5 A0 C4 C5 CD CF C6
618C: C9 CC C5 AC C1 A4 B1 B0
6194: B0 B0 AC CC A4 B5 B0 B0
619C: 8D 00 190 HEX 8D00
191 *
619E: 60 192 SFIN RTS
193 *
194 *
619F: A9 8D 195 LOAD LDA #$8D
61A1: 20 ED FD 196 JSR COUT
197 *
61A4: 20 C0 61 198 JSR PRINT
61A7: 84 199 HEX 84
61A8: C2 CC CF 200 ASC "BLOAD DEMOFILE,A$1000"
61AB: C1 C4 A0 C4 C5 CD CF C6
61B3: C9 CC C5 AC C1 A4 B1 B0
61BB: B0 B0
61BD: 8D 00 201 HEX 8D00
202 *
61BF: 60 203 RTS
204 *
205 *
206 *
61C0: 68 207 PRINT PLA
61C1: 85 06 208 STA PTR
61C3: 68 209 PLA
61C4: 85 07 210 STA PTR+1
61C6: A0 01 211 LDY #$01
61C8: B1 06 212 P0 LDA (PTR),Y
61CA: F0 06 213 BEQ PFIN
61CC: 20 ED FD 214 JSR COUT
61CF: C8 215 INY
61D0: D0 F6 216 BNE P0 ; (ALWAYS)
217 *
61D2: 18 218 PFIN CLC
61D3: 98 219 TYA
61D4: 65 06 220 ADC PTR
61D6: 85 06 221 STA PTR
61D8: A5 07 222 LDA PTR+1
61DA: 69 00 223 ADC #$00
61DC: 48 224 PHA
61DD: A5 06 225 LDA PTR
61DF: 48 226 PHA
61E0: 60 227 EXIT RTS
228 *

]
1
18 As
sembl
yLi
nes

229 *
61E1: 00 230 EOF BRK
231 *
232 *
61E2: 89 233 CHK

Tounde rst
andhowi twor ks,considerthesec ondit
ions:
Da t
awillbestoredint heareaf r
om $1000- $14FF.⇢i sareaiscall
edab uEe.
r
At otalofFvestri
ng swillbes tored,eachbe gi
nni ngatane xactpag eboundary
($1000,$1100,$1200, et
c.).Itisassume dt hatnos t
ri
ngwi llbelong erthan255
bytes–afairl
ysafea s
sumpt ions i
ncetheINPUTr outi
newon’ tal
lowt hiseit
her
.
Az ero-pagepointer( clever
lyl abel
edPTR)wi l
lbeus e dtoc ontrolwhich
rangeinthebuLe riscurrentlybeingac ce
ss edforapa rt
icula
rs t
ring.
⇢eba s
icroutinesusedt oma ketheov era
llideawor kareasfoll
ows :
1. AnINPUTr outineusingt heMoni tor( $FD6F=GETLN) 2
.
2
. APRINTr
out
ineus
ingaJSRa
ndas
tac
kma
nipul
at
ion.
(Nott
heDATAt
ype
.)
3
. Asingle
-keyinputr
out
inepr
ese
nti
ntheMoni
torus
edt
oge
tthec
omma
nd
k
ey(
$FD0C=RDKEY).
4. ⇢ee xecuti
onofDOSc ommandsf
rom as
sembl
yla
nguagebypr ec
edi
ng
phr
aseswit
ha<CTRL>D.
Tousetheprogra
m, c
alli
tdi
rec
tl
yfr
om BASICwi
thaCALL24576.
Ame nu
wil
lappe
arwiththe
sechoi
ces:
1) INPUT NAMES
2) PRINT NAMES
3) SAVE NAMES
4) LOAD NAMES
5) END PROGRAM

Totryther outineout,useopt i
on1t oent e
rFv esamplenames.⇢e nuse
option2t ov i
ewt heda tayou’
v eente
red.Youma yt
he nuseopt
ion3t osavethe
dataa sabinar yFleonadi skette
.⇢e nr e
runt heprogram,andveri
fythatonly
thenumbe rs1t hroug h5exis
ti nthebuLer(option2 )
.⇢e nretr
ieveyourdata
byus i
ngtheLOADc omma nd( opti
on4 ),a
ndv iewa gai
ntoconFrm as ucce
ssf
ul
load.
Inde
tail,
thisishowt heprog r
am works:
Atentry,PTRi ss ett
opoi ntto$1000,whe rethena mebuLerbe gi
ns.⇢e
Acc umulatoristhenl oade
dwi ththeASCIIvalueforthechar
acte
r1 ,
a ndtheCLR
routineentered.

2[
CT]Te
chni
cal
l
y,ourpr
ogr
ami
sus
ingNXTCHAR(
$FD75)i
nst
eadofGETLN(
$FD6A)
,to
a
voi
dpr
int
ingoutt
hepr
omptc
har
act
er.
1
4.Re
adi
nga
ndWr
it
ingFi
le
sonDi
sk 1
19

CLRp utst hec ha ra cter s1t hroug h5i nt oe achoft hes trings pa ces .Ea c hdi gi
t
isfollowe dbya<SPACE>, andt he na$00. Ius e d$00a sa ne nd- of-stringma rk er,
butt hec hoi cei ss ome wha ta rbi trary.
MENU c lea rst hes c ree na ndpr esent st heus erwi tht hea v ailablec hoi c es.
Point sofi nt eres the rea ret heVTABope r
a tiona ndt hePRINTr out ine .ToVTABt o
ag ive nl inef rom a ssembl yl a ng ua ge,oneoft hee as iestwa ysi st ol oa dCVwi t
h
thel iney ouwi sht og ot o,a ndt he nJSRt ot heMoni tor ’
sVTABr out ine( $FC22) .
Nor ma lly,wemi ghta l
s owi sht oe it
he rpr intac a rria ger eturn,ors etCHt o0 .
Not et ha tCV a ndCHa r et hec omput er’sv er ti
c alandhor i
zont alc urs orpos ition
bytes ,asus edbyt heMoni tor. Youc ana lwa yst el
l t
hec ur sorpos itionbye xa mi n-
ingt he seby tes,a ndCHma ybef or c
e dtoade siredv aluet oa cc ompl isht hes ame
asa nHTABi nBAS IC.
⇢ ePRINTr out inei st heonede scri
be di nc ha pt er1 3,a ndi sus e fulb eca use
theJSRPRINTc a nbef ollowe di mme di atelywi tht heda tat opr int .⇢i si smor e
simila rtot heBAS ICPRINTs ta teme nt,anda lsoa voi dss etti
ngupal otofs pe ciFc
datat abl est odot hepr inting .
Onc et heme nui spr int edont hes cree n,l i
ne8 7oft hes our ceFl edoe st he
JSRt oRDKEY. ⇢i sg etst hec omma ndk eyf rom t heus e r,whi chi st he nt estedby
theM1t oM6s er iesofc he ck s.
A4e rc allingRDKEYt hek e yboa rdv aluewa sr e tur ne di nt heAc cumul a t
or ,
andwec a ndi re ctlyt estt os eewhi chk eywa spr essed. ⇢ek eyi st henc ompa red
withe achoft heFv ede sire dr es pons es. I
fnoma tchi sf ound, thepr og ra mj umps
backt oMENU t or e pea tt hedi spl aya ndc omma ndi nput .Ot he rt ha nRESET,
option5i st heonl ywa yt oe xitt hepr og r
a m.
Le t
’se xami net heme nuopt ions :
Ify oue nte r1 , cont rol isdi re ctedt othes e ct
ionl abe ledINPUT. ⇢eFr stthi ng
donet he r ei st oJSR t oDSPLY.Att hispoi nt ,it’sne ce ssaryonl yt ounde rsta nd
thatDSPLYj ustc l
e arst hes cre ena nds howst heFv es tr ing sc urr entlyi nme mor y.
A4erDSPLY,PTR i si ni ti
a lizedt opoi ntt ot hebe ginni ngoft hebuLe r
($1000) ,a ndt hec ount e riss e tt o0 .⇢ema inINPUTl oopc ome sne x t.He reCTR
isus edt oc alcul atewha tl ine( v ert i
ca lposition)t oputt hec urs oron. ( DSPLYu sed
thes a mea lgor ithm t odi splayt hec ur r
e ntda t
a .
)A4e rVTAB,t hee qui va l
e ntof
HTABi sdone ,f ollowe dbyt hej umpt ot hea ctuali nputr out ine ,he rel a be l
e dIP.
⇢i si sther out inef r om t hepr ev iousc ha pte rtha tg etsal inea ndt he nmov esi tto
aloc ationi ndi ca tedbyPTR.
⇢e r ea reaf e ws ubt lei temsi ntheIPr out inet ha ts houl dbenot ed. ⇢eFr st
isline1 4 0.I f<RETURN>a lonei se ntered( i.e.none wda ta),ther out inei mme di-
atel
yr etur nswi thoutr ewr itingt heol ds tr i
ng .⇢i si st oa l
lowe ditingofas ing l
e
entrybys k i
ppi ngt hee ntr i
e snotofi ntere st.Tr yi ttos eehowi twor ks .
⇢es econdi temi st hec ha ra ct
e r
isticoft hispa r ticulari nputr out inet oput
thet railingz eroa tt hee ndoft hel i
ne .⇢i sisdoneonl i
ne s1 4171 43 .

]
1
20 As
sembl
yLi
nes

Whe ni tr eturnsf rom IP, thec ounteri sinc r


eme nteda ndc heck
edt os eeifit
exceeds#$04.I fnot ,ILOOPr e
peatsunt ilFv es tri
ng sha v ebe eninput .A4e rt he
F4hs tr
ingi se ntered, thepr ogramr eturnst ot heme nu.
Ifoption2i se ntered,t heDSPLYr outinei sc al
led.⇢es olepurpos eoft his
sect
ionist oc leart hes creena ndpr intt heFv ena me sinme mory.Ate ntryt o
DSPLY,aJSR$FC58d oesaHOMEa ndt heCTRi siniti
a l
izedt o0 .Asint heINPUT
sect
ion,CTRi sthe nus edt oc al
cula
tet heVTABpos iti
ont opr inte achline.
D1 i
st hepa rtt hata ctuall
ypr intse ac hlinebys canning( andout putt
ing
throughCOUT)a l
loft heby te
sa te achr ang ei ndica t
edbyPTR.Not et hata sa
saf
etychec k, ifa0di dnotha ppent obepr esentduet os omeot hererror ,
e ve
nt u-
all
ytheY- Re g is
terwi l
lpa s
s#$FFa ndt hepr og ram wi llfallt
hroug htoDIFIN.
DIFINp rov i
de sa ne ndingc ar
r ia
g ereturnt ot hes tringa ndt heni ncreme nts
CTRu nti
la l
l Fv estringsha vebe enpr inted.
⇢el oad/ saveope r
a t
ionsa requi t
es impl e.Knowi ngwhe rethebuLe ri s
loc
a t
ed,thee ntirebl ockisa ccess
edbydoi nge it
he raBLOADorBSAVE. Re me mbe r
thatdiskc omma ndsa redonef rom a ssembl yl angua gej usta stheywoul dbe
donef r
om BAS IC. ⇢epr og ra
m ne edonl yout puta<CTRL>Df oll
owe dbyal eg a
l
DOSc omma nda nda<RETURN>.Ag ai
nt hePRINTr outinei sus edt ofa c
il
itate
thi
s.
Ifoption5i se ntered,t hentheJMPt ot heDOSBAS ICe nt r
yv ectorise xe-
cutedtoendt hepr og r
a m.

Re
adi
nga
ndWr
iti
ngTe
xtFi
les
⇢issecondlis
ti
ngisba
sic
all
yamodiFc
ati
onoftheFrs
tprogr
am.Ify
ou
wis
h,rathertha
nr et
ypet
heenti
reFl
e,y
oucanjus
tedi
ttheFrs
tli
st
ingt
oadd
l
ines20
72 9and1 9
47228.
1 ********************************
2 * AL14-NAME FILE DEMO PROGRAM 2*
3 ********************************
4 *
5 *
6 * OBJ $6000
7 ORG $6000
8 *
9 HOME EQU $FC58
10 COUT EQU $FDED
11 RDKEY EQU $FD0C
12 GETLN EQU $FD75
13 BUFF EQU $200
14 VTAB EQU $FC22
15 CH EQU $24
16 CV EQU $25
17 CTR EQU $08
18 PTR EQU $06
19 *
20 PROMPT EQU $33
1
4.Re
adi
nga
ndWr
it
ingFi
le
sonDi
sk 1
21

21 CURLIN EQU $75


22 LANG EQU $AAB6
23 REENTRY EQU $3D0
24 *
6000: A9 40 25 ENTRY LDA #$40
6002: 8D B6 AA 26 STA LANG ; LANG = FP
6005: 85 76 27 STA CURLIN+1 ; RUNNING PROG
6007: A9 06 28 LDA #$06
6009: 85 33 29 STA PROMPT ; NOT DIRECT MODE
600B: A9 00 30 LDA #$00
600D: 85 06 31 STA PTR
600F: A9 10 32 LDA #$10
6011: 85 07 33 STA PTR+1
6013: A9 B1 34 LDA #$B1
6015: 85 08 35 STA CTR
36 *
6017: A0 00 37 CLR LDY #$00
6019: 91 06 38 STA (PTR),Y
601B: C8 39 INY
601C: A9 A0 40 LDA #$A0
601E: 91 06 41 STA (PTR),Y
6020: A9 00 42 LDA #$00
6022: C8 43 INY
6023: 91 06 44 STA (PTR),Y
6025: E6 07 45 INC PTR+1
6027: E6 08 46 INC CTR
6029: A5 08 47 LDA CTR
602B: C9 B6 48 CMP #$B6
602D: 90 E8 49 BCC CLR
50 *
51 * PUTS ’#1-5,SPC,00’ IN BUFFER
52 *
602F: 20 58 FC 53 MENU JSR HOME
6032: A9 02 54 P1 LDA #$02
6034: 85 25 55 STA CV ; VTAB 3
6036: 20 22 FC 56 JSR VTAB
6039: 20 0A 62 57 JSR PRINT
603C: B1 A9 A0 58 ASC "1) INPUT NAMES"
603F: C9 CE D0 D5 D4 A0 CE C1
6047: CD C5 D3
604A: 8D 00 59 HEX 8D00
60 *
604C: A9 04 61 P2 LDA #$04
604E: 85 25 62 STA CV
6050: 20 22 FC 63 JSR VTAB ; VTAB 5
6053: 20 0A 62 64 JSR PRINT
6056: B2 A9 A0 65 ASC "2) PRINT NAMES"
6059: D0 D2 C9 CE D4 A0 CE C1
6061: CD C5 D3
6064: 8D 00 66 HEX 8D00
67 *
6066: A9 06 68 P3 LDA #$06
6068: 85 25 69 STA CV
606A: 20 22 FC 70 JSR VTAB ; VTAB 7
606D: 20 0A 62 71 JSR PRINT
6070: B3 A9 A0 72 ASC "3) SAVE NAMES"

]
1
22 As
sembl
yLi
nes

6073: D3 C1 D6 C5 A0 CE C1 CD
607B: C5 D3
607D: 8D 00 73 HEX 8D00
74 *
607F: A9 08 75 P4 LDA #$08
6081: 85 25 76 STA CV
6083: 20 22 FC 77 JSR VTAB ; VTAB 9
6086: 20 0A 62 78 JSR PRINT
6089: B4 A9 A0 79 ASC "4) LOAD NAMES"
608C: CC CF C1 C4 A0 CE C1 CD
6094: C5 D3
6096: 8D 00 80 HEX 8D00
81 *
6098: A9 0A 82 P5 LDA #$0A
609A: 85 25 83 STA CV
609C: 20 22 FC 84 JSR VTAB ; VTAB 11
609F: 20 0A 62 85 JSR PRINT
60A2: B5 A9 A0 86 ASC "5) END PROGRAM"
60A5: C5 CE C4 A0 D0 D2 CF C7
60AD: D2 C1 CD
60B0: 8D 00 87 HEX 8D00
88 *
60B2: A9 0C 89 P6 LDA #$0C
60B4: 85 25 90 STA CV
60B6: 20 22 FC 91 JSR VTAB ; VTAB 13
60B9: 20 0A 62 92 JSR PRINT
60BC: D7 C8 C9 93 ASC "WHICH DO YOU WANT? "
60BF: C3 C8 A0 C4 CF A0 D9 CF
60C7: D5 A0 D7 C1 CE D4 BF A0
60CF: 00 94 HEX 00
95 *
60D0: 20 0C FD 96 M1 JSR RDKEY
60D3: C9 B1 97 CMP #$B1 ; '1'
60D5: D0 06 98 BNE M2
60D7: 20 08 61 99 JSR INPUT
60DA: 4C 2F 60 100 JMP MENU
60DD: C9 B2 101 M2 CMP #$B2 ; '2'
60DF: D0 09 102 BNE M3
60E1: 20 4D 61 103 JSR DSPLY
60E4: 20 0C FD 104 JSR RDKEY
60E7: 4C 2F 60 105 JMP MENU
60EA: C9 B3 106 M3 CMP #$B3 ; '3'
60EC: D0 06 107 BNE M4
60EE: 20 83 61 108 JSR SAVE
60F1: 4C 2F 60 109 JMP MENU
60F4: C9 B4 110 M4 CMP #$B4 ; '4'
60F6: D0 06 111 BNE M5
60F8: 20 C7 61 112 JSR LOAD
60FB: 4C 2F 60 113 JMP MENU
60FE: C9 B5 114 M5 CMP #$B5 ; '5'
6100: D0 03 115 BNE M6
6102: 4C D0 03 116 JMP REENTRY
6105: 4C 2F 60 117 M6 JMP MENU
118 *
119 *
6108: 20 4D 61 120 INPUT JSR DSPLY ; SHOW WHAT’S THERE
1
4.Re
adi
nga
ndWr
it
ingFi
le
sonDi
sk 1
23

121 *
610B: A9 00 122 I0 LDA #$00
610D: 85 06 123 STA PTR
610F: A9 10 124 LDA #$10
6111: 85 07 125 STA PTR+1 ; SET PTR=$1000
126 *
6113: A9 00 127 LDA #$00
6115: 85 08 128 STA CTR
6117: 18 129 ILOOP CLC
6118: A5 08 130 LDA CTR
611A: 65 08 131 ADC CTR
611C: 85 25 132 STA CV
611E: 20 22 FC 133 JSR VTAB
6121: A9 00 134 LDA #$00
6123: 85 24 135 STA CH
6125: A8 136 TAY
6126: 20 34 61 137 JSR IP
6129: E6 07 138 INC PTR+1
612B: E6 08 139 INC CTR
612D: A9 04 140 LDA #$04
612F: C5 08 141 CMP CTR
6131: B0 E4 142 BCS ILOOP ; GET 5 NAMES
143 *
6133: 60 144 IFIN RTS
145 *
6134: A2 00 146 IP LDX #$00
6136: 20 75 FD 147 JSR GETLN
6139: 8A 148 TXA
613A: F0 10 149 BEQ IPFIN ; EXIT IF <CR> ONLY
613C: A8 150 TAY
613D: A9 00 151 LDA #$00
613F: 99 00 02 152 STA BUFF,Y
6142: B9 00 02 153 IPLOOP LDA BUFF,Y
6145: 91 06 154 STA (PTR),Y ; MOVE DATA TO PTR
155 * ; BLOCK
6147: 88 156 DEY
6148: C0 FF 157 CPY #$FF
614A: D0 F6 158 BNE IPLOOP
614C: 60 159 IPFIN RTS
160 *
614D: 20 58 FC 161 DSPLY JSR HOME
6150: A9 00 162 LDA #$00
6152: 85 08 163 STA CTR
164 *
6154: 85 06 165 STA PTR
6156: A9 10 166 LDA #$10
6158: 85 07 167 STA PTR+1
615A: 18 168 D0 CLC
615B: A5 08 169 LDA CTR
615D: 65 08 170 ADC CTR
615F: 85 25 171 STA CV ; VTAB (2*CTR)+1
6161: 20 22 FC 172 JSR VTAB
6164: A9 00 173 LDA #$00
6166: 85 24 174 STA CH ; HTAB 1
6168: A8 175 TAY
176 *

]
1
24 As
sembl
yLi
nes

6169: B1 06 177 D1 LDA (PTR),Y


616B: F0 06 178 BEQ D1FIN
616D: 20 ED FD 179 JSR COUT
6170: C8 180 INY
6171: D0 F6 181 BNE D1 ; (ALWAYS)
182 *
6173: A9 8D 183 D1FIN LDA #$8D
6175: 20 ED FD 184 JSR COUT ; END WITH <CR>
6178: E6 07 185 INC PTR+1
617A: E6 08 186 INC CTR
617C: A9 04 187 LDA #$04
617E: C5 08 188 CMP CTR
6180: B0 D8 189 BCS D0 ; PRINT 5 NAMES
190 *
6182: 60 191 DSFIN RTS
192 *
193 *
6183: A9 8D 194 SAVE LDA #$8D
6185: 20 ED FD 195 JSR COUT ; CLEAR OUTPUT BUFFER
6188: 20 0A 62 196 OPENW JSR PRINT
618B: 84 197 HEX 84 ; <CTRL>D
618C: CF D0 C5 198 ASC "OPEN DEMOTEXTFILE"
618F: CE A0 C4 C5 CD CF D4 C5
6197: D8 D4 C6 C9 CC C5
619D: 8D 84 199 HEX 8D84
619F: D7 D2 C9 200 WRITE ASC "WRITE DEMOTEXTFILE"
61A2: D4 C5 A0 C4 C5 CD CF D4
61AA: C5 D8 D4 C6 C9 CC C5
61B1: 8D 00 201 HEX 8D00
202 *
61B3: 20 4D 61 203 SVLOOP JSR DSPLY ; PRINT NAMES TO DISK
204 *
61B6: 20 02 62 205 CLOSEW JSR PRINT
61B9: 8D 84 206 HEX 8D84
61BB: C3 CC CF 207 ASC "CLOSE"
61BE: D3 C5
61C0: 8D 00 208 HEX 8D00
61C2: 60 209 SVFIN RTS
210 *
211 *
61C3: A9 8D 212 LOAD LDA #$8D
61C5: 20 ED FD 213 JSR COUT
214 *
61C8: 20 02 62 215 OPENR JSR PRINT
61CB: 84 216 HEX 84
61CC: CF D0 C5 217 ASC "OPEN DEMOTEXTFILE"
61CF: CE A0 C4 C5 CD CF D4 C5
61D7: D8 D4 C6 C9 CC C5
61DD: 8D 84 218 HEX 8D84
61DF: D2 C5 C1 219 READ ASC "READ DEMOTEXTFILE"
61E2: C4 A0 C4 C5 CD CF D4 C5
61EA: D8 D4 C6 C9 CC C5
61F0: 8D 00 220 HEX 8D00
221 *
61F2: 20 0B 61 222 RDLOOP JSR I0 ; GET NAMES FROM DISK
223 *
1
4.Re
adi
nga
ndWr
it
ingFi
le
sonDi
sk 1
25

61F5: 20 02 62 224 CLOSER JSR PRINT


61F8: 8D 84 225 HEX 8D84
61FA: C3 CC CF 226 ASC "CLOSE"
61FD: D3 C5
61FF: 8D 00 227 HEX 8D00
6201: 60 228 RDFIN RTS
229 *
230 *
231 *
6202: 68 232 PRINT PLA
6203: 85 06 233 STA PTR
6205: 68 234 PLA
6206: 85 07 235 STA PTR+1
6208: A0 01 236 LDY #$01
620A: B1 06 237 P0 LDA (PTR),Y
620C: F0 06 238 BEQ PFIN
620E: 20 ED FD 239 JSR COUT
6211: C8 240 INY
6212: D0 F6 241 BNE P0 ; (ALWAYS)
242 *
6214: 18 243 PFIN CLC
6215: 98 244 TYA
6216: 65 06 245 ADC PTR
6218: 85 06 246 STA PTR
621A: A5 07 247 LDA PTR+1
621C: 69 00 248 ADC #$00
621E: 48 249 PHA
621F: A5 06 250 LDA PTR
6221: 48 251 PHA
6222: 60 252 EXIT RTS
253 *
254 *
6223: 00 255 EOF BRK
256 *
257 *
6224: A1 258 CHK

⇢et
heor
ytot
hiss
econdpr
ogr
ami
sfa
irl
ysi
mpl
e.I
fyout
hinka
bouti
t,t
he
INPUTa
ndDSPLYs
ect
ionsa
ree
sse
nti
al
lye
qui
val
entt
oaFOR I=1 TO 5/NEXT I
typel oopt hatrespectivelyi
nputsa ndpr i
ntsFv estri
ng s.InaBAS ICpr ogram, a
ll
thatwoul dber equire dtoaccessat extFlewoul dbet opr e
cedethee x
e c
utionof
thos eroutineswi tht heOPEN,READa ndtheOPEN, WRITEc omma nds.(I
’ma ssum-
ingy ou’
ref a
mi l
ia rwi ththenor ma lacces
sofAppl eDOSt extFles
.Ifnot,r ea
d
yourma nua l
!)
Ifyoue x a
mi net henews avea ndl oadr outi
ne sy ou’l
lnoticetwoc hanges.
First,ratherthanpr inti
ngBSAVEorBLOAD,t heFle sa reOPENeda ndtheREADor
WRITEc omma ndout put.Noti
cet hate a
chc omma ndbe gi
nswi t
ha<CTRL>Da nd
endswi t
hac arr
iag er et
urn.Second, a4erthec omma ndi spri
nted,aJSRi sdone
tot heIPorDSPLYr outineasisappr opria
te.La s
tofa ll,aCLOSEisoutputbe fore
returningt otheme nu.

]
1
26 As
sembl
yLi
nes

Ac cor dingt owha twek nows of ar,t


heseshoul dbet heonl yc ha ng esne ces-
saryt oa c cesste xtFles. ⇢e rei sonel astcat
chthoug h.
Appl eDOSc ompl icat
e sthing sbynota ll
owi ngt heus ert oOPENt extFles
from t hei mme diatemode .Whe nama c
hinelangua gepr ogr ami srunni ng ,DOS
think swe ’res til
lint hei mme diatemodea ndwon’ tletusa ccesst het e xtFles.
Wha t’
sne e dedi sawa yt ofool DOSi ntothinkingwe ’rer unni ngapr og ram.
⇢i si sdonebyus ingt hr eeinternalma nag eme ntl ocationsi nt heAppl e.
LANG( $AAB6)i swha tDOSus estok eeptrackofwhi chl ang ua geisc urr ent l
yrun-
ning .CURLIN( $75,$76)i sAppl eso4’sregis
terfort heby t
esoft hepr ogr aml i
ne
numbe rc ur r
e ntlybe inge xe cuted.Int heimme diatemode ,t hehi gh- or derby te
($76)d efaul t
st o#$FF. Appl eso4c antelli
fapr ogrami sr unni ngbyl ook ingfora
non- #$FFv aluei nthi sloc a
tion. ⇢eot herwa yitk nowsapr og r
a mi sr unni ngi s
toc hec kl oc ati
on$33, whi chhol dst heAS CIIv a
luef ort hepr omptc ha racter
. In
theimme diatemodeofAppl e so4, t
hisis#$DD, e
qui va l
e nttot he‘ ]’ cha racter
. In
ar unni ngpr og ram, thisc hang esto#$06.
Tof oolDOS ,allwene e dt odoi sloadthe s
et hre eloc ationsa ppr opr iatel
ya t
thebe ginni ngoft her outine.Fi nally,whene xi
tingt hepr og r
a m,r athe rtha n
usingas impl eRTS,t heJMP$3D0i se x
e c
utedtodoas o4r eentryt oBAS IC.⇢i s
wi l
lre storet heby t
e swe ’
vea lteredtof oolDOSa nda l
s or eturnust ot hec urr
e nt
langua ge .3

Tr yt he sepr og ramsout .You’ llFndt heyma k ea ne x cell


ents umma ryof
ma nyoft hei de asandr outine sdiscussedsofar,andt he ya l
s opr ovideav aluable
mode lfory ourownpr ograms .

3S
omepe
opl
eha
vea
lsoi
nqui
reda
stowhe
the
rthec
hec
kfo
rawr
it
e-pr
ote
ctl
abe
lca
nbe
defe
atedbymodi f
yingDOS.⇢ea nsweri
sy e
sandno.Yes,t
hepa r
tofthecodethat
genera
testhee
rrorcanbeel
iminate
d,butbeca
uset
hewrit
e-pr
otec
tswit
chisphys
ical
ly
wiredintotherecordi
nghead’
swr i
tesyst
em,youcannotdefe
atitwit
houtactual
l
y
re
mov ingoral
ter
ingtheswi
tc
hi t
sel
f.
1
5
1
5. S
pec
ial
Pro
grammi
ngTe
chni
que
s
De
cemb
er1
981

Ithasl ongbe e
nmyf eel
ingt hatitisnote noug hj usttok nowa na r
bitr
ary
sel
e c
tionofopt ionsorc omma ndswhe nus i
nga nyt ool,program,orpr ogra
m-
mi nglang uage
.Equa l
lyimpor t
a nta r
ethet ec
hni queswi thwhi chtheoptionsare
combi nedt oachiev
ethede s
iredr e s
ults
.
Wi tht i
mea ndpr ac
tic
ey ouwi l
lde v
e l
opy ourowns kil
lsatcreat
ingeWc i
ent
assembly-languageroutines
,butt hatproc es
sc anbea s
sist
edbye xaminingthe
techniquesthatothershavede velopedinpr evi
ouspr ogrammi nge Lorts
.
Iha vetrie
di nthisbookt opr ovidear eas
ona bl
emi xofprog r
ammi ngtech-
niques,alongwi t
htheus ualr
a t
ionofne wc omma nds .

Re
loc
ata
blev
ers
usNon-
rel
oca
tabl
eCode
Inchapte
r13Ipr
ese
ntedt
wopr intsubrouti
nesfortheoutputoft
exttot
he
s
cree
nordi skte
xtFl
e.⇢edisadv
ant ag
eofbot hr outi
nesisthatthe
ya r
enot
r
elo
cata
b l
e.Tose
ewhatt
hisme
ans,considerthefol
lowingprog
r a
m:
1 ********************************
2 *AL15-NON-RELOCATABLE PRINT DEMO
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 COUT EQU $FDED
9 *
10 *
0300: 20 0D 03 11 ENTRY JSR PRNT
12 *
0303: 4C 0C 03 13 DONE JMP EXIT
14 *
0306: D4 C5 D3 15 DATA ASC "TEST"
0309: D4
030A: 8D 00 16 HEX 8D00
17 *
030C: 60 18 EXIT RTS
19 *
030D: A2 00 20 PRNT LDX #$00
030F: BD 06 03 21 LOOP LDA DATA,X
0312: F0 EF 22 BEQ DONE
0314: 20 ED FD 23 JSR COUT
0317: E8 24 INX

]
1
28 As
sembl
yLi
nes

0318: D0 F5 25 BNE LOOP


031A: 60 26 FIN RTS

⇢i spr og r am,a swr it


ten,c anr unonl ya tt hel ocations pe c iFedbyt heORG
stat
e me nt ,int hisc ase$300. ⇢usi tisc all
e dno n- relocatab ec
l ode . Ma chinec ode
be c
ome snon- r eloc a
tablet hr oug ht heus eofa nys tate
me ntswhi chi nvol vea bso-
lutea ddr e ssing .⇢emos tc ommone xampl e sa ret heJMP a ndJSRc omma nds ,
andt heus eofda tastateme nt s,usua l
lyi npr intr out ines.
⇢eFr sts ta teme ntoft hist ypeoc c ursonl ine1 1.⇢eJSRt oPRNT( $30D) will
wor konl ys ol onga sPRNTi sa t$30D. I fther out inewe ret obel oa dedint ome m-
orya t$400 ( inste a
d of$300) ,ther out inewoul dt a
k et heJSR t oabl oc kof
none xiste ntc odea t$30D.
Li kewi se,t heJMPonl ine1 3ha sthes amedi Wc ult
y, asdoe st heDATA,Xs tate-
me ntonl ine2 1 .Anya tte mptt or unt hec odea ta na ddr essot hert han$300wi ll
resultindi sa ster.
Its houl dbenot ed,howe ver,tha tnota llJSRsa ndJMPsa reuni versa l
lyt rou-
blesome .⇢eJSRCOUT( $FDED)wi llex ecutepr ope rlynoma tterwhe ret heobj ect
codei sl oc ateds inc ether eferencei st oal oca ti
onout s i
deoft heobj ectc ode
block.
⇢eg e ne ra lrulet he ni st ha ta nyc odewhi chma k esr efere ncet oa bs olute
addr esseswi thi ni tselfwi llnotber eloc atable,whe rea
sc odet ha tdoe snots uLe r
from thi sl i
mi tationc a nber una ny whe rei nme mor y .
⇢epr obl em ofr eloc atabili
tyma ys eems lights inc ea nyg ivenr outinei s
usua l
lyde sig ne dt obeputa tade Fnitel oc at
ion( us ual
lye i
thera t$300ora tt he
topofme mor y)a ndt he npr otectedv iat heAppl eso4HIMEM:s ta t
eme nt .How-
ever,a st henumbe rofr out inesy ouus ei nc r
e ases,y ouwi llenc ount ermor ea nd
mor ec onSi c t
sbe t
we enr out inesor igina ll
ywr ittent ooc c upyt hes ameme mor y
rang es.Ina ddi tion,ita lsoi soc casiona ll
yde sirabl etodi re ct
lya ppendma c hine
codet ot hee ndofAppl eso4pr og rams ,whe ret he ywi llSoa tupa nddowni n
me mor ya tt hee ndoft heBAS I
C por t
ionoft hel is
ting ,be inga utoma ti
c all
y
mov e da sl ine sa rea dde dorde leted.
Fort he s er ea sons,iti sbe t
teri nthel ongr unt owr it
ec odet or una ny whe r
e
inme mor ywhe npos sible,t husa voidi ngf utur ehe adache sa boutwhe retoput
everything .
⇢er ema inde roft hisc hapterwi lldis c
us st hev ar
iouswa ysofa voidingt he
use ofa bs olut ea ddr essing ,thusc r
ea ti
ngc odet hatc anbeus eda nywhe rei n
me mor yr eg ardl essoft heORGs t
ateme ntus eda ta ssembl ytime .

J
MPComma
nds
⇢isisa
nexampl
eofacommonuseoftheJMPcommandt
ojumpov era
r
ang
eofmemor
y,her
ere
pre
sent
edbyt
heFllse
cti
on.Att
hedes
ti
nat
ion,EXPT,
1
5.S
pec
ial
Prog
rammi
ngTe
chni
que
s 1
29

t
heBELLrout
inei
sca
ll
eda
sat
ri
vi
ale
xampl
eofwhe
reas
ubr
out
inemi
ghtbe
e
xec
uted.
1 ********************************
2 * AL15-NON-RELOCATABLE JMP DEMO*
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 BELL EQU $FF3A
9 *
10 *
0300: 4C 04 03 11 ENTRY JMP EXPT
12 *
0303: EA 13 FILL NOP
14 *
0304: 20 3A FF 15 EXPT JSR BELL
16 *
0307: 60 17 DONE RTS

Anal
te
rnat
ivet
othi
sist
heus
eofaf
orc
edbr
anc
hst
ate
ment
,ass
howni
n
t
hise
xampl
e:
1 ********************************
2 * AL15-RELOCATABLE JMP 1 *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 BELL EQU $FF3A
9 *
10 *
0300: 18 11 ENTRY CLC
0301: 90 01 12 BCC EXPT
13 *
0303: EA 14 FILL NOP
15 *
0304: 20 3A FF 16 EXPT JSR BELL
17 *
0307: 60 18 DONE RTS

Noti
cethatbyc l
eari
ngthec a
rrya ndthenimme di
atelyexecuti
ngtheBCC,
t
hes amer e
sultisobta
inedaswhe ntheJMPc omma ndwa sus edinthee a
rli
er
l
ist
ing.
⇢ema inc a
utiontoobser
veisthatthefor
c e
dbranchc annotbema deover
adist
anceofgreate
rthan127by t
es,al
thoughmos tas
semblerswillgi
veanerror
atasse
mblytimei fthi
sisatt
empt e
d.I naddit
ion,s
incethec arr
yi scl
ear
edt o
f
orcethebranch,routi
nesthatsetorc l
earthecarr
ytoi ndicat
ec e
rtai
ncondi-
t
ionsma yhavecompa t
ibi
li
typrobl
emswi ththi
sapproac
h.

]
1
30 As
sembl
yLi
nes

Bot hl imitationsc a nbes olvedbys lightmodi Fc ationst ot hisl ist


ing .⇢e
Fr stisbyus ingt heov erSowSa g,o4enr epresent edbyaV.Yous houl dr eme m-
be rt hatt heS ta t
usRe giste
roft he6 502c ont ainsce rtainSa gst hata rec ondi -
tione d byv ar
iousope rati
ons .⇢e seSa gsc an bec heck eda nd a ppropr ia t
e
respons e
sc anbema dede pe ndingont heirstatus .
Ex ampl esofSa gsalreadyc ov-
ereda rethec arr y(C)a ndz e ro( Z)Sa gs
.
⇢eov e
rSowSa gi sanot he rbitintheS t
a t
usRe gisterwhi chi ssete i
the rby
theBITc omma nd( theov erSowSa gissettobi t6oft heme mor ylocation), orby
anADCc omma nd. ⇢eov er
Sowwi llbesetwhe nevert herei sac a
rryf rom bi t6t o
bit7a sar e s
ultofa nADCope ration.
⇢e sede tailsareme ntione donl yinpa ssinga tthi spoi nt, andy oune ednot
bec oncer nedifi tisnote ntirel
yc lear.⇢ema inr easonf orbr ingingitupi st hat
theov erSowSa gisus e
dmuc hmor einfreque ntlythant hec arr y
, andt husi ti sa
slight l
ymor ede sirableSa gtous ewhe nc reati
ngaf orcedbr anc h.
Toma k
ej umpsov erdi st
a ncesg rea
tert han1 27by tes,as teppingt echni que
ca nbeus e d.⇢i sisdonebyc re ati
ngas eriesoft hebr anc hc omma ndst hr oug h-
outt hecodet of acil
itatethepr ogram Sowf rom onepa rttoa nothe r.
I ti
sg ene r
-
allynott oo di Wc ul ttoFnd br eaksbe twee nr outine st oi nsertt hebr anc h
stateme nt srequi redf orthes teppinga cti
on. Bot htechni que sa reill
us t
ra t
edhe re:
1 ********************************
2 * AL15-RELOCATABLE JMP 2 *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 BELL EQU $FF3A
9 *
10 *
0300: B8 11 ENTRY CLV
0301: 50 01 12 BVC STEP
13 *
0303: EA 14 FILL1 NOP
15 *
0304: 50 01 16 STEP BVC EXPT
17 *
0306: EA 18 FILL2 NOP
19 *
0307: 20 3A FF 20 EXPT JSR BELL
21 *
030A: 60 22 DONE RTS

Al
thoug
honlyonest
episs
hownhe r
e,anynumbe
rma
ybeus
ed,
depe
ndi
ng
onwha
tisneede
dtospa
ntherequi
reddi
st
ance.
1
5.S
pec
ial
Prog
rammi
ngTe
chni
que
s 1
31

De
ter
mini
ngCodeLoc
ati
on
S
olvi
ngtheJMPpr oble
mi sonlythebeginningoft hetas
k.Veryo4enitis
i
mport
anttoknowj us
twhe reinme morythec odeisc ur
rent
lybei
ngrun.One
e
xampleoft
hisisthecodepr e
sentonthediskc ontrol
lercar
ds.S
incethecar
d
c
anbeputinoneofs e
v enslot
s,andsi
nceeachs lotoccupi
esauniqueme mor
y
r
ange
,somete
chniqueisrequir
edtoanswertheque st
ion,“Wherear
ewe? ”
1 ********************************
2 * AL15-LOCATOR 1 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 PTR EQU $06
9 RTRN EQU $FF58
10 STCK EQU $100
11 *
0300: 20 58 FF 12 ENTRY JSR RTRN
0303: BA 13 TSX
0304: BD 00 01 14 LDA STCK,X
0307: 85 07 15 STA PTR+1
0309: CA 16 DEX
030A: BD 00 01 17 LDA STCK,X
030D: 85 06 18 STA PTR
030F: 60 19 DONE RTS

⇢es uc ces soft hisrout i


nei sba sede nti
relyonbot hthepr e
dicta
blena ture
ofthesta cka ndi tsfunc t
ionwhe naJSRi sexecuted.
⇢es tackwa sbr i
eSyde scri
bedi nc hapterni ne.Att hispoi ntalit
tl
eg r
eater
detai
lisne c
e ssary.⇢es t
ac kisar e s
ervedpa rtofme mor yfrom $100t o$1FF. I
t
isuseda sat empor a ryhol dingbuLe rforv ari
ousk i
ndsofi nforma t
ionre quired
bythe6 502mi c r
opr ocessor.Informa t
ionputont hes tackisa l
wa ysret
rievedi n
theoppos iteor de rfrom whi c hitwa sde posit
ed. ⇢i si so4e ncalledLIFO( “
La st
-
InFirst-Out ”). ⇢ea nalogyofas tackofpl ateswa sus e dearli
er,butthetimeha s
comet oe x a
mi newha tactua l
lyocc urs.
Whe nev eraJSRi sdone ,thes tackisus edt ohol dt hea ddresstowhi cht he
ret
urns houl dbema dewhe nt hee xpectedRTSi se nc ountered.⇢edi agramson
thenex tpa gei l
lustratethis.Loc at
ion$FF58i sas impl eRTSi nt heMoni torROM
whichwi l
lbeus edt os e
tupadummyr eturna ddr e
s s.BeforetheJSR,t heS tack
Point
e riss ettos omea rbitrarypos it
ionint hestac k.Upone xecutingtheJSR, the
ret
urna ddre ssof$302i sputont hes tackandt heS ta ckPoi nterisdecreme nted
twoby tes.Not etha tthes tacks t
ore stheda tafrom t het opdown,a dvancingt he
point
e ra sne wda tai sadde d.Whe nt heRTSi se ncount ered( i
mme diat
elyint he
case of$FF58) ,t heS t
ackPoi nte
ri sr eturnedt oi tsor igi
na lpos i
ti
ona ndt he
ret
urnma de.

]
1
32 As
sembl
yLi
nes

Be
for
eJSR$FF58 Dur
ingJSR$FF58 A5e
rRTSf
rom $FF58

⇢ea rrowpointst othecurr


entSt
ack
Point
erS,whichi saone-bytepoi
nte
r
tothenextava
il
a bleposit
iononthe
sta
ck(notthela
sts tore
dby t
e).

Not ethatt hea ddr esss t


or ed, $302, ist hel as
tby teoft heJSRc omma nd–or ,
puta notherwa y ,oneby tele sstha nt hea ddr e s
soft hene xtimme di
atec omma nd
foll
owi ngtheJSR.
Uponr eturnf rom t heJSR, theS tackPoi nterist r
ans f
e r
redt otheX- Re g
ister
wi t
ht heTSXc omma ndonl ine1 3.Be caus et heStackPoi nt erisa tt
hene xtav ail
-
ableby teont hes tack,thiswi lla lsopoi nta tthehi gh-or derby t
eoft her e
tur n
addr es
ss t
il
lle 4i nme mor yt he re.⇢i sisr e t
rievedwi tht heLDASTCK,Xonl ine
15a ndputi nat empor arypoi nterloc ati
onPTR+1( $07).⇢eX- Reg i
st
e risthe n
de c
reme nteda ndt hel ow- or derby t
er etri
e ve dandputi nPTR( $06).
⇢eFna lRTSo fther out iner e t
ur nsc ont roltothec aller,atwhi chpoi nt$06,
$07ma ybee x amine dt ov erifyt hes uc cessful det
er minationoft hea ddress$302.
Youma ywisht or unt hisr out i
nea tse ve
ra ldi Lerentlocationsinme mor ytov er-
ifythatineachc asePTRi spr ope rlyse ttoENTRY+2. Wha ty ouha v etheni sas hort
routinewhi chc ande t er
mi newhe rei nme mor yi tisc urrentlybe ingr un.⇢e
onlydi sadvanta get ot hisr out inei st hatt hehi gh- orderby t
ei sre tr
ieve dFr st
,
thusc ompl i
ca t
ingt hing si fwewa ntt oa dds omeoLs etv aluet ot her e
tur n
addr es
s.⇢ede sir
a bi
lityoft hi swi llbes howns hortly.Intheme a nt
ime ,c onsider
thisalter
edv ersionoft heLoc ator1r outine :
1 ********************************
2 * AL15-LOCATOR 2 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 PTR EQU $06
9 RTRN EQU $FF58
10 STCK EQU $100
1
5.S
pec
ial
Prog
rammi
ngTe
chni
que
s 1
33

11 *
0300: 20 58 FF 12 ENTRY JSR RTRN
0303: BA 13 TSX
0304: CA 14 DEX
0305: BD 00 01 15 LDA STCK,X
0308: 85 06 16 STA PTR
030A: E8 17 INX
030B: BD 00 01 18 LDA STCK,X
030E: 85 07 19 STA PTR+1
0310: 60 20 DONE RTS

Wha tI
’vedonehe rei
sdecrementtheX-Regist
er(li
ne1 4)i
mme di
ate
lya4er
theTSXstat
eme ntsothatt
helow-orderbyt
eoft headdresscanbere
tri
evedFrst
.
⇢eINXi st
henl a
t e
rusedtogoba c
ka ndgetthehigh-orderbyt
e.⇢ea dvant
age
ofthiss
yste
mi sthatitmakesaddi
nga noLsetmuc heasi
er.
Toshowwha twecannowdo, lookatt
hisrevis
edprintrouti
ne:
1 ********************************
2 * AL15-RELOCATABLE PRINT 1 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 PTR EQU $06
9 COUT EQU $FDED
10 RTRN EQU $FF58
11 STCK EQU $100
12 *
13 *
0300: 20 58 FF 14 ENTRY JSR RTRN
0303: B8 15 CLV
0304: 50 06 16 BVC CONT
17 *
0306: D4 C5 D3 18 DATA ASC "TEST"
0309: D4
030A: 8D 00 19 HEX 8D00
20 *
030C: BA 21 CONT TSX
030D: CA 22 DEX
030E: 18 23 CLC
030F: BD 00 01 24 LDA STCK,X
0312: 69 04 25 ADC #$04
0314: 85 06 26 STA PTR
0316: E8 27 INX
0317: BD 00 01 28 LDA STCK,X
031A: 69 00 29 ADC #$00
031C: 85 07 30 STA PTR+1
31 *
031E: A0 00 32 PRNT LDY #$00
0320: B1 06 33 LOOP LDA (PTR),Y
0322: F0 06 34 BEQ FIN
0324: 20 ED FD 35 JSR COUT
0327: C8 36 INY

]
1
34 As
sembl
yLi
nes

0328: D0 F6 37 BNE LOOP ; ALWAYS UNTIL 255


38 *
032A: 60 39 FIN RTS
032B: 28 40 CHK

A4erc all
ingthedummyr eturnsta
teme nt,af orce
dbr anchov ertheda t
a
sect
ionisdone .⇢i swi l
lhav enoe Lectont hea ddressrema i
ningont hesta
c k
.
AtCONT,wet aketheg eneralpr ocedureusedi nLoc ator2a nda ddt heCLCa nd
ADCs t
ateme nt
sne ededt oadda noLsettot hea ddr e
ssont hes tack.Wha twe
needisthes ta
rti
nga ddressoft heAS CIIdatat obepr inte
d. Si
ncet heda t
asta
r t
s
at$306 andt hea ddressont hes t
ackis$302( se eearl
ierexampl es)theoLs e
t
neededis#$04.
⇢isma ys eema rbit
rarybutt hev al
uet oa ddwi l
la l
waysbe#$04i fyou
alwaysdot heCLV,BVC$XXXXbr anchimme diate
lya 4erther e
turn.⇢e nfoll
ow
thatwit
ht heda t
atobepr inted.
Oncet hea c
tualaddressoft heASCIIda taha sbeenc al
culated,itispri
nted
inthePRNTs ecti
onbyus eoft hei ndexedpointera tLOOP.

J
SRSi
mul
ati
ons
Youmi ghtg ettheimpr ess
ionf r
om t heabovee xamplethatatremendous
codee xpa
nsiontakespl a
c et oaccomplis
ht herel
oc a
tabili
tyofaprogra
m. ⇢isis
some whattr
ue ,butitde pe ndsonhowy ouwr i
tethepr ogram.⇢eus eofCLV,
BVC$XXXXt akesonlyt hr
e eb yte
swhe retheJMP$XXXXi twasrepl
aci
nga l
soused
threebyte
s.
⇢es tackope ra
tionsj ustdiscuss
edta keasma llnumbe rofbyt
estoimple-
me ntbutcouldbecomer athe rl
argeifusedma nytime s
. Whatisnee
dedisawa y
toputthestackope r
a t
ionsi nas ubrouti
ne. Unf
ortunately
,JSRisoneofthenon-
rel
oc a
tabl
ecomma nds.
1 ********************************
2 * AL15-NON-RELOCATABLE JSR DEMO*
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 BELL EQU $FF3A
9 *
10 *
0300: 20 04 03 11 ENTRY JSR TEST
12 *
0303: 60 13 DONE RTS
14 *
0304: EA 15 TEST NOP
16 *
0305: 20 3A FF 17 EXPT JSR BELL
18 *
0308: 60 19 FIN RTS
1
5.S
pec
ial
Prog
rammi
ngTe
chni
que
s 1
35

20 *
21 * WILL RETURN TO DONE
22 *

⇢i srout
inei sverysi
mi la
rtot henon-r
elocatableJMPde mopr es
entedear
-
li
er,
withtheexcepti
ont hatthecal
ltotheBELLr outineha sbe
e nmadeas ubr
ou-
ti
neits
elf
,hea
de dbyt helabelTEST.Inthi
sli
sting,TESTi sfol
lowedbyadummy
NOPsta
tement
, butwe ’
llFl
lthatinshort
ly.
⇢i sprogram,a swr i
tte
n,c anrunonlya tthea ddresss
pe c
iFe
di ntheORG
st
atement.
Herei sanimprov edvers
ion,usi
ngas imul ati
onoftheJSRc ommand:
1 ********************************
2 *AL15-RELOCATABLE JSR SIMULATION
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 PTR EQU $06
9 BELL EQU $FF3A
10 RTRN EQU $FF58
11 STCK EQU $100
12 *
13 *
0300: 20 58 FF 14 ENTRY JSR RTRN
0303: B8 15 CLV
0304: 50 01 16 BVC TEST
17 *
0306: 60 18 DONE RTS
19 *
0307: BA 20 TEST TSX
0308: CA 21 DEX
0309: 18 22 CLC
030A: BD 00 01 23 LDA STCK,X
030D: 69 03 24 ADC #$03
030F: 85 06 25 STA PTR
0311: E8 26 INX
0312: BD 00 01 27 LDA STCK,X
0315: 69 00 28 ADC #$00
0317: 85 07 29 STA PTR+1
30 *
0319: 20 3A FF 31 EXPT JSR BELL
32 *
031C: A5 07 33 FIX LDA PTR+1
031E: 48 34 PHA
031F: A5 06 35 LDA PTR
0321: 48 36 PHA
0322: 60 37 FIN RTS
38 *
39 * WILL RETURN TO DONE
40 *

⇢i spr
ogra
mi sve
rysimi
la
rtot
hePri
nt1prog
ram,wit
htwoex
cept
ions
.
Fi
rst
,#$03isa
ddedins
tea
dof#$04t
othea
ddr
essonthes
tac
k.⇢isi
sasubtl
e

]
1
36 As
sembl
yLi
nes

pointwor t
hme ntioning ,andy ous houldr eviewt hel ist
ingsunt i
ly ouf eelc om-
fort
ab l
ewi thwha ti sbe i
ngdone .Re me mbe rt ha tther eturna ddressf ora
JSR/RTSi sa lwa yso nel e
sst hant hea ddressy ouwa ntt or et
ur nto.I nt hec a seof
theDATAs tateme nt,wene ededt oknowt hee xactaddr e
ssoft heFr stchara cterof
thestringt obepr inted.He nc ethedi Le r
enc ei nt heoLs e
tv a
lueus edi ne ach
case.
Onc et heoLs etha sbe ena ddeda ndt hepr operr e t
urna ddr essc al
cula t
ed, the
FIXs ecti
onus est hePHAc omma ndstoputt he seont hestack. ⇢uswhe ntheRTS
isencount ered, thepr og ramr e t
urnst oDONE. Not icet hatweha ves eemi ng l
yv i
o-
lat
edt wog ene r
al rulesofa s s
embl y-languagepr og rammi ng .⇢eFr stisus ingt he
PHAc omma ndswi t
houtc orrespondi ngPLAs tateme nt s,andt hes econdi st heus e
ofanRTSwi thoutac alli
ngJSR.
Uponf ur t
he rthoug ht,howe v
e r,itshoul dbe c omea ppa r
e ntt hatt het wo
counterac t
ede achot her, andtha tanRTSi sreall
ye qui val
e nttot woPLAs .
⇢ec onv erseoft hisisus ingt woPLAswi thinar out i
nec a l
ledbyaJSRt o
avoidr eturni ngt ot hec a l
linga ddre ss
.⇢i sise qui vale
ntt ous ingaPOPc om-
ma ndi naBAS ICs ubr out i
nec alle
dbyaGOSUB.
Ha vingt huss i
mul a tedt heJSR c omma nd,l et’sputi ta l
lt og et
he ri ntoa
rewriteoft hePr int1r out i
net hatus escall
stos ubr out i
nest omi ni mizet hee x t
ra
coder equire dtoma ket her outiner elocat
able:
1 ********************************
2 * AL15-RELOCATABLE PRINT 2 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 PTR EQU $06
9 COUT EQU $FDED
10 RTRN EQU $FF58
11 STCK EQU $100
12 *
13 *
0300: 20 58 FF 14 ENTRY JSR RTRN
0303: B8 15 CLV
0304: 50 15 16 BVC PRINT
17 *
0306: D4 C5 D3 18 DATA1 ASC "TEST1"
0309: D4 B1
030B: 8D 00 19 HEX 8D00
20 *
030D: 20 58 FF 21 L2 JSR RTRN
0310: B8 22 CLV
0311: 50 08 23 BVC PRINT
24 *
0313: D4 C5 D3 25 DATA2 ASC "TEST2"
0316: D4 B2
0318: 8D 00 26 HEX 8D00
1
5.S
pec
ial
Prog
rammi
ngTe
chni
que
s 1
37

27 *
031A: 60 28 DONE RTS
29 *
031B: BA 30 PRINT TSX
031C: CA 31 DEX
031D: 18 32 CLC
031E: BD 00 01 33 LDA STCK,X
0321: 69 04 34 ADC #$04
0323: 85 06 35 STA PTR
0325: E8 36 INX
0326: BD 00 01 37 LDA STCK,X
0329: 69 00 38 ADC #$00
032B: 85 07 39 STA PTR+1
40 *
032D: A0 00 41 PRNT LDY #$00
032F: B1 06 42 LOOP LDA (PTR),Y
0331: F0 06 43 BEQ FIX
0333: 20 ED FD 44 JSR COUT
0336: C8 45 INY
0337: D0 F6 46 BNE LOOP ; ALWAYS UNTIL 255
47 *
0339: 18 48 FIX CLC
033A: 98 49 TYA
033B: 65 06 50 ADC PTR
033D: 85 06 51 STA PTR
033F: A5 07 52 LDA PTR+1
0341: 69 00 53 ADC #$00
0343: 48 54 PHA
0344: A5 06 55 LDA PTR
0346: 48 56 PHA
0347: 60 57 FIN RTS
58 *
59 * WILL RTS TO L2/DONE
60 *
0348: AC 61 CHK

⇢i sr outineha sthea dvantageofa l


lowingt hePRINTs tate
me nt
stobeus ed
verymuc ha sthoug hthe ywe reint henon- relocatabl
ev ersi
ong i
veninc hapte r
13. ⇢ee x
t rabytesrequi r
edf orthes tackcalcula
tionsa reconFnedtoonepl a
c e,
andt herea reonlythreee xtrabytespe rli
netobepr inte
d, c
ompa redtothec hap-
ter13r outine.
⇢er eturntot hee ndofe a
chpr intedstri
ngi sa cc
ompl is
hedbyus i
ngt heY-
Re gi
sterinFIX. Ate ntryt oFIX, t
heY- Regis
terwi llholdthelengt
hofthes tring
printed,whi chisthe na ddedt oPTRt oc alc
ulatethepr operaddresst
or et
ur nt o.
Ag ai
nweus ethetwoPHAsf oll
owe dbya nRTSt oa ccomplis
ht here
turn.

Se
lf-
Modi
fyi
ngCode
Ah,he
reisana r
eatomak
et hest
ronge
sthear
tquiv
er–theideatha
tapr o-
g
ramrewri
tesi
ts
elftoac
compl
ishit
sgivent
ask.
⇢epos si
bil
it
ie
sareendle
ss,but
f
ornowwe ’
llj
ustlookatawayofcopingwit
hs t
ate
mentsli
keLDA$ADDR,X.It

]
1
38 As
sembl
yLi
nes

wast
hist
ypeofst
ate
me nti
ntheve
ryFrstpr
ogr
am oft
hischa
pte
rtha
tcont
ri
b-
ut
edtoit
snon-
rel
ocat
abil
it
y.He
re’
sthenewmyst
erypr
ogra
m:
1 ********************************
2 * AL15-RELOCATABLE PRINT 3 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 PTR EQU $06
9 COUT EQU $FDED
10 RTRN EQU $FF58
11 STCK EQU $100
12 *
13 *
0300: 20 58 FF 14 ENTRY JSR RTRN
0303: B8 15 CLV
0304: 50 14 16 BVC PRINT
17 *
0306: D4 C5 D3 18 DATA ASC "TEST"
0309: D4
030A: 8D 00 19 HEX 8D00
20 *
030C: A2 00 21 PRNT LDX #$00
030E: BD 06 03 22 LOOP LDA DATA,X
0311: F0 06 23 BEQ DONE
0313: 20 ED FD 24 JSR COUT
0316: E8 25 INX
0317: D0 F5 26 BNE LOOP ; ALWAYS UNTIL 255
27 *
0319: 60 28 DONE RTS
29 *
031A: BA 30 PRINT TSX
031B: CA 31 DEX
031C: 18 32 CLC
031D: BD 00 01 33 LDA STCK,X
0320: 69 04 34 ADC #$04
0322: 85 06 35 STA PTR
0324: E8 36 INX
0325: BD 00 01 37 LDA STCK,X
0328: 69 00 38 ADC #$00
032A: 85 07 39 STA PTR+1
40 *
41 *
032C: A0 09 42 FIX LDY #$09 ; LEN OF $ + 5
032E: A5 06 43 LDA PTR
0330: 91 06 44 STA (PTR),Y
0332: C8 45 INY
0333: A5 07 46 LDA PTR+1
0335: 91 06 47 STA (PTR),Y ; REWRITE DATA ADDR
0337: B8 48 CLV
0338: 50 D2 49 BVC PRNT
50 *
033A: 4E 51 CHK
1
5.S
pec
ial
Prog
rammi
ngTe
chni
que
s 1
39

⇢isprogr
am wi
lla
ctua
ll
yre
writ
ethea
ddre
sspr
esentonli
ne22fort
heLDA
DATA,X s
tat
ement
.⇢eme t
hoduse
stheaddr
essonthestac
kt oca
lcul
at
ethe
addressfort hebe ginni ngoft heAS CIIstr i
ngt obepr inted.Itist hisaddr essthat
wewi l
lwa nte vent ua l
lyt oputi ntothec odea t$30F,$310t or e wri
tet heda t
a
sta
teme nt.
A4e rc alculatingt hea ddressinlines3 073 9,ther e
sultiss toredi nPTR.⇢e
FIXs ecti
ont he na ddst helengthoft hepr inteds tringpl usFv ea ndus e sthisas
theY- RegisteroLs ett oFna l
lypointto$30F.⇢el ow-a ndhi gh-or derby tesare
thenwr it
tent ot hec odea ndar et
urndonet ot heac tualPRNTr outine .
⇢i se xa mpl ec ome swi t
hma nyc a uti
ons .⇢ev al
ueonl ine4 2mus tbe
appropr i
atet ot hel e ngthoft hes t
ri
ngbe ingpr inted.Al so,t heor de roft he
ENTRY, DATA,a ndPRNTr outi
neswa sdeliberatel
yc hos ent oma ket her ewrit
ea s
easya spos s
ible .Ex tremec aremus tbet a ke
nwhe ne v erconstr uctingapr ogram
thatalter
si t
self,butt her esul
tsc a
nbev e
r ypowe rful.
Ifyoua r einc li
ne dt opur suethis,studyt hisex ampl ewe llunt ilyoua r
ev ery
surewhye ac hs tepwa sdone .Tov er
ifyi t
sv ersat
ility,yous houl da s
se mblet he
codef ort hise x ampl ea ndt henr uni ta tsev era
ldi Lerentme mor yl ocati
ons.
A4e reachr un, listthec odef r
om theMoni t
ora nds e ehowt hes tateme ntonl ine
22ha sbeenr e wr it
ten. I
t’sreal
lyquitefascinati
ng !

I
ndi
rec
tJumps
Tor oundoutt hischa pter,onemor et echniquewi l
lbedi scussed.Al though
thesteppi ngme t
hodus ingf orc e
dbr anchingc anbeus edtos panla r
g edistances
,
itcang etr at
herinc onvenie nttoha vetok eepinsert
ingsteppingpoi nt sthrough-
outyourc ode.Ana lt
ernatet echniqueistous etheindirectJMPc omma nd.
Int heindire
c tjump, at wo-bytepointeriscreate
dwhi c hindicat
e swhe r
ethe
j
umps houl dbema deto.⇢ea dde da dvantageoft hisc omma ndi st hatthe
pointerne e
dnotbec reatedont hezeropa ge,whi chalreadyisi nhig hde ma nd
fornume rousothe ruses.1⇢e basi
csyntaxfort heindire
ctjumpi s:
0300: 6C FF FF 99 J1 JMP ($FFFF)

Herei
sas
amplepr
ogr
ams howi
nghowt
hisc
anbec
ombi
nedwi
tht
hes
tac
k
ope
rat
iont
ocr
eat
earel
oca
tabl
ejumpcomma
nd:
1 ********************************
2 * AL15-RELOCATABLE JMP 3 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 PTR EQU $06
9 BELL EQU $FF3A

1[
CT]S
eeAppe
ndi
xFf
ort
hel
is
tofa
vai
la
blez
ero-
pag
eloc
ati
ons
.

]
1
40 As
sembl
yLi
nes

10 RTRN EQU $FF58


11 STCK EQU $100
12 *
13 *
0300: 20 58 FF 14 ENTRY JSR RTRN
15 *
0303: BA 16 CALC TSX
0304: CA 17 DEX
0305: 18 18 CLC
0306: BD 00 01 19 LDA STCK,X
0309: 69 17 20 ADC #$17
030B: 85 06 21 STA PTR
030D: E8 22 INX
030E: BD 00 01 23 LDA STCK,X
0311: 69 00 24 ADC #$00
0313: 85 07 25 STA PTR+1
0315: 6C 06 00 26 JMP (PTR) ; TO 'EXPT'
27 *
0318: EA 28 FILL NOP
29 *
0319: 20 3A FF 30 EXPT JSR BELL
31 *
031C: 60 32 DONE RTS

⇢es ystemi sfairl


ys impl e,basicallyjustus ingthes tackt og etaba seaddr ess
andt hena ddi ngwha t
eve rthedi st
a ncei sbe twe enthee ndoft heJSRRTRNs t
a te
-
me nta ndt hede stinat
ionoft heJMP().Aswi thsomeoft heot he rsy ste
ms ,
thoug h,thisdi s
tanc ewi llchang ea sc odei sa ddedorde letedbe t
we ent het wo
points.Youma ythusha vetoc hang et hev aluesonl ine s20a nd2 4r athe rfre-
quent l
yt oke epupwi t
hy ourc odec ha ng es.
Howe ver,itdoe sav oidthepr oble msa s soci
atedwi thma nys t
eppi ngpoi nts
sprinkledthr oughouty ourc ode ,aswoul dbene ces
saryus ingt heot hera lterna-
tiv
e.
⇢e reisonebugi nt heus eoft hei ndi rectjumpt hats houl dbeme ntione d. I
t
ispr ese
nti nt he6 50 2mi c
ropr ocessori t
self,andoc cur swhe nev ert hei ndire c
t
pointers t
raddl e
sapa gebounda ry2F
. ore xampl e,ifyouus edt hes ta t
eme ntJMP
($06), thede sti
nationwoul dber etrievedf rom locati
ons$06a nd$07. Howe v er,
ifyouwe retous eJMP($3FF), thede s
tina ti
onwoul dber et
rieve dfrom $3FFa nd
$300. ⇢ehi gh-orde rbyt eisnotpr ope rlyinc rementedbyt he6 502.⇢i si sus u-
all
ynotac onc ern,thoug h, si
nc ethe rea rege nerall
yma nya lternateloc ati
onsf or
thede st
inationpoi nter.
I
nc onclus i
ont hen,c ert
a i
nt echni que sca nbeus edt opr oduc ec odewhi chi s
notr estri
ctedt or unninga tapa rticulara ddr essinme mor y.Al thoug habi t
harde rtocons tr
ucti nit
ially,ands l
ig htlylarge rintermsofFna lme mor yre quire-
me nts,thepr oductisg ene r
allymuc hmor eve rsa
til
einitsa ppl i
c a
tions .

2[
CT]⇢i
sbugwa
sFx
edi
nthe6
5C0
2.
Volume 2

]
1
6
1
6. Pas
si
ngDat
afr
omAppl
eso
(BAS
IC
J
anua
ry1
982

One usefula ppli


cat
ion ofa ss
e mbly-la
nguage programming isint he
enhancementofy oure x
ist
ingAppl eso4pr ograms.Somepe opl
eareincl
inedt o
writeal
ltheirpr ogramsina s
semblyl anguage,butitma ybemor eeWcienton
occasi
ontowr it
e“ hybri
ds”–pr ogra
mst hatareacombina t
ionofApple
so4a nd
ass
e mblyla
ng uage. I
nthiswa y,part
ic
ul arfunct
ionscanbedonebyt hel
ang uage
bestsui
tedtothepa r
tic
ulartask.
Ifyouhadt owr i
teas hortprogr
a mt os t
oretenname s
,itwouldbebe stto
doitinApples o4:
10 FOR I = 1 TO 10
20 INPUT N$(I)
30 NEXT I

⇢i sismuc hs implertha
nt heequi
vale
ntpr ogra
mi na s
sembl
yl angua
ge .In
ca
seswhe rene i
thers pee
dnorpr ograms i
zeisac oncern,Apple
so4i sac om-
pl
ete
lyaccept
a blesoluti
on.
Howe ver
,i fyouha dtosortathousandna mes,spee
dwoul dbecomeac on-
ce
rna nditwoul dbewor thconsi
deri
ngwhe therthejobcouldbestbedonei n
as
semblylangua ge
.
Ifyouhav eeverdoneaCALLi noneofy ourBAS ICprograms,t
heny ouha ve
al
rea
dyc ombine dAppl es
o4wi t
hma chi
nec ode.Forexample:
10 HOME
20 PRINT "THIS IS A TEST"
30 PRINT “THIS IS STILL A TEST”
40 GET A$
50 VTAB 1: HTAB 5: CALL -958

I
nt hisprogra
m, alineoft e
xtisprint
edont hes
c r
ee n.A4eryoupr essakey,
allt
extont hescr
eena4e rtheFr s
twor d“THIS”iscl
eared.
Nowa l
thoughitmi ghtbepos si
bletoaccompli
sht hesamee Lec tinAppl e-
so4bypr i
nti
ngma nybl anklines,i
twoul dnotbeasfastora seWcie ntinterms
ofcodea stheCALL-958.
I
ne xecuti
ngthea bovepr ogram,theAppleso4inte
r pr
e t
ergoesa longcarry-
ingouty ourinst
ruct
ionsunt i
litreachestheCALLst
a t
eme nt.Atthatpoi ntaJSR
isdonetot headdres
si ndica
tedbyt heCALL.Whe ntheFna lRTSise nc ounte
red,

]
1
44 As
sembl
yLi
nes

controlre
tur
nst otheBAS ICpr og
ram.I nbetween,howev
er,youcandoa ny-
thi
ngy ou’
dlike!
CALLi
ngroutinesi
sha r
dlycomplic
atedenought owar
rantanenti
recha
pter
ont hesubje
ct.⇢er ealques
ti
onsa re
,how doy oupassdatabackandfort
h
betweenthetwopr ograms,andhowc ant heproble
m ofhandli
ngthatdat
abe
ma deeasi
erf
orthea ss
embly-l
anguageprogra
m?

Si
mpl
eInt
erf
aci
ng
⇢ee
asi
estwa
ytopa
ssda
tat
oana
sse
mbl
y-l
ang
uag
erout
inei
ssi
mpl
yto
POKE t
hea
ppr
opr
iat
eva
lue
sint
ounus
edme
mor
yloc
ati
onsa
ndt
henr
etr
iev
e
t
hem wheny ougettoy ourassembl
y-l
anguager
out
ine
.Toill
ust
rat
ethis
,le
t’
s
r
esurr
ectt
hetonerouti
nefrom chapt
erei
ght.
Tousethis
,as
s e
mblet hecodeandpla
cetheFna
lobje
ctc
odeat$300.⇢en
e
nterthea
ccompanyingAppl e
so4progr
am.
1 ********************************
2 * AL16-SOUND ROUTINE 3A *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 *
9 PITCH EQU $06
10 DURATION EQU $07
11 SPKR EQU $C030
12 *
0300: A6 07 13 ENTRY LDX DURATION
0302: A4 06 14 LOOP LDY PITCH
0304: AD 30 C0 15 LDA SPKR
0307: 88 16 DELAY DEY
0308: D0 FD 17 BNE DELAY
030A: CA 18 DRTN DEX
030B: D0 F5 19 BNE LOOP
030D: 60 20 EXIT RTS

⇢i
sAppl
eso4pr
ogr
ami
sus
edt
oca
lli
t:
10 INPUT "PITCH, DURATION? ";P,D
20 POKE 6,P: POKE 7,D
30 CALL 768
40 PRINT
50 GOTO 10

⇢eAppl eo4pr
s ogra
m wor ksbyFrstr
equesti
ngval
uesforthepi t
cha nd
durat
ionofthetonefrom t
heuser.⇢eseval
uesa r
ethenPOKEdint
ol oca
tions6
and7a ndthetoner out
ineCALLed.⇢etoner out
ineuse
stheseval
ue stopr o-
ducethedesir
eds oundandt henret
urnstotheCALLi
ngpr og
ramf oranot her
round.
1
6.Pa
ssi
ngDa
taf
rom Appl
eso4BAS
IC 1
45

⇢iste
chni
queworksFneforli
mit
edappl
ic
ati
ons.Havi
ngtoPOKEal
lofthe
des
ir
edpar
ameter
sint
ovari
ouscorner
sofmemoryisnotSex
ibl
e,howe
ver,
and
st
ri
ngsa
renear
lyi
mpossi
ble
.⇢e remustbea
nalt
ernat
ive
.

⇢eI
nte
rna
lSt
ruc
tur
eofAppl
eso5
Ify
ou’vebeenfoll
owinga l
ong,you’venodoubtFguredoutbynowthatI
’m
agre
atbel
ieve
rinus i
ngroutinesal
readypres
entintheApple,whe
reposs
ibl
e,to
ac
compli
shapa rti
cula
rtask.Sincerouti
nesalr
eadye x
isti
nAppl e
so4forpro-
ce
ssi
ngvari
able
sdirect
ly,
whynotus et hem?
Toanswerthis
,wemus ttakeabr i
efdet
ourtooutli
nehowAppl e
so4actu-
al
ly“
runs”aprogra
m. Considerthi
ssimpleprogr
am:
10 HOME: PRINT "HELLO"
20 END

A4e ryou’veente
redthisintothec omputer
,typingLISTshouldre
produce
thelis
tingg i
venhe r
e.Ani nter
esti
ngque st
ionari
ses:“Howdoe sthecomputer
act
ua l
lystore,
andthenlate
re xec
ute,thisprog
ram?”
Toa nswerthat,we’
llhavetog ot otheMonitora ndexaminetheprog
ram
datadirectl
y.
⇢eFr stquest
iontoa nsweris,exactl
ywhereint hecomputeristhepro-
grams tored?⇢i scanbefoundbye nteri
ngtheMoni t
orandtypi
ngin:67 68
AF B0a ndpr e
ssi
ng<RETURN>.
⇢ec omputershoul
dr es
pondwi th:
0067- 01
0068- 08
00AF- 18
00B0- 08

⇢eFrstpairofnumbe rsisthepoi
nterfortheprogr
am beginni
ng–byte
s
rev
erse
dofc ourse
.⇢e yi
ndicatethatt
hepr ogramstar
tsat$801.⇢es ec
ond
pai
ristheprogra
me ndpointe
r,andtheyshowt hati
tendsat$818.Usi
ngthi
s
inf
ormati
onlet
’sexaminetheprogra
m databytypingi
n:
801L

Yous
houl
dge
t:
0801- 10 08 BPL $080B
0803- 0A ASL
0804- 00 BRK
0805- 97 ???
0806- 3A ???
0807- BA TSX
0808- 22 ???
0809- 48 PHA
080A- 45 4C EOR $4C

]
1
46 As
sembl
yLi
nes

080C- 4C 4F 22 JMP $224F


080F- 00 BRK
0810- 16 08 ASL $08,X
0812- 14 ???
0813- 00 BRK
0814- 80 ???
0815- 00 BRK
0816- 00 BRK
0817- 00 BRK
0818- F9 A2 00 SBC $00A2,Y
081B- 86 FE STX $FE

⇢i
sobv
ious
lyi
snotdi
rec
tl
yex
ecut
abl
ecode
.Nowt
ypei
n:
801.818

⇢i
swi
ll
giv
e:
0801- 10 08 0A 00 97 3A BA
0808- 22 48 45 4C 4C 4F 22 00
0810- 16 08 14 00 80 00 00 00
0818- 8C

Tounde r
standthis
,let’
sbrea
ki tdownones ect
ionatat i
me.Whe nthe
Applestore
sal ineofBAS IC,i
tencodeseachk e
ywordasas i
ngle
-bytet
oken.
⇢ust hewordPRINTi sst
or e
dasa$BA. ⇢isdoeswonder
sforconse
rvi
ngme m-
ory
.Ina ddit
ion,the
reiss omeoverheadass
ociat
edwithpackag
ingtheli
ne:a
byt
etos ig
nifytheendoft heli
ne,afewby t
esatthebegi
nningofeachli
neto
hol
dinformationrel
ate
dt oit
sle
ngth,andt
helinenumberit
sel
f.
Tobemor espec
iFc:
0801- 10 08 0A 00 97 3A BA
0808- 22 48 45 4C 4C 4F 22 00
0810- 16 08 14 00 80 00 00 00
0818- 8C

⇢eFr sttwobyte sofeveryli


neofa nAppl es
o4pr ogr
ama rea ni ndext othe
addressofthebe gi
nni ngofthene x
tline.At$801, $802weF ndt hea ddr es
s$810
(bytesreverse
d).⇢i si swhereline20s ta
r t
s.At$810weFndt hea ddr ess$816.
⇢i siswhe rethenex tlinewouldstartift
he r
ewe r
eone .⇢edoubl e$00a t$816
tel
lsAppl es
o4t hatthisistheendoft heBAS I
Cl is
ti
ng .I
tisimportantt or eali
ze
thatt he$00 00 endoft heAppl e
so4pr og ram usuall
y,butno talwa y ,c
s orr
e-
spondst othec ont
ent sof$AF,$B0.Itispos s
ibletohidema chi
ne -
lang uagec ode
betwee ntheendofthel inedataandthea ctualendasindicat
edby$AF, $B0–b ut
mor eont hatlat
er.
⇢ene xtinfor
ma tionwithinali
nei sthelinenumbe rit
self
:
0801- 10 08 0A 00 97 3A BA
0808- 22 48 45 4C 4C 4F 22 00
0810- 16 08 14 00 80 00 00 00
0818- 8C
1
6.Pa
ssi
ngDa
taf
rom Appl
eso4BAS
IC 1
47

⇢e$0A00i st
hetwo-byt
efor
m oft
hede c
imalnumbe r10
,thelinenumber
oftheFrs
tli
neoftheApples
o4progr
am. Li
kewis
e,the$1400isthedatafort
he
l
inenumbe r20.⇢eby t
esareaga
inrev
erse
d.A4e rthe
sefourbyteswese et
he
act
ualtok
ensforea
chli
ne.
0801- 10 08 0A 00 97 3A BA
0808- 22 48 45 4C 4C 4F 22 00
0810- 16 08 14 00 80 00 00 00
0818- 8C

Allbyteswi t
hav alueof$80org r e
a t
e ra reAppl eso4k eywor dsi ntoke n
form.By te
sl esst ha n$80r epresentnor ma lAS CI Idata(let
tersoft hea l
phabe t
,
fore xample ).Ex ami ningt heda tahe rewes eea$97f oll
owe dby$3A.$97i sthe
toke nforHOME,a nd$3At hec olon.Ne xt ,
$BAi st hetokenf orPRINT.⇢i sisfol-
l
owe dbyt hequot e( $22) ,thet extforHELLO( $48454C4C4F) ,a ndt heclosi
ng
quot e($22). Lastofa l
l,the$00i ndicatesthee ndoft heline.
I
nl i
nenumbe rtwe nt y,the$80i sthet ok enf orEND. Asbe f
ore,thel i
neister-
mi natedwi tha00.
Whe napr ogrami se xecuted,thei nterpre t
e rscansthroug htheda ta.Eac h
timei te ncount ersat ok en,s ucha sthePRINTt oken,itlooksupt hev al
uei na
tabletos eewha tactions houldbet aken.I nt hec as
eofPRINT,t hiswoul dbet o
outputt hec har actersfollowi ngt hetoken, name lyHELLO.
⇢i scons tantt ransla t
ioni sther e
a sonfort heus eofthet ermi nterpr
eterfor
Appl eso4BAS IC.
Ma chi
nec ode ,ont heot he rhand,i sdirec tl
ye x
e c
utablebyt he6 502mi cro-
proc es
sora ndhe nc eismuc hfaster,si
ncenot a blelookupsa rerequired.
I
nAppl eso4,as ynt axe rrorisg ene ratedwhe neveras eriesoft okensi s
enc ounteredt ha tisnotc onsistentwithwha tthei nte
rpreterexpectstoFnd.

Pa
ssi
ngVa
ria
ble
s
S o,ba cktot hepoi ntofa llthis.⇢ek eyt opa ss
ingv ar
iablestoy ourown
ass
e mbl y-l
ang uag er outinesi stowor kwi t
h Appl eso4 i nte rmsofr outi
nes
alr
e adypr esenti nt hema chine.Oneoft hes i
mpl e
stme thodswa sde sc
ribedin
chapt er13,whe re i
nag ivenva r
iableisthev er
yFr stonede Fne diny ourpr ogr
am
(se
et heinputr out i
ne )
.⇢i sisok ay,butr atherr est
ricti
ve.A be t
terwa yisto
namet hev ari
abley ou’rede al
ingwi thr ightint heCALLs tatement .
⇢ei mpor t
a ntpoi ntsherea retwoc ompone ntsoftheAppl eso4interpret
er:
TXTPTRa ndCHRGET( andr el
atedrout ines).
TXTPTRi sthet wo- bytepointer($B8, $B9)tha tpoi
nt stothene xtt
ok e
nt obe
analyz ed.CHRGET( $B1)i sav er
ys hor trout i
net hatresi
de sont hez eropa geand
thatr eadsag ivent ok enintot heAc cumul ator.I nadditiontoi tsoc cas
ionall
y
beingc a l
leddirec t
ly,ma nyot herr outinesus eCHRGETt opr ocessas tr
ingofda t
a
ina nAppl eso4pr ograml i
ne.

]
1
48 As
sembl
yLi
nes

He
ret
heni
sther
evi
sedt
oner
out
ine:
1 ********************************
2 * AL16-SOUND ROUTINE 3B *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 *
9 PITCH EQU $06
10 DURATION EQU $07
11 SPKR EQU $C030
12 *
13 COMBYTE EQU $E74C
14 *
0300: 20 4C E7 15 ENTRY JSR COMBYTE
0303: 86 06 16 STX PITCH
0305: 20 4C E7 17 JSR COMBYTE
0308: 86 07 18 STX DURATION
19 *
030A: A6 07 20 BEGIN LDX DURATION
030C: A4 06 21 LOOP LDY PITCH
030E: AD 30 C0 22 LDA SPKR
0311: 88 23 DELAY DEY
0312: D0 FD 24 BNE DELAY
0314: CA 25 DRTN DEX
0315: D0 F5 26 BNE LOOP
0317: 60 27 EXIT RTS

⇢eAppl
eso4c
all
ingpr
ogr
am woul
dthe
nber
evi
sedt
ore
ad:
10 INPUT “PITCH DURATION? “;P,D
20 CALL 768,P,D
30 PRINT
40 GOTO 10

⇢i sisamuc hmor eeleg antwa yofpa ssingt hev a


luesanda lsorequiresno
mis c
ell
ane ousme moryl ocationsa ssuch( al
thoug hforpur posesofsimplicit
ythe
tonerout i
nei tselfsti
llusesthes amez ero-pagel oc at
ions.
)
⇢es ecrett ot hene wt ec hniquei stheus eoft her outi
neCOMBYTE( $E74C).
⇢i sisa nAppl eso4r outinewhi chc hecksforac ommaa ndthe nreturnsav al
ue
between$00a nd$FF( 072 55)int heX- Reg i
ster.
Itisnor ma l
lyusedf ore va l
ua t
ingPOKEs ,HCOLOR=, ands ofort
h, butdoesthe
jobv er
yni celyhe re
.Ita ls
ol eav esTXTPTRpoi nt ingt otheendoft hel i
ne( ortoa
colonift herewa sone )byus ingCHRGET t oa dv anceTXTPTRbyt henumbe rof
charactersfollowi nge achc omma .Notea ls
ot ha tanyl e
gale x
pr e
ssion–s ucha s
(X75 )
/2–c anbeus edt opa sst heda t
a.
Tov erifyt heimpor tanc eofma nagingTXTPTR, t
r yputt
ingas impleRTS( $60)
at$300.Ca l
lingt hisy ouwi llg etaSYNTAXERROR,s inceuponr eturnAppl eso4’s
1
6.Pa
ssi
ngDa
taf
rom Appl
eso4BAS
IC 1
49

TXTPTRwi
llbeont
heFr
stc
ommaa
4ert
heCALL,
andt
hephr
ase“
,P,D”i
snota
le
galApples
o4e xpre
ssi
on.
Whatabouttwo-byt
equa nti
ti
es?Todea
lwitht
hem, anumbe rofothe
rr ou-
ti
nesareused.Fore xa
mpl e
,t hi
srouti
newilldotheequiv
alentofat wo-byte
point
erPOKE.Supposef
orinstanceyouwante
dt os
toret
heby t
esfortheaddr e
ss
$9600atl
ocati
ons$1000,$1001.Normall
yinApple
so4y ouwoulddoi tl
ikethis
:
*
*
50 POKE 4096,0: POKE 4097,150
*
*

wher
e4096and4097arethedeci
ma lequi
val
ent
sof$1000and$1001a nd0a
nd
150ar
ethelow-
orderandhi g
h-orderbyte
sfortheaddr
ess$9600($96=150,
$00=0).
Amor ec
onve
nientapproa
chmi ghtbel
iket
his:
*
*
50 CALL 768, 4096, 38400
*
*

orpe
rha
ps:
*
*
50 CALL 768, A, V
*
*

⇢er
out
inef
ort
hiswoul
dbe
:
1 ********************************
2 * AL16-POINTER SETUP ROUTINE *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 *
9 CHKCOM EQU $DEBE
10 FRMNUM EQU $DD67
11 GETADR EQU $E752
12 LINNUM EQU $50 ; ($50,$51)
13 *
14 PTR EQU $3C
15 *
0300: 20 BE DE 16 ENTRY JSR CHKCOM
0303: 20 67 DD 17 JSR FRMNUM ; EVAL FORMULA
0306: 20 52 E7 18 JSR GETADR ; PUT FAC INTO LINNUM
0309: A5 50 19 LDA LINNUM

]
1
50 As
sembl
yLi
nes

030B: 85 3C 20 STA PTR


030D: A5 51 21 LDA LINNUM+1
030F: 85 3D 22 STA PTR+1
23 *
0311: 20 BE DE 24 JSR CHKCOM
0314: 20 67 DD 25 JSR FRMNUM
0317: 20 52 E7 26 JSR GETADR
27 *
031A: A0 00 28 LDY #$00
031C: A5 50 29 LDA LINNUM
031E: 91 3C 30 STA (PTR),Y
0320: C8 31 INY
0321: A5 51 32 LDA LINNUM+1
0323: 91 3C 33 STA (PTR),Y
34 *
0325: 60 35 DONE RTS
0326: 09 36 CHK

⇢es pecialitemsi nthisr outineinc ludeCHKCOM,as ynta x-


che ckingroutine
tha tservest wopur pos e
s.Fi rsti tv e
riFe st hatac omma ndf ollowst heCALL
addr ess,ands e c
ondl yi tadvanc esTXTPTRt opointt ot heFr stby teoft heexpres-
sioni mme diatelyf ol l
owi ngt hec omma .I fac ommai snotf ound,aSYNTAX
ERRORi sg ener at
ed.
FRMNUMi sar out inet ha teva l
uatesa nye xpr
essiona ndput sther ealSoating-
poi ntnumbe rr esul ti ntoAppl eso4’sCo ating-pointAc cumul a t
or,us uall
yc al
led
FAC. ⇢isisas ix-by t
eps eudor egister($977$9C)u sedt ohol dtheSoa ti
ng-point
repr ese
ntationofanumbe r.Itinc l
ude ss uchni ceti
esa sthee xpone ntia
lma gni-
tudeoft henumbe ra ndt hee qui val
entoft hedigitsoft hel og arit
hm oft henum-
be rstored.
Atthiss t
a gey ou’ dha vetobes ome t
hi ngofama soc histtowa nttode alwith
thenumbe rini tsc urr entform, s
ot hene xtstepisus e dt oc onv er
ti tintoatwo-
by teinteger.
GETADRd oest hisbyput ti
ngt het wo- by teresultintoLINNUM, LINNUM+1( $50,
$51) .
Eveni fthi sisnote xac t
lya ni n-dept he xplanationofa llthemos tprec i
se
de tail
soft heope ration,t hebot tom linei sthatthet hr eeJSRs( CHKCOM, FRMNUM,
andGETADR)wi l
la lwa ysre s
ultint helow- or derandhi gh- orde rbytesofwha tever
ex pressi
onf ollowsac ommabe ings t
oredi nLINNUMa ndLINNUM+1.
⇢e ses impl es ubr outiness houldbequi t
ea dequa tef orma nya ppli
cat
ions.
Ne xtchapter,howe ver,we ’l
llooka tstringpa s
sing,s omeot herus efulrouti
ne s
,
andhowt opa ssda t
ab acktot heCALLi ngAppl e
so4pr og ram.
1
7
1
7. Mo
reAp
ple
so(Dat
aPas
si
ng
Fe
brua
ry1
982

Inthepr
evi
ouschapt
erwebe g
anadiscus
sionofhowt opassvar
iabl
esback
andfort
hbetweenAppleso4 a
nda s
sembly-
langua
gepr ogra
ms .⇢isc ha
pter
we’
llc
omplet
ethedi
scuss
ionwithmorei
nformati
ona bouthowallt
ypesofvar
i-
abl
esar
ehandle
dandhowda tacanbepas
sedba c
ktot hecal
li
ngAppleso4pro-
gra
m.

Appl
eso5Va
ria
ble
s
⇢e rea res ixt ypesofv ariablesinAppl e so4BAS IC.⇢e sea rer eal,int ege ,
r
ands tringv ariables,a ndt heira rrayc ounte rpa r
ts.Tounde rstandf ul l
yhow t o
uset hesev ari
abl eswemus tFr stt akeamome ntt oe xa mi net hedi Le re nc es
be twe ent hema swe lla showt hev ariablesarea c t
ua ll
ys tore di nt hec omput er .
Re alv ari
abl esa renumbe rv aluesbe twe en1 038 and71 0 3
8
,whi cha r ev e ry
larg epos iti
vea ndne gativenumbe rs.Ina ddit i
on,t hev alue sne ednotbewhol e
numbe rs;av al
ues ucha s1.25i sallowe d.Intege rva r
iables, ont heot he rha nd, a re
limi tedinma gni tudet other ang eof73 27 67t o+3 276 7.⇢e ya rea l
sol imi tedt o
whol enumbe rv alue s,suc ha s1 ,2 ,3 ,ands oon.Va l
ue ss ucha s1 .
2 5a renot
allowe d.
Re alv ari
ablesa rei ndic a
tedi nBAS I
Cbya na lpha beti
cc ha rac t
er( At oZ)f ol-
lowe dbyal ett
e rornumbe r( At oZor0t o9) .Anyc ha racte rsa 4ert heFr sttwo
arei gnor edwhe nAppl eso4l ook supt hev al
uef ort hev aria ble. I
nt e
g erv ariabl es
ares imi lar,butt hena mei ssuWx edbyape rc ents ign( %).⇢usAwoul dr epr e-
sentt her e alv
ar iable,whe re asA%woul dr epres enta ni ntege rv ariable.
Whe npa ssingda tas uc ha same mor ya ddr essoras i ng le-by t
ev a l
uet oput
inme mor y,inte gerv ar i
ableswoul dbequi tea dequa tea nd,a ddi ti
ona ll
y ,woul d
requi renoc onv ersioni nt hea ssembl y-langua ger out i
ne .Howe v er,i
ti sg ener ally
mor ec onv enientt ot heBAS I Cpr og ramme rnott oha vet oputt he%s i
g ni nt he
var iablena mea nd,i ns te
a d,toc onv ertthev a l
ueus ingt heAppl eso4r out ine
FRMNUM( $DD67)a sde s cri
be dint hepr eviousc hapte r
.Fort her ecord,t houg h,I
wi llpre senta ne x ampl es hor tl
yonhow t or et r
ievea nint e gerv ar
iablef r
om a
callingBAS I
Cpr og ram.
S t
ringv ari
a blesc ons i
stofas eriesofa nyl eg alAS CI Ic ha racters,wi t
hama x-
imum l eng thof2 55c ha racters.Stringsa reindi catedbya$s uWxt othev aria ble
na me .

]
1
52 As
sembl
yLi
nes

Anyoft hesevariable
sma ybepr ese
nte i
thersinglyorinana rr
ay.Ar raysare
gr oupingsofv ari
ablesthatus eac ommonna mea ndt henade l
imit
ings ubscr
ipt
toi denti
fye achindi vi
duale leme nt.Arrayvariabl
esa reindi
catedbyapa i
rof
pa renthe
sesfoll
owi ngt heva ri
ablena mebe t
we enwhi chanumbe rorex pressi
on
ma ybeus e
dt ospecifythede si
rede lement.
Youpr obablya rea l
readys ome whatfamiliarwi t
htheg e
ne r
alpoint sme n-
tione dsofar;they’reraise
dnots omuc htot eachy ouaboutAppl e
so4v ari
able
type sassuchbutr athertose tthes t
ageforwha tistofoll
ow,name l
yhowe a
chof
the sevar
iabletypesisstoredwi thi
nt heme mor yoftheAppl ecomputer.

Me
mor
yMa
ps
Inc ha pteronewepr es
entedag raphicr epresentat
ionoft heme mor yus ag e
ofthec omput er.We ’
dl iketor evivet het opicint heinterestofourc urrents ub-
ject
.
Ame mor yma pi sus edt os howt her elat
iveplaceme ntofda tawi thint he
avail
ableme mor yl ocationsi nt hec omput er.Re c
a l
lthatt he rea r
eat otalof
6553 6l oca t
ionsa v ai
lable,whi ch wei de ntif
ywi t
h hexade cima la ddres s
esof
$0000t o$FFFF.
⇢ec ha rtinTa bl
e1 7-1s howsat ypicalAppl eme mor yma p,wi thDOS
booteda nda na r
bitraryAppl es
o4pr og rami nme mor y.
In pr e vi
ousc ha pters,thea re ass hown ha vebe en de s
cribedi nv arying
degreesofde t
ail
.You’ l
lr e
callthatt hea re af r
om $C000t o$CFFFi sreserv edf or
theinterfac ecarda ddres si
ng,andt hatAppl eso4BAS I
Ci sstore dinROM be gin-
ninga t$D000. ⇢eMoni t
orROM be ginsa t$F800.
A nor malAppl eso4 pr og r
a ms tartsa t$800,wi t
ht hehi g
he sta vail
a ble
addressus uall
yjustbe l
ow$9600, whic hisi dentiFedwi t
ht hel owerbounda ryof
theDi skOpe rati
ngS ys
te m( DOS ).
⇢ea reafrom $300 t o$3CF i sa vailableforus erassembl y-
langua gepr o-
grams .$3D0t o$3FFi sr eservedforAppl es yste
mv ect
ors,suc ha stheDOSe nt ry
vectors.Ze r
opa ge,t hes tack,andt hei nputbuLe ra l
soha v ebe endi scus s
edi n
somede tail.
Sinc eourma i
nc onc ernisi nthea reaofAppl eso4v ari
a bles,l
et’
sc ons i
de ra
revi
s e
dma pe mpha sizi
ngAppl eso4pr og rams .
Ta ble1 7-2s howst hatwhe na nAppl eso4pr ogrami sRUN,s i
mpl e( non- ar-
ray)v ariablesarepl acedi mme diatelya 4e rthee ndoft heBAS ICpr ogra m,f ol-

$00 $100 $200 $300 $400 $800 ... $9600 $C000 $D000 $D800

Ze
ro Input Use
r Sc
ree
n FP FP F8
S
tac
k Fr
ee DOS S
lot
s
Pa
ge BuLer Pa
ge Di
spl
ay Pr
ogra
m BAS
IC ROM

Ta
ble17-
1:Appl
eMe
mor
yMa
p
1
7.Mor
eAppl
eso4Da
taPa
ssi
ng 1
53

$00 $800 $XX $XX $XX $XX $9600

FP S
imple Array S
tri
ng
Fr
ee DOS
Pr
ogra
m Var
iabl
es Var
iabl
es Data

$67,$68− $69,$6A $6B,$6C $6D,$6E $6F,$70 $73,$74


$AF,$B0 LOMEM: HIMEM:

Ta
ble17-
2:Appl
eso5Me
mor
yLa
yout

l
owe dbyt hea r r
a yv a riables.Be c aus et heda taf ore achs tringv a riabl eise ve r
-
cha ng ingi nl eng th,s t r
ingda tai ss tor eddy na mi ca ll
ya tt het opofme mor y
,
wor k i
ngdown. ⇢es pa cei nbe twe ent he sec onv erg inga rea sist hes o-c all
edf ree
spa c eoft hes y s
t e
m.
HIMEM:a ndLOMEM:a reus edbyt heBAS I Cpr og r amme rt os ett heuppe ra nd
l
owe rboundsofv a riables t
or age .Ifnots peciFc a ll
yde c
laredwi thint hepr og r
a m,
the sede fa ulttot hebot tom ofDOSa ndt hee ndoft heAppl e so4pr og r
a m,
res pe ct
iv ely.⇢e ydono t,howe ve r,alwa ysha v et ober estrictedt ot he s
el oc a
-
tions . I
ti spos s
iblet omov eLOMEM:up, orHIMEM:down, soa stos eta sideapor -
tionofme mor yint hec omput ert ha twon’ tbea Lec tedbyt her unni ngpr ogra m.
⇢i si sdonef oroneorbot hoft wor ea sons :Fr stt opr otecte itherorbot hoft he
hi- resdi spl aypa ge sfr om v ariabl et ablee ncroa c hme nt ;or,s econd,t opr ovidea
pr ot ecteda re
af oraus e r’sa s
s embl y-lang ua gepr og ra m.
Nowt hatwek nowwhe ret hei nfor ma tionf ore achv ariablei ss tor edi nt he
comput e r,let’
se xa mi net hef orma toft hei nf or ma tionf ore a chv a ri
a ble .Wi thin
thea rea sindi cated, av ar i
a blet ablei sc ons truc tedt ha tcont ainsbot ht hena meof
theg ivenv a r
iablea ndi tsv aluei ft hev a riablei sar ea lori nteg er.Ift hev ariableis
as tr i
ng , apoi nteri ss tor edt ha ti ndi catest hes tring ’
ss tartingl oc ationa tt het op
ofme mor ya ndi tsc orr espondi ngl e ngth( 0to2 5 5c ha ra c
ters) .
Figur e1 7-3s umma r izesthede tailsoft hef or ma tf orthe set abl es.
Eac ht imeav a riablei sFr ste nc ount eredi nar unni ngAppl eso4pr og ram, an
ent ryi sma defori ti nt hev ariabl eta ble. Fors impl ev ariables, Appl eso4l ookst o
thepoi ntera t$6B,$6Ct os eewhe ret hee ndoft hec ur rents impl ev ar i
a bletable
is.I tthe nope nsups evenby te sfort hene wv a ria blea ndput sabl oc kofda tasim-
il
a rt ot hats howni nFi gur e1 7 -3, asi sa ppr opr iatet ot het ypeofv ar i
a blede Fne d.
Rea lv a r
iabless t
or et hev aluei nal oga rithmi cf orm,whe ree ac hv al
uei s
indi ca t
e dbyt hee x pone nta ndf ourma nt issas.I nteg erv ariabl esre qui reonl yt hat
thehi gh-a ndl ow- orde rby tesoft hev a l
uebes tore d.⇢er ema ini ngt hreepos i
-
tionsa reunus e
d, wi thdummy0v aluespl acedi nt het able.It’simpor tantt onot e
he ret ha tf ori nte gerv ariables ,t het wo- by ter e pr esentation oft hev aluei s
rev er s
edf rom wha twewoul dnor ma ll
ye xpe ct.⇢a ti s
,t hehi gh- or derby teis
pla ce dFr st, f
ollowe dbyt hel ow- orde rby te.

]
1
54 As
sembl
yLi
nes

Fi
gur
e17-
3:Si
mpl
eVa
ria
bleSt
ora
geFor
mat

Fors tring sonl yt hr eeby tesofi nforma t


iona rer equi red,na me lythel e
ng th
anda ddr e
s sda tame nt ione de arlie
r.Ag ain,t hel astt wopos itionsa reFl l
edwi th
dummyz eros .
Itshoul dbee vi
de ntf rom t histabletha tthes a mea mountofme mor yi sa l
lo-
catedfora l
ls implev aria blet ype s
:t herei snoa dv antag eins pecifyi
ngi nteger
varia
blesv ers usr eal
st os a veme mor y.⇢i swi l
l notbet hec asewi tha rrays.
Not ic
et ha tther ea ret wodi stinctpa rtst oe ac hs ev e
n- by t
ev ar
iablee nt ry
.
⇢eFr sttwoby tesde Fnet hena me ,whe r e
,i nc i
de ntally,thehi g
h- orderby teis
usedine a
chc haractert oi ndi ca t
et owhi choft het hr eev ari
a bletype s(real,inte-
ger,orstring )t hatent ryc orre sponds .⇢el astFv eby tesma keupt hea ctua ldata
foreachv ariabl eandc ons istofe i
therther equi rednume r
ici nfor ma t
ionor ,in
thecaseofas tring,thel eng tha nda ddr e
s sinfor ma tion.
⇢er ea sont os tr
e sst hi sdi sti
nc t
ioni st ha t
,ine xami ninga rrays,wenot i
ce
thatiti
st hisFv e-
bytebl oc kt ha tgetsrepe atedal argenumbe roft ime s
, depe nd-
ingont het ot a lnumbe rofe leme ntsint hea rr ay.Fora rrays,amuc hlarg ertable
needstobec ons t
ruc t
e d,a ndt hi sisc r
eate ds tart
inga tt hea ddr essindic ate
dby
$6B,$6C. Whe ne verane wa rra yisde Fne d,t hepoi nt era t$6D,$6Ei sex amine d
tode t
ermi net hee ndoft hec urrenta rrayt ablea ndane we nt ryisma dea c c
or d-
ingtothef or ma tshowni nFi gure1 7-4.
Int hisf or ma t
,thee ntryi sg ivenahe ade rtha tliststhev ari
a blena me ,fol-
lowedbya noLs etva l
ueus edt ode terminet hea ddr essoft hene xta rraye ntryif
oneispr esent . ⇢eoLs eti se ncode di ntheus ua lt
wo- bytema nne r.Followi ngt he
oLsetisaby tei ndicatingt henumbe rofdi me nsionsi nt hea rray,a4e rwhi chi s
lis
tedaby t
ef ore achdi me nsions t
atingi tssize.Al thoug hnots howni nt hedi a-
1
7.Mor
eAppl
eso4Da
taPa
ssi
ng 1
55

Fi
gur
e17-
4:Ar
rayVa
ria
bleSt
ora
geFor
mat

gr
am, e
a c
hs i
zeindic
atorisatwo-byt
epa i
r,alt
houg hinthi
scasethehighbytei
s
al
waysgive nFrst.
I
mme diat
elya4ertheheaderarethea c
tualdatablock
s,eachblockconsi
st-
i
ngofFv e,t wo,orthreebyte
spe rarr
a yele
me nt,dependi
ngonwhi c
hv ari
abl
e
t
ypeisinvol ve
d.Notethat,i
nt hi
scase
, i
ntegervari
ablearr
aysdotakemuc hles
s
memoryt ha nanequival
entrealar
ray.
Asa ne xample,i
fyouwe retodimensionana rr
aywi t
hthisst
ate
me nt:
DIM A$(10,10)

t
hehe
ade
rbl
ockwoul
dlookl
iket
his
:

whe re$41,$80a retheAS CI Ival


uesf oranAf oll
owe dbyanul l
.⇢ehi ghbitis
oLi nt heFr stc haract
er,andoni nt hes econd–i ndic
ati
ngas t
ring.⇢ene xt
arr
a yv ari
ablewoul dbef ounda tthea ddressoftheFr stnamec ha r
acte
rplus
$174. ⇢e rea ret wodime nsionstot hea rra
y ,asindicat
edbyt he$02.⇢e$00
$0Bindi cat
ese l
ev enele
me ntsine ac
hdi mensionoft hearr
ay.⇢i sshouldnotbe
surprisi
ngwhe ny ourecal
lt hattenpl usthez er
othpos it
ionma keseleve
ne le-
me nts.
Followingt hisheaderwewoul dFnd1 21t hree-byt
eblocks
,e a
c hindi
cat
ing
thele ngtha nda ddres
sofas tr
inga r r
aye lement,ifpresent
.1 1×1 1= 1 21;
(121×3 )+9[ fort heheader]=3 72=$ 174.

]
1
56 As
sembl
yLi
nes

Pa
ssi
ngVa
ria
ble
stoAs
sembl
yLa
ngua
ge
Att hispointy ouma ywe llt
hi nkt ha tweha ves trayedv eryfarfr om t het opic
ofassembl y-l
ang uag eprog rammi nga ndha v ebe comeov erl
yinv olv edwi tht he
str
uctur eofAppl eso4. Uponal i
ttler eSe ct
ion, howe ver,itshouldbe comea ppa r-
enttha twemus tha vesomef ami lia
ritywi thhowt he sev ari
ablesar es toredi fwe
aretoi nteractsuc c
e ssf
ullywi tht he m.
Ine i
therr eadingorc re ati
ngAppl es
o4v a ri
abl es,c l
earl
ywemus tha ndle
eLecti
v elye achc ompone ntoft heda ta .Wemus tbea bletoide ntifyt hena me
andloc ationoft hev ari
a bl
ewea reint erestedi n,a nda l
sot omodi fyt hati nfor -
ma t
ioni fne cessary.
⇢et emptationa tthispoi ntmi ghtbet ot ak ethisne w- f
oundk nowl edg ea nd
writ
eourownr out i
ne stoa cc ompl i
sht hene ededope rations,buts uc ha nunde r-
taki
ngwoul dbequi teunne cessar y–nott ome ntionl ikelytoha vey oumi nd-
l
e s
sl
yba bbl i
ngt oy ourselfinnot i
me .For tuna te
ly ,Appl eso4a lrea dyc ont ains
therout inesne cessarytodoa l
mos tany thingwewi sh.⇢ema int rickwi l
lbet o
properlyi denti
fya ndus ethea ppr opriateone s.
Int hepr eviousc hapt erIma deus eofaf ewoft heset oaccompl ishac erta i
n
degreeofSe xi
bilit
yi npa ssingnume ricda tatoa na ssembly-l
a ngua ger outine .
Let’
sc ompl etethes tudybyf or ma lizi
ngt hepos sibleope rat
ions.
⇢eFr stgene r
a lcategor yispa s
singda tatoar out i
ne .Wec a npa ssanyofs i
x
vari
ablet ype s
.Tomi nimi z
et hec onfus ion,letuse s tabli
s hafairl
ys impl eg oal: t
o
passtheda tasuc cessfull
ya ndpr ov es obys toringt heda tainanon- Appl eso4
l
oc a
tion.
I
nte
g erVari
able
s
Forint
egerva
ria
ble
sthec
all
ingAppl
eso4pr
ogr
aml
ook
sli
ket
his
:1

10 A% = 258
20 CALL 768, A%
30 PRINT PEEK(896), PEEK(897)
40 REM 896,897 = $380,$381
50 END

⇢ema
chi
ne-
lang
uag
erout
ines
houl
dbea
sse
mbl
edf
rom t
hisl
is
ti
ng:
1 ********************************
2 * AL17-INTEGER VARIABLE *
3 * READER *
4 * 2/1/82 *
5 ********************************

1[
CT]Foramor
eint
era
cti
vepr
ogr
am,
repl
acel
ine
s10
730wi
tht
hef
oll
owi
ng:
5 PRINT CHR$(4); "BLOAD AL17.READINT"
10 INPUT "INPUT INTEGER: "; A%
20 CALL 768,A%
30 PRINT "LO: "; PEEK (896); " HI: "; PEEK (897)
⇢e
ntr
yva
lue
ssuc
has2
58,
1,71
,32
767
,and73
276
7.Tr
y73
276
8.
1
7.Mor
eAppl
eso4Da
taPa
ssi
ng 1
57

6 *
7 *
8 * OBJ $300
9 ORG $300
10 *
11 CHKCOM EQU $DEBE
12 PTRGET EQU $DFE3
13 VARPNT EQU $83
14 MOVFM EQU $EAF9
15 CHKNUM EQU $DD6A
16 DATA EQU $380
17 *
0300: 20 BE DE 18 ENTRY JSR CHKCOM ; CHK SYNTAX
0303: 20 E3 DF 19 JSR PTRGET ; FIND VARIABLE
20 * Y,A = ADDRESS OF VALUE
0306: 20 F9 EA 21 JSR MOVFM ; MOV VAL -> FAC
0309: 20 6A DD 22 JSR CHKNUM ; FAC = NUM?
030C: A0 00 23 LDY #$00
030E: B1 83 24 LDA (VARPNT),Y
0310: 8D 81 03 25 STA DATA+1
0313: C8 26 INY
0314: B1 83 27 LDA (VARPNT),Y
0316: 8D 80 03 28 STA DATA
29 *
30 * NOTE! HIGH BYTE FIRST!
31 *
0319: 60 32 DONE RTS
031A: F1 33 CHK

Int hisr out i


ne,CHKCOM( $DEBE=CHe c Kf orCOMma )isus e
dt oma k esure
thes y ntaxi sc orre
ct( t
ha ti s
,ac omma ),a ndt oa dvanc eTXTPTR( $B8=Te XT
PoinTe R)t ot heFr stbyteoft hev a ri
ablena mebe inge valuated.Re f
ertothepr e-
viousc hapte rforadi scussionoft he setwor out ines .
PTRGET( $DFE3=P oinTe RGET)i snowc all
ed,whi chisas ubrouti
net hat
readsi nav a r
iablena mea ndt he nl ocatesiti nt hev ariableta bl
e.Asabonus ,if
thev a ri
ablena me ddoe snota lreadye xi
stint het able,PTRGETwi llcreat
eane ntry
forit.⇢i sa ppl i
estov ariablesofa llsixtype s.A4e rre t
ur ni
ngf rom PTRGET,t he
addr essoft hev a
luefort hev ar i
abl eishe l
di nt heY- Re g
istera ndtheAc cumul a-
tor(l owby te ,highby t
e). ⇢i st husi ndicatest hel ocationi nme moryoft hetwo-
to-Fv eby teda tablockdi scuss e
de arlie
r.⇢eda taint heY- Regist
era ndt he
Ac cumul atori sa ls
o dupl ic
a tedi nVARPNT,VARPNT+1 ( $83,$84 = VARi able
PoiNTe r
) ,whi chwi l
lbeus edl aterint hepr ogr am.
Att hiss ta g
ei twoul dbeas i
mpl ema ttert ous ei ndirecta ddres
singt o
retri
e vet het woby tes,butal ittl
emor ee Lortwi l
lr esultinamuc hmor et hor-
oug hr out ine .Itispos si
blet ha ttheus ermi ghtha vec a
lledther out i
newi tha n
impr ope rv a riabletypef ollowi ngt heCALLs t
a t
e me nt,suc hasas tr
ing.⇢i sc an
bec he cke df orbyt hene xtt wopr og r
a ms teps.
MOVFM( $EAF9=MOVet oFACf rom Me mor y)wi llmov ewha teve
rda tais
point edt obyt heY-Re gist
e ra ndt heAc cumul atorintot heSoa ti
ng-pointAc cu-

]
1
58 As
sembl
yLi
nes

mulator( $F97A2=FAC) .⇢ec ontentst he nc anbec hec kedf orvari


abletypeby
thec al
lt oCHKNUM ( $DD6A =CHe cK NUMbe r
).⇢epr esenc eofas t
ringhe re
wouldy i
e ldaTYPEMISMATCHe rr
or.2Un fortuna t
ely,itisnotpa rt
ic
ular
lye as
yt o
tes
tforar e a
l v
a ria
bl eha vingbe enmi s t
ak enlyus edhe re
.
Presumi ngnoe rr
oroc curs,wewi llnow ma keus eoft heda t
as avedi n
VARPNT( sincet heY- Registera ndAc cumul atornodoubtha vebeena l
teredby
MOVFM a ndCHKNUM)t oa ctual
lyr etr
iev et het wo-bytev aluepa ss
ed.⇢ei ndirect
addressingmodei sus edt omov ethev ariableda t
ai ntoourt woda t
aby te
s.⇢e
addressof$380, $381wa sa r
bitraril
yc hos enf orthisex a
mpl e.
Itisimpor tantt onot etha tspecialc areisus edinl i
ne s25a nd2 8,
sinceinte-
gerva r
iabl esstoret het woda t
aby t
eshi gh- orderFr st
, a
sme ntionedearli
er.⇢i s
isoppos i
t etothenor mal 6502c onve ntion.
⇢i sr outi
newi llwor kequa ll
ywe llforr e t
ri
evingda tafrom simpleinteger
vari
ablesa ndfrom i ntege rarrayv ari
a bl
e s.
Whe ny our unt hise xampl e,thenumbe rs2a nd1s houldbepr i
ntedout ,
thes
ebe ingt hel ow-a ndhi gh- orderby te soft henumbe rpa ssedtother outi
ne
(258=$102) .
Re a
lVa ria
bles
Onc einassemblyl
a nguage ,
thehandli
ngofSoati
ng-pointnumbers
,suc
ha s
repres
entedbyr ealv
ari
a bl
es,issomewha ti
nvol
ved.Addi
tionall
y,t
hema j
ori
tyof
thetimey ouwi l
lbec oncerne donlywi t
hpa s
si
nga nintegerbetwe
en0a nd
65535.⇢e refore
,wewi llc
ons iderhe
rehowt ouseareal
v ar
iablet
opassanum-
berinthisrangetoag i
vens ubrouti
ne.
⇢i srevi
sionofoure a
rlierApples
o4pr ogr
am wil
ldothet r
ick
:
10 A = 258
20 CALL 768, A
30 PRINT PEEK(896), PEEK(897)
40 REM 896,897 = $380,$381
50 END

⇢ea
sse
mbl
y-l
ang
uag
epr
ogr
amf
ort
hisi
s:
1 ********************************
2 * AL17-REAL VARIABLE *
3 * READER *
4 * 2/1/82 *
5 ********************************
6 *
7 *
8 * OBJ $300
9 ORG $300
10 *
11 CHKCOM EQU $DEBE
12 FRMNUM EQU $DD67

2[
CT]Ac
tua
ll
y,t
ypi
ngas
tri
ngwi
ll
giv
ea?REENTERwa
rni
ngme
ssa
ge.
1
7.Mor
eAppl
eso4Da
taPa
ssi
ng 1
59

13 GETADR EQU $E752


14 LINNUM EQU $50
15 DATA EQU $380
16 *
0300: 20 BE DE 17 ENTRY JSR CHKCOM ; CHK SYNTAX
0303: 20 67 DD 18 JSR FRMNUM ; EVALUATE NUM
0306: 20 52 E7 19 JSR GETADR ; FAC -> INT
0309: A5 50 20 LDA LINNUM
030B: 8D 80 03 21 STA DATA
030E: A5 51 22 LDA LINNUM+1
0310: 8D 81 03 23 STA DATA+1
0313: 60 24 DONE RTS
0314: 2F 25 CHK

⇢i sisbasi
call
yar epeatofthepr evi
ouschapter’
sPoi nt
erS etuprout
ine,
wit
htheresul
tsbeingputintoDATA,DATA+1.⇢ea dvanta
g eofthisrouti
necom-
par
edtotheInteg
e rVaria
bleReaderisthatnotonlyisi
ts hort
er,butalsot
hati
t
wil
lac
c e
pteit
herinteg
erorr e
alvari
ables(s
impleorarray)ands t
il
ldot hest
ri
ng
er
rorcheck.⇢is,
then,isusual
lytheprefe
rredmethod.
Stri
ngVaria
bles
⇢egoalher
eistorea
dsomestr
ingdat
afrom t
hecal
li
ngAppl es
o4prog r
am
andthenputits
ome wherei
nme morywher
ei tpr
esuma
blywillbeava
il
a bleto
otherpor
ti
onsofthea s
sembl
y-l
anguag
eprogram.Toil
lust
rat
et hi
s,e
nte rthe
fol
lowi
ngtwoprogra
ms :
10 A$ = "TEST"
20 CALL 768, A$
30 END

1 ********************************
2 * AL17-STRING VARIABLE *
3 * READER *
4 * 2/1/82 *
5 ********************************
6 *
7 *
8 * OBJ $300
9 ORG $300
10 *
11 CHKCOM EQU $DEBE
12 FRMEVL EQU $DD7B
13 CHKSTR EQU $DD6C
14 FACMO EQU $A0
15 FACLO EQU $A1 ; FAC+5
16 VARPNT EQU $83
17 DATA EQU $380
18 *
0300: 20 BE DE 19 ENTRY JSR CHKCOM ; CHK SYNTAX
0303: 20 7B DD 20 JSR FRMEVL ; EVALUATE
21 * (FACMO,LO) -> DESCRIPTOR
0306: 20 6C DD 22 JSR CHKSTR ; VAR = $?
23 *

]
1
60 As
sembl
yLi
nes

0309: A0 00 24 LDY #$00


030B: B1 A0 25 LDA (FACMO),Y ; LEN OF $
030D: AA 26 TAX ; SAVE LEN
030E: C8 27 INY ;Y=1
030F: B1 A0 28 LDA (FACMO),Y ; ADDR LO BYTE
0311: 85 83 29 STA VARPNT
0313: C8 30 INY ;Y=2
0314: B1 A0 31 LDA (FACMO),Y ; ADDR HI BYTE
0316: 85 84 32 STA VARPNT+1
0318: 8A 33 TXA ; RETRIEVE LEN
0319: A8 34 TAY
35 *
031A: 88 36 LOOP DEY
031B: B1 83 37 LDA (VARPNT),Y ; GET CHR
031D: 99 80 03 38 STA DATA,Y
0320: C0 00 39 CPY #$00
0322: D0 F6 40 BNE LOOP
41 *
0324: 60 42 DONE RTS
0325: 4F 43 CHK

A4e
rrunni
ngtheca
ll
ingpr
ogr
am,e
nte
rtheMoni
tora
ndl
is
toutt
heDATA
r
egi
onofmemorywit
h:
*380.383 <RETURN>

⇢i
sshoul
dpr
intoutt
hef
oll
owi
ngda
ta:
0380- 54 45 53 54

⇢i sshowst ha tthehe xvalue sforthec hara cters“TEST”h a v


ebe ens uccess-
ful
lyt ransferred.Le t’
ss e
ehowi twa sa c
compl ishe d.
⇢er outines tartsoLr at
herl ikethepr eviousone sbyus ingCHKCOMt oma ke
sureac ommawa sus eda 4e rtheCALLa ndt opr epa r
eTXTPTRf orreadingint he
data .FRMEVL ( $DD78 =F oRMul aEVa Lua t
ion)i sav er
yni ceg ene r
a l
-purpose
routinet ha ttakesi nv irtuallya nynume r
icors tringe xpressionorl i
teral
,a nd
placest heFna lresultinFAC.I ti srelat
edt oFRMNUMbuti smuc hmor eomni v
o-
rous.Upon r et
ur ning f rom FRMEVL,FACMO a ndFACLO ( $A0,$A1 = “ .
..
sorry,
couldn’ tFndoutwhe r
et heyg ott hena me s
...
” 3
)hol dthea ddr essoft hes t
ring’
s
descriptor,tha tis,t het hree-byteg roupg i
v i
ngt hel engtha nda ddressoft he
actual st
ringda ta.
Ourr outi
neus e sFACMO,FACLOi nthei ndire ctaddre ss
ingmodet oretrie
ve
theFr stby teoft hede sc
r i
ptor,whi c
hi sthel eng thoft hes t
ring .⇢i sisputi nto
theX- Registerfort empor a
rys torage.S omepe oplepr e
fe rtopus hi tontot he
sta
c kwi thaPHAc omma nd; it’
sama tterofc hoice .Ne x
t,thea ddr essofthes t
r i
ng
dataisr etriev
e dfrom t hede s
criptora ndputi ntoVARPNT, whi chi sa s
sume dtobe

3[
CT]FACMO a
ndFACLOa
ret
heMi
ddl
e-Or
dera
ndLow-
Orde
rby
tesoft
hef
our
-by
te
ma
nti
ssaf
ort
heSoa
ting
-poi
ntAc
cumul
at
or.
1
7.Mor
eAppl
eso4Da
taPa
ssi
ng 1
61

notinuseattheti
me .Lastofa l
l,wec opyt hedat
afrom itsl
ocati
on,i
ndi c
atedby
theVARPNTpoint
er,toourDATAa ddr e
s s
.
Inexper
imenting,noticethatt hea re
af r
om $380 to$3CFisope n,butthat
theareasta
rti
nga t$3D0i sreser
v edf orDOS .Enteri
ngv er
ylongs t
ringsinthe
examplema ylea
dt osomepr obl
e ms .Inyourownpr ograms,itwouldbene ces-
sarytosetasi
deaone -
pag ea r
ea( $100=2 56by t
es)toputt hedata,unle
ssof
courseyoucanli
mi tt
heleng t
hoft hes tri
ngbe f
oredoingthecall
.
Youma ya l
sowi s
ht ot r
yv ariati
onsi ntheAppl es
o4pr ogram byde l
eti
ng
l
ine1 0andrewri
tingli
ne2 0a s:
20 CALL 768, "ABC" + "DEF"

or
20 CALL 768, LEFT$("ABCDEF")

or
10 A$(5,5) = "TEST"
20 CALL 768, A$(5,5)

Pa
ssi
ngDa
taf
rom As
sembl
yLa
ngua
ge
⇢ec onverseofthetec
hniqueswe’v
ediscussedsof a
ractual
lyisfair
lysim-
pl
e.⇢ek e ytomuc hofiti
sthePTRGETrouti
neus edearli
er.
Be c
ausethisrouti
ne
wi
llev
e nc re
a t
eav a
riabl
ewhe nit
’snotal
readypr e
sent,wecans i
mpl ymor eor
l
essr
everset heprocessoftheprevi
ousroutinestopassda t
aba c
kt oac a
ll
ing
Appl
eso4pr ogram.
Again,I’l
lil
lus
trat
eane xa
mpleforeachva r
iabl
etype.
I
nte
gerVa
ria
bles
⇢eAppl
eso4pr
ogr
a 4
m:
10 POKE 896,2: POKE 897,1
20 CALL 768, A%
30 PRINT A%
40 END

4[
CT]Fora
nint
era
cti
vepr
ogr
am,
repl
acel
ine1
0wi
th:
5 PRINT CHR$ (4); "BLOAD AL17.SENDINT"
6 INPUT "ENTER INTEGER LO,HI BYTES: "; A%,B%
10 POKE 896,A%: POKE 897,B%
Tr
yent
eri
ng“
2,1
”,“
255
,12
7”,
or“
1,1
28”
.Nowt
ry“
0,1
28”
.Ist
hisal
ega
lint
ege
rva
lue
?

]
1
62 As
sembl
yLi
nes

⇢ea
sse
mbl
ysubr
out
inet
obec
all
edi
s:
1 ********************************
2 * AL17-INTEGER VARIABLE *
3 * SENDER *
4 * 2/1/82 *
5 ********************************
6 *
7 *
8 * OBJ $300
9 ORG $300
10 *
11 CHKCOM EQU $DEBE
12 PTRGET EQU $DFE3
13 VARPNT EQU $83
14 MOVFM EQU $EAF9
15 CHKNUM EQU $DD6A
16 DATA EQU $380
17 *
0300: 20 BE DE 18 ENTRY JSR CHKCOM ; CHK SYNTAX
0303: 20 E3 DF 19 JSR PTRGET ; FIND VARIABLE
20 * Y,A = ADDRESS OF VALUE
0306: 20 F9 EA 21 JSR MOVFM ; MOV VAL -> FAC
0309: 20 6A DD 22 JSR CHKNUM ; FAC = NUM?
030C: A0 00 23 LDY #$00
030E: AD 81 03 24 LDA DATA+1
0311: 91 83 25 STA (VARPNT),Y
0313: C8 26 INY
0314: AD 80 03 27 LDA DATA
0317: 91 83 28 STA (VARPNT),Y
29 *
30 * NOTE! HIGH BYTE FIRST!
31 *
0319: 60 32 DONE RTS
031A: F1 33 CHK

⇢isprogramisarat
hertr
ivi
alexer
cis
einthatal
lthatnee
dstobedonei
sto
re
versetheoperandsofl
ines24,25and2 7,28from theFrs
tInte
gerVari
abl
e
Readerprogr
am. Agai
n,t
heonlycaut
ionistomakesurethatt
hebytesa
ret
rans
-
f
erredintheproperor
der
,si
nceinteg
erdataisr
evers
ed.
Real
Va ri
abl
es
Realvar
iabl
esre
quir
etheint
roduc
ti
onofaf e
w ne
wr outi
nes
.⇢es
ame
Appl
eso4cal
li
ngprogr
amisus
edwithonl
yaminormodi
Fca
tion.
10 POKE 896,2: POKE 897,1
20 CALL 768,A
30 PRINT A
40 END
1
7.Mor
eAppl
eso4Da
taPa
ssi
ng 1
63

⇢es
ubr
out
inei
sent
ere
das
:
1 ********************************
2 * AL17-REAL VARIABLE *
3 * SENDER *
4 * 2/1/82 *
5 ********************************
6 *
7 *
8 * OBJ $300
9 ORG $300
10 *
11 CHKCOM EQU $DEBE
12 PTRGET EQU $DFE3
13 CHKNUM EQU $DD6A
14 GIVAYF EQU $E2F2
15 MOVMF EQU $EB2B
16 DATA EQU $380
17 *
0300: 20 BE DE 18 ENTRY JSR CHKCOM ; CHK SYNTAX
0303: AD 80 03 19 LDY DATA
0306: AC 81 03 20 LDA DATA+1
0309: 20 F2 E2 21 JSR GIVAYF ; DATA -> FAC
030C: 20 E3 DF 22 JSR PTRGET ; FIND VARIABLE
030F: 20 6A DD 23 JSR CHKNUM ; VAR = NUM?
24 * Y,A = ADDRESS OF VARIABLE DATA
0312: AA 25 TAX
0313: 20 2B EB 26 JSR MOVMF ; FAC -> MEMORY
0316: 60 27 DONE RTS
0317: D1 28 CHK

⇢et e chniquehe rei stous et her outineGIVAYF( $E2F2=GI VeAc cumul a


-
tora ndY- Registert oFAC)t oputt het woby tesofouri ntegernumbe rintothe
FAC.GIVAYFr equi restha ttheAc cumul at
ora ndY- Reg i
st
erbel oadedwi ththe
high-a ndl ow-or de rby t
e s
,r espective ly,fort heintegernumbe rtobetransferr
ed5.
Asabonus ,thenumbe rma ye venbes igne d–t hatis,posit
iveornegativ
e.Signed
binar ynumbe r
swe r ecov er
e di ng rea tde tailinchapter1 0.
Lines1 9a nd2 0l oa dt hea ppr opr iater egis
ter
s,then,a 4ercall
ingGIVAYF,
PTRGET a ndCHKNUMa reus edt ode te rmi net hena meoft hev ar
iabl
et ous ein
returningt heda ta .Re calltha ta4e rr eturningf r
om PTRGET,t heY-Re gi
sterand
Ac cumul atorwi llhol dt hel ow-a ndhi gh-orde rbytesofthea ddres
soft heda t
a
forthene wv ari
abl edi geste
dbyPTRGET.
MOVMF ($EB2B =MOVet oMe mor yf rom FAC)i sther outi
newe ’l
lus eto
compl et
et hepr oc ess.Itr equirest ha tt heY- Re gi
st
era ndX- Regist
erbel oaded
witht hea ddressoft heme mor yloc ationt owhi chthec onte
ntsoftheFACwi l
lbe
mov e d.SincePTRGETha sjus tdete rmi ne dt hatforus, t
heonl yhitchisthatPTR-
GETl e4t hehi gh- orde rby t
ei nt heAc c umul atorinsteadofi ntheX- Regis
teras

5[
CT]⇢eor
igi
nala
rti
cl
eswi
tc
hedt
heme
ani
ngoft
heGIVAYFhi
gha
ndl
owby
tes
.⇢e
c
odea
bov
eha
sbe
enc
orr
ect
eda
ndpr
oduc
esav
alueof2
58.

]
1
64 As
sembl
yLi
nes

wer
equi
re.
As i
mpleTAXsol
vest
hatpr
obl
em,
andt
her
out
inei
sconc
lude
dwi
th
t
heca
llt
oMOVMFandanRTS.

Pr
ogr
ammi
ngTi
p
Wheneverar outi
nee ndswi t
haJSRt oanotherr outi
ne,imme diat
el
yf ol
-
l
owe dbytheendingRTSoft hemainr
outine
,theli
nec anbes hortenedoneby t
e
byc ha
ngingthel
a stJSRtoaJMP.Whe ntheRTSinthel a
stcall
eds ubrouti
neis
encounte
red,
theRTSwi llc
a useanexi
tfr
om thema i
nr outineinst
ead. Ane xa
m-
pleofthi
swouldbet orewrit
et hee
ndoftheprogr
amj us tl
ist
eda s
:
*
*
*
030F: 20 6A DD 23 JSR CHKNUM ; VAR = NUM?
24 * Y,A = ADDRESS OF VARIABLE DATA
0312: AA 25 TAX
0313: 4C 2B EB 26 DONE JMP MOVMF ; FAC -> MEMORY
AND RETURN!

Stri
ngVa r
iables
S tri
ngv ariablesa renotmuc hdiLerentbutwi l
lrequireas l
ightlyclums y
call
ingAppl eso4pr ogramt odemons tr
ate.Line10isas e
riesofPOKEst hatwi l
l
putt heAS CI Ida taf orthestr
ing“TEST”i nt
ome mor yatourus ualDATA($380)
l
oc ation.Addi ti
ona lly,adeli
mi t
erwillbeplacedattheendoft hes t
ringsot hat
ther outineswewi llbec al
li
ngc andeterminethest
r i
ng’
slength.Useofade l
im-
ite
rismor epr actic
a l
, es
pecia
ll
yins it
uati
onswhe r
ey oudon’tknowt heleng t
hof
ani nc omings tringunt ilthecarri
ager et
urnorot herdel
imitershowsup.⇢e
Appl eso4r outinewe ’l
lusewillautomatical
lydet
erminetheleng t
hbys c
anni ng
thest ri
ngfort hede limiter
.
10 POKE 896,84: POKE 897,69: POKE 898,83: POKE 899,84: POKE 900,0
20 REM "TEST" + NULL DELIMITER
30 CALL 768, A$
40 PRINT A$
50 END

⇢es
ubr
out
inef
ort
hisi
s:
1 ********************************
2 * AL17-STR$ VARIABLE SENDER *
3 * 2/1/82 *
4 * R. WAGNER *
5 ********************************
6 *
7 *
8 * OBJ $300
9 ORG $300
10 *
11 CHKCOM EQU $DEBE
1
7.Mor
eAppl
eso4Da
taPa
ssi
ng 1
65

12 PTRGET EQU $DFE3


13 CHKSTR EQU $DD6C
14 FORPNT EQU $85
15 MAKSTR EQU $E3E9
16 SAVD EQU $DA9A
17 DATA EQU $380
18 *
19 *
0300: 20 BE DE 20 ENTRY JSR CHKCOM ; CHK SYNTAX
0303: 20 E3 DF 21 JSR PTRGET ; FIND VAR
0306: 20 6C DD 22 JSR CHKSTR ; VAR = $?
0309: 85 85 23 STA FORPNT
030B: 84 86 24 STY FORPNT+1 ; ADDR OF DESCR
030D: A9 80 25 LDA #$80
030F: A0 03 26 LDY #$03 ; A,Y = $380
0311: A2 00 27 LDX #$00 ; DELIMITER='00'
0313: 20 E9 E3 28 JSR MAKSTR ; DATA -> MEMORY
0316: 20 9A DA 29 JSR SAVD ; VARPNT = NEW $
0319: 60 30 DONE RTS
031A: CD 31 CHK

⇢ene wr outineshe r
ea reMAKSTR ($E3E9 = MAKeS TRing)a ndSAVD
($DA9A=S AVeDe s
c r
iptor).MAKSTRr equi
resthattheAc cumulatora ndt heY-
Re g
isterholdt hea ddress(low, high)ofthestri
ngtobes canneda ndtha ttheX-
Re g
isterhol dthev aluefort hede li
miti
ngc haract
er.⇢i sexampleus es$00,but
anothe rcommonv ariat
ionwoul dbet ouseac arri
agereturn($8D)o rac omma
($2C).( Notet ha t<RETURN>i sa lmostalwaysfoundi ntheinputbuLe rwi tht he
highbi tset,t
ha tis,$8Dv ersus$0D) .
A4e rscanni ngf ort hede limiter
,MAKSTRmo vestheda t
aupt ot hes t
ring
stora
g ea re
aa tthet opofme mor y.
SAVDi sac ompa nionr outinewhi chwi l
ltakewha te
verstri
ngde sc r
iptoris
currentlypoi ntedt obyFORPNT( $85,$86=F ORmul aPoiNTer)andma tc
hi tto
theda tajustmov edbyMAKSTR.
Look inga tt helisti
ng ,wec ans eethattheonlyc r
eati
vewor ktobedonei s
mov ingt hec ont entsoft heAc cumulatorandY- Regist
ertoFORPNT.⇢eAc cu-
mul a
t or,Y-Re gister,andX- Re gis
ter
sa rethenpr e
pa r
eda swa sjustde scri
bed,
andther ema i
ningc all
saredone .Voil
a!Insta
ntstri
ngs!

Conc
lus
ion
You’l
lnoticethata
lloftheroutineshandl
ea r
ra y
saswe l
lassimplev ari
ables
.
Addi t
ional
ly,cert
ainmores ubtl
epoi ntsbec
omea ppar
entasy oustudyt helist
-
ings.Forexa mple,eac
hoft helastthreeAppleso4l i
st
ingswa sdonewi thout
deFningther eturnedvari
ablepriort otheCALL.⇢i swa stode mons tr
atethat
PTRGETd oesav erynic
ejobofc r
eatingthevari
ableforus.Ina ddi
tion,ine ach
casetheda t
at hatwasputint oav aria
bleandthenl at
erret
rievedatDATA( and
vic
ev ersa
)shoul dbeconsis
t ent
,thusde monstr
atingtheaccuracyoft heme th-
ods.

]
1
66 As
sembl
yLi
nes

Youma yals
owi shtoexpe ri
me ntwi thusi
ngf ormula
sors t
ringcalc
ulati
ons
a
4ertheCALLstat
eme nttoconFrmt ha talll
egalApples
o4ope ra
tionsareaccept-
a
ble
.
Lastbutnotle
a s
t,Iwoul dliketog i
vecredi
ta ndthankstoCr ai
gPe t
e r
son
f
orhishel
pinprovidingsomeoft heinf ormati
onus edinpre
pa r
ingthischapter
.
Nextchapt
erwe ’
lll
ooka tsomeot he rappl
icat
ionsofint
ernalAppleso4rou-
t
ine
swi t
hincust
om assembly-l
angua gepr ogr
ams .
1
8
1
8. Ap
ple
so(Hi
-Re
sGr
aphi
cs
Ma
rch1
982

Inthepr e
v iousc ha pterwee xami nedt het e
chni que sforpa ssi
ngda taba ck
andf orthbe twe enAppl eso4a nda ss
e mblyl angua gei nt hef or m ofs tanda rd
Appl e
so4v a ri
a bles.⇢i swa sg reatlyfacili
tatedbyt heus eofe xis
tingi nternal
Appl e
so4 rout ine s.A na turale xtensionoft hisi de aist ous eot heri nternal
Appl e
so4r out ine sa sma ybea ppr opriatet oourg ivent ask.Oneoft hemos t
int
e r
esti
nga ppl icationsoft hisisi nthea reaofhi -r
e sg raphics
.
⇢e rea ret woma inr easonsf ordoi nghi -resg raphi csfrom a ssembl yl an-
guage.⇢eFr sta ndmos tobv iousi ss peed.Bydoi ngma nyoft heope rations
direct
lyina ssembl yl ang ua ge,theba s
icov erhe ad( sot os peak)ofAppl eso4i s
avoided,thuspr oduc i
nganot i
ce ablespee di ncreasei nt heov era l
lpr ogram.Be
awa r
ehowe ver,t ha tsinc ewea reul t
ima tel
ys t
illca l
lingAppl eso4r outine s,the
speedincreaseha sac er tainlimi t.Gr e
a terspe edsa reobt ai
nedonl ybyc reating
special
ize
da ndde dicate dr outine stha tpe rform onl yas peciFcf unc t
ion.⇢e
norma lAppl eso4r out i
ne sa rede signedt obeSe xi
bl ea ndt ooc cupyami ni mum
ofspa c
e.Fa ste
rr out i
ne swi l
ldol essandpos siblybel argerint er msofme mor y
use.⇢et rade -
oLmus tbewe ighed.
⇢es econdr e asoni ssimpl yt hec onv enienc eofbe inga blet odot hes ame
thi
ng s,i
ncludingg raphic s,from a ssembl ylang ua get haty oua rea bl
et odof rom
BAS IC.Tot hise nd,t het echnique spr esente dint hisc hapters houl dbequi t
e
adequate.Inf ut ur ec ha pters,we ’llexploret hec reationofs pec ial
izedr outines
thatgivehighers pe e da ndg r
e at
e rindepe nde ncef rom t heAppl e so4r outi
ne s.

Gr
oundSc
hool
Beforej umpi ngi nt
ot hei nt ri
c atede tai
lsoft hev a
riousr outi
nes,we’l
l
impos eupony ourpa ti
encelonge noug htodescribebr i
eSythemode lofApple
hi-
resg raphicsus edf orthecur r
e ntdi scussi
on.⇢i sma yseem unne c
essar
y,but
itwillprovidet hec ommong roundf ort hepointst obema dei nthispart
icul
ar
pres
e ntat
ion. Asy ou’llseeinlaterc ha pters
,therea reanumbe rofwa ysoflook-
ingatthet otalscreene nvir
onme nt.
Forthet imebe ing,we’l
lus ea na ppr oximati
onofwha tisus ual
lyvie
we das
the“class
ical”ex pl
ana ti
onoft hehi -ress cre
eninFi gur e1
8-1.
⇢ea rrayofpos si
blepointstobepl ott
edconsistsofaFe l
dof1 92lines
,each
ofwhi chi sma deupof2 80poi nts.I fami xedmodeofg ra
phi c
splustextis

]
1
68 As
sembl
yLi
nes

Fi
gur
e18-
1:Hi
-re
ssc
ree
ncoor
dina
tes

s
elec
ted,only160graphic
sli
nesaredis
played.Onthemajori
tyofAppl
es,s
ix
c
olorsareavai
la
ble
:blac
k,whi
te,g
reen,
violet
,ora
nge,a
ndblue.
⇢e secolor
shavebeenas
si
gnedtoeightnumeri
cval
ues
, a
sfol
lows
:

Se
t1 Se
t2
0=Bl
a c
k1 4=Bl
ack2
1=Green 5=Orang
e
2=Viol
et 6=Bl
ue
3=White1 7=White
2

Whi teisc reat


edbypl ott
ingt woc ol
orpoi ntsr ig
htne xttoe a chot her
(green/vi
ole
toror ang e
/ blue).Black,whe ns peci
Fc all
ypl ott
ed,ispr oducedby
turningoLtwoa dj
ace ntc olordots.
⇢emode lget
ss hak ywhe nweha vetotellyout hatthingsli
ke“ oddc olors”
(greenoror ange)canbepl ottedonlya toddx -
coor dinat
e s(1,3,5..
.),andt hat
"evencolor”(
s bl
ueorv iolet)canbepl ottedonlya tevenx -coordi
na t
es( 0,2
,4 ..
.
).
Itgetsevenwor se,butwe ’
llsavethehor rorstor
ie sforcha pte
r2 0.Fort hetime
be i
ng,you’l
lha v
ema nyf e werhe a
dac hesifyoulimi tyourselftousingt hecolors
from onlySet1orS e t2. Ev enbett
e r
,s t
icktoblacka ndwhi t
efornow, andfewe r
my ster
iousthingswillha ppe n.

La
ndma
rksa
ndEnt
ryPoi
nts
Anumberoft
hef
unda me
nta
lhi
-re
srout
ineent
rypoi
ntsa
redoc
umente
din
v
ari
ouspubl
ic
ati
onsr
ela
tingt
otheAppl
e.Abrie
fsumma r
yisgi
venint
hefol
-
l
owi
ngtabl
e.
1
8.Appl
eso4Hi
-Re
sGr
aphi
cs 1
69

Rout
ine Addre s
s Des cr
iption
HGR $F3E2 Ini
tial
izestohi -re
spa ge1 ,cl
earsscr een.
HGR2 $F3D8 Ini
tial
izestohi -re
spa ge2 ,cl
earsscr een.
HCLR $F3F2 Clearscurre nts cr
eentobl ack1 .
BKGND $F3F6 Clearscurre nts cr
eentol astplottedHCOLOR.
HCOLOR $F6F0 Set
sHCOLORt oc ontent
sofX- Re gi
ster( 0
77 ).
HPOSN $F411 Positi
onshi -res“ c
ur s
or ”withoutpl otti
ng .Ent erwi th
X,Y( l
ow,hi gh)=hor izontalpos i
tion,Ac cumul at
or=
vert
icalpos i
t i
on.
HPLOT $F457 Identi
c altoHPOSN,butpl otsc urrentHCOLORa tc oordi-
natesgiven.
HFIND $F5CB Returnsc ur rent“ curs
or ”pos iti
on.Us ef
ula 4eraDRAW
toFndwhe rey ou’vebee nle4. Coor dinate
sr e t
urnedi n:
$E0,$E1=h orizontal(l
ow, high),$E2=v ertical.
HLIN $F53A Drawsal inef rom l as
tpl ottopoi ntg i
ven.Ac cumul a-
tor
, X( l
ow, hig h)=hor izontal,Y=v e
r t
ica
l positi
on.
SHNUM $F730 Putsa ddressofs hapenumbe rindi cate
dbyX- Re gi
ster
into$1A,$1B;r et
urnswi thX,Y( low,hi gh)a ls
os ett o
addressoft hats hape-t
a bleentry.
DRAW $F601 Draws hapepoi ntedtobyX,Y( l
ow,hi gh)i nc urr
e nt
HCOLOR.No te:X,Y poi ntt os pe ciFce nt ry
,no tthe
beginningoft het able.Ca l
lSHNUMFr st.
XDRAW $F65D Erasesshapej us tdrawn( i
fther e
)bydoi nga ne xc l
usive
ORwi tht hes cree
nda ta.Loa dX,Y( low,hi gh)wi th
addressofs ha pet oXDRAW orc al
l SHNUM F rstwi t
hX-
Register=s ha penumbe r.

ATe
stFl
ight
:Hi
-Re
sDe
mo
Toil
lus
tra
tehowt
hes
ear
eac
tua
ll
yputt
ous
e,a
sse
mbl
eandr
unt
hef
oll
ow-
i
ngprog
ram:
1 ********************************
2 * AL18-HIRES DEMO 1 *
3 ********************************
4 *
5 *
6 * OBJ $6000
7 ORG $6000
8 *
9 PREAD EQU $FB1E
10 WAIT EQU $FCA8
11 PB0 EQU $C061
12 HCOLOR EQU $F6F0
13 HGR EQU $F3E2

]
1
70 As
sembl
yLi
nes

14 HPLOT EQU $F457


15 HPOSN EQU $F411
16 HLIN EQU $F53A
17 ROT EQU $F9
18 SCALE EQU $E7
19 SHNUM EQU $F730
20 DRAW EQU $F601
21 PTR EQU $E8
22 *
6000: 4C 11 60 23 ENTRY JMP E2
6003: 01 00 04 24 TABLE HEX 010004
6006: 00 12 3F 25 HEX 00123F
6009: 20 64 2D 26 HEX 20642D
600C: 15 36 1E 27 HEX 15361E
600F: 07 00 28 HEX 0700
29 *
6011: 20 E2 F3 30 E2 JSR HGR ; CLR SCRN
6014: A2 03 31 LDX #$03 ; WHITE = 3
6016: 20 F0 F6 32 JSR HCOLOR
33 *
6019: A9 00 34 BORDER LDA #$00 ;Y=0
601B: A8 35 TAY
601C: AA 36 TAX ;X=0
601D: 20 57 F4 37 JSR HPLOT ; PLOT 0,0
6020: A9 17 38 LDA #$17 ;
6022: A2 01 39 LDX #$01 ; X = $117
6024: 20 3A F5 40 JSR HLIN ; HLIN TO 279,0
41 *
42 *
6027: A9 17 43 LDA #$17
6029: A2 01 44 LDX #$01 ; X = 279
602B: A0 9F 45 LDY #$9F ; Y = 159
602D: 20 3A F5 46 JSR HLIN ; HLIN TO 279,159
47 *
6030: A9 00 48 LDA #$00
6032: A2 00 49 LDX #$00 ;X=0
6034: A0 9F 50 LDY #$9F ; Y = 159
6036: 20 3A F5 51 JSR HLIN ; HLIN TO 0,159
52 *
6039: A9 00 53 LDA #$00
603B: A2 00 54 LDX #$00 ;X=0
603D: A0 00 55 LDY #$00 ;Y=0
603F: 20 3A F5 56 JSR HLIN ; HLIN TO 0,0
57 *
6042: A9 03 58 SET LDA #$03
6044: 85 E8 59 STA PTR
6046: A9 60 60 LDA #$60
6048: 85 E9 61 STA PTR+1 ; SET TABLE TO $6003
62 *
604A: A2 00 63 READ LDX #$00 ; PDL(0)
604C: 20 1E FB 64 JSR PREAD
604F: 98 65 TYA
6050: D0 02 66 BNE R1
6052: A9 01 67 LDA #$01 ; FIX 0 -> 1
6054: 85 E7 68 R1 STA SCALE
6056: A9 18 69 LDA #$18
1
8.Appl
eso4Hi
-Re
sGr
aphi
cs 1
71

6058: 20 A8 FC 70 JSR WAIT


605B: A2 01 71 LDX #$01 ; PDL(1)
605D: 20 1E FB 72 JSR PREAD
6060: 84 F9 73 STY ROT
6062: A9 18 74 LDA #$18
6064: 20 A8 FC 75 JSR WAIT
76 *
6067: A2 8B 77 DSPLY LDX #$8B
6069: A0 00 78 LDY #$00 ; X = 139
606B: A9 4F 79 LDA #$4F ; Y = 79
606D: 20 11 F4 80 JSR HPOSN
6070: A2 01 81 LDX #$01 ; SHAPE #1
6072: 20 30 F7 82 JSR SHNUM ; FIND SHP ADDR
6075: A5 F9 83 LDA ROT
6077: 20 05 F6 84 JSR DRAW+4 ; USE SHNUM ENTRY PT
85 *
607A: AD 61 C0 86 CHK LDA PB0
607D: 30 92 87 BMI E2 ; BUTTON PUSHED
607F: 10 C9 88 BPL READ ; NO PUSH
89 *
6081: F0 90 CHK

Whe nr un,t hisr outinewi lldr aw abor dera roundt hehi -re ss creen,a nd
the ndr a wi nt hec enteroft hes creent hes ha pede Fne dbyt het able.S ca l
ea nd
rota t
ionv aluesma ybec hang edbya djus t
ingpa ddle s0a nd1 ,respe ctivel
y .Pus h-
ingbut ton0wi l
l r
e -
c l
earthehi -ressc reenoft hea ccumul at
edi ma ges.
⇢er out i
nes tartswi thaj umpov erada tatabl etoE2.⇢et a blei sas impl e
sha pet ablet akenf rom pa ge9 5oft heAp pl
eso ftIIBAS ICProgr ammi ngRe f
e rence
Ma nua l.Iti sat ablet odr aws ome thingr e sembl ingas quare.⇢et ablec ould
ha vebe enputa tthebe g i
nni ngoft her outine ,buti twouldnott he nha vebe en
ablet obeBRUN.
Line3 0c learsa nddi spla yst hehi -
r esdi splaypa ge(page1 );line s3 1,3 2us e
HCOLORt os etthec olortobeus edt owhi te1.
A bor deri sthe ndr awni nl ine s3 4 750 .HPLOT( l
ine37)i sus edt opl ott he
startingpoi nt( ar equireme ntf ors ubs eque ntus eofHLIN,unl essHPOSNi sus ed
fora“ no- plot”).
Line sa redr awnbe twe ent hef ourc orne rpoi ntsoft hemi xed- modedi spl ay.
Se eFi gure1 8-
1t oc onFr mt hec oordi nate s
.
Onc et hebor de risdone ,pr epa rati
oni sma det ous et
hes ha pet abl
e .Loc a
-
tions$E8,$E9a reus e
dbyAppl es
o4t opoi ntt ot hebe gi
nningofas hapet a ble.
SETi nitializesthispoi ntert ooure xampl eta blea t$6003. ⇢et abl ene ednotbe
pa rtoft hea ct
ua lcode ,howe v er,andc oul dha vebe enl ocat
edv irtua l
lya ny whe r
e
inme mor y .(Obv i
ouse xcept ionswoul dbet hehi -respa g
ea rea,$20007$3FFF,
andot he rre se
rv eds ys
t e
ma r eas .)
READl oadst heX- Re giste rwi th0t ot ellPREADt ha twewa nttor eadpa ddl e0
andt he nput st her esults( foundi nt heY- Re gist
e r)intotheSCALEpa rame te
r
loca t
ion( $E7). Line6 6t estsf oraSCALEv al
ueof0 .Be causeAppl e so4t r
ea ts0a s

]
1
72 As
sembl
yLi
nes

thel arg estsca l


e, thisiss hi4edba ckt o1t oma ket hepa ddlesmor eus ablef rom a
huma ns tandpoi nt .
Line s6 9,7 0us etheWAITr outinet owa itar a thera rbitrarya mountoft ime
befor er e adingpa ddle1 .⇢ev alue#$18wa sus eda sthede layv a l
uef orv ery
uns cient i
Fcr easons .⇢el ar
g erthev alue ,themor ea ccura t
et hes ubs eque ntpa d-
dler e ading s,butt hepa ddleswi l
ls eeml e ssr espons ive.S horterde l
ay sg ivef ast
paddl er e spons e,butl essaccur acy .⇢i se Le ctisduet ot hef acttha ttheMoni tor
readst hepa ddl esbyme asuringt het imei tt akestoc hargeac apa ci
torwi thint he
system.⇢ehi ghe rt hepa ddles etting,t hel ong eri ttak es.⇢es amec apa citori s
usedf ora llpa ddle s.Whe ntwoormor epa ddl esa rerea dinr apids uc cession, t
he
capa citordoe snotha vet imet or eturnt oi ts0v al
uebe foret hene xtr eads t
a rts
,
andhe nceaf alsev a l
uei sr e
turne d. ⇢ede laya ll
owst hes ys t
emt oma keabe t
ter
returnt ot hede sire ds t
at e
s.
⇢ei nteractionbe twe ent het wopa ddl esi smos tappa r e
ntwhe npa ddl e1i s
setto2 5 5( f
ull ri
g ht ).
Whe npa ddl e0i st heni ncrea sedf r
om 0 ,thes qua rei ss e
e n
tor ota te,ast hes c alepa rame terisi ncre ased. ⇢i sdoe snotha ppe nwhe npa ddle
1isa tal ows etting .Onet echni quef ormi nimi zingpa ddlei nteractioni st or e
a d
thes a mepa ddl et wi cewhe ng ettingar eadi ng( aswes awi nc ha pter1 2 )
.I fl i
ne
64a nd7 2we redupl icatedi nt hel ist
ing ,t her esultwoul dbemor es table .Tr y
alt
e ringt hel isti
nga ndr eassembl ingwi tht hene wt echnique .You’ llFndt hedi s-
tortionofpa ddle0muc hl esspr onounc edt hanbe fore.
⇢ eDSPLYs ections et
st hec oor dina test odr awt hes hapea t$8B,$4F( 13 9
,
79). Itthe nc a l
lsHPOSNt opos it
iont hei ma ginaryhi - r
esc ursora ttha tpoi ntwi th-
outa ctua l
lypl ottingapoi nt.SHNUMi st he nc alled, whi chFndst hea ddre ssoft he
Frsts hape -tablee nt r
y. SHNUMr eturnswi tht heX-a ndY- Registershol di ngt he
l
ow-a ndhi g h-or de rby tesfort hee ntry .⇢eAc c umul a
tori st henl oa de dwi th
theROTa t
ionv alue ,andDRAWc alled.1

Be forer epea tingt hec y cl


e ,pus hbut ton0i sc hec kedf orabut t
onpr e
s s
,
whi chi ndi catest heus erwa ntst oc learthehi -ress creen.

AMi
norDi
ver
sion
Hig h-
r e
sol
utiongraphi csaregeneral
lyus e
df ort
woma i
npur poses
.⇢eFr s
t
isthepr es
entat
ionofg raphicaldata,s
uc hass a
leschart
sande quati
ons.⇢er ou-
tinespresente
dhe rearea dequateforthat,butov er
allt
hetaskisprobablybet
ter
donedi rectl
yinAppl eso4a ny way.Appl e
so4i so4e ngiv
enawor sereputa
tion
thani tdeserves
.I ti
squi t
ev ersat
il
ea nd,whe nc ombinedwi tha s
sembly-l
an-
guag esubrouti
nes,ca
npe rf
or m qui
teadmi rabl
y.
1[
CT]Li
ne8
4wa
scor
rec
tedi
ntheJ
uly1
982S
ofta
lk:⇢eDRAWr
out
ine(
$F601)i
snor
-
ma l
lycal
ledwitht
heX-a ndY-Regis
ter
ss e
ttotheaddr e
ssoftheindi
viduals
hapet
obe
drawn.⇢i scanbeautomate
d,however,byFrstc
a l
li
ngSHNUM( $F730)
.Whe nSHNUMis
call
ed,however
,alate
re nt
rypointtoDRAWisuse
d.S peciFc
all
y,thi
sshouldbeDRAW+4
($F605).Ent
eri
ngat$F601bymistakecanproduceratherunpre
dict
abl
er e
sult
s.
1
8.Appl
eso4Hi
-Re
sGr
aphi
cs 1
73

⇢eot herma inar eaofconc e


rni sthepr oducti
onofs cr
eena nimati
on, asis
commonl yse e
ni na r
c ade-ty
peg ame s.⇢i sa re
abr ingsups omene wr equire-
mentsinoure xpe
rtis
e ,becausedepi c
tionsofmot ionont hes c
reena r
ere al
lya
matterofcreat
ingac omput ersimul at
ionofmot i
on, usi
ngt helawsofphy si
csto
mimict herealworld.( Nexttimes ome bodybug sy oua boutwr it
ingorpl ayi
ng
games,jus
ttellt
hemy ou’rebusydoi ngc omput ersimulations.
)
I
twoul dbei mpos sibl
etopr es
e ntma nymor eideasi nthea r
eaofg raphics
wit
houtr el
yingona nunde rly
ingunde rstandingofs omeoft heprinci
plesus e
d
i
nc reat
ingas imulati
onpr ogram.Al thoug hwe ’l
lc ert
ainlynott r
yt opresenta
compr e
hensivetut
orial onbasicphy si
csa ndc omput ergraphics,wec a
ng e
tqui te
abitofmileageoutofoneort wor athers i
mpl econc ept
s.

Loc
ati
on
Its houldbef ai
rlyobv ioust ha twhe nspeci
fyi
ngthecoor
dinat
e sofapoi
nt
ont hes c
r e
e n,wea r
eg ivingi nfor mationabouttherel
ati
veloca
tionofsome-
thi
ng .Aboutt heonl yt hingdi Le renta bouttheApplesc
ree
nisthatthenumber
syst
em us edi slaidouts ome wha tdiLer-
entl
yf rom t heCa rtesi
a ns ystem de scri
be d
injuniorhi ghs chool ma thc lass es.
Int heus ual s
y s
tem, thepoi ntwi t
h
thecoor dina te
s0 ,0(theor i
g in)i sa tt
he
centeroft hedi spl
a y
,a nda llpos sible
combi na t
ionsofpos i
tivea ndne ga t
ive
numbe rsa r
es howni nt hef ourqua drants.
⇢i sismor et hanwene edt odo
Appleg raphicsthoug h,be caus et hes creen
usesonl ypos it
ivev al
ue s,wi t
ht heor i
gin
(0,0)int heuppe r-l
e4c orne r.
⇢el oc ati
onofobj ectsa l
wa ysc anbe
givenbyt henumbe rpa iras socia t
e dwi th
theXa ndY( hor i
zontal andv ertica l
)ax es
.

Mot
ion
S
omuc hfordi s
c us
sionsofe l
eme nta
ryg raphing.Ifyouunde r
stoodtheFrst
exampleofdr a
wi ngt hebor deront hescre
e n,allthi
si salreadyknownt oy ou.
⇢er easonweme ntionitistopr e
pareyouf orthene xtidea,theoneofmot ion.
Whe nsome t
hingi smov ing,wes a
yitha sav el
ocity.Velocit
yhasonlytwo
compone nt
s:direct
iona ndma gni
tude.⇢a tistos aythattheonlythingswe
havetoworrya boutwhe ns i
mul at
ingamov ingobjectareitsspeedandi t
sdirec-
ti
onoftravel.
Spe edisme asuredinuni t
sofdi s
tancepe runitoftime.

]
1
74 As
sembl
yLi
nes

Int hec aseofours creendi spla ya ss hownt ot he


right, some thingmov i
ngf rom poi ntAt opoi ntBi n
ones econdwoul dha veas pe edof+5uni tspe rs econd.
Lik ewi sefors ome thingmov ingf rom poi ntAt oC.
Ne g ativevalue sa reus edtoi ndi cates ome thingmov -
ingi nadi rectionoppos i
tetheg ivenc oor di nates ys-
tem. Anobj ectmov i
ngf rom poi ntBt oAi none
secondwoul dha v eas peedof75uni tspe rsec ond.
Nowa tt hispoi ntyouma yFndy our selft empt edt othrowupy ourha nds
ands ay ,“Icant ellwhe rehe ’
sg oinga ndi tdoe sn’tsoundf un!”Youmi ghtt hink
you’ reg oingt opl ungede e peri nt ot hee soterica ndr atheruni nt
erestingr a m-
bling sofaphy sicst eachera nde ndupwho- knows -whe reandf orwha tg oodr ea-
sona ny way?
We ll
,Fr stofa ll
, y
ou’reonl yg oingt oha v etowa dei nav eryli
tt
lebi tde eper
(thes c arypa rtc ome swhe nwet ryt odone g ati
v enumbe rsinbinary!).Ands e c
-
ondofa l
l,thepoi ntofa llt hiswi llbet hes impl eg oa lofbounc i
ngal itt
leba ll
aroundont hes cr een.Asi tha ppe ns ,wemus tknowabi tabouthowt heuni ve r
se
wor k sifwea reg oi ngt osimul atei tonourTVs creen. Andi fyour eal
lyi ntendt o
endupwi ths pac eshipsca reeningwi ldlya bout ,you’llha vet oshowal ittlede ter
-
mi na ti
onnowt og e
tt heba s i
csunde ry ourbe lt.Somuc hfort hehal4i mepe p
talk.
⇢es ti
c kyque sti
oni show t oha ndl eobj ectst hata remov i
ngf r
om,s ay,
poi ntCt oA.Asac a s
eofe x t
reme l
yg oodf ortune ,ittur nsoutwec anc onsider
thec omp one ntsoft hemot ionqui t
ee asilya nda chieveoure ndr esult,wi t
hout
ha vingt ok nowt heobj ect’
sr ealdia gona lspee d.
Wha tthi sme ansi sthatwec ang i
v ea nobj e ctbot hahor i
zontalandv ertical
compone ntt oi tsmot i
on, andt he ndot hea ppr opr i
atec a l
culat
ionssepa ratel
y .
Spe edc anber ephraseda s“ ac ha ng einpos iti
onwi threspecttot ime .
"On
thes creen,wha tt hisme ansi stha ts ome thingwi lla ppe artomov ec ons i
s t
ent
wi t
ht her ealwor lda slonga swek eepr e-plottingi t
spos it
ioni nar e
g ularma n-
ne r.⇢et ime ba seoft heope rationse ndsupde pendi ngonhow f as
twec ycl
e
throug hther e-plot t
ingpa ttern. Sincea ne x ampl ec anwor kwonde r
s,let’stak ea
mome ntt oe xami neapr og rami n( ohno! )Appl eso4:
10 HGR
20 X = 0: Y = 80
30 V = 1
100 REM DRAW LOOP
110 HCOLOR = 3: REM WHITE
120 HPLOT X,Y : REM DRAW OBJECT
130 HCOLOR = 0: REM BLACK
140 HPLOT X,Y : REM ERASE IT
200 REM MAKE IT MOVE!
210 X = X + V
220 IF X > 278 THEN V = V*(-1)
1
8.Appl
eso4Hi
-Re
sGr
aphi
cs 1
75

230 IF X < 1 THEN V = V*(-1)


240 GOTO 100

⇢i spr og ra
m wi l
lbounc eat inys potoLt hele4a ndr ights idesoft he
screen.⇢ei mpor ta ntthing stonot ea rethat(1 )moti
oni ss imulatedbya ddi nga
constantv el
oc i
tyf ac t
orVt ot hepos it
ionofe a c
hc y
cle;(2)t heobj ectise r ased
from itsoldpos itionbe forebe ingr edrawna tthene wone ;and( 3 )abounc eis
ba s
ical
lyac ompl eter eve rsalofthev eloc it
yfactor,t
hatis,thev aluei smul tiplied
bymi nusone .⇢es pe edwi thwhi c he verythingise xecutedde pendsont he
inherentspe e
doft hepr og r
a mmi ngla ng uageandhowf astwec anc yclethr oug h
these r
viceloop. Iff orsomer ea s
ont hel oops hownwa stoof ast,
y ouc ouldputa
FOR-NEXTd el
a yloopi na nywhe rea l
ongt hel i
ne .I
fitwa st oos low,y ouc oul d
increasethes peedf a c
tor ,V,from 1t oal arge
rnumbe r.Larg ernumbe rspr oduc e
mor ejerkymot i
on,howe ver.⇢eot he ropt i
onwoul dbet owr iteiti nasse mbl y
language!
Be f
oredoi ngt hat,thoug h,l e
t’
sma keittwo- di
me nsiona lbyg ivingt heba l
l
bothhor i
z ontalandv erticalcompone nt stoitsmot i
on:
10 HGR
20 X = 140 : Y = 80
30 XV = 1 : YV = 1
100 REM DRAW LOOP
110 HCOLOR = 3: REM WHITE
120 HPLOT X,Y : REM DRAW OBJECT
130 HCOLOR = 0: REM BLACK
140 HPLOT X,Y : REM ERASE IT
200 REM MAKE IT MOVE!
210 X = X + XV : Y = Y + YV
220 IF X > 278 THEN XV = XV*(-1)
230 IF X < 1 THEN XV = XV*(-1)
240 IF Y > 158 THEN YV = YV*(-1)
250 IF Y < 1 THEN YV = YV*(-1)
260 GOTO 100

Inthispr
ogram weseebothcomponentsofmoti
on,ver
ti
cala
ndhor i
zont
al.
Again,abounc econsi
st
softakingthenegat
iveva
lueofthecomponentwea r
e
reve
r s
ing.⇢eSi c
k e
riscause
dbye rasi
ngthedotsosoona4erwedr a
wi t
,and
al
sobyt hesc
anningnatureoftheTV ormoni t
or.I
tcanbes moothedoutby
addingal i
ne:
125 FOR I = 1 TO 5: NEXT I

⇢isal
sowillsl
owdownt hespee
dofthebal
l abit
,butitdoeshelptheover-
all
scree
nappearance
.Youareadvis
edtowatc
ht hisfa
scinat
ingprogramrunfor
awhi l
e,medi
tat
ingonthenatureofthepr
ogra
mmi ngstepsoccur
ringthroug
h-
outthetra
vel
,andpa rt
ic
ula
rlyateachbounc.⇢i
e sconceptisess
enti
altoany
furt
herani
mati
one Lort
sonyourAppl e
.

]
1
9
1
9. Cal
li
ngHi
-Re
sGr
aphi
csRo
uti
nes
Ap
ril
198
2

I
nt heprevi
ousc ha
pterwediscus
sedhi
-r
esgr
aphic
sa ndhow t
oplota
bounci
nghi -
resbal
l.Wec ons
truc
tedas i
mpl
eAppl
eso4progra
mtoill
ust
rat
e
thepri
ncipl
esi
nvolv
e d:
10 HGR
20 X = 140 : Y = 80
30 XV = 1 : YV = 1
100 REM DRAW LOOP
110 HCOLOR = 3: REM WHITE
120 HPLOT X,Y : REM DRAW OBJECT
130 HCOLOR = 0: REM BLACK
140 HPLOT X,Y : REM ERASE IT
200 REM MAKE IT MOVE!
210 X = X + XV : Y = Y + YV
220 IF X > 278 THEN XV = XV*(-1)
230 IF X < 1 THEN XV = XV*(-1)
240 IF Y > 158 THEN YV = YV*(-1)
250 IF Y < 1 THEN YV = YV*(-1)
260 GOTO 100

Not et
ha tthi
sl oopha saba si
cpatt
ernof:drawferaefc
s a
lculaefc
t heckf( do
itagai
n...
).
Fort heAppl eso4pr ogramshown,t hi
sworksfa
irl
ywe llandisveryunde r-
standable.⇢e rei sonepr oblem,howe ver
:v e
ryli
tt
letimepa sse
sbe tweent he
drawa nde r
ases t
ag e
s,compa redtot heamountoftimes pentinthec al
c ul
ate
andt e
s tsect
ions .⇢er esultonthesc r
eenisalar
geamountofSi cker,result
ing
from t
hedots pendi ngmor eofitsti
mebl ac
kthanwhite.
Ones ol
utiont ot hi
si stoma keas mal Fc
lmodi at
iontot heorigi
na lApple-
so4pr ogram, sot hatitappearsasf
ollows:
0 REM FP DOT DEMO PROGRAM
10 HGR
15 HCOLOR = 3 : HPLOT 0,0 TO 279,0 TO 279,159 TO 0,159 TO 0,0
20 X = 140 : Y = 80
30 XV = 1 : YV = 1
100 REM CALC NEW POSN
110 TX = X + XV : TY = Y + YV

200 REM CHECK POSN


210 IF TX > 277 THEN XV = XV*(-1) : GOTO 110
220 IF TX < 2 THEN XV = XV*(-1) : GOTO 110

]
1
78 As
sembl
yLi
nes

230 IF TY > 157 THEN YV = YV*(-1) : GOTO 110


240 IF TY < 2 THEN YV = YV*(-1) : GOTO 110

300 REM ERASE OLD POSN


310 HCOLOR = 0: REM BLACK
320 HPLOT X,Y

400 REM DRAW NEW POSN


410 X = TX : Y = TY
420 HCOLOR = 3: REM WHITE
430 HPLOT X,Y
440 GOTO 100

⇢isr outinenotonl ydr awsani c ebor dera roundt hes c


reen,buta lsof
ol-
l
owst hi
sg eneralpa t
tern:calculaefc
t heckfe r
a sefdr awf( st
artove r)
.
⇢ea dv antageoft histechniqueist hatrelati
v el
ylit
tletimeisspe ntbetween
theeras
ea ndr edraws tages.⇢ust hedoti sont hes c
reent hema jorit
yoft he
timeandv erylit
tleSickerisa pparent.
Anotherne w de tailistheus eofas etoft empor aryv ari
ables
,TX a ndTY.
⇢e sest
or ethene wpos iti
onwhi l
etheol donei sbeinge rasd.⇢ene
e wonei s
thendrawna ndTX, TYa rema de“oWc ial”bybe ingpa sse
dt othe“ r
eal”X, Yvari
-
able
s.
Asami norpoi nt,alsonot ethatweha v ereduc edthebounda ryt estpoi
nts
inli
nes20 072 40s othatthedotr e
versesdi r
e ct
ionbe forea c
tuall
ycont acti
ngthe
boundaryweha vedr a
wn.Ot herwise,thebounda r ywoul dbee r
a s
edbyt hedot
passi
ngthroug hi tone achbounc e.
Nowl et’
slooka thowt oimpl eme ntthisprog rami na s
semblylang uage.

Ta
kingt
heOppos
iteofaSi
gne
dNumbe
r
Inc hapte r10wedi s
cus sedthes i
gnbitandhowt or eprese
ntne ga t
ivenum-
bers1R
. ecalltha tnega
tiv
enumbe rsaredeFne dus i
ngt het wo ’
sc omp leme ntsys
-
tem:reve r
see a c
hbitofthepos iti
venumbe r,thena ddone .
Allthatwene ednowi sar outinethatwillproducet heoppos it
eofanumbe r
givenit–t ha tisproducet het wo’scompleme ntofapos it
ivenumbe ra nda l
so
thepos i
tivee quival
entwhe ng ivenane g
a t
ivevalue.Todot hi
s, we’l
lus et heEOR
comma nd.
EOR isus efulincreatingar outi
net oc onv er
tbe tweens igne dnumbe r
s
becauseofi tsa bil
it
ytor e
v erseallofthebitsinag i
venby te.⇢ec onv ersi
onis
donewi tht woi ndivi
dualrout i
nes.Intheexampl esbelow, t
her outine sconv e
rta
constantv alue ,
#$34,backa ndf orth.I
nawor kingv ersionoft hispr og r
a m,the
valuewoul dbepa ss
edv i
aar e
gist
erorame mor ylocat
ion, aswi llbes hownl ater
.

CT]⇢eor
1[ igi
nal
Softa
lka
rti
cl
e#9(
1 Apr
il1
982
)cont
aine
das
ect
ionon“
sig
nedbi
nar
y
number
s.
”InAs
semb
lyLi
ne:TheBo
s ok(
andi
nthi
sbook
),t
hisma
ter
iali
spr
ese
nte
din
cha
pte
r10.
1
9.Ca
ll
ingHi
-Re
sGr
aphi
csRout
ine
s 1
79

Positive to Negative
ENTRY LDA #$34 ; %00110100 = +52
; TO BE CONVERTED TO -52
EOR #$FF ; %11111111 TO REVERSE BITS
; RESULT = %11001011
CLC
ADC #$01 ; RESULT = RESULT + 1
= %11001100 = $CC
STA MEM ; STORE RESULT
DONE RTS

Negative to Positive
ENTRY LDA #$CC ; %11001100 = $CC = -52
; TO BE CONVERTED BACK
SEC
SBC #$01 ; ACCUM = ACCUM - 1
; = %11001011 = $CB
EOR #$FF ; REVERSE ALL BITS
; RESULT = %00110100 = $34 = +52
STA MEM ; STORE RESULT
DONE RTS

Notetha
tinthisexampl
etheperc
entsi
gni
suse
dt oindic
atet
hebina
ry
f
orm oft
henumbe
r .
S omeass
embl
ers(
sucha
sMer
ln)s
i uppor
tthi
snot
ati
on.

⇢eRe
al⇢i
ng:
Hi-
Resi
nAs
sembl
y
Wenow havet
het ool
snece
ssa
rytocons
truc
ttheasse
mbly
-la
ngua
gev
er-
s
ionoft
hel
astAppl
eso4lis
ti
ng.
Assembl
eandrunthi
sli
st
ing:
1 ********************************
2 * AL19-HIRES ONE DOT PROGRAM *
3 ********************************
4 *
5 *
6 * OBJ $6000
7 ORG $6000
8 *
9 X EQU $E0 ; $E0,$E1
10 Y EQU $E2
11 XV EQU $06 ; $06,$07
12 YV EQU $08
13 TX EQU $09 ; $09,$0A
14 TY EQU $0B
15 *
16 PREAD EQU $FB1E
17 WAIT EQU $FCA8
18 HCOLOR EQU $F6F0
19 HGR EQU $F3E2
20 HPLOT EQU $F457
21 HPOSN EQU $F411
22 HLIN EQU $F53A
23 *
6000: 20 E2 F3 24 ENTRY JSR HGR

]
1
80 As
sembl
yLi
nes

6003: A2 03 25 LDX #$03 ; WHITE


6005: 20 F0 F6 26 JSR HCOLOR
27 *
6008: A9 00 28 BOX LDA #$00 ;Y=0
600A: A8 29 TAY
600B: AA 30 TAX
600C: 20 57 F4 31 JSR HPLOT ; PLOT 0,0
600F: A9 17 32 LDA #23 ; 279 MOD 256
6011: A2 01 33 LDX #01 ; 279/256
6013: 20 3A F5 34 JSR HLIN ; FROM 0,0 TO 279,0
35 *
6016: A9 17 36 LDA #23
6018: A2 01 37 LDX #01
601A: A0 9F 38 LDY #$9F ; Y = 159
601C: 20 3A F5 39 JSR HLIN ; 279,0 TO 279,159
40 *
601F: A9 00 41 LDA #$00
6021: A2 00 42 LDX #$00
6023: A0 9F 43 LDY #$9F
6025: 20 3A F5 44 JSR HLIN ; 279,159 TO 0,159
45 *
6028: A9 00 46 LDA #$00
602A: A2 00 47 LDX #$00
602C: A0 00 48 LDY #$00
602E: 20 3A F5 49 JSR HLIN ; 0,159 TO 0,0
50 *
6031: A9 00 51 SET LDA #$00
6033: 85 07 52 STA XV+1
6035: A9 01 53 LDA #$01
6037: 85 06 54 STA XV ; XV = 1
6039: 85 08 55 STA YV ; YV = 1
56 *
603B: A2 8C 57 POSN LDX #$8C
603D: A0 00 58 LDY #$00 ; X = 140
603F: A9 50 59 LDA #$50 ; Y = 80
6041: 20 11 F4 60 JSR HPOSN ; SET CURSOR AT X,Y
61 *
6044: 18 62 CALC CLC
6045: A5 E0 63 LDA X
6047: 65 06 64 ADC XV
6049: 85 09 65 STA TX
604B: A5 E1 66 LDA X+1
604D: 65 07 67 ADC XV+1
604F: 85 0A 68 STA TX+1 ; TX = X + XV
69 *
6051: 18 70 CLC
6052: A5 E2 71 LDA Y
6054: 65 08 72 ADC YV
6056: 85 0B 73 STA TY ; TY = Y + YV
74 *
6058: A5 0A 75 CHK LDA TX+1
605A: D0 09 76 BNE CHK2
605C: A5 09 77 LDA TX
605E: C9 02 78 CMP #$02
6060: B0 03 79 BCS CHK2
6062: 20 AE 60 80 JSR RVRSX ; TX < 2
1
9.Ca
ll
ingHi
-Re
sGr
aphi
csRout
ine
s 1
81

81 *
6065: A5 0A 82 CHK2 LDA TX+1
6067: C9 01 83 CMP #$01
6069: 90 09 84 BCC CHK3
606B: A5 09 85 LDA TX
606D: C9 16 86 CMP #$16
606F: 90 03 87 BCC CHK3
6071: 20 AE 60 88 JSR RVRSX ; TX >= $116 (278)
89 *
6074: A5 0B 90 CHK3 LDA TY
6076: C9 02 91 CMP #$02
6078: B0 03 92 BCS CHK4
607A: 20 D6 60 93 JSR RVRSY ; TY < 2
94 *
607D: A5 0B 95 CHK4 LDA TY
607F: C9 9E 96 CMP #$9E
6081: 90 03 97 BCC ERASE
6083: 20 D6 60 98 JSR RVRSY ; TY >= $9E (158)
99 *
6086: A2 00 100 ERASE LDX #$00 ; BLACK = 0
6088: 20 F0 F6 101 JSR HCOLOR
608B: A6 E0 102 LDX X
608D: A4 E1 103 LDY X+1 ; GET X,X+1
608F: A5 E2 104 LDA Y ; GET Y
6091: 20 57 F4 105 JSR HPLOT ; ERASE POINT
106 *
6094: A2 03 107 PLOT LDX #$03 ; WHITE1 = 3
6096: 20 F0 F6 108 JSR HCOLOR
6099: A6 09 109 LDX TX
609B: A4 0A 110 LDY TX+1 ; GET TX,TX+1
609D: A5 0B 111 LDA TY ; GET TY
609F: 20 57 F4 112 JSR HPLOT ; PLOT POINT
113 *
60A2: A2 00 114 DELAY LDX #$00 ; PDL0
60A4: 20 1E FB 115 JSR PREAD
60A7: 98 116 TYA
60A8: 20 A8 FC 117 JSR WAIT
118 *
119 *
60AB: 4C 44 60 120 GOBACK JMP CALC
121 *
122 *
60AE: A5 07 123 RVRSX LDA XV+1
60B0: 30 12 124 BMI NEGPOSX
125 *
60B2: A5 06 126 POSNEGX LDA XV
60B4: 49 FF 127 EOR #$FF
60B6: 18 128 CLC
60B7: 69 01 129 ADC #$01
60B9: 85 06 130 STA XV
60BB: A5 07 131 LDA XV+1
60BD: 49 FF 132 EOR #$FF
60BF: 69 00 133 ADC #$00
60C1: 85 07 134 STA XV+1
60C3: 60 135 RTS ; XV -> -XV
136 *

]
1
82 As
sembl
yLi
nes

60C4: A5 06 137 NEGPOSX LDA XV


60C6: 38 138 SEC
60C7: E9 01 139 SBC #$01
60C9: 49 FF 140 EOR #$FF
60CB: 85 06 141 STA XV
60CD: A5 07 142 LDA XV+1
60CF: E9 00 143 SBC #$00
60D1: 49 FF 144 EOR #$FF
60D3: 85 07 145 STA XV+1
60D5: 60 146 DONEX RTS ; -XV -> XV
147 *
148 *
149 *
60D6: A5 08 150 RVRSY LDA YV
60D8: 30 0A 151 BMI NEGPOSY
152 *
60DA: A5 08 153 POSNEGY LDA YV
60DC: 49 FF 154 EOR #$FF
60DE: 18 155 CLC
60DF: 69 01 156 ADC #$01
60E1: 85 08 157 STA YV
60E3: 60 158 RTS ; YV -> -YV
159 *
60E4: A5 08 160 NEGPOSY LDA YV
60E6: 38 161 SEC
60E7: E9 01 162 SBC #$01
60E9: 49 FF 163 EOR #$FF
60EB: 85 08 164 STA YV
60ED: 60 165 DONEY RTS ; -YV -> YV
166 *
60EE: 3A 167 CHK

Whe ny our unt hisr outine, noticehowmuc hf asteritex e


c ute
sa ndhowt he
speedoft hedotc anbev ariedus ingpa ddl e0 .
⇢i sr out i
nee ssentiall
ypa rall
e l
stheAppl eso4r out i
nes howne arli
er.Li nes
2475 0c l
ea rthehi-re ss cre
e na nddr awt hebor de r
.Li nes5 175 5setthev e locit
y
compone ntst o1 ;lines5 776 1pos i
ti
ont hehi -resc ursori nt hec ente
roft he
scr
ee n.⇢i sa l
soc onv e nientlyloa ds$E0-E2wi tht hede si
re dXa ndYc oor di-
natesoft hedot .Reme mbe rtha t$E0,$E1,a nd$E2a rethez ero-pageloc a ti
ons
usedbyt heAppl eso4hi -re
sr out i
ne sfortheXa ndYc oor dinatesofitscurs or.
Lines6 2773c al
c ulatet hene wpos it
ionoft hedotbya ddi ngther espe cti
ve
vel
oc it
yc ompone ntst ot heX a ndY c oor dina te
s.Li nes7 5798c heckt os e
e
whethe rthisne wpos i
tioni ss t
illwi t
hint hes pec iFeds creenbounda ri
es.Ifi thas
reac
he dthee dge,thea ppropr i
a tev el
ocit
yc ompone nt sarer eversedforthene xt
go-round’ scalcula
tion.
Line1 0 0start
sthee rasingoft hecurre ntdotpos iti
on, i
mme di
a t
elyfollowe d
byadr awi ngoft hene w pos it
ion.Not et ha tthee quivalentoft heX=TX:Y=TY
sta
teme nti sappa r
ent lymi ssing. Ina c
tuality,itisa utoma ticall
ya ccomplishe dby
theJSR HPLOTonl ine1 12. Reme mbe rtha tthec onte ntsoft heAc cumul ator ,X-
andY- Re gister
sa rea utoma ti
callya ss
igne dt o$E07E2byHPLOT. Line1 14doe sa
1
9.Ca
ll
ingHi
-Re
sGr
aphi
csRout
ine
s 1
83

shortdelaybyget
tingavaluefr
om paddle0tobeusedbytheWAIT( $FCA8)rou-
ti
ne.A4e rthedel
ay,aJMP CALCres
tar
tstheenti
repr
ocess
.
Lines1267165areappli
cati
onsofthesign-
reve
rsa
lrouti
nesshowne arl
ier
.
NoticethatRVRSYisaone -
byterev
ers
al,whil
eRVRSXil
lus
trat
estherev e
rsalofa
two-bytevalue
.Simila
rly
,CALCshowsthatthesameaddit
ionrouti
nei susedfor
bothsignedbinary(ourcurr
entcondit
ion)andunsi
gnedbinary(asinpr e
v i
ous
chapters
).

Ta
ble
-Dr
ive
nGr
aphi
cs
Forg ra
phicsofa nyc omplex i
ty–a nyt
hinginv olvi
ngmor ethanonedot –a
l
itt
leimpr ovementont hisrouti
neisne eded.Oneoft hemos tcommonwa y
sof
doingthisistous eat ableofa l
lthecurrentpointsont hes c
reena ndt hei
rcorre-
spondingv el
ocit
ies.Mot ionisthenma nagedbys equenti
all
ys c
a nningt hr
oug h
thetableandus i
ngt hee nti
recalcula
tion,checka nde ras
e/plotsect
iona sas ub-
routi
ne.
Toc onvertther out i
nepr e
sentede arl
ie
r,ma ket hefoll
owingc hang est
ot he
sourcecode( t
hehe xda tafr
om thea ss
e mblyisincludedt oassi
stine rrorcheck-
i
ng )2
:
1. Addt heseli
ne stot heendoft heli
s t
ing(newl inenumbe rsshown) :

237 *
238 *
239 *
240 *
6154: A2 00 241 SETUP LDX #$00
6156: BD 62 61 242 LOOP LDA DATA,X
6159: 9D 00 10 243 STA TABLE,X
615C: E8 244 INX
615D: E0 28 245 CPX #40 ; 8 BYTES * NUM DOTS
615F: 90 F5 246 BCC LOOP
6161: 60 247 DONE RTS
248 *
6162: 8C 00 50 249 DATA HEX 8C005000 ; X,Y(1) = 8C,50
6166: 01 00 01 250 HEX 01000100 ; XV,YV(1) = 1,1
251 *
616A: 8E 00 52 252 HEX 8E005200 ; X,Y(2) = 8E,52
616E: 01 00 01 253 HEX 01000100 ; XV,YV(2) = 1,1
254 *
6172: 90 00 54 255 HEX 90005400 ; X,Y(3) = 90,54
6176: 01 00 01 256 HEX 01000100 ; XV,YV(3) = 1,1
257 *
617A: 92 00 56 258 HEX 92005600 ; X,Y(4) = 92,56
617E: 01 00 01 259 HEX 01000100 ; XV,YV(4) = 1,1
260 *
6182: 94 00 58 261 HEX 94005800 ; X,Y(5) = 94,58
6186: 01 00 01 262 HEX 01000100 ; XV,YV(5) = 1,1

2[
CT]⇢ec
hec
ksum f
ort
hene
wpr
ogr
ami
s$06.

]
1
84 As
sembl
yLi
nes

263 *
618A: 06 264 CHK

2
. Re
wri
tel
ine1
20(
wil
lendupa
s19
0)a
s:
6113: 60 190 GOBACK RTS

3
. Re
wri
tet
hebe
ginni
ngoft
hes
our
cea
s:

1 ********************************
2 * AL19-HIRES LOTS DOTS *
3 ********************************
4 *
5 *
6 * OBJ $6000
7 ORG $6000
8 *
9 TABLE EQU $1000
10 CTR EQU $0C
11 NUM EQU $05 ; FIVE DOTS
12 *
13 X EQU $E0 ; $E0,$E1
14 Y EQU $E2
15 XV EQU $06 ; $06,$07
16 YV EQU $08
17 TX EQU $09 ; $09,$0A
18 TY EQU $0B
19 *
20 PREAD EQU $FB1E
21 WAIT EQU $FCA8
22 HCOLOR EQU $F6F0
23 HGR EQU $F3E2
24 HPLOT EQU $F457
25 HPOSN EQU $F411
26 HLIN EQU $F53A
27 *
6000: 20 E2 F3 28 ENTRY JSR HGR
6003: A2 03 29 LDX #$03 ; WHITE
6005: 20 F0 F6 30 JSR HCOLOR
31 *
6008: 20 54 61 32 TABLESET JSR SETUP
33 *
600B: A9 00 34 BOX LDA #$00 ;Y=0
600D: A8 35 TAY
600E: AA 36 TAX
600F: 20 57 F4 37 JSR HPLOT ; PLOT 0,0

4
.Ins
ertt
hec
odef
ort
het
abl
elook
ups
tar
ti
nga
tne
wli
ne6
8:

6047: A9 00 68 LOOKUP LDA #$00


6049: 85 0C 69 STA CTR
604B: A5 0C 70 GET LDA CTR
604D: 0A 71 ASL
604E: 0A 72 ASL
604F: 0A 73 ASL ; X = CTR*8
1
9.Ca
ll
ingHi
-Re
sGr
aphi
csRout
ine
s 1
85

74 *
6050: AA 75 TAX
6051: BD 00 10 76 LDA TABLE,X
6054: 85 E0 77 STA X
6056: E8 78 INX
6057: BD 00 10 79 LDA TABLE,X
605A: 85 E1 80 STA X+1
605C: E8 81 INX
605D: BD 00 10 82 LDA TABLE,X
6060: 85 E2 83 STA Y
6062: E8 84 INX
6063: E8 85 INX ; Y + 1 NOT USED
86 *
6064: BD 00 10 87 LDA TABLE,X
6067: 85 06 88 STA XV
6069: E8 89 INX
606A: BD 00 10 90 LDA TABLE,X
606D: 85 07 91 STA XV+1
606F: E8 92 INX
6070: BD 00 10 93 LDA TABLE,X
6073: 85 08 94 STA YV
95 *
6075: 20 AC 60 96 SERVICE JSR CALC
97 *
6078: A5 0C 98 PUT LDA CTR
607A: 0A 99 ASL
607B: 0A 100 ASL
607C: 0A 101 ASL
607D: AA 102 TAX
103 *
607E: A5 E0 104 LDA X
6080: 9D 00 10 105 STA TABLE,X
6083: E8 106 INX
6084: A5 E1 107 LDA X+1
6086: 9D 00 10 108 STA TABLE,X
6089: E8 109 INX
608A: A5 E2 110 LDA Y
608C: 9D 00 10 111 STA TABLE,X
608F: E8 112 INX
6090: E8 113 INX ; SKIP BYTE
114 *
6091: A5 06 115 LDA XV
6093: 9D 00 10 116 STA TABLE,X
6096: E8 117 INX
6097: A5 07 118 LDA XV+1
6099: 9D 00 10 119 STA TABLE,X
609C: E8 120 INX
609D: A5 08 121 LDA YV
609F: 9D 00 10 122 STA TABLE,X
123 *
60A2: E6 0C 124 INC CTR
60A4: A5 0C 125 LDA CTR
60A6: C9 05 126 CMP #NUM ; NUMBER OF DOTS
60A8: 90 A1 127 BCC GET
60AA: B0 9B 128 BCS LOOKUP
129 *

]
1
86 As
sembl
yLi
nes

130 *
131 *
60AC: 18 132 CALC CLC
60AD: A5 E0 133 LDA X
60AF: 65 06 134 ADC XV

Runt hisroutinef
rom t
heMoni t
orwitha6000Gorf rom Apples
o4witha
CALL 24576.I
fca l
li
ngfr
om theMonit
or,mak esur
ey ouhaveent
eredtheMoni
-
torfr
om Appl eso4whe nyoudot heCALL -151 t
oe ns
urethattheApple
so4
ROM orRAM c ardba nki ss elected.Not et hata lthoug ht hee nti
rer out i
nei sin
assembl yl ang ua ge,i tdoe sr equiret hepr e senc eoft heAppl e so4hi-r esr outines
int he$D0000-$F7FFr ange .Byus ingpa ddl e0y ouc anv aryt hes peedofe xecu-
ti
onc ons i
de rabl y.Onedr a wba ckofus i
ngt heWAITr out i
nei sthat0wi l
lbej ust
ass low a s2 55whe na djus ti
ngt hepa ddl e.Ot he rwi s
e,i ts houldbe ha vequi t
e
nicely. Tos pe edt hing supf ur t
he r,NOPo utt heJSRt oWAITonl i
ne1 87 .Ane ven
greaters pe edi nc reasei sa chieve dbys i
mi larlydi s
abl i
ngt heJSR PREADonl ine
185 ,althoug hwi thPREADg onet he rei snol ong era nyc ont rolov erthes pe ed.H
Howe v er
,t hiswi l
lg ivey oua ni de aoft hema ximum s pee dpos sibl
ef ort heFv e
dotsus i
ngs t
anda rdAppl eso4hi -resrout ine s.
⇢ema inpoi ntst onot eint hene wl istinga ret heJSRt oSETUPonl ine3 2,
theLOOKUPs ectioni nl i
ne s6 871 28 ,andt het abl eg ene ratora tthee ndi nl ines
241 72 63.
SETUPc reate sada tata blestartinga tl oc at
ion$1000t hatc ontainsanumbe r
ofe ight -
by tebl oc ks ,e a
c hofwhi c hc ont ainst hei nforma ti
onne ce ssaryf ora
givendot .⇢ebl ocki sma deupoft wof our -by tes ubuni ts.⇢eFr stf ourby te
s
givet heloc ationda taf ort heXa ndYc oor di nate s.Not i
c etha tthefour thby teis
notus ed.S pacei nt het ablec ouldha vebe e ns ave dbyomi tti
ngt hisby te,butt he
eightb ytel eng th pe re nt rya ll
owsust o us eaf e ws impl eASLs,a swi l
lbe
expla i
ne dmome nta ril
y.
⇢es ec ondf ourby teshol dt hev eloc ityda ta ,ag ai
ni na nX,Yf or ma t,with
bytef ourbe i
ngunus ed.
LOOKUPb asicallydoe st hreet hing s.Fi rsti tr etr
ie vestheda taforadota nd
putsi ti nt hec ur rentX,Y,XV,YVby tes .S ec ond,i tf eedst he setot heCALCa nd
PLOTr outine s.⇢i rd, whe nCALC/ PLOTr etur ns ,thene wl oca t
iona ndv elocityv al
-
uesa res t
or edba cki nt het able.
Ex amini ngt hec odes tart
inga tGET,y ouc a ns e etha tCTRi sus edt ok eep
tra
c kofwhi chdotwe ’rec ur r
entlypr oce ssing .⇢i si smul ti
pliedby8t og etthe
basea ddr essoft heda tafort hatdot .Re me mbe rt hatASLc a nbeus e
dt omul ti
ply
1
9.Ca
ll
ingHi
-Re
sGr
aphi
csRout
ine
s 1
87

easi
lybyapowe roftwode pendingonthenumbe rofASLsyouuse
.Ea chASLi
s
equival
entt
omul ti
ply
ingby2 .
Onc et
hebaseaddres
soLs etisde
ter
mi ned,t
hisi
sputintheX-
Re g
iste
rand
theda taret
ri
evedv iaas erie
sofLDA/ STA oper
ati
ons.A4erret
urningfrom
CALC/PLOT,
theproces
sisreve
rsedt os
tor
et henewdata.

Conc
lus
ion
Hi-resi sa ninv olvedt opi c,andi t’schalleng i
ngt otrytopr e
sentt her i
ght
mi xofc larit
ya ndi n- dept he xpl a
na ti
on.Myg oa listopr ovi
dee noug hoft he
ba s
icstogivey out hes pring boardt opur suey ourowni nter
e s
ts.
Ing ener al
,thepr i
nc i
ple spr ovidedi nthisc hapterandt heonebe foreitare
thefounda tionofmos tani ma tedg raphicsprog rams .Tabl
esa r
eespec i
all
ywor th
yourc onside r
a t
iona st heypr ovideas trai
ghtforwa rdwa yofma nagingal a r
ger
numbe rofs c r
e e
npoi nts.
Bynowi talsoshoul dbee v i
de ntthate ve
ni na ss
emblyl a
nguage, t
heAppl e-
so4r outi
ne st hems el
v esa res t
illthemos trestrainingporti
oni ntermsofs peed
ande xecution. I
na llfairne s
st oAppl eso4,thoug h, re
ali
zethatthei
rs peedissac-
riFcedforsimpl icit
ya ndc onv enienc eofope r
a t
ion.
Ne xtcha pter’
st opicwi llbet hel ayoutoft hehi -r
esscreenit
s e
lf,andhow
certai
nde dicatedr outinesc a nbec re at
edt ogetal it
tlemoreoutoft heol ’Apple.

]
2
0
2
0. S
truc
tur
eoft
heHi
-Re
sDi
spl
ayS
cre
en
Ma
y19
82

Int hepr ece


dingdi scuss
ionsofhi-re
sgraphi c
swe ’
verel
ie
dont hee xi
sting
Appl e
s o4BAS ICr out
inestodot henecessa
rypl ott
ingofpointsfrom ass
embl y
l
a nguag e.From y ourpr evi
ouse xper
iencewithAppl eso4ande venf r
om t he
mos tre c
e nthi-r
esmov i
ng -
dotprogramspre s
ented,y ouma yhavenotice
dc e r
-
tainpe culiari
ti
esa bouthi -
resg r
aphi
cs.⇢epr obl
e msl i
einc er
tai
ni ntr
ins i
c
shortcomi ngsinthee xpla
na t
ionofhi-
resgraphicsoLe re
dsofar.
Toe xplorethisareafurtherle
t’
se xa
mine ,onebyone ,anumbe rofpr ob-
l
e mst hatc anoc cur–a ndthusdi s
covertheunde rlyi
ngs tr
uctur
eoft hehi-res
displ
ays creen.

Loa
dingaHi
-Re
sSc
ree
n:t
he“
Fil
l”E?e
ct
⇢ef unda me nt a
lque stiont obea nswe r
e dint hisdi s
cus sioni s,“How a re
indiv i
dual pointspl ottedont hes creen?”Itshoul dber el
a t
ivelye asytoacceptt he
notiont hatt odi splayas cr eenwhos ea ppearancec anbea rbi t
ra ri
lychange d,the
RAMp orti
onoft hec omput ermus tbeus ed.⇢ea r
eaus edisther angeofme mor y
from $2000t o$3FFF( 8 19 2t o1 6383de ci
ma l
).⇢i sisca l
ledt hepa geonehi -res
display.⇢eAppl eIIi sa lsoc apableofdi splayinga na lterna t
eme mor yr a
ng e
call
e d,cleverlye noug h,t hepa get wohi -
resdisplay.⇢i sdi splayi sderi
vedf rom
theda tacont ainedi nt heme mor yr ange$4000t o$5FFF.
⇢i sc hapterwi l
lf oc uspr i
ma r
ilyonpa geone ,a lt
houg hf ort hemos tpa r
t
pag etwoc anbec onside redt obej ustas i
mpl eoLs etfrom pag eone .
Itals
os houldbei ntui ti
v el
yobv i
oust hatthedi s
playmus tins omewa ybe
li
nk edt othea ct
ua lcont e ntsofe achby teinther ang esme ntione d.⇢i scane a s
-
il
ybei nvestigatedbydoi ngt hef oll
owi ng:
From Appl eso4BAS IC,s electandc l
earthepa g eonehi -
r esdispla
ybyt yp-
ingi nHGR<RETURN>. Ift hec ursorisnots t
il
lvisible
, press<RETURN>unt ilitreap-
pearsa tthebot tom oft hes creen.
Now e ntert heMoni torwi t
haCALL -151.⇢eFr s
tt hingt odoi st oFl l
me mor ywi thas ampl ev a lue. Dot hisbye nteri
ngt hef ol
lowing :
2000:FF
2001<2000.3FFFM

]
1
90 As
sembl
yLi
nes

Whenyoupre
ss<RETURN>,thescr
eens
houldr
api
dlyFl
ltowhit
e.Ent
er
<CTRL>Ct
oret
urnt
oBAS IC.Let’
ssav
ethesc
ree
nnowbyplac
ingaconve
nie
nt
di
ski
nthedr
ivea
nde
nte
ring
:
BSAVE TESTPIC,A$2000,L$2000

Besi
despr ovi
dingt heinforma t
iononhowt osaveahi-re
sima g
e,thepur-
poseofthisinstr
uctionwa stoallowy outowa t
chthescree
nFl la
talit
tl
es l
ower
pace.Youma yha v
enot icedwhe ny ouFll
edthes cr
eenjustnowthatitdidnot
Fllinanexa c
tl
yc ontinuouspa t
tern,li
ne-
by-l
inefrom t
optobot t
om. I
tdidhap-
penr a
therquickl
y ,
thoug h.
Clea
rt hescreenbyt ypingHGR<RETURN>a g
ain,andnowl oadthedatafr
om
diskbackintome mor ybye nter
ing:
BLOAD TESTPIC

⇢i st i
met hes creens houl dFll mores lowly,andthes ome wha tst
rangepa t
-
ternt hisg enerateswi llbemor ea ppa re
nt. Sonowourpr obl
emi s:“Howi sav er-
tic
a lscreenpos iti
on( line)se l
ec t
edi ntermsofi tsme mor yaddr es
s? ”(
Or :
"Why
doe sthes creenl oa dins uchaf unnywa y? ”)
YourFr stimpul semi ghtbet os ay“ We ll
,ifIwe rede s
igningt hecomput er
,
I’
dj us tmul ti
plyt henumbe roft helineIwa ntedbythenumbe rofby t
esperline
tog ett heba sea ddr ess(thea ddr essoft heFr stbyteoft heli
ne )fortheline.For
exampl e,ifeachl i
net ookf or tyby tes(whi ch,bythewa y,i
tdoe s),li
ne0woul d
hav eaba sea ddr essof$2000.Li ne1woul dbe$2000+1d$28( $28=40d eci
-
ma l)=$2028. Line2woul dbe$2000+2d$28=$2060, ands oon.
Ana dditi
ona lbene Ftwoul ds howupi ntheform ofs omeunus edby t
eson
thehi - r
e spag e.For1 92lines ,thela s
ta ddr e
ssus edwoul dbe$2000 +(192d
$28) 71 =$3DFF.S incewe ’
v ea l
lotte
dt hea r
eaf r
om $2000 t o$3FFFf orpa ge
one ,thiswoul dl e
a ve$200( 512d e
c i
ma l
)by t
esle4ov e
r!
Unf ortuna tel
y ,tha t
’snott hewa yt heAppl ewa ss etup.I tt ur
nsoutt hat
mul tiplicat
ionr outinesa rek indofadr a gint er
msofs peeda ndme moryus age
,
unles sy ou’reus inge x actmul tiplesoftwo.Amuc hmor ecompa ct(andfaster
)
algorithm i s:
1 ********************************
2 * AL20-HIRES BASE ADDRESS *
3 * CALCULATOR ROUTINE *
4 ********************************
5 * OBJ $300
6 ORG $300
7 GBAS EQU $26
8 HPAG EQU $E6 ; HGR=$20, HGR2=$40
9 *
10 * CALC BASE ADDRESS FOR Y-COORD IN ACCUM.
11 * GBAS = ADDR OF 1ST BYTE OF LINE SPECIFIED.
12 * ASSUME ACCUM HAS BITS abcdefgh, C=carry
0300: 48 13 ENTRY PHA ; abcdefgh
2
0.S
truc
tur
eoft
heHi
-Re
sDi
spl
ayS
cre
en 1
91

0301: 29 C0 14 AND #$C0 ; ab000000


0303: 85 26 15 STA GBAS
0305: 4A 16 LSR ; 0ab00000
0306: 4A 17 LSR ; 00ab0000
0307: 05 26 18 ORA GBAS ; abab0000
0309: 85 26 19 STA GBAS
030B: 68 20 PLA ; abcdefgh
030C: 85 27 21 STA GBAS+1
030E: 0A 22 ASL ; bcdefgh0 C=a
030F: 0A 23 ASL ; cdefgh00 C=b
0310: 0A 24 ASL ; defgh000 C=c
0311: 26 27 25 ROL GBAS+1 ; bcdefghc C=a
0313: 0A 26 ASL ; efgh0000 C=d
0314: 26 27 27 ROL GBAS+1 ; cdefghcd C=b
0316: 0A 28 ASL ; fgh00000 C=e
0317: 66 26 29 ROR GBAS ; eabab000
30 *
0319: A5 27 31 LDA GBAS+1 ; cdefghcd
031B: 29 1F 32 AND #$1F ; 000fghcd
031D: 05 E6 33 ORA HPAG ; 001fghcd (PAGE 1)
031F: 85 27 34 STA GBAS+1 ; 001fghcd
35 *
0321: 60 36 DONE RTS

Althoug hi t
’sperha psnotobv i
oushowt hiswor ks, theroutinedoe stakea ny
valuei nt heAc cumul ator,fr om 0t o1 91 ,a ndr etur nt hea ppropr i
ateba se
addr essoft hec or r
espondi ngl i
nei nl oc ations$26,$27 ( GBAS) .⇢i scodei s
“stolen”f r om as imilarr outinei nt heAppl eso4hi -resr outineHPOSN( $F411)
me ntione di nt hepr eviousc hapter.1

⇢eov erallpa t
ternt ot hes creen- Fllingope r
ationi sa sf ollows .⇢eFr s
t
fortyby t
e sofme mor yc orrespondt oline0oft hes creendi splay.⇢ene xtfor t
y
bytesf orml i
ne6 3, a
ndt hene x tfortyby te sline1 2 7
. Att hee ndoft heline1 27is
abl ockofe i
g htunus edby tes
.( 3d4 0+8=1 28by tes).⇢i spa tterni srepeated
sixty-threemor et i
me st oc re
a teall1 92s c reenl ines2(
. 3d6 4=1 9 2lines;64d
128by tes=8 ,
1 92by tespe rhi-respa ge.
)
Whe nhi -respa ge1i sl oadedf rom di s k,ther a ngeofme mor yisFl led
seque nt i
allyf rom $2000 t o$3FFF.Wha ty ous eeont hes creena ret wenty-four
screenbl oc k
s,e ac hcons i
s t
ingofe ightl inesg raduall
ybe ingFl led.⇢et we nty-
fourbl ock salsoc anbev iewe da se ightt ripl ets
,wi the a
cht ripletma deupof
threel ines,onel ineea cha tthet op,mi ddl e,a ndbot tom por t
ionsoft hes cr
een.
⇢eg ene ralsc reenFl lpatternt heni s:0 , 63,1 2 7;8,7 1,13 5;16,7 9 ,143;… 6 2,
126 ,19 1.

1[
CT]Cor
rec
ti
onst
othi
scodewe
ret
ake
nfr
om t
heJ une1 982Ass e
mb l
yLi nesartic
le.
Additi
onalcomme ntswe r
ea dded,foll
owingthosefrom BobS ande r
-Cederlof
’sAppl e
-
so4disas
semblyathttp:
//
www. txbobsc.com/
scsc/
scdoc ument
or/.
2[CT]⇢ene xt128bytes($20807$20FF)c orr
espondt os c
ree
nl i
nes8 ,71,and1 35.⇢e
128bytesa4erthatma ptolines16,7 9,143
,ands of orh.⇢eFr
t steig
htlinesstartat:
$2000,$2400,$2800,$2C00,$3000, $3400,
$3800, $3C00.Seechapter31forde t
ail
s.

]
1
92 As
sembl
yLi
nes

I
tisnotesse
nti
alatt
hispoi
ntthaty
oubee nti
rel
ySuentintermsofwhi ch
l
inec or
res
pondstowhichme moryrang
e;onl
yt ha
ty our
ea l
iz
ethatthes c
reen
doesnotFlli
nqui t
ethepat
ter
nthatmightot
herwis
ebeex pect
ed.Fortunat
el
y,
therouti
nejustgi
vencancalc
ula
tethebaseaddre
ssofanyhor i
zontalli
newe
wishtoacce
ss.

Anot
herPr
obl
em:
Shi
5ingCol
ors
Ent
ert
hef
oll
owi
ng:
HGR
HCOLOR = 1
HPLOT 0,0
CALL 62454
HCOLOR = 5
HPLOT 0,0 TO 100,100

⇢eFr s
ttwos t
epsaref ai
rlyinnoc ent;the yme relyselectandc lea
rthehi -res
page,thensett hec olortog reen.Tr yingt oHPLOT0,0g ive stheFr s
tproblem:i t
doesn’tseemt owor k.⇢i si sc onsistentwi ththewa rningg ivene a
rli
er,t hat
even-numbe redc ol
or splotonl ye venc oor dinates
,a ndodd- numbe re
dc olors
plotonlyoddc oordinates.Gr een,be inga nodd- valuec olor,isnotpl ott
e da t
X=0 .
⇢ eCALL62454i sac a l
ltoar outinet ha tclea
rsthes c reentothelastc olor
plott
ed(whe therornott her esultwa sv is
ible )
.A4e ry ous etthec ol
ortoor ang e
(HCOLOR=5) , a
na tte
mptt odr awadi ag ona llineproduc esas eri
esofrectang les
.
Wha taccountsf orbot hofthe s
ee Le c
ts?
You’l
lrec a
llt hat4 0by te
spe rli
nea reus e dtohol dtheda t
at odispl
ayt he
280dotpos it
ionsone achline.⇢e rea reeig htbitsinaby te,giv
ingusat ota lof
320bi t
stowor kwi th.Asi thappe ns,onl ys ev enofea che i
g htareusedinma p-
pingthedisplaye dscreendot s(7d4 0=2 80dot s)
.
Considerthei ll
us t
rat
ionbe low:

Addr
ess
: $2000 $2001 $2002
Bi
t:0 1 2 3 4 5 6 7 0123456701234567

:V GVGVGV0 GVGVGVG0VGVGVGV0
Col
or BOBOBOB1 OBOBOBO1BOBOBOB1
Xc
oor
dina
te:0 1 2 3 4 5 6 ⇥ 7 8 9 10111213 ⇥14151617181920 ⇥

Fi
gur
e20-
1:Bi
tPos
iti
onsa
ndSc
ree
nCol
ors
2
0.S
truc
tur
eoft
heHi
-Re
sDi
spl
ayS
cre
en 1
93

Wha tFigure2 0 -
1showsi sthec olora ndpositi
ona s
si
g nmentofe ac
hbi t
wit
hintheFr s
tthr e
eby t
esofme mor yf orpa g
eoneoft hehi -ressc
reendispl
a y
.
Alt
houghonl ytheFr stt
hreebytesofline0a r
eshown, thepa tt
ernholdsf
ort he
ent
iredi
splay.
Notethef ol
lowingma jorpoints
:
1. Note verycolorc anbedi s
playeda te ver
yXc oordinat
e .Speci
Fcal
ly
,e ven
col
ors(vi
olet=2 ,blue=4 )areavai
labl
eonl yone venXc oordinat
es.Oddcolors
(gr
een=1 ,orange=5 )area v
ail
ableonlya toddXc oordinates.
2. Wi t
hinanyby t
e,bit7isusedtode ter
minewhi chrow–toporbottom–is
sel
e c
ted.⇢ismea nsthatforanypart
iculargroupofsevendotpos
it
ions,r
epre
-
sentedbyas i
ngl
eby te,onlythecol
orsine i
therthetoporbott
om rowscanbe
showna toneti
me .Fore x
ampl e
,iti
sno tposs
ibletohavegr
eenandorangedot
s
displ
ayedsimul
taneouslywithi
nthesames even-dotg
roup.
3. ⇢eor deroft hecolor swi t
hi ne ve ryothe rby teisr ev
e r
s edwi thres pe c
ttoi t
s
ne i
ghbor s.⇢i sistoe ns ur etha tthei ndividua lc olorspr ope r
lya lt
er na t
ewi th
successiveXpos it
ions,suc ha sbe twe enby tes0a nd1 ,1and2 ,ands oon.
⇢ec ol orchartisshownbe low:
Now pe rha psitwi llma k eal ittlemor e
Se t1 Se t2 sens e.S
e t1c olor sarea l
l t
hos eselec tedwhe n
0=Bl ack1 4=Bl ack 2 thehi gh-or derbi tisoL( bit7=0 ).S e
t2a re
1=Gr e e
n 5=Or ang e allthoses electe dwhe nt hehi g h-or derbitis
2=Vi olet 6=Bl ue on( bi
t7=1 ).Anya ttemptt opl otapoi nt
3=Whi te1 7=Whi te 2 from ones etwi l
lconv erta nye xis ti
ngdot s
from t heot he rs et,pr ovideda lldot sa re
de Fnedwi thinac ommonby te.Obv iously
,
plotti
ngadota tXc oordina t
e7( by t
e$2001)wi llnotha vea nye Le ctondotpos i
-
tions0t o6 ,sincetheya res toredinas eparateby te( $2000).
Whi t
ei sdr awnbyt ur ningont woa djacentdot s,ei
the rav iolet-gr eenpa i
r
forwhi t
e1,orabl ue-
or ang epa irforwhi t
e2.Conv ersel
y,bl ackisf orma ll
ydone
byt urningoLt wodot sa tonc e,thepa irofwhi chwoul dc orrespondt ot heone s
us e
df orawhi teplotasjus tde s
cr i
be d.
Wi t
hinapa rt
icularby te,bit7wi ll al
way sbel e4i nas ta t
ede termi ne dbyt he
na t
ureoft hel astcolorpl ot,regardl essofhow ma nydot swe repr ev iousl
yi n
someot herpa r
ti
cularc ondi ti
on.⇢i si swhyt hee a rl
ierdia gonallinepl otacted
sos tr
a ngely.Byc lear
ingt hes creent og reen,e verys c
reenby tewa ss ets oa sto
ha vetheg reenbitsona ndt hev ioletbi tsoL( bit7=0 ).SeeFig ure2 0-2.

]
1
94 As
sembl
yLi
nes

Addr
ess
: $2000 $2001 $2002
Bi
t:0 1 2 3 4 5 6 7 0123456701234567
Va
lue
:0 1 0 1 0 1 0 0 1010101001010100
Col
or: G G G ⇥G G G G⇥ G G G ⇥
Xc
oor
dina
te:0 1 2 3 4 5 6 ⇥ 7 8 9 10111213 ⇥14151617181920 ⇥

Fi
gur
e20-
2:Bi
tVa
lue
sforGr
eenPi
xel
s

Locat
ion$2000, f
orexample,wouldholdthev al
ue$2A. Si
ncethepa tt
ernis
shi4edfor$2001,ana l
l-
greendotgroupwoul dcorres
pondt othevalue$55. To
addt otheconfusi
on,reme mberthatFigure20-1showst hebitsinther ev
erse
orderfrom t
henotationnorma l
lyusedinthisbook.Ordinaril
ywe ’
ds howloca-
ti
on$2000hol dinga$2Ai nbinarynotati
ona s
:00101010.Sincethescreendots
aredispla
yedbyl east-
sig
niFcantposit
ionFr s
t,though,thi
si sreversedwhe n
showingas c
reendispla
ytoma keiteas
iertoint
erpret
:
$2A = 00101010 f(
rev
ers
etoma
tchFi
gur
e20
-1)f 01010100

a
ndf
ort
heot
herby
tes
:
$5A = 01010101 f(
rev
ers
ed)f 10101010

Whe nHPLOTt ri
edtodrawanor angedotat0,0wewoul dordina
ril
y
expectnoe Lect.However,
thehighbitwa sreve
rsed,andthisconvert
edthedis-
playofallcurr
entgreendotst
oor a
ng e.
Atalloddc oordi
nate
sthedirec
tplotissucces
s f
ul,butallr
ema i
ningdotsi
n
theparti
cularbytesti
ll
conver
tedtotheirhig
h-bit
-one quiva
lents
.
Fig
ure2 0-3showstheconte
ntsof$2000t o$2002a 4ertheorangeHPLOT.

Addr
ess
: $2000 $2001 $2002
Bi
t:0 1 2 3 4 5 6 7 0123456701234567
Va
lue
:0 1 0 1 0 1 0 1 1010101101010101
Col
or: O O O ⇥O O O O⇥ O O O ⇥
Xc
oor
dina
te:0 1 2 3 4 5 6 ⇥ 7 8 9 10111213 ⇥14151617181920 ⇥

Fi
gur
e20-
3:Bi
tVa
lue
sforOr
ang
ePi
xel
s
2
0.S
truc
tur
eoft
heHi
-Re
sDi
spl
ayS
cre
en 1
95

Anot
hers
mal
le
rbute
qua
ll
yannoy
inge
xampl
eiss
hownbyt
hiss
impl
epr
o-
c
edure
:
HGR
HCOLOR = 1: HPLOT 1,0
HCOLOR = 5: HPLOT 5,0

HGR
HCOLOR = 1: HPLOT 1,0
HCOLOR = 5: HPLOT 6,0

HGR
HCOLOR = 1: HPLOT 1,0
HCOLOR = 5: HPLOT 7,0

Stept hr
oughe achstate
me ntcareful
ly
,not i
ng whatha ppensa 4erthe
att
e mpttoplottheorangedot.IntheFrstcase
,theFrstgre
e ndotisconvert
ed
eventhoug hthedotsarevi
sual
lysepar
a t
ed.⇢isisbecausetheyarebothdeter
-
mine dwi t
hinthesameby t
e.Inthesecondcase,eve
nthoug htheseconddotis
notplotted,t
hec onve
rsi
onstil
loccur
s.Inthethirdcas
e,thesecondplotusesa
seconda nddisti
nctbyt
e,sotheFrstdotisunaLecte
dre g
ardles
softhec ol
orof
thesecondplot.

Ot
herPr
obl
ems
:Whe
nIsWhi
teNotWhi
te?
Answe r
: whe ny ou’replot t
ingonlyonedota tatime .
Inthel astfewpr ogramsi nv olv
ingt hemov ementofhi -resdot s,y ouma y
havenot i
cedt ha ta ts l
ows peedst hecoloroft hedota lt
e r
nate dbe twe e nv i
ol e
t
andg reende pendi ngoni t
spos i
ti
on.S imilarly,eventhoug hwes peciFe dwhi t
e
asthecolortobeus edi ntheboxf ramedr a wna tthebeg i
nningofe a
c hpr ogr
a m,
thele4verti
ca lli
newa sv i
oletwhi l
ether i
g htonewa sgreen.
⇢i sisbe causewhi tedoe snota ctuallytur nont wodot sa tonc e .Wha tit
real
lydoesisl eteithe rdot(v iol
e t
/gree
noror a
ng e/bl
ue)bea ccepta bl
ef orag iven
HPLOT.Whi tea ppe arsonl ywhe ntwoa djace ntdotsa r
edra wn, usua l
lya sar e
s ul
t
ofalinebeingdr a wnwi thsomede gr
eeofhor iz
ont alt
il
ttoi t
.
Inthemov ing -dotpr og rams,thedota ppea r
swhi tewhe nmov inga thigher
speedsbecaus ethea lternat
ionbe tweenc olorsoc cursquicklye noug ht ha tyour
eyetendstodot hebl endingonat imeba sis,rat
he rthantheus ual pos i
tiona lone .

Supe
rHi
-Re
sGr
aphi
cs
⇢el asttopi
cf orthischa
pterisnotapr oble
m,butr at
heranunhe ra
lded
beneFtofthiscraz
ys ys
tem ofscr
eendisplays
.Youma yhav enotic
edinthepre-
vi
ouse xampl ethatwhe nthes econddotwa splot
ted,theg ree
ndotmov ed
sl
ightl
ytother i
ghtwhe nitchangedtoor a
nge.Upuntilnow, you’
vebeenledto
bel
ievethatthev i
olet/
blueorg r
een/or
a ngeopti
onsfore achbitrepr
esent
eda

]
1
96 As
sembl
yLi
nes

uniquescr
eenposi
ti
on–asi
ngledot.Forthe280-
pointmodel
ofthescr
een,
they
do.Fore x
ample,ei
the
rvi
ole
torbl uec a
nbepl ot
tedwithanHPLOT0,0stat
e-
ment.
Inre
ali
ty,
however
,amoreaccuraterepr
ese
ntat
ioncanbeconst
ruct
edasin
Fig
ur e2
0-4.

Addr
ess
: $2000
Bi
t: 0 1 2 3 4 5 6 7

:V G V G V G V 0
Col
or B O B O B O B 1
Xc
oor
dina
te:0 1 2 3 4 5 6 7 8 9 10 11 12 13

Fi
gur
e20-
4:Bi
tPos
iti
onsa
ndCol
orsf
or560-
dotMode

Inthi
smode l
, youc a
ns eethatthehigh-bit-
onc olorsareshi4e dahal
fposi-
tiontotherig
htoft hehigh-bit
-oLc olors
.Wha tthisme ansistha tyoucanpl
ot
pointsi
na5 60-
dotmode ,gi
v i
ngamuc hbetterresol
utiont hant heusual28
0-
pointmode .
⇢i sinvolvesenoug hcalc
ulati
ont hatit’
sbestdonei na sse
mblyl
an-
guage.I
nt henextc hapterwe ’
l
li nves
tig
atethet echniquesforpl otti
nginal
lof
theseva
r i
ousmode susingsomene wroutine
s .
2
1
2
1. Hi
-Re
sPl
ott
ingi
nAs
semb
ly
J
une1
982

Inthepr e
viouschapterwel ook e
dathowt heApplehi-ressc
re enisse
tup
a
ndathowe ac
hdotont hescr
e e
ni sli
nkedt
oabi tpos
iti
onofaby teinme mory.
Inthischapte
rwe ’
llpresentamor edet
ail
ede xpl
anat
ionofplot t
ingapoint
a
nd,morespeciFc
all
y,provideroutinesf
orsomene wwa ysofplot
tingtothehi-
r
ess
cre
en.

Nor
mal
Poi
ntPl
ott
ing
InFig ure2 0 -
1( Cha pter2 0)wes awhowt hehi -
resscre encolorsa r
ema ppe d
outi nme mor y.
You’llr eme mbe rtha twec oulda ccesseithe rthev i
olet/greenorbl ue /
ora nge
dotpa i
rsde pe ndingonwhe t
he rthehi gh-orde rbit( bi
t7 )oft heby t
ei nque stion
wa ss et
.Topl otac olordotont hes c
r eenwene edt oc arryoutt hef ol
lowi ng
steps:
1. Us et heYc oor dina tetode t
ermi neonwhi chhor izont alscre
e nlinet opl ot.
Be causethel inesa renotma ppedc ontinuous ly,as pecialr outi
nei sus edtoc al
-
culatetheba sea ddr ess. Inthisc aset het e
r mr eferstothea ddressassociatedwi th
theFr stby teont hel ineg ivenbyY.
In nor ma lAppl es o4,t hisba sea ddressi sc al
ledGBAS ( “Gr a
phi c
sBa s
e
addr ess
”)a ndi ss tor edi nt heby tepa ir$26,$27.Loc ationHPAG( $E6=Hi - re
s
PAGe )isus e dtoi ndi ca t
ewhe thert hepl otist obeonpa ge1orpa ge2oft hehi -
ressc re
en.
Asitha ppens ,wec a nuset heHPOSN( $F411) routi
nei nAppl e
so4t odot his
ca l
culati
onf orus ,butt helisti
ngi nc ha pter2 0( HIRES BASE ADDRESS CALCULA-
TOR) isprov ide dfory oure nte r
tainme nt,andf orpos si
bleus eifyous houl d
de ci
det owr it
ea nAppl eso4-inde pende ntrout i
ne .
2. Onc et heba s
ea ddr essofthehor izontallineha sbe ende t
ermine d, t
hepos i
-
tionoft heby ter e
la tivet othel e4e dg ene edst obee stablis
hed.Be causesev en
dotsa res tor edi ne a chby t
e,theby tewene edt oa cce
ssc anbede ter
mi nedby
dividingt heXc oordi na teby7 . ⇢i sr esultisstor e
di nloca ti
onHNDX( $E5=Ho r
-
izontaliNDe X).Itisus e dbyput ti
ngt hec onte ntsof$E5i ntotheY- Re gi
ste
rf or
anLDA($26),Yope r ati
on–butmor eont hatlater.

]
1
98 As
sembl
yLi
nes

ForX=Ev
en ForX=Odd
$F6F6: $00 = 0000 0000 Black1 (0) $00 = 0000 0000
$F6F7: $2A = 0010 1010 Green (1) $55 = 0101 0101
$F6F8: $55 = 0101 0101 Violet (2) $2A = 0010 1010
$F6F9: $7F = 0111 1111 White1 (3) $7F = 0111 1111

$F6FA: $80 = 1000 0000 Black2 (4) $80 = 1000 0000


$F6FB: $AA = 1010 1010 Orange (5) $D5 = 1101 0101
$F6FC: $D5 = 1101 0101 Blue (6) $AA = 1010 1010
$F6FD: $FF = 1111 1111 White2 (7) $FF = 1111 1111

Fi
gur
e21-
1:Appl
eso5Col
orMa
sks

3. ⇢ec olorma skne e dstobes etup. ⇢ec olorma skisabi tpatter


nt ha tshows
whi chbi tsi naby tea rea ccept ablepos si
biliti
esf orapl ot.⇢ec olorma skis
storedi nl ocation$E4 ( COLBYTE) .Ra t
he rt hanl iter al
lycalculati
ng,Appl eso4
storesa lloft hepos siblec olorma sk sstarti
nga tlocation$F6F6( seeFig ure2 1-1 )
.
One sa ndz erosa reus e
dt oi ndi catewhi c hdot sa reona ndwhi cha reoLf or
thec olori ndicated. Bla ck1i sthes impl e
st: i
tisa chiev edbyt urninge verydotoL.
Whi te1i sitsconv e rse, achievedbyt urninge v er
ydoton. Not ethatbit7doe snot
corre spondt oadi splay eddota ndi sle4a0( hi g
hbi toL) .
Ify ouc ompa r et hec olorma sk sforg r e
e na ndv iol
ettot hec hartinFi gure
20 -
1 ,y ou’ l
lnot et ha tt heone sma tcht hea vai
labledot sfortheg ivenc olori na
by t
e. Re me mber ,theor de roft hebi tsisre versedwhe nma ppingt othes c reen,so
thatbi ts0t o6a rema ppe dl e4t or i
g htont hes c
r een.
⇢es e c
onds etofma sksi nFi g ure2 1-1a ret hec ol
orswi ththehi gh-bits et
(bit7=1 ).⇢es amepa tterna sbe forei sus ed,e xceptt hatthehi ghbi tiss etfor
allfourc olors.
Look inga tFi gur e2 0-1a gain,y ou’llnot etha tt hema sksshownont hel e4
wi l
lwor kf ora l
l eve n-a ddr e
s s
e dby te s
, t
ha tis,bytess uc has$2000, $2002, ands o
on. Fort heodd- addr es sedby tes( $2001, $2003, ands oon) ,t
hec olorsa reshi 4ed
onebi tpos it
ion.Whe nHPOSNi sc a l
led,a longwi t
hde t
erminingGBAS,i tc hec ks
theHNDXc al
culateda nd, i
ftha tisa nodda ddr e
ss,shi 4sthec olorby t
e.⇢er esult,
whe the rs hi4e dornot ,isa lwa ysputi nl oc ati
on$1C( HCOLOR1) .⇢er esultsof
suchapos si
bles hi4a res hownont her i
ghts ideofFi gure2 1-1.
(Ani nter
estingr e sultoft hispr ocessi stha ty ouc annotc leart hee ntir
e
screent oa na ctual col or[ green, violet,blue ,oror ang e]byFl li
ngme mor ywi tha
singlev alue.Tr yi t.Cl e arthehi -re ssc r
eenwi tha nHGR,t hene ntertheMoni t
or
wi t
hCALL-151. ⇢e nt y pein:
*2000:2A
*2001<2000.3FFFM
2
1.Hi
-Re
sPl
ott
ingi
nAs
sembl
y 1
99

⇢es creens houldcl


e a
rtoa lt
ernatingv e
rtic
a lbarsofg ree
na ndv i
olet.
)
4. Nowt hea ctua
lbitpositi
onofi nterestne edstobes elect
ed.⇢i sa c
tuall
yha s
alre adybe endonebyHPOSN. ⇢er e
s ultoftheXc oordinatedividedbys evenwa s
puti nHNDX,a ndther ema i
nde roft hatdiv i
sion
jus tha ppenstoc orr
espondt othea ctualbitpos i
-
tionwi thintheby t
ewewa nt.⇢eonl yrema ining $F5B2: $81 = 1000 0001
pr oble mi sthattheresul
ti sanumbe rf r
om 0t o $F5B3: $82 = 1000 0010
6, andwha twene edisaby tewithonl ythatpa - $F5B4: $84 = 1000 0100
r
$F5B5: $88 = 1000 1000
ticula rbitturnedon. ⇢isisa gai
nde rivedfrom a $F5B6: $90 = 1001 0000
tabl ewi thinAppl eso4 (i
nt hisc ases tart
inga t $F5B7: $A0 = 1010 0000
$F5B2–s eeFi gure2 1-
1).⇢er es
ul tfrom t hi
s $F5B8: $C0 = 1100 0000
tabl eisthenputi nlocati
on$30( HMASK) . Figure21-2:BitMa sk
Nowa tlas
twe ’
rereadytodot hea ct
ualpl ot.
⇢epl otti
ngs equence(norma l
lyf ounda t$F45A)l ookslikethis:
F45A- A5 1C LDA $1C ; HCOLOR1
F45C- 51 26 EOR ($26),Y ; (GBAS),Y
F45E- 25 30 AND $30 ; HMASK
F460- 51 26 EOR ($26),Y ; (GBAS),Y
F462- 91 26 STA ($26),Y ; (GBAS),Y
F464- 60 RTS

⇢isl
astope
rat
ioni
spr
oba
blybe
stc
lar
iFe
dwi
tha
nac
tua
lex
ampl
e.
Gi
ven:
HGR
HCOLOR = 1
HPLOT 15,0

Pr
ocedure:
1. JSR $F3E2(
HGR)
.Cl
ear
sthehi
-r
ess
cre
en.
Set
sHPAG(
$E6)t
o#$20.
2
. LDX #$01
JSR $F6F0(
HCOLOR)
⇢isput st
hema skvalue%00101010inHCOLOR1(
$E4)
.
3
. LDX X(l
ow- orderbyteoftheXc oordi
nate
)
LDY X+1(high-orde
rby teofX)
LDA Y(Yc oordinat
e)
JSR HPOSN
Notethattheperc
entsign(%)inthema skv a
lueisus
edtoindica
tethebinar
y
f
orm ofanumbe r
.⇢i sformi susedint heremarkport
ionsofma nyofthe
s
ourcelis
tingsi
nthisbookasana ddedaidtotheexpl
anat
ions.Al
thoughsome
as
semblersal
lowbinar
ynumbe rsi
nt heoperand,wehavel
imit
e dt
heiruseher
e
t
other e
ma rkFel
dtoreducecompa t
ibi
li
typroble
ms.

]
2
00 As
sembl
yLi
nes

⇢eproceduregi
venabovewill
:
a
) Ca l
culat
ethebaseaddres
susi
ngthepageinde
xa t$E6(usua
ll
y$20).I
nthi
s
c
aset
heresultwi
llbe$2000.
⇢er es
ulti
sst
oredinGBAS,GBAS+1(
$26,$27)

b) Divide15( t
heXc oordinate
)by7 .⇢er esult(
2)isputinHNDX( $E5).⇢e
remai
nde rofthedivi
si
on( 1)isuse
dtoa cces
st hebi
tma s
kt abl
e.⇢er es
ultof
thi
sta
blelookup(%10000010foundat$F5B2,Xwhe r
eX=1)isputinHMASK($30).
c) Che ckHNDXtoseeifthebyt
eoLsetisodd.Ifso,shi
4t hecolorby
tema s
k.
Sinc
eint hi
scase$E5holdsa2,noshi4isrequi
red.⇢ust hecolormask%0010
1010isputi
nHCOLOR1($1C)inprepa
rat
ionfortheplot
.
4
. JSR $F45A(
HPLOT)
.⇢i
scompl
et
est
hepr
oce
sswi
th:

LDY HNDX ($E5) = '2'


LDA HCOLOR1 ($1C) = %0010 1010
EOR (GBAS),Y ($2002) = %0000 0000
%0010 1010 (EOR’d)
AND HMASK ($30) = %1000 0010
%0000 0010 (AND’d)
EOR (GBAS),Y ($2002) = %0000 0000
%0000 0010 (EOR’d)
STA (GBAS),Y ($2002) = %0000 0010

screen looks like: 0100 000- Green dot lights!

⇢ene teLe
ctofst
ep4istosay:“
Lookatthebi
tmaskpat
ter
nandcompareitt
o
thecolormask.
Ifthe
reisaoneinthecol
ormaskatt
hegive
ndotpos
it
ion,tur
n
thatdoton(se
tthebitt
o1 )I
fthere
’sa0attha
tposi
ti
on,t
urnt
hedotoL( c
lea
r
bitto0).

Al
ter
nat
ePl
ott
ingMode
s
S ofar,allweha vereall
ydonei stoe xpl
ainf urthe rsome thingwewe re
alreadyus i
ng .⇢i sne wexplanati
onma kespos s
ibl
es omea lte
rnativewa ysof
plotti
ngtot hehi -resscr
een.Inf a
ct,byusingtheex i
sti
ngAppl e s
o4r outines
,the
newr out
inesa rer a
thershortand,bestofall
,easytoe x
pl ain.Ifyoua reunsett
led
ri
g htnowa boutt heFnerdetail
softhema s
kingoperati
ons ,don’twor r
y. ⇢er ea
l
pointofa l
ltha tistog i
veyous omefe e
lforthegeneralpr ocessesinvolved.
Fors tar
ter s
,l et
’srevi
ews omeba sicproblemse ncount e
reds of a
rwi t
ht he
nor ma lAppl eso4HPLOT.⇢eFr sta r
iseswhe nyout ryt opl otusingj us
tone
color.Bys ett
ingHCOLORe qualto1 ,2,5,or6 ,wel i
mi tt hepos si
bledot swhich
canbepl ottedt oe ve
ryotherdotont henor malscree
n. ⇢i scanbedi sconcert
ing
whe ny ouha veas t
atementli
ke :
HCOLOR = 1: HPLOT 100,100
2
1.Hi
-Re
sPl
ott
ingi
nAs
sembl
y 2
01

andnot hi
ngha ppens.⇢er eas
onsf orthi
swe r
edi s
c usse
di near
lie
rchapter
s,but
nowi tshouldbee venmor eobviousthatthecolorma s
ks pec
iFesonlyodd-dot
posit
ionsforHCOLOR=1, ma ki
ngitimpos s
ibl
etopl otatX=1 00.
⇢es econdpr oble
m occurswhe nyou’
replotti
ngwi thHCOLOR=3orHCOLOR
=7.Ev enthoug hweha v
es peciFedwhite,ana ttemptt oplotas i
nglepoint
al
wa yscome souta sacol
oreddot. I
tisonlywhendr awingmor etha
nonepoi nt
(suchasinal ine)thatwhiteappears.Let
’sexaminepos s
ibl
esoluti
onstothese
problems.

140-
Poi
ntRe
sol
uti
onMode
Fort heFr stproblem ofinvis
ibl
epoints,onesol
uti
oni stoacceptthatther
e
areonly1 4 0pointsava i
labl
eforag i
vencolorandtoalt
erourf r
ameofr e
ferenc
e
tor e
cog nizethatreali
t y
.Ane asywa yofdoingthisi
stoa l
wa ysworkwi thanX
coordina t
ev alueint her angeof0t o1 39,andthentodoubl ethev a
luewhe n
actuall
ydoi ngt heHPLOT.⇢ema indr a
wba cktothi
sapproachisthes peedloss
duet othemul ti
pli
cations,andthefactt
hatoddc ol
orvalue
smus talsobes hi
4ed
byone( sinceoddc olorscanonlyplota toddXpos it
ions).⇢es itua
tionnow
woul dlookl i
ket hi
s:
HCOLOR = 2 HCOLOR = 1
X = 15: Y = 30 or X = 20: Y = 30
HPLOT X*2, Y HPLOT X*2 + 1, Y

Anot
herappr
oachistocre
ateanass
embly-
lang
uag
erout
inet
odot
hisf
or
usa
utomat
ica
ll
y.Her
e’st
hesourcel
is
ti
ngfors
ucharout
ine
.
1 ********************************
2 * AL21-HIRES PLOT.140 *
3 * *
4 ********************************
5 *
6 *
7 * OBJ $300
8 ORG $300
9 *
10 CHKCOM EQU $DEBE
11 FRMNUM EQU $DD67
12 GETADR EQU $E752
13 LINNUM EQU $50
14 COMBYTE EQU $E74C
15 *
16 X EQU $E0
17 Y EQU $E2
18 *
19 HCOLOR EQU $F6F0
20 HGR EQU $F3E2
21 HPLOT EQU $F457
22 HPLOT2 EQU $F45A
23 COLBYTE EQU $E4

]
2
02 As
sembl
yLi
nes

24 *
0300: 20 BE DE 25 ENTRY JSR CHKCOM
0303: 20 67 DD 26 JSR FRMNUM
0306: 20 52 E7 27 JSR GETADR
28 *
0309: 06 50 29 CALC ASL LINNUM
030B: 26 51 30 ROL LINNUM+1 ; X*2
31 *
030D: A9 02 32 LDA #$02 ; %0000 0010
030F: 24 E4 33 BIT COLBYTE
0311: F0 06 34 BEQ C1 ; NO MATCH COLOR EVEN
0313: E6 50 35 INC LINNUM
0315: D0 02 36 BNE C1
0317: E6 51 37 INC LINNUM+1
38 *
0319: A5 50 39 C1 LDA LINNUM
031B: 85 E0 40 STA X
031D: A5 51 41 LDA LINNUM+1
031F: 85 E1 42 STA X+1
43 *
0321: 20 4C E7 44 GETY JSR COMBYTE
0324: 8A 45 TXA ; PUT Y-COORD IN ACC
0325: A6 E0 46 PLOT LDX X
0327: A4 E1 47 LDY X+1
0329: 20 57 F4 48 JSR HPLOT
49 *
032C: 60 50 DONE RTS
032D: C1 51 CHK

⇢ispr ogra
mi sde s
ignedtobec a
ll
edfrom Apples
o4,servi
nga sas ubr
ou-
tinefora nunde Fne dov er
a l
lprogram.⇢ea dvanta
geoft her outi
nei stha
t
HCOLORma ybes ettoa nyvalue,al
thoughwhitewil
lsti
llpl
otonlyonec ol
or.Val
-
ue sf
ortheXc oordinatema yrangefrom 0to139.
Assumingtha ttheroutineisloadedst
arti
ngatlocati
on$300( 76
8de ci
ma l
),
thesynt
a xforcal
lingitwouldbe :
CALL 768, X, Y

whereXa ndYa ret hec oordi


natesfort hedesir
e dplot.
Examiningt hel ist
ing,youwi llse ethattheFr ststepistous etheca l
lsto
Appleso4onl i
nes2 5t hr
ough2 7t or etri
evet heXc oordi
na t
ef rom Appl e
so4.
⇢er e
sulti
ngt wo-by ter e
pre
s e
ntationf orthev a l
uewi l
lendupi nLINNUM( $50,
$51).
Onceweha vet hev al
ueforX, ther emainingpr oc
e s
sisv e
rys tra
ightf
orwa r
d.
⇢eXc oordinateisdoubl edbyt hepa irofle4s hi4sonl i
nes29a nd3 0.Nex t
,the
col
orby teischecke dt oseei
ftheHCOLORpr e v
ious l
yse l
ecte
dwa sanoddore ven
col
orv al
ue.Abr iefl ooka tt
hec olorma skcha rtinFigure2 1
-1s howst hatbit1
(ra
therthanbit0 )ist hekeytowhe therac olorisoddore ven.Ifthecolorisodd,
LINNUMisincreme ntedbyonet oselectthene xtoddX- coordinatepositi
on.
2
1.Hi
-Re
sPl
ott
ingi
nAs
sembl
y 2
03

⇢eYc oordinateisthenretrie
vedusingCOMBYTE.Si
nceYcannotbelarger
t
han191,theone-byteretri
evalr
out i
nec
anbeus ed.
Atthatpoint,t
heus ualca
llt
oHPLOTi sdonewiththenewXcoordinate
.
Alitt
lerumina t
ionont hisrouti
neshouldconvinceyout
hatiti
sf unct
ion-
a
ll
yidenti
caltothisBAS ICalg
orithm:
0 HGR: HOME: VTAB22
10 INPUT “HCOLOR”;C : HCOLOR = C
20 INPUT “COORDINATES:”; X,Y
30 X = X * 2
40 IF C / 2 <> INT (C / 2) THEN X = X + 1
50 HPLOT X,Y

⇢ea ss
embly-l
anguagerouti
negivencanal
waysbeuseddir
ect
lyfr
om othe
r
a
sse
mbly-la
nguageprogramsbyde le
tingli
nes25throug
h2 7andc ha
nging44
a
nd4 5toreadLDYY.⇢er outi
newouldthenbecal
ledbyputti
ngthedes
ire
dX
c
oordi
nateinLINNUM($50,$51)
,andtheYcoordi
nateinY($E2)
.

560-
Poi
ntRe
sol
uti
onMode
⇢edi sadv anta geoft he1 40 -pointme thodj usts howni sthatt her esolution
oftheg raphi csi sobv i
ous l
yl imi t
e d.⇢i si spa rti
cul arl
ya pparentina ttempt sto
draw ne ar-v erti
c alline s
;i t
’se asyt oobs ervet hede gr eeofs tair
- steppingt ha t
occur s.Low- resolut i
onpl ot t
ingmode spr oduc ever ybr oke nne ar
-ve rti
ca ll
ines.
Ifcolori snotac onc ern( suc ha swhe nus ingabl ack-a
nd- whi temoni tor),
thenwhynotj us tplotus i
ngwhi te?Sinc ewewon’ tk now t hatt hec olor sa r
e
actua ll
yv ary i
ngde pendingont heXc oor dinatespe ciFe d,ablack-a nd- whitedi s-
playwi l
llookFne .
We ll,iftha t’
st hec a
se, t
heny oumi ghta swe llg ofora l
lyouc a ng eta ndus e
the5 60-pointmode .⇢et heoryt othismodei stha tt hehi gh-orde rbi tofe ach
screenby tec a nbeus edt oc hoos ebe t
we endot ss hi4e done -halfofapos i
tion
withr es
pe ctt ot heus ual2 80-poi ntmode .⇢ea rgume nta gainstthi sme t
hodi s
thatt hepl ottingofdot swithint hes ameby tec andi storttheFr stby teplotted.
Fore x ampl e ,iftheFr stdotpl ott
edi sont hef a rt
he stle4pos itionpos si
ble
(highbi toL) ,the nas ucces s
ivepl otofa nyHCOLORwi t
ht hehi ghbi ts et(HCOLOR
=4t hroug h7 )wi llcha nget hec oloroft hedota nds hi4i ttother i
g ht. Asi thap-
pens ,thisi snotmuc hofa na rgume ntsinc ethes amehol dstruef ort henor ma l
280 -pointmode ,a nde venf ort he1 40-poi ntmode .⇢ei nescapabl ef actistha t
plottingt woc olor swi thconSi ctinghi gh-bi tconditionswi thi
nt hes ameby tewi ll
alwa ysaLe c ttheFr stdotpl otted. Ifthedi storti
oni suna voidablethe ny oumi ght
aswe llenjoyt hebe ne Ftsoft hehi gherr esoluti
on,e spe c
iall
yi fyou’ reg oingt o
hav et ocopewi t
ht hedi st
or t
ionpr oblema nywa y.
Wi t
houtf urthe rintroduc t
ion, herethe ni sar out i
nei mpl ement ingt he5 60-
pointpl ottingmode .

]
2
04 As
sembl
yLi
nes

Li
ket
hePLOT.140rout
ine,t
hisisass
umedtobeloade
dat$300a
ndwoul
d
bec
all
edi
nama nneri
denti
cal
tothatfort
hepr
eviousr
outi
ne:
CALL 768, X, Y

⇢ema
indi
Ler
enc
ehe
rei
stha
tXc
annowha
vear
ang
eof0t
o55
9,a
ndt
hat
HCOLORi
sal
way
sse
ttowhi
te
.Aswi
thnor
malAppl
eso4,wha
tthi
sre
all
yme
ans
i
sthatwe’
llt
akea
nycol
orwecangetf
oragi
venpl
ot,a
ndthatt
ruewhi
tewil
l
r
esul
tonl
ywhe ndot
sar
eplot
teda
djac
entt
oeachot
her.He
re’
sthel
is
ti
ngfor
t
hisr
outi
ne:
1 ********************************
2 * AL21-HIRES PLOT.560 *
3 * *
4 ********************************
5 *
6 *
7 * OBJ $300
8 ORG $300
9 *
10 CHKCOM EQU $DEBE
11 FRMNUM EQU $DD67
12 GETADR EQU $E752
13 LINNUM EQU $50
14 COMBYTE EQU $E74C
15 *
16 X EQU $E0
17 Y EQU $E2
18 *
19 HPLOT EQU $F457
20 COLBYTE EQU $E4
21 *
0300: 20 BE DE 22 ENTRY JSR CHKCOM
0303: 20 67 DD 23 JSR FRMNUM
0306: 20 52 E7 24 JSR GETADR
25 *
0309: 46 51 26 CALC LSR LINNUM+1
030B: 66 50 27 ROR LINNUM ; X/2
030D: A9 7F 28 C0 LDA #$7F ; %0111 1111
030F: 85 E4 29 STA COLBYTE
0311: 90 04 30 BCC C1 ; X=EVEN
0313: A9 FF 31 LDA #$FF ; %1111 1111
0315: 85 E4 32 STA COLBYTE
33 *
0317: A5 50 34 C1 LDA LINNUM
0319: 85 E0 35 STA X
031B: A5 51 36 LDA LINNUM+1
031D: 85 E1 37 STA X+1
38 *
031F: 20 4C E7 39 GETY JSR COMBYTE
0322: 8A 40 TXA ; PUT Y-COORD IN ACC
0323: A6 E0 41 PLOT LDX X
0325: A4 E1 42 LDY X+1
0327: 20 57 F4 43 JSR HPLOT
2
1.Hi
-Re
sPl
ott
ingi
nAs
sembl
y 2
05

44 *
032A: 60 45 DONE RTS
032B: 9B 46 CHK

⇢eope rationoft hisroutinei sa l


sofairl
ysimple. Aswi t
hthePLOT.140pr o-
gram, thevaluef orXi sretri
eve df rom thec al
li
ngpr og ram.Inthiscase,
thoug h,
CALCd ivi
dest hepa s
s edv a
luebyt wo. Not ethatale4-shi4ope rati
onisused,not
therightshi4s( foramul ti
ply)t hatwe r
eus edinthe1 40 -
mode .
You’llrecallt hatLSRLINNUM+1( LSR=L ogica
lS hi4Ri ght
)wi l
lshi4allbits
inLINNUM+1( thehi g h-orderby te
)t ot herightonepos it
ion,forci
nga0a tthe
rig
htmos tpos i
tiona ndput ti
ngt heol dbit0int hecarry.⇢i sisimme dia
telyfol-
lowedbyt heROR( “rotateright ”)instructi
onwhi c
ha gainshi4sa l
lthebitsin
LINNUM( thelow- orderby te),put st hec a
rryintobi t7 ,anddr opsthelastbit0
intothec ar
ry, t
husr eplaci
ngt heol dv alue.Fore x
ampl e:
X-COORD = 289 = $121 = %0000 0001 0010 0001
LINNUM+1 LINNUM

LSR LINNUM+1: %0000 0001 ⇤ %0000 0000 (Carry=1)


ROR LINNUM: %0010 0001 ⇤ %1001 0000 (Carry=1)

⇢erathercoi
ncident
albe
aut
yoft hi
sisthatthecarr
ySagwil
lendupbe
ing
setorcl
ear
e ddependingonwhethe
rt heori
g i
nalvalueforXwasoddoreve
n.
⇢i si
sneededbe c
auseinthe560
-pointmode ,we’l
lusetheoddoreve
nnat
ure
ofXtodeterminewhethert
osett
hehighbit.
X(
560) X(
280) Col
ormasktous
e
0 0 White
1(bi
t7=0)
10 5 White
1(bi
t7=0)
2
01 1
00 White
2(bi
t7=1)
5
01 2
50 White
2(bi
t7=1)
Basical
lywha twedoi stodi v i
det heXc oordi
na tebyt wot og e
tav alue
ac
ceptabl
et onor malAppleso4,a ndt henforcethec olort obee i
therwhite
1or
whit
e2de pendingonhowwewa ntt hehighbits e
tintheFna lplot
.
Lines28t hrough32setthec olorma s
ktothea ppropr iat
ev al
uebychecking
t
hec a
rrySa gt oseeiftheorigi
na lv a
lueofXwa soddore ven.⇢e nLINNUMis
t
ransf
erredtooura ctua
lX-coordi nateby t
es.⇢er outinei sthencompletedwith
t
heusua l
c a
lltoHPLOT,aswasdonei nt hePLOT.140rout ine.
⇢i sproce s
scouldbesimulate dfrom Apple s
o4wi tht hefoll
owingrouti
ne:
0 HGR: HOME: VTAB 22
10 INPUT "COORDINATES? "; X, Y
20 HCOLOR = 3 : REM WHITE1
30 IF X / 2 <> INT(X / 2) THEN HCOLOR = 7 : REM WHITE2 FOR X=ODD
40 X = X / 2
50 HPLOT X, Y

]
2
06 As
sembl
yLi
nes

I
t’
sli
kel
y,however
,tha
tyou’
llFndthea
sse
mbly-
lang
uag
erout
inec
ons
ide
r-
a
blyf
ast
er,
andcert
ainl
ymuc hea
siert
oimpl
ement
.

ADe
mons
tra
tionPr
ogr
am
Tog i
veyousomet
hingtoshow oLthes
erout
ine
s,her
e’sapr og
ramin
Appl
eso4tha
tca
ll
sbot
hrouti
nesa
ndshowsthedi
Ler
ence
sint
he i
rappear
anc
e.
10 D$ = CHR$(4): REM AL21.PLOTLINE.A
100 REM NORMAL TEST
110 HGR : HCOLOR = 3
120 FOR I = 0 TO 100
130 HPLOT I,I
140 NEXT I
200 REM PLOT.140 TEST
205 PRINT D$;"BLOAD AL21.PLOT140,A$300"
210 FOR I = 0 TO 100
220 CALL 768,I,I
230 NEXT I
300 REM PLOT.560 TEST
305 PRINT D$;"BLOAD AL21.PLOT560,A$300"
310 FOR I = 0 TO 100
320 CALL 768,I,I
330 NEXT I

Not i
c et hatt hi
spr og raml oa dse achr out inef rom adi skFl ea si t
’sne e
de d.
Basicall
yt hisillustratesthes t
ee pe s
tv ertic
a la ng l
ea twhi c hal inec a nbedr a
wn
withouta nynot iceables tair-s
te pping ,orbr ea king ,int hel ine.I ta l
soc onv e-
nientlyshowsape r
ha psune xpec tedc hangei nt hea ctualv i
sua lresultoft heplot,
event houg ha llthreelineswe redonewi ths imi larFOR- NEXTl oops .
Nor ma l
ly,the2 80 -pointmodei sc onv enie ntlypr opor t
iona l.⇢a tistosay, a
mov eofFv epoi ntshor izontallyont hes cree ni sa boutt hes amea ctua ldist
anc e
ont hescre ena samov eofFv epoi ntsv erti
ca l
ly .⇢i sensur estha tas qua r
ewi l
lin
factlook“ squa r
e”whe ndr awn ont hes cre en.⇢ust heFr stpl oti sa tthe
“prope r
”4 5de greeswhe ndr awnus i
ngHPLOT1,1.
Whe nt henumbe rofs creenpoi ntsisha lv ed, asint hec aseoft hePLOT.140
routine,ther e
s ultwillbet o“stre tc
h”t hes cre enhor izontallybyaf a ct
oroft wo.
Similarl
y,pa ckingi nt wic ea sma nypoi nts( na me ly5 60v ersus2 8 0)a crossha s
thee Lectofc ompr essingt hes c ree
n.⇢e s ee Le ctsmus tbec onsider e
dwhe n
doingg eome tr
icde signsont hes creen.
We ’
llleavei tasa ne xe rci
sef ory out odr a wt hr eepa ral
lellinesus inge achof
thethr e
emode s.
Bynow, you’ vepr oba blya l
s onot iceds omemi norSa wsi nt hec l
arityofthe
560-pointl ine.I nthene x tchapt erwe ’
llexpl or ethema tterf urther,dis c
ov er
ing
whyt hef aintspot soc cura ndhowt oFxt he m.
2
2
2
2. Ev
enBe
tte
rHi
-Re
sPl
ott
ing
J
uly1
982

⇢epr eviousc hapt erc onc l


udedwi thade mons tra t
ionpr og ramt ha tshowe d
therelati
v ea ppea ranc esofal inedrawnwi t
hthenor ma lHPLOTc omma nda swe l
l
aswiths pe cial140 -a nd5 60 -
pointmodepl ott
ingr out ine s
.
⇢ee ntirepl otti
ngpr ocesswa sba sedonamode lofpoi ntdi splayi nwhi ch
eachpoi ntont hes creenc orrespondst othes tatusofapa r
ticularbitwi thina
me mor yby te .Forg e nera lplotti
ng,Fi gure20 -1( inCha pter2 0 )il
lustrat
e sthe
correspondi ngc ol orpoi nt s.
⇢e1 40- pointmodewa sc r
eatedt oensuret hatf ora nyHPLOT- t
ypea ction,a
consiste
ntc olordotwoul da l
wa ysbepl ott
ed.⇢i sc ons istencyi snotor dinari
ly
avail
ableint heAppl e’sus ua l280-pointmode .
Fori ns tance si nwhi chc olorisnotac onc ern,a na l
terna tesche mewa s
devis
e dt hatwoul dbei ndi Lerenttot hec oloroft hedoti l
lumi nated( a sthe
vie
we rwoul dbewhe nus i
ngabl ack-and-whi temoni t
or) .Ana dde df eat
ur eof
thi
ssc hemea l
lowsar e solutionof5 60poi ntspe rli
ne . ⇢i swa sdonebyus i
ngt he
high-orderbi tofe achby tet oshi4ag ivendotone -ha lfofapos it
ion.
Whe nt heFna lde mons trat
ionpr ogram wa sr un,t hel astlinewa sdr awni n
the56 0-pointmode .Youma yhavenot ice
d,t houg h,t ha tc e
rtainpoi ntsont he
l
inewe r
er a
t herf aint. ⇢i sbr ingsust othedisc ussionofoneoft helast(?)bug s
inthehi -resg raphi csr outine s.
Cha ng et hepr e v
iousc ha pter
’stestprogramt oa ppe ara sfoll
ows :
10 D$ = CHR$(4)
40 HOME: INPUT "BLACK1 OR BLACK2? (1 OR 2)"; I
100 REM NORMAL TEST
110 HGR: HCOLOR = I*4 - 4: HPLOT 0,0: CALL 62454: HCOLOR = 11 - I*4
120 FOR I = 0 TO 100
130 HPLOT I,I
140 NEXT I
200 REM PLOT.140 TEST
205 PRINT D$;"BLOAD AL21.PLOT140,A$300"
210 FOR I = 0 TO 100
220 CALL 768,I,I
230 NEXT I
300 REM PLOT.560 TEST
305 PRINT D$;"BLOAD AL21.PLOT560,A$300"
310 FOR I = 0 TO 100
320 CALL 768,I,I
330 NEXT I

]
2
08 As
sembl
yLi
nes

Whe ny our unt hisprog ram, entere it


he r1or2t os pecifywhich“ Savor”of
blacky ouwa ntf ort heba ck g
round.Unde rnor ma lcircums tances,anHGRs t
ate
-
me ntc l
earst heba ckg r
oundt oblack 1,( hi
ghbi toLone ac hby t
e)andpl otsa r
e
doneus i
ngwhi te1.⇢i spr ogramc ha ng esthatbyus i
ngt hea l
ter
na t
ewhi tefor
theba ckgrounds elected;thatistos ay ,ifyous el
e ctblack 1fortheba ckground,
white2wi llbeus edt opl ot
.Ifyous electbl ac
k 2,white1wi llbeus e
d.
Ex aminingt hel ist
ing,then, y
ou’ l
lnot icethatline1 1 0setsHCOLORt obl a
ck1
orblac k2,doe sther equire
dpl ot,andt he nc l
earstheba ck groundtot hatcolor.A
CALL 62454wi lla lwa ysclearthec urre nthi-resscreent ot hel a
stcolorpl ott
ed
(seepa ge1 34ofy ourAp ples
oftIIBAS ICPr o g
rammi ngRe f
erenceMa nua lfora
descripti
onoft his).
Followingt hes cr e
e nclear,whitei ss e
tint hea l
terna t
emodede sc
ribede ar
-
li
er.
Lines1 00 71 40dr awadi agonal l
inepoi nt-by-pointa swa sdonei nthepr evi
-
ousc hapter,butnowt helineshoul da ppe artoha veaf ewf aintspotsinit.Ifyou
choos eblack2a stheba ckground, t
hel inewi llhavepl aceswhe r
ethedot sappe a
r
sli
ghtlylargert ha ny ou’dha vee x
pec t
e d.
Simi l
are Le ctsca nbeobs ervedi nt he1 40-and5 60 -
model i
nes.

I
nte
rac
tionsbe
twe
enAdj
ace
ntBy
tes
⇢ee nti
repr emi seoft he5 6
0 -
pointmodewa sthatt hehi gh-orderbi tof
eac
hby teaLectedt heFna ldisplaypositi
onofe achot herbitwi t
hi nit.Weha ve
see
nhowc hang i
ngt hes ta
tusofbi t7( t
hehi gh-orderbit)ma ys hi4ag ivendot
one-hal
fofapos it
ion, de pendingonwhe therthebitisset
.
Nowfort hene wwr ink l
e.Itturnsoutthatfordot sassociat
edwi thbi t6ofa
byt
e,thehigh-orderbi tofthene xtby t
einme morya ls
oa Eectsthedi s
p l
ayo fthe
?rs
tb y
te.
Asane xample ,Fr stclearthehi -r
ess c
reenwi tha nHGRa ndt hene nte
rt he
Moni t
orviatheusua lCALL-151.
Nowe nt
e rthef ollowingv aluesint
ome mory .
Yous houl dseea ne Lectsimi-
l
artothede s
cripti
ona tther i
ghtofe achstatement.
*2138: 40 (Dot is plotted; width = 1 unit)
*2139: 80 (The dot extends; new width = 1.5 units)
*2139: 00 (The dot is back to normal; width = 1 unit)
*2138: C0 (The dot grows fainter; width = 0.5 units)
*2139: 80 (The dot is back to normal; width = 1 unit)

⇢er e
ferencestoawi dthareanapproa
cht oexpla
ini
ngwha thappens.If
youhaveablack-and-whitemonit
or,
therel
at
ivevis
ualstr
engthsoft
hedotsca n
berela
tedtoa na ppare
ntwi dt
hofthedotswhe nil
luminate
dont hemoni tor
scr
een.Onac olortel
evi
sionormonitort
hewidthsaren’
tdis
cerni
ble
,butdiLer-
encesi
ncolorandbr i
ghtnessca
nbeseen.
2
2.Ev
enBe
tte
rHi
-Re
sPl
ott
ing 2
09

Be forea nyf urtherexplanati


ons ,let
’sre-e
x ami net he5 60-pointmode l
.
You’ llrecalltha talt
hought hev iole
ta ndbluedot soWc iall
yoc cupyt hesame
scr
ee n pos iti
on hor izontal
ly,ina ctuali
tyaha l
f-pos i
ti
on s hi4 ma yha ppen,
depe ndingonwhe t
he rthehighbi tisset.Whe n$2138wa ss etto$40, wewe re,as
such, plott
ingpos it
ion1 2ont hedi splay.Whe n$2138wa ss etto$C0, posit
ion1 3
wasi ll
umi na ted. ⇢eSa wc anbee xplai
ne dbyi ma giningt hatthehi gh-orderbit
of$2139( thene xtby tea4er$2138)c ana l
sopr oduc eas l
ig htshi4onadotpr o-
duce dbybi t6of$2138.⇢eg ene r
a lruleistha tfora nydotpr oducedbybi t6,
thesuc ceedingby teofme morymus tha veahig h-orderbi t(bit7)settot hesame
val
uea sbit7oft heby tebeingpl ott
ed.
Ifthisr uleisnotobs erv
ed,oneoft wot hi
ng swi llhappe n:
1.I fbi t7oft hedi splay
edby teisc learandt hene xtby tei sset
,thedotwi l
lbe
extende dore nlarged–s li
ghtly.
2.I fbit7oft hedispla
yedbyt
eissetandthenex
tbyteisc
lear
,thedotwil
lbe
re
duceds l
ightly
,res
ulti
nginafai
nte
rima ge
.
Ani nter e
sti
ngre s
ulti
stheconclusi
onthateve
nt he“normal
”methodof
pl
otti
ng( t
ha tis
,white)wil
lgi
veragg
e ddis
pla
yswhenadjac
entbytesha
vecon-
tr
aryhig
h- bitset
ti
ngs!

Some“
Newa
ndI
mpr
ove
d”Rout
ine
s
We ll
,then. .
.t
ha thasbe enal ottodig e
st.Inf act
, a
tthi spointy oumi ghtjust
wa nttot akeabr e
a ktol etever
y t
hingsinki n,ma beFxy
y our s
elfani cecupoft ea
andme dit
ateoni tforawhi le.
Gladt os eey oua gain!Oneoft hedi Wc ulti
esinpr ese nt
ingt hema t
eri
a lin
thel astfewc ha ptersha sresul
tedf r
om t hedi scoverytha thi-resgraphicsisnot
alltha tlogical.Muc hofhi -resg r
aphicss eemst obev erye mpi r
ic a
li nnatur e
.
⇢a tis,it’
smor eama t
terofa ccepti
ngt hatt hingsa r
eac ertai
nwa ya sderived
from e xperime ntati
on, thanoft r
y i
ngtoa ccountf orthei nne r
mos twor kingsofa
seemi ng l
yrandom e vent. (
Inthiscase,theinne r
mos twor kingsarer elat
edtot he
pur el
ye l
ectr
oni cwor ldofwi r
es,videopr otoc ol
s,ands of orth,whi chismos t
ly
incident a
ltot hepr ogramme r!
)
⇢ewor stisproba blyov e
r,though.Att hispointy ous houldha v eatle
a sta
gene ralfeelforhowt hedot sarema ppe dont hes c
r e
en.Le t
’snowc reat
es ome
Fna lr outi
ne st hate ncompa s
st hev ariousqui rk
soft hehi -r
ess y s
tema sit
present l
ye xi
sts.

]
2
10 As
sembl
yLi
nes

PLOT.
140+
⇢eFr s
tonet oFxi sthe1 4
0 -
poi
ntmoderouti
ne.Forallr
outi
nest
he
appr
oac
hwillbever
ydire
ct:
1. Det
erminewhethert
hedotbeingpl
ott
edi
nvol
vesbi
t6ofthebyt
eofmem-
oryi
nques
ti
on. I
fnot,
don’twor
ry.
2
.Ifbi
t6i
sus
ed,
che
ckt
hes
tat
usoft
hehi
gh-
orde
rbi
t(bi
t7)oft
heby
te.
3. Fi
xthehi
gh-or
derbitofthene
x tbyt
einmemory,i
fne
ede
d,t
oma
tcht
hat
oft
heFrs
tbyt
e.Her
e’st
hene wrout
inetodojus
ttha
t:

1 ********************************
2 * AL22-HIRES PLOT.140+ *
3 * *
4 ********************************
5 *
6 *
7 * OBJ $300
8 ORG $300
9 *
10 CHKCOM EQU $DEBE
11 FRMNUM EQU $DD67
12 GETADR EQU $E752
13 LINNUM EQU $50
14 COMBYTE EQU $E74C
15 *
16 X EQU $E0
17 *
18 HPLOT EQU $F457
19 COLBYTE EQU $E4
20 HMASK EQU $30
21 HNDX EQU $E5
22 GBAS EQU $26
23 *
0300: 20 BE DE 24 ENTRY JSR CHKCOM
0303: 20 67 DD 25 JSR FRMNUM
0306: 20 52 E7 26 JSR GETADR
27 *
0309: 06 50 28 CALC ASL LINNUM
030B: 26 51 29 ROL LINNUM+1 ; X*2
30 *
030D: A9 08 31 LDA #$08 ; %00001000
030F: 24 E4 32 BIT COLBYTE
0311: F0 06 33 BEQ C1 ; NO MATCH COLOR EVEN
0313: E6 50 34 INC LINNUM
0315: D0 02 35 BNE C1
0317: E6 51 36 INC LINNUM+1
37 *
0319: A5 50 38 C1 LDA LINNUM
031B: 85 E0 39 STA X
031D: A5 51 40 LDA LINNUM+1
031F: 85 E1 41 STA X+1
42 *
2
2.Ev
enBe
tte
rHi
-Re
sPl
ott
ing 2
11

0321: 20 4C E7 43 GETY JSR COMBYTE


0324: 8A 44 TXA ; PUT Y-COORD IN ACC
0325: A6 E0 45 PLOT LDX X
0327: A4 E1 46 LDY X+1
0329: 20 57 F4 47 JSR HPLOT
48 *
032C: A5 30 49 CHK LDA HMASK
032E: C9 C0 50 CMP #$C0 ; %11000000
0330: D0 11 51 BNE DONE
52 *
0332: A4 E5 53 FIX LDY HNDX
0334: C8 54 INY
0335: B1 26 55 LDA (GBAS),Y
0337: 24 E4 56 BIT COLBYTE
0339: 30 04 57 BMI HISET
033B: 29 7F 58 HICLR AND #$7F ; %01111111
033D: 10 02 59 BPL STORE ; ALWAYS
033F: 09 80 60 HISET ORA #$80 ; %10000000
0341: 91 26 61 STORE STA (GBAS),Y
0343: 60 62 DONE RTS
0344: 06 63 CHK

⇢el isti
ngt hr oug hl ine4 7s houl da ppe ars i
mi l
art ot hepr eviousc ha pter’
s
routine .Line s4 8thr oug h6 2a ddac heckt os eewhe thert hene x tby teinme mor y
needst obea dj
us teda c cor dingt ot het hree -s
te pprocedur ej us tde scribe d.
⇢eFr ststepi st oe x ami nel oc ati
on$30( HMASK) .You’ llr eme mbe rf rom t he
previousc ha ptert ha tt hisi sama s kus edt oi ndicatewhi c hbi tpos i
tioni st obe
settopl ott hepoi nt. Ifbi t6we r
es et,thisloca ti
onwi llhol dt hev alue$C0( binary
%11000000) . Lines4 9t hr oug h5 1c he c
kf ort his.
Ifama tchi sf ound, wek nowbi t6wa ssetbyt hepl ot. Wemus tnowa ccess
thene xtby t
ei nme mor ya nde it
he rs e
torc le
a rbit7oft ha ttoma tchouror iginal
byte.S inceHNDX( $E5)h ol dst heoLs etoft hec urre
ntby te( us ua llyus edbyc om-
biningwi thGBAS( $26)i nt hef or m LDA(GBAS,Y)) ,wec anl oadt heY- Re gist
er
withHNDXa ndt he ni ncre me ntus ingt heINYonl i
ne5 4t os hi 4oura ttentiont o
thene xtby te.⇢eda t
af ort ha tby tei sthe nl oadedint ot heAc cumul atoronl ine
55. Nowf ort hes leig htofha nd. Wewa ntt oc heckthes ta tusoft heFr stby te,but
ifwel oadt heAc c umul a torwe ’
lll oset heda tac urrentl
yhe ldt he r
e .Tos olvet he
probl em, cons ide rt his :⇢ec olorma skby t
eCOLBYTE( $E4)h ol dst hema skus ed
onlymome nt sbe for et odot hepl ot.Wec a nc heckt hehi g h-or de rbi toft his
valuet ode t
e rmi net hes tatusofbi t6i nt heby t
ea ccesse dbyt hepl ot.S incei t’
s
bit7we ’reint e
r estedi n,wec ana lsous et heBITc omma ndt odot hec he ck .
Line5 6doe saBIT COLBYTE. ⇢i swi llmov ebit7ofCOLBYTEi ntot heS t
atus
Re gi
s ter,a4e rwhi c haBMI( Br anc honMI nus )oraBPL( Br a nc honPLus )c anbe
usedt oc he ckhowt hebi twa ss et.
Int hisc ase,theBMIi sus e dt ode tectbit7be i
ngs e t.I ft hisbr a nchi st aken,
thepr ogram wi l
ls k ipt ol i
ne6 0.I fnot ,theHICLR( “high- bi tc lear” )se ct
ionwi l
l
bee nt ered.I nt hiss ection,t heANDope ratori susedt of orc et hec learingoft he

]
2
12 As
sembl
yLi
nes

highbiti ntheAccumul at
or .Si
ncethiswi l
lals
oc l
earthes ig
nbi toftheS ta
tus
Registe
r,theBPLfoll
owingt hisoper
ationisal
wa ystake
n.
IfHISET(“hi
gh- bi
tset
”)i sent
ered,theORA #$80wi l
lforcetheset
tingofbit
7oft heAc cumulator.(
Ifyoune e
dmor einfor
ma ti
onont helogic
alope ra
tors
,
youma ywi s
htoc onsultcha pt
er12.
)Li ne61( STORE)putsthec onte
nt softhe
Accumul atorbac
ki ntome mor y,imme diat
elyfoll
owedbyt heRTSwhi c hends
theroutine.

PLOT.
560+
⇢isrouti
neisa
lsoava
ria
tiononaprogra
m pre
sent
edinthepr
evi
ousc
hap-
t
era
ndag
a i
nusesachec
ksyst
emi de
nti
calt
othatus
edinPLOT.
140.
1 ********************************
2 * AL22-HIRES PLOT.560+ *
3 * *
4 ********************************
5 *
6 *
7 * OBJ $300
8 ORG $300
9 *
10 CHKCOM EQU $DEBE
11 FRMNUM EQU $DD67
12 GETADR EQU $E752
13 LINNUM EQU $50
14 COMBYTE EQU $E74C
15 *
16 X EQU $E0
17 *
18 HPLOT EQU $F457
19 COLBYTE EQU $E4
20 HNDX EQU $E5
21 HBIT EQU $30
22 GBAS EQU $26
23 *
0300: 20 BE DE 24 ENTRY JSR CHKCOM
0303: 20 67 DD 25 JSR FRMNUM
0306: 20 52 E7 26 JSR GETADR
27 *
0309: 46 51 28 CALC LSR LINNUM+1
030B: 66 50 29 ROR LINNUM ; X/2
030D: A9 7F 30 C0 LDA #$7F ; %0111 1111
030F: 85 E4 31 STA COLBYTE
0311: 90 04 32 BCC C1 ; X=EVEN FROM ROR
0313: A9 FF 33 LDA #$FF ; %1111 1111
0315: 85 E4 34 STA COLBYTE
35 *
0317: A5 50 36 C1 LDA LINNUM
0319: 85 E0 37 STA X
031B: A5 51 38 LDA LINNUM+1
031D: 85 E1 39 STA X+1
40 *
2
2.Ev
enBe
tte
rHi
-Re
sPl
ott
ing 2
13

031F: 20 4C E7 41 GETY JSR COMBYTE


0322: 8A 42 TXA ; PUT Y-COORD IN ACC
0323: A6 E0 43 PLOT LDX X
0325: A4 E1 44 LDY X+1
0327: 20 57 F4 45 JSR HPLOT
46 *
032A: A5 30 47 CHK LDA HBIT
032C: C9 C0 48 CMP #$C0 ; %11000000
032E: D0 11 49 BNE DONE
0330: A4 E5 50 FIX LDY HNDX
0332: C8 51 INY
0333: B1 26 52 LDA (GBAS),Y
0335: 24 E4 53 BIT COLBYTE
0337: 30 04 54 BMI HISET
0339: 29 7F 55 HICLR AND #$7F ; CLEAR BIT 7
033B: 10 02 56 BPL STORE
033D: 09 80 57 HISET ORA #$80 ; SET BIT 7
033F: 91 26 58 STORE STA (GBAS),Y
59 *
0341: 60 60 DONE RTS
0342: 56 61 CHK

Ordinari
lythi
swoul dbeaFnepl acetoe ndthisc hapte
r,butthere’
sone
morerouti
newor t
hpr ese
nting.Sofarwha tyou’vegotisac hoi
cebet
we enpl ot-
t
ingasinglecol
or(PLOT.140)o rtak
ingwha tevercolory ougeti
ne x
c hangef or
t
hecapacit
yforgre
aterhor i
zontal
resolut
ion.
We l
l,wit
hjustal i
tt
lemor eeLortwec anc re
atear outi
nethatwil
loLert he
sa
mede greeofhorizontalaccur
acya ndg ua
ranteethata nydotplot
te dwillbe
whit
e.

PLOT.
560-
Whi
te
Norma l
lywhe ny ous pecifywhitewhe nus ingAppl eg raphics,you’rerea
lly
sayi
ng ,“
Idon’tc ar
ewha tcolor,”becauseanya tte
mptt oplotas inglepointwi l
l
il
luminateonlyac oloreddot, notatruewhi tedot.⇢i sisbecausewhi t
ei sre
ally
formedbypl otti
ngtwoa dja
ce ntdots.⇢i sisconsistentwi t
ht heex a
mi nati
onof
theCOLBYTEbi tpatt
e r
nf oracceptabl
ebi tst
os etcombi ne dwiththeg ivenHMASK
bitpatte
rnf oras peciFedhor izonta
lpos it
ionwi thint heby te.⇢i spr oces
sof
plott
ingwa sde s
cri
be ding reaterdetailinthepr eviousc hapterbut,a saqui ck
refr
esher,r
eme mbe rthatthisc ombinationwoul ds uccessf
ull
ydot hee quiv
alent
of:
St
ate
ment: HCOLOR = 3 : HPLOT 3,0
MaskPa
tte
rns
: COLBYTE: %0111 1111 (HCOLORbitmask)
HMASK: %1000 1000 ( bit3set
;ignor
ehighbit
)
RESULT: %0000 1000 ( posit
ion3issetg
reen)
Youmightimag
inetha
tiftheHMASKc
ouldhav
ebeense
tupt
oha
vet
wo
a
dja
centbi
tss
et,
ther
esul
tmightha
vebeenatr
uewhit
edot
:

]
2
14 As
sembl
yLi
nes

St
ate
ment: HCOLOR = 3 : HPLOT 3,0
MaskPa
tte
rns
: COLBYTE: %0111 1111 (HCOLORb i
tmask
)
HMASK: %1001 1000 ( bit
s3a nd4set
)
RESULT: %0001 1000 ( posi
tions3and4–whi
te
)
Asi
tha
ppe
ns,
thi
sca
nbedone
,andhe
re’
sthene
wrout
inet
odoi
t!
1 ********************************
2 * AL22-HIRES PLOT.560W *
3 * *
4 ********************************
5 *
6 *
7 * OBJ $300
8 ORG $300
9 *
10 CHKCOM EQU $DEBE
11 FRMNUM EQU $DD67
12 GETADR EQU $E752
13 LINNUM EQU $50
14 COMBYTE EQU $E74C
15 *
16 X EQU $E0
17 *
18 HPOSN EQU $F411
19 HPLOT EQU $F457
20 COLBYTE EQU $E4
21 HCOLOR1 EQU $1C
22 HMASK EQU $30
23 *
0300: 20 BE DE 24 ENTRY JSR CHKCOM
0303: 20 67 DD 25 JSR FRMNUM
0306: 20 52 E7 26 JSR GETADR
27 *
0309: 46 51 28 CALC LSR LINNUM+1
030B: 66 50 29 ROR LINNUM ; X/2
030D: A9 7F 30 LDA #$7F ; %01111111 WHITE1
030F: 85 E4 31 STA COLBYTE
0311: 90 04 32 BCC C1 ; X=EVEN
0313: A9 FF 33 LDA #$FF ; %11111111 WHITE2
0315: 85 E4 34 STA COLBYTE
35 *
0317: A5 50 36 C1 LDA LINNUM
0319: 85 E0 37 STA X
031B: A5 51 38 LDA LINNUM+1
031D: 85 E1 39 STA X+1
40 *
031F: 20 4C E7 41 GETY JSR COMBYTE
0322: 8A 42 TXA ; PUT Y-COORD IN ACC
0323: A6 E0 43 PLOT LDX X
0325: A4 E1 44 LDY X+1
0327: 20 11 F4 45 JSR HPOSN
032A: A5 30 46 LDA HMASK
032C: 0A 47 ASL
032D: 05 30 48 ORA HMASK
2
2.Ev
enBe
tte
rHi
-Re
sPl
ott
ing 2
15

032F: 85 30 49 STA HMASK


0331: 20 5A F4 50 JSR HPLOT+3
51 *
0334: 24 30 52 CHK BIT HMASK
0336: 50 22 53 BVC DONE ; BIT 6 CLEAR
54 * BIT 6 CLEAR: POSITION = 0-9
55 * BIT 6 SET: POSITION = 10-13
56 *
0338: 24 1C 57 CHK2 BIT HCOLOR1
033A: 10 06 58 BPL HICLR ; BIT 7 TEST
033C: A9 FF 59 HISET LDA #$FF ; WHITE2
033E: 85 1C 60 STA HCOLOR1
0340: D0 04 61 BNE CHK3 ; ALWAYS
0342: A9 7F 62 HICLR LDA #$7F ; WHITE1
0344: 85 1C 63 STA HCOLOR1
64 *
0346: A9 20 65 CHK3 LDA #$20 ; %00100000
66 *
67 * HMASK: %11100000 IF POSITION = 10,11
68 * HMASK: %11000000 IF POSITION = 12,13
69 *
0348: 24 30 70 BIT HMASK
034A: D0 06 71 BNE NOPLOT ; BIT 5 SET
034C: A9 81 72 PLT LDA #$81 ; %10000001
034E: 85 30 73 STA HMASK
0350: D0 04 74 BNE FIX ; ALWAYS
0352: A9 80 75 NOPLOT LDA #$80 ; %10000000
0354: 85 30 76 STA HMASK
77 *
0356: C8 78 FIX INY
0357: 20 5A F4 79 JSR HPLOT+3
80 *
035A: 60 81 DONE RTS
035B: 88 82 CHK

⇢i srout i
nes t
a r
tsoutmuc hl iket heot herPLOT.560 r outi
ne.Li nes2 4
t
hrough4 4a reidenticalandpe rformt hes amef unctionofc al
culati
ngwhi c
hX
val
uetoha ndt ot henor ma lApple s
o4r outine.⇢eFr stdiLer
enc eappearsatline
50,whe r
eaJSRHPOSNi spe rfor
me dinsteadofaJSRHPLOT. ⇢i si sdonetoa ll
ow
Appleso4t og ot hroughi tsusualpr eparati
onf orapl ot.⇢i ss et
supt hec olor
maska ndpos iti
onma skby tesanda ls
ot hebasea ddresscalc
ulati
on.
Atthispoi nt,wes t
e pintotheus ualpr oces
st otampe rwitht heHMASK( $30)
val
ue.Asi nt hee ar
lierexa mple,thisordinari
lywoul dha vejustas i
nglebitpos i
-
t
ion“ ma r
k ed”f ort heupc omingPLOT.Howe ver,byus ingtheASL,ORAHMASK
combinati
ononl i
ne s47a nd4 8
, wec ans hi
4t heor i
ginalpatt
erna ndthens uper-
i
mpos ethene wpa t
te r
nont heold.
Example:ForX=3
Original HMASK: %1000 1000
ASL: %0001 0000
ORA HMASK: %1001 1000

]
2
16 As
sembl
yLi
nes

⇢eaddre
ssus
ual
lygive
nfortheHPLOT r
out
ine
,$F457,i
ncl
udesaJSRto
HPOSN.Be
caus
ewe’
vealr
eadydonethi
s,aJSRHPLOT+3accompl
is
hestheFr
st
stageofourope ra
tion; name l
y,theplott
ingofapur ewhi tedot .
Now ther ema iningpr oblemi stotak ec a
reofe nd- of-the-
byteSaws.⇢i s
ca noccurforfourpos sibleplots.Foreachby te,therear ef ourtee
npossibl
eposi-
tionswhi chcanbepl otted,numbe red0t hroug h1 3(se eFi gure20-4inchapter
20 ).
Forpos i
tions1 0a nd1 1,bits5and6wi l
l beset.
Be ca us ebit6canbeaLecte
d
bybi t7ofthene x
tby tei nme mory,achec kforbitag r
ee me ntmustbema de.
St
rangerstil
l,ifpos i
tions1 2and1 3a replotted,onlybi t6isavai
la
ble,whic
h
woul dnor mallyputusba ckt ohavingplottedonl yac olor eddot .
ToFxt his
,we
ha vetog oaga i
nt ot hene xtby t
einme mor yanddoa not herplottoil
luminate
justtheveryFr stdotoft ha tby t
e.
I
ng enera
l then, t
hepr ocesswillbe
:
1. Fi xHMASKt ot urnont woa dja
centbitswhe repos s
ible .
2
. HPLOTwi
tha
lt
ere
dHMASK.
3
. Che
ckf
orbi
t6us
age
.Ifnone
,ex
itr
out
ine
.
4.S e
tbit7oft
henextbyt
etoagre
ewi t
hbit7oft hec
urr
entby
te.Che
ck
whe
therbi
t5i
sbe
ingus
ed.I
fnot
,godi
rec
tl
ytoHPLOT+3.
5
.Ifbi
t5i
sse
t,s
etHMASKt
opl
otonl
ytheFr
stdotoft
hene
xtby
te.
6. Ma keas econdpa sstoHPLOTt opl ottheX+1s creenc oor dinate,s
ingl
edot
only.IfHMASKs e tto#$81, onlyt hehigh- orderbitwi llbes et
, withnoa ct
ualplot
done .
I
fy ounow e xami neli
ne5 2oft hel ist
ing,y ou’llseet heBITc omma ndi s
againus ed,thi st i
met oc heckbi t8ofHMASK.⇢eBITc omma ndf orc
esbit6of
theS t
atusRe gister( t
heov erSowSa g)t othes amev aluea sbit6ofHMASK.⇢us
BVS (Br a
nc hoVe rSow S et)andBVC ( Br a
nc hoVe rSow Cl ear)c anbeus edt o
checkf orbi t6s etorc lea
r,respe ct
ivel
y .Inourc a se,BVCwi l
lbr anc htotheex i
t
point,DONE, ifbit6i sclear.
Ifbit6i ss e t
,l i
ne s57763s etthehi g hbitoft heot he rc olorma skby t
e,
HCOLOR( $1C) ,toa gre
ewi ththepr evi
ouspl ot.⇢i sc olorma skby teisusedlater
byHPLOT. Bec ausewe ’
llbeskippi ngtheus ualentrypoi nt($F457) ,weha vetoset
thisbytespe ciFca l
ly.
Onc et hec olorby teisset,a notherc heckisdonet os eeifbi t5i sset
.⇢i sis
donebya gainus i
ngt heBITc omma nd.S inceonl ybi t
s6a nd7c anbec hecked
viatheS tatusRe gist
er,wemus tloadtheAc cumul atorwi thanume ri
cima g
eof
thebitwewi sht ot e
stfor.Inthi scase,thev al
ueus edi s#$20( %00100000) .
A4e r
theBITc omma nd,aBNE( Branc hNotEqua l)willbet ake nifbi t5i sset
.Ye s
,it
soundsba ck wa rd, butthenBITi sar a
the rstr
angec omma nd.
2
2.Ev
enBe
tte
rHi
-Re
sPl
ott
ing 2
17

Giv
entheappropr
iat
er e
sultoftheBITtest
,HMASKi sloa
dedwithe i
the
r#$81
or#$80dependi
ngonwhe t
herwewa nte
dana ct
ua l
plottotakepl
ace.
Atli
ne78(FIX),
weta k
ea dvantageofthefactthattheY-Reg
iste
ri ss
til
lse
t
tothecorre
ctvaluetoaccessthec urre
ntme mor yby t
e.Bydoingt heINY,we
advancethepointe
rtot hene xtby t
es othattheJSR HPLOT+3 wil
lma kethe
appropr
iat
ecorr
e c
ti
onstothene xtbyteinmemor y.

AFi
nal
DemoPr
ogr
am
ToFnishthi
ngsoL,let
’str
yonel
a s
tApple
so4progra
mt oma keuseoft
he
newr outi
nes
.⇢i sisanext
ensi
onoftheFr
stli
sti
ngpres
entedatt
hebe gi
nni
ng
ofthischa
pter
,anditwil
lgi
veyouanoppor
tuni
tytocomparether
elat
ives
cre
en
appearanc
esofdiLe
rentr
outine
s.
10 D$ = CHR$(4)
40 HOME: INPUT "BLACK1 OR BLACK2? (1 OR 2)"; I : I = I - 1
100 REM NORMAL TEST
110 HGR: HCOLOR = I*4: HPLOT 0,0: CALL 62454: HCOLOR = 7 - 4*I
120 S = 1: REM SCALE FACTOR
130 K = 20: REM OFFSET VALUE
140 GOSUB 900
200 REM PLOT.140 TEST
205 PRINT D$;"BLOAD AL22.PLOT140,A$300"
210 S = 0.5: K = 40
220 F = 1: REM FUNCTION FLAG
230 GOSUB 900
300 REM PLOT.560 TEST
305 PRINT D$;"BLOAD AL21.PLOT560,A$300"
310 S = 2: K = 60
320 GOSUB 900
400 REM PLOT.560+ TEST
405 PRINT D$;"BLOAD AL22.PLOT560,A$300"
410 K = 80
420 GOSUB 900
500 REM PLOT.560W TEST
505 PRINT D$;"BLOAD AL22.PLOT560W,A$300"
510 K = 100
520 GOSUB 900
600 END
900 REM PLOTTER
930 FOR I = 0 TO 100
940 X = (I + K)*S : Y = I
950 X = X / 2
960 IF F = 0 THEN HPLOT X,Y
965 IF F THEN CALL 768, X, Y
970 NEXT I
980 RETURN

You’l
lal
sonoti
cet
hatthi
shast
hesc
ali
ngf
act
orsbui
lti
ntoi
ttomakeea
ch
l
inesl
antatt
hesameangl
e.⇢eoLsetf
act
orKisuse
dtomov ee
achpl
ottot
he
r
ightal
it
tlef
orappe
ara
nce’
ssake
.

]
2
18 As
sembl
yLi
nes

Bya
ddi
ngl
ine9
55l
ikes
o:
955 X = X / 2

youc ansla
nttheli
nese v
enfurt
hertoshowoLt hema xi
mum s l
antpos
si
blef
or
the560-pointmodes
. Youmightal
sowanttotr
yt hi
sprogram wi
ththe140
-and
560-pointrout
ine
sfrom thepre
viousc
ha pt
ert
os eehowt heyperf
orminpla
ce
ofthene wones.

Conc
lus
ion
⇢e ser out inesa r
ebe stus edi nma thema ti
calcha rtsrathert hani npur e
graphicssucha spictures.⇢ema ina rgume ntag ai
nstthe5 60-pointmodei sthat
youc an’tbea ssuredt hatpl otti
ngonepoi ntwi llnota Le ctne arbypoints.As
we’vede mons tr at
edhe re
,t herea ppa rent
lyi snoa ppr oac hthatc anguarantee
thi
swi l
lnotha ppe n. I
twoul ds ee m, the n,ama tterofy ourownpr efer
enceast o
whicht ouse .Ourhopei st hatt he ser out
ine swillwide ny ouropt ionsforyour
ownpr ogrammi ngg oal
sa ndt hatt he y’vet
a ughtyoual ittl
ea l
ongt hewa y.
⇢eus ua la pproachi nt hisbookha sbe entos impl i
fya nyi deawhe nFr s
t
present
ingit.I nt hea reaofg raphi cs,thoug h,simpli
cityha snotbe ene as
y.For
themos tpart, hi-resgraphicsg ivest hei mpr es
sionofbe ingonl yma rgi
nall
ylog i
-
cal
.Ina nye vent ,thoug h,nowy ou’ repr oba bl
ys t
arti
ngt og e
taf eelforhowt he
contentsofme mor yaLe ctwha ti sdi splay
e dont hes creen.Int heFna lanalysis
,
therealchalleng eofhi -resg raphic sisma nipul
a t
ingthec ontentsofme moryt o
producethev i
s ualeLectsy ouwa nt.
2
3
2
3. Hi
-Re
sGr
aphi
csS
CRNFunc
ti
on
Aug
ust1
982

Inl o-r
esg r
a phics,theSCRN(X,Y)f uncti
onre turnsthevalueoft hecolorof
thes creenattheX,Yc oordinatespe ciFed.Unfor tunat
ely
,noe quiva l
entfunc-
tionex i
stsf
orus ewi thhi -r
esg ra
phic sinAppl es
o4BAS I
C.
Int helastfewc ha pterswe ’
ves ee nhowt opl otpointsinav arietyofwa ys
.
Now,he reisar outinef ordoi ngahi -resequi
val
e ntoft heSCRN(X,Y)f uncti
on.
Onec oncei
v a
bleus efort hismi g
htbei nag amepr og ra
mi nwhi c
hi t
’simpor t
ant
tok nowwhe noneobj ectistouchinga not her
.Usingt heSCRNroutineg iv
enhe re
,
youc a ntes
tt ose ewhe thera nypoi ntsha vealr
eadybe enplot
teda tthec oor
di-
na t
esapr esumabl ymov i
ngobj ectisa bouttouse.
1 ********************************
2 * AL23-HI-RES SCRN FNCTN *
3 * 6/22/82 *
4 ********************************
5 *
6 *
7 * OBJ $300
8 ORG $300
9 *
10 CHKCOM EQU $DEBE
11 FRMNUM EQU $DD67
12 GETADR EQU $E752
13 LINNUM EQU $50
14 COMBYTE EQU $E74C
15 PTRGET EQU $DFE3
16 CHKNUM EQU $DD6A
17 GIVAYF EQU $E2F2
18 MOVMF EQU $EB2B
19 *
20 X EQU $E0
21 Y EQU $E2
22 *
23 HPOSN EQU $F411
24 HNDX EQU $E5
25 HBIT EQU $30
26 GBAS EQU $26
27 *
0300: 20 BE DE 28 ENTRY JSR CHKCOM
0303: 20 67 DD 29 JSR FRMNUM
0306: 20 52 E7 30 JSR GETADR
31 *
0309: A5 50 32 SET LDA LINNUM

]
2
20 As
sembl
yLi
nes

030B: 85 E0 33 STA X
030D: A5 51 34 LDA LINNUM+1
030F: 85 E1 35 STA X+1
36 *
0311: 20 4C E7 37 GETY JSR COMBYTE
0314: 86 E2 38 STX Y
39 *
0316: A5 50 40 CHKX LDA LINNUM
0318: 4A 41 LSR ; PUT BIT 0 IN CARRY
0319: A9 01 42 LDA #$01 ; SET BIT 0
031B: 85 50 43 STA LINNUM ; %0000 0001
031D: B0 02 44 BCS CHKHI ; X='ODD'
031F: 06 50 45 ASL LINNUM ; SHIFT LEFT ONE POSN
46 ; %0000 0010
47 *
0321: A6 E0 48 CHKHI LDX X
0323: A4 E1 49 LDY X+1
0325: A5 E2 50 LDA Y
0327: 20 11 F4 51 JSR HPOSN
52 *
032A: A4 E5 53 LDY HNDX
032C: B1 26 54 LDA (GBAS),Y
032E: 48 55 PHA ; SAVE DATA
032F: 10 08 56 BPL HICLR ; BIT 7 CLR
0331: A5 50 57 HISET LDA LINNUM
0333: 09 04 58 ORA #$04 ; SET BIT 2
0335: 85 50 59 STA LINNUM ; BIT 'ON'
0337: D0 06 60 BNE CHKBIT ; ALWAYS
0339: A5 50 61 HICLR LDA LINNUM
033B: 29 8B 62 AND #$8B ; CLR BIT 2
033D: 85 50 63 STA LINNUM
64 *
033F: 68 65 CHKBIT PLA ; RETRIEVE SCREEN BYTE
0340: 25 30 66 AND HBIT ; SELECT BITS OF INTEREST
0342: 29 7F 67 AND #$7F ; CLR BIT 7
0344: D0 06 68 BNE SEND ; BIT IS "ON"
69 *
0346: A5 50 70 OFF LDA LINNUM
0348: 29 8C 71 AND #$8C ; CLR BITS 0,1
034A: 85 50 72 STA LINNUM
73 *
74 *
034C: 20 BE DE 75 SEND JSR CHKCOM
034F: A4 50 76 LDY LINNUM
0351: A9 00 77 LDA #$00
0353: 20 F2 E2 78 JSR GIVAYF
0356: 20 E3 DF 79 JSR PTRGET
0359: 20 6A DD 80 JSR CHKNUM
035C: AA 81 TAX
035D: 20 2B EB 82 JSR MOVMF
83 *
0360: 60 84 DONE RTS
0361: 0C 85 CHK
2
3.Hi
-Re
sGr
aphi
csS
CRNFunc
ti
on 2
21

AnOv
erv
iew
You’ llr e me mbe rt ha tint hepr eviousc ha pte rweus e dtheAppl eso4HPLOT
rout i
net opl otapoi nt .⇢eXa ndYc oordina tesf orthepoi ntwe repa ssedt othe
rout i
nev ianor ma l Appl e so4v ariabl es.
⇢eFna lpl otwa sa ccompl ishe dbys ettingapa rt
icul a
rbi twi thinaby teof
me mor y .⇢ebi tt obes etisde termi ne dbyc reatinga“ ma sk”f ort hebi tpos it
ion
withint heby te.
Figur e2 0 -1( ourol df riendf r om c hapte r2 0)wa sus eda sag ui det owhi c h
bit
sa res etf ora nyg i
v enc olora ndXc oordina te.
Forourhi -r esSCRNf unc tionwe
needt oi de nt ifywhe thert hebi tc or - Col or Va l
ue Bi na r
y
respondi ngt oag ive nX,Y c oor di - Bl a ck1 0 0000 0000
nate ha s be e ns e t
,t ot a kei nt o Gr ee n 1 0000 0001
accountt hehi g h- orde rbi t( bit7 ) Vi olet 2 0000 0010
whe rene ce ssar y,a ndt he nr e turna Whi te1 3 0000 0011
valuebe twe en0a nd7c orrespondi ng B l
a ck2 4 0000 0100

tot hec oloroft hedot .Be foreg oi ng O ran ge 5 0000 0101


Blue 6 0000 0110
anyf ur the r, tak eal ooka tFig ur e2 3 -
Whi te2 7 0000 0111
1,whi chs howst hebi tpa tternsf or
the c olor v a lue st hat mi g ht be Fi gur e23-1: Col orBi tPa tterns
ret
ur ne d.
Wha ttheSCRNr out inedoe sise stabli
shat empor aryr egisterinwhi c hthebi t
patternf ort hec olorv al
uet ober e tur nedt ot heus e
rwi llbec ons truc te d.Not ice
thatf ora nyoft hepos s
iblec olorv a lue swene edc onc er
nour selv esonl ywi tht he
las
tt hr eebi tpos itions .⇢i sg rea t
lys impl i
Fe sourt a sk.
Not ea l
s ot ha twhe nadoti s“ oL”( e i
the rbl ac
k 1orbl a ck2 )
,t her out i
ne
ret
ur nsanumbe rwi t hbi ts0a nd1c leared.Bi t2wi l
lstillhavet obes peciFcally
condi tione d, howe ve r,sinc eBl ack 2s etsthehi gh-or derbi tofaby t
ee ve nthoug h
nodoti sillumi na te d.
Be caus ene ithe rwhi tei sdi re ctlypl otte d,t her out inewi llne v err eturna
valueof3or7 . Re me mbe rtha twhe nwhi tei sspe ciFed, Appl eso4nor ma ll
ypl ots
onlyonec ol or .⇢usourSCRNr out ineha snowa yofde te
rmi ningwhe the ra
givendoti sapur ec olororpa rtofal argerdotpa t
ternc reatingawhi telineor
area.
Tode ter mi neadot ’sc olorf rom a mongt hef ourr ema i
ningc ol or s,wel ook
attheXpos itionoft hedot .S inc ey ouc anpl otonl ye venc ol orv a lue sate ve n
coor dina te s,a ndoddc olorv alue sa toddc oor dina tes,t
het woFna lbi tpos it
ions
oft hec olorr egiste rv al
uewi llbe0 1or1 0de pendi ngonwhe t
he rXi soddor
even.⇢es ta tusoft het hirdbi tde pe ndsonwhe t herthedot ’
shi gh- or derbi tis
set
.

]
2
22 As
sembl
yLi
nes

Whe nallofthe
sechecksa
recol
lec
tedint
oaroutine,weha vethefol
lowi
ng
proc
edure.
1. Lines28738ret
rie
vetheval
uesoftheXandYc oordinat
esfrom theApple-
so4cal
lcomma nd.⇢e s
earetr
ansf
err
edtothehi
-r
esre g
iste
rs($E07$E2).
2. ⇢ev aluef ort heXc oor dina tei sr et
ur ne di nLINNUM( $50, $51) a nd, ass uc h,
canbec he cke dforwhe the ritisoddore v en. Todot hi swene e donl yc he ckt he
low-orde rby tet os e ewhe thert hel astbit( bit0 )iss e t.⇢ee asiestwa ytodot hi s
istous et heLSR( Log icalS hi4Ri ght )comma ndonl i
ne4 1t os hi4t hel astbi t
intothec ar rySa g, whi chwi l
l bet e steda lmos timme di ately.
Le t
’st alkabi t( pardont hepun)ofpr og rammi ngs tylehe re.Wec oul dt est
forallsixpos siblec olorc ondi t
i onsi ndiv i
dua lly,buti tt urnsoutt ha titi se a sier
tos e
tupt heFna lc olorv al
uemor es ubtly.We ’lls tartbya ssumi ngt ha ts ome
colorwi l
lbepr esent .Line4 2put sapos siblev al
ue( #$01)i ntoLINNUMa sas tart-
ingpoi nt.( Sincewe ’
redonewi thLINNUMf rom l ine s2 8 738wec annowus ei ta s
ourwor kingr eg i
s t
e rfort hec ol orv alue.Al sonot et ha twenol ong e
rne edt o
wor r
ya boutLINNUM+1s inc et hec ol orv aluewi l
l nev ere x ceed2 5 5.
)
Nowwec andot hec a rr
yt e st,BCS( Br anc honCa rryS et),tos eewhe thert he
coordina tewa soddore v en. Ifthec arrybi tiss et,Xwa sodda ndLINNUMa lready
containst hebi tpa tt
e rnfora lloft hec olorst ha tc oul dbepl otteda ta noddc oor -
dinate.Ift hec a rryi sc l
ea r,line4 5wi llbee xe cuteda ndwi llshi4t hepa tternt o
thele4onepos it
iont ocor respondt ot he“ eve n”c ol or s.
3. Li ne s4 875 1dot heJSRHPOSNwhi chwi llc alcula tet hea ddr essoft heby tein
me mor yt ha tc orre spondst ot hec oor dinate sg iv en.S eet hepl otr out ine sf rom
previousc ha pte rsify oune e
dr efre shingont his.Li ne s5 3
75 5loadt heby tei nto
theAc cumul a t
ora ndpus hi tont ot hes tackt ober etr i
e vedl ater.
⇢et estonl ine5 6c he cksf orwhe the rt hehi gh- orde rbi twa ss et.ABPL
(BranchPLus )i sdonei ft hebi twa sc lear.Ift hebi twa ss et, wene edt os e tbi t2
ofLINNUM( ourc olorr egi
s ter).Not et hatbi t2i sc l
ea rf orHCOLORS0 73a nds etfor
HCOLORS4 77 .Bi t2i sse tus ingt heORA( l
og icalORwi thAc cumul ator ).Ift he
high-orde rbi twa sc lear,thel og ica lANDc omma ndi sus edt oc learbi t2 .
4. Fi na lc heck .Nowwene edt os eewhe the rt hedotwa sa ctuallyt ur nedon.
⇢eme mor yby tei sretriev edf rom t hes tac kus ingt hePLA( PulLAc cumul a t
or )
andma skedwi t
hHBIT( $30) .HBIT i sama skc rea tedbyt heHPOSNr out inet o
show whi chbi tc orrespondst ot heg ivenXc oor di na t
e .Byma sk i
ngHBITwi t
h
theme mor yby tewec ani sola t
et hebi twe ’
rei ntere stedi n. Asaf urthe rs tep, the
AND #$7Fc learst hehi gh- orde rbi t( whic hwe ’vea lre adyt estedf ora ny wa y).As
ane xampl e ,suppos et hatt heme mor yloc ationha dhe ldt hev a l
ue#$9Ba ndt he
valueforXwa s4 :
Not et heFna l resultwi llonl ybenonz eroi fthedoti son.
2
3.Hi
-Re
sGr
aphi
csS
CRNFunc
ti
on 2
23

5
.I fthedotison,eve
rythingisalr
eadys
etup,andwec a
nproceedtotheFnal
e
xitpha s
e.I
fthedotisoL,t heAND#$8Conl i
ne7 1wil
lcl
earonlybit
s0a nd1.
⇢isa l
lowsustode t
erminet hest
atusoft
hehigh-or
derbit
,eve
ni fadotisnot
a
ctual
lyplot
tedattheposi
tiong i
ven.
6. SEND( l
ines7578 4)isi
dentic
altotheREAL VARIABLE SENDroutinegi
venin
chapte
r1 7andisus edt osendourr esult
ingv al
uebacktoAppleso4.⇢eonl y
thi
ngdi Ler
entint hiscaseisthatther outineloadsa0intotheAc cumula
tor
ins
teadofthehigh-orde rby
t eofLINNUM( LINNUM+1)s
inc
e,asme nti
onedprevi
-
ously
,thevaluef
orc olorwil
lneverexceed2 55.

Sa
mpl
ePr
ogr
am
Tot
estt
hisr
out
ine
,BLOADi
tat$300a
ndc
all
itus
ingt
hes
ynt
ax:
CALL 768, X, Y, C

whereXa ndYa ret


hescreencoordinat
estoexa
mi ne,andCisthevari
abl
e
int
owhicht herout
inewil
lret
ur nt
heresult
ingcol
orval
uefrom LINNUM.
Asane xampleofusingt heSCRNroutinefr
om BAS IC,t
hisprogram wi
ll
ret
urnallthepossi
bleval
uesforCa ndil
lust
ratet
hedependenceofthos
ev a
lue
s
onHCOLORa ndtheXposit
ion:
0 HOME: VTAB 22: X = 0: Y = 0
5 PRINT CHR$ (4);"BLOAD AL23.HGRSCRN"
10 FOR I = 0 TO 7
20 HGR: HCOLOR = I
30 HPLOT X, Y
40 CALL 768, X, Y, C
50 PRINT "X = ";X;" COLOR = ";I;" RESULT = ";C
60 NEXT I
70 X = X + 1: IF X = 1 THEN 10
80 TEXT: END

⇢epr og
ramg oesthroughtwopa s
ses,t
heFrstplott
inga l
le i
ghtcol
orsat
X=0,andthesecondwi t
ha lle
ightcol
orsatX=1 .A4erdoi ngtheplot,
thepro-
gr
amc a
ll
stheSCRNr outi
net overi
fythatitre
adsthecolorwet hinkweplott
ed.
I
twil
ldosoexceptinthefoll
owingc as
es:
1. Whitewi
llalwaysreada sei
ther1,2,5,or61.
⇢i si sbecausewhenwhiteis
s
peci
Fed,asingleHPLOTill
uminatesonlyonec
olordot .
2. Anatte
mptt oplotan“odd”HCOLOR(1or5)onane ve
nXcoordi
nat
eoran
“e
ven’
’HCOLOR (
2or6 )ona noddX c oordi
nater
etur
ns0or4a sthere
sul
t
bec
aus
eoftheplott
ingres
tri
cti
onsdescr
ibedinse
ver
aloft
hepr
evi
ouscha
pte
rs.

1[
CT]or
igi
nal
l
yre
ad2
,3,
5,6

]
2
24 As
sembl
yLi
nes

Conc
lus
ion
⇢eSCRNr outinec anbeapplie
di nav ar
iet
yofwa ys
.Ingener
al,youc anus e
thisrouti
newhe nev ery ouwanttoe xaminethescree
nt odet
erminewha tcolor
hasbe endrawn.Pos sibleappli
cati
onsmi ghtincl
udegraphi
cspri
ntingr outines
andg ame si
nwhi chi tisneces
sarytodete
r minetheexi
ste
nceofli
nestha trepre-
sentwa l
lsorobstacle
s.
Ifyouwisht ous etherouti
nedi r
ectl
yf r
om asse
mblylanguag
ewi thoutc all
-
i
ngi tfrom Appleso4, simplydel
etetheentryrouti
nesandloadLINNUMwi t
ht he
Xc oordinat
eand$E2wi tht
heYc oordinate.
2
4
2
4. ⇠eCo
lli
si
onCo
unt
er,
DRAW,
XDRAW
S
ept
emb
er1
982

Int hepr evi


ousc hapte
rwel ook eda taroutinetos imulatet heSCRN(X,Y)
f
unc tionofBAS I
C. ⇢enot ionofi nquiringaboutpoi ntsont hescr e
eni sclose
ly
rela
tedt ot hi
schapter’
stopi
c,thec olli
si
onc ounter.
⇢ ec oll
is
ionc ounterisaone -byt
eme moryl ocati
ononpa gez erooft he
Appl e’sme mory.It
sv al
ueisaf unctionoft heAppl es
o4hi -
resgraphi c
sr outi
nes
speciFc a
ll
yr el
ate
dtos hapetables.⇢epur poseofthec ol
lisi
onc ounterist okeep
t
r ackofa ny“ c
oll
isi
ons”be t
weenas hapebe i
ngdra wnont hescreena nda nypre-
viouslydr awns cre
e nima g
es.⇢ec oll
isioncount e
ri slocateda t$EA( de ci
mal
234)a ndi saLect
edonl ybythec omma ndsDRAWa ndXDRAW.

SomeEx
per
ime
nts
Toill
ust
ratethebe haviorofthecoll
isi
onc ount
er,we’
llFr
stneedashape
tabl
etoe x
peri
me ntwith. ⇢eoneg i
venhereisproba
blythesi
mples
tonepossi
-
ble–asingl
edot.
Toentertheshapei ntome mory,gointotheApple’
sMonitorbyty
pingin
CALL-151<RETURN>,andt henente
r:
300: 01 00 04 00 04 00
E8: 00 03

⇢i
swi
llpl
acet
het
abl
einme
mor
yatl
oca
tion$300a
nds
ett
hepoi
nte
rat
$E8,
$E9t
opoi
ntt
othet
abl
e.
⇢eFr sttwoby tesofthet abl
e( $0100)i ndicat
et henumbe rofshape sinthe
table,whi
chi nourc aseisjustone .⇢ene x
ttwoby t e
s( $0400)g ivet heoLs e
t
from thebe gi
nningoft het able( $300)t othes tar
toft hea ct
uals ha peda t
a
($304).⇢ene x
tt woby t
es($0400)a ret heactualbytesofda tafort hes hape
i
ts e
lf.I
nthisexampl ethes hapet ableisas i
ngle“mov e”ofonepos i
tionupt he
screen.
Youma ywis
ht or evi
ewt heinfor ma t
ionons hapet ablesi
ny ourAp ple
softI
I
BAS ICProgrammingRe f
erenceMa nua l(
19 78),
pa g
es9 279 6.
⇢eFr stexperime ntistov eri
fyt hatweha veinf actinsta
ll
edaus a bleshape
table.⇢isismoste asi
lyteste
dbyput ti
ngy ourAppl eintoAppl eso4BAS I
Ca nd
typingin:
HGR: HCOLOR = 3: ROT = 0: SCALE = 1

]
2
26 As
sembl
yLi
nes

⇢es
cre
ens
houl
dcl
ear
.Youc
annowt
ypei
n:
DRAW 1 AT 100,100

Asi
ngl
edots
houl
dappe
aront
hes
cre
en.Youc
anc
hang
ethes
cal
etot
hre
eby
t
ypi
ngi
n:
SCALE = 3

Te
stt
hisbyt
ypi
ngi
n:
DRAW 1 AT 100,100

Av erti
callineoft hre epixelss
houlda ppear.Ifa l
lhasgonewe lls ofar
,y ou
cannow t ryat hir
de xpe riment.⇢epur poseoft hee x
peri
me ntwi llbetos ee
how thec oll
isionc ounterr eactswithv ar
iousc ombi nat
ionsofdra wingc olors,
backgroundc olors,shape -drawingcomma nds,andt hepr evi
ouscondi ti
onoft he
coll
is
ionc ounte r.
Clearthes creenwi thHGRa gai
na ndtrythiss e
que nceofcomma nds,noting
foreachonewha tthec ondi t
ionsoft hescree
na ndc oll
is
ioncountera rebefore
anda 4erthec omma ndi se x
ecuted.(Notethatreferencesto“col
or”int hischap-
terwillbeinter msof“ black”a nd“whi t
e”aswoul dbes eenonabl a
c k-and-whi t
e
moni t
or .I
fyouha veac olordi s
play,t
hedot swil
l appearassingl
e-colordots–a s
expla
ine dinpr evi
ousc ha pter
s.)
HCOLOR = 3: SCALE = 1: POKE 234,0: DRAW 1 AT 100,100: PRINT PEEK(234)
(
0shoul
dbepr
int
eda
longwi
thawhi
tedotont
hes
cre
en)
DRAW 1 AT 100,100: PRINT PEEK(234)
(
1,wi
thawhi
tedot
)
DRAW 1 AT 100,100: PRINT PEEK(234)
(
1,wi
thawhi
tedot
)
HCOLOR = 0: DRAW 1 AT 100,100: PRINT PEEK(234)
(
0,t
hedoti
ser
ase
d)
DRAW 1 AT 100,100: PRINT PEEK(234)
(
1,wi
thnodot
)

Ifyoutr
ya l
ltheva
riousc
ombinat
ions
,yous
houl
dbea
blet
ore
pli
cat
ea
c
har
tsomet
hingl
iket
heoneonthenex
tpage.
2
4.⇢eCol
li
si
onCount
er,
DRAW,
XDRAW 2
27

HCOLOR Comma
nd Ba
ckg
round C=0 C=1 Re
sul
t
White DRAW Bl
ack 0 0 Whit
e
White DRAW Whit
e 1 1 Whit
e
Bla
ck DRAW Bl
ack 1 1 Bl
ack
Bla
ck DRAW Whit
e 0 0 Bl
ack
White XDRAW Bl
ack 1 1 Bl
ack
White XDRAW Whit
e 0 0 Bl
ack
Bla
ck XDRAW Bl
ack 1 1 Whit
e
Bla
ck XDRAW Whit
e 0 0 Bl
ack

⇢eFr stc olumns howst hev al


ueofHCOLOR f ort heDRAW orXDRAWc om-
ma nd.⇢es econdc olumns howswhi c
hc omma ndweus e
d.⇢et hir
dc olumn
showswhi chba c
k groundc olorwa spresentwhe nt heshapetablewa sdrawn.
⇢ehe ading sC=0a ndC=1r efert othes ta
tusoft hec oll
isi
onc ount er
beforet heDRAW orXDRAW.⇢ee nt
riesine achc olumns howt hev aluea 4erthe
comma ndi se xecuted.⇢eFna lcol
umns howswhe thertheresulti
ngdoti swhi t
e
(“on” )orblack( “oL” )
.
⇢ec onc lusionstobe“ drawn”f r
om t hischartare:
1.I faDRAWi sdone ,ther esult
ingdotwi l
lbec onsis
tentwi t
ht heHCOLORus ed.
⇢ec ol
li
sionc ount erwi l
linc rementoneuni tfore ac
hdotont hes creentha tis
alreadya tthes ame“ col
or”a sthedotbe ingdr awn.⇢a tis,ifwhi teisy our
HCOLOR, thec olli
sionc ounterwi l
l c
ountt henumbe rofwhi t
edot sthesha pehits.
Ify ourHCOLORi sblack,thec ol
lis
ionc ount e
rwi llret
ur nthenumbe rofbl ac
k
dotst hes hapedr awsov er
.⇢i sa l
lowsy out ous ealightba c
k grounda ndda r
k
shape sands ti
llha veev er
ythingwor k!
2.I fXDRAWisused,
thec
urre
ntHCOLORhasnoe Lect
.XDRAWalwaysrev
ers
esthe
backg
rounddot s
.Forablackbackgr
ound,XDRAWwi l
lincre
me ntthecol
li
si
on
count
eronlyforthos
edot
stur
ned“on.”I
ftheba c
k g
roundiswhite
, t
hecol
li
si
on
count
erwillbese
tto0onl
yifal
loft
hedotsareturned“oL.

3. ⇢eprevi
ouss t
ateofthec
oll
is
ioncount
erha
snoe Lec
tontheFnalval
ue
a4e
rtheDRAW orXDRAW.⇢ismeanstha
tnoprecondi
ti
oni
ngorini
ti
al
izi
ngis
nec
ess
aryi
nagivenrouti
ne.

DRAW v
ers
usXDRAW
Beforepr
oceedi
ngf urt
herwiththec oll
is
ioncounter
,itisimpor t
anttot a
k e
amome nttocl
ari
fythedist
inct
ionbe t
wee nthetwoshape-t
ablecomma ndsDRAW
andXDRAW.
DRAWisver
ydi r
ecti
nt hati
tbasical
lydoe sanHPLOTinwha tev
e rthecurrent
HCOLORi s
,usi
ngthes pe
c Fe
i dshape.Asme nti
onedearl
ier
,thec ol
li
s i
onc ounter
si
mpl yaddsupthetotalnumberofc ol
li
sionswithexis
ti
ngdot sinthes a
me“ on”

]
2
28 As
sembl
yLi
nes

or“oL”s tat
east heHCOLORbe ingus e
d. Mov i
ngs hapeswithDRAWi sdoneFr stby
drawingtheFg ure, andthene itherrever
singthec ol
orbys et
tingHCOLORt oblack
andt hendoinga notherDRAW,orus ingXDRAWt oa cc
ompl is
ha ppr oxi
matel
yt he
samet hi
ng .
XDRAW,ont heot herha nd,us estheEOR( Exclusi
veOR)f unc ti
ont oact
ua ll
y
revers
ethebi t
sont hescree
nwhe r
ethes hapeistobedra wn.Wha tthi
sme ansis
thataFx edcol
ora ss uchisnotus ed.Ra t
her,eachbitont hescre
e ni nt
hedesired
shapepa t
ternisr evers
edf r
om i t
scurrentstat
us.Byf ol
lowingt hiswithanother
XDRAW, t
hescreeni sres
toreda nde xis
tingbackgroundFg uresarenote ras
ed.

Pr
inc
ipl
esofAni
mat
iona
ndCol
li
si
on
Anyhi -
resg ameors imul ati
oni sba sica l
lyj ustas imulati
onofr eali
tyi n
whi c
has cre
eni ma ges uc
cess f
ullymi mic sthebe haviorofa nobj e
ctint her eal
wor l
d.⇢epr ima rythingst obes i
mul at
e dg e nerall
ya remot ionandc ol
lisi
ons .
Bothoft hesehav ebe endiscuss e
di nearli
e rc hapte r
s,parti
cularl
ywi t
hr e
g a
r dto
theideaofs i
mula ti
ngc onvinc i
ngmot i
ons .
Inourpr eviouspr ograms ,thepos i
tionsofa nobjectwa susedtode t
e r
mi ne
whe t
he ri twastimet obounc etheobj ectoLofawa l
lors omeot herobject.In
thi
ss ens e,wec ans ayt hatc olli
si
onswe repr edictedratherthande te
cted.⇢e
coll
isi
onc ounterg i
vesusawa yofde tec
tingc olli
sionswithobj ec
tsonthes creen
whos ec urrentpos it
ionma ynotbek nown.⇢i stakesonpr act
ica
lsigni
Fc ance
whe ny ouma ynotwa nttok eept rackofa llthet hingsSy i
nga boutthescreen, a
s
i
squi tepos si
bleinma nyga mes ce
na r
ios.
Put ti
ng a l
loft hi
st oge ther,wec ome up wi tht hef oll
owing g e
ne ral
approa che s
:
1. DRAWaFg ure .Che ckt
hec oll
isi
onc ount erf ornonz erovaluestodetectac ol-
l
ision.DRAWwi thbl ack,orXDRAW,t oe r
asef ort hene xtmov ement.Backg r
ound
Fgureswi l
lbeera s
edwhe nus ingthistechni que .
2. XDRAWaFg ure.⇢ev alueinthec ol
li
si
onc ountershoul dequalthenumbe r
ofdotsintheFg ure(t
ha ti
s,aconstantval
ue )iftherei
snoc oll
is
ionwithex i
st
ing
ima g
es.XDRAWag a
intoe ra
s e.
⇢ev aluei
nt hec oll
is
ionc ountershouldreturnto
0i fnopr ev
iousc ol
li
sionwa sma de.⇢i swi lllea
v ebackgroundi ma g
esi nt
act
,
butFg uresdrawnwi llhav ea“ ha
rlequi
n”a ppearancea stheypa s
sov erback-
groundima ges
.S e
ethef ol
lowingdemons tr
a ti
onpr ogramf orane xa
mpl eofthis
.

⇢ eSc
anne
r
⇢ef oll
owingtwode mons tra
tionprog r
amsarecal
le
dTheSca rbe
nne caus
e
t
heyarer
e mini
scentoftheclass
icradarscr
e e
nsweeppat
ter
n.
⇢eFr stprogram use
st heXDRAW,XDRAWsyst
em ofr
edra
wingt
hei
mag
eand
t
hus,
isnonde s
truct
ivetootherimag e
sont hesc
reen.
2
4.⇢eCol
li
si
onCount
er,
DRAW,
XDRAW 2
29

1 ********************************
2 * AL24-SCANNER-XDRAW,XDRAW *
3 ********************************
4 *
5 * OBJ $7000
6 ORG $7000
7 *
8 FLAG EQU $06
9 RT EQU $07
10 SCL EQU $08
11 *
12 *
13 PREAD EQU $FB1E
14 WAIT EQU $FCA8
15 HCOLOR EQU $F6F0
16 HGR EQU $F3E2
17 HPLOT EQU $F457
18 HPOSN EQU $F411
19 SPKR EQU $C030
20 *
21 ROT EQU $F9
22 SCALE EQU $E7
23 PTR EQU $E8
24 SHNUM EQU $F730
25 DRAW EQU $F605
26 XDRAW EQU $F661
27 CTR EQU $EA
28 *
7000: 4C 09 70 29 ENTRY JMP E2
30 *
7003: 01 00 04 31 TBL HEX 010004
7006: 00 04 00 32 HEX 000400
33 *
7009: A2 03 34 E2 LDX #$03 ; WHITE
700B: 20 F0 F6 35 JSR HCOLOR
700E: A2 00 36 LDX #$00
7010: 86 07 37 STX RT
7012: A2 03 38 LDX #$03
7014: 86 E8 39 STX PTR
7016: A2 70 40 LDX #$70
7018: 86 E9 41 STX PTR+1
42 *
701A: A9 01 43 SET LDA #$01
701C: 85 06 44 STA FLAG
45 *
701E: A2 8C 46 POSN LDX #$8C
7020: A0 00 47 LDY #$00 ; X = 140
7022: A9 50 48 LDA #$50 ; Y = 80
7024: 20 11 F4 49 JSR HPOSN ; SET CURSOR X,Y
50 *
7027: E6 07 51 CALC INC RT
7029: A2 00 52 LDX #$00 ; PDL 0
702B: 20 1E FB 53 JSR PREAD
702E: 98 54 TYA
702F: D0 01 55 BNE STORE
7031: C8 56 INY ; SCALE = 1

]
2
30 As
sembl
yLi
nes

7032: 84 08 57 STORE STY SCL


58 *
7034: A5 06 59 CHKFLG LDA FLAG
7036: F0 04 60 BEQ ERASE
7038: C6 06 61 DEC FLAG
703A: F0 14 62 BEQ PLOT ; ONLY ONCE
63 *
703C: A2 01 64 ERASE LDX #$01
703E: 20 30 F7 65 JSR SHNUM
7041: A5 F9 66 LDA ROT
7043: 20 61 F6 67 JSR XDRAW
68 *
7046: A6 EA 69 SOUND LDX CTR
7048: F0 06 70 BEQ PLOT
704A: AD 30 C0 71 CLK LDA SPKR
704D: CA 72 DEX
704E: D0 FA 73 BNE CLK
74 *
7050: A2 8C 75 PLOT LDX #$8C
7052: A0 00 76 LDY #$00
7054: A9 50 77 LDA #$50
7056: 20 11 F4 78 JSR HPOSN
7059: A2 01 79 LDX #$01
705B: 20 30 F7 80 JSR SHNUM
705E: A5 08 81 LDA SCL
7060: 85 E7 82 STA SCALE
7062: A5 07 83 LDA RT
7064: 85 F9 84 STA ROT
7066: 20 61 F6 85 JSR XDRAW
86 *
7069: A2 01 87 DELAY LDX #$01 ; PDL 1
706B: 20 1E FB 88 JSR PREAD
706E: 98 89 TYA
706F: 20 A8 FC 90 JSR WAIT
91 *
7072: 4C 1E 70 92 GOBACK JMP POSN
93 *
7075: D3 94 CHK

A4e
ras
sembl
ingt
hec
odea
t$7000,
ent
ert
hef
oll
owi
ngf
rom Appl
eso4:
HGR: HCOLOR = 3: HPLOT 100,0 TO 100,160

Pr
esetpaddl
e0t othemini
mum (0=farl
e4)a
ndpa
ddl
e1t
othema
ximum
(
255=farrig
ht).
Nowa ct
ivat
etherouti
nebyent
eri
ng:
CALL 28672

Exper
imentwithdiLere
ntpa ddlevalues,sl
owlyinc
reasi
ngt hera
diuswith
pa
ddle0untilthesc
annerinte
rsectsthev e
rtic
alli
ne.Atthatpointyoushould
he
aranumbe rofcl
ic
ksfrom t
hespe akerastheli
nescros
seachother.
Let
’sse
ehowt heprogram works.Line2 9st
art
stheactualc
odebyj umping
ov
ertheda t
af ort
hes hapetabl
e .⇢i sist hesameone -
dots hapetabl
eyou
2
4.⇢eCol
li
si
onCount
er,
DRAW,
XDRAW 2
31

enterede arlieri nt hi sc ha pter .Lines3 4and3 5i nitia l


izet heHCOLORt o3( whi te),
alt
houg hf ort hi spr og ramt ha ta ct
ua ll
yi snotne c ess ary. Li nes3 6t hr oug h4 1s e
t
ourv aluef orr ota tiont o0( tobeus edl ater),a nds ett hepoi nter$E8, $E9t opoi nt
atourt ablea t$7003.
Nowhe re’sthet rick ypa rt.Ing ene ralwewa ntt os tor et wopos i
tionsf ort he
li
newe ’lldr aw. ⇢eFr stistheol dpos iti
on( whe rei twa sla stdr awn)a ndt hes ec-
ondi st hene wpos itionwhe rethene wl inewi llbedr awn.You’ llr eca l
lt ha twe
dev el
ope dt hist echni quei ne a rl
iercha ptersa sawa yofmov ingdot swhi lemi ni-
mi zi
ngt hes creenSi ck er.
Fort hes impl edot s,i tdi dn’tr ea l
lyma tteri font heFr stpa sst hr oug ht he
prog r
a m wee ras edadott ha twa sn’tr eallyt here.I nt hi sc ase
,t houg h,i tdoe s
ma tterbe caus eus ingXDRAWwi llcaus ea ni ma get oa ppe ari fonewa sn’ta lre ady
theret oe ra se.
⇢i si ss olve dbyus i
ngaone -pas sSa gt ha twi llt ellthepr ogr amt os kipov er
theERASEr out ineont heFr s ttimet hr oug h.Li ne s4 3a nd4 4i nitializet hi sSa g
to1 .
Li nes4 6t hr oug h4 9us eHPOSN t opr e pa ref ort hel a terus eoft hes ha pe
tables.Li ne5 1i nc reme ntst hev aluef orr ota tionone ac hpa sst hroug ht hel oop.
⇢i sc a use st hel inet or e vol ve.Wr ap-aroundha ppe nsa ut oma ti
c al
lywhe nRT
reache s2 55 ,sonoc he ckingf orILLEGALQUANTITYe rror si sr equire d.
Li nes5 2t hroug h5 7g ett hes ca l
ev al
uef r om pa ddl e0 , whi chc or respondst o
thee vent ua lleng thoft hepl ottedl i
ne .Not et hatas pe ci
a lc he cki sdonet oa v oid
scalebe ings etto#$00, sinceAppl eso4t reat st histhes amewa yitt reats2 5 5. ⇢i s
ma kest hepa ddl e sal ittlemor efriendl ytot heus er.
Ont heFr stpa sst hroug h, FLAGwi llequa l1 ,sot het e stonl ine6 0wi llf ail
. I
t
willthe nbede cr eme nte dt o0t oc l
eart heSa g ,andt hef orc edbr anc ht oPLOTwi ll
bee xec uted.
⇢er out i
nef ordr awi ngt hes ha pei sv e rys imi l
a rt or outine si npr og rams
present edi ne a rlierc ha pte r
s .⇢ema indi Le re ncei nt hisr out inei sourus eoft he
routineXDRAW( $F661) ,whi c hi sus edt hes amewa yt heDRAWr out i
newa sus ed
before.
Onc et hePLOTs ectioni sc ompl eted, awa itisdonea tline s8 7t hroug h9 0by
usingt heWAIT( $FCA8) rout inea saf unc t
ionofpa ddl e1 .
Not icet ha tons uc cessiv epa ssest hroug ht hel oop, FLAGwi llequa l0, ands o
ERASEwi l
l alwa y se raset heol dpos i
tionbe f
or ePLOTc r eate sthene wone .RT( $07)
andSCL( $08)a reus edt ohol dt hene wv a lue sf orr ota tiona nds cale,r e spec-
tivel
y.
Be c aus ewea reus ingt heXDRAW,XDRAW me thodf ort hea ctua lc ollision
detection,wewi llus eme t
hod2 ,whi chs ay st ha tt hec ollisionc ount ers houl d
returnt o0a 4e rt heFg ur ei se rased. Weus et hisf ac ttoc he ckonl i
ne s6 9a nd7 0
foraz ero- valuec ol li
sionc ount er.Ift hec ount eri snot0 ,t hes pe akeri sc li
c ked
thatnumbe roft ime sbe f
or et hepr og ram doe st hene xtpl ot .

]
2
32 As
sembl
yLi
nes

Inpracti
cethespeakerisali
ttleunde pendablebecausethefrequencyofthe
c
li
cksissohi gh.Youma ywi shtoe x per
ime ntwithdi Lere
ntde l
ay sintheCLK
l
oop,asisdonei nthesoundr outines.Youma ypreferthec urr
entme t
hodfor
t
hisdemobe causeoftheintui
tivena tureoft hecli
cks,butmus ic
a lsoundscan
a
lsoprov
idesomei nter
est
inginsightsintothepr oc
ess.
⇢eus ualHGRequiva
lentfrom t hisroutinehaspur poselybeenl e4outto
a
ll
owy outoa lte
rthescreenwi t
hHPLOTa ndot herAppl es
o4c omma ndsbef
ore
r
unningthescanner.Anotheri
ntere s
tingvariati
onistot y
pein:
HGR: HCOLOR = 3: HPLOT 0,0: CALL 62454

⇢es
cre
ens
houl
dcl
eart
oal
lwhi
te
.Nowa
cti
vat
ethes
canne
rbyt
ypi
ngi
n:
CALL 28672

Now t hec l
ick i
ngwi llde pe ndmor edi r e
ctl
yon t hele ng
thoft hel i
ne,
alt
houg hs omei nt er
est
ingv ariati
onc a
nbeobs erv
edde pendingont heangleof
thelinea swe ll
.Whi l
ey ou’rer e
a dinga longy oumi ghtponde rwhyt hatwould
be,conside ri
ngtha tthes c
reenwoul dsee mt obec l
earl
yuniformi nthenumbe r
ofdotsthel ineisinters
ecting.
Onc ey ou’vee nte
rtai
ne dy our s
elfsuWc i
e ntl
ywi t
htheFr stprogram,tr
ythis
secondv ariati
on, onethatus estheDRAW, XDRAWme t
hod. Herethepointofinte
r-
estisthatthes canninglinee rasesany t
hi ngi ttouchesands oleav
e savis
ibl
etrai
l
ofwhe reitha sbee nwhena c
tivateda gains tas ol
idwhitebackground.
1 ********************************
2 * AL24-SCANNER-DRAW,XDRAW *
3 ********************************
4 *
5 * OBJ $7000
6 ORG $7000
7 *
8 FLAG EQU $06
9 RT EQU $07
10 SCL EQU $08
11 PREAD EQU $FB1E
12 WAIT EQU $FCA8
13 HCOLOR EQU $F6F0
14 HGR EQU $F3E2
15 HPLOT EQU $F457
16 HPOSN EQU $F411
17 SPKR EQU $C030
18 *
19 ROT EQU $F9
20 SCALE EQU $E7
21 PTR EQU $E8
22 SHNUM EQU $F730
23 DRAW EQU $F605
24 XDRAW EQU $F661
25 CTR EQU $EA
26 *
2
4.⇢eCol
li
si
onCount
er,
DRAW,
XDRAW 2
33

7000: 4C 09 70 27 ENTRY JMP E2


28 *
7003: 01 00 04 29 TBL HEX 010004
7006: 00 04 00 30 HEX 000400
31 *
7009: A2 03 32 E2 LDX #$03 ; WHITE
700B: 20 F0 F6 33 JSR HCOLOR
700E: A2 00 34 LDX #$00
7010: 86 07 35 STX RT
7012: A2 03 36 LDX #$03
7014: 86 E8 37 STX PTR
7016: A2 70 38 LDX #$70
7018: 86 E9 39 STX PTR+1
40 *
701A: A9 01 41 SET LDA #$01
701C: 85 06 42 STA FLAG
43 *
701E: A2 8C 44 POSN LDX #$8C
7020: A0 00 45 LDY #$00 ; X = 140
7022: A9 50 46 LDA #$50 ; Y = 80
7024: 20 11 F4 47 JSR HPOSN ; SET CURSOR X,Y
48 *
7027: E6 07 49 CALC INC RT
7029: A2 00 50 LDX #$00 ; PDL 0
702B: 20 1E FB 51 JSR PREAD
702E: 98 52 TYA
702F: D0 01 53 BNE STORE
7031: C8 54 INY ; SCALE = 1
7032: 84 08 55 STORE STY SCL
56 *
7034: A5 06 57 CHKFLG LDA FLAG
7036: F0 04 58 BEQ ERASE
7038: C6 06 59 DEC FLAG
703A: F0 0A 60 BEQ PLOT ; ONLY ONCE
61 *
703C: A2 01 62 ERASE LDX #$01
703E: 20 30 F7 63 JSR SHNUM
7041: A5 F9 64 LDA ROT
7043: 20 61 F6 65 JSR XDRAW
66 *
7046: A2 8C 67 PLOT LDX #$8C
7048: A0 00 68 LDY #$00
704A: A9 50 69 LDA #$50
704C: 20 11 F4 70 JSR HPOSN
704F: A2 01 71 LDX #$01
7051: 20 30 F7 72 JSR SHNUM
7054: A5 08 73 LDA SCL
7056: 85 E7 74 STA SCALE
7058: A5 07 75 LDA RT
705A: 85 F9 76 STA ROT
705C: 20 05 F6 77 JSR DRAW
78 *
705F: A6 EA 79 SOUND LDX CTR
7061: F0 06 80 BEQ DELAY
7063: AD 30 C0 81 CLK LDA SPKR
7066: CA 82 DEX

]
2
34 As
sembl
yLi
nes

7067: D0 FA 83 BNE CLK


84 *
7069: A2 01 85 DELAY LDX #$01 ; PDL 1
706B: 20 1E FB 86 JSR PREAD
706E: 98 87 TYA
706F: 20 A8 FC 88 JSR WAIT
89 *
7072: 4C 1E 70 90 GOBACK JMP POSN
91 *
7075: A9 92 CHK

I
nt hi
srout i
ne,t
heFr s
tvaria
tionisi ntheuseofDRAW(ver
susXDRAW)online
77.Inaddit
ion, bec
ausewea renowus ingtheDRAW,XDRAWmethod,thecol
li
si
on
counterdet
ec t
ionnowg oe
sa 4erthei nit
ialcre
ati
onoft hei
ma geasisdoneby
PLOT.Inter
msofpr ogrammingt hen,thec hangesar
emi nor
.Itisi
nt e
res
ti
ngt o
note,t
hough, howdiLerentl
ythes c
reenbe haves.
I
tismos tinst
ruct
ivetostar
tbyt ypingin:
HGR: HCOLOR = 3: HPLOT 0,0: CALL 62454

⇢eCALL62454i st
herout
inethatcl
earsthehi-r
esscr
eentothela
stHCOLOR
plot
ted,s
owe ’
l
ltak
ea dva
ntag
eofi ttoFllthes cr
eenwithdotsforourDRAW,
XDRAWscannertode
tect
.Makesurethepaddlesaresett
o0f orpa
ddle0and2 55
forpaddl
e1 .
⇢e nacti
vat
etherout
inebytypingin:
CALL 28672

Asy oue v e
ntuallys we e
pouta llpos s
ibleanglesandradii,you’l
lnot i
cethat
nota l
lscreenl ocati
onsc anber eachedf rom aFx edpoint.⇢i sisbe c
auseofa
l
imi tednumbe rofr otationpos i
tions( asoppos edt oacont i
nuous3 60-
degree
mot i
on)a nda ls
obe caus eoftheli
nena tur
eoft hescreendispl
ay.
Bylook ingcarefullyy oucanse ethattherearemor epointinterc
e pt
ions,
and
thusc oll
isionsa ndc li
c ks,atthene ar-verti
cal
,-horizonta
l,and4 5-
de gr
eepos i
-
tionsthana tthea nglesinbe twee
n. ⇢i st
e ndstogiveamodul ate
ds oundtot he
clicksasthe“ beam”s canswhe nrunni ngtheFr s
tpr ogr
ama g
ainstawhi teback-
gr ound.

⇢ ePos
sibi
li
ti
es
Oncey ouunderst
andt heideabe
hindthecoll
isi
onc ounter
,itcanbev ery
usef
ulinbot harc
adegame -
typeso4wareandot
hersimulati
ons.You’l
lpr obably
beabletoima gi
nealls
ortsofnovelwa
ysofappl
yingthistechni
queiny ourown
programs.
Inthene x
tchapte
r,we ’
llg
ivenon-gr
aphi
csenthusias
tsabr ea
ka ndl ooka
li
tt
lemor eintosomea r
easofas s
embl
y-la
nguag
epr ogrammi ngthatweha ve
n’t
yetcovere
d.
2
5
2
5. Ex
plo
sio
nsandS
pec
ial
E4e
cts
Oc
tob
er1
982

Inthepr e
viouschapterwel ookeda tthecoll
isi
onc ounteranda thow i
t
couldbeus e
di nhi-r
esgraphicsprogramsinwhi chcoll
is
ionsmi ghthavetobe
detec
ted.⇢i schapt
erwe ’
l
ls e
es omefurtherusesofthecoll
isi
onc ounte
r,al
ong
withsimplee x
ampl e
sofhow a ne xpl
osionmi ghtbes i
mul a
ted.Inawa y,thi
s
chaptercoul
dbec onsi
dere
dabr i
efint
roductiont
os omespecialeLect
s.

Ex
plos
ions
,Ra
ys,
andOt
her⇢ i
ngs⇢a
tGoBumpi
ntheNi
ght
⇢eba si
cpr inci
plesbe hindwr it
ings i
mpl et
oner out
ine
sina ss
emblylan-
guagewerepre s
entedinc hapterei
g ht.Asy ou’
llreca
ll,s
oundofanykindisgen-
erat
edbya ccessi
ngme mor yloc a
tion$C030.Ea chtimethisl
oca
tionisacce
ssed
byeit
herar e
a dorwr i
teoper a
tion(s ucha sanLDAorSTAc omma nd)thes
peaker
cl
icksonce.At oneorot hernoi s
ei spr oducedbydoi ngalar
genumbe rofvery
fas
tacces
ses.Consider
,forex ample, thi
ss ampleli
st
ing:
1 ********************************
2 * *
3 * AL25-SIMPLE NOISE ROUTINE *
4 * *
5 ********************************
6 *
7 * OBJ $300
8 ORG $300
9 *
10 DRTN EQU $06
11 NUM EQU $07
12 SPKR EQU $C030
13 *
14 COMBYTE EQU $E74C
15 RND EQU $EFAE
16 FAC EQU $9D
17 *
0300: 20 4C E7 18 ENTRY JSR COMBYTE
0303: 86 06 19 STX DRTN ; SET LEN OF 'NOTES'
0305: 20 4C E7 20 JSR COMBYTE
0308: 86 07 21 STX NUM ; SET # OF 'NOTES'
22 *
030A: 20 AE EF 23 LOOP JSR RND ; CREATE A RND VALUE
030D: A6 06 24 LDX DRTN ; SET A COUNTER
030F: AD 30 C0 25 TICK LDA SPKR ; TOGGLE SPEAKER
0312: A4 9F 26 LDY FAC+2 ; PITCH=RANDOM VALUE

]
2
36 As
sembl
yLi
nes

0314: 88 27 DELAY DEY


0315: D0 FD 28 BNE DELAY ; WAIT AWHILE
0317: CA 29 CYCLE DEX
0318: D0 F5 30 BNE TICK ; KEEP PLAYING
31 *
031A: C6 07 32 NUMBR DEC NUM
031C: D0 EC 33 BNE LOOP ; PLAY ANOTHER NOTE
031E: 60 34 EXIT RTS
031F: 71 35 CHK

⇢isrout
ineisi
nte
nde
dtobec
all
edf
rom Appl
eso4BAS
ICbyapr
ogr
am
s
uchast
hisone:
10 INPUT "DURATION,NUMBER:";D,N
20 CALL 768,D,N
30 GOTO 10

Whe nt her outinei sc alled,l ine s1 8t hroug h2 1us et her outineCOMBYTE


($E74C)i nAppl eso4t or ea dthev aluesbe i
ngpa ssedbyt hec all
ingpr og rama nd
storet he seva l
uesinDRTN( $06) andNUM( $07) .
DRTNi sthenus edt ode termi net hele ng thofat onet obeg enerated,andNUM
de t
er mi neshowma nyt one swi llbepl aye d.Youc ouldt hinkoft hisprog rama sa
random me l
odyg ener ator.
Atl i
ne2 3,aJSRi sdonet oAppl eso4’ sra ndom- numbe rfunc t
ion.⇢i sFlls
theSoa ti
ng -pointAc cumul ator( us uallyc al
ledFAC:$9D7$A2)wi thar andom
numbe ri nSoa t
ing -
poi ntf orm.Forourpur poseswene e donl yas ingl
eby te,
whi chwe ’
llgetfrom $9F. Ve rys hor t l
ywe ’l
l retri
evethi sby tefrom FACf orusei n
ourr out i
ne .Youmi g htt hinkt ha ta nyoft hes i
xby te
si nt heFACwoul dbes uW-
cientlyr andom,buti ttur nsoutt ha ttheFr s ttwoby tes,FAC a ndFAC+1( $9D,
$9E) ,don’ tv arysuWc ientlyt og ene rateg oodr andom numbe rs
.
Line2 4r etrie
v est hev aluef orDRTNt opr eparefore nteringt hema intone
servic eloop. TICKc l
ick sthes pe ake ronc ea ndt henloa dst heY- Re gis
terwi thour
random v alue.Be caus et hi sv a l
uei st he nus edi ntheDELAYl oop,thei nterval
be t
we enc li
ck svariese acht i
meane wr andom numbe ri sus ed.⇢i sise quivalent
toadi Lerentf reque ncybe i
ngpr oduc ede a cht i
me ,andt husg ivesusr a
ndom-
ly-pitche dnot es.
CYCLEi sas econda ryl oopt ha te xecut e
st heTICK/ DELAYl oopac ertainnum-
be roft ime s
,de termi ne di nt hisc a sebyt hev aluegiv ent oDRTNbyt hec al
li
ng
prog ra m. ⇢enumbe rofCYCLEsde termi ne stheov eralla ppa re
ntl engthofapa r-
ticul
a rt oneuni t
.
NUMBRi sal a r
g erloopt ha tde te rmine showma nynot e
st hes oundr outi
ne
wi l
lg ene rate,accordingt ot hev alueg i
v enf orNUM.
Runt heAppl eso4pr og r
a m wi tht hisr out i
nea ss
embl eda t$300a ndt rydif-
ferentc ombi nationsf orDRTNa ndNUM. IfDRTNi salargev alue(g rea
tert han2 0),a
random me lodyofNUM not esi sg ene rated.AsDRTNg etssma l
ler,youha vet o
incre aseNUMt og etsoundst ha tlas te quiva l
e ntlengthsoft i
me .⇢ev aluepa i
r1 0,
2
5.Ex
plos
ionsa
ndS
pec
ial
ELe
cts 2
37

50forDRTNa ndNUMc r
ea t
ess ortofar ay-gunsound,andthepa ir3,20pr oduc e
s
areasonablee xplos
ione Lect.Int helatte
rc ase
,theamountoft i
mee achnot eis
pla
yedbe come ss oshorttha tthenot estendtoblendtogetherintowha t’
sessen-
ti
al
lyjustar andom noi s
epa ttern.
Ar andom t onalpa tt
erni s,inf a
c t
,thek eytoanyde Fnit
ionofnoi s e
,a nd
nois
ei swha tane xplos
ioni sa l
labout. Wha twene e
disawa yofg enerati
ngal ot
ofhigh-speedr andom da taf orag oodnoi serouti
ne.⇢eRNDf unc t
ionhe l
psus
tocreatether andom da ta,buti ttake ssolongtoe xec
utether outinefore ach
notethatt hereisal i
mi ttot henumbe rofnot eswec ang e
ne r
a t
einas hort
peri
odoft i
me .
Onet echniqueweus e
de arl
ierwhe ns peedwasapr oblem wa stabl
el ook-
ups.Le t
’sapplyt histechniquet os oundg e
nerat
ionands eewha twec anpr o-
duce.
1 ********************************
2 * *
3 * AL25-SIMPLE NOISE ROUTINE 2 *
4 * *
5 ********************************
6 *
7 * OBJ $300
8 ORG $300
9 *
10 CTR EQU $06
11 DRTN EQU $07
12 PTCH EQU $08
13 SPKR EQU $C030
14 *
15 COMBYTE EQU $E74C
16 RND EQU $EFAE
17 FAC EQU $9D
18 *
0300: A9 00 19 INIT LDA #$00
0302: 85 06 20 STA CTR
0304: 20 AE EF 21 LOOP JSR RND
0307: A5 9F 22 LDA FAC+2
0309: A4 06 23 LDY CTR
030B: 99 00 10 24 STA $1000,Y
030E: E6 06 25 INC CTR
0310: D0 F2 26 BNE LOOP
0312: 60 27 DONE RTS
28 *
0313: 20 4C E7 29 ENTRY JSR COMBYTE
0316: 86 08 30 STX PTCH
0318: 20 4C E7 31 JSR COMBYTE
031B: 86 07 32 STX DRTN
33 *
031D: A0 00 34 READ LDY #$00
031F: B9 00 10 35 BYTE LDA $1000,Y
0322: A2 08 36 LDX #$08
0324: 4A 37 SHIFT LSR
0325: 90 03 38 BCC NEXTBIT

]
2
38 As
sembl
yLi
nes

0327: 8D 30 C0 39 TICK STA SPKR


40 *
032A: CA 41 NEXTBIT DEX
032B: D0 F7 42 BNE SHIFT
032D: A6 08 43 LDX PTCH
032F: CA 44 DELAY DEX
0330: D0 FD 45 BNE DELAY
0332: C8 46 NEXTBYTE INY
0333: D0 EA 47 BNE BYTE
0335: C6 07 48 DEC DRTN
0337: D0 E4 49 BNE READ
0339: 60 50 EXIT RTS
033A: 33 51 CHK

⇢i srout
ineha stwoent
rypoint
s.⇢ismeansthatt
heroutinehastobe
ca
ll
edtwice.⇢eFr stti
me,ac a
llto$300(7
68dec
imal)ge
nerat
esthetabl
eof
dat
atobeus e
d.⇢i sneedbedoneonlyonce
.⇢enoisepat
ter
nisg e
nera
tedby
ca
lli
ng$313 (78
7de ci
ma l
)wheneverasoundi
sdesir
ed.⇢i srout
ineisal
so
des
igne
dtobec al
le
df r
om anApple
so4BASICpr
ogramsuchas1
:
10 CALL 768: REM CREATE TABLE
20 INPUT "PITCH,DURATION";P,D
30 CALL 787,P,D : REM CALL NOISE ROUTINE
40 GOTO 20

Int hiscasethetwopa ra meterspass edtot henoi serout inea r epitch( PTCH)


anddur at
ionoft henoi seper i
od( DRTN). AtFr stthoug ht ,pitchma yseemt obea
contradictorynotionwhe na pplie
dt onoi s e
,pa rti
c ularl
yi nl ightofourpr evious
deFnitionofnoi seasar andom mi xoff reque ncies. ⇢epi tch, howe ver,doesnot
needt obea ne nt
irelyhomog e ne
ousmi x tureoff reque nc i
es .
It’
spos sibl
et ofavore it
he rhighorl owf re que nciesint hemi xa ndt hust o
inSuenc ethes uggesti
v ena tureoft henoi s
e.Hi gh- f
reque nc ymi xess oundl ike
raysorf ast-
mov i
ngr oc kets.Low-freque ncymi x esr emi ndt hel is
te neroft helow
roarofas l
ow- mov i
ngr ocketorag arden-v a
rietye xplosion.
Ex aminingthene wr out i
ne,then,l et’
sse ehowt hisnoi s ege neratorwor ks
.
⇢eFr stc al
ltoINITc reatesthetableofr andom v a l
ue s.Line s1 9a nd2 0ini t
iali
ze
to$00ac ounterwe ’l
lbeus ingshor tl
y.A c a l
lt ot her a
ndom f unc t
ioni sthe n
ma det og enerat
ear andom by t
e.Ne xt,theY- Re gist
eri sl oade dwi tht hev alue
heldi nCTR.⇢i sv aluei sus edasa ni nde xt ot hel oc ationi nt her ang efrom
$1000 t hrough$10FFwhe rewewi l
lstor ether andom by te.CTRi stheni nc r
e-
me ntedt othenex tpositi
ona ndLOOPi se xecutedunt ilCTRwr a psba cka roundt o
$00a 4e rcycl
ing2 56time s.
Youma ywonde rwhyt hiscodewa snotus e dins tead:
LDY #$00
LOOP JSR RND
LDA FAC+2

1[
CT]Li
ne2
0inc
orr
ect
lyha
dINPUT“D,P”;P,D
2
5.Ex
plos
ionsa
ndS
pec
ial
ELe
cts 2
39

STA $1000,Y
INY
BNE LOOP

Althoug hi t
’smuc hs hor tera ndmor edi re ct,ther outinef ail
sbe caus eRND
scramblestheY- Re gister,thusl os i
nga nyr unni ngv al
uef orourpos it
ioni nt he
tablebeingc re ated.⇢i sfac tne ce ssi
tatest heus eofaba ck-upc ounter( CTR)t o
reme mbe rthec ur rentv al
uet hatYs houl dbes ett o.
⇢eINITr out ine ,the n, willFl l256by tesofme mor ys tarti
nga t$1000wi tha
random pa tternofby te s
.Mor ei mpor tant ,thi sa lsor esultsinar andom pa ttern
ofbits,whichwi ll beus edv e rysoonbyt henoi ser outine .
Whe n$313( 78 7de cima l)i sc all
e d, COMBYTEi sus e dt or eadt hev al
ue sfor
PTCHa ndDRTNf rom t hec allingAppl eso4pr og ram.
READt hens ta rtsthepr ocessofs ca nni ngt heda tata blefort her a
ndom da t
a
tobeus eding e ne ratingt henoi sepa t
te rn.⇢et ri
cki nt hisprog ramc ome si n
us i
ngthea ctua lbi ts t
a tusoft heda tarathe rt ha ne nti
reby tes.
A4e rea chby tei sl oade di ntot heAc cumul a toronl ine3 5 ,
abi t-
shi4ingr ou-
tineisexecute de ightt ime st ode termi net heonoroLs tatusofe a c
hbi t.Line3 6
init
ial
izestheX- Re gistertoa cta sourc ount eri nt hise i
g ht-steploop.
Line3 7us e st heLSRc omma nd( Log ic alS hi4Ri ght)t omov ea l
lofthebi tsin
theAc cumul a toronepos itiont ot her ight .⇢ee nd- pos it
ionbi t,bit0,f a
llsi nto
thec a
rry.
Line3 8t he nt e st
st hec arrySa ga nd, ift heSa gi scle ar(bitnots et)
, skipst he
speaker-t
og glings tepf ounda tline3 9.
NEXTBITd ec reme ntsourc ount e
ri nt heX- Re gi
ster,a ndi fXha s
n’tr ea ched
0,loopsba c kt oSHIFT.I fXha sr ea c
he d0 ,Xi sr es
e twi tht hePTCHv aluea nda
de l
ayloopi se nte red.
Whe nthede la yloopi sFni she d,theY- Re gisterisi nc r
eme nt e
di npr e pa r
a-
tionforreadingt hene xtby tei nt heda tat able .
Asi tha ppe ns ,re adinge achbi tof2 56by tesdoe snott aket hatlong .Our
soundwoul dbeov err athe rsooni fwedi dn’ tdoj ustonee xt
ras tep.Althoug hwe
couldg eneratea ndr e adl arge rta bles,a not he ra ppr oac hi stor ereadthet ablea
setnumbe roft ime s.⇢i si swhe ret heDRTNv aluei sus e d,a ndt hetabler ea dis
repeatedthenumbe roft i
me sspe ciFe dbyDRTN.
⇢ema ina reaofe xpe rime nta ti
oni nt hi sr out i
nei swi t
hdi Le r
entv alue sfor
PTCH. Sma l
lerv a lue spr oduc ehi ghe r-soundi ngnoi sepa tterns;largerva l
ue sg en-
eratemor eofar oa r.

ALi
tt
leMor
eSophi
sti
cat
ion
⇢isla
strout
ineproba
blysoundsmoreli
kea nexplosi
ont oyouthanthe
Frstonedi
d.⇢isisduetot
hehighernoi
sec
ontentoft
hes ounda sc
omparedto
themor emusi
calFrs
trouti
ne.Somethi
ngisst
illmis
sing,though.A cl
ass
ica
l

]
2
40 As
sembl
yLi
nes

explos i
ondoesn’tsoundthes a
mefrom s
tar
ttoFni
sh.Itusual
lyst
artsatahig
her
orl owe rpi
tcha ndwor ksitswayupordown,dependingont hena t
ureofthe
explos i
on.Wha twene e
di sawa yt
omodulat
ethef
requencymi xasafunct
ionof
time .
Bylink
ingt hedelayv a
luetoourposi
ti
oninthet a
ble,weca naccompli
sh
thisg oal
.Here’
st henewlist
ing:
1 ********************************
2 * *
3 *AL25-SIMPLE RAMP NOISE ROUTINE*
4 * *
5 ********************************
6 *
7 * OBJ $300
8 ORG $300
9 *
10 CTR EQU $06
11 DRTN EQU $07
12 PTCH EQU $08
13 SPKR EQU $C030
14 *
15 COMBYTE EQU $E74C
16 RND EQU $EFAE
17 FAC EQU $9D
18 *
0300: A9 00 19 INIT LDA #$00
0302: 85 06 20 STA CTR
0304: 20 AE EF 21 LOOP JSR RND
0307: A5 9F 22 LDA FAC+2
0309: A4 06 23 LDY CTR
030B: 99 00 10 24 STA $1000,Y
030E: E6 06 25 INC CTR
0310: D0 F2 26 BNE LOOP
0312: 60 27 DONE RTS
28 *
0313: 20 4C E7 29 ENTRY JSR COMBYTE
0316: 86 07 30 STX DRTN
0318: A0 00 31 READ LDY #$00
031A: B9 00 10 32 BYTE LDA $1000,Y
031D: A2 08 33 LDX #$08
031F: 4A 34 SHIFT LSR
0320: 90 03 35 BCC NEXTBIT
0322: 8D 30 C0 36 TICK STA SPKR
37 *
0325: CA 38 NEXTBIT DEX
0326: D0 F7 39 BNE SHIFT
0328: A6 07 40 LDX DRTN
032A: CA 41 DELAY DEX
032B: D0 FD 42 BNE DELAY
032D: C8 43 NEXTBYTE INY
032E: D0 EA 44 BNE BYTE
0330: C6 07 45 DEC DRTN
0332: D0 E4 46 BNE READ
0334: 60 47 EXIT RTS
2
5.Ex
plos
ionsa
ndS
pec
ial
ELe
cts 2
41

0335: 39 48 CHK

⇢ispr
ogr
ami
sde
sig
nedt
obec
all
edf
rom a
nAppl
eso4pr
ogr
amt
hatl
ook
s
l
iket
his
:
10 CALL 768: REM GENERATE TABLE
20 INPUT "START?";S
30 CALL 787, S
40 GOTO 20

⇢ema indiLerencebetweenthisrout
ineandthepr e
viousonei st
hatjust
pri
ortothedelayloop,theX- Regi
ste
risloadedwiththecurrentDRTNcounter
val
ue,asoppos
edt oaus e
r-deFnedpitchval
ue.⇢us ,noPTCHisspeciFe
dint he
cal
li
ngprograma ndy ouma ys el
ectonlyastar
ti
ngpointont heramp,a sitis
someti
mescall
ed.
Enteri
ngav alueof2 55re s
ult
sint helonge
stsoundpos s
ible
.Itisrat
he r
i
ntere
sti
ngtoha veyourAppl esoundlikea7 27rea
dyt ota
k eoLthroughyour
cei
li
ng.

Put
ti
ngi
tAl
lTog
ethe
r
Nowt hatwe ’
veg otsomes oundeLect
stoa ddtoourknowledg
eofhi -
res
g
ra phi
cs,let
’spute very
thingtoget
herint
oas impledemonst
rat
ionofhow a n
e
xpl os
ionmi ghtbesimulatedi
nag ameprogra
m.
Asse
mbl et hefol
lowinglist
ingandruni teit
herwit
hBRUN orCALL 4096
(
from BAS IC),or1000G(from t
heMonitor)
.
1 ********************************
2 * *
3 *AL25-SIMPLE EXPLOSION ROUTINE *
4 * *
5 ********************************
6 *
7 ORG $1000
8 *
9 NUM EQU $06
10 SPKR EQU $C030
11 *
12 RND EQU $EFAE
13 FAC EQU $9D
14 KYBD EQU $C000
15 STROBE EQU $C010
16 *
17 HGR EQU $F3E2
18 HCOLOR EQU $F6F0
19 SHNUM EQU $F730
20 XDRAW EQU $F661
21 HPOSN EQU $F411
22 SHTBL EQU $E8
23 SCALE EQU $E7
24 *

]
2
42 As
sembl
yLi
nes

1000: 4C 63 10 25 ENTRY JMP START


26 *
1003: 03 00 55 27 TABLE HEX 0300550033000800
1006: 00 33 00 08 00
100B: 2C 24 2D 28 HEX 2C242D242DE4DB93
100E: 24 2D E4 DB 93
1013: 3E 36 37 29 HEX 3E36372E362D3635
1016: 2E 36 2D 36 35
101B: 36 2D C6 30 HEX 362DC6DBDB23272C
101E: DB DB 23 27 2C
1023: 25 2C 3C 31 HEX 252C3C3F363F373E
1026: 3F 36 3F 37 3E
102B: 36 40 C0 32 HEX 3640C040C028352E
102E: 40 C0 28 35 2E
1033: 35 2D 00 33 HEX 352D00243F3CBC12
1036: 24 3F 3C BC 12
103B: 0E 96 09 34 HEX 0E9609C04C493C2C
103E: C0 4C 49 3C 2C
1043: 2C 2D 24 35 HEX 2C2D2494921A352D
1046: 94 92 1A 35 2D
104B: 36 EE DB 36 HEX 36EEDB233C27941B
104E: 23 3C 27 94 1B
1053: 3E 36 3F 37 HEX 3E363F06001B282D
1056: 06 00 1B 28 2D
105B: 2D F8 DB 38 HEX 2DF8DB636DE52300
105E: 63 6D E5 23 00
39 *
1063: 20 E2 F3 40 START JSR HGR
1066: A2 03 41 LDX #$03 ; WHITE
1068: 20 F0 F6 42 JSR HCOLOR
106B: A9 03 43 LDA #$03
106D: 85 E8 44 STA SHTBL
106F: A9 10 45 LDA #$10
1071: 85 E9 46 STA SHTBL+1 ; TABLE AT $1003
1073: A9 01 47 LDA #$01
1075: 85 E7 48 STA SCALE ; SCALE = 1
1077: A9 0A 49 LDA #$0A
1079: 85 06 50 STA NUM ; # OF CYCLES
51 *
107B: A2 8C 52 SHIP LDX #$8C
107D: A0 00 53 LDY #$00 ; X = 140
107F: A9 50 54 LDA #$50 ; Y = 80
1081: 20 11 F4 55 JSR HPOSN ; POSITION 'CURSOR'
1084: A2 01 56 LDX #$01 ; #1 = SHIP
1086: 20 30 F7 57 JSR SHNUM
1089: A9 00 58 LDA #$00 ; ROT = 0
108B: 20 61 F6 59 JSR XDRAW
60 *
108E: AD 00 C0 61 KEY? LDA KYBD
1091: 10 FB 62 BPL KEY? ; NO KEYPRESS
1093: 8D 10 C0 63 STA STROBE ; CLEAR STROBE
64 *
1096: A2 8C 65 ERASE1 LDX #$8C
1098: A0 00 66 LDY #$00
109A: A9 50 67 LDA #$50
109C: 20 11 F4 68 JSR HPOSN
2
5.Ex
plos
ionsa
ndS
pec
ial
ELe
cts 2
43

109F: A2 01 69 LDX #$01


10A1: 20 30 F7 70 JSR SHNUM
10A4: A9 00 71 LDA #$00
10A6: 20 61 F6 72 JSR XDRAW ; ERASE SHIP
73 *
10A9: A2 8C 74 LOOP LDX #$8C
10AB: A0 00 75 LDY #$00
10AD: A9 50 76 LDA #$50
10AF: 20 11 F4 77 JSR HPOSN
10B2: A2 02 78 LDX #$02 ; 1ST EXPL SHAPE
10B4: A5 06 79 LDA NUM
10B6: 6A 80 ROR
10B7: B0 01 81 BCS SET ; IF 'ODD'
10B9: E8 82 INX ; 2ND EXPL SHAPE
10BA: 20 30 F7 83 SET JSR SHNUM
10BD: A9 00 84 LDA #$00
10BF: 20 61 F6 85 JSR XDRAW ; DRAW EXPLOSION
86 *
10C2: 20 AE EF 87 GETPTCH JSR RND
10C5: A2 10 88 LDX #$10
10C7: AD 30 C0 89 TICK LDA SPKR ; CLICK SPEAKER
10CA: A4 9F 90 LDY FAC+2 ; PITCH = RND
10CC: 88 91 DELAY DEY
10CD: D0 FD 92 BNE DELAY
10CF: CA 93 CYCLE DEX
10D0: D0 F5 94 BNE TICK
95 *
10D2: A2 8C 96 ERASE2 LDX #$8C
10D4: A0 00 97 LDY #$00
10D6: A9 50 98 LDA #$50
10D8: 20 11 F4 99 JSR HPOSN
10DB: A2 02 100 LDX #$02
10DD: A5 06 101 LDA NUM
10DF: 6A 102 ROR
10E0: B0 01 103 BCS SET2 ; IF 'ODD'
10E2: E8 104 INX ; 2ND EXPLOSION FIG.
10E3: 20 30 F7 105 SET2 JSR SHNUM
10E6: A9 00 106 LDA #$00
10E8: 20 61 F6 107 JSR XDRAW ; ERASE FIGURE
108 *
10EB: C6 06 109 DRTN DEC NUM
10ED: D0 BA 110 BNE LOOP
10EF: 60 111 EXIT RTS
10F0: 28 112 CHK

Whe nt heprogrami srun,thehi-resscreenshouldclea


ra ndaSy i
ng-saucer-
li
k es hips houlda ppearint hemi ddleoft hescreen.Pres
si
nga nyk eywi l
lblow
upt hes pa ceship.Let
’sseehowt hisisdone .
Line s27t hrough3 8c onta
int heda taforat hree
-el
eme nts hapetable.⇢i s
tablei sjumpe doverwhe nthepr ogrami sFrstrun.STARTclearsthehi-resscr
e e
n
int heus ua lma nnera ndiniti
al
izesthes hape-t
ablepointe
rsa ndt heHCOLORa nd
SCALEv alues.Lines49a nd5 0s etNUMt o1 0,t
obeus edlat
era st henumbe rof
cyclest hee xplosi
onrout i
newi l
lg othrough.

]
2
44 As
sembl
yLi
nes

SHIPd rawst hes pa ce s


hi pi nt hec ent eroft hes cre en.KEY?wa it
sf orak ey-
press.Whe nak eyi spr e
s se
d, thec odemov esont oERASE, whiche ra sesthes hip
priort os tart
ingt hee xplos i
ons e que nc e .
⇢ee xplosions e que ncei ts elfc ons istsofat hr e
e -pa rtloop.⇢e sepa rt
sc on-
sis
tof :( 1)dr awi ngoneoft woe xplos ions ha pes,(2 )c reatingal ittlenoi sewi th
thespe a ker,and( 3)e rasingt hee x plos ions hapedr a
wni ns tep1 .
⇢i ss eque nc ei st he nr e pe atedanumbe roft i
me sde pendi ngonhowl ong
youwa ntt hee xpl os iont ol ast. Inde ta il
, her e’
showt hiss eque ncei sc arriedout .
Li nes7 477 7pos itiont hehi -resc urs ora tthes hip’ soldpos it
ion. Line s7878 2
thens electoneoft het woe x plos ions ha pesi nclude di nt het ableba sedon
whe the rNUM( t
hec ur re ntloopc ount er)i soddore ve n.
⇢i si sdonebyFr stloa di ngt heX- Re gis
terwi thwha tmi ghtbec al
leda
defaultv alueof$02f ort heFr ste xpl osions ha pe( whi chi sthes ec ondi temi nt he
table).NUMi sthe nl oa dedi nt ot heAc cumul at
ora ndaROR( ROt a teRi ght )com-
ma ndi sdonet os hi4a l
loft hebi tst ot her i
ghtonepos it
ion.Bi t0wi llthenbe
forcedi nt othec a rry ,whe rewec ant es twi t
ht heBCS( Br anc hCa rryS et)com-
ma nd. ( ⇢i siss i
mi lart ot het echni queus ede arl
ierf ort henoi ser out i
ne .Inf act
,
theLSRc omma ndwoul dha v ewor k edj us ta swe llhe re,butal ittlev arietyc an
some time sbeni ce .
)
Ift hec arrywa ss et,the nNUMwa sodda ndwe ’l
lg or ightt ot hene xtpha se .I
f
thec arr ywa sc lea r,t he nNUMwa se v ena ndt heINX( INc reme ntX)wi llbee xe-
cuted.Re me mbe rt ha ttheX- Re g isteri sa l
wa ysl oade dwi tht hes ha penumbe r
youwa nttoDRAWorXDRAWpr iort oc all
ingSHNUM. Ift heINXi sdone ,Xg oe sfrom
$02 t o$03,t husi ndi c at
ings ha penumbe r3 ,whi chc orrespondst ot hes econd
explos i
ons hapei nt het able.
Onc ea ne x plos ions ha peha sbe endr a wn,t heFr stnoi ser out inepr esented
earli
eri sus e
dt og e ne rateas hor tbur s tofqui ckr andom not es.⇢i spa sse
sf or
someba ckgroundnoi s efora ne x plos ion. A4e raf ewqui c ksounds , ERASE2a gain
XDRAWst hes hapes ele ct
e di nLOOP.⇢i sha sthee Le ctofe rasingt hepr evious
ima ge. Finally,li
ne s1 0 9a nd1 1 0c he c kNUMt oseei ft hel oopi sFni shed. Aswr it
-
ten,line4 9setst hel oopc ount ert ot enpa sses,buty ouma ywa ntt ot rydi Lerent
valuest os uityourownt a st
e s.
Be c ausea l
li ma gingi sdonewi thXDRAW, theHCOLORs ett
inga ctua l
lyi sirrel
e-
vant;t hisr outinewoul dwor kona nys c reenba c kground.Youma ywa nttot ry
clear
ingt hes creent odi Le r
e ntba ck g roundsa sde scribe dint hepr ev i
ousc hapter
ands eehowt her out ineg ivenhe r ebe ha v es.
2
5.Ex
plos
ionsa
ndS
pec
ial
ELe
cts 2
45

⇢eShoot
erPr
ogr
am
Wha tweneednowi ssomesor
tofc ol
lec
ti
vee x
ampleofhowa l
loft
hisca
n
beputtoge
the
rasitmightbedonei
na na c
tualg
ame .Al
thoug
hit’
snotne
cess
ar-
il
yyourdeFnit
ivehi
-re
sa rca
degame,thefoll
owingisoLere
dforyourgene
ral
int
ere
standamusement2
.
1 ********************************
2 * AL25-SHOOTER PROGRAM *
3 ********************************
4 *
5 *
6 ORG $1000
7 *
8 FLAG EQU $E3
9 X EQU $E0
10 Y EQU $E2
11 X0 EQU $06
12 Y0 EQU $08
13 NUM EQU $0C
14 *
15 PREAD EQU $FB1E
16 WAIT EQU $FCA8
17 PB0 EQU $C061
18 HCOLOR EQU $F6F0
19 HGR EQU $F3E2
20 HPLOT EQU $F457
21 HPOSN EQU $F411
22 HLIN EQU $F53A
23 ROT EQU $F9
24 SCALE EQU $E7
25 SHNUM EQU $F730
26 DRAW EQU $F605
27 XDRAW EQU $F661
28 HFIND EQU $F5CB
29 CTR EQU $EA
30 PTR EQU $E8
31 SPKR EQU $C030
32 RND EQU $EFAE
33 FAC EQU $9D
34 *
1000: 4C 67 10 35 ENTRY JMP E2
36 *
1003: 04 00 59 37 HEX 0400590037000C00
1006: 00 37 00 0C 00
100B: 0A 00 04 38 HEX 0A0004002C242D24
100E: 00 2C 24 2D 24
1013: 2D E4 DB 39 HEX 2DE4DB933E36372E
1016: 93 3E 36 37 2E
101B: 36 2D 36 40 HEX 362D3635362DC6DB
101E: 35 36 2D C6 DB
1023: DB 23 27 41 HEX DB23272C252C3C3F

2[
CT]Li
nes9
479
8we
rec
hang
edt
odi
videt
hepa
ddl
eva
lueby4t
oconv
ertt
her
ota
tion
i
ntot
hea
ll
owe
dra
ngeof0
763
.

]
2
46 As
sembl
yLi
nes

1026: 2C 25 2C 3C 3F
102B: 36 3F 37 42 HEX 363F373E3640C040
102E: 3E 36 40 C0 40
1033: C0 28 35 43 HEX C028352E352D0024
1036: 2E 35 2D 00 24
103B: 3F 3C BC 44 HEX 3F3CBC120E9609C0
103E: 12 0E 96 09 C0
1043: 4C 49 3C 45 HEX 4C493C2C2C2D2494
1046: 2C 2C 2D 24 94
104B: 92 1A 35 46 HEX 921A352D36EEDB23
104E: 2D 36 EE DB 23
1053: 3C 27 94 47 HEX 3C27941B3E363F06
1056: 1B 3E 36 3F 06
105B: 00 1B 28 48 HEX 001B282D2DF8DB63
105E: 2D 2D F8 DB 63
1063: 6D E5 23 49 HEX 6DE52300
1066: 00
50 *
1067: 20 E2 F3 51 E2 JSR HGR ; CLR SCRN
106A: A2 03 52 LDX #$03
106C: 20 F0 F6 53 JSR HCOLOR
54 *
106F: A2 00 55 WALL LDX #$00
1071: A0 00 56 LDY #$00 ;X=0
1073: A9 05 57 LDA #$05 ;Y=5
1075: 20 57 F4 58 JSR HPLOT ; PLOT 0,5
1078: A9 17 59 LDA #23 ; 279 MOD 256
107A: A2 01 60 LDX #01 ; 279/256
107C: A0 05 61 LDY #$05 ;Y=5
107E: 20 3A F5 62 JSR HLIN ; 0,5 TO 279,5
63 *
1081: A9 17 64 LDA #$17
1083: A2 01 65 LDX LDX #$01 ; X = 279
1085: A0 06 66 LDY #$06 ;Y=6
1087: 20 3A F5 67 JSR HLIN ; 279,5 TO 279,6
68 *
108A: A9 00 69 LDA #$00
108C: A2 00 70 LDX #$00 ;X=0
108E: A0 06 71 LDY #$06 ;Y=6
1090: 20 3A F5 72 JSR HLIN ; 279,6 TO 0,6
73 *
1093: A9 03 74 SET LDA #$03
1095: 85 E8 75 STA PTR
1097: A9 10 76 LDA #$10
1099: 85 E9 77 STA PTR+1 ; SET TBL = $1003
109B: A9 01 78 LDA #$01
109D: 85 E7 79 STA SCALE
109F: 85 E3 80 STA FLAG
10A1: A9 0A 81 LDA #$0A
10A3: 85 0C 82 STA NUM ; # OF EXPLOSIONS
10A5: A2 8C 83 SHIP LDX #$8C
10A7: A0 00 84 LDY #$00
10A9: A9 50 85 LDA #$50
10AB: 20 11 F4 86 JSR HPOSN
10AE: A2 01 87 LDX #$01 ; #1 = SHIP
10B0: 20 30 F7 88 JSR SHNUM
2
5.Ex
plos
ionsa
ndS
pec
ial
ELe
cts 2
47

10B3: A9 00 89 LDA #$00 ; ROT = 0


10B5: 20 05 F6 90 JSR DRAW
91 *
10B8: A2 00 92 CALC LDX #$00
10BA: 20 1E FB 93 JSR PREAD
10BD: 84 F9 94 STY ROT ; [CT] NEED TO SCALE TO 0-63
10BF: 46 F9 95 LSR ROT ; [CT] DIVIDE BY 2
10C1: 46 F9 96 LSR ROT ; [CT] DIVIDE BY 2
10C3: A9 48 97 PAUSE LDA #$48
10C5: 20 A8 FC 98 JSR WAIT
10C8: AD 61 C0 99 SHOOT? LDA PB0
10CB: 30 03 100 BMI YES
10CD: 4C B8 10 101 JMP CALC ; (NOPE)
102 *
10D0: A2 8C 103 YES LDX #$8C
10D2: A0 00 104 LDY #$00
10D4: A9 4E 105 LDA #$4E ; Y = INSIDE SHIP
10D6: 20 11 F4 106 JSR HPOSN
10D9: A2 04 107 LDX #$04 ; #4 = SINGLE DOT
10DB: 20 30 F7 108 JSR SHNUM
10DE: A5 F9 109 LDA ROT
10E0: 20 61 F6 110 JSR XDRAW ; DRAW RAY
111 *
10E3: A2 05 112 SOUND LDX #$05 ; # OF CYCLES
10E5: AD 30 C0 113 TICK LDA SPKR
10E8: A4 E7 114 LDY SCALE
10EA: 88 115 DELAY DEY
10EB: D0 FD 116 BNE DELAY
10ED: CA 117 CYCLE DEX
10EE: D0 F5 118 BNE TICK
119 *
10F0: A2 8C 120 ERASE1 LDX #$8C
10F2: A0 00 121 LDY #$00
10F4: A9 4E 122 LDA #$4E
10F6: 20 11 F4 123 JSR HPOSN
10F9: A2 04 124 LDX #$04
10FB: 20 30 F7 125 JSR SHNUM
10FE: A5 F9 126 LDA ROT
1100: 20 61 F6 127 JSR XDRAW ; ERASE RAY
1103: A5 EA 128 LDA CTR
1105: C9 02 129 CMP #$02
1107: B0 0F 130 BCS HIT
131 *
1109: E6 E7 132 NEXT INC SCALE
110B: E6 E7 133 INC SCALE
110D: E6 E7 134 INC SCALE
110F: A5 E7 135 LDA SCALE
1111: C9 90 136 CMP #$90
1113: 90 BB 137 BCC YES
1115: 4C 7B 11 138 JMP MISS
139 *
1118: 20 CB F5 140 HIT JSR HFIND ; GET CURSOR POSN
111B: A5 E0 141 LDA X
111D: 85 06 142 STA X0
111F: A5 E1 143 LDA X+1
1121: 85 07 144 STA X0+1

]
2
48 As
sembl
yLi
nes

1123: A5 E2 145 LDA Y


1125: 85 08 146 STA Y0 ; SAVE CURSOR POSN
1127: A9 01 147 LDA #$01
1129: 85 E7 148 STA SCALE ; RESET SCALE
149 *
112B: A2 03 150 EXPLOS LDX #$03 ; WHITE
112D: 20 F0 F6 151 JSR HCOLOR
1130: A6 06 152 LDX X0
1132: A4 07 153 LDY X0+1
1134: A5 08 154 LDA Y0
1136: 20 11 F4 155 JSR HPOSN
1139: A2 02 156 LDX #$02 ; 1ST EXPLOSION
113B: A5 0C 157 LDA NUM
113D: 6A 158 ROR
113E: B0 01 159 BCS BOOM ; IF 'ODD'
1140: E8 160 INX
1141: 20 30 F7 161 BOOM JSR SHNUM
1144: A9 00 162 LDA #$00
1146: 20 05 F6 163 JSR DRAW ; DRAW 1ST EXPLOSION
164 *
1149: 20 AE EF 165 GETPTCH JSR RND
114C: A2 10 166 LDX #$10 ; # OF CYCLES
114E: AD 30 C0 167 TICK2 LDA SPKR
1151: A4 9F 168 LDY FAC+2 ; PITCH = RND
1153: 88 169 DELAY2 DEY
1154: D0 FD 170 BNE DELAY2
1156: CA 171 CYCLE2 DEX
1157: D0 F5 172 BNE TICK2
173 *
1159: A2 00 174 ERASE2 LDX #$00 ; BLACK
115B: 20 F0 F6 175 JSR HCOLOR
115E: A6 06 176 LDX X0
1160: A4 07 177 LDY X0+1
1162: A5 E2 178 LDA Y
1164: 20 11 F4 179 JSR HPOSN
1167: A2 02 180 LDX #$02
1169: A5 0C 181 LDA NUM
116B: 6A 182 ROR
116C: B0 01 183 BCS BOOM2 ; IF 'ODD'
116E: E8 184 INX
116F: 20 30 F7 185 BOOM2 JSR SHNUM
1172: A9 00 186 LDA #$00 ; ROT = 0
1174: 20 05 F6 187 JSR DRAW ; ERASE FIGURE
1177: C6 0C 188 DRTN DEC NUM
1179: D0 B0 189 BNE EXPLOS
190 *
117B: A9 01 191 MISS LDA #$01
117D: 85 E7 192 STA SCALE ; RESET SCALE
117F: A9 0A 193 LDA #$0A
1181: 85 0C 194 STA NUM ; RESET NUM
195 *
1183: 4C B8 10 196 AGAIN JMP CALC
197 *
1186: 66 198 CHK
2
5.Ex
plos
ionsa
ndS
pec
ial
ELe
cts 2
49

⇢i si sa ni ndependentprogramt hatcanbec all


edfrom Appl eso4BAS ICby
typi
ng i nCALL 4096 orf rom theMoni torbyt ypingi n1000G.Youc ana ls
o
dire
c tl
yBRUNt hea s
sembledobjectFle.
Whe nt hepr ogra
mi srun,aspa ces
hip-li
keforms i
mi l
a rtot heonedr awni n
thee xplosionr outineisdrawni nt hec enterofthes creen.Att hetopoft he
scre
e n,awa l
lma deupoft wohor i
zontalli
nesisa l
sodr awn.Tur ningpa ddle0
andpr ess i
ngt hec orres
pondingpus hbuttonwillFrear ayf rom thes hip.Ifthe
rayhi t
st hewa ll,anexplosi
onoc cursandt hewa l
lisle4s uitabl
yda maged.You
mus tpres sRESETt oterminat
ethepr ogram.
⇢epr og r
amc ombinesma nyoft hetechniquesde s
c ri
be dint hischa pte
r
andt hepr e viousone .I
tcanbesumma r
izedasfoll
ows :
1.I nit
ia l
iz eas hapetablecontaini
ngf ourshape s
:aspa ceship,twoe xplosions,
andaone - dots hapefortherayeLe c
t.
2
. HPLOTawa
lloft
wohor
izont
all
ine
satt
opofs
cre
en.
3
. DRAWs
hape1(
thes
pac
eshi
p)a
tthec
ent
eroft
hes
cre
en.
4
. Re
adpa
ddl
e0.
Stor
ethev
aluei
nther
ota
tionr
egi
st
er.
5
. Pa
uset
oenc
our
agepa
ddl
ere
li
abi
li
ty.
6
. Re
adpus
hbut
ton0
.Ifi
tisnotpr
ess
ed,
goba
ckt
ost
ep4
.
7
. But
tonpus
hed:
Sta
rtt
heFr
ese
que
nce
.
8. Dra
wadotshapes
tar
ti
ngins
idet
hes
hip.⇢er
ota
tionv
alues
eti
nst
ep4
de
ter
minest
hea
ngl
eoft
heshot
.
9
. Ma
kes
omenoi
sewi
tht
hes
impl
enoi
ser
out
ine
.
1
0.Er
aset
hedots
hape
.
1
1.Che
ckt
hec
oll
is
ionc
ount
ert
ose
eifa
nyt
hingwa
shi
t.
12
.Ifnot
hingwa
shi
t,a
dd3t
otheSCALEv
alue
.Ifi
tiss
ti
lll
esst
han#$90,g
o
ba
ckt
oste
p8 .
1
3.I
fthe
rewa
snoi
mpa
ct,r
est
oreSCALE t
o1a
ndt
hee
xpl
osi
onc
ount
ert
o
#$0A.
⇢eng
oba
ckt
ost
ep4
.
1
4.Ifsomethi
ngwashit
,Fndthee
ndoft
her
aybyc
all
ingt
heAppl
eso4HFIND
r
out
ine
. S
avethi
spos
it
ionval
ue.
1
5.DRAWoneoft
hee
xpl
osi
ons
hape
sinwhi
te
.
1
6.Ma
kes
omenoi
se.
1
7.DRAWthesa
meex pl
osi
onshapei
nbl
ackt
oer
asenotonl
ythes
hape
,buta
lso
t
hepa
rtsoft
hewal
lthatwerehi
t.

]
2
50 As
sembl
yLi
nes

1
8.Goba
ckt
ost
ep1
5te
nti
mesf
ora
nex
cit
ing(
?)e
xpl
osi
on.
19.Re stor
eSCALEa ndthee xplos i
onc ounter.Goba cktostep4.
Becauset hispr ogrami sma deupoft hev ar
iousr out
inesusede arl
ier
,t hi
s
summa r
yshoul dbes uWcientt oe xpl
a i
nt heovera
llme t
hodofope ration.
⇢eus eoft hes ingle
-dots ha pet ocreat
ether ayissimil
artot het echnique
usedinc hapter2 4’sScannerpr og rams .⇢ene wt hi
ng sprese
ntedinS hootera r
e
theincreme ntingbyt hree(line s1 3
2 7134)toc r
eateaf ast
erFringa ppearance
andtheus eofHFINDi fani mpa ctisde te
c t
ed.
Reme mbe rt hattheHFIND( $F5CB) routi
neinAppl eso4isuse
da 4erdr awing
anys hapetoFndoutwhe rewe ’vebe enle4.Weus edHFINDinthi spr ogramt o
deter
mi newhe retheimpa ctoc curred.
Alsonot etha tDRAWr at
he rtha nXDRAWi susedinthisprogra
mt oe nsurethat
porti
onsoft hewa llaredestroy edbyt hei mpact.Inc ontra
sttot hee xplosion
program,thispr og ramc annotber unona nyhi-resscreenbackgroundwi thout
chang i
ngthec olor susedbyt her aya ndthee x
plosi
onr outi
nes.
2
6
2
6. Pas
si
ngFl
oat
ing
-Po
intDat
a
No
vemb
er1
982

Inc ha pters1 6a nd1 7wedi scus sedhow Appl e


s o4v ariabl eda tac oul dbe
pa s
se dfrom BAS ICt oa ssembl yla ng uagea ndba cka g ain.⇢er a ti
ona lewa stha t
inma nyc asesapr og r
amc reatedbyc ombi ningAppl eso4a nda ssembl yl ang uag e
isa ne Le ctiv ea ppr oacht oapr obl em.S uc c essivec ha ptersonhi -resg raphi cs
include dt he setec hnique ssoa st oha veac onv eni entwa yofe x perime nt i
ngwi t h
thev a r
iousr outine s.
Itishi ghlyr e comme nde dt haty our evie wt hea ppr opr iatec hapte rsify ou’ re
note ntire l
yf ami liarwi t
ht hena tureofAppl eso4v ari
a bles torag e.Pa ges1 27a nd
13 7int heAp plesoftIIBAS ICPr ogra mmi ngRe ferenceMa nua la l
sopr ov idev e ry
va l
ua bl
ei nf orma ti
onwe llwor thr eferri
ngt oi nt hec our seofr eadingt hema te-
rialpr e
se ntedhe re.
Fort hemos tpa r
t,howe ver,a l
loft hepa stdi scussionswe rel i
mi tedt ode al-
ingwi t
ht wo- bytei ntegerda ta. ⇢a tist os ay ,thepos sibili
tyofde al
ingwi tht rue
Soa ti
ng -poi ntda tawa snotc ons idere d.I nma nyc ases,i ntege rv al
ue sf rom 0t o
65 535or73 2 767t o3 2 767a remor et hana de qua tef orourpur pos es,aswa st he
ca s
ewhe npa ss
ingt oner outinesorXa ndYc oor dinate sforpl ot ti
ng .1Ho we ve r
,
therea ret i
me swhe ng reaterpr ecision, orf ra ctiona lva lue s
,a rer equired.
De ali
ngwi thSoa ti
ng -pointnumbe rsf r om apur ea s
se mbl y-lang uag epr o-
grami saf airl
yc ompl ext opic,a ndouri nt enthe reisnott oe x pla
inc ompl etely
thei nne rwor king sofSoa ti
ng -pointope r ati
ons . Rathe r, l
et’se xploret heopt ions
ma dea va i
lablebyt a
kinga dva ntag eoft hee xistingr outine si nt heAppl eso4
BAS I Cint erpr et
e r.⇢e seg enera l
lyc anbec ons ide redt obea lwa yspr esenti nt he
ba ckgroundofa nope r
atinga ssembl y-lang ua gepr og r
a m.
Fort hos eofy ouwhohopet os pe e
dupSoa t
ing -poi ntope rati
onsi nAppl e-
so4, wr i
tingy ourownr out i
ne sma ynotbet ha te Lec t
iv e.⇢i sisbe caus et her ou-
tinesi nAppl es
o4a rea l
re adywr itt
e ni nma chinel ang uag e.Wec an,howe ver,
ga i
ni mpor tants pe edi mpr oveme ntsj ustbyc a l
lingt her out i
ne sdi rectly.⇢i si s
be c
a usewec ane liminatet henor ma lpr oce ssofi nt e
r pr et
ingBAS ICs tateme nt s
thatot he rwi sewoul doc curi nAppl e s
o4.⇢i si swha tthec ur rentlya v ai
labl e
compi l
ersdo,a ndwec ane xpe cts i
mi l
ars pe edi mpr ov eme ntst oaBAS ICpr o-

1[
CT]Re
cal
lfr
om c
hapt
er1
0tha
tthemi
nimum t
echni
cal
lys
houl
dbe73
276
8.Howe
ver
,
Appl
eso4a
ndI
nte
gerBAS
ICr
est
ri
ctt
hemi
nimum i
nte
gert
o73
276
7.

]
2
52 As
sembl
yLi
nes

g
ram byusi
ngrout
inesdi
rec
tl
yfr
om a
sse
mbl
yla
ngua
ge(
twot
oFv
eti
mesf
ast
er
t
haninst
rai
ghtApple
so4)
.

I
nte
rna
liz
ati
onofDa
ta:
Int
ege
rve
rsusRe
alVa
ria
ble
s
⇢eFr stst
epinourinquir
yistoinve
sti
gat
ehowApple s
o4stor
esnumeri
c
dat
aa ndtolookatthedi
Lerence
sinhowintege
rvar
iabl
esandrea
l v
ari
abl
esar
e
st
ored.
S t
artbyini
ti
al
izingy
ourApple’
sme morywit
hanFPsta
tement.⇢enente
r:
A% = 10: A = 10

⇢eresul
tist
hattwovar
iabl
esa
ndt hei
rvalue
shav
ebeense
tupi
nme
mor
y.
NowtoFndthem!
Ent
ert
heMoni t
orwit
htheusualCALL-151.⇢enent
er:
67 68 AF B0

Yous
houl
dge
t:
0067- 01
0068- 08
00AF- 03
00B0- 08

Youma yre
callf
rom chapter16thatthesefourmemorylocatons(
i $67,$68
and$AF,$B0)areusedtostorethebeginningandtheendofthec urr
entAppl e
-
so4progr
am. Wec anseefr
om thedi s
playthatt
heprogramresi
desfrom $801to
$803.Averyshortprogra
m,i ndeed,butthat’
sunders
tanda
blesinceweha ven’
t
enter
edanyprogramlines
.
Nowlet
’sexaminethepointerat$69,$6Aand$6B,$6C.
Dot hisbytypi
ng
69.6C

a
ndpr
ess
ingr
etur
n.Yous
houl
dge
t:
0069- 03 08 11 08

⇢i
ste
ll
sust
hata
lls
impl
e(t
hati
s,non-
arr
ay)v
ari
abl
esa
res
tor
edf
rom
$803t 2E
o$810. xa
minet
hisa
reabye
nte
ring
:
803.810

Yous
houl
dge
t:
0803- C1 80 00 0A 00
0808- 00 00 41 00 84 20 00 00
0810- 00

2[
CT]Onel
esst
han$811.
2
6.Pa
ssi
ngFl
oat
ing
-Poi
ntDa
ta 2
53

You’
llr
ecal
lfr
om ourdi
scus
si
onsinpr
e v
iousc
hapter
stha
tint
ege
randrea
l
v
ari
able
sarest
oredint
hefol
lowi
ngfor
mat:
I
nte
g e
r: C1 80 00 0A 00 00 00
“A” “” “0
” “10” – – –
Name Name Hi
gh Low Unus
ed Unus
ed Unus
ed
char
1 char
2 By
te Byt
e
(
bit7se
t)(
bit7se
t)

Re
al: 41 00 84 20 00 00 00

A” “” 1000 0010 0000 0000 0000
0100 0000 0000 0000 0000
Name Name Expone
ntManti
ssa Ma
nti
ssa Ma
nti
ssa Ma
ntis
sa
cha
r1 cha
r2 m.s
.b. l
.
s.b.
(
bit7cl
ear
)(bi
t7cl
ear
)

Startinga t$803,weFndt hev ariableA%s tore df rom $803t o$809. ⇢eFr s


t
twoby te sa r et hena mec ha racter s
.Twoby tesa r ea lwa ysus e d.I ft hev ar i
able
namei sonl yonec ha racte rt he nanul l( $00f orr e alor$80f ori nte ger )iss tored
inthes econdpos ition. Not et ha tint eger,rea l,a nds t
ringv ariablena me sa redi f
-
fer
e ntiatedb yt hec ombi na tionofhi ghbi ts etting sint het wona me -c hara ct
er
bytes .Sinc eonl ybi ts0t hr oug h6a reus edf ort hec ha racter( AS CI Iisonl yas ev-
en-bi tcode ) ,bi t7( thehi gh- or derbi t)isa vaila blef ore ncodi ngt hev aria blet ype.
Intege rv ariable sa l
wa ysha vebot hhi g hbi tss et.Re alv a r
iabl esa lwa ysha ve
bothhi ghbi t sc lear.S tringv a ri
a blesa lwa ysha v et heFr stna mec har ac t
e rc l
ear
andt hes ec ondc ha racters e t
.( ⇢enot ationf ors t
ringv ariabl ena me st ot he
oppos i
tee Le ctonpa ge1 37oft heAp p le
so ftI IBAS ICPr ogra mmi ngRe ference
Ma nua lisi ne r rori nt hisr eg ard.)
⇢ene xtt woby tes
, $00a nd$0A,a ret hehi gh-a ndl ow- or derby te sfort he
value1 0.Youha vepr oba bl ynot icedt ha tint e gerv ari
abl esa res tore di nav ery
si
mpl ewa y ,wi tht hev a luebe i
ngbr oke ndowni ntot hel ow-a ndhi gh- order
bytes .Aboutt heonl ype c uliari temi sthef ac tt hatt het woby t
esa res t or edhi gh-
orde rby teFr st, whi chi sba ck wa rdf rom t hewa ywenor ma llyse ethe m pa iredi n
mos ta ssembl y-lang ua gec ode .
⇢et hr e er ema iningby te sa reunus ed.
$80At o$810i swhe r et her e alv ari
a bleAi ss tor e
d. Youc ans eet ha ttheFr s
t
twoby tesa ga ina ret hena mec ha racters,thi st imewi tht hehi g hbi tsc lear.⇢e
rema i
ningby te sma k eupt hev aluef ort hev a riableA.
Itshoul dbeobv ioust ha ta lthoug ht hev a luesoft hei nt ege ra ndr ealv ari
-
ablesa res tor eda se qua l,t hema nne rinwhi cht he ya res toredi snot .⇢er ea
l
variableha sbe ene nc ode di nt oaFv e-by tes eque nc e,thel og icbe hi ndwhi chis
notr eadilya ppa r
e nt.We ll,don’ tde spa ir
;i ti snota ctua l
lyne c e
ss aryf orust o
unde r s
tandt hee xactde ta il
soft hec onv ersi
onr out ine.

]
2
54 As
sembl
yLi
nes

Ing eneral,i
twi lls uWc et os a yt hata ne xpone ntialnot at
ioni sus edt ostore
3
8
thenumbe r.⇢i si shownumbe rsofs uc hlargema gni t
ude s(±1 0 )a reaccom-
moda tedbyAppl e s
o4.I fy our ous es omeofy ourmor ea nc ienthi gh-school
me mor i
es,you’ l
lre callthatt heba si
ci de atoe xpone ntsist hata nynumbe rc an
bee xpr ess
edwi t
ht wonumbe rs ,thee x pone ntandt hema nt i
ssa.
Fore xampl e,thenumbe r1 0i se qua lto1 01,⇢enumbe r1 0 0ise qualto1 02.
Itisr easonablet oa ssume ,t he n,t hatanumbe rlike5 0mi ghtj ustha ppent obe
1.
5
equa lto1 0 .Asi tha ppe ns ,t ha t’
snotqui teright,butt heba sici deai sthere.In
fact,5 0isre all
ye qua lt o1 01.69897 (ort hereabout s)
.⇢e1pa r
toft henumbe ris
call
e dthee xp onent( oroc ca siona ll
yt heor derofma gnitude )oft henumbe r
. ⇢e
69 897i scal
le dthema ntis
sa. Youma yha vefondme mor i
esofs pendi ngpleasant
hour si nma thc l
asse slook ingt hroug hbook swi thl ookupt a blest oFndt hese
value sforgivennumbe rs.
Ina nye vent,it’
spr eci
s elyt hist ypeoft echniquet hati sus edt oe ncodet he
value sofr e
a lvari
a bles.3For tuna telyf orus ,itwi l
lnotbene cessaryt oc rea
teour
ownr outinest oha ndlenumbe rsi nthi sforma t;awe althofs uchr outinesalr
e ady
existinAppl eso4.
⇢er ema inderoft hi
sc ha pterwi llconc entrateons omebr ie
fe xercis
esi n
pa s
singSoa t
ing -
poi ntnumbe r sba c ka ndf orthbe twe enAppl e so4a nda ss
embl y
langua ge.⇢e ni nupc omi ngc ha pterswe ’l
le xplorehow t ope r
for mv arious
ma the ma t
icalope rationsonc ey oura ssembl y-l
ang uag epr ogram ha spos s
ession
oftheda ta.

⇢ eFl
oat
ing
-Poi
ntAc
cumul
ator(
FAC)
Apples
o4ha sitsowni nt er
na lsetofregistersthatitusesdur i
ngi t
sv arious
calc
ulat
ions.⇢emos timpor tantoft hesebyf aristheSoa ti
ng-pointAc cumul a-
tor.⇢isisusual
lylabele
dFACi ns ourceli
sti
ng sthata c
cessthi
sr egi
st
e r.
⇢ewor dregi
sterisus edi nas li
ghtl
ydi Le rentwa yheret hani tiswhe n
refe
rri
ngt o6 5
02r egis
terss ucha st heAc cumul atorort heX-orY- Reg i
sters
.
BecauseaSoa ti
ng-pointnumbe ri srepresentedbyas eri
esofby tes,theFAC
occupie
stheby t
esfrom $9Dt hroug h$A2.
Youma ybepuz zl
eda stowhyt heFACus ess i
xby teswhenv ari
a blestorage
usesonlyFve.⇢i si sbecaus etheFAC us es$A2a sthesignby tetoindi c
a t
et he
posit
iveorne gati
ves tat
usoft hev a
lue.Whe n Fna l
lye ncoded,t hes igni s
incl
udedi nthee xponenta ndma ntiss
aby tesa ndt husi snol ong erne eded.
Float
ingpointnumbe r
sint heFv e -
by t
eforma tares ai
dt obe“ packed.”⇢es i
x-
3[
CT]Fr
om t
heAp
pleI
ITe
chni
calNo
te:
sfort
hee
xpone
nt,
thet
opbi
tist
hes
ign(
wit
h0
f
ornegat
ive)
.⇢er emainderoftheby t
emi nusoneisthev a
lueofthee x
ponent(f
or
e
xample,$84isaposit
iveexponentof3 ).⇢ema nti
ssaisabinar
yf ra
cti
on,withan
i
mpli
edstart
ingval
ueof1.⇢eFr stbi
tisthesi
gnbit(t
histi
mewi t
h0f orposi
ti
ve)
.⇢e
r
emaini
ngbitsaref
rac
tiona
lvalue
ss t
art
ingwith0.5
,0.25,0.
125,e
tc.Forexample
,$20
g
ive
sama nti
ssaof1+0.25.
So$84$20e qual
s1.
25d2 i=1 0.
2
6.Pa
ssi
ngFl
oat
ing
-Poi
ntDa
ta 2
55

byteformatis“unpack
ed.”⇢eunpa c
kedf ormatisfas
terforca
lculat
ions.⇢e
packedformatisusedt
omi ni
mi zestor
agespa ce
.
Ingener
al,wheneve
ranyt ypeofcalculat
ionisdonebyAppleso4t heFACis
theprimaryregi
ste
rusedtoholdt heres
ult.As e
condregi
ster,
ARG(ARGume nt)
,
isusedfortwo- va
luecal
cula
tions,sucha s1 5d1
. 7.⇢eARGr egi
sterusesthe
bytes$A5through$AA.Forthet i
mebe ing,though,wene e
donlyc oncernour-
sel
veswithFAC.

Pa
ssi
ngDa
taf
rom Appl
eso5t
otheFAC
⇢eFr stareatoi
nv e
stigat
ei show tog etaSoa t
ing-
pointnumberpa s
sed
fr
om Appleso4toa na
ssembl y-
languagerouti
ne ⇢ee
. as
ies
twa yisbymeansof
theUSR functon.⇢eUSR i
i sar atherneglect
edpa rtofAppl e
so4,pr
obably
beca
useofthel a
ckofdocume ntati
ononi tsnatureandappli
cat
ions.Apr
og r
am
st
ate
me ntusingUSRmightlooksome thi
ngl i
kethis:
10 X = USR(Y)

Whent hi
sst
ate
menti
sexecut
ed,t
hreet
hing
shappen:
1. ⇢ee xpre
ssi
onorva
ria
blewit
hinthepa
rent
hes
esisev
alua
teda
ndt
her
esul
t
puti
ntheFAC.
2. Ac al
ltol
ocat
ion$0A(decima l
1 0
)isdone.⇢i sisequival
entt
oaCALL10i n
Apple
so4.⇢e rei
sat hre
e-byt
ej umpi ns
truct
iona tloca
tion$0A.Itisass
ume d
tha
ttheuserhasi
nsert
edthelocati
onofa nexist
inga ss
embly-l
anguageroutine
.
Forexa
mple,thec
odeJMP$300mi ghtbefoundat$0A. ⇢epr ogram wouldthen
j
umpt o$300toexec
utewha t
everrouti
netheusermi ghthaveputthere.
3
. Whe nt heus
errouti
neev
ent
ual
lydoe sanRTS,t
hec
ont
ent
soft
heFACa
re
a
ssi
gnedtotheva
ria
bletot
hel
e4oft
hee quals
ign.
Fore
x a
mple,t
ypeinandr
unthi
sprogram:
10 POKE 10,0
20 Y = 10
30 X = USR(Y)

Whe
nrun,
thepr
ogr
ams
houl
dfa
lli
ntot
heMoni
tor
.⇢e
nty
pei
n:
9D.A2 (return)

Yous
houl
dge
t:
009D- 84 A0 00
00A0- 00 00 20

]
2
56 As
sembl
yLi
nes

⇢i sist heda t
af ort hev al
ue1 0
,whi chtheFACstor e
sinunpa ckedf orm.4

He re
’swha tha ppened: Line10s e
tlocati
on$0At oaBRK. Whe ntheUSRfunc ti
on
wa scal
led,itputthes eque ncefor10int heFACandthenc all
ed$0Aa sexpe ct
ed.
Sincethiswa sabr e
ak ,wewe ntint
ot heMoni t
orandc ouldthenimme di at
ely
examinet heFAC.
Notet hatitisnotpos si
bletosettheFACfrom Apple s
o4a ndthent ov er
ify
thestat
usoft heFACbye nteri
ngtheMoni t
orwit
htheus ualCALL-151. S
inc ethe
FACwi l
lbeus edinc alculati
ngthev al
ueof-151,anypr iordatawouldbeov er-
wr i
tt
en.
Whi leyou’reintheMoni tor,l
et’
ssetup$0Aforourne xtexperi
me nt.Enter:
0A: 4C 00 03

⇢i
swil
lsett
hev
ect
ortopointatloc
ati
on$300.Nowc
rea
teat
ri
vi
alpr
o-
g
ram(i
nthi
sca
se,
ani
mme di
ateRTS)a
t$300byent
ering
:
300: 60

Nowr
etur
ntoAppl
eso4a
nde
nte
randr
unt
hispr
ogr
am:
10 Y = 10
20 X = USR(Y)
30 PRINT X

Yous houldg e
tt henumbe r1 0pr intedout.Ifyouc onsiderwha twe ’vedis-
cusse
ds ofar,itshouldbea ppa rentwhy .⇢ev al
ue1 0he ldbyYwa spa ssedtot he
FAC b yt heUSR f unc ti
on.Whe n our“ r
outi
ne”a t$300 wa sc all
ed,t heFAC
remainedunc ha nged.Uponr eturnf rom ourr outi
ne ,theFAC( s
til
le qualto1 0)
wasa s
si
g nedt othev ariabl
eX.
Alt
houg ht heUSRf unc ti
oni sac onv enie
ntwa yofpa ssingda ta,itisrather
li
mitedint ermsofs y ntax.Ifyouwa nt edtopa s
sanumbe rofpa rame ter
st oa
routi
ne,a nothert echniquewoul dber equi
red.Youma yre c
allfrom pr evi
ous
chapter
sar out i
nec alledFRMNUM( $DD67=F oRMul aNUMe ricevaluator)thatwe
usedtoev al
ua t
ev ari
a blesbe i
ngpa ssedt oasse
mbl y-l
ang uager outi
ne s.A4erc all
-
ingFRMNUM, GETADR( $E752=GE TAdDRe ss
)wa susedt oc onv er
tthenumbe rt o
atwo-byteintege rLINNUM( $51,$52=L INeNUMbe r).
We l
l,si
nc ewha twewa ntistheFAC, we’
vea l
readyg otthes ol
ution:
1 ********************************
2 * *
3 * AL26-BASIC TO FAC *
4 * *
5 * SYNTAX: CALL 768,Y *
6 ********************************
7 *
8 * OBJ $300
9 ORG $300

4[
CT]Cor
rec
tedf
rom t
heor
igi
nal
art
ic
le,
whi
chpr
ese
nte
dther
esul
tinpa
cke
dfor
m.
2
6.Pa
ssi
ngFl
oat
ing
-Poi
ntDa
ta 2
57

10 *
11 CHKCOM EQU $DEBE
12 FRMNUM EQU $DD67
13 *
0300: 20 BE DE 14 ENTRY JSR CHKCOM
0303: 20 67 DD 15 JSR FRMNUM
0306: 00 16 BRK

⇢iscodes
houl
dbea
sse
mbl
eda
t$300a
ndc
all
edwi
tht
hef
oll
owi
ngAppl
e-
s
o4pr
ogr
am:
10 Y = 10
20 CALL 768,Y

Whe
nthi
spr
ogr
ami
srun,
yous
houl
dfa
lli
ntot
heMoni
tor
.⇢e
nent
er:
9D.A2

Yous
houl
dge
t:
009D- 84 A0 00
00A0- 00 00 20

⇢isshouldv e
rifyt hattheFACwa sproperlyloadedwiththev al
ue1 05
.
I
nreviewingthel i
s ti
ng, you’llseethatli
ne1 4call
sCHKCOM( $DEBE=CHe cK
forCOMma )t oadv anc eAppl eso4’sTXTPTR( $B8,$B9=Te XTPoi nTeR)past
thecommaf oll
owingt he7 68 .Li ne1 5thenc a
llsFRMNUM,whic heval
uatest
he
vari
abl
eore xpress
ionf ollowingt hec ommaa ndput stheresul
ti ntheFAC.Li
ne
16thendoe stheBRKt ol ea
v eusi ntheMoni tor,fr
om whi chwec ancheckt
he
FACtoveri
fythatthec or r
ectv alueha sbeens t
or e
d.
Weha venow,t he n,twot echniquesforpa s
singda t
afrom Appl es
o4t ot
he
FAC.⇢eFr stistous et heUSRf unc t
ion(beings ure,ofcourse
,tos etuptheve
c-
torat$0A).⇢es econdi stous eFRMNUM( $DD67)t oe val
uatethee xpr
essi
onor
vari
abl
easpa rtofapa rame terlistfoll
owingaCALLs tate
me nt.

Mov
ingt
heFACt
oaMe
mor
yLoc
ati
on
Si
nc etheFACi ss
ohea v
il
yused,i
tiss
ometi
me shel
pfultomovet
hedatai
n
i
ttoanotherlocati
onforlat
eruse.I
nApples
o4,thisi
smos to4e
nat e
mporar
y
r
egi
sterora na ct
ualvar
iabl
e.Fornow,le
t’
sseei
fwec anmov ethedatat
oan
a
rbit
raryloca
tion.
1 ********************************
2 * *
3 * AL26-FAC TO MEMORY *
4 * *
5 * SYNTAX: CALL 768,Y *
6 ********************************

5[
CT]Ag
ain,
theFACs
tor
est
hev
aluei
nunpa
cke
dfor
m.

]
2
58 As
sembl
yLi
nes

7 *
8 * OBJ $300
9 ORG $300
10 *
11 CHKCOM EQU $DEBE
12 FRMNUM EQU $DD67
13 MOVMF EQU $EB2B
14 *
0300: 20 BE DE 15 ENTRY JSR CHKCOM
0303: 20 67 DD 16 JSR FRMNUM ; BASIC->FAC
0306: A0 03 17 LDY #$03 ; HI BYTE
0308: A2 80 18 LDX #$80 ; LO BYTE
030A: 20 2B EB 19 JSR MOVMF ; FAC->MEMORY
030D: 60 20 DONE RTS
21 *

⇢ek eyt othist


echniqueisarouti
nei nAppleso4c al
ledMOVMF($EB2B=
MOVet oMe moryfr
om FAC),whi
chtakesthev al
ueinFACa ndmov esittothe
loc
ationpointedt
obyt heX-andY-Regis
ter
s( X,Y=l owby t
e,hig
hby t
e).
⇢el is
tinggivenhe r
eus e
sourpr evi
ousFRMNUM techni
quet ogetapr e-
dict
ablenumbe ri
ntotheFAC.⇢eX-a ndY- Regis
ter
sa r
ethenloadedtopointto
$380.Whe nMOVMFiscall
ed,t
hecont
entsoftheFACwi l
lbedeposit
edthere.
Toseet hi
s,runthesameAppleso4prog ra
m,t henentertheMoni t
orand
enter
:
380.384

Yous
houl
dge
t:
380- 84 20 00 00 00

⇢i
sprove
sthatweha
ves
ucc
ess
ful
lymov
edt
heda
taf
rom FACt
oana
rbi
-
t
rar
ypl
acei
nme mory
.6

Mov
ingMe
mor
yint
otheFAC
⇢ec onve
rseofthi
sope rat
ioni saccompl i
she
dinmuc ht hesamewa y.In
thi
scase
,theAppl e
so4routi
neMOVFM( $EAF9=MOVet oFACf rom Memory)is
use
d.Itrequi
resthatt
heY- Regi
stera ndAc cumulat
orbeloadedwi t
hthehigh-
andlow-orde
rby te
softhea ddres
st obeus e dasthedat
as ourcefortheFAC.
(Notet
hatther
eisadiLere
nc ehere
: MOVMFu s
e sXandY;MOVFMu se
sXa ndA!)
1 ********************************
2 * *
3 * AL26-MEMORY TO FAC *
4 * *
5 * SYNTAX: CALL 768 *
6 ********************************

CT]MOVMFFr
6[ stc
onv
ert
sfr
om unpa
ckedFACfor
mt opack
edform,the
nmov e
sthe
da
ta.
Simi
la
rly
,MOVFMc
onv
ert
sfr
om pack
edfor
m backt
otheunpa
ckedFACfor
m.
2
6.Pa
ssi
ngFl
oat
ing
-Poi
ntDa
ta 2
59

7 *
8 * OBJ $300
9 ORG $300
10 *
11 MOVFM EQU $EAF9
12 *
0300: A0 03 13 ENTRY LDY #$03 ; HI BYTE
0302: A9 80 14 LDA #$80 ; LO BYTE
0304: 20 F9 EA 15 JSR MOVFM ; MEMORY->FAC
0307: 00 16 BRK

Assumingt ha
tthepr ev
iousr
outi
neha sal
rea
dybeenexecut
edandt hat$380
i
sloadedwiththedataappropri
atet
ot heval
ue10,t
ypeinCALL768.
Yous houl
de ndupi ntheMonitor,a
twhichpointyoucanv e
rif
yt hecon-
t
entsoftheFACbye nt
ering:
9D.A2

Yous
houl
dge
t:
9D- 84 A0 00
A0- 00 00 20

Agai
n,theBRKwa susedtoe ndtherout
inesotha
twecoul
dimmedi
ate
ly
ex
aminethecont
entsoftheFAC.⇢i sr
out
ineshowst
hatwec
anmoveda
taf
rom
asec
ti
onofme morybackint
ot heFAC.

Pa
ssi
ngFACDa
taBa
ckt
oAppl
eso5
IftheFACdoe scontai
ntheres
ul tofanope r
ati
on,howc anwepa s
sitbackto
ac al
lingAppleso4pr ogram,pref
erablyintothev a
riabl
eofourc hoi
ce?Ag ai
n,
theans weristous eMOVMF.I
nt hi
sc ase,r
atherthanmov i
ngt hecont
entsofthe
FACint oanarbit
ra r
yme moryloca
tion, we’
l
l Fndtheloca
tionofthedat
aby t
esof
ag i
venr ea
lv a
riableandthenmov et heFACintothem. ⇢isha st
heeLectofset
-
tingt
hev ari
ablee qualt
otheconte
nt softheFAC.
Cons i
derthislis
ti
ng:
1 ********************************
2 * *
3 * AL26-FAC TO BASIC *
4 * *
5 * SYNTAX: CALL 768,Y *
6 ********************************
7 *
8 * OBJ $300
9 ORG $300
10 *
11 CHKCOM EQU $DEBE
12 PTRGET EQU $DFE3
13 MOVMF EQU $EB2B
14 MOVFM EQU $EAF9

]
2
60 As
sembl
yLi
nes

15 *
0300: A0 03 16 ENTRY LDY #$03 ; HI BYTE
0302: A9 80 17 LDA #$80 ; LO BYTE
0304: 20 F9 EA 18 JSR MOVFM ; MEMORY->FAC
19 *
0307: 20 BE DE 20 JSR CHKCOM
030A: 20 E3 DF 21 JSR PTRGET
030D: AA 22 TAX
030E: 20 2B EB 23 JSR MOVMF ; FAC->VARIABLE
0311: 60 24 DONE RTS

⇢i sr out inea g a
ina ss
ume stha ttheSoa ting -poi
ntda tafort henumbe r1 0
sti
lle xistsa t$380.Whe nthisrout i
nei srun,l ine s16t hroug h1 8dupl ic
a t
et he
prev i
ousl i
stingt omov etheSoa ting-pointda tafr om $380t hr ough$384i ntot he
FAC.
Line2 0us esCHKCOMt oc heckthec ommaa ndmov eTXTPTRt otheFr stcha r
-
acterpa stthec omma .Line2 1us esthePTRGET( $DFE3=P oinTe RGETr outine)
tol ocatet hev a r
iablec urr
e ntl
ypoi nt edtobyTXTPTR.PTRGETi shandya l
soi n
thati twi llc reatet hev ari
ableint hev ari
ablet ableifitdoe snota l
readye xist
.
PTRGETr et
ur nswi t
ht heY- Re gi
stera ndAc cumul atorpoint i
ngt ot heda t
aby tes
ofthes pe c
iFe dv a r
iable.⇢i swillbepr e
cisel
ywhe rewewa ntt heda t
aint heFAC
tobemov edt o. ⇢eonl ycorrecti
ont obema dei si nregardt ot hefactthatMOVMF
requi r
e stha ttheY-a ndX- Re g
isters( r
athertha nYa ndt heAc cumul at
ora swa s
le4byPTRGET)hol dt hedestinati
ona ddress.Line2 2solvesthi sbyus i
ngt heTAX
comma nd,a twhi chpoi ntMOVMF i sc all
ed.We ’renow done ,a ndt heRTSwi l
l
returnt ot hec all
ingpr ogram.
Tes tthisr outinewi ththefollowi nglis
ti
ng :
10 CALL 768,X
20 PRINT X

Xg
etss
ett
o10byha
vingourr
out
inet
rans
fert
heSoa
ting
-poi
ntda
taf
rom
$380t
hroug
h$384t
otheda
taby
tesf
ort
hev
ari
abl
eX.

Put
ti
ngi
tAl
lTog
ethe
r
Forarea
ltestofthe
secombinedt
echniques,l
et
’sseei
fwecansucc
ess
ful
ly
pas
sdataf
rom Apples
o4totheFACtoame mor ybl
ockandthenbac
ktotheFAC
andbacktoAppl e
so4.⇢ef oll
owingroutineshoulddemonst
rat
etheenti
re
ope
rat
ionasanoveral
lexampl
eofthei
deaspr e
sent
edthusf
ar.
1 ********************************
2 * *
3 * AL26-BASIC.FAC.MEM.FAC.BAS *
4 * *
5 * SYNTAX: CALL 768,Y,X *
6 ********************************
7 *
8 * OBJ $300
2
6.Pa
ssi
ngFl
oat
ing
-Poi
ntDa
ta 2
61

9 ORG $300
10 *
11 CHKCOM EQU $DEBE
12 PTRGET EQU $DFE3
13 FRMNUM EQU $DD67
14 MOVFM EQU $EAF9
15 MOVMF EQU $EB2B
16 *
0300: 20 BE DE 17 ENTRY JSR CHKCOM
0303: 20 67 DD 18 JSR FRMNUM ; FP->FAC
19 *-------------------------------------
0306: A0 03 20 LDY #$03 ; HI BYTE
0308: A2 80 21 LDX #$80 ; LO BYTE
030A: 20 2B EB 22 JSR MOVMF ; FAC->MEMORY
23 *-------------------------------------
030D: A0 03 24 LDY #$03
030F: A9 80 25 LDA #$80
0311: 20 F9 EA 26 JSR MOVFM ; MEMORY->FAC
27 *-------------------------------------
0314: 20 BE DE 28 JSR CHKCOM
0317: 20 E3 DF 29 JSR PTRGET
031A: AA 30 TAX ; MOVE LO BYTE->X
031B: 20 2B EB 31 JSR MOVMF ; FAC->FP
32 *-------------------------------------
031E: 60 33 DONE RTS

Tr
ythi
sAppl
eso4pr
ogr
amt
oca
llt
her
out
ine
:
10 Y = 10
20 CALL 768,Y,X
30 PRINT X

⇢ev al
ue10s houl
dbepr inte
df orX.Da s
hedli
nesha
vebeenus e
dtosepa-
r
atethef
ourmajorsect
ionsoftherouti
ne.Whe nyoucompar
ee a
chs e
cti
onwith
t
hefourrout
inespresente
d,thene topera
tionoftheexampl
es houl
dbe c
ome
c
lear
.
⇢ eUSRrouti
nea l
socouldhav ebeenusedandwouldeli
minatetwoofthe
s
ecti
ons:
1 ********************************
2 * *
3 * AL26-BASIC.FAC.MEM.FAC.BAS *
4 * VIA THE 'USR' *
5 * *
6 * SYNTAX: X = USR(Y) *
7 ********************************
8 *
9 * OBJ $300
10 ORG $300
11 *
12 CHKCOM EQU $DEBE
13 PTRGET EQU $DFE3
14 FRMNUM EQU $DD67
15 MOVFM EQU $EAF9

]
2
62 As
sembl
yLi
nes

16 MOVMF EQU $EB2B


17 *
0300: A0 03 18 ENTRY LDY #$03 ; HI BYTE
0302: A2 80 19 LDX #$80 ; LO BYTE
0304: 20 2B EB 20 JSR MOVMF ; FAC->MEMORY
21 *-------------------------------------
0307: A0 03 22 LDY #$03
0309: A9 80 23 LDA #$80
030B: 20 F9 EA 24 JSR MOVFM ; MEMORY->FAC
25 *-------------------------------------
030E: 60 26 DONE RTS

Noti
cethatsincetheUSR f unct
ionc al
lstheroutinewi t
ht heFAC al
rea
dy
loa
ded withthev al
uef orY,t heFr stsect
ionoft hepr evi
ousr outi
neisnot
needed.Al
so,si
ncetheUSRf unctionwillaut
oma t
ic
allyassi
gnthec ont
entsoft
he
FACtothevari
abl
eX, t
helastsectionoftheprev
iousroutineisnotneeded.
⇢ec al
li
ngprogramfort heroutinewouldlooklikethi
s:
10 POKE 11,0: POKE 12,3: REM SET UP USR VECTOR
20 Y = 10
30 X = USR(Y)
40 PRINT X: REM SHOULD PRINT '10'

Conc
lus
ion
Bynowy ouprobablyfe
elfa
irl
yc omfort
abl
ewiththei deaoftheSoat
ing-
poi
ntAc c
umul a
tor(FAC)andhowda tacanbemov e
da boutbe t
weenAppleso4
andass
emblyl a
nguage.Inthenextchapt
e rwe’
l
lbeginlook i
ngatsomeoft he
moresophis
ti
catedrouti
nesApple
so4us e
st ope
rfor
mv ariousarit
hmeti
cfunc-
ti
ons
.
2
7
2
7.Fl
oat
ing
-Po
intMat
hRo
uti
nes
De
cemb
er1
982

Int hisc hapter,we’l


lc onti
nuewi thourdi scussi
onofSoa ting-pointnumbe r
ope ra t
ions.I nt hepr evi
ousc ha pterwel ookeda thowAppl eso4us estheSoa t-
ing -pointAc cumul at
or( FAC)a sthema inregi
s t
erformos tofitsnume ricope ra-
tions .Rout ineswe repr esentedt hatde monstrat
e dhowda tac anbepa ssedba c
k
andf orthbe twe enar unningAppl es
o4pr ograma ndana sse
mbl y-langua gesub-
rout inea nda l
sohownume ri
cda tac a
nbemov edina ndoutofbl ockme mory
stor age.
Us ingt hisfounda t
ion, wec annowe xami nehowt ous eAppl eso4’sr outines
fors uc hba sicma t
hf uncti
onsa sa ddit
ion,subtracti
on,mul t
iplica
tion,a nddi vi-
sion.
Awor dofa dviceisinor der ,howe ver
,beforepr oc
eeding.YourFr s
ti nclina-
tionma ybet ot hi
nkt hatt her outinesg i
venhe rewi l
lenabley out odos imple
ma thope rationswi thgreaterspe edi nanAppl eso4pr ogra
m. Asi tha ppe ns,this
wi llnotdi re ct
lypr oducet hespe edi ncreas
ey ouwa nt
.Re membe r,Appl eso4i s
alrea dyus ingt hesev er
ys amer outines;giv
ent ha t
,nos pe
e dincreases houl dbe
ex pec t
edf ors uchsimpl eope r
ationsa sX=5d1 0.
Ourne ws yntaxwillbe :
CALL 768,5,10,X

anda ss uchi nvolve


sj usta bouta smuc hov er
headi nt hec a l
li
ngoft her out i
ne
andt hepa ssi
ngofda taa swoul dbei nv ol
vedinAppl eso4.
Youma ya skthe n,“ Whyus ea nassembly-l
ang uag ec allfort he s
eope r
a-
tions?”
⇢e reareanumbe rofr e
asons ,twooft hemorei mpor tantofwhi chf ollow.
First,whe nde al
ingwi thpr ogramst hatrequir
eahi ghde g
r e
eofa ccuracy,
intege rda t
ama ynotbes uWc i
ent .
Suppos e,fore x
ampl e,youha veapr ogramthats imul atesthemot i
onofa n
obj e
c ttraveli
ngi nane lli
ptical(orothe rma t
hema t
icall
yc ompl ex)pa t
h.
Ifthec urrentpositionoft heobj ectisconti
nuallyma intai
ne dbyus ingi nt
e-
ge rcoor dinatesinther a ngeoft henor ma lscr
eenc oordi nates,errorswi llbe g
in
toc reepi nwi thsuccessiverecalculati
onsofpos i
ti
on. ⇢i smi ghtbee vidence dby
theFg uref ai
lingtor etracei t
self.Althoughi ntheoryt heobj ectshoul da lways
returnt oi tsstart
ingpoi ntwhe nf ol
lowinga nell
ipticalpa th,mul tipleroundi ng

]
2
64 As
sembl
yLi
nes

errorsma yc ausetheobj ectt


o“ miss”itsori
gina
lsta
rti
ngpointbyaf e
ws cr
een
units.
A mor er el
iablea pproac
hwoul dbet oma i
ntai
nt hecurr
e ntposi
ti
onina
trueSoa t
ing-pointforma tandr oundthenumbe rtothene a
restinte
gerpri
orto
eachpl ot.
S e
c ond,c alcula
tionsde ali
ngwi thal argenumbe rofv aria
blesor,more
speciFca l
ly,wi t
ha rraysofr ealvari
abl
e s,wil
lbeexecute
df as
terbya na s
sem-
bly-l
ang ua geroutine.
If,fore xampl e,
youwa nt
e dtomultiplyanenti
rearra
yby5 , i
twouldinfac
t
bef a
stertoe mployar outinethatuse
dt hissy
ntax:
CALL 768, A(0), 5

t
hant
ous
e:
FOR I = 1 TO 100: A(I) = A(I) * 5: NEXT I

⇢e sei dea sa ndot her sa reputt ous ei nanumbe rofc omme rc i


a lso4wa re
pr oduc tsf ort heAppl e.Ma nypr og ramst hatr equi r
ebot hs pe eda ndahi gh
de greeofa c curac yus eSoa ting -pointr epre se
nta t
ionsofnumbe rsi na ssem-
bly -
lang ua ger out ine s.Anot herc ommont e chniquei st ous et heBCD( “Bina r
y
Code dDe cima l
”)f orma tfort heda ta.
⇢e rea rea lsopr og r
a mmi ngut ili
tiest hatpr ov i
dema chi ne -l
ang uag erou-
tinest o be c alled di rectlyf rom Appl eso4. Ro utineMa c
hi ne ,Amp e
rMa g
ic,
Amp e
r wa re,Ap p l
eS pice,a ndTheLi nk er(publ is
he dbyS out hwe sternDa taS ys
-
tems , Ant hro- Dig it
a lS o4wa re ,Sc i
e ntiFcS o4wa r
ePr oduc ts,Adv ent ureI nt e
rna-
tiona l
,a ndMi cr oLa b,r espec ti
v ely)a rea llde si
g nedt oa ll
ow t hepr og ramme r
ac cesst ous e f
ulr out ine swr ittene ntirelyinma c
hi nel ang uag e.Onepr oduc tin
pa rticular,Amp ersoftPr og ram Li b raryVo l.1( aRo utineMa chine -relatedpa c
k-
ag e),de alsalmos te ntirelywi tha rray-relatedr outine stha tus et hea dv a ntagesof
thes ec ondpr i
nc ipleme ntione dt os pee dupa rray-relat
e dpr og rams .
Ing ener a
l,a l
loft he sepr oduc tsa reba sedonma kingus eoft hea mpe rsand
ve ct
ort oc allspe ciali
ze dr outine s,thewa ywe ’vedi scussedi npa stc ha pters.And
reg ardlessofwhi chpa c kagey oupr efer,it’
ss afetos ayt ha ttheov era l
li deaofa
us er-selectablel ibra ryofpr e wr it
ten ma c
hi ne-l
a ngua ger outine se asilyc all
ed
from BAS ICi soneoft hemos tpowe rfula nde x citi
ngi de ast oc omea longi n
Appl eso4pr og r
a mmi ngi nt hel a stfewy ears. Infa c
t, virtua ll
ya l
loft her out i
nes
pr esent e
dov ert hel asty eara rec ompa ti
blewi thma nyoft he sea mpe rsa ndut i
lit
y
pa ckag es.
Asama tterofr e f
e re
nc e,i ta l
s os houldbenot edt ha tt echni que sa reavail
-
abl ef orf asternume ricope rationswi t
houtha vi
ngt oc a llAppl eso4r outines.
⇢e ser angef rom a rithme ti
cpr oce ssorboa rds,suc ha st hos ema nuf acturedby
Ca li
for niaComput erS yst
e msa ndot he rs,t
os o4wa res ubs y stemss uc ha sSpe e
d/
AS M( publishe dbyS ierraOn- Li ne).Ac ombi nationofbot hha rdwa rea nds o4-
2
7.Fl
oat
ing
-Poi
ntMa
thRout
ine
s 2
65

wareisals
oa vai
labl
efrom AppliedAna l
y t
ic
si nthef orm ofMi cro
-Sp
eed,a
Fort
h-l
ikela
ng uagecombi nedwit
ha na r
ithmeti
cpr oc
essorboa rd.ALFProd-
uct
soLersan8 0 88proce
ssorcar
dt ha
tincl
ude sso4warethatspeedsupApple-
so4mathfunctions.I
tals
oa l
lowsthecal
li
ngofde dic
ate
dma t
hf uncti
onsf
rom a
rec
urri
ngma chine-l
anguageprogr
am, i
ndependentofAppleso4.

Mor
eAppl
eso5I
nte
rna
ls
We l
l,t
hen,j
usthowdoe saper s
onus ethee xistingroutine
sinAppl es
o4?As
withmos tthi
ngswe ’
vecovered,thei mportantthi ngt oknowi stheaddress
esof
theentrypointstotheAppl e
so4r out
inesf ort heba s
icma t
hope rati
onsthat
int
erestus.Wea l
sone e
dtor eviv
et hediscuss i
onoft heARG( “ar
gume nt
”)regis
-
ter
,whi c
hweme nti
onedbr i
eSyint hepreviousc ha pter.
⇢eARGr egist
erisi
denticali
nf orma tt
ot heFACa ndisusedtoholdthes ec
-
ondnumbe rinSoating-
pointforma twhendoi ngt wo- va
luefuncti
onss uchas
addit
ion,subtr
acti
on,mul ti
pli
cation,anddi vi
sion.⇢eARGr egi
ste
rusesby te
s
$A5through$AA.
Tos e
ehowARGi sused,considertheseimpor tantentrypointstoAppleso4
ma t
hr outi
nes
:

Funct
ion ARG<f unc >FAC MEM <f unc>FAC
Addit
ion FADDT($E7C1) FADD($E7BE)
Subt
ract
ion FSUBT($E7AA) FSUB($E7A7)
Multi
pli
cat
ion FMULTT($E982) FMULT($E97F)
Divi
si
on FDIVT($EA69) FDIV($EA66)

FortheFr s
tc ol
umnofl abe l
s,t
hea ss
ociat
eda ddresse
sshowt heent
rypoint
forther outi
nest hatwil
lpe rf
ormt heg i
venf uncti
onbe tweentheARGr eg
iste
r
andt heFAC.Fore xample,acallt
oFSUBT( $E7AA)wo uldsubtr
actthecont
entsof
theFACf rom t
hec ontent
softheARG. ⇢er esul
twoul dbele4intheFAC.
Priortocal
linga nyofthesefourrouti
ne s
,theAc cumulat
ormus tbeloaded
withthee xponentva l
ueoftheFAC( FACEXP=$9D) .⇢i salsoser
vestocondit
ion
theze roSag.Fore xample,tomul t
ipl
yFACt i
me sARG,thefol
lowingcodecould
beuse d;
LDA $9D
JSR $E982

⇢es econdcol
umnofl abel
sreferst
ot her out
inesuse
dtoperf
ormtheindi-
c
ate
df uncti
onbe t
we e
nt heFACa ndda tas tor
e dinme mory(s
uchasinar eal
v
ari
abl
e )orinadatablocksetupbyt heprog ra
mme r.
Tous ethes
e,t heY- Re
g i
st
era ndAc cumul a
tormus tbesetupwiththe
a
ddres
soft hememor ylocat
ionholdingthenume ricdat
a(Y,A=highbyt
e, l
ow

]
2
66 As
sembl
yLi
nes

byt
e).Whenar
out
inei
scal
led,t
hedat
apoint
edt
obyY,Awi l
lthe
nbetr
ans
-
fe
rre
dint
oARGa
ndthedi
rec
tfunct
ionr
out
ine(
Frs
tcol
umn)t
hencal
le
d.

AnEx
ampl
e⇢ a
tDoe
sn’
tWor
k
Youma ywonde rwhyas ampleli
st
ingthatdoesn’
tworkisincl
udedhere.
⇢er eas
onisthatthi
sli
sti
ngdoe spre
sent,i
nac lea
rway,anover
allexa
mpleof
whatwe’v
ebe e
ndiscuss
inginthischa
pterandtheprevi
ousone.
Itwilla
lsohe
lp
youunders
tandthechangeswe’l
lbema ki
nglateroninorde
rtocreat
earouti
ne
t
hatdoeswork!
From Appleo4,
s therout
inewouldbec al
le
df r
om aprogr
amlikethi
s:
10 INPUT “X1, X2:"; X1, X2
20 CALL 768, X1, X2, RSLT
30 PRINT XI; " + "; X2; “ = ”; RSLT

Whe r
eX1 a ndX2a r
et hetwoa r
gumentsfort
headdi
ti
onrouti
ne,t
hatrout
ine
wi
llbecal
led.⇢er esultoft
hecalc
ulat
ionwil
lbes
entbac
ktotheAppl
eso4pro-
gr
ami nt
ot hevar
iabl
eRSLT.
He r
e’sthel
is
ti
ngf ortheaddi
tionr
outi
ne:
1 ********************************
2 * *
3 * AL27-M.L. ADDITION SUBR 1 *
4 * (DOESN’T WORK) *
5 * *
6 * SYNTAX: CALL 768,X1,X2,RSLT *
7 * RSLT = X1 + X2 *
8 ********************************
9 *
10 * OBJ $300
11 ORG $300
12 *
13 CHKCOM EQU $DEBE
14 PTRGET EQU $DFE3
15 FRMNUM EQU $DD67
16 FACEXP EQU $9D
17 MOVMF EQU $EB2B
18 MOVAF EQU $EB63
19 FADDT EQU $E7C1
20 *
0300: 20 BE DE 21 ENTRY JSR CHKCOM
0303: 20 67 DD 22 X1 JSR FRMNUM ; FP -> FAC
0306: 20 63 EB 23 JSR MOVAF ; FAC -> ARG
24 *
0309: 20 BE DE 25 X2 JSR CHKCOM
030C: 20 67 DD 26 JSR FRMNUM ; FP -> FAC
27 *
030F: A5 9D 28 ADD LDA FACEXP
0311: 20 C1 E7 29 JSR FADDT ; X1 + X2
30 *
0314: 20 BE DE 31 RSLT JSR CHKCOM
2
7.Fl
oat
ing
-Poi
ntMa
thRout
ine
s 2
67

0317: 20 E3 DF 32 JSR PTRGET


031A: AA 33 TAX ; MOVE LO BYTE TO X
031B: 20 2B EB 34 JSR MOVMF ; FAC -> FP
35 *
031E: 60 36 RTS

Li
ne2
1be
ginst
her
out
inebyFr
stt
aki
ngc
areoft
hec
ommaf
oll
owi
ngt
he
768 i
nt heCALL st
atement
.FRMNUM (
$DD67)ist henusedtoevalua
tetheFr st
expre
ssi
on.FRMNUMconveni
ent
lyleavesther
e s
ultintheFAC.S
incewewillwa nt
theFrs
targumentintheARGregi
ste
r,MOVAF($EB63=MOVet oARGfrom FAC)is
the nus edtomov et heda ta.
Li ne2 5ag ai
nc allsCHKCOMt o“ gobbl e”thene x tcomma ,a4e rwhi chFRMNUM
isa gainus edtoeva lua tethene xtva l
uea ndpl aceitint heFAC.
Wewoul dnowe x pectther esultt obei nt heFAC.Li ne3 1t akesc ar
eoft he
thir dc omma ,a4erwhi chPTRGET( $DFE3)F nds( orc reat
e s
)t hev a ri
ableinwhi ch
wewa ntt heresul
tr e turne d.
Att hispoint,e ve rythingha sbe enpr operlypla cedfort heus eoft heFADDT
rout inet oa ddtheFACa ndARGr e gi
sterst ogether.Line2 8l oa dstheAc cumula t
or
wi thFACEXP( $9D)a st hee ntryrequi r
e me ntfort hene xtins t
r uction,whi chisthe
ac tual executi
onoft heFADDTr outine.
⇢ eTAXonl ine3 3i sus eda 4erPTRGETt omov ethel ow- or derby t
eoft he
va riableda taaddr essi nt otheX- Re gister,a4e rwhi chMOVMF( $EB2B=MOVet o
Me mor yfrom FAC)i sus edt ocompl etet heda tatrans f
er.
Not e:Ifyou’reunf ami l
iarwi t
ht hef unda me ntalmov er out ines,youma y
wi sht og oba cktot hepr eviousc hapt er,whi chc ov er
edt he ses uppor ti
ngr ou-
tine s.
⇢eni ceparta boutt hisroutinei showe a si
lythes etupf ort hea dditi
onr ou-
tinewa saccompl i
s he d.Wi thal i
ttlethoug ht,thoug h,youma yr eal
iz
et hisisto
bee x pected.A4e ra ll,thei nternalr out i
ne swe rec r
eatedi nt heFr stpl ac
et o
pr oce ssda t
aea s
il
ywi t
hi na nAppl eso4pr og r
a m.

Whyi
tDoe
sn’
tWor
k
⇢er outinefail
sbe c
auseofFRMNUM. Alt
houghitwa smentione dthatFRMNUM
le
avesitsresultinFAC,wha tyouwe ren’tle
tinonwa sthefactthatita l
souses
ARGd ur
ingi t
sc alc
ulati
ons.⇢i sme a
nst hatwhenwec al
lFRMNUMas econdtime
online26 ,
wea reunknowinglydestr
oy i
ngt heval
uewes etupi nARGi nl i
nes22
and2 3.
⇢es olution,then,istosavetheFACc ontent
sf r
om theFr stv a
luec al
cula
-
ti
oni nme mor yataplaceotherthanARG.
⇢e rearet woa l
ter
na t
ive
s.⇢eFr s
ti st
ous esomeofAppl e
so4’sownt em-
porarynume ricr egi
ste
rs,whicha r
ec all
ed,cle
verlyenough,TEMP1( $937$97),
TEMP2($987$9C) ,andTEMP3( $8A7$8E).⇢eonl yr i
skhereisint hede st
ruct
ion

]
2
68 As
sembl
yLi
nes

ofda tala
teronbyothert
emporar
ycal
culat
ionsbyFRMNUM($DD67)a ndFRMEVL
($DD7B).
Anot he
rposs
ibi
li
tywouldbetose
tasideourownt emporarystorag
ea re
a.
Fort hisnexte
xamplewe’
lldojus
ttha
t,usingthel
asthalfoftheinputbuLe r
,
$2807$284.
He re
’stherevi
sedli
sti
ng,ca
ll
ed us
ingt hesameAppl eso4 progra
ma s
before:
1 ********************************
2 * *
3 * AL27-M.L. ADDITION SUBR 2 *
4 * *
5 * SYNTAX: CALL 768,X1,X2,RSLT *
6 * RSLT = X1 + X2 *
7 ********************************
8 *
9 * OBJ $300
10 ORG $300
11 *
12 CHKCOM EQU $DEBE
13 PTRGET EQU $DFE3
14 FRMNUM EQU $DD67
15 FACEXP EQU $9D
16 MOVMF EQU $EB2B
17 CONUPK EQU $E9E3
18 FADDT EQU $E7C1
19 *
0300: 20 BE DE 20 ENTRY JSR CHKCOM
0303: 20 67 DD 21 X1 JSR FRMNUM ; FP -> FAC
22 *
0306: A0 02 23 LDY #$02
0308: A2 80 24 LDX #$80 ; $280
030A: 20 2B EB 25 JSR MOVMF ; FAC -> MEMORY
26 *
030D: 20 BE DE 27 X2 JSR CHKCOM
0310: 20 67 DD 28 JSR FRMNUM ; FP -> FAC
29 *
0313: A0 02 30 ADD LDY #$02
0315: A9 80 31 LDA #$80 ; $280
0317: 20 E3 E9 32 JSR CONUPK ; MEMORY -> ARG
031A: A5 9D 33 LDA FACEXP
031C: 20 C1 E7 34 JSR FADDT ; X1 + X2
35 *
031F: 20 BE DE 36 RSLT JSR CHKCOM
0322: 20 E3 DF 37 JSR PTRGET
0325: AA 38 TAX ; MOVE LO BYTE TO X
0326: 20 2B EB 39 JSR MOVMF ; FAC -> FP
40 *
0329: 60 41 RTS

You’l
lnotic
einthisl
is
tingtha
tli
nes23and24setupt
heY-andX-Re
gist
ers
f
orthesubsequentc
alltoMOVMF.⇢isstor
estheda
tafort
heFrs
tval
uesaf
el
yin
memory.
2
7.Fl
oat
ing
-Poi
ntMa
thRout
ine
s 2
69

⇢ewor d“s af
ely”i
sus edwi t
hc e
rtai
nc av eat
s.⇢ei nputbuLe risaus ef
ul
areainwhicht os t
oretempor aryda t
a,buty ous houldbea wareoft hek indsof
condit
ionsthatwi l
loverwriteda taplacedt he r
e.DOSc omma ndsa ndi nput
stat
ementsa ret hemos tlikelyt hreat
s.Al s
o,c omma ndse xecute
df rom t he
imme dia
temodec anov e
rwr i
tethei nputbuLe r.⇢i sisi nf actwhyweus ed
$2807$284fort het emporaryregister
.⇢i sa llowsy outot r
yt heroutinef rom
theimme diat
emode ,si
ncey oua reunlikel
yt ous emor et han1 27charac tersas
yourcomma ndl inewhe ntest
ingther outi
ne.
Oncetheda t
aiss t
oreds af
elyinme mory ,li
ne2 8e valuat
est henex tv al
ue,
leavi
ngtheresultintheFAC.Att hispointweus eanotherroutine,CONUPK( $E9E3
=CONv ert(?)a ndUnPa c
K) ,tomov etheda tafrom $2807$284 ba ckt oARG.
Re member,thisisne ce
ssarybec a
us eFRMNUMonl i
ne28ma kesitimpos sibleto
storet
hev a
lueforX1i nARG.
A4erCONUPKput stheda t
aba ckinARG, FADDT( $E7C1) addsFACt oARG.

ALi
tt
leMor
eFi
nes
se
Inthechar
tshowingthevari
ousma throuti
neentr
ypoints,y
ou’l
lremem-
be
rthatther
ewa sasetofrout
inesthatall
owf ordea
li
ngwi thdat
ainme mory
di
rec
tly
. Wecanusethes
etocreat
eas l
ightl
ysma l
le
rvers
ionofthepre
viouspr
o-
gr
am whichwil
leli
minateourhavi
ngtol oadARGdire
ctl
ypriortocal
li
ngFADDT.
Here
’sthei
mprovedlis
ti
ng:
1 ********************************
2 * *
3 * AL27-M.L. ADDITION SUBR 3 *
4 * *
5 * SYNTAX: CALL 768,X1,X2,RSLT *
6 * RSLT = X1 + X2 *
7 ********************************
8 *
9 * OBJ $300
10 ORG $300
11 *
12 CHKCOM EQU $DEBE
13 PTRGET EQU $DFE3
14 FRMNUM EQU $DD67
15 FACEXP EQU $9D
16 MOVMF EQU $EB2B
17 CONUPK EQU $E9E3
18 FADD EQU $E7BE
19 *
0300: 20 BE DE 20 ENTRY JSR CHKCOM
0303: 20 67 DD 21 X1 JSR FRMNUM ; FP -> FAC
22 *
0306: A0 02 23 LDY #$02
0308: A2 80 24 LDX #$80 ; $280
030A: 20 2B EB 25 JSR MOVMF ; FAC -> MEMORY
26 *

]
2
70 As
sembl
yLi
nes

030D: 20 BE DE 27 X2 JSR CHKCOM


0310: 20 67 DD 28 JSR FRMNUM ; FP -> FAC
29 *
0313: A0 02 30 ADD LDY #$02
0315: A9 80 31 LDA #$80 ; $280
0317: 20 BE E7 32 JSR FADD ; X1 + X2
33 *
031A: 20 BE DE 34 RSLT JSR CHKCOM
031D: 20 E3 DF 35 JSR PTRGET
0320: AA 36 TAX ; MOVE LO BYTE TO X
0321: 20 2B EB 37 JSR MOVMF ; FAC -> FP
38 *
0324: 60 39 RTS

⇢eonl ydiLer
encebetweenthisrout
ineandthepre
viousoneisthatline30
nows et
supt heY- Regi
sterandAc cumulat
orforadirec
tc a
lltoFADD( $E7BE)
.
⇢i sent
rypoi ntautomati
call
ytransfe
rsthecont
entsof$2807$284 t
oARGa nd
the
n“ f
all
sinto”FADDT($E7C1).

Ot
herOpe
rat
ions
:Subt
rac
tion,
Mul
ti
pli
cat
ion,
andSoOn
Cr eat
ingr outine stodot heot hert hreefunctionsisv erys imple.Re writ
ing
li
nes1 8a nd3 2oft hei mprovedl i
sti
ngt ouseFSUB( $E7A7), FMULT( $E97F),and
FDIV( $EA66) willcreatetherout i
nest ope r
formt hec orres
pondi ngf unc ti
ons.
Asi tha ppe ns,the r
e’salsoav a
rie t
yofot hers implef unc t
ionst hatcanbe
performe dont heFACwi thas ingleJSR. Abr ie
flistisprese ntedi nAppe ndixD
(Moni torSubr outines).
Informa ti
onl ikewha t’
sgiveni nt hatl
istisqui t
ev al
ua ble,ifnoti ndis
pens-
able,whe ny ou’rewr iti
ngy ourowna ssembly-l
ang uagerout inesthatus eApple-
so4.⇢e rea reaf ew not abl
es ourcesf ors uchinf ormation.⇢eFr stisina n
art
iclebyJ ohnCr os sl
eyofAppl eComput ercalled“ Appl eso4I nternalEntry
Points,”whi ch ha sbe enr eprintedi n anumbe rofpl a cesi ncl
udi ngApp l
e
Orcha rd,Ca l
l-
A. P.
P. L.E.a
,ndCa ll
-A.P.P.L.
E. I
nDe pth#1.
⇢e reisalsoabookc all
edWha t’sWhe reint heAp ple?byWi l
li
a m F.Lueb-
berttha tli
stsma nyoft hee ntrypoi ntstonotonl yAppl eso4buta l
sot othe
Moni tor,DOS , IntegerBAS IC, andmor e.

Conc
lus
ion
Weha veseenhowt heFACandARGreg i
ster
sareusedasthec e
ntra
l poi
ntsi
n
almostal
lofAppl e
so4’snumeri
ccalc
ulat
ions.Inaddit
ion(nopuni ntended)
,we
ha v
eseenhow t heindivi
dualmathrouti
ne sarecal
ledtope rfor
mt hede s
ire
d
functi
ons.
⇢e s
ene wr outi
nesshouldbev er
yus e
fulincreat
ingy ourownSoa t
ing-
pointuti
li
ti
es.Youma ywi sht
otrytocreatearouti
netope r
form asimplefunc-
tiononane nt
irearr
aya sanexe
rci
seinusingthesenewtechniques.
2
8
2
8. ⇠eBCD,
orBi
nar
yCo
dedDe
cimal
J
anua
ry1
983

⇢i schapt e
r’sdiscuss i
onc enter
sonal it
tle-me ntionedope rat
ionalmodeof
t
he6 5
02mi croproc e
ssork nowna sBCD,whi chs tandsf orBinaryCode dDe ci-
ma l
.Inpr eviousc hapterswe ’
vel ookeda tarithme ticope rati
onsthatus ebina r
y
andhe xadecima l
r epresenta t
ionsofthenumbe rsinv olved.Suchope ra
tionso4e n
r
e quir
eac e r
tainde greeofme nt a
ltransla ti
ont oproduc eade ci
ma lequi v
alent.
I
nt e
rmsofpr i
ntinganumbe ri nAS CI If orm,e venmor ediWcultyi stobe
expecte
di fyou’reus ingy ourownc onve rsi
onr outine sratherthanthebui lt-i
n
f
unc ti
onsofDOS ,Apple so4, andI nt
e gerBAS IC.
⇢eBCDmodeg reatlys i
mpl iFesthispr oc essbys toringnumbe r
si noneor
mor ebytereg i
ste
rs( ei
the rme mor y,X,Y, ortheAc cumul at
or)inade cima l
-ori-
entedma nner .I
tdoe sthisbyus ingtwof our-bi tgroupsi ne achbytetor epre
sent
adigitinba seten.I nt hiswa ytwodi gitspe rby tec anbes t
ored,thusg ivi
nga
t
ot a
lvaluer a
ng eof0t o9 9 ,versus0t o25 5us ingbi na ry.
⇢i stabl
epr ov i
desa ne x
a mpleofhowt heBCDc ount i
ngs c
hemeg oe s:

BCD He
x Bi
nar
y “
Rea
lVa
lue

0 $00 0000 0000 0
1 $01 0000 0001 1
2 $02 0000 0010 2
3 $03 0000 0011 3
.
.
.
9 $09 0000 1001 9
10 $10 0001 0000 16
11 $11 0001 0001 17
.
.
.
14 $14 0001 0100 20
15 $15 0001 0101 21
16 $16 0001 0110 22
17 $17 0001 0111 23
18 $18 0001 1000 24
19 $19 0001 1001 25
20 $20 0010 0000 32

Oneoftheni
cethi
ngsabouthexa
decimalnota
tionist
hate
achdigitofthe
he
xnumberr e
pre
sent
sone-
half(
fourbit
s)ofthebi
na r
ynumber.⇢isisag r
eat
he
lpwhenyoumustmenta
ll
yconvertf
rom hextobi
na r
yandbac
ka g
ain.BCDi s

]
2
72 As
sembl
yLi
nes

avariati
onont hist hemei nwhi c
ht hehe xnumbe rr e
allyc anbes aidt oe qualthe
decima lvalue( thati s,thede ci
ma landhe xc olumnswi l
la l
wa ysma tc h) .
Aboutt hist imey ouma ybet hinking ,“We l
l,that’
sa llv e
r yni ce ,butwhe re
doesthe6 5 02c omei ntot hepicture?”
Sof ar,allweha vehe reisapos sibl
es y s
temf orstoringde c i
ma lnumbe rsv i
a
ourus ualhe xby tes.⇢eg oodne wsi sthatt he6502a ctuallysuppor t
st hismode
inthea dditiona nds ubt r
actionope rati
ons.
⇢a t’
sr i
g ht.⇢es ecrettoma k i
ngi twor kistot e
llthe6 5 02t ha ty ouwi s
ht o
operateint hismode . ⇢i sisdonebyme ansoft heinstructionSED, whi c
hstands
forSEtDe cima lmode .Onc ethisinstructionha sbee ne xecut ed,a llf utur
ea dd
ands ubtractope rationswi l
lbedonei nt heBCD mode .Whe ny ou’ redone ,be
suretoc leare ve rythingba cktonor ma lwi ththeCLD,f orCLe arDe cima lmode ,
inst
ruction.
Spec i
al note: Inadv ertentset
tingoft hede ci
ma lmodec anc aus et heAppl eto
behaver athe rstrang e l
ya ndc anbemos tpuz zl
ingwhe ny ou’r etry ingt ode bug
programs .RESETd oesnotc le
arthede cima lSag(bit3oft heS tatusRe gi
st
e r
)1
.
Whe ni ndoubtdoaCALL-155, orFF65Gf rom theMoni tor, t
oc lea rt hedecima l
mode .
Let’sver i
fyt hatt hismodea ct
ua l
lywor kswithas ampl epr og r
a m: 2

1 ********************************
2 * *
3 * AL28-BCD DEMO ROUTINE 1 *
4 * *
5 ********************************
6 *
8000: F8 7 START SED ; SET BCD MODE
8001: 18 8 CLC
8002: A9 46 9 LDA #$46
8004: 69 38 10 ADC #$38
8006: D8 11 CLD
8007: 00 12 DONE BRK ; BRK TO DISPLAY

Us i
ngtheBRKc ommandisaneasywaybotht
oe ndtheprogramanddis
pla
y
t
her es
ultoftheadditi
onint
heAc cumul
a t
or.Whent hi
sroutinei
scal
le
dwi t
h
e
it
he ran8000GoraCALL32768from BASIC,youshouldgettheMonit
orbrea
k
r
esponsewithadi
s pla
ysomet
hingli
kethi
s:
8009- A=84 X=90 Y=00 P=34 S=DE

Ignor
ingtheres
toftheli
ne,whe nwes eetheA=84wek nowthatt
heAccu-
mula
torholds84,
thecor
rectres
ultoftheaddit
ionopera
tion.Youcans
ubs
ti
tute
ot
hernumbe r
stoveri
fyt
hatitworkscorre
ctl
ywi t
ha l
ll
egalval
ues.
1[
CT]Ac
tua
ll
y,e
vent
houg
hthe6
502CPURES
ETdoe
snotc
lea
rthede
cima
lSa
g,i
nthe
Appl
eROM t
heRESETc
odedoe
sis
sueaCLD.
2[
CT]⇢eor
igi
nal
sampl
enumbe
rswe
re1
2and3
4,whi
cha
ctua
ll
yha
vet
hes
ames
um i
n
BCDa
ndnor
mal
mode
.Wi
th4
6and3
8,t
hes
um i
s84i
nBCDbut$7Ei
nnor
mal
mode
.
2
8.⇢eBCD,
orBi
nar
yCode
dDe
cima
l 2
73

As
imi
la
rex
per
ime
ntwor
kswi
ths
ubt
rac
ti 3
on:
1 ********************************
2 * *
3 * AL28-BCD DEMO ROUTINE 2 *
4 * *
5 ********************************
6 *
8000: F8 7 START SED ; SET BCD MODE
8001: 38 8 SEC
8002: A9 46 9 LDA #$46
8004: E9 38 10 SBC #$38
8006: D8 11 CLD
8007: 00 12 DONE BRK ; BRK TO DISPLAY

Int hiscas
e,ther e
sults
houldbe8 .
Ag ai
n,youma ywis
ht osubs
ti
tut
ediLe
r-
entv
alue stoveri
fyitsopera
tion.
Forbot haddit
iona ndsubtra
ction,res
ult
soft heoperat
ions“wrapgr
ound”
i
nama nne rsi
milartothewayhe xadeci
ma lcal
cul
ationsdo.⇢a tist
osayt
hat99
+1willg ivearesul
tof0 0(1
00l e
sstheleadi
ng1 )and071wi llg
ive9.

Li
mit
ati
ons
Likeeverythinge lseinl if
e,BCD ha sit
st rade oLsandf ai
li
ng s.⇢eFr s
t
involv
esthatv a
g uereferencema dee a r
liertoev er
y t
hi ngworkingwith“ l
e g
a l
v al
-
ues.”“Wha t
’sleg a
l?”youma yask.You’ l
lnot ethatc e
rtai
nhe xvalues,sucha s
$0A,n ev
era ppea r
.⇢i sisbe causei nt heBCD modes uchav al
uei s“ i
lle
g al

becauseitusesadi g
itoutoft her angeof0t o9 .I fy ouatte
mptt ous es ucha
val
uei ntheBCDmode ,you’l
lgetina ccurateresult
s.
Toa ddt ot hef un,not ea lsot hatt heBEQ,BNE a ndINC,DEC f amilie
sof
inst
ructi
onsdon’ twor ka sexpectede it
her. ⇢eN- Sa g(si
gn/negat
iveSa g)andZ-
Sag( z
eroSa g)area l
llinkedt obina r
yope rat
ionsa ndnott oBCD. ⇢us0 1+9 9
willyiel
d0 0,butN a ndZ r ema inuna Le c
ted,s i
nc ethe“ t
rue”bi naryr esul
t
shouldha v
ebe en$9A.Al so,nopr ovisioni sma def orne ga
tiv
enumbe rs(signed
ari
thme t
ic)
.How, then, dowet estforspe cialc
ondi ti
ons ?

⇢eCa
rryFl
ag
⇢ ecar
rySa gistheonlydirectindic
ati
onofa r
ithmeti
cresul
tsinBCD.In
addit
ionoperat
ions,thecarrywillbesetiftheresultexcee
ds9 9(
ov e
rSow).I
n
s
ubtract
ion,t
hec ar
rywi l
lbecle
arediftheresul
tislessthan0(under
Sow).
I
nmul t
ipl
e-byteoperat
ionsthecarryisuse
di nthes a
mewa yasitisi
n“nor-
ma l
”hexade
c i
ma la
rit
hme ti
c.

3[
CT]⇢eor
igi
nalc
odei
ncor
rec
tl
yha
dCLC i
nst
eadofSEC.I
naddi
ti
on,t
heor
igi
nal
s
amplenumbe
rswe
re34and12,whi
chac
tual
lyg
ivet
hes
ameres
ulti
nBCD andnor
-
mal
mode .
Wit
h46and38
,subt
rac
ti
ongi
ves8i
nBCDbut$0Ei
nnormalmode
.

]
2
74 As
sembl
yLi
nes

CommonOpe
rat
ions
Sinc
eINCandDECdon’
tpe
rfor
m pr
ope
rlyi
ntheBCDmode
,the
irf
unc
ti
ons
mus
tbeimpl
ement
edbyusi
ngtheADCa
ndSBCins
truc
ti
ons
:
1 ********************************
2 * *
3 * AL28-BCD DEMO 'INC' ROUTINE *
4 * *
5 ********************************
6 *
7 MEM EQU $06
8 BEEP EQU $FBDD
9 *
8000: F8 10 START SED ; SET BCD MODE
8001: 18 11 CLC
8002: A5 06 12 LDA MEM
8004: 69 01 13 ADC #$01
8006: B0 04 14 BCS ERR ; OVERFLOW
8008: 85 06 15 STA MEM ; MEM = MEM + 1
800A: D8 16 CLD
800B: 60 17 DONE RTS
800C: 4C DD FB 18 ERR JMP BEEP

1 ********************************
2 * *
3 * AL28-BCD DEMO 'DEC' ROUTINE *
4 * *
5 ********************************
6 *
7 MEM EQU $06
8 BEEP EQU $FBDD
9 *
8000: F8 10 START SED ; SET BCD MODE
8001: 18 11 SEC
8002: A5 06 12 LDA MEM
8004: E9 01 13 SBC #$01
8006: 90 04 14 BCC ERR ; UNDERFLOW
8008: 85 06 15 STA MEM ; MEM = MEM - 1
800A: D8 16 CLD
800B: 60 17 DONE RTS
800C: 4C DD FB 18 ERR JMP BEEP

Noti
cehowthec a
rrysta
tusischec
kedtodetec
tov e
rSow (r
esul
t>9 9)or
unde
rSow(re
sul
t<0 )i
nt headdi
ti
onandsubtr
act
ionr outine
s,r
espec
ti
vel
y.MEM
i
same moryl
oca
tionpres
umedt ohol
dalega
lBCDv alue.4

4[
CT]⇢e
rei
sonepr
obl
em wi
tha
lloft
hes
erout
ine
s:I
nthec
aseofa
ner
ror
,theCLDi
s
neverreac
hed.Ifyouruntheserout
ineswithav
a l
uein$06tha
tcaus
esanov
e r
Sow( or
underSow),yourApplewilliss
ueas tra
nge“bah-be
ep”
.Lucki
ly,t
heBEEPsubrout
ine
cl
earsthedeci
ma lmodebeforeret
urni
ng .
However,i
nyourownprogra
ms,y
ous hould
besuretoiss
ueaCLDfo ra
llcodepaths
.
2
8.⇢eBCD,
orBi
nar
yCode
dDe
cima
l 2
75

Mult
ipl
e-byt
eoper
ati
onsaredonei
nama
nne
rsi
mil
art
othewa
ythe
ir
he
xade
cimalequi
val
ent
sareha
ndle
d:
1 ********************************
2 * *
3 * AL28-BCD ADDITION ROUTINE *
4 * *
5 ********************************
6 *
7 *
8 MEM1 EQU $06 ; 6,7
9 MEM2 EQU $08 ; 8,9
10 RSLT EQU $0A ; A,B
11 BEEP EQU $FBDD
12 *
8000: F8 13 ENTRY SED
8001: 18 14 CLC
8002: A5 06 15 LDA MEM1
8004: 65 08 16 ADC MEM2
8006: 85 0A 17 STA RSLT
8008: A5 07 18 LDA MEM1+1
800A: 65 09 19 ADC MEM2+1
800C: 85 0B 20 STA RSLT+1 ; RSLT = MEM1 + MEM2
800E: B0 02 21 BCS ERR ; OVERFLOW
8010: D8 22 CLD
8011: 60 23 DONE RTS
8012: 4C DD FB 24 ERR JMP BEEP

1 ********************************
2 * *
3 * AL28-BCD SUBTRACT ROUTINE *
4 * *
5 ********************************
6 *
7 *
8 MEM1 EQU $06 ; 6,7
9 MEM2 EQU $08 ; 8,9
10 RSLT EQU $0A ; A,B
11 BEEP EQU $FBDD
12 *
8000: F8 13 ENTRY SED
8001: 38 14 SEC
8002: A5 06 15 LDA MEM1
8004: E5 08 16 SBC MEM2
8006: 85 0A 17 STA RSLT
8008: A5 07 18 LDA MEM1+1
800A: E5 09 19 SBC MEM2+1
800C: 85 0B 20 STA RSLT+1 ; RSLT = MEM1 - MEM2
800E: 90 02 21 BCC ERR ; UNDERFLOW
8010: D8 22 CLD
8011: 60 23 DONE RTS
8012: 4C DD FB 24 ERR JMP BEEP

]
2
76 As
sembl
yLi
nes

Pr
int
ingBCDVa
lue
s
Oneoft hebiggestadvantagesofBCDi st hattheval
uesa reeas
ilyprinte
dto
thes cre e
nordi sk
.Whe nus inghex a
de c
ima lma t
h,somes ortofhe x-to-ASCII
stringde cima l
conv e
rsionrout i
neisrequire
d. ⇢i sist
henfollowedbyt hepr i
nt-
ingoft hedig i
tsviasomes tringprintrouti
ne .I
nBCD,onl yami nima lconver-
sioni sne e
de d,andthepr i
ntingisdonef ai
rl
ye as
ily.
⇢ee asies
twa yt oprintanumbe ristous eoneoft heMoni t
orr outi
nes.
PRBYTE( $FDDA=P RintBYTE) ,
fore x
ampl e
, pri
ntsthecontentsoftheAc cumu-
latora sahe xby t
e.He re
’saroutinethattakestwoBCDv al
uesf rom memor yand
pr i
ntst hesum:
1 ********************************
2 * *
3 * AL28-BCD PRINT ROUTINE 1 *
4 * *
5 ********************************
6 *
7 *
8 MEM1 EQU $06
9 MEM2 EQU $07
10 PRBYTE EQU $FDDA
11 BEEP EQU $FBDD
12 *
8000: F8 13 ENTRY SED
8001: 18 14 CLC
8002: A5 06 15 LDA MEM1
8004: 65 07 16 ADC MEM2 ; ACC = MEM1 + MEM2
8006: B0 05 17 BCS ERR ; OVERFLOW
8008: D8 18 CLD
8009: 20 DA FD 19 JSR PRBYTE
800C: 60 20 DONE RTS
800D: 4C DD FB 21 ERR JMP BEEP

Youcanexper
imentbyput t
ingdi
Ler
entv a
luesin$06a nd$07a ndcal
li
ng
therout
ine
.Fortwo-byt
evalues(
0to9999
)onec anusePRNTAX($F941=P RiNT
Accumulat
orandX-Re g
ist
er),
whichex
pectst
heAc cumulat
orandX- Regi
ste
rto
beloade
dwiththebyte
stobepr i
nte
dpri
ortothecall
:
1 ********************************
2 * *
3 * AL28-BCD PRINT ROUTINE 2 *
4 * *
5 ********************************
6 *
7 *
8 MEM1 EQU $06 ; 6,7
9 MEM2 EQU $08 ; 8,9
10 PRNTAX EQU $F941
11 BEEP EQU $FBDD
12 *
8000: F8 13 ENTRY SED
2
8.⇢eBCD,
orBi
nar
yCode
dDe
cima
l 2
77

8001: 18 14 CLC
8002: A5 06 15 LDA MEM1
8004: 65 08 16 ADC MEM2
8006: AA 17 TAX ; STORE RSLT IN X
8007: A5 07 18 LDA MEM1+1
8009: 65 09 19 ADC MEM2+1 ; RSLT+1 IN ACC
800B: B0 05 20 BCS ERR ; OVERFLOW
800D: D8 21 CLD ; CLR FOR PRNTAX
800E: 20 41 F9 22 JSR PRNTAX
8011: 60 23 DONE RTS
8012: 4C DD FB 24 ERR JMP BEEP

Itisi mpor tanttonot i


cet hatine achr outinet heCLDi sus edtoc l
e a
rt hede c-
ima lmodeb eforec all
ingPRBYTE orPRNTAX.⇢i sisbe c auset heMoni torne eds
thenor ma lbi narymodet oc alcula t
es c
reena ddressesa ndpos it
ionspr ope rl
y.If
youc allt heMoni torwi tht heBCD modes et,strang ething swi l
lha ppe nwhe n
thet e
x tr eac hest hee ndoft hel i
neort hes creenne edst obes crolleda ndt he
Moni torr out inesa tt
e mptt oc alculatewhe retoputt hene x tlineofte xt.
Ify oudon’ twa nttous etheMoni t
orby tepr intr outine sor,f orwha tever
reason,j us twa ntt oc reat
et heAS CI Icha ractersy our sel
f,t hec onv ersionsa re
str
aightfor wa rda ndCOUT ( $FDED = Ch aracterOUTput –us ual
lypr onounc ed
“C-out ”)c anbeus eddi re
ctl
y .
⇢eonl yr ea lobs tac
lei show Lette r ASCI IVa lue* BCDVa lue
toc onve rtt heBCD di git
st ot he i
r 0 $B0 $00
AS CIIe qui v ale
nt s.Asi tha ppe ns, 1 $B1 $01
thi
si se ve ne asi
e rtodot ha ny ou 2 $B2 $02
mig hta tFr sts uppos e.Cons ider 3 $B3 $03
thetablea tther ight. 4 $B4 $04
From l ook inga tt hetabl e,we 5 $B5 $05
cans eet ha tt hel owe rdigitoft he 6 $B6 $06
AS CIIv aluec or respondst ot he 7 $B7 $07
8 $B8 $08
digitenc ode di nt heBCD f orma t
9 $B9 $09
and,c oinc ide ntall
ye noug h,t ot he
*highbi tset
numbe ri tselft o be pr i
nt ed.I f
therewa sawa yofa dding$B0t o
thev al
uef ort hedi g
itt obepr i
nt ed,we ’dha v ejustt hev aluewene ededt osend
toCOUTt opr i
ntt hea ppropr iatec har a
cter.
Toa dd$B0t ot heBCDv al
ue ss hownwoul dnor ma ll
yr e
quiret heus ualCLC,
ADCi nstruc tions.⇢e reisamor ee legant( thati s
,s hor t
e r)wa y,howe v er.You
ma yr eme mbe rt hatt heORA( logica lORwi thAc cumul a tor)canbeus eda sa
ma sktope rfor ma nov erl
ay-likeope rati
on.
He re’showapos s
ibleORAope rationwoul da ppe ar:
Accumulator: 0000 0110 ($06 BCD)
ORA #$B0: 1011 0000
Result: 1011 0110 ($B6 = ASCII "6”)

]
2
78 As
sembl
yLi
nes

Whati
ftheupperBCDdigi
tisi
nvol
ved?⇢epr oc
edur
ethe
nisFr
stt
oshi
4
t
heuppe
rfourbi
ts“down”t
othel
owernibbl
epos
it
ion:
BCD value: 0101 0000 ($50 BCD)
LSR 0010 1000
LSR 0001 0100
LSR 0000 1010
LSR 0000 0101
Result: 0000 0101 ($05 BCD)

Ah,youask,whati fbothdigi
tspos
sibleareindica
tedbyt heBCD v al
ue?
⇢ea ns
werherei
sFrsttos hi
4theuppernibbledownt othelowernibbl
e,aswas
j
ustshown,andtoprinttheAS CIIc
harac
terarri
v e
da t
.⇢e ntheorigina
lval
ueis
rel
oadedint
otheAc cumul at
orandtheuppe rnibblei
sma skedout.⇢i scanbe
doneus i
ngtheANDins t
ructi
on,whichhastheabili
tytoc l
earadesignat
edpor-
ti
onofaby t
etozeros
. Forexample:
Accumulator: 0101 0110 ($56 BCD)
AND #$0F: 0000 1111
Result #1: 0000 0110 ($06)
ORA #$B0: 1011 0000
Result #2: 1011 0110 ($B6 = ASCII “6”)

He
re,
the
n,i
sthec
ompl
et
erout
ine
:
1 ********************************
2 * *
3 * AL28-BCD PRINT ROUTINE 3 *
4 * *
5 ********************************
6 *
7 *
8 MEM EQU $06
9 COUT EQU $FDED
10 *
8000: D8 11 ENTRY CLD ; BCD MODE NOT NECC
8001: A5 06 12 LDA MEM ; GET BCD NUMBER
8003: 4A 13 LSR ; SHIFT UPPER NIBBLE
8004: 4A 14 LSR ; TO BOTTOM POSITION
8005: 4A 15 LSR
8006: 4A 16 LSR
8007: 09 B0 17 ORA #$B0 ; %1011 0000
8009: 20 ED FD 18 JSR COUT ; PRINT DIGIT
800C: A5 06 19 LDA MEM ; RETRIEVE ORIG BCD
800E: 29 0F 20 AND #$0F ; %0000 1111
8010: 09 B0 21 ORA #$B0 ; %1011 0000
8012: 20 ED FD 22 JSR COUT
8015: 60 23 DONE RTS

⇢ eCLDisdoneatthebegi
nningjus
ttoemphasi
zetha
ttheBCD modei s
notr
equi
redher
esinc
ethedigi
tispre
sumedtoexi
stal
rea
dyinMEMandnoari
th-
meti
coper
ati
onsarea
nti
c i
pat
ed.Remembertha
ttheBCDmodei sr
equi
redonly
2
8.⇢eBCD,
orBi
nar
yCode
dDe
cima
l 2
79

duri
ngt hea c
tuala dditionors ubtract
ionope ra
tions.Althought heBCD mode
wouldha v enoha rmf ule Le c
tont heAND a ndORA ope rati
ons,COUTwo ul
dc er
-
tai
nlytakeoLe nsea tbe ingc all
edwhi l
et heBCDmodewa ss t
il
line Lect.
Lines1 2throug h1 6g ettheoriginalBCDv a
luefrom me mor ya ndthe nshi4
itl
e4f ourtime stomov et heuppe rnibbletothelowe rposit
ion.Att hispointthe
ORA#$B0i sdonet oc onv ertthevaluei ntheAc cumulatortot hepr ope rASCII
val
ue ,atwhi c hpoi ntt heJSR COUT onl i
ne1 8pr int
st heFr stdi g
it.Line1 9
ret
ri
e vestheor ig
ina lvaluea g
ain,a4erwhi chtheAND#$0Fc le
arstheuppe rdigi
t
to0a ndtheAS CIIc onv ersioniscompl ete
da ndpr i
ntedasbe f
ore.
⇢er emainde roft her outinei
sident i
caltotheprevi
ouse xampl epr ogra
m.

Conc
lus
ion
⇢eBi na
r yCodedDe cimalmodeoft he6502canbec onv e
nientf
oravarie
ty
ofreas
ons.It
smos tf
re quentuseistofaci
li
tat
einputandout put,
part
icul
arl
yfor
sci
enti
Fcinst
rume nt
ation.
Anumbe rofpointsshouldbek epti
nmi ndwhe nusi
ngt heBCDmode :
1. ⇢emodes houl
dbes etonlyfora r
it
hme t
icproce
sse
st hatuseBCDv alues
,
suchasaddit
ionands ubtr
a c
ti
on.
2
. Onlyle
galv
alue
sareallowed:079 f
orea
ch di
gi
t.Va
lue
sout
si
det
he
e
xpe
cte
dra
ngewi
llge
ner
atei
naccura
tere
sul
ts
.
3
. ⇢eBCDmodes houldbecle
aredassoona sposs
ibl
ewhenar
ithmeti
coper
-
a
tionsa
rec
omplet
edsoastoavoi
dposs
iblecompl i
cat
ionswi
thot
herso4war
ein
t
heApplet
hatne
it
herex
pe c
ts,
norchec
k sfor
,theBCDmode .
4
. RESETdoesnotcl
earthedec
imalmodeofthe6
5025On
. l
ytheCLDins
truc
-
t
iondoe
s.Youcanal
socle
a rt
hemodebyme a
nsofaCALL-155f
rom BASICor
a
nFF59Gfr
om theMonit
or.
5. ⇢eNa ndZ-Sagsar
eunrel
ia
bleasame ansofde
tec
ti
ngthere
sul
tsofc
om-
pa
ris
onsorofinc
rement/
decr
ementopera
tions.Onl
ytheca
r r
ySagshoul
dbe
us
edtodet
ectt
heres
ult
sofsuc
hopera
tions
.
6
. ⇢ec a
rrySagwi
llbese
tforre
sul
tsg
rea
tert
han9
9(ov
erSow)a
ndc
lea
red
f
orr
esul
tsl
esst
han0(unde
rSow).
7
. BCDope
rat
ionsdo“
wra
par
ound.
”⇢a
tis
,99+0
1=0
0and0
070
1=9
9.

5[
CT]S
eef
oot
not
e1e
arl
ie
rint
hec
hapt
er.

]
2
80 As
sembl
yLi
nes

Spe
cia
lNot
e:Count
ingDown
⇢esearesomeg ener
a lr
ulest
ohelpinprogra
msusi
ngtheBCD modeof
t
he650
2.⇢e reisonlyonenotabl
eexc
epti
onthatmayonocc
asi
onpr
oveuse
ful
.
⇢etes
tfor0(BNE,BEQ)canbeusedwhencounti
ngdownint
heBCDmode.For
e
xampl
e:
SED
SEC
LDA #$01
SBC #$01
BEQ DONE

woul
dwor
k,whe
rea
s
SED
CLC
LDA #$99
ADC #$01
BEQ DONE

wouldnot.
I
tmightbea ninter
est
ingchal
lengeforyoutousetheinformati
ong i
venin
thi
schapte
randi nprevi
ouschapt
erstotrytowri
tearoutinethatwouldaddtwo
Apples
o4s t
ringstoget
herusi
ngtheBCD modea ndreturnther es
ultinathir
d
str
ing.⇢iswouldpr ovi
deawa yofextendi
ngthenorma lpre
c i
si
onofAppl e
so4
forma t
hematic
alope r
ati
onsrequir
ingmor et
hanninedi gi
ts,apr obl
emt hat
unfor
tunat
elydoesnothindermype rs
ona l
chec
kbookprog r
am.
2
9
2
9. I
nte
rce
pti
ngOut
put
Fe
brua
ry1
983

I
/Or outi
ne sareresponsi
bleforhandlingthecomputer’
scommuni c a
tions
wit
ht heoutsi
dewor ld.⇢e i
rde s
ignisals
ooneoft hemoreint
eres
ti
nga spectsof
ass
embl y-
languageprogramming .We ’
ll
spendthischa
pterandthenextlearning
howtoi nt
ercepttheI/Ov ec
torsoftheAppleandimplementourownr outines.
I
twi l
lma k
et hene xtfew demonstrat
ionsmuc heasi
erifyoudi s
c onne c
t
DOSf rom theI/Os yst
em. ⇢a t’
smos teas
il
ydonebyr unni
ngt hi
sshortAppl e-
so4BAS ICprog ra
m:
10 IN#0: PR#0: END

⇢a
twi
ll
kee
pDOSoutoft
hewa
yfort
heupc
omi
nge
xer
cis
es.

Out
put
Ine ar
lierchapterswedi scuss
edhowCOUT( $FDED)couldbeus edt oprint
cha
racte
rstot hesc r
een,todisk,ortoot heroutputde vi
ces.⇢eg eneralproce-
durewast oloadt heAc cumulatorwitht heAS CIIvalueforthec hara
cteryou
wantedtopr i
ntandt hentodoaJSRCOUT.
Tos eewha tha ppensa t$FDED wheny oudot his,ente
rt heMoni torby
meansoftheus ualCALL-151. ⇢e ntypein:FDEDL<RETURN>.
⇢eFr stinst
ructi
onl i
st
eds houldbeaJMP($0036). ⇢i si
sa nindir
ectjump
toalocat
ionpoi ntedtobyt heby tepai
r$36,$37.Tos eewhe rethesebytesare
cur
rentl
ypoi nti
ng,typein:36.37<RETURN>.
Youshouldg et:
0036- F0 FD

⇢i stel
lsyout hatthejumpwi l
lbema deto$FDF0,whi chi nt hiscasehap-
penstobethene x
ti nstr
ucti
ona 4ertheJMP($0036).$FDF0i sc alledCOUT1a nd
isuse
donlyt oprintc har
acter
st otheApple’sscree
n.Whe nout puti sgoingto
thedis
k,totheprinter,ortosomeot herdevice,$36,$37wi l
lpoi ntsome whe r
e
othe
rthan$FDF0.
Ifyouaresendingc haract
erstoaprinte
r,forexampl e
,$36, $37mi g
htpoint
to$C102.
CSW( Char ac
teroutputSWi t
ch)isthena meg i
v e
ntot heby t
epa i
r$36,
$37.Apointersucha sthi
sisusua l
lyca
ll
e dav ec
tor,i
nt hati
tdir ectstheSowof

]
2
82 As
sembl
yLi
nes

programc ont r
oltowha te
verroutine( thatis,wha teveraddre s
sinme mory
)is
appropriat
ea tthemome nt.
⇢ec ha ngingoft heCSWv ectori swha tha ppenswhe ny ouex e
cuteaPR#n
comma nd.CSWi spoint
e dtothea ddre s
sCn00,whe reni sthes l
otnumbe rgi
ven
inthePR#ns tatement.Ifnodeviceispr ese
nti nt heslot
,thennopr ogr
am wil
lbe
founda t$Cn00.⇢i sexplai
nswhyaBAS ICpr og ra
m ha ng swhe nanimproper
PR#c omma ndi sgiven:thecomput eriswa i
tingf ortheFna lRTSfrom anonexis
-
tentrouti
ne .Tov e
rif
yfory ours
elftha tt
heloc kupdoe s
n’toccurunt i
lachar
acte
r
isoutput,runt hisprogrami nAppl eso4BAS IC:
10 HOME
20 PR#5: REM OR SOME OTHER EMPTY SLOT
30 FOR I = 1 TO 20
40 POKE 1024 + I, 192 + I
50 NEXT I
60 PRINT "YOU WON’T SEE THIS”

Whe ny our unt hispr ogram, yous houl dseethelette


rsAt hr oug hTpr inte d
ont hes creen,butt hephr aseonl i
ne6 0s houldnota ppe ar
.⇢i ng sha ppent his
waybe causet hel ooponl ines2 0t hroug h4 0put stheda tadi rectlyi ntot he
scr
e enme mor ywi thoutg oingt hroug hCOUT.Re membe rthata l
lt hist i
meCSWi s
pointingt o$C500. It’
sonl ywhe nt heYc ha ract
erg et
ssenttoCOUTt ha tthec om-
puterha ng s
.
IfDOSwe reins tal
leda ndl ine2 0saidPRINTCHR$(4);"PR#5", thepr og ram
woul dha ngont ha tstateme ntbe causeoft hec arri
ager e
turns enta tt hee ndof
thepr i
nts tat
eme nt. I
t’sinstructivetonot ethatthec ar
riagereturni snota ctually
neededf ort hePR#t owor k.Addi ngas e micolontot hepr i
nts tateme ntwoul d
res
toret hepr ogramt oi tsoriginalsemi -f
unc ti
onal s
tat
e.
Onewoul dthinkf rom t hepr ecedingt houghtsthathook i
ngupar outinet o
theout puthook swoul dbef a
irlys i
mpl e.⇢epr oblemi sthatmos toft het ime
you’llwa nttoha veDOSa c t
ive,andDOSha sbeenc l
everl
yde si
g nedt odoe v ery-
thi
ngpos si
bletok eepi t
selfc onne c
ted.Whe nDOSi sinstal
led, CSW a ctually
pointst o$9EBD,apor ti
onofDOS ,andi t’sverydiWc ulttog etittopoi nte l
se-
whe re.
Spe ciFcal
ly,whe nev e
re i
the rinputorout putisdone ,bot hv ectorsa re
checke dtoma kesur eDOSi sstil
lhook edup. ⇢i smea nsthat,eve nt houghy ou
couldt empor aril
yc ha ngeCSW,a nyi nput -t
ypea cti
on woul dc aus eDOSt o
res
torei tsel
ft otheout putSow.He re’sapr ogramt os howt his.You’ l
lne edt o
reconne ctDOS( pre s
singRESETwi l
ldot hat)tot r
yit:
10 HOME
20 PR#0
30 PRINT CHR$(4);"CATALOG”
40 INPUT "TURN THINGS BACK ON"; I$
50 PRINT CHR$(4);“CATALOG"
2
9.I
nte
rce
pti
ngOut
put 2
83

⇢et heoryhe reisthatthePR#0 s etsCSWtopoi ntdire


c t
lyt o$FD0Cr a t
her
thantoDOS .⇢i siswhyt heCATALOGdoe sn’
twor ki nli
ne3 0.Howe ver,whe n
theinputisdone ,DOSi ssti
llhook edupt othei nputvect
or.Re a
li
zingtha tthe
outputconnec t
ionha sbee
nl ost,DOSt husreconnectsit
sel
f.Li ne50t henpe r-
for
msa sexpec t
ed.
Ingeneral,
DOSc anbedi s
c onne c
tedbye x
e cuti
ngbotha nIN#0a ndaPR#0
withinaBAS ICpr ogram,providedt hatoneisdonei mme di
a te
lya 4ertheot her
withnoi nputorout putinbe twee n.⇢eone -
lineBAS ICpr ogram use da tthe
beginni
ngoft hischapte
rtodisconne ctDOSe mpl oysthi
sprinciple.
Pres
singRESET wi l
lhookt hingsba ckupa nyt
imey ouwa nt.Not i
c ethat
thesear
enotdonea sDOSc omma ndss uchas:
10 PRINT CHR$(4);“IN#0": PRINT CHR$(4);“PR#0”

AnIN#0orPR#0a sadire
c tBASICcomma ndredi
rec
tsI/
Ot ot
heMonit
or.
⇢esa
mec omma ndsdoneasDOSc omma ndss
e tt
heI/
Ot oDOS.
Let
’sseej
usthowDOSdoe shandl
etheoutputvec
tor
s.WithDOSins
tal
le
d
a
ndact
ive
,ent
ertheMonitorandtypein:
36.37 AA53.AA54

Yous
houl
dge
t:
0036- BD 9E
AA53- F0 FD

WithDOSa c
tive
,CSWp oint st
oama inoutputentrypoi
nta t$9EBD. ⇢i sis
thebeginni
ngofthesectiontha twatc
hestheoutputforDOSc omma nds.Ev en-
tual
lyitdoesit
sowni ndirectjumpv iat
hev ec
torat$AA53,$AA54,whi chc om-
plet
esthepathtoCOUT1( $FDF0),Whe nyoudoaJSRCOUT( $FDED),t
he n,he r
e ’
s
thegeneral
Sowoft hi
ngs:
1. Wi ththeappropri
atevalueintheAccumulator,aJSRCOUT($FDED) isdone .
2
. At$FDED isaJMP t
otheaddresss
pec
iFe
dinCSW (
$36,$37)
.Wi
thDOS
i
nst
al
le
d,CSWpoint
stoDOSat$9EBD.
3. Whe nDOSisthr
oug
hlooki
ngatthechar
acter
,itdoe
saj
umpt
othea
ddr
ess
he
ldat$AA53,
$AA54.
⇢isnormal
lypoi
ntsto$FDF0.
4
. Ev
ent
ual
lya
nRTSr
etur
nsc
ont
rol
tot
hec
all
ingpr
ogr
am.

I
nte
rce
pti
ngOut
put
Anobv i
ousques
ti
onnowari
ses
.Howdowehookourr out
inetoDOS ?⇢i s
bas
ical
lydepe
ndsonwhe t
heras l
otisus
ed.Ifyouhappenedt obewr i
ting
Fr
mwa reforanint
erf
acec
ard,f
orexampl
e,t
hePR#comma ndwhe nex
ecuted
wouldautomat
ic
all
yhandl
etheset
ti
ngupofCSWtomakee ve
rythingwork.If
,

]
2
84 As
sembl
yLi
nes

howe ve
r ,youwa ntt oputar outi
nea talocat
ionotherthanthe$C000space,
anothera pproachi snee
ded.
⇢epr ocedurea ct
ual
lyisfairl
ys i
mple.Allyoune e
dt odoi ssetCSW t
o
whe r
ey ouwa nttheoutputtobeeventual
lyse
nta ndt
hencal 1
l$3EA.
Fore xa mple,let
’sputatrivi
alrouti
neat$300thatme re
lyjumpstoCOUT1
($FDF0).Goi nt
ot heMonitorandenter:
300: 4C F0 FD

I
fyoul
is
tthi
srout
iney
ous
houl
dge
t:
300L

0300- 4C F0 FD JMP $FDF0


0303- 00 BRK
0304- 00 BRK

Tohooki
tup,
typei
nthef
oll
owi
ngf
rom t
hei
mme
dia
temodeofAppl
eso4
POKE 54,0: POKE 55,3: CALL 1002

⇢issetsCSWt
opointt
o$300andthencal
ls$3EA.⇢es
amet
hingc
anbe
donef
rom wit
hina
nas
sembly
-la
ngua
geprogr
am wit
h:
LDA #$00
STA $36
LDA #$03
STA $37
JSR $3EA
RTS

Oncec onnect
edint hisway,ev
eryt
hingwills
til
llookthesameont he
s
creen.I
nr eal
it
y,however,ever
ychara
ctergoi
ngtot hescr
eeni
snow goi
ng
t
hrough$300.Youcanchec kt
henewrouti
ngbyente
ringtheMoni
torwhi
let
his
r
outi
neisinst
all
edandt
y pingi
n:
36.37 AA53.AA54

Yous
houl
dge
t:
0036- BD 9E
AA53- 00 03

⇢eMoni t
or,DOS ,a
ndBAS ICa l
lsendout
putviat
hejumpa tCOUT.⇢i s
s
ti
llpoint
st oDOS ,butnow DOSpoi ntsnott
oCOUT1 (
$FDF0)
,butt o$300.
⇢ere,ourrout
inedoesaj
umpt oCOUT1tocompl
etet
heSow.

1[
CT]⇢et
echni
quewoul
dbedi
Ler
entf
orPr
oDOS
,whi
chdoe
sn’
tha
veahook
upr
ou-
t
ine
.Ins
tea
d,youcanmanual
lyc
hanget
heoutputv
ect
orat$BE30,$BE31topoi
ntt
o
y
ourout
putrout
ine
.Se
echapt
ers
eve
nofIns
idet
heAppl
e//
e,byGaryB.Lit
tl
e.
2
9.I
nte
rce
pti
ngOut
put 2
85

Tov erifyt hatc ha ractersa reg oi ngt hr oug h$300,j ustt ypei nPOKE768,0.
Or ,from t heMoni tor ,type :300:0.
⇢ec omput erwi lli mme diate l
yha nga spr og ram Sow hi t
st he00 ( BRK
instruc t
ion)a t$300.⇢eBRKr out i
nei nt heMoni torwi llthent ryt os endt he
br e
ake rrorme ssaget hr oug hCOUT,a twhi chpoi nt$300wi l
lbec alleda g a
ina nd
thepr oc esswi llrepe a titselfindeFni te ly.
Ani ntere st
ingpoi nthe rei st ha twhe nCOUTi st urne doL( f
ori nstance ,a
simpl eRTSa t$300wi l
ldot het rick ),not hinga ppe arsont hes creende spitet he
facttha tt hec omput e riss til
lfullyf unc t
iona l.Ev ent houg hy ouc a n’tseewha t
you’ret ypi ng, youc oul dt ypei nCATALOGa ndt hedi skdr ivewoul dc omeon. ⇢e
Sa s
hi ngc ur sorwoul dr ema inont hes c
reens inceRDKEY( partoft hei nputr ou-
tinea t$FD1B)a ddr esse sthes creendi re c
tlyfort hec ur sor.
Toe xpe rime ntwi thCOUTs omemor e,let’stryar out i
net ha t’
sal it
tlemor e
interesting. Cont r
ol c ha r
a ctersarenor ma l
ly“ inv i
sible”i nthatt he y
’r enots entt o
thes c reenbyCOUT1.I fwec ouldde tectthec ont rolc hara c
terbe for eitg ott o
COUT1a ndc oul dc ha ng eitt oadi Le re ntva l
ue ,wec oul dha veitdi spla yasinv erse
ora ss omeot he rv isi
bl ec ha r
a ct
er.
Nor ma l
lya llc ha ractersg oingt hr oughCSWha vet hehi ghbi ts et.⇢a ti s
,a l
l
va l
uesa r eg reatert ha n$80. Inv ersea ndSa shingc ha rac t
ersa rec reate dbys e
nd-
ingc ha racterswi t
hav aluel esstha n$80t oCOUT.Al lcha r
a c
t e
r sint her angeof
$00t o$3Fc omeouti nve rse,anda llt hosef rom $40t o$7Fa reSa s hing.Ing en-
eralwha tt hisme ansi st hat,ift hehi ghbi ti sc l
ea red,c ontrolc ha racte
r swi l
l
comeouti ni nv ersea nd“ standa rd”c ha ra
c tersinSa shing .
⇢i si s,inf act,howt heFLASHa ndINVERSEc omma ndsofAppl eso4wor k.
⇢er outinea tCOUT1i nclude sapor ti
ont hatdoe sa nANDope rationont hev alue
aboutt obes tore dont hes cre e
na ndama skv a l
ues toreda tloc ati
on$32( c a
lled
INVFLG, shor tfor“ INVe rseFLa G” ). INVFLGn or ma l
lyhol dsa n$FF, sonoc ha nge
takespl ace .Howe v er, t
heBAS ICc omma ndsINVERSEa ndFLASHs etthev a
lue sto
$3Fa nd$7F, re spectiv ely,whi chpr oduc est hede s i
redr e s
ults.
⇢ef ollowi ngdi ag rami llustr
a test heINVFLGma sk’seLe ctonout goingc ha r
-
ac t
erss entt oCOUT:
He
x Bi
nar
y Cha
rac
ter
Char
acte
r: $C1 %1100 0001 A(
Nor
mal
)
I
NVFLG: $FF %1111 1111 –
ANDresult
: $C1 %1100 0001 A(
Nor
mal
)

Char
acte
r: $C1 %1100 0001 A(
Normal)
I
NVFLG: $7F %0111 1111 –
ANDresult
: $41 %0100 0001 A(
Fla
shi
ng)

Char
acte
r: $C1 %1100 0001 A(
Normal
)
I
NVFLG: $3F %0011 1111 –
ANDresult
: $01 %0000 0001 A(
Inv
ers
e)

]
2
86 As
sembl
yLi
nes

Wec a
ndoourowns pec
ial
ize
dproc
ess
ing
,thoug
h,s
oast
ohi
ghl
ig
htj
ust
c
ont
rol
char
act
ers
.He
re’
sthel
ist
ing2
:
1 ********************************
2 *AL29-CONTROL CHARACTER DISPLAY*
3 ********************************
4 *
5 ORG $300
6 *
7 COUT1 EQU $FDF0
8 *
0300: C9 A0 9 ENTRY CMP #$A0 ; FIRST NON-CTRL CHAR
0302: B0 06 10 BCS PRINT ; CHAR OKAY
0304: C9 8D 11 CMP #$8D ; LET 'CR' THRU
0306: F0 02 12 BEQ PRINT
0308: 29 3F 13 MASK AND #$3F ; CLEAR TOP 2 BITS
030A: 4C F0 FD 14 PRINT JMP COUT1 ; PRINT IT

⇢i sr out i
ne ’
sope r
a ti
oni sv erys tr
aightf
orwa rd.A c ompa ri
soni sdonea s
eachc ha racterr eachest her outinea t$300.Al l“ us ual
”c haract
ersa resent
throught oCOUT1una l
tered.Ifac haracte
ri sfoundt obeac ontrolc hara
cter,
though,at estisdonet os eeifi t’
sac a r
riagereturn.I fso,thattooispa sse
dt o
COUT1. A4e ra l
l,wedowa ntthes creent olooks ome whatnor mal.Ifac ontrol
charact
e r(othe rtha na<RETURN>)i sfound,howe ver,a nAND#$3Fc onv ert
sthe
charact
e rtoa ni nversecharact
e r,atwhi chpointitwi llbeforwardedtoCOUT1.
Anyc ont rolc hara
c te
r sge nerate
dbyapr ogra m,wi tht hee xcepti
on of
<RETURN>( <CTRL>M) ,willnowbes howni ni nverse.Whe ntypedf r
om t hekey-
boa r
d, <ESCAPE>,t her ight-ar
row k ey(<CTRL>U) ,a nd<CTRL>Xwon’ ts how up
sincethe ya reinterceptedbyt heMoni torinputr out ineandne verma keitto
COUT.

Ot
herOut
putDe
vic
es
Sofar ,al
lwe ’
vedonei sint
erce
ptCOUT,Flt
ert hechara
ctersgoingthrough,
ande v
ent uall
yr et
urnc ontr
ol t
otheMonitorscre
e nrouti
neCOUT1. I
fweha dour
ownout putde vice,t
hiswoul dnotbenece
ssary.⇢epoi nthereist
ode mons t
rate
thepossibili
tyofa l
ternateoutputdevi
ces
.Ul t
ima t
elythi
scouldincludepr i
nter
cards
,termi na l
s,anal
ogde vi
cessuchasmot ors,andmor e.S uc
hpr oj
e c
tsa re
rathe
rinv olved,howev er
,sofornowl e
t’
sjustse
ei fwec a
nwr it
eourownpr i
mi -
tiv
escreenr outine.

2[
CT]Whe
nrunni
ngunde
rDOS3
.3,
youc
anhookupy
ourr
out
inebye
xec
uti
ng:
POKE 54,0: POKE 55,3: CALL 1002
UnderProDOS,youcandi
rec
tl
ymodifyt
heoutputv
ect
orat$BE30,$BE31byr
unni
ng
t
hefol
lowingApple
so4pr
ogra
m( donotr
unthi
sasadir
ectc
omma nd):
10 POKE 48688,0: POKE 48689,3
2
9.I
nte
rce
pti
ngOut
put 2
87

⇢eba si
cmode l willbetos e
ta si
deonelineoft hescree
na sourdispla
ywi n-
dowa ndt oa t
temptt oc ontrolte
xtout putwithi
nt ha twi
ndow. Toa voidha vi
ng
toc r
ea t
ev erti
calscroll
ingr outi
nesa ndc ur
sorma nagementr outi
nes,we ’
llli
mi t
alloutputt othesingleli
nea nds croll
textonlytothel e4aseachne wc haracteris
displ
a yedont her i
ght.
Ifthi ssoundss uspiciouslysimilartoac a
lculatordis
pla y,you’
r eri
g ht.It
shouldbee asynowt ose ewhy ,withlimite
dr esourcesofdisplayha r
dwa rea nd,
mor es i
g niFcantl
y,limitedme mor yforma nageme ntrouti
ne s,suchadi splay
woul dbede si
rable
.
He re’sthesumma ryoft hedesignpoints
:
1. Di s
pl aywillbelimitedtoonel ine.
2
. Cha
rac
ter
swi
ll
beout
putont
her
ight
mos
tpos
it
ion.
3
. ⇢er ema
inde
roft
hel
inewi
lls
crol
ltot
hel
e4t
oma
ker
oom f
ore
achne
w
c
har
act
er.
4
. Noc
ont
rol
cha
rac
ter
swi
ll
bedi
spl
aye
d.
5
. ⇢el
e4-
arr
owk
ey,
<CTRL>H,wi
llbede
sig
nat
eda
sa“
cle
ardi
spl
ay”c
har
ac-
t
er.
6. Noe diti
ngcapabili
ti
es(thatis,backs
pace,forwardc opy,andsoon)wi l
lbe
providedf or,exc
eptfornumbe r5a bove.
Be f
orepr ocee
ding,let
’sdig
ressforamome nttome ntionthev a
lueofthelis
t
asapr ogrammi ngt e
chnique.Ify ouc a
n’tbringy ourse
lftoSowc ha rt
,atleas
t
ma keal i
s ttoclar
ifyexactl
ywha ty ourprogr
a m willdo.⇢i she l
pstoor gani
ze
yourt houg ht
sinag ene r
alwaybe foreyouhavet oleapina ndc odethedetail
ed
part
s.Ev eni fyoua mendi tasthec odi
ngprog res
ses,suchal i
stishelpful
.Now
backtoourr egul
arl
ys chedul
edpr ogram..
.
1 ********************************
2 * AL29-SPECIAL DISPLAY ROUTINE *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 LINE EQU $700 ; $700-727
9 YSAV1 EQU $35
10 *
0300: 84 35 11 ENTRY STY YSAV1 ; SAVE Y-REGISTER
0302: C9 A0 12 CMP #$A0 ; FIRST NON-CTRL CHAR
0304: B0 11 13 BCS SCROLL ; DISPLAY THE CHAR
0306: C9 88 14 CHK CMP #$88 ; BACKSPACE
0308: D0 0A 15 BNE DONE1
030A: A0 27 16 CLEAR LDY #$27
030C: A9 A0 17 LDA #$A0 ; SPACE
030E: 99 00 07 18 LOOP1 STA LINE,Y ; ERASE A CHAR

]
2
88 As
sembl
yLi
nes

0311: 88 19 DEY
0312: 10 FA 20 BPL LOOP1 ; UNTIL Y=$FF
0314: A4 35 21 DONE1 LDY YSAV1 ; RESTORE Y
0316: 60 22 OUT1 RTS ; DON’T SHOW
23 *
0317: 48 24 SCROLL PHA ; SAVE THE CHAR
0318: A0 01 25 LDY #$01
031A: B9 00 07 26 LOOP2 LDA LINE,Y
031D: 99 FF 06 27 STA LINE-1,Y
0320: C8 28 INY
0321: C0 28 29 CPY #$28
0323: 90 F5 30 BCC LOOP2 ; UNTIL Y=$28
0325: 68 31 PRINT PLA ; RETRIEVE CHAR
0326: 8D 27 07 32 STA LINE+$27
0329: A4 35 33 DONE2 LDY YSAV1 ; RESTORE Y
032B: 60 34 OUT2 RTS
032C: 27 35 CHK

A4ert hel i
sti
ngha sbeena ss
embled,ther out
ineishookedupt oCOUT,j ust
l
iketheot herr out
ine.Youwi llpr
obabl
ywa nttotypeinHOMEtog iveyouac l
ear
scree
nf ory ourdisplay.Onc eyourrout i
nei sinstal
le
d,e v
erythi
ngy out y
pe
shouldscroll ac
rossalineintheupperhalfofthescreen.Noti
cethatal
l expect
e d
outputf
r om t heApplei snowdoneoni tsownc us
tom displ
ay.Youc a
nl istpro-
grams,catalogadi sk,ordoa nyoftheusua lopera
tions.Tryt
ypingint hiscom-
ma ndli
nei nAppl eso4:
FOR I = 1 TO 127: PRINT CHR$(I);: NEXT I

Whe ny oupr essr eturn,y ous houlds eeawhol es e r


ie sofc haractersg o
whi z
zingt hroug hthewi ndow,e ndingwi tht hel owe r
ca sel ette r
s(althoug ht hey
ma ynotl ookqui ter ightify oudon’ thav eal owe rcas
edi spla yde vi
c e)
.Re me m-
ber,thele4a rrowwi ll cl
ea rthedispla ywi ndow.
⇢er out i
neitselfisfairlysimple .⇢eonl yme mor yl oc ationsde Fne da rethe
me mor yr angef ort hes c reenl i
nea t$700,at empora rys t orageby t
eus edby
COUT1, andourr outinet opr es
ervet hec ont ent softheY- Re g ist
er.⇢epr ogram
alsoconta i
nss omei ns t
ruc ti
vepoi ntsofs tyle.
One ntry,theY- Re gisteriss aved.⇢i si sbe causet he“ oWc i
al”out putr ou-
tine,COUT1,r eturnswi t
ha llregist
ers( A,X,a ndY)i nt ac twhe nc a l
led.Ma ny
otherrout inesinBAS ICa ndDOSa ssumet ha talloutputwi llbedonea ssafely
,
sowemus thonort ha tconv entiona swe ll.
Onc eYi ss a
ved,t hev aluepa s
s edtot hisr outinef rom t heAc c umul atoris
appropriatet otheAS CI Iv aluefort hec ha racte rtobepr i
nt ed.Aswa sdonei n
thec ontrol-charact
e rdi splayr outine,ac he cki sdonef orc ontr
olc haracters
.
Re membe rthatinthi sprog rama llcont rol cha racte
rs,eve n<RETURN>, wi llbeFl-
teredout .Ifac ontrolc ha racte
ri sde tected,thec ompa r isononl i
ne1 2wi llfai
l
andac hec kwi l
lbema def orthel e4a r
row( <CTRL>H) .I fthec harac t
eri snota
2
9.I
nte
rce
pti
ngOut
put 2
89

<CTRL>H,wewi
lli
mme
dia
tel
yex
itv
iaDONE1,whe
ret
heY-
Reg
ist
erwi
llbe
restor eda ndnoc hara cterwi llbedi spla yed.
Ifa<CTRL>Hi sde tecte d, theCLEARr out inec l
e arsthedi splaywi ndowt os pa-
ces.Anot ehe rea boutt heBPLonl ine2 0t ode terminewhe nt hel oopi sdone :
Youmi ghtt hinkt hatwewoul dwa ntt ous eaBNEt oFndoutwhe nYr eached0 .
⇢epr oblemi stha t,whe nYr ea che d0 ,thebr a nc hwoul df allthr oug ha ndwe
woul dnots toreas pac ea t$700,s ot hel ea dingc harac t
e rc oul dnotbec leared
from t hedi splaywi ndow.
Be caus ewek now t ha tYi ss tar t
e da t$27,wec ant estf orYr eachingt he
valueof$FFa sit“ wr apsa round”a 4e rr eac hing0 .Ana l
terna tea ppr oa chwoul d
ha vebe e
nt oma k eline1 8s a ySTALINE-1,Ya ndt os tartYwi thav alueof$28on
li
ne1 6 .LINE-1 wo ulde v alua tet o$6FF,a ndt huswec oul dus et heBNE t est
.
Eithe rwa ywor ks,butt hiss econda ppr oa chpr ov idesawa yofs howi nga not her
prog ra mmi ngt echni que .A4e rc learingt hewi ndow,t her out iner e t
ur nsv ia
DONE1, ag ainwi t
houtdi spla yinga nyne wc ha racte r.
Ifal eg iti
ma tec ha rac teri sde te ctedonl ine s1 2a nd1 3,c ont rolSowst o
SCROLL, whi chma kesr oom f ort hene wc ha ractert obedi splay e
d.Be causewe ’l
l
ne e
dt ous et heAc cumul a torf ort hes c r
ol li
ng ,t hec ha rac tert obepr intedi s
pus he dont ot hes tackt os avei tforf utur eus e.
Att ha tpoi nt ,theY- Re g ist
e ri ss ett o$01i npr epa rat i
onf ort heme mor y
mov et of ollow. Line2 6l oa dsac ha racterf rom onepos ition,a 4erwhi chl ine2 7
wi l
ls toret hec ha racteri nt hepos iti
oni mme diate l
yt ot hel e4.Fore x ampl e,on
theFr stpa s sthr oug h,t hev a l
uewi llbel oade df r om $701( $700,Ywh ereY=1 )
ands toreda t$700( $6FF,Ywh ereYs tillequa l
s1 ).
Not icet heus eoft wodi Le re ntba sea ddr essesf ort hei nde xeda ddre ss
ing.
⇢i sa ll
owsust ous et hes amev a l
uei nt heY- Re gistertol oa da nds tor ea ttwo
diLe renta ddr esses.⇢el oopi sr e pea tedunt i
lweha vemov eda llthec hara c
ters
onepos i
tiont ot hel e4. ⇢er out inet he nf all
si ntoPRINT.
PRINTF r s
tr etrieve st hec ha rac t
e rtobepr i
nt edf rom t hes tac kbyme ansof
thePLAonl ine3 1.I tthe ns tor est hec ha r
a ctera t$727.⇢ec odei swr it
te nthis
wa y( LINE+$27)t os howt ha ty ouc a n,i nmos ta ss
e mbl ers,a dda nya mountt o
ana ddr ess.Youa ren’tlimi tedt ot heus ua lADDR,ADDR+1t ha t’
smos to4e ns een.
A4e rt hec ha racterha sbe ens tor eda t$727,t heY- Reg i
s teri sre stor eda ndt he
rout i
ner etur nsv iaDONE2.
Yous houl dv erifyf ory our selft ha tt heAc cumul atora ndY- Re g i
stersa re
alwa ysl e4i nt he i
ror igina lc ondi tionsr eg ardle ssofwhe thert heRTS i sdone
throug hDONE1orDONE2.S inc ewedi dn’tus et heX- Re gister,ita lsowi l
lbepr e-
serve d.

]
2
90 As
sembl
yLi
nes

Conc
lus
ion
Her
earethemainpoi
ntsofourdis
cuss
ionontheout
putvec
tor
.
1
. ⇢ema inoutputvec
toriscall
edCSW,whic
hs t
andsforChar
act
erout
put
S
Wit
ch.
CSWisthebyt
epai
r$36,$37.

2
. DOSma
int
ainsi
tsownout
putv
ect
ora
t$AA53,
$AA54.

3
. DOScanbedi
sconne
cte
dbye
xec
uti
ngt
heBAS
ICs
tat
eme
ntIN#0:PR#0
(
nota
saDOSc
ommand).
4
. DOSc
anber
econne
cte
dbypr
ess
ingRESET.
5. Anyat
tempttoalt
erCSWdire
ctl
ywit
hDOSa c
ti
vewi
llbeundonebyDOS
ont
heFrs
tinputs
tat
ementfol
lowi
ngthea
tte
mpt
.
6
. Tohookar
out
inei
ntot
heout
putv
ect
ors
,ex
ecut
ethee
qui
val
entof
POKE 54,LB: POKE 55,HB: CALL 1002

wher
eLBandHBarethel
ow-a
ndhi
gh-
orde
rby
tesoft
hea
ddr
essy
ouwi
shout
-
putt
obedi
rec
tedt
o.3

7
.I fyou’
reha ndli
ngalloftheFnalout
put,
e ndtherouti
newi t
htheusua lRTS.
I
fyou’r
eme relyFlter
ingorwa tc
hingtheout
put ,y
oumus teventual
lypasscon-
t
rolontowhe retheFnaloutputwil
lbedone,usual
lyCOUT1($FDF0).
Inthenextc hapt
erwe ’
l
llookatthei
nputhook sa
nda thowt ouseyourown
r
outine
sont helist
eni
ngs i
deoftheApple.

3[
CT]⇢ee
qui
val
entf
orPr
oDOSwoul
dbe
:
10 POKE 48688,LB: POKE 48689,HB
3
0
3
0. I
nte
rce
pti
ngI
nput
Ma
rch1
983

I
t’
st i
met oe xami net hei nputs y s
te m oftheAppl e.Ma nypa ral
lel
scanbe
drawnbe tweeni ta ndt heout puts ystem,di s
c ussedi nt hepr evi
ousc hapter.
⇢oug hnotr equired, somef a mil
iaritywi ththatc hapter’
sma j
orpoi nt
swi l
lhe l
p
youunde rs
tandourc urr e
ntt opic
.
⇢ema i
nde mor out inesint hisi nstal
lme ntinv olv
el owercasetext;there-
for
e,it’
sstrongl
yr ecomme nde dtha tyoua cquirelowe rca
s edisplayha r
dwa reif
youdon’ thavei talrea dy.Lowe rcasec hipsforAppl eswi thre v
isionnumbe r
s
gre
a t
erthan7c anbepur chasedfor$ 2 0to$ 30.Ea rl
ierAppl esrequiremor ethan
asingl
ec hi
p. Apple/ /edoe s
n’trequirea nya dditionalso4wa reorha r
dwa re;t
he
l
owe rc
asedi s
playcapa bi l
it
yi sbuiltin. Forse r
iouss tudya nde xplorat
ionoft ex
t
i
nput/outputme thods ,lowe rcas
ec apabi l
ityi
se ssenti
a l
lyrequire
d.

⇢eI
nputVe
ctor
:KSW
⇢eby tepa ir$38,$39 c ons
tit
utest hema ininputv ect
ora ndi sg enerall
y
labe
ledKSWf orKe yboa r
di nputS Witch.Lik eCSW( theCha racteroutputS Wi t
c h)
,
KSWisusedt oswi tchi nputt oBAS ICandt heMoni torfrom di Le r
ents our ce
s. As
isevi
dentfrom t hef acttha tanINPUTs tateme ntwi l
lr eadaDOSt e
x tFlea ndt he
acti
onoft heEXECc omma ndont extFl e
s, t
hek e y
boa rdisn’ ttheonlypl acefrom
whichtheAppl ec a
nobt ai
nAS CI
Idata.
Whe ny ou’ r
ewr it
inga na ss
embl y-langua g
epr ogra mt hatne e dsas in-
gle-
chara
c te
ri nputf r
om t heout si
dewor ld,theus ua lpr ocedur eistodoaJSR
RDKEY($FD0C) andt henus ethev al
uet hatisreturne dint heAc cumul ator.
Aswedi dwi thCOUT( $FDED),le
t’
ss eewha tRDKEYdoe stog ett
ha tc haracter
.
Toe xaminet her outine, e
nt ertheMoni torwi ththeus ualCALL-151a ndl i
stthe
codebytyping$FD0C<RETURN>.
Here,shownwi thlabelsandc omme nts,i
st hec odea tthatloc a
tion: 1

FD0C- A4 24 RDKEY LDY CH ; Get horizontal cursor


FD0E- B1 28 LDA (BASL),Y ; Get character from screen
FD10- 48 PHA ; Store it
FD11- 29 3F AND #$3F ; Clear bits 6,7
FD13- 09 40 ORA #$40 ; Set bit 6 (flash)
FD15- 91 28 STA (BASL),Y ; Put on screen

1[
CT]⇢ec
odes
howni
sfora
nAppl
eIIorAppl
eIIPl
us.⇢ec
odef
ora
nAppl
e//
eis
qui
teabi
tdi
Ler
ent
,butt
hee
ntr
ypoi
nt tRDKEYa
sa ndKEYINa
ret
hes
ame
.

]
2
92 As
sembl
yLi
nes

FD17- 68 PLA ; Get the original character


FD18- 6C 38 00 JMP (KSW) ; To ‘real’ input
FD1B- E6 4E KEYIN INC RND ; RND = RND + 1
FD1D- D0 02 BNE KEYIN2
FD1F- E6 4F INC RND+1
FD21- 2C 00 C0 KEYIN2 BIT KBD ; Check for key
FD24- 10 F5 BPL KEYIN ; No, again
FD26- 91 28 STA (BASL),Y ; Restore old character
FD28- AD 00 C0 LDA KBD ; Get input character
FD2B- 2C 10 C0 BIT KBDSTRB ; Clear strobe
FD2E- 60 RTS ; Return with character

On e ntr
yt oRDKEY t heFr stthreei nstr
ucti
onsr eadt hec harac
teront he
Appl es cr
eena ndputitont ot hes t
ack.Re me mberthatwha ty ous eeon-scr
e e
nis
ther epresentati
onofaby tes tore
di nt heme moryr angeof$400 t o$7FF.To
de t
ermi newha tby t
ecorr e
s pondst oas c
re enposit
ion, y
oune e donl yloa
dt heY-
Re gi
sterwi t
ht hehorizonta lcursorpositi
on( CH=$24)a nda ddt hi
soLsettothe
ba s
ea ddr es
sfort hecurrentl ine.⇢i sba seaddressisa l
way sstor e
di n$28,$29
(BASL, BASH).
Onc ethee xi
sti
ngc har acteron-scre
e nha sbeenr eadands tored(sowec an
puti tba ckon- s
creena 4ert hei nput),thene xtthreeinstr
uc tionsha vethene t
eLectofput ti
ngaSa s
hi ngc ha rac
teront hes c
reene quiva
lentt ot hecharacte
r
thatwa son- s
creeninthec urrentcursorpos it
ion.
⇢ea cti
onoft heANDsa ndORAsma ynotbei ntuiti
vel
yobv i
ous.Let
’scon-
siderthisexampl e:

He
x Bi
nar
y Charact
er
Or
igi
nalc
ha r
acte
r: $C1 %1100 0001 A(Norma l
)
AND: $3F %0011 1111 cl
earbit
s6,7
Fi
rstr
esul
t: $01 %0000 0001 A(I
nv e
rse)

ORA: $40 %0100 0000 se


tbit6
Fi
nal
res
ult
: $41 %0100 0001 A(Fl
ashi
ng)

Re membe rthattheacti
onoft heANDi stoc l
ea ra nybitsintheAc cumul at
or
thata r
ema tc
he dbya0i nthema s
kv al
ue.Bi tsint heAc c
umul atorma t
che dby
1sint hemaska r
el e4unchange d,whe t
hert heya r
e0 sor1 s.
AnORA,ont heotherhand,s e
tsto1a nybi tsint heAc c
umul atorthata re
ma t
c hedbya1i nt hema skvalue .Bit
sint heAc cumul atorma t
c hedby0 si nthe
ma ska r
ele4unc hanged.
Youmi g
htwonde ratFr s
twhyt woi nstruct
ions –t heANDf oll
owe dbyt he
ORA–we reneeded.A4e rall
,int hepreviousc hapt erdi dn’
twec hangec ontrol
character
stoinve rs
einjustones tep?Whynotj ustus eadi Lerentma skvalueto
getSa shi
ngc haract
ers?⇢ea ns werliesint hedi Le rencesbetwe ent hebitpa t-
ter
nsf orinve
r s
ea ndSa s
hingc ha ra
cters
.Al linversec ha r
acte
rsha v ethetopt wo
3
0.I
nte
rce
pti
ngI
nput 2
93

bitsc lear( bits6a nd7 ) .Fla shingc ha r acters,ont heot he rha nd,ha veonebi t
clear( bit7=0 )a ndt heot he rs et( bit6=1 )
.
Whe nt hec ursori sonac ha ractera ndt hec ha rac t
e ri st obec onv ertedt o
Sa s
hi ngt empor ar i
ly, wemus tnotonl yc leart hehi ghbi t(a tlea stf ora ll“nor ma l

text),buta lsomus tonoc c
a sions etbi t6 .⇢i sc ombi na tionofas eta ndac l
ear
requi restwoope rations .
Onc eRDKEYha st husputaSa shingc ha rac teron- scre ent os howt hec ur sor’
s
locat i
on,t hec ha racte ror i
gina l
lyont hes cree ni sr e trieve df rom t hes tacki n
pr e
pa rati
onf ort hej umpt oKEYIN( ort oa nyot he rinputr out inet ha twi llwa nt
tor e storet heor igina lc ha rac teri fnone wc ha racteri se nte red) .Fi na lly,the
ac t
ua lindi rec tj umpv iaKSWi sdone .
InCOUT( $FDED) ,t hej umpv i
aCSWwa sma dei mme di ately .⇢i se x trapor -
tioni nRDKEYpr ecedi ngt hea c tua ljumpe xpla i
nst hepr ese nc eoft hec urs oron-
scree ndur ingat ext-Fler ead. Al thoug hDOSi sha ndl ingt hei nputa tt ha tpoi nt,
thec alliss tilldonev iaRDKEY,a ndt hust hepr es enceoft hec ur sori ss ti
lls ome -
wha tuna v oida ble .
IfDOSi snota ctive ,KSW o rdina rilypoi nt st oKEYIN( $FD1B) .KEYINi sthe
rout iner espons iblef org ett
ingc hara ctersf rom t hek ey boa r d; itthusi nv olv esthe
ke yboa rdme mor yha r dwa re( $C000 a nd$C010)di rec t
ly .I fi nputwa sf rom a
mode m ors omeot here x t
erna lde vic ei nstalledi nape riphe rals lot ,KSWwo uld
pointt o$Cnxx,whe r en i st hes l
otnumbe ra ndxxi st hei nputr out inee ntry
point .Be for ec ons ide ringt heunus ua ls i
tua tions ,let’ss eewha tha ppe nsmos tof
thet ime ,whe nKSWpoi nt stoKEYIN.
KEYINF r sti ncre me ntsther andom- numbe rby tepa ir, $4E, $4F. ⇢i si sapa rt
ofthel oopt ha twi llber epe atedunt i
l ak eyi spr e ssed. ⇢et he or yi stha tt hepa s
-
sageoft i
mebe twe e nk eypr ess esi sr andom.⇢i sby tepa iri sus edpr ima ri
lyby
Integ erBAS IC. Appl eso4ha sit sownr andom- numbe rr egist ersa ndr out ines .
A4e ri nc re me nt i
ngt her andom by tepa ir, KEYIN2t he ndoe st hea ctua lke y
-
boa rdc hec k,r epe atingt hepr oc essbyg oingba c kt oKEYINi fnok e yha sbe en
pr e
s sed.Re me mbe rt ha tt heBITi nstruc tionma ke sthet e stpos siblebys ett
ing
thes ignSa goft heS tatusRe g istere qua l tobi t7oft hec ha rac t
e rv aluede tec tedat
thek eyboa r d( $C000) .BPLt husc anbeus e dtode t
e ct(byf ailing )whe nbi t7g oes
high( bit7=1 ),indi ca t
ingak e ypr ess.
Onc eak eyha sbe e npr e sse d,thev a l
uei nt heAc cumul atori sputba cki nto
scree nme mor y .Re me mbe rtha tthisi st hev alueoft heol dc ha ract erpr esuma bly
ther e,no tt hene wc ha ra cterinput .I fthec ha ra ctere nt eredi sar ighta rrow,t his
signiFe st ha twewa ntt omov et hec ur sorov ert hedi spla ye dc ha r acterwi thout
cha ng ingt ha tc ha rac ter.⇢eLDAKBDi swha tput st hei nputc ha rac terint ot he
Ac cumul ator ,a twhi c hpoi ntt hes t
r obei sc le aredbya cc essing$C010a ndt he
Fna lr e t
ur ni sdone .⇢ec allingpr og ra mt he nha st heopt ionofpr i
nt ingt he
inputc har ac tert ot hes c re e
n.

]
2
94 As
sembl
yLi
nes

Ot
herI
nputSour
ces
KSWdoe
snota
lwa
yspoi
ntt
oRDKEY.I
nfac
t,itdoe
sn’
tpoi
ntt
her
ewhe nDOS
i
sins
tal
le
d.Wit
hDOSboote
dandact
ive
,ent
ertheMonit
orandt
ypein:
38.39 AA55.AA56<RETURN>

Yous
houl
dge
t:
0036- 81 9E
AA55- 1B FD

You’llseet ha tKSWa c tuall


ypointst oDOSa t$9E81,whi cht heneventua l
ly
pointstoRDKEY( $FD1B)a t$AA55,$AA56.Li ketheout puts ystem,DOSi sr ather
perma nentl
yma depa r toft heinputpa th.Anya ttempt stodi s
c onnec
tDOSby
modi f
y i
ngKSWdi rect
lywi llbeundonebyDOSi fa nyout puti sdone .DOSha sits
own i nt
e r
na li nputv ectora t$AA55,$AA56.I ta l
terst hisv ector,notKSW,a s
neededt ogaina ccesstov ariousslot
s(ort odiskFl es,asappr opr i
a t
e).
Youc ani ns tallyourownr outi
nei ntothei nputpa t
hbyme ansofapr oce-
duresimi l
artot heoneus edi nthepre v
iouscha ptert ointer c
e pttheoutputpa t
h.
Putthelow-a ndhi gh-orde rbytesofthede s
ti
na tiona ddre ssintoKSW( $38,$39=
56,57de cima l
)a nddoac allt
o$3EA( 1
0 02decima l
).⇢i sc aus esDOSt oc ha nge
it
sownv ectorsa t$AA55,$AA56t othea ddressspe ciFed,a ndt he ntorest
or eKSW
sothatitpoint st oDOSa g ain,usual
lyat$9E81.2

InAppl eso4t hiswoul dt aketheform:


10 POKE 56, LB: POKE 57, HB: CALL 1002

I
nthi
sexampl
e,LBandHBaret
hel
ow-andhigh-
orderby
tesoft
hede
sti
na-
t
iona
ddre
ss.
Inass
emblyla
ngua
ge,i
twoul
dlookl
iket
his:
LDA #LB
STA $38
LDA #HB
STA $39
JSR $3EA

Justasout putha stwoba si


cclas
sesofr outines,t
herearetwoma intypesof
inputr outines–t hos
et hatint
erceptincomi ngc hara
ctersanddos omes ortof
processing,andt hosethatenti
rel
yr e
placethei nputrouti
nesalre
adybe i
ngus e
d.
Ifyoua redoi ngthelatt
er,thi
ngsarefair
lysimpl e.Onceinst
all
ed,yourrouti
neis
enti
relyi nc hargeofg ett
ingtheinputc hara c
ter;whent ha
tc hara
cteris“got,

yourr outinee ndswitha nRTSt opasscontrolba cktothecall
ingprogram.⇢i s
approa chissimilartoourc ust
om outputrout i
nesfrom t
hepr e
v i
ouschapter
.

2[
CT]J
ustl
iket
heout
putv
ect
or(
des
cri
bedi
ncha
pte
r29
),whe
nus
ingPr
oDOSy
ouc
an
dir
ect
lycha
ngethei
nputve
ctorat$BE32,$BE33topoi
ntt
oyouri
nputr
out
ine
.Se
e
chapt
ers
ixofI
nsi
det
heApp
le//,
ebyGa r
yB.Litt
le
.
3
0.I
nte
rce
pti
ngI
nput 2
95

⇢eFr s
tcl
assofi
nputrout
ine
s,i
nwhichincomi
ngchar
act
ersaretobe
i
nte
rce
pte
d,mustbehandl
edsli
ght
lydi
Ler
ent
lythanourout
putexpe
riment
s
wer
e.

I
nte
rce
pti
onRout
ine
s
Whe nwewe rede al
ingwi t
ht heoutputpr ocess,t
hepoi nta twhi chwei nter
-
ceptedtheda taSowr e
allydi dn’tma t
ter.Becauset hec al
li
ngpr og raml oadsthe
Ac c
umul at
orwi tht hecha ractertobeout put,thec haractercanbee xaminedat
anypointa l
ongt hewa y.Wi thi nput,t
hec haracterinputisnota va i
lableunti
lthe
veryendoft hepr ocedure ,whe ntheRTSr et
urnsc ontroltothec a l
lingprogram.
Fortunate
ly,thereisarelativelyeasywaya roundt hisli
mi tati
on.
Inbotht heinputa ndout puts ys
tems ,t
hel inksint hepr oce s
sa redoneby
me ansofas eri
esofJMPs( a
soppos edt oJSRs )
.You’ llrecal
lf rom ourout put
inte
rcepti
onf rom thepr eviousc hapte
rt hattheFna lexitfrom ther out i
newa sa
JMP$FDF0( orwhe rever)a 4erthepr oce
s si
ngwa sdone .
Wi t
hinput ,
thes ecretistodoaJSRt oKEYIN( orwhe rever)Fr standthendo
yourpr oces
sing,followedbya ne ventualRTSt ot hec al
lingpr ogr am.Forour
Frstexperi
me nt,we ’
llt
rywr i
tingar outinetoc onvertallincomi ngc haracte
rsto
lowercase
:
1 ********************************
2 * AL30-SIMPLE CASE CONVERTER *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 KEYIN EQU $FD1B
9 *
0300: 20 1B FD 10 ENTRY JSR KEYIN
0303: C9 C1 11 CMP #$C1 ; ASCII 'A'
0305: 90 02 12 BCC DONE
0307: 09 20 13 MASK ORA #$20 ; %0010 0000
0309: 60 14 DONE RTS

I
nt heory,anythingy out ypei nnow s houldbedi s
play edinlowe rc
a se
.
Nume ri
ca ndcontrol c
ha ract
e r
ss houl dbeuna Lec
t e
d.⇢er outineworksbyFr s
t
cal
li
ngKEYIN, whichg etsac haracterf r
om thek ey
boa r
da ndput sitintheAc cu-
mulator
.Att hatpointourr outinee nsuresthatwe ’
vegotac a pit
alle
tter,rather
thananume ri
corc ontrolcha ract
e r.Ifwedon’ tha v
ea na lphabeti
cc haracter
val
uelessthan$C1,the nther outines kipstoDONE.
I
fwha tweha veisa na lphabeticc haract
er,theconversiont olowe rcas
ei s
donebyf orci
ngbit5oft heAS CI Ivaluet o1.⇢ev al
uesofa l
llowe r
casec harac-
ter
saree qualt
ot hev aluesoft hec orrespondinguppe r
casele t
terspl
us3 2 .⇢i s
me a
ns,asa nASCIIc hartshowi ngbi tv al
uesreve
als,t
hatcapitallet
ter
sha vebi t5

]
2
96 As
sembl
yLi
nes

cl
earandlowe r
casel
ette
rshavebit5set
.Li
ne13ofourrouti
nese
tsbi
t5,t
hus
conve
rti
ngt hechara
ctertolower
case
.Fina
ll
y,l
ine14re
turnsust
othecal
li
ng
prog
ram.
Ourr outi
neshouldworkfrom wi
thi
nApple
so4.
Trythi
s:3

6 INPUT “ENTER A STRING:”


10 POKE 56,0: POKE 57,3: CALL 1002
20 INPUT I$
30 PRINT I$
40 PRINT CHR$(4); "IN#0": REM DISCONNECT ROUTINE

Don’
tbes
urpr
ise
dift
hispr
ogram does
n’twor
k.4

Tr
ychang
ingl
ine20t
olookli
kethis
:
20 GET A$: PRINT A$;: IF A$ <> CHR$(13) THEN I$ = I$ + A$: GOTO 20

Now r un thepr og ram.⇢er esultst hi


st imes houldbemor el ikey ou
expe cte
d. Line3 0i sus e dt oc onFr mt hef acttha tthelowe rca s
eda t
awet ypedin
onl ine2 0a ct
ua l
lyma dei tt oAppl eso4.
⇢eque st
ionnowi s,whydi dn’tt heFr stprog ram wor k?I nas e
ns ei tdid.If
youl ike,g oba cka ndr unt heFr stpr og r
a m wi t
houtl i
ne4 0 .Whe nt hepr ogram
ends ,goi ntot heMoni tora ndc heckt heDOSi nputv ec
tora t$AA55,$AA56.I t
shoul dindic at
etha tourr out inea t$300i sbe i
ngus ed.
⇢epr obleml iesinAppl eso4’sus eoft heGETLN( GETLi Ne )routinef orthe
INPUTs t
a te
me nt .⇢i sr out inei sus e dt oinpute nti
rel i
nesa tat ime .Al though
GETLNd oe sus et heRDKEYr out inet og etindi vi
dua lcharacte rs
,i tunf ortunatel
y
tampe rswi ththec hara cter se nteredbe f
orei tret
ur nstheda tat oAppl e so4, DOS ,
ort heMoni tor.
SpeciFc al
ly,GETLNc onv e r
tsa nyl owe rcasec haracter
sc omi ngi nt ouppe r-
case. ⇢us ,e ve
nt houg hourr outinec onv ertstheuppe rcasec harac t
ersc omi ngin
throug ht hek eyboa rdt ol owe r cas
e ,GETLN u ndoe se ver
yt hingbyc onv e
rti
ng
them ba ckbe foret hey ’
r ee ve ne choe dt othes creen.
Anot hera nnoy anc eofGETLN i st hati tconv ert
sc hara ct
erst haty ouc opy
from thes creenus i
ngt her i
g hta rrow.
⇢er easont hepr og ram wor kswi tht hene wv e
rsionofl ine2 0i st hatthe
Appl eso4GETs tateme ntus esadi rectca l
ltoRDKEYa nddoe snotus eGETLN.
Onewa yt os olvet hepr oblem oft heINPUTs t
ateme ntnotwor kingi sby
wr i
tingy ourowni nputr out inei nsteadofus ingt heGETs eque nce.⇢ee asi
est
thingt odohe rewoul dpr oba blybet oc opyt heGETLNr outinea nde l
imi na t
ethe
conv ersi
onpor ti
ons tartinga t$FD7E. 5

3[
CT]Unde
rPr
oDOS
,yous
houl
dcha
ngel
ine1
0toPOKE48690,0:POKE48691,3
4[
CT]Ona
nAppl
e//
e(unde
rDOS
)thi
spr
ogr
ama
ctua
ll
ywi
ll
wor
k.
5[
CT]I
ntheAppl
eIIa
ndAppl
eIIPl
ust
hec
odea
t$FD7Ec
hec
kswhe
the
rthec
har
act
er
sj$E0a
i ndifso,doe
sa nAND#$DF,whi
chconver
tsf
rom l
ower
caset
ouppe
rca
se.I
n
t
heAppl
e//
ethi
shasbeenrepl
ace
dwithAND#$FF,
whic
hdoesnot
hing.
3
0.I
nte
rce
pti
ngI
nput 2
97

Inst
ead,l
et’
sse
eifwecanimprov
eonthesi
mpleinputrouti
nejus
tshown,
ma
kingi
talit
tl
emoreSexi
ble
,wit
houtre
wri
ti
ngtheGETLNrout
ine.

Some
thi
ngMor
eUs
eful
:Lowe
rca
seI
nput
Alt
houg hther outi
nejustgiveni
ll
ust
rat
est heconceptofi nte
rcepti
nginput,
i
t ’
snotreal
lytha tusef
ulbecauseitpr
ovi
desnowa yofswitc
hi ngbe t
wee nupper
-
caseandlowe rc
a sele
tte
rsatwi l
l.
Whynotc reateaninputr outinethatall
owsus
toshi4betweenuppe r
caseandlowe r
cas
elet
tersaswei nputthe m?Aswedi df
or
theoutputroutineinthepreviouschapt
er,
we ’
llFrstma kealistofwha twewant
therouti
netodo:
1. ⇢er outines houldall
ow nume ri
candc ontrolcharacterstopa s
st hr
ough
unalt
ered.
2
. ⇢er out
ines
houldbese
tupsucht
hatpr
ess
ing<ESCAPE>onc
ewhe
nint
he
l
owe
rca
semodewill
shi4onl
ythene
xtl
et
tert
oupper
case.
3
. Pr e
ssi
ng<ESCAPE>t
wicewheni
nthelower
cas
emodeshouldshi
4alls
uc-
c
ess
ivei
nputt
otheupperc
asemode(
thi
sissome
ti
mesc
all
ed“ca
pslock
”).
4. Pr e
ssing<ESCAPE>onc ewhe nintheuppe r
casemodes houldr e
turnthes ys
-
temt othel owe rcasemode .
⇢es y st
em ofus i
ng<ESCAPE>a sas hi4k eyi ssome wha tsta
ndard.Be f
ore
goingont ot helisting,thoug h,let’
sthinkal i
tt
lemor ea boutwha tisne ededto
imple mentt hissyste m. FirstoL,we ’
llneeds omewa yt or eme mberwhi c
hmode
(lowercaseoruppe r case)we ’r
ei n.⇢emos tdirectwa yofdoi ngthisistous ea
Sa g
,whi c
hwe ’
llca llCSFLG ( Ca SeFLa G) .Toa v
oidaz ero- pageconSict,we ’
ll
reserveapl acefort heSa ga tt hee ndofther outine.
Inor dert ofulFll t
her e quireme ntst
a t
edi nitemt hr e
eonourl is
t,wene edto
storethev alueoft hel astc hara c
terinput–t hatis,thec ha r acterj
ustbeforethe
onec urrentlybe ingi nput ,ina notherstoragelocation.⇢i swi l
lall
owust otel
l
whe n<ESCAPE>ha sbe enhi ttwi c
einar ow. We ’l
lcallthisl oc at
ionLSTCHR( LaST
CHa Ra cter
).
⇢eg ener a
lpa tternwi llbet odos omebr ieftestse ac ht i
meac haracte
ris
inputa nd,i fnoc onv ersioni sne ces
sary,topa sstheuppe rcasele
tterthrough
una l
tered.Onl ywhe na n<ESCAPE>s equenc eiscomi ngt hr oughorwhe nwe ’r
e
inthel owe rcasemodewi llwee vera l
te
rt heinputc harac ter.He r
e,then,isthe
impr ovedlisting:
1 ********************************
2 * AL30-LOWERCASE INPUT ROUTINE *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300

]
2
98 As
sembl
yLi
nes

7 *
8 KEYIN EQU $FD1B
9 ESC EQU $9B
10 *
0300: 20 1B FD 11 ENTRY JSR KEYIN ; GET KEY
0303: 48 12 PHA ; SAVE CHAR
0304: C9 9B 13 CMP #ESC
0306: F0 19 14 BEQ ESC1
15 *
0308: AD 3F 03 16 CHAR LDA LSTCHR
030B: C9 9B 17 CMP #ESC
030D: F0 0D 18 BEQ XFER ; CAP THIS CHAR
19 *
030F: 2C 40 03 20 CASE BIT CSFLG
0312: 30 08 21 BMI XFER ; CAPS
22 *
0314: 68 23 CVERT PLA ; RETRIEVE CHAR
0315: C9 C1 24 CMP #$C1 ; ASCII 'A'
0317: 90 02 25 BCC X2 ; DON’T CHANGE
0319: 09 20 26 ORA #$20 ; SET BIT 5
031B: 48 27 X2 PHA ; PUT CHAR BACK
28 *
031C: 68 29 XFER PLA ; RETRIEVE CHAR
031D: 8D 3F 03 30 STA LSTCHR ; LSTCHR = CHR
31 *
0320: 60 32 DONE RTS
33 *
0321: AD 3F 03 34 ESC1 LDA LSTCHR
0324: C9 9B 35 CMP #ESC
0326: D0 10 36 BNE CASE2
37 *
0328: A9 80 38 LOCK LDA #$80 ; BIT 7 = 1
032A: 8D 40 03 39 STA CSFLG ; UC
032D: D0 ED 40 BNE XFER ; ALWAYS
41 *
032F: 68 42 UNLOCK PLA ; PULL CHAR
0330: A9 00 43 LDA #$00
0332: 48 44 PHA ; CHR = NULL
0333: 8D 40 03 45 STA CSFLG ; 0 = LC
0336: F0 E4 46 BEQ XFER ; ALWAYS
47 *
0338: 2C 40 03 48 CASE2 BIT CSFLG
033B: 10 DF 49 BPL XFER ; LC NEEDS NO ACTION
033D: 30 F0 50 BMI UNLOCK ; UNLOCK UC
51 *
033F: 00 52 LSTCHR DFB $00
0340: 00 53 CSFLG DFB $00 ; DEF = LC; #$80 = UC
54 *
0341: 9C 55 CHK

A4e rasse
mbl i
ngandi nst
al
li
ngthisrout
ineat$300,t
rytheAppleso4pro-
g
r a
m wi tht
healte
redli
ne2 0agai
n.⇢isti
mey oushouldbeabl
etoe nt
erast
ri
ng
c
onta i
ningbothuppe r
caseandlowerc
a s
elet
ters
,withthe<ESCAPE>k e
yfunc-
t
ioningasdescr
ibedintherequi
rement
slis
t.
3
0.I
nte
rce
pti
ngI
nput 2
99

Not etheus eofEQUt ode FneESCi nl i


ne9 .⇢el a belESCi sus eda sav alue
rathe rt hanal oca ti
on. ⇢i swa yy ouc a nc hang ethek eyus edf ors hi 4byc ha ng-
ingt hev aluee qua tedi nl ine9 .
Al ooka tt hes our cel ist
ingr eve al
swha t’
sg oi ngon.Fi rs t,aJSRKEYINi s
donet og etac ha rac t
erf rom t hek eyboa rd. KEYINh a ndl estheSa shi ngc ur sora nd
key boa rdha rdwa ref orus .Ne xt,t hei nputc ha racte ri spus he dont hes tacks o
we ’
ll bef reet ous et heAc cumul at orifne c essarywi thoutl osingt hei nputc ha rac-
ter.
Ne x t,at e s
ti sdonet os eewhe thert hec urrentc haracte risa n<ESCAPE>
cha racte r.Ifso,abr a nchi sdonet ot he<ESCAPE>- ha ndl i
ngr out ine, ESC1( line
34). ⇢eFr s
tt hingdonea tESC1i st os eei fthel astc ha rac t
erwa sa n<ESCAPE>a s
we l
l, inwhi chc a seLOCK( li
ne3 8 )s etsc aps-lockmodebyput t
inga$80i nCSFLG.
Ifnot ,t he nCASE2( line4 8)c heck sCSFLGt os eewhe the rwe ’rec ur rentlyi nl ow-
erca seoruppe rca se.
Tos impl ifyt hist est,we ’veus edav alueof$00f orCSFLGt os ig nifyt hel ow-
erca semode .Av a l
ueof$80s igni Fest heuppe r ca
s emodei noure x ampl e. ⇢e se
value swe rec hos ent oa ll
ow t heus eoft heBIT c omma nd.Be c auset heBIT
instruc tionc ondi t i
onst hes ignSa g( bit7 )oft heS t
a tusRe gistera c cordingt obi t
7oft heme mor yl oca t
ionr eferenc ed,wec a nt estt hes tatusofCSFLGwi thout
actua llyha vingt ol oa dt heAc cumul atorwi tha nythingt odot het est.
CASE2u sest heBITi nstructiont ot e
s tbit7ofCSFLG. Ifbit7i sc l
e ar,we ’rei n
lowe rc asemodea nda lltha tnee dst obedonei stopa sst hisFr st<ESCAPE>c ha r-
actert hr oug ht oXFER, whe reitwi l
lbes toredi nLSTCHR.⇢a twa yt he<ESCAPE>
canbeus edtos ig nifyas hi4t ouppe rc as
ei fthene xtc ha ra c
teri sal e t
ter.
Ifbi t7i ss et,t henwe ’
rei nuppe rcase,a ndwene edt o“ unl oc k”t heuppe r-
casemode .UNLOCKd oe st hisbyput t
inga0v aluei nCSFLG. You’ llalsonot icet hat
thec ur rentc ha rac t
e risc ha ngedf rom a n<ESCAPE>t oanul l.⇢i si sdones ot hat
a4e rdown- shi4i ng ,wec a nstil
lpr ess<ESCAPE>onc emor etoc apitalizet hene xt
l
e t
te r.Ifweha dn’ tc hang e dtha t<ESCAPE>t oanul lwhe nwedown- shi4e d, we ’d
beba cki nc aps -l
oc kmode .
Fort hene x tpa sst hroug h,l et
’ss eewha tha ppe nswi thanon- <ESCAPE>
cha racte r.We ’llre sumet rac i
ngt her outiner ighta 4e rENTRYha sde cide dtha tt he
curr entc ha racteri snota n<ESCAPE>c hara ct
e r
.
⇢ene xts e ctioni sCHAR,whi chc he ckst os eewhe t
he rt hel astc ha r
a cter
throug hwa sa n<ESCAPE>c ha r
a cter.Ifs o, wene edt oma kes ur et hec ur rentl et-
teri sc apitalized,e v ent houg hwe ’repr esuma blyi nt hel owe rca semode .⇢i si s
easilydone ,thoug h:pr og r
a m Sow pr oce edsdi rectlyt oXFER.Re me mbe r, XFER
simpl ys t
or esthec ur renti nputc ha r
a cterinLSTCHRa ndt he nr etur nst ot hec a ll
-
ingpr og ram.I nt hisc a se,be caus ea llcha ractersg ene ratedbyKEYINa rea lwa ys
uppe r case( exc e
ptont heAppl e/ /e),we ’
ll j
us tleavet hec apitalle tt
e rinput“ asi s”
andpa ssitthr oug h.

]
3
00 As
sembl
yLi
nes

Ifthela
stchara
cte
rwa snotan<ESCAPE>,progr
am Sow c
ont
inue
stothe
CASEse
cti
on,whic
hde c
ide
swhethe
rtoconvertthecha
rac
terc
omingthr
oug
hby
check i
ngt os eewhe therwe ’reinuppe rcas
eorlowe rcasemode .
CASEu sestheBITi nstruct
iontodot histe
st.Ifwe ’
reintheuppe rcasemode
(bi
t7=1 ,the r
e f
or eBMIwor ks)
,noc onv er
sionoft heincominguppe r c
a selet
ter
isne ede da ndt hepr ogram br anchesdi r
ectl
ytot heXFERr outine.XFERr etri
eves
theor iginali nputc harac t
ers t
oredont hestack,upda t
esLSTCHR( sinc ethiswi l
l
nowbet he" l
astc ha racter”ont hene xtpa ssthrough),andt henr eturnst ot he
ma i
nc all
ingpr ogra mv i
atheRTS.
Ift heCSFLGwa ss etto0 ,li
ne2 1woul dnotbr anch,andt heCVERT( Con-
VERT)r out i
newoul dbee ntered.CVERTF rstretri
evestheinputc ha racterfrom
thesta cka ndt he nc hec kstos e
eifthec haract
erha sanAS CIIv al
ueles stha nthat
ofthel etterA. I
fs o,thec har ac
tercomi ngthroughi sanumbe rorac ont rolchar-
actera nd,a ss uc h,s houldnotbec onverte
dt olowe rcas
e.Ifsuchac haracteris
detected,t her out inej umpsov erthec onversi
onr outinetoline2 7,whi chput s
thec har act
e rba ckont hes tack(whe reXFERe xpectstoFndi t
)a ndg oe sthroug h
totheXFERs ection.
Ift hec ha racterha sa nAS CIIva l
uee qualtoorg reat
erthant hatoft hel et
ter
A,t hent heORA#$20s et
sbi t5,thusc onverti
ngt helett
ertol owerca se.Att hat
pointt hene wc ha racterisputont hes tackfortheXFERr outi
ne .

Conc
lus
ion
⇢i sisde Fni telyoneoft hos epr og ra
mst ha ttakeaSowc ha r
tt ode sign,so
don’ tfe eldiscour agedi fever
y t
hi ng ’snoti mme diatel
yc le
a r.Cons ide ringallthe
pos si
bl es i
tua ti
onsof<ESCAPE>s e que nc e
sa ndc ur re
ntc ase,itma yt akeal i
ttl
e
timebe forey ouf eelcomf ortablewi thi t
.
Ev eni fthepr ogram ne verma k esc omple t
es ense,reme mbe rtha ttheimpor -
tantt hi nghe rei stounde r
standt hewor ki
ng soft hei nputs ystemi ng eneral,
rathert ha nthi spa rti
c ularl
itt
ler out ine .
Ofc ourse, thebe stwa ytounde rs t
andwha t’
sg oingoni stoe xpe rime ntwith
yourownr out ine s
.Doi ngt hisa lwa yshe l
psbr ingoutt her ighta ndwr ong
assumpt i
onsa boutt hewa ywet hi nkt hingswor k. Youmi g htwa nttot rywr it
ing
theg e ne ral
izedi nputr outines ug ge stede ar
lier
,orpe rhapsy ou’reoneoft hose
pe oplewho’ vehook edupawi r
ef rom t he<SHIFT>k e
ytopus hbut ton2 .I
fso, se
e
whe the ry ouc a ni mpr ov etheinputr out i
netoa llowy ourse l
ft ous et he<SHIFT>
ke ya swe l
l.Anot heri nterest
ingpr oje ctwoul dbet owritey ourownKEYINr ou-
tinet obeus edbyt hei nputrout ine ,the nseeify ouc ang ene r
a t
eadi Le re
ntk ind
ofc ur sor–ors olvet hepr oblem oft hec urs
ornotl ook i
ngqui ter ightwhe nit’
s
onal owe r
c a
s eletter.
3
1
3
1. Hi
-Re
sChar
act
erGe
ner
ato
r
Ap
ril
198
3

⇢i scha pte
rs t
a r
tsadiscuss
iona bouthowt owr i
teyourownhi -
rescharac-
terg enerator,andt hushowt ousetex tont hehi-resscr
eeni nyourowna ssem-
bly-languag eprograms .
⇢edi s cuss
ionwi l
lcoveranumbe rofpoint s
.Firs
t,we’l
llooka ttheme mor y
ma ppi ngoft hehi-ressc
re e
ntose ewha tconsiderat
ionsmus tbema detoputthe
dataf orthea ppropriat
ec har
acte
rsont hes cre
e n.
Ne xt
,we ’l
llooka tt
hec odene e
de dt oint
e rce
ptthechara c
tersbe i
ngoutput
tot henor ma ltextscreen,andhow t hisinf
or mati
onc anbeus edt oac t
ually
impl eme ntthehi -
rescharacte
rgenerator .
Lastofa ll
,alis
tingforac ha
ractere di
torwi l
lbepresent
e d,soy oucanma ke
upy ourownc haract
ers et
sore v
enpr oduc especia
lc haract
ersf orunus ual
graphi cseLec t
s.

Te
xta
ndHi
-Re
sSc
ree
nMa
ppi
ng
⇢eFr stcons i
de r
a t
ioni nc rea
tingourc hara cte
rg e
ne rat
orist het opicof
wha tactuall
ywi l
l ber equiredtoputac haracteront hehi -resscree
n. Inpr evious
chaptersweha v es eenhowe a
chdotont heg raphic sscreenisr e
latedtoa ni ndi-
vidualbitwi thinaby t
eofme mor yassignedt othehi -resdi spl
ay.Ine arli
err ou-
tineswec r
eatedg raphicsbypl ott
ingdot susingt her outinesbuiltintoAppl e so4
BAS IC.⇢i stimet heappr oachwi l
lbes ome wha tdiLe rent.
Toc r e
ateac ha rac
teront hehi -
ress c
reen,a ne nti
rea rrayofdot swillhav eto
bet urnedon.Al thoug ht heHPLOTr out i
nesofAppl e
so4c ouldbeus e
d,itt urns
outt hereisamuc hs impl erwa ytoa chievethede siredr esul
t.⇢i sme thodi s
ba s
edons imilaritiesbe t
we enthenor ma ltextdisplaypa gea ndthehi -
resg r
a ph-
icsdispl
a ypa ge.Tof ullyunde rst
andt histechnique ,thoug h,abr i
efov erviewof
thes cr
eenme mor yma ppingwi l
lber equire
d.
Ont heAppl e,textdis pl
ayisnorma l
lyconFne dt owha tiscall
edt extdisplay
pa g
e1 .⇢i sdi splayc orrespondst oabl ockofme mor yi nthea ddressrang e$400
to$7FF( 1024to2 0 47de cimal)
.Ac ha ract
erispr i
nt edont hescree
nbys toringa
singleby teint hisme mor yrange.⇢ec omput e rha rdwa rethent akesc areof
conv er
tingt hiss toredc ha ra
cterintoav i
deoi ma g eony ourmoni t
orort elevi
-
sions e
t.

]
3
02 As
sembl
yLi
nes

Li
ne# Addr
ess(
hex
) Addr
ess(
dec
) ⇢eme mor yf ort hes cr eendi s
pl ayi snot
0 400 1024
1 480 1152 ma ppe di nas impl e,c ont inuouspa ttern.⇢a t
2 500 1280 ist os a y,i fy ouwe ret oFl lme mor ys e que n-
3 580 1408
4 600 1536
tiallywi thac e rt
a i
nv alue,t hes c r
e eni ma ge
5 680 1664 woul dnotbec hang edi nal i
ne -by -
line ,c ha r
-
6 700 1792 ac ter-by -cha racterpa ttern.I nstead,ar ather
7 780 1920
8 428 1064 unus ua lpa tternwoul dbef ol lowe d.⇢et able
9 4A8 1192 atl e4g ivest hea ddr essoft heFr stc ha ra c
ter
10 528 1320
11 5A8 1448
one achl i
neoft henor ma lte xtdi splaypa g e.
12 628 1576 Youma ya lsowi sht ol ooka tpa g e1 6oft he
13 6A8 1704 Ap pleI IRe ferenc eMa nua lf oramor ec om-
14 728 1832
15 7A8 1960 ple tec ha rt.
16 450 1104 Youma yr ecallf rom e arlierc ha pterst hat
17 4D0 1232
18 550 1360 itwa snotne ce ssaryt oc al
cul atet hebe ginni ng
19 5D0 1488 addr ess( some time sc alledt heb as ea ddre ss)of
20 650 1616
21 6D0 1744
ea chl ineour se l
ve s.I ns tead,wec an us ea
22 750 1872 Moni torr out i
nec alledVTAB( $FC22) .
23 7D0 2000 Whe nt hisr out inei sc alled,i tt ake st he
va lues tor edi nl ocation$24( ca l
ledCVf orCur -
sorVe rt
ic alpos ition)a ndc alculatest heba sea ddr essoft hel i
nec or r
e spondi ng
tot hatv e r
ticalpos ition. CVi sa ssume dt obei nt her a ng eof$0 t o$17( 0t o2 3
de c
ima l)whe nVTABi sc al
led.
⇢i si swha tCOUT( $FDED)d oe swhe ne vert hec ur sormov e stoane wl i
ne ,
sucha swhe n<RETURN>i spr essed, orwhe naVTABc omma ndi sdonei nBAS IC.
⇢eba sea ddre ssi sre turne di naz er o-pa gepoi nte rca l
le dBASL, BASH( $28, $29=
Ba s
eAddr esSLowby teandHi ghby t e).
AtFr stg l
a nc e,the r
ema ys eemt obet oof ewhor izont al r
owst or e
pr ese nta l
l
19 2l i
nes .Howe ve r,ify oul ooka tt heFg ureont hene x tpa g e,int hemi ddle
you’llseeabl ow- upofoneboxoft hema p. Ea chofe i
g htl ineswi thint heboxi s
labeledwi t
honeofe i
g htv alues.Wha tt hisme ansi st ha te achboxont hema in
char tactua ll
yr epr ese ntse i
g hts cree nl ine sont hedi spla y.Twe nty-fourbox es
time se i
g htline si ne achboxg i
vesust het otalof1 9 2s cr eenl ine s.ToFndt he
basea ddr essoft het hirds creenl ine ,fori ns tanc e,youwoul da ddt hec orre cti
on
fort het hirdl inewi thinabox( $800)t ot heba sea ddr essf ort hepr ima rybox
($2000) tog e
tt hea ctua lba sea ddres s($2800) .
Look inga tt hehor i
z ontalr ows ,you’ llnot i
c et ha tt herea re4 0by t
e st hat
ma k eupt he2 80hor izont aldotpos iti
ons .S evenbi tsi ne achby t
ea r eus e dt o
ma pt hes creendot s( 7d4 0=2 80 ).
Att hispoi nty ouma ybeg et
tingdi sc our agedt hi nk ingt ha tal otofc ompl i
-
catedc alculationsa reg oingt ober e quire dt oe v enbe gint ok nowwhe ret os t
a r
t
drawi ngourc ha rac teron- screen.Ta kehe art,t houg h!I fy oug ivei tal ittl
e
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
03

thoug ht,y ous houl dbea blet os eear ema rkabl


es imi l
ari
tybe t
we enthehi -
res
pagea ndt het e
x tpageinr egardtot heirme mor yma pping.
⇢eFr stsi
mi lari
tyisinthenumbe rofby t
esusedf oreachhor i
zontall
ineon
thes c
r e
e n. Inea chc a
se,40by tesa reuse dfora nentireline.Couldtherebee ven
mor es i
mi l
a ri
ty?Re adon!
Ify oul ooka ttheFr s
tfourl inesoft het extpage ,t
heba seaddress
esar ethe
values$400,$480,$500,a nd$580.I fy oue xaminet heFr stfourblocksofe i
ght
li
ne seac hont hehi -resscree
n,t heba sea ddr e
ssesar e$2000,$2080,$2100,a nd
$2180.
You’ llnot icethatifyoua ddt hev alue$1C00t oe achofthetext-s
creenv al
-
ues,you’ llg ett
hec orr
espondi ngba sea ddressforthehi -resscre
en.⇢i spa t
tern
continue st hroug houtallt
we nty-fourt extscreenli
ne s.
Wha ta boutt hee ig
htl i
ne sfore achbl ock?Ea chs uccess
ivelinewithina
blockc anbec a
lculatedbya ddingt hev alue$400t ot headdr e
ssfortheli
nea bove
it.⇢i swi llturnoutt obej ustperfectforc reati
ngac haract
er.
Asi tha ppens ,acharacteront henor ma ltex
ts creenisma deupofdot sina
ma tri
xs ev endot shi hbyFv
g edot swi de. Aroundt hisma tr
ixthereisabounda ry

]
3
04 As
sembl
yLi
nes

ofonedotpos i
tionone ithe rs i
dea ndonedotpos it
iona l
ongt hebot tom.⇢i s
perma ne ntlye mpt yr eg i
oni ss etupt opr ovideag ua rant e
e dsepa rat
ionbe twe en
chara cterswhe npr int edon- sc r
ee n.⇢us ,theFna lma trixisa c
tua l
lys ev endot s
widebye ightdot shi g h. ⇢eFg urebe low, f
ore x ampl e ,showst hema trixpa ttern
forthel etterA.
Ac olumnofdotpos itionsone a chs i
deoft hec ha ract
era ndar owont he
bottom a rel e4ope n. Att hispoi nt, alitt
lelightiny ourmi ndi spr obablys t
arting
tog l
ow.⇢es ev endotpos itionsa crosse achc ha r
a cterc anc orrespondt os e ven
bitsine ac hoft he4 0hi -re ss creenby tesus edone a chl ine.⇢ee ig
hthor i
zont al
rowswi l
lc or respondt ot hee ightby tesa ssi
gne dt oe a chpr imar yboxde scribed
earli
e r.
Al l this, t
he n,br ing sust ot hepr ecipice.Itistimet oma ketheme nt al l
eapt o
unde rsta ndi ngt hec onc eptofhowahi -rescha r
a ct
e rca nbec r
eated.
Inabl oc kofe i
g hts eque nt i
a lby tesofme mor y, wec ans t
orea l
loft hei nfor-
ma ti
onne ede dt oc rea teas i
ng lec ha racteront hes c
r een.Ea chby tewi llcor re
-
spondt ooneoft hee ig htr owsi nt hema trix.Ea chbi twi t
hi n aby t
ewi ll
correspondt oonepos sibledotpos it
ionwi t
hinag ive nr ow.
Fore xa mpl e,toe ncodet hel e t
te r
A,wemi ghts toret hef ollowi ngby tes: DotMatri xforA Hex Bi nar
y
$08 %0000 1000
$08, $14, $22, $22, $3E, $22, $22, $00. $14 %0001 0100
Toi l
lus tratehowt hisr ea l
lyf orms $22 %0010 0010
thele tterA,t ak eal ooka tt het a blet o $22 %0010 0010
$3E %0011 1110
ther ig htoft heFg ur e ,whi chs hows $22 %0010 0010
theses amenumbe rsi nadi Le rentwa y . $22 %0010 0010
Int her ight-handc ol umni st he $00 %0000 0000

binaryf or m ofe achnumbe r.Youc an


seewhi chbi tsar eona ndwhi cha r eoL. ⇢i srel
a tesdi rectlytohowt hec harac t
er
isdispla ye don- scre
e n. ⇢ebi tsa repl ottedinr everseor der–t hati s
,wi thbi t0i n
thele 4mos tpos iti
on.Bi t7( thehi g hbi t)isne verdi spla yedon- screen.Atmos t
,
bit7c anbeus e donl yt os hi4t heot he rdot sone - halfpos it
ion.S eet hee arli
er
chapt ersonhi -resplot tingi fy oune edal itt
ler e
freshe ri nthisa r
e a.

⇢ eCha
rac
terGe
ner
ator
Nowt oactuall
ydescri
bethecharac
tergener
atort
hatwil
lbeusedt
oputthe
a
ppr
opri
ateAS CIIchar
acte
ront hehi-
resscre
en.
⇢epr ocessi
twi l
luseisasf
oll
ows:
1
. Aroutinewillbehookedupt otheoutputve
ctort
ointe
rcepte
achcha
rac
ter
t
obepr
int
e dtothenorma lte
xtscr
een.
2
.I ft
hechara
cteri
sacontr
olchar
act
er,nos pe
cia
lpr
oce
ssi
ngwi
llbedonea
nd
t
hec
harac
terwi
llbepas
sedontoCOUT1($FDF0).
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
05

3.I fthec harac


terisnotacontr
olcharacter
,anexaminati
onofCV($23=Cu r
-
sorVerti
calpos i
ti
on)andthec ur
renttex
t-pageaddres
swillbemade.Av al
ueof
$1C00willb eaddedtoBASL,BASH($28,$29)tocalc
ulat
ethebaseaddres
softhe
pri
ma r
yhi -resscree
nl i
ne.⇢ec ont
entsofCH( $22=Cu r
sorHoriz
ontalposi
-
ti
on)willthenbea ddedtothisbaseaddresstocal
cul
atetheact
ualhi-
resscr
een
bytet
obemodi Fed.
4. ⇢eAS CIIva
lueofthecha r
actert
obepr i
ntedwil
lbeus e
dt odeterminethe
posit
ioninac har
act
erdatatablefrom whic
hthee i
ghtbyte
sc onta
iningthedata
forthecha r
act
erwil
lber et
ri
ev e
d.⇢epos i
ti
onc anbede t
erminedbyFr stsub-
trac
ting32from t
heAS CIIvalue(toma keupforthemissi
ngc ontr
olcharact
ers
inthetable)
.⇢er esul
ti
ngv al
uei sthenmulti
pli
edbye ig
ht(foreightbytesper
charact
er)todet
erminethecorrectsta
rti
ngposit
ionofthedataforthatparti
cu-
l
a rchara
cter.⇢egener
al f
ormula,then,i
s:
Posi
tion=(ASCIIvalue73 2)d8

5. ⇢ec ha
rac
terwil
lact
ual
lybeproducedbyst
oringtheFrstby
teinthec
alc
u-
l
ate
dbaseaddres
s.⇢ene xtsevenbyteswil
lthenbes tore
da ttheaddr
esse
s
de
termi
nedbysucce
ssi
vel
yaddingt
hev a
lue$400tothebaseaddre
ss.
6. Att hatpoi ntthepr i
nt i
ngt ot hehi-resscreenwi llbec omplete.⇢eor i
ginal
charactertobepr intedwi llt henbes entt oCOUT1( $FDF0)s otha tt
heMoni tor
routinescanha ndlec arr
iag er e
turns,backspac es,ands oon.⇢i sact
ionbyt he
Moni torwi llautoma tica
llye nsurethattheBASL,BASHpa irisma int
ainedpr op-
erl
ys otha twec ana l
wa ysr elyoni t
sa ccuracyi npos i
ti
oni ngthet ex
tout puton
thesc r
een.
⇢i slas tpointma yneedabi tofexplanation. I
fwene versentac harac t
ert o
COUT1, wewoul dha v
et oha ndlethee ntir
es creenma nag ementour s
e l
ves.⇢i s
me ansthatwhe nweg ottot hee ndoft hel i
ne ,wewoul dha v
et odetecti tand
thena dvanc eCVa ndr ecalculateBASL,BASHa cc ordi
ng ly.Bypa ss
inge a
c hc harac-
tertoCOUT1( event hought ec hnical
lywene v
ers eethet exts c
reen),t
heMoni tor
willkeepBASL, BASH, CH,andCVa llmaintainedi nawa yc onsis
tentwitht heda ta
printedtot hesc r
een.
⇢usa llwene edt odoi slooka tBASL,BASH,CH,a ndCVf ore achc haracter
printedtoha vethehi -resscre e
npr operlymi mi cwha tisg oingonwi tht het ext
displ
a ypage .
He re,then, i
sthel i
sti
ngf orthehi -
r e
sc haractergene rat
or:
1 ********************************
2 * AL31-CHARACTER GENERATOR *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *

]
3
06 As
sembl
yLi
nes

8 CSW EQU $36


9 BASL EQU $28
10 CH EQU $24
11 TABLE EQU $9000
12 POSN EQU $3C ; (BAS2)
13 SCRN EQU $3E ; (A4)
14 VECT EQU $3EA
15 COUT1 EQU $FDF0
16 *
0300: A9 0B 17 HOOK LDA #ENTRY ; PRODUCES LOW BYTE
0302: 85 36 18 STA CSW
0304: A9 03 19 LDA #>ENTRY ; #> PRODUCES HIGH BYTE
0306: 85 37 20 STA CSW+1
0308: 4C 3A FF 21 JMP VECT
22 *
030B: C9 A0 23 ENTRY CMP #$A0
030D: 90 51 24 BCC OUT ; CTRL CHARACTER
030F: 48 25 PHA ; STORE CHAR
0310: 29 7F 26 AND #$7F ; CLEAR HI BIT
0312: 85 3C 27 STA POSN
0314: A9 00 28 LDA #$00
0316: 85 3D 29 STA POSN+1
0318: 98 30 TYA
0319: 48 31 PHA ; SAVE Y
32 *
031A: 38 33 CALC1 SEC
031B: A5 3C 34 LDA POSN
031D: E9 20 35 SBC #$20
031F: 85 3C 36 STA POSN ; CHAR < 96
0321: 06 3C 37 ASL POSN ; *2 = CHAR < 192
0323: 06 3C 38 ASL POSN ; *4 < 384
0325: 26 3D 39 ROL POSN+1
0327: 06 3C 40 ASL POSN ; *8 < 768
0329: 26 3D 41 ROL POSN+1
42 *
43 * POSN = (ASC - $20)*8 BYTES PER CHAR
44 *
032B: 18 45 CLC
032C: A9 00 46 LDA #TABLE ; LOW BYTE
032E: 65 3C 47 ADC POSN
0330: 85 3C 48 STA POSN
0332: A9 90 49 LDA #>TABLE ; HIGH BYTE
0334: 65 3D 50 ADC POSN+1
0336: 85 3D 51 STA POSN+1 ; POSN = POSN + TABLE ADDR
52 *
0338: 18 53 CALC2 CLC
0339: A5 28 54 LDA BASL
033B: 65 24 55 ADC CH
033D: 85 3E 56 STA SCRN
033F: A5 29 57 LDA BASL+1
0341: 69 1C 58 ADC #$1C
0343: 85 3F 59 STA SCRN+1 ; SCRN = BASL + CH + $1C00
60 *
0345: A0 00 61 GETBYTE LDY #$00
0347: B1 3C 62 G1 LDA (POSN),Y
0349: 91 3E 63 STA (SCRN),Y
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
07

034B: C8 64 INC INY


034C: 18 65 CLC
034D: A5 3E 66 LDA SCRN
034F: 69 FF 67 ADC #$FF
0351: 85 3E 68 STA SCRN
0353: A5 3F 69 LDA SCRN+1
0355: 69 03 70 ADC #$03
0357: 85 3F 71 STA SCRN+1 ; SCRN = SCRN + $3FF
72 *
73 * $3FF TO MAKE UP FOR GROWING VALUE OF 'Y'
74 *
0359: C0 08 75 DONE? CPY #$08
035B: 90 EA 76 BCC G1
77 *
035D: 68 78 YES PLA
035E: A8 79 TAY ; RESTORE Y
035F: 68 80 PLA ; RESTORE CHAR
0360: 4C F0 FD 81 OUT JMP COUT1
0363: D8 82 CHK

⇢er out i
nei sr el
a tivelys hor ta ndi splace da tl ocation$300( 768de c
ima l)
.
Whe nac allto$300i sdonebye it
he ra300Gf rom t heMoni tororaCALL768
from BAS IC,t her outinewi lls ett heout putv ector stopoi ntt oENTRYa ndthe n
calltheDOShook upr out inede s cribedi ne arlierc ha pters.Att hispoi nt,a l
l
futurec ha ract
e rout putwi llpa sst hroug hthisr out i
ne ,unt iliti sdisconne cted
eitherbyaPR#0orbypr e ssingRESET.
AtENTRY,t heFr stthi ngt ha ti sc hec kedf ori st os eewhe the rthec haracter
be i
ngout putisac ont rol c ha ra ct
e r.Re me mbe rt ha tatthi spoi ntt hehi g
hbi twi l
l
bes etona lltextg oingt ot hes creen.⇢e r
efor e,e v ent houg h$20i st hemor e
nor ma lAS CI Iv aluef oras pa c ec ha ra c
ter,wi tht hehi ghbi ts etitwi llbes ent
throug hCOUTa sa n$A0.
I
fac ont r
olc ha racteri sde tec tedhe re,theCMPa ndBCCwi llpasscont r
olt o
thee xitpoi ntoft her out ine ,OUT. Re me mbe rt ha tBCCi sus edt ode tecta l
lvalues
int heAc cumul atorl esst ha nt hev alueus edi nt heCMPi ns truct i
on.Al lcontrol
cha ract
e r
swi l
lha vea nAS CI Iv aluel es sthant ha toft hes pa cec ha ra
cter.
I
ft hec haracte risanon- cont r olc har act
er, it
’st henpus he dont ot hes t
ackin
l
ine2 5.⇢i sist os avet hec ha rac tert obepr inteds ot hati te ventua l
lycanbe
pa ss
edont oCOUT1.⇢ene x tline ,2 6 ,thenc le arst hehi ghbi toft hec haracter
ands tor e
st here sultingv aluei nPOSN.
⇢i sr esul
tingt rueAS CI Iv aluewi l
lbeus eds hortlyt oc alculatethene ede d
pos i
tioninourc ha racte rta ble, sol ine s28a nd2 9s tor ea0i nt hehi gh-order-byte
pos i
tionofPOSN.Be ca use9 6c ha racte rstime s8by tese a c
hwi l
lr equireat able
76 8($300) by teslong ,POSNwi llha vet obea blet oi nc l
udeat wo- bytevalue.⇢us
l
ine s2 8a nd2 9t ak et hisoppor tuni tyt os ett hehi ghby teofPOSNt o0nowi n
ant i
cipati
onoff ut ur ec alcula tions .
Anot herbi tofpr og rammi ngt e chniquea ppe a rsonl ine s3 0a nd3 1. Because
theAppl ea ssume st ha ta llout putr out ineswi l
ll eav ea l
loft her egiste
r s(X,Y,

]
3
08 As
sembl
yLi
nes

andA)una l
tere d,wemus tsavet heY- Re gist
e rsoa st obea bl et ores toreittoi t
s
or i
gina lcondi ti
onl aterone x i
t.Toa v oidha v i
ngt ous ea not he rze ro- pageloca-
tionf ort his,we ’vede layeds a v
ingt heY- Re gist
erunt ilnows ot hati tsv a
luec an
beputi nt heAc cumul a t
ora ndt he npus he dont ot hes tack .Pr iort os avi
ngt he
cha r
a cterv a
luei nPOSN,a nya ttemptt oputYi nt heAc cumul at
orwoul dha ve
erasedt hev aluef ort hec ha ract
e rwewa nt edtopr int.
Nowf ort hec alculationpha se. ⇢eFr ststepi st osubt rac t3 2from t heAS CII
va l
uei npr epa ra t
ionf orc alculatingt het ablepos i
tion.Li ne s3 3t hr oug h3 6do
this.⇢ene xts tepist omul ti
plyby8t og etther elativepos iti
oni nt het able.For-
tuna t
e l
y ,8i sa ne as ynumbe rbywhi cht omul tiply .Youma yr eme mbe rfrom
ea r
li
e rc ha pte rst ha tal e 4-shi4 ope rationi se qui valentt omul tiplyingby2 .
⇢e refore,a llwene eddoi sshi4l e4t hr eetime st og etthee Le ctofmul ti
plying
by8( 2d2d2=8 ).
Nor ma l
ly, bec ausePOSNi sat wo- by tevalue ,
e ac hs hi4woul dha vet obeas e
t
ofASLsa ndROLs .Howe v er,be caus ewek now we ’res tartingwi thav aluele s
s
than9 6,wek nowt heFr stshi4c annotpos si
blyg iv ear es
ul tg rea t
ert ha n2 56.In
lookinga tl i
ne s3 7t hroug h4 1 ,youc ans eetha tline3 7doe st heFr stmul tipl
yby
2. I
ti sthe nl i
ne s3 8thr oug h4 1t ha tdot hetwo- by tes hi4st og e ttheFna lr e
sult
.
Re me mbe ra lsot ha ta nASLput st hebi tpus hedoutt hee ndi ntot hec arrySa g.
⇢a ta l
lowsROLt opi ckupt hec arr ywhe ns hi4ingt hehi gh- or derby t e.
Cons ide rt hee xa mpl eint het a bl
ebe lowt os e ehowt hes hi4swor k.⇢el et
-
terAha sa nAS CI Iv alueof$41( 6 5de cima l).A4e rs ubtrac ting$20( 3 2dec i
ma l
)
we ’
llha vear e sul
tof$21( 33de c ima l).A4e rmul tiplyingby8 ,wes houl dg eta
resultof$108( 264de cima l)
.

Pr
ogr
am Comma
nd POSN+1(
hex
)POSN(
hex
) POSN+1 Ca
rry POSN
36: Start $00 $21 %0000 0000 0 %0010 0001
37: ASL POSN $00 $42 %0000 0000 0 %0100 0010
38: ASL POSN $00 $84 %0000 0000 0 %1000 0100
39: ROL POSN+1 $00 $84 %0000 0000 0 %1000 0100
40: ASL POSN $00 $08 %0000 0000 1 %0000 1000
41: ROL POSN+1 $01 $08 %0000 0001 0 %0000 1000

Oncethemul ti
plic
a t
ionby8ha sbe endone ,theonlythingr ema iningisto
taketherela
tiveoLs etpos i
ti
onde ter
mi neda nda ddt hattotheba sea ddressof
thetabl
e.Int hisc as
e,wewi llas s
umet hatthet abl
eha sbeenl oa de da t$9000
(andpresumablypr otectedbys etti
ngHIMEM:36864) .
Oncethet a
bl epositi
oni scalculat
ed, thescr
ee nbytetobemodi Fedmus tbe
calc
ulat
eda swe l
l.⇢i si sdonebyCALC2.Li nes53t hrough59t ak et hec ontent
s
ofBASL,BASHa nda dd$1Cxxt ot hat,whe rexxi sthev al
ueofCHa ttha tpoint.
Adding$1C00g i
v estheba sea ddressoft hehi-resscreenli
nec or r
e spondi ngto
thecurre
ntt ext-
pa geline.Wec ouldha veus e
dt heY- Regis
terf orCH,butt hat
wouldhav epreventedusf rom e asil
yusingt heY- Regist
ertoindext hec haracte
r
tabl
edata.⇢e refore,wea ddCHt oma keBASL, BASHt headdressoft heFr sthi-re
s
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
09

scr
e enby tetobemodi Fed. Not et ha ta na dde da dvanta geofthisa ppr oachist ha t
HTAB a ndVTABc omma ndswi llcont inuet owor kont hehi -
respa ge.S cr
ol l
ing ,
howe ve r
, wil
lnotbea vailable .
GETBYTE( li
ne6 1)i st hes ecti
onr espons ibleforput tingthec ha racteront he
hi-
r e
ss c
re e
n.⇢i sisdonei nanumbe rofs t
ag es
.⇢eFr s
tstepi st os ettheY-
Reg i
stert o#$00t opr epa retor e trievet heda taby tesf rom thet able.G1t he n
sta
r t
st her etr
ievalloopbyg ett
ingt heFr s tbyteoft hec haract
erf rom t het a ble
ands toringitont hehi -ress creen.
Nowhe re’
swhe rei tg etsinte resting .Nor ma lly
,t hene xtste pswoul dbet o
increme ntYt og etthene x tchar acterf rom t het able
,a ndt oa l
soa dd$400t ot he
POSNv al
uet oa cc
e s
st hene xthor iz ont allineont hes creen.⇢epr obl emi stha t
,
ifYc ha ng e
s,wewon’ ta ccessthel inedi rectlybe l
owt heonewej us tmodi Fe d,
butrat heroneby tetot her ightofwhe rewewa ntt obe .
⇢es ol
utionist oa dd$3FF, rathe rt ha n$400, toPOSN. ⇢a twa yt hev alueof
POSNwi l
lg rowi nawa yc ompa ti
bl ewi tht hei ncreasedv a
lueoft heY- Re giste r
.
⇢i spa r
toft hel i
stingi swor ths tudy ingunt ilyouunde rst
andt hec onc ept.I t
savesal otofne edlesss toringoft heY- Re gisterandhe nc enee dle sse xtr
at ime
andme mor yusag e
.⇢et echni quec anbea ppliedtoma nyot he rs ituati
onsa s
well.
Onc ethee ntiree ightby tesha v ebe enputont hehi -ress cr een,l i
ne s7 8
throug h8 1r e
storetheAc cumul atort ot hev alueoft heor i
ginalc ha r act
ert obe
pri
nt eda ndt heY- Re g i
ste rtoi tsor igina lvalue.⇢ej umpt oCOUT1( $FDF0)i s
thendonet oc ompl etethepr i
ntingt ot henor ma ltextscr e
en. ⇢ea dv antage sof
thi
swe rediscussede arli
er( ma inte na nceofBASL, BASH, CV, a
nds oon) .

AHi
-Re
sCha
rac
terSe
t
⇢ewa yt ousethec haractergeneratoristoloa dtheassembledbinaryrou-
ti
nea t$300( 768decima l
).Ina nAppl eso4pr ogram, y
ouwoul dthenexecutean
HGRc omma nd, f
oll
owe dbyaCALL768t oa cti
vatetherouti
ne .
Ifyouwe retous ether out i
nee nti
relyfrom assemblylanguage,youwoul d
havet oc a
llHGRd ire
c t
ly.Seec ha pt
er19f ormor einformationonc alli
ngthehi-
ressubroutine s
.
⇢e reis ,howe v
e r
,onemi norde t
ails t
il
lmi s
sing.⇢a ti sthetabl
ethatwe
assume de xistedat$9000.S inc eyoudon’ tyethaveame ansofe asil
ycreati
ng
yourownc ha ract
erset,you’
llne edat a
blet ouse.
⇢i sda ta,alt
houghl engthy ,wil
lpr ovideyouwi thac ompl et
echa r
acte
rs e
t
tobel oadeda t$9000.Al t
houg hi twill
tak eawhi l
etoe nt
ertheda ta
,itwil
lprob-
ablybeal ittleeasi
ertha ncrea ti
nge achc haract
erwi t
ha ne dit
or,al
thoughy ou
willhavethatoppor tunityinthene xtcha pter
.

]
3
10 As
sembl
yLi
nes

1 ********************************
2 * AL31-ASCII CHARACTER SET *
3 ********************************
4 *
5 ORG $9000
6 *
9000: 00 00 00 00 00 00 00 00 7 HEX 0000000000000000 ; SPACE
9008: 08 08 08 08 08 00 08 00 8 HEX 0808080808000800 ; !
9010: 14 14 14 00 00 00 00 00 9 HEX 1414140000000000 ; "
9018: 14 14 3E 14 3E 14 14 00 10 HEX 14143E143E141400 ; #
9020: 08 3C 0A 1C 28 1E 00 00 11 HEX 083C0A1C281E0800 ; $
9028: 06 26 10 08 04 32 30 00 12 HEX 0626100804323000 ; %
9030: 04 0A 0A 04 2A 12 2C 00 13 HEX 040A0A042A122C00 ; &
9038: 0B 00 06 00 00 00 00 00 14 HEX 0808080000000000 ; ’
9040: 08 04 02 02 02 04 08 00 15 HEX 0804020202040800 ; (
9048: 08 10 20 20 20 10 08 00 16 HEX 0810202020100800 ; )
9050: 08 2A 1C 08 1C 2A 08 00 17 HEX 082A1C081C2A0800 ; *
9058: 00 08 08 3E 08 08 00 00 18 HEX 0008083E08080000 ; +
9060: 00 00 00 00 00 00 08 04 19 HEX 0000000000000804 ; ,
9068: 00 00 00 3E 00 00 00 00 20 HEX 0000003E00000000 ; -
9070: 00 00 00 00 00 00 08 00 21 HEX 0000000000000800 ; .
9078: 00 20 10 08 04 02 00 00 22 HEX 0020100804020000 ; /
9080: 1C 22 32 2A 26 22 1C 00 23 HEX 1C22322A26221C00 ; 0
9088: 08 0C 08 00 08 08 1C 00 24 HEX 080C080808081C00 ; 1
9090: 1C 22 20 18 04 02 3E 00 25 HEX 1C22201804023E00 ; 2
9098: 3E 20 10 18 20 22 1C 00 26 HEX 3E20101820221C00 ; 3
90A0: 10 18 14 12 3E 10 10 00 27 HEX 101814123E101000 ; 4
90A8: 3E 02 1E 20 20 22 1C 00 28 HEX 3E021E2020221C00 ; 5
90B0: 18 04 02 1E 22 22 1C 00 29 HEX 1804021E22221C00 ; 6
90B8: 3E 20 10 08 04 04 04 00 30 HEX 3E20100804040400 ; 7
90C0: 1C 22 22 1C 22 22 1C 00 31 HEX 1C22221C22221C00 ; 8
90C8: 1C 22 22 3C 20 10 0C 00 32 HEX 1C22223C20100C00 ; 9
90D0: 00 00 08 00 08 00 00 00 33 HEX 0000080008000000 ; :
90D8: 00 00 08 00 08 08 04 00 34 HEX 0000080008080400 ; ;
90E0: 10 08 04 02 04 08 10 00 35 HEX 1008040204081000 ; <
90E8: 00 00 3E 00 3E 00 00 00 36 HEX 00003E003E000000 ; =
90F0: 04 08 10 20 10 08 04 00 37 HEX 0408102010080400 ; >
90F8: 10 22 10 08 08 00 08 00 38 HEX 1C22100808000800 ; ?
9100: 1C 22 2A 3A 1A 02 3C D0 39 HEX 1C222A3A1A023CD0 ; @
9108: 08 14 22 22 3E 22 22 00 40 HEX 081422223E222200 ; A
9110: 10 22 22 1E 22 22 1E 00 41 HEX 1E22221E22221E00 ; B
9118: 1C 22 02 02 02 22 1C 00 42 HEX 1C22020202221C00 ; C
9120: 1E 22 22 22 22 22 1E 00 43 HEX 1E22222222221E00 ; D
9128: 3E 02 02 1E 02 02 3E D0 44 HEX 3E02021E02023E00 ; E
9130: 3E 02 02 1E 02 02 02 00 45 HEX 3E02021E02020200 ; F
9138: 3C 02 02 02 32 22 3C 00 46 HEX 3C02020232223C00 ; G
9140: 22 22 22 3E 22 22 22 00 47 HEX 2222223E22222200 ; H
9148: 1C 08 06 08 08 08 1C 00 48 HEX 1C08080808081C00 ; I
9150: 20 20 20 20 20 22 1C 00 49 HEX 2020202020221C00 ; J
9158: 22 12 0A 06 0A 12 22 00 50 HEX 22120A060A122200 ; K
9160: 02 02 02 02 02 02 3E 00 51 HEX 0202020202023E00 ; L
9168: 22 36 2A 2A 22 22 22 00 52 HEX 22362A2A22222200 ; M
9170: 22 22 26 2A 32 22 22 00 53 HEX 2222262A32222200 ; N
9178: 1C 22 22 22 22 22 1C 00 54 HEX 1C22222222221C00 ; O
9180: 1E 22 22 1E 02 02 02 00 55 HEX 1E22221E02020200 ; P
9188: 1C 22 22 22 2A 12 2C 00 56 HEX 1C2222222A122C00 ; Q
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
11

9190: 1E 22 22 1E 0A 12 22 00 57 HEX 1E22221E0A122200 ; R


9198: 1C 22 02 1C 20 22 10 00 58 HEX 1C22021C20221C00 ; S
91A0: 3E 08 03 08 03 08 08 00 59 HEX 3E08080808080800 ; T
91A8: 22 22 22 22 22 22 1C 00 60 HEX 2222222222221C00 ; U
91B0: 22 22 22 22 22 14 06 00 61 HEX 2222222222140800 ; V
91B8: 22 22 22 2A 2A 36 22 00 62 HEX 2222222A2A362200 ; W
91C0: 22 22 14 08 14 22 22 00 63 HEX 2222140814222200 ; X
91C8: 22 22 22 14 03 08 03 00 64 HEX 2222221408080800 ; Y
91D0: 3E 20 10 08 04 02 3E 00 65 HEX 3E20100804023E00 ; Z
91D8: 3E 06 06 0E 06 06 3E 00 66 HEX 3E06060606063E00 ; [
91E0: 00 02 04 06 10 20 00 00 67 HEX 0002040810200000 ; \
91E8: 3E 30 30 30 3D 30 3E 00 68 HEX 3E30303030303E00 ; ]
91F0: 00 00 08 14 22 00 00 00 69 HEX 0000081422000000 ; ^
91F8: 00 00 00 00 00 00 00 7F 70 HEX 000000000000007F ; _
9200: 04 08 10 00 00 00 00 00 71 HEX 0408100000000000 ; ’
9208: 00 00 1C 20 3C 22 3C 00 72 HEX 00001C203C223C00 ; a
9210: 02 02 1E 22 22 22 1E 00 73 HEX 02021E2222221E00 ; b
9218: 00 00 3C 02 02 02 3C 00 74 HEX 00003C0202023C00 ; c
9220: 20 20 3C 22 22 22 3C 00 75 HEX 20203C2222223C00 ; d
9228: 00 00 1C 22 3E 02 3C 00 76 HEX 00001C223E023C00 ; e
9230: 18 24 04 1E 04 04 04 00 77 HEX 1824041E04040400 ; f
9238: 00 00 1C 22 22 3C 20 1C 78 HEX 00001C22223C201C ; g
9240: 02 02 1E 22 22 22 22 00 79 HEX 02021E2222222200 ; h
9248: 08 00 0C 08 08 08 1C 00 80 HEX 08000C0808081C00 ; i
9250: 10 00 18 10 10 10 12 00 81 HEX 100018101010120C ; j
9258: 02 02 22 12 0E 12 22 00 82 HEX 020222120E122200 ; k
9260: 0C 03 08 0B 08 08 1C 00 83 HEX 0C08080808081C00 ; l
9268: 00 00 36 2A 2A 2A 22 00 84 HEX 0000362A2A2A2200 ; m
9270: 00 00 1E 22 22 22 22 00 85 HEX 00001E2222222200 ; n
9278: 00 00 1C 22 22 22 1C 00 86 HEX 00001C2222221C00 ; o
9280: 00 00 1E 22 22 1E 02 02 87 HEX 00001E22221E0202 ; p
9288: 00 00 3C 22 22 3C 20 20 88 HEX 00003C22223C2020 ; q
9290: 00 00 3A 06 02 02 02 00 89 HEX 00003A0602020200 ; r
9298: 00 00 3C 02 1C 20 1E 00 90 HEX 00003C021C201E00 ; s
92A0: 04 04 1E 04 04 24 18 00 91 HEX 04041E0404241800 ; t
92A8: 00 00 22 22 22 32 2C 00 92 HEX 0000222222322C00 ; u
92B0: 00 00 22 22 22 14 08 00 93 HEX 0000222222140800 ; v
92B8: 00 00 22 22 2A 2A 36 00 94 HEX 000022222A2A3600 ; w
92C0: 00 00 22 14 08 14 22 00 95 HEX 0000221408142200 ; x
92C8: 00 00 22 22 14 08 08 06 96 HEX 0000222214080806 ; y
92D0: C0 00 3E 10 08 04 3E 00 97 HEX 00003E1008043E00 ; z
92D8: 38 0C 0C 06 0C 0C 30 00 98 HEX 380C0C060C0C3800 ; {
92E0: 08 0B 03 08 08 08 08 08 99 HEX 0808080808080808 ; |
92E8: 0E 18 1B 30 18 18 0E 00 100 HEX 0E18183018180E00 ; }
92F0: 2C 1A 00 00 00 00 00 00 101 HEX 2C1A000000000000 ; ~
92F8: 7F 7F 7F 7F 7F 7F 7F 7F 102 HEX 7F7F7F7F7F7F7F7F ; CURSOR
9300: 6F 103 CHK

Asasi
denote,thi
sisanoddpr
ogr
aminthatitdoes
n’tact
ual
lydoany
thi
ng.
I
tjus
tcre
atesadatatable
.Asse
mblei
tany
wa yands a
vet heobj
ectc
odeunder
t
henameAL31.ASCII.

]
3
12 As
sembl
yLi
nes

Totes
ta l
lofthi
sout,youcanus ethi
ss i
mpleAppl
eso4program.1You
probabl
yshoul
dverif
ythaty
ouc a
na tl
eastge
tthismuc
htoworkbe f
orediv
ing
inandtryi
ngtousetherout
ine
sfrom wit
hinyourownass
embly
-languag
epro-
grams.
10 PRINT CHR$(21): REM 40-COLUMN
20 PRINT "CHAR TABLE FILE, <RETURN> FOR DEFAULT": INPUT A$
30 IF LEN(A$) = 0 THEN A$ = "AL31.ASCII"
40 PRINT CHR$(4); "BLOAD "; A$
50 PRINT CHR$(4); "BLOAD AL31.CHARGEN,A$300
60 HGR: HCOLOR= 3
70 HPLOT 0,0 TO 279,0
80 HPLOT TO 279,159
90 HPLOT TO 0,159
100 HPLOT TO 0,0: REM DRAW FRAME
110 REM IF DOS 3.3 THEN SET UP CSW VECTOR
120 IF PEEK(1002) = 76 THEN CALL 768: GOTO 150
130 REM IF PRODOS, SET UP OUTPUT LINK AT $BE30,31
140 POKE 48688,11: POKE 48689,3
150 VTAB 1: HTAB 10
160 PRINT "HI-RES CHARACTER GENERATOR"
170 END
180 REM USE RESET OR PR#0 TO TURN OFF

Conc
lus
ion
Atthispoi ntyous houl dfeelfa
irl
yc omfor t
ablewi t
ht heideaofhowahi -
res
charact
erg ene ra
torwor ks.⇢ei deaspr e
sentedhe r erel
yhe avilyonag eneral
degreeoffami l
iari
tywi thav ari
etyofte c
hnique sdi sc
ussedi ne ar
li
erc hapter
s,
speci
Fcall
y,out putv ectorus eandi nte
rc e
ption,me moryma ppingoft hehi -
res
andt extscr
e ens,a ndofc ourseg ener
alt e
chni quesofa ssembly -l
anguagepr o-
grammi ng.Ify oua r
eha vi
ngdi Wc ul
tyina nyoft hesea re
as,youma ywi sht o
revi
ewpr evi
ousc hapters.
Allinall,yous houldFndt hea pproachs hownhe retobemuc he asie
rtha n
youFr stthoug ht.⇢es imilari
ti
esbe t
we ent het extandhi -ress c
reensg reat
ly
reducetheamountofdi Wc ult
yi ncreat
ingac har a
cterg e
ne r
ator.
Inthene x tchapter,we ’l
ldevelopac haractere dit
ortoc reateyourownhi -
rescharact
erf onts( t
het erm usedforthec haracterde s
ign),anda ls
otak eabr i
ef
l
ooka thow hi -r
e sgraphicsi na r
cade-sty
leg ame sc antakea dvantageoft hese
samet e
chnique stoc reateawi devarie
tyofe Lects.

1[
CT]ForPr
oDOS
,wema
nua
ll
ycha
nget
heout
putv
ect
ora
t$BE30,$BE31t
opoi
ntt
o
ENTRY(
$30B)
.Se
efoot
not
e1i
ncha
pte
r29f
ormor
edi
scus
sion.
3
2
3
2. Hi
-Re
sChar
act
erEdi
tor
Ma
y19
83

Inthepr e viousc ha pte rwepr ese nt edal isti


ngf orahi -rescha ra cte rg ene ra-
tora longwi tht het heor ybe hindi tsope ration.⇢eg ene r
a t
orus eda ne x isti
ng
cha racterset, loade da tloc ation$9000i nme mor y,a ndc ont a
inedt heda taf or9 6
AS CI Icha racte r
s .
Toc rea tey ourownc ha racte rse t
, a llthati sne ede di saut il
ityf ore ditingt he
ex i
stingc ha rac t
e rs etandc rea t
ingt hene wf ont ,orc ha ract
e rde sig n,t ha ty ou
de si
re .
Beforepr esent ingthel i
s ti
ngf ort hec ha ractere ditor ,cons i
de rf oramome nt
thei nforma tiona ndt ec
hni que st ha tmus tbepr ov i
de df or .⇢i si sav e ryi mpor -
tantpa rtofs olvinga nypr obl em,pr og rammi ngorot he rwi se,andi si ns t
rume n-
talindi rectinga ndc l
ari
fy ingone ’sthoug htpr oce ss
es .
Indi scus singt hec ha ra cters e
t,y ou’ llre calltha te achc hara cte ri sr e pre-
sent e
dbyas er i
esofe ightby t
e si nt het able,a ndt ha te a chdoti nt hec har acter
ima gei srepr e sentedbyabi twi thi noneoft hos eby tes.⇢eFr s
tt woc ons ide ra-
tions, t
he refore ,arehowt oa ddr esst hes eriesofby test ha tcor r
espondt oag i
ve n
AS CI Icha racte ra ndhowt oi de ntifya nda lt
e rthebi tcor respondi ngt ot hepa r-
ticulardoti nt hec ha r
acteri ma get ha twewi s
ht omodi fy.
Ine dit
inge a chc harac ter, wewi llwa ntt obea blet ot urnag ive nbi tonoroL
(setitto1or0 )a ndt omov eac ur sorf rom onebi ttoa not her.You’ lla l
s or e c
a l
l
from t hepr e viousc haptert ha te achby teoft hec ha racte r’
sda tac or re spondst o
onel ineofi tsima geont hes cree n. Wi t hine a chby te, sev enbi t
sa reus e dt oma p
thes evens cr eendot suse dt og ene rateag ivenl i
neofac ha ra c
ter.
Whe nwee ditt heindi vidua l scre endot s
, i
twoul dbeni ceifwec oul dus ethe
standa rda rrowk eyst omov et hec urs ora roundi naboxc ont ai
ni ngt hec ha rac-
terima g e.
Spea ki
ngoft hec ha r
a cte rbox ,s omet houg htwi llha vetobeg i
v ent ohow
thee ntirec ha racteri t
selfwi ll bedi s
pl ay ed. Wec ouldj us tpr i
ntthec ha racte ron-
screene a cht imeamodi Fc ationi sdone ,butbe ca useoft hes ma llsiz et hiswoul d
be comet e
di ousa 4e rawhi le, Abe ttera ppr oa chwoul dbet odispla yama g niFe d
ima geoft hec ha racter,uponwhi chourc ursorc a nbepos it
ione dtoe di ta nypa r-
ticularbitint heov eral
lima g e.
Tous et hee ditor,we ’
lla lsoha v et obea blet os pec i
fywhi chc ha ra cterwe
wa ntt oe dit,a ndt henl atert os ignifyt ha twea r edone .Tok eept hi ng ss impl e,

]
3
14 As
sembl
yLi
nes

we’l
lsel
e c
tac hara
cterbypre s
singtheequiv
ale
ntk eyandstor
ethecompl e
ted
imageba c
ki nthecha r
acte
rtablewhen<RETURN>ispres
sed.
Loa di
nga nds avingofthec omplet
etabl
eisnotpr ovi
dedforint
hee di
tor
butcanbea ccomplishedeasi
lyfrom t
heimme di
atemodewi t
hBLOADandBSAVE.
Moreont ha tl
a t
er.
He re,then,isthec omplet
el i
st
ing,whichwillbeexpl
aine
di ndet
ail
.1See
youatthebot t
om!
1 ********************************
2 * AL32-CHARACTER EDITOR *
3 * 2/7/1983 *
4 ********************************
5 ORG $8000
6 CSW EQU $36
7 BASL EQU $28
8 CV EQU $25
9 CH EQU $24
10 CR EQU $06
11 CC EQU $07
12 MASK EQU $08
13 CHR EQU $09
14 TABLE EQU $9000
15 POSN EQU $3C ; (BAS2)
16 SCRN EQU $3E ; (A4)
17 VECT EQU $3EA
18 COUT EQU $FDED
19 COUT1 EQU $FDF0
20 HGR EQU $F3E2
21 HCOLOR EQU $F6F0
22 HPLOT EQU $F457
23 HLIN EQU $F53A
24 X1 EQU $22 ; 34
25 X2 EQU $54 ; 84
26 Y1 EQU $17 ; 23
27 Y2 EQU $58 ; 88
28 VTAB EQU $FC22
29 RDKEY EQU $FD0C
30 BELL EQU $FBDD
31 B1 EQU %10101010
32 B2 EQU %01010101
33 *
34 CURDAT EQU $FFFF
35 *
8000: A9 81 36 HOOK LDA #HCOUT ; PRODUCES LOW BYTE
8002: 85 36 37 STA CSW
8004: A9 81 38 LDA #>HCOUT ; #> PRODUCES HIGH BYTE
8006: 85 37 39 STA CSW+1
8008: 4C DD FB 40 JSR VECT
41 *
800B: 20 E2 F3 42 ENTRY JSR HGR
800E: A9 00 43 LDA #$00
8010: 85 06 44 STA CR ; CR=0

1[
CT]Li
nes2
10a
nd2
1872
22we
r Fe
emodi dt
oal
lowy
out
opr
ess<CTRL>Qt
oqui
t.
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
15

8012: 85 07 45 STA CC ; CC=0


8014: EA 46 TITLE NOP
47 *
8015: A9 03 48 CHRLIST LDA #$03
8017: 85 25 49 STA CV
8019: 20 22 FC 50 JSR VTAB
801C: A2 20 51 START LDX #$20
801E: 8A 52 CH2 TXA
801F: 29 0F 53 AND #%00001111 ; 2^4 - 1
54 * RESULT = VALUE MOD 16
8021: D0 09 55 BNE CONT ; NOT MULT OF 16
8023: A9 8D 56 LDA #$8D
8025: 20 ED FD 57 JSR COUT ; PRINT RETURN
8028: A9 14 58 LDA #$14 ; MARGIN FOR NEW LINE
802A: 85 24 59 STA CH
802C: 8A 60 CONT TXA ; RESTORE CHAR
802D: 09 80 61 ORA #$80 ; SET HI BIT
802F: 20 ED FD 62 JSR COUT ; PRINT CHAR
8032: E8 63 NEXTC INX
8033: E0 80 64 CPX #$80
8035: 90 E7 65 BCC CH2
66 *
8037: A2 03 67 MATDSP LDX #$03
8039: 20 F0 F6 68 JSR HCOLOR
803C: A2 22 69 BOX LDX #X1 ; LOW BYTE
803E: A0 00 70 LDY #>X1 ; HIGH BYTE
8040: A9 17 71 LDA #Y1
8042: 20 57 F4 72 JSR HPLOT ; PLOT X1,Y1
8045: A9 54 73 LDA #X2
8047: A2 00 74 LDX #>X2
8049: A0 17 75 LDY #Y1
804B: 20 3A F5 76 JSR HLIN ; TO X2,Y1
804E: A9 54 77 LDA #X2
8050: A2 00 78 LDX #>X2
8052: A0 58 79 LDY #Y2
8054: 20 3A F5 80 JSR HLIN ; TO X2,Y2
8057: A9 22 81 LDA #X1
8059: A2 00 82 LDX #>X1
805B: A0 58 83 LDY #Y2
805D: 20 3A F5 84 JSR HLIN ; TO X1,Y2
8060: A9 22 85 LDA #X1
8062: A2 00 86 LDX #>X1
8064: A0 17 87 LDY #Y1
8066: 20 3A F5 88 JSR HLIN ; TO X1,Y1
8069: A9 03 89 MATD2 LDA #$03
806B: 85 25 90 STA CV
806D: 20 22 FC 91 JSR VTAB
8070: A0 00 92 GETROW LDY #$00
8072: A9 05 93 GR1 LDA #$05
8074: 85 24 94 STA CH
8076: B9 69 81 95 LDA MAT,Y
8079: A2 00 96 SCAN LDX #$00
807B: 4A 97 S1 LSR
807C: 48 98 PHA ; SAVE RESULT
807D: A9 A0 99 LDA #$A0 ; SPACE
807F: 90 02 100 BCC PRINTM

]
3
16 As
sembl
yLi
nes

8081: A9 FF 101 LDA #$FF


8083: 20 ED FD 102 PRINTM JSR COUT
8086: 68 103 PLA ; RESTORE ACCUM
8087: E8 104 NXTBIT INX
8088: E0 07 105 CPX #$07
808A: 90 EF 106 BCC S1
808C: A9 8D 107 LDA #$8D ; RETURN
808E: 20 ED FD 108 JSR COUT
8091: C8 109 NXTROW INY
8092: C0 08 110 CPY #$08
8094: 90 DC 111 BCC GR1
112 *
8096: 18 113 CURSOR CLC
8097: A5 06 114 LDA CR ; CURSOR ROW
8099: 69 03 115 ADC #$03
809B: 85 25 116 STA CV
809D: 20 22 FC 117 JSR VTAB
80A0: 18 118 CLC
80A1: A5 07 119 LDA CC ; CURSOR COLUMN
80A3: 69 05 120 ADC #$05
80A5: 85 24 121 STA CH
122 *
80A7: 20 C2 81 123 CURCALC JSR SCRNCALC
80AA: A4 06 124 STATUS LDY CR
80AC: B9 69 81 125 LDA MAT,Y
80AF: A6 07 126 LDX CC
80B1: 4A 127 ST1 LSR
80B2: CA 128 DEX
80B3: 10 FC 129 BPL ST1
80B5: 90 02 130 BCC CLEAR
80B7: B0 04 131 BCS SET
80B9: A9 00 132 CLEAR LDA #$00
80BB: F0 02 133 BEQ PRNTCURS
80BD: A9 08 134 SET LDA #$08
135 *
80BF: 18 136 PRNTCURS CLC
80C0: 69 71 137 ADC #CURSDATA ; LOW BYTE
80C2: 85 3C 138 STA POSN
80C4: A9 00 139 LDA #$00
80C6: 69 81 140 ADC #>CURSDATA ; HIGH BYTE
80C8: 85 3D 141 STA POSN+1
142 *
80CA: 20 D0 81 143 JSR PUTBYTE
80CD: 20 0C FD 144 CMD? JSR RDKEY
80D0: C9 A0 145 CMP #$A0
80D2: 90 12 146 BCC EDIT ; CTRL CHAR
80D4: 85 09 147 CHAR STA CHR
80D6: 20 9B 81 148 JSR POSNCALC
80D9: A0 07 149 LDY #$07
80DB: B1 3C 150 MOVE LDA (POSN),Y
80DD: 99 69 81 151 STA MAT,Y
80E0: 88 152 DEY
80E1: 10 F8 153 BPL MOVE
80E3: 4C 37 80 154 CHRX JMP MATDSP
155 *
80E6: C9 8D 156 EDIT CMP #$8D ; RETURN
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
17

80E8: D0 14 157 BNE E1


80EA: A5 09 158 ACCEPT LDA CHR
80EC: 20 9B 81 159 JSR POSNCALC
80EF: A0 07 160 LDY #$07
80F1: B9 69 81 161 XFER LDA MAT,Y
80F4: 29 7F 162 AND #$7F ; CLEAR BIT 7
80F6: 91 3C 163 STA (POSN),Y
80F8: 88 164 DEY
80F9: 10 F6 165 BPL XFER
80FB: 4C 15 80 166 XFX JMP CHRLIST
167 *
80FE: C9 9B 168 E1 CMP #$9B ; ESCAPE
8100: D0 18 169 BNE E2
8102: 38 170 TOGGLE SEC
8103: A6 07 171 LDX CC
8105: A9 00 172 LDA #$00
8107: 2A 173 SHFT ROL
8108: CA 174 DEX
8109: 10 FC 175 BPL SHFT
810B: 85 08 176 STA MASK
810D: A4 06 177 LDY CR
810F: B9 69 81 178 LDA MAT,Y
8112: 45 08 179 EOR MASK
8114: 99 69 81 180 STA MAT,Y
8117: 4C 37 80 181 TGX JMP MATDSP
182 *
811A: C9 8B 183 E2 CMP #$8B ; <CTRL>K
811C: D0 0B 184 BNE E3
811E: C6 06 185 UP DEC CR
8120: 10 04 186 BPL UPX
8122: A9 07 187 LDA #$07
8124: 85 06 188 STA CR
8126: 4C 37 80 189 UPX JMP MATDSP
190 *
8129: C9 8A 191 E3 CMP #$8A ; <CTRL>J
812B: D0 0F 192 BNE E4
812D: E6 06 193 DOWN INC CR
812F: A5 06 194 LDA CR
8131: C9 08 195 CMP #$08
8133: 90 04 196 BCC DX
8135: A9 00 197 LDA #$00
8137: 85 06 198 STA CR
8139: 4C 37 80 199 DX JMP MATDSP
200 *
813C: C9 88 201 E4 CMP #$88 ; <CTRL>H
813E: D0 0B 202 BNE E5
8140: C6 07 203 LEFT DEC CC
8142: 10 04 204 BPL LX
8144: A9 06 205 LDA #$06
8146: 85 07 206 STA CC
8148: 4C 37 80 207 LX JMP MATDSP
208 *
814B: C9 95 209 E5 CMP #$95 ; <CTRL>U
814D: D0 0F 210 BNE E6 ; [CT] CHECK FOR QUIT
814F: E6 07 211 RIGHT INC CC
8151: A5 07 212 LDA CC

]
3
18 As
sembl
yLi
nes

8153: C9 07 213 CMP #$07


8155: 90 04 214 BCC RX
8157: A9 00 215 LDA #$00
8159: 85 07 216 STA CC
815B: 4C 37 80 217 RX JMP MATDSP
815E: C9 91 218 E6 CMP #$91 ; [CT] <CTRL>Q TO QUIT
8160: D0 01 219 BNE ERR ; UNKNOWN CTRL CHAR
8162: 60 220 QUIT RTS ; [CT] QUIT PROGRAM
8163: 20 DD FB 221 ERR JSR BELL
8166: 4C CD 80 222 JMP CMD?
8169: 55 AA 55 223 MAT DFB B2,B1,B2,B1,B2,B1,B2,B1 ; WORKAREA
816C: AA 55 AA 55 AA
224 *
8171: 7F 225 CURSDATA DFB %01111111
8172: 41 226 DFB %01000001
8173: 41 227 DFB %01000001
8174: 41 228 DFB %01000001
8175: 41 229 DFB %01000001
8176: 41 230 DFB %01000001
8177: 41 231 DFB %01000001
8178: 7F 232 DFB %01111111
233 *
8179: 00 234 DFB %00000000
817A: 3E 235 DFB %00111110
817B: 3E 236 DFB %00111110
817C: 3E 237 DFB %00111110
817D: 3E 238 DFB %00111110
817E: 3E 239 DFB %00111110
817F: 3E 240 DFB %00111110
8180: 00 241 DFB %00000000
242 *
8181: C9 A0 243 HCOUT CMP #$A0
8183: 90 13 244 BCC OUT ; DON’T PRINT CTRL CHARS
8185: 48 245 PHA ; STORE CHAR
8186: 85 3C 246 STA POSN
8188: 98 247 TYA
8189: 48 248 PHA ; SAVE Y
249 *
818A: A5 3C 250 CALC1 LDA POSN ; GET CHAR
818C: 20 9B 81 251 JSR POSNCALC
252 *
818F: 20 C2 81 253 CALC2 JSR SCRNCALC
254 *
8192: 20 D0 81 255 PRINT JSR PUTBYTE
256 *
8195: 68 257 PLA
8196: A8 258 TAY ; RESTORE Y
8197: 68 259 PLA ; RESTORE CHAR
8198: 4C F0 FD 260 OUT JMP COUT1
261 *
819B: 29 7F 262 POSNCALC AND #$7F ; CLEAR HI BIT
819D: 85 3C 263 STA POSN
819F: A9 00 264 LDA #$00
81A1: 85 3D 265 STA POSN+1
81A3: 38 266 SEC
81A4: A5 3C 267 LDA POSN
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
19

81A6: E9 20 268 SBC #$20


81A8: 85 3C 269 STA POSN ; CHR < 96
81AA: 06 3C 270 ASL POSN ; *2 = CHR < 192
81AC: 06 3C 271 ASL POSN ; *4 < 384
81AE: 26 3D 272 ROL POSN+1
81B0: 06 3C 273 ASL POSN ; *8 < 768
81B2: 26 3D 274 ROL POSN+1
275 *
276 * POSN = (ASC - $20) * 8 BYTES PER CHAR
277 *
81B4: 18 278 CLC
81B5: A9 00 279 LDA #TABLE ; LOW BYTE
81B7: 65 3C 280 ADC POSN
81B9: 85 3C 281 STA POSN
81BB: A9 90 282 LDA #>TABLE ; HIGH BYTE
81BD: 65 3D 283 ADC POSN+1
81BF: 85 3D 284 STA POSN+1 ; POSN = POSN + TABLE ADDR
81C1: 60 285 RTS
286 *
81C2: 18 287 SCRNCALC CLC ; ENTER WITH BASL,CH SET UP
81C3: A5 28 288 LDA BASL
81C5: 65 24 289 ADC CH
81C7: 85 3E 290 STA SCRN
81C9: A5 29 291 LDA BASL+1
81CB: 69 1C 292 ADC #$1C
81CD: 85 3F 293 STA SCRN+1 ; SCRN = BASL + CH + $1C00
81CF: 60 294 RTS
295 *
81D0: A0 00 296 PUTBYTE LDY #$00 ; ENTER WITH POSN,SCRN SET UP
81D2: B1 3C 297 G1 LDA (POSN),Y
81D4: 91 3E 298 STA (SCRN),Y
81D6: C8 299 INC INY
81D7: 18 300 CLC
81D8: A5 3E 301 LDA SCRN
81DA: 69 FF 302 ADC #$FF
81DC: 85 3E 303 STA SCRN
81DE: A5 3F 304 LDA SCRN+1
81E0: 69 03 305 ADC #$03
81E2: 85 3F 306 STA SCRN+1 ; SCRN = SCRN + $3FF
307 * $3FF TO MAKE UP FOR GROWING VALUE
308 * OF 'Y'
309 *
81E4: C0 08 310 DONE? CPY #$08
81E6: 90 EA 311 BCC G1 ; NO
81E8: 60 312 YES RTS
81E9: E7 313 CHK

A4eras
sembli
ngthelis
ti
ng,
BLOADthecha
rac
tersetf
rom c
hapte
r31atl
oca-
ti
on$9000.⇢e nBLOAD t
hechar
act
eredit
orat$8000 (
donotBRUN)andtype
CALL32768from Apple
so4or8000Gfr
om theMonitor(Appl
eso4mustbethe
sel
ecte
dlanguag
e)2
.

2[
CT]Ane
xampl
eBAS
ICpr
ogr
ami
sgi
vena
tthee
ndoft
hec
hapt
er.

]
3
20 As
sembl
yLi
nes

Whe nt hepr ogrami sc all


ed,t hes c
reenwi llcl
eara ndaboxwi t
hama tr
ix
patte
rni nsidei twi llappe ar ,al
ongwi t
ht hec omple t
ec hara ct
ersetloadeda t
$9000.Ifthec hara cte
rsa ppe arscr ambled,rechecktoma kesur eyouhaveloa ded
thecharacters etpr ope r
lya t$9000.
Tos el
ec tac ha r
actert oe dit
, simplypressanynon- controlkey.Ane nlarged
ima g
eoft ha tc ha racters houl da ppeari nthebox .Tomov et heedit
ingc ursor
around,us et hel e4a ndr i
g htarr owst omov el e4a ndr i
ght ,and<CTRL>Ja nd
<CTRL>Kt omov eupa nddown.I fy ouha vea nAppl e/ /
e,thef ourdirect
ional
arrowswi l
la l
s owor k.Eve nonas tandardAppl eI I
,youma yFndi teasi
ertohol d
downt he<CTRL>k eywi t
ht helitt
leFng e
rofy ourle4ha nda ndt henpresstheH,
U, J,
andKk ey swi thyourr i
g htha ndt omov earound.
Pressi
ng<ESCAPE>wi lltogg l
ebi tsint hec hara
c t
erona ndoL.Tos avea
charact
erba ckt ot hetable,pr ess<RETURN>. Ifyouwa nttostartov e
rwithac ha r
-
acter
,si
mpl ypr esst heorig i
na lletterkeyagain.
Tos avet hea lteredtabl eba ckt odisk,s
impl ypress<CTRL>Q, andthent ype:
BSAVE TABLENAME, A$9000, L$300

Youca
nre
pla
ceTABLENAMEwi
tha
nyna
mey
ouwi
sht
ogi
vet
hene
wcha
rac
-
t
ers
et.

Howi
tWor
ks
Alt
houghthel i
sti
nglooksr
athe
rlong ,don’tbediscourag
ed.Asi thappens
,
muchofthelis
tingconsi
stsofr
outi
ne sthatwer epres
ent e
dine a
rli
erc hapt
ers
.
Forex
ample,l
ines2 4
3through313(HCOUT)arethec har
a ct
ergene
ratorthatwas
des
cri
bedinchapter31.
Toseehow t heedit
orworks,l
et’
sFr stconside
rthi sover
vie
w oft hepro-
gra
m:
HOOK:
Hook
supt
hec
har
act
erg
ene
rat
or,
HCOUT,
tot
heout
putv
ect
orss
otha
tthe
hi
-r
esc
har
act
ersc
anbepr
int
ed.
ENTRY:
Cle
arst
hehi
-r
ess
cre
ena
ndi
nit
ial
iz
est
hec
olumna
ndr
owc
ount
erst
o0.
CHRLIST:Pr
int
sal
l96AS
CIIc
har
act
erst
othes
cre
en.We
’l
lex
ami
nepa
rtoft
he
pr
oce
ssi
nde
tai
lshor
tl
y.
MATDSP:
Dra
wst
hema
tri
xpa
tte
rnt
oindi
cat
ewhe
ret
hec
har
act
erwi
llbee
dit
ed.
⇢isi
sal
sotheent
rypoi
ntf
orthee
dit
ingl
oopf
ore
achc
har
act
er.⇢i
sse
c-
t
ionc
anbebrok
endownasf
oll
ows:
BOX:
⇢eApples
o4hi -
resrouti
nesareus
edtodra
waboxwi
thf
ours
tra
ight
li
nes
.⇢i
sfor
mst heboundaryofthemat
rixa
rea
.
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
21

GETROW:Eac
hbyteofthemat
ri
xpa t
ternisre
tri
eve
dhe
re,a
4erwhi
chSCAN
wil
lproces
sanddis
pla
ythei
ndiv
idualbit
s.
SCAN:
⇢iss
ect
ions
hi4se
achbi
toft
her
owi
ntot
hec
arr
yand,
depe
ndi
ngon
whe
the
rit
’ss
et,
dis
pla
ysas
oli
dora
nempt
ybl
ock
.
NXTROW:I
ncr
eme
ntst
her
ow c
ount
er(
theY-
Reg
ist
er)unt
ila
lle
ightr
ows
ha
vebe
endi
spl
aye
d.
CURSOR:Cal
cula
test
hec
urr
entc
urs
orpos
it
ionus
ingCC(
Cur
sorCol
umn)
andCR( Cur
sorRow)
.
CURCALC:⇢ispart
,alongwit
hPRNTCURS,det
ermine
swhe t
herthebi
tatthe
cursorpos
it
ionisse
t.Ifi
tiss
et,awhit
ec ur
sorispr
int
ed;ifnot
,anoutl
ine
ofthecurs
orisdi
spl
ayed.
CMD?:Att
hispoi
ntwearerea
dyt
ogetac
omma ndfr
om thekeyboar
d.⇢egen-
e
ralt
heoryi
store
fres
hthes
cre
enwit
htherout
ine
si nMATDSPeac
hti
mea
c
ommandisent
ere
d.⇢a
twa
ywedon’
tha
vet
oupda
teonl
ypa
rtoft
he
s
cre
ens
pec
iFc
all
y.
I
facont
rolc
haract
erisent
ere
d,itisa
ssumedt
hatitwi
lle
it
herbeadi
rec
-
t
iona
lke
yor<RETURN>,s
ocontr
olispa
ssedt
oEDIT.
Ifanon-
c ontrolchar
acte
risent
ere
d,iti
sas
sumedthatt
hisisachar
act
ert
o
beedit
ed. MOVEthenretri
evest
heeig
htbyt
esfort
hatchara
cterandmove
s
themtothewor ka r
ea(MAT).
EDIT:Ift
heuserpres
ses<RETURN>,ACCEPTwil
lst
oret
hechar
act
erdat
abackin
t
hetabl
e.I
f<ESCAPE>ispres
sed,these
lec
tedbi
twi
thi
nthebyt
efort
hatrow
wi
ll
bet
ogg
led.
I
foneoft
hedirec
ti
onalk
eysi
spr
ess
ed,
thepos
it
ionc
ount
ersCCa
ndCRa
re
a
djus
teda
ccor
dingl
y.
Press
ingacontr
olkeyotherthanthele
galc
omma ndc
har
act
erswi
llg
ener
-
ateaBELLsound.I
na nycase
, a4e
rak e
yisent
ere
d,aj
umpismadebac
kto
MATDSPtos
tartt
heproces
sove rag
ain.

AndNowwi
tht
heMa
gni
fyi
ngGl
ass
⇢epr ece
dingovervi
ews howeding ene r
alhowt heeditorworks
.Nowwe ’
l
l
spendal it
tlemoret i
mee xa
mi ni
ngt hepa rti
cul
artechni
que susedineachrou-
tine.Someoft herouti
nesta
ke nfr
om earl
ie rchapt
erswillnotbedescr
ibedinas
muc hde tai
lasthosepres
entedherefortheFr stti
me .Youma ywis
ht oref
erto
pr e
vioussecti
onsifsomepa r
tsseem diWc ult
.Tohe lpyous canthr
oug htojus
t
thepa rt
sthatint
eres
tyou,eachsect
ioniske yedtotheprecedingove
rvi
ew.

]
3
22 As
sembl
yLi
nes

HOOK
Bystori
ngthea ddr
essoftheHCOUT r
outi
neinCSW a
ndt he
nc a
ll
ingVECT
(
$3EA)
,allf
utur
eoutputwil
lpas
sthroughtheHCOUTr
out
ine,a
ll
owingustopri
nt
t
hehi-
reschara
cte
rsonthescr
een.
ENTRY
⇢i si
sthemainent
rypoi
nttot
heedit
or;
itse
rve
stocl
eart
hehi-
ressc
ree
n
andini
ti
al
izet
hecolumnandrowposi
ti
onofthecur
sort
o0,0(uppe
r-l
e4cor-
neroft
hema t
ri
x).
CHRLIST
Todi s
pla
ya l
loft
heexi
sti
ngcha
rac
ter
s,CHRLISTl
oopsthr
oug
ht hev
alue
s
$20 t
hrough$7F(32t
hroug
h1 27=96charac
ters)
.Be
causewecan’
tpri
nt96
charact
e rsononel ine ,somes or tofa est
heticpl aceme ntisde s
irable.⇢ef or
ma t
chosenwa sagroupof6l inesof1 6c haracterse ach.
STARTi sthebe ginningoft hisl oop( X- Reg i
st
e rse tto$20) ,a ndCH2i sthe
topoft hepr i
ntingl oop.Ani nt ere st
ingpr obl em he reishowt ode terminee ac h
timeweha v
epr inte d1 6cha rac ters.A s eparatec ount erc ouldha vebeenk ept,
butifitwe repossiblet odoamodul of unctionwec oul djus ttestforourc urrent
charact
e rcounterf ormul t
iplesof1 6.Be causemodul or etur nsther ema i
nde rof
adi vi
si
on, wewoul de xpectar esul tof0e acht i
met hec ount erwa sa tamul ti
pl e
of1 6or,inotherwor ds,hadj us tFni she danot herlineof1 6c haracters.
Asi thappens,t heANDi nstr uctionc anbeus edtope rf
ormt hee quival
entofa
modul of oranypowe rof2 .⇢et echniquei st odoa nANDwi t
ht hev aluey ou
wa ntmi nus1.Be caus e16isapowe rof2( 24=1 6),wene edonl ydoa nAND#$0F
foll
owedbyaBNEt ot estfore achc ompl etedl i
neof1 6c harac te
rs.
Ifal ineha sbe enFni she d,ac arriager eturni spr inted,f oll
owe dbyt he
equival
e ntofanHTAB20.
Not ic
ethata se achc haract eri spr i
ntedt hehi ghbi tiss etwi thanORA#$80.
⇢i sistoma keCOUTha ppy ,asi ta lwa ysexpe ctsthehi ghbi tt obes etonc hara c-
terstobepr i
nted.
MATDSP
⇢i ssect
ionbe ginsthepartthatc re
atesthema t
rixdisplayusedi ne di
ting
theindividualc haracte
rs.⇢i ssecti
onwi llbee xecutede achtimeac omma nd
char
a ct
erisente red.
⇢eFr stpa r
t,BOX,d r
awsaboxout lini
ngt hec haracterimag eusingt he
Appleso4HLINr outines.
Onc etheboxi sdrawn,theindividualbytesmus tbedi s
playedwi ththest a-
tusofe achbi tindi c
a t
ed.⇢ea l
gorithm istos cant hroughe achbi tpos i
ti
on,
pri
ntingas pacei fthebitisclea
ra ndpr i
ntingar ubout( $FF)ifthebitiss e
t.I n
thepr evi
ousc hapter’scharact
ert able
,ar uboutwa sas ol
id block,sot hi s
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
23

approachs houldwor k.( Not ethatifyoue di tthespaceort her uboutc harac


ter
,
thema tr
ixpa tt
ernwill bea l
ter
eda c
cording l
y .)
⇢e rea reatotalofe ightby t
estober etrievedanddi spla
y edfore achc hara
c-
ter.GR1ist hesecti
ont ha tdoesthee quivale ntofanHTAB5( forpr ope rscr
een
placement )andthenl oa dsaby t
efrom thewor kareaMAT( seeline2 25).Onc ea
byteisretrieve
d,SCANu se stheLSRinstructiont oshi4abi tintot hecar rySag.I
f
thec a
rryiss e
t,arubout( $FF) i
spri
nt e
d; othe rwis
eas pace($A0) isprinted.
Becaus etheAc cumul a
torwi l
lbeus edt oprintac haracterviaCOUT,t he
shi4edby teispres
ervedbypus hingitont ot hestackonl i
ne9 8a ndl aterpull
ing
itbackoLonl i
ne1 03.
A4e re ac
hs e
v e
nbi tsa r
e“ pri
nted,
”ac arr i
ageret
urni sprinte donl ines107
and1 08a ndtheloopi sr epea t
eduntilal
l ei
g htby t
esha v
ebe endi spla
y ed.
CURSOR
Onc ethec hara cterma t
rixha sbee ndisplay
e d, wene edtodi s
playthec urs
or.
Lines115t hroug h1 23us ethec ursorr owa ndc ol umn( CRa ndCC)t ocalcul
a t
e
theHTAB, VTABp os i
tion. Reme mbe rtha ts
incewea remi rror
ingac t
ionstakenon
thetextpa gewec ana l
sous et het extpa geasaf r
a meofr ef
erenceforhi -
res
scre
enope r
ations.
STATUSi susedt or e
adthepa rt
icularbitthatc orrespondst othecurrentcur-
sorposit
ion. Not et hatCR( CursorRow)c onvenient lyisequaltowhi cheverby t
e
i
nt heindi vi
dua lc hara c
terdeFni t
ionwewi l
lne edt or ead,andt hatCC( Cursor
Column)de termine show ma nybi tsne edt obes hi4e doutt oputt heoneof
i
nt e
restintot hec arrySa g.De pendi ngonwhe the rthebi tisclearors et,the
Ac c
umul atorwi l
l bel oadedwi tha$00or$08, thepur poseofwhi chwi l
lbecome
i
mme diatel
yobv ious .
PRNTCURS
Si
nceCHa ndCV( $24,$25) hav ebe e nsetup, wec anuseas pec
ialform ofthe
HCOUTrouti
ne,calle
dPRNTCURS, topr intas ma ll
erbloc korablockoutline.You’l
l
noti
cethatthehi-rescharacterg enera tora tHCOUTha sbe e
nmodi Feds l
ig
htlyto
usethepointe
rPOSN( $3C,$3D)t opoi ntt ot hedatat a
ble.Ouror i
gi
na lcharact
er
generat
oralwaysa s
sume dthatthet a blewoul dbea t$9000. Normall
yHCOUTs et
s
POSNtopointat$9000onl ines2 78t hr ough2 85
.
WithPOSNse tuptopoi ntatas pec ial
two- characterdeFni
tiontableonlines
227through2 43,thePUTBYTEr outinewi lldot hee quival
entofprintingoneof
thetwone c
essa
ryspecialcharact
ersatthecur sorposi
tion.
Youma ywi shtoc ompa retheHCOUTr outi
nec ontainedint heedi
torwith
theprevi
ouschapter’
sc haract
ergeneratortos eewha tcha nge
sha vebeenma de
tofaci
li
tat
ethecall
ingbyt hePRNTCURSr outine.
An inte
rest
ing digressi
on:By a voidingCOUT a nd wr it
ingt othescreen
dir
e c
tl
y,wea r
eont heve r
geofbe i
nga bletodobl ocksha pes,
at e
chniqueuse
di n
ma nyhi-
resarca
de-typega mes.

]
3
24 As
sembl
yLi
nes

CMD?
⇢epr oce
ssingoft hec omma ndc ha r
a c
ter
sisdonei nt hissection.⇢ec har-
acte
ri sreadfrom t hek eyboardus i
ngt heMoni torr outineRDKEY( $FD0C).⇢i s
routi
newi llpl
acet heAS CI Ival
ueforthek e
ypr e
ssedi ntotheAc cumul ator.
⇢eFr stma jordi s
tincti
ont obema deiswhe the rac ont r
olc haracte
rha s
beenpr es
sed.Lines1 45a nd1 46dot hi
s ,
forwardinganyc ontrolcha racterstothe
EDITs ec
tion.
I
fanon- c
ont rolcharacterhasbe e
npr esse
d,theus e
rwa ntstoe ditthatchar-
acte
r.CHARa ndMOVEus et heAS CIIvalueofthek eypr ess
e dtoc al
c ulatethepos i
-
ti
onoft heda t
aoft hatcha ra
cteri
nt het abl
e,thenmov et hatdatai ntot hewor k
area,MAT.A4e rthemov e,aj umpi sma deba cktoMATDSPt orefresht hedi s
play
withthene wcha racte
ra ndt ogetthene xtcomma ndk ey.
EDIT
Ifac ont rol ke yispr e
ssed,oneofanumbe roffunc t
ionsmus tbepe rforme d.
Wewi llcons ide rthe seint heor de rtheya reexecuted.
Re t
ur n: ⇢i simpl ie sthattheus erwa ntstoa cc
eptt hec ha r
actera sdis pl
a yed
andc opyi tba c ki ntot hec haractert able. ⇢i sisdonebye ssential
lyr eversingt he
proc essus edbyCHARa ndMOVE( lines1 4 7t hrough1 53).
To ggle:If<ESCAPE>i spressed, thea ppr opriat
ebi tpositi
onmus tbes wi t
che d
toitsoppos it
ec ondi ti
on–oLt oonoront ooL. ⇢i sisdonebyc reatingama sk
bytewi tht hepr ope rbi tset.Todot hi s,thec arrySagi sseta ndtheAc c umul ator
loade dwi tha0 .Whe na nROLi sdone ,t hissetbitwi llbes hi4e dthr oug ht he
Acc umul ator.Bydoi ngt heROLag ivennumbe roft ime s(de t
ermi nedbyCC)we
cans e tag ivenbi tint heMASKby te($08) .
Onc ethema skha sbe enc r
e ated, wene edonlyr etri
evet hepr operby tefrom
thewor ka rea( dete r
mi ne dbyCR)a ndt henma skitwi t
ht heMASKby t
e( l
ines1 78
throug h1 8 0)
.Onc et hi sisdone ,wea g ainjumpba c ktoMATDSPt or efresht he
displaya ndg e tthene xtc haracter.
Cur sorc o ntro :Tomov
l et hec ursora round,we l
lus et hefourdi r
e ct
iona l
keysont heAppl e/ /ek eyboard.Ev eni fy oudon’ tha vea/ /e,youc ang enera t
e
thes amec hara ctersi nt hema nne rme ntione dearl
ierint hisc hapter.Tor efresh
yourme mor y ,t hek ey swe ’l
lus ewi l
lbe<CTRL>H,<CTRL>U,<CTRL>J,a nd
<CTRL>K, f
orl e4, right, down, andupr e spe c
tivel
y.
⇢ec odeonl ine s1 85t hroug h2 1 9i sfairl
ys t
r a
ightforwa rd.⇢eupa nd
down mot ionsa redonebyi nc reme nt i
ngorde creme ntingt hec ur sorr ow
count e r
;l e4a ndr i
g htmot i
onsa redonebyi ncreme ntingorde creme ntingt he
cursorc olumnc ount er. Allmot ionswr a pa r
ound.
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
25

Runni
ngt
heEdi
tor
⇢ef ol
lowingAppl
eso4progra
mc anbeuse
dtoloa
dac har
act
ers
et,
runt
he
c
har
act
eredit
or,andt
hensav
et hecha
ract
ers
eta4e
rexi
ting
.3

10 PRINT CHR$(21): REM 40-COLUMN


20 PRINT "CHAR TABLE FILE, <RETURN> FOR DEFAULT": INPUT A$
30 IF LEN (A$) = 0 THEN A$ = "AL31.ASCII"
40 PRINT CHR$ (4);"BLOAD ";A$
50 PRINT CHR$(4);"BLOAD AL32.CHAREDIT,A$8000"
60 VTAB 21
70 PRINT "PRESS A KEY TO EDIT THAT CHARACTER"
80 PRINT "<CTRL>H,U,J,K OR ARROW KEYS TO MOVE"
90 PRINT "<ESC> TO TOGGLE A BIT, <RETURN> TO SAVE"
100 PRINT "<CTRL>Q TO QUIT AND SAVE TO A FILE";
110 REM IF DOS 3.3 THEN SET UP CSW VECTOR
120 IF PEEK(1002) = 76 THEN CALL 32768: GOTO 150
130 REM IF PRODOS, SET UP OUTPUT LINK AT $BE30,31
140 POKE 48688,129: POKE 48689,129: CALL 32779
150 TEXT: PRINT: PRINT
160 PRINT "FILENAME TO SAVE, OR <RETURN> TO EXIT": INPUT A$
170 IF LEN(A$) > 0 THEN PRINT CHR$ (4);"BSAVE ";A$;",A$9000,L$300"
180 END

3[
CT]⇢eAppl
eso4pr
ogr
ami
sne
wtot
hise
dit
ion.S
imi
la
rtoc
hapt
er3
1,f
orPr
oDOS
t
heoutputv
ect
orat$BE30,$BE31isdi
rec
tl
ycha
nge
dtopoi
ntt
other
out
inea
tHCOUT
(
$8181).
⇢ema inpr
ograma t$800Bi
sthe
nrun.

]
3
26 As
sembl
yLi
nes

Mi
sce
ll
ane
ousNot
es
Althoug ht he ycanbedi spl
ayed,lowe r-cas
ec hara c
tersma ynotbee asyto
edi
tbe causet he yarenote asi
lyge
ne ratedf rom theAppl eIIkeyboard.Apple//
e
owne rswillha venot r
ouble.Iti
spos siblet ouset hel owercaseinputrouti
ne
desc
ribe dina ne arl
ierchapte
rtog enera t
elowe rcasec haract
ersfrom asta
nda r
d
AppleI Ikey boa rd.Si
mpl yacti
vat
ether outinepr i
ort oc al
li
ngt hecharac
teredi-
tor
.⇢e<ESCAPE> a nd<SHIFT>func tionswi l
lcontinuet owor kproper
ly,pre-
suma bl
ywi thnoi l
leLectsontheeditorr outines
.
Itiswor thnot i
ngtha tt
hecharac t
ers etsuseda ndc reatedbyt hi
sedit
ora r
e
ide
nticalinf orma ttotheDOSTo o
lKi tAni ma t
rxc
i haracte
rs ets
,alt
houghthe
char
a ctereditorpr ovidedwiththatpa c
k agedoe sha veoneort womi nor,
though
notinconse que ntial
,fea
turesnotavaila
bl eint hi
seditor .

Conc
lus
ion
⇢isconcludesourdi sc
ussi
onofhi-rescharacte
rgenerat
ionande dit
ing;i
t
shoul
dprovideyouwi ththebasi
cprinci
plesofthesetec
hniques.⇢eideaca nbe
exte
ndedintoblockg raphic
sforarc
a de
-st
y l
ega mesora simprove
me ntstothe
artofhi
-rescharact
erg ener
ati
on.Youmi ght,forexample,wanttoexperiment
withove
rsi
z el
ette
rs,col
or e
dtext
,orsimpleanima t
ion.
3
3
3
3. ⇠e6
5C0
2
J
une1
983

⇢i slas tcha pt erde a l


swi thane wv e rsionofourbe lov ed6 50 2mi c ropr oc es-
sorknowna st he6 5C0 2. Althoug hthec hipha sj ustbe enr el
e ase dwi thint hel ast
few mont hsa ndha sy e ttoFndi tswa yi nt ot hema ins tream ofc omput e rs,it
seemsl ik e
lyt ha twe ’l
lbehe aringmor ea boutt hisitemi nt heupc omi ngy ea r.
Be forej umpi ngr i
ghti nt oi tsne wf unc t
ions ,thoug h,l et ’
sFr stg etal it
tle
backgr oundi nfor ma ti
onoutoft hewa y.
⇢e6 5 02wa sde s
igne dbyChuc kPe ddl ea ndBi llMe nschofMOSTe c hnol -
ogy( nowowne dbyCommodor eBus ine ssMa chines)a nd,a soft hepr esent ,7 0
percentofi tsus ei sbyAppl e, At ari
, andCommodor e 1⇢ec
. ur r entma nuf a ctur-
ersoft he6 5 02a reRoc kwe llInt erna t
iona l,MOSTe c hnol ogy ,a ndS yne rtek .As
some time sha ppe nswi t
ht heset hing s,thoug h, someoft hek eype rsonsi nv olve d
witht he6 50 2we ntt owor ka tane wc ompa ny ,We s ternDe sig nCe nt er 2⇢i
. s
compa ny ,then, ist heor igi
na lsour ceoft hene w6 5C0 2c hip. Butt hes tor ydoe s-
n’tendt he re.We ste r
nDe s
ignCe nterha sl icens edt hede si
g nt oa tle astt hr ee
indepe nde ntma nuf act
ur ers:Roc k we l
lI nte rnational,GTE,a nd NCR.⇢e se
compa niest ookt hei nitial65C0 2de signa nda dde dthe irowne nha nceme nt s.
⇢epi ctur ea tt hispoi ntist hate achoft hesec ompa ni eswi ll bema rk etingi ts
ownv ersionoft he6 5C0 2. ⇢ec hipsa r
emor eorl esst hes ame , butt heRoc k we l
l
chipha st hel arg esti nst
r ucti
ons et.
“La rges tinst ruc ti
ons e t
,”y oua sk ?Ye s!⇢ene w6 5 C0 2ha sha dtheol d6 50 2
inst
ruc tions e
ta ppe nde dwi thav a ri
etyofne wi nstruc tions .Be causet heRoc k-
we l
lchi pa ppe a rst obeas upe rsetofa lloft heot he rchi ps ,thebul koft hisc hap-
terwilla ssumet ha tit’
st heonet ha t’
sbe ingus ed. Atthee ndoft hischa pt erwe ’l
l
describedi Le renc e sa mongt het hre echi ps.
⇢eRoc kwe llc hipha sat otaloft we lvene wi ns truc ti
onsa ndt wone w
addres si
ngmode s.I na ddi t
ion,anumbe rofa ddressingmode snotpr e vious ly
avail
abl etoa ni ns truction( suc ha st hei mme diatemodef ort heBITi ns t
r uc ti
on)
arenowa va il
able .⇢e rea r
eat otalof5 9a c t
ua lne wopc ode s.⇢eme aningofa l
l
ofthes enumbe rswi llbe comec lears hortly .

1[
CT]Cor
rec
tedf
rom t
heor
igi
nala
rti
cl
e,whi
chl
is
tedCommodor
eBus
ine
ssMa
chi
nes
a
stheor
igi
nal
des
igne
r.
2[
CT]S
pec
iFc
all
y,Bi
ll
Mens
ch,
thede
sig
neroft
he6
5C0
2.

]
3
28 As
sembl
yLi
nes

Ne
wAddr
ess
ingMode
s
Sincet hisisoneoft hes mal
lernumbe rs
,let’
sstarther
e .You’l
lre
cal
lfrom
ma nye arl
ierdiscuss
ionst hateach6 50
2i nst
ructi
onha supt os i
xaddress
ing
mode s.⇢a tnumbe risarri
v edatbycountingsomemode sasme revar
iat
ionsof
othersa ndnoti ncludingthev a
lue( r
ela
tiv
ea ddres
sing)associa
tedwithbranch
instr
uctions( BEQ,BNE,BCC,BCS,a nds oon)a sana ddres
singmodehe re
.To
refre
shy ourme mory,al i
stofmode sandv ari
ati
onsi sprovidedinthet a
ble
belowf ortheLDA( LoaDAc cumulator
)inst
ructi
on.

Addre
ssi
ngMode CommonSy
nta
x
1.Absol
ute LDA $1234
ZeroPag
e LDA $12
2.Immedia
te LDA #$12
3.Absol
ute
,X LDA $1234,X
ZeroPag
e,X LDA $12,X
4.Absol
ute
,Y LDA $1234,Y
5.(I
ndir
ect
,X) LDA ($12,X)
6.(I
ndir
ect
),Y LDA ($12),Y

I
ndi
rec
tAddr
ess
ing
⇢eFr stofthet wone wa ddressi
ngmode sisqui teeasytoe xpl
a i
nbe causei t
ise s
senti
allyanothe rvaria
tionofa ne xi
sti
ngmode .⇢ene w modei sindire ct
addressi
ng.⇢i sma ys oundv eryfami l
iarbe causet hisiss i
mi lartot heinstruc-
tionsusedt oaccessme mor ylocati
onsv i
aaz er
o- pagepoi nter.Us ua l
ly
,thoug h,
theY- Regi
sterissetto0ors omeot herv a
lue,whi chi sthena dde dt othea ddres s
indicat
edbyt hez e
ro- pagepointertode t
ermi net hea ddressofi nterest
.
⇢i sisFnef ora ddress
ingal ar
g etabl
eofda ta,butma nyt ime swea reinte r-
estedinonl yoneby t
eofme morya ndmus tthe ng ot hroug htheobl i
gatoryLDY
#$00 topr operlyc onditi
ont heY- Re gis
ter
.( S
e ee ntrie
s5a nd6i nthet abl e
abov e
.)
⇢ene wi nstr
uc ti
ona ll
owsust oignoret hec ont entsoft heY- Re gi
stera nd
ga i
nac c
esstotheme mor ylocat
iondi rect
ly.⇢i sc ons erv
e stwoby tesofcodef or
ea c
hr ef
erence,becausetheY- Registerdoesnotha vet obel oade d.Ifyouwa ntt o
scanabl ockofme mor y,sucha sforat able,thisi nstruct
ions tillcanbeus e
di f
youa rewill
ingtoINCorDECt hezero-pagepoi ntera ccordingly.

Addres
singMode CommonSy
nta
x
7.Indi
rect LDA ($12)

⇢i
sne
waddr
ess
ingmodei
sav
ail
abl
efort
hei
nst
ruc
ti
onsl
is
tedbe
low.
3
3.⇢e6
5C0
2 3
29

I
nst
ruct
ionswit
h
I
ndir
ectAddre
ssi
ng Opc
ode
ADC ($12) 72
AND ($12) 32
CMP ($12) D2
EOR ($12) 52
LDA ($12) B2
ORA ($12) 12
SBC ($12) F2
STA ($12) 92

I
nde
xedAbs
olut
eIndi
rec
t
⇢es econd ne wa ddr essingmodeha sana met hatobv i
ous lywa snot
designedwi the asyr ecallinmi nd. For tunate l
y,thist ooi sav ar i
ati
onona ne x i
s t
-
ingthe mea nda ss uc hs houl dbee asyt or eme mbe r. Inthepa st
,weha di nde xed
indir
e cta ddr essing .Wec al
ledt hismodepr e -i
nde xedf orc l
ari
ty ’
ss ake.An
exampl ewoul dbeLDA($22,X). Pr e-inde xingme a nst hatthec onte ntsoftheX-
Registera r ea ddedt ot hea ddr essoft hez ero-pa ger e f
erenc ebefor eusingt he
sum oft hos enumbe r
st ode termi newhi chz e ro-pa gepa irt ous e
.Fore xampl e
,
theins t
r uc t
ionLDA($22,X), whe ret heX- Registerhe l
dt hev a l
ue4 , woulda ctu-
all
yus eby tes$26, $27t og e
ttheFna lde st
ina t
iona ddr ess.
⇢i sdi Lersfr om i ndi rectinde x ed, whichwer eferredt oa spost- i
ndex i
ng .In
post-inde xing,t hev a
lueoft heY- Re gis
teri sa dde da 4ert heba sea ddressi s
determi ne d.Fore xa mpl e ,i
nt hei nstruc t
ionLDA($22),Y,whe ret heY- Reg i
ster
holdst hev a l
ue4a nd$22,$23 poi ntt ol ocation$1000,t heme mor yloc ation
accessedwoul dbe$1004.
You’ ll r
e c
a l
l alsot ha tpre-a ndpos t-i
nde xingwe rel i
mi tedint he i
ruseoft he
X-a ndY- Re gisters.Pr e-inde xingc ouldus eonl yt heX- Registerandpos t
- i
nde x-
ingonl yt heY- Re gister.Be forey oug ettooe x citedi na ntici
pa ti
ngt hepos sibili
-
tie
soft hene wi nst ruction, restrainy our sel
f :⇢i smuc hha snotc hang ed.
Wha tha sc ha ng edi st ha tpr e-inde xi
ngi snol ong erlimi t
edt oz ero-pa ge
pointers.⇢ene wmodea l
lowsa nyt wo-by tev aluet obeus ed.⇢i sme anst hat
theX- Re g i
sterc anbea dde dt otheba sea ddres sofat a
bleofme mor ypoi nters
thatpr e
v ious l
yc oul dha vebe enl oc atedonl yont hez eropa geofme mor y.

Addres
singMode CommonSy
nta
x
8.IndexedAbs
olut
eIndi
rec
t JMP ($1234,X)

Fore
xampl
e,suppos
eyouhadacomma ndinte
rpret
ert
ha ta
ccepte
dac om-
mandval
uebet
we e
n0a nd2.Withthe65C02,suc
ha nint
erpre
tercannowbe
use
dinconj
unc
tionwithaJMPt
ableloc
ate
danywhe r
einme mory,const
ruct
ed
asi
nthef
oll
owi
nge xa
mple:

]
3
30 As
sembl
yLi
nes

JMP DATA TABLE:


1200: 80 10
1202: A0 10
1204: CO 11

1 ********************************
2 * AL33-SAMPLE COMMAND PROCESSOR*
3 ********************************
4 XC ; MERLIN: ALLOW 65C02 OPCODES
5 ORG $1000
6 TABLE EQU $1200
7 *
1000: 20 00 40 8 ENTRY JSR GETCMD ; GET VALUE FROM 0-2
1003: 0A 9 ASL ; MULTIPLY BY 2
1004: AA 10 TAX ; PUT IN X-REGISTER
1005: 7C 00 12 11 GO JMP (TABLE,X) ; EXECUTE PROPER ROUTINE
12 *
13 * ...MORE CODE HERE...
1080: EA 50 CMD1 NOP ; FIRST ROUTINE
51 * ...MORE CODE HERE...
10A0: EA 100 CMD2 NOP ; SECOND ROUTINE
101 * ...MORE CODE HERE...
11C0: EA 150 CMD3 NOP ; THIRD ROUTINE
151 * ...MORE CODE HERE...

⇢isi
saveryf
astandeLe
cti
vet
echnique
.⇢ef
oll
owi
ngt
abl
eshowst
heone
i
nst
ruc
ti
ontha
tcanusethi
snewmode.3

I
nde
xedAbs
olut
eIndi
rec
tAddr
ess
ing Opc
ode
JMP ($1234,X) 7C

Ne
w“St
anda
rd”Addr
ess
ingMode
s
⇢e rea reaf ewi nstructi
onst hathaveaddre s
si
ngmode sthata r
ene wj ustto
the m. Fore xampl e,twooft hemos texci
ti
ngone sareINCa ndDEC.
Previously ,a nyus esofINC a ndDECwe rel i
mitedtome mor yl ocations.In
addi ti
on( sot os pe ak),us i
ngt heX-a ndY- Re g
iste
rswa stheonlywa ytoma in-
tainas i
mpl eloopc ounterwi thoutusingade dicat
edme mor ylocation.⇢es ur-
pr iseherei st hatINC a ndDECwi llnowwor kont heAc c
umul ator.⇢i si snice
be causeyouc annowma i
ntainac ounterintheAc cumulator,
ore vendof udging
ofSa gval
ue sa st heya rebe i
ngha ndledintheAc cumul a
tor.
Somef utur ea ssemble r
sma yr equir
ethe“ some whatusual”(ifnoti nconve-
nie nt)useofDECAorINCAa stheyseemt opreferforLSR,ASL,andot he rope r
a-
tionsont heAc cumul at
or.
⇢ eBITi ns tructiona ls
oa ll
owss omea ddit
ionaladdressi
ngmode st hatma y
pr oveus e
f ul.
Pr eviously,theBITi nstr
ucti
ons upportedonlyabsolutea ddressi
ng.
3[
CT]⇢eor
igi
nala
rti
cl
einc
orr
ect
lyl
is
tede
ightot
heri
nst
ruc
ti
ons
:ADC,AND,CMP,EOR,
LDA,
ORA,
SBC,
STA.
Ina
ddi
ti
on,
thec
odee
xampl
eha
sbe
enc
orr
ect
edt
ous
ethene
wJMP
i
nst
ruc
ti
on.
3
3.⇢e6
5C0
2 3
31

⇢atist
osayt
hatadir
ect
lyref
ere
ncedme mor
yloc
ati
onwa
sus
eda
sthev
alue
a
gai
nstwhi
cht
heAccumul
atorwasoper
ate
don.

Addres
singMode CommonSy
nta
x
Absol
ute BIT $1234
Zer
oPa g
e BIT $12

⇢i sisuse
fulfortes
tingame morylocat
ionforagi
venbitpatt
ern,butnot
direc
tl
ys uit
abl
ef orte
sti
ngthebitpa
tter
noft heAc c
umul
ator.Formanyope r
a-
tions
,thismeansy ouhavetorat
herart
iFci
all
yloadsomeme morylocat
ionwith
thevalueyouwa ntedtocomparetot
heAc cumulator
.
⇢ene w65C0 2supportst
hreenewa ddres
si
ngmode sfortheBITinstr
uc-
tion:

Addres
singMode CommonSy
nta
x Opc
ode
Immedia
te BIT #$12 89
Absol
ute
,X BIT $1234,X 3C
Zer
oPa g
e BIT $12,X 34

AtLa
st,
theRe
alSc
oop!
NewI
nst
ruc
tions
Ofcourse,there
alquest
ionl
urki
nginev e
ryone’
smindis:“Butwhatarethe
newinst
ruct
ions?”
⇢eg reatthinga
boutthe65C02i
sthatnotonlyaremanyoftheoldinst
ruc-
ti
onsenhanced,butthereal
soareanumbe rofabsol
utel
yterr
iFcnewi nst
ruc-
ti
ons–twelv
e,tobeex a
ct.⇢enewinst
ructi
onsareshowninthetabl
ebelow.

I
nst
ruction4 De s
cript
ion Opc ode
BBR BranchonBi tRe s
et(cl
ear
) 0F 1F 2F 3F 4F 5F 6F 7F
BBS BranchonBi tSet 8F 9F AF BF CF DF EF FF
BRA BRanchAl wa ys 80
PHX PusHXont os t
ack DA
PHY PusHYont ostack 5A
PLX PulLXf rom stac
k FA
PLY PulLYf rom sta
ck 7A
RMB Reset(cl
ear)Me moryBit 07 17 27 37 47 57 67 77
SMB SetMe moryBi t 87 97 A7 B7 C7 D7 E7 F7
STZ SToreZe ro 64 74 9C 9E
TRB TestandRe set(cl
ear
)Bit 14 1C
TSB TestandS etBit 04 0C

4[
CT]⇢eBBR,BBS,RMB,a
ndSMBi
nst
ruc
ti
onsa
ppa
rent
lywe
rene
vera
vai
la
bleona
ny
6
5C0
2chi
psus
edbyAppl
e.

]
3
32 As
sembl
yLi
nes

Sowha
texact
lydot
hes
eins
truc
ti
onsdo?We
ll
,le
t’
sex
ami
nes
omeoft
he
e
asyone
sFrs
t.
..
PHX,
PHY,
PLX,
andPLY
Comma ndsf orpushi
ngaby teontothestac
ka ndpull
ingaby teoLofthe
s
tackexis
tfort heAc cumula
torbutnotfortheX-a ndY-Re g
ist
ersinthe6502
.
Oneofthemor ecommonus e
sforthesta
ckistosaveal
loftheregis
ter
spri
orto
goi
ngintoar outinesothateveryt
hingcanber e
stor
edjustpriortoexi
ti
ng.
Ordi
naril
y,t
os a v
et heAccumulat
or,X-Regi
st
er,andY-Regi
ster
,we ’
dhavet
odo
s
ome t
hingli
k ethis:
ENTRY PHA ; SAVE A
TXA ; PUT X IN A
PHA ; SAVE IT
TYA ; PUT Y IN A
PHA ; SAVE IT
WORK NOP ; YOUR PROGRAM HERE
DONE PLA ; GET Y
TAY ; PUT IT BACK
PLA ; GET X
TAX ; PUT IT BACK
PLA ; GET A
EXIT RTS

⇢epr obl
emi scompl
icate
de ve
nfurtherinprog
ramslik
ethechar
acte
rgen-
er
atorli
st
e dinc hapte
r31.⇢e r
eweha dt or e
fertotheorig
ina
lv a
lueofthe
Accumulat
orseveralti
mes,a
ndt hi
sinte
rfer
edwi t
hanysimplewaytopushal
lof
t
her e
gis
terdataontothest
ack.
Wit
hthene w6 5C02,
thiscoul
da l
lberesol
vedwiththef
oll
owing:
ENTRY PHX ; SAVE X
PHY ; SAVE Y
PHA ; SAVE A
WORK NOP ; YOUR PROGRAM HERE
DONE PLA ; GET A
PLY ; GET Y
PLX ; GET X
EXIT RTS

Al
lfoura
reone
-by
tec
omma
nds
,addr
ess
ingonl
ythei
ndi
cat
edr
egi
st
er.
BRA
BRA(
bra
ncha
lwa
ys)i
soneoft
hos
eins
truc
ti
onst
hatwi
llt
hri
llwr
it
ersof
re
loca
tabl
ec ode.Oneoft
hete
chniquesf
orwriti
ngc odetha
tisl
oca
tion-
inde
-
pende
ntinvolve
stheus
eofaf
orce
dbr a
nchins
truct
ion,suc
has:
CLC ; CLEAR CARRY
BCC LABEL ; ALWAYS
3
3.⇢e6
5C0
2 3
33

Unf ort
una tel
y,thisme answemus tforcesomeSa goft heS tat
usRe gi
ster
,
whichma ynotbec onv enienta tthet i
me .Ina ddit
ion,t hepr ocesstake
supa n
extr
aby teonmos toc casions.
BRA all
eviatesbot hoft hesepr oblemsbya l
wa y
sbr anchingt othede s
ired
address,subje
c tofc ours etotheus uall
imi t
ati
onsofpl usormi nus1 28bytesas
thema ximum br a
nc hingdi sta
nc e.
Itisworthme ntioning,int heinterestofprog r
ammi ngs tyl
e,thatma nypeo-
pleindiscri
mi na t
elyus eaJMPt og oba ckt othet opofal oopwhe nabr anch
inst
ructionwoul ddot het r
ick;t hi
sonl ya ddsonemor el i
mi t
ati
ont otheFna l
codei nthepr ocess.Hope f
ully,thisne wbr anchinstructionwi llencouragepeo-
pletoma kethe i
rc odemor el oc at
ion-independent .
BRA,l i
ket her e
stoft he
branchi nst
ructionsont he65 C0 2,usesonlyr e
lati
vea ddressing.
STZ
STZ(
STor
eZe
ro)i
sus
edf
orz
eroi
ngoutme
mor
yby
teswi
thoutc
hang
ing
thecont ent
sofanyoft her e
g i
sters.
Ma nytimesitisne ce
ssaryt osetanumbe rofinternalprogra
mr egis
tersto0
beforepr oce
edingwi thther out i
ne .⇢i sisespecial
lyne ededinma thema t
ical
routi
ne ssuchasmul t
iplic
a t
iona nddi vis
ion.
Ordi na
ril
ythisisdonebyl oa dingtheAc cumul a
torwi th0andt he nstori
ng
thatvalueintheappropr i
ateme mor ylocat
ions.⇢i siseasytodowhe ny ouhave
toloadt heAccumul ator,X-Reg is
ter,orY- Regi
st
erwi th0a ny way.⇢epr oblem
isthatonoc cas
iontheonl yreasononeoft heregis
tersisloadedwi th0isbe ca
use
ofthene edtozeroame mor yloca t
ion.
STZa ll
owsustoz e r
oouta nyme moryby t
ewi thouta l
teri
ngc urr
entr egi
ste
r
contents.Nota l
loft hea ddressingmode susuall
ya vail
abletot heSTA,STX,or
STYins tr
ucti
onsa r
ea v a
il
ablewi thSTZ,t hough.⇢ef oll
owi ngtableshowswha t
mode sa reava
il
able.

STZAddr
ess
ingMode
s CommonSy
nta
x
Absol
ute STZ $1234
Zer
oPa g
e STZ $12
Absol
ute
,X STZ $1234,X
Zer
oPa g
e,X STZ $12,X

SMBandRMB
SMBandRMB(
Set
/Re
setMe
mor
yBi
t)wi
lla
ll
owy
out
ose
torc
lea
rag
ive
nbi
t
ofaby
tei
nme mory
.Prev
iousl
ythi
swoul
dhaver
equi
redt
hre
ese
par
atei
nst
ruc-
ti
onst
oac
hie
vethesameres
ult
.Forex
ampl
e:
LDA MEMORY ; LOAD VALUE FROM MEMORY
AND #$7F ; %0111 1111 IS PATTERN NEEDED TO CLEAR BIT 7
STA MEMORY ; PUT IT BACK

]
3
34 As
sembl
yLi
nes

Wi
tht
hene
wins
truc
ti
on,
wec
ana
ccompl
is
hthes
amet
hingwi
th:
RMB7 MEMORY ; RESET (CLEAR) BIT 7 OF MEMORY

ors
ett
hebi
tag
ainwi
th:
SMB7 MEMORY ; SET BIT 7 OF MEMORY

⇢e r
ea r
etwoi nter
est
ingt hingstonotehere.⇢eFr stisthatforsomer ea-
sontheterm“ r
eset”isuse
di nsteadof“cle
ar”toindicat
et hezeroi
ngofag ive
n
bit
.⇢es econdi t
emi sthatwenow ha vefour-char
acterinstr
uction codes
(mnemoni cs
),t
hel astcharact
e rbeingthenumbe rofthebi tbeinga ct
edon.
Wha tproble
mst hi
sma yc auseins omea s
semblersremainstobes een,butthis
news pe
ciesofi
ns t
ructi
ons ee
mst ohavearr
ive
d.5⇢e s
eins t
ruct
ionsa rel
imited
tozero-
pageaddressi
ngonly.
BBSandBBR
BBSandBBR(
Bra
nchonBi
tSe
t/Re
set
)ar
etwone
wbr
anc
hins
truc
ti
onst
hat
ma keitpos si
bletot esta nybi tofaz e r
o-pagel ocati
ona ndt henbr anchde pend-
ingoni t
sc ondi t
ion.⇢i sinstruc ti
onwi l
lbev er
yus efulfortes t
ingSa g
si npro-
gramst ha tne edt o pa ck Sa g -typeda tai ntoa sf ew by t
esa spossible.By
tra
ns f
er r
ingI /
O de vicer e gi
ster stoz eropa g e
,i tisalsopos sibl
et otestbi t
sin
theseregiste r
sdirectlyfors tatus -bi
tc ondit
ions .
⇢e sei nstr
uctionsa r eve rys imilarinbot ha ppearancea ndus agetot heSMB
andRMBi nstructi
onsj us tdis cus sed.⇢e y,t oo,us efour-chara c
termne moni cs
.
⇢edi Ler ence,ofc ourse, istha twea retes
tingbi tsta
tusr atherthanc hangingit
.
⇢e s
ea r
et hree-
by teins tructions ,theFr stby tebe i
ngt heopc ode,thes econd
beingt heby t
et ote s
t,a ndt het hirdbe i
ngar elati
vebr anchv a l
ue.Ina ssembly
,
thesecomma ndsa ctuall
ywi l
l requi retwolabe l
s 6
!
Oneoft heFr sta ppl i
c a
tionsi sthet e
stingofwhe t
he ranumbe risoddor
even.Pr evious l
y,thisha dt obedonewi thanLSRorRORi nstr
uc t
ion,fol
lowe dby
atestoft hec arrySa g,suc ha s:
LDA MEMORY ; LOAD A WITH VALUE
LSR ; SHIFT BIT 0 INTO CARRY
BCS ODD ; SET IF ODD
BCC EVEN ; CLEAR IF EVEN

⇢eequi
val
entc
annow bedonewi
t Le
houta c
ti
ngt
hec
arySa
r gort
he
Ac
cumul
at
or:
BBR0 MEMORY,EVEN ; BRANCH IF BIT 0 = 0 = EVEN
BBS0 MEMORY,ODD ; BRANCH IF BIT 0 = 1 = ODD

CT]⇢epr
5[ obl
emi
smoots
inc
eSMBa
ndRMBa
renota
vai
la
bleonmos
tAppl
ema
chi
nes
.
6[
CT]Ag
ainmoots
inc
eBBSa
ndBBRa
renota
vai
la
ble
.
3
3.⇢e6
5C0
2 3
35

⇢i sals
ocoul
dbeusef
ulincrea
tingdri
v e
rsfort
henewApple//
e8 0-
col
-
umne xt
endedmemoryboa
rdsincethisc
ardusesonebankofmemoryorthe
othe
rforthete
xtsc
ree
n,depe
ndingonwhe thert
hescre
encolumnposi
ti
onis
oddoreven.
TSBa ndTRB
TSB a ndTRB (Tes
ta ndS et
/Re
setBit
)arethemostcomple
xoft hene w
ins
tructi
ons.⇢e seins
truct
ionsarerat
herli
keac ombi
nat
ionoftheBIT a
nd
AND/
ORAi nst
ruct
ionsofthe6 50
2.⇢e ysee
m pri
maril
ydes
igne
dforcontr
oll
ing
I/Ode vi
cesbutma yhaveotheri
nteres
ti
ngappl
icat
ionsast
hingsdev
elop.
⇢ea ct
ionofthesetwoinst
ructi
onsist
ouseama s
kstore
dintheAcc umu-
lat
ort ocondit
ioname morylocat
ion.⇢ema skintheAccumulat
orisunal
tere
d,
butt heZ-Sagoft heSta
tusRe g
ist
eriscondi
ti
onedba s
edont heme morycon-
tentspri
ortotheope r
ati
on.
Fore x
ampl e
,tosetbot
hbi t
s0a nd7ofame morylocat
ionwecouldusethe
foll
owings e
tofinstr
ucti
ons:
LDA #$81 ; %1000 0001 = MASK PATTERN
TSB MEM1 ; SET BITS 0,7 OF MEMORY
BNE PRSET ; ONE OF THESE WAS 'ON' ALREADY
BEQ PRCLR ; NEITHER OF THESE WAS 'ON' ALREADY

⇢i
swoul
dcl
eart
hebi
ts
:
LDA #$81 ; %1000 0001 = MASK PATTERN
TRB MEM2 ; CLR BIT 0,7 OF MEMORY
BNE PRSET ; ONE OF THESE WAS 'ON' ALREADY
BEQ PRCLR ; NEITHER OF THESE WAS 'ON' ALREADY

⇢e
sei
nst
ruc
ti
onsus
eonl
yabs
olut
eandz
ero-
pag
eaddr
ess
ing
.

Ot
herDi
?er
enc
es
⇢e rea r
eanumbe rofothe rdi Lerencesbetweenthe6 502a nd65C02,most
notabl
ythepowe rconsumpt i
on. ⇢epowe ruseofthe6 5
C0 2isone-t
enththa
tof
the6 50
2,s othec hipr unscons i
de rablycooler.⇢el owerpowe rrequir
ement
opensnewpos si
bil
iti
esforportablec omput er
sa ndterminals.
Onepoi ntofi nter
estisthatt heol d65 02indi
rectjumppr obl
em hasbeen
Fxed.Ify ou’r
enota wareofit,the6 502ha sawe ll
-docume ntedproble
m with
indi
rectjumpst hatuseapa i
rofby te sthats
traddleapa gebounda r
y.
Forex ample,consi
derthesethr eeinstr
uctions:

I
nst
ruc
tion Poi
nte
rsWa
nte
d Poi
nte
rsUs
ed
JMP ($36) $36,$37 $36,$37
JMP ($380) $380,$381 $380,$381
JMP ($3FF) $3FF,$400 $3FF,$300

]
3
36 As
sembl
yLi
nes

Not icet ha t,i nt het hirdi ns tanc e,thepoi nter sus e da renott hos ea nt ici
-
pa t
e d. ⇢i si sbe ca uset hehi ghby teoft hepoi nt era ddr e ssisnotpr ope rl
yi nc re-
me nt edbyt hes ta nda rd6 502.
⇢i spr obl em ha sbe enFx edi nt he6 5 C0 2. ⇢eonl ypos s i
bl epr obl em he reis
“clev er”pr ote ctions c
he me stha tus et hi sbugt ot hr ow oLpe opl et ryingt o
de codet hepr ote ctionme thod. Ot he rwi se, thiss houl dnotpr esenta nypr obl ems
toe xistings o4wa r e.
Ar et he rea nypr obl emst obea nt ici
pa ted?I nt he ory ,no.⇢ene w6 5C0 2is
compa tiblepi nf orpi nwi tht heol done ,a nda lsoupwa rdlyc ompa tibleint erms
ofs o4wa re .S o4wa r efort heAppl e, PET, At ari,orot he r6 50 2-ba se dmi croc om-
put erss houl dwor kwi thoutpr obl e mswi tht hene wc hi p.Ar et he rea nye x cep-
tions ?Unf or t
una tely ,yes.
⇢eFr stbi gpr oblemc oncer nsi nterna l mi c ropr oc essort imi ngont heAppl e
IIa ndI IPl usc omput ers.⇢eAppl eI Ia ndI IPl usdonotha ndl et hemi cropr o-
ce s
sorc loc kc ycle sint hes a mewa yt heAppl e/ /edoe s .Ont hes ur fac e,the6 5 C0 2
shoul ddi rec t
lyr epl acet he6 502 ;howe ve r,be c aus et he6 5C0 2i saf as t
erc hip,
da t
ai snota va il
a blef ora sl onga ndbi t
sc a ng etl ost. Wha tthi sme a nsfornowi s
thatt he6 5C0 2c anbeus e donl yi nt heAppl e/ /ea ndAppl e/ //ma chine s.None
oft hema nuf ac t
ur e rsatt histimepr oduc eac hi pt ha twor ksont heAppl eIIorI I
Plus .I tc anbee x pe cted,t houg h,t ha tr evisionswi llbema dei nt hene arf ut ure
thatwi lla llowt he6 5C0 2t obei mpl e me nt edi nt heol derma c hine s.
⇢e rea lsoi sapos sibilit
yofpr obl e mswi ths omee xisti
ngs o4wa re.As ma l
l
pe r
c ent ag eofs o4wa rema ybeus ingundoc ume nt edbug sor“ f
e atur es”oft heol d
65 02c hi p, a ndt he semi g htnotf unc tiona sa nt i
c ipate dwi tht he6 5C0 2.
Fore x a mpl e ,ar e asona bleque sti
on mi ghtbe ,“ Whe redi da llthene w
opc ode sc omef rom?A4e ra ll
,wa sn’ tthec hipf ull?”Toa nswe rt his,c ons ider
howt hei ns tr
uc t i
onsweus enowa res t
ruc tur ed.⇢e6 5 02ope ra tesbys ca nni ng
me mor ya ndpe rfor mings pe ci
Fcope rationsba s edont hev alue st ha titFndsi n
ea c
hme mor yl oc ation.Youwoul dt he ne xpe c tat otalof2 5 6pos s i
bleopc ode s
.
Asi tha ppe ns ,a ll2 56pos s i
blev alue sa r enotus e
d.I tist hi sg roupofunus ed
opc ode st ha ta ll
owsf ort hene wi ns truc t
ionsa nda lsoc reatest hepos si
bi l
ityofa
sma l
l pe rce nta geofdi Wc ultieswi the x i
stingpr og rams .
Al thoug hr arelydoc ume nte d,t hepr evious l
y“ unus ed”v alue si nt he6 5 02
wi l
lc aus ec erta i
nt hing st oha ppe n, muc ht hes amewa yt hatal ega lva l
uewoul d.
Fori ns ta nc e, thec ode$FFona6 5 0 2i slabe leda sa na lterna teNOP. ⇢i sisoneof
thec ode st ha tha vebe enc onv erte dt oane wf unc tioni nt he6 5C0 2 ,na me lyBBS7
(Bra nc honBi t7S et).
⇢e rea reot he runus edc ode s,t houg h,t ha tha v ec ombi na tione Lec ts–us u-
allyofl ittleus e–s uc ha sl oadingt heAc cumul atora ndde creme nt i
ngar eg i
ster
atthes a met i
me . ⇢e irma ina ppl ica ti
oni ss imi la rtot hei ndi rec tjumppr obl em:
crea ti
ngc odet ha tc a nnotbec asua llyi nte rpr eted. I
ft hes eins truc t
ionsha vebe en
3
3.⇢e6
5C0
2 3
37

usedi nex i
sti
ngs o4wa re ,proble msc oulda risewhe nt hatso4wa reisr unont he
65C0 2.
Wi thsuc hdiWc ulties,t
he n, whybot he rtos ubstit
utethene w6 5C0 2i ntoa n
exist
ingAppl e?⇢ea nswe rsarev a ri
ed.
Firstofa ll
,the6 5
C0 2isl ikelyt oa ppe arinupc omi ngr el
e as
esofe x i
sting
comput ers( i
nane wr eleaseoft heAppl e/ /e,pe rhaps?),anda ssuchy ouc an
experime ntnowwi tht hene wes tversionoft hisversatil
ede vice
.
Second, therelik el
ywi l
lbes peciFca ppl icat
ionswhe r
et hea dvantagesoft he
chipwi l
lma keitwor ths upply i
ngwi tht hes o4wa re ,si
ncet hedi s
adv antag e
sa re
practi
callynone x
istentf ort heAppl e/ /
ea ndAppl e/ /
/.Coder ewrittent ot ake
advantageoft hene wi ns t
ructionsc anbee xpe ct
edt obe1 0t o1 5pe rcents ma l
ler
andr unpr opor t
iona ll
yf ast
er.I nc ertai
na pplications,eve ng rea
te rimpr ove-
me ntscouldbee xpec ted.
Att hiswr i
t i
ng,t heRoc kwe l
lc hips eemst oha v ethela r
gestse tofi nstruc-
ti
onsoft hethre eva rietiesava il
able.⇢eGTEa ndNCRc hipslackt hebi t
-ma -
nipulati
oni nstructi
onsbuta reot he r
wi seide ntic
a l
.
Ast oa ssembl e
r ssuppor t
ingt hei nstructions,thec urrentv ers
ionofMe rl
in
suppor t
sa l
lthene wopc ode sinbot ht hea ssembl ya ndS ourcerorpor t
ionsoft he
produc t
.S -CS o4wa rei soLe ringa6 5 C0 2c ross-as
s emblert oregist
e r
e downe rs
oft heS-CAs semb lera tar educ edr a te
.Ha y denwi l
lbeoLe ri
nga nupda tet o
ORCA t os uppor ttheGTEv er s
ionoft hec hip.Anya ssemblert hats uppor t
s
ma croca pabil
it
iess houl dbea blet obeus edi mme diatel
ybyde Fningt hepr ope r
hexc odes.

]
3
38 As
sembl
yLi
nes

Anot
efr
om Rog
erWa
gne
r,J
une1
983
:

⇢i sinst
all
me ntma rksthelas
tinthisseri
es.Iwanttothankthe
ma nyreadersofthiscolumnov erthelastsev
eralye
arsforthei
r
enthusi
asti
cs upporta nd val
uables ugg
e s
ti
ons.Iha vea l
ways
beli
evedtha tt
hehuma nele
me nttothisi
ndustry
,andinfactany
endeavor,i
st hetrul
yr ewardi
ngpa rt
.Iwoul dals
oliketothank
Softa
lkforg iv
ingmet heoppor t
unitytosharetheexci
tementof
programmi ngwithitsreaders
,anda l
sothankBr i
anBrit
tforhis
helpinresearc
hingthi
smont h’sar
tic
le.

Forbet
terorworse,t
houg h,you’
renotlikel
ytobecompl e
tel
yrid
ofme.⇢e rea
r er
umor sofothercol
umnsa ndpr
oje
c t
s,andIlook
for
wardtobeingas mallpartoft
heS ofta
lkfami
lyforsomey ear
s
tocome.

Anot
efr
om S
ofta
lke
dit
orMa
rgotComs
toc
kTomme
rvi
k,J
une1
983
:

Itwa sne a
rlythreeyearsagothatRog erWa gner
’sAs s
emb l
yLine s
begana ppeari
ngi nSoftal
k;t
hema gazinewa sonl
yonemont hold.
Intha tFrstyear,Wag ner’
scolumne li
c i
te
dmor ema i
lfromS oft-
al’
ksr eadersthananyot herfeat
ure,a ndproperl
ys o:Itwa st
he
Frsttimea ss
embl yl
ang uag
eha dbeene xpla
inedfrom st
epone .In
fac
t,inhi sFr stcol
umn,Wa gnerdi dn’tevenintroduceac om-
ma nd.

Wit
ht hi
sissue,RogerWa gner
’sAss
emb l
yLi se
ne nds
.⇢eFr s
t
y
ear
’scol
umnspl usappendix
esandrevi
sionshavebeenavai
la
ble
f
orsomet i
mei nAssemblyLine
s:TheBoo k.Vol
ume2 ,cover
ing
t
heres
tofthecolumns,wil
lbere
lea
sedshortl
ybyS o4al
kBooks.

RogerWa gnerwil
lnotfa deaway.He’spla
nni
ngocca
sionalf
ea-
t
urea rt
icl
esforSofta
lka
ndhe ’spromise
dt or
emai
na v
a i
la
bleto
ans
we rquest
ionsf
rom Softa
lkr
eaders
.
Ap
pendi
xA:
Cont
est
IntheMa r
ch1 981edit
ionofS ofta
lkma gaz
ine,wec hall
engedt hereader
sof
t
he“As s
embl yLines”col
umnt oac ontest
. Usi
ngthec omma ndsdi scuss
edinthe
col
umnf rom Oc tober1980t hroughMa rch1981(allma ter
ialcove re
dthrough
cha
ptersixi nthisbook )
,c ontest
ant swe reas
kedt os ubmitpr og ra
mswhi ch
wouldbej udgedbyt hestaL,thes hor t
estandmos tinteres
tingpr ogram be
ing
t
hewi nner.Conte s
trule
sa rere pr
intedhe reasthe
yor iginal
lya ppearedinthe
Marchiss
ueofS oftal
k.

Cont
estRul
es

Createt
heshort
estposs
ibleprogram usingal
landonlyt hec om-
mandspresent
edthusfarinthisser
iesthatdoessomethinginter
-
est
ing.⇢epr ogr
am mus tbee nti
rel
yi nma chinela
ng uage
,a nd
maynotc a
llanyrouti
nesinI nt
egerorAppl es
o4.Itma ycallany
oftheMonit
orrouti
nesfrom $F8007$FFFF.

⇢epe r
sonwhos ubmit
stheshor
testpr
ogra
m ofthemostint
eres
t
wi
llbeawarded$50worthofproductf
rom anyadve
rti
serinthi
s
i
ss
ueofSoftal
kandtheprog
ram wil
lbepubl
ishe
dinSofta
lk.

Judgi
ngwi llbeba s
edont heopi nionsofar athersubj e
cti
vely
sel
ect
edpane lma deupofpe opleatSofta
lk,myse
lf,a
nda nyother
haple
sspass er
sbywec anropei nt
ot histhi
ng.Me mbe rsoft he
stLsofS
a oftal
ka ndS out
hweste
rnDa t
aS ys
temsandpr ofessi
onal
progr
amme rsarenote l
ig
ibl
etowi n.Entrie
sshouldbes ubmi t
ted
nolate
rthanApr il15,1981.
Tieswillbesett
le
dbyAppl e’
sr andom
numbe rge
ne rat
or (
.Ipromis
enott oseedit!
)

Conte
stres
ult
swer
ea nnounce
di nt
heJune1
981edi
tionofS
ofta
lk.⇢e
wi
nningprog
ramfort
hecontes
tisli
st
edbeow.⇢ec
l ommentar
yac
compa ni
es
t
heli
sti
ng.

Cont
estRe
sul
ts
Withtheus ualcomme ntsinmi nda bouthow hardi twast odec i
deona
winner,Ihe r
ebya nnouncethewi nne rofthec ontes
ta sS t
ev en Morri
s,of
Queens,NewYor k.Hisprogr
amc ombi nesanumbe rofthepri
nc ipl
eswe ’
vedis
-
cuss
eds ofaranda l
sos howssomenicet ouche
sinpr ogr
a mming .It
’saneleg
ant
useofallt
heg iv
e ncode s
.Ofpart
icularinte
res
tisas el
f-
modi f
yingpa rtwhere
in
theprogra
ma ctuall
yr e
wr i
te
sasma l
lpor ti
onofit
sel
fuponus ercomma nd.

]
3
40 As
sembl
yLi
nes

Ithinki twi l
lbeofi nter
e s
t,anda lsoag oodr evi
ew,tog othr oug hMor r
is’s
l
istingt ose ewha t
’sb eendone .Be f
oredoi ngt hat,howe ver,alit
tleba ckg r
ound
ononemor ek indoft onerout i
neisi nor der.⇢i swillmak eMor ris
’spr og r
am
thatmuc hmor eunde rst
anda bl
e.
Incha ptere i
g ht,Idiscussedsimpl etoner out inesinwhi chthes pe akerwa s
accesseda tac onst a
ntr ateforag i
venl eng t
hoft ime.⇢e setwof act
or sde t
er -
mi nedt hepi tcha nddur at
ionoft het onepl aye d.Av ari
ati
onont hisist oha v e
thepi tchde cre
a seori ncreaseasthetonei spla yed, creati
ngeLectsr a t
he rlikethe
sound us uall
ya ssoc iated wi tha
falli
ng bomb or a r i
sing s ir
e n,
respe cti
vely.⇢i sr e quirest hre e
va riables
,andwi thoutg e tt
ingt oo
techni cal
, l
etmet akeamome ntto
il
lus trat
ewi t
hthec ha rtatl e4.
⇢e v ert
icala x isr epr ese
nt s
thef requencyoft het onebe ing
pl aye d.Put ting s e veralt one s
tog etherintoas e
riesov erape r
iod
oft imec re
ates,i
nt hi sc ase,ar is-
ings cale
.Ase ac
ht onei spla y
e d,
thepi tchisinc reased.Ea chindivi
dua ltonel astss omea r
bit
rarytime ,T, andput
together,thes erieslastsanov eral
lti
mepe riod, labeledhe r
ea sDURATI ON.
Ifthepi tchi sde creas
edbyac ert
aina mounte achtime,t hepa tte
rni s
reversed.⇢i si ss ome ti
me sc al
le
dar ampt onepa tt
ern.Inpa r
ent he ses,Iha v e
indicatedhowe achoft hesev al
uesisde t
ermi ne di nMor r
is’
sprogra m.
He r
eisal isti
ngoft heprog r
am:
1 ********************************
2 * ASSEMBLY LINES CONTEST WINNER*
3 * BY STEVEN MORRIS *
4 ********************************
5 * OBJ $302
6 ORG $302
7 *
8 PTCH EQU $300
9 DRTN EQU $301
10 SPKR EQU $C030
11 PREAD EQU $FB1E
12 PB0 EQU $C061
13 PB1 EQU $C062
14 GRSW EQU $C050
15 TXTSW EQU $C051
16 CLRSCR EQU $F832
17 *
0302: CA 18 LOOP DEX ; DEC THIS DELAY
0303: D0 06 19 BNE CYCLE ; DONE? NO = SKIP CLK
20 *
Appe
ndi
xA:
Cont
est 3
41

0305: AE 00 03 21 CLK LDX PTCH ; REFRESH X-REG


0308: AD 30 C0 22 LDA SPKR ; CLK SPKR
23 * SPKR CLKS ONLY ONCE
24 * FOR EVERY ($300) PASSES
25 *
030B: 88 26 CYCLE DEY ; # OF CYCLE CTR.
030C: D0 F4 27 BNE LOOP ; DONE?
28 * NO = KEEP GOING
030E: CE 01 03 29 DEC DRTN
0311: F0 06 30 BEQ CHKPDL ; DONE W/ RAMP?
31 * YES = CHK PDLS
0313: EE 00 03 32 RAMP INC PTCH
0316: 4C 02 03 33 JMP LOOP
34 *
0319: A2 00 35 CHKPDL LDX #$00
031B: 20 1E FB 36 JSR PREAD ; READ PDL(0)
031E: 8C 00 03 37 STY PTCH ; SET PTCH
0321: E8 38 INX
0322: 20 1E FB 39 JSR PREAD ; READ PDL(1)
0325: 8C 01 03 40 STY DRTN ; SET DRTN
0328: A0 7F 41 LDY #$7F
032A: CC 62 C0 42 CPY PB1 ; #1 PRESSED?
032D: 90 27 43 BCC TOGGLE ; BRCH IF YES
44 *
032F: C8 45 INY ; #$7F -> #$80; AN EXCUSE
0330: 98 46 TYA ; TO USE THESE
0331: AA 47 TAX ; COMMANDS.
0332: EC 61 C0 48 CPX PB0 ; #0 PRESSED?
0335: B0 CB 49 BCS LOOP ; BRCH IF NO
50 *
0337: 20 32 F8 51 SCREEN JSR CLRSCR ; CLR TOBLK
033A: 8D 50 C0 52 S1 STA GRSW ; SHOW GRAPHICS MODE
033D: 8D 51 C0 53 STA TXTSW ; SHOW TEXT MODE
0340: 4C 3A 03 54 JMP S1
55 *
0343: A8 56 SETDEC TAY ; USE UP THIS CODE
0344: A2 CE 57 LDX #$CE ; OPCODE FOR 'DEC'
0346: 8A 58 TXA
0347: CD 13 03 59 CMP RAMP ; IS IT 'DEC' NOW?
034A: F0 04 60 BEQ SETINC ; BRCH IF YES.
034C: 8D 13 03 61 STA RAMP ; NO. MAKE IT 'DEC'
034F: 60 62 RTS
63 *
0350: A2 EE 64 SETINC LDX #$EE ; OPCODE FOR 'INC'
0352: 8E 13 03 65 STX RAMP
0355: 60 66 RTS
67 *
0356: 20 43 03 68 TOGGLE JSR SETDEC
0359: 4C 02 03 69 JMP LOOP
70 *
035C: C0 71 CHK

I
’l
ltr
ytoexpl
ainea
c hpa
rtoft
heprog
r a
m,hope
full
ywit
haprope
rbal
ance
ofenoughdet
ailt
ojogyourmemorya
nde noug
hbrevi
tytok
eept
hing
sre
ason-
abl
yshort.

]
3
42 As
sembl
yLi
nes

Ifalloft hi sse emsov e rwhe lmi ng ,y ou’r et ryingt or eadt hroug hi ttoof ast
.
Goba ckt hroug hi ts l
owl y , takingy ourt i
me .Ha veani cec upoft eawhi ley ou’ re
ati t.
Re me mbe r,we ’
repa c k i
ngs ixc ha pters’wor thofs ubj ectma t
teri ntoone
pr og ram.Don’ twor ryi ft heFnede tail
soft het oner out inee scapey ou.⇢e
impor tantpa rti stoma k es ur et ha ty oua tl ea str ecallthee x i
stenc ea ndg ene ral
na tur eofe achi ndiv i
dua lcomma ndus edint hepr og ram.
Toe xpla int hepr ogra m, thee a siestplac et os ta r
tisa ctua llya tCHKPDL, whe re
thepa ddle sa rec he ck edf orne wv a l
ue sa tthee ndofe ac hr a mps e ri
es( l
ine3 5a t
addr ess$319) . ⇢eX- Re gis teri sl oa de dwi tha$00t otellt hec omput erwewa nt
tor ea dpa ddl e0i nt hene x ts tep,t he nJSR t o$FB1E.⇢a tr e t
ur nswi t
ht heY-
Re g i
s terhol dingt hev alueoft hepa ddl e( $00 t o$FF) ,whi chi st hens t
or e di n
loc ation$300,l abe ledPTCH( “pi tch” ).⇢eX- Re gisterv aluei sthe ni ncreme nt ed
from $00 t o$01onl i
ne3 8,a ndpa ddl e1r ea d.⇢er etur ne dv a l
uei ss tore da t
$301f ort hedur at
ionv alue .
Ifpa ddl epus hbut ton1i spr es sed, location$C062wi llhol danumbe rg reater
tha n$7F.Toc heckf ort hi s,t heY- Re gist
e ri sl oa dedwi th$7Fa ndc ompa red
ag ains t$C062.I f$C062hol dsav alueg reate rt ha n$7F,t heBr anc hCa rryCl ear
(BCC)wi llbet a ken( Y- Re gis t
e r<me mor yl oc ation=c arryc l
e ar).We ’
llse ewha t
tha tdoe sla ter.
Ifthev a luei sl esstha n$7F,pr og ra me xe cutionwi llfa llthroug htol ine4 5
.
He ret he$7Fi sinc rea sedt o$80a ndt hatv a luepa ssedt ot heX- Re gis
terv i
at he
Ac c umul ator .⇢e ses tepsa rehe ret oe x erciset heINY,TYA,TAXc omma nds ,and
toa ll
owust ous et heCPXc omma ndne xttof ulFl lthec ont estre quireme nt s.At
line4 8t hec ompa risoni sdone . Ift heX- Re giste risg reate r( reme mbe rithol dsa
$80h ere), thebut toni snotpr e sseda ndt heBr anc hCa rryS et( BCS)wi l
lbet ak en
(X- Re gi
ste r>me mor yl oc ation=c ar rys et
)t ha ts endsust ot hema i
nt onel oop.
Ate nt ryt ot hisl oop, t heX- Re g ist
era ndt heY- Registerhol dr a t
hera rbi trary
va lue s,butt heov era l
lt he or yi st ha t,sta r
tinga tCLKonl ine2 1 ,theX- Re g i
s teris
loa de dwi tht hepi t
c hv aluea ndt hes pea kerc licke donc e .Atl ine2 6theY- Re gis-
teri sde cre me nted;t hisi sac ount erf ort hel eng thoft ha tpi tchv al
ue .Jumpi ng
ba ckt oloop, thene te Le cti st ha tt hepr ogra m wi llma kenpa ssest hroug hbe fore
click ingt hes pea keronc e,whe reni st hepi tc hv aluehe ldi n$300.⇢i sc reates
thede l
aybe twe enc licksne ede df orag i
v ent one .
⇢el e ng thoft ha tpa r ticula rt onei sde te rmi ne dbyt heY- Re gis
ter.Whe ni t
re ache sav a lueof$00,t heBNE( Br a nc hNotEqua l)failsa ndt hec ounterf ort he
ov era lldur ationi sde c reme nte d. Asl onga sthe r e’stimel e 4( tha tis,DRTN>$00) ,
thene xtt estf a i
ls( BEQ=Br anc hi fEqua lt oZe ro)a ndt hepi tchv al
uei si nc re-
me nte d.
Goi ngba c ktoLOOPpl ayst hisne xtnot eunt ilalloft henot e sint hes eries
ha v ebe e npl ay ed.I nc reme ntingpi tc hg ivesade scendi ngnot epa t
tern.( Re c al
l
tha tt heg re atert hepi tc hv a l
ue , thel owe rthet onepl a y
ed. )
Appe
ndi
xA:
Cont
est 3
43

Whe nDRTNdoe srea ch0 ,t


hepr og ram br anche stot hepa ddleche ckr outine
thatwes t
a rt
edi n. Let’sseewha tha ppe nswhe nabut toni spr essed.Ifbut ton1i s
pressed,t hepr ogra mg oesv iaTOGGLEt oSETDEC.⇢i sc leve rs ect
ion( ignor ethe
TAY)l oadst heX- Re gisterwi ththev alue$CE. ⇢i sist heopc odef orDEC( DECr e-
me ntame mor yloc ati
on) .
Ifthec ompa r i
sonf ai
ls,thati s,the r
ei snota$CEc urre ntlythere,the$CEi s
storeda tRAMP, theRTS( ReTur nf rom S ubr outine)r etur nst oTOGGLEa ndt heJMP
loops endse verythingba cki ntot het onel oop,t hist imewi thaDEC PTCHt here
inste
a d.⇢i sgivesa na scendi ngpi tchs erie
s .
Ifthec ompa r i
soni strue,itme anst ha ta$CEwa sputt he reearli
e r
,a ndt he
BEQ g oest oSETINC,whi chr estorest hec odef orINC a tRAMP( $313) ,a ndt hen
returnswi ththeRTS, JMPLOOPa sinthepr ev i
ousc ase.
⇢e set woopt ionsg ivet hepr ogr amt hea bil
itytor e wr i
tei t
self
,a ni nterest
-
ingandpowe rful i
de a.
Ifpa ddlebut ton0i spr essed, t
hebr ancha tline4 9f a il
sa ndt hepr og ramf all
s
intoa ni nFnitel oopa tSCREEN( $337) .Int hisloop,t hes creeni sclearedt ot he
colorbl ackbyt heMoni torr outinea t$F832.
Loc at i
ons$C050 a nd$C051 a res o4-swi t
c hes:a cce ssingt he s
ec ha ng esthe
displ
a ymodeoft heAppl e.⇢es c r
eenc anbev iewede it
he ri nat extmodeori na
graphi c
smode .Ac cessing$C050onl ine5 2s et
st heg ra phi csmode ,sot hes creen
appea rsbl ack
.Ac cessing$C051s etsthedi splayt ote xt, whi cha ppe arsasi nv er
se
“@”s igns.
⇢ eJMPS1r epea t
st hisc y
c leba cka ndf or thsof astt ha tyoudon’ ta ctual
ly
seetheSi cker,justa ni nteresti
ngpa tternc reatedbyt hes cre enss wi
tchi ngf ast
er
thany ours c
reenmoni torc andi splayt hem.
Att hi spointy ouha vetohi tRESETt oe nd.
⇢e rewe r eanumbe rofot he rex cell
ente nt ri
es.Honor ableme ntions hould
bema deofS t
e veHa wley ,RayRa nsom, StephenGa g ola ,Jr.,andMa ttBr ook over
forthe i
re Lorts.

]
Ap
pendi
xB:
Ass
emb
lyCo
mmands
⇢i ss ectionma ywe llser
v ea sthemos to4e n-use dpor ti
onoft hisbook .I
ha veme ntione de lsewhe rethatl ear
ningpr og rammi ngc anbel ook edupona s
me relyfa mi lia
rizingy our se
lfwi ththea vail
ablet oolst oa cc omplishas pe c
iFe d
task .⇢ef ollowi ngs ecti
ons umma r
izest het oolsa vailablet oa na ss
embl y-lan-
gua g epr ogra mme r.
Whe ny oua r
eFr s
tl ear
ningt opr og r
am,muc hc anbeg aineds implyby
br ows ingt hr ought hef ol
lowingpa gesa ndc asua l
lynot ingt hev arie
tyofi nst
r uc-
tionsa vailablewhe nwr i
tingar outi
ne.Ea che ntrypr ov idest heus ualtechnic a
l
da taont hei ns t
ruc t
iona ndo4e nabr iefex ampl eofitsus ea swe ll
.
Ple asenot ethati ns omee x ampl e
sape rcentsign( %)i sus edt oindicatea
bina ryf orm ofanumbe r.Somea ssembl erssuppor tt hisde limite
rwhi chcanbe
ve ryc onv eni ent,par t
icularl
ywhe nwor kingwi t
ht hel og ic
a loperatorsands hi4
ins t
ruc ti
ons .Fore x ampl e,thef ol
lowi ng r e
pr esentationsa rea l
le quiv a
le nt
:
100=$64=%01100100.
Whe nl ook i
nga taddr es
singmode s,it’
se asytof orgett hesubt l
etie
soft he
diLe renc esbe twe ent heX-a ndY- Re gistera sus edwi thi ndir
ecta ddressing.
Re me mbe rt ha tthes yntax($FF,X)i ndi catespr e
-inde xing ,whi l
e($FF),Yi ndi-
ca te
spos t-i
nde xing.Seec hapters e
v e
nf ort he“ oWc i
a l
”e x planati
onofa ddressing
mode s
.

ADC:
ADdwi
thCa
rry
Descr
iption:⇢i si
nstr
ucti
onaddsthecontentsofamemoryloca
tionori mme-
dia
tevaluetothec ont
entsoft
heAc c
umul ator,pl
ust
hec a
rrybi
t,ifitwasset
.
⇢er esultisputbackintheAccumulator
.ADCwor ksforbot
hbina r
ya ndBCD
(Bi
naryCode dDe c
imal)modes
.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l l l

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Immedia
te ADC #$12 69 12
Zer
oPa g
e ADC $12 65 12
Zer
oPa g
e ,
X ADC $12,X 75 12
Absol
ute ADC $1234 6D 34 12
Absol
ute
,X ADC $1234,X 7D 34 12
Absol
ute
,Y ADC $1234,Y 79 34 12
Appe
ndi
xB:
Ass
embl
yComma
nds 3
45

Addr
essi
ngModes CommonSy
nta
x He
xCodi
ng
(
Indi
rec
t,
X) ADC ($12,X) 61 12
(
Indi
rec
t),
Y ADC ($12),Y 71 12
(
Indi
rec
t)[65
C02
]1 ADC ($12) 72 12

Uses
:Pe
culi
arl
yenough,
ADCismos
to4e
nus
edt
oaddnumbe
rst
oge
the
r.He
re
ar
esomecommone xa
mples
:

1
.Addi
ngac
ons
tantt
oar
egi
st
erorme
mor
yloc
ati
on:
CLC
LDA MEM
ADC #$80
STA MEM ; MEM = MEM + #$80

2
.Addi
ngac
ons
tant(
suc
hasa
noLs
et)t
oat
wo-
byt
eme
mor
ypoi
nte
r:
CLC
LDA MEM
ADC #$80
STA MEM
LDA MEM+1
ADC #$00
STA MEM+1 ; MEM,MEM+1 = MEM,MEM+1 + #$80

3
.Addi
ngt
wo(
2)t
wo-
byt
eva
lue
stog
ethe
r:
CLC
LDA MEM
ADC MEM2
STA MEM
LDA MEM+1
ADC MEM2+1
STA MEM+1 ; MEM,MEM+1 = MEM,MEM+1 + MEM2,MEM2+1

AND:
Log
ica
lAND
Descr
ipt
ion:⇢i si
ns t
ructi
ontakesea c
hbi toftheAccumulatorandperf
ormsa
l
ogic
alAND wi theachc orr
espondingbitoft hespec
iFe dme moryl
ocat
ionor
i
mme dia
tevalue
.⇢er esul
tisputba ckint heAccumul a
tor
.⇢eme moryloca-
t
ionspec
iFedisunaLecte
d.(Seeals
oORA. )
ANDme a
nsthatifbothbit
sare1t hentheresul
twil
l be1,othe
rwis
etheres
ult
wil
lbe0.

1[
CT]Opc
ode
sing
raya
reonl
yav
ail
abl
eont
he6
5C0
2.

]
3
46 As
sembl
yLi
nes

Tr
uthTa
ble 0 1 Ex
ample
Acc
umulat
or: 00110011
0 0 0
Memory: 01010101
1 0 1 Re
sult
: 00010001

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addres
singModes CommonSy
nta
x He
xCodi
ng
Immedia
te AND #$12 29 12
ZeroPag
e AND $12 25 12
ZeroPag
e ,
X AND $12,X 35 12
Absol
ute AND $1234 2D 34 12
Absol
ute
,X AND $1234,X 3D 34 12
Absol
ute
,Y AND $1234,Y 39 34 12
(I
ndir
ect
,X) AND ($12,X) 21 12
(I
ndir
ect
),Y AND ($12),Y 31 12
(I
ndir
ect
)[65C0
2] AND ($12) 32 12

Use s:AND i
susedprimaril
ya sama sk,thatis,toletonl
ycert
ainbitpatt
erns
throughas ecti
onofapr ogr
a m.⇢ema skisc reate
dbyputti
ng1 sineachbit
positi
onwhe redataistobea ll
owe dthrough,a nd0 swher
edataistobes up-
pressed.Forexampl
e,iti
sfrequentl
yde si
rabletoma skoutt
hehigh-or
derbitof
AS CIIda t
a,suchaswoul dcomef r
om t hek eyboardoranot
herinputdev i
ce
(perhapsadiskFle
).⇢er outi
nes howna ssuresthatnomatt
erwhatval
ueisgot-
tenfrom thedevi
ce,t
hehigh-orderbitoftheva l
uei nMEMwi
lla
lwaysbecl
ear:
Code Ex
ampl
e1 Ex
ampl
e2
LDA DEVICE 01010111 11010111
AND #7F 01111111 01111111
STA MEM 01010111 01010111

ANDi
sa l
sousedwhenyouknow t
hehi
ghbitwil
lbeseta
ndyouwanti
t
c
lea
red.⇢isisthecas
ewhengett
ingASCI
Ichar
act
ersf
rom t
hek
eyboa
rd.A
c
ommonr out
inetoge
tacha
ract
erf
rom t
heke
yboa
rdis
:
WATCH LDA KYBD ; $C000
BPL WATCH ; AGAIN IF < #$80
BIT STROBE ; CLEAR STROBE: $C010
AND #$7F ; CLR HIGH BIT
STA MEM
Appe
ndi
xB:
Ass
embl
yComma
nds 3
47

Anot
he rwa yoflookinga tt
hissamee Lecti
stos a
yt hatANDc a
nbeus edt o
forcea0i na nydesir
edpos it
ioninaby t
e’sbitpat
ter
n. (
Se eORAtoforce1s).
A0
i
sputi nthema skval
uea ttheposit
ionstobeforce
dt o0,a ndallr
ema i
ningposi-
tionsaresetto1 .Whene veradatabytei
sAND’dwiththisma s
k,a0willbeforced
ate achpositi
onma r
k e
dwi t
ha0i nthema sk,whil
ea l
lot herposi
ti
onswi l
lbe
una Lect
ed,rema i
ning0 sor1s,asinthei
rorigi
nalcondit
ion.
⇢eMoni torusestheANDi nst
ructi
oni ntheGETLNr outi
ne($FD6A)toc on-
ve r
tlowercaselet
ter
stouppe rcase
:
FD7C- B1 28 807 LDA (BASL),Y ; GET CHARACTER
FD7E- C9 E0 808 CAPTST CMP #$E0 ; ALPHA?
FD80- 90 02 809 BCC ADDINP ; NO, DON’T XVERT
FD82- 29 DF 810 AND #$DF ; XVERT TO CAPS
FD84- 9D 00 02 811 ADDINP STA IN,X ; PUT CHAR BACK

⇢e rea realsoatle as
ttwoot he rrat
herobscureusesfortheANDi nstruct
ion.
⇢eFr stoft hes
ei stodot hee quivale
ntofaMODf unct
ion,i
nv olvi
ngapi e
ceof
dataa nd apowe roft wo.You’ llrecal
ltha ttheMOD functi
onpr oducest he
rema i
nderofadi vis
ionope ra
tion.Fore xample:12MOD4=0 ;1 4MOD4=2 ;1 8
MOD4=2 ;17MOD2=1 ; et
c.
n
⇢eg eneralformul ais:Accumul at
orMOD2 =RESULT
⇢ea ctualoperationi scar
r i
edoutbyus i
ngav al
ueof(2n71 )asthema sk
val
ue .⇢et heoryofope rat
ionist hatonlythelastnbitsoftheda t
aby tearelet
through,thuspr oducingt heresultcorre
spondingt oaMODfunction.

Ex
ampl
e:
LDA MEM
AND #$07 ; %00000111 = 2^3-1
STA MEM ; MEM = MEM MOD 8

⇢istechni
queprov
ide
soneofs
eve
ralwa
ysoft
est
ingf
ort
heodd/
eve
n
a
ttr
ibut
eofanumber
:
LDA MEM
AND #$01 ; %00000001 = 2^1-1
BEQ EVEN
BNE ODD

⇢er es
ultoftheANDofa nynumbe rand$01wi l
la l
waysbeeit
her0or1
depe
ndi
ngonwhe therthenumberwasoddoreven.
⇢et hi
rda ppl
icat
ionisinde t
ermi
ningifag i
venbi tpatt
ernispres
ent
amongt
heotherdatainanumber.Forexa
mple,t
otestifbit
s0,3and7areon:
LDA MEM
AND #$89 ; %10001001
CMP #$89
BEQ MATCH
BNE NOMATCH

]
3
48 As
sembl
yLi
nes

⇢egener
altechniqueistoFrs
tANDthedataa
gainstt
heval
uefort
hebyt
e
wi
thj
ustt
hedesi
redbitssetto1(a
llot
her
s0),andt
heni mmedi
ate
lydoaCMPt
o
t
hesa
mevalue
.Ifal
l t
hes pe
ciFe
dbitsmat
ch,aBEQwil
lsucc
eed.

Note
:BIT(des
cri
bedl
ate
r)canbeuse
dt ote
stforoneormorema t
che
s,butt
he
ANDt
echni
quedes
cri
bedher
econFrmsthatal
lofthebi
tsofi
nte
res
tmatch.

ASL:
Ari
thme
ticShi
5Le
5
Desc
ripti
on:⇢i sinst
ruc
ti
onmov eseachbi
toftheAccumul
atororspec
iFed
memorylocat
iononeposit
iontothele4.A0isf
orcedatt
hebi
t0positi
on,and
bi
t7(thehig
h-orderbi
t)f
all
sint
othec ar
ry.⇢eres
ulti
sle
4intheAccumulat
or
ormemorylocat
ion.(S
eeal
soLSR, ROL,a
ndROR.
)

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l l l

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Accumulat
or ASL 0A
Zer
oPa g
e ASL $12 06 12
Zer
oPa g
e ,
X ASL $12,X 16 12
Absol
ute ASL $1234 0E 34 12
Absol
ute
,X ASL $1234,X 1E 34 12

Uses
:⇢emostcommonus
eofASLi
sformul
ti
ply
ingbyapoweroft
wo.
Youare
al
rea
dyfami
li
arwit
ht Le
hee ctinba
sete 123 d10 = 1230(
n: s
hi4l
e4)
.For
ex
ample
:
LDA MEM
ASL ; TIMES 2
ASL ; TIMES 2 AGAIN
STA MEM ; MEM = MEM*4 (4 = 2^2)

⇢eotherus
eistoc
hec
kagive
nbitpos
it
ionbyc
ondi
ti
oni
ngt
hec
arySa
r g.
Fore
xampl
e,che
cki
ngbi
t4woul
dlookl
iket
his:
LDA MEM
ASL
ASL
Appe
ndi
xB:
Ass
embl
yComma
nds 3
49

ASL
ASL BIT 4 NOW IN CARRY
BCS SET
BCC NOTSET

NOTE:⇢i st e
chniquedestr
oyst heconte
ntsoft heAc cumulatorint he
processofc hecki
ngt hebit.
AND o rBIT i
nst
ruct
ionsa r
eg ener
a l
lypre
ferred
inst
eadoft hi
ste
chnique.
Ifte
sti
ngbits0through3,LSRorRORma ybemor eappropri
ate(f
ewershi4s
needed).ROLals
oc anbeusedinst
eadofASLdependingonwhe t
herthedataisto
bepr es
erved.

BCC:
Bra
nchCa
rryCl
ear
Descri
pti
on:Execut
esabranc
hifthecarr
ySa gi
sclear
.Ignoredifcar
ryisset.
Manya ss
emble
rshaveanequiva
lentpse
udo-opcal
ledBLT( Bra
nchLe s
s⇢a n,
nottobeconf
usedwitht
hes a
ndwich)
,si
nceBCCiso4enus edimme di
ate
lyfol-
l
owingac ompari
sontose
ewhe t
hertheAccumulat
orisle
s sthanthespeci
Fed
val
ue.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BCC Address 90 FF

Not
e:⇢ec
arr
ySa
g,uponwhi
cht
hisde
pends
,isc
ondi
ti
one
dbyADC,ASL,CLC,
CMP,
CPX,
CPY,
LSR,
PLPROL,
RTI,
SBC,
andSEC.

Uses:Asme nti
one
d, BCCisuse
dtodet
ectwhe ntheAc c
umula
torholdsaval
ue
tha
ti sl
essthanas peci
Fedv a
lue
.⇢eus ua
la ppe
aranceoft
hec odeisl
ist
ed
bel
ow.Not ethati
nat wo-by
tecompa
risonthehi g
h-orde
rbyte
sa reche
cked
Frs
t.

One
-By
teCompa
ris
on:
ENTRY LDA MEM
CMP MEM2
BCC LESS ; Goes to LESS if MEM < MEM2
BCS EQ/GRTR

Two-
Byt
eCompa
ris
on:
ENTRY LDA MEM+1
CMP MEM2+1
BCC LESS ; MEM,MEM+1 < MEM2,MEM2+1
BEQ CHK2 ; MEM+1 = MEM2+1

]
3
50 As
sembl
yLi
nes

BCS GRTR ; MEM,MEM+1 > MEM2,MEM2+1


CHK2 LDA MEM
CMP MEM2
BCC LESS ; MEM,MEM+1 < MEM2,MEM2+1
BCS EQ/GRTR ; MEM,MEM+1 >= MEM2,MEM2+1

BCS:
Bra
nchCa
rrySe
t
Descr
ipt
ion:Exe
cut
esabr
anchonl yi
ftheca
rrySagisse
t.Somea s
semble
rssup-
portt
hepseudo-
opBGT(
“BranchGr e
ater⇢an”),
sincet
hiscomma ndisuse
dt o
te
stwhet
hertheAcc
umula
torisequalt
oorgreat
erthanthespe
ciFedval
ue.
Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BCS Address B0 FF

Note:⇢ecar
rySag,uponwhic
ht hi
sdepe
nds,i
scondi
ti
one
dbyADC,ASL,CLC,
CMP,
CPX,
CPY,LSR,
PLPROL,
RTI,SBC,
andSEC.
Uses:BCSisusedtodet
ectwhet
hert
heAcc umulat
ori
sg r
eat
ertha
nore qualt
oa
spe
ciFedv al
ue.BCSca
nbec ombine
dwithBEQt odet
ectagrea
ter
-tha
nr el
at
ion-
shi
p.Not ethatinthetwo-by
tecompa
rison,thehig
h-orde
rby t
esarechecked
Frs
t.
One
-By
teCompa
ris
on:
ENTRY LDA MEM
CMP MEM2
BCC LESS ; Goes to LESS if MEM < MEM2
BEQ EQUAL ; Goes to EQUAL if MEM = MEM2
BCS GREATER ; Goes to GREATER if MEM > MEM2

Two-
Byt
eCompa
ris
on:
ENTRY LDA MEM+1
CMP MEM2+1
BCC LESS ; MEM+1 < MEM2+1
BEQ CHK2 ; MEM+1 = MEM2+1
BCS GRTR ; MEM+1 > MEM2+1
CHK2 LDA MEM
CMP MEM2
BCC LESS ; MEM,MEM+1 < MEM2,MEM2+1
BEQ EQUAL ; MEM,MEM+1 = MEM2,MEM2+1
BCS GRTR ; MEM,MEM+1 > MEM2,MEM2+1
Appe
ndi
xB:
Ass
embl
yComma
nds 3
51

BEQ:
Bra
nchi
fEQua
l
Descri
pti
on:Execut
esabranchi
ftheZ-Sag(z
eroSag
)iss
et,
indi
cat
ingt
hatt
he
re
sultofaprevi
ousopera
tionwas0 .Se
eBCStoseehowacompari
sonfort
he
Accumulat
orequalt
oagivenval
ueisdone.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BEQ Address F0 FF

Note:⇢ezer
oSa g,uponwhi c
ht hi
sde pe
nds ,i
sc ondit
ionedby :ADC,AND,ASL,
BIT,
CMP,
CPX,CPY,
DEC,DEX,DEY,EOR,INC,INX,INY,LDA,LDX,LDY,LSR,ORA,PLA,
PLP,
ROL,
RTS,SBC,
TAX,TAY,
TXA, andTYA.

Us es:Inaddit
iont obe i
ngus edinc onjunct
ionwi thcompa reoperat
ions,BEQis
usedt otestwhetherther es
ul tofav a
r i
etyofotherope r
ationswas0.⇢ec om-
monc las
sesoftheseope ra
tionsa r
einc r
eme ntandde c
r e
me nt,l
ogic
aloperators
,
shi4s,andr egi
sterloads.Evene asi
ert oreme mberistheg enera
lprinci
plethat
whe neveryou’vedones ome thingthatresul
tsin0 ,chancesaregoodthattheZ-
Sagha sbe enset.Likewise
,a nynonz eroresultofanope rati
onisli
kelytoc l
ear
theZ- Sag.Oneoft hemos tc ommoni nst
anc e
siswhe nche cki
nganinputs t
ring
fora0 ,usual
lyuseda sade l
imi t
er:

Ex
ampl
e:
ENTRY LDA DEVICE
BEQ DONE ; DATA = 0
WORK (...)
JMP ENTRY
DONE RTS

BIT:
compa
reAc
cumul
atorBI
Tswi
thme
mor
y
Desc r
ipti
on:Pe rformsal ogicalAND ont hebi t
soft heAc c
umul atorandt he
contentsoftheme mor yloc
ation.⇢eoppos i
teofther esul
tiss t
oredi ntheZ-
Sag.Wha tthisme ansi st
hatifanybi t
ssetintheAc cumul atorhappe ntoma tch
anys eti
nt hev al
ues peci
Fed,theZ- Sagwillbec l
eared.Ifnoma tc
hi sfound,it
wil
l beset
.BNEi susedt odet
ectama tch,BEQd etec
tsano- ma tc
hc onditi
on.
Full
yunde r
standingthef unct
iona ndv ari
ousa pplic
a t
ionsoft hisins
truc-
ti
oni sas i
gnofha vi
nga rri
veda sana sse
mbl y-la
ngua geprog r
amme rands ug-
gest
sy oua repr obablythehi tofpa rt
ies
,t hri
ll
ingy ourf r
iendsbydoi nghe x
ari
thme ti
ciny ourhea dandrec i
ti
ngAS CIIcode soncomma nd.

]
3
52 As
sembl
yLi
nes

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

M7 M6 l

Addres
singModes CommonSy nt
ax He
xCodi
ng
Zer
oPa g
e BIT $12 24 12
Absol
ute BIT $1234 2C 34 12
Immedia
te[65C02
] BIT #$12 89 12
Zer
oPa g
e ,
X[65C02] BIT $12,X 34 12
Absol
ute
,X[ 6
5C02] BIT $1234,X 3C 34 12

Us
es:
BITpr
ovi
desame
ansoft
est
ingwhe
the
rag
ive
nbi
tiso
ninaby
teofda
ta.

I
mpor tant:BITwi l
lindica
teonlyt hatatleastoneofthebitsinque st
ionma t
ch.
I
tdoesnotindi cat
ehowma nyactua l
lydoma tch.Se
etheANDi nstr
uctiononhow
t
odoac he c
kf orallma t
chi
ng.
⇢et es
tma skcanbehe ldeitherint heAc c
umulator(iftest
ingame mory
l
oca
tion),oriname mor yloc
ati
on( whent e
stingtheAcc umulator)⇢ema
. skis
c
rea
tedbys et
tinga1i neachbitpos it
iony oua reint
erest
edi n,andl e
avi
ngall
r
emainingpos i
tionssetto0.

Exa
mpl e
s:
1.Showi
ngthere
sul
tsoft
hebitope
rat
ion:
Acc: 10011011
Mem: 01010101
Resul
t: 00010001 f1f(opposi
t)f0 BNEwor
e ks,
BEQnott
ake
n

S
tat
usRe
gis
ter
:
N V – B D IZ C
0 1 0

2
. Ac
c: 10011011
Mem: 01000100
Re
sul
t: 00000000 f0f(
oppos
it
e)f1 BEQwor
ks,
BNEnott
ake
n

S
tat
usRe
gis
ter
:
N V – B D IZ C
0 1 1
Appe
ndi
xB:
Ass
embl
yComma
nds 3
53

3
.Sa
mpl
erout
ine
s:
Te
stAc
cumul
atorf
orbi
t4on Te
stme
mor
yforbi
t4on
ENTRY LDA #$10 ; %00010000 ENTRY LDA #$10 ; %00010000
STA MEM BIT MEM
LDA DEVICE BNE MATCH
BIT MEM BEQ NOMATCH
BNE MATCH
BEQ NOMATCH

BITal
soset st
heN-andV-Sags
, a
ndt husprovi
desaveryfas
twayoftes
ti
ng
bit
s6a nd7 .Sincebit7i
sthehi
gh-orderbitandisfr
equentl
yusedtoindi
cat
e
cert
aincondit
ions,thi
sca
nbequit
eha ndy.Hereisanexampleofhowtowa t
ch
forak e
ypre
ss:
LOOP BIT KYBD ; $C000
BPL LOOP ; VAL < 128 = NOT PRESS
BIT STROBE ; $C010
DONE RTS

Not
ethatinthisex
a mple,noda t
aisactuall
yret
ri
evedfr
om thekeyboa
rd.
Onlyawa i
tisdoneuntiltheke y pre
ss.⇢eBITSTROBEs tepintheexampleal
so
pr
ov i
desanill
ust
rat
ionofas e
c onda ppli
cat
ionofBIT,whichi
stoacce
ssahard-
war
el oca
tion(o4encal
ledas o ft-s
witch)withoutda
ma g
ingtheconte
ntsofthe
Accumulat
or.

BMI:
Bra
nchonMI
nus
Des
cript
ion:Ex
ecutest
hebranchonl
yiftheN-Sa
g(si
gnSag
)iss
et.⇢eN- Sa
g
i
ssetbyanyoperat
ionproduci
ngaresul
tinthera
nge$80t
o$FF(i.
e.hi
ghbit
s
et)
.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BMI Address 30 FF

Note:⇢esi
gnSa g,uponwhi c
ht hi
sde pe
nds ,i
sc ondit
ionedby :ADC,AND,ASL,
BIT,
CMP,
CPX,CPY,
DEC,DEX,DEY,EOR,INC,INX,INY,LDA,LDX,LDY,LSR,ORA,PLA,
PLP,
ROL,
TAX,
TAY,TXS,TXA,
a ndTYA.

Us e
s:BMI i
smostcommonlyus e
dt odete
ctnega
tiv
enumbe r
swhensi
gned
binarymat
hisus
ed,
butisal
soequal
lycommonintes
ti
ngf orase
thi
ghbi
t,s
uch
asinwatc
hingt
hekeyboa
rdforakeypr
ess
.(S
eeal
soBIT.)Forex
ampl
e:

]
3
54 As
sembl
yLi
nes

LOOP LDA KYBD


BMI PRESS ; DATA > $7F
BPL LOOP ; DATA < $80

BMIi
sal
sousef
ulf
ort
ermina
tingal
oopt
haty
ouwa
ntt
ore
ach0a
ndwhi
ch
ot
herwi
sewi
ll
sta
youtoft
he$80to$FFr
ange
:
ENTRY LDX $20 ; TO LOOP 33 TIMES
LOOP DEX
BMI DONE ; WHEN X = $FF
BPL LOOP ; WHILE X > $FF
DONE RTS

BNE:
Bra
nchNotEqua
l
Des
cri
ption:
Exe
cute
sthebr
anchi
ftheZ-
Sag(ze
roSa
g)i
scl
ear
,tha
tist
osa
y,i
f
t
heres
ultofa
noper
ati
onwasanonz
e r
ova
lue.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BNE Address D0 FF

Note:⇢ezer
oSa g,uponwhi c
ht hi
sde pe
nds ,i
sc ondit
ionedby :ADC,AND,ASL,
BIT,
CMP,
CPX,CPY,
DEC,DEX,DEY,EOR,INC,INX,INY,LDA,LDX,LDY,LSR,ORA,PLA,
PLP,
ROL,
RTS,SBC,
TAX,TAY,
TXA, andTYA.

Uses:O4e nusedinl
oopstobranc
hunti
lthecount
erre
aches0
.Al
sous
edin
datainputl
oopstove
rif
ythenonze
rona
tureoft
helas
tbyt
ein,a
swhe
nchec
k-
ingfortheend-
of-
dat
adeli
mit
er.

Exampl
es:
1.
Simpl
eloop
ENTRY LDX #$20 ; WILL COUNT 32 TIMES
LOOP DEX
BNE LOOP ; UNTIL X = 0
DONE RTS

2
.Da
tai
nputr
out
ine
ENTRY LDA DEVICE
BNE CONTINUE
DONE RTS
Appe
ndi
xB:
Ass
embl
yComma
nds 3
55

3
.Asus
edi
nat
wo-
byt
einc
reme
ntr
out
ine
ENTRY LDA MEM
ADC #$01
STA MEM
BNE DONE ; UNLESS MEM = 0
LDA MEM+1
ADC #$00 ; MEM+1 = MEM+1 + 1
STA MEM+1
DONE RTS

BPL:
Bra
nchonPLus
Descri
pti
on:Execute
sthebranchonl
yiftheN-Sa
g(si
gnSag)i
scle
ar,a
swould
bethecas
ewhent heres
ultofanoper
ati
onisi
nthera
ngeof$00to$7F(hi
ghbi
t
cl
ear)
.Seeal
soBMI.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BPL Address 10 FF

Note:⇢esi
gnSa g,uponwhi c
ht hi
sde pe
nds ,i
sc ondit
ionedby :ADC,AND,ASL,
BIT,
CMP,
CPX,CPY,
DEC,DEX,DEY,EOR,INC,INX,INY,LDA,LDX,LDY,LSR,ORA,PLA,
PLP,
ROL,
TAX,
TAY,TXS,TXA,
a ndTYA.

Uses:BPLisaneas
ywa yofs
tay
inginaloopunt
ilthehig
hbitiss
et.I
tisal
so
use
di ngener
alt
ode t
ectt
hest
atusoft
hehighbi
t.Here
’sourf
amil
iarkey
pre
ss
che
ckus i
ngBPL:
LOOP LDA KYBD
BMI PRESS ; DATA > $7F
BPL LOOP ; DATA < $80

BPLi
sal
sousef
ulf
ort
ermina
tingal
oopt
haty
ouwa
ntt
ore
ach0a
ndwhi
ch
ot
herwi
sewi
ll
sta
youtoft
he$80to$FFr
ange
:
ENTRY LDX $20 ; TO LOOP 33 TIMES
LOOP DEX
BMI DONE ; WHEN X = $FF
BPL LOOP ; WHILE X > $FF
DONE RTS

]
3
56 As
sembl
yLi
nes

BRA:
BRa
nchAl
way
s[65C02]
De
scr
ipt
ion:
Alwa
yse
xec
ute
sthebr
anc
h(6
5C0
2onl
y).

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addre
ssingModes CommonSy nt
ax He
xCodi
ng
Rel
at
iveonly[
65C02
] BRA Address 80 FF

Uses: BRA( branchalways)isusef


ul f
orwrit
ingrel
ocat
ablecode.Normal
ly,i
fyou
hadal oopwi thaJMPba cktothet opyouwouldma kethisr
eloc
atabl
ebyforc
ing
abr anc h.⇢i swoul dinvolvesett
ingorclear
ingaS t
atusRegis
terSagandthen
is
suingt hea ppropri
atebr anc
hi nstr
uct
ion.Inst
ead,youc ansimplyi
ssueBRA
wit
houtc hangingtheStatusRegiste
rSags.⇢eonl yli
mitat
ionisthema x
imum
branchi ngdi s
tanceofplusormi nus128bytes
.

Ex
ampl
e:
8000: A9 12 2 LOOP LDA #$12
8002: EA 3 NOP ; MORE CODE HERE
8003: 80 FB 4 BRA LOOP

BRK:
BRe
aK(
so5wa
rei
nte
rrupt
)
Desc
ript
ion:
Whe naBRKisenc
ounte
redinapr
ogra
m,prog
rame
xec
uti
onha
lt
s
andt
heusergene
ral
lys
eess
omethi
nglik
ethef
oll
owi
ng:
0302- A=A0 X=00 Y=01 P=36 S=E7

Whata ct
uallyhappensisthatt
hePr ogram Counter
,plustwo,iss a
vedon
thesta
ck,imme diat
elyfol
lowedbytheS tat
usRe gi
st
er,inwhichtheBRKbi thas
beenset
.⇢epr oces
sorthenjumpstot headdres
sat$FFFE,$FFFF.Ont heApple
IIPlusandAppl e//
et hi
sr outi
ne(at$FA40)jumpst oav ect
ora t$3F0,$3F1
whichpointst
ot heBRKha ndle
rrout
ine(at$FA59)whichproducestheoutput.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly BRK 00
Appe
ndi
xB:
Ass
embl
yComma
nds 3
57

Us e
s:BRKc anbev eryusefuli
nde buggi
nga ss
e mbl y-
langua gepr ogra
ms .ABRKi s
simplyi nsert
edi ntothec odeats t
rat
egicpointsi nther outine .Whe nthepr o-
gramc ome stoas creec
hingha l
t,onecane xa
mi nethes ta
tusofv ari
ousme mor y
l
oc at
ionsa ndr egi
ster
st os e
eife ver
ythingisa syout hinki tshouldbe .⇢i s
processc anbef ormaliz
ed, a
ndhe nceconside
r ablyimpr ovedon, byusingaso4-
wa r
eut i
li
tycall
e dade buggerwhi c
ha l
lowsy out os t
ept hroug hapr og r
am one
inst
ruc ti
ona tat i
me .Munc h-A-
Bug ,al
ongwi thot her
s ,providest hi
soption.On
Integ
e rAppl es,apr imit
iveS t
epa ndTr acefunc ti
oni spr ovideda spa r
tofthe
Moni tor.

BVC:
Bra
nchonoVe
rFowCl
ear
Descr
ipt
ion:Exec
utesthebra
nchonlyiftheV-Sag(ov
erSowSa g
)iscl
ear
.⇢e
ove
rSow Sagiscl
eare
dwhe nevert
heresultofanopera
tiondidnotent
ailt
he
ca
rryofabitfr
om posi
tion6topos
it
ion7. ⇢eoverSowSa ga
lsocanbecl
ear
ed
wit
haCLVc ommand.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BVC Address 50 FF

Not
e:⇢eov
erSowSa
g,uponwhi
cht
hisde
pends
,isc
ondi
ti
one
dby
:ADC,BIT,
CLV,
PLP,
RTI,
andSBC.

Uses
:BVCisuse
dprimar
il
yindet
ect
ingapos
sibl
eov
e r
Sowf r
om t
heda t
apor-
ti
onofthebyt
eint
othesi
gnbi
twhenusings
igne
dbinarynumbe
rs.Forexa
m-
pl
e:
ENTRY CLC
LDA #$64 ; %01100100 = +100
ADC #$40 ; %01000000 = + 64
BVC STORE ; NOT TAKEN HERE
ERR RTS ; RESULT = +164 =
; %10100100 > $7F
STORE STA MEM

BVCcanalsobeusedasafor
cedbr
anc
hwhe nwri
ti
ngrel
oca
tabl
ecode.⇢e
a
dvantag
eisthatthec
arryre
ma i
nsuna
Lec
ted,
thusa
ll
owingi
ttobetes
tedla
ter
i
ntheconvent
ionalmanner
.
CLV ; CLEAR V FLAG
BVC LABEL ; (ALWAYS)

]
3
58 As
sembl
yLi
nes

BVS:
Bra
nchoVe
rFowSe
t
De scr
ipt
ion:Exe
cutesthebr
anchonlywhe ntheV-Sag(over
SowSa g)iss
et.
⇢e
ov e
rSowSa giss
etonlywhenther e
s ul
tofanope rat
ioncaus
esac arr
yofabi t
from pos
iti
on6t opositi
on7.Notethatther
ei snotacomma ndtos pec
iFca
ll
y
settheover
SowSa g(aswouldcor
respondtoaSECc omma ndforthecarr
y)but,
intheApple,t
heinst
ructi
onBIT$FF58iso4enus edtosett
heover
SowSa g.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BVS Address 70 FF

Not
e:⇢eov
erSowSa
g,uponwhi
cht
hisde
pends
,isc
ondi
ti
one
dby
:ADC,BIT,
CLV,
PLP,
RTI,
andSBC.

Uses
:BVSisuse
dprimar
il
yindet
ect
ingapos
sibl
eov
e r
Sowf r
om t
heda t
apor-
ti
onofthebyt
eint
othesi
gnbi
twhenusings
igne
dbinarynumbe
rs.Forexa
m-
pl
e:
ENTRY CLC
LDA #$64 ; %01100100 = +100
ADC #$40 ; %01000000 = + 64
BVS ERR ; RESULT = +164 =
; %10100100 > $7F
STORE STA MEM
DONE RTS
ERR JSR BELL ; ALERT TO OVERFLOW

CLC:
CLe
arCa
rry
De
scr
ipt
ion:
Cle
arst
hec
arr
ybi
toft
heS
tat
usRe
gis
ter
.
Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly CLC 18

Uses:
CLCi
susual
lyr
equi
redbe
for
etheFr
stADCins
truc
ti
onofana
ddit
ionope
r-
at
ion,t
omakesur
etheca
rryha
sn’
tina
dver
tent
lybee
ns e
tsome
wheree
lsei
nthe
Appe
ndi
xB:
Ass
embl
yComma
nds 3
59

prog
ramandthusincor
rect
lya
ddedtot
heva
luesus
edintherout
ineit
sel
f.A
CLCal
soc
anbeusedtoforc
eabranc
hwhenwr
it
ingre
loc
ata
blec
ode,suc
has:
CLC
BCC LABEL ; (ALWAYS)

CLD:
CLe
arDe
cima
lmode
Desc
ript
ion:CLDi
susedt
oe nt
ert
hebina
rymode(
whicht
heApplei
susual
lyi
n
bydef
ault
)soa st
oprope
rlyuset
heADCandSBCi
nst
ruct
ions
.(S
eeSEDforse
t-
ti
ngdeci
ma l
mode.)

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly CLD D8

Use
s:⇢ea r
it
hme t
icmodeoft he6502isanimport
antpointt
ok e
epinmind
whenusingtheADC andSBCinst
ruc
tions
.Ifyouareinthewrongmodefr
om
whatyoumi ghtass
ume ,ra
therunpredi
cta
blere
sult
scanoc c
ur.Seet
heSED
i
nst
ruc
tionentryf
ormor ede
tai
lsontheothe
rmode.

CLI:
CLe
arI
nte
rruptma
sk
De
scr
ipt
ion:
⇢isi
nst
ruc
ti
one
nabl
esi
nte
rrupt
s.
Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly CLI 58

Us e
s :CLItel
lst
he6 502torecognizeincomingIRQ(I
nte
rruptReQue s
t)si
gnal
s.
⇢eAppl e
’sdef
aul
ti stoha veinterr
uptsenabl
edbut
,a4ertheFrsti
nterr
upt,
all
succeedingint
err
upt saredisable
dbyt he6502unti
laCLIisre-i
ss
ued.Asama t-
t
e rofi nt
eres
t,t
iming -
de pe
nde ntrouti
nesli
ketheDOSRWTS( Read/
Wr i
teTrack
Sector)routi
nedis
a bleinte
rruptswhileonandthenal
lowthema gai
nwi t
haCLI
atexit.

]
3
60 As
sembl
yLi
nes

CLV:
CLe
aroVe
rFowFa
g
Des
cri
pti
on:⇢i
scl
ear
stheV-
Sag(
ove
rSowSa
g)bys
ett
ingt
heVbi
toft
heS
ta-
t
usRegi
st
ert
o0.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly CLV B8

Us
es:
Bec
aus
etheov
erSowSa
gis
,inf
act
,cl
ear
edbyanon-
ove
rSowr
esul
tofa
n
ADCi
nst
ruc
ti
on,
itus
ual
lyi
snotne
ces
sar
ytoc
lea
rtheSa
gpr
iort
oana
ddi
ti
on.
It
i
s,however,occa
sional
lyusedasarel
at
ive
lyunobtrus
ivewayoffor
cingabranch
whenwr i
ti
ngr el
ocatab
lecode.
⇢isisdoneinama nnersi
mila
rtotheCLC,BCCorSEC,BCSpairsdi
scus
sed
i
nc hapt
er15 .
⇢eg ener
alfor
mi s
:
CLV
BVC ADDRESS

⇢ist
echni
quehastheadvant
ageofnotaLe
cti
ngt
hec
arr
ySa
g,s
houl
dthe
us
erwa
nttotes
tthec
arr
ya 4ert
hefor
cedbrea
k.

CMP:
CoMPa
ret
oAc
cumul
ator
Descr
ipt
ion:CMP compa re
st heAccumulatortoas pe
ciFedval
ueorme mory
l
ocati
on.⇢eN- Sag( s
ignSag )
,Z-Sa
g( z
eroSa g
),andC- Sag(c
arr
ySag)a
recon-
di
ti
oned.A condi
ti
ona lbranchisusuall
ythendonet odeter
minewhet
herthe
Accumula
torwaslessthan,equal
to,orgre
atertha
nt hedata.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l
Appe
ndi
xB:
Ass
embl
yComma
nds 3
61

Addres
singModes CommonSy
nta
x He
xCodi
ng
Immedia
te CMP #$12 C9 12
ZeroPag
e CMP $12 C5 12
ZeroPag
e ,
X CMP $12,X D5 12
Absol
ute CMP $1234 CD 34 12
Absol
ute
,X CMP $1234,X DD 34 12
Absol
ute
,Y CMP $1234,Y D9 34 12
(I
ndir
ect
,X) CMP ($12,X) C1 12
(I
ndir
ect
),Y CMP ($12),Y D1 12
(I
ndir
ect
)[65C0
2] CMP ($12) D2 12

Uses:CMP i
susedt ochecktheval
ueofaby tea ga
instcer
tai
nv a
luessuc
ha s
wouldbedonei nl oopsorindata-
proces
singr outi
nes.⇢er out
inetypi
cal
ly
dec
ideswhethertheresul
tisl
esst
han,equalto,org r
eatert
hanac r
it
ica
lval
ue.
⇢eus ualpat
ter
ni s
:
BCC: Ac
cumul
at
or<v
alue
BCS: Ac
cumul
aorjv
t a
lue
BEQ,
BCS: Ac
cumul
at
or>v
alue
S
eet
hes
ect
iononBCCt
hroug
hBCSf
ors
pec
iFce
xampl
es.

I
mpor
tant
:ACMP#$00s
houl
dne
verbedone
.2Cons
ide
rthi
sex
ampl
e:
LOOP DEC MEM
LDA MEM
CMP #$00
BCS LOOP ; (ALWAYS TAKEN!)
BCC DONE
DONE RTS

Bec
ause$01through$FFar
egrea
tert
han$0,t
hebranchwi
llbet
ake
nwhi l
e
MEMi
sinthisra
nge.Since$0=$0,whenMEMrea
ches$0thebr
anchwil
lst
il
lbe
t
ake ⇢e
n. ref
ore
,theexampl
ecrea
tesanendles
sloopwhi
chwi
ll
ne v
erte
rminat
e.
Si
mila
rly
,iftheBCCisdoneFrsti
twillneverbet
ake
nbecaus
etherei
sno
v
aluel
esst
han0totrig
gerit
.

CT]⇢i
2[ sshoul
dpr
oba
blys
tat
e“i
tshoul
dnotbeus
edwi
thBCSorBCC.
”I sFnet
ti ous
e
CMP#$00wi
thBEQa
ndBNE.

]
3
62 As
sembl
yLi
nes

CPX:
ComPa
reda
tat
otheX-
Reg
ist
er
Desc
ript
ion:CPX c
ompa r
esthecont
ent
soft
heX-
Reg
ist
era
gai
nstas
pec
iFe
d
va
lueormemor yl
ocat
ion.Se
eal
soCMP.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Immedia
te CPX #$12 E0 12
Zer
oPa g
e CPX $12 E4 12
Absol
ute CPX $1234 EC 34 12

Uses
:CPXisuse
dprimari
lyi
nloopswhichrea
dda t
ata
ble
s,wi
tht
heX-Regis
ter
be
ingusedastheoLseti
ntheAbsolut
e,
Xa ddre
ssi
ngmode.⇢eX- Re
gis
teris
us
uall
yloade
dwi t
h0a ndthenincr
emente
dunt i
litr
eac
hest
helengt
hoft he
da
tastr
eamtoberead.
Forexa
mple:
ENTRY LDX #$00
LOOP LDA DATA,X
JSR PRINT
INX
CPX #$05
BCC LOOP
DONE RTS
DATA ASC "TEST!"

Fort
hes
amer
eas
onsdi
scus
sedunde
rCMP,
aCPX#$00s
houl
dnotbeus
e 3
d.

CPY:
ComPa
reda
tat
otheY-
Reg
ist
er
Desc
ript
ion:CPY c
ompa r
esthecont
ent
soft
heY-
Reg
ist
era
gai
nstas
pec
iFe
d
va
lueormemor yl
ocat
ion.Se
eal
soCMP.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

3[
CT]S
imi
la
rtoCMP,
i sFnet
ti ous
eCPX#$00wi
thBEQa
ndBNE.
Appe
ndi
xB:
Ass
embl
yComma
nds 3
63

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Immedia
te CPY #$12 C0 12
Zer
oPa g
e CPY $12 C4 12
Absol
ute CPY $1234 CC 34 12

Uses
:⇢eY- Regis
terus
uall
yisuse
dwhe nre
adingast
rea
m ofdatafr
om azero-
pa
gepointe
r.CPYall
owsforche
cki
ngthecurr
entval
ueoftheY-Reg
ist
erag
ainst
acri
ti
calva
lue.I
nt hi
sexample
,theY-Regi
st
erisusedt
or et
ri
evetheFr
stFv e
by
tesofanApples
o4programli
ne:
ENTRY LDY #$00
LOOP LDA ($67),Y ; PROG BEG + Y
STA ($06),Y ; TEMP STORAGE AREA
INY
CPY #$05
BCC LOOP ; LOOP FOR 5 BYTES
DONE RTS

Fort
hes
amer
eas
onsdi
scus
sedunde
rCMP,
aCPY#$00s
houl
dnotbeus
e 4
d.

DEC:
DECr
eme
ntame
mor
yloc
ati
on
Descr
ipti
on:⇢ec onte
ntsoft
hespeci
Fedmemoryloca
tionarede
cre
me nt
edby
one.I
ftheorig
inalcontent
swereequa
lto$00,t
hentheresul
twil
lwrapar
ound
andgi
vear e
sultof$FF.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l l

Addres
singModes CommonSy nt
ax He
xCodi
ng
Zer
oPa g
e DEC $12 C6 12
Zer
oPa g
e ,
X DEC $12,X D6 12
Absol
ute DEC $1234 CE 34 12
Absol
ute
,X DEC $1234,X DE 34 12
Accumulat
or[
65C02
] DEC A 3A

Uses
:DECusua
ll
yisus
edwhendecr
ementi
ngaone-
byt
ememor
yval
ue(suc
has
anoLse
t)orat
wo-byt
ememorypoi
nte
r.Herea
ret
hecommone
xampl
es:

4[
CT]S
imi
la
rtoCMP,
i sFnet
ti ous
eCPY#$00wi
thBEQa
ndBNE.

]
3
64 As
sembl
yLi
nes

One
-By
teVa
lue
:
ENTRY DEC MEM
DONE RTS

Two-
Byt
ePoi
nte
r:
ENTRY DEC MEM
LDA MEM
CMP #$FF ; WRAP-AROUND?
BNE DONE ; NO
DEC MEM+1 ; YES: DEC MEM+1
DONE RTS

A4ertheDECope ra
tion,theN-and/orZ-Sagso4enarechec
kedtose
eift
he
r
esul
twasne ga
tiveorazero/nonze
rovalue,r
espect
ive
ly.
⇢et e
c hniqueshownf orthetwo-byt
eDECope rat
ionisnotnec
ess
ari
lyt
he
moste
Wc i
ent.SeetheSBCe ntr
yforanalt
ernat
iveme t
hod.

DEX:
DEc
reme
ntt
heX-
Reg
ist
er
Descr
ipt
ion:⇢eX- Reg
ist
erisdec
rement
edbyone .Whentheor
igi
nalv
aluei
s
$00,
theres
ultwi
llwr
aparoundtogi
veares
ultof$FF.
Seeal
soDEC.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly DEX CA

Use
s:DEXo4eni
suse
dinrea
dingada
tabl
ockv
iai
nde
xeda
ddr
ess
ing
,i.
e.Abs
o-
l
ute
,X.Her
eisasi
mplee
xample:
ENTRY LDX #$05
LOOP LDA DATA-1,X
JSR PRINT
DEX
BNE LOOP
DONE RTS
DATA ASC "!TSET"

Note:⇢erearesev
era
l poi
ntsofi
nter
estinthise
xample.
Besi
desthegener
aluse
oftheX-Regi
sterintheindexedaddress
ingmode ,not
icethatt
heloopr uns
bac
k wa
rdsfr
om $05to$01.⇢el oopisterminat
edwhentheX- Re
gis
terrea
ches
0.Beca
usethelooprunsfrom hig
hme morydown,theAS CIIst
ri
ngi sputin
Appe
ndi
xB:
Ass
embl
yComma
nds 3
65

me moryinrev
ers
eorder,asevi
dencedinthel
is
ti
ng.Als
onotetha
ttheba
se
addre
ssofthel
oopisDATA-1.
⇢i sal
lowst
heuseoft
he$05to$01va
lue
soft
he
X-Regi
ste
r.

DEY:
DEc
reme
ntt
heY-
Reg
ist
er
Descr
ipt
ion:⇢eY- Regi
st
erisdec
rement
edbyone .Whentheor
igi
nalv
aluei
s
$00,
theres
ultwi
llwra
paroundtogi
veares
ultof$FF.
Seea
lsoDEC.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly DEY 88

Uses:DEY usuall
yisus edwhende crementi
ngar ev
ersescanofada t
ablock,
usi
ng a z ero-page poi
nterv i
ai ndirec
ti ndexeda ddre
ssing (s
ucha sLDA
($FF),Y).Revers
escanso4ena r
eus e
dbe causeit
’ssoeasytous eaBEQinst
ruc
-
ti
ont ode t
ectwheny ou’r
edone.DEYi sal
sous e
dwhe nma kingac ounte
rfora
smallnumbe rofcycl
es.Here
’sarouti
newhi choutput
sav ar
iablenumberofcar
-
ri
agereturns,a
sindicat
edbythecontentsofMEM.
ENTRY LDY MEM
LOOP LDA #$8D ; <RETURN>
JSR COUT ; $FDED
DEY
BNE LOOP ; UNTIL Y=0
DONE RTS

EOR:
Exc
lus
iveORwi
thAc
cumul
ator
Descri
ption:⇢ev alueintheAc cumula
torisexcl
usiv
eOR’ dwi t
hthes pecFe
i d
dat
a.⇢eN- Sag(si
gnSa g)andZ-Sag(zeoSa
r g)a
rea l
socondit
ioneddepending
ontheresult
.⇢er es
ultisputbackintheAccumulator.⇢eme mor yloc
a t
ion(i
f
spe
ciFed)isunaLec
ted.
EORme a
nsthatifei
therbi
t,butnotboh,
t is1thentheresul
twillbe1,other
-
wis
ether es
ultwil
lbe0.

]
3
66 As
sembl
yLi
nes

Tr
uthTa
ble 0 1 Ex
ample
Acc
umulat
or: 00110011
0 0 1 Memory: 01010101
1 1 0 Re
sult
: 01100110

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addres
singModes CommonSy
nta
x He
xCodi
ng
Immedia
te EOR #$12 49 12
ZeroPag
e EOR $12 45 12
ZeroPag
e ,
X EOR $12,X 55 12
Absol
ute EOR $1234 4D 34 12
Absol
ute
,X EOR $1234,X 5D 34 12
Absol
ute
,Y EOR $1234,Y 59 34 12
(I
ndir
ect
,X) EOR ($12,X) 41 12
(I
ndir
ect
),Y EOR ($12),Y 51 12
(I
ndir
ect
)[65C0
2] EOR ($12) 52 12

Uses
:EORhasawidevar
iet
yofuses
:
()⇢emos
1 tcommoni stoencodeda
tabydoi
nganEORwit
hanarbi
tr
aryone
-
byt
ekey.⇢edatama yt
henbede c
ode
dlat
erbyag
aindoi
nganEORofe
achdat
a
byt
ewitht
hesamek e
y.
CODE LDX #$05
LOOP LDA DATA1,X
EOR #$7D ; ARBITRARY "KEY"
STA $300,X ; REWRITE TABLE
DEX
BNE LOOP ; UNTIL X=0
DONE RTS
DATA ASC "TEST!"
DECODE LDX #$05
LOOP LDA $300,X ; RETRIEVE CODED DATA
EOR #$7D
STA $380,X ; PUT IN NEW LOC
DEX
BNE LOOP
DONE RTS

(
2)Anothe
rappli
cat
ionist
oreve
rsea
nyg i
venbitorbi
tsofadatabyt.⇢e
e
mas
kisc r
eat
edbyput t
ingaoneinthepos i
ti
onswhi
chy ouwishtoha v
e
r
eve
rse
d.A0i sputi
na l
lrema
ini
ngposi
ti
ons.WhentheEORwit
ht hemaskis
Appe
ndi
xB:
Ass
embl
yComma
nds 3
67

done,bi
tsinthespe
ciFedposi
ti
onswillr
eve
rs,
ei.
e.one
swil
lbecomezer
os,a
nd
vi
c ev
ers
a .
SeetheFr
stexamplei
nthisent
rytover
if
ythi
seLe
ct.
(3)⇢eN- Sag(si
gnSag)canbeuse
dt odet
ecti
fbothmemoryandtheAcc
umu-
l
atorhavebit7se
t:
ENTRY LDA MEM
EOR MEM2
BMI MATCH ; BOTH SET
BPL NOMATCH ; BOTH NOT SET

(4)⇢eZ-Sag(
zer
oSa
g)Sa
gwi
llbes
eti
fei
the
rtheAc
cumul
at
ororme
mor
y,or
both,
equa
l0:
ENTRY LDA MEM
EOR MEM2
BEQ ZERO ; MEM=0 AND/OR MEM2=0
BNE NOTZ ; NEITHER MEM NOR MEM2 = 0

(
5)EORi
sal
sous
efuli
nproduc
ingt
het
wo’
scompl
eme
ntofanumbe
rforus
ein
s
igne
dbi
nar
yari
thmeti
c.
ENTRY LDA #$34 ; %00110100 = +52
; TO BE CONVERTED TO -52
EOR #$FF ; %11111111 = $FF
; RESULT = %11001011
CLC
ADC #$01 ; RESULT = RESULT + 1
; = %11001100 = $CC
STA MEM ; STORE RESULT
DONE RTS

(
5a)Andt
oconv
erts
igne
dne
gat
ivenumbe
rsba
ck:
ENTRY LDA #$CC ; %11001100 = $CC = -52
; TO BE CONVERTED BACK
SEC
SBC #$01 ; ACC = ACC - 1
; = %11001011 = $CB
EOR #$FF ; REVERSE ALL BITS
; RESULT = %00110100 = $34 = +52
STA MEM ; STORE RESULT
DONE RTS

INC:
INCr
eme
ntme
mor
y
Descr
ipti
on:⇢ec onte
ntsofaspec
iFedme mor
yloc
ati
ona r
einc
rement
edby
one.I
ftheorigi
nalval
ueis$FF,t
henincr
ementi
ngwil
lre
sul
tinawraparound
gi
vingar e
sultof$00.⇢eN- Sa
g(si
gnSa g
)andZ-Sag(ze
roSag)ar
econdi-
ti
oneddependi
ngont heres
ult
.

]
3
68 As
sembl
yLi
nes

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l l

Addres
singModes CommonSy nt
ax He
xCodi
ng
Zer
oPa g
e INC $12 E6 12
Zer
oPa g
e ,
X INC $12,X F6 12
Absol
ute INC $1234 EE 34 12
Absol
ute
,X INC $1234,X FE 34 12
Accumulat
or[
65C02
] INC A 1A

Uses:INCi
susedmosto4enf
ori
ncre
ment
ingaone
-byt
eva
lue(
suc
ha noL-
sa
s
et)oratwo-by
tepoi
nte
r.Her
ear
ethemos
tcommonfor
ms:

One
-By
teVa
lue
:
ENTRY INC MEM
DONE RTS

Two-
Byt
ePoi
nte
r:
ENTRY INC MEM
BNE DONE
INC MEM+1
DONE RTS

A4ertheINC ope
rat
ion,theN-a nd/orZ-Sa
gso4enarecheck
edt
ose
e
whe
thert
heres
ultwasneg
a t
iveoraz
ero/nonze
roval
ue,
res
pec
tiv
ely
.

INX:
INc
reme
ntt
heX-
Reg
ist
er
De s
cri
pti
on:⇢ec ont
ent
soft
heX-Regi
st
era r
eincr
emente
dbyone.
Iftheori
gi-
nalval
ueis$FF,
theni
ncr
ement
ingwil
lres
ultinawraparundg
o i
vi
ngar esul
tof
$00.⇢eN- Sag(si
gnSag
)andZ-Sag(ze
roSa g)a
recondi
ti
one
ddependingon
theres
ult
.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly INX E8
Appe
ndi
xB:
Ass
embl
yComma
nds 3
69

Uses
:INXi
sus
edi
nfor
war
d-s
canni
ngl
oopswhi
chdi
ges
tada
tas
tre
ama
sshown
he
re:
ENTRY LDX #$00
LOOP LDA DATA,X
BEQ DONE ; DELIMITER?
JSR COUT ; $FDED
INX
JMP LOOP ; NEXT CHAR
DONE RTS
DATA ASC "TEST!"
HEX 00 ; END OF DATA

Notet
hati
nfor
wa r
d-s
canni
ngl
oops,t
hebaseaddr
esscanbeDATAit
sel
f(s
ee
DEXf
oranot
hers
it
uat
ion).
INXal
socanbeusedasagene
ral
-purpos
ecounte
rf ormis
cel
lane
ousrou-
t
ine
s:
ENTRY LDX #$00
LDA #$8D ; <RETURN>
LOOP JSR COUT ; $FDED
INX
CPX #$05
BCC LOOP ; UNTIL X = 5
DONE RTS ; PRINTS 5 <CR>S

INY:
INc
reme
ntt
heY-
Reg
ist
er
De s
cri
pti
on:⇢ec ont
ent
soft
heY-Regi
st
erareinc
remente
dbyone.
Iftheori
gi-
nalval
ueis$FF,
theni
ncr
ement
ingwil
lres
ulti
nawr aparundg
o i
vi
ngar esul
tof
$00.⇢eN- Sag(si
gnSag
)andZ-Sag(ze
roSag)arecondi
ti
one
ddependingon
theres
ult
.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly INY CB

Uses
:INY isuse
di nf or
ward-sc
a nni
ngloopswhi chusetheindi
rec
tinde
x e
d
addr
ess
ingmode ,forexampleLDA($FF),Y.⇢i sisquit
ecommoni nrout
ines
whic
hpr oc
essst
ringsf
orc e
rtai
nc hara
cte
rs,s
earchrouti
nes
,et
c.Hereisar
ou-
t
inewhichsca
nstheinputbuLerfortheFrs
tcarr
iageret
urn:

]
3
70 As
sembl
yLi
nes

ENTRY LDY #$00


STA PTR
LDA #$02
STA PTR ; PTR,PTR+1 = $200
LDY #$00
LOOP LDA (PTR),Y
CMP #$8D ; CHR = <CR>?
BEQ FOUND
INY
BNE LOOP ; UNTIL Y = $00
DONE RTS
FOUND STY MEM
BEQ DONE ; (ALWAYS)

JMP:
JuMPt
oaddr
ess
De
scr
ipt
ion:
Caus
espr
ogr
ame
xec
uti
ont
ojumpt
othea
ddr
esss
pec
iFe
d.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addr ess
ingModes CommonSy ntax He
xCodi
ng
Absolute JMP $1234 4C 34 12
I
ndirect JMP ($1234) 6C 34 12
(
Abs olut
eIndi
rec
t,
X)[
65C0
2] JMP ($1234,X) 7C 34 12

Not e
:⇢e6 502hasawe l
l-
docume ntedbugrega rdi
ngt heindirectjump.5Ifthe
jumps peci
Fe dusespointer
swhi chdonotc r
ossapa gebounda ry( f
orexample,
$3C0,$3C1) ,thena l
lwi l
lg oa spredic
ted.If,howe v er
,t hepoi nte
rsc r
ossa
bounda r
y( suchas$3FF,$400),thentheassume dby te
swi llnotbeus d.
e Inst
ead,
theaddressda t
awillberet
rieve
d( i
noure xample )fr
om loc a
tions$3FFa nd$300.
⇢a tistos a ythatthehigh-orderbyteisnotpr operl
yi ncrement e
da ndbot h
bytesareretrie
v e
df rom t
hes amepa geofme mor y.⇢i sshouldbet ake
ni nto
accountifsuchas i
tuati
oncanpos si
blyari
seiny ourroutine.

Uses
:Be s
idestheobv i
ousappli
cat
ion oftheus ua
la bs
oluteaddr
ess
edJMP
i
nst
ruct
ion,theindi
rectJMPi
susedwhenc rea
tingvect
ore
dj umps.⇢eApple
us
esmanys uchi
ndir
ectjumps
,themostnota
bleofwhichar
e:
Function Rout i
ne J
umpst
oAddr
essa
tVe
ctorLoc
ati
on
Inte
rruptVector IRQ ($FA40) IRQLOC ($3FE,$3FF)
BreakVector BREAK ($FA4C) BRKV ($3F0,$3F1)
InputVector RDKEY ($FD0C) KSWL ($38,$39)
OutputVe ct
or COUT ($FDED) CSWL ($36,$37)

5[
CT]⇢i
sbugi
sFx
edi
nthe6
5C0
2.
Appe
ndi
xB:
Ass
embl
yComma
nds 3
71

Anindir
ectJMPa l
socanbeus e
dwhe nwr i
ti
ngr el
ocata
blecode.Ifthec
ur-
r
entl
ocat
ionofthecodec a
nbede t
ermined,t
hena noLsetcanbec a
lcula
tedand
t
hevect
orssetups othattheJMPwillberel
at
ivetothec urre
ntlocat
ionofthe
c
ode.S
eechapter15formor ei
nfor
ma ti
onaboutthes
etechniques
.

JSR:
Jumpt
oSubRout
ine
Description:⇢ea ddres
softheinst
ructionf
oll
owingtheJSRi
spus
hedontot
he
s
tack.⇢ea ddre
ssfoll
owingt heJSR isthe
nj umpedto.WhenanRTS i
nthe
cal
leds ubrout
ineisencounte
red,areturntotheloc
ati
ononthest
ack(t
heone
a4ertheJSR)isdone .
⇢i sisanal
ogoustoaGOSUBinBAS I
C.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Absol
uteonl
y JSR $1234 20 34 12

Us es:JSR isoneoft hemos tc ommonl yus edi nstr


ucti
ons,beingusedt ocal
l
o4e n-ne e
de dsubroutines.⇢edi sadvantageofthei nst
ruct
ioni st
hatiftheJSR
referencesa na ddresswithinthec ode(a soppose dtoroutinesexte
rnaltothe
pr ogram,s ucha sint heMoni torROM) ,thecodec anbee x e
cutedonlyatthe
loc a
tionforwhi chthec odewa sor i
ginall
ya ss
embl ed.
Becausethec al
lingaddressi ssavedont hes tack
,aJSRt oak nownRTSc an
bedone ,andt heda tacanber etrievedtode ter
mi newhe r
einme morytherou-
tineisc urr
e nt
lybe i
nge xecut
ed.
Seecha pt
er1 5formor edeta i
lsaboutbothoft heset
opics.

LDA:
Loa
DAc
cumul
ator
Descr
iption:LoadstheAc
cumul
atorwit
heit
herthespeci
Fedv a
lueort
hecon-
te
ntsofthede s
ignat
edmemoryl
ocati
on.⇢eN- Sag(s
ignSa g
)andZ-Sag(
zero
Sag)arecondit
ionedwhenaval
uewiththehi
ghbi ts
etisloade
d,orwhena0
val
ueisloaded.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

]
3
72 As
sembl
yLi
nes

Addres
singModes CommonSy
nta
x He
xCodi
ng
Immedia
te LDA #$12 A9 12
ZeroPag
e LDA $12 A5 12
ZeroPag
e ,
X LDA $12,X B5 12
Absol
ute LDA $1234 AD 34 12
Absol
ute
,X LDA $1234,X BD 34 12
Absol
ute
,Y LDA $1234,Y B9 34 12
(I
ndir
ect
,X) LDA ($12,X) A1 12
(I
ndir
ect
),Y LDA ($12),Y B1 12
(I
ndir
ect
)[65C0
2] LDA ($12) B2 12

Uses
:LDAi spr
obabl
ythemos
tusedi
nst
ruct
ion.⇢evastmaj
ori
tyofoper
ati
ons
ce
nteraroundtheAcc
umula
tor,
andthi
sins
truct
ioni
suse
dtogetdat
aint
othis
i
mpor t
antregi
st
er.

LDX:
Loa
DtheX-
Reg
ist
er
Descri
ption:Loadst
heX-Regi
st
e rwit
hei
therthespe
ciFedva
lueorthec
onte
nts
ofthedesignat
edmemoryloca
tion.⇢eN- Sag(si
gnSa g
)andZ-Sag(ze
roSag)
areconditi
onedwhenav al
ueisl oa
dedt
ha thasthehighbi
tset,orwhena0
val
ueisloaded.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Immedia
te LDX #$12 A2 12
Zer
oPa g
e LDX $12 A6 12
Zer
oPa g
e ,
Y LDX $12,Y B6 12
Absol
ute LDX $1234 AE 34 12
Absol
ute
,Y LDX $1234,Y BE 34 12

Use
s:⇢isi
sthepr
ima
rywa
yinwhi
chda
tai
spl
ace
dint
otheX-
Reg
ist
er.Wha
t
morec
anIs
ay?
Appe
ndi
xB:
Ass
embl
yComma
nds 3
73

LDY:
Loa
DtheY-
Reg
ist
er
De sc
ript
ion:Loadst
heY-Reg
ist
erwi
the
it
herthespe
ciFedva
lueorthec
ont
ent
s
ofthedesigna
tedmemoryloc
ati
on.⇢eN-Sag(si
gnSa g
)andZ-Sag(ze
roSag
)
arecondit
ionedwhenaval
uewitht
hehi
ghbitse
tisl
oaded,
orwhe na0val
uei
s
l
oa ded.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Immedia
te LDY #$12 A0 12
Zer
oPa g
e LDY $12 A4 12
Zer
oPa g
e ,
X LDY $12,X B4 12
Absol
ute LDY $1234 AC 34 12
Absol
ute
,X LDY $1234,X BC 34 12

Use
s:⇢isist
hepr
imar
ywayi
nwhi
chda
tai
spl
ace
dint
otheY-
Reg
ist
er.
SeeLDX
f
oraddi
ti
onalc
omment
s.

LSR:
Log
ica
lShi
5Ri
ght
Desc
ripton:⇢i
i sins
truc
ti
onmov ese a
c hbitoftheAc cumula
tororspecFe
i d
memorylocat
iononeposi
tiontother i
ght.A0isforcedatthebi
t7posit
ion(
the
hi
gh-orde
rbit
),andbit0f
allsi
ntothec a
rry.⇢er es
ultisl
e4intheAccumulat
or
ormemorylocat
ion.(
Seeal
s oASL,ROL,andROR.)

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

0 l l l l

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Accumulat
or LSR 4A
Zer
oPage LSR $12 46 12

]
3
74 As
sembl
yLi
nes

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Zer
oPa g
e ,
X LSR $12,X 56 12
Absol
ute LSR $1234 4E 34 12
Absol
ute
,X LSR $1234,X 5E 34 12

Uses
:LSRprov
ide
sa ne
asywa yofdi
vidi
ngbytwo.⇢ecor
respondi
ngeLecti
n
de
cimala
rit
hmeti
ciswe
llknown:123
/10=12.
3(shi
4ri
ght
).Asane x
ample
:
ENTRY LDA MEM
LSR ; DIV BY 2
LSR ; DIV BY 2 AGAIN
STA MEM ; MEM = MEM / 4

LSRa
lsopr
ovi
desaf
astwa
yofde
tec
ti
ngwhe
the
ranumbe
risoddore
ven:
ENTRY LDA MEM
LSR
BCS ODD
BCC EVEN

Beca
usebit0det
erminestheodd/
eve
nnatur
eofanumber
,thi
siseas
il
ytra
ns-
fe
rre
dtothecar
ryviatheLSRandt
henche
ckedv
iat
heBCS/
BCCi
nstr
uct
ions
.

NOP:
NoOPe
rat
ion
Des
cri
pti
on:Doe
snothi
ngf
oronei
nst
ruc
ti
on(
twoc
ycl
es)
.Ma
yre
mindy
ouof
s
omepeopl
eyouknow.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly NOP EA

Uses:NOPisusedprimari
lytodisableport
ionsofc odewri
tte
nb yotherprogra
m-
me r
sthatyouha vedeci
dedy ouc anli
vewi thout.Acla
ssi
cexa mpleofthi
sisthe
plac
ingoft hre
eNOPsa tbytes$D3,$D4,and$D5onTr ack0,S ect
or9,ofas t
an-
dardDOS3 .3dis
kette
.Byt hestrate
gicplaceme ntoft
heseNOPs,abootwillnot
for
ceac l
earofthelanguagecard,thusavoidingtherat
hermonot onousLOADING
LANGUAGE CARDme ss
ageone veryboot6
.
Addit
ionall
y,NOPsma ybeus edduri
ngde buggi
ngtodisablecer
tai
nstepsor
tocreat
ecertai
ntimingperiods.
6[
CT]Youc
ana
ccompl
is
hthes
amet
askbya
ddi
ngt
hre
eNOPsa
t$BFD3:
POKE -16427,234: POKE -16428,234: POKE -16429,234
a
ndt
heni
nit
ial
iz
ingadi
sk.
Whe
nthedi
ski
sboot
edi
twi
ll
note
ras
ethel
ang
uag
eca
rd.
Appe
ndi
xB:
Ass
embl
yComma
nds 3
75

ORA:
Inc
lus
iveORwi
tht
heAc
cumul
ator
Descr
ipti
on: ⇢isinstr
uctiontakeseachbi
toftheAc c
umulatorandpe r
formsa
l
ogic
alinclus
iveORwi t
he achcorres
pondingbitoft
hes pe
ciFedme moryloca-
t
ionorimme di
ateva
lue.⇢er esulti
sputba c
kintheAccumulator
.⇢eme mor y
l
ocat
ion,ifspeci
Fed,isuna Le
cted.Condit
ionst
heN- Sag(si
gnSa g)andZ- Sag
(
zeroSag)dependingont heres
ult.(
Seeals
oANDa ndEOR.
)ORAme ansifei
theror
bohbi
t tsare1thentheresulti
s1. Onl
ywhe nbothbit
sare0isthere
sult0.
Tr
uthTa
ble 0 1 Ex
ample
Acc
umulat
or: 00110011
0 0 1
Memory: 01010101
1 1 1 Re
sult
: 01110111

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l
Addres
singModes CommonSy
nta
x He
xCodi
ng
Immedia
te ORA #$12 09 12
ZeroPag
e ORA $12 05 12
ZeroPag
e ,
X ORA $12,X 15 12
Absol
ute ORA $1234 0D 34 12
Absol
ute
,X ORA $1234,X 1D 34 12
Absol
ute
,Y ORA $1234,Y 19 34 12
(I
ndir
ect
,X) ORA ($12,X) 01 12
(I
ndir
ect
),Y ORA ($12),Y 11 12
(I
ndir
ect
)[65C0
2] ORA ($12) 12 12

Us e
s :ORAisusedpri
maril
yasama s
kt ofor
ce1si
ns pe
ciFe
dbi tposi
ti
ons.(
See
ANDt oforce0s.
)Tocrea
tethemask
,a1i sputi
neachbi
tposit
ionwhichistobe
forc
e d.Allot
herposi
ti
onsarese
tto0.Forexampl
e,her
eisaroutinewhic
hwi l
l
setthehighbitonanyASCIIdat
agoingoutthr
oug
hCOUT:
ENTRY LDA DEVICE
ORA #$80 ; %10000000, SET HIGH BIT
JSR COUT ; $FDED

ORAa
lsoc
anbeus
edt
oconv
ertuppe
rca
sec
har
act
erst
olowe
rca
se:
ENTRY LDA CHAR ; GET CHARACTER
CMP #$C1 ; (A) IS IT ALPHABETIC?
BCC DONE ; NO, DON’T CONVERT
CMP #$E0 ; IS IT ALREADY LOWERCASE?
BCS DONE ; YES, DON’T CONVERT
ORA #$20 ; UPPERCASE TO LOWERCASE
STA CHAR ; PUT CHARACTER BACK

]
3
76 As
sembl
yLi
nes

PHA:
Pus
HAc
cumul
ator
De
scr
ipt
ion:⇢ispushe
sthecont
ent
softheAccumulat
orontot
hes
tac
k.⇢e
Ac
cumul
atora
ndStat
usRegi
st
erar
eunaLe
cted.
(Seea
lsoPLA.
)

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly PHA 48

Uses:⇢isisoneofthemos tcommonwa ysoftempor ar


ilys
tori
ngabyteortwo.
Iti
scombi nedwithPLAt oretr
iev
etheda ta.General
lyspeaki
ng,e
achPHAmus t
bema t
chedbyaPLAl aterintherouti
ne.OtherwisetheFnalRTSofyourrout
ine
wil
ldel
iveryou,notbacktothec a
ll
ingBAS ICpr ogr
am orimme di
atemode,but
rat
heroLintotheweeds,asthesayi
ngg oes.
He r
eisane xa
mpl eofasimplestor
e /
ret
riev
eope ra
tion:
ENTRY LDA #$80 ; TEST VALUE
PHA ; STORE IT
LDA #$FF ; DESTROY ACC.
PLA ; RETRIEVE VALUE
STA MEM ; SAVE IT TO LOOK AT
DONE RTS

Anot he
rmor eobs
cur
eus eofPHAistosetupa nar
tiFci
alJMPbyexe
cut
ing
anRTS forwhichaJSRwa sneverdone.Provi
dingthattwoPHAsha vebeen
donepri
ortotheRTS,t
hepseudo-
jumpwi l
lbeexecut
ed.
S eechapt
er1
5formore
det
ail
saboutthi
s.

PHP:
Pus
HPr
oce
ssors
tat
us
De
scr
ipti
on:⇢ispushestheSt
atusReg
ist
eront
othest
ackforla
terret
ri
eva
l.
⇢eSt
atusReg
ist
eri
ts
elfi
suncha
ng e
dandnoneoft
here
gis
ter
sareaLec
ted.

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly PHP 08

Uses:PHPisdonetopres
ervetheSt
atusRegi
ste
rforl
at
ert es
tingforaspeci
Fc
condit
ion.⇢isishandyi
fy oudon’
twanttotes
taSagri
g htthen,butt
henext
i
nstructi
onwouldruinwhatyouwanttote
stfor
.Byputt
ingtheS ta
tusReg
ist
er
Appe
ndi
xB:
Ass
embl
yComma
nds 3
77

onthest
ackandthenlat
err
etr
iev
ingi
t,y
ouc
ant
estt
hing
sli
ket
hes
ignSa
gor
ca
rrywhenit
’smostconv
eni
ent
.
ENTRY CLC ; CLR CARRY
PHP ; SAVE REG
SEC ; SET CARRY
PLP ; RETRIEVE REG
BCC DONE ; (ALWAYS!)
BRK ; (NEVER)
DONE RTS

ENTRY LDA #$00 ; SET Z-FLAG


PHP ; SAVE REG
LDA #$FF ; DESTROY
PLP ; RETRIEVE
BEQ DONE ; (ALWAYS!)
BRK ; (NEVER)
DONE RTS

Aswi t
ht hePHA i
nst
ructi
on,PHP a
lwaysshoul
dbeac
compani
edbya nequal
numberofPLPinst
ruc
tionstokeeptheAppleha
ppy
.Remember
:It
’snotni
cet
o
fool
thest
ack!

PHX:
Pus
HX-
Reg
ist
er[
65C02]
Desc
ript
ion:⇢ispushest
hecont
ent
softheX-Re
gis
terontot
hesta
ck(
65C0
2
onl
y).
⇢eX- Re
gis
tera
ndStat
usRe
gis
tera
reuna
Lect
ed.(Se
eals
oPLX.
)

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addr
ess
ingModes CommonSy
nta
x He
xCodi
ng
I
mpli
edonly[
65C0
2] PHX DA

Uses:PHX i
sus ef
ulfortemporari
lyst
ori
ngtheX-Regi
st
erwi
thouthavingto
tra
nsfe
rittotheAc c
umul a
torFrst
.Iti
scombine
dwit
hPLXtor
e t
ri
evetheda t
a.
Justli
kePHA/PLA,eachPHXshouldnormal
lybematc
hedbyaPLX( oranother
pulli
nst
ruct
ion)lat
erintherout
ine.

Example:Wit
ht he6
5 C0
2,youca
nea
sil
ysa
vea
ndr
est
orea
lloft
her
egi
st
ers
us
ingcodesi
mila
rtothef
oll
owi
ng:
ENTRY PHX ; SAVE X
PHY ; SAVE Y
PHA ; SAVE A
WORK NOP ; YOUR PROGRAM HERE
DONE PLA ; GET A
PLY ; GET Y
PLX ; GET X

]
3
78 As
sembl
yLi
nes

PHY:
Pus
HY-
Reg
ist
er[
65C02]
Desc
ript
ion:⇢ispushest
hecont
ent
softheY-Re
gis
terontot
hesta
ck(
65C0
2
onl
y).
⇢eY- Re
gis
tera
ndStat
usRe
gis
tera
reuna
Lect
ed.(Se
eals
oPLY.
)

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addr
ess
ingModes CommonSy
nta
x He
xCodi
ng
I
mpli
edonly[
65C0
2] PHY 5A

Uses:J
ustl
ikePHX,PHYisusef
ulfortempor
ari
lyst
ori
ngtheY-Reg
ist
erwit
hout
havi
ngt ot
ransferittot heAccumulat
orFrst.I
tiscombinedwithPLY (
or
anot
herpul
linst
ruct
ion)toret
ri
evethedat
a.Se
ePHXforausa
geexampl
e.

PLA:
Pul
LAc
cumul
ator
Descri
pti
on:⇢isi
sthec onve
rseoft
hePHA i
nst
ruct
ion.PLAre
tri
eve
soneby t
e
fr
om thest
acka
ndpla
cesitintheAcc
umulat
or.⇢isaccordi
ngl
ycondit
ionst
he
N-Sag(si
gnSag)a
ndZ- Sag(zeroSa
g),j
ustast
houg hanLDAinst
ruct
ionhad
beendone.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly PLA 68

Use:⇢i
s sisc
ombinedwit
hPHAtore
tri
eveda t
af r
om thes ta
ck.SeePHAfora
n
ex
ampleofthi
s.
Additi
onal
ly
,PLAcanbeus
edtocancelac urrentRTS,muc hli
k eaPOPi
n
Appl
eso4BASIC.Tocanc
elt
hemostr
ecentRTS,twoPLAsa rerequi
red:
ENTRY JSR LEVEL1
RTS ; WOULD EXIT HERE NORMALLY
LEVEL1 LDA #$00 ; ARBITRARY OPERATION
PLA
PLA ; 'POP' RTS
EXIT RTS ; WILL EXIT ENTIRELY HERE
Appe
ndi
xB:
Ass
embl
yComma
nds 3
79

PLP:
Pul
LPr
oce
ssorSt
atus
Descri
ption:⇢i sisuseda 4e
raPHPt ore
tri
evetheStat
usRegi
ste
rdat
afrom t
he
s
tack.⇢eby t
eisputi nt heSta
tusRegis
terandalloftheSag
sa r
econdi
ti
oned
cor
respondingtot hest
atusofeac
hbitinthebyt
eplacedthe
re.⇢eAccumulat
or
andotherregi
st
er sareunaLect
ed.(S
eePHP.)

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l l l l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly PLP 28

Uses
:PLPisusedt
or et
riev
et heSta
tusRegis
tera raPHPha
fte sst
oredtheSagson
t
hestac
k.SeePHPforane x
ampl e.
AswiththePHA/PLAset,PLPsal
waysshouldbema t
chedwithac or
res
pond-
i
ngnumbe rofPHPi ns
truct
ionsinaone -
to-oner e
lat
ionshi
p.Fai
luretoobser
ve
t
hisre
quir
eme ntc
a nres
ultinsomev eys
r tra
ng eresul
ts!

PLX:
Pul
LX-
Reg
ist
er[
65C02]
Des
cri
pti
on:PLXret
rie
vesonebyt
efr
om t
hest
ackandpl
ace
siti
ntheX-Re
gis
ter
(
65C02onl
y).⇢iscondit
ionst
heN-Sag(
si
gnSag)a Sa
ndZ- g(z
eroSag
),j
ustas
t
houghaLDXins
truc
ti
onha dbee
ndone.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingModes CommonSy
nta
x He
xCodi
ng
I
mpli
edonly[
65C0
2] PLX FA

Use:⇢i
s siscombi
nedwi
thPHXt
ore
tri
eveda
taf
rom t
hes
tac
k.S
eePHXf
ora
us
ageex
ample
.

]
3
80 As
sembl
yLi
nes

PLY:
Pul
LY-
Reg
ist
er[
65C02]
Des
cri
pti
on:PLYret
rie
vesonebyt
efr
om t
hest
ackandpla
cesiti
ntheY-Re
gist
er
(
65C02onl
y).⇢iscondit
ionst
heN-Sag(
si
gnSag)andZ-Sag(z
eroSag
),j
ustas
t
houghaLDYins
truc
ti
onha dbee
ndone.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingModes CommonSy
nta
x He
xCodi
ng
I
mpli
edonly[
65C0
2] PLY 7A

Use:⇢i
s si
scombi
nedwi
thPHY t
ore
tri
eveda
taf
rom t
hes
tac
k.S
eePHY f
or
de
tai
ls.

ROL:
ROt
ateLe
5
Descr
ipt
ion:⇢i sinst
ruct
ionmov ese
ac hbi
toftheAccumulatororthespe
ciFed
memorylocat
iononepos i
tiont
ot hele4.⇢ec arr
ybitispushedintoposi
ti
on0
andisre
place
dbybi t7(thehigh-or
de rbi
t.⇢eN-
) Sag(si
g nSa g
)andZ- Sag
(
z e
roSag)ar
ec ondit
ione
dde pe
ndingont here
sultoft
heshi4.(Seeal
soASL,LSR,
andROR.
)

ROLLRot
ateOneBi
tLe
5 RORLRot
ateOneBi
tRi
ght

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l l l

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Accumulat
or ROL 2A
Zer
oPa g
e ROL $12 26 12
Zer
oPa g
e ,
X ROL $12,X 36 12
Absol
ute ROL $1234 2E 34 12
Absol
ute
,X ROL $1234,X 3E 34 12
Appe
ndi
xB:
Ass
embl
yComma
nds 3
81

Uses:
ROLc a
nbeusedasoneoft hevari
ousme t
hodst
otes
tforasethig
hbit.
⇢e
di
sadvant
agetote
sti
ngforthehighbiti
nt hi
swayist
hatt
hec ont
entsmustt
hen
beres
tore
dwi t
hacorre
spondingRORinstr
ucti
on.
ROLisusedmoreo4eni ncombinati
onwithASLinmult
iplyanddivi
derou-
ti
nes.

ROR:
ROt
ateRi
ght
Descript
ion:⇢isins
truct
ionmoveseachbitoft
heAccumul at
ororthespeci
Fed
me morylocat
iononeposit
iontot
her i
ght.⇢ec a
rrybitispushe
dintoposit
ion
7(thehigh-or
derbit
),andisre
pla
cedbybi t0.⇢eN- Sag(si
gnSa g
)a ndZ-Sag
(ze
roSa g)areals
oconditi
oneddependingontheresultoftheshi4.(Se
ea l
so
ASL,LSR,andROL.
)

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l l l

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Accumulat
or ROR 6A
Zer
oPa g
e ROR $12 66 12
Zer
oPa g
e ,
X ROR $12,X 76 12
Absol
ute ROR $1234 6E 34 12
Absol
ute
,X ROR $1234,X 7E 34 12

Uses:
RORprovi
desanal
ter
natewayoft
est
ingfortheodd/e
venna t
ureofanum-
be.⇢ec
r arr
yistes
teda4erthes
hi4todete
ctwhe t
herthenumbe rwasoddor
ev
en.
RORFndsgreat
erus
ewhe nc
ombinedwiththeshi4opera
tionsincre
ati
ng
mult
iplya
nddivi
derout
ines.

RTI:
ReTur
nfr
om I
nte
rrupt
Desc
ript
ion:⇢isres
tore
sbotht heProgr
a m Counte
randtheSt
atusRegi
st
erin
pr
eparat
iontor
esumingtheroutinebei
nge x
e c
utedatt
hetimeofthei
nter
rupt
.
Al
lSagsoftheS
tat
usRegist
erarerese
ttothei
rorigi
nalv
alue
s.

]
3
82 As
sembl
yLi
nes

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l l l l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly RTI 40

Us es:RTIi
susedi nmuc hthesamewa ytha tanRTSwoul dbeus edinr e
turni
ng
from aJSR.A4e ra ninte
rrupthasbee
nha ndl edandtheba ckgr
oundope r
ati
on
pe r
formed,thereturnisdonev i
atheRTIc omma nd.Us uall
ytheus erwil
lwa nt
tor est
oret
heAc cumul at
or,theX-Regi
st
er ,andtheY- Re g
ist
erpriortoretur
n-
ing.
RTI al
soise quival
enttoaPLPRTSi nt hattheStatusRegis
teri sre
stor
e d
from thest
ackandar eturnisdonetot
hea ddr e
ssonthest a
ck.

RTS:
ReTur
nfr
om Subr
out
ine
Des
cri
pton:⇢i
i sres
torest
hePr
ogram Counte
rtot hea ddr
essst
oredonthe
s
tac
k,us
uall
ytheaddr
essoft
hene
xtins
truc
ti
ona 4ertheJSRthatc
all
edther
ou-
t
ine
.Anal
ogoustoaRETURNt
oaGOSUBi
nBAS IC.(S
e eal
soJSR.)

Fl
ags&Re
gis
ter
sA?e
cte
d:None

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly RTS 60

Uses
:RTSi s
,sur
pri
sing
lyenough,mos to4enus e
dt oreturnfrom subrouti
nes.
Onoc c
asionitcanbeus edtos i
mul at
e aJMP inst
ruct
ion byus i
ngt woPHA
i
nstr
uct
ionstoputafal
sere
turnaddressonthesta
cka ndthenex e
cutingtheRTS.
Se
ethesecti
ononPHA,andals
ocha pt
er15formor edeta
il
s .
AnRTSc anbePOP’
donelevelbytheexecut
ionoftwoPLAi nstructi
ons.

SBC:
SuBt
rac
twi
thCa
rry
De scri
pt i
on:S ubt
ract
st hec ont
ent
soft heme moryl oca
tionort hes peciFed
va l
uefrom theAc cumulator.⇢eoppos i
teofthecarryisals
os ubtra
cted,andi n
thisinstancethecarr
yisc al
le
dabor r
ow. ⇢eN- Sag( s
ignSa g),V-Sag(overSow
Sa g)
,Z- Sag(zeroSag),
andC- Sa
g(carySa
r g)areallcondit
ionedbyt hisope r
a-
tion,andt heyo4ena r
eus edtodete
ctthenatureofther e
sultofthesubtract
ion.
⇢er esultofthesubtr
actionisputbackintheAc cumul a
tor.⇢eme mor yloca-
Appe
ndi
xB:
Ass
embl
yComma
nds 3
83

t
ion,i
fspe
ciFe
d,i
sunc
hang
ed.
SBCwor
ksf
orbot
hthebi
nar
yandt
heBCDa
rit
h-
meti
cmodes.

I
mportant:AnSECshoul
dal
waysbedonebef
oret
heFrs
tSBCopera
tion.⇢isi
s
e
quiva
lenttoc
lea
ringt
hebor
rowandisana
log
oust
otheCLCdonebef
or ea
nADC
i
nst
ruct
ion.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l l l

Addres
singModes CommonSy
nta
x He
xCodi
ng
Immedia
te SBC #$12 E9 12
ZeroPag
e SBC $12 E5 12
ZeroPag
e ,
X SBC $12,X F5 12
Absol
ute SBC $1234 ED 34 12
Absol
ute
,X SBC $1234,X FD 34 12
Absol
ute
,Y SBC $1234,Y F9 34 12
(I
ndir
ect
,X) SBC ($12,X) E1 12
(I
ndir
ect
),Y SBC ($12),Y F1 12
(I
ndir
ect
)[65C0
2] SBC ($12) F2 12

Uses
:SBCi
suse
dmosto4enforsubt
ract
ingacons
tantormemoryv
aluef
rom
ei
the
raone
-by
teme
moryloca
tionoratwo-by
tememoryloc
ati
on.

One
-by
tes
ubt
rac
tion:
ENTRY SEC
LDA MEM
SBC #$80
STA MEM ; MEM = MEM - #$80
DONE RTS

Two-
byt
esubt
rac
tion:
ENTRY SEC
LDA MEM
SBC #$80
STA MEM
LDA MEM+1
SBC #$00
STA MEM+1 ; MEM,MEM+1 = MEM,MEM+1 - #$80
DONE RTS

]
3
84 As
sembl
yLi
nes

SEC:
SEtCa
rry
De
scr
ipt
ion:
⇢iss
etst
hec
arr
ySa
goft
heS
tat
usRe
gis
ter
.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly SEC 38

Uses:SECusuall
yisusedj us
tpriortoaSBCope r
aton.⇢ec
i ar
ryi
soccasi
onal
ly
usedt hought oindicat
ee r
ror( orother)condit
ions,asi sdone byRWTS
(Read/Writ
eTr a
ckS e
ctor)inDOS .Inthes
eins
tancesSECisusedt
osett
hec ar
ry
toindicat
eane rr
or.⇢i swouldbede t
ect
edsome t
imelateri
nprograme x
ecu-
ti
on, 4e
a rareturnfr
om RWTSha salre
adybeenma de.
SECisal
sosometimesusedtoforceabranc
h.Fore xa
mpl e
:
SEC
BCS ADDRESS ; (ALWAYS)

SED:
SEtDe
cima
lmode
Desc
ript
ion:SED s
etst
he6502totheBina
ryCode
dDe
cima
l(BCD)mode
,in
pr
eparat
ionf
oranADCorSBCope
rat
ion.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly SED F8

Uses:BCDmathisusedwhenagre
aterdeg
reeofprec
isi
onisre
qui
red.I
nthi
s
modee ac
hfourbi
tsofabyt
ere
pre
sentonedig
itofaba
se-t
ennumbe
r.Herei
sa
br
iefexampl
eofaBCDaddi
ti
onoper
ati
on:
ENTRY SED ; SET DEC MODE
CLC
LDA #$25 ; %00101001 = DECIMAL 25
ADC #$18 ; %00011000 = DECIMAL 18
Appe
ndi
xB:
Ass
embl
yComma
nds 3
85

STA MEM ; RSLT = %01000011 = DECIMAL 43


CLD ; CLR DEC MODE
DONE RTS

SEI:
SEtI
nte
rruptdi
sabl
e
Des
cript
ion:
SEIisus
edtodisa
blet
hei
nterr
uptr
espons
etoanIRQ(
amaska
ble
i
nte
rrupt
).⇢isdoesnotdi
sabl
ethere
sponset
oa nNMI(Non-
Mask
abl
eInt
er-
r
upt=RESET).

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly SEI 78

Uses:SEIi sautomatic
a l
lysetwhenevera ni nter
ruptoc curssothatnof urt
her
int
erruptscandi s
tur
bt hesyst
em whileitisg oingthroughthev e
c t
orpa thf
rom
$FFFE,$FFFF to$3FE,$3FF.⇢eus e
ri sexpe ctedtous eCLItor e-
ena bl
einte
r-
ruptsupone ntrytohisorhe rowni nterruptr out
ine.DOSt ypical
lydoesa
SEI/CLIo per
ationupone nt
rancetoande xitfrom RWTSs othati
nterruptsdonot
int
erfer
ewi t
ht hehighl
ytiming-de
pende ntdiskr ead/
wr i
terouti
nes.

STA:
STor
eAc
cumul
ator
Descri
pti
on:Stor
estheconte
ntsoftheAcc
umulat
orint
hespec
iFedmemory
l
ocaton.⇢ec
i onte
ntsoftheAccumula
tora
renotcha
nge
d,norareanyoft
he
St
atusRegi
st
erSags.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

]
3
86 As
sembl
yLi
nes

Addres
singModes CommonSy
nta
x He
xCodi
ng
ZeroPag
e STA $12 85 12
ZeroPag
e ,
X STA $12,X 95 12
Absol
ute STA $1234 8D 34 12
Absol
ute
,X STA $1234,X 9D 34 12
Absol
ute
,Y STA $1234,Y 99 34 12
(I
ndir
ect
,X) STA ($12,X) 81 12
(I
ndir
ect
),Y STA ($12),Y 91 12
(I
ndir
ect
)[65C0
2] STA ($12) 92 12

Us
e s
:STAisanot
herfr
eque
ntl
yusedinst
ruc
ti
on,bei
nge
mpl
oyedatthee
ndof
manyoper
ati
onstoputt
heFnalr
esul
tint
oaspec
iFe
dmemor
yloc
ati
on.

I
ngener
al,t
heLDA/
STAc
ombi
nat
ioni
sus
edt
otr
ans
ferby
tesf
rom onel
oca
tion
t
oanot
her.

STX:
STor
etheX-
Reg
ist
er
Desc
ripti
on:STXs
tor
esthecont
ent
softheX-
Regi
st
e ri
nthespe
ciFedmemor
y
l
ocat
ion.⇢eX- Re
gis
teri
sunchang
edandnoneoftheSta
tusRe
gist
erSa
gsar
e
aLe
cted.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Zer
oPa g
e STX $12 86 12
Zer
oPa g
e ,
Y STX $12,Y 96 12
Absol
ute STX $1234 8E 34 12

Uses:Iti
soc cas
ional
lyusefultobea bletos
torethecont
entsoftheX-Regis
ter
forl
aterref
erence.Anotherfai
rlycommonus eofSTXi si
nAppl e
so4’sdet
ermi-
nati
onofs t
ringlengt
hs.A4e rgetti
ngda t
afr
om theinputbuLer($200to$2FF)
thelengt
hoft heinputs t
ri
ngi sheldintheX- Re
g i
st
erandissavedtoas t
ring
desc
ript
orforlat
eruse.Seechapter13forali
st
ingofasimplei
nputrouti
ne.
Appe
ndi
xB:
Ass
embl
yComma
nds 3
87

STY:
STor
etheY-
Reg
ist
er
Desc
ripti
on:STYs
tor
esthecont
ent
softheY-
Regi
st
e ri
nthespe
ciFedmemor
y
l
ocat
ion.⇢eY- Re
gis
teri
sunchang
edandnoneoftheSta
tusRe
gist
erSa
gsar
e
aLe
cted.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Zer
oPa g
e STY $12 84 12
Zer
oPa g
e ,
X STY $12,X 94 12
Absol
ute STY $1234 8C 34 12

Uses
:STYisusedtos t
oretheva
lueoftheY-
Regis
ter
,us
ual
lyf
rom wi
thi
nstr
ing
ordat
a-s
canningloops.
Forexample
,herei
sarout
inewhi
chre
tur
nsthepos
it
ion
heFr
oft stcontr
olcharac
teri
nablockofdat
a:
ENTRY LDY #$00 ; ZERO COUNTER
LOOP LDA DATA,Y ; GET CHARACTER
BEQ NOTF ; CHR = 0 = END
CMP #$20 ; 'SPC'
BCS NXT ; CHR > CTRL’S
FOUND STY POS ; SAVE Y-REG
DONE RTS
NXT INY ;Y=Y+1
BNE LOOP ; UNTIL Y = 0 AGAIN
BEQ DONE
NOTF LDY #$FF ; FLAG NOT FOUND
BNE FOUND

STZ:
STor
eZe
roi
nme
mor
y[65C02]
Desc
ripti
on:STZstor
esa0i
nazero-pa
geme
mor
yloc
ati
on(
65C0
2onl
y).
None
oft
heS t
atusReg
ist
erSag
sar
eaLe
cted.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

]
3
88 As
sembl
yLi
nes

Addres
singMode s CommonSy nt
ax He
xCodi
ng
Zer
oPa ge[65C02] STZ $12 64 12
Zer
oPa ge,
X[ 65C0
2] STZ $12,X 74 12
Absol
ute[65C02] STZ $1234 9C 34 12
Absol
ute,
X[ 65C02] STZ $1234,X 9E 34 12

Use
s:STZi
sus
edtos
tor
ea0iname mor
yloc
ati
on.Usi
ngSTZa
voi
dsha
vingt
o
l
oada0int
heAcc
umula
torj
ustt
ose
tamemoryl
ocat
ion.

TAX:
Tra
nsf
erAc
cumul
atort
oX-
Reg
ist
er
Desc
ripti
on:Put
sthec
onte
ntsoft
heAc
cumul
at
ori
ntot
heX-
Reg
ist
er.TAXdoe
s
nota
Le c
ttheAcc
umulat
or.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly TAX AA

Uses
:Mos tsimpl
y,TAXisus
e dfortr
ansf
erri
ngdat
afr
om theAc
cumul
atort
othe
X-
Re g
ist
er.Equall
yimporta
nt ,howeve
r,isit
scombi
nat
ionwit
hTYAtotra
nsf
er
da
tafrom theY-Regi
st
ertotheX- Re
gis
ter
:
ENTRY LDY #$00 ; LOAD Y
TYA ; PUT IN A
TAX ; PUT IN X

TAY:
Tra
nsf
erAc
cumul
atort
oY-
Reg
ist
er
Desc
ripti
on:Put
sthec
onte
ntsoft
heAc
cumul
at
ori
ntot
heY-
Reg
ist
er.
TAYdoe
s
Le
nota c
ttheAcc
umulat
or.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l
Appe
ndi
xB:
Ass
embl
yComma
nds 3
89

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly TAY A8

Uses
:Mos tsimpl
y,TAYisus
edfortr
ansf
erri
ngdat
afr
om theAc
cumul
atort
othe
Y-
Regist
er.Equall
yimporta
nt,howeve
r,isit
scombi
nat
ionwit
hTXAtotra
nsf
er
da
tafrom theX-Regi
st
ertotheY-Re
gis
ter
:
ENTRY LDX #$00 ; LOAD X
TXA ; PUT IN A
TAY ; PUT IN Y

TRB:
Tes
tandRe
setBi
ts[
65C02]
Des
cri
ption:TRB use
stheAc c
umula
torasama sktoc
lea
rbitsinaspe
ciFe
d
memorylocat
ion(65C02only)
.⇢eAc cumul
a t
ori
suncha
nged,butt
heZ-Sag
(
zer
oSa g
)Sa giscondit
ione
dbase
dont hev
alueoft
hos
eme morybit
spr
iort
o
t
heoper
ation.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l

Addres
singModes CommonSy
nta
x He
xCodi
ng
Zer
oPa ge[
65C02
] TRB $12 14 12
Absol
ute[65
C02] TRB $1234 1C 34 12

Uses:TRBislikeacombinati
onofBIT andAND,wi
tht
headdedbonustha
tthe
newv al
ueisstor
edbackinthememorylocat
ion.
Forex
ampl e,t
osetbot
hbits0a
nd7ofame moryl
oca
tion,
wecoul
dusethe
fol
lowingsetofi
nstr
ucti
ons:
LDA #$81 ; %1000 0001 = MASK PATTERN
TSB MEM1 ; SET BITS 0,7 OF MEMORY
BNE PRSET ; ONE OF THESE WAS 'ON' ALREADY
BEQ PRCLR ; NEITHER OF THESE WAS 'ON' ALREADY

⇢i
swoul
dcl
eart
hebi
ts
:
LDA #$81 ; %1000 0001 = MASK PATTERN
TRB MEM2 ; CLR BIT 0,7 OF MEMORY
BNE PRSET ; ONE OF THESE WAS 'ON' ALREADY
BEQ PRCLR ; NEITHER OF THESE WAS 'ON' ALREADY

]
3
90 As
sembl
yLi
nes

TSB:
Tes
tandSe
tBi
ts[
65C02]
Descri
pt i
on:TSBuse
stheAccumul
atora
sama sktosetbi
tsinaspe
ciFedme m-
orylocati
on(65C0
2onl y
).⇢eAc c
umulatori
sunchanged,butt
heZ- Sag(z
ero
Sag)Sa gisc
ondit
ionedbas
edonthev al
ueofthoseme morybi
tspriortothe
operat
ion.

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l

Addres
singModes CommonSy
nta
x He
xCodi
ng
Zer
oPa ge[
65C02
] TSB $12 04 12
Absol
ute[65
C02] TSB $1234 0C 34 12

Use
s:TSBi
sli
keacombi
nati
onofBIT andORA,wit
htheaddedbonustha
tthe
ne
wv al
uei
sst
ore
dbacki
nthememorylocat
ion.S
eeTRBfora
ne x
ample.

TSX:
Tra
nsf
erSt
ackt
oX-
Reg
ist
er
Descr
ipt
ion:⇢isput
sthec
ont
entsoft
heS
tac
kPointeri
ntot
heX-Regi
st
e r
.⇢e
N-Sag(s
ignSag
)a ndZ-Sa
g(zeoSa
r g
)arecondi
ti
oned.⇢eSta
c kPointe
ris
uncha
nged.
Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly TSX BA

Uses:⇢emos tobvi
ousus eofTSXi
sinpr es
ervingtheval
ueoft
hesta
ckatacer
-
tai
npoint.Si
milart
ot heuseofPLAswi thRTS,thiscoul
dbeusedtodupli
cat
e
BAS I
C’sPOPcomma nd–t hatist
os ay
,adi r
ectr e
turntoadiLer
entle
velt
han
theonewhichhadact
ua l
lycal
ledasubrout
ine.Forexample:
ENTRY LDA #$00 ; DUMMY OPERATION
TSX ; SAVE CURRENT RETURN PTR
JSR LEVEL1
RTS ; NORMAL EXIT, BUT IT WILL NEVER BE CALLED
LEVEL1 TXS ; PUT PTR TO 1ST RETURN BACK
DONE RTS ; EXIT TO MAIN CALLING PROGR
Appe
ndi
xB:
Ass
embl
yComma
nds 3
91

Not ethatthisi ss ome whatda ngerousint hatyoumus tbev eryc ertainast o


thea ctualc onte
nt soft hes ta
c k
,a ndint hek nowl e
dg ethatt hedataha snotbe en
chang edbyi nterme dia t
ePHAsa ndPLAsf ori nstance.Re me mbe rt hatt heS tack
Pointe risonl yap oint ert othesta c
ka nddoe snotpr es
e r
v ether e
tur na ddr es
sa s
such, butonl yitspos itioni nthes tack.
Anot herusef orTSXi sinretriev
ingda tafr om thest ackwi t
houtha vingt odo
aPLAi nstructi
on. Al thoug haPLA/ PHA/TAXs eque ncewoul dbet ra
ns pa renttothe
stac
k ,a nda ccompl isht hes amer esult
s,TSXc anbeus edt or et
rievei nforma ti
on
thatiso Jc ialyl
l osta tt ha tpoint.Wha tIa ma lludingtoi sr etr
ievingda tathatis
lowe rinme mor yt ha nt hec urrentS t
ackPoi nt er,andt hatwoul dbeov erwr i
tt
en
bythene xtPHAi nstruc tion.Oneoft hepr imee x a
mpl esoft hisisinus ingaJSR
toak nownRTSi nt heMoni torfornoot herpur posetha ntobea blet oimme di-
atel
yr etrievetheot he rwi setranspa r
entr et
urna ddress.⇢i sisdones ot hatrelo-
catablec odeha sawa yofFndi ngoutwhe r
ei t’
sc urrentlyloc at
ed.S eec hapter1 5
forat hor oughe xplana t
ionoft het echnique.Forqui ckr ef
e r
e nce,
he re’stheba sic
routine :
ENTRY JSR RETURN ; $FF58
TSX
LDA STACK,X ; $100,X
STA PTR+1
DEX
LDA STACK,X ; $100,X+1
STA PTR ; PTR,PTR+1 = ENTRY+2
DONE RTS

Cauti
on:MostS t
epa ndTr a
ceut i
li
ti
eswi l
lnotpr operl
yt r
acec odelikethis
beca
useofthesome whatil
le
galuseoft hestac
k .St
rict
lyspeaki
ng ,goodpr o-
gra
mmi ngpr
inci
plesdic
tat
ethatoncedataisoWcial
lyoLt hest
ack,itiscounted
asbei
ngeLec
tiv
elylost
.⇢isisespeci
al
lytrueinthecaseofinterr
upt s
,whe r
ea n
int
err
uptint
hemi ddleoft
hedummyJSR,RTSa ndr e
tri
evalprocesscouldpro-
duceacompl
etel
yinvali
dres
ultinPTR,PTR+1.Cavea
te mptor!

TXA:
Tra
nsf
erXt
oAc
cumul
ator
Desc
ript
ion:⇢isputsthecont
ent
softheX-Reg
ist
eri
ntot
heAccumul
ator
,and
t
huscondit
ionstheSta
tusRegi
ste
rjus
ta si
fanLDAhadbeene
xecut
ed.⇢eX-
Reg
ist
eri
suna Le
c t
edbytheope
rati
on.
(Seeal
soTAX.
)

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

]
3
92 As
sembl
yLi
nes

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly TXA 8A

Us es
:TXAp rovi
desawa yofr et
rie
vingthevalueintheX-Re gi
ste
rforappropri
ate
processi
ngbyt heprogram.I nthecaseofstr
ing-
re l
at
edr outi
nes,t
hisiso4enthe
l
e ngt
hoft hestri
ngjustenteredorscanned.⇢eAc cumul at
orcantheng oabout
thethingsi tdoessowe lli
nt ermsofputti
ngt hev al
ueintothemos tusef
ulpart
ofme mor y .Not i
cethattherea r
emor ea ddres
singmode savai
la
blet otheSTA
comma nd, nottome nti
ont heoveral
lpowe rsgr
a nt
edtheAc cumulatorinter
ms
oflogi
c alope r
ators
.
Asdi scussedunde rTAY,TXAc anbec ombine dwit
hTAYt oform aTXY-li
ke
(tra
nsferXt oY)functi
onlikeso:
ENTRY LDX MEM ; GET DATA
TXA ; PUT IN A
TAY ; MOVE TO Y

TXS:
Tra
nsf
erXt
oSt
ack
Desc
ript
ion:⇢i sput
sthecont
entsoftheX-Re
gist
eri
ntotheSt
ackPoint
er.
NoneoftheSt
atusReg
ist
erSa
gsar
ea Le
cted,
nori
stheX-
Regi
ste
rit
sel
fcha
nged.

Fl
ags&Re
gis
ter
sA?e
cte
d:None
Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly TXS 9A

Uses:TXSisusedtoputda t
adi r
ect
lyintotheStackPointer.Becauset
herei
sno
TAS(Ac c
umul a
tortoS ta
ck)ore venTYS( Y-Regist
ertoS t
a c
k )
,thisi
stheonly
waytog etaspeci
FcbyteintotheStackPointer
. ⇢isus ual
lyisusedinconj
unc-
ti
onwi t
hTSXt orest
oreprevi
ouslysaveddata.I
nt hecaseoftheAppl e
so4st
ack-
Fxprogram,itisuse
dt oavoidproble
mst hatotherwis
ewoul doc curi
faRESUME
werenotuseda4erane rr
orha doccurr
edwi t
hinaFOR- NEXTlooporaGOSUB:
ENTRY PLA ; GET LOW BYTE OF CURRENT RETURN ADDR.
TAY ; SAVE INTO Y
PLA ; GET HIGH BYTE OF RETURN ADDR.
LDX ERRSTK ; $DF = S PTR BEFORE ERROR
TXS ; PUT BEFORE-ERR PTR BACK
PHA ; PUT HIGH BYTE BACK
TYA ; GET LOW BYTE IN ACC.
PHA ; PUT LOW BYTE BACK.
DONE RTS ; RETURN TO APPLESOFT WITH STACK FIXED

S
eea
lsoTSXf
orot
hera
ppl
ic
ati
onsofTXS.
Appe
ndi
xB:
Ass
embl
yComma
nds 3
93

TYA:
Tra
nsf
erYt
oAc
cumul
ator
Desc
ript
ion:⇢isputsthecont
entsoft
heY-Reg
ist
eri
ntot
heAccumul
ator
,and
t
huscondit
ionstheSta
tusRegi
ste
rjustasi
fanLDAhadbeene
xecut
ed.⇢eY-
Reg
ist
eri
suna Le
c t
edbytheope
rati
on.(S
eeal
soTAY.
)

Fl
ags&Re
gis
ter
sA?e
cte
d:
N V – B D I Z C Ac
c X Y Me
m

l l l

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly TYA 98

Uses:TYAp rovi
desawa yofretri
evi
ngt heva
lueintheY-Regis
terforappropr
iat
e
proce
ssingbyt heprog ram.⇢i scome sinhandyinscanningada tabloc
kwhe n
inf
orma ti
onr egar
dingc ert
ainlocat
ionsistobeprocess
ed.Asme nti
onedunder
TXA,t
heAc cumulatorha sfarg r
eat
erSe xi
bil
it
ythantheY- Regis
terintermsof
addre
s s
ingmode sandl ogica
l opera
torsavai
la
ble.
TYAa lsoiscombi nedwithTAXt oformtheequival
entofaTYX( Trans
ferYto
X).⇢eope rat
ionha sthefor
m of :
ENTRY LDY MEM ; GET DATA
TYA ; PUT IN A
TAX ; MOVE TO X

]
Ap
pendi
xC:
650
2Ins
truc
ti
onS
et
Por
ti
onsofAppendi
cesC,D,andEarer
epr
int
edf
rom t
heAp
pleI
IRe
fer
enc
e
Manua,
lcour
tes
yAppleComput
er,
Inc
.

6502Mi
cropr
oce
ssorI
nst
ruc
tions
ADC Addme
mor
ytoAc
cumul
at
orwi
th LDX Loa
dX-Regi
ste
rwithmemory
carr
y LDY Loa
dY-Regi
ste
rwithmemory
AND ANDme morywi thAc cumula t
or LSR Shi
4ri
ghtonebit(
memoryor
ASL Shi4le4onebi t( me mor yorAc cu- Ac c
umul at
or)
mula t
or) NOP Noope ra ti
on
BCC Branchonc arryc lear ORA ORAc cumul atorwi thme mor y
BCS Branchonc arrys et PHA PushAc cumul at
oront os t
ac k
BEQ Branchonr esult=z e
r o PHP Pushpr oc e
ssorsta t
usont os t
ack
BIT Testbitsinme mor ywi thAc cumu- PHX PushX- Re gis
teront ostack
l
a t
or PHY PushY- Re gis
teront ostack
BMI Branchonr esult=mi nus PLA PullAcc umul at
orf rom stack
BNE Branchonr esult=notz ero PLP Pullproc es
sors t
atusf r
om s tac
k
BPL Branchonr esult=pl us PLX PullX-Re gist
erfrom s t
a ck
BRA Brancha lwa y
s1 PLY PullY-Re gist
erfrom s t
a ck
BRK Forcebr eak ROL Rotatele4onebi t(me mor yor
BVC Branchonov erSowc lear Ac c
umul at
or)
BVS Branchonov erSows e t ROR Rotaterightonebi t(me mor yor
CLC Clearcarr ySag Ac c
umul at
or)
CLD Clearde cimalmode RTI Re t
urnf rom inte
r rupt
CLI Clearinte r
ruptdi sablebit RTS Re t
urnf rom subrout i
ne
CLV Clearov erSowSa g SBC Subtractme mor yf rom Ac cumula-
CMP Compa reme mor yandAc cumul a- torwithbor row
tor SEC SetcarrySa g
CPX Compa reme mor yandX- Regis
ter SED Setdecima lmode
CPY Compa reme mor yandY- Regis
ter SEI Setinterruptdisablestatus
DEC Decreme ntme mor ybyone STA StoreAc cumul at
ori nme mor y
DEX Decreme ntX-Re gisterbyone STX StoreX- Regist
eri nme mor y
DEY Decreme ntY-Re giste
rbyone STY StoreY-Re gist
erinme mor y
EOR Exclusi
v eORAc cumul atorwith STZ Storezeroi nme mor y
me mor y TAX TransferAc cumul a t
ortoX
INC Increme ntme mor ybyone TAY TransferAc cumul a t
ortoY
INX Increme ntX-Re g i
sterbyone TRB Te s
tandr e
setbits
INY Increme ntY-Re g i
sterbyone TSB Te s
tands e
tbits
JMP Jumpt one wloca ti
on TSX TransferS t
ackPoi nt e
rtoX
JSR Jumpt one wloca ti
ons avingreturn TXA TransferXt oAc c umul at
or
addressonS t
ack TXS TransferXt oS tackPoi nter
LDA LoadAc cumul atorwi thme mor y TYA TransferYt oAc c umul at
or

1[
CT]Opc
ode
sing
raya
ref
ort
he6
5C0
2.
Appe
ndi
xC:
650
2Ins
truc
ti
onS
et 3
95

Us
ageCha
rtof6502I
nst
ruc
tions

]
3
96 As
sembl
yLi
nes

Pr
ogr
ammi
ngMode
l

Pr
oce
ssorSt
atusRe
gis
ter
Appe
ndi
xC:
650
2Ins
truc
ti
onS
et 3
97

Not
ati
on
⇢ef
oll
owi
ngnot
ati
ona
ppl
ie
stot
he6
502I
nst
ruc
ti
onCode
sta
ble
:

A Accumul at
or ↑ Trans
ferfrom St
ack
X,Y IndexReg i
ste
r ↓ Trans
fertoStack
M Me mory → Trans
ferto
C Carry ← Trans
ferto
C Borrow PC Progr
am Count e
r
P Proces
sorS ta
tusRe
gis
ter PCH Pr ogr
am Count e
rHi gh
S Stac
kPoi nter PCL Pr ogr
am Count e
rLow
∧ Logica
lAND #$FF I mmediateAddressi
ngMode
∨ Logica
linclusi
veOR $FF Two-byte(zer
opa g
e )ope
rand
v Logica
lex c
lusi
veOR $FFff Four-
byte(absol
ute)oper
and

Fi
gur
eC-
1:ASL(
shi
5onebi
tle
5)a
ndLSR(
shi
5onebi
tri
ght
)

Fi
gur
eC-
2:ROLLRota
teonebi
tle
5 Fi
gureC-
3:RORLRot
ateonebi
t
(
memoryorAc
cumulat
or) ri
ght(me
moryorAc
cumul a
tor
)

]
3
98 As
sembl
yLi
nes

6502I
nst
ruc
tionCode
s
⇢eTi meisgi
veninclockcycl
es(1msat1MHz ).Forti
meswit
ha“ +”,add1ifapage
bounda
ryi
scross
ed.Forbranc
hinstr
uct
ionswit
ha“ *”,
add1ifthebranchist
aken,a
nd
add1moreift
hebranchcross
esapageboundar
y.Fortimeswi
tha“ d”,add1ifindec
i-
malmodeonthe65C02(butnotonthe6502)
.

Na
me Addr
ess
ing Ass
embl
y Op- Pstat
us
De
scr
ipt
ion Ope
rat
ion Mode La
nguag
e c
ode By
te me NZCIDV
s Ti
ADC A+M+Cf Immediat
e ADC #$FF 69 2 2d NZC---
AddAcc
umula
torto A,C ZeroPag
e ADC $FF 65 2 3d
memor
ywit
hcarr
y ZeroPag
e,X ADC $FF,X 75 2 4d
Absol
ute ADC $FFff 6D 3 4d
Absol
ute
,X ADC $FFff,X 7D 3 4d+
Absol
ute
,Y ADC $FFff,Y 79 3 4d+
(I
ndir
ect
,X) ADC ($FF,X) 61 2 6d
(I
ndir
ect
),Y ADC ($FF),Y 71 2 5d+

AND A∧M fA Immediat


e AND #$FF 29 2 2 NZ----
ANDAccumul
at
orwi
th ZeroPag
e AND $FF 25 2 3
memor
y ZeroPag
e,X AND $FF,X 35 2 4
Absol
ute AND $FFff 2D 3 4
Absol
ute
,X AND $FFff,X 3D 3 4+
Absol
ute
,Y AND $FFff,Y 39 3 4+
(I
ndir
ect
,X) AND ($FF,X) 21 2 6
(I
ndir
ect
),Y AND ($FF),Y 31 2 5+

ASL s
eeFi
gC-
1 Acc
umulat
or ASL 0A 1 2 NZC---
S 4l
hi e4onebi
t(me
mor
yor Ze
roPage ASL $FF 06 2 5
Accumul
ato
r) Ze
roPage,
X ASL $FF,X 16 2 6
Abs
olut
e ASL $FFff 0E 3 6
Abs
olut
e,
X ASL $FFff,X 1E 3 72

BCC Br
anc
honc
arr
ycl
ear Br
anc
hC=0 Re
lat
ive BCC $FF 90 2 2* ------

BCSBr
anc
honc
arr
yse
t Br
anc
hC=1 Re
lat
ive BCS $FF B0 2 2* ------

BEQ Br
anc
honr
esul
tze
ro Br
anc
hZ=1 Re
lat
ive BEQ $FF F0 2 2* ------

BIT A∧M Ze
roPage BIT $FF 24 2 3 NZ---V
Testwi
thAcc
umul
at
or3wi
th M7fN Abs
olut
e BIT $FFff 2C 3 4
bi
tsinmemory M6fV

BMIBr
anc
honr
esul
tmi
nusBr
anc
hN=1 Re
lat
ive BMI $FF 30 2 2* ------

BNEBr
anc
honr
esul
tnot Br
anc
hZ=0 Re
lat
ive BNE $FF D0 2 2* ------
ze
ro

BPLBr
anc
honr
esul
tpl
us Br
anc
hN=0 Re
lat
ive BPL $FF 10 2 2* ------

BRKFor
cebr
eaki
nte
rr 4 PC+2↓ P↓
upt I
mpl
ie
d BRK 00 1 7 ---I--

2[
CT]Ont
he6
5C0
2,ASLAbs,Xt
ake
s6c
ycl
esi
fapa
gebounda
ryi
snotc
ros
sed.
3Bi
ts6a
nd7a
ret
rans
fer
redt
otheS
tat
usRe
gis
ter
.Ift
her
es tofA∧M i
ul s0,
the
nZ=1
;
ot
her
wis
eZ=0
.
4ABRKc
omma
ndc
annotbema
ske
dbys
ett
ingi
nte
rruptdi
sabl
eI.
[
CT]Ont
he6
502
,BRKdoe
snotc
lea
rthede
cima
lSa
g;ont
he6
5C0
2,i
tdoe
s.
Appe
ndi
xC:
650
2Ins
truc
ti
onS
et 3
99

Na
me Addr
ess
ing Ass
embl
y Op- Pstat
us
De
scr
ipt
ion Ope
rat
ion Mode La
nguag
e c
ode By
te me NZCIDV
s Ti
BVC Br
anc
honov
erSow Br
anc
hV=0 Re
lat
ive BVC $FF 50 2 2* ------
cl
ear
BVSBr
anc
honov
erSows
etBr
anc
hV=1 Re
lat
ive BVS $FF 70 2 2* ------
CLC Cl
earc
arr
ySa
g5 0fC I
mpl
ie
d CLC 18 1 2 --C---
CLD Cl
earde
cima
lmode 0fD I
mpl
ie
d CLD D8 1 2 ----D-
CLICl
eari
nte
rruptdi
sa e 0fI
bl I
mpl
ie
d CLI 58 1 2 ---I--
CLV Cl
earov
erSowSa
g 0fV I
mpl
ie
d CLV B8 1 2 -----V
CMP AnfM Immediat
e CMP #$FF C9 2 2 NZC---
Compa
rememor
yand ZeroPag
e CMP $FF C5 2 3
Acc
umula
tor ZeroPag
e,X CMP $FF,X D5 2 4
Absol
ute CMP $FFff CD 3 4
Absol
ute
,X CMP $FFff,X DD 3 4+
Absol
ute
,Y CMP $FFff,Y D9 3 4+
(I
ndir
ect
,X) CMP ($FF,X) C1 2 6
(I
ndir
ect
),Y CMP ($FF),Y D1 2 5+
CPX XnfM Immediat
e CPX #$FF E0 2 2 NZC---
Compareme
mor
yandX- Zer
oPa g
e CPX $FF E4 2 3
Reg
ist
er Absol
ute CPX $FFff EC 3 4
CPY YnfM Immediat
e CPY #$FF C0 2 2 NZC---
Compareme
mor
yandY- Zer
oPa g
e CPY $FF C4 2 3
Reg
ist
er Absol
ute CPY $FFff CC 3 4
DEC M71fM Ze
roPage DEC $FF C6 2 5 NZ----
Dec
reme
ntme
mor
ybyone Ze
roPage,
X DEC $FF,X D6 2 6
Abs
olut
e DEC $FFff CE 3 6
Abs
olut
e,
X DEC $FFff,X DE 3 7
DEX De
cre
mentXby1 X71fX I
mpl
ie
d DEX CA 1 2 NZ----
DEY De
cre
mentYby1 Y71fY I
mpl
ie
d DEY 88 1 2 NZ----
EOR Av M fA Immediat
e EOR #$FF 49 2 2 NZ----
Exc
lus
iveORAc
cumul
at
or ZeroPag
e EOR $FF 45 2 3
wi
thme mor
y ZeroPag
e,X EOR $FF,X 55 2 4
Absol
ute EOR $FFff 4D 3 4
Absol
ute
,X EOR $FFff,X 5D 3 4+
Absol
ute
,Y EOR $FFff,Y 59 3 4+
(I
ndir
ect
,X) EOR ($FF,X) 41 2 6
(I
ndir
ect
),Y EOR ($FF),Y 51 2 5+
I
NC M+1fM Ze
roPage INC $FF E6 2 5 NZ----
I
ncr
eme
ntme
mor
ybyone Ze
roPage,
X INC $FF,X F6 2 6
Abs
olut
e INC $FFff EE 3 6
Abs
olut
e,
X INC $FFff,X FE 3 7
I
NX I
ncr
eme
ntXby1 X+1fX I
mpl
ie
d INX E8 1 2 NZ----
I
NY I
ncr
eme
ntYby1 Y+1fY I
mpl
ie
d INY C8 1 2 NZ----
J
MP PC+1fPCL Absol
ute JMP $FFff 4C 3 3 ------
J
umpt
one
wloc
ati
on PC+2fPCH (I
ndir
ect
) JMP ($FFff) 6C 3 5/66

5[
CT]CLC,
CLD,
andCLVha
dthewr
ongS
tat
usRe
gis
terSa
gs.
6[
CT]I
ndi
rec
tJMPt
ake
s5c
ycl
esont
he6
502a
nd6c
ycl
esont
he6
5C0
2.

]
4
00 As
sembl
yLi
nes

Na
me Addr
ess
ing Ass
embl
y Op- Pstat
us
De
scr
ipt
ion Ope
rat
ion Mode La
nguag
e c
ode By
te me NZCIDV
s Ti
J
SR PC+2↓ Ab
sol
ute JSR $FFff 20 3 6 ------
J
umpt onewloca
tions
avng PC+1fPCL
i
r
etur
na ddr
ess PC+2fPCH
LDA M fA Immediat
e LDA #$FF A9 2 2 NZ----
Loa
dmemoryi
nto ZeroPag
e LDA $FF A5 2 3
Acc
umul
at
or ZeroPag
e,X LDA $FF,X B5 2 4
Absol
ute LDA $FFff AD 3 4
Absol
ute
,X LDA $FFff,X BD 3 4+
Absol
ute
,Y LDA $FFff,Y B9 3 4+
(I
ndir
ect
,X) LDA ($FF,X) A1 2 6
(I
ndir
ect
),Y LDA ($FF),Y B1 2 5+
LDX M fX Immediat
e LDX #$FF A2 2 2 NZ----
Loadmemoryi
nto Zer
oPa g
e LDX $FF A6 2 3
X-Reg
ist
er Zer
oPa g
e,Y LDX $FF,Y B6 2 4
Absol
ute LDX $FFff AE 3 4
Absol
ute
,Y LDX $FFff,Y BE 3 4+
LDY M fY Immediat
e LDY #$FF A0 2 2 NZ----
Loadmemoryi
nto Zer
oPa g
e LDY $FF A4 2 3
Y-Reg
ist
er Zer
oPa g
e,X LDY $FF,X B4 2 4
Absol
ute LDY $FFff AC 3 4
Absol
ute
,X LDY $FFff,X BC 3 4+
LSR s
eeFi
gC-
1 Acc
umulat
or LSR 4A 1 2 NZC---
S 4r
hi i
ghtonebi
t(me
mor
y Ze
roPage LSR $FF 46 2 5
orAcc
umulat
or) Ze
roPage,
X LSR $FF,X 56 2 6
Abs
olut
e LSR $FFff 4E 3 6
Abs
olut
e,
X LSR $FFff,X 5E 3 77
NOP Noope
rat
ion I
mpl
ie
d NOP EA 1 2 ------
ORA A∨M fA Immediat
e ORA #$FF 09 2 2 NZ----
Logi
cal
ORAc c
umul
at
or ZeroPag
e ORA $FF 05 2 3
wit
hme mor
y ZeroPag
e,X ORA $FF,X 15 2 4
Absol
ute ORA $FFff 0D 3 4
Absol
ute
,X ORA $FFff,X 1D 3 4+
Absol
ute
,Y ORA $FFff,Y 19 3 4+
(I
ndir
ect
,X) ORA ($FF,X) 01 2 6
(I
ndir
ect
),Y ORA ($FF),Y 11 2 5+
PHA Pus
hAc
cumul
at
or A↓ I
mpl
ie
d PHA 48 1 3 ------
ont
ost
ack
PHP Pushpr
oce
ssors
taus P↓
t I
mpl
ie
d PHP 08 1 3 ------
ont
osta
ck
PLA Pul
lAc
cumul
at
orf
rom A↑ I
mpl
ie
d PLA 68 1 4 NZ----
st
ack
PLP Pul
lproc
ess
ors
tat
us P↑ I
mpl
ie
d PLP 28 1 4 f
rom S
tac
k
f
rom st
ack

7[
CT]Ont
he6
5C0
2,LSRAbs,Xt
ake
s6c
ycl
esi
fapa
gebounda
ryi
snotc
ros
sed.
Appe
ndi
xC:
650
2Ins
truc
ti
onS
et 4
01

Na
me Addr
ess
ing Ass
embl
y Op- Pstat
us
De
scr
ipt
ion Ope
rat
ion Mode La
nguag
e c
ode By
te me NZCIDV
s Ti
ROL s
eeFi
gC-
2 Acc
umulat
or ROL 2A 1 2 NZC---
Rot
ateonebi
tle
4(me
mor
y Ze
roPage ROL $FF 26 2 5
orAccumul
at
or) Ze
roPage,
X ROL $FF,X 36 2 6
Abs
olut
e ROL $FFff 2E 3 6
Abs
olut
e,
X ROL $FFff,X 3E 3 78
ROR s
eeFi
gC-
3 Acc
umulat
or ROR 6A 1 2 NZC---
Rot
ateonebi
tri
ght(
memor
y Ze
roPage ROR $FF 66 2 5
orAccumul
at
or) Ze
roPage,
X ROR $FF,X 76 2 6
Abs
olut
e ROR $FFff 6E 3 6
Abs
olut
e,
X ROR $FFff,X 7E 3 78
RTI P↑ PC↑ I
mpl
ie
d RTI 40 1 6 f
rom S
tac
k
Ret
urnf
rom i
nte
rrupt
RTS PC↑ I
mpl
ie
d RTS 60 1 6 ------
Ret
urnf
rom s
ubr
out
ine PC+1fPC
SBC A7M7CfA Immediat
e SBC #$FF E9 2 2d NZC--V
Subtr
actmemor
yfrom ZeroPag
e SBC $FF E5 2 3d
Accumulat
orwi
thborr
ow ZeroPag
e,X SBC $FF,X F5 2 4d
Absol
ute SBC $FFff ED 3 4d
Absol
ute
,X SBC $FFff,X FD 3 4d+
Absol
ute
,Y SBC $FFff,Y F9 3 4d+
(I
ndir
ect
,X) SBC ($FF,X) E1 2 6d
(I
ndir
ect
),Y SBC ($FF),Y F1 2 5d+
SEC S
etc
arySa
r g 1fC I
mpl
ie
d SEC 38 1 2 --C---
SED S
etde
cima
lmode 1fD I
mpl
ie
d SED F8 1 2 ----D-
SEIS
eti
nte
rruptdi
sabl
e 1fI I
mpl
ie
d SEI 78 1 2 ---I--
STA AfM ZeroPag
e STA $FF 85 2 3 ------
St
oreAc
cumul
at
ori
n ZeroPag
e,X STA $FF,X 95 2 4
memory Absol
ute STA $FFff 8D 3 4
Absol
ute
,X STA $FFff,X 9D 3 5
Absol
ute
,Y STA $FFff,Y 99 3 5
(I
ndir
ect
,X) STA ($FF,X) 81 2 6
(I
ndir
ect
),Y STA ($FF),Y 91 2 6
STX XfM Ze
roPage STX $FF 86 2 3 ------
St
oreX-
Reg
ist
eri
nme
mor
y Ze
roPage,
Y STX $FF,Y 96 2 4
Abs
olut
e STX $FFff 8E 3 4
STY YfM Ze
roPage STY $FF 84 2 3 ------
St
oreY-
Reg
ist
eri
nme
mor
y Ze
roPage,
X STY $FF,X 94 2 4
Abs
olut
e STY $FFff 8C 3 4
TAX Tr
ans
ferAt
oX AfX I
mpl
ie
d TAX AA 1 2 NZ----
TAY Tr
ans
ferAt
oY AfY I
mpl
ie
d TAY A8 1 2 NZ----
TSX Tr
ans
fers
tac
ktoX SfX I
mpl
ie
d TSX BA 1 2 NZ----
TXA Tr
ans
ferXt
oA XfA I
mpl
ie
d TXA 8A 1 2 NZ----
TXSTr
ans
ferXt
ost
ack XfS I
mpl
ie
d TXS 9A 1 2 ------
TYA Tr
ans
ferYt
oA YfA I
mpl
ie
d TYA 98 1 2 NZ----

8[
CT]Ont
he6
5C0
2,ROL/
RORAbs,Xt
ake6c
ycl
esi
fapa
gebounda
ryi
snotc
ros
sed.

]
4
02 As
sembl
yLi
nes

65C02I
nst
ruc
tionCode
s
⇢eTi meisgiv
eninc l
ockcy
cle
s(1msat1MHz )
.Fortimeswit
ha“ +”,a
dd1i fapage
boundar
yiscr
osse
d.Fortime
switha“d”,a
dd1i findeci
ma lmode.⇢istabl
edoesnot
inc
ludethebi
t-
manipul
ati
oninst
ruc
ti
onsBBR,BBS,RMB,andSMB,whic
ha reonl
yavai
l-
abl
eontheRockwel
landWDCc hips
.

Na
me Addr
ess
ing Ass
embl
y Op- “
P”st
atus
De
scr
ipt
ion Ope
rat
ion Mode La
nguag
e c
odeBy
tesTi
me NZCI
DV
ADC AddAcc
umula
to o A+M+CfA,
rt C(I
ndi
rec
t) ADC ($FF) 72 2 5d NZC---
memorywi
thc
arr
y
AND ANDAcc
umul
aor A∧M fA
t (
Indi
rec
t) AND ($FF) 32 2 5 NZ----
wi
thmemor
y
BIT Te
stAcc
umul
at
orwi
th A∧M Immediat
e BIT #$FF 89 2 2 -Z----
bi
tsi
nme mory M7fN Zer
oPa g
e,X BIT $FF,X 34 2 4 NZ---V
M6fV Absol
ute
,X BIT $FFff,X 3C 3 4+ NZ---V
BRA Br
anc
hal
way
s Br
anc
h Re
lat
ive BRA $FF 80 2 3+ ------
CMP Compa
reme mor
y AnfM (
Indi
rec
t) CMP ($FF) D2 2 5 NZC---
andAc
cumul
ator
DEC De
cre
mentA M71fM Ac
cumul
aor DEC
t 3A 1 2 NZ----
EOR Ex
clus
iveOR Av M fA (
Indi
rec
t) EOR ($FF) 52 2 5 NZ----
Acc
umulat
orwit
hmemor
y
I
NC I
ncr
eme
ntA M+1fM Ac
cumul
aor INC
t 1A 1 2 NZ----
J
MP J
umpt
one
wloc
aton PC+1fPCL (
i Abs
olute JMP ($FFff,X) 7C 3 6 ------
PC+2fPCH I
ndi
rec
t,
X)
LDA LoadAcc
umul
at
or M fA (
Indi
rec
t) LDA ($FF) B2 2 5 NZ----
wit
hme mor
y
ORA Log
ica
lOR A∨M fA (
Indi
rec
t) ORA ($FF) 12 2 5 NZ----
Acc
umula
torwi
thme
mor
y
PHX Pus
hXont
ost
ack X↓ I
mpl
ie
d PHX DA 1 3 ------
PHY Pus
hYont
ost
ack Y↓ I
mpl
ie
d PHY 5A 1 3 ------
PLX Pul
lXf
rom s
tac
k X↑ I
mpl
ie
d PLX FA 1 4 NZ----
PLY Pul
lYf
rom s
tac
k Y↑ I
mpl
ie
d PLY 7A 1 4 NZ----
SBC Subt
rac
tmemory A7M7CfA (
Indi
rec
t) SBC ($FF) F2 2 5d NZC--V
fr
om Awithbor
row
STA S
toreAc
cumul
at n AfM
ori (
Indi
rec
t) STA ($FF) 92 2 5 ------
memory
STZ S
tor
eze
roi
nme y 0fM
mor Ze
roPage STZ $FF 64 2 3 ------
Ze
roPage,
X STZ $FF,X 74 2 4
Abs
olut
e STZ $FFff 9C 3 4
Abs
olut
e,
X STZ $FFff,X 9E 3 5
TRB Te
sta
ndr
ese
tbi
ts ̄ ∧M fM
A Ze
roPage TRB $FF 14 2 5 -Z----
Abs
olut
e TRB $FFff 1C 3 6
TSB Te
sta
nds
etbi
ts A∨M fM Ze
roPage TSB $FF 04 2 5 -Z----
Abs
olut
e TSB $FFff 0C 3 6
Appe
ndi
xC:
650
2Ins
truc
ti
onS
et 4
03

He
xOpe
rat
ionCode
s
Not
e:Ta
blee
ntr
iesi
ngr
aya
reopc
ode
sfort
he6
5C0
2.

0 1 2 3 4 5 6 7 8 9 A B C D E F
ORA TSB ORA ASL ORA ASL TSB ORA ASL
0
x BRK (
zp,
x) z
p z
p z
p
PHP
# A abs abs abs
BPL ORA ORA TRB ORA ASL ORA INC TRB ORA ASL
1
x r
el (
zp)
,y (
zp) z
p z p,
x zp,
x
CLC
abs
,y A abs abs
,xa
bs,
x
J
SR AND BI
T AND ROL AND ROL BIT AND ROL
2
x abs (
zp,
x) zp z
p z
p
PLP
# A abs abs abs
BMI AND AND BIT AND ROL AND DEC BIT AND ROL
3
x r
el (
zp)
,y (
zp) zp,
x zp,
x zp,
x
S
EC
abs
,y A a
bs,
x abs
,xabs
,x
EOR EOR LSR EOR LSR J
MP EOR LSR
4
x RTI (
zp,
x) z
p z
p
PHA
# A abs a
bs abs
BVC EOR EOR EOR LSR EOR EOR LSR
5
x r
el (
zp)
,y (
zp) zp,
x zp,
x
CLI
abs
,y
PHY
abs
,xa
bs,
x
ADC S
TZ ADC ROR ADC ROR MP ADC ROR
J
6
x RTS (
zp,
x) zp z
p z
p
PLA
# A (
ind) a
bs a
bs
BVS ADC ADC S
TZ ADC ROR ADC JMP ADC ROR
7
x r
el (
zp)
,y (
zp) z
p,x z
p,x z
p,x
S
EI
abs
,y
PLY
(
abs
,x)a
bs,
xabs
,x
BRA STA S
TY S
TA S
TX BI
T S
TY STA S
TX
8
x r
el (
zp,
x) zp zp zp
DEY
#
TXA
abs a
bs abs
BCC STA STA S
TY STA STX STA S
TZ S TA S
TZ
9
x r
el (
zp)
,y (
zp) z
p,x z
p,x z
p,y
TYA
a
bs,
y
TXS
abs a
bs,
xabs,
x
LDY LDA LDX LDY LDA LDX LDA LDY LDA LDX
Ax # (zp,
x) # zp zp zp
TAY
#
TAX
a
bs abs abs
BCS LDA LDA LDY LDA LDX LDA LDY LDA LDX
Bx r
el (
zp)
,y (
zp) zp,
x zp,
x zp,
y
CLV
abs
,y
TS
X
abs
,x a
bs,
x a
bs,
y
CPY CMP CPY CMP DEC CMP CPY CMP DEC
Cx # (zp,
x) z
p z
p z
p
I
NY
#
DEX
abs abs abs
BNE CMP CMP CMP DEC CMP CMP DEC
Dx r
el (
zp)
,y (
zp) zp,
x zp,
x
CLD
abs
,y
PHX
abs
,xabs
,x
CPX S
BC CPX S
BC I
NC S
BC CPX S
BC INC
Ex # (z
p,x
) z
p zp zp
I
NX
#
NOP
abs abs a
bs
BEQ SBC SBC S
BC INC SBC SBC I
NC
Fx r
el (
zp)
,y (
zp) z
p,x z
p,x
S
ED
a
bs,
y
PLX
a
bs,
xabs,
x

Abbr
evi
ati
ons Addr
ess
ingMode
s
#=i mme di
ate a
bs,x=i nde xe
dbyX
A=Ac c
umul at
or a
bs,y=i nde x
edbyY
abs=absolute (
abs)=i ndirect
rel=rel
ativ
e (
abs,x)=i ndexedabs
oluteindir
e c
t
zp=z er
opa ge z
p,x=i nde xedbyX
x=X- Regist
er z
p,y=i nde xedbyY
y=Y- Regis
ter (
zp)=i ndi r
ect
(
zp,x)=i ndexedindi
rect(pre-
indexed)
(
zp),
y=i ndirec
tinde
xed( post
-index
e d)

]
Ap
pendi
xD:
Moni
torS
ubr
out
ine
s
Hereisal istofsomeus ef
ulsubrout
inesint heAppl e
’sMonitorand
Autosta
rtROMs .Tousethesesubrouti
nesf
rom a ss
embly -
langua
geprogra
ms,
l
oadt heproperme mor
yloc a
tionsor6502regis
tersasrequiredbythesubr
ou-
t
inea ndexecuteaJSRtot hesubrouti
ne’
sstart
ingaddress.Itwil
lper
formthe
f
unc t
ionandreturnwit
hthe6 502’
sregi
ste
rssetasdesc
ribed.

Out
putSubr
out
ine
s

$FDED COUT Outputac ha r


acter
COUT i
sthestandardchar
acteroutputsubroutine.⇢ec hara
ctertobeoutput
shoul
dbei ntheAc cumulat
or.COUTcal
lsthec urrentchara
c t
eroutputsubr
ou-
ti
newhos eaddres
sisstor
edinCSW (loc
a t
ions$36a nd$37),usual
lyCOUT1(s
ee
be
low)
.
$FDF0 COUT1 Out
putt
oscr
een
COUT1di
spl
ayst
hec ha
rac
teri
ntheAc
cumul
at
orontheAppl
e’
ssc
ree
natt
he
curr
entoutputcurs
orpos iti
ona ndadvance
stheoutputcurs
or.Ithandle
st he
cont
rolc har
act
ers,<RETURN>,li
nefee
d,a ndbell
.Itret
urnswitha l
lregis
ters
int
act
.Cha r
act
ersinther angeof$00to$3Fc omeoutinver
se;char
acte
rsfrom
$40to$7FareSashi
ng ;c
haracte
rsfrom $80t
o$FFa r
enormal.
$FE80 SETINV SetI
nversemode
Set
sInve
rsevi
deomodef orCOUT1.Al
loutputcha
rac
ter
swillbedis
pla
yedas
bl
ackdot
sonawhi teback
ground.⇢eY- Regi
st
erisse
tto$3F;allot
hersa
re
uncha
nged.
$FE84 SETNORM SetNormalMode
Set
sNor malv
ide
omodef orCOUT1.Al
loutputcha
rac
ter
swillbedispl
ayeda
s
whitedot
sonablackbackground.⇢eY- Regi
st
eriss
etto$FF;allothe
rsar
e
unchange
d.
$FD8E CROUT Gene
rat
ea<RETURN>
CROUTse
ndsa<RETURN>cha
rac
tert
othecur
rentout
putde
vic
e.
$FD8B CROUT1 <RETURN>withcle
ar
CROUT1c
lea
rst
hescre
enfrom t
hec
urrentc
urs
orpos
it
iontot
hee
dgeoft
het
ext
window,
thenc
all
sCROUT.
$FDDA PRBYTE Pri
ntahexade
cimalbyt
e
⇢issubr
outineoutputst
hecont
ent
softheAcc
umulatori
nhexade
cimalont
he
c
urre
ntoutputdevi
c e
.⇢ec ont
ent
soft
heAccumulat
orares
cra
mbled.
Appe
ndi
xD:
Moni
torS
ubr
out
ine
s 4
05

$FDE3 PRHEX Printahexa


deci
ma l
digit
⇢issubrouti
neoutput
sthelowerni
bbleoftheAccumula
torasas
ing
lehe
xa-
de
cimaldigi
t.⇢econtent
softheAcc
umul a
torar
esc
rambled.
$F941 PRNTAX Pri
ntAa ndXi nhexa
deci
ma l
⇢i soutputsthec ontent
soft heAccumulatorandX-Regi
st
erasaf our-
digi
t
hexadec
ima lval
ue.⇢eAc cumulat
orcontai
nstheFrs
tbyt
eoutput
; t
heX-Regis
-
te
rc ont
ainsthesecond.⇢ec onte
ntsoftheAccumula
torar
eusual
lyscr
ambled.
$F948 PRBLNK Print3spaces
Out
put
sthrees
pa c
ec hara
c t
erstothestandar
dout putde
vice
.Upone
xit
,the
Acc
umula
torus
uall
yc ont
ains$A0,t
heX- Regi
st
ercontai
ns0.
$F94A PRBL2 Pri
ntmanyspaces
Output
sfrom 1t o256s pac
ec harac
ter
stothesta
ndardout
putdev
ice.Upon
ent
ry,t
heX- Re
gis
tershouldconta
inthenumberofspa
cest
obeout
put.I
ftheX-
Regi
ste
ris$00,t
henPRBL2wi l
loutput256bl
anks
.
$FF3A BELL Out
puta“bel
l”char
act
er
Se
ndsabell
(<CTRL>G)cha
rac
tert
othec
urre
ntoutputde
vic
e.I
tle
ave
stheAc
cu-
mula
torhol
ding$87.
$FBDD BELL1 Beept
heAppl
e’ss
peake
r
Beepst
heApple’
sspeakerf
or0
.1s
econda
t1KHz.
Itscr
amblest
heAc
cumul
at
or
andY-Reg
ist
er.

I
nputSubr
out
ine
s

$FD0C RDKEY Getaninputcharac


ter
⇢isist
hestanda
r dchara
c t
erinputsubr
outine.I
tplac
esaSashinginputcur
sor
onthesc
ree
na tthecurrentcursorposi
ti
ona ndjumpstotheinputsubrout
ine
whosea
ddres
sisstor
edinKSW( $38,$39)
,usuall
yKEYIN(se
ebelow).
$FD35 RDCHAR Getani
nputcha
ract
eroresc
apecode
RDCHARi
sana
lt
ernat
einputs
ubr
out
inewhi
chget
schar
acte
rsf
rom t
hesta
nda
rd
i
nputbuta
lsoi
sca
pabl
eofi
nte
rpr
eti
ngt
hee
lev
ene
sca
pec
ode
s.
$FD1B KEYIN ReadtheAppl e’
skey
board
⇢isi
sthek ey
boardinputsubrouti
ne.ItreadstheApple
’ske
yboar
d,wait
sfora
k
eypr
ess,andrandomizestherandom- numbers e
ed.Whenitget
sakeypre
ss,i
t
r
emovestheSashi
ngcursorandr e
turnswiththekeycodeint
heAccumula
tor.
$FD6A GETLN Getaninputli
newi t
hprompt
GETLNist
hesubroutinewhic
hg at
hersi
nputline
s.Yourprog
ramscanca
llGETLN
withtheprope
rpr omptc hara
cterinloca
tion$33;GETLNwil
lre
turnwit
ht he

]
4
06 As
sembl
yLi
nes

i
nputlineintheinputbuLer(
beginni
nga
tloc
ati
on$200)a
ndt
heX-
Reg
ist
er
hol
dingthel
engt
hoft hei
nputl
ine
.
$FD67 GETLNZ Getaninputli
ne
GETLNZi
sanal
ter
na t
ee nt
rypoi
ntforGETLNwhichis
sue
sa<RETURN>t
othes
tan-
dardout
putbe
for
ef al
li
ngintoGETLN(s
eeabove)
.
$FD6F GETLN1 Geta
ni nputl
ine,
noprompt
GETLN1 i
sana
lt
ernateent
rypoi
ntf
orGETLN whi
chdoesnoti
ssueapr
ompt
bef
oreitga
the r
stheinputli
ne.
If,
howev
er,theuserca
ncelst
hei
nputli
ne(ei
ther
wit
htooma nyba c
kspacesorwit
ha<CTRL>X),thenGETLN1wi
lli
ssuethecon-
te
ntsofloc
ation$33a sapromptwhe
nitget
sa not
herli
ne.

Low-
ResGr
aphi
csSubr
out
ine
s

$F864 SETCOL Setl


ow-re
sgra
phi
csc
olor
⇢issubrout
ines
etsthecol
orus
edf
orpl
ott
ingont
hel
ow-r
esscr
eent
othec
olor
pa
sse
di ntheAcc
umul at
or.
$F85F NEXTCOL I
ncre
mentcol
orby3
⇢i
sadds3t
othecurre
ntcol
orus
edforl
ow-
resgr
aphi
cs.
$F800 PLOT Pl
otablockontheLow-ResSc
reen
⇢issubrouti
neplot
sas i
nglebloc
kont helow-r
esscree
nofthepre-
spec
iFed
c
olor.
⇢ebl ock’
svert
ica
lposi
ti
onispasse
dintheAc c
umulat
orandit
shori
zon-
t
alposi
ti
onintheY-Regis
ter
.PLOTret
urnswit
htheAc c
umulat
orscr
amble
d,but
t
heX-Regist
erandY-Regi
ste
rareunmolest
ed.
$F819 HLINE Dr awahorizontallineofblocks
⇢issubr
outinedrawsahor i
zontall
ineofblocksoft hepr e
-speci
Fedcoloron
t
helow-r
esscree
n.Yous houl
dc a
llHLINEwiththeve r
ticalc
oordinat
eoftheli
ne
i
ntheAc cumulat
or,thele4mosthorizont
alcoordinateintheY- Regi
st
er,and
t
her i
ght
mos thori
zontalcoordi
nateinl oca
tion$2C.HLINE r et
urnswiththe
Acc
umulatorandY-Re g
ist
ersc
rambled,butwit
ht heX- Regi
ste
rintact
.
$F828 VLINE Drawav ert
icall
ineofblocks
⇢issubrouti
nedr awsavert
ic
a lli
neofblocksofthepre-speci
Fedc ol
oronthe
l
ow-re
ssc r
een.Yous houl
dcallVLINEwiththehorizont
alcoordinat
eoftheli
ne
i
ntheY- Regi
ste
r,thetopvert
icalcoordi
nateintheAc cumulator,a
ndt hebot-
t
om vert
icalcoordinat
einloc at
ion$2D.VLINE r
eturnswi t
htheAc c
umulator
s
cra
mbl e
d.
Appe
ndi
xD:
Moni
torS
ubr
out
ine
s 4
07

$F832 CLRSCR Cle


artheent
irel
ow-re
sscre
en
CLRSCR c
lea
rst
hee nti
relow-
resg
raphi
csscr
een.Ifyoucal
lCLRSCRwhi
let
he
v
ideodispl
ayisi
nTe x
tmode,i
twi
llFl
lthes
cree
nwithi
nve
rse
-mode“@”c
har
-
a
cte
rs.CLRSCRde
str
oysthec
ont
ent
softheAcc
umula
tora
ndY-Reg
ist
er.
$F836 CLRTOP Clea
rthetopoft
helow-re
sScre
e n
CLRTOPi
sthes
amea sCLRSCR(
abov
e),e
xceptt
hati
tcl
earsonl
ythetop40r
ows
oft
hes
cre
en.
$F871 SCRN Rea
dthelow-ress
cre
en
⇢issubr
outi
nereturnsthecolorofas
ingl
eblockonthelow-r
esscr
een.Cal
lit
asyouwouldca
llPLOT( above
).⇢eblock’
scolorval
uewi l
lbere
turnedint
he
Acc
umulator
.Noot herr
eg i
st
ersar
echang
ed.

Hi
-Re
sGr
aphi
csSubr
out
ine
s

$F3E2 HGR Hi-


respage1
⇢isisthee
ntr
ypoi ntfortheHGRcomma nd.I
tini
ti
al
iz
eshi
-r
espa
ge1
,the
n
c
lea
rsanddi
spl
aysthesc
reen.
$F3D8 HGR2 Hi-
respage2
⇢isisthee
ntr
ypoi ntf
ortheHGR2c
omma nd.I
tini
ti
al
iz
eshi
-r
espa
ge2
,the
n
c
lea
rsanddi
spl
aysthesc
ree
n.
$F3F2 HCLR Cl
eart
obl
ack
Cl
ear
sthec
urr
entscre
ent
obl
ack
1.
$F3F6 BKGND Cl
eartocolor
Cl
ear
sthec
urr
entscree
ntot
hel
astpl
ott
edHCOLOR.
$F6F0 HCOLOR Se
tcol
or
S
etst
hec
urr
entHCOLORt
othec
ont
ent
soft
heX-
Reg
ist
er(
077
).
$F411 HPOSN Posit
ionthecurs
or
Pos
it
ionst
hehi-
re s“cur
sor
”wi
thoutplott
ing
.EnterwithX,Y(l
ow,high)equa
l
tot
hehori
zont
alposit
ion,a
ndt
heAc cumulat
orequaltothev
ert
ic
alpos
iti
on.
$F457 HPLOT Pl
ota tc
urs
or
I
dent
ic
alt
oHPOSN,butplot
scur
rentHCOLORatc
oor
dina
tesg
ive
n.
$F5CB HFIND Returnthecursorposi
ti
on
Returnsthecurr
ent“cursor”posi
ti
on.⇢i sisusef
ula4eraDRAWt
oFndwhe re
you’v
ebe e
nle4.⇢ec oordi
nate
sa r
er e
turnedin:$E0,$E1=hor
izont
al(
low,
high)
, $E2=vert
ic
al.

]
4
08 As
sembl
yLi
nes

$F53A HLIN Drawal i


ne
⇢issubr
outi
nedra
wsal i
nefr
om thepr e
viousplott
othepoi
ntgi
ven.
Oni nput
,
s
etA,X( l
ow,hi
gh)tothehori
zontalposi
tion,a
ndYe qualt
othever
ti
calposi
-
t
ion.
$F730 SHNUM Loadshapenumber
⇢i srout
ineputstheaddr
essoft
heshapenumbe ri
ndic
ate
dbyX-Regi
st
erinto
$1A,$1B.SHNUMretur
nswit
hX,Y( low,hig
h)a l
sose
ttoaddr
essoft
hatshape
tabl
eentr
y .
$F601 DRAW Drawashape
Drawthesha
pepointedtobyX,Y(l
ow,hi
gh)i
nthecur
rentHCOLOR.Note
:X, Y
poi
nttothespe
ciFcentr
y,nott
hebegi
nni
ngofthet
abl
e.Bes ur
et oca
llSHNUM
Frs
t.
$F65D XDRAW Er a
s easha
pe(drawXOR)
Erase
sashapetha twasjus
tdra
wn(ifthere
)bydoingane xc
lusi
veORwitht
he
scr
eendat
a.Oni nput,l
oadX,Y(low,high)wit
ht headdressoft
hesha
peto
XDRAWorc
allSHNUMF rs
twit
htheX-
Re g
iste
requal
totheshapenumber.

Fl
oat
ingPoi
ntAc
cumul
ator

$EBAF ABS Abs ol


uteval
ue
⇢issubrout
inet
akesthea
bsol
utev
alueoftheFl
oat
ingPoi
ntAc
cumul
at
or(
FAC
=$9D7$A2).
$EC23 INT INTfunc
tion
⇢ eINTfunc
tionuse
sQINT(
$EBF2)t
oconve
rttheFACt
oint
ege
rfor
mandt
hen
backtoaSoa
ting-
poi
ntnumberi
nFAC.
$EFAE RND Random number
⇢isisthes
ameast heRNDc
omma
nd.
Pr oduc
esa(
poorqua
li
ty)ps
eudo-
random
numberint
heFAC.
$EB82 SIGN Si
gnofFAC(inAcc
umulator)
S
etst
heAc
cumul
atorto$01,
$00,
or$FFi
ftheFACi
spos
iti
ve,
zero,
orne
gat
ive
.
$EB90 SGN SignofFAC(i
nFAC)
Call
sSIGNFr
st,
thenset
sFACba
sedupontheAc
cumul
at
orva
lue
.
$EE8D SQR Squar
er oot
⇢isi
stheSQRcomma nd.I
tcompute
sthesquarer
ootofFACus
ingas
lowe
xpo-
ne
nti
ati
onmethod:X0.5.
$EF09 EXP Expone
nti
ati
on
⇢i sr
out
iner
ais
esetotheFACpowe
randle
ave
sthere
sul
tinFAC.
Appe
ndi
xD:
Moni
torS
ubr
out
ine
s 4
09

$E941 LOG Logari


thm ba
see
⇢i
scomput
est
helogar
it
hm (
bas
ee)ofFAC.
$EE97 FPWRT Rai
seARGt ot
heFACpowe r(bas
ee )
⇢iscompute
sARG tot heFAC powe
rusingthefor
mulaEXP(LOG(ARG)*FAC).
Be
for
ecal
li
ng,y
ous houl
dloadtheAccumula
torwi
thFACEXP(
$9D).
$EBB2 FCOMP CompareFACt ome mor y
Bef
orec
all
ing
,loadthememor yloc
ati
onintheY-Regist
erandAccumul
at
or.On
ex
it,A=$01ifthevaluea
ttheme moryloc
a t
ionisle
ssthanFAC;A=$00ift
he
memoryequal
sFAC;A=$FFi fthememoryisgrea
terthanFAC.
$EED0 NEGOP Mul
ti
plybyL1
⇢i sr
out
inet
ogg
lesthes
ignofFAC.
$E7A0 FADDH Add0.
5
⇢i
srout
inea
dds0
.5t
oFAC.
$EA55 DIV10 Di
videby10
⇢i
srout
inedi
vi
desFACby1
0.I
tre
tur
nsposi
ti
veva
lue
sonl
y.
$EA39 MUL10 Mult
iplyby10
⇢isrout
inemul
tipl
iesFACby1
0.I
twor
k sonbot
hpos
it
ivea
ndne
gat
ivenum-
be
rs.
$EFEA COS Cos
ine
⇢ec
osi
nef
unc
ti
onofFAC.
$EFFA SIN Si
ne
⇢es
inef
unc
ti
onofFAC.
$EFF1 TAN Ta
nge
nt
⇢et
ang
entf
unc
ti
onofFAC.
$F09E ATN Ar
cta
nge
nt
⇢ea
rct
ang
entofFAC.
$ED34 FOUT Creat
eas t
ring
Cr
eateast
ri
ngatthest
artoft
hest
ack($1007$110)equi
val
entt
otheFACval
ue.
Onexitt
heY-Reg
ist
erandAccumula
torpointtothest
ri
ng.⇢estr
ingist
ermi-
na
tedbya$00.

]
4
10 As
sembl
yLi
nes

Ot
herSubr
out
ine
s

$FCA8 WAIT Dela


y
⇢i ssubrouti
nedel
aysf
oraspeci
Fcamountofti
me ,
the
nr e
turnstotheprog r
am
whichc a
lledi
t.⇢ea mountofde
layi
sspeci
Fedbytheconte
ntsoftheAc cumu-
l
atorA.⇢ede l
ayisgiv
enby0 .
5102d( 26+2 7
A +5 A2)mi cr
oseconds.WAIT
re
turnswi t
htheAccumulat
orzer
oedandtheX-andY-Regis
tersundist
urbed.
$FB1E PREAD Rea
dagamec ont
rol
le
r
PREAD r
etur
nsanumberr
epr
ese
nti
ngthepos
it
ionofag amec
ont
rol
le
r.You
s dFr
houl stpasst
henumberofthegamec
ont
rol
le
r(0to3)intheX-Regi
ste
r.I
f
thi
snumbe ri
snotva
li
d,st
rangethi
ngsma
yhappe
n.PREADret
urnswithanum-
berfr
om $00to$FFi
ntheY-Regis
ter
.⇢eAcc
umulat
orissc
rambled.
$FF2D PRERR Pri
nt“
ERR”
Se
ndsthewor
d“ ERR” ,f
oll
owedbyabel
lcha
rac
ter
,tot
hes
tanda
rdout
put
de
vic
e.⇢eAccumulatori
sscr
ambl
ed.
$FF4A IOSAVE Saveal
lre
g i
ste
rs
⇢ec onte
ntsofthe6 502’
sint
erna
lreg
ist
ersaresav
edinl
ocat
ions$45t
hrough
$49intheorderA-X- Y-
P-S.⇢econte
ntsoftheAccumul
at
ora ndt
heX-Regi
s-
terar
echang
e d;t
hede ci
malmodeisc
lear
ed.
$FF3F IOREST Res
toreal
lre
gist
ers
⇢ec ontent
softhe6 5
0 2
’si
nte
rnalreg
ist
ersar
el oa
dedf
rom l
oca
tions$45
t
hrough$49.
Ap
pendi
xE:
ASCI
IandS
cre
enChar
ts

YouGe
tWha
tYouASCI
IFor
..
.
⇢i sc hartshowsma nyoft hepos si
bleinterpretationsofaby tev aluei n
me mory .⇢eFr s
tthreecolumnss howt hehexv aluea ndi tsdecima la ndbi nary
equival
ents.⇢i sc anbeha ndywhe nc onvers
ionsa rene e ded.⇢ene xtc olumn
showswha tkeyona nAppl eIIkeyboa rdgeneratesthatc ha r
acter
,ifa ny .
Althought hestandar
dAppl eI Idoe snotha veal owe rcasekeyboa rd, lower-
casek e
y sareshownt oall
owf orma chineswiths peciala dapter
s,e xterna lkey-
boards,etc.
⇢es cre
enc olumns howswha tc haract
eri stobee xpecte
di ftha tv alueis
stor
e dinthesc r
eenme mor yarea
, $4007$7FF. Inversec ha r
acter
sa res urr ounde d
bys quarebrackets[A],whileSashingc haract
ersa r
es urrounde dbya ng lebr ack-
ets>A<.
⇢eAppl e s
o4c olumni ndic
a t
e showAppl eso4BAS ICi nt
erpre tst ha tbyte
whe ntok e
nizi
ngpr ograms.
Not ethatforcontrolcharac
t ers,t
he“ ^”sy mboli sus ed.⇢usaCont rol
-A
woul dbeindicated^A.

He
x De
c Bi
nar
y Ke
y S
cre
en Appl
eso5
$00 0 0000 0000 [@] ^@
$01 1 0000 0001 [A] ^A
$02 2 0000 0010 [B] ^B
$03 3 0000 0011 [C] ^C
$04 4 0000 0100 [D] ^D
$05 5 0000 0101 [E] ^E
$06 6 0000 0110 [F] ^F
$07 7 0000 0111 [G] ^G
$08 8 0000 1000 [H] ^H
$09 9 0000 1001 [I] ^I
$0A 10 0000 1010 [J] ^J
$0B 11 0000 1011 [K] ^K
$0C 12 0000 1100 [L] ^L
$0D 13 0000 1101 [M] ^M
$0E 14 0000 1110 [N] ^N
$0F 15 0000 1111 [O] ^O
$10 16 0001 0000 [P] ^P
$11 17 0001 0001 [Q] ^Q
$12 18 0001 0010 [R] ^R
$13 19 0001 0011 [S] ^S
$14 20 0001 0100 [T] ^T
$15 21 0001 0101 [U] ^U
$16 22 0001 0110 [V] ^V
$17 23 0001 0111 [W] ^W
$18 24 0001 1000 [X] ^X
$19 25 0001 1001 [Y] ^Y
$1A 26 0001 1010 [Z] ^Z
$1B 27 0001 1011 [[] ^[
$1C 28 0001 1100 [\] ^\
$1D 29 0001 1101 []] ^]

]
4
12 As
sembl
yLi
nes

He
x De
c Bi
nar
y Ke
y S
cre
en Appl
eso5
$1E 30 0001 1110 [^] ^^
$1F 31 0001 1111 [_] ^_
$20 32 0010 0000 [ ] Space
$21 33 0010 0001 [!] !
$22 34 0010 0010 ["] "
$23 35 0010 0011 [#] #
$24 36 0010 0100 [$] $
$25 37 0010 0101 [%] %
$26 38 0010 0110 [&] &
$27 39 0010 0111 ['] '
$28 40 0010 1000 [(] (
$29 41 0010 1001 [)] )
$2A 42 0010 1010 [*] *
$2B 43 0010 1011 [+] +
$2C 44 0010 1100 [,] ,
$2D 45 0010 1101 [-] -
$2E 46 0010 1110 [.] .
$2F 47 0010 1111 [/] /
$30 48 0011 0000 [0] 0
$31 49 0011 0001 [1] 1
$32 50 0011 0010 [2] 2
$33 51 0011 0011 [3] 3
$34 52 0011 0100 [4] 4
$35 53 0011 0101 [5] 5
$36 54 0011 0110 [6] 6
$37 55 0011 0111 [7] 7
$38 56 0011 1000 [8] 8
$39 57 0011 1001 [9] 9
$3A 58 0011 1010 [:] :
$3B 59 0011 1011 [;] ;
$3C 60 0011 1100 [<] <
$3D 61 0011 1101 [=] =
$3E 62 0011 1110 [>] >
$3F 63 0011 1111 [?] ?
$40 64 0100 0000 >@< @
$41 65 0100 0001 >A< A
$42 66 0100 0010 >B< B
$43 67 0100 0011 >C< C
$44 68 0100 0100 >D< D
$45 69 0100 0101 >E< E
$46 70 0100 0110 >F< F
$47 71 0100 0111 >G< G
$48 72 0100 1000 >H< H
$49 73 0100 1001 >I< I
$4A 74 0100 1010 >J< J
$4B 75 0100 1011 >K< K
$4C 76 0100 1100 >L< L
$4D 77 0100 1101 >M< M
$4E 78 0100 1110 >N< N
$4F 79 0100 1111 >O< O
$50 80 0101 0000 >P< P
$51 81 0101 0001 >Q< Q
$52 82 0101 0010 >R< R
$53 83 0101 0011 >S< S
$54 84 0101 0100 >T< T
$55 85 0101 0101 >U< U
$56 86 0101 0110 >V< V
$57 87 0101 0111 >W< W
Appe
ndi
xE:
ASCI
IandS
cre
enCha
rts 4
13

He
x De
c Bi
nar
y Ke
y S
cre
en Appl
eso5
$58 88 0101 1000 >X< X
$59 89 0101 1001 >Y< Y
$5A 90 0101 1010 >Z< Z
$5B 91 0101 1011 >[< [
$5C 92 0101 1100 >\< \
$5D 93 0101 1101 >]< ]
$5E 94 0101 1110 >^< ^
$5F 95 0101 1111 >_< _
$60 96 0110 0000 > < `
$61 97 0110 0001 >!< a
$62 98 0110 0010 >"< b
$63 99 0110 0011 >#< c
$64 100 0110 0100 >$< d
$65 101 0110 0101 >%< e
$66 102 0110 0110 >&< f
$67 103 0110 0111 >'< g
$68 104 0110 1000 >(< h
$69 105 0110 1001 >)< i
$6A 106 0110 1010 >*< j
$6B 107 0110 1011 >+< k
$6C 108 0110 1100 >,< l
$6D 109 0110 1101 >-< m
$6E 110 0110 1110 >.< n
$6F 111 0110 1111 >/< o
$70 112 0111 0000 >0< p
$71 113 0111 0001 >1< q
$72 114 0111 0010 >2< r
$73 115 0111 0011 >3< s
$74 116 0111 0100 >4< t
$75 117 0111 0101 >5< u
$76 118 0111 0110 >6< v
$77 119 0111 0111 >7< w
$78 120 0111 1000 >8< x
$79 121 0111 1001 >9< y
$7A 122 0111 1010 >:< z
$7B 123 0111 1011 >;< {
$7C 124 0111 1100 ><< |
$7D 125 0111 1101 >=< }
$7E 126 0111 1110 >>< ~
$7F 127 0111 1111 >?< Rubout
$80 128 1000 0000 ^@ @ END
$81 129 1000 0001 ^A A FOR
$82 130 1000 0010 ^B B NEXT
$83 131 1000 0011 ^C C DATA
$84 132 1000 0100 ^D D INPUT
$85 133 1000 0101 ^E E DEL
$86 134 1000 0110 ^F F DIM
$87 135 1000 0111 ^G G READ
$88 136 1000 1000 ^H H GR
$89 137 1000 1001 ^I I TEXT
$8A 138 1000 1010 ^J J PR #
$8B 139 1000 1011 ^K K IN #
$8C 140 1000 1100 ^L L CALL
$8D 141 1000 1101 ^M M PLOT
$8E 142 1000 1110 ^N N HLIN
$8F 143 1000 1111 ^O O VLIN
$90 144 1001 0000 ^P P HGR2
$91 145 1001 0001 ^Q Q HGR

]
4
14 As
sembl
yLi
nes

He
x De
c Bi
nar
y Ke
y S
cre
en Appl
eso5
$92 146 1001 0010 ^R R HCOLOR=
$93 147 1001 0011 ^S S HPLOT
$94 148 1001 0100 ^T T DRAW
$95 149 1001 0101 ^U U XDRAW
$96 150 1001 0110 ^V V HTAB
$97 151 1001 0111 ^W W HOME
$98 152 1001 1000 ^X X ROT=
$99 153 1001 1001 ^Y Y SCALE=
$9A 154 1001 1010 ^Z Z SHLOAD
$9B 155 1001 1011 ^[ [ TRACE
$9C 156 1001 1100 ^\ \ NOTRACE
$9D 157 1001 1101 ^] ] NORMAL
$9E 158 1001 1110 ^^ ^ INVERSE
$9F 159 1001 1111 ^_ _ FLASH
$A0 160 1010 0000 Space Space COLOR=
$A1 161 1010 0001 ! ! POP
$A2 162 1010 0010 " " VTAB
$A3 163 1010 0011 # # HIMEM:
$A4 164 1010 0100 $ $ LOMEM:
$A5 165 1010 0101 % % ONERR
$A6 166 1010 0110 & & RESUME
$A7 167 1010 0111 ' ' RECALL
$A8 168 1010 1000 ( ( STORE
$A9 169 1010 1001 ) ) SPEED=
$AA 170 1010 1010 * * LET
$AB 171 1010 1011 + + GOTO
$AC 172 1010 1100 , , RUN
$AD 173 1010 1101 - - IF
$AE 174 1010 1110 . . RESTORE
$AF 175 1010 1111 / / &
$B0 176 1011 0000 0 0 GOSUB
$B1 177 1011 0001 1 1 RETURN
$B2 178 1011 0010 2 2 REM
$B3 179 1011 0011 3 3 STOP
$B4 180 1011 0100 4 4 ON
$B5 181 1011 0101 5 5 WAIT
$B6 182 1011 0110 6 6 LOAD
$B7 183 1011 0111 7 7 SAVE
$B8 184 1011 1000 8 8 DEF FN
$B9 185 1011 1001 9 9 POKE
$BA 186 1011 1010 : : PRINT
$BB 187 1011 1011 ; ; CONT
$BC 188 1011 1100 < < LIST
$BD 189 1011 1101 = = CLEAR
$BE 190 1011 1110 > > GET
$BF 191 1011 1111 ? ? NEW
$C0 192 1100 0000 @ @ TAB
$C1 193 1100 0001 A A TO
$C2 194 1100 0010 B B FN
$C3 195 1100 0011 C C SPC(
$C4 196 1100 0100 D D THEN
$C5 197 1100 0101 E E AT
$C6 198 1100 0110 F F NOT
$C7 199 1100 0111 G G STEP
$C8 200 1100 1000 H H +
$C9 201 1100 1001 I I -
$CA 202 1100 1010 J J *
$CB 203 1100 1011 K K /
Appe
ndi
xE:
ASCI
IandS
cre
enCha
rts 4
15

He
x De
c Bi
nar
y Ke
y S
cre
en Appl
eso5
$CC 204 1100 1100 L L ;
$CD 205 1100 1101 M M AND
$CE 206 1100 1110 N N OR
$CF 207 1100 1111 O O >
$D0 208 1101 0000 P P =
$D1 209 1101 0001 Q Q <
$D2 210 1101 0010 R R SGN
$D3 211 1101 0011 S S INT
$D4 212 1101 0100 T T ABS
$D5 213 1101 0101 U U USR
$D6 214 1101 0110 V V FRE
$D7 215 1101 0111 W W SCRN(
$D8 216 1101 1000 X X PDL
$D9 217 1101 1001 Y Y POS
$DA 218 1101 1010 Z Z SQR
$DB 219 1101 1011 [ [ RND
$DC 220 1101 1100 \ \ LOG
$DD 221 1101 1101 ] ] EXP
$DE 222 1101 1110 ^ ^ COS
$DF 223 1101 1111 _ _ SIN
$E0 224 1110 0000 ` ` TAN
$E1 225 1110 0001 a a ATN
$E2 226 1110 0010 b b PEEK
$E3 227 1110 0011 c c LEN
$E4 228 1110 0100 d d STR$
$E5 229 1110 0101 e e VAL
$E6 230 1110 0110 f f ASC
$E7 231 1110 0111 g g CHR$
$E8 232 1110 1000 h h LEFT$
$E9 233 1110 1001 i i RIGHT$
$EA 234 1110 1010 j j MID$
$EB 235 1110 1011 k k
$EC 236 1110 1100 l l
$ED 237 1110 1101 m m
$EE 238 1110 1110 n n
$EF 239 1110 1111 o o
$F0 240 1111 0000 p p
$F1 241 1111 0001 q q
$F2 242 1111 0010 r r
$F3 243 1111 0011 s s
$F4 244 1111 0100 t t
$F5 245 1111 0101 u u
$F6 246 1111 0110 v v
$F7 247 1111 0111 w w
$F8 248 1111 1000 x x
$F9 249 1111 1001 y y
$FA 250 1111 1010 z z
$FB 251 1111 1011 { {
$FC 252 1111 1100 | |
$FD 253 1111 1101 } }
$FE 254 1111 1110 ~ ~
$FF 255 1111 1111 Rubout Rubout

]
4
16 As
sembl
yLi
nes

Te
xtSc
ree
nMe
mor
yMa
p
Appe
ndi
xE:
ASCI
IandS
cre
enCha
rts 4
17

Hi
-Re
sMe
mor
yMa
p

]
Ap
pendi
xF:
Zer
o-Pag
eMe
mor
yUs
age

Spe
cia
lLoc
ati
ons
⇢istabl
ewa sadaptedf
rom J
onBe t
tenc
ourt’
sApp
leIIInf
oArc
hiv
es,t
he
Appl
es
oftI
IBAS IC Prog
rammingRef
ere
nceMa nua,a
l ndBeneat
hAppl
eDOS
(Wort
handLechner).

$0A7$0C JMPtoUSRr outi


ne $79,$7A BAS
ICa
ddr
essofl
inenumbe
r
$1A,$1B BASIChi -
resshapepointer t
obee
xec
ute
dne
xt
$1C BASIClastCOLORus e
d $7B,$7C BASI
Ccurr
e ntli
nenumbe rof
$1D BASICline-pl
ott
ingcounte r DATA
$20 Le4ma r
g i
n( 0
739 /
79,default0) $7D,$7E BASI
Cnexta ddr
essofDATA
$21 Width(1740or8 0,defa
ult40 , $7F,$80 BASI
CaddressofINPUT/DATA
0c
ras
hesAppl
eso4) $81,$82 BASI
Clas
t-usedvari
ablename
$22 Topma
rgi
n(0
723
,de
faul
t0,
or $83,$84 BASI
Caddressoflas
tusedvar
i-
2
0forg
raphi
cs) a
ble
’sv
alue
$23 Bot
tom ma rgi
n(0724,def
a ul
t2 4) $9B,$9C Pointe
rforFNDLIN($D61A)and
$24 Hori
z ont
a l
curs
orposi
tion( 0739/
7 9
) GETARYPT($F7D9)
$25 Ver
ticalcurs
orposi
ti
on( 0723 ) $9D7$A3 Float
ing-poi
ntacc
umul a
torFAC
$26,$27 GBASL/HBAS I
Ca ddressofle4- $A57$AB Float
ing-poi
ntarg
ume ntARG
mos
tby
teofc
urr
entpl
otl
ine $AF,$B0 BAS I
Cpr ogr
ame ndaddress
$28,$29 BASL/Hc ursorpos iti
ona ddres
s $B17$C8 CHRGETsubrouti
ne;BASICcall
s
$2B BOOTs otd1
l 6 he
ref
ort
hene
xtc
har
act
er
$2C Lo-resHLIN/VLINe ndpoi nt $B87$B9 Po i
ntertolastcharac
terfr
om
$30 COLOR ⌥ 1 7 CHRGET
$32 Tex tmask($FF=No rma l
,$3F= $C97$CD BAS ICRNDr andom number
Invers
e,$7F=F lashing ) $D6 BAS ICprotect
ionSa g,def
aulti
s$00,
$33 Promptc haracter sett
o$FFf orrun- onlymode
$36,$37 CASL/Ho utputr out inea ddress $D87$DE ONERRp ointers
/ sc
rat
ch
$38,$39 KASL/Hi nputr outinea ddress $DF ERRSTKstackpoint e
r
$48,$49 RWTSIOBa ddres s $E0,$E1 Ho r
izontalHPLOTc oordi
nate
$50,$51 Re s
ultoft hec onv ersionoft he $E2 Ve r
tic
alHPLOTc oordinate
FACtoa1 6-bi tinteger $E4 HCOLOR(0=0 ,1=42,2 =85,
3=1 2
7,
$67,$68 BAS ICpr og rams t
a rtaddr ess 4
=12
8,5
=17
0,6
=21
3,7
=25
5)
(defa
ulti
s$0801) $E6 HGRpag e:
HGR=$20, HGR2=$40
$69,$6A BAS ICv ariabless t
a rta ddress $E7 SCALEv al
ue(0=2 5 6)
$6B,$6C BAS ICa rrays tarta ddre s
s $E8,$E9 Ad dre
ssofs t
artofshapeta
ble
$6D,$6E BAS ICv ariablese nda ddr e
ss $EA DRAW/XDRAWc oll
is
ionc ounte
r
$6F,$70 BAS ICstr i
ngda tas tartaddr e
ss $F1 SPEEDv al
ue(subtracte
df r
om 256)
$73,$74 BAS ICHIMEMa ddr e s
s+1 $F3 TextORma skforSa shi
ngt e
xt
$75,$76 BAS ICc urr e
ntl inenumbe r $F47$F8 ONERRp ointer
s
$77,$78 BAS IClinenumbe rwhe reEND $F9 ROTvalue
orSTOPorBREAKoc curred $FF UsedbySTR$f unct
ion
Appe
ndi
xF:
Zer
o-Pa
geMe
mor
yUs
age 4
19

Me
mor
yUs
ageTa
ble
⇢i sta
blecomesf
rom t
hecomp.sys.apple2FAQ.
⇢einfor
mati
onisdra
wn
fr
om theAppleI Ite
chni
calmanua l
s,Beneat
hAp peDOS(
l DonWor t
ha nd
Pie
terLec
hner)
,andExpl
ori
ngAppleGS/OSa ndProDOS8(
Ga r
yB.Li
tt
le
).

Low Nibble of Address


Hi| 0 1 2 3 4 5 6 7 8 9 A B C D E F
0 | AP AP A A A A − − − − A A A A A A
1|A A A A A A A A A − A A A A − *
2 | M M M M M M MA3 MA3 M M M3 M3 MA3 MA3 M3 MA3
3 | M M M M M M3 M3B M3B M3B M3B MP MP MP MP M3P M3P
4 | M3P M3P M3P M3P M3P M3P M3P M3P M3P MP I3P I3P I3P I3P MP M
5 | MA MA MA MA MA MAI AI AI AI AI AI AI AI AI AI AI
6 | AI AI AI AI AI AI AI AI3 AI3 AI3 AI3 AI AI AI AI AI3
7 | AI3 AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI
8 | AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI
9 | AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI
A | AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI3
B | AI3 AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI
C | AI AI AI AI AI AI AI AI AI AI AI3 AI3 AI3 AI3 I I
D | AI AI AI AI AI AI I I AI3 AI AI AI AI AI AI AI
E|A A A − A A A A A A A − − − − −
F|A A A A A A A A A A − − − − − −

M Moni tor;
* usedine arl
yApple//
eROMs ,nowfree
A Apples o4BAS I
C
I Inte
ge rBAS IC
3 DOS3 .3
P ProDOS( $407$4Ei
ssav
edbefor
eandr e
store
da4eruse
)
B ProDOSBASIC.SYSTEM( al
sousesa
llApples
o4loc
ati
ons)
⇥ Free;notus e
d

]
Ap
pendi
xG:
Beg
inne
r’sGui
det
oMe
rli
n
⇢i sse
c t
ionisadaptedfrom T. Peter
sen’s“Begi
nner’sGui det oUsingMe r
-
l
in,
”i nt heMe r
linProUs er’
sMa nua ,Rog
l erWa gnerPubl ishing,19 8
4 .⇢e
i
nstructi
onss houldworkonbot htheor igi
nalMe rl
inMa c r
oAs s
emb l
era ndthe
MerlinProMa cr
oAssemb l
er.
⇢epur poseofa na s
sembleri stot ransl
atehuma n-re
a dablecodei nto
machineins t
ructi
onswhi chthe ncanbee xecutedbyt hec omput er.For6 502
ass
embl ylanguagethec odec onsi
stsofas erie
soft hre
e -
lett
erc omma nds(the

opc odes”
)a l
ongwi t
ht hei
ra s
s oc
iat
edda ta(t
he“ opera
nds ”)
.Wi t
ha nassembler
s
uc ha sMerlny
i ouc a
na l
sous eoptionalla
be l
sandma c r
ost oma keyourc ode
eas
iertoreada nddebug.

Cont
rol
Mode
s
Me rl
inha st woma i
nmode sofope ra
tion:Ex ec
utiveCont rolModea ndEdi-
torCont rolMode .
⇢eEx ecutiveCont rolModei st hema inme nuwhi cha ppearswhe nyou
startthepr ogram( seet hei magebe low) .⇢epr ompti sindicate
dbyt he“ %”
character.⇢eEx ec
utiveCont rolModel etsy oupe rfor
m di ska c
tionss uchas
l
oa dinga nds avingsourcec odeorobj ectcode ,quit
tingt oBAS IC,ors witchi
ng
totheEdi tor/
As sembler.
⇢eEdi t
orCont rolModec ons i
st soft heEdi t
or ,theAs sembler,a ndthe
Link e.⇢epr
r ompti sindicate
dbyt he“ :”cha ract
er.⇢eEdi torCont rolMode
l
e t
sy oue ntera ndmodi fyc ode,deFnema cros,ass
embl ey ourcode,a ndl i
nkin
externalFles.

MERLIN-PRO 2.43
By Glen Bredon

C :Catalog
L :Load source
S :Save source
A :Append file
R :Read text file
W :Write text file
D :Drive change
E :Enter ED/ASM
O :Save object code
Q :Quit
Source: A$0901,L$0000
Drive: 2
%
Appe
ndi
xG:
Beg
inne
r’sGui
det
oMe
rli
n 4
21

Ge
tti
ngSt
art
ed
Asdi
scus
sedi
ncha
pte
rthr
ee,a
sse
mbl
ycodet
ypi
cal
lyha
sthef
oll
owi
ng
f
orm:
7 START JSR BELL ; RING THE BELL

Eachl inec onsist


sofs everalFe lds:t
hel i
nenumbe r ,an optionalla
bel
(START),thecomma nd( JSR),theopera nd(usi
ngalabelsuc
ha sBELLoranum-
bersucha s$FBDD),anda nopt i
onalcomme nt
.InMe rl
in,t
hel i
nenumbe rsare
addeda utoma t
ical
lya ndc annotbee dited.Whenlist
ingora ss
embl i
ngt hepro-
gram, a
llofthoseFe l
dsa reseparatedbyt abchar
acte
rstoproduceni c
elyformat-
ted output.Whe ni nputti
ngc ode,y ou needtypeonl yas ingle<SPACE> to
advancefrom oneFe ldtot hene xt
–y oudonotne e
dtoinsertta
bsy oursel
f.
Tog etstart
ed,trycr e
atingas hortprogramthatwillmakey ourAppl ebeep
once:
1. Booty ourMe rl
inorMe rli
nPr odisk .
2. A4e
rthemainExe
cuti
veCont
rolModeme
nua
ppe
ars
,ty
pe“
E”a
tthe“
%”
pr
ompttoent
ert
heEdi
torCont
rol
Mode.
3. Toe nteranew program,a tt
he“ :
”promptt ype“ A”(forAdd)andpr e
ss
<RETURN>.Yo ushoul
ds eea“ 1 ”appea
ra ndt hecur
s orshouldbeplac
edone
spacetother i
ghtofthatl
inenumbe r.Asy oue nt
ercodethelinenumberswil
l
advancea ut
oma t
ic
all
y.⇢e selinenumbe r
sa reusedonlywhi l
eedit
ingcodein
theEditorandarenotpartofyouractua
lprogram.
4. Onl i
ne1 ,hit<CTRL>P.Alineofaster
isk
ss houldappea
r.Anast
eri
skasthe
Fr
stchara
cterindicat
esac omme ntli
ne.Any t
hinga 4ert
heFrsta
ste
ris
kwillbe
i
gnor
e dbythea s
sembler.Hit<RETURN>toaccepttheli
neandadva
ncetoli
ne2.
5
. Onl i
ne2 ,ty
peas ingl
e<SPACE>a ndthe nhit<CTRL>P.Youshoul
dnows ee
a
na st
eri
ska teit
herendoft heline .Spa
c eov e
raf ewc hara
cte
rsandthe
nt ype

DEMO PROGRAM 1”.Hit<RETURN>t oaccepttheli
ne.Youdonotne e
dtohavethe
c
ursoratthee ndoft hel i
newhe ny ouhi t<RETURN>–t heenti
reli
newillbe
a
ccept
ed,regardl
essofwherethec ursori
sl ocat
ed.
6
. Onl i
ne3,ag
ainhit<CTRL>Pandthen<RETURN>toFni
shma
kingani
cel
y
f
orma
tte
dboxofas
ter
is
k scont
aini
ngyourpr
ogram name.
7. Onli
ne4,t
ypeasi
ngleast
eri
skandhit<RETURN>.From t
hispoi
nton,
itwi
ll
bea
ssumedt
hatyouhi
t<RETURN>tocomple
teeachline.
8. Onl i
ne5 ,hitt
he<SPACE>baronc
etoadvanc
etothecommandFe l
d,ty
pe
ORG,hi
t<SPACE>a g
aintoadvanc
etotheope
randFel
d,the
nt y
pe$8000.Sofar
yourpr
ograms houl
dlookli
kethi
s:

]
4
22 As
sembl
yLi
nes

1 ********************************
2* DEMO PROGRAM 1 *
3 ********************************
4*
5 ORG $8000

⇢ eORG deFnestheor
igi
n,t
heme
mor
yloc
ati
onf
rom whi
cht
hepr
ogr
ami
s
de
sig
nedt orun.

Quickti
p:Ifyouma k
eami sta
ke,don’tpani
c.Hit<RETURN>onablankli
neto
ex
itfr
om Addmode .Type“L”toli
styourprogr
am. Type“Dn”t ode
let
elinen.
Type“A”tore
- e
nte
rEditorContr
olModea ndaddtoy ourcur
rentpr
ogram.

9
. Nowwewi
ll
useourFr
stl
abe
l.Ty
pe:
BELL<SPACE>EQU<SPACE>$FBDD

⇢i sde FnesthelabelBELLtobee qualtothehe xval


ueFBDD.Whe r
everyouuse
thelabe l
BELLi nane xpre
ssi
on,theas s
embl e
rwi l
lautomat
ic
all
yreplac
eitwi t
h
$FBDD.Wh ynots implyusethea ddress$FBDDe v
erywher
e?We l
l,usi
ngal abel
ma kesthec odeeasi
ert or
eadanda ls
oma kesiteas
iertoc
hangethelocat
ionin
thefuture.
10.Nowwene edtoringourbel
l.Onl i
ne7 ,t
ype:
START<SPACE>JSR<SPACE>BELL<SPACE>; RING THE BELL

Noti
cet
hatwest
art
edourcomme ntwit
has e
micol
on.
Anycha
ract
erswit
hinthe
commentFel
dwil
lbei
gnored;us
ingthese
micol
onjus
tmak
esitc
leart
hatthi
sis
acomment.
11.We’r
ealmos
tdone!Online8,t
ype:
DONE<SPACE>RTS

1
2.Online9pre
ss<RETURN>toe xi
tfr
om Addmode.Beca
useli
ne9wase
mpt
y,
i
twi
ll
notbeadde
dtoy ourprogram.
Type“L”t
ogetali
stingofyourprogr
am.Its
houl
dlookli
kethi
s:
1 ********************************
2* DEMO PROGRAM 1 *
3 ********************************
4*
5 ORG $8000
6 BELL EQU $FBDD
7 START JSR BELL ; RING THE BELL
8 DONE RTS
Appe
ndi
xG:
Beg
inne
r’sGui
det
oMe
rli
n 4
23

Notethateac
hs t
ringofcha r
ac t
ersha
sbeenmov
edt ot
hec
orr
ectFe
ld:
l
abe
ls,commands,oper
a nds
,andc omme nt
s.I
nsummary
,whena
ddi
ngcode,
s
paceoncetoadv
ancetothenextFeld.

De
let
ingLi
nes
Ifyouma k
eami sta
k eornolongerneedcert
ainli
nes
,youc
andel
etel
ines
whil
eintheEdi
torControlMode.
Fore xa
mpl e
:
1. Whi l
eyouareatthe“ :
”pr
ompt ,t
ypeD6<RETURN>.
Nothi
ngcha
nge
sont he
sc
reen.
2
. Type“L”tol
is
tyourprogr
am. Not
ic
etha
ttheor
igi
nal
line6(
wit
htheBELL)
i
snowgoneandt
heremaini
ngline
shavemovedup.
3
. Ty
peD5,6<RETURN>t
ode
let
ether
ang
eofl
ine
sfr
om 5
76.
4
. Ty pi
ng“L”r
eve
alst
hatourpoorpr
ogr
am nowha
sonl
yonel
ineofc
odel
e4,
j
ustt
heRTS.

Cauti
on:Noti
c etha
ttheautomati
crenumberi
ngca
usedthelinenumbe r
sto
shi
4upwa r
d.Ifyouinte
ndtodelet
ese
v e
rall
ine
sinsuc
ces
sion,besuretos
tar
t
bydel
eti
ngthehighes
tdesi
redl
inenumberandwork
ingbackwardstothel
ow-
est
.

I
nse
rti
ngLi
nes
Wenownee
dtores
toreourdelet
edli
nes
.
1
. Atthe“:
”prompt
,typeI5<RETURN>toi
nse
rtne
wli
ness
tar
ti
ngj
ustbe
for
e
l
ine5
.
2. Typeourmi
ssi
ngt
hre
eli
nes
,ma
kings
uret
ous
espa
cest
ose
par
atet
he
Fe
lds
:

ORG $8000
BELL EQU $FBDD
START JSR BELL ; RING THE BELL

3. Agai
n,hi
t<RETURN> ont
hene
xte
mpt
yli
net
ore
tur
ntoEdi
torCont
rol
Mode.
4
. Ty
pe“
L”t
oconFr
mtha
tthec
odeha
sbe
enr
est
ore
d.

]
4
24 As
sembl
yLi
nes

Edi
ti
ngLi
nes
Whileedit
ingal i
ney oucanus
ecert
ainkey
boa
rdshor
tcutst
oins
ertor
del
et
echarac
ter
s.Trythis
:
1. Atthe“:”prompt,type“E8
”toedi
tli
ne8.Li
ne8shoul
da ppe
arwi
ththe
cur
sorove
rtheDi nDONE.
2. Pr
ess<CTRL>Dt
ode
let
ethec
har
act
erunde
rthec
urs
or.
Pre
ss<CTRL>Dt
hre
e
moret
imes.
3. Hi t<RETURN>t
oac
c e
ptthechangesandFni
shedi
ti
ngtheli
ne.Ty
pe“L”to
li
styourprogramandconFr
mt hatthela
stli
nenowhasjus
ttheRTScommand
butnolabel.
4. Ty pe“E8 ”t
or e-
edi
tline8. Now, pre
ss<CTRL>It og oi
ntoinsertmode .
Ty pe
thewor dDONEa ndpress<RETURN>. Wha tdoy out hi
nkha ppe
nsi fyouforgetto
pres
s<CTRL>I?I fyou’
dlike,goba c
ka ndr epea
tsteps174butskipt he<CTRL>I.
Notic
et hatwhenwedi d“ E8”a ndFni s
hede dit
ingourl i
ne,wer et
urnedtoEdi -
torCont r
olMode .Youc ana l
sot ypear angeofl ine
s,suchas“ E3,6”
.⇢i swi l
l
cal
lupe achlinefrom 376ins uccess
ion.Pr e
ssi
ng<RETURN>wi lltakeyoutot he
nextl
inetoe dit
,unti
lyou’vereachedthee ndofy ourrange.

Ti
p:Ifyouhavecomple
tel
ybotc
hedyourl
ine
,youcanpres
s<CTRL>Ct
oca
nce
l
t
hechangesf
orthecur
rentl
inea
ndre
turnt
oEditorCont
rolMode.

As
sembl
ingt
heCode
⇢ene
xts
tepi
stoa
sse
mbl
eandr
unourc
ode
.Att
he“
:”pr
ompt
,ty
pe
ASM<RETURN>.
Ony
ours
cre
ens
houl
dappe
art
hef
oll
owi
ng:
UPDATE SOURCE (Y/N)?

Ty
pe“
N”a
ndy
ous
houl
dthe
nse
e:
1 ********************************
2 * DEMO PROGRAM 1 *
3 ********************************
4 *
5 ORG $8000
6 BELL EQU $FBDD
8000: 20 DD FB 7 START JSR BELL
8003: 60 8 DONE RTS

--End assembly, 4 bytes, Errors: 0

Symbol table - alphabetical order:


BELL =$FBDD ? DONE =$8003 ? START =$8000
Symbol table - numerical order:
? START =$8000 ? DONE =$8003 BELL =$FBDD
Appe
ndi
xG:
Beg
inne
r’sGui
det
oMe
rli
n 4
25

Ifthes yst
e m beepsa nddisplaysa nerrorme ssage,re
me mbe rthel i
nenum-
bertha twa sre fer
enceda ndpre ss<RETURN>unt i
lthea ss
embl ycompl etes.⇢e n
goba ckt hroug hy ourprograma ndc ompa reitwiththel i
sti
nga bov e.Us eyour
new-founde ditingskil
lstocorr
e cttheline,thenre-ass
e mblebyt ypingASM.
Tot hel e4oft heli
nenumbe rswenows eethea ss
embl edma chinec ode.For
exampl e,theJSRBELLha sbeenc onve r
tedto20DDFB, wherethe20i st hehexa-
deci
ma lcodef orJSR andDDFBi stheBELLa ddressinr evers
eby teor der.⇢e
nextlinec ontainsas i
ng l
eopcode ,the60f ortheRTS, toret
ur nfrom ours ubrou-
ti
ne.Not i
cet ha tnoneoft hela bel
sorc omme ntsarewi t
hint hema chine-l
an-
guagec odeon t hel e4-hand s i
de.Fi nall
y,wes e
et hatt hec odeha sbe en
ass
embl e data ddress$8000,aswei nstructedwi t
ht heORGc omma nd.

Sa
vinga
ndRunni
ngYourPr
ogr
am
Ass
umingthatyourcodea s
semble
dwi t
hnoe r
rors
,y oucannows a
v eand
r
unyourprogr
am.
1
. Att he“:
”prompt,t
ype“ Q”toret
urntotheExecut
iveControlMode.Your
s
ourcec
odeandobjectc
odea res
afei
nme mory.I
fyouwish,youcouldr
eturnto
t
heEdit
orandconti
nueedit
ingyourcode.
2. Atthe“
%”pr
ompt
,hi
t“S
”tosaveyoursour
cecode.
TypeaFlena
mes uc
ha s
⇢eFl
DEMO1. ewi
ll
bes
ave
dwith“
.S”appendedt
oindic
atet
hati
tisasour
ceFle
.
3. Nowhi t“O”tosa
veyourobje
ctcode.Merli
nwil
ldi
spla
ythesmeFl
a e
name
witha“?
”attheend.
Hit“Y”toacce
ptDEMO1a st
heobj
ectFl
ename.Bec
auset
he
sourc
ecodeFlehadt
he“.
S”appendedtoi
t,thetwoFl
eswil
l onSi
notc c
t.
4
. Nowhi t“Q”toqui
tMe r
lin.Ty
peCATALOGtover
if
ythatyourprogr
am was
s
ave ⇢e
d. nty
peBRUNDEMO1t
or unyourpr
ogram.Youshoul
dhe a
raBEEP!
Cong
rat
ulat
ions
!You’
vejustwr
itt
enyourFrs
t6502ass
embly-l
angua
gepro-
g
ram!

]
Li
sto
fPr
ogr
ams
AL0
3-SAMPLEPROGRAM. ..
..
...
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
..
.15 AL16-
POINTERS ETUPROUTI NE..
...
..
..
..
..
.
..
..
..
.
..
..
149
AL0
3-TESTPROGRAM 1 ..
..
..
..
..
..
...
.
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.19 AL17-
INTEGERVARI ABLEREADER. .
..
..
..
.
..
..
..
.
..
..
156
AL0
4-LOOPPROGRAM 1 .
...
.
...
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
.
..
.25 AL17-
REALVARI ABLEREADER. .
..
.
...
..
..
..
..
..
.
..
..
..
.
..
158
AL0
5-LOOPPROGRAM 2 .
...
.
...
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
.
..
.27 AL17-
STRINGVARI ABLEREADER. ..
..
..
..
..
.
..
..
..
..
.
..
159
AL0
5-LOOPPROGRAM 2 A..
...
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
.
..
.28 AL17-
INTEGERVARI ABLES ENDER. .
.
..
..
..
..
..
..
.
..
..
162
AL0
5-LOOPPROGRAM 2 B..
..
..
..
...
.
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.30 AL17-
REALVARI ABLES ENDER. ..
..
...
..
..
..
..
.
..
..
..
.
..
..
163
AL0
5-LOOPPROGRAM 3 .
...
.
...
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
.
..
.31 AL17-
STR$VARI ABLES ENDER. .
..
..
...
..
..
..
..
.
..
..
..
.
..
..
164
AL0
5-PADDLEPROGRAM 1 .
..
..
..
..
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.32 AL18-
HIRESDEMO1 ..
.
..
...
..
..
.
..
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
.
..
..
169
AL0
5-PADDLEPROGRAM 1 A..
..
..
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.34 AL18-
BALL..
...
..
..
.
...
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.
..
...
..
..
..
..
.
..
..
..
.
..
..
175
AL0
6-PADDLEPROGRAM 2 A..
..
..
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.39 AL19-
HIRESDOT. ..
..
..
..
..
..
..
..
..
..
..
..
..
..
.
..
...
..
..
..
..
.
..
..
..
.
..
..
177
AL0
6-KEYBOARDPROGRAM 1 A...
..
..
..
..
..
.
..
..
..
..
..
.
..
.42 AL19-
HIRESONEDOTPROGRAM. ..
..
..
..
..
.
..
..
..
.
..
..
179
AL0
6-KEYBOARDPROGRAM 1 B..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.43 AL19-
HIRESLOTSDOTS .
.
..
..
..
..
..
..
..
.
..
...
..
..
..
..
..
.
..
..
..
.
..
184
AL0
7-SAMPLEDATAPROGRAM. ..
..
..
..
..
..
..
.
..
..
..
..
.
..
.48 AL20-
HIRESBAS EADDRES S.
..
..
..
..
..
.
...
..
..
..
..
..
.
..
..
..
.
..
190
AL0
7-SCREENCLEARPROGRAM 1 A..
..
..
..
.
..
..
..
..
..
.
.50 AL21-
HIRESPLOT. 140.
..
...
..
..
..
..
.
...
.
..
..
...
..
..
..
..
.
..
..
..
.
..
..
201
AL0
7-SCREENCLEARPROGRAM 1 B..
.
..
..
..
..
..
.
..
..
..
.
.51 AL21-
HIRESPLOT. 560.
..
...
..
..
..
..
.
...
.
..
..
...
..
..
..
..
.
..
..
..
.
..
..
204
AL0
8-SOUNDROUTI NE2 .
..
..
..
..
...
.
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.56 AL21-
PLOTLI NES .
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.
..
...
..
..
..
..
.
..
..
..
.
..
..
206
AL0
8-SOUNDROUTI NE3 .
..
..
..
..
...
.
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.56 AL22-
HIRESPLOT. 140+...
.
..
..
..
..
..
..
..
..
.
...
..
..
..
..
..
.
..
..
..
.
..
210
AL0
8-SOUNDROUTI NE4 .
..
..
..
..
...
.
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.58 AL22-
HIRESPLOT. 560+...
.
..
..
..
..
..
..
..
..
.
...
..
..
..
..
..
.
..
..
..
.
..
212
AL0
8-SOUNDROUTI NE5 .
..
..
..
..
...
.
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.59 AL22-
HIRESPLOT. 560W. .
..
..
..
..
..
..
.
..
..
...
..
..
..
..
.
..
..
..
..
.
..
214
AL0
9-BYTEDISPLAYPROGRAM 1 .
...
.
..
..
..
..
..
..
.
..
..
..
.
.61 AL22-
PLOTLI NES .
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.
..
...
..
..
..
..
.
..
..
..
.
..
..
217
AL0
9-BYTEDISPLAYPROGRAM 2 .
...
.
..
..
..
..
..
..
.
..
..
..
.
.63 AL23-
HI-RESS CRNFNCTN. ..
..
..
..
..
..
.
...
..
..
..
..
..
.
..
..
..
.
..
219
AL1
0-ADCS AMPLEPROGRAM 1 .
..
..
..
..
..
..
..
.
..
..
..
..
..
.
.67 AL24-
SCANNER- XDRAW, XDRAW. ..
..
..
..
.
..
..
..
..
.
..
..
229
AL1
0-ADCS AMPLEPROGRAM 2 .
..
..
..
..
..
..
..
.
..
..
..
..
..
.
.67 AL24-
SCANNER- DRAW, XDRAW. ..
...
..
..
..
..
.
..
..
..
.
..
..
232
AL1
0-ADCS AMPLEPROGRAM 3 .
..
..
..
..
..
..
..
.
..
..
..
..
..
.
.68 AL25-
SIMPLENOI S EROUTI NE..
..
..
...
..
..
..
..
.
..
..
..
.
..
..
235
AL1
0-ADCS AMPLEPROGRAM 4 .
..
..
..
..
..
..
..
.
..
..
..
..
..
.
.68 AL25-
SIMPLENOI S EROUTI NE2 .
..
...
..
..
..
..
.
..
..
..
.
..
..
237
AL1
0-ADCS AMPLEPROGRAM 5 A..
..
..
..
..
..
.
..
..
..
..
..
.
.69 AL25-
SIMPLERAMPNOI SEROUTI NE. .
.
..
..
..
..
..
.
..
240
AL1
0-ADCS AMPLEPROGRAM 5 B..
..
..
.
..
..
..
..
..
.
..
..
..
.
.70 AL25-
SIMPLEEXPLOS IONROUTI NE. .
.
..
..
..
..
..
.
..
..
241
AL1
0-ADCS AMPLEPROGRAM 5 C..
..
..
..
..
..
.
..
..
..
..
..
.
.71 AL25-
SHOOTERPROGRAM. .
..
..
..
..
..
.
...
..
..
..
..
..
.
..
..
..
.
..
245
AL1
0-ADCS AMPLEPROGRAM 5 D..
..
..
..
..
..
..
.
..
..
..
..
.
.71 AL26-
BASICTOFAC. ..
..
.
...
..
..
..
..
.
...
.
..
..
...
..
..
..
..
.
..
..
..
.
..
..
256
AL1
0-SBCSAMPLEPROGRAM 6 .
...
.
...
.
..
..
..
..
..
.
..
..
..
..
.
.72 AL26-
FACTOMEMORY. .
..
..
..
..
.
...
.
..
..
...
..
..
..
..
.
..
..
..
.
..
..
257
AL1
0-BPLKEYTESTPROGRAM 1 .
..
..
..
..
..
..
..
.
..
..
..
..
..
.
.75 AL26-
MEMORYTOFAC. .
..
..
..
..
.
...
.
..
..
...
..
..
..
..
.
..
..
..
.
..
..
258
AL1
0-BPLKEYTESTPROGRAM 2 .
..
..
..
..
..
..
..
.
..
..
..
..
..
.
.75 AL26-
FACTOBAS IC...
..
.
...
..
..
..
..
.
...
.
..
..
...
..
..
..
..
.
..
..
..
.
..
..
259
AL1
0-BPLBUTTONTES T..
..
..
..
..
..
..
...
.
...
.
..
..
..
..
..
.
..
..
..
.
..
.76 AL26-
BASIC.FAC. MEM. FAC. BAS..
..
...
..
..
..
..
.
..
..
..
.
..
..
260
AL1
1-GENERALPURPOS ERWTS .
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.83 AL26-
BASIC.FAC. MEM. FAC. BASUS R..
.
..
..
..
..
..
..
.
..
261
AL1
2-BINARYFUNCTI ONDI SPLAY. ..
..
..
..
.
..
..
..
..
.
..
.99 AL27-
M.L.ADDI TI ONS UBR1 .
...
.
..
..
...
..
..
..
..
.
..
..
..
..
.
..
266
AL1
3-DATA-TYPEPRI NT1 .
...
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.105 AL27-
M.L.ADDI TI ONS UBR2 .
...
.
..
..
...
..
..
..
..
.
..
..
..
..
.
..
268
AL1
3-SPECI
ALPRINT2 .
.
..
...
.
...
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.107 AL27-
M.L.ADDI TI ONS UBR3 .
...
.
..
..
...
..
..
..
..
.
..
..
..
..
.
..
269
AL1
3-INPUTROUTI NEFORBI NARY. ..
.
..
..
..
..
..
..
.
.108 AL28-
BCDDEMOROUTI NE1 ..
..
..
.
..
...
..
..
..
..
..
.
..
..
..
.
..
272
AL1
3-INPUTROUTI NEFPBAS I
C. .
.
...
.
..
..
..
..
..
.
..
..
..
.
.110 AL28-
BCDDEMOROUTI NE2 ..
..
..
.
..
...
..
..
..
..
..
.
..
..
..
.
..
273
AL1
4-NAMEFI LEDEMOPROGRAM. .
..
.
..
..
..
..
..
..
.
.113 AL28-
BCDDEMO' INC' ROUTI NE..
...
..
..
..
..
..
.
..
..
..
.
..
274
AL1
4-NAMEFI LEDEMOPROGRAM 2 .
.
..
..
..
..
..
..
.
.120 AL28-
BCDDEMO' DEC' ROUTI NE....
..
..
..
..
.
..
..
..
..
.
..
274
AL1
5-NON-RELOCATABLEPRI NTDEMO. ..
..
..
.
.127 AL28-
BCDADDI TIONROUTI NE...
...
..
..
..
..
.
..
..
..
.
..
..
275
AL1
5-NON-RELOCATABLEJ MPDEMO. ..
.
..
..
..
..
.
.129 AL28-
BCDS UBTRACTROUTI NE...
...
..
..
..
..
..
.
..
..
..
.
..
275
AL1
5-RELOCATABLEJ MP1 .
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.129 AL28-
BCDPRI NTROUTI NE1 .
...
.
..
..
...
..
..
..
..
.
..
..
..
.
..
..
276
AL1
5-RELOCATABLEJ MP2 .
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.130 AL28-
BCDPRI NTROUTI NE2 .
...
.
..
..
...
..
..
..
..
.
..
..
..
.
..
..
276
AL1
5-LOCATOR1 .
..
.
...
..
..
.
..
..
..
..
..
...
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.131 AL28-
BCDPRI NTROUTI NE3 .
...
.
..
..
...
..
..
..
..
.
..
..
..
.
..
..
278
AL1
5-LOCATOR2 .
..
.
...
..
..
.
..
..
..
..
..
...
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.132 AL29-
CONTROLCHARACTERDI SPLAY...
..
..
.
..
..
286
AL1
5-RELOCATABLEPRI NT1 ..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.133 AL29-
SPECIALDI SPLAYROUTI NE. .
..
..
.
..
..
..
..
..
.
..
..
287
AL1
5-NON-RELOCATABLEJ SRDEMO. ..
..
..
..
..
..
.
.134 AL30-
SIMPLECAS ECONVERTER. .
...
..
..
..
..
..
.
..
..
..
.
..
295
AL1
5-RELOCATABLEJ SRS I
MULATI ON. .
.
..
..
..
..
.
.135 AL30-
LOWERCAS EI NPUTROUTI NE. .
..
..
.
..
..
..
..
.
..
297
AL1
5-RELOCATABLEPRI NT2 ..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.136 AL31-
CHARACTERGENERATOR. .
...
..
..
..
..
.
..
..
..
.
..
..
305
AL1
5-RELOCATABLEPRI NT3 ..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.138 AL31-
ASCIICHARACTERS ET..
..
..
..
.
...
..
..
..
..
..
.
..
..
..
.
..
310
AL1
5-RELOCATABLEJ MP3 .
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.139 AL32-
CHARACTEREDI TOR. ..
..
..
..
.
..
...
..
..
..
..
.
..
..
..
..
.
..
314
AL1
6-SOUNDROUTI NE3 A..
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
.
.144 ASS
EMBLYLI NESCONTES TWI NNER. ..
..
..
..
..
..
.
..
340
AL1
6-SOUNDROUTI NE3 B..
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
.148
Di
rec
tor
yLi
st
ingf
orPr
ogr
amDi
sks
⇢epr og
ramsareconta
ine
dont woSoppydi
sks
.ForProDOStheFl
esarec
ontai
nedina
f
olde
rname dCODE.⇢e.Ss uWxindi
cate
saMerl
insourc
eFl
e.⇢e.As uWxindi
cat
esan
Appl
eso4BASICFle.Nameswit
houtasuWxar
ecompile
dobj
ectFl
es.
/
ALDI
SK1/
CODE /
ALDI
SK2/
CODE
AL03.SAMPLE.S AL11.RWTS.S AL18.BALL.A AL27.MLADD.A
AL03.TEST1 AL12.OPERATOR AL18.HIRES1 AL27.MLADD1.S
AL03.TEST1.S AL12.OPERATOR.A AL18.HIRES1.S AL27.MLADD2.S
AL04.LOOP1.S AL12.OPERATOR.S AL19.HIRESDOT AL27.MLADD3
AL05.LOOP2.S AL13.INPUTBIN.S AL19.HIRESDOT.A AL27.MLADD3.S
AL05.LOOP2A.S AL13.INPUTFP AL19.HIRESDOT.S AL28.BCDADD.S
AL05.LOOP2B.S AL13.INPUTFP.A AL19.LOTSDOTS AL28.BCDDEC.S
AL05.LOOP3.S AL13.INPUTFP.S AL19.LOTSDOTS.S AL28.BCDDEMO1.S
AL05.PADDLE1.S AL13.PRINT1.S AL20.HGRADDR AL28.BCDDEMO2.S
AL05.PADDLE1A.S AL13.PRINT2.S AL20.HGRADDR.S AL28.BCDINC.S
AL06.KBRD1A AL14.FILE1 AL21.PLOT140 AL28.BCDPRNT1.S
AL06.KBRD1A.S AL14.FILE1.S AL21.PLOT140.S AL28.BCDPRNT2.S
AL06.KBRD1B AL14.FILE2 AL21.PLOT560 AL28.BCDPRNT3.S
AL06.KBRD1B.S AL14.FILE2.S AL21.PLOT560.S AL28.BCDSUB.S
AL06.PADDLE2A AL15.LOCATE1.S AL21.PLOTLINE.A AL29.CTRLCHAR
AL06.PADDLE2A.S AL15.LOCATE2.S AL22.PLOT140 AL29.CTRLCHAR.S
AL07.HGR AL15.NRJMP.S AL22.PLOT140.S AL29.DISPLAY
AL07.HGR.S AL15.NRJSR.S AL22.PLOT560 AL29.DISPLAY.S
AL07.SAMPLE AL15.NRPRINT.S AL22.PLOT560.S AL30.CASECVRT
AL07.SAMPLE.S AL15.PRINT1 AL22.PLOT560W AL30.CASECVRT.A
AL07.SCREEN1A AL15.PRINT1.S AL22.PLOT560W.S AL30.CASECVRT.S
AL07.SCREEN1A.S AL15.PRINT2 AL22.PLOTLINE.A AL30.LCINPUT
AL07.SCREEN1B AL15.PRINT2.S AL23.HGRSCRN AL30.LCINPUT.A
AL07.SCREEN1B.S AL15.PRINT3 AL23.HGRSCRN.A AL30.LCINPUT.S
AL08.SOUND2 AL15.PRINT3.S AL23.HGRSCRN.S AL31.ASCII
AL08.SOUND2.S AL15.RELJMP1.S AL24.SCAN1 AL31.ASCII.S
AL08.SOUND3 AL15.RELJMP2.S AL24.SCAN1.S AL31.CHARGEN
AL08.SOUND3.A AL15.RELJMP3.S AL24.SCAN2 AL31.CHARGEN.A
AL08.SOUND3.S AL15.RELJSR.S AL24.SCAN2.S AL31.CHARGEN.S
AL08.SOUND4 AL16.POINTER AL25.EXPLODE AL32.CHAREDIT
AL08.SOUND4.S AL16.POINTER.S AL25.EXPLODE.S AL32.CHAREDIT.A
AL08.SOUND5 AL16.SOUND3A.S AL25.NOISE AL32.CHAREDIT.S
AL08.SOUND5.S AL16.SOUND3B AL25.NOISE.A AL32.PIGFONT
AL09.BYTE1 AL16.SOUND3B.A AL25.NOISE.S ALAPP.CONTEST
AL09.BYTE1.S AL16.SOUND3B.S AL25.NOISE2 ALAPP.CONTEST.S
AL09.BYTE2 AL17.READINT AL25.NOISE2.A
AL09.BYTE2.S AL17.READINT.A AL25.NOISE2.S
AL10.ADC1.S AL17.READINT.S AL25.RAMP
AL10.ADC2.S AL17.READREAL AL25.RAMP.A
AL10.ADC3.S AL17.READREAL.S AL25.RAMP.S
AL10.ADC4.S AL17.READSTR AL25.SHOOTER
AL10.ADC5A.S AL17.READSTR.S AL25.SHOOTER.S
AL10.ADC5B.S AL17.SENDINT AL26.BASICFAC.S
AL10.ADC5C.S AL17.SENDINT.A AL26.BFMFB
AL10.ADC5D.S AL17.SENDINT.S AL26.BFMFB.S
AL10.BPLKEY1.S AL17.SENDREAL AL26.BFMFBUSR
AL10.BPLKEY2.S AL17.SENDREAL.S AL26.BFMFBUSR.S
AL10.BPLPB.S AL17.SENDSTR AL26.FACBASIC.S
AL10.SBC6.S AL17.SENDSTR.S AL26.FACMEM.S
AL11.RWTS AL26.MEMFAC.S

]
I
nde
x
6 bra
nchoLs e
ts.
...
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
..
28
6
502bug.
..
..
.
..
..
..
.
..
..
.
..
..
..
..
.
..
..
.
..
..
..
..
.
..
..
.
..
..
..
.1
40,335,
370 bra
nchtest
sforr anges.
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
38
6
5C02.
..
.
...
..
.1
3,140,3
27-336,
345,356
,370,37
7-380
, bra
nch,rever
se...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
28
387-
390,394
,398-
403 bre
akme ss
ag e
...
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
.3,5,
356
BREAKs ubroutine.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
370
A BRK..
..
..
.
...
.
..
..
..
...
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
.256
,356
ABSs ubr outine .
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
408 BRKv e
ctorlocati
on..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
.356
,370
absolutea ddr essing.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
18,45,128,
328 BRUNc omma nd..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
35
Accumul ator.
...
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.6,
18 BSAVEc omma nd..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
113
ADC. ....
...
...
..
..
...
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
66,274,
344 buLer
,me mory .
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
118
additi
oni na ssembl y.
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.65,
265 BVC..
..
..
..
..
..
.
...
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.96,
357
additi
on, two- byte.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
69 BVS..
.
..
..
..
..
..
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.96,
358
addres s.
...
...
..
..
...
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..3
,4
addres singmode s .
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
.18,45,
328
C
ampe rs andv ect
or .
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
264 CALLc omma nd. .
...
..
..
...
.
...
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
6,15,1
43
AND. ..
...
..
....
.
...
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
92,278,322,
345 carr
ySa g.
...
....
....
..
...
...
..
...
.
...
..
..
..
.
..
..
..
..
..
..
.
..
38,66,90,2
73
Apple/ //.
...
..
..
..
...
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.336,
337 CATALOGc omma nd...
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.10
6
Apple/ /e.
...
..
..
...
..
...
.
..
..
..
..
13,94,291,2
96,320,335-
337 CHl ocat
ion. ...
...
...
...
...
.
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.30
5
AppleI I.
..
....
..
..
..
..
...
..
..
..
.
..
..
..
..
..
.13,
53,94,296,336,
411 CHKdi r
ectiv e...
..
...
...
..
...
.
...
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
xii
i,
16
AppleI IPl us.
..
...
..
..
..
..
..
..
.
..
..
..
..
..
.5,
13,94,291,296,
336 CHKCOM s ub rout i
ne .
..
. .
150,157,1
60,219,257,2
67
Apple so4a rrayv ari
able
s..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
154 CHKNUM s ubr out ine..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
158,163,2
19
Apple so4BAS IC...
..
..
..
..
.
..
..
..
..
..
..
..
.
..
..
5,10,11,110,
128 CHKS TRs ubr out ine .
.
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
159,1
65
Apple so4v ariabl
e s
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
151 CHRGETs ubr out i
ne .
..
..
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.14
7
ARGr egister.
..
...
.
...
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
265 CLC...
..
..
..
...
....
...
...
...
...
..
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.67,3
58
ASCdi re ctiv
e...
..
..
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
106 CLD. .
..
.
..
..
....
....
...
...
...
...
.
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
272,3
59
ASCI I...
...
...
..
..
...
..
...
..
..
.
..
..
.19,40,
94,106,305,313,
411 CLI..
..
..
..
.
...
...
....
...
....
..
...
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.35
9
ASL....
....
..
....
.
...
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
89,186,
348 CLRS CRs ubr outine ..
.
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.40
7
ASMc omma nd..
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.
..
..
16 CLRTOPs ubr out ine..
...
.
...
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.40
7
ass
e mbl e r
....
.
...
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
2,9,13,
15 CLV. .
.
..
..
..
...
....
....
..
...
...
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.36
0
ass
e mbl yl angua ge..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.2
,9 CMP. .
..
..
..
...
.....
...
...
...
..
..
...
.
...
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
.38,3
60
ast
erisk sforc omme nts.
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.17,
421 codelocat
ion, de termi ning.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.13
1
ATNs ubr outine..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
409 COLBYTEl oc a t
ion. .
..
..
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
198,211,2
13
COMBYTEs ubr out i
ne .
.
...
..
..
..
.
..
..
..
..
..
..
.
..
..
148,219,2
36
B comma ndFe l
di na ssembly..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
.17,4
21
base16numbe rs.
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.4,
22 comme ntFe ldi na ssembl y.
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
.17,4
21
base2numbe rs.
..
..
.
...
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.21,
65 compa r
ec omma nds ..
.
...
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.3
8
baseaddress...
...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
17 compleme nt s,numbe r..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.7
3
BAS Llocati
on. ..
..
..
.
...
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
.292,
302 CONUPKs ubr out ine..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.26
9
BBR. .
.
..
..
..
..
..
....
...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
331 COSs ubrout ine ..
...
...
..
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.40
9
BBS .
..
..
..
.
..
...
..
....
..
..
.
..
...
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
331 counter
s..
...
....
....
..
...
...
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..2
1
BCC. .
..
..
..
..
..
..
....
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
38,70,
349 COUTs ubr out ine .
...
..29,120,277,2
81,323,370,4
04
BCS .
..
..
..
..
..
..
..
....
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
.38,
350 COUT1s ubr out ine ..
..
..
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
281,304,4
04
BELLs ubrout ine.
..
..
..
..
..
..
..
.
..
..
..
..
.17,
135,321,405,
422 CPX. .
.
..
..
..
...
....
....
..
...
...
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.40,3
62
BELL1s ubrout ine.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
405 CPY...
..
..
..
...
....
...
...
...
...
..
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.40,3
62
BEQ. .
..
..
.
..
...
..
....
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
27,40,280,
351 CROUTs ubr out ine...
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.40
4
BinaryCode dDe cimal.
..
.264,271,
344,359,383,
384 CROUT1s ubr out ine .
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.40
4
binarynumbe rs..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
.22,
65 CSWl ocation. ...
...
...
..
..
...
.
...
..
..
.
..
..
..
..
..
.28
1,291,322,3
70
BIT..
..
..
..
..
...
.
.....
..
..
96,130,211,216,
293,299,330,
351 CURLI Nl oc ation. .
...
..
..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.12
6
bits
..
..
..
..
..
..
..
..
....
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.22,
65 CVl ocat
ion. ..
....
..
...
...
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.30
2
BKGNDs ubr outi
ne .
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
.169,
407
BLOADc omma nd...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.35,
113
D
BMI ..
..
.
..
..
...
.
....
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.75,
353 de
bug gi
ng .
.
..
..
..
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
357,
374
BNE. .
..
..
.
..
...
..
....
..
..
..
.
...
.
..
..
..
..
..
.
..
..
..
..
..
.2
4,25,40,280,
354 DEC...
.
..
...
..
..
..
.
..
...
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
23,330,
363
borrow,fors ubtracti
on..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
72 de
cimal numbe rformula.
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
22
BPL...
..
..
..
..
..
..
....
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.75,
355 de
creme nti
ng..
..
...
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
.
..
23
BRA. .
..
..
.
..
...
..
....
..
..
..
.
...
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
331 de
laysine x
ecution.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
54
branchco mma nds..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
38 de
li
mi t
er s
..
..
..
.
..
...
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
16
branchinstruc ti
on..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.25,
27 DEX...
.
..
...
..
..
.
..
..
...
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.23,
364
I
nde
x 4
29

DEY..
..
..
.
...
..
...
...
...
..
...
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.2
3,365 HEXdi rec tiv e..
...
.....
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
.48
DFBdirecti
v e..
..
...
..
..
...
.
..
...
..
.
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
48 he
xadec ima l..
...
....
....
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
.4,22,
65,271
di
rec
ti
v e
s,as sembl er.
..
..
...
..
.
...
..
.
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
17 HFINDs ubr out ine .
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.1
69,407
di
sas
sembl y.
....
.
...
..
...
..
..
..
...
.
..
...
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
10 HGRs ubr out ine ...
...
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.1
69,407
di
ske
tteha r
d- sectoring.
...
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
80 HGR2s ubr out ine .
...
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.1
69,407
di
ske
tteorga nization..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
78 hi
-r
es1 4 0-poi ntmode ..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.2
01,207
di
ske
ttesectori nterlea
ving..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
79 hi
-r
es5 6 0-poi ntmode ..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.2
03,207
di
ske
tteso4- sectoring..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
80 hi
-r
esc ollisionc ount er.
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
225
DIV10subrout ine...
..
..
..
...
.
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
409 hi
-r
esc olorma sk ...
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.1
98,211
DOS..
..
..
..
..
..
....
..
...
..
...
..
..
..
..
..
16 ,3
5,77,79,281,29
0,294 hi
-r
esc olors hi 4......
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
.192,20
3,208
DOSbe l
lmodi Fc ati
on..
...
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
88 hi
-r
esc olort a ble....
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.1
68,193
DOSbuLe rpoi nt er.
..
..
..
...
.
..
...
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
85 hi
-r
ese nt rypoi nt s...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
168
DOSc at
alogk e
ymodi Fc at
ion..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
87 hi
-r
esFl l eLe ct.
....
....
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
189
DOSdisk-vol umemodi Fcati
on..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
86 hi
-r
esme mor yma p.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
.189,30
3,417
DOSinput /
out putv ector..
.
...
..
.
..
..
..
..
..
..
..
.
.284,29
4,322 hi
-r
esobj ec tv eloc ity.
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
173
DOSIOBt able..
...
..
..
...
.
..
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.81,8
3,84 hi
-r
essc re enc oor dinate
s..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
168
DRAW c omma nd...
..
..
..
...
.
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
225 hi
-r
essc re enl oc ations.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
173
DRAW s ubrout ine..
..
..
..
...
..
.
...
..
.
..
..
..
..
169,172,22
9,408 hi
-r
essc re enmot ion...
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
173
dummyr eturna ddress.
...
..
.
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
131 hi
-r
eswhi tec olorpr oblem..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.1
95,201
hi
ghbit...
......
....
...
.....
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
.29
E hi
gh-orde rby te..
....
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
.
.11
EOR..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
.
..
.97,
365 HIMEM c omma nd. ..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.85,1
28,153
EQUdirecti
ve.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
.
..
..
..
.
..
.17,
422 HLINs ubr out ine ...
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
.169,32
2,408
ex
clus
iveOR...
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
.
..
..
.
..
..
97 HLINEs ubr out ine ..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.
..
..
..
406
EXPsubrouti
ne..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
.
..
..
.
..
408 HMAS Kl oc a tion. ..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
.199,21
1,213
ex
ponent,re
alnumbe r
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
.
..
..
.
..
254 HNDXl oc a tion. .
.....
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
197
HOMEs ubr out ine ..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
.19
F HPAGl oc ation. ....
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
197
FACr e
gister..
..
....
.
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.254,
265 HPLOTs ubr out ine .
..
..
.
..
..
..
..
..
..
.
..
..
..
..
169,182,2
21,407
FACEXPl oc ation..
..
..
..
..
..
.
..
...
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
267 HPOS Ns ubr out ine .
..
..
..
..
..
..
.
..
..
169,172,197,2
19,407
FADDs ubr outine .
..
.
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
270 HTABs ubr out ine ...
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
119
FADDHs ubr out i
ne..
..
..
..
.
..
...
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
409
FADDTs ubr outine.
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
.267,
269 I
FCOMPs ubr out i
ne.
..
...
.
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
409 i
mme diatea ddressing..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.1
8,45,328
FDIVsubr out i
ne .
..
..
..
..
..
..
.
..
...
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
270 i
mplic
itaddr e s
sing...
..
..
..
.
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
.45
Sags
,Sta
tusRe g is
ter
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
22 i
mplie
da ddr e s
sing..
...
..
.
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.45
FLASHc omma nd...
..
.
...
.
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
285 I
NC...
..
..
..
......
...
..
..
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.23
,330,367
Soat
ing-
poi nta ccumul at
or(FAC) .
..
..
.
..
..
150,236,
254 i
ncl
usiveOR. ..
.
...
..
..
...
..
..
..
.
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.97
Sowofcont rol, machinelanguage.
..
.
..
..
..
..
..
..
.
..
..
..
.
..
..
94 i
ncre
me nt ing ..
..
..
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
.23
FMULTs ubr outine.
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
270 i
ndexeda bs oluteindi r
ectaddress
ing.
..
..
..
..
..
.
..
..
..
..
.
.329
for
cedbranc hs tate
me nt.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
.129,
134 i
ndexeda ddr es
sing...
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
46,328
FOUTs ubr outine .
.
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
409 i
ndexedindi rectaddr es
sing.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
47,329
FPWRTs ubr outine.
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
409 i
ndir
ectaddr e s
sing...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
.328
FRMEVLs ubr out i
ne.
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.160,
268 i
ndir
ectinde x e
da ddr es
sing.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
46,328
FRMNUM s ubr outi
ne..
..
.150,159,2
19,256,257,
267 i
ndir
ectjump. .
..
..
...
..
...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
139,281
FSUBsubr out i
ne .
..
..
..
..
..
..
.
..
...
.
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.
..
..
.
..
270 i
nputrout ine s.
..
..
...
..
...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
.108
i
nputvector ....
.
...
..
..
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
291,370
G I
NTs ubrout ine..
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.408
GBASloc
a tion...
...
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
.197,
211 I
nteg
erBAS IC. .
..
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
5,13
GETADRs ubr outine.
..
..
..
.
..
..
..
..
..
.
..
..
.150
,159,219,
256 i
nte
gerv aria bles.
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
156,161
GETLNsubr outine..
..
..
..
.
..
..
..
..
..
..
.
..
..
.109
,118,296,
405 i
nte
rruptv e ctor..
...
.
...
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.370
GETLN1s ubr outine..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
406 i
nte
rrupts.....
...
..
..
..
..
...
..
..
..
..
..
..
..
..
356,359,37
0,381,385
GETLNZs ubr out i
ne .
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
.
..
406 I
NVERS Ec omma nd. .
..
..
.
...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
.285
GIVAYFs ubr outine..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
.163,
219 I
NVFLGl oc ati
on. .
.
...
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
95,285
GOSUBc omma nd...
..
..
..
.
..
..
..
..
..
..
.
.11,17
,136,371,
382 I
NX...
..
..
..
......
...
..
..
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
23,368
gr
aphi
cs,
ta bledr iven.
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
183 I
NY...
..
..
..
.....
....
.
...
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
23,369
I
ORES Ts ubr outine..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.410
H I
OSAVEs ubr out i
ne .
..
..
..
.
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
.410
HCLRsub
rout
ine..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
.169
,407 I
RQma ska bl einterrupt.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
359,385
HCOLORsubrouti
ne..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.169
,407 I
RQs ubrout ine..
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.370
HCOLOR1loc
a t
ion.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
.
..
198
4
30 As
sembl
yLi
nes

I
RQv
ect
orl
oca
ti
on.
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
370 NEXTCOLs ubrouti
ne .
.
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
406
NMI(non-maskableint
err
upt).
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
385
J non-
rel
oca
tabl
ec ode..
..
.
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
128
J
MP..
..
..
..
.
..
..
...
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
28,139
,281,3
70 NOP..
..
.
..
..
.
..
..
..
..
.
...
..
..
..
..
..
.
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
.54,
374
J
MPs i
mulation..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
376,38
2 numeri
creg
ist
ers,temporar
y..
.
..
..
..
..
.
..
..
..
.
..
..
..
..
.
..
..
.
..
267
J
SR.
.
...
.
..
..
..
..
.
....
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.11,1
3,63,3
71
J
SRsi
mul a
tion..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
.1
34 O
J
SRtoJMPt rick.
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
..
..
.
..
.1
64 OBJdirect
ive.
..
..
..
..
..
.
..
..
...
.
...
..
..
.
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
.xi
ii
,17
obj
ectcode.
...
..
..
.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
15
K one’
scompl ement.
..
..
..
..
..
...
.
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
73
ke
yboar
dbuLe r.
.
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
.42 opc
odes..
..
..
..
..
..
..
..
..
..
.
...
.
...
.
...
..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
.2,10
,17
ke
yboar
dinput..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
..
..
.
.42 OPENc omma nd..
.
..
..
..
..
..
...
.
..
..
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
125
ke
yboar
dinputswitc
h..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
291 ope
randFe l
di nassembly..
..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
.17,
421
ke
yboar
dstrobe.
..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
42,293 ope
rands..
..
..
..
..
..
..
.
..
..
..
..
..
..
...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.10,
421
KEYINsubrout
ine.
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
.2
93,405 ope
rati
onal mode,ma chinelanguage.
..
.
..
..
..
..
..
..
..
.
..
..
93
KSWlocat
ion..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
291,3
70,405 ORA...
.
...
..
.
...
..
..
..
..
.
..
..
..
..
..
..
...
..
.
..
...
.
..
..
..
..
..
..
.
..
.97,277
,375
ORGdi rec
tive.
..
..
..
.
..
..
..
..
..
...
..
..
.
...
..
.
..
..
..
..
..
..
.
..
..
..
..
.17,
422
L out
putroutines.
..
..
.
..
..
..
..
..
...
.
..
..
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
105
l
abelFel
di na ssembly.
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
17,42
1 out
putv e
ctor.
..
..
..
..
..
..
..
.
...
.
...
..
..
.
...
..
..
.
..
..
..
..
.
..
.94,281
,370
LANGl ocation..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
.12
6 ove
rSowSa g..
..
..
..
..
..
.
...
.
...
.
...
..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
.96,
130
LDA...
..
.
..
..
...
...
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
18,37
1
LDX...
.
..
..
..
...
...
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
18,37
2 P
LDY..
..
..
..
..
...
..
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
18,37
3 paddleinput .
...
.3 2,39 ,59,6
2, 103,
171,182,230
,249,
LI
FO( Last-I
nFi rst-
Out)..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
61,13
1 34
2
LI
NNUM l oc at
ion..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
.219,
222,25
6 paddleint eractions ..
..
...
.
..
..
..
..
..
..
..
..
..
..
.
..
..
..
.103
,172,24
9
LOGs ubroutine..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
.
.40
9 paddlepus hbut ton. .
.
...
..
..
..
..
..
.
...
..
..
.
..
..
.76,171
,249,34
2
l
ogari
thmicf orm...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
.15
3 parit
y,numbe r....
..
...
..
..
..
..
..
..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
..
.9
1
l
ogic
alope rators
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
.
..
.9
2 PHA. .
..
..
....
...
...
....
..
...
..
..
...
..
..
.
..
..
..
..
..
..
..
..
.
..
..
..
..
.6
2,136,37
6
LOMEM c omma nd.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
.15
3 PHP. .
.
..
...
...
....
...
....
..
...
..
..
..
..
..
..
..
.
...
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.37
6
l
ow-orderby te.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
.
..
.1
1 PHX. .
..
..
....
...
...
...
...
...
..
..
...
..
..
.
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
331,37
7
l
owercasetext..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
.29
1 PHY. .
..
..
...
...
....
...
...
..
...
..
...
..
..
.
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
331,37
8
LSR.
..
..
..
..
..
...
.
....
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
89,37
3 PLA...
..
..
....
...
...
....
..
...
..
..
...
..
..
.
..
..
..
..
..
..
..
..
.
..
..
..
..
.6
2,136,37
8
PLOTs ubr out ine...
..
..
...
..
..
.
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
.40
6
M PLP...
.
..
...
...
....
...
....
..
...
..
..
..
..
..
..
..
.
...
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.37
9
machinela ng uage...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
9 PLX...
..
..
....
...
...
...
...
...
..
..
...
..
..
.
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
331,37
9
MAKS TRs ubr out i
ne..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
165 PLY...
..
..
...
...
....
...
...
...
..
..
...
..
..
.
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
331,38
0
manti
ssa, real numbe r.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
254 pointe
rs( ve ctor s).
...
.
...
..
..
..
..
.
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
..
.4
7
mask,AND. .
..
...
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
93,96,292
,346 POKEc omma nd. .
...
.
...
..
..
..
..
.
..
...
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.14
4
mask,EOR. ..
..
...
..
..
...
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
366 POPc omma nd. .
...
...
.
...
..
..
..
..
.
..
...
.
..
..
..
..
..
.
..
..
..
..
.
..
..
136,37
8
mask,inve rseSa g..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
285 POS Nloc ation. ...
..
...
..
..
..
..
..
..
..
.
...
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.32
3
mask,ORA. ..
....
.
..
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
277,292
,375 post-
inde xeda ddr essing..
.
..
..
..
..
..
..
..
..
.
..
..
..
..
.4
7,329,34
4
mathsubr out ines.
...
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
263 PRBL2s ubr out ine...
..
..
..
..
..
..
..
.
...
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.40
5
MAXFI LESc omma nd..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
78 PRBLNKs ubr out i
ne .
..
..
..
..
..
..
.
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
.40
5
memor yma p....
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.3
,152 PRBYTEs ubr out ine..
...
..
..
.
..
..
..
..
..
..
..
..
..
.
..
..
..
..
.
..
..
276,40
4
memor ypa g e..
..
..
...
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
4 pre-i
ndex eda ddr essing..
..
..
..
.
...
..
..
.
..
..
..
..
..
..
.4
8,329,34
4
Merl
inAs sembl er...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
13,16,337
,420 PREADs ubr out ine..
...
..
..
..
..
..
.
...
..
..
..
.
..
..
..
..
..
.3
2,171,41
0
Mini-
As sembl er.
..
...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.1
3,19 PRERRs ubr out ine ..
.
...
..
..
..
..
.
..
...
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.41
0
mnemoni c s..
..
...
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..2
,13 PRHEXs ubr out ine..
...
..
..
..
..
.
..
...
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.40
5
Monitor..
...
...
...
..
..
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
.5,
9,13 PRNTAXs ubr out ine..
..
..
..
..
.
..
...
.
..
..
..
..
..
.
..
..
..
..
.
..
..
276,40
5
Monitors ubr outines.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
41 process
ingmode ,ma chinelanguage.
..
..
.
..
..
..
..
.
..
..
..
.
..
.9
3
MOVAFs ubr outine..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
267 ProDOS ....
...
...
....
...
..
..
...
..
..
..
100,284,286,29
4,312,32
5
movec omma nd..
....
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
54 Program Count e r.
...
.
...
..
..
..
..
..
.
...
..
..
.
..
.356,38
1,382,39
6
MOVFM s ubr outine.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.157
,258 pseudoopc ode s .
...
..
..
...
..
..
..
..
.
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
..
.1
7
MOVMFs ubr outine.
..
..
..
..
.
..
..
..
..
..
.
..
..
163,219,25
8,267 pseudo-jump. ..
....
..
...
..
...
.
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
376,38
2
MUL1 0s ubr outine .
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
409 PTRGETs ubr out ine .
..
..
...
157,161,163,21
9,260,26
7
mult
iplicationa nddi vis
ion.
..
..
..
..
..
..
.
..
..
..
..
..
90,265
,381
Munch- A- Bug ..
..
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.13
,357 R
RAM (
random a cc
essme mory)
..
..
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.4
N ra
ndom numbe rgenera
tor.
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
..
.
.293
ne
gat
ivenumber
s..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
.72,
178 RDCHARs ubrout
ine.
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
..
.
.405
NEGOPs ubr
out
ine.
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
409 RDKEYs ubrouti
ne..
.
..
..
..
..
..
.
..
..
.11
8,291,324
,370,405
I
nde
x 4
31

READc omma nd...


..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
..
125 S
TA. .
.
..
..
...
..
..
..
.
..
...
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
.
..
18,38
5
rea
ding/wr i
tingda taFles
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
113 s
tac
k .
.
..
..
...
..
..
..
.
..
...
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
.61,
62,13
1
rea
ding/wr i
tingt e
xtFles.
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
120 S
tackPointer.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
.61,1
31,39
6
rea
lvari
a bles.
...
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
158,162
,252 S
tat
usRe gist
er.
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
21,22,37,1
30,39
6
reg
ist
ers,6 502..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.6
,396 s
tri
ngv a
r i
ables
...
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.159,1
64,38
6
rel
at
ivea ddressing.
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.46
,328 S
TX...
..
..
...
.
...
.
..
..
...
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
18,38
6
rel
ocat
abl ecode .
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.127
,371 S
TY...
..
..
...
.
...
.
..
..
...
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
18,38
7
REM comma nd...
..
.
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
17 S
TZ...
..
..
..
..
..
..
..
..
...
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.3
31,38
7
RESET....
..
..
..
...
..
...
.
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
385 s
ubtrac
tionina ss
embly..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
65,26
5
RMB. .
.
..
....
..
..
..
...
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
331 s
uperhi-resgraphic
s..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
.
.19
5
RNDs ubr outine..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
..
408
ROL...
..
..
...
..
..
...
..
..
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.91
,380 T
ROM ( r
e ad-onlyme mory).
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.
..
..
.
.4 TANs ubrouti
ne .
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
409
ROR...
..
...
...
.
..
....
.
...
.
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.91
,381 TAX..
..
..
..
..
..
..
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
34,388
rot
atecomma nds..
.
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
91 TAY..
..
..
..
..
..
..
..
.
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
34,388
RTI..
.
..
..
...
...
..
..
...
.
...
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
381 t
extsc
reenme mor yma p.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.19,3
02,416
RTS..
..
..
...
..
..
..
...
..
...
.
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.11,13
,382 t
ransf
ercomma nds.
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
34
RWTSe rrorc odes.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
85 TRB..
.
...
.
..
...
.
..
..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
.331,3
35,389
RWTSs ubrout i
ne s
..
..
..
..
..
..
..
..
.78,8
1,83,359,384
,385 TSB.
..
..
..
..
.
...
..
..
.
..
...
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
.331,3
35,390
TSX.
..
..
..
..
..
..
..
..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
.131,1
32,390
S t
wo’scompl e
me nt.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.74,1
78,367
S
-CAs sembl er..
...
..
..
.
...
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
xvii
i,33
7 TXA..
..
..
..
..
..
..
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
34,391
S
AVDs ubr out ine..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.16
5 TXS.
..
..
..
..
..
..
..
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
..
392
S
BC...
...
...
....
...
...
...
..
..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
72,274,38
2 TXTPTRl ocati
on..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.1
47,257
s
cree
nout put ..
..
...
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.3
0 TYA..
..
..
..
..
..
..
..
.
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
34,393
S
CRNs ubr out i
ne ..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
219,40
7
S
EC...
...
...
....
...
...
...
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
.72,38
4 U
S
ED....
...
...
...
....
..
....
.
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.272,38
4 UCMDl oc
ation.
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
.
..
..
.
.85
S
EI..
..
...
..
....
...
....
..
...
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.38
5 USLOTloc
ation.
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
84,90
s
elf
-modi f yingc ode.
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.13
7 USRcomma nd..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
255
S
ENDs ubr out i
ne ..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.22
3
S
ETCOLs ubr out i
ne...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.40
6 V
S
ETINVs ubr out i
ne.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.40
4 ve
ctor
..
..
...
..
.
...
..
.
....
.
..
..
..
..
.
..
..
..
..
.
.35,82,
94,281,291,
370
S
ETNORM s ubr outine.
..
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.40
4 VLINEs ubroutine.
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.4
06
S
GNs ubr out ine .
..
..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.40
8 VTABs ubroutine..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.119,
302
s
hapet able s.
....
..
...
..
..
.
...
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.171,22
5 VTOC( VolumeTa bleofContents
).
..
..
..
..
.
..
..
..
..
.
..
..
..
.8
0
s
hi4ope ra tor s.
..
...
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.8
9
S
HNUM s ubr out i
ne...
..
..
.
..
..
..
..
..
..
..
..
.
..
..
..
..
169,172,40
8 W
s
ignbit..
...
....
...
...
...
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.7
3 WAITsubrout
ine.
.
..
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
172,183,4
10
s
ignSa g..
....
...
...
....
.
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
.75,9
0 wa
rm-reent
ryvec
tor.
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
.
..3
5
S
IGNs ubr out ine .
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.40
8 wr
aparoundofnumbers.
..
..
.
..
..
..
24,363-36
5,367-3
69
S
INs ubr out ine....
..
.
..
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.40
9 WRITEcomma nd.
..
..
..
.
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.1
25
S
MB. .
...
...
....
...
...
....
.
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.33
1
s
neaker, we t..
...
...
..
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.4 X
s
o4-swi t
c hha rdwa relocati
on..
..
.
..
..
..
..
..
..
..
41,343,35
3 X-
Regi
st
er.
..
..
.
...
..
.
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
6,18
,396
s
ounddur ation....
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.5
6 XDRAW comma nd..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
.
..
..
.
..
225
s
oundf rom pa ddleinput..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.5
9 XDRAW subrout
ine.
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.1
69,229,
408
s
oundg ene ration...
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.5
3
s
oundpi t
c h....
..
....
..
.
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.5
6 Y
s
oundr out i
ne s..
...
..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
.53,23
5 Y-
Reg
ist
er.
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.6
,18
,39
6
s
ourcec ode ....
..
....
.
..
..
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.1
5
S
ource r
or ...
...
...
...
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
.29,33
7 Z
s
peakers o4- s witch..
...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.5
3 z
eroSa
g..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
22-2
4,9
0
S
QRs ubr out ine...
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.40
8 z
eropa
geaddre
ssi
ng.
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
.45
,32
8
Qui
ckRe
fer
enc
e

Me
rli
nAs
sembl
er
Edit
ingComma nds
EditorCont rol Mode <CTRL>B7Be ginni ngofline
A7Ad dmode ,<RETURN>t oe xit <CTRL>CorX7Abor tEditmode
E7Ed i
ta l
lli
nes <CTRL>D7De letec ha r
acter
Emo rEm, n7Edi tlineorl i
ner ange <CTRL>Fc7F indc ha r
acte
r
E“ s
tri
ng”7Edi tli
nesc ont ainings t
ring <CTRL>I7I nse
r t;<RETURN>t oe xi
t
Im7I nsertli
ne sstartinga tm <CTRL>L7To gg lelowe rc
ase/upperc
ase
Dmo rDm, n7De l
etelineorl inerange <CTRL>N7En dofl ine
Rmo rRm, n7Re placel ineorl i
nerange <CTRL>O7I nse
r tspe ci
alcharacte
rs
L7L i
stsource <CTRL>P7F il
llinewi t
h*****
Lmo rLm, n7Li stspec iFclineorr ange <SPACE><CTRL>P7Bo r
derwi th* *
.(peri
od)7Li stf r
om pr eviousr ange <CTRL>Q7De leter estoftheline
P,Pn ,Pm, n7Li s
twi thoutl inenumbe rs <CTRL>R7Re storel inetoorigina
l
F“ s
tri
ng”7Fi ndt heg iv ens t
ring <RETURN>7Ne x tlineore xit
C“ s
tr1”“st
r2”7Cha ng es t
r1t ostr2
FW“ word”7Fi ndt heg iv enwo r
d Merl
inProFul lScr
eenEdi tor
CW“ s
tr1”“st
r2”7Cha ng es t
r1t ostr2 B/N7Got obe gi
nning/
e ndofsourc
e
EW“ word”7Edi tl
inesc ont ainingwo rd D/R7De l
eteorreplac
et hecurre
ntli
ne
COPYmTOn7Co pyl ine E7Ex c
ha nge(Fnda ndreplace)
COPYl,mTOn7Copyl ine s F7Findt ex
t
MOVEmTOn7Mo veline I7Ins
ertabl a
nkl i
ne
MOVEl,mTOn7Mov el ines L7Locatelabel,
ma rker
,orl i
nenumber
123o r$1237He x/De cima lc onvers
ion Q7Re t
ur ntoEditorContr olMode
X/V7Cl i
pboardcutandpa ste
ASM7Assembl ethesourcecode Y7S e
lectall
texttotheend
LEN7Sourcelengthandby tesremai
ning 87Createalineofaster
isks
MON7ExittotheMoni tor 97Createaboxofa ster
isks
NEW7Clearthecurrentsourcecode
PR#17Sendout puttoprinteri
ns l
ot1 PseudoOpc ode s
VAL“e
xpres
sion”7Comput ee x
press
ion l
abelEQUe x pr
ession
VID3orVID07Tur n8 0-columnson/oL l
abelKBD7De Fnel abelduringasse
mbly
Q7Qu i
tEditor,ret
urntoEx ecuti
ve ASC“ s
tring ”7De FneAS CIItext
CHK7Ad dac he cksum by te
Ex
pre
ssi
ons ERRe xpre s
sion7For ceerrorifnonzer
o
2*LABEL1-LABEL2+$231 HEXd ata7De Fnehe xda ta
1234+%10111 LSTONo rOFF7Ena ble
/disablel
is
ti
ng
“K”-“A”+1 LUP...--^7L oopa ndr epeatopcodes
“0”!LABEL(“0”EORLABEL) OBJe xpre s
sion7As sembl yaddres
s
LABEL&$7F(LABELAND$7F) ORGe xpre s
sion7Runa ddr e
ss
LABEL.$FFFF(LABELOR$FFFF) PUT? l
e na me7I nsertT.?lename
*-2(curr
entaddressminus2
) SAV? l
e na me7S avec urrentcode
DUM/DEND7Du mmys ectionofcode
DOex p
r essi
o n..
. ELSE. .
.FIN
IFcha r,]var...ELSE. ..
FIN
Moni
torComma
nds
1

Comma nd Sy
nta
x De sc ri
ption
Ent
er CALL -151 Ent ertheMoni t
orf rom BAS IC.
Dis
pla
y 300 Dis playtheby tea t$300.
300.320 Dis playtheby tesf rom $300t o$320.
S
tor
e 300:00 01 02... Stor ebytev al
ue ss tart
inga t$300.
:03 04 05 Ty pe“ :
”toc ont inuea ddi ngv al
ue s.
Mov
e 2001<2000.2FFFM Copyme mor yf rom $2000t o$2FFFi nto
l
oc ations tar
tinga t$2001.
Ve
rif
y 800<400.7FFV Dis playdiLere nce sinme mor yfrom $400
to$7FFwi thby tess tarti
nga t$800.
Ex
ami
ne <CTRL>E Dis playthe6 50 2r egis
ters.
:01 02 03 04 05 Ty pe“ :
”a ndt hene wv aluestomodi f
y.
Go 300G Runt hepr ograma t$300.
Li
st 300L Dis assemble2 0l ine s
,starti
nga t$300.
L Ty peLt oc ontinuet helist.
Addby t
es 2F+3B Addt woby t
es, displayther esult
.
Subtr
act 3B-2F Subt racttwoby tes,displayther esult
.
Norma l N Setnor ma l v
ide omode .
Inve
rse I Seti nversevide omode .
User <CTRL>Y Jumpt otheus err outinea t$3F8-3FA.
Keyboar
d 2<CTRL>K Ca us esl
ot2t obe comet heinputs ource.
Pri
nter 1<CTRL>P Ca us esl
ot1t obe comet heout putdevice
.
Exit <CTRL>C Ex i
tMoni tora nde nterBAS IC.

Addr
ess
ingMode
s
Mode Ex
ampl
e By
tes Ti
me(]s)
Impli
e d RTS 1 277
Imme diate LDA #$FF 2 2
ZeroPa ge LDA $06 2 375
ZeroPa geIndexed, X LDA $06,X 2 476
Absolute LDA $C000 3 376
AbsoluteIndexed, X LDA $2000,X 3 477
AbsoluteIndexed, Y LDA $2000,Y 3 475
Indi
rectIndexed( post-i
nde
xed) LDA ($06),Y 2 576
IndexedIndire
ct( pre-
indexe
d) LDA ($06,X) 2 6
Rela
tive BCC $300 2 274
Indi
rectJump JMP ($0036) 3 576
ZeroPa geIndi
rect[ 65C02] LDA ($06) 2 5
IndexedAbs ol
uteI ndire
ct[
65C02] JMP ($1234,X) 3 6

1[
CT]Ada
pte
dfr
om Ta
ble3
-1i
nIns
idet
heAp
ple/
/e,
byGa
ryB.
Lit
tl
e.
Aboutt
heAut
hor
Rog erWa g nerboug htoneoft hee arl
yAppl eI Icomput ersin1 97 8a ndov er
thene xtfewy earswr oteamont hlytutoria
lf orS oftak ma
l gazine .As semb l
y
Lines:TheBo ok ,publ ishedi n1982, wastheFrstbooks peciFca ll
ya boutc r
e at
ing
assembl y-l
ang ua gepr og ramsont heAppl eII.
Roger al s o wr ote
Ap pl
eI IGSMa c hineLa n-
guagef orBe g inne sa
r nd
numer ous pr ogr ams
includi ngRo ge r's Easel,
ThePr ogra mme r ’
sUt i
li
ty
Pa c
k, andThe Wr ite
Cho i
ce . He was the
designera nd c reator of
Hy perStudi ofo rt heAp p l
e
IIGS,amul t
ime diapa ck-
agetha ts oonbe c amet he
mos t
- useds o4wa rei nK-
12c l
as s
rooms .
Te chnolo y& Le
g arningma gazi
nena me dRog eroneoft het op5“ Most
Impor tantEduc ationa lTe chnologyGur usofthePa stTwoDe c ades”a longwi th
SeymourPa pe rt,Bill Ga t
e s
, S
teveWoz niak,andS teveJobs .
Rog erc oine dthephr ase“Copy rig
htFriendly,”apr edec es
s ort ot heCr eat
ive
Commonsl ic ense,a ndhewor kedwi ththeCr eati
v eCommonsor gani zat
iont o
developa utoma ti
ca ttributionsystems.
Rog erse rvesont heBoa rdofDi rect
orsofCa liforniaComput er
-Us ingEdu-
cators(CUE)I nc.,a ndi sa n
ener get ic advocat ef or
enablings t
ude nt st oe Lec-
tivel
y us et ec hnol og y,a s
we l
la sforis suesr elatingt o
digit
alc i
ti
ze nshipa nds tu-
dentpr ivacy .Fina ll
y ,Rog er
istheinv entoroft heHy per-
Dui no,a pl atfor m t hat
enabless t
ude nt st oe asi
ly
createi nter active ma ker
projectsthatc ombi nephy si
-
calha rdwar e wi th di git
a l
content.
Formor einfor ma tion, visi
thttp:/
/roger
wa gner.
com.

Rog
erWa g
nerdidn’tjus
tre
adt
he⌧r
stbookonpr
ogr
ammi
ngt
heAppl
e
c
omput
er–hewroteit
.”
–S t
eveWoz ni
ak

“Icannote xpre sshow t rul


yl uckyIwa st oha vegotte
nRog e r
’sAssembl
y
Line
s:TheBo ok .Ia m note xa
gg e
r a
tingwhe nIs aythatIme r
elyreadthe⌧rs
t
fe
wpa gesinc ha pteronea nd,allofasudden, ass
emblyl a
nguagetotal
lymade
sens
etome .Ve r yfewt i
mesinmyl i
fehaveIha dsucha nimpor t
anteve
nt
happentomei nl i
ghtningbolt
- l
ikeforma swhe nIr eadthe⌧rstchapt
erand
unders
toodwi tho uterrorexac
tlyhowi twork ed.

–J ohnRome ro,Co-f
ounde rofidS o'wa r
e,desig
nerof
Wo lfens t
ein3 D,DangerousDa v,
e Do m,
o andQua ke

Ro
gerWagner
,St
eveWoznia
k,Bo
bClar
dy
Hangg
li
ding
,Baj
aCali
for
nia,Ma
rch1
981

Roger Wa gner’
sAs se
mb ly Linesa r
ticl
esorigi
nall
ya ppe aredinS oftal
k
ma gaz
inefrom October1 980toJ une19 8
3. Now,forthe⌧r s
ttime,a l
lthirt
y-
thre
ea rt
icl
esareav
a il
ableinonec omplet
ev ol
ume. 1i seditiona l
soconta i
ns
newa ppendic
esont he6 5C02,zero-pageme moryusage ,
a ndag ui
detous ing
theMe r
li
nAs sembler.1ebooki sdesignedfors t
ude ntsofa l
la ges:the
nostal
gicprogr
amme renjoyi
ngt heretrorevol
uti
on,thene wcome rinte
rested
i
nl earni
nga ss
emblyc oding,ort hee mbedded-sy
s t
emsde ve l
operusingt he
l
a t
est65C02chipsf
r om We ster
nDe si
gnCe nte
r.

You might also like