Assembly Lines Complete Wagner
Assembly Lines Complete Wagner
Assembly Lines Complete Wagner
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
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
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
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
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
⇢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
]
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
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
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
]
2
0 As
sembl
yLi
nes
⇢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
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
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
]
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
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
⇢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
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
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
]
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
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
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
]
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
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
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
]
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
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
]
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
]
6
2 As
sembl
yLi
nes
⇢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
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:
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
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
⇢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
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
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
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
]
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
4[
CT]$FFi
snotav
ali
dDOSv
olumenumbe
r.
1
1.DOSa
ndDi
skAc
ces
s 8
5
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
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
*
*
*
⇢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
ROLLRot
ateOneBi
tLe
5 RORLRot
ateOneBi
tRi
ght
]
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
]
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
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
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
]
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.
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
]
1
02 As
sembl
yLi
nes
⇢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'
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
]
1
08 As
sembl
yLi
nes
⇢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
]
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
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
]
1
16 As
sembl
yLi
nes
]
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
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
]
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
⇢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
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
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
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
]
1
32 As
sembl
yLi
nes
Be
for
eJSR$FF58 Dur
ingJSR$FF58 A5e
rRTSf
rom $FF58
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
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
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
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
⇢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
⇢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
⇢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
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
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
FP S
imple Array S
tri
ng
Fr
ee DOS
Pr
ogra
m Var
iabl
es Var
iabl
es Data
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
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
:
]
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
]
1
58 As
sembl
yLi
nes
⇢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
⇢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
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
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
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
⇢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
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
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
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
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
⇢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
]
1
78 As
sembl
yLi
nes
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
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
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:
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
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
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
Fi
gur
e21-
1:Appl
eso5Col
orMa
sks
⇢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:
⇢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
⇢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
⇢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
]
2
08 As
sembl
yLi
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
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
⇢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
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
⇢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
]
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
⇢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
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
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
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
]
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
]
2
34 As
sembl
yLi
nes
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
⇢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
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
⇢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
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
]
2
44 As
sembl
yLi
nes
⇢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
]
2
48 As
sembl
yLi
nes
]
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
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
)
]
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 *
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
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
]
2
64 As
sembl
yLi
nes
t
hant
ous
e:
FOR I = 1 TO 100: A(I) = A(I) * 5: NEXT I
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
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
⇢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
]
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”
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
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
<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
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
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)
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
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
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
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
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
]
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
]
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
⇢ 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
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
⇢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
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
]
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
]
3
16 As
sembl
yLi
nes
]
3
18 As
sembl
yLi
nes
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
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
]
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
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
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
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
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
]
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:
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
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
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
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
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
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
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
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
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+
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
I
nputSubr
out
ine
s
]
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
Hi
-Re
sGr
aphi
csSubr
out
ine
s
]
4
08 As
sembl
yLi
nes
Fl
oat
ingPoi
ntAc
cumul
ator
]
4
10 As
sembl
yLi
nes
Ot
herSubr
out
ine
s
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).
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
).
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
]
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
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
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
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
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.