Assembly Lines Complete Wagner

You might also like

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

Assembly Lines:

The Complete Book

A Beginner’s Guide to 6502


Programming on the Apple ] [

by
Roger Wagner

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

Thisworkisma deavail
ableunderaCreati
veCommonsAt tr
ibut
ion-NonComme r
cial
-ShareAl
ik e2
.0li
cense.
You
arefre
etoshareanda daptthema t
eri
ali
na nyme di
um orformatunderthef
ollowingterms:(1)Attr
ibut
ion–You
mus tgi
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
thema t
eri
al,
youmus tdis
tri
buteyourcontr
ibuti
onsunderthesamelic
enseastheorigi
nal.Forthecomplete
l
icensese
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 otwa
nds rebr
andna
mesa
ret
rade
mar
ksofAppl
eInc
.,r
egi
st
ere
d
i
ntheU.S.
andothercountr
ies.

Thecont
ent
sofVol
ume1(chapte
rs1−15
,appe
ndicesA−E)wer
eori
gi
nal
l
ypr
int
edi
nAs
semb
lyLi
ne:TheBo
s ok
(Rog
erR.Wagne
r,ota
S lkPubl
ishi
ng,Nor
thHol
lywood,CA,1
982)
.

Thecont
ent
sofVolume2(chapter
s16−3
3)we
reor
igi
nal
l
ypr
int
edi
nSfta
o lkma
gaz
ine(
Sota
lkPubl
is
hing
,Nor
th
Holl
ywood,
CA,Januar
y19
82 −June19
83).

ThecoverimagesoftheApple][Plusa
ndthegr
eenbarcompute
rpape
rwer
ec r
eat
edbyChrisTorr
enc
e.Thecove
r
imageofRo ge
rWa gnerwasedite
dbyDougS herman.The“Usag
eChar
tof6 50
2Inst
ruc
tions”i
sadapt
edfrom
Fig
.2-1i nIns
idetheAp pl
e/ /
e,byGa ryB.Li
tt
le
,andi sus
edbypermi
ssi
on.Allot
herimagesandigure
sa r
e
©Rog e
rR. Wa gner
.

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
ureThatCounts7
2.
TheMoni
tor
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.
...
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
9
Ex
plori
ngt
heMoni
tor 9
Di
sass
embl
y1 0
3.
Ass
embl
ers
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.
...
..
..
..
..
..
13
TheMi ni
-Asse
mbler1 3
Assembler
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
TheStatusReg
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
BranchOfs e
tsandRe 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.
TheS
tac
k..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.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
TheSignBit7 3
TheSignFla
g7 5
11.
DOSa
ndDi
skAc
ces
s..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.77
TheOvervi
ew:DOS 7 7
Dis
kett
eOr ga
nizat
ion 7 8
DOSModi icat
ions 85
Dis
k-VolumeModi ica
tion 86
Cata
logKeypres
sModiic a
tion 87
Bel
lModiicati
onandDr iveAcc
ess88
12.
Shi
tOpe
rat
orsa
ndLog
ica
lOpe
rat
ors
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.89
S tOpe
hi 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
esotBASI
C..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.143
Si
mpl eI
nte
rfac
ing 144
TheInte
rnalS
truct
ureofAppl
esot 1
45
Pas
si
ngVa ri
ables1 47
17.
Mor
eAppl
esotDa
taPa
ssi
ng.
.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.151
ApplesotVariable
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
esotHi
-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
ngtheOppos i
teofaSi
gnedNumbe
r1 7
8
TheRealThing
:Hi-Resi
nAs s
embl
y1 79
Tabl
e-Driv
enGr a
phics1 83
Concl
usi
on 1 87
20.
Str
uct
ureoft
heHi
-Re
sDi
spl
aySc
ree
n..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.189
LoadingaHi-ResScre
en: t
he“Fil
l”Efe
ct1 8
9
AnotherProbl
em: S ti
hi ngColors1 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.
TheCol
li
si
onCount
er,
DRAW,
XDRAW.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
225
SomeExperiment
s2 2
5
DRAW v ers
usXDRAW 2 27
Pri
nci
plesofAnimat
ionandCol
li
si
on 2
28
TheSca
nne r2 28
ThePoss
ibil
it
ie
s2 3
4
25.
Expl
osi
onsa
ndSpe
cia
lEfe
cts
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.235
Expl
osions,Rays
,andOtherThi
ngsTha
tGoBumpi
ntheNi
ght 2
35
ALittl
eMor eSophis
ti
cat
ion 239
Putt
ingitAllToget
her2 41
TheShooterProgra
m 2 45
26.
Pas
singFl
oat
ing
-Poi
ntDa
ta.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.251
Int
ernal
izat
ionofDa ta
: I
nte
gervers
usRea
lVa r
iabl
es2
52
TheFloati
ng-PointAccumulat
or(FAC) 254
Pas
singDa t
af r
om Appl e
sottotheFAC 2 5
5
MovingtheFACt oaMe moryLocati
on 257
MovingMe mor yintotheFAC 2 58
Pas
singFACDa taBacktoApplesot 25
9
Putt
ingitAllTog e
ther2 60
Conclus
ion 2 62
Ta
bleofCont
ent
s i
x

27.
Floa
ting
-Poi
ntMa
thRout
ine
s..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.263
MoreAppleotI
s nt
ernal
s2 65
AnExampleThatDoe 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.
TheBCD,
orBi
nar
yCode
dDe
cima
l..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.271
Limi
tat
ions 273
TheCarryFl
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
TheInputVector: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
TheCharact
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.
The65C02.
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
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
rDiference
s3 35
Appe
ndi
xA:
Cont
est
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
339
x As
sembl
yLi
nes

Appe
ndi
xB:
Ass
embl
yComma
nds
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
.344
ADD 3
44 BRK 3
56 DEX 3
64 LSR 3
73 ROL 3
80 STZ 3
87
AND 3
45 BVC 3
57 DEY 3
65 NOP 3
74 ROR 3
81 TAX 3
88
ASL 3
48 BVS 3
58 EOR 3
65 ORA 3
75 RTI 3
81 TAY 3
88
BCC 3
49 CLC 3
58 INC 3
67 PHA 3
76 RTS 3
82 TRB 3
89
BCS 3
50 CLD 3
59 INX 3
68 PHP 3
76 SBC 3
82 TSB 3
90
BEQ 3
51 CLI 3
59 INY 3
69 PHX 3
77 SEC 3
84 TSX 3
90
BIT 3
51 CLV 3
60 JMP 3
70 PHY 3
78 SED 3
84 TXA 3
91
BMI 3
53 CMP 3
60 JSR 3
71 PLA 3
78 SEI 3
85 TXS 3
92
BNE 3
54 CPX 3
62 LDA 3
71 PLP 3
79 STA 3
85 TYA 3
93
BPL 3
55 CPY 3
62 LDX 3
72 PLX 3
79 STX 3
86
BRA 3
56 DEC 3
63 LDY 3
73 PLY 3
80 STY 3
87

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

InOc tobe r1 980,int hes econdi ssueofS o ftal


kma gazine ,ane ws e rie
sof
arti
clesma dei tsde but.Itstitl
ewa s“ Asse mbl yLi nes”wi tht hes ubtitle“ Every-
one ’
sGui det oMa chineLa ng ua g
e.”1 Thea ut horwa sRog e rWa g ner,thepr esi
-
dentofS outhwe st
ernDa taS y st
ems .Byt hen,Rog e rha da lreadye stablis
he d
hims elfa sawe l
l-re
spe c
teds otwa r
epubl isherwhoc areda boutt hee ndus e r
.As
AlTomme rviks tat
es,“Hispr og ramsr ele ctc onc e r
nt ha ttheus erg etmor etha n
util
ity–hes houl da l
sog ai
nk nowl edge–f rom us eoft hes otwa re.”2

Be for et ha tiss
ueofS oftalk,af ew br aves oul sha dl ea rneda ssembl yl an-
guag ea llont heirown,us i
ngc luesfrom t heROM l istingsi nt heAp pleI IRe fe
r-
enceMa nua l.The s
ei ncludedde ve l
ope rss uc ha sJ orda nMe chne r( creatorof
Ka rateka) ,S ilasWa r
ner( Ca stl
eWo lf
e nstein) ,a nd Da n Br ick l
ina nd Bob
Frank ston( VisiCa l
c).Butnowhe rewa sas e
rie sofa rticlestha tta ughtt her estof
ushowt opr og ramt he6 502.
Iir stbe ca mea cquaintedwi t
hAs semb lyLi ne swhe n,a sane wAppl eI IPlus
owne r,Ir eceiv edmyc ompl i
me ntaryissueofS o ftal
kma g azinei nJ anua ry1 982.
Thes erieswa sa l
readyonPa rt1 6;i tha dmov e dbe yondt heba si
csa ndwa s
beginni ngt oe xploresounda ndhi -resolutiong ra phics.De spi temi ssi
ngt hec ru-
cialint roduc tor ya rt
icle
s,Ie ager
lyl ook e df or wa rdt or ece i
vingS oftalke ach
mont ht os eewha tRog erWa gnerha dt ot eac husa boutt he6 502a nda ssembl y
langua ge .
The n, i
nMa rch1 982,Sota l
ka nnounc edt ha titwa spubl ishingRog er’
sa rt
i-
clesinbookf orm. Assemb l
yLi nes:TheBo okc ont a
ine dt heir stite e na rticl
es
plusa na ppe ndi xof6 502a sse mbly -
lang ua gec omma nds ,Moni tors ubr out i
ne s
,
anda ni nde x.
Rog erc ont inuedt owr itehi smont hly“ As s embl yLi nes”a rticl
esunt i
lJune
19 8
3 .Pa rt3 3c ontaineda nint roduc t
iont ot hene w6 5C0 2c 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(
Sota
lkPubl
is
hingI
nc.
),pp.3−3
0 2.
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
.
Thea
rti
cl
ethe
nende
dwi
thanot
efr
om t
heEdi
tor
,st
ati
ng:
Theirsty
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
ybySota
lk
Books.
De spitenume rousa nnounc eme ntsa boutVol ume2ov ert hene xty ear
,
whe nS ota l
kPubl i
shingI nc. we ntba nk ruptinAug us t1984 ,As s
emb lyLines:The
Bo o
k, Vo l
ume2r emainedi nc ompl etea ndunpubl is
he d.
Itist heref
oreag r
ea tpr ivil
egea ndal ong -
overduehonort opr esenta l
lof
Rog erWa gner’
s“As s
embl yLi nes”a rticl
e sinonec ompl etev olume .Thisvolume
containsa lloftheor i
gina lAs semb l
yLi nes:TheBo ok ,includingt hea ppendices,
plusthec ontentofther ema ini ngeight eenS o ftal
ka r
ticl
e s
.
Thec ompl etevolumes houl da ppe al t
ol ong -t
imer e a
de rswhoma ynotha ve
accesst otheor igi
nalSo ftal
ka rti
cles,e speciallythos ea r
ticlesfrom themi ss
ing
Volume2 . Ial
sohopet ha tRog erWa g ner’sc l
e arexplanationsa ndhi ssubtlebut
ever-presenthumorwi lle nc our a
g ene wr eade rstodi s
cov erthej oy sof6 502
assembl y-
languagepr ogra mmi ngont heAppl eII.AsDa vidFinni gannot esin
TheNe wAp pl
eIIUs er’
sGui de ,“Ther 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
ctor-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(
Sota
lkPubl
is
hingI
nc.
),
pp.
19−2
9 0
4.
4Fi
nni
gan,
Dav
i TheNe
d, 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 o k, heir
t 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 dit
ion, tha tcha pterha sbe enr e -spli
ti nt ot woc hapt ers
: their s ton
theAppl e’sarchitectur ea ndt hes econdont heAppl eMoni tor. Bydoi ngt his, al
l
ofthec ha pternumbe rsnowa gre ewi t
ht heor iginalS o ftalka rti
clenumbe rs.
Al loft hea ssembl y-lang uag epr ogra msnowi ncludet heCHKps eudo- opc ode
atthebot tom.TheCHKi nstruct i
oni ns ert
sas ing l
e -by tec he cksum a tthee nd.
Youc anus et hisc he c
k sum t ov eri
fyt haty ouha v et ype di nt hepr og r
a mc or-
rectl
y. CHKi sav ai
lablei nt heMe r l
inMa croAs semb ler. Ify oura ssembl erdoe snot
suppor tthisps eudo- opc odey ouc anignor ei tint hec ode .
Theor ig
ina lpr ogramsi nc ludedt heOBJps eudo- opc ode ,whi che s
tablis hes
thea ddr essatwhi cht hec odewi l
lbea ssembl ed. TheOBJdi r
e ctiveisnotus ua l
ly
necess arya ndi sinc ompa ti
blewi thla t
erv er si
onsoft heMe rl
ina ss
embl e r( Me r-
l
inPr oa ndMe rlin8 /
1 6).Int hise ditiona lloft heOBJ l i
ne sha vebe enc om-
me ntedout .
S pell
ing ,g r
a mma r,a ndot he rmi norc or rectionsha v eb eenqui etlyma de.
Mor es igniic antc orrections( suc ha sc odinge rror s)a rema r
k edwi t
haf oot not e
andmyi nitial
s[ CT] .
InAppe ndixBa ndC, the6 5 C02i ns t
ruc ti
onsha 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.TheUs a geCha rtinAppe ndi xCwa 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 ryUs a ge)a ndAppe ndixG ( Be gin-
ner’
sGui det oMe rl
in) .

Ac
knowl
edg
eme
nts
Thise dit
ionwa sc re
a t
e donaMa cBookPr ous i
ngOpe nOic e4 .
1..The
1
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) .Thei ma ges
weres ca
nne dus ingt heFl i
pPa lMobi leS canner( htt
p://lip-pal.com) .Thec over
imageoft heg re
e nba rcomput e rpaperwa sc reatedusingI DL8 .4.
Thete 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 bits
k y,andPe t
erCa y l
oroft heS ota lkAppl ePr oject
,who
providedPDFa ndOCRv e
r s
ionsoft heAs sembl yLine sarticl
e s.Theire nthus i
-
asma nde xperta dvi
cema dethi 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.Thr ought heirde dic
a t
iona nda ttentiont ode tai
lt hey
x
iv As
sembl
yLi
nes

caughtma nyi ns
idiouserror.Tha
s nksa lsotot heme mbe rsof6502.org and
comp.sys.apple2, whopr ovi
deds ugge
stionsforc hapter33ont he65C02 .
Ic oul
dnotha vecompl e
tedthisbookwi thoutthehe lpa ndsupportofmy
wonde rfulfamily
:mywi feGigia ndmyda ughtersMi aa ndEl y
ss.Tha
a nksto
Gigiforbe ingag uineapigont heBe g
inne r’
sGui detoMe rl
inappendix,andto
Mi aandEl ys
saforr ea
dingpa g
e sofhexc odesoutl oudwhi leIproofreadthe
programs .
Finall
y,Iwoul dl i
ketothankRog erWa gnerforg ivi
nghi spermis
s i
onfor
theproj ec
t,forans weri
nginnume r
abl
ee ma i
lsaboutAs s
emb l
yLinesmi nut
iae
,
andfora llofhiscontri
buti
onstot heAppleI Icommuni tyovertheyear
s.
Volume 1
I
ntr
oduc
ti
on

Oneote ng etst hei mpr es si


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

Myi ntentinthisbooki stopr es


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

beca useofwha tIc allthei nstante xpe r tmy th. Howma nype oplehe ars ome one
playapi anowe l
l,a nds ay,“ My ,wha tabe a utifult hing .It hinkI ’
llg e tonea nd
learnhowt opl aymy self!”The 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
lylow f rom t heiring ers!The 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 tforalowe rv ase .
I’
v es eent hi ss amee fecti 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 ob esa ti
sie dwi t
hmi nor
learnings teps .Pl ayingt heAppl ei si nma nywa ysmuc he asiertha nl ea rningt o
playapi ano,buty ous houl ds t
illnote xpe ctt os itdowna ndwr itet hewor ld’s
greates tda taba sei ny ourir ste veni ng .
Sety our se lfsomes impl ea nda chi e vableg oa l
s. 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 ttert 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 rcenti sc e rtainly
diic ul t,butni ne ty-iv epe rce ntiss ur pr ising lye asy.
Thi sbooki swr it
te nwi tht hei nt e ntionofpr ov i
di ngt hos esimpl ea chie vable
ste
ps .Ands ur pris i
ng lye noug h,byt het i
mey ouini 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 tiesa swe ll!
Soha ngi nt he rea nddon’ te xpe c tt obea ne xpe rtonpa geiv e .Iwi llg 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 eallyi s.
Oneina 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 ves 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,t he yha ve
ma dea llmye for 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 if.The
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 etoexpre
ss
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

Theirstare
atoconsideristheg e
ne r
alstruct
ureoftheAppleitsel
f.Tohelp
v
isual
izewhat’
sgoi
ngoni nthere,whynott ak ealookinsi
de.That’sri
ght–rip
t
hecoverofa ndse
ewha t’sinthere!Don’tbet imid–getyournos eri
ghtdown
i
nthereandseewhatyous hel
ledoutallthoseha r
d-ear
nedbuc k
sfor.
Provi
dingyouhaven’
tg ott
enc a
r r
iedawa yindismemberingyourAppl e
,the
i
nnerworkingss
houldappe a
rsome wha tli
kethoseinthephotobelow.

Thema 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 le
ctronicswhiz,itreallydoesn’
tma tt
er.You
cant a
keita sade vi
ceofma gicfora l
litma tt
ers.Theme mor ychi
psha vet he
capabil
it
yofs t
oringthousandsofi ndiv
idualnumbe rv alue sandthe6 5
02s uper-
vis
esthea c
tivi
ti
estherei
n. Alltherestofthee l
ect
roni cde bri
swi t
hinissupplied
onlytosupporttheme mor ya ndthe6 502.Thecircuitsallowy outoseedisplays
ofthisdataont hescreen,andpe rmitthec omput ertowa tchthekeyboardf or
youracti
ons.

]
2 As
sembl
yLi
nes

Thescre
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?Thehe artoft hes y s
temi st he6 502mi cr
opr ocessor.
Thisde viceope ratesbys canningt hroug hag ivenr a ng eofme mor ya ddresses.At
eachl ocation, itindss omepa rt
icularv al
ue.De pendi ngonwha titinds , i
tex e-
cutesag iv enope rati
on. Thisopera t
ionc ouldbea ddi ngs omenumbe rs,stori
nga
numbe rs ome whe re,ora nyofav arietyofot he rta sks.The seinterpret
edv a
lue s
areote 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 .Ate rawhi l
e,some onei nvente d
ane asierwa y, usingas otwa 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. Thec om-
puterwoul dt he nig ur eoutwhi c hv aluestous ea nds upe rvis
et hestor i
ngof
thes
ev aluesi nc ons ecutiveme mor yl ocati
ons .Thi 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 x t
re mec aseoft heassembl er.
Wej us tus ewor dslikePRINTa ndINPUTt ode scribeawhol esetoft heope rat
ions
neede dtoa ccompl ishourde sir
eda cti
on.
Ins omewa ys,assembl ylanguag eise vene asiert hanBAS IC. Therea reonl y
ity-iv 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 raton.The
i sea tt
ributesope nupma ny
possibili
tiesforpr ogramst ha twoulde it
herrunt oos lowlyort akeupt oomuc h
room i nBAS IC.

Me
mor
yLoc
ati
ons
Probablythemostunfa
mi l
iarpar
tofde ali
ngwi t
htheAppleinr e
gardto
machine-
leveloper
ati
onsisthewa ya ddress
esa ndnumbersing e
ne r
ala r
e
t
reat
ed.Unl es
syouleadanunusual
lycha r
me dli
fe,a
tsomepointi
ny ourdeal-
i
ngswi t
hy ourAppleyouha
veha dita
br uptl
ystopwhatitwa
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

Thi soc cur swhe ns omema c hi


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

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

]
4 As
sembl
yLi
nes

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

He
xade
cima
lNot
ati
on
Tounde rst
andt hisnot at
ion,let’
ss e
ehowt he6 5
0 2count s
.Ifwepl a
ceour
by t
ea ttheir s
tavail
ableloc at
ion, i
tsaddressis$0.Thedol l
ars 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
ledhe x,whichishow
thec omput erdis
playsa nda cc
e ptsdataattheMoni t
orl ev
el.
Ate rby te$0,succe s
sivelocati
onsa relabel
edint heusua lpatt
ernupt o$9.
Att hispointthecomput erusesthec haracter
sAt hroughFf orthene xtsi
xloca-
tions.Thel ocat
ionr ighta ter$F is$10.Thi sisnottobec onfusedwithten.It
representsthede cima lnumbe rs i
xteen.Thepa tte
rnr epeatsitsel
fasinus ual
count i
ngwi th:
$10, $11, $12, $13... $19, $1A, $1B... $1E, $1F, $20

Trynott ol ett hiswa yofc ount i


ngups e tyou. Thepa tte
rni nwhi chape rson
(orma chine )c ount sisr athera rbi trary,ands houldbej udg edonl yonwhe therit
ma ke sac compl ishingat aske asierornot . Thebi ggestpr oblemf ormos tpe opleis
mor eama tterofha vingbe entr aine dtous ena me slikeo nehundr e dwhe nt hey
seet henume r als1 00 .Howma nyi temsthi sc orrespondst or eal
lyde pendsmor e
ont hec onv ent ionswea greet ous et hanona nyc osmi cde cree.Toa i
di ny our
escapef rom y ourpos si
blyna rrowv iewofc ounting ,youma ywi s
ht or e adt he
diver s
iona rys t orya tt hee ndoft hi schapt er. Inanye v e
nt, i
twi l
lbes uic ie ntfor
ourpur pos est ounde rstandt hat$1Fi sasle giti
ma teanumbe ra s3 1
.
Thehe xnumbe r$FF( 2 55)ist hel argestv a
lueas inglebytec anhol d.Ab lock
of2 56by t
e s(f ori nstanc e$00t o$FF)i sote nc all
edap ageofme mor y.Int heig -
urea tright ,a llthea ddr essesfr om $00 t o$FFa res howni nbl ock( b).Fourof
thesebl ock st og ethe r,asin( c),ma k eup1 Kofme mor y.Asy ouc ans ee,the rea r
e
actua l
ly1 ,02 4by tesin1 K.Thusa4 8Kma c hinea ctua l
lyha s49 ,
15 2bytesofRAM
(Random Ac ce ssMe mor y).
Block( d)s howst heAppl e’
se ntirerang ea gain. Ifyoudonotha veaf ull48 K
ofme mor y,t he nt hemi ssingr a ng ewi llj usta ppe art ohol dac ons t
antv alue
(usua ll
y$FF) , andy ouwi llnotbea bletos tor eanypa rti
cularv al
uet here.
Ther a ng ef rom $C000t o$FFFF,a na ddi ti
ona l1 6K,i sallreser
v edforha rd-
war e.Thi sme a nst ha tanyda tast ore dint hisr angei sofape rma nentna tur ea nd
cannotbea lt
e re dbyt heus er.Somea r
easa rea ct
ua llyaphy sic
a lconne ctiont o
thi
ng sli
k et hes peak erorg ames wi tches.Ot he rs,l
ik e$E000t o$FFFFa reil ledin
byt hec hipsi nt hema chinec all
edROMs .

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

1
.Appl
e’
sAr
chi
te
ctur
e 5

ROM s t
andsf orRe adOnl yMe mor y.The sec hi
pshol dt hema c hine-lan-
gua ger outi
nestha tma k
eupe ithe rAppl esotBAS ICorI ntegerBAS 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 sirstt urne don
soy ouc a
ntalktoi t.
TheMoni 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.Thea mountofc oder equi redt odoj ustt hese
thing sisnottri
v i
a l
,a ndina dditionpr ovide suswi thar eady -ma demi ni-library
ofr outinesthatwec anc al
lfrom ourownpr og rams ,a swi llbes hownl ateri n
thisbook .
Appl eprovidesa ne xcell
entdi s
cussionoft heMoni tora ndi tsc omma nds
andope r
ati
onwi thint heAp pleIIRe f
erenceMa nua l
,cur rentlys uppl i
e dwi tha l
l
newAppl es
.Youma ywi shtoc onsultthisify oua reuns ur eoft 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

The$8BF2i sana ddr e


ssinmemor .Thedi
y splayindic
ate
sthatt
hebreak
a
ctual
lyoccurredatthea ddre
ssgi
venminustwo($8BF2−2=$8BF0) .Forr
ea-
s
onsthataren’tworthg oingint
ohere,theMoni
tora l
wa y
sprint
soutabreak
a
ddressi
nt hisplus
-twof ashi
on.Whataboutt
herestoftheme s
sag
e?Consi
der
t
henextthr
e eit
ems:
A=03 X=9D Y=00

The6 5 02 ,ina dditiont obe i


nga blet oa ddr essthev ariousme mor yloc a
tions
i
nt heAppl e ,ha sanumbe rofi nterna lreg i
ster.The
s sea reuni t
sinsidet he6 5 02
i
tselftha tca ns t
or eag i
vennumbe rva lue,a ndt heya reindi v
iduall
ya ddr es
sa ble
i
nmuc ht hes amewa yme mor yis.Thedi fere nceistha ti
ns te
adofbe ingg i
vena
hexa dec i
ma l addr ess,theya rec a
ll
edt heAc cumul ator,theX- Regis
ter,andt heY-
Reg i
s t
er .Inoure rrorme ssage,wea rebe ingt oldthes tatusofthe s
et hreer egis-
ter
sa tt hebr e ak.3

Theig ur ebe l
owi ll
ustrat
e swha twek nows of ar.The6 50
2isami croproc es-
sorc hipt ha tha sthea bil
itytos canthr oug hag ivenr angeofme mory ,whi chwe
willg ene r
allys pec if
ybyus inghe xnot a ti
onf ort hea ddr e
ss e
s.Dependi ngont he
val
ue si tindsi ne a chloca t
iona sitscanst hroug h,itwi l
lpe rf
ormv ariousope ra-
ti
ons .Asa na dditionalf e
a t
uret oitsope rati
on, ithasanumbe rofinterna lregis-
ter
s ,spe c
iic allyt heAc cumul a
tor,t heX- Re g
iste
r ,theY- Regist
er.Me mor y-
rel
a t
e dope r ationsa rebe stdonebye nte ringt heMoni torleveloftheAppl e(us u-
all
ywi thaCALL-151)a ndus ingthev a riousr outinesa vai
labletous.

CT]Theina
3[ 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
eTh a
tCount
s
Ma nype opl eha ver ema rk edt hatourc hoi ceoft e na sanumbe rba sei s
rela
te dtot hef acttha tweha vete ning ersonourha nds .Onec anonl yg ue sshow
adife rents etofc ircums tanceswoul dha vepr ofoundl yc ha 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 hting e rsi senoug ht ok eeponea wa k eatni ght .
Al ivinge xampl eoft hisa rbitraryna tur eofnumbe rb aseswa sr ec ent l
y
broug htt ol i
ghtbyt hedi scove ryofal ong -l
os ttribel ivi
ngi nther emot ej ung les
ofS outhAme rica. Itwoul dse emt het ribeha dbe eni solatedfrom 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 ssoout numbe re
dt hepe o-
ple( sot os pea k)t ha tthepe opleha de v olvedac ount ings yste
m ba sedont he
numbe rofl egsonadog ,asoppos edt oourmor er at
iona l baseten.The 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 t
h( 0 ,1,2,3)whe nt he yg ot
tothenumbe ra te rt hree,theywr otei ta s1 0a ndc all
e di tdoggy ,t
husc onir m-
ingt hequa ntit
yi nt ermsofana turaluni tint he i
re nv i
r onme 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 s2 0a 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 ta gainf orceanot herpos i
tion
inthenumbe rdi s
pl ay .
You’ r
epr oba bl ywonde r
ingwha tthe yc all
e di t
. Thedi gi
tsareofc our se1 0 0.
Oh, thena me ?Why , ofcour se,it’
so neho undr ed.

]
2
2
. TheMo
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,indt hi stypeofda t
adi icultt oreada nda remor e
l
ikel
ytoma kemistakeswhilewor kingwithit
.
Amor ec onvenie
ntme thodofpr ogra
mmi ngistoa s
signs omek i
ndofc ode
wordtoe achv al
ue.Thec ompute rwilltr
ansl
atethiswordi nt
ot hec orr
ectnum-
bertostoreinme mor y.Thistranslati
onisdonebya 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,irsti nthe
machine-l
ang uageformata ndthe ni nthea s
sembl y
-la
ng uageforma t.Firs
twe
mustentertheMoni tor.Typein:
CALL -151

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

Thi
ste
ll
stheMoni
torwewa
ntt
oex
ami
net
her
ang
eofme
mor
yfr
om $F800t
o
Theg
$F825. 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

Thera
ngeIhavepick
edi st
hev er
ybe gi
nningoft
heMonitorROM.The
dat
aherec
anbedi
rect
lyreadbythe6502,butisve
r ic
ydi ul
tformosthuma
ns
tomakemuchs
ens
eof.Thi
sismac hi
nela
ng ua
ge.

Di
sas
sembl
y
Nowt
ypei
n:
F800L

Thi
st e
ll
stheMoni t
ort
ogiveusadi
sas
sembl
yoft
hene
xtt
went
yins
truc
ti
ons
,
st
art
inga Thes
t$F800. 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.
Aterhi
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

Thisisadi sass
embl edl i
sting.Al t
houg hi tprobablydoesn’
tdoal otfory ou
rightnow, Ithinkit’
sobv ioustha titisatleastmor edist
inct
ive.
Let’slooka tital i
ttl
emor ec losely.InBAS IC,li
nenumbe r
sa r
eus edt o
beg i
nea chs e
tofs t
ateme nts.The y’repa rt
ic
ul ar
lyha ndywheny ouwa nttodoa
GOTO o rGOSUBt os omeot herpa rtoft hepr ogram.I nass
embl ylanguage,the
addressesthems el
vestak ethepl aceoft heli
nenumbe r
s.Inourexample,thec ol
-
umnofnumbe r
sont hef arle ta rethea ddr es
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 ddre s
ses.The sea retheopcodeswi t
ht hei
r
accompa ny i
ngope rands.
2
.TheMoni
tor 1
1

At$F802, fori ns tanc e, i


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

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

TheMi
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 fe rentope rat
ionsde pe ndingonwha t
nume ri
cv a luesi tindsa 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 er
isus edt ot ra nslat
ea bbr eviatedc ode wor ds,c al
ledmne moni cs,int ot hepr ope r
numbe rva lue stobes tore dinme mor y .
Thet y pe sofa ss
e mbl ersa vailablea requi tedi v
erse,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 rli
n.
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 ealprogra m
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 epsthes peake 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. Thent 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
i Theg
sRUN. e
neralsy
ntaxi
s:

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

]
1
4 As
sembl
yLi
nes

<start address>G

Theprompts
houl
dc ha
ngetoa
ne xc
lamati
onma r
k(!).Tous
etheMini-
Assem-
bl
er,y
oumustfol
lowaba si
cpa
tte
rnofinput.
Seepage49int
henewestAppl
eII
Ref
ere
nceManualforathor
oug
hde s
cri
pti
onofthi
s.Fornow,t
houg
h,enter
:
!300: JSR FBDD<RETURN>

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

Theinputsynta
xistoentertheaddressa
twhichtos ta
r tt
hepr ogra
mf ol
-
l
owedbyac ol
ona ndaspace
,thenenterthemnemonic
,a notherspace,a
ndthen
t
heopera
nd, 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

Thi
swil
lexi
ttheMini
-Asse
mbl e
r,gi
vingyoubackt
heas
ter
is
kpr
ompt(
*)oft
he
Monit
or.
Youc annowli
styourprog
ram byty
pingi
n:
300L

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

Wha tfoll
owsater$303ismor eorlessra
ndom a nddoesnotafec
tthecodewe
havet ypedin.Whenr un,thisprogr
am willjumpt ot
hebeeprouti
neat$FBDD.
Att hee ndofthatrout
ineisa nRTSthatwillreturnustoourprogramat$303.
Th eRTSt herewil
lthendoaina lr
etur
nf rom theprogram bac
kt oei
thert
he
Moni tororBAS I
Cde pendi
ngonwhe rewec al
litfrom.
From theMonitortypein:
300G

Thespe
akers
houldbeepandyouwi
llg
ett
hea
ste
ris
kpr
omptba
ck.
Nowg
oba
ck
int
oBASICwitha<CTRL>B.
Typei
n:
CALL 768
3
.As
sembl
ers 1
5

Thespeak e
rshoul
da gai
nbe epa ndt
heng i
veyout heBAS ICpromptback.CALL
768shouldworkfrom Intege
rorAppl e
sot.
Aslonga stheprogramsarenotveryinvol
ved,theMi ni
-Asse
mbleri
sha ndy
forwri
tingquickrouti
ne s
.Ac omplet
etableofroutinesi
nt heMonit
orappe a
rs
inAppendi xD atthee ndoft hebook.Trytowr iteyourownJSRst ooneor
moreoft hes
erouti
nes.Youmi ghteve
nt r
ydoingseve r
ali
nar owforf
un.

As
sembl
ers
Now l et’
sl ooka tt heope rati
onofamor et ypic
a lassembl er.Thise 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 ferenta s
sembl ert hatgivesy oudi fe 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’dli
k etoc lar
ifytwoc ommonl yus edt e
rmsi n
assembly-l
a ngua gepr ogrammi ng ,sour
c ecodea ndo bjectcode. Sourcec odeisthe
Eng l
ish-l
iket exty oue nte rintothea s sembl e
r.Thist 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- t
ypestateme 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’spre f
erred(andi nfa ctonlya cceptable
)
dietisone-t ot hree-bytec hunksofme mor yi nwhi chs implea nduna mbi guous
numbe r
sar efound.
Thea ssembl ertak e
st histex
ta ndpr oduc esthepur enume ricda ta,cal
ledthe
objectcode ,whichi sdirectlyexecutabl
ebyt 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
.
Thi
sist
heprogra
m,code
dusi
ngmnemoni
csandv a
riousnamesorlabe
lsf
or

]
1
6 As
sembl
yLi
nes

diferentpa rtsofy ourr outine .Ve ryf ewa ctuala ddr es


sesorv aluesa reus edi n
thesour c ecode .2

Tot hel eti stheobj ectc ode .Thi si swha ti sa ctuall


yputi nme mor ya sthe
ma chinel ang ua gepr og ram.Theobj ectc odei swha tthec omput era c tuallye xe-
cutes;itisobv ious l
yr a therdi ic ultt ounde rstand, atleastc ompa redt ot ryingt o
unde rstandi twhe ny ouha vet hea dv ant ageoft hes our c
ec ode . Beingmor er ead-
il
ya blet ounde rstandt hec odi ngpl ac esg reat
e rimpor t
anc eonha vingt hes our ce
li
sti
ngf orag iv enpr og ra ma nde xpla i
nswhyy ourAp pleI IRe fer enc eMa nual
conta i
nsas our cel i
stingf ort heAppl eMoni t
or .S uc hlisting swe r ec ons ide r
e d
necessar yindoc ume nt ingas ys t
em whe nt heAppl ec ameout .
Howe ver,s our cel ist
ing sf orAppl esotBAS IC, Inte
g erBAS I
C, andt heDi sk
Ope ratingS yst em( DOS )a remuc hha rde rtoc omebya nda renotdi rect l
ydi s-
tri
but edbyAppl eComput erCo. ,Inc .I ndepe nde ntlycreate ds ourc el isti
ng sfor
DOSa ndAppl esotBAS ICha v ebe e npr eparedbyi ndividua lsnotdi r ectlya sso-
ciat
edwi thAppl eComput erCo. ,I nc .anda rec omme rciallya vailable .TheDOS
3.3s ourc ecompi ledbyRa ndyHy dei sa vailablef rom La ze rS ystems ,I nc .An
Appl esotBAS ICs our c elistingi si nclude di nt heMe r
li
nAs semb lerf r om S outh-
we s
te r
nDa t
aS y stems .
Mos ta s
se mbl ersdi splaybot ht heobj e c
tc odea ndthes our cec odewhe nt he
ASM ( f
orAS se Mbl e)c omma ndi sus e d.Obj e cta nds our cec odea re,howe v
e r
,
usuallys avedt odi ska st wos epa ratea nddi st
inc til es.Initi
a ll
y ,l
et’sc ons ide rjust
thesour c eli
sting .
Their stt hingt onot icei stha t,jus tlikeinBAS IC, wea gainha vel inenum-
bers.Ina sse
mbl yl ang ua ge,t houg h,thel i
nenumbe rsares ole l
yf orus ewi tht he
progr a
me dit
or ,a nda r enotus eda ta l
lt or eferenc er outine s.Ins e
r tingal inei s
donewi thas pe ciale ditorc omma nd,a nda llf ollowi ngl ine sa r
ea ut oma tical
ly
renumbe redt oa ccommoda tet hene wl i
ne .
Ne x tnotic et hes ynt ax, orpr ope ror de r
ingoft heinfor ma tion. Ge nera l
lyt he
syntaxc onsistsoft hr e eba sice l
e me nt s,orie l
ds ,t oe achl ine .The s eie ldsa re
eit
he rde inedbyt he irpos itionont hel i
neor ,mor eote n,byde limi ters.A
deli
mi teri sac ha racterus edt os epa ra teoneie l
df rom a not he r
.I nmos ta ssem-
bler
s ,as pacei sus ed. Us ingt hi sconv ent i
on, youdon’ tha vet ot abov e rtos ome
speciicpos i
tionf ore a chie ldont hel ine.Ins teady ouj ustma kesur ee achie ld
issepara tedfrom t hea djacentonebyas pace.

2[
CT]Li
ne1
2cont
ainst
heCHKps
eudo-
opc
odet
hati
spr
ovi
dedbyt
heMe
rli
nMa
cro
Ass
emble
r.CHKinstr
uc t
st hea s
semblertoinser
tasing
lebyteconta
ininga“ c
hecksum”
f
ortheenti
reprogram.I fyoua reusi
ngMe r
li
n,youcanusetheresult
ingchecks
um to
ver
if
ythatyouhavetypedi ntheprogramcorrect
ly.
Forexa
mpl e,
forthispa
rti
cula
rpro-
gra
my oushouldgetac he c
ksum of$66.Ifyoua r
enotusi
ngMe rli
ny oushouldig
nore
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

Their stie l
di sforal abe la ndi sopt i
ona l.Line s1 0a nd1 1,fore x ampl e
,
eachha v eal abe ltha tappl ie stot ha tpoi nti nther outine.Int hisc ase,t hel a bel
STARTi ndica t
e swhe reweir stbe gint hepr ogram;ENDi sthec leverlabe l use df or
theini sh.Youma ye ve
nr ec ogni zet hispr ograma st heoneweus edt obe ept he
spea kere a r
lier.S omea s sembl e rsl i
mi tt henumbe rofc haractersus edi nt he
label.
Ast hepr og ram be c ome smor ec ompl ex,wec andot hee qui valentsofGOTO
andGOSUBbyus i
ngt hesel abe l
si ns teadofal i
nenumbe r.You’llnot i
c et ha ttodo
this,BELLh astobede ine ds ome whe rei nt hel i
sting.S inceBELLdoe snotoc cur
asal abelwi thinourownpr ogr am( lines1 0a nd1 1),itisde ine da tt hebe g in-
ningus ingt heEQU( EQUa ls)s t
ate me nt .Thes tateme ntr eads:“BELLEQUa ls$FBDD. ”
Thiswa y ,whe ne verweus et hel abe lBELL, thea ssembl e
rwi llautoma tica ll
ys etup
theJSRorwha teve rtothea ddr e ss$FBDD.
Thes econdie ldi st hec omma ndie ld,whi 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.
Thet hirdie ld, tother i
g ht, i
st hec omme ntie l
d. Useoft hec omme ntie ldi 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) .Thes e mi coloni nt hes our c
ec odei sus eda st he
de l
imi terfort hec omme ntsie ld. Comme ntsc ana lsobedonea tthev er ybe g in-
ningoft hel i
nebyus inga na st
e ri
ska st heREMa rkc ha ract
er.3 Asi nBAS IC,
every t
hi nga te rthea ste
r iski signor edbyt hea s
se mb l
e r.
As sembl ersa lsoha vewha ta r es ome time sc al
ledps e
udoopc ode sordi 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 ini t
ionsa stheobj ectc ode
isassembl ed.
The ya rec al
le ddi rectiv esbe c aus et he ydi rectthea ssemble rtope rforma
spec icf
i unc tiona tthatpoi nts uc ha ss toreaby te, s
a veail etodi sk,etc .
Thes ampl epr og r
am us est wodi rectives,OBJ a ndORG,onl i
ne s6a nd7of
thes our cel ist
ing .OBJs tandsf orOBJ e cta ndde ine swhe ret heobj ec tc odewi l
l
bea ssembl edinme mor y .4O RGs tandsf orORi Gina ndde ine stheba sea ddr esst o
beus e
dwhe nc reatingt heJSRs ,JMPs , andot herf unc ti
onst hatre f
e r
enc es pec iic
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 pec iic
informa ti
onont heus eoft he sec omma nds .

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

]
1
8 As
sembl
yLi
nes

Reme
mbe r,onl
ythea c
tualpr
ogramisconv
ert
edint
otheobj
ectcode.The
r
ema
rksa
ndt heEQU,OBJ,andORGsta
tement
sareonl
yusedi
nthesourcecode
a
ndar
eneve
rtransf
err
edtotheobje
ctcode
.

Loa
d/St
oreOpc
ode
s
Oneoft hemos tfundame nta
loperat
ionsinma c
hinec odeistransf
erri
ngthe
numbe rv al
uesbetwe endi fe
r e
ntlocati
onswi thinthec omput er.You’l
lr e
call
tha
ti na dditi
ontot he6 4
K ofa ct
ua lmemor ylocati
ons ,therewe reregi
ster
s
i
nsidethe6 502it
sel.The
f sewe r
etheAc cumula t
or,theX- Regist
er,andt heY-
Regis
ter.The r
ea r
eanumbe rofopc odesthatwi l
lloade achoft heseregis
ter
s
wit
hapa r t
ic
ularvaluea nd,ofcourse,anothersettostor ethesev a
luessome-
whereint hecomput erThet
. a
blebelowsumma riz
esthes
e:

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

Their stmne monic,LDA,standsforLoa D Ac cumulator.LDAisus e


dwhe never
youwi shtoputav al
uei ntotheAc cumul at
or.Conv ersel
y,tos tor
ethatv a
lue
some where,
youwoul de x
e cut
etheSTAc omma nd,whichs t
andsf orSToreAccu-
mula t
or.TheopcodesfortheX- Regis
terandY- Regi
ste
ra resimilarandperf
orm
theidenti
calf
uncti
onwi ththea s
soci
atedregisters
.
Nowt hequesti
oni s
,howdowec ontrolwha tnumbe rsgetputintothereg-
ist
erwe ’
reconcer
nedwi th?The rearebasi
c a
llytwoopt ions.Their s
tistoputa
specicnumbe
i rthere.Thisisusual
lyindic
a t
edi nthesourcelist
ingbypre c
eding
thenumbe rwewa nttobel oadedwitha“ #
”c haracte
r.
99 LDA #$05 ; LOAD ACC. WITH THE
100 ; VALUE ’$05’

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

I
nt hiscase
,weareloadi
ngtheAc c
umul at
orwit
hwha t
everloc
ation$05ha ppens
t
obehol dingatthemome nt.
Thesetwooptionsarecal
le
da ddres
si
ngmodesTheir
. stex
a mple(#$05)we
c
alltheimme di
atemo de,be
causei
ti snotne
ces
sar
ytog otoame morylocati
on
t
og etthedesi
redva l
ueThes
. econdc as
ewecall
theabsol
utemo de.I
nthismode ,
3
.As
sembl
ers 1
9

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

Put
ti
ngi
tAl
lTog
ethe
r
Wenow ha vet hea bi l
it
yt ot ransf
e rnumbe rsabouti nt hec omput e r
,t o
j
umpt oot hersubr out i
ne swithintheAppl ev i
aaJSR, andtor eturns afelytot he
nor malwor l
dv iaa nRTSwhe nwe ’r
edone .Ina dditi
on,weha vea na s
sembl er
thatwillallowuse asilytog enerateas our cel is
tingforourpr og ram,whi chc a n
alsobee asilymodi ied.Le t
’sputa llthist og et
he rtowr it
eas hortpr ogramt o
pr i
ntsomec haract
e rsont hescreen.Appe ndi xEc ontainstwoc ha rt
s( theAS CI I
table,
andt heTe x
tS cr eenMe moryMa p)t ha twillsupplythene cessa r
yi nforma-
tiontoa c
hi evethis
.
Whe nac haracteri spr i
ntedont hes cre en,wha tisreall
yha ppe ningi sthata
numbe rv alueisbe ings toredint hea reaofme mor yreservedfort hes creendi s-
play.Cha ng eav al
uet herea ndac haracteront hes creenwillcha ng e.TheTe xt
Sc r
eenMe mor yMa pg ivesthev ariousa ddr essesofe a
c hposit
ionont hes creen.
Theuppe rl etcorne rc orrespondst olocation$400, thelowerrig htto$7F7.
TheAS CIItables howswhi chnumbe rv aluescreatewhichs creenc ha ract
e rs
.
Suppos ewewa nttopr i
ntt hewor dAPPLEi nnor ma lte
x tThet
. ableindi catesthat
wes houldus ethefol lowingv alues:
A: $C1
P: $D0
P: $D0
L: $CC
E: $C5

Ifwewa ntthewor dt oappe aronthes event


hl i
neofthes cr
een,wes houldload
thes
ev al
ue sintolocati
ons$700 t o$704.Tot estthi
s,enterthefoll
owi ngpro-
gram us i
ngy oura ssembler.Ifyous t
illdon’
tha veone,theAppleMi ni-Assem-
ble
rc a nbeus ed,alt
houg hwewi llsoonr ea
cht hepointwhe reitwi l
lnotbe
suic i
e ntforourne e
ds.Ify oua r
eus i
ngt heAppleMi ni-Ass
embler,enteronly
thepr ogrami tsel
f,ignori
ngt heOBJ a ndORGs tat
ements.InplaceofJSRHOME
enterJSR$FC58.
Att hebe ginningoft hepr ogram,wede inewhe reitistobea ssemble
d.
Thenwede inear outi
nei nt heAppl ecall
edHOME,whic hispartoftheAppl e
Moni torandi sat$FC58.Whe neverthisrouti
nei sca
lle
d,thes c
reeni scle
ared
andt hec ursorputi nt heuppe rletc or
ner.Thisensuresthatonlyt hewor d
APPLEwi llbepr i
ntedont hes cre
en.
1 ********************************
2 * AL03-TEST PROGRAM 1 *
3 ********************************
4 * OBJ $300
5 ORG $300

]
2
0 As
sembl
yLi
nes

6 HOME EQU $FC58


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

Theroutinewi l
lbe gi
nbydoi ngaJSR t ot hehomer out i
net oclearthe
scr
een.Thent heAc cumulatorwi l
lbel oadedwitha ni mme diate$C1,thev a
lue
f
ort helet
terA.Thi swi l
lthenbes toreda tloca
tion$700ont hescree
n,whi ch
wil
lc a
usethel et
terAt obev i
si
bleont hescreen.Thene xtvalueloadedisforthe
l
ette
rP,a ndt hi
si sstoreda t$701 and$702.I ti snotne c
e ss
a r
yt orel
oadt he
Accumulator,s i
nces tori
ngt henumbe rdoe snota ctual
lyr emov eitfrom the
Accumulator.Thenumbe risjustdupl i
cate
da tthei ndi
c a
teds pot.Thepr oc
ess
cont
inuesint hispatternunt i
laliv
l el ett
ershavebe enprinted,andt he
na nRTS
ret
urnsustonor malope r
ation.
Oncey ouha vea ssembledt heroutineat$300,t rycall
ingi tbothfrom the
Moni t
orle
v elwith:
300G

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

Yous
houldal
sochangetheLDA/
STAtot
heX-Reg
ist
era
ndY-
Reg
ist
ere
qui
v-
a
lent
stov
eri
fytha
ttheyworkinasimil
armanne
r.

Conc
lus
ion
Younow ha vea ty ourdi s
pos a
lat otalofe i
ghtopc odesandaf amiliar
ity
witha s
sembl e
rs.Thesef ewopc odesa reprobablythemos tote nused,a ndwi th
j
ustt hesealoney ouc andoqui t
eanumbe roft hing.TheJSRa
s ll
owsy out o
ma keus eofa l
lther outinesa l
readya vai
labl
ei ntheMoni t
or.Ihig hl
yr ecom-
me nd ge t
tngTheAp
i pleMo nitorPe e
ledbyW. M.Doug herty
,a va
ilabl
ee xclu-
si
ve l
yfrom Appl e
, f
ormor einforma t
iononus ingt hes
eroutines.Hisbookg ives
alotofde t
ailonwha tisa va
ilable
.
Int hene xtchapterwe ’
lllooka tsomemor ea dvanceda ddr
e ss
ingt ech-
ni
que s,andhowt odoc ountersandl oops.
4
4
. Lo
opsandCo
unt
ers
J
anua
ry1
981

Now weg e ti ntonotonl ymor emne moni cs,butt het echni quesofus 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 rt sofma nypr og rams,a ndt hisi snol esst r ueinma chi nepr o-
gr a
mmi ng .Theonl ydi ferenc 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 yla ngua ge,
thet estingi sdonebye xami ningla gsint heS tatusRe g i
st er. The sela gsi ndi cat
e
thes tatusoft hev ariousr egistersa ndme mor yl oca ti
ons . TheS t
a tusRe g ist
e risa
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 ielydi sc usstheS tatusRe gis
-
tera nd, ina ddition, bina r
ynumbe r
s.
Lik et heot he rt hreer egis
ter s–t heAc c umul ator,t heX- Re gister,andt heY-
Re gister–t heS tatusRe g i
sterhol dsas ingleby te.You’ llr ec alltha teachby tein
theAppl ec anha v eav aluefrom 0t o2 55( $00t o$FF) .
Asi tha ppe ns , therea rema nywa ysofl ook i
nga ta ndi nt erpre t
ingnumbe rs
.
Theoneofc ommone x periencei stha tinwhi chwec ons ide ronl yt hema g nitude
oft henumbe r.Not icingt hat2 5 5isl ar
g ert han1 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 ll
owsust 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 ese ntsthepr esenc eora bse nc eofag ivenc ondi t
ion.Thus ,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 l
ldonet o
ma k et hing se asier,notha r
de r.Be sides,lear ningba ses i
x tee n( he x)wa s n’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 tufi sallabout .

]
2
2 As
sembl
yLi
nes

Bi
nar
yNumbe
rs
TheAppl 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 roforon. That’sit.No
in-be t
we en.Ha vingt wopos si
bleposit
ionsi spe rf
ectforba setwo.Thei de aofa
numbe rba sehast odowi thhowma nys y mbol s,oruni ts,y ouusef orc ounting.
Wenor ma llyuset en. Weha veatota
l oft enpos sibl
es ymbol stowr itei
nas ing l
e
positionbe foreweha v
et ostar
tdoubl ingupa ndus ingt wopos i
tionst or epre-
sentanumbe r.You’ llrecal
linhextha t,byus ing0t hroug h9a ndAt hr oughF,
weha ds i
x teenpos sibil
iti
es;t
hus,wewe reinba ses i
xteen. Wi t
htheon/ ofna ture
oftheAppl e,we’relimi t
edtotwopos sibili
t i
es:0or1 .
Howhi ghca nwec ountinonepos it
ion?Notv ery.Wes t
artat0 ,t
he ng ot o
1,a ndt hat’sit.The nweha vetoadda nothe rpos it
ion.Thene 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(represe
nt ingt he
qua ntit
yf ouri nba set e
n).
Forag ivennumbe rbase,t
herei
saf or mulaf orthehi g hestde c
ima lnumbe r
youc anre presentwi thag ive
nnumbe rofpos i
tions:
N=BP−1
whereNi sthelar
gestde 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!Thi 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 a
c hbyt
e.Whe thereachbitisa0
ora1de pendsonwhe therthepa r
toft
hecircui
tthati
sresponsibl
eforthatbitis
oforon.

Th 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.The
importantdiferencebetweenthisr egi
sterandtheot hersisthatitisnotus edto
st
orenumbe rval
ues.Inst
e a
ditindi ca
tesvari
ousc onditi
ons .
Thebi t
softheS t
atusRe g
isterarenumbe redfrom righttolet, 0−7.Eachbit
inthisregist
erindica
testhes t
atusa nd/orresult
sofdi ferentope rat
ionsa ndis
cal
ledala g
.Itisbyus i
ngt hi
sr egist
erthatwec anc reat
ec ounte
r sandl oopsin
ourpr ograms.Thela gwewi llbei mme diate
lyc oncernedwi t
hi sbi tone,the
zeola
r g.Int e
rmsoft hecomma ndswea l
readyknow, t
hez eola
r gisa fectedby
anLDA, LDX,orLDY.
4
.Loopsa
ndCount
ers 2
3

I
fthev a
luel oadedintotheAc cumul a
tor,X-Reg
ist
er,orY-Regist
erwere$00,
hela
t gwoul dbes e
tto1 .I fitwe r
eanonz er
onumbe r,thelagwoul dbe0 .
S
eemi nglyba c
k wardper haps,butr emembe r
,eachlagiss e
ttos howthepr es-
e
nc eora bse
nc eofag ivenc ondit
ion,inthiscas
e,$00.Thesett
ingorc l
eari
ngof
e
achS t
atusRe gist
erlagi sdonea utomati
cal
lybythe6 502atere achprogra
m
s
tep,indica
tingther es
ultsofa nypa r
tic
ularoper
ati
on.

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
rementi
ngbyonee achti
me ,asindi
cated.

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

Thet abl
ea boves howst hemne monic
sus e
dtoincre
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
ement
ingtheAc cumulat
ori snot
pos
sibleThei
. nc re
me nt/de
creme ntcommandsafec
tthezerolag,dependingon
whet
he rther
esultoftheope rat
ionis0ornot.
Theus ua
l s
y nta
xf orusi
ngt hesecommandsina
na 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-
lagset? 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

Thee fectsofi ncr eme ntinga ndde creme ntingdi ferentv aluesares hown,
alongwi ththee fectsont hez erola ga te rtheope r
ations.Their stcaseissim-
ple,5+1=6 ,5−1=4 . I
nbot hcases,ther es
ulti snonz ero,sot hez e
rola gisnot
set.For$0F, t
hes amehol dstrue.Re me mbe rt hat,inhe x,thene x tnumbe rater
$0Fi s$10.Int heca s
eof$01,i ncreme ntingpr oduces$02.Whe nwede crement
$01, t
her es
ultis$00; thez erola gisset.
He r
e ’
swhe reitg etsinter es
ting.Whe nt hes tart
ingv aluei s$FF, a
ddingone
woul dnor mallygive$100.Howe ver,sinceas i
ng leby teonlyha sar angeof$00
to$FF, t
he1i signor ed, andt hev al
uebe c
ome s$00. Thi ssetsthez eola
r g.Inthe
caseofde cre
me nti
ng ,$FF−1=$FE,s ot hez erola gisnots et
.
Ifwes tartwith$00,a lthoughi nc reme nt i
ngpr oduce sthee x
pected$01,
de cr
eme nti
ngwr apsa r oundi nt here v
e rs
eoft hepr eviousc ase,givi
ng$FF. Both
result
sa renonz er
o,s oZ–s hor tfort hez erola g–i sc l
ear,thati s
,nots et
,for
bothope rat
ions.

Loopi
ngwi
thBNE
Theonlypr
oce
durer
ema i
ningtoenableyout
oc r
eat
eal
oopisawayoft
est
-
ingt la
heZ- gandthe
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.Thent
heval
uei
s
pr
int
ed,dec
rement
ed,a
ndthepr
oce
ssre
peat
edunt
ilthec
ounte
rrea
che
s0.We
4
.Loopsa
ndCount
ers 2
5

canma ketheloope xecut


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

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

Inthispr ogram, weir stdoaJSRt othec lea


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

]
2
6 As
sembl
yLi
nes

getprint
ed–why ?)Tov eri
fyt hateachpassisbe ingexec
uted,repla
c etheSTX
$700inthes ourceli
sti
ngwi thaJSR$FBDD. Ifyoudon’ twanttohear2 55be e
ps,
tr
yc hangingtheiniti
alval
ueoft heX- Regi
st
e rinline10.Asbe f
ore,yous houl
d
beabletoc a
llthi
sprog r
amf rom theMonitorwi t
ha300G, orfrom BAS ICwi t
ha
CALL768.
Youma ya ls
owi shtotrythee qui
val
entve r
sionoftheprogram usingtheY-
Regi
s t
erorame mor ylocat
iona sthecounter.Iwoul dsugge
sttryi
ngt owr it
ea
program usi
ngINC, INX,orINYt odriv
ethecount erasapract
iceprogram.
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/nonzeroisdoneviathezerobitorla
, g
,of
theStatusRe gis
teroft he6 5
0 2.
Thec omple me ntoft heBNE i nstr
uction issomethingc al
ledBEQ,which
obscurelye noughs tandsf orBr anchEQua l.Itopera
tesi
njus tt
heoppos i
tefa
sh-
ion fr
om BNE;t hati s,itbr anchesonlywhe nther e
gis
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
inuesasl
ongasXisnotequa
lto0.Ifi
tis
,the
bra
nchinstr
ucti
oniscar
rie
douta ndtheprog
ramends
.Inasse
mblyla
nguage
,
thi
sprog
ram wouldbet
heequivale
nt:
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
ogramr e
quirestheadditi
onofane winstr
ucti
ontoourreper-
toir
e:theJMPc omma nd.ThisisanalogoustoaGOTOinBAS I
C,andinthispro-
gram willcauseprog
rame xecuti
ont ojumpt otherouti
nestar
ti
ngatLOOPe a
c h
time.Onl ywhe nt
heX- Regi
sterre
a c
hes0doe stheBEQtakeefecta
ndc auset
he
programt os k
iptotheRTSa tEND.He reisthewaythiswouldappearwhenput
i
nt ome morya ndthe
nlist
edwi ththe“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

Thea ssembl
era ut
omati
cal
lytra
nsl
atestheposit
ionsofLOOP andENDinto
t
hea ppropri
ateaddres
sest
obeus e
dbyt heBEQa ndJMPwhe nitassembl
esthe
code
.
Re membe rt
hattothelta
e r
etheaddresse
sandthev al
uesforeachopcode
anditsaccompanyingoper
and.Themorei nt
ell
ig
ibl
et r
ansl
ati
ont otheri
ghtis
Apple’
sinter
pret
ati
onofthi
sdata.

Br
anc
hOfs
etsa
ndRe
ver
seBr
anc
hes
Noticet hattheJMPsa ndJSRsareimme diat
el
yf oll
owedbyt headdress
es
(r
ever
sed)tha tt
heyaretoj
umpt o,suchasintheirstJSRas$300.
Howe ve r
,branchinst
ructi
onsarehandledalitt
ledifere
ntl
y.The$03i san
ofse
tthattell
sthe6502tojumpt hr
eebytespastt
hene xti
nstr
ucti
on.
Sincethene xti
nst
ruct
ionisat$30B,t
he6 502willbra
nchto$30E,thusski
p-
pi
ngtheJMPc omma ndandg oi
ngdi r
ect
lytotheRTS, whic
hterminate
stherou-
ti
ne.
Branche scanals
obedonei ntherever
sedirect
ion.Hereisar a
therinei-
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

TheMoni
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 ogr amt omov eba c k
upt hr oug ht hel i
sting .Toi ndicatethisbr anc hi nt heoppos i
tedi rection, thehi gh
bitiss et. Thisi st
hes amet echniquet ha tisote nus edt oshowne gativenumbe rs
ina ssembl y-langua gepr og rams. Ple
a senot et ha titisnotj ustama tterofs e
tting
thehi g hbi t.Ifthatwe rethec ase,thev aluef ollowi ngt heBEQc omma ndmi g
htbe
expec tedt obe$89. (Thea ddr essofthene xtins truction( $30C)mi nuswhe rewe
wa ntt og oto( $303) e qua l
s$09. The nwi t
ht hehi ghbi ton, weha v e$89. )
Thi si sa lmos tc or r
e c.Thea
t ct
ua lv aluei sa rrive da tbys ubt ractingt he
branc hdi stanc efrom $100.Thus$100mi nus$09e qua ls$F7. Thisi ssot hatthe
destina tiona ddressc a ns t
illbea rri
ve da tt hroug ha ddi ti
on.Not icet ha t$30C+
$F7 =$403.I tisthe nv erye asyfort he6 50 2t oc or rectt hi
sba 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 c ht heofs et
bytei sde termineds i
nc ey oura ssembl erwi l
l de t
e r
mi net hepr ope rv aluesf ory ou
whe na ssembl i
ngc ode ,
a ndAppl e’
sdi sassembl er ,aswe l
l asma nyot he rs,includ-
ingS our c eror,willgiv ethede sti
na t
iona ddre sswhe nr eadingot herc ode .
Thi si sa lsoag oodt imet os tre
sst hei mpor t
a nceofwor kingt hr oug he a
chof
thesee xampl esony ourown,s t
e pbys tep,toma kes ur ey ouunde r
s tande xactly
wha tha ppe nsa teac hs t
e p,andhowi tr elatest other e stoft hepr og ram. I fyou’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
lsuft
t hev
aluei
nto
$700s
owec
ans
ees
ome
thi
ngont
hes
cre
ena
sthec
ount
era
dva
nce
s.

]
3
0 As
sembl
yLi
nes

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


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

Appl
e’
s“L”c
omma
ndwi
ll
giv
ethi
sate
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,lashing,andi nverse.Thebe
) ep
youhe aristhe<CTRL>G( bel
l)be i
ngp rint
e dtot hescreenv iaCOUT.Thei nvi
sibl
e
cont rolcharact
ersaccountfort heblankr egionbe tweenthet woma insegments
ofout putcharacte
rs.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 etsqua r
e
br acket(_,\,[).
Thea lphabetisba ckwa r
d be causewes t
a r
teda tthehi ghestvaluea nd
wor kedourwa ydown. From c hapterfour,thoug h,you’l
lre membe rthatwhena
by teisincrementedbyonef rom $FF,t heres ultwr apsaroundba c
kt o$00.Thi s
wi llproducea na ct
iont e
stablebyaBEQ.Us ingt hiswr a
p- ar
ounde fectofthe
inc re
me ntc omma nd,wec a
nr ewri
tet hepr og ramt obeal i
ttl
emor econven-
tiona lli
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 efa
mil
iar
manner
.Atlastourprog
ramsares
tar
ti
ngt odosomethi
ngint
ere
sti
ng!Itg
ets
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 es
ur et
ha nyoua rewha tgoodthi
smi ght
be,buti
t’sguarante
edt obeane wprogrami nyourli
bra r
y!
ThePREADs ubrouti
nei nAppendixDi ndi
catesthatapa ddlecanber e
adby
l
oadingt heX-Re g
isterwiththeval
ueforthenumbe roft hepaddleyouwi shto
rea
d, f
oll
owe dbyaJSR$FB1E. Whent herouti
ner e
turns,thevalueofthepaddle
wil
lbeint heY- Regist
er.Allwehavetodot henisgrabt hisval
ue,stufitinthe
Accumul at
or,andthe ndoourJSRCOUT.
1 ********************************
2* AL05-PADDLE PROGRAM 1 *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7 TEMP EQU $06
8 PREAD EQU $FB1E
9 HOME EQU $FC58
10 COUT EQU $FDED
11 *
12 START JSR HOME
13 LDX #$00
14 LOOP JSR PREAD
15 STY TEMP
16 LDA TEMP
17 JSR COUT
18 JMP LOOP
19 * INFINITE LOOP

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

0303- A2 00 LDX #$00


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

Thisr outinewhe nc alledwi llqui cklyillupt 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 sininiteloop.
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 initepe riodsoft imewhi ler unni ngt hi
spr ogram.
Att hei nvers ea nd la s hinge 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, te
a rtheinv er
se
andla shingr ange, yous houl dbea bletos t
opt helowbymov i
ngi ntothec on-
t
rolc har ac
terr ang e.Wi t hs uic ientde xte
rity
,y ous houl dbea blet olockont o
t
hepe rsistentbe epoft hebe l
l (
<CTRL>G) .
Shor tl
ya te rthispoi nt ,thesc r
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 creenil l
ing .
Why ,youma ye venwi shto
t
rywr itingy ourna mebyde tc ontrol ofthepaddl e–c hild’splay!

Pa
ddl
ePr
ogr
am Pr
obl
ems
Returningt or eal
ityhe r
e,itiswor t
hme ntioningtha tsomepr oble
msi n
a
ccurac
yc ana ris
efrom r epe at
edlyreadingthepa ddlesoqui ckly.Theana l
ogc i
r-
c
uit
sdon’ thavetimet ore turnto0 ,andv ari
ouspr oblemsc reepin.
Als
o,weha vebee nabi tnegli
genti nlookingoutf orc onlicti
ngus eoft he
r
egi
ster
sbyt heva r
iousr out i
neswea r
ec al
ling.The r
eisote 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
fe
a cte
dbye achoft herout ines.
Forther ecord,hereisar ea
sonablefacsimileofourpr ogr a
mi nAppl esot:
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
esotonet
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.Ther
ea r
ef ourcomma ndsf or
transf
erri
ngc ontentsoftheX- Regis
terort heY-Regi
stertoa ndfrom theAcc u-
mul at
or.Theya 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 ola guponexecuti
on, soi
tispossibl
et o
testwhathasbe entransf
e r
red.Therei
snoc omma ndtotransferdi
rect
lybetwee n
theX- Regi
ste
ra ndt heY-Re gi
st
er.
Thisgive
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’ yite
sonl 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, st
ra ng e
thingswi llha ppe n.Thisi sbe causeDOSi nterfereswitha 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.Thisisbe causeDOSi sal
wa yswa tchingCOUT
forDOSc omma nds,sucha sPRINT D$;"CATALOG".Whe ny ouBRUNail ey ou
aree ss
entia l
lyinaDOSs ubroutine,andfur t
herus eofCOUTma kesDOSmor eor
lessforgetwhe r etor e
turntowhe ne ver
ythingisc ompl e
ted.Therea retwos olu-
tionstot hispr oblem.Their s
tist r
ivia
l–don’ tBRUNil esthatus eCOUT. Instea d,
BLOADt heil ea ndt hencallt
her outineintheus ualwa y.
I
f,howe ver,youi nsi
stonBRUNi ngail e,theot herchoiceist oex i
tv iat he
wa rm-re entryv ector$3D0.Aj umpt othisa ddressreplace
st heina lRTSi na ny
pr ogra
my ouwi s
ht oBRUN.Fore xampl e
,r epla
cingl i
ne1 9inLOOPPROGRAM2B
wi thJMP$3D0wi llall
owy out oBRUNt heil ewi t
hnopr oble
ms .Pleasek eept hi s
inmi ndwhe na t
tempt i
ngtoBRUNa nyothe rlis
ti
ng sthroughoutthisbook .

]
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 a.Theba
r s
icideasy ous houldbec omfort
ablewithatthis
pointarefai
rlysi
mpl e.The6 502mi croprocessorisourma inoperat
ionalunit
.
Therearethreema 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-
bitlagsto
indica
tevari
ousc ondit
ions.Sof a
r,theonl yonewe ’
veconsi
deredistheZ-lag,
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 ine das$FC58.
Inchaptriv
e 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 ddsomele xi
bili
tyt owha twec andowi thloopsa ndtes
ts
ingeneral
.

]
3
8 As
sembl
yLi
nes

Inourpre
viouspr
ogramswerel
iedonourc ount
ersre
achi
ng0andte
sti
ng
v
iatheZ-la
gtot ak
eappropr
iat
eact
ion.Suppos
e,howeve
r,tha
twewis
htotes
t
f
oraval
ueotherthan0
.Thisi
sdoneusingtwonewidea
s.

Compa
reComma
ndsa
ndCa
rryFl
ag
Theirstist
hec omparecommand,themnemonicforwhi
c hisCMP.Thi
s
te
llsthecomputertocompa r
etheconte
ntsoft
heAc c
umulat
ora g
a i
nstsome
otherval
ue.Theotherval
uecanbespecie
i dinava
rie
tyofways.As i
mpletes
t
agains
taspecicv
i aluewoul
dlookl
ikethi
s:
CMP #$A0

Thiswoul
dber ead,“CompareAc c
umul at
orwit
hanimme di
at ”Thi
eA0. swould
te
llthe65
02toc ompa r
etheAccumulatortot
hespec
iicva
lue$A0.Ontheother
hand,wema ywa nttocomparetheAc cumula
torwit
hthec onte
ntsofgive
n
memor yl
oca
tion.Thiswouldbeindic
atedby:
CMP $A0

Inthiscase ,the6 502wouldg ot ol


oc at
ion$A0, s
eewha twa st
her e
,andc ompare
thattotheAc cumulator.Itisi
mpor t
anttounde rs
tandt ha tt
hec ontentsof$A0
ma ybea ny thingfrom $00t o$FF,a nditisaga
instthisv a
luethattheAc c
umula-
torwillbec ompa red.Ine achcase,the65 0
2doe sthec ompa ris
onbyi nter
nal
ly
subtra
cti
ngt hes pecie
i dvaluefrom theAc c
umul at
or.TheAc cumul a
torrema i
ns
unchange d,howe ver,
andt heresul
toft hecompa r
isonisr elec
tede l
sewhe re
.
Thes econdi mportantideaisthatoftheca r
ryla g.Thec arrylage nabl
esus
todetermi net her e
sultofthec ompa ri
son.Rightnextt otheZ- lagint heStat
us
Regist
erist hebi tcal
ledthec ar
ry.

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

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

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

Let’
sst
ept hroughthepr ogram. AtertheJSRt othec l
earscreenr outi
ne,we
loadXwi th0i npr e
pa r
ationforreadingapa ddle.The#$00wi lltelltheroutine
thatwewi shtor eadpa ddle0.Ate 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 a
luefort 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,las hi
ngc haract
erswillbeoutputtothes c
reen.

]
4
0 As
sembl
yLi
nes

Thene xtcompa ris


onisfortheAS CIIvaluef orthec har
acter“[”.Thisc
om-
paris
ona ssurest
ha ttheBCSwi l
lcatcha l
lval
ue shi gherthantheonef orZ.The
irsttabl
einAppe ndixEi susef
ulinseeingwhe ret hesenumbe rscomef r
om.1

Onlynumbe r
sf rom $C1to$DAwi l
lma kei tthrought obepr i
ntedoutusing
COUT($FDED) .
Ag a
intheloopi sininit
e,s
oRESETi srequire dtoe x
it.
TheX- Regis
terandY- Regis
tercana l
sobec ompa redinas i
mi l
arma nne
rby
codesCPXa ndCPY. Ca nyourewrit
ethisprogramt ouseCPYi nste
adofCMP?
BEQ andBNEa rea ls
os t
il
lusableaterac 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 ext.I ti salsous edint heAppl ef ore nc odi ngt ex tinme mory ,scr een
display ,diskiles,pr inte rout put,andma nyot hera reas.Appe ndi xEg ivesac ha rtofa 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 harac ters( uppe randl owe rc
as e),nume rics,spec i
a lsymbol s,andc ontrol
code sus ingonl y1 28c ha rac
te rs
.Thisme a nst ha tAS CIIi sc ons i
de reda7 -bitcode .This
me anst hatallthei nf orma t
ionr equiredt ode ter mi newhi chc hara cterha sbe ens e
nti s
conta inedi nbi t
s0 −6oft heby t.Thus$8Ai
e 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.Thema t
teroft hehi ghbi tbe ings etorc learc anc re-
atecons iderabl
ec onf 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.Tha tis
tos ay,c ha ra
c t
ersr etriev edf rom thek eyboa rdv ia$C000a ndc ha racterss t
oredi nt he
screena reaofme mor ya ndondi s
ka l
lus uall
yha vet hehi g hbi tse t(i.
e .av al
uee qua lto
org rea terthan$80) .Thi sisa lsothewa yAppl esots t
or esda tawi thi npr og r
aml i
ne s.To
keepy ouony ourt oe s,thoug h,Applepr interc a rdsus uallydonots uppor tha v i
ngt he
highbi ts e
twhe ns endi ngout putt oapr inter,a nds tri
ng swi t
hinapr og r
a m( suc ha sA$
=”CAT”)a ls
oha v et 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 rea t
er
than$80)be foreca l
lingCOUT.
Iwi shIc oulds ayi twa sa lleasi
ertha nt ha t,butt hena gaini fi twe rea l
lt ha tea sy
,
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 sasinputde vi
ce s.Thisi s
becaus et hey ’
reane as
ywa yofs e ndi ngv aluesf r om $00t o$FFi ntot hes ystemi n
av erys moot ha ndna tura lwa y .Wec a ng e ts imi l
arda tafrom t hek e yboar d,
thoug h. The rethea dv ant a
g eist ha twec anj umpf rom onev aluetoa not herwi th
not rans i
tionbe t
we ent het wov alue s.
Ag oodpa rtofma nyf orma la s s
embl y-la ng uagec our sesde a
lswi ths yste m
I/O–t ha tis,gett
ingda taina ndoutv iadi fe rentde vices.Wr it
ings ucht hing sa s
printerdr i
v er
s,diskort apea c
c essr outine s,ha rdwa rei nterfa
ces otwa re, et
c.,a re
thea reast ha tha rdcorepr og ramme rss pendt he iry outhsma ster
ing .Us ingt he
Moni torr outinesont heAppl es impl iiest hi sf orusg re
a t
lybe causewedon’ t
havet odoal otofI /O de tail
s.You’ v
ea l
readys hownt hisbyus i
ngt hepa ddl es
($FB1E)f ori nputa ndt hes cree n( $FDED)f orout putwi thoutha v i
ngt ok now
any t
hinga bouthow t hea ctualope rati
oni sc a rrie
dout .Thek eyboa rdi sev en
easie
r.
Ime ntionede arl
iert ha tthea ddr essr ang ef rom $C000t o$FFFFi sde vote d
toha rdwa re–t heseme mor yrang esc annotbea lteredbyr unningpr og rams .(I’m
ignoringt heRAM c ardsf ort het imebe i
ng )Ther
. a
ng ef rom $D000t o$FFFFi s
usedbyROM r outinest ha twe ’vebe enc all
ing . Ther ang efrom $C000t o$CFFFi s
assig
ne dt oI /O de vi
ces .Ty pica l
lyt hes econddi git(orma y
beIs houldc al
li ta
hexit)f rom t hele tg ivesust hes lotnumbe roft hede v ic
e.Fori nstanc e,ify ou
haveapr interins lotone ,list
ingt hec odea t$C100wi llrevealthema chinel an-
guagec odeonROM oft hec ardt hatma ke si twor k .At$C600y ou’llpr obabl y
indt 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 ttemptt odi sa
ssembl efrom $C000
willnotpr oduc ear ecog nizablel isting,buti twi l
lpr oba bl
yc ausey ourAppl et o
actabi todd.Thi sr ang eisma deupofanumbe rofme mor yloca t
ionsa ct
ua lly
wiredt ophy sicalpa rt
sofy ourAppl e .I
fy out y pei n:
*C030

from theMoni tor


,ina dditi
ont og ett
ingsomer andom valuedisplaye
d,the
speakershouldc li
ck.Ifitdoesn’
tc l
icktheir s
ttime ,t
rya g
ain.Eacht i
mey ou
access$C030,thespeakerwil
lclic
ka sitmov e
si nr es
ponsetoyoura c
tion.
Thek eyboardisal
sot i
edintoas peciiclocation.Bylooki
nga ttheconte
nts
of$C000, youc antel
lifak e
yha sbeenpr ess
e d.InBAS IC,it
’sdonewi thaPEEK
-16384.(Seepa ge6oft he1981Ap pleIIRe fere
nc eMa nual.
)Ina s
s embl
ylan-
guagey ouwoul dusual
lyloadar egi
ste
rwi ththec ontent
sof$C000,sucha s
:
LDA $C000

]
4
2 As
sembl
yLi
nes

Re
adi
ngDa
taf
rom t
heKe
yboa
rd
Be causeitisdi ic ultt or eadt hek eyboarda texac tl
ytheins 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. Thes trobeiswi redt ocleara nyc ha racte
rsof
thek eyboa r
dt ha tma ybeha nginga roundf ora nynumbe rofv ari
ousr ea
sons .
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
theiring ersa c
r osst hek e yboa rdwhi lewa i
ti
ngf oroneofAppl e’slightning-like
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. Thes 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.Thusa
LDA$C010wo uldwor kj usta swe lla saSTA$C010. 2Thel 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 e ssthan$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
ightdifere 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 ference.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 ufe r
s
whichwills
tor eawhol estringofc haracte
r senteredbytheus er,
inst
eadoft heus ualone
normall
yuse df orthek eyboard.Ase achc haracterisreadin, i
tistakenoutoft hebufe 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 hebufe r:theoney ouj ustrea da nd
thenextonei nl ine.Ther ef
ore,itisg e
ne rall
yg oodpr act
icet oc l
earthes tr
obewi tha
readoperat
ion, sucha saLDA$C010, X=PEEK-16368, ort hel i
ke.LikeIsaid,ifitwe re
tooeasy
..
.
6
.I/
OUs
ingMoni
tora
ndKe
yboa
rds 4
3

11 HOME EQU $FC58


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

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

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

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

]
4
4 As
sembl
yLi
nes

030A- 8D 10 C0 STA $C010


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

Thiss houl dwor kbe tte


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

Let’
slooka tthev ari
ousaddre s
si
ngmode sus edina ss
embl y -
lang uagepro-
gra
mmi ng.Thisc onceptisrat
he rfundame ntalinpr ogrammi nga ndy ouma y
j
ustiiablywonde 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-ivethousandindividualme mor ylocati
ons.S incet hen,we’ve
work e
dmos tofourma gicbys i
mpl yma nipul
a t
ingthec ontentsoft hoseloca-
ti
ons .
Flexi
bil
it
yi nthewa ysinwhichy ouc anaddr es
sthe s
elocationsi sthek e
yt o
eveng r
eaterpowe rinyourownpr ogr
ams .
Cons i
derthischartoftheaddressi
ngmode sa vai
lableonthe6 502:

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

Inlookinga tt hee xamples,yous houl dinda llbutt hel astthreev eryf amil
iar
.
Weha veus ede a c
hoft hemi nprev i
ouspr ograms .
I
mme diatemo dewa susedt ol oadar egis
terwi thas peciicv alue.Inmos t
asse
mbl er
s,t hisisi ndica
tedbyt heus eoft henumbe rs i
g n( #)pr ecedingthe
val
uet obel oa ded.Thi scontras
tswi ththea bsolutemo dei nwhi cht hev al
ueis
ret
rie
vedf rom ag i
v enme mor yloca t
ion.Inthismode ,thee x actaddr e
s syou’
re
int
eres
tedi ni sg iven.Zerop agei sjustav ari
ationont hea bsolutemode .The
ma i
ndife rencei sthenumbe rofby t
esus edint hec oding.Itt akesthre einthe
genera
lc a
s e;inz er
opa ge,onlytwoa r
er e
quired.
I
mp li
cit,ori mp li
ed,iscerta
inlyt hemos tc ompa c
ti nstr
uc t
ioni nt hatonly
oneby t
ei sus e d.TheTAYc omma nd,Tr ansf
erAc c umul atortot heY- Regis
ter
,

]
4
6 As
sembl
yLi
nes

ne edsnoa ddit
ionaladdr es sbyte sbe c
ausethes ourcea ndde sti
nat
ionoft hedata
arei mpl i
edbyt heveryi ns tructioni t
self
.
Relativeaddres
singi sdonei nr el
ati
ont owhe retheir stbyt
eoft heinst
ruc-
tioni ts
e l
fisfound.Al thoug ht hee x
ampl eint
er pr
etsita sabr ancht oas pe
c ic
i
addr ess,you’ll
noticetha tthea c tualhexc odeisme relyapl usormi nusdispl
a c
e-
me ntfrom thebranchpoi nt .Thist oowa scoveredpre viously.
Wi ththeseaddressingmode s,wec ancreat
equi teav a r
iet
yofpr ogr
ams .The
pr oblem wi ththesemode si stha tthepr ogra
msa rer atherinlexi
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 ndl argeblocksofda 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 heina la ddress.Int hee xampl eg i
v en,if
theX- Reg i
sterhol dsa$0,t heAc cumul at
orwi llbeloade dwi tht hec ontentsof
locat
ion$200.I f,instead,theX- Re gist
erhol dsa$04,t henl ocation$204wi l
lbe
access
e d.Theus e f
ulnessina ccessingt a
blesa ndt helikeshoul dbeobv ious.
Thepr 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 tchang es.Anot herpr oblem
occurswhe nt het ableg rowslarg erthan2 5 6by tes.Becaus ethema ximum ofs et
possi
bl eusingt heX- Re gist
erorY- Re gi
steris2 55,wewoul dnor ma ll
ybeoutof
luck.
Thes 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 ,itwouldg ot o
$80 a nd$81 t og etthel ow-orde ra ndhi gh-or derby tesoft hea ddresss t
or ed
there.Theni ta ddst hev alueoftheY- Re g
istertot hataddr ess
.
7
.Addr
ess
ingMode
s 4
7

Ottimes,thesetwo- bytezero-pagea ddresspair


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

Some
time
sXa
ndYAr
en’
tInt
erc
hang
eabl
e
Youma yha venot i
cedt ha tIus edt heX- Re gist
e rinonec a sea ndt heY- Re g-
ist
eri nt heot he r.Itt ur nsoutt hatt heX- Re gist
e ra ndt heY- Re gist
e rc annot
alwa ysbeus edi ntercha ng eabl y.Thedi fe re
nc es howsupde pe ndingonwhi c h
addr essi
ngmodea 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 a nbeus edwi thag ivenc omma nd,y ou
canr efertoe i
the roft woa ppe ndi cespr ov ideda ttheba c koft hisbook .Appe ndi x
Bi sr at
herl ikeadi c
tiona ryofa llthepos sible6 502c omma ndsa ndde v otessev-
eralpa ragraphst oe achc omma nd.Appe ndixC,ont heot he rha nd,i samor e
conde nsedf or m oft heir sta ppe ndixa ndma yma k eite as i
e 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 sessent i
a l
lyy ourt ool boxofa vail-
ablec omma ndsf ors olvingapa rticularpr ogr ammi 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
blec omma ndsa nds eei fa nypa r t
icularc om-
ma ndi nspi resapos siblea ppr oa ch. Gr ant ed,thi sislike l
yt oha ppe nmor ewhe n
you’r ewor kingonr athe rs impl eg oalss uc ha smov ingaby tef rom he r
et othe re,
bute v e
nt hel ar gestpr ogra msa rema deupofj us tsuc hs i
mpl es tepsa sthat.
Thela sta ddr ess
ingmode ,inde xe dindi r
ect, i
spr oba blyt hemos tunus ua l
.I n
thisc ase
,t hec ont e
nt soft heX- Re gister( theY- Registerc annotbeus edf orthi s
mode )area dde dt ot heba sea ddr essbe foreg oingt og ett hec ont ent s
.I nac a se
similartot heot herone ,ift heX- Reg is
t erhe ld$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 entsoft hei ndica tedl oc a t
ion.I f,i nstead,t heX- Reg isterhe lda$04,t he
me mor ya ddr e sswoul dbede termi ne dbyt hec ontent sof$84a nd$85!

]
4
8 As
sembl
yLi
nes

Usual
ly
,then,theX-Regis
teri
sloadedwithmult
ipl
esoftwotoa cce
ssa
s
eri
esofconti
nuouspoint
ersinpag
ez e
ro.Thi
sisal
socal
ledpr
e-i
nde
xingsince
t
heindexi
saddedtothezer
o-pagel
ocat
ionbef
orede
ter
miningt
hebas
ea ddres
s.

St
ori
ngPur
eDa
ta
Beforewec anputa llthisne wi nforma tiontowor k,wenowne edtoa nswer
onemor equesti
on.Howdoy ous torej ustpur edatawithi napr og ram?Al lthe
comma ndswe ’
vec overe
ds of ara rea ctualcomma ndsfort he6 50 2.Thereisno
datacomma nda ssuch.Wha tarea vailable
, t
houg h,aretheAs sembl e
rdirecti
v e
s
ofyourpa r
tic
ularassembler.The s
ewi l
lv aryfrom onea ssembl ertoa nother,so
you’l
lha vetoconsultyourownma nua ltos e
ehowy ouras semblerope rat
es.
Ing enera
l,thetheoryi stode ineabl oc kofoneormor eby te
sofda taa nd
thentos kipov e
rtha tbl
oc kwi thabr anc horj umpi nst
r uctionwhe ne x
ecuting
yourpr ogram.Us uall
y,datac anbee nt erede i
therashe xby t e
sora stheAS CII
charact
e r
sy ouwi s
ht ous e
.I nt hes econdc ase
,thea s
sembl erwi llautoma t
icall
y
tra
nslatetheAS CIIcharactersintot hepr operhe xnumbe r s.
Mos tassemblershavehe xc omma ndf ordirect
lyenter i
ngt hehe xby t
esofa
datatable.TheAppl eDOSTo olKi tassembl erisonee xception. I
tdoe snotha ve
theHEXc omma nd( norma nyot hers)a ndy oumus tuset heDFB( “deineby 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 sgiven
here:
1 ********************************
2 * AL07-SAMPLE DATA PROGRAM *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 COUT EQU $FDED
9*
10 START LDX #$00
7
.Addr
ess
ingMode
s 4
9

11 LOOP LDA DATA,X


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

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

Thispr og rami sani mpr ove dv ersi


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

]
5
0 As
sembl
yLi
nes

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

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

Wes ta
rtofb yinit
iali
zinglocat
ions$06 and$07t oholdt heba seaddressof
$400,theirstbyteofthes c
reenme morya r
ea. Thenwee 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.Thiswholepr ocessisrepeateduntill
ocat
ion$07
reac
he savalueof$08( corres
pondingtoaba seaddressof$800), atwhichpoint
wer et
urnfrom theroutine.
7
.Addr
ess
ingMode
s 5
1

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

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

]
5
2 As
sembl
yLi
nes

031F- A5 07 LDA $07


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

Ent erthisprog rama ndr unf rom BAS I


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

Soundg enerat
ioni nasse mbl ylangua geissuc hal ar getopici ni ts
e l
fthata n
entir
ebookc ouldbedoneont hatsubjecta l
one .Howe ver ,si
mpl erout inesares o
easytha tthey ’
rewor tha tleastabr iefexa minationhe re .The serout ineswi 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.
Their 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 hatisde 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.Thes pe akerc anbes i
mi l
ar l
y
accessedbyl ook i
nga tlocation$C030.Thee x ceptionhe rei stha tthev aluea t
$C000( thek eyboard)v ari
eda ccordingt owha tk e
ywa 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.Thi 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
r a
l
times.Ea cht ime,thes peake rwi l
lclickonc e.Youc anima ginet hat,ifwec oul d
repeatedlya ccessthes pe a
k era taf aste noug hr a t
e,thes eriesofc l
ickswoul 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

Thepit
chofthet onegener
ate
dde pendsontherat
eatwhi
chthespeak
eris
a
ccess
ed.Becaus
eI ntege
rBAS ICisfaste
rinitsexe
cuti
onthanAppl
e ot,t
s he
t
onegenera
tedwil
l benoti
ceabl
yhigherinpi
tchint
heInte
gerv
ers
ion.
Inass
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
sembl
eit,a
s
oppos
edtotheusualasse
mbly -
languagesourc
elis
ti
ng.Theprog
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

Thenrunthepr og r
am bytypi
ng300G.
Disappointed?Thepr ogrami swor k
ing.Theprobl
e misthatther outi
neis
act
uall
ytoofa s
tf orthespeakertorespond.Wha t
’sl
ackinghereissomewa yof
cont
roll
ingther a t
eofe xec
utionoft heloop.Thisisusual
lyaccompl is
hedby
putt
ingade l
ayofs omek i
ndi ntheloop.Wes houldalsobeabletos 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 eng t
hoft he
prog ramt oincrea set hee x ecutiont 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 ddles.
Fort heir stme thod, youc anus eane wa nda dmi tte dlyc ompl exc omma nd.
Themne moni cfort hisi ns t
ruc tioni sNOPa nds t
andsf orNoOPe ration.Whe n-
evert he6 5 02mi cropr oce s
sore ncount er
st his,itj us tc ont inuest ot hene xt
instruc ti
onwi t
houtdoi nga nythi ng .Thiscodei sus e
df orj ustwha twene edhe re
–at imede lay.
Itismor eote nus ed, thoug h, ase i
therat empor ar yil lerwhe na ssembl i
nga
blockofc ode( suc ha sf orl aterda t
at ables
)ort oc ancel oute xisti
ngope rationsi n
apr ev iouslywr it
te ns ec ti
onofc ode .Qui teote 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 v
ea ctive( sucha stheNOTDIRECT
comma nde rrort hatpr eve ntsy ouf rom doi ngaGOTOdi re ctl
yt 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 et henumbe rofc yclespe rsec ondoft hetonef reque nc y
.
Thema inpr oblemi nwr itingt hene wv e r
sionwi l
lbede termi ningt henumbe rof
NOPst ha twillha vetobei ns erted.
Thee as
iestwa yt og etal arg ebl ockofme mor yc lea redt oas pe ciicv aluei s
tous et hemov er out inea lreadypr esentint heMoni tor .Toc learthebl oc k ,loa d
their stme mor yl oca ti
oni nt her ang etobec lear
edwi tht hede s
iredv alue .The n
typei nt hemov ec omma nd, mov i
nge ver
ythi ngfrom t hebe ginningoft her ang e
tothee nduponeby te .Fori ns tanc e,toc l
eart her angef r om $300t o$3A0a ndil 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

Thent
ypein300L,f
oll
owe
dwi
thLf
ore
acha
ddi
ti
ona
lli
sts
ect
ion,t
ovi
ewy
our
newpr
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.
Thetoneproduc edshoul
dbeav e
rynice,puret one.Thepi 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
.
Foradifer
e nttone,
hitRESETtost
opthepr og
r a
m, t
hent y
pein:
350: 4C 00 03

Whe nthisisr un(300G),thet onewi l


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

]
5
6 As
sembl
yLi
nes

De
layVa
luei
nMe
mor
y
Usual
lyt
hewayt
onepr
ogr
amswor ki
stopas
sapi
tc
hvaluef
rom BASICby
putti
ngtheva
luei
namemoryl
oca
ti Thi
on. 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.Thes peake
risthenc l
ic
ke d.Atthatpoi nt,we
enterade l
ayl oopt hatcy cl
esnt i
me swhe renisthenumbe rv al
ueforpi tchhe l
d
inloc at
ion$06.
Tor unthi sprog ra
m, irstloadl ocat
ion$06wi t
hv al
uesofy ourc hoic
e( 0to
255de c
ima l
, $00t o$FFhe x)a ndthe nr unwith300G.Thisismor ecompa cta nd
controllabl
et ha ntheir stexampl e,buti tst
il
lsufersf
rom beinga ninini t
el oop.
Wha twene edt odoi sspec i
fyadur a ti
onforthetoneaswe l
l.Ag a
iny ouha vet he
optionofe ithe rma kingt hev aluepa rtoftheprogram orpa ss
ingitint hes ame
waya swe ’
rec urrentlydoingt hev alueforpitc
h.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

Thi
srout i
neisusedbyl oa di
ng$06wi thav al
uef orthepitc
hy oudesir
e,$07
wit
hav al
uef orhowl ongy ouwa ntthetonet olast,andthenr unningtherou-
t
inewi t
hthe300G.
Examiningthisli
sti
ng ,
y ou’l
lseethatitisbasic
allyane xt
ensionofrouti
ne2 .
Inthisrevis
edv ersi
on,i ns
te a
dofa l
wa ysjumpi ngba cktot heLDYoft hepl a
y
cyc
le,wede crementac ounter( t
heX- Regist
er).Thiscountsthenumbe roftimes
we’
rea l
lowedt opassthrought heloop,andt hustheina llengt
hoft hepla
y.
Thi
sc a
nbeus e dbyBAS ICpr ogr
a ms,asill
ustratedbythissampleApplesot
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

Thisma kesi
tveryeas
ytoe x
perimentwithdifere
ntv al
uesforbothpitch
anddur at
ion.Themainbugint
hisrouti
neist
hatevenforaix e
dv al
uefordura-
tion,t
hele ngt
hofthenotewillvar
yde pe
ndingont hepitchspecie
i d.Thisis
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
Thene xtva
ria
tionistogett
hepit
chonacontinua
lba
sisfr
om anout
si
de
s
our
ce.I
nt hisc
ase,thesour
cewil
lbet
hekey
board.Typei
na ndas
sembl
ethi
s
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.Cha r
acter
swi thlowAS CIIv a
lueswi llproducehighertonesthanone s
withhighervalue.Thi
s smeansthatthec ontrolc har
act
e r
swi l
lproduceunus u-
all
yhightones.Toe xi
tpress<CTRL><SHIFT>P( <CTRL>@).
Let
’srevi
ewhowt herouti
nefunctions.
Attheentrypoint($300),t
hev eyir
r stthingdonei stog e
tav a
luefrom the
keyboard.Thes 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
ii-
8
.SoundGe
ner
ati
on 5
9

cal
lyfor$80, thecomput erl
ook stoseeifa<CTRL>@ha sbe e
npr e
ssed.Thi
si sj
ust
anicetouc ht ogiveusawa yofe xi
ti
ngt hepr ogram.Ate rthecheck,wetr ans
fer
theAc cumul atorv al
ue( equival
entt opitc hinthee a
r li
erprograms)tot heY-
Regis
tera ndini shwi t
ht hesamer outineus edinSoundRout i
ne2.
Ofc ourse,Iha v
etog i
vey oua tleastonepr ogra
m us ingthepaddles.This
oneg i
ve susa noppor tunit
yt ous ethee xte
rnalroutinesintheMoni t
or,too.
Don’tforg etthatus i
ngt her outi
nesa lreadypresentint heMoni t
ori sthereal
secr
ettoe a s
ya sse
mbl y-languageprog rammi ng.Itsa
ve syouthet r
oubleofha v-
ingtowr iteyourownI /Oa ndot hersophi st
icat
edroutinesandletsyouconc en-
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

TheMoni
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
tingresult
s.Thetheor
yof
t
hisr
outi
neiselegantl
ys i
mpl e.
Ittur nsoutt
ha tjustre
adingapa ddletake
sacer-
t
ainamountoftime, uic
s ienttocrea t
eourne ededdelay.Theg r
eate
rthepaddl
e
r
eadi
ng,thel
onge rthedelaytore a
di t.
Wha thappensi nthisroutinei sthatwea c
tuall
yha vetwodi st
inctde
lay
s
c
reat
ed,onebye achpa ddle.Reme mbe rthattor eadapa ddl
e ,youirsthav
eto
l
oadtheX-Regis
terwi t
hthenumbe roft hepaddl eyouwishtor eadandthendo

]
6
0 As
sembl
yLi
nes

theJSRt othepa ddlere


adr outi
ne Thev
. alueisr e
turnedi ntheY-Regis
ter
,butin
thi
sc as
ewedon’ tneedtok nowwha tthev aluewa s.
Thec ombi nat
ionoft hetwodi ferentpe riodsofde laycrea
testheefectof
twotone satonc eandanumbe rofotherv eryuniques ounds.
Thishasbe enonlythemos tbasi
cdisc uss
ionofs oundg e
nerat
ioninass
em-
blylanguage,butIt hi
nky ou’lindt
l hatitill
ustrat
eswha tcanbedonewi thonly
afewc omma nds,andthatma chinelanguag eofe r
sma nyadvant
agesinter
msof
me moryus ea ndexecut
ions peed.
9
9
. TheS
tac
k
J
une1
981

Oneoft hemor eobs cur epa r


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

]
6
2 As
sembl
yLi
nes

14 START JSR HOME


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

Thi
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

Thi spr og r
amg etsav aluef rom $00 t o$FF f rompa ddle0 ,a nds toresi tin
location$06.Thi si sne e
de 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 eept hev a
luea thandbe caus etheAS CIIc ha ract
e rcorre-
spondi ngt oi tisthe npr i
nt edoutr i
ghta terthenumbe rusingCOUT.Thec y cl
e
thenr epe a
t suntil
y oupr essRESET.
Loc at
ion$06i sus e dforonl yamome nte achpa s
stos 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. Thec omma ndstodot hisa rePHAa ndPLA. PHAs tandsf or
“PusH Ac cumul atoront os ta ck.”Whe nt hisisus edi nline1 7be low,t hev alue
currentlyi ntheAc cumul atori sputont ot hestack.TheAc cumul atoritsel
fg oes
una l
tere d,andnoneoft hes tatusla gs,sucha sthec ar
ryorz erola gs,arec ondi-
tioned. Thev alueiss i
mpl yc opie da ndstoredforus .
La teron,whe nwewa ntt or etri
evet hev alue,thePLA( “Pul LAc cumul ator
from sta ck”)onl i
ne2 1pul lst hev aluebackoft hes tackintot heAc cumul a
tor. A
PLAc omma nddoe sc ondi t
iont hez erola g,anda l
sot hes ignbi t,whi chha snot
be e
nc ov eredy et
.
9
.TheS
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

Thi
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

Thes t
ackisa l
sous eda utomatic
all
ybyt he6 502f
ors tori
ngt here t
urn
addressforea
chJSRa sit’se
nc ounter
ed.Eachti
mey oudoaPHA, t
hisaddressis
buriedoneleveldeeper
.Youmus thavedonea nequi
val
entnumbe rofPLAsa t
somepoi ntint heroutinebeforer 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 al
uesintheoppos iteorderi nwhichtheywe rest
ored.Onc eav alueis

]
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
The r
ei salimittohowmuc hyouca nputi nthes tac.Thel
k 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
krareyil
l lsupunl 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’lindi
l t
quit
eus eful.
1
0
1
0. Addi
ti
onandS
ubt
rac
ti
on
J
uly1
981

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

Bi
nar
yNumbe
rs
Bynowy ou’rec ert
ainlycomf ortablewi t
ht hei deaofaby t
ebe i
nga ni ndi-
vidualme mor yloc ati
onwhi c
hc anhol dav al
uef rom $00t o$FF( 0to2 55).This
numbe rc ome sa bouta sadi rectresultoft hewa ythec omput eri
sc onstructed
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 nonorofpos ition.Wec a ncountbya ssigni
ng
eachpos s
iblecombi nati
onofonsa ndofsauni quenumbe rv al
ue.
Int hef ol
lowi ngdi a
gra ms,ifaswi t
chi sof, i
twi l
lber epres
entedbya0i nits
parti
c ularpos i
tion.I fit’
son,a1wi llbes hown.Whe na lltheswit
chesa reof,
we ’
llcallthat0 .
Inba setwo,e achoft hee ightpos i
t i
onsint heby t
eisc all
edabi t
,a ndt he
posit
ionsa renumbe redfrom righttol et:[ 7 6 5 4 3 2 1 0 ].
Thepa tternf orcount i
ngi ssimi l
art onorma ldecimalorhe xnot a
tion.The
valuei sincreasedbya ddingonee acht i
met othedi gitont hefarri
ght,ca rr
ying
asnec essary.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. .
.heir
t 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 alue1totheval
ue2,weadda1tothe1
al
rea
dyattheirstposi
ti
on(bit0
) Thi
. sgene
rat
esthec
arr
ytoi
ncre
mentt
hesec-
ondpos
it
ion(bit1).
He r
eistheendofthese
rie
s:

$FD 253 1111 1101


$FE 254 1111 1110
$FF 255 1111 1111

Nowthemos timportantpart
.Observ
ewha thappe
nswhe ntheuppe
rli
mit
oft
hecounterisinal
lyr e
ached.At$FF( 2
55),al
lposi
tionsar
efull
.Whenthe
nex
tincr
ementisdone ,wes houl
dcarryaonet othenextposi
ti
ontothel
et;
unf
ort
unate
ly,t
hatnextposit
iondoes
n’texi
st
!

Addi
ti
onwi
thADC
Thisiswherethecar
rybitoftheStat
usRe gist
erisuse
da g
ain.Bef
ore,i
twas
usedint hecompareoperat
ions(CMP,forinsta
nc e
),but,a
sithappens,i
tisal
so
condit
ionedbyt henextcomma nd,ADC.ThisstandsforADdwi thCarry
.Whe n
the$FFisincre
me ntedus
inganADCc omma nd,t
hingswilll
ookli
kethi
s:

Ca
rry
$100 256 0000 0000 1

Thebyteha sr e
turne dt oav alueof0a ndthec arrybitissettoaone .
Wedi scussedt hewr ap-aroundt o0e ar
li
er,witht heinc re
me nt/
decrement
comma nds,butwedi dn’ tme ntiont hec arr
y.Tha t
’sbe ca
us etheINC/DECc om-
ma ndsdon’ tafe ctthec arrylag .
Howe ver,theADCc omma nddoe sconditi
ont hec ar
ryla g.Thec a
rrywillbe
setwhe nevert her esultoft hea dditi
oni sg r
eaterthan$FF. 1 WithADC,youc an
ma key ourcount ersinc reme ntbyv aluesotherthanone –r a
therli
k et
heFORI=
1TO10STEP5s tateme ntinBAS IC. ButADCi susedmor eote nforgeneralmath
ope rat
ions,suc hasc al
c ulati
ngne wa ddres
sesors cr
ee npos i
ti
ons,amongawi de
varietyofothe rapplications.
Whe neverADCi sus ed,thev al
uei ndi
c a
tedisa ddedt ot hecontent
soft he
Ac cumul at
or.Thev 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
r te
ya ri
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,therea retwopos si
bleresul
ts
. I
fthecarr
yha ppenedtobec l
ear
whenthiswasexecuted,t heval
uei nresul
twoul dbe$05.I f
,however,t
hecarry
hadbeenset(pe
rha psa stheresultofsomeot heropera
tion),t
henthecarrybit
wouldbeincl
udeda ndt heresul
twoul dbe$06.
Thepointofallthisisthatthecarrylagmus tbecle
ar e
dbe f
oretheirs
tADC
oper
ati
on.Theexampl ea boveshouldha v
ebeenwr it
tenas:
CLC (CLear Carry)
LDA #$05
ADC #$00
STA RESULT

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

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

]
6
8 As
sembl
yLi
nes

0300: A5 06 12 START LDA N1


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

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

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

Intheirstprogra
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
esul.Thi
t sres
ultisthenr e
turne
dinl oca
tion$0A.Thisrou-
t
inecoul
dbeus edasasubrouti
nefora notherass
embly-l
anguageprogra
m, orit
c
ouldbecal
le
df rom BASICa te
rpassingthev a
luest
olocati
ons$06a nd$08.
1
0.Addi
ti
ona
ndS
ubt
rac
ti
on 6
9

Thel at
tertwopr ogramsa remor ee l
aborat
ee xampleswhe ret heindi
r ec
t
mode sareus edtoindt hev a
luefrom atablest
ar t
inga t$300.Inpr ogram3 ,an
indexvalueispa s
sedtolocati
on$08. Thatisusedasa nofs etviatheX- Regi
ster.
Inpr og
ram4 ,thelow-orderandhi g
h-orderbytesf ortheaddr es
s$300a reir st
putinapa irofpointerbyte
s($1E,$1F) a
ndt heofsetisputint heY-Re gis
ter
.
Inalloftheseprograms,howe ver
,wea reli
mi tedt oaddingtwos i
ngle
-by t
e
valuesandf urt
herr e
stri
cte
dt oaone -byt
er es
ult.Notv e
rypr act
icalinther ea
l
wor l
d.
Thes ol
utionistous ethec ar
ryla gtoc re
ateat wo-byteadditionroutine.
He re
’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
cetha tN1,N2,andRSLTa r
ea l
ltwo- byt
enumbe r
s,wit
ht hes econdby t
e
ofe a
chpa i
rbe ingusedf orthehi gh-orderby t
e.(Ifyou’reunsur eoft hei deaof
l
ow-a ndhi g h-orderbytes,ref
ertoc haptertwo, footnoteone).Thi sa l
lowsust o
usev al
uesa ndr e
sult
sfrom $00t o$FFFF( 0t o65 535).Thisi
ss uic i
entf orany
addressintheAppl eII,alt
houg hbyus ingthr e
eormor eby t
es,wec oulda ccom-
moda tenumbe rsmuc hlargerthan$FFFF.
Af ew wor dsofe xplanati
ona boutthispr ogram.Fi rst
,theCLC ha sbeen
mov edt othebe gi
nningoft her outine.Althoughi tne edonlypr ecedet heADC
comma nd,itha snoe fectont heLDA, soitisputa tthebe gi
nningoft her outi
ne
foraestheti
cpur poses.Onc ethetwol ow-orde rbytesofN1a ndN2a rea dde dand
thepartialresultst
ored,thehi gh-orderby t
esa r
ea dded.Ifthere sultoft hisirs
t
additi
oni sgr eat
erthan2 55,thec arrywillbes e
ta nda ne xt
rauni ta dde dinthe
seconda ddition.Not etha tthec arryrema insuna fecteddur i
ngt heLDAN1+1
operati
on.

]
7
0 As
sembl
yLi
nes

TheMonit
orli
st
ingi
sgi
venf
ort
hisones
otha
tyouc
anc
alli
tfr
om t
his
BAS
I Cpr
ogr
am:2

0 REM AL10-ADC 5A ADDITION ROUTINE


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

TheABS()state
mentsonli
ne30e l
imina
teval
uesl
essthan0 .Al
thoughther
e
ar
ec onve
nti
onsforhandl
ingnega
tiv
enumbe rs
,thi
srouti
nei snott
ha ts
ophis
ti-
ca
ted.
Ma nyti
me sthenumberbei
nga ddedtoabaseaddressisknowna l
waysto
be$FForl e
ss,soonlyonebyteisneededforN2.A t
wo/ oneadditi
onrouti
ne
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,theprogr
ams ki
psdi
rec
tlyt
o
theend.If,however
,thec ar
ryiss
et,thevalueinN1+1getsi
ncrement
edbyone
eventhoug htheADCsaysa nimmediate$00.The$00a c
tsasadummyv aluet
o
all
owt hecarr
ytodoitsjob.Ifs
pee
di snotac onc
ern,t
heBCCc a
nbeletoutwit
h
noillefect
.

CT]Thi
2[ 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

Thefol
lowingpr
ogr
amshowsana l
ter
nat
eme thodus
ingt
heINCc
omma
nd.
I
nthisca
se,theBCCi
sre
qui
redf
orproperope
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

Thereas
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
Subtract
ionisdonel i
k eaddi t
ione xceptt hatab or
rowi srequired.Rather
thanus i
ngas e
paraela
t gforthisope rat
ion,t hec omputerrecog
ni zesthecarr
y
laga ssortofarever
seborrow.
Thati s
,as e
tcarrylagwi l
lbet r
eatedbyt hes ubt
ractcomma nda saclea
r
borrow( noborrowtaken);
ac l
ea rcarryasas etb or
row(borrowuni ttaken)3
.
Thec omma ndforsubtrac
tioni sSBC,forS uBtractwit
hCa rry
. Thebor r
owi s
cle
aredwi ththecomma ndSEC, forSEtCa rry.(Reme mber,thi
ngsa r
eba c
kwa r
d
here).As ubt
racti
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

Theprog
ramcanbec
all
edwi
tht
hes
ameBAS
ICpr
ogr
amt
hatweus
edf
or
t
hea
ddit
ionr
out
ine
.

Pos
iti
vea
ndNe
gat
iveNumbe
rs
Sofarweha vedis
cuss
edonlyhowt orepres
entwholenumbersgreate
rthan
orequaltozerousingoneortwoby t
es.Ar e
asonabl
eque s
ti
onthenis:“
Howdo
wer e
presentnegat
ivenumbers?

Negati
venumbe rscanbethoughtofasawa yofhandlingce
rtai
nc ommon
ari
thmeticposs
ibil
it
ies
,suchaswhensubtract
ingala
rgernumberfrom asma l
le
r
one,forinst
ance,3−5=−2 ,orwhe na ddi
ngapos i
ti
v enumbertoane g
ati
ve
numbe r,
forinst
ance,5+−8=−3 .

3[
CT]J
ustl
ikeADC,
SBCa
lsoc
ondi
ti
onst
hec
aryla
r g.I
fther
esul
tre
qui
resabor
rowt
hen
thecar
ryiscl
ear
ed(borr
owset
,fore
xample$50−$80).I
fthere
sul
tdoe
snotr
equi
rea
borr
owt he
nthecar
ryiss
et(
borr
owcle
ar,
forexample$50−$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.

TheSi
gnBi
t
Themos ti
mme diates olutiont otheque stionofs i
gne dnumbe rsistousebit
7toi ndica
tewhe theranumbe rispos i
ti
veorne ga
tive.Ifthebitisc l
ear,the
numbe risposi
tiv
e .I
fthebi tiss e
t,thenumbe rwi l
lberegardedasneg at
ive.
Thus+5woul dber epre s
e nted:00000101
While−5woul dbes howna s:10000101
Notethatbys acriicingbi t7t os howt hes ign,we’renowl imit
edt ov al
ues
fom −1
r 27to+1 2 7.Whe nus ingt woby te
st or e
presentanumbe rsuc hasa n
address
,thisme anst hatwe ’
llbel i
mi tedtot her a
ng eof−3 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 erypl e
a s
ingi nt er
msofs implic
ity,itdoes
haveonemi nordr awba ck–i tdoe sn’twor k
.Ifwea t
temptt oaddapos iti
vea nd
negati
venumbe rus i
ngt hiss che meweg e
tdistur bi
ngresults:
5 00000101
+ -8 10001000
-3 10001101 = -13!

Althoughwes houl
dg et−3a sther esult,
usingoursignedbits
ystem weget
−13.Ts k,tsk.Theremus tbeabe tt
e rwa y.We ll
,withthehelpofwha ti
se s
sen-
ti
al
lyal i
ttl
enume ricma gicwec ang etsome thi
ngwhi chworks,al
thoughsome
ofthec onceptuals
implici
tyg e
tslostinthepr ocess
.
Wha twe’l
linvokeisthei deaofnumbe rcomplement
s.Thesimples
tcomple-
menti swha tiscal
ledao ne’scomp lement. Theone’scomplementofanumbe ris
obtai
ne dbyr evers
ingeach1a nd0t hroug houttheorigi
nalbi
narynumbe r.
Fore xample,theone’sc ompleme ntto5woul dbe:
00000101 = 5
11111010 = -5

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

Thi
spr oc
essi
se s
sent
iall ini
yoneofde t
ion:wesi
mplyde
clar
etot
heworl
d
t
hat11110111 wi
llnow repres
ent−8wit
houtspec
iica
ll
ytr
yingtoj
ust
if
yit.
4[
CT]Te
chni
cal
l
y,f
ort
wo’
scompl
eme
nt,t
hemi
nimum s dbe−3
houl 27
68.Howeve
r,
Appl
esotandInteg
erBASICres
tri
ctt
hemini
mum i
nte
gerto−32
767
.Seecha
pte
r1 7
f
orawa yt
ofoolApple
soti
ntodi
spl
ayng−3
i 2 7
68.

]
7
4 As
sembl
yLi
nes

Undoubt edl
ythe r
ea relovelyma t
hema t
icalproof
sofsuchthingstha
tpresent
marvelouswa y
sofs pendinga naternoonbut, f
orourpurpos
e,agener
alnot
ion
ofwha ttheter
m me a
nswi llbesuic i
ent.Fortuna
tel
ycomputersar
everygood
atfoll
owinga rbit
rarynumbe ri
ngscheme swithoutask
ing“butwhyi sitthat
way?”
Nowl et
’sseeifwe’r
ea nyclose
rtoawor kingsyst
em:
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
urnsoutther
eisaina l
sol
uti
on,andthatistousewha tiscal
le
dt hetwo’
sco
mp l
ements
y s
tem.
Theonl ydiferencebetweenthisandt heone’
scomplementsys
temistha
t
aterder
ivingthene gati
venumbe rbyr eve
rsi
ngeachbitofi
tscorres
pondi
ng
posi
ti
venumbe r,wea ddone.
Soundsmy ste
rious.Let’
sseehowitlooks:

For−5
: For−8
:
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
lletyouofthehookabi
tbys a
yingt
hat
noneofthisknowledg
ewi l
lbes pecic
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
ngtoknowa
bouta
ndisve
ryusefuli
nunder-
st
andingt
henextide
a,t
hatoft
hesi
gna
ndove
rlowlag
sint
heSt
atusRegi
st
er.

TheSi
gnFl
ag
Sincebit7ofa nyby tec anrepresentwhe therthenumbe rispositi
veorne g-
ativ,ala
e gi nt heS t
atusRe gi
sterispr ovi
dedf oreasyte s
tingofthena tureofa
givenby te.Whe neveraby t
eisl oadedi ntoar e
gist
er,orwhe nana ri
thme t
ic
ope r
ationisdone ,thes ignla gwillbec onditione daccordingtowha tthes t
ateof
bit7is.
Fore xampl e,LDA#$80wi llsett
hes i
gnla gt o1 ,
whe reasLDA#$40woul dset
itto0 .Thi
si 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
di nva 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 r
ingt 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

Thisprog
ram wi
llst
ayinaloopunt i
lakeyispres
sed.Thekeypr
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
.Thestr
obeisthenclea
redandthereturndone.
As imil
artec
hniqueisusedforde t
ect
ingwhetherapa ddl
epushbutt
onhas
bee
npr es
sed.
1 ********************************
2 * AL10-BPL BUTTON TEST *
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 *
9 PB0 EQU $C061
10 *
11 *
0300: AD 61 C0 12 CHECK LDA PB0
0303: 10 FB 13 BPL CHECK ; NO BUTTON PUSH
14 *
0305: 60 15 END RTS
1
1
1
1. DOSandDi
skAc
ces
s
Aug
ust1
981

Oneoft hemor euseful appli


cat
ionsofa ss
e mblyla ngua geisina cce s
singt he
diskdi rect
lytos t
or eorr etriev
eda t
a.Youmi ghtdot histomodi fyinf orma t
ion
alreadyont hedi sk,sucha swhe nyou’rema kingc ustom modi icati
onst oDOS ,
ort ode alwithda tawi thinil esont hedi sk
,s ucha swhe ny ou’repa tc
hingor
repairingdama g e
dori mpr operl
ywr i
ttnil
e es.
Toc overDOSwe llr e qui
resmor et hanaf ewc hapterss ucha st his .My
intenthe re,then,i stos uppl yyouwi the noug hi nf
or ma ti
ont oa l
low y out o
accessa nyportionofadi ska ndtohavee noughba sicunde rs
tandingoft heov e r
-
alllayoutofDOSa nddi skst oma kesomes e
nseofwha ty ouindt here.1

He r
e’swha twe ’
llcove rinthischapter.First
,we ’
llpa intag enera love rview
ofwha tDOSi sa ndhowt heda t
aont hedi s
ketteisa r
ra nged.The 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 xplor at
ionsof
thisaspe c
tofy ourAppl ec omput er.

TheOv
erv
iew:
DOS
AnAppl
ewi
thoutadi
skdr
iveha
snowa
yofunde
rst
andi
ngc
omma
ndsl
ike
CATALOGorREAD.Thes
ene
wwordsmustent
eri
tsv
ocabul
aryf
rom s
ome
where
.
Whe nanAppl ewit
hadis
kdr
iveat
tac
hedisir
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.Thisdata
contai
nsthecodene cessaryt oreada nother10 Kofma c
hinelanguager ef
e r
redto
asDOS .Thisblockofr outi
ne sisresponsibleforal
ldis
k-rel
ate
dope rationsinthe
comput e
r.Itnorma llyr es
ide sintheuppe r10Kors oofme mory,from $9600t o
$BFFF.
Aterbooting,t heor g anizat
ionoft heme mor yusedbyDOSl ookss ome-
thi
ngliketheig ureshownont hene xtpage.

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

]
7
8 As
sembl
yLi
nes

Their s ta reac ont ainst het hr ebufe


e rsse ta sidef orthelowofda tat oa nd
from t hedi sk .Abufe ri sabl oc kofme mor yr eser vedt oholdda tat empor aril
y
whi l
ei t’
sbe ingt ransferre d. TheMAXFILESc omma nd( ale
g a
l DOSc omma nd; see
yourma nua lify ouha ven’ te nc ount e
r edi tbe for e)c ana lt
erthenumbe rofbuf -
fersr e
s e
rv eda ndt husc ha ng et hebe gi
nni nga ddr es sfrom $9600t oot he rv a l
ue s.
Asi tha ppe ns ,thr eebufe r sa rea l
mos tne verne ede ds o,inapi nc hf orme mor y,
youc anus ua ll
ys e tMAXFILESt o2 ,ndote
a nj us tto1 .
Fore xa mpl e,ify ouha dope nedat extil ec a l
le dTEXTFILE,t heda tabe ing
readorwr i
tte nwoul dbet ra nsfe rr
edv iabufe r1 .If, whilethisil ewa ss tillope n,
youdi dac a talog ,bufe r2woul dbeputi nus e. If,ins tea
d, y
ouope nedt woot her
iles,sa yTEXTFILE1a ndTEXTFILE2,a ndt he nt r i
edt odoaCATALOG,y ouwoul d
getaNOBUFFERSAVAILABLEe r ror(a s
sumi ngMAXFILESwa sse tatthr ee).Bufe r1
startsat$9AA6, bufe r2a t$9853, andbufe r3a t$9600. I
fMAXFILESi ss eta t3a s
inanor ma ls ystem,i t’soc casiona ll
yus e f
ultous et hede ads pac eoft heunus ed
bufe r3f ory ourownr out ine s.
Thema inDOSr out ine ss tarti
nga t$9D00a ret heone sr espons i
bl ef ort he
interpretingc omma ndss uc ha sCATALOGa nd,i ng e neral
,fora llowi ngDOSt o
talktoBAS I Cv ias t
ateme nt spr e ixedwi th<CTRL>D.
Theil ema na geri sas e tofr outine stha ta ctua llyexecutet hev ari
ousc om-
ma ndss entv iat hema inr out ine sandt hatma ke ss ureil esarestor edi nal og i
c a
l
(we l
l,almos t)ma nne ront hedi sk.Ittak escareofindi ngail ey ouna me ,che ck-
ingt os eewhe theri t
’sunl oc ke dbe foreawr ite,indi nge mpt ys pac eont hedi sk
forne wda ta, andc ount l
e ssot he rt a
s ksr equiredt os toreev e
nt hes impl es tile .
Whe ntheil ema na g erg etsr e
adyt or eadda taf rom orwr iteda tat ot hedi sk,
itma kesus eoft her ema ini ngr outines,c all
edt heRWTSr outines.Thi ss ta ndsf or
Re ad/Wr iteTr ac kS ector.Tounde rst
a ndf ullywha tt hissecti
ondoe s,thoug h,it
wi l
lbene cessa rynowt ol ooka ttheg ene ralor ga niza t
ionoft hedi ski tsel
f.

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
rtyivei 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.Thisme 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 essedones ec t
or, i
tdoe sn’tha vesui-
ci
entt i
met or e
adt hene xtphy si
call
y-contiguouss ectorpr ope r
ly.Ift hes ec
tors
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 hataterreadingsector0 ,DOSc anle tasma nya ssixot hersec t
or sg obya nd
st
illhavetimetos tartlookingf ors ect
or1 .Thi sa l
te
rna ti
onofs e c
tor sissome-
t
ime scall
edthesk ewfactororj ustsect
ori nt
er l
eaving.
Lookingforag iv
e ns e
ctori sdonewi thtwoc ompone nts.Their s tisaphy si
-
calone,whe r
einther ead/writehe adispos it
ioneda tas pec icdi
i stanc efrom the
centerofthediskett
etoa ccessag i
ventra ck.Thes ect
ori slocatedv i
as otwa r
eby
l
ook ingforaspec icpa
i tte
rnofi denti
fyi
ngby tes
. Inaddi t
iont ot he2 5 6bytesof
actualdatawit
hinas ector,eachs e
c t
orispr ecededbyag r
oupofi de ntifyi
nga nd

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

]
8
0 As
sembl
yLi
nes

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

Ce rt
aintracksa nds ectorsarer es
ervedfors peciicinf orma t
ion.Tr ack1 7
($11),forexampl e,c ontainsthedi rect
ory.Thisg iv e
se achil eana me ,anda l
so
tell
show t oindoutwhi c hs e
c t
orsont hedi skc ontai
nt heda t
af ore achil e
.
Tr a
ck1 7,sector0c ontainst heVol umeTa bleofCont ents( VTOC) ,whi chisa
ma st
ert abl
eofwhi c hs ectorscurrentl
yhol dda ta ,andwhi cha rea vail
ab l
efor
stori
ngne wda t
a.I fa l
loft r
ack1 7i sdama g
e d,itma ybene ar
lyimpos si
bleto
retr
ievea nyda t
af rom t hedi ske venthought heil esthems el
vesmi ghtsti
llbe
i
nt ac
t.
Theot herma i
nr eserveda reaisontracks0t hr ough2 . Thesetrack sholdthe
DOSt hatwillbeloa dedwhe nt hediskisboote d.Ifa nyoft hesetrack 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
gi
na llyha di nmi nd.Thesee nha nc ementswi l
lbe comepa rtof
1
1.DOSa
ndDi
skAc
ces
s 8
1

yours y
stem whene veryoubooty ourmodi ieddiskett
e.Somemodi icati
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.This
i
smos teasil
ydonebyus ingtheRWTSr outi
nesme nti
onedear
li
er.Fortunatel
y,
Appleha sma dethemf airl
yeasytousef r
om theus er’
sasse
mbly-
languagepr o-
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
v hebufe
et r
a
ddr
ess
).
3. Fi nall
y, callRWTSt odot her ead/writeope ration.
Ift heope ra t
ioni st obear ead,thenwewoul dpr e
suma blydos ome thing
wi t
ht heda tai nt hebufe rate rther eadisc ompl et
e.Ifawr it
ei stobedone , then
thebufe rs houl dbel oa dedbe f
orec all
ingRWTSwi tht hea ppropria t
eda t
a.Us u-
ally,thewa ya ll t
hi swor k sistor eadinas ectorir st
,the nma kemi norc hang esto
thebufe r,andt he nwr itethes ectorba ckoutt ot hedi sket
te.
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”).Thisisde scribedinde t
ail
(alongwi tht hes ec t
oror ganiz at
ion)i nt heAp pleDOSMa nua l
,buthe re’
s
enoug hinforma tiont o“ ma key ouda ng erous ,
”a sthes ayi
ngg oe s.
Th eIOBt a blei sat abley ouma k
eupa ndpl acea taloc a
tionofy ourchoi ce.
(Youc anals oma k eus eoft heonea lreadyinme mor ythatisus edinDOSope r
a-
tions.)Mos tpe opl eIk nows eemt opr efert oma keupt hei
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 hetablea l
readys etupi nDOS .
Thet ablei sma deupofs event e
enby tesa nds tartsat$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
etonepl
ace
.

]
8
2 As
sembl
yLi
nes

Loc
ati
on Code Purpose
B7F0 $00 Low-orderbyteofdaabufe
t rsta
rti
nga ddre
ss
B7F1 $20 High-orderbyt
eofdatabuferst
art
inga ddr
ess
B7F2 $00 Unused
B7F3 $00 Unused
B7F4 $02 Comma ndcode;$02=writ
e
B7F5 $00 Err
orc ode(orla
stbyeofbufe
t rreadin)
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
r eadys etthist ableupf ory ou,i tisn’tne cess a
ryt ol oa d
everyl ocationwi ththea ppr opr i
a t
ev alues.Inf a ct,ify ou’r ewi l
lingt ocont inue
usingt hel astaccess
eddi skdr ive, youne edonl ys pe cifythet ra c
ka nds ector ,set
thec omma ndc ode,andt he nc leart hee r
r ora ndv ol umev aluest o#$00.How-
ever,forc ompl e t
ea ccuracy,thes l
ota nddr i
v ev alue ss houl da l
sobes ets oy ou
knowf ors urewha tthee ntryc ondi t
ionsa re.
Onc et heIOBt ab l
eha sbe e
ns etup,t heY- Re gistera ndAc c umul atormus t
bel oade dwi ththelow-a ndhi gh-orde rbyt e
soft heIOBt able ,andt hent heJSR
toRWTSmus tbedone .Al t
houg hRWTSa ct
ua l
lys tartsa t$B7B5, t
hec allisus ua l
ly
donea sJSR$3D9whe nDOSir stboot s.Thea dva nt ageofc alli
nghe rei stha tif
Appl ee verc hangesthel oca t
ionofRWTS,onl yt hev e ctora ddr essa t$3D9wi llbe
chang eda ndac al
lto$3D9wi ll s
tillwor k.
Av ectoristheg eneralterm us edf orame mor yl oc ati
ont ha thol dsthei nf or-
ma tionf oras econdme mor ya ddr ess.Av ec t
ori sus eds ot hataj umpt oas ing le
placei nme mor ycanber outedt oanumbe rofot he rme mor yl oc ati
ons ,usua l
ly
thebe ginni ngsofv a
riouss ubr outines.Av ectorisr a therl i
keat elephones wi t
c h-
boa rd:ev e
nt houg htheus era lwa ysc al
lsthes amea ddr ess,thepr og ram lowc an
bedi rectedt oa nynumbe rofdi ferentpl acess impl ybyc ha ngingt woby tesa t
thev ectorloc a
tion.
Fore xampl e,s
uppos ea tloca ti
on$3F5wewe ret oputt he sethr 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

Asi tha ppens, $3F5isusedinj ustsuc


haf as
hionbyt hea mpersa
nd( &)
functi
onofAppl eot.TheAp
s pl
esoftIIBASICProgrammingRe f
erenceManual
providesmor einforma ti
ononthisfeature
.
Thebe stwa yt oini s
he x
plaini
nghow t ousetheIOB tabl
ea ndRWTSisto
presentthefollowingut i
li
tytoaccessag i
ventr
ackands e
ctorusingRWTS.We ’
l
l
thens t
epthroug ht heprograma ndlearnwhythev ar
iousste
psa redonetouse
RWTSs ucce
ssfully
.
1 ********************************
2 * *
3 * AL11-GENERAL PURPOSE RWTS *
4 * DOS UTILITY *
5 * *
6 ********************************
7 *
8 *
9 * OBJ $300
10 ORG $300
11 *
12 CTRK EQU $06
13 CSCT EQU $07
14 UDRIV EQU $08
15 USLOT EQU $09
16 BP EQU $0A ; BUFFER PTR.
17 UERR EQU $0C
18 UCMD EQU $E3
19 * USER SETS THIS TO HIS CMD
20 *
21 RWTS EQU $3D9
22 *
23 * BELOW ARE LOCS IN IOB
24 SLOT EQU $B7E9
25 DRIV EQU $B7EA
26 VOL EQU $B7EB
27 TRACK EQU $B7EC
28 SECTOR EQU $B7ED
29 BUFR EQU $B7F0
30 CMD EQU $B7F4
31 ERR EQU $B7F5
32 OSLOT EQU $B7F7
33 ODRIV EQU $B7F8
34 *
35 READ EQU $01
36 WRITE EQU $02
37 *
38 *
39 *
40 ********************************
41 * ENTRY CONDITIONS: SET *
42 * TRACK, SECTOR, SLOT, DRIVE, *
43 * BUFFER, AND COMMAND. *
44 ********************************
45 *
46 *

]
8
4 As
sembl
yLi
nes

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

Whe nt hispr ogra mr uns ,ita s


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

Int hene xtf ours etsofope rati


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

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

Thene xtpa irofstateme ntstak etheus ercomma ndUCMDa ndputt hatinthe


table
.Ify ouwa ntt or e
a das ector,s e
tUCMD=$01.Awr itei sUCMD=$02.Af ew
otheropt i
onsa r es el
dom us e d.The sea r
ede scribedinmor ede taili
nt heDOS3 .3
ma nualint hes ectiononRWTS.
Nex t,thebufe rpoi nteri ssettot hev alueg i
venbyt heus erinl ocati
ons$0A
and$0B.Ther e quireds pac ei s25 6by t
es( $100)a ndc anbeputa ny wherethat
won’ tconli ctwi t
hda t aa lreadyi nt hec omput er
.Conv e nientpl acesa rethe
numbe rt hre eDOSil ebufe r( $9600) ,t
hei nputbufe ritself($200) ,ora nareaof
me morybe low$9600pr otec tedbys e
tti
ngHIMEMt oa na ppr opriatev al
ue.5Inthe
example st ha tfol l
ow,I ’l
lus et hea re
af rom $1000 t o$10FFbe causenoBAS IC
program wi llber unninga nd$1000i sani cenumbe r.Int hisc ase,$0Aa nd$0B
willbeloa de dwi t
h#$00a nd#$10, respectivel
y.
Lastofa ll
, theY- Re gi
steri sloade dwi t
h#$E8a ndt heAc cumul atorisloaded
with#$B7, thel ow- ordera ndhi gh-orderby tesoftheIOBt ablea ddress.
Ate rt hec alltoRWTSv i
at hev e c
tora t$3D9, t
hec arryla gisc heckedfora n
error.I
fthec arryi scl
ea r,t
he rewa snoe rrora ndther outiner eturnsv iatheRTS.
Ifane r
rori se nc ountered, thec odewi l
lbet ransfer
redf rom t heIOBt abletothe
userlocation. Thepos siblee rrorc odesa r
e :

Code Condition
$10 Diskwrite-prot
ected,andc a
nnotbewr i
tte
nto.
$20 Volumemi smatche rr
or.Volumenumbe rfoundwa fe
sdi rentthan
specie
i d.
$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
ica
tions
TheERRby t
eoftheIOBtableissome wha tunusua l
inthatitdoe snotrema i
n
at0e veniftheread/wri
teoperati
onwa ss uccess
ful.Ina c
tualope rat
ion,ifa n
err
ordoe snotoc c
ur ,t
heERRby t
ewi llcontainthel as
tby teofthes ectorjust
acc
es s
ed.
Iti
simpor t
antthere
foretoa l
wa ysus ethec aryla
r gt ode t
ectwhe t
hera n
err
orha soccurr
ed.Infa
ct,asyoure xperi
enc egrows,youwi llnoti
cetha tagreat
manys ubrouti
nesusethecarryla ga sani ndic
a t
oroft heresult
soft heope ra-
t
ion. I
nthec as
eofRWTS,thecarrywillbec l
earediftheac c
esswa ssuccessf
uland

5Not
e:Thei
nputbufe
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
llbede
str
oye
d.
Othe
rtha
ntha
t,i
t’
saha
ndypla
cetous
e.

]
8
6 As
sembl
yLi
nes

seti
fane r
roroc curre
d.I tisnotne c
essarytoconditi
ont hec ar
rybeforecall
ing
RWTS.
Oneoft hebe stwa ystog ra
spthisroutinei
stousei ttomodi f
ytheDOSon
asampl ediska ndobse r
v et
hedi fere
nce s
.Be f
oreproceedingwiththee xamples
,
bootanAppl ema st
erdi sk,thenINITabl ankdiskThi
. swi llbeourtestpiec
e,so
tospeak.Donott rythe see xper
imentsonadi s
ka l
readyc onta
iningimpor t
ant
data.I
fdonec orr
e c
tl
yt hec hangeswon’thur t
,butifane r
r orweretooc c
ury ou
couldloseag oodde alofwor k!

Di
sk-
Vol
umeModi
ica
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: 0

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

Thedi
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

Thisappa r
entnons enseistheAS CI
Idataforthewords“DISKVOLUME”
inreverseorder.Thisi
sloa de
di nwhe nt
hediskisboote
da ndisus
edinal
lsub-
sequentcatal
ogope rat
ions.
Theda t
awa sr e
tri
eve dfr
om track2,se
ctor2,andputinabuferst
art
ingat
$1000.Thes equencewe ’
reinter
este
dinstart
satbyte$AFintha
tsec
tor
.Tomod-
if
ythat,typein:
*10AF: A0 D4 D3 C5 D4 A0 AD
*E3: 02
*300G

Their
stl
inerewri
te
stheASCIIda
tat
her
e,t
heE3:02c
hang
est
hec
omma
nd
t
o“writ
e,
”andthe300Gputsi
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
ica
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
*E3: 01
*300G

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

Their
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

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

]
8
8 As
sembl
yLi
nes

Asithappe ns,thispa rtofthedis


ki s
n’tus eda ndpr ovidesanic eplacet oput
thisnewmodi icati
on.
Whe ny our eboota te rma ki
ngthischa ng e
,pl aceadi skwithal ongc ata
log
oni tinthedr i
vea ndt y
pei nCATALOG.Whe nt hel i
stingpa usesa tert heir s
t
groupofna me s
,pr e
ss<RETURN>.Thel is
tings houlds top,leavi
ngt hena me sjus
t
shownont hes creen.I finste
a dofpre s
si
ng<RETURN>y oupr essa nyot he rkey
,
thecatal
ogwi llcontinuejus tasitnorma l
lywoul d, goingont othene xtg roupof
name s.
Bothoft hesemodi icati
onswi l
lgointoe fectwhe neveryouboott hes ample
disk.Thes
ef eaturesca nalsobepr opagate
dt oot he rdisksbyboot ingt hes ample
diskandusingt hene wDOSt oINITf r
e s
hdi sks.
Ma nymodi icationst ot heexis
tingDOSc anbema det hiswa y,andwe
hav e
n’tev
ens t
artedtot al
ka boutstori
ngbi naryda taing eneral.

Be
llModi
ica
tiona
ndDr
iveAc
ces
s
(1)Their stti
mey ouc al
ltheac cessut i
li
tyf rom t heMoni tor,itwillreturn
wi t
hjustthea steri
skpr ompt. Aterthat,unl essyouhi tRESETordoaCATALOG, i
t
wi l
lret
urnwi t
ht hea st
eris
ka ndabe ep.Thi sisbe causet hes ta
tusstorageby tefor
theMoni tor( $48)g e t
ssettoanonz erov a
luebyRWTS.I fthebe epa nnoysy ou,
modi f
ythea c c
es sutil
itytoset$48ba ckto#$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 c c
ess edwhe ny oudo, forex ample,thene xt
CATALOG. Thisisbe causeDOSdoe s
n’ta ctuall
yl ooka tthel ast-sl
ot/dri
ve -
accessed
va l
ueswhe ndoi ngaCATALOG.I nste
a d,itlook sa t$AA66f orthev olumenumbe r
(usuall
y#$00) ,a t$AA68f orthedrivenumbe r,anda t$AA6Af orthes l
otnumbe r
(timessixteen).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
hitOpe
rat
orsandLo
gic
alOpe
rat
ors
S
ept
emb
er1
981

Shi
tOpe
rat
ors
He reI ’
dl ik
et oc overtwoma i
ng roupsofa s
sembly-l
a ngua gec omma nds:
s fto
hi p erator
sa ndl ogic
alo pe rat
ors.S hitsa r
ee asi
ertounde r
stand,s owe ’
lldo
them ir st
.
You’ l
lreca l
lthattheAc c umul atorhol dsas i
ngleeight-
bitv alue ,
a ndt hatin
previ
ouspr ogramsi thasbe enpos sibl etotestindivi
dualbitsbye x ami ningla g s
intheS ta
tusRe gist
er.Ane xampl eoft hiswa susedint es
tingbi t7a tera nLDA
operati
on.I ftheAc cumul atori sloa dedwi thav a
luefrom $00 t o$7F,bi t7i s
cle
ara ndonl yBPLt estswills ucceed,s incethes i
gnla grema insc lear.If,how-
ever,av aluefrom $80t o$FFi sloade d,BMIwi l
lsucce
eds i
nc ebit7woul dbes e t
;
hencet hes i
gnla gwi l
l al
sobeaone .
Th eshitc omma ndsg reatlyextendoura bil
it
ytot e
stindividua lbi t
sbyg iv-
ingust heopt ionofs hitinge achbi ti ntheAc c
umul at
oronepos itiont othel et
orright .Therea retwodi r
ec tshitc omma nds,ASL( Arit
hme ticS hitLe t)a nd
LSR(Log i
calShitRi ght).

ASL−Ar
ithme
ticShi
tLe
t

I
nt heca
seofASL,eac
hbitismovedtotheletonepositi
on,wit
hbit7goi
ng
i
ntothecar
ryandbit0be i
ngforc
edto0 .I
na ddit
iontothecarry
,thesi
gnand
z
erolagsar
ea l
soafec
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 heir stc ase,t he r


e’snone tc hanget otheAc cumul a
tor,alt
houg ht he
carrya nds i
g nla gsa rec le
areda ndt hez erolagiss e
t.The0a teachbitpos i
tion
wa sr eplacedbya0t oit srig
ht.
Howe ver,i nt hec a s
eof$01,t hev alueint heAc cumulatordoubl est o
bec ome$02a st he1i nbi t0mov e
st ot hebit1pos it
ion.Inthisc a
se,allthree
lag swi llbec l
ea red.
Whe nt hes tarti
ngv al
ueis$80org re
a t
er,t
hec arr
ywi l
lbeset.Inthec aseof
$80i tself
, t
heAc cumul atorreturnsto0a tertheshit,si
ncetheonly1i nt hepa t
-
tern, bit7,ispus he douti ntot hecarry.
Not i
c etha tint hec aseof$FF, t
hes i
gnla 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 fe ctofdoubl i
ngt heby tebe i
ngope rat
edon.Thi scanbeus ed
asa ne asywa yt omul tiplybyt wo. I
nf act,byusingmul ti
pleASLs,youc anmul ti
-
plybyt wo,f our ,e i
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 hes lotnumbe rbyteint het a
bletobes i
xteenti
me sthetruev al
ue.
Ifyoudi dn’twa ntt odot hemul tipl
ica t
iona hea
doft i
me ,youcoulddoi tiny our
acce ssprog ram, a sbe l
ow.
*
*
*
A5 09 LDA USLOT
0A ASL
0A ASL
0A ASL
0A ASL
8D E9 B7 STA SLOT
*
*
*

USLOTh oldst hevaluefrom onetos e


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

Thecomple
me ntoftheASLcommandisLSR.I
tbeha
vesi
dent
ic
all
yex
cept
t
hatthebi
tsa
lls
hittotheri
ghtandbi
t7be
come sa0.

LSR−Log
ica
lShi
tRi
ght

LSRc anbeus edtodi vi


debymul tipl
esoftwo.It’
sals
oani cewaytote s
t
whethe ranumbe risevenorodd.Ev ennumbe r
salwayshavebit0cl
ear.Odd
al
wa yshaveitset
. Bydoinga nLSRfoll
owedbyBCCorBCS, youc a
ntes
tforthis.
Whe theranumbe ri
soddore venissometi
me scal
le
ditspari
ty.Anevennum-
berha sapari
tyof0 ,
anda noddnumbe rapar
ityof1.
LSRa l
soconditi
onsthesignandz e
rolags.
InbothLSRa ndASL,onee ndortheotheralway
sg et
sfor
cedtoa0.Some -
ti
me sthisisnotdesir
able.Thesolut
iontothi
sistherotat
ecomma nds
,ROLa nd
ROR(ROt ateLet,ROtateRight)
.

ROL−Rot
ateOneBi
tLe
t ROR−Rot
ateOneBi
tRi
ght

Inthe secomma nds,


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

]
9
2 As
sembl
yLi
nes

Log
ica
lOpe
rat
ors
Log i
c alope ratorsa re,totheuni nitiated,someoft hemor ee sotericoft he
assembl y-l
a nguag ec omma nds.Aswi t
he verythingwe ’vedonebe for e,thoug h,
withal it
tlee xplanationt hey’
llbe c
omequi teuse f
ul.
Le t
’ss tartwi thoneoft hemos tc ommonl yus edc omma nds , AND.Yo u’re
alreadyfa mi l
iarwi tht heba si
ci deaoft hisonef rom y ourda i
lys pe ech.Ift his
andt hata reac ertainwa y,t
henI ’l
l dos ome t
hing .Thiss amewa yoft hinkingc an
bea ppliedt oyourc omput er.
Aswe ’ves een,e ac
hby t
eisma deupofe ightbi ts
.Le t’
stakejustt hel t-
e 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.Tha tis,allcharactersg oingout
throughCOUT ( $FDED)s hould bee qua ltoorg r e
ate rthan#$80 ( 10 000 00 0
binary )
.Li kewise ,whe nwa tc
hingt hek eyboa rdf orak eypress,wewa itunt il
$C000h asav aluee qual toorg reaterthan#$80.
S uppos eweha dapr ogram whe rei
nwewoul dpr intcha r
acterstot hes creen
onlywhe nak eywa spresseda ndas t
anda r
dc ha ract
e rwa sbeings e ntthroug h
thes yste
m.Wha twe ’
res ayi
ngi stopr intc haractersont hesc reeno nlywhe n
botht hec ha r
actera ndt hek e
yboa rdbufe rshowbi t7s etto1.
Wec andr a w as impl echa rtt hati ll
ustratesa l
lt hepos 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

Thec ha r
tshowsf ourpos si
bil
it
ies.Ifthec ha r
acte
r’sbit7i s0( anon-stan-
dardc hara cte
r)andt hek eyboardbitis0( nok eypr e
ss),
thent 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 haracte
riss t
illnotpr i
nted.Onl y
whe nbot hde s
ire
dc ondi t
ionse xi
stwi llwebea llowedtopr int,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 git(orperha pswes houldc al
li tabigit
),the
possi
bi l
it
ie sarefew,andt hea nswersar egive
na sas imple0or1 .
Wha ta boutlargernumbe rs
?Doe stheterm 5AND3ha veme a ning?Itturns
outtha titdoe s,a
lthoug hthea nswerint hiscasewi l
lnotbe8 ,andi tisnowt hat
wemus tbec auti
ousnott oletourda ilyuseoft hewor d“ a
ddition”bec onfused
withourne wme aning.
1
2.S
hitOpe
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 reatede arli
e randa pplyittoe ac
hs etofma tchingbi t
s
i
nxa ndy ,weobt aint her esultshown.S t
a rti
ngont helet,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
tiondoweg etthene ces
sary1 sinbit0ofb oth
numbe rstoy ie
lda1i nt her esult.
Thus5AND3doe sha veme aning ,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 teverythingi sg oodf orsome t
hing!
ANDi sus edforav a
rietyofpur pos es
.The seinc l
ude:
(1)Tof orcezerosinc ertainbi tpos i
tions .
(2)Asama skt oletthroug honl yone sinc ertai
npos i
ti
ons .
Whe na nANDope r
a t
ioni sdone ,thec ont e
ntsoft heAc cumulatora r
eAND’ d
wit
ha nothe rspeciiedv alue.Ther esultoft hisope rat
ionisthenputba ckint he
Accumul ator.Theot herv aluema yb ee itherg ivenbywa yoft hei mme diate
modeorhe ldiname mor yl ocation.The sea resomepos si
blewa ysofusi
ngAND:
LDA #$80
AND #$7F
AND $06
AND $300,X
AND ($06),Y

Tounde rstandbe tte


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

]
9
4 As
sembl
yLi
nes

Inthec as
eofthet womode sofuse,opera
tiona
landproc
ess
ing,wea r
e
r
eal
lyjustta
lki
ngabouttwodiferentwaysofl
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 oa dedwi tht he
AS CI Ivaluef ort hec ha ra ctert obepr inted, thehi ghbi tisse t,a ndaJMPt oCOUT
($FDED)i sdone .Fr om l ook inga tthel i
s t
ing , youc ans eetha ta t$FDEDt he reisa n
indirectjumpba sedont hec ont entsof$36, $37( cal
ledav ector ).
Ifthiss ee msal itt
lev ague ,the nc ons ide rf oramome ntwha tIc al
lthelo w
ofc ontrolint hec omput e r.Thi sme anst ha tt hec omput e
ri sa lwa y sexe cuti
nga
prog rams ome whe re.Ev enwhe nt here’snot hi ngbutala s hingc ursoront he
screen,t hec omput eri ss tilli nal ooppr ogr amme dt og etac ha racterf r
om t he
key board.Whe ny ouc a lly ourownr out ine s,t hec omput eri sj ustt empor ar i
ly
leavingi tsowna ctivit
iest odoy ourt asksunt ili thitst hatla stRTS.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 i
nte r,ora ny whe ree lse
,
ther esalowofc
’ ont rolt ha tc arriesalongt hec ha ractertobepr inted. Forv irtu-
all
ye ve
r yc ha racterpr int ed, the6 5 02s canst hr oug ht hisreg iona 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 e astbe foreDOSi sboot ed).Thi s
ma ys eem al itt
lea bs urdunt ilyour e al
izet ha tag reatde alofle x i
bi l
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 terpr i
nt ingac ha racter
,
usua ll
yr etur nst owhe re$36, $37u sedt opr int.
Thec a rdt husbor rowst helowofc ont roll onge noug ht opr intthec harac-
ter,ate rwhi chi tg ivesc ont rolba ckt ot hes cree npr introut 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 sfound,t heout put
isre t
urne dt o$FDF0.
Now,ba ckt owha tANDi sus edf or.Nor ma llywhe nt her out inee nt e
rsa t
$FDF0, theAc cumul a t
orwi l
lhol dav aluebe twe en$80a nd$DF2.Thec har a
cte r
s

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

from $80t o$9Fa reallcont


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

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

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

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

Intheir ste xampl e ,ANDingwi th$FFy iel


dsar e
sultidenti
calt otheor i
ginal
v
alue.Ther 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 tther esultofANDi ngwi t
h$00woul da lwaysyield? )Thisme a ns
t
hatthec haracterc ome souti ni tsorigi
nalform.
Int hes econdc a s
e ,ANDingwi th$7Fha sthee fectofforcinga0i nbi t7of
t
heresult.Ex ami ningt hec ha rtinAppe ndixE,wec ans eethat$41c orresponds
oala
t shingA.
TheAppl eus e sthel eadingt wobi t
stode t
ermi nehowt opr intt hechar act
e r
.
I
fthelea dingt wobi tsa reo f,the nt hecharacterwillbei ninver se.Ifbit7i s0
a
ndbi t6i s1 ,thent hec hara ct
erwi l
lbepr i
ntedi nla 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.
Ing 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 analsothinkofANDa 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
mesig uringexac
tl
ywha tvalueyous houldusef
ort hedesi
re dres
ult
istri
cky.Asag eneralfor
mul ,ir
a stdeci
dewha tbit
syouwantt oforcet o0and
thencalculat
ethenumbe rwit
ha llotherpositi
onssettoones.Thiswillgivethe
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 olKitusersshouldnotethatwhens ti
hi ngtheAc c
umulator,
Apple’sass
emblerrequire
st headdit
ionoftheAope r
and(Example
:LSR A).Thi
s
appl
iest oASL,LSR,ROR,a ndROL.Mos totherass
embler
sdonotr equir
etheA
operand,andthatisthesyntaxusedinthi
sbook .

BI
T
Thec omma nds ome wha tr elatedt oANDi sBIT. Thi sisprovidedt oallowt he
usertode termi nee as
ilythes tatusofs pecicbi
i t
si nag ivenby te.Whe nBITi s
execut
ed, quiteanumbe roft hing sha ppen.Firstofa ll,bit
s6a nd7oft heme m-
orylocationa ret r
ansferreddi rec t
lyt othes igna ndov erlowbi tsoft heS tatus
Regist
er.S i
ncewe ’venotdi sc ussedt heov erlowla g ,letmes aybr ielyt ha tit
s
rel
atedc omma nds,BVCa ndBVS, ma ybeus edj ustasBPLa ndBMIa reus edtot es
t
thestat
usoft hes ignla g.I
fV( t
heov erlowla g)isclear,BVCwi llsucceed. I
fVi s
set
,BVSwi llwor k.
Mos ti mpor t
ant,though, ist hec onditi
oningoft hez erola g.Ifoneormor e
bit
sint heme mor ylocati
onma tchbi tsseti
nt heAc cumul at
or,thez erola gwi l
l
becleared( Z=0 )
.Ifnoma tchi sma de,Zwi l
l bes et(Z=1 )
.ThisisdonebyAND-
ingtheAc cumul atora ndt heme mor yl oc
ationa ndc onditi
oningZa ppropr i
-
atel
y.Thec onf usingpa rtist ha tthisma ysee ms ome wha tback ward.Al as,it’
s
unavoida ble;it
’sjustoneoft hos enot estoscribbleiny ourbooks oast oreme m-
berthequi r
ke a chtimey ouus eit.
Not et hatoneoft hema ina dvantagesofBIT i st hattheAc cumul atoris
unafectedbyt het est.
1
2.S
hitOpe
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
iics
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”

Thi
sla
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
The sel
a s
ttwoc omma ndsbr ingupa nintere
stinger r
orofs or
tsint heEng-
l
ishla nguage,andt ha tisthedi fer e
nc ebe t
we ena ninclusiveORa ndt hee xc
lu-
siv
eOR. Wha tallthi sisabouti sthephe nome nont hatsayingsomethingl i
ke“I’l
l
gotot hes t
oreifi tstopsr ainingo rifabusc ome sby ”hast wopossi
blei nte
rpre-
tat
ions .Theirsti st ha ti
fe it
he rev entha ppens,ande venifbothe v
e ntsoccur,
thenther esul
twi llha ppen. Thisisc al
leda ninclusi
v eORs tatement
.
Theot herpos sibil
ityist hatthec onditi
onst obeme tmus tbeoneort he
otherbutnotbot h. Thismi ghtbec al
ledt hepur es
tf orm ofanORs tateme nt.I
tis
eit
herni ghtorda y, butne verbot h.Thisi scall
eda ne xc
lusiveORs t
ate
me nt.
Ina sse
mbl yla ng uage,thei nclusiv
eORf uncti
oni sc al
ledORAforORAc cu-
mul a
tor .Theot he ri sc al
ledEORf orEx cl
usiveOR.Thet ablebel
ow s howst he
chartsforbothfunc 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 iderthet ableforORA.I feit
herorbothc orres
pondingbitsi
nt he
Acc umul atora ndt hetestv al
uema tch,thentheres
ultwillbeaone .Onlywhe n
neithe rbi
ti s1doe sa0v alueresultforthatbit
.Thema inuseforORAistoforcea
onea tag ivenbi tposit
ion. Inthisma nner
,it
’ssomethingofthecompl e
me ntto
theus eoft heANDope ratortoforcez eros.
Thef ollowingt abl
epr es
entss omee xa
mpl e
soft hee fe
ctoft heORAc om-
ma nd.

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

Use ofORA c ondi t


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

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

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

Thi
slippingphe nome nonisuse
de xt
ensi
vel
yinhi-re
sg raphicstoall
owone
imagetoov erl
aya notherwithoutdest
royi
ngtheima gebelow.EORa l
soc anbe
usedtor eve
rses pecicbi
i ts
:S i
mplyplaceonesint hepos i
tionsy ouwi shto
rev
e r
se.
Youmi ghtindi tquiterewardi
ngtowr i
teyourowne xperimentalrouti
ne
tha
twi l
lEORc e
rtainrangesofme morywithgive
nv al
ues.Thenma kethesecond
passtoveri
fythattheda t
aha sbeenres
tore Thi
d. sisespe
c i
all
yi nt
erest
ingwhe n
donee i
theront hehi -r
ess cr
eenorblocksofAS CIIdatas ucha sont het ex
t
scr
een.
1
2.S
hitOpe
rat
orsa
ndLog
ica
lOpe
rat
ors 9
9

Itwoul dbeas ha meifyou’vesta


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

]
1
00 As
sembl
yLi
nes

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

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

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


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

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

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


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

]
1
02 As
sembl
yLi
nes

1300 REM 'BIT'


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

1800 REM 'ROR'


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

Theba 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 hediferentope r
ator s
.The sev al-
uesa r
ec ont ai
ne dint heda tastateme ntonl i
ne2 0 0
0oft heAppl esotpr ogra m.
Inadditi
on,t herearer out i
nestopr intt hev alueinl ocati
on$06i nbot hbi na ry
andhe xf orma ts.Also,the r
ei sar outinet os howc ont r
olc hara
c ter
sini nve rse .
Youma ywi sht oexa minee achoft heset ode terminet helogic,ifany ,behi nd
thei
rope ration.
TheAppl esotpr og r
a mi ts
elfope ratesbyg et
ti
ngav aluefort heAc cumul a-
torandtheme mor ylocationfrompa ddl es0a nd1 .Thedoubl erea dsinlines1 1 0
and1 20mi nimizet heint e
racti
onbe twe ent het wopa ddl es
.Pr essi
nga nyk e y
advancest hedisplaytot hene xtfunction; theleta rr
owba cksup. Press
ingA, B, E,
L,O,orRwi l
l j
umpt othes elect
edf unction.
Thes c r
e e
ndi splays howst hehe xa ndbi na r
yv aluesfore achnumbe ra nd
als
owha tc ha r
acterwoul dbepr i
nte dv iaaPRINTCHR$(X)s ta
te ment( cont rol

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

'EOR'

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

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

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

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

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

N V – B D I Z C
^ ^

]
1
04 As
sembl
yLi
nes

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

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

Pr
intRout
ine
s
Ihavet
wofavori
tewa y
sofsimul
at
ingthePRINTs
tat
eme
nt.Their
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

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

program,ors houl de xitv i


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

]
1
08 As
sembl
yLi
nes

032E: 60 38 EXIT RTS


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

Thisonei 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,t her eturna ddres
s
mi nusonei sputont hestack .Upone ntr
yt ot hePRINTr outi
ne , weus ethisfac
t
toputt ha ta ddre ssinPTR,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 inte
dunt 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.Ate rtha thappens ,
the
Y-Re gister( thel engthoft hes tringpr inted)ist ransf
erre
dt ot heAc cumul at
or
anda dde dt ot hea ddr es
si nPTR,PTR+1,a ndt her es
ultpushe dba ckont othe
stack.Re me mbe rtha ttheol dre turna ddresswa sE0-1unt ili
twa spulledof.
Nowwhe nt heRTSi se ncount e
red,thepr og ram wil
lbef ool edint oret
urn-
ingt oDONEi nsteadoft oE0a sitot herwisewoul dha vedone.
Tos umma rize,the n:
1. AnyDOSc omma ndc anbee x
ecut e
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 ta
-typepri
ntroutineusesASCIIcha
rac
tersinal a
beledblock,
whichis
thencal
le
dbyna meus ingt heX-
Re g
ist
erinadir
e c
ti ndex
eda ddres
si
ngmode .
Thestr
ingtobepri
nteds houldha
v et
hehighbi
tset(AS CI
Ivalue+$80),andthe
str
ingmustbeter
minatedbya0( atle
astwhenusi
ngt herouti
neg i
venhere)
.
3. AJSRtoaspe
cia
l pr
intr
out
inecanal
sobedone.I
nthi
scas
etheAS
CIIda
ta
shoul
dimmedi
ate
lyfoll
ow t
heJSR.Aga
in,hav
et hehi
ghbits
etandendwi
th
$00.

I
nputRout
ine
s
Theothersi
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
.”Thes ol
uti
onis
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

Thehe ar toft hisr out ineisac al


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

]
1
10 As
sembl
yLi
nes

Ne xt,C2b eginsal oopthatloadseachc ha rac teri ntothebufe r


,doe sa nAND
with#$7F, andthe ns t
orestheresultatal ocationpoi ntedtobyPTR,PTR+1pl us
theY- Re gi
sterofset.
TheAND #$7Fha sthee fec
tofc le
aringt hehi g hbi tbyf or
cingbit7t o0 .
TheY- Re gi
sterist hende cre
me nteda ndt hel oopr epeate
dunt i
lt heDEY
forc
esYt oa n$FF.Thi swilli
ndicatetha tthel a stv a l
uewa s$00,a ndweha ve
thusc ompl ete
ds canningthebufe r.
Thi sroutinewi llworkinea 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 esota nda ssemblylanguage.Thi sisbe caus eunl es
sspeedi srequire
d,
Apple sotdoe sofe rsomea dv
antag e
sint ermsofpr og r
amc lari
tya nde aseof
modi ic at
ion.Ate ra l
l,iftherewe renoa dva ntag et oAppl esot,whywoul d
some bodyha vewr i
ttenitintheirstplace?
So, tothatend, herearetwone wl i
stings,their sti nApplesot:
5 PRINT CHR$(4);"BLOAD AL13.INPUTFP"
10 IN$ = "X"
20 PRINT "ENTER THE STRING: ";
30 CALL 768: IN$ = MID$(IN$,1)
40 IF IN$ = "END" THEN END
50 PRINT IN$: PRINT: GOTO 20

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

030F: C8 27 INY ;Y=4


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

Thei mportantdi ferenc etonot i


cehereisthatIN$ha sbe endeinedasthe
irstv a
riablei
nt heAppl eotpr
s ogram,andt hattheassembly-l
anguagerouti
ne
usesthisfactt
otra nsferthestri
ngt oAppl e
sot.
Thewa ythisisdonebe gi
nsa tXFER.Whe na nAppl esots t
ri
ngv ar
iabl
eis
stored,thename ,l ength,a ndl ocat
ionoft hestringareputi nat abl
ewhos e
be g
inningispointedt obyl ocations$69,$6A(VARTAB,VARTAB+1).
S i
nceIN$wa stheir stv ar
iabledeined,wek nowt hatitsnamea ndpoint
er
wi l
lstar
ta twherev e
rVARTABpoi nt
s.Thenamei she l
dinpos it
ions$00and$01,
thelengthin$02, andt heloc at
ionin$03a nd$04.

Byloadi
ngtheY- Regi
ste
rwi t
h#$02, wecanstorethelengt
hoft heenter
ed
st
ringintheproperplac.Thel
e ocationofIN$isthensetto$200byput ti
ngthe
appropri
atebyte
si ntoposit
ions$03 a nd$04.Now Appl esotiste mporar
il
y
f
ooledintothi
nkingthatIN$isat$200–r i
ghtwhereourinputstr
ingisheld!
Theroutneini
i shesbyc l
eari
ngt hehighbi
t,asbefore
,a ndthenr e
turni
ng
withtheRTS.

]
1
12 As
sembl
yLi
nes

WhentheRTSisdone
,li
ne30oft
heAppl
esotpr
ogrami
mme dia
tel
yass
igns
IN$t
oit
sel
finsuc
hawa yastof
orc
eApple
sottomoveIN$f
rom wher
eitwasin
theinputbufe rt oanewl ocat
ionupi nitsusualvari
ablestora
gea r
ea.Thene t
resul
tc a
nbeobt ai
nedinv ari
ousotherwa ysbesi
destheMID$s t
atement,butthe
ways howni stheleas
tintrusi
veintermsofa fect
ingothervaria
bles
.(Youc ould
useA$=IN$:IN$=A$,butt henyou’dne e
das e
condv ari
ableinyourprog r
am–
nopr oble
m, justmorena mestok ee
ptrackof.)
Ma kesuret hei
nputr out
ineisloadedat$300b ef
orerunningtheAppl eot
s
program. Notethatyouc anentercomma s,quote
s,<CTRL>C’s
,etc
.Onlyent e
ring
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
Thisc hapte
rwi llcha l
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 naf ewmi nutes’t
ime . Unfortunately,thel
isti
ngsforthi
s
chapterareabi tlongert hanus ual.Butchinup!Ther esultwi l
lbewor t
hi t
!I’
ve
recei
vedqui teanumbe rofr e
que stsforinfor mationonhowt oreadandwr it
e
ilesont hedi s
k.Thepr og ramsl i
stedwi l
lc ombi nema nyoft hetechniquesand
routi
nesy ou’vele
a r
ne dsof arintoas i
nglemi ni -
da t
abasepr ogram.1

The ir stprog r
a m s avesa nd l oadst he da ta by me ansofa s i
mple
BSAVE/BLOADo perati
on. Thisisfastandv 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]The
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 otware,1984,p.6-61)
,
<CTRL>D doesnotwor kwithPr oDOSc omma ndsfrom ass
emblyc ode.I
nst
ead,you
mustplac
et hecomma nds t
ringint nputbufe
heGETLN i rat$200a ndthencal
lthe
BASICInter
pret
er(BI)at$BE03.Thisisl
etasane xe
rcis
efortherea
de r
.

]
1
14 As
sembl
yLi
nes

6004: A9 10 23 LDA #$10


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

608B: 8D 00 71 HEX 8D00


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

]
1
16 As
sembl
yLi
nes

6111: 85 25 123 STA CV


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

6173: C5 08 179 CMP CTR


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

]
1
18 As
sembl
yLi
nes

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

Tounde rst
andhowi twor ks,considerthesec ondit
ions:
Da t
awillbestoredint heareaf r
om $1000- $14FF.Thisareaiscall
edab ufer
.
At otalofivestri
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
rangeinthebufe riscurrentlybeingac ce
ss edforapa rt
icula
rs t
ring.
Theba 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. Thee xe
cuti
onofDOSc ommandsf
rom as
sembl
yla
nguagebypr ec
edi
ng
phr
aseswit
ha<CTRL>D.
Tousetheprogra
m, c
alli
tdi
rec
tl
yfr
om BASI
CwithaCALL24576.
Ame nu
wil
lappea
rwiththe
sechoi
ces:
1) INPUT NAMES
2) PRINT NAMES
3) SAVE NAMES
4) LOAD NAMES
5) END PROGRAM

Totryther outineout,useopt i
on1t oent riv
e esamplenames.The nuse
option2t ov i
ewt heda tayou’
v eente
red.Youma yt
he nuseopt
ion3t osavethe
dataa sabinar yileonadi skett.The
e nr e
runt heprogram,andveri
fythatonly
thenumbe rs1t hroug h5exis
ti nthebufer(option2 .The
) nretr
ieveyourdata
byus i
ngtheLOADc omma nd( opti
on4 ),a
ndv iewa gai
ntoconirm as ucce
ssf
ul
load.
Inde
tail,
thisishowt heprog r
am works:
Atentry,PTRi ss ett
opoi ntto$1000,whe rethenamebufe rbe gi
ns.The
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 i
e stwa 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 e turn,ors etCHt o0 .
Not et ha tCV a ndCHa r et hec omput er’sv er ti
ca landhor 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.
Th 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 .Thi si smor e
simila rtot heBAS ICPRINTs ta teme nt,anda lsoa voi dss etti
ngupal otofs pe ciic
datat abl est odot hepr inting .
Onc et heme nui spr int edont hes cree n,l i
ne8 7oft hes our ceil edoe st he
JSRt oRDKEY. Thi sg etst hec omma ndk eyf rom t heus e r,whi chi st he nt estedby
theM1t oM6s er iesofc he ck s.
Ate 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. Thek eyi st henc ompa red
withe achoft heiv 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. Their 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 heiv es tr ing sc urr entlyi nme mor y.
AterDSPLY,PTR i si ni ti
a lizedt opoi ntt ot hebe ginni ngoft hebufe r
($1000) ,a ndt hec ount e riss e tt o0 .Thema 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 ta )Ate
. 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.
Thisi 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.
The r ea reaf e ws ubt lei temsi ntheIPr out inet ha ts houl dbenot ed. Their 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 .Thi 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 .
Thes 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 .Thi sisdoneonl i
ne s1 41−1 43 .

]
1
20 As
sembl
yLi
nes

Whe ni tr eturnsf rom IP, thec ounterisinc r


eme nteda ndc he c
kedt os eeifit
exceeds#$04.I fnot ,ILOOPr e
peatsunt iliv estri
ng sha v ebe e
ni nput .Ate rt he
iths tr
ingi se ntered, thepr ogramr eturnst otheme nu.
Ifoption2i se ntered,t heDSPLYr outineisc al
led.Thes ol epurpos eoft his
sect
ionist oc leart hes creena ndpr intt heiv ena me sinme mor y.Ate ntryt o
DSPLY,aJSR$FC58d oesaHOMEa ndt heCTRi sinit
ia l
izedt o0 .Asi nt heINPUT
sect
ion,CTRi sthe nus edt oc al
cula
tet heVTABpos iti
ont opr inte achline.
D1 i
st hepa rtt hata ctuall
ypr intse achlinebys canning( andout putt
ing
throughCOUT)a l
loft heby te
sa te achr angei ndica t
edbyPTR.Not et hata sa
saf
etychec k, ifa0di dnotha ppent obepr ese
ntduet os omeot hererror,e ve
nt u-
all
ytheY- Re g is
terwi l
lpa s
s#$FFa ndt hepr ogram wi llfallt
hroug htoDIFIN.
DIFINp rov i
de sa ne ndingc ar
r ia
g ereturntot hes tringa ndt heni ncreme nts
CTRu nti
la l
l iv estringsha vebe enpr inted.
Thel oad/ saveope r
a t
ionsa requi t
es imple.Knowi ngwhe r
et hebufe ri s
loc
a t
ed,thee ntirebl ockisa ccess
edbydoi nge it
heraBLOADorBSAVE. Re me mbe r
thatdiskc omma ndsa redonef rom a ssemblyl angua gej usta stheywoul dbe
donef r
om BAS IC. Thepr og ra
m ne edonl yout puta<CTRL>Df oll
owe dbyal eg a
l
DOSc omma nda nda<RETURN>.Ag ai
nt hePRINTr outinei sus edt ofa c
il
itate
thi
s.
Ifoption5i se ntered,t hentheJMPt ot heDOSBAS ICe ntryv ectorise xe-
cutedtoendt hepr og r
a m.

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

21 CURLIN EQU $75


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

]
1
22 As
sembl
yLi
nes

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

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

]
1
24 As
sembl
yLi
nes

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


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

61F5: 20 02 62 224 CLOSER JSR PRINT


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

Thet
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
ntsiv estri
ng s.InaBAS ICpr ogram, a
ll
thatwoul dber equire dtoaccessat extilewoul 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 extiles
.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 heile 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, aterthec omma ndi spri
nted,aJSRi sdone
tot heIPorDSPLYr outineasisappr opria
te.La s
tofa ll,aCLOSEisoutputbe fore
returningt otheme nu.

]
1
26 As
sembl
yLi
nes

Ac cor dingt owha twek nows of ar,t


heseshoul dbet heonl yc ha ng esneces-
saryt oa c cesste xtiles. Therei sonel astcat
chthoug h.
Appl eDOSc ompl icat
e sthing sbynota ll
owi ngt heus ert oOPENt extiles
from t hei mme diatemode .Whe nama c
hinelang uagepr ogr ami srunni ng,DOS
think swe ’res til
lint hei mme diatemodea ndwon’ tletusa ccesst het extiles.
Wha t’
sne e dedi sawa yt ofool DOSi ntothinkingwe ’rer unni ngapr og ram.
Thisi sdonebyus ingt hr eeinternalma nag eme ntl ocationsi nt heAppl e.
LANG( $AAB6)i swha tDOSus estok eept r
ackofwhi chl ang ua geisc urr entl
yrun-
ning .CURLIN( $75,$76)i sAppl esot’sregis
terfort heby t
esoft hepr ograml i
ne
numbe rc ur r
e ntlybe inge xecuted.Int heimme diatemode ,t hehi gh- orderby te
($76)d efaul t
st o#$FF. Appl esotc antelli
fapr og ra
mi sr unni ngbyl ook i
ngfora
non- #$FFv aluei nthi sl ocat
ion. Theot herwa yitk nowsapr og r
a mi sr unningi s
toc hec kl oc ati
on$33, whi chhol dst heAS CIIv a
luef ort hepr omptc ha ra
cter
. In
thei mme diatemodeofAppl e sot, t
hisis#$DD, equiva l
e nttot he‘ ]’ cha rac
ter
. In
ar unni ngpr og ram, thisc hang esto#$06.
Tof oolDOS ,allwene edt odoi sloadthe s
et hre eloc ationsa ppr opr i
atel
ya t
thebe ginni ngoft her outine.Fi nally,whene xit
ingt hepr og r
a m,r athertha n
usingas impl eRTS,t heJMP$3D0i se x
e c
utedtodoas otr eentryt oBAS C.Thi
I s
wi l
lr estoret heby t
e swe ’v
ea lteredtof oolDOSa nda l
s or eturnust ot hec urr
e nt
langua ge .3

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


ents umma ryof
ma nyoft hei de asandr outinesdi scussedsofar,a ndt he ya l
s opr ovideav aluable
mode lfory ourownpr og ra
ms .

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.Thea nsweri
sy e
sandno.Yes,t
hepa r
tofthecodethat
genera
testhee
rrorcanbeel
iminat
ed,butbeca
uset
hewrit
e-pr
otec
tswit
chisphys
ical
ly
wiredintotherecordi
nghead’
swr i
tesyst
em,youcannotdefe
atitwit
houtactual
l
y
re
mov ingoral
teri
ngtheswi
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 usttoknowa na r
bitr
ary
sel
e c
tionofopt ionsorc omma ndswhe nus i
nga nyt ool,progr
am,orpr ogra
m-
mi nglang uage
.Equa l
lyimpor t
a nta r
ethet ec
hni queswi thwhi c
htheoptionsare
combi nedt oachiev
ethede si
redr e s
ults
.
Wi tht i
mea ndpr ac
tic
ey ouwi l
lde v
elopy ourowns kil
lsa
tcrea
tingeic i
ent
assembly-languageroutines
,butt hatproc es
sc anbea s
sist
edbye xa
mi ningthe
techniquesthatothershavede velopedinpr evi
ouspr ogrammi ngeforts
.
Iha vetrie
di nthisbookt opr ovidear eas
ona bl
emi xofprogr
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
esent
edt
wopr intsubrouti
nesfortheoutputoft
exttot
he
s
cree
nordi skte
xtil
e.Thedi
sadv
ant ag
eofbot hr outi
nesisthatthe
ya r
enot
r
elo
cata
b l
e.Tose
ewhatt
hisme
ans,considerthefol
lowingprog
r a
m:
1 ********************************
2 *AL15-NON-RELOCATABLE PRINT DEMO
3 ********************************
4 *
5 *
6 * OBJ $300
7 ORG $300
8 COUT EQU $FDED
9 *
10 *
0300: 20 0D 03 11 ENTRY JSR PRNT
12 *
0303: 4C 0C 03 13 DONE JMP EXIT
14 *
0306: D4 C5 D3 15 DATA ASC "TEST"
0309: D4
030A: 8D 00 16 HEX 8D00
17 *
030C: 60 18 EXIT RTS
19 *
030D: A2 00 20 PRNT LDX #$00
030F: BD 06 03 21 LOOP LDA DATA,X
0312: F0 EF 22 BEQ DONE
0314: 20 ED FD 23 JSR COUT
0317: E8 24 INX

]
1
28 As
sembl
yLi
nes

0318: D0 F5 25 BNE LOOP


031A: 60 26 FIN RTS

Thi spr og r am,a swr i


tten,c a nr unonl ya tt hel ocations pe cie
i dbyt heORG
stat
e me nt ,int hisc ase$300. Thusi tisc alledno n- relocatab l
ec ode . Ma chinec ode
be c
ome snon- r eloc a
tablet hr oug ht heus eofa nys tate
me ntswhi chi nvol vea bs o-
lutea ddr e ssing .Themos tc ommone x ampl e sa ret heJMP a ndJSRc omma nds ,
andt heus eofda tastateme nt s,us ua l
lyi npr intr out ines.
Their sts ta teme ntoft hist ypeoc c ur sonl ine1 1.TheJSRt oPRNT( $30D) wi l
l
wor konl ys ol onga sPRNTi sa t$30D. I fther out inewe ret obel oa dedint ome m-
orya t$400 ( inste a
d of$300) ,t her out inewoul dt a
k et heJSR t oabl oc kof
none xiste ntc odea t$30D.
Li kewi se,t heJMPonl ine1 3ha sthes amedi ic ult
y, asdoe st heDATA,Xs tate-
me ntonl ine2 1 .Anya ttemptt or unt hec odea ta na ddr essot hert han$300wi l
l
resultindi sa ster.
Its houl dbenot ed,howe v er,tha tnota llJSRsa ndJMPsa r euni versa l
lyt rou-
blesome .TheJSRCOUT( $FDED)wi llex ecut epr ope rlynoma tterwhe ret heobj ect
codei sl oc ateds inc ether eferenc ei st oal oca ti
onout s i
deoft heobj ectc ode
block.
Theg e ne ra lrulet he ni st ha ta nyc odewhi chma k esr efer encet oa bs olut e
addr esseswi thi ni tselfwi llnotber eloc ata ble,whe r ea
sc odet ha tdoe snots ufe r
from thi sl i
mi tationc a nber una ny whe rei nme mor y .
Thepr obl em ofr eloc atabi l
ityma ys e ems lights inc ea nyg ivenr outinei s
usua l
lyde sig ne dt obeputa tade ini tel oc ation( us ual
lye i
thera t$300ora tt he
topofme mor y)a ndt he npr ot ectedv iat heAppl esotHIMEM:s ta t
eme nt .How-
ever,a st henumbe rofr out ine sy ouus ei nc re ases,y ouwi llenc ount ermor ea nd
mor ec onli c t
sbe t
we enr out ine sor igina llywr ittent ooc c upyt hes ameme mor y
rang es.Ina ddi tion,ita lsoi soc c asiona llyde sirablet odi re ct
lya ppendma c hine
codet ot hee ndofAppl esotpr og rams ,whe ret he ywi llloa tupa nddowni n
me mor ya tt hee ndoft heBAS I
C por tionoft hel is
ting ,be inga utoma ti
c ally
mov e da sl ine sa rea dde dorde l
ete d.
Fort he s er ea sons,iti sbe tteri nthel ongr unt owr it
ec odet or una ny whe re
inme mor ywhe npos sible,t husa voidi ngf utur ehe adache sa boutwhe retoput
everything .
Ther ema inde roft hisc ha pterwi lldi scus st hev ar
iouswa ysofa voidingt he
useofa bs olut ea ddre ssi
ng ,t husc r
ea tingc odet hatc anbeus e da nywhe rei n
me mor yr eg ardl essoft heORGs tateme ntus eda ta ssembl ytime .

J
MPComma
nds
Thi
sisa
nexampl
eofacommonuseoftheJMPcommandt
ojumpov era
r
ang
eofmemor
y,her
ere
pre
sent
e heil
dbyt lsec
ti
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

Notic
ethatbyc l
eari
ngthec a
rrya ndthenimme di
atel
yexecuti
ngtheBCC,
t
hes amer es
ultisobta
inedaswhe ntheJMPc omma ndwa susedinthee a
rli
er
l
ist
ing.
Thema i
nc a
utiontoobser
veisthatthefor
c e
dbranchcannotbema deover
adistanc
eofgreate
rthan127by t
es,al
thoughmos tas
semble
rswillgi
veanerror
atassemblyti
mei fthi
sisatt
empt e
d.I naddit
ion,si
ncethecarr
yi scl
ear
edt o
f
orcet hebra
nch,routi
nesthatsetorc l
earthecarr
ytoindicat
ec e
rtai
ncondi-
t
ionsma yhavecompa t
ibi
li
typrobl
emswi ththi
sapproac
h.

]
1
30 As
sembl
yLi
nes

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


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

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

De
ter
mini
ngCodeLoc
ati
on
S
olvi
ngtheJMPpr oble
mi sonlythebeginningoft hetas
k.Veryotenitis
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
,sometec
hniqueisrequir
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

Thes uc ces softhi srout i


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

]
1
32 As
sembl
yLi
nes

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

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

Not ethatt hea ddr esss t


or ed, $302, ist hel as
tby teoft heJSRc omma nd–or ,
puta notherwa y ,oneby tele sstha nt hea ddr e s
soft hene xtimme di
atec omma nd
foll
owi ngtheJSR.
Uponr eturnf rom t heJSR, theS tackPoi nterist r
ans f
e r
redtotheX- Re gi
ster
wi t
ht heTSXc omma ndonl ine1 3.Be caus et heStackPoi nt e
risa tt
hene x tavail
-
ableby teont hes tack,thiswi lla lsopoi nta tthehi gh-or derbyteoft her et
ur n
addr essst
il
ll eti nme mor yt he re.Thi sisr e t
rievedwi tht heLDASTCK,Xonl ine
15a ndputi nat empor arypoi nterloc ati
onPTR+1( $07).TheX- Reg i
st
e ri st
he n
de c
reme nteda ndt hel ow- or derby t
er etri
e ve dandputi nPTR( $06).
Theina lRTSoft her out iner e t
ur nsc ont roltothec aller,a
twhi chpoi nt$06,
$07ma ybee x ami nedt ov erifyt hes uc cessfulde t
er minationoft hea ddress$302.
Youma ywisht or unt hisr out i
nea tse ve
ra ldi ferentlocationsinme mor yt over-
ifythatineac hc asePTRi spr ope rlyse ttoENTRY+2. Wha ty ouha vetheni sas hort
routinewhi chc ande t er
mi newhe rei nme mor yi tisc urrentl
ybe ingr un.The
onlydi sadvanta get ot hisr out inei st hatt hehi gh- orderby teisre tr
ieve dir st
,
thusc ompl i
c at
ingt hing si fwewa ntt oa dds omeofs etv al
uet ot her et
ur n
addr essThede
. sir
a bi
lityoft hi swi llbes howns hortly.Intheme ant
ime ,c onsider
thisalter
edv ersionoft heLoc ator1r outine :
1 ********************************
2 * AL15-LOCATOR 2 *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *
8 PTR EQU $06
9 RTRN EQU $FF58
10 STCK EQU $100
1
5.S
pec
ial
Prog
rammi
ngTe
chni
que
s 1
33

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

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

]
1
34 As
sembl
yLi
nes

0328: D0 F6 37 BNE LOOP ; ALWAYS UNTIL 255


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

Aterc all
ingthedummyr eturnsta
teme nt,af orcedbr anchov ertheda t
a
sect
ionisdone .Thiswi l
lhav enoe fectont hea ddressr ema i
ningont hesta
c k
.
AtCONT,wet aketheg eneralpr ocedureusedi nLoc ator2a nda ddt heCLCa nd
ADCs t
ateme nt
sne ededt oadda nofsettot hea ddr e
ssont hes tack.Wha twe
needisthes ta
rti
nga ddressoft heAS CIIdatat obepr inted.S i
ncet heda t
astart
s
at$306 andt hea ddressont hes t
ackis$302( se eearli
ere xampl es)theofs e
t
neededis#$04.
Thi
sma ys eema rbit
rarybutt hev al
uet oa ddwi llalwaysbe#$04i fyou
alwaysdot heCLV,BVC$XXXXb r anchimme diatel
ya tert her e
turn.The nfoll
ow
thatwit
ht heda t
atobepr inted.
Oncet hea c
tualaddressoft heASCIIda taha sbee nc al
culated,itispri
nted
inthePRNTs ecti
onbyus eoft hei ndexedpointera tLOOP.

J
SRSi
mul
ati
ons
Youmi ghtg ettheimpr ess
ionf rom thea bovee xamplethatatremendous
codee xpa
nsiontakespl a
c et oaccompl is
ht herel
oc a
tabili
tyofaprogra
m. Thi
sis
some whattr
ue ,butitde pe ndsonhowy ouwr i
tethepr ogram.TheuseofCLV,
BVC$XXXXt akesonlyt hr
e eby te
swhe r etheJMP$XXXXi twasrepl
aci
nga l
soused
threebyte
s.
Thestackope ra
tionsj ustdiscuss
e dtak easma llnumbe rofbyt
estoimple-
me ntbutcouldbecomer athe rl
argeifus edma nytime s
. Whatisnee
dedisawa y
toputthestackope r
ationsi nas ubroutine.Unfortunately
,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 *

Thisroutinei sverysi
mi la
rtot henon-r
elocatableJMPde moprese
ntedear
-
li
er,
withthee xcepti
ont hatthecall
totheBELLr outineha sbe
e nmadeasubr
ou-
ti
neits
elf
,he a
de dbyt helabelTEST.Inthi
sli
sting,TESTi sfol
lowedbyadummy
NOPsta
teme nt
, butwe ’
llil
lthatinshort
ly.
Thisprog ram,a swr i
tte
n,c anrunonlya tthea ddresss
pecie
i dintheORG
st
atement.Herei sanimprov edversi
on,usi
ngas imul ati
onoftheJSRcommand:
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 *

Thispr
ogra
mi sve
rysimi
la
rtot
hePri
nt1prog
ram,wit
htwoexce
ptions
.
Fi
rst
,#$03isa
ddedins
tea
dof#$04t
othea
ddr
essonthes
tac
k.Thi
sisasubtl
e

]
1
36 As
sembl
yLi
nes

pointwor t
hme ntioning ,andy ous houldr eviewt hel is
tingsunt i
ly ouf eelc om-
fort
ab l
ewi thwha ti sbe i
ngdone .Re me mbe rt ha tther eturna ddres sf 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 essoft heir s
tc hara cterof
thestringt obepr inted.He nc ethedi fe r
enc eint heofs e
tv a
lueus edi ne ach
case.
Onc et heofs etha sbe ena ddeda ndt hepr operr e t
urna ddr essca l
cula t
e d,the
FIXs ecti
onus e
st hePHAc omma ndstoputt he seont hestack .Thuswhe ntheRTS
isencount ered, thepr ogr amr 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 ogr ammi ng .Their 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 .
Thec onv erseoft hisisus ingt woPLAswi thi
nar out i
nec a l
ledbyaJSRt o
avoidr eturni ngt ot hec a l
linga ddre s.Thi
s sise qui val
entt 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 outinest omi nimi zet hee xtra
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

Thi
sr outineha sthea dvantageofa l
lowingt hePRINTs tate
me nt
stobeus e d
verymuc ha sthoug hthe ywe reint henon- relocatabl
ev ersi
ong i
veninc hapte r
13. Thee x
t rabytesrequi r
edf orthes tackcalcula
tionsa reconinedtoonepl a
c e,
andt herea reonlythreee xtrabytespe rli
netobepr inte
d, c
ompa redtothec hap-
ter1 3routine.
Thereturntot hee ndofe a
chpr intedstri
ngi sa cc
ompl is
hedbyus i
ngt heY-
Re gist
erinFIX. Ate ntryt oFIX, t
heY- Regis
terwi llholdthelengt
hoft hes t
ring
printed,whi chisthe na ddedt oPTRt oc alc
ulatethepr operaddresstoret
ur nt o.
Ag ainweus ethetwoPHAsf oll
owe dbya nRTSt oa ccomplis
ht heret
urn.

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

]
1
38 As
sembl
yLi
nes

wast
hist
ypeofst
ate
me nti
ntheve
ryirstpr
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

Thi
sprogr
am wi
lla
ctua
ll
yre
writ
ethea
ddre
sspr
esentonli
ne22fort
heLDA
DATA,X s
tat
eme .Theme
nt t
hoduse
stheaddr
essonthestac
kt oca
lcul
at
ethe
addressfort hebe ginni ngoft heAS CIIstr i
ngt obepr inted.Itisthisaddr essthat
wewi l
lwa nte vent ua l
lyt oputi ntothec odea t$30F,$310t ore wri
tet heda t
a
sta
teme nt.
Ate rc alculatingt hea ddressinlines3 0−3 9,ther e
sultisstoredi nPTR.The
FIXs ecti
ont he na ddst helengthoft hepr inteds tringpl usiv ea ndus e sthisas
theY- Registerofs ett oina l
lypointto$30F.Thel ow-a ndhi g
h-or derby tesare
thenwr it
tent ot hec odea ndar et
urndonet ot heac tualPRNTr outine .
Thise xa mpl ec ome swi t
hma nyc a uti
ons .Thev al
ueonl ine4 2mus tbe
appropr i
atet ot hel e ngthoft hes t
ri
ngbe ingpr inted.Al so,theor 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 erconstructingapr 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 lluntilyoua r
ev ery
surewhye ac hs tepwa sdone .Tov er
ifyi t
sv ersat
ility,yous houlda s
se mblet he
codef ort hise x ampl ea ndt henr uni ta tsev e
r a
ldi ferentme mor yl ocati
ons.
Ate 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.Thea dde da dvantageoft hisc omma ndi st hatthe
pointerne e
dnotbec reatedont hezeropa ge,whi chalreadyisi nhig hde ma nd
fornume rousothe ruses.1The basi
csyntaxfort heindire
ctjumpi s:
0300: 6C FF FF 99 J1 JMP ($FFFF)

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

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

]
1
40 As
sembl
yLi
nes

10 RTRN EQU $FF58


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

Thes ys temi sfairlysimpl e,basicallyjustus ingthes tackt og etaba seaddr ess


andt hena ddi ngwha t
ev erthedi st
a ncei sbe twe enthee ndoft heJSRRTRNs t
a te
-
me nta ndt hede stinationoft heJMP().Aswi thsomeoft heot he rsy ste
ms ,
thoug h,t hisdi s
tanc ewi llchang ea sc odei sa ddedorde letedbe t
we ent het wo
point s.Youma ythusha vetoc hang et hev aluesonl ine s20a nd2 4r athe rfre-
quent lytok eepupwi t
hy ourc odec ha ng es.
Howe v er,itdoe sa voidthepr oble msa s soci
atedwi thma nys t
eppi ngpoi nts
sprink ledthr oug houty ourc ode ,aswoul dbene ces
saryus ingt heot hera lterna-
tiv
e.
The reisonebugi nt heus eoft hei ndi rectjumpt hats houl dbeme ntione d. I
t
ispr esenti nt he6 50 2mi c
ropr ocessori t
self,andoc cur swhe nev ert hei ndire c
t
point ers t
ra ddlesapa gebounda ry
.2F ore xampl e,ifyouus edt hes ta te
me ntJMP
($06), t
hede st
inationwoul dber etrievedf rom locati
ons$06a nd$07. Howe v er,
ifyouwe ret ous eJMP($3FF), thede s
tina ti
onwoul dber et
rieve dfrom $3FFa nd
$300. Thehi gh-orde rby teisnotpr ope rlyinc rementedbyt he6 502. Thisi sus u-
all
ynotac onc ern,thoug h, si
nc ethe rea rege nerall
yma nya lternateloc ati
onsf or
thede st
inat i
onpoi nter.
Inc onc lusiont hen, cert
a i
nt echni que sca nbeus edt opr oduc ec odewhi chi s
notr estri
cte dt or unni nga tapa rticulara ddr essinme mor y.Al thoug habi t
harde rtoc ons tr
ucti niti
a l
ly,ands l
ig htlylarge rintermsofina lme mor yre quire-
me nt s,t
hepr oduc tisg ene r
allymuc hmor eve rsa
til
einitsa ppl i
c a
tions .

CT]Thi
2[ six
sbugwa edi
nthe6
5C0
2.
Volume 2

]
1
6
1
6. Pas
si
ngDat
afr
omAppl
estBAS
o IC
J
anua
ry1
982

One usefula ppli


cat
ion ofa ssembly-la
nguage programming isint he
enhancementofy oure x
ist
ingAppl e
sotpr ograms.Somepe opl
eareincl
inedt o
writeal
ltheirpr ogramsina s
semblylanguage,butitma ybemor eeicienton
occasi
ontowr it
e“ hybri
ds”–pr ogr
amst hatareacombina t
ionofApple
sota nd
ass
e mblyla
ng uage. I
nthiswa y,pa
rti
cularfunct
ionscanbedonebyt hel
ang uage
bestsui
tedtothepa r
tic
ulartask.
Ifyouhadt owr i
teas hortpr
ogramt os t
oretenname s
,itwouldbebe stto
doitinApples ot:
10 FOR I = 1 TO 10
20 INPUT N$(I)
30 NEXT I

Thisismuc hs implertha
nt heequi
vale
ntpr ogra
mi na s
sembl
yl ang
uage.In
ca
seswhe rene i
thers pee
dnorpr ograms i
zeisac oncern,Apple
soti sacom-
pl
ete
lyaccept
a blesoluti
on.
Howe ver
, ifyouha 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 ouhave
al
rea
dyc ombine dAppl eotwi
s 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.Ateryoupr essakey,
allt
extont hescr
eenate rtheir s
twor d“THIS”iscl
eared.
Nowa l
thoughitmi ghtbepos si
bletoaccompli
sht hesamee fec tinAppl e-
sotb ypr i
nti
ngma nybl anklines,i
twoul dnotbeasfastora seicie ntinterms
ofcodea stheCALL-958.
I
ne xecuti
ngthea bovepr ogram,theApplesotinte
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 ntheina 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.Ther 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
Thee
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
,let
’s
r
esurr
ectt
hetonerouti
nefrom chapt
erei
ght.
Tousethis
,as
s e
mblet hecodeandpla
cetheina
lobje
ctc
odeat$300.The
n
e
nterthea
ccompanyingAppl e
sotprogr
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

Thi
sAppl
esotpr
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

TheAppleotpr
s ogra
m wor ksbyirstrequesti
ngval
uesforthepi t
cha nd
durati
onofthetonefrom t
heuser.Thes
ev a
luesa r
ethenPOKEdint
ol oca
tions6
and7a ndt
hetoner out
ineCALLed.Thetoner out
ineuse
stheseval
ue stopr o-
ducet hede
sir
eds oundandt henretur
nstot heCALLi
ngpr og
ramf oranot her
round.
1
6.Pa
ssi
ngDa
taf
rom Appl
esotBAS
IC 1
45

Thi
ste
chni
queworksineforli
mit
edappl
ic
ati
ons.Havi
ngtoPOKEal
lofthe
des
ir
edpar
ameter
sint
ovari
ouscorner
sofmemoryisnotlex
ibl
e,howe
ver,
and
st
ri
ngsa
renear
lyi
mpossi
ble
.Theremustbea
nalt
ernat
ive
.

TheI
nte
rna
lSt
ruc
tur
eofAppl
esot
Ify
ou’vebeenfoll
owinga l
ong,you’venodoubtiguredoutbynowthatI’
m
agre
atbel
ieve
rinus i
ngroutinesal
readypres
entintheApple,whe
reposs
ibl
e,to
ac
compli
shapa rti
cula
rtask.Sincerouti
nesalr
eadyexis
tinAppl e
sotforpro-
ce
ssi
ngvari
able
sdirect
ly,
whynotus et hem?
Toanswerthis
,wemus ttakeabr i
efdet
ourtooutli
nehowAppl e
sota c
tu-
al
ly“
runs”aprogram.Considerthi
ssimpleprogr
am:
10 HOME: PRINT "HELLO"
20 END

Ate 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.
Their stquest
iontoa nsweris,exactl
ywhereint hecomputeristhepro-
grams tored?Thiscanbefoundbye nteri
ngtheMoni t
orandtypi
ngin:67 68
AF B0a ndpr e
ssi
ng<RETURN>.
Thec omputershoul
dr es
pondwi th:
0067- 01
0068- 08
00AF- 18
00B0- 08

Their
stpairofnumbe rsisthepoi
nterfortheprogr
am beginni
ng–byte
s
rev
ersedofcours.The
e yi
ndicatethatt
hepr ogramstar
tsat$801.Thesec
ond
pai
ri st
heprogra
me ndpointe
r,andtheyshowt hati
tendsat$818.Usi
ngthi
s
inf
ormati
onlet
’sexaminetheprogra
m databytypingi
n:
801L

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

]
1
46 As
sembl
yLi
nes

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


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

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

Thi
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
tencode sea
c hk e
ywordasas i
ngle
-bytet
oken.
ThusthewordPRINTi sst
or e
dasa$BA. Thi
sdoe swonder
sforconse
rvi
ngme m-
ory.Inaddit
ion,the
reiss omeoverheadassociat
edwithpackag
ingtheli
ne:a
byt
et osig
nifytheendoft heli
ne,afewby t
e satthebegi
nningofeachli
neto
hol
di nf
ormationrel
ate
dt oit
sle
ngth,andthelinenumberit
sel
f.
Tobemor espec
iic:
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

Their sttwobyte sofeveryli


neofa nAppl eotpr
s ograma rea ni ndext othe
addressofthebe gi
nni ngofthene x
tline.At$801, $802wei ndt hea ddr es
s$810
(bytesreverse
d).Thisi swhereline20s ta
r t
s.At$810weindt hea ddr ess$816.
Thisiswhe rethenex tlinewouldstartift
he r
ewe r
eone .Thedoubl e$00a t$816
tel
lsAppl eott
s hatthisistheendoft heBAS I
Cl is
ti
ng .I
tisimpor tantt or eali
ze
thatt he$00 00 endoft heAppl e
sotpr og ram usually
, b
utno ta lwa y s
,c orr
e-
spondst othec ont
ent sof$AF,$B0.Itispos s
ibletohidema chine -
lang uagec ode
betwee ntheendofthel inedataandthea ctualenda si
ndicatedby$AF, $B0–b ut
mor eont hatlat
er.
Thene xtinfor
ma tionwithinali
nei sthelinenumbe ritsel
f:
0801- 10 08 0A 00 97 3A BA
0808- 22 48 45 4C 4C 4F 22 00
0810- 16 08 14 00 80 00 00 00
0818- 8C
1
6.Pa
ssi
ngDa
taf
rom Appl
esotBAS
IC 1
47

The$0A00i sthet
wo-bytef
orm ofthedecimalnumber10
,thelinenumber
oftheirstli
neoftheApplesotprogr
am. Li
k e
wise
, t
he$1400isthedatafort
he
l
inenumbe r20.Thebyte
sa rea
gainrev
e r
sed.Aterthes
efourbyteswese et
he
act
ua lt
okensforeachl
ine.
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

Allby teswi t
hav alueof$80org rea t
e ra reAppl esotk eywor dsi ntoke n
form.By tesl esst ha n$80r epresentnor ma lAS CI Idata(let
tersoft hea l
phabe t
,
fore xampl e ).Ex ami ningt heda tahe r
ewes eea$97f oll
owe dby$3A.$97i sthe
toke nforHOME,a nd$3At hec olon.Ne xt,$BAi st hetokenf orPRINT.Thi sisfol-
l
owe dbyt hequot e( $22) ,thet extforHELLO( $48454C4C4F) ,a ndt heclosi
ng
quot e($22) . Lastofa l
l,the$00i ndicate
st hee ndoft heline.
Inlinenumbe rtwe nt y,the$80i sthet ok enf orEND. Asbe f
ore,thel i
neister-
mi na t
edwi tha00.
Whe napr ogrami se xecuted,theinte rpre t
e rscansthroug htheda ta.Eac h
timei te nc ount ersat ok en,s ucha sthePRINTt oken,itlooksupt hev al
uei na
tablet os e
ewha tactions houldbet aken.I nt hec as
eofPRINT,t hiswoul dbet o
outputt hec har actersfollowi ngt hetoken, name lyHELLO.
Thisc ons tantt ransla t
ioni sther e
asonf ort heus eofthet ermi nterpr
eterfor
Appl esotBAS IC.
Ma chinec ode ,ont heot he rhand,i sdirec tl
ye x
e c
utablebyt he6 502mi cro-
proc essora ndhe nc eismuc hfaster,si
ncenot a blelookupsa rerequired.
InAppl esot,as ynt axe rrorisg ene ratedwhe neveras eriesoft okensi s
enc ount er
e dt ha tisnotc onsistentwithwha tthei nte
rpreterexpectstoind.

Pa
ssi
ngVa
ria
ble
s
S o,ba cktot hepoi ntofa llthis.Thek eyt opa ss
ingv ar
iablestoy ourown
ass
e mbl y-l
ang uag er outinesi stowor kwi t
h Appl esot 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 eyir
r stonede ine diny ourpr ogr
am
(se
et heinputr out i
ne .Thi
) sisok ay,butr atherr est
ricti
ve.A be t
terwa yisto
namet hev ari
abley ou’rede al
ingwi thr ightint heCALLs tatement .
Thei mpor t
a ntpoi ntsherea retwoc ompone ntsoftheAppl esotinterpre
ter:
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 esotpr 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

TheAppl
esotc
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

Thisi
samuc hmor eeleg antwa yofpa ssingt hev a
luesanda lsor equiresno
mis c
ell
aneousme moryl ocationsa ssuch( al
thoug hforpur posesofs implicit
ythe
tonerouti
nei tselfsti
llusesthes amez ero-pagel ocations.
)
Thesecrett ot hene wt ec hniquei stheus eoft her outi
neCOMBYTE( $E74C).
ThisisanAppl esotr outinewhi chc hecksforac ommaa ndthe nre t
urnsav al
ue
between$00a nd$FF( 0−2 55)int heX- Reg i
ster.
Iti
snor ma l
lyusedf ore valua t
ingPOKEs ,HCOLOR=, ands oforth,butdoe sthe
jobv e
rynicelyhe re
.Ita ls
ol eav esTXTPTRpoi nt ingtot heendoft hel ine( ortoa
coloniftherewa sone )byus ingCHRGET t oa dv anceTXTPTRb yt henumbe rof
charact
ersfollowi nge achc omma .Notea ls
ot ha ta nyl e
gale x
pr e
s si
on–s ucha s
(X−5 )
/2–c anbeus edt opa sst heda t
a.
Tov e
rifyt heimpor tanc eofma nagingTXTPTR, t
r yputt
ingas impl eRTS( $60)
at$300.Ca l
lingt hisy ouwi llg etaSYNTAXERROR,s inceuponr etur nAppl esot’s
1
6.Pa
ssi
ngDa
taf
rom Appl
esotBAS
IC 1
49

TXTPTRwil
lbeont heir s
tc ommaa tertheCALL,a
ndt hephrase“,P,D”isnota
le
galAppleote
s xpre
ssi
on.
Whatabouttwo-bytequa nti
ti
es?Tode a
lwiththem, anumbe rofothe
rr ou-
ti
nesareused.Fore xa
mpl e,thi
sroutinewilldothee quiv
ale
ntofat wo-byte
point
erPOKE.Supposeforins t
anceyouwa nte
dt os
toretheby t
esfortheaddr e
ss
$9600atl
ocati
ons$1000, $1001.Nor mall
yinApplesoty 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
*
*

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

]
1
50 As
sembl
yLi
nes

030B: 85 3C 20 STA PTR


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

Thes pecialitemsint hi sr outinei ncludeCHKCOM,as ynta x-che ckingr outine


tha ts ervest wopur pos e
s .Fi rsti tv eriiest ha tac omma ndf ollowst heCALL
addr ess, ands e c
ondlyita dv anc esTXTPTRt opoi ntt ot heir stby teoft hee xpre s
-
sioni mme diatelyfoll
owi ngt hec omma .Ifac ommai snotf ound,aSYNTAX
ERRORi sg ener at
ed.
FRMNUMi sar outi
net ha te valua t
esa nye xpressiona ndput st her e
a lloa t
-
ing -pointnumbe rr e
sulti nt o Appl esot’slo at
ing -
p ointAc cumul ator,usua ll
y
calledFAC. Thi sisasix-byteps eudor egis
ter( $97−$9C)u sedt ohol dt heloa ting-
poi ntr epre s
ent ati
onofanumbe r.I tinclude ssuc hni cet
ie sa st hee xpone ntia
l
ma gnitudeoft henumbe ra ndt hee quiva l
entoft hedi gitsoft hel og arithm oft he
numbe rstored.
Att hiss t
a geyou’dha v etobes ome thingofama sochi sttowa nttode alwith
thenumbe rini tscurre
ntf or m, s
ot hene xtste pisus e dtoc onv e r
ti tintoat wo-
by teinteg er.
GETADRd oesthisbyput ti
ngt het wo- byter esultintoLINNUM, LINNUM+1( $50,
$51) .
Ev eni fthi sisnote xa ctl
ya ni n- depthe xplanationofa llt hemos tpre c
ise
de tail
soft heope r
ati
on,t hebot tom l inei stha tthet hr eeJSRs( CHKCOM, FRMNUM,
andGETADR)wi l
lalwaysre sultint hel ow- orde randhi gh-or de rby tesofwha tever
ex pressionf ollowsac ommabe ings toredi nLINNUMa ndLINNUM+1.
The ses impl esubroutine ss houl dbequi t ea dequa tef orma nya pplicat
ions.
Ne xtcha pter,howe v
er,we ’lllooka ts tri
ngpa ssing,s omeot he rus efulrout i
ne s
,
andhowt opa ssda t
aba ckt ot heCALLi ngAppl esotpr og ram.
1
7
1
7. Mo
reAp
ple
sotDat
aPas
si
ng
Fe
brua
ry1
982

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

Appl
esotVa
ria
ble
s
The rea res ixt ypesofv ariablesinAppl e sotBAS IC.The 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 tir stt akeamome ntt oe xa mi net hedi fe 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 and−1 0 3
8
,whi cha r ev e ry
larg epos iti
vea ndne gativenumbe rs.Ina ddi ti
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 eof−3 27 67t o+3 276 7.The 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 beticc ha rac t
er( At oZ)f ol-
lowe dbyal ett
e rornumbe r( At oZor0t o9) .Anyc ha racte rsa tert heir sttwo
arei gnor edwhe nAppl esotl ook supt hev al
uef ort hev aria ble. I
nt e
g erv ariabl es
ares imi lar,butt hena mei ssuix edbyape rc ents ign( %).ThusAwoul dr epr e-
sentt her e alv
ar iable,whe re asA%woul dr epres enta ni ntege rv ariable.
Whe npa ssingda t
as uc ha same mor ya ddr essoras ing le-by t
ev aluet 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 esotr 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 etrievea ni nte gerv ar
iablef rom a
callingBAS I
Cpr og ram.
S t
ringv ari
a blesc ons i
stofas eriesofa nyl eg alAS CI Ic ha rac t
ers,wi t
hama x-
imum l eng thof2 55c ha racters.Stringsa reindi catedbya$s uixt othev aria ble
na me .

]
1
52 As
sembl
yLi
nes

Anyoft hesevariable
sma ybepr ese
nte i
thers
ing l
yorinana 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.Arrayvari
abl
esa reindi
catedbyapa i
rof
pa renthe
sesfoll
owi ngt heva ri
ablena mebe t
weenwhi chanumbe rorex pressi
on
ma ybeus e
dt ospecifythede si
rede lement.
Youpr obablya rea l
readys ome whatfamili
arwi t
htheg e
ne r
alpoint sme n-
tione dsofar;they’reraise
dnots omuc htot e
achy ouaboutAppl e
sotv ari
able
type sassuchbutr athertose tthes t
ageforwhatistofoll
ow,name l
yhowe a
chof
the sevar
iabletypesisstoredwi thi
nt heme moryoftheAppl ecomputer.

Me
mor
yMa
ps
Inc ha pteronewepr es
entedag raphi crepresentat
ionoft heme mor yus ag e
ofthec omput er.We ’
dl iketor evivethet opicint heinterestofourc urrents ub-
ject
.
Ame mor yma pi susedt os howt her elat
iveplaceme ntofda tawi thint he
avail
ableme mor yl oca t
ionsi 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.
Thec ha rtinTa ble1 7-1s howsat ypicalAppl eme mor yma p,wi thDOS
booteda nda na rbi
trar yAppl eotpr
s og rami nme mor y.
In pr e vi
ousc hapt er
s,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 ddr essi
ng,andt hatAppl esotBAS I
Ci sstore dinROM be gin-
ninga t$D000. TheMoni t
orROM be ginsa t$F800.
A nor malAppl esot pr og r
a ms tartsa t$800,wi tht hehi g
he sta vail
a ble
addressus uall
yjustbe l
ow$9600, whic hisi dentiiedwi t
ht hel owerbounda ryof
theDi skOpe rati
ngS ystem( DOS )
.
Thea reaf r
om $300 t o$3CF i sa vail
a bleforus erassembl y-
langua gepr o-
grams .$3D0t o$3FFi sreservedforAppl es yste
mv ect
ors,suc ha stheDOSe nt ry
vectors.Ze r
opa g e
,t hes tack,andt hei nputbufe ra l
soha v ebe endi scussedi n
somede tail.
Sinc eourma i
nc onc ernisi nthea reaofAppl esotv ari
a bles,l
et’
sc ons i
de ra
revi
s e
dma pe mpha sizingAppl esotpr og rams .
Ta ble1 7-2s howst hatwhe na nAppl esotpr ogrami sRUN,s i
mpl e( non- ar-
ray)va riablesa r
epl ace dimme diatelya te 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 Bufer Pa
ge Di
spl
ay Pr
ogra
m BAS
IC ROM

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

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

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

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


$AF,$B0 LOMEM: HIMEM:

Ta
ble17-
2:Appl
esotMe
mor
yLa
yout

l
owe dbyt hea r r
a yv a riables.Be c aus et heda taf ore achs tri
ngv a riablei se v
e 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. Thes pa cei nbe twe ent he sec onv erg inga re asist hes o-c al
ledf ree
spa c eoft hes y s
t e
m.
HIMEM:a ndLOMEM:a reus edbyt heBAS I Cpr og r amme rtos ett heuppe ra nd
l
owe rboundsofv ariables t
or age .Ifnots peciic a ll
yde cl
are dwi thint hepr og ram,
the sede fa ulttot hebot tom ofDOSa ndt hee ndoft heAppl e sotpr og ram,
res pe ct
iv ely.The ydono t,howe ve r,alwa ysha v et ober es tr
ictedt ot hesel 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 fec tedbyt her unni ngpr og ram.
Thi si sdonef oroneorbot hoft wor ea sons :ir stt opr otec teitherorbot hoft he
hi- resdi spl aypa ge sfr om v ariabl et ablee ncroa c hme nt ;or ,second,t opr ov idea
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 ar i
ablei ss tor edi nt he
comput e r,let’
se xa mi net hef orma toft hei nf or ma tionf ore achv a ri
a ble .
Wi t
hin
thea rea sindi cated, av ar i
a blet ablei sc ons truc tedt ha tcont a i
nsbot ht hena meof
theg ivenv a r
iablea ndi tsv aluei ft hev a riablei sar ea lori nt eger.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
te rs).
Figur e1 7-3s umma r izesthede tailsoft hef or ma tf ort he s
et abl es.
Eac ht imeav a riablei sir ste nc ount eredi nar unni ngAppl e sotpr ogra m, an
ent ryi sma defori ti nt hev ariabl eta ble. Fors impl ev ariable s
, Appl esotl ook sto
thepoi ntera t$6B,$6Ct os eewhe ret hee ndoft hec ur rents impl ev ariablet a
ble
is.I tthe nope nsups evenby te sfort hene wv aria blea ndput sabl oc kofda tas i
m-
il
a rt ot hats howni nFi gur e1 7 -3, asi sa ppr opr iatet ot het y peofv ar i
a blede ine d.
Rea lv a r
iabless t
or et hev aluei nal oga rithmi cf orm,whe ree ac hv a lueis
indi ca t
e dbyt hee x pone nta ndf ourma nt issas.I nteg erv aria blesre qui reonl ythat
thehi gh-a ndl ow- orde rby tesoft hev a l
uebes tore d.Ther 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 esenta ti
on oft hev aluei s
rev er s
edf rom wha twewoul dnor ma l
lye xpe ct.Tha ti s
,t hehi gh- or derby teis
pla ce dir st, f
ollowe dbyt hel ow- orde rby te.

]
1
54 As
sembl
yLi
nes

Fors tring sonl yt hr eeby tesofi nforma t


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

gr
am, e
a c
hs i
zeindic
atorisatwo-byt
epa i
r,alt
houg hinthi
scasethehighbytei
s
al
waysgive nirst.
I
mme diat
elyatertheheaderarethea c
tualdatablock
s,eachblockconsi
st-
ngofiv
i 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
a ywit
hthisst
atement:
DIM A$(10,10)

t
hehe
ade
rbl
ockwoul
dlookl
iket
his
:

whe re$41,$80a retheAS CI Ival


uesf oranAf oll
owe dbyanul l.Thehighbitis
ofi nt heir stc haract
er,andoni nt hes econd–i ndic
ati
ngas tri
ng.Thene xt
arr
a yv ari
ablewoul dbef ounda tthea ddressoftheir stnamec har
acte
rpl us
$174. Therea ret wodime nsionstot hea rra
y ,asindicat
edbyt he$02.The$00
$0Bindi cat
ese l
ev enele
me ntsine ac
hdi mensionoft hearr
ay.Thisshouldnotbe
surprisi
ngwhe ny ourecal
lt hattenpl usthez er
othpos it
ionma kese
levenele-
me nts.
Followingt hisheaderwewoul dind1 21t hree-byt
eb l
ocks,eachindi
cat
ing
thele ngtha nda ddres
sofas tr
inga r r
aye lement,ifpresent
.1 1×1 1= 1 21;
(121×3 )+9[ fort heheader]=3 72=$ 174.

]
1
56 As
sembl
yLi
nes

Pa
ssi
ngVa
ria
ble
stoAs
sembl
yLa
ngua
ge
Att hispointy ouma ywe llt
hi nkt ha tweha ves trayedv eryfarfr om t het opic
ofasse mbl y-l
ang uag eprog rammi nga ndha v ebe comeov erl
yi nvol vedwi tht he
str
uctur eofAppl esot. Uponal i
ttler ele 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 itherr eadingorc re ati
ngAppl eotv
s a ri
abl es,c l
earl
ywemus tha ndle
efectivelye achc ompone ntoft heda ta .Wemus tbea bletoide ntifyt hena me
andl ocationoft hev ariablewea reint erestedi n,a nda l
sot omodi fyt hati nfor -
ma t
ioni fne cessary.
Thet 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 e dedope 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 esota lrea dyc ont ains
therout inesne cessarytodoa l
mos tany thingwewi sh.Thema int rickwi l
lbet o
prope rl
yi denti
fya ndus ethea ppr opriateone s.
Int hepr eviousc hapt erIma deus eofaf ewoft heset oaccompl ishac erta i
n
degreeofle xi
bilit
yi npa ssingnume ricda tatoa na ssembly-l
a ng ua ger outine .
Let’
sc ompl etethes tudybyf or ma lizi
ngt hepos sibleope rat
ions.
Their stgene r
a lcategor yispa s
singda tatoar out i
ne .Wec a npa ssanyofs i
x
vari
abl etype s
.Tomi nimi z
et hec onfus ion,letuse s tabli
s hafairlysimpl eg oal: t
o
passt heda tasuc cessfull
ya ndpr ov es obys toringt heda tainanon- Appl esot
l
oc a
tion.
I
nte
g erVari
able
s
Forint
egerva
ria
ble
sthec
all
ingAppl
esotpr
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

Thema
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
s1−3
0 0wi
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)
The
ntr
yva
lue
ssuc
has2
58,
1,−1
,32
767
,and−3
276
7. y−3
Tr 276
8.
1
7.Mor
eAppl
esotDa
taPa
ssi
ng 1
57

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

Int hisr out i


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

]
1
58 As
sembl
yLi
nes

mulator( $F9−A2=FAC) .Thec ontentst he nc anbec hec kedf orvari


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

Thea
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
esotDa
taPa
ssi
ng 1
59

13 GETADR EQU $E752


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

Thisisba si
call
yar epeatofthepr evi
ouschapter’
sPoi nt
erS etuprout
ine,
wit
htheresultsbeingputintoDATA,DATA+1.Theadvanta
g eofthisrouti
necom-
par
edtotheI nteg
e rVaria
bleReaderisthatnotonlyisi
ts hort
er,butalsot
hati
t
wil
lac
c e
pteitherinteg
erorr e
alvari
ables(s
impleorarray)ands t
il
ldot hest
ri
ng
er
rorcheck.This,
then,isusual
lytheprefe
rredmethod.
Stri
ngVa ria
bles
Thegoalher
eistorea
dsomestr
ingdat
afrom t
hecal
l
ingAppleotpr
s og r
am
andt henputits
ome wherei
nme morywher
ei tpr
esuma
blywil
lbeava
il
a bleto
otherporti
onsofthea s
sembl
y-l
anguag
eprogram.Toil
lus
tra
tethi
s,e
nte rthe
fol
lowingtwoprogra
ms :
10 A$ = "TEST"
20 CALL 768, A$
30 END

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

]
1
60 As
sembl
yLi
nes

0309: A0 00 24 LDY #$00


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

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

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

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


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

3[
CT]FACMO a
ndFACLOa
ret
heMi
ddl
e-Or
dera
ndLow-
Orde
rby
tesoft
hef
our
-by
te
ma
nti
ssaf heloa
ort ting
-poi
ntAc
cumul
at
or.
1
7.Mor
eAppl
esotDa
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
ringsint he
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 eotpr
s 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
Thec onverseofthetec
hniqueswe’v
edisc us
sedsof a
ractual
lyisfa
irl
ysim-
pl
e.Theke ytomuc hofiti
sthePTRGETrouti
neus edearli
er.
Be c
ausethi
srouti
ne
wi
llevenc re
a t
eav a
riabl
ewhe nit
’snotal
readypr e
sent,wecans i
mplymor eor
l
essreve
rset heprocessoftheprevi
ousroutinestopassda t
aba c
kt oac a
ll
ing
Apple
sotpr ogram.
Again,I’l
lil
lus
trat
eane xa
mpleforeachva ri
abl
etype.
I
nte
g erVa
ria
bles
TheAppl
esotpr
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

Thea
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

Thi
sprogramisarat
hertr
ivi
alexer
cis
einthatal
lthatnee
dstobedonei
sto
re
versetheoperandsofl
ines24,25and2 7,28from theirs
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
ne.Thes
s ame
Appl
esotcal
li
ngprogr
amisus
edwithonl
yami ic
normodi a
tion.
10 POKE 896,2: POKE 897,1
20 CALL 768,A
30 PRINT A
40 END
1
7.Mor
eAppl
esotDa
taPa
ssi
ng 1
63

Thes
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

Thete chniquehe rei stous ether out ineGIVAYF( $E2F2=GI VeAc cumul a
-
tora ndY- Registert oFAC)t oputt het woby tesofouri nte gernumbe rintothe
FAC.GIVAYFr equi restha ttheAc cumul a t
ora ndY- Reg i
sterbel oadedwi ththe
high-a ndl ow-or de rby t
e s
,r espectively,fort heint e
g ernumbe rtobetransferr
ed5.
Asabonus ,thenumbe rma ye venbes igne d–t ha tis,positi
v eornegativ
e.Signed
binar ynumbe r
swe r ecov er
e di ng reatde tailinc hapter1 0.
Lines1 9a nd2 0l oa dt hea ppr opria ter egiste
rs,the n,a tercall
ingGIVAYF,
PTRGET a ndCHKNUMa reus edt ode t
e rmi net hena meoft hev ar
iabl
et ous ein
returningt heda ta .Re calltha tate rr et
ur ningf rom PTRGET,t heY-Re gi
sterand
Ac cumul atorwi llhol dt hel ow-a ndhi gh- orde rby tesoft hea ddres
soft heda t
a
forthene wv ari
abl edi geste
dbyPTRGET.
MOVMF ($EB2B =MOVet oMe mor yf rom FAC)i sther outi
newe ’l
lus eto
compl et
et hepr oc ess.Itr equirest hatt heY- Re gist
era ndX- Regist
erbel oaded
witht hea ddressoft heme mor ylocationt owhi cht hec ontentsoftheFACwi l
lbe
mov e d.SincePTRGETha sjus tdete r
mi nedt hatforus , t
heonl yhitchisthatPTR-
GETl ett hehi gh- orde rby t
ei nt heAc cumul atori nsteadofi ntheX- Regis
teras

CT]Theor
5[ igi
nala
rti
cl
eswi
tc
hedt
heme
ani
ngoft
heGIVAYFhi
gha
ndl
owby
te.The
s
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
-
lowedbytheendingRTSoft hemainroutine
,theli
nec anbes hortenedoneby t
e
byc ha
ngingthel
a stJSRtoaJMP.Whe nt heRTSinthel a
stcall
eds ubrouti
neis
encounte
red,
theRTSwi llc
a useanexi
tfrom thema i
nr outineinst
ead. Ane xa
m-
pleofthi
swouldbet orewrit
et hee
ndoft heprogr
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 hdiferentbutwi l
lrequireas l
ightl
yc l
ums y
call
ingAppl esotpr ogramt odemons tr
ate.Line10isas e
riesofPOKEstha twil
l
putt heAS CI Ida taf orthestr
ing“TEST”i nt
ome mor yatourus ualDATA($380)
location.Addi ti
ona lly,adeli
miterwillbeplacedattheendoft hes t
ri
ngs othat
ther outineswewi llbec al
li
ngc a
nde terminethest
r i
ng’
slength.Useofade li
m-
ite
rismor epr actic
a l
, es
pecia
ll
yins it
uati
onswhe r
ey oudon’tknowt helengthof
ani nc omings tringunt ilthecarri
ager et
urnorot herdel
imitershowsup.The
Appl esotr outinewe ’l
lusewillautomatical
lydet
erminetheleng t
hbys canning
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

Thes
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
esotDa
taPa
ssi
ng 1
65

12 PTRGET EQU $DFE3


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

Thene 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 cannedandt ha ttheX-
Re g
isterhol dt hev aluefort hede li
miti
ngc haract
er.Thisexampleus es$00,but
anothe rcommonv ariat
ionwoul dbet ouseac arri
agereturn($8D)o rac omma
($2C).( Notet ha t<RETURN>i sa lmostalwaysfoundi ntheinputbufe rwi tht he
highbi tset,t
ha tis,$8Dv ersus$0D) .
Ate rscanni ngf ort hede limiter
,MAKSTRmo vestheda t
aupt ot hes t
ring
stora
g ea re
aa tt hetopofme mor y.
SAVDi sac ompa nionr outinewhi chwi l
ltakewha te
verstri
ngde sc r
iptoris
currentlypoi nte dtobyFORPNT( $85,$86=F ORmul aPoiNTer)andma tc
hi tto
theda tajustmov edbyMAKSTR.
Look inga tthel i
sti
ng ,wec ans eethattheonlyc r
eati
vewor ktobedonei s
mov ingt hec ont entsoft heAc cumulatorandY- Regist
ertoFORPNT.TheAc cu-
mul a
t or,Y-Re gist
er,andX- Reg is
ter
sa rethenpr e
pa r
eda swa sjustde scri
bed,
andther ema iningc 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 helastthreeApplesotl i
st
ingswa sdonewi thout
deiningther eturnedvari
ablepriort otheCALL.Thiswa 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
te
a rtheCALLstat
eme nttoconirmt ha talll
egalAppleotope
s 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
ernalApplesotrou-
t
ine
swi t
hincust
om assembly-l
angua gepr ogr
ams .
1
8
1
8. Ap
ple
sotHi
-Re
sGr
aphi
cs
Ma
rch1
982

Inthepr e
v iousc ha pte rwee xamine dt het e
chni que sforpa ssi
ngda taba ck
andf orthbe twe enAppl esota nda ss
e mblyl angua gei nt hef or m ofs tanda rd
Appl e
sotv a ri
a bles.Thi swa sg reatlyfacili
tatedbyt heus eofe xis
tingi nternal
Appl e
sot rout ine s.A na turale xtensionoft hisi de aist ous eot heri nternal
Appl e
sotr out ine sa sma ybea ppr opriatet oourg ivent ask.Oneoft hemos t
int
eresti
nga ppl icationsoft hisisi nthea reaofhi -r
e sg raphics
.
Therea ret woma inr easonsf ordoi nghi -resg raphi csfrom a ssembl yl an-
guage.Their 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 esoti 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 esotr 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 peciicf unc t
ion.The
norma lAppl esotr out i
ne sa rede signedt obele 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.Thetrade ofmus
- tbewe ighed.
Thes 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
izedrout ines
thatgivehighers pe e da ndg reat
e rindepe nde ncef rom t heAppl e sotr 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
elythemode lofApple
hi-
resg raphicsus edf orthecur r
e ntdi scusson.Thi
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.
Thea rrayofpos si
blepointstobepl ott
edconsistsofaie l
dof1 92lines
,each
ofwhi chi sma deupof2 80poi nts.I fami xedmodeofg r a
phi c
splustextis

]
1
68 As
sembl
yLi
nes

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.
Thesecolor
shavebeenas
si
gnedtoeightnumeri
cval
ues
, a
sfol
lows
:

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

Whi teisc reat


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

La
ndma
rksa
ndEnt
ryPoi
nts
Anumberoft
hef
unda me
nta
lhi
-re
srout
ineent
rypoi
ntsa
redoc
umente
din
v
ari
ouspubl
ic
ati
onsr
ela
tingt
otheAppl
e.Abrie
fsumma r
yisgi
venint
hefol
-
l
owi
ngtabl
e.
1
8.Appl
esotHi
-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( −7
0 ).
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 urr
e ntHCOLORa tc oordi-
natesgiven.
HFIND $F5CB Returnsc ur rent“ curs
or ”pos i
t i
on.Us ef
ula teraDRAW
toindwhe rey ou’vebee nlet. 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 ertica
l positi
on.
SHNUM $F730 Putsa ddressofs hapenumbe rindi cate
dbyX- Re gi
ster
into$1A,$1B;r et
urnswi thX,Y( l
ow,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 ciice nt ry
,no tthe
beginningoft het able.Ca lSHNUMir
l 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 alSHNUMir
l stwi t
hX-
Register=s ha penumbe r.

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

]
1
70 As
sembl
yLi
nes

14 HPLOT EQU $F457


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

6058: 20 A8 FC 70 JSR WAIT


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

Whe nr un,t hisr outinewi lldr aw abor dera roundt hehi -re ss creen,a nd
the ndr a wi nt hec enteroft hes creent hes ha pede ine dbyt het able.S ca l
ea nd
rota t
ionv aluesma ybec hang edbya djus t
ingpa ddl es0a nd1 ,respe ctivel
y .Pus h-
ingbut ton0wi l
l r
e -
c l
earthehi -ressc reenoft hea ccumul at
edi ma ges.
Ther out i
nes tartswi thaj umpov erada tatabl etoE2.Thet 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.Thet 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 −50 .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 onir mt hec oordi nate s
.
Onc et hebor de risdone ,pr epa rati
oni sma det ous et
hes ha pet a bl
e .Loc a-
tions$E8,$E9a reus e
dbyAppl eott
s opoi ntt ot hebe gi
nningofas hapet a ble.
SETi nitializesthispoi ntert ooure xampl eta blea t$6003. Thet abl ene ednotbe
pa rtoft hea ct
ua lcode ,howe ve r,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,$2000−$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
er )intotheSCALEpa rame te
r
loca t
ion( $E7). Line6 6t estsf oraSCALEv al
ueof0 .Be causeAppl e sott r
ea ts0a s

]
1
72 As
sembl
yLi
nes

thel arg estsca l


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

Be forer epea tingt hec y cl


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

AMi
norDi
ver
sion
Hig h-
r e
sol
utiongraphi csaregeneral
lyus e
df ort
woma i
npur poses
.Their s
t
isthepr es
entat
ionofg raphicaldata,s
uc hass a
leschart
sande quati
ons.Therou-
tinespresente
dhe rearea dequateforthat,butov er
allt
hetaskisprobablybet
ter
donedi rectl
yinAppl esota ny way.Appl e
soti sote 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:TheDRAWr
out
ine(
$F601)i
snor
-
ma l
lycal
ledwitht
heX-a ndY-Regis
ter
ss e
ttotheaddr e
ssoftheindi
viduals
hapet
obe
drawn.Thiscanbeautomate
d,however,byirstc
a l
li
ngSHNUM( $F730)
.Whe nSHNUMis
call
ed,however
,alate
re nt
rypointtoDRAWisuse
d.S peciic
all
y,thi
sshouldbeDRAW+4
($F605).Ent
eri
ngat$F601bymistakecanproduceratherunpre
dict
abl
er e
sult
s.
1
8.Appl
esotHi
-Re
sGr
aphi
cs 1
73

Theot herma inare aofconc e


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

Loc
ati
on
Its houldbef ai
rlyobv ioust ha twhe nspeci
fyi
ngthecoor
dinat
e sofapoi
nt
ont hes c
r e
e n,wea r
eg ivingi nfor mationabouttherel
ati
veloca
tionofsome-
thi
ng .Aboutt heonl yt hingdi fe renta bouttheApplesc
reeni
sthatthenumber
syst
em us edi slaidouts ome wha tdifer-
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.
Thi 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
etc orne r.
Thel 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
stoodtheirst
exampleofdr a
wi ngt hebor deront hescre
e n,allthi
si salreadyknownt oy ou.
Thereasonweme 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.Thatistos aythattheonlythingswe
havetoworrya boutwhe ns i
mul at
ingamov ingobjectareitsspeedandi t
sdirec-
ti
onoftravel.
Spe edisme asuredinuni t
sofdi s
tancepe runitoftime.

]
1
74 As
sembl
yLi
nes

Int hec aseofours creendi spla ya ss hownt ot he


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

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


240 GOTO 100

Thispr og ra
m wi l
lbounc eat inys potoft hel eta ndr ights idesoft he
screen.Thei mpor ta ntthing stonot ea rethat(1 )mot i
oni ss imulatedbya ddi nga
constantv el
oc i
tyf ac t
orVt ot hepos it
ionofe a chcycle;(2)t heobj ectise r ased
from itsoldpos itionbe forebe ingr edrawna tt henewone ;and( 3 )abounc eis
ba s
ical
lyac ompl eter eve rsalofthev eloc i
tyfactor,thatis,thev aluei smul tiplied
bymi nusone .Thes pe edwi thwhi che verythingi sex ecutedde pendsont he
inherentspe e
doft hepr og r
a mmi ngla ng uag
ea ndhowf astwec a
nc yclethr oug h
these r
viceloop. Iff orsomer ea s
ont hel oops hownwa st oof ast,
y ouc ouldputa
FOR-NEXTd el
a yloopi na nywhe rea l
ongt hel i
ne .Ifitwa st oos low,y ouc oul d
increasethes peedf a c
tor ,V,from 1t oal ar
gernumbe r
. Larg ernumbe rspr oduc e
mor ejerkymot i
on,howe ver.Theot he roptionwoul dbet owr it
ei ti nasse mbl y
language!
Be f
oredoi ngt hat,thoug h,l e
t’
sma keittwo- dime nsiona lbyg ivingt heba l
l
bothhor i
z ontalandv erticalcompone nt stoit
smot 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.Thelic
k e
riscause
dbye rasi
ngthedotsosoonaterwedr 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

Thi
sals
owi l
lsl
owdownt hespee
dofthebal
l abit
,butitdoeshelptheover-
all
scree
nappe a
rance
.Youareadvis
edtowatc
ht hisfa
scinat
ingprogramrunfor
awhi l
e,medi
tati
ngonthenatureofthepr
ogra
mmi ngstepsoccur
ringthroug
h-
outthetra
vel,andpart
ic
ula
rlyateachbounc.Thi
e sconceptises
senti
altoany
furt
herani
ma ti
onefort
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
aphicsandhow t
oplota
bounci
nghi -
resbal
l.Wec ons
truc
tedas i
mpl
eAppl
esoftpr
ogra
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:draw→e r
ase→c a
lculae→c
t heck→( do
i
ta gai
n...
).
Fort heAppl esoftprogramshown,t hi
sworksfai
rl
ywe llandisveryunde r
-
standable.he rei sonepr oblem,howe ver
:v e
ryli
ttl
etimepa sse
sbe tweent he
drawa nde r
ases t
ag es
,compa redtot heamountoftimes pentinthec al
c ul
ate
andt e
s tsect
ions .her esultonthesc r
eenisalar
geamountofli cker,result
ing
from t
hedots pendi ngmor eofitsti
mebl ac
kthanwhite.
Ones ol
utiont ot hisistoma keas mal ic
lmodi ati
ontot heorigi
na lAppl e
-
softprogram, sot hatitappearsasf
ollows:
0 REM FP DOT DEMO PROGRAM
10 HGR
15 HCOLOR = 3 : HPLOT 0,0 TO 279,0 TO 279,159 TO 0,159 TO 0,0
20 X = 140 : Y = 80
30 XV = 1 : YV = 1
100 REM CALC NEW POSN
110 TX = X + XV : TY = Y + YV

200 REM CHECK POSN


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

]
1
78 As
sembl
yLi
nes

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


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

300 REM ERASE OLD POSN


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

400 REM DRAW NEW POSN


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

hisr outinenotonl ydr awsani c ebor dera roundt hes c


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

Ta
kingt
heOppos
iteofaSi
gne
dNumbe
r
Inc hapte r10wedi s
cussedthes i
gnbitandhowt or eprese
ntne ga t
ivenum-
bers1R
. ecalltha tnega
tiv
enumbe rsaredeine dus i
ngt het wo ’
sc omp leme ntsys
-
tem:reve r
see a c
hbitofthepositi
venumbe r,thena ddone .
Allthatwene ednowi saroutinethatwillproducet heoppos i
teofanumbe r
givenit–t ha tisproducethet wo’scompleme ntofapos it
ivenumbe ra nda l
so
thepos i
tivee quival
entwheng ivenane g
a t
ivevalue.Todot hi
s, we’l
lus et heEOR
comma nd.
EOR isus efulincreati
ngar outi
net oc onv er
tbe tweens i
gne dnumbe r
s
becauseofi tsa bil
it
ytor e
v e
rseallofthebitsinag i
venby te.hec onv ersi
onis
donewi tht woi ndivi
dualrouti
nes.Intheexampl esbelow, t
her outine sconv e
rta
constantv alue ,
#$34,backandf orth.I
nawor kingv ersionoft hispr og ram,the
valuewoul dbepa ss
edv i
aar e
gist
erorame mor ylocat
ion, aswi llbes hownl ater
.

CT]heor
1[ igi
nal
Softa
lka
rti
cl
e#1
9(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
li
n)suppor
tthi
snot
ati
on.

TheRe
alTh 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
esoftl
is
ti
ng.
Assembl
eandrunthi
sli
st
ing:
1 ********************************
2 * AL19-HIRES ONE DOT PROGRAM *
3 ********************************
4 *
5 *
6 * OBJ $6000
7 ORG $6000
8 *
9 X EQU $E0 ; $E0,$E1
10 Y EQU $E2
11 XV EQU $06 ; $06,$07
12 YV EQU $08
13 TX EQU $09 ; $09,$0A
14 TY EQU $0B
15 *
16 PREAD EQU $FB1E
17 WAIT EQU $FCA8
18 HCOLOR EQU $F6F0
19 HGR EQU $F3E2
20 HPLOT EQU $F457
21 HPOSN EQU $F411
22 HLIN EQU $F53A
23 *
6000: 20 E2 F3 24 ENTRY JSR HGR

]
1
80 As
sembl
yLi
nes

6003: A2 03 25 LDX #$03 ; WHITE


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

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

]
1
82 As
sembl
yLi
nes

60C4: A5 06 137 NEGPOSX LDA XV


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

Whe ny our unt hisr outine, noticehowmuc hf asteritex e


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

shortdelaybyget
tingavaluefr
om paddle0tobeusedbytheWAIT( $FCA8)rou-
ti
ne.Afterthedel
ay,aJMP CALCres
tar
tstheenti
repr
ocess
.
Lines126−165areappli
cati
onsofthesign-
reve
rsa
lrouti
nesshowne arl
ier
.
NoticethatRVRSYisaone -
byterev
ers
al,whil
eRVRSXil
lus
trat
esthereversalofa
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 rase
/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-
ing)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

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

]
1
84 As
sembl
yLi
nes

263 *
618A: 06 264 CHK

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

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

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

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

6047: A9 00 68 LOOKUP LDA #$00


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

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

]
1
86 As
sembl
yLi
nes

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

Runt hisroutinef
rom t
heMoni t
orwitha6000Gorf rom Appleoftwi
s tha
CALL 24576.I
fca l
li
ngfr
om theMonit
or,mak esur
ey ouhaveent
eredtheMoni-
torfr
om Appl esoftwhenyoudot heCALL -151 t
oe ns
urethattheApple
soft
ROM orRAM c ardba nki ss elected.Not et hata lthoug ht hee ntirerout i
nei sin
assembl yl ang uage,i tdoe sr equiret hepr e senceoft heAppl e softhi-resr outines
int he$D0000-$F7FFr ange .Byus ingpa ddl e0y ouc anv aryt hes peedofe xecu-
ti
onc ons i
de rably.Onedr a wba ckofus i
ngt heWAITr out i
nei stha t0wi llbej ust
ass low a s2 55whe na djus ti
ngt hepa ddl e.Ot he rwi s
e,i ts houl dbe ha vequi t
e
nicely. Tos pe edthing supf ur t
he r,NOPo utt heJSRt oWAITonl ine1 87. Ane ven
greaters pe edi ncreasei sa chieve dbys i
mi larl
ydi s
abl i
ngt heJSR PREADonl i
ne
185 ,althoug hwi t
hPREADg 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 peedpos si
blefort heiv e
dotsus i
ngs t
anda rdAppl esofthi -resrout ines.
hema inpoi ntst onot eint hene wl isti
nga ret heJSRt oSETUPonl ine3 2,
theLOOKUPs ecti
oni nl i
ne s6 8−1 28 ,andt hetabl eg ene ratora tt hee ndi nl ines
241 −2 63.
SETUPc reatesada tata blestartinga tl ocat
ion$1000t hatc ont ai
nsanumbe r
ofe ight -
by tebl ocks ,e a
c hofwhi c hc ont ai
nst hei nforma ti
onne cessaryf ora
givendot .hebl ocki sma deupoft wof our -
by tes ubuni ts.heir stfourby te
s
givet heloc ationda taf ort heXa ndYc oor dinate s.Not icetha tthef our thby teis
notus ed.S pacei nthet ablec ouldha vebe ens ave dbyomi tti
ngt hisby t
e ,butt he
eightb ytel eng th pe re ntr ya ll
owsust ous eaf e ws impl eASLs ,a swi l
lbe
expla i
ne dmome nta ril
y.
hes ec ondf ourby teshol dt hev eloc itydata ,ag ai
ni na nX,Yf orma t,with
bytef ourbe i
ngunus ed.
LOOKUPb asi
callydoe st hreet hing s.Fi rstitr etri
e vestheda t
af oradota nd
putsi ti nt hec ur rentX,Y,XV,YVby tes .S econd,i tf eedst he set otheCALCa nd
PLOTr outine s.hi 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.hi si smul ti
pliedby8t og etthe
basea ddr essoft heda tafort hatdot .Re me mbe rt hatASLc a nbeus edt omul ti
ply
1
9.Ca
ll
ingHi
-Re
sGr
aphi
csRout
ine
s 1
87

easi
lybyapowe roftwode pendingonthenumbe rofASLsyouuse
.Eac hASLi
s
equival
entt
omul ti
ply
ingby2 .
Onc et
hebaseaddresofs
s etisde
ter
mi ned,t
hisi
sputintheX-
Re g
iste
rand
theda taret
ri
evedv iaas erie
sofLDA/ STA oper
ati
ons.Afte
rret
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 esentt her i
ght
mi xofc larit
ya ndi n- dept he xpl a
na ti
on.Myg oa listopr ovidee noug hoft he
ba s
icstogivey out hes pring boardt opur suey ourowni nteres
ts.
Ing ener al
,thepr i
nc i
ple spr ovidedi nthisc hapterandt heonebe foreitare
thefounda tionofmos tani ma tedg raphicsprog rams .Tabl
esa reespec 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 anguage, t
heAppl e-
softrouti
ne st hems el
v esa res t
illthemos trestrainingportioni ntermsofs peed
ande xecution. I
na llfairne s
st oAppl esoft,
thoug h, re
ali
zetha tthe
irs peedissac-
riicedforsimpl icit
ya ndc onv enienc eofope r
a t
ion.
Ne xtcha pter’
st opicwi llbet hel ayoutoft hehi -r
essc r
e e
ni t
s e
lf,andhow
certai
nde dicatedr outinesc a nbec re at
edt ogetal it
tlemor eoutoft heol ’Apple.

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

Int hepr ece


dingdi scuss
ionsofhi-re
sgraphi c
swe ’
vereli
edont hee xi
sting
Appl e
s oftBAS ICr out
inestodot henecessa
rypl ott
ingofpoi ntsfr
om a s
sembl y
l
a nguag e.From y ourpr evi
ouse xper
iencewithAppl esoftande ve
nf rom t he
mos tre c
e nthi-r
esmov i
ng -
dotprogramspre s
ented,y ouma yha venotice
dc e r
-
tainpe culiari
ti
esa bouthi -
resg r
aphi
cs.hepr obl
e msl i
ei nc e
rta
ini ntr
ins i
c
shortcomi ngsinthee xpla
na t
ionofhi-
resgraphicsofe re
ds ofar
.
Toe xplorethisareafurtherle
t’
se xa
mine ,onebyone ,anumbe rofpr ob-
l
e mst hatc anoc cur–a ndthusdi s
covertheunde rlyi
ngs truct
ureoft hehi-res
displ
ays creen.

Loa
dingaHi
-Re
sSc
ree
n:t
he“
Fil
l”Efe
ct
hef unda me nt a
lque stiont obea nswere 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 ha ttodi splayas cr eenwhos ea ppearancec anbea rbi t
r ari
lychange d,the
RAMp orti
onoft hec omput ermus tbeus ed.hea r
eaus edist her angeofme mor y
from $2000t o$3FFF( 8 19 2t o1 6383de ci
ma l
).hi sisca l
ledt hepa geonehi -res
display.heAppl eIIi sa lsoc apableofdi splayinga na lter
na t
eme mor yr a
ng e
call
e d,cleverlye noug h,t hepa get wohi -
resdisplay.hi sdi splayi sderi
vedf rom
theda tacont ainedi nt heme mor yr ange$4000t o$5FFF.
hi sc hapterwi l
lf oc uspr i
ma rilyonpa geone ,a lt
houg hf ort hemos tpa r
t
pag etwoc anbec onside redt obej ustas i
mpl eofs etfrom pa geone .
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.hi scane a s
-
il
ybei nvestigatedbydoi ngt hef oll
owi ng:
From Appl esoftBAS IC,s electandc l
earthepa geonehi -
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 thaCALL -151.heir s
tt hingt odoi st oil 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
dlyil
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
chthescrenil
e la
talit
tl
es l
ower
pace.Youma yha v
enot icedwhe ny ouill
edthes cr
eenjustnowthatitdidnot
illinanexa c
tl
yc ontinuouspa t
tern,li
ne-
by-l
inefrom t
optobot t
om. I
tdidhap-
penr a
therquickly,
thoug h.
Clea
rt hescreenbyt ypingHGR<RETURN>a g
ain,andnowl oadthedatafr
om
diskbackintome mor ybye nter
ing:
BLOAD TESTPIC

hi st i
met hes creens houl dill mores lowly,andt hesome 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 moryaddr es
s? ”(
Or :
"Why
doe sthes creenl oa dins uchaf unnywa y? ”)
Yourir stimpul semi ghtbet os ay“ We ll
,ifIwe r
ede s
igningt hecomput er
,
I’
dj us tmul ti
pl ythenumbe roft helineIwa ntedbyt henumbe rofby t
esperline
tog ett heba sea ddr ess(thea ddr essoft heir stbyteoft heli
ne )fortheline.For
exampl e,ifeac hl i
net ookf or tyby tes(whi ch,byt hewa y,i
tdoe s),li
ne0woul d
hav eaba sea ddr essof$2000.Li ne1woul dbe$2000+1×$28( $28=40d eci
-
ma l)=$2028. Line2woul dbe$2000+2×$28=$2060, ands oon.
Ana dditiona lbene itwoul ds howupi nthef orm ofsomeunus edby t
eson
thehi - r
e spag e.For1 92lines ,thela s
ta ddr e
ssus edwoul dbe$2000 +(192×
$28) −1 =$3DFF.S incewe ’
v ea l
lotte
dt hea r
eaf rom $2000 to$3FFFf orpa ge
one ,thiswoul dl e
a ve$200( 512d e
c i
ma l
)by t
esleftover!
Unf ortuna tel
y ,tha t
’snott hewa yt heAppl ewa ssetup.I tt ur
nsoutt hat
mul tiplicat
ionr outinesa rek indofadr a gint er
msofs peeda ndme moryus age
,
unles sy ou’reus inge x actmul tiplesoftwo.Amuc hmor ecompa ct(andfaster
)
algorithm i s:
1 ********************************
2 * AL20-HIRES BASE ADDRESS *
3 * CALCULATOR ROUTINE *
4 ********************************
5 * OBJ $300
6 ORG $300
7 GBAS EQU $26
8 HPAG EQU $E6 ; HGR=$20, HGR2=$40
9 *
10 * CALC BASE ADDRESS FOR Y-COORD IN ACCUM.
11 * GBAS = ADDR OF 1ST BYTE OF LINE SPECIFIED.
12 * ASSUME ACCUM HAS BITS abcdefgh, C=carry
0300: 48 13 ENTRY PHA ; abcdefgh
2
0.S
truc
tur
eoft
heHi
-Re
sDi
spl
ayS
cre
en 1
91

0301: 29 C0 14 AND #$C0 ; ab000000


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

Althoug hi t
’sperhapsnotobv i
oushowt hiswor ks, t
heroutinedoe stakea ny
valuei nt heAc cumul ator,fr om 0t o1 9 1 ,a ndr etur nthea ppr opri
ateb ase
addr essoft hec orrespondi ngl i
nei nl oc ations$26,$27 ( GBAS).hi scodei s
“stolen”f rom as imil
a rr outinei nt heAppl esofthi-resroutineHPOSN( $F411)
me nt i
one di nthepr evi
ousc hapter.1

heov erallpatter
nt ot hes creen- illingope rat
ioni sa sfoll
ows .heir s
t
fortyby t
esofme mor yc orrespondt ol i
ne0oft hes creendisplay.hene xtfor t
y
byte sforml i
ne6 3,andt hene x tfortyby t e
sl ine1 2 7.Att heendoft heline1 27is
abl ockofe ightunus edby tes
.( 3×4 0+8=1 28by tes).hi spatte
rni srepeated
sixty-threemor et i
me stoc rea teall1 92s cr eenl ines2(
. 3×6 4=1 92lines;64×
128by tes=8 ,192by te
spe rhi-respa ge.)
Whe nhi -respa ge1i sl oadedf rom di s k,ther a ngeofme mor yisil led
seque ntiall
yf rom $2000 t o$3FFF.Wha ty ous eeont hescreena r
et wenty-four
scree nbl ocks,e achcons isti
ngofe ightl ine sg raduallybe ngil
i led.het we n-
ty-fourbl oc ksalsocanbev ie
we dase ig htt riplet
s ,withe ac
ht ri
pletma deupof
threel ines,onel ineeacha tthet op,mi ddle ,a ndbot tom por t
ionsoft hes c
reen.
heg ene ralscreenillpa tternt heni s:0 ,6 3,1 2 7;8,7 1,135;16,79 ,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.
Addi t
ionalcomme ntswe r
ea dded,foll
owingthosefrom BobS ande r
-Cederlof
’sAppl e
-
softdi
s as
semblyathttp:
//
www. txbobsc.com/
scsc/
scdoc ument
or/.
2[CT]hene xt128bytes($2080−$20FF)c orr
espondt os c
ree
nl i
nes8 ,71,and1 35.he
128by tesafte
rthatma ptolines16,7 9,143
,ands of orh.heir
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
reylue
l ntintermsofwhi ch
l
inec or
res
pondstowhichme moryrang
e;onl
yt ha
ty our
ea l
iz
ethatthes c
reen
doesnotilli
nqui t
ethepat
ter
nthatmightot
herwisebeexpect
ed.Fortunat
el
y,
therouti
nejustgi
vencancalc
ula
tethebaseaddre
ssofanyhor i
zontalli
newe
wishtoacce
ss.

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

heir s
ttwos t
epsaref ai
rl
yi nnoc ent;the yme relyselectandc lea
rthehi -res
page,thensett hec olortog reen.Tr yingt oHPLOT0,0g ive stheir s
tproblem:i t
doesn’tseemt owor k.hi si sc onsistentwi t
ht hewa rningg ivene a
rli
er,t hat
even-numbe redc ol
or splotonl ye venc oor dinates
,a ndodd- numbe redc olors
plotonlyoddc oordinates.Gr een,be inga nodd- valuec olor,isnotpl ot
te da t
X=0 .
h eCALL62454i sac a l
ltoar outinet ha tclear
sthes c reentothelastc olor
plott
ed(whe therornott her esultwa sv is
ible .Afte
) ry ous etthec ol
ortoor ang e
(HCOLOR=5) , a
na tte
mptt odr awadi ag ona llineproduc esas eri
esofr e
ctang les
.
Wha taccountsf orbot hofthe s
ee fe c
ts?
You’l
lrec a
llt hat4 0by t
espe rli
nea reus e dtohol dtheda t
at odispl
ayt he
280dotpos it
ionsone achline.he 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(7×4 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
hintheir s
tthr e
eby t
esofme mor yf orpa g
eoneoft hehi -ressc
reendispl
a y
.
Alt
houghonl ytheir 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 .Specic
i al
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,bit7i suse
dtode ter
minewhi chrow–toporbottom–is
sel
e c
ted.hismea nsthatforanypart
iculargroupofsevendotpos
it
ions,r
epre
-
sentedbyas i
ngl
eby te,onlythecol
orsine i
therthetoporbott
om rowscanbe
showna toneti
me .Fore x
a mple,
iti
sno tposs
ibletohavegr
eenandorangedot
s
displ
ayedsimul
taneouslywithint
hesames even-dotg
roup.
3. heor deroft hecolor swi t
hi ne ve ryothe rby teisr eversedwi thres pe ctt
oi t
s
ne i
ghbor s.hi sistoe ns uret ha tthei ndividua lc olorspr ope r
lya lt
er na t
ewi th
successiveXpos it
ions,suc ha sbe twe enby tes0a nd1 ,1a nd2 ,ands oon.
hec 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 eselect edwhe n
0=Bl ack1 4=Bl ack 2 thehi gh-or derbi tisof( bit7=0 ).S e
t2a re
1=Gr e e
n 5=Or ange allthoses electe dwhe nthehi g h-or derbitis
2=Vi olet 6=Bl ue on( bi
t7=1 ).Anya tt
emptt opl otapoi nt
3=Whi te1 7=Whi te2 from ones etwi l
lc onverta nye xis ti
ngdot s
from t heot he rs et,pr ovideda lldot sare
de inedwi thinac ommonby te.Obv iously
,
plotti
ngadota tXc oordina te7( by t
e$2001)wi llnotha v eanye fe ctondotpos i
-
tions0t o6 ,sincetheya res toredinas eparateby te( $2000) .
Whi t
ei sdr awnbyt ur ningont woa djacentdot s,e i
therav iolet-gr eenpa i
r
forwhi t
e1,orabl ue-
or ang epa irforwhi t
e2.Conv ersel
y ,blackisf orma ll
ydone
byt urningoft 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 eftinas tat
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 ton.hi
i si swhyt hee a rl
ierdi agonallinepl otacted
sos tr
a ngely.Byc lear
ingt hes creent og reen,e verys c
re enby tewa ss ets oa sto
ha vetheg reenbitsona ndt hev ioletbi tsof( bit7=0 ).SeeFi gure2 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
shiftedfor$2001,ana l
l-
greendotgroupwoul dcorres
pondt othevalue$55. To
addt otheconfusi
on,reme mberthatFigure20-1showst hebitsinther ev
erse
orde rfrom t
henotationnorma l
lyusedinthisbook.Ordinaril
ywe ’
ds howloca-
ti
on$2000hol dinga$2Ai nbinarynotati
ona s
:00101010.Sincethescreendots
aredi spla
yedbyl east-
sig
niicantposit
ionir s
t,though,thi
si sreversedwhe n
showi ngas c
reendispla
ytoma keiteas
iertoint
erpret
:
$2A = 00101010 →(
rev
ers
etoma
tchFi
gur
e20
-1)→ 01010100

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

Whe nHPLOTt ri
edtodrawanor angedotat0,0wewoul dordina
ril
y
expectnoe fect.However,
thehighbitwa sre
v e
rsed,andt hi
sconvert
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 quival
ents
.
Fig
ure2 0-3showstheconte
ntsof$2000t o$2002a fte
rtheorangeHPLOT.

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 fte
rt he
att
e mpttoplottheorangedot.Intheirstcase
,theirstgre
e ndotisconv er
ted
eventhoug hthedotsarevi
sual
lysepar
a t
ed.hi si
sbecausetheyarebothde t
e r
-
mine dwi t
hinthesameby t
e.Inthesecondcase,eve
nthoug htheseconddoti s
notplotted,t
hec onve
rsi
onstil
loccur
s.Inthethirdcas
e,thesecondplotus esa
seconda nddisti
nctbyt
e,sotheirstdotisunafecte
dre g
ardles
softhec olorof
thesecondplot.

Ot
herPr
obl
ems
:Whe
nIsWhi
teNotWhi
te?
Ans we r
: whe ny ou’replot t
ingonlyonedota tatime .
Int hel 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 twee nv iole
t
andg reende pendi ngoni t
spos i
ti
on.S imilarly
,e venthoug hwes pecie
i dwhi t
e
asthec olortobeus edi ntheboxf ramedr a wna tthebeg i
nningofe a
chpr ogram,
theleftverti
ca lli
newa sv i
oletwhi l
ether i
g htonewa sgreen.
hi si sbe causewhi tedoe snota ctuallytur nont wodot sa tonc e.Wha ti
t
real
lydoe sisl eteithe rdot(v iol
e t
/gree
noror a
ng e/bl
ue)bea ccepta bl
eforag iven
HPLOT. Whi tea ppe arsonl ywhe ntwoa djace ntdotsa r
edra wn, usua l
lyasar esul
t
ofalinebe ingdr a wnwi thsomede gr
eeofhor iz
ont alt
il
ttoi t
.
Int hemov ing -dotpr og rams, t
hedota ppea r
swhi tewhe nmov i
nga thigher
speedsbe caus ethea lternat
ionbe tweenc olorsoc cursquicklye noug ht ha tyour
eyetendst odot hebl endingonat imeba sis,rat
he rthantheus ua lposit
iona l
one .

Supe
rHi
-Re
sGr
aphi
cs
hel asttopi
cf orthischa
pterisnotaproble
m,butr at
heranunhe ra
lded
beneitofthiscraz
ys ys
tem ofscr
eendispl
ays
.Youma yhav enotic
edinthepre-
vi
ouse xampl ethatwhe nthes econddotwa splot
ted,theg ree
ndotmov ed
sl
ightl
ytother i
ghtwhe nitchangedtoora
nge.Upuntilnow, you’
vebeenledto
bel
ievethatthev i
olet/
blueorg r
een/or
angeopti
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

Int
hismode l
, youc a
ns e
et hatthehigh-bit-
onc olorsareshiftedahal
fposi-
tiontotherig
htoft hehigh-bit
-ofc olors
.Wha tthisme ansistha tyoucanpl
ot
pointsi
na5 60-
dotmode ,gi
vingamuc hbetterresol
utiont hant heusual28
0-
pointmode hi
. 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
riousmode 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 tposi
ti
onofaby teinme mory.
Inthischapte
rwe ’
llpresentamor edet
ail
ede xpl
anat
ionofplot t
ingapoint
a
nd,morespeciic
all
y,provideroutinesf
orsomene wwa ysofplot
tingtothehi-
r
ess
cre
en.

Nor
mal
Poi
ntPl
ott
ing
InFig ure2 0 -
1( Cha pte r2 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 the 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 tet ode 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. Int hisc a
s et het ermr eferstothea ddressassociatedwi th
their stby teont hel ineg ive nbyY.
In nor ma lAppl es oft,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 nus et heHPOSN( $F411) routi
nei nAppl e
softtodot his
ca l
culati
onf orus ,butt hel isti
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 esoft-inde pende ntrout i
ne .
2. Onc et heba s
ea ddr essoft hehor izontallineha sbe ende t
ermine d, t
hepos i
-
tionoft heby ter e
la tivet ot hel efte dg ene edst obee stablis
hed.Be causesev en
dotsa res tor edi ne a chby te,theby tewene edt oa cce
ssc anbede ter
mi nedby
dividingt heXc oordi na teby7 .hi sr esultisstor e
di nloca ti
onHNDX( $E5=Ho r
-
izontaliNDe X).Itisus e dbyput ti
ngt hec onte ntsof$E5i ntotheY- Re gi
ste
rf or
anLDA($26),Yope r ati
on–butmor eont hatlater.

]
1
98 As
sembl
yLi
nes

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

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


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

Fi
gur
e21-
1:Appl
esotCol
orMa
sks

3. hec olorma skne e dstobes e


tup. hec olorma skisabi tpatter
nt ha tshows
whi chbi tsi naby tea rea cce ptablepos sibil
itiesf orapl ot.hec olorma ski s
storedi nl ocation$E4 ( COLBYTE) .Ra t
he rt ha nl iter al
lyc alculati
ng,Appl esoft
storesa lloft hepos siblec olorma s
k ss t
arti
nga tloc ation$F6F6( seeFig ure2 1-1 ).
One sa ndz erosa reus e
dt oi ndi c a
tewhi chdot sa reona ndwhi cha reoff or
thec olori ndicated. Bla ck1i sthes impl es
t: itisa chiev edbyt urninge verydotof.
Whi te1i sitsconv e rse, achieve dbyt urninge verydoton. Not ethatbit7doe snot
corre spondt oadi splay eddota ndi slefta0( highbi tof) .
Ify ouc ompa r et hec olorma sk sforg reena ndv iol
e ttot hec hartinFi gur e
20 -
1 ,y ou’ l
lnot et ha tt heone sma tcht hea vail
a bledot sfort heg ivenc olori na
by t
e. Re me mber ,theor de roft hebi tsisre ve r
sedwhe nma ppingt othes c r
ee n, so
thatbi ts0t o6a rema ppe dl efttor i
g htont hes cre en.
hes e c
onds etofma sksi nFi g ure2 1- 1a ret hec ol
or swi ththehi gh-bits e t
(bit7=1 ).hes amepa tterna sbe for eisus e
d,e x ceptt ha tthehi ghbi tiss etf or
allfourc olors.
Look inga tFi gur e2 0-1a ga i
n,y ou’
llnot et ha tt hema sksshownont hel e ft
wi l
lwor kf ora l
l eve n-a ddr e
s sedby te s,t
ha tis,by tess uc ha s$2000, $2002, ands o
on. Fort heodd- addr es sedby te s($2001, $2003, ands oon) ,t
hec olorsa reshi fted
onebi tpos it
ion.Whe nHPOSNi sca ll
ed,a longwi t
hde t
e r
mi ningGBAS,i tc he ck s
theHNDXc al
culateda nd, i
ftha tisa nodda ddr ess,shi ftsthec olorby t
e.her esul t,
whe the rs hiftedornot ,isa lwa ysputi nl oc ation$1C( HCOLOR1) .her esultsof
suchapos si
bles hifta res hownont her i
ghts i
deofFi gure2 1-1.
(Ani nter
estingr e sultoft hispr ocessi st ha ty ouc a nnotc leart hee ntire
screent oa na ctual col or[ green, viole t
,blue ,oror ang e]byil li
ngme mor ywi tha
singlev alue.Tr yi t.Cl e arthehi -r
e ss cr
eenwi t
ha nHGR,t hene ntertheMoni tor
wi t
hCALL-151. he nt y pein:
*2000:2A
*2001<2000.3FFFM
2
1.Hi
-Re
sPl
ott
ingi
nAs
sembl
y 1
99

hes creens houldcl


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

hisl
astope
rat
ioni
spr
oba
blybe
stc
lar
iie
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)
hisput 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
edtoindic
atethebinar
y
f
orm ofanumbe .hi
r sformi susedint heremarkport
ionsofma nyofthe
s
ourcelis
tingsi
nthisbookasana ddedaidtotheexpl
anat
ions
.Althoughsome
as
semblersal
lowbinar
ynumbe rsi
nt heoperand,wehavel
imit
edtheiruseher
e
t
other e
ma rkiel
dtoreducecompa t
ibi
li
typroble
ms.

]
2
00 As
sembl
yLi
nes

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

b) Divide15( t
heXc oordinate
)by7 .her esult(
2)isputinHNDX( $E5).he
remai
nde rofthedivi
si
on( 1)isuse
dtoa cces
st hebi
tma s
kt abl
e.her e
sultof
thi
sta
blelookup(%10000010foundat$F5B2,Xwhe r
eX=1)isputinHMASK($30).
c) Che ckHNDXtoseeifthebyeofs
t etisodd.Ifso,s ftt
hi hecolorby
tema s
k.
Sinc
eint hi
scase$E5holdsa2,noshifti
srequi
red.hust hecolormask%0010
1010isputi
nHCOLOR1($1C)inprepa
rati
onfortheplot
.
4
. JSR $F45A(
HPLOT)
.hi
scompl
et
est
hepr
oce
sswi
th:

LDY HNDX ($E5) = '2'


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

screen looks like: 0100 000- Green dot lights!

hene tefe
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
ur hedotof(
nt 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 .hi sne wexplanati
onma kespos s
ibl
es omea lte
rnativewa ysof
plotti
ngtot hehi -resscr
een.Inf a
ct,byusingtheex i
sti
ngAppl e oftr
s outines
,the
newr out
inesa rer a
thershortand,bestofall
,easytoe x
pl ain.Ifyoua reunsett
led
ri
g htnowa boutt heinerdetail
softhema s
kingoperati
ons ,don’twor r
y. her ea
l
pointofa l
ltha tistog i
veyous omefe e
lforthegeneralpr ocessesinvolved.
Fors tar
ter s
,l e
t’srevi
ews omeba sicproblemse ncount e
reds of a
rwi t
ht he
nor ma lAppl esoftHPLOT.heir 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. hi 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.her eas
onsf orthi
swe r
edi s
c usse
di near
lie
rchapter
s,but
nowi tshouldbee venmor eobviousthatthecolorma s
ks pec
iiesonlyodd-dot
posit
ionsforHCOLOR=1, ma ki
ngitimpos s
ibl
etopl otatX=1 00.
hes econdpr oble
m occurswhe nyou’
replotti
ngwi thHCOLOR=3orHCOLOR
=7.Ev enthoug hweha v
es peciiedwhite,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 heir stproblem ofinvis
ibl
epoint s,onesol
uti
oni stoacceptthatthere
areonly1 4 0pointsava i
labl
eforag i
venc olorandtoalt
erourf r
ameofr e
ference
tor e
cog nizethatreali
t y
.Ane asywa yofdoi ngthisi
stoa l
wa ysworkwi thanX
coordina t
ev alueint her angeof0t o1 39,a ndthentodoubl ethev a
luewhe n
actuall
ydoi ngt heHPLOT.hema indr a
wba cktothi
sapproachisthes peedloss
duet othemul ti
pli
cations,andthefactt
ha toddc ol
orvalue
smus talsobeshifte
d
byone( sinceoddc olorscanonlyplota toddXpos it
ions).hes 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

hispr ogra
mi sde s
ignedtobec a
ll
edfrom Appleoft,s
s ervi
nga sas ubrou-
tinefora nunde ine dov er
a l
lprogram.hea dvanta
geoft her outi
nei sthat
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 cimal)
,
thesynt
a xforcal
lingitwouldbe :
CALL 768, X, Y

whereXa ndYa ret hec oordi


natesfort hedesiredpl ot.
Examiningt hel is
ting,youwi llse ethattheir ststepistous etheca l
lsto
Applesoftonl i
nes2 5through2 7t or etri
evet heXc oordi
na t
ef rom Appl e
soft.
her e
sulti
ngt wo-by terepre
s e
ntationf orthev a l
uewi l
lendupi nLINNUM( $50,
$51).
Onceweha vet hev al
ueforX, ther emainingpr oc
e s
sisv e
rys tra
ightf
orwa r
d.
heXc oordinateisdoubl edbyt hepa irofleftshiftsonl i
nes29a nd3 0.Nex t
,the
col
orby teischecke dtos eei
ftheHCOLORpr e vi
ous lyse l
ecte
dwa sanoddore ven
col
orv al
ue.Abr iefl ooka tt
hec olorma skcha rtinFi gure2 1
-1s howst hatbit1
(ra
therthanbit0 )isthek eytowhe therac olorisoddore ven.Ifthecolorisodd,
LINNUMisincreme ntedbyonet oselectthene xtoddX- coordinatepositi
on.
2
1.Hi
-Re
sPl
ott
ingi
nAs
sembl
y 2
03

heYc oordinateisthenr e
trie
vedusingCOMBYTE.Si
nceYcannotbelarger
t
han191,theone-byteretri
evalrouti
neca
nbeus ed.
Atthatpoint,t
heus ualca
lltoHPLOTisdonewiththenewXcoordinate
.
Alitt
lerumina t
ionont hisrouti
neshouldconvinceyout
hatiti
sf unc
tion-
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

hea ss
embly-l
anguagerouti
negivencanal
waysbeuseddir
e c
tl
yfr
om othe
r
a
sse
mbly-la
nguageprogramsbyde le
tingli
nes25throug
h2 7a ndchang
ing44
a
nd4 5toreadLDYY.her outi
newouldthenbecal
ledbyputti
ngthedesi
redX
c
oordi
nateinLINNUM($50,$51)
,andtheYcoordi
nateinY($E2)
.

560-
Poi
ntRe
sol
uti
onMode
hedi sadv anta geoft he1 40 -pointme thodj us tshowni sthatt her esol uti
on
oftheg raphi csi sobv i
ous l
yl imi t
e d.hi si spa rt
ic ularlya ppa rentina ttempt st o
draw ne ar-v erti
c alline s
;i t
’se asyt oobs ervet hede gr eeofs tair
- steppingt hat
occur s.Low- resolut i
onpl ot t
ingmode spr oduc ev erybr oke nne ar
-v erti
ca llines.
Ifcolori snotac onc ern( suc ha swhe nus ingabl ack-a
nd- whi temoni t
or )
,
thenwhynotj us tplotus i
ngwhi te?Sinc ewewon’ tk now t hatt hec olor sa re
actua ll
yv ary i
ngde pendingont heXc oor dinates pec ie
i d,ablack-a nd- whi tedis-
playwi l
llookine .
We ll,iftha t’
st hec a
se, t
heny oumi ghta swe llg of ora l
lyouc a ng eta ndus e
the5 60-pointmode .het heoryt othismodei st ha tt hehi gh-orde rbi tofe ach
screenby tec a nbeus edt oc hoos ebe t
we endot ss hi ftedone -halfofapos i
ti
on
withr es
pe ctt ot heus ual2 80-poi ntmode .hea rg ume nta gainstthi sme thodi s
thatt hepl ottingofdot swithint hes ameby tec andi stor ttheir stby teplott ed.
Fore x ampl e ,iftheir stdotpl ott
edi sont hef arthe stleftpositionpos s
ible
(highbi tof) ,the nas ucces s
ivepl otofa nyHCOLORwi tht hehi ghbi ts et( HCOLOR
=4t hroug h7 )wi llcha nget hec oloroft hedota nds hi ftittother ight. Asi tha p-
pens ,thisi snotmuc hofa na rgume ntsinc ethes amehol dstruef ort henor ma l
280 -pointmode ,a nde venf ort he1 40-poi ntmode .hei nescapabl ef actist hat
plottingt woc olor swi thconli ctinghi gh-bi tcondi ti
onswi thi
nt hes ameby tewi l
l
alwa ysafe c ttheir stdotpl otted. Ifthedi storti
oni suna voidablethe ny oumi ght
aswe llenjoyt hebe ne itsoft hehi gherr esoluti
on,e s pe c
iall
yi fyou’ reg oi ngt 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 inei mpl eme ntingt 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

hema
i fe
ndi r
enc
ehe
rei
stha
tXc
annowha
vear
ang
eof0t
o55
9,a
ndt
hat
HCOLORi
sal
way
sse
ttowhi
te
.Aswi
thnor
malAppl
esoft,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

heope ra t
ionoft hisroutinei sa l
sofairl
ysimple. Aswi t
hthePLOT.140pr o-
gram, thevaluef orXi sretri
eve df rom thec al
li
ngpr og ram.Inthi scase,thoug h,
CALCd ivi
dest hepa s
s edv a
luebyt wo. Not ethataleft-
shiftoperationisus ed,not
therightshifts(foramul ti
ply)t hatwe r
eus edinthe1 40-mode .
You’llrec a
llt hatLSRLINNUM+1( LSR=L ogica
lS hiftRight
)wi l
lshiftallbits
inLINNUM+1( t
hehi g h-orderby te
)t ot herightonepos it
ion,forc i
nga0a tthe
rig
htmos tpos it
iona ndput ti
ngt heol dbit0int hecarry.hi sisimme diatelyfol-
lowedbyt heROR( “rotateright ”)instructi
onwhi c
ha gainshiftsallthebi tsin
LINNUM( thel ow- orderby te),put st hec a
rryintobi t7 ,anddr opst helas tbit0
intothec ar
ry ,t
husr eplaci
ngt heol dv alue.Fore x
ampl e:
X-COORD = 289 = $121 = %0000 0001 0010 0001
LINNUM+1 LINNUM

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


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

herathercoi
ncident
albe
aut
yoft hi
sisthatt
hecaryla
r gwil
lendupbe
ing
setorcl
ear
e ddependingonwhethe
rt heori
g i
nalva
lueforXwasoddoreve
n.
hi 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 st odi v i
det heX c oordi
na tebyt wot og e
tav al
ue
ac
ceptabl
et onor malApplesoft,a ndt henforcethec olort obee i
therwhite1or
whit
e2de pendingonhowwewa ntt hehighbits e
tintheina lplot
.
Lines28t hrough32s e
tt hec olorma sktothea ppropr iat
ev al
uebyche c
k i
ng
t
hec a
rryla gt oseeiftheor ig
ina lv a
lueofXwa soddore ven.he nLINNUMis
t
ransf
erredtooura ctua
l X-coordi nateby t
es.her outinei sthencompletedwith
t
heusua l
c a
lltoHPLOT,aswa sdonei nt hePLOT.140rout ine.
hi sproce s
scouldbesimul ate dfrom Apple oftwi
s 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’
llindthea
sse
mbly
-la
ngua
ger
out
inec
ons
ide
r-
a
blyf
ast
er,
andcert
ainl
ymuc hea
siert
oimpl
ement
.

ADe
mons
tra
tionPr
ogr
am
Tog iv
ey ousomet
hingtoshow ofthes
erout
ine
s,her
e’sapr og
ramin
Appl
esoftt
hatca
ll
sbot
hrouti
nesa
ndshowst fe
hedi r
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 skil ea si t
’sneede d.
Basicall
yt hisillustratesthes t
ee pe s
tv ertic
a la ng l
ea twhi c hal inec a nbedr awn
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 lresultofthepl ot,
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
. ha tistosay, a
mov eofiv epoi ntshor izont a
llyont hes cree ni sa boutt hes amea ctua ldis
tanc e
ont hescre ena samov eofiv epoi ntsv erti
ca l
ly .hi sensur estha tas qua r
ewill i
n
factlook“ squa r
e”whe n dr awnon t hes cre en.hust heir stpl oti satt he
“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 cros
sha s
thee fectofc ompr essingt hes c ren.he
e s ee fe ctsmus tbec onsideredwhe n
doingg eome tr
icde signsont hes creen.
We ’
llleavei tasa ne xe rci
sef ory out odr a wt hr eepa ral
lel l
inesus inge ac
hof
thethr e
emode s.
Bynow, you’ vepr oba blya l
s onot iceds omemi norla wsi nt hec l
arit
yoft he
560-pointl ine.I nthene x tchapt erwe ’
llexpl or ethema tterf urther,di sc
overing
whyt hef aintspot soc cura ndhowt oixt he m.
2
2
2
2. Ev
enBe
tte
rHi
-Re
sPl
ott
ing
J
uly1
982

hepr eviousc hapt erc onc l


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

]
2
08 As
sembl
yLi
nes

Whe ny our unt hisprog ram, entere it


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

I
nte
rac
tionsbe
twe
enAdj
ace
ntBy
tes
hee nti
repr emi seoft he5 6
0 -
pointmodewa sthatt hehi gh-orderbi tof
eachbyteafectedt heina ldisplaypositi
onofe achot herbitwi t
hi nit.Weha ve
see
nhowc hang i
ngt hes ta
tusofbi t7( t
hehi gh-orderbit)ma ys hiftagivendot
one-hal
fofapos it
ion, de pendingonwhe therthebitisset
.
Nowfort hene wwr ink l
e.Itturnsoutthatfordot sassoci
a t
edwi t
hbi t6ofa
byte
,thehigh-orderbi tofthene xtb yt
einme morya ls
oa fectsthedi s
p l
ayo fthe
irs
tb y
te.
Asane xample ,ir 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 fectsimi-
la
rtothede 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)

her e
ferencestoawi dthareanapproa
cht oexplai
ningwhathappens.If
youhaveablack-and-whitemonit
or,
therel
at
ivevis
ual s
tre
ngthsoft
hedotsca n
berela
tedtoa na ppare
ntwi dt
hofthedotswhe nil
luminatedonthemoni tor
scr
een.Onac olortel
evi
sionormonitort
hewidthsaren’
tdisc
erni
ble
,butdifer-
encesi
ncolorandbr i
ghtnessca
nbeseen.
2
2.Ev
enBe
tte
rHi
-Re
sPl
ott
ing 2
09

Be forea nyf urtherexpl anati


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

Some“
Newa
ndI
mpr
ove
d”Rout
ine
s
We ll
,then. .
.t
ha thasbe enal ottodig e
st.Inf act,atthi spointy oumi ghtjust
wa nttot akeabr e
a ktol etever
y t
hingsinki n,ma ybeixy our s
elfani cecupoft ea
andme dit
ateoni tforawhi l
e .
Gladt os eey oua gain!Oneoft hedi ic ulti
esi npr ese nt
ingt hema t
eri
a lin
thel astfewc ha ptersha sresul
tedf r
om t hedi scov e
ryt ha thi-resgraphicsisnot
alltha tlogical.Muc hofhi -resg r
aphicss eemst obev erye mpi r
ic a
li nnatur e
.
ha tis,it’
smor eama t
terofa ccepti
ngt hatt hingsa reac ertai
nwa ya sderived
from e xperime ntati
on, thanoft r
y i
ngtoa ccountf ort hei 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,a nds of orth,whi chismos t
ly
incident a
ltot hepr ogramme r!
)
hewor stisproba blyov e
r,though. Att hispoi nty ous houldha v eatle
a sta
gene ralfeelforhowt hedot sarema ppe dont hes creen.Le t
’snowc reat
es ome
ina lr outi
ne st hate ncompa s
st hev ariousqui rksoft hehi -r
ess ystema sit
present l
ye xi
sts.

]
2
10 As
sembl
yLi
nes

PLOT.
140+
heir s
tonet oixi 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
heir
oft s
tbyt
e.Her
e’st
hene wrout
inetodojus
ttha
t:

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

0321: 20 4C E7 43 GETY JSR COMBYTE


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

hel istingt 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 djusteda c cor dingt ot het hree -s
te pprocedur ej us tde scribe d.
heir ststepi st oe x ami nel oc ati
on$30( HMASK) .You’ llr eme mbe rf rom t he
previousc ha ptert ha tt hi sisama 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 tc hisf ound, wek nowbi t6wa ssetbyt hepl ot. Wemus tnowa ccess
thene xtby teinme mor ya nde it
he rs e
torc le
a rbit7oft ha ttoma t
chouror iginal
byte.S inceHNDX( $E5)h oldst heofs etoft hec urre
ntby te( us ua l
lyus edbyc om-
biningwi thGBAS( $26)i nt hef or m LDA(GBAS,Y)) ,wec anl oa dt heY- Re gist
er
withHNDXa ndt he ni nc reme ntus ingt heINYonl i
ne5 4t os hi ftoura ttentiont o
thene xtby te.heda t
af ort ha tby t eisthe nl oadedint ot heAc cumul atoronl ine
55. Nowf ort hes leig htofha nd. Wewa ntt oc heckthes ta tusoft heir stby te,but
ifwel oadt heAc c umul atorwe ’
lll oset heda tac urrentl
yhe ldt he re.Tos olvet he
probl em, cons ide rt his :hec olorma skby t
eCOLBYTE( $E4)h ol dst hema skus ed
onlymome ntsbe for et odot hepl ot.Wec a nc heckt hehi g h- orde rbi toft his
valuet ode termi net hes tatusofbi t6i nt heby t
ea ccesse dbyt hepl ot.S incei t’
s
bit7we ’reint erestedi n, wec ana lsous et heBITc omma ndt odot hec he ck .
Line5 6doe saBIT COLBYTE. hi swi llmov ebit7ofCOLBYTEi ntot heS t
atus
Re gi
s ter,afterwhi 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 le a
r” )se ct
ionwi l
l
bee nt ered.I nt hiss ect i
on,t heANDope ratori susedt of orc et hec learingoft he

]
2
12 As
sembl
yLi
nes

highbiti ntheAccumul a
tor.Si
ncethiswi l
lals
oc l
earthes ig
nbi toftheS ta
tus
Registe
r,theBPLfoll
owingthisoper
ationisal
wa ystake
n.
IfHISET(“hi
gh- b
itset
”)isent
ered,theORA #$80wi l
lforcetheset
tingofbit
7oft heAc cumulator.(
Ifyoune e
dmor einfor
ma ti
onont helogi
calope ra
tors
,
youma ywi s
htoc onsul
tcha pt
er12.
)Li ne61( STORE)putst heconte
nt softhe
Accumul atorbac
ki ntome mory,imme diat
elyfoll
owedbyt heRTSwhi c hends
theroutine.

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

031F: 20 4C E7 41 GETY JSR COMBYTE


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

Ordinari
lythi
swoul dbeainepl acetoe ndthisc hapter
,butther
e’sone
morerouti
newor t
hpr ese
nting.Sofarwha tyou’vegotisac hoic
ebet
we enplot-
t
ingasinglecol
or(PLOT.140)o rtak
ingwha tevercolory ougetinex
c ha
ngef or
t
hecapacit
yforgre
aterhor i
zontal
resolut
ion.
We l
l,wit
hjustal i
tt
lemor eefortwec anc re
atear outi
nethatwilofe
l rthe
sa
mede greeofhorizontalaccur
acya ndg ua
ranteethata nydotplot
tedwi l
lbe
whit
e.

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

]
2
14 As
sembl
yLi
nes

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

032F: 85 30 49 STA HMASK


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

hi sr outi
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 ndtot henor ma lApple oftr
s outine.heir stdifere
nc eappearsatline
50,whe r
eaJSRHPOSNi spe rfor
me dinsteadofaJSRHPLOT. hi si sdonetoa ll
ow
Applesofttog ot hroughi tsusualpr eparati
onf orapl ot.hi 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 rked”f ort heupc omingPLOT.Howe ver,byus ingtheASL,ORAHMASK
combinationonl i
ne s47a nd4 8
, wec ans ftt
hi 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

headdre
ssus
ual
lygive
nfortheHPLOT r
out
ine
,$F457,i
ncl
udesaJSRto
HPOSN.Be
caus
ewe’
vealr
eadydonethi
s,aJSRHPLOT+3accompl
is
hestheir
st
stageofourope rati
on; name l
y,theplott
ingofapur ewhi t edot .
Now therema iningpr oblemi stotak ec a
reofe nd- of-the-
bytelaws.hi 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
ti
ons1 0a nd1 1,bits5and6wi l
l beset.
Be ca us ebit6canbeafecte
d
bybi t7ofthene xtbytei nme mory,achec kforbitag r
ee me ntmustbema de.
St
rangerst
ill
,ifpos i
tions1 2and1 3a replotted,onlybi t6isavai
la
ble,whic
h
woul dnor mall
yputusba ckt ohavingplottedonl yac olor eddot Toixt
. his
,we
ha vetog oagaintot hene xtby t
einme mor yanddoa not herplottoil
luminate
justtheveryirstdotoft ha tby t
e.
I
ng enera
lthen, t
hepr ocesswillbe
:
1. Fi xHMASKt oturnont 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
ytheir
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
ualpl ot
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.heBITc omma ndf orc
esbit6of
theS t
atusRe gister( t
heov erlowla g)t othes amev aluea sbit6ofHMASK.hus
BVS (Br a
nc hoVe rlow S et)andBVC ( Br a
nc hoVe rlow Cl e a
r)c anbeus edt o
checkf orbi t6s etorc lea
r,respe ct
ivel
y .Inourc a se,BVCwi l
lbr anc htothee x i
t
point,DONE, ifbit6i sclear.
Ifbit6i ss e t
,l i
ne s57−63s etthehi g hbitoft heot he rc olorma skby te,
HCOLOR( $1C) ,toa gre
ewi ththepr evi
ouspl ot.hi sc olorma skby teisusedl ater
byHPLOT. Bec ausewe ’
llbeskippi ngtheus ualentrypoi nt($F457) ,weha vetos et
thisbytespe ciica l
ly.
Onc et hec olorby teisset,a notherc heckisdonet os eeifbi t5i sset
.hi si s
donebya gainus i
ngt heBITc omma nd.S inceonl ybi t
s6a nd7c anbec hecke d
viatheS tatusRe gist
er,wemus tloadtheAc cumul atorwi thanume ri
cima geof
thebitwewi sht ot e
stfor.Inthi scase,thev al
ueus edi s#$20( %00100000) Afte
. 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
Toinishthi
ngsof,let
’str
yonel
a s
tApple
softpr
ogra
mt oma keuseoft
he
newr outi
ne.hi
s sisanext
ensi
onoftheir
stli
sti
ngpres
entedatt
hebe gi
nni
ng
ofthischa
pter
,anditwil
lgi
veyouanoppor
tuni
tytocomparether
elat
ives
cre
en
appearanc
esofdife
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.heofsetf
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
hertoshowoft 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
he ser out inesa r
ebe stus edi nma thema ti
c alcha rtsrathert hani npur e
graphicssucha spictures.hema ina rgume ntag ainstthe5 60-pointmodei sthat
youc an’tbea ssuredt hatpl otti
ngonepoi ntwi llnota fe ctne arbypoints.As
we’vede mons tr at
edhe re
,t herea ppa rent
lyi snoa ppr oac hthatc a nguarantee
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 swillwi de ny ouropt ionsforyour
ownpr ogrammi ngg oal
sa ndt hatt he y’vet
a ughty oual ittl
ea l
ongt hewa y.
heus ua la pproachi nt hisbookha sbe ent os impl i
fya nyi deawhe nir s
t
present
ingit.I nt hea reaofg raphi cs,thoug h,simpl i
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
a rti
ngt og e
taf eelforhowt he
contentsofme mor yafe ctwha ti sdi splay
e dont hes creen.Int heina lanalysis
,
therealchalleng eofhi -resg raphic sisma ni pul
a t
ingt hec ontentsofme moryt o
producethev i
s ualefectsy 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 turnsthev a
lueoft hecolorof
thes creenattheX,Yc oordinatespe ciied.Unfor tunat
ely,noe qui
va l
entfunc-
tionex i
stsf
orus ewi thhi -r
esg ra
phic sinAppl eoftBAS
s I
C.
Int helastfewc ha pterswe ’
ves ee nhowt opl otpointsinav ar
ietyofwa 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 chit
’simpor t
ant
tok nowwhe noneobj ectistouchinga not her
.Usingt heSCRNr outi
neg iv
enhe re
,
youc a ntes
tt ose ewhe thera nypoi ntsha vealr
eadyb eenplotte
da 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 edt heAppl esoftHPLOT
rout i
net opl otapoi nt .heXa ndYc oordina tesf orthepoi ntwe repa ssedt othe
rout i
nev ianor ma l Appl e softv ariabl es.
heina lpl otwa sa ccompl ishe dbys ettingapa rt
icularbi twi thinaby teof
me mor y .hebi tt obes etisde termi ne dbyc reatinga“ ma sk”fort hebi tpos it
ion
withint heby te.
Figur e2 0 -1( ourol df riendf r om c hapte r2 0)wa suseda 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 lue Bi na r
y
respondi ngt oag ive nX,Y c oor di - Bl a ck1 0 0000 0000
nate ha s be e ns e t
,t ot a kei nt o Gr ee n 1 0000 0001
accountt hehi g h- orde rbi t( bit7 ) Vi olet 2 0000 0010
whe rene ce ssar y,a ndt he nr e turna Whi te1 3 0000 0011
valuebe twe en0a nd7c orrespondi ng B l
a ck2 4 0000 0100

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


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

]
2
22 As
sembl
yLi
nes

Whe nallofthe
sechecksa
recol
lec
tedint
oaroutine,weha vethefol
lowi
ng
procedure.
1. Li nes28−38ret
rie
vetheval
uesoftheXandYc oordinat
esfrom theApple-
softc
a l
lcomma nd.he s
earetr
ansf
err
edtothehi
-r
esre g
iste
rs($E0−$E2).
2. hev aluef ort heXc oor dina tei sre t
ur ne di nLINNUM( $50, $51)a nd, ass uc h,
canbec he cke dforwhe the riti soddore 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.hee asiestwa ytodot hi s
istous et heLSR( Log icalS hi ftRi ght )comma ndonl i
ne4 1t os hiftt hel astbi t
intothec ar ryla 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 tionsi ndiv i
dua lly,buti tt urnsoutt ha titi se a sier
tos e
tupt heina 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 −38wec annowus ei ta s
ourwor kingr eg i
ste rfort hec olorv 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 est,BCS( Br anc honCa rryS et),tos eewhe thert he
coordina tewa soddore v en. Ift hec 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 llshiftthepa tternt o
theleftonepos it
iont ocor respondt ot he“ eve n”c ol or s.
3. Li ne s4 8−5 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 e f
re shingont hi s.Li ne s5 −5
3 5l oadt heby tei nto
theAc cumul a t
ora ndpus hi tont ot hes tackt ober etr i
e vedl ater.
het estonl ine5 6c he ck sf 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 −3a nds etfor
HCOLORS4 −7 .Bi t2i sse tus ingt heORA( l
og i
c alORwi thAc cumul ator ).Ift he
high-orde rbi twa sc lear,thel og i
ca lANDc omma ndi sus edt oc learbi t2 .
4. Fi na lc heck .Nowwene edt os eewhe the rt hedotwa sa ctuallyt ur nedon.
heme 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 tethebi 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 nywa y).As
ane xampl e ,suppos et hatt heme mor yl oc ationha dhe ldt hev a l
ue#$9Ba ndt he
valueforXwa s4 :
Not et heina 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
nproceedtotheinal
e
xitpha s
e.I
fthedotisof,t heAND#$8Conl i
ne7 1wil
lcl
earonlybit
s0a nd1.
hisa l
lowsustode t
erminet hest
atusoft
hehigh-or
derbit
,eve
ni fadotisnot
a
ctual
lyplot
tedattheposi
tiong i
ven.
6. SEND( l
ines75−8 4)isi
dentic
altotheREAL VARIABLE SENDroutineg i
venin
chapte
r1 7andisus edt osendourr esult
ingv al
uebacktoApplesoft.heonl y
thi
ngdi fer
entint hiscaseisthatther outineloadsa0intotheAc c umula
tor
ins
teadofthehigh-orde rbyteofLINNUM( LINNUM+1)s
inc
e,asme nti
one dprevi
-
ously
,thevaluef
orc olorwil
lneverexceed2 55.

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

whereXa ndYa ret


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

hepr og
ramg oesthroughtwopa s
ses heir
,t stplott
ingalle i
ghtcol
orsat
X=0,andthesecondwi t
ha lle
ightcol
orsatX=1 .Afte
rdoi 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 hi
,or61. si sbeca
us ewhenwhiteis
s
pecie
i d,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
heSCRNr outinec anbeapplie
di nav ar
iet
yofwa ys.I
ng ener
al,youc anus e
thisrouti
newhe nev ery ouwanttoe xaminethes c
reentode t
erminewha tcolor
hasbe endrawn.Pos sibleappli
cati
onsmi ghtincl
udeg r
aphicspri
ntingr outines
andg ame si
nwhi chi tisneces
sarytodete
r minetheexis
tenceofli
nestha trepre-
sentwa l
lsorobstacle
s.
Ifyouwisht ous etherouti
nedi r
ectl
yf r
om assemblyla
ng uag
ewi thoutc all
-
i
ngi tfrom Applesoft,simplydel
etetheentryrouti
nesandl oadLINNUMwi t
ht he
Xc oordinat
eand$E2wi tht
heYc oordinate.
2
4
2
4. heCo
lli
si
onCo
unt
er,
DRAW,
XDRAW
S
ept
emb
er1
982

Int hepr evi


ouscha pte
rwel ook eda taroutinetos imulatet heSCRN(X,Y)
f
unc tionofBAS I
C. henot ionofi nquiringaboutpoi ntsont hescr e
eni sclose
ly
rela
tedt ot hi
schapte
r’stopi
c,thec olli
si
onc ounter.
h ec oll
is
ioncount erisaone -byt
eme moryl ocati
ononpa gez erooft he
Appl e’sme mory.It
sva l
ueisaf unctionoft heAppl eofthi
s -
resgraphi c
sr outi
nes
speciic a
ll
yr el
ate
dtos hapetables.hepur poseofthec ol
lisi
onc ounterist okeep
t
r ackofa ny“ c
oll
isi
ons”be t
weenas hapebe i
ngdra wnont hescreena nda nypre-
viouslydr awns cr
eeni ma g
es.hec oll
isioncount e
ri slocateda t$EA( de ci
mal
234)a ndi safect
edonl ybythec omma ndsDRAWa ndXDRAW.

SomeEx
per
ime
nts
Toill
ust
ratethebe haviorofthecoll
isi
onc ount
er,we’
llir
stne e
das ha
pe
tabl
etoe x
peri
me ntwith. heoneg i
venhereisproba
blythesi
mplestonepos
si-
ble–asingl
edot.
Toentertheshapei ntome mory,gointotheApple’
sMonitorbytypi
ngin
CALL-151<RETURN>,andt henente
r:
300: 01 00 04 00 04 00
E8: 00 03

hi
swi
llpl
acet
het
abl
einme
mor
yatl
oca
tion$300a
nds
ett
hepoi
nte
rat
$E8,
$E9t
opoi
ntt
othet
abl
e.
heir sttwoby tesofthet abl
e( $0100)i ndicat
et henumbe rofshape sinthe
table,whi
chi nourc aseisjustone .hene x
ttwoby t e
s( $0400)g ivet heofs e
t
from thebe gi
nningoft het able( $300)t othes tar
toft hea ct
ua lsha peda t
a
($304).hene 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 it
ionupt 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(1
9 78),
pa g
es9 2−9 6.
heir stexperime ntistov eri
fyt hatweha veinf actinsta
ll
edaus ableshape
table.hisismoste asi
lyteste
dbyput ti
ngy ourAppl eintoAppl esoftBAS I
Ca nd
typingin:
HGR: HCOLOR = 3: ROT = 0: SCALE = 1

]
2
26 As
sembl
yLi
nes

hes
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
call
ineoft hre epixelss
houlda ppear.Ifa l
lhasgonewe lls ofar
,y ou
cannow t ryat hir
de xpe riment.hepur poseoft hee x
peri
me ntwi llbetos ee
how thec oll
isi
onc ounterr eactswithv ar
iouscombi nat
ionsofdra wingc olors,
backgr oundc ol
ors,shape -drawingcomma nds,andt hepr evi
ouscondi ti
onoft he
coll
is
ionc ounter.
Cl earthescreenwi thHGRa gai
na ndtrythisseque nceofcomma nds,noting
foreac honewha tthec ondi t
ionsoft hescree
na ndc oll
is
ioncountera rebefore
anda fterthecomma ndi se x
ecuted.(Notethatreferencesto“col
or”int hischap-
terwill beintermsof“bl ack”a nd“whi t
e”aswoul dbes eenonabl a
c k-and-whi t
e
moni tor .I
fyouha veac olordi s
play,t
hedot swil
l appearassingl
e-colordots–a s
expla
ine dinprevi
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.heCol
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

heir stc olumns howst hev al


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

3. hepr evi
ouss t
ateofthec
oll
is
ioncount
erhasnoefec
tontheinalval
ue
afte
rtheDRAW orXDRAW.hismeansthatnopr
econdi
ti
oni
ngorini
ti
al
izi
ngis
necess
aryi
nagivenrouti
ne.

DRAW v
ers
usXDRAW
Beforepr
oceedi
ngf urt
herwiththec oll
is
ioncounter
,iti
simpor t
anttotake
amome nttocl
ari
fythedist
inct
ionbe t
wee nthetwoshape-t
abl
ecomma ndsDRAW
andXDRAW.
DRAWisver
ydi r
ecti
nt hati
tbasical
lydoe sanHPLOTinwhatev
e rthecurre
nt
HCOLORi s
,usi
ngthes pe
c ie
i dshape.Asme nti
onedearl
ier
,thecol
li
s i
onc ount
er
si
mpl yaddsupthetotalnumberofc ol
li
sionswithexis
ti
ngdotsinthes a
me“ on”

]
2
28 As
sembl
yLi
nes

or“of”s tat
east heHCOLORbe ingus e
d. Mov i
ngs hapeswi t
hDRAWi sdoneir stby
drawingtheig ure, andthene itherrever
singthec ol
orbys ett
ingHCOLORt 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 unctiontoact
ua ll
y
revers
ethebi t
sont hescree
nwhe r
ethes hapeistobedra wn.Wha tthi
sme ansis
thataix edcol
ora ss uchisnotus ed.Ra t
her,eachbitont hescreeni nt
hedesired
shapepa t
ternisr evers
edf r
om i t
scurrentstat
us.Byf ol
lowingt hiswithanother
XDRAW, t
hescreeni sres
toreda nde xis
tingbackgroundig uresa r
enote 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 ea
l
wor d.hepr
l 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 ectofofawa l
lors omeot herobject.In
thi
ss ens e,wec ans ayt hatc olli
si
onswe repr edictedratherthande te
cted.he
coll
isi
onc ounterg i
vesusawa yofde tect
ingc olli
sionswithobj ec
tsonthes creen
whos ec urrentpos it
ionma ynotbek nown.hi stakesonpr act
ica
lsig ic
ni ance
whe ny ouma ynotwa nttok eept rackofa llthet hingsly i
nga boutthescreen,as
i
squi tepos si
bleinma nyga mes ce
na ri
os.
Put ti
ng a l
loft hi
st oge ther,wec omeup wi tht hef ol
lowing g e
ne ra
l
approa che s
:
1. DRAWaig 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
igureswi l
lbeera s
edwhe nus ingthistechni que .
2. XDRAWaig ure.hev alueinthec ol
li
si
onc ountershoul dequalthenumbe r
ofdotsintheig ure(t
ha ti
s,aconstantval
ue )ifther
eisnoc oll
is
ionwithex i
st
ing
ima g
es.XDRAWag a
intoe ra
s ehev
. aluei
nt hec oll
is
ionc ountershouldreturnto
0i fnopr ev
iousc ol
li
sionwa sma de.hi swi lllea
veba ckgroundi ma g
esi nt
act
,
butig 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
.

Th eSc
anne
r
hef oll
owingtwode monstra
tionprog r
amsarecal
ldTheS
e canne
rbe
caus
e
t
heyarer
e mini
scentoft
heclass
icradarscr
e e
nsweeppat
ter
n.
heir stprogra
m usestheXDRAW,XDRAWsyst
em ofr
edra
wingt
heimag
eand
t
hus,
isnonde s
truct
ivet
ootherimag e
sont hesc
reen.
2
4.heCol
li
si
onCount
er,
DRAW,
XDRAW 2
29

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

]
2
30 As
sembl
yLi
nes

7032: 84 08 57 STORE STY SCL


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

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

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

Exper
imentwithdifere
ntpaddlevalues,sl
owlyinc
reasi
ngther adi
uswith
pa
ddle0untilthesca
nnerinte
rse
ctsthev e
rtic
alli
ne.Atthatpointyoushould
he
aranumbe rofcl
icksf
rom t
hespeakerastheli
nescros
seachother.
Let
’sse
ehowt heprogra
m works.Line2 7st
art
stheactua
lcodebyj umping
ov
ertheda t
af ortheshapetabl
e.hi sist hesameone -
dotsha petabl
eyou
2
4.heCol
li
si
onCount
er,
DRAW,
XDRAW 2
31

enterede arlieri nt hi sc ha pter .Lines3 2and3 3i nitia l


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

]
2
32 As
sembl
yLi
nes

Inpracti
cethespeakerisali
ttleunde pendablebecauset hefrequenc yofthe
c
li
cksissohi gh.Youma ywi shtoe x per
ime ntwithdi ferentde l
ay sint heCLK
l
oop,asisdonei nthesoundr outines.Youma ypreferthec urr
entme thodfor
t
hisdemobe causeoftheintui
tivena tureoft hecli
cks,butmus ic
a lsoundsc an
a
lsoprov
idesomei nter
est
inginsightsintothepr oc
ess.
heus ualHGRequiva
lentfrom t hisroutinehaspur pose lybeenl eftoutto
a
ll
owy outoa lte
rthescreenwithHPLOTa ndot herAppl esoftcomma ndsbe f
ore
r
unningthescanner.Anotheri
ntere s
tingvariati
onistot y
pei n:
HGR: HCOLOR = 3: HPLOT 0,0: CALL 62454

hes
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 ine,
alt
houg hs omei nt er
est
ingv ariati
onc a
nbeobs erv
edde pendingont hea ngl
eof
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
elfsuic i
e ntl
ywi t
htheir stprogram,trythis
secondv ariati
on, onethatus estheDRAW, XDRAWme t
hod. Herethepointofinter-
estisthatthes canninglinee rasesany t
hi ngi ttouchesands oleav
e savis
iblet
r a
il
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.heCol
li
si
onCount
er,
DRAW,
XDRAW 2
33

7000: 4C 09 70 27 ENTRY JMP E2


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

]
2
34 As
sembl
yLi
nes

7067: D0 FA 83 BNE CLK


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

I
nt hi
srout i
ne,heir
t s
tva ri
ationisi ntheuseofDRAW(ver
susXDRAW)online
77.Inaddit
ion, bec
ausewea renowus ingtheDRAW,XDRAWmethod,thecol
li
si
on
counterdet
ec t
ionnowg oe
sa fterthei 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, howdiferentl
ythes c
reenbe haves.
I
tismos tinst
ruct
ivetostartbyt ypingin:
HGR: HCOLOR = 3: HPLOT 0,0: CALL 62454

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

Asy oue v e
ntuallys we e
pouta l
lpos s
ibleanglesa ndradii,you’l
lnoti
cethat
nota l
lscreenl ocati
onsc anber eachedf rom aix edpoi nt.hi si
sbe c
auseofa
l
imi tednumbe rofr otationpos i
tions( asoppos edt oac ont i
nuous3 60-
degree
mot i
on)a nda ls
obe caus eoftheli
nena tur
eoft hescre endispl
ay.
Bylook ingcarefullyy oucanse ethattherearemor epointinter
cept
ions,
and
thusc oll
isionsa ndc li
c ks,atthene ar-verti
cal
,-horiz onta
l,and4 5-degr
eepos i
-
tionsthana tthea nglesinbe twee
n. hi st
e ndstogiv eamodul at
edsoundtot he
clicksasthe“ beam”s canswhe nrunni ngtheir s
tpr og r
ama g
ainstawhiteback-
gr ound.

Th ePos
sibi
li
ti
es
Oncey ouunderst
andt heideabehi
ndt hecoll
isi
onc ounter
,itcanbev ery
usef
ul i
nbot harc
adegame -
typesoftwa
reandothersimulati
ons.You’l
lprobably
beabletoima gi
nealls
ortsofnovelwaysofappl
yingthistechni
queiny ourown
programs.
Inthene x
tchapte
r,we ’
llg
ivenon-graphi
csenthusias
tsabr ea
ka ndlooka
li
tt
lemor eintosomea r
easofas s
embly-l
anguag
epr ogrammi ngthatweha ve
n’t
yetcovere
d.
2
5
2
5. Ex
plo
sio
nsandS
pec
i Efe
al 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.hi 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 omespecialefect
s.

Ex
plos
ions
,Ra
ys,
andOt
herTh i
ngsTh a
tGoBumpi
ntheNi
ght
heba 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, thissampleli
st
ing:
1 ********************************
2 * *
3 * AL25-SIMPLE NOISE ROUTINE *
4 * *
5 ********************************
6 *
7 * OBJ $300
8 ORG $300
9 *
10 DRTN EQU $06
11 NUM EQU $07
12 SPKR EQU $C030
13 *
14 COMBYTE EQU $E74C
15 RND EQU $EFAE
16 FAC EQU $9D
17 *
0300: 20 4C E7 18 ENTRY JSR COMBYTE
0303: 86 06 19 STX DRTN ; SET LEN OF 'NOTES'
0305: 20 4C E7 20 JSR COMBYTE
0308: 86 07 21 STX NUM ; SET # OF 'NOTES'
22 *
030A: 20 AE EF 23 LOOP JSR RND ; CREATE A RND VALUE
030D: A6 06 24 LDX DRTN ; SET A COUNTER
030F: AD 30 C0 25 TICK LDA SPKR ; TOGGLE SPEAKER
0312: A4 9F 26 LDY FAC+2 ; PITCH=RANDOM VALUE

]
2
36 As
sembl
yLi
nes

0314: 88 27 DELAY DEY


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

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

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


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

50forDRTNa ndNUMc r
ea t
ess ortofar ay-
g unsound,andthepa ir3,20pr oduc e
s
areasonablee xplosi
one fect.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,infac t
,thek eytoanyde init
ionofnoi se,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.heRNDf unc t
ionhe l
psus
tocreatether andom da ta,buti ttake ssolongtoe xec
utether outinefore ach
notethatt hereisal i
mi ttot henumbe rofnot eswec ang e
ne r
a t
einas hort
peri
odoft i
me .
Onet echniqueweus e
de arl
ierwhe ns peedwasapr oblem wa stabl
el ook-
ups.Le t
’sapplyt histechniquet os oundg e
nerat
ionands eewha twec anpr o-
duce.
1 ********************************
2 * *
3 * AL25-SIMPLE NOISE ROUTINE 2 *
4 * *
5 ********************************
6 *
7 * OBJ $300
8 ORG $300
9 *
10 CTR EQU $06
11 DRTN EQU $07
12 PTCH EQU $08
13 SPKR EQU $C030
14 *
15 COMBYTE EQU $E74C
16 RND EQU $EFAE
17 FAC EQU $9D
18 *
0300: A9 00 19 INIT LDA #$00
0302: 85 06 20 STA CTR
0304: 20 AE EF 21 LOOP JSR RND
0307: A5 9F 22 LDA FAC+2
0309: A4 06 23 LDY CTR
030B: 99 00 10 24 STA $1000,Y
030E: E6 06 25 INC CTR
0310: D0 F2 26 BNE LOOP
0312: 60 27 DONE RTS
28 *
0313: 20 4C E7 29 ENTRY JSR COMBYTE
0316: 86 08 30 STX PTCH
0318: 20 4C E7 31 JSR COMBYTE
031B: 86 07 32 STX DRTN
33 *
031D: A0 00 34 READ LDY #$00
031F: B9 00 10 35 BYTE LDA $1000,Y
0322: A2 08 36 LDX #$08
0324: 4A 37 SHIFT LSR
0325: 90 03 38 BCC NEXTBIT

]
2
38 As
sembl
yLi
nes

0327: 8D 30 C0 39 TICK STA SPKR


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

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

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


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

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

STA $1000,Y
INY
BNE LOOP

Althoug hi t
’smuc hs hor tera ndmor edi re ct,ther outinef ail
sbe ca us eRND
scramblestheY- Re gister,thusl os i
nga nyr unni ngv al
uef orourpos it
ioni nt he
tablebeingc re ated.hi sfac tne ce ssi
tatest heus eofaba ck-upc ount er( CTR)t o
reme mbe rthec ur rentv al
uet hatYs houl dbes ett o.
heINITr out ine ,the n, willil 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 alue sfor
PTCHa ndDRTNf rom t hec a l
lingAppl esoftpr og ram.
READt hens ta rtsthepr ocessofs ca nni ngt heda tata blefort her andom da ta
tobeus eding e ne ratingt henoi sepa t
te rn.het 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.
Afterea chby tei sl oade di ntot heAc cumul a toronl ine3 5,abi t-
s hiftingr ou-
tineisexecute de ightt ime st ode termi net heonorofs 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 hiftRi ght)t omov ea l
l ofthebi tsin
theAc cumul a toronepos itiont ot her ight .hee nd- pos it
ionbi t,bit0 ,f allsi nto
thec a
rry.
Line3 8t he nt e st
st hec arryla ga nd, ift hela gi scle ar(bitnots e t)
, sk ipst he
speaker-t
og glings tepf ounda tline3 9.
NEXTBITd ec reme ntsourc ount e
ri nt heX- Re gi
ster,a ndi fXha sn’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 layl oopi sini she d,theY- Re gisterisi nc r
eme nt e
di npr e para-
tionforreadingt hene xtby tei nt heda tat able .
Asi tha ppe ns ,re adinge achbi tof2 56by tesdoe snott aket ha tlong .Our
soundwoul dbeov err athe rsooni fwedi dn’ tdoj ustonee xtr
as tep.Al thoug hwe
couldg eneratea ndr e adl arge rta bles,a not he ra ppr oac hi stor ereadt het ablea
setnumbe roft ime s.hi si swhe ret heDRTNv aluei sus e d,a ndt het abler ea dis
repeatedthenumbe roft i
me sspe ciie dbyDRTN.
hema ina reaofe xpe rime nta ti
oni nt hi sr out i
nei swi t
hdi fe r
entv a lue sfor
PTCH. Sma l
lerv a lue spr oduc ehi ghe r-soundi ngnoi sepa tterns;largerv alue sg en-
eratemor eofar oa r.

ALi
tt
leMor
eSophi
sti
cat
ion
hisla
strout
ineproba
blysoundsmoreli
kea nexplosi
ont oyouthanthe
irstonedi
d.hisisduetot
hehighernoi
sec
ontentoft
hes ounda sc
omparedto
themor emusi
calirs
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
ttoini
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
iaEfe
l cts 2
41

0335: 39 48 CHK

hispr
ogr
ami
sde
sig
nedt
obec
all
edf
rom a
nAppl
esoftpr
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

hema indiferencebetweenthisrout
ineandthepr e
viousonei st
hatjust
pri
ortothedelayloop,theX- Regi
ste
risloadedwiththecurrentDRTNcounter
val
ue,asoppos
edt oaus e
r-deinedpitchval
ue.hus ,noPTCHisspeciie
dint he
cal
li
ngprograma ndy ouma ys el
ectonlyastar
ti
ngpointont heramp,a si
ti s
someti
mescall
ed.
Enteri
ngav alueof2 55re s
ult
sint helonge
stsoundpos s
ible
.Itisrat
he r
int
ere
sti
ngtoha veyourAppl esoundlikea7 27rea
dyt ota
k eofthroughyour
cei
li
ng.

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

]
2
42 As
sembl
yLi
nes

1000: 4C 63 10 25 ENTRY JMP START


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

109F: A2 01 69 LDX #$01


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

Whe nt heprogrami srun,thehi-resscreens houldclea


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

]
2
44 As
sembl
yLi
nes

SHIPd rawst hes pa ce s


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

TheShoot
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
yyourdeinit
ivehi
-re
sa rca
degame,thefoll
owingisofere
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
4−9
8we
rec
hang
edt
odi
vi
det
hepa
ddl
eva
lueby4t
oconv
ertt
her
ota
tion
i
ntot
hea
ll
owe
dra
ng −6
eof0 3
.

]
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
iaEfe
l cts 2
47

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


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

]
2
48 As
sembl
yLi
nes

1123: A5 E2 145 LDA Y


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

hi si sa ni ndependentprogramt hatcanbec all


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

]
2
50 As
sembl
yLi
nes

1
8.Goba
ckt
ost
ep1
5te
nti
mesf
ora
nex
cit
ing(
?)e
xpl
osi
on.
19.Re stor
eSCALEa ndthee xplos i
onc ounte r.
Goba cktos t
e p4 .
Becauset hispr ogrami sma deupoft hev a r
iousr outi
ne sus ede arl
ier
,t hi
s
summa r
yshoul dbes uicientt oe xpl
a i
nt heov e
r a
llme t
hodofope ration.
heus eoft hes ingle
-dots ha pet ocreatether ayissimi l
a rtot het echnique
usedinc hapter2 4’sScannerpr og rams .hene wt hi
ng spres entedinS hootera r
e
theincreme ntingbyt hree(line s1 3
2 −134)t oc r
eateaf asterir inga ppearance
andtheus eofHFINDi fani mpa ctisde te
c t
ed.
Reme mbe rt hattheHFIND( $F5CB) routi
nei nAppl esoftisus e
da fte
rdr awing
anys hapetoindoutwhe rewe ’vebe enleft.Weus edHFINDi nthi spr ogramt o
deter
mi newhe retheimpa ctoc curred.
Alsonot etha tDRAWr at
he rtha nXDRAWi suse dinthispr og ra
mt oe nsurethat
porti
onsoft hewa llaredestroy edbyt hei mpa ct.Inc ontras ttot hee xplosion
program,thispr og ramc annotber unona nyhi -resscreenba ckgroundwi thout
chang i
ngthec olor susedbyt her aya ndthee xplosi
onr outines .
2
6
2
6. Pas
si
ngFl
oat
ing
-Po
intDat
a
No
vemb
er1
982

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


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

1[
CT]Re
cal
lfr
om c
hapt
er1
0tha
tthemi
nimum t
echni
cal
l
ys dbe−3
houl 276
8.Howe
ver
,
Appl
esofta
ndI
nte
gerBAS
ICr
est
ri
ctt
hemi
nimum i
nte
ge o−3
rt 276
7.

]
2
52 As
sembl
yLi
nes

g
ram byusi
ngrout
inesdire
ctl
yfr
om a
sse
mbl
yla
ngua
ge(
t oiv
wot eti
mesf
ast
er
t
haninst
rai
ghtApple
soft)
.

I
nte
rna
liz
ati
onofDa
ta:
Int
ege
rve
rsusRe
alVa
ria
ble
s
heir stst
epinourinquir
yistoinve
sti
gat
ehowApple ofts
s tor
esnumeri
c
dat
aa ndtolookatt fe
hedi rence
sinhowintege
rvar
iabl
esandre a
lvari
abl
esar
e
st
ored.
S t
artbyini
ti
al
izingy
ourApple’
sme morywit
hanFPsta
teme nt.henente
r:
A% = 10: A = 10

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

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

Youma yr e
callf
rom chapter16thatthesefourmemorylocati
ons($67,$68
and$AF,$B0)a reusedtostorethebeginningandtheendofthec urr
entAppl e
-
softprog
ram. Wec anseefr
om thedi s
playthatt
heprogramresi
desfrom $801to
$803.Av eryshortprogra
m,i ndeed,butthat’
sunders
tanda
blesinceweha ven’
t
entere
da nyprogramlines
.
Nowl et
’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

hi
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
lfrom ourdi
scus
sionsinpr
e v
iousc
hapt
e r
stha
tint
ege
randrea
l
v
ari
able
sarest
oredinthefol
lowi
ngf or
mat:
I
nte
g e
r: C1 80 00 0A 00 00 00
“A” “” “0” “10” – – –
Name Name Hi
gh Low Unus
ed Unus
ed Unus
ed
char
1 char
2 By
te Byt
e
(
bit7se
t)(
bit7se
t)

Re
al: 41 00 84 20 00 00 00

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

Startinga t$803,weindt hev a


riableA%s t
or edf rom $803t o$809. heir s
t
twoby te sa r et hena mec ha racters.Twoby tesa rea lwa ysus e d.I ft hev ar i
able
namei sonl yonec ha racte rt he nanul l( $00f orr ealor$80f ori nte ger )iss tored
inthes econdpos ition. Not et ha tinteger,rea l,ands t
ringv ariablena me sa redi f
-
fer
e ntiatedb yt hec ombi na ti
onofhi ghbi ts etti
ng sint het wona me -c harac t
er
bytes .Sinc eonl ybi ts0t hr oug h6a r
eus edf ort hec ha racter( AS CI Iisonl yas ev-
en-bi tcode ) ,bi t7( thehi gh- or derbi t
)isa vailabl efore ncodi ngt hev aria blet ype.
Intege rv ariable sa l
wa y sha vebot hhi g hbi tsset.Re alv a r
iabl esa lwa ysha ve
bothhi ghbi t sc le
a r.S tringv a ri
a ble
sa lwa ysha vet heir stna mec har ac t
e rc l
ear
andt hes ec ondc ha racters e t
.( henot ationf ors t
ringv ariabl ena me st ot he
oppos i
tee fe ctonpa ge1 37oft heAp p le
so ftIIBAS ICPr ogra mmi ngRe ference
Ma nua lisi ne r rori nt hisr e gard.)
hene xtt woby tes
, $00a nd$0A,a ret hehi gh-a ndl ow- or derby te sfort he
value1 0.Youha vepr oba blynot icedtha tint eg erv ari
abl esa res tore di nav ery
si
mpl ewa y ,wi tht hev a luebe i
ngbr oke ndowni ntot hel ow-a ndhi gh- order
bytes .Aboutt heonl ype c uliari temi sthef ac ttha tthet woby t
esa res t or edhi gh-
orde rby teir st, whi chi sba ck wa rdfrom t hewa ywenor ma llyse ethe m pa iredi n
mos ta ssembl y-lang ua gec ode .
het hr e er ema iningby tesa reunus ed.
$80At o$810i swhe r et her e alvari
a bleAi ss t
or e
d. Youc ans eet ha ttheir s
t
twoby tesa ga ina ret hena mec ha rac
ters,thi st imewi tht hehi g hbi tsc lear.he
rema i
ningby te sma k eupt hev aluefort hev a riableA.
Itshoul dbeobv ioust ha ta lt
houg ht hev alue soft hei nt ege ra ndr ealv ari
-
ablesa res tor eda se qua l,t hema nne rinwhi cht he ya res toredi snot .her ea
l
variableha sbe ene nc ode di nt oaiv e-by tes eque nc e,thel og i
cbe hi ndwhi chis
notr eadilya ppa r
e nt.We ll,don’ tdespa ir
;i ti snota ctua ll
yne c e
ss aryf orust o
unde r s
tandt hee xactde ta ilsoft hec onv ersi
onr out ine.

]
2
54 As
sembl
yLi
nes

Ing eneral,itwills uic et os a yt hata ne xpone ntialnotat


ioni sus edt ostore
thenumbe r.hi si shownumbe rsofs uc hlargema gni t
udes(±1 03
8
)a reaccom-
moda tedbyAppl e oft.I
s fy our ous es omeofy ourmor eanc ienthi gh-school
me mor i
es,you’ l
lre callthatt heba si
ci de atoe xpone ntsisthata nynumbe rc an
bee xpr ess
edwi t
ht wonumbe rs ,thee x pone ntandt hema nti
ssa.
Fore xampl e,thenumbe r1 0i se qua lto1 01,henumbe 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 hebasici deai sthere.In
fact,5 0isre all
ye qua lt o1 01.69897 (ort hereabout s.he1pa
) r
toft henumbe ris
call
e dthee xp onent( oroc ca siona ll
yt heor derofma gnitude)oft henumb er
. he
69 897i scal
le dthema ntis
sa. Youma yha vefondme mor i
esofs pendi ngpleasant
hour si nma thc la
sse slook ingt hroug hbook swi thl ookupta blest oindt hese
value sforgivennumbe rs.
Ina nye vent,it’
spr eci
s elyt hist ypeoft echniquet hatisus edt oe ncodet he
value sofr e
a lvariables.3For tuna telyf orus ,itwi l
lnotbene cessaryt oc rea
teour
ownr outinest oha ndlenumbe rsi nthi sforma t;awe althofsuchr outinesalr
e ady
existinAppl esoft.
her ema inde roft hi
sc ha pterwi llconc entrateons omebr ie
fe xercis
esi n
passingloa t
ing -pointnumbe r sba c ka ndf orthbe twe enApple softa nda ss
embl y
langua ge.he 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-la
ng uag eprogram ha spos s
ession
oftheda ta.

Th eFl
oat
ing
-Poi
ntAc
cumul
ator(
FAC)
Appleoftha
s sitsowni nt er
na lsetofregistersthatitusesdur i
ngi t
sv arious
calc
ulat
ions.hemos timpor tantoft hesebyf aristheloa ti
ng-pointAc cumul a-
tor.hisisusual
lylabele
dFACi ns ourceli
sti
ng sthata c
cessthi
sr egi
st
e r.
hewor dregi
sterisus edi nas li
ghtl
ydi fe rentwa yheret hani tiswhe n
refe
rri
ngt o6 5
02r egis
terss ucha st heAc cumul atorort heX-orY- Reg i
sters
.
Becausealoa ti
ng-pointnumbe ri srepresentedbyas eri
esofby tes
,t heFAC
occupie
stheby t
esfrom $9Dt hroug h$A2.
Youma ybepuz zl
eda stowhyt heFACus ess i
xby teswhenv ariablestorage
usesonlyive.hi si sbecaus etheFAC us es$A2a sthesignby tetoindi c
a t
et he
posit
iveorne gati
ves tat
usoft hev a
lue.Whe n ina 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 heiv e -
by t
eforma tares ai
dt obe“ packed.”hes i
x-
3[
CT]Fr
om t
heAp
pleI
ITe
chni
calNo
tes
:fort
hee
xpone
nt,
thet
opbi
tist
hes
ign(
wit
h0
f
ornegat
ive.her
) emainderoftheby t
emi nusoneisthev a
lueofthee x
ponent(f
or
e
xample,$84isaposit
iveexponentof3 ).hema nt
iss
ai sabinar
yf ra
cti
on,withan
i
mpli
edstart
ingval
ueof1.heir stbi
tisthesi
gnbit(
thisti
mewi t
h0f orposi
ti
ve)
.he
r
emaini
ngbitsaref
rac
ti
onalvalue
ss t
art
ingwith0.
5,0.2
5 ,0.
125,e
tc.Forexample
,$20
g
ive
sama nti
ssaof1+0.25.
So$84$20e qual
s1.
25×2³=1 0.
2
6.Pa
ssi
ngFl
oat
ing
-Poi
ntDa
ta 2
55

byteformatis“unpack
ed.”heunpa c
kedf ormatisfas
terforca
lculat
ions.he
packedformatisusedt
omi ni
mi zestor
agespa ce
.
Ingener
al,wheneve
ranyt ypeofcalcul
a t
ionisdonebyApplesofttheFACis
theprimaryregi
ste
rusedtoholdt heres
ult.As e
condregi
ster,
ARG(ARGume nt)
,
isusedfortwo- v
aluecal
cula
tions,sucha s1 5×1
. 7.heARGr egi
sterusesthe
bytes$A5through$AA.Forthet i
mebe ing,though,wene e
donlyc oncernour-
sel
veswithFAC.

Pa
ssi
ngDa
taf
rom Appl
esott
otheFAC
heir stareatoi
nv e
stigat
ei show tog etaloa t
ing-
pointnumbe rpa s
sed
fr
om Applesoftt
oa na
ssembl y-
languagerouti
ne .hee as
ies
twa yisbyme ansof
theUSR functon.heUSR i
i sar atherne g
lecte
dpa rtofAppl e
soft,pr
oba bl
y
beca
useofthel a
ckofdocume ntati
ononi tsnatureandappli
cat
ions.Aprog r
a m
st
ate
me ntusingUSRmightlooksome thi
ngl i
kethis:
10 X = USR(Y)

Whent hi
sst
ate
menti
sexecut
ed,t
hreet
hing
shappen:
1. hee xpre
ssi
onorva
ria
blewit
hinthepa
rent
hes
esisev
alua
teda
ndt
her
esul
t
puti
ntheFAC.
2. Ac allt
olocat
ion$0A(decima l
1 0
)isdone.hi sisequival
entt
oaCALL10i n
Apple
soft.he r
eisat hre
e-byt
ej umpi ns
truct
iona tloca
tion$0A.Itisass
ume d
tha
ttheus e
rhasinsert
edthelocati
onofa nexist
inga ss
embly-l
anguageroutine
.
Forexa
mpl e,t
hecodeJMP$300mi ghtbefoundat$0A. hepr ogram wouldthen
j
umpt o$300toe x
e c
utewha t
everrouti
netheusermi ghthaveputthere.
3
. Whe nt heus
errouti
neev
entual
lydoe sanRTS,t
hec
ont
ent
soft
heFACa
re
a
ssi
gnedtotheva
ria
bletot
hel
eftoft
hee quals
ign.
Fore
x a
mple,t
ypeinandr
unt hi
sprogram:
10 POKE 10,0
20 Y = 10
30 X = USR(Y)

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

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

]
2
56 As
sembl
yLi
nes

hi sist heda t
af ort hev al
ue10,whi chtheFACs t
or e
sinunpa ckedf orm.4

He re
’swha tha ppened: Line10s e
tlocati
on$0At oaBRK. Whe ntheUSRf uncti
on
wa scal
led,itputthes eque ncefor10int heFACandt hencall
ed$0Aa sex pect
ed.
Sincethiswa sabr e
ak ,wewe ntint
ot heMoni t
ora ndc ouldthenimme diat
ely
examinet heFAC.
Notet hatitisnotpos si
bletosettheFACfrom Appl eofta
s ndthent ov er
ify
thestat
usoft heFACbye nteri
ngtheMoni t
orwit
ht heusualCALL-151. S
inc ethe
FACwi l
lbeus edinc alculati
ngthev al
ueof-151,a nypriordatawouldbeov er-
wr i
tt
en.
Whi leyou’reintheMoni tor,l
et’
ssetup$0Af orournextexperi
me nt.Enter:
0A: 4C 00 03

hi
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
esofta
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 .hev 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 ,t
heFAC( 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 s
singda 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.Afte
rc 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

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

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

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

hisshouldv e
rifyt hattheFACwa sproperlyloadedwiththev al
ue1 05
.
I
nreviewingthel i
s ti
ng, you’llse ethatli
ne1 4call
sCHKCOM( $DEBE=CHe cK
forCOMma )t oadv anc eAppl esoft’
sTXTPTR( $B8,$B9=Te XTPoi nTeR)pas
t
thecommaf oll
owingt he7 68 .Li ne1 5thenc a
llsFRMNUM,whi cheval
uatest
he
vari
abl
eore xpress
ionf ollowingt hec ommaa ndput stheresultintheFAC.Li
ne
16thendoe stheBRKt ol ea
v eusi nt heMoni tor,fr
om whi chwec ancheckt
he
FACtoveri
fythatthec or r
ectv alueha sbeens t
or e
d.
Weha venow,t he n,twot echni quesforpa s
singdatafrom Appl eoftt
s ot
he
FAC.heir stistous et heUSRf unc ti
on( beings ure,ofcourse,
tos etuptheve
c-
torat$0A).hes econdi stous eFRMNUM( $DD67)t oe val
uatethee xpr
essi
onor
vari
abl
easpa rtofapa rame terlistfollowingaCALLs tate
me nt.

Mov
ingt
heFACt
oaMe
mor
yLoc
ati
on
Si
nc etheFACi ss
ohea v
il
yused,i
tiss
ometimeshel
pfultomov et
hedatai
n
i
ttoanotherlocati
onforlat
eruse.I
nAppleoft,t
s hisi
smos tofte
nat e
mporar
y
r
egi
sterora na ct
ualvar
iabl
e.Fornow,le
t’
sseeifwecanmov 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 *

hek eyt othist


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

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

hi
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
hec onve
rseofthisope rat
ioni saccompli
she
dinmuc ht hesamewa y.In
thi
scase
,theAppl e
softrouti
neMOVFM( $EAF9=MOVet oFACf rom Memory)is
use
d.Itrequi
resthattheY- Regi
stera ndAc c
umulat
orbeloadedwi t
hthehigh-
andlow-orde
rby te
soft hea ddres
st obeus edasthedat
as ourcefortheFAC.
(Notet
hatther
eisadifer e
nc ehere
: MOVMFu s
esXandY;MOVFMu se
sXa ndA!)
1 ********************************
2 * *
3 * AL26-MEMORY TO FAC *
4 * *
5 * SYNTAX: CALL 768 *
6 ********************************

CT]MOVMFir
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.hi sr
out
ineshowst
hatwec
anmoveda
taf
rom
asec
ti
onofme morybackint
ot heFAC.

Pa
ssi
ngFACDa
taBa
ckt
oAppl
esot
IftheFACdoe scontai
ntheres
ul tofanope r
ati
on,howc anwepa s
sitbackto
ac al
lingApplesoftprogram,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 indtheloca
tionofthedat
aby t
esof
ag i
venr ea
lv a
riableandthenmov et heFACintothem. hisha st
heefectofset
-
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

hi sr out inea g a
ina ss
ume stha ttheloa ti
ng -poi
ntda t afort henumbe r1 0
stil
le 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 etheloa ting-pointda t
afr om $380t hrough$384i ntot he
FAC.
Line2 0us esCHKCOMt oc heckthec ommaa ndmov eTXTPTRt otheir 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 c umul atorpoint ingtot heda t
aby tes
oft hes pe c
iie dv a r
iable.hi swillbepr ecisel
ywhe rewewa ntt heda t
aint heFAC
tobemov edt o. heonl ycorrecti
ont obema deisi nregardt othefactthatMOVMF
requi r
e stha ttheY-a ndX- Re gi
sters( r
athert hanYa ndt heAc cumul at
ora swa s
leftbyPTRGET)hol dt hedest i
nati
ona ddress.Line2 2solvest hisbyus i
ngt heTAX
comma nd,a twhi chpoi ntMOVMF i sc all
ed.We ’renow done ,a ndt heRTS wi l
l
returnt ot hec all
ingpr ogram.
Tes tthisr outinewi ththefollowi nglis
ting :
10 CALL 768,X
20 PRINT X

Xg
etss
ett
o10byha
vingourr
out
inet
rans
fe heloa
rt 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
ltestofthes
ecombinedt
echniques,l
et
’sseei
fwecansucc
ess
ful
ly
pas
sdataf
rom Appleoftt
s ot
heFACtoame mor ybl
ockandthenbac
ktotheFAC
andbacktoAppl e
soft.hefoll
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
esoftpr
ogr
amt
oca
llt
her
out
ine
:
10 Y = 10
20 CALL 768,Y,X
30 PRINT X

hev 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
.
h eUSRrouti
nea l
socouldhav ebeenusedandwouldeli
minatetwoofthe
s
ecti
ons:
1 ********************************
2 * *
3 * AL26-BASIC.FAC.MEM.FAC.BAS *
4 * VIA THE 'USR' *
5 * *
6 * SYNTAX: X = USR(Y) *
7 ********************************
8 *
9 * OBJ $300
10 ORG $300
11 *
12 CHKCOM EQU $DEBE
13 PTRGET EQU $DFE3
14 FRMNUM EQU $DD67
15 MOVFM EQU $EAF9

]
2
62 As
sembl
yLi
nes

16 MOVMF EQU $EB2B


17 *
0300: A0 03 18 ENTRY LDY #$03 ; HI BYTE
0302: A2 80 19 LDX #$80 ; LO BYTE
0304: 20 2B EB 20 JSR MOVMF ; FAC->MEMORY
21 *-------------------------------------
0307: A0 03 22 LDY #$03
0309: A9 80 23 LDA #$80
030B: 20 F9 EA 24 JSR MOVFM ; MEMORY->FAC
25 *-------------------------------------
030E: 60 26 DONE RTS

Noti
cethatsincetheUSR f unct
ionc al
lstheroutinewi t
ht heFAC al
rea
dy
loa
ded withthev al
uef orY,t heir 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.
hec 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
Bynow y ouprobablyfeelfa
irl
yc omfor
tabl
ewi t
hthei deaoft heloa
t-
i
ng-poi
ntAccumulat
or(FAC)andhowda t
acanbemov edaboutbe t
weenAppl
e-
ofta
s ndasse
mblylanguage.I
nt henextchapt
erwe ’
l
lbegi
nl ookingatsomeof
t
hemor esophis
ti
cat
edr outi
nesApplesoftus
estope r
for
mv ar
iousarit
hmet
ic
f
uncti
ons.
2
7
2
7.Fl
oat
ing
-Po
intMat
hRo
uti
nes
De
cemb
er1
982

Int hisc hapter,we’l


lc onti
nuewi thourdi scussionofloa ting-pointnumbe r
ope ra t
ions.I nt hepr evi
ousc hapterwel ookeda thowAppl esoftus estheloa t-
ing -pointAc cumul at
or( FAC)asthema inregi
s t
erf ormos tofitsnume ricope ra-
tions .Rout ineswe repr esente
dt hatde monstrat
e dhowda tac anbepa ssedba c
k
andf orthbe twe enar unningAppl eoftpr
s ograma nda na s
s e
mbl y-langua gesub-
rout inea nda l
sohownume ri
cda tac a
nbemov edi na ndoutofbl ockme mory
stor age.
Us ingt hisfounda t
ion, wecannowe xami nehowt ous eAppl esoft’
sr outines
fors uc hba sicma t
hf uncti
onsa sa ddit
ion,subtraction,mul t
iplica
tion,a nddi vi-
sion.
Awor dofa dviceisinor der,howe ver
,beforepr ocee ding.Yourir stinclina-
tionma ybet ot hi
nkt hatt heroutinesgivenhe rewi l
lena bley out odos imple
ma thope rationswi thgreaterspeedi nanAppl esoftpr ogr a
m. Asi tha ppe ns,this
wi llnotdi re ct
lypr oducet hespeedi ncreas
ey ouwa nt
.Re membe r,Appl esoftis
alrea dyus ingt hesev er
ys amer outines;gi
vent ha t
,nos pee dincreases houl dbe
ex pec t
edf ors uchsimpl eope r
ati
onsa sX=5×1 0.
Ourne ws yntaxwillbe :
CALL 768,5,10,X

anda ss uchi nvolvesjusta bouta smuc hov er


headi nt hec a l
li
ngoft her out i
ne
andt hepa ssi
ngofda t
aa swoul dbei nv ol
vedinAppl e
soft.
Youma ya skthe n,“ Whyus eana ssembly-languag ec allfort he s
eope r
a-
tions?”
he reareanumbe rofr e
asons ,twooft hemor eimpor tantofwhi chf ollow.
First,whe nde al
ingwi thpr ogramst hatrequireahi ghde g
r e
eofa ccuracy,
intege rda t
ama ynotbes uic i
ent .
Suppos e,fore x
a mpl e,youha veapr ogramtha tsimul atesthemot i
onofa n
obj e
c ttraveli
ngi nane lli
ptical(orothe rma t
hema ti
call
yc ompl ex)pa t
h.
Ifthec urrentpositionoft heobj ectisconti
nua ll
yma intai
ne dbyus ingi nt
e-
ge rcoor dinatesinther a ngeoft henor ma lscr
eenc oordi nates,errorswi llbe g
in
toc reepi nwi thsuccessiverecalculati
onsofpos i
tion.hi smi ghtbee videnc edby
theig uref ai
lingtor etracei t
self.Althoughi ntheoryt heobj ectshoul da lways
returnt oi tsstart
ingpoi ntwhe nf ol
lowinga nell
ipticalpa th,mul tipleroundi ng

]
2
64 As
sembl
yLi
nes

errorsma yc ausetheobj ectt


o“ miss”itsori
gina
lstar
ti
ngpointbyaf e
ws cr
een
units.
A mor er el
iablea pproac
hwoul dbet oma i
ntai
nt hecurr
e ntposi
ti
onina
trueloa t
ing-pointforma tandr oundthenumbe rtothene a
restinte
gerpri
orto
eachpl ot.
S e
c ond,c alcula
tionsde ali
ngwi thal argenumbe rofv aria
blesor,more
speciica l
ly,wi t
ha rraysofr ealvari
abl
e s,wil
lbeexecutedfas
terbya na s
sem-
bly-l
ang ua geroutine.
If,fore xampl e,
youwa nt
e dtomultiplyanenti
rearra
yby5 , i
twouldinfac
t
bef a
stertoe mployar outinethatuse
dt hissy
ntax:
CALL 768, A(0), 5

t
hant
ous
e:
FOR I = 1 TO 100: A(I) = A(I) * 5: NEXT I

he sei dea sa ndot he rsa reputt ous ei nanumbe rofc omme rc i


a lsoftware
pr oduc tsf ort heAppl e.Ma nypr og ramst hatr e qui r
ebot hs pe eda ndahi gh
de greeofa c curac yus eloa ting -pointr epre se
nt ationsofnumbe rsi na ssem-
bly -
lang ua ger out ine s.Anot herc ommont e chniquei st ous et heBCD( “Bina r
y
Code dDe cima l
”)f orma tfort heda ta.
he rea rea lsopr og rammi ngut ili
tiest hatpr ov i
dema chi ne -l
ang ua g erou-
tinest o be c alled di rectlyf rom Appl esoft.Ro ut ineMa c
hi ne ,Amp e r
Ma g
ic,
Amp e
r wa re,Ap p l
eS pice,a ndTheLi nk er(publ ishe dbyS out hwe sternDa taS ys-
tems , Ant hro- Dig it
a lS oftware ,Sc i
e ntiicS oftwa rePr oduc ts,Adv ent ureI nt e
rna-
tiona l
,a ndMi cr oLa b,r espec t
iv ely)a rea llde signe dt oa l
low t hepr og ramme r
ac cesst ous e f
ulr out ine swr ittene ntirelyinma chi nel ang uag e.Onepr oduc tin
pa rticular,Amp ersoftPr o gram Li b raryVo l.1( aRo utineMa chine -relatedpa c
k-
ag e),de alsalmos te ntirelywi tha rray-relatedr out ine stha tus et hea dv a ntagesof
thes ec ondpr i
nc ipleme ntione dt os pee dupa rray-r elat
e dpr og rams .
Ing ener a
l,a l
loft he sepr oduc tsa reba sedonma kingus eoft hea mpe rsand
ve ct
ort oc allspe ciali
ze dr outine s,thewa ywe ’vedi s cusse di npa stc ha pters.And
reg ardlessofwhi chpa c kagey oupr efer,it’
ss afet os ayt ha ttheov eralli deaofa
us er-selectablel ibra ryofpr e wr it
ten ma c
hi ne-lang ua ger outine se asilyc all
ed
from BAS ICi soneoft hemos tpowe rfula nde xc iti
ngi de ast oc omea longi n
Appl esoftpr og r
a mmi ngi nthel a stfewy ears. Inf act, virtua ll
ya l
loft her out i
nes
pr esent e
dov ert hel asty eara rec ompa ti
blewi thma nyoft he sea mpe rsandut i
li
ty
pa ckag es.
Asama tterofr e f
e rence,i ta l
s os houldbenot edt ha tt echni que sa reavail-
abl ef orf asternume ricope rationswi t
houtha vingt oc a llAppl esoftr outines.
he ser angef rom a rithme ti
cpr oce ssorboa rds,s uc ha st hos ema nuf acturedby
Ca li
for niaComput erS ystemsa ndot he rs,t
os oftwa res ubs y stemss uc ha sSpe e
d/
AS M( publishe dbyS ierraOn- Li ne).Ac ombi nationofbot hha rdwa rea nds oft-
2
7.Fl
oat
ing
-Poi
ntMa
thRout
ine
s 2
65

war eisals
oa vai
labl
efrom AppliedAna l
y t
ic
si nt hef orm ofMi cro
-Sp
eed,a
Forth-
lik
el a
ng uagecombi nedwit
ha na r
ithmeti
cpr oc
essorboa rd.ALFProd-
uctsofersan8 0 88proce
ssorcar
dt ha
tincl
ude ssoftwarethatspeedsupApple-
softmathfunctions.I
tals
oa l
lowsthecal
li
ngofde dicate
dma t
hf uncti
onsf
rom a
recurr
ingma chine-l
anguageprogr
am, i
ndependentofAppl esoft.

Mor
eAppl
esotI
nte
rna
ls
We l
l,t
hen,j
usthowdoe saper s
onus ethee xistingroutine
sinAppl eoft?As
s
withmos tthi
ngswe ’
vecovered,thei mportantthi ngtok nowi stheaddress
esof
theentrypointstotheAppl e
softr out
inesf ort heba s
icma t
hope rati
onst hat
int
erestus.Wea l
sone e
dtor eviv
et hediscuss i
onoft heARG( “ar
gume nt
”)reg i
s-
ter
,whi c
hweme nti
onedbr i
elyint hepreviousc ha pter.
heARGr egist
erisi
denticali
nf orma tt
ot heFACa ndisusedtoholdthes ec-
ondnumbe rinloating-
pointforma twhendoi ngt wo- va
luefuncti
onss ucha s
addit
ion,subtr
acti
on,mul ti
pli
cation,anddi vi
sion.heARGr egi
ste
rusesby tes
$A5through$AA.
Tos e
ehowARGi sused,considertheseimpor tantentrypointstoApplesoft
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)

Fort heirs
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. her esul
twoul dbelefti
ntheFAC.
Pr i
ortocal
linga nyofthesefourrouti
ne s
,theAc cumulat
ormus tbeloaded
witht heexponentva l
ueoftheFAC( FACEXP=$9D) .hi salsoser
vestocondit
ion
theze rolag.Fore xample,tomul t
ipl
yFACt i
me sARG,thefol
lowingcodecould
beuse d;
LDA $9D
JSR $E982

hes 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 ea
l
v
ari
abl
e )orinadatablocksetupbyt heprog ra
mme r.
Tous ethes
e,t heY- Regi
st
era ndAc cumul a
tormus tbesetupwiththe
a
ddres
soft hememor ylocat
ionholdingthenume ricdat
a(Y,A=highbyt
e,low

]
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
i ir
ne( s
tcol
umn)t
hencal
le
d.

AnEx
ampl
eTh a
tDoe
sn’
tWor
k
Youma ywonde rwhyas ampleli
st
ingthatdoesn’
tworkisincl
udedhere.
her 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 Appleoft,
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
hatrouti
ne
wi
llbecal
led.her esultoft
hecalc
ulat
ionwil
lbes
entbac
ktotheAppl
esoftpr
o-
gr
ami nt
ot hevar
iabl
eRSLT.
He r
e’sthel
is
ti
ngf ortheaddi
tionr
outi
ne:
1 ********************************
2 * *
3 * AL27-M.L. ADDITION SUBR 1 *
4 * (DOESN’T WORK) *
5 * *
6 * SYNTAX: CALL 768,X1,X2,RSLT *
7 * RSLT = X1 + X2 *
8 ********************************
9 *
10 * OBJ $300
11 ORG $300
12 *
13 CHKCOM EQU $DEBE
14 PTRGET EQU $DFE3
15 FRMNUM EQU $DD67
16 FACEXP EQU $9D
17 MOVMF EQU $EB2B
18 MOVAF EQU $EB63
19 FADDT EQU $E7C1
20 *
0300: 20 BE DE 21 ENTRY JSR CHKCOM
0303: 20 67 DD 22 X1 JSR FRMNUM ; FP -> FAC
0306: 20 63 EB 23 JSR MOVAF ; FAC -> ARG
24 *
0309: 20 BE DE 25 X2 JSR CHKCOM
030C: 20 67 DD 26 JSR FRMNUM ; FP -> FAC
27 *
030F: A5 9D 28 ADD LDA FACEXP
0311: 20 C1 E7 29 JSR FADDT ; X1 + X2
30 *
0314: 20 BE DE 31 RSLT JSR CHKCOM
2
7.Fl
oat
ing
-Poi
ntMa
thRout
ine
s 2
67

0317: 20 E3 DF 32 JSR PTRGET


031A: AA 33 TAX ; MOVE LO BYTE TO X
031B: 20 2B EB 34 JSR MOVMF ; FAC -> FP
35 *
031E: 60 36 RTS

Li
ne2
1be
ginst
her
out
inebyir
stt
aki
ngc
areoft
hec
ommaf
oll
owi
ngt
he
768 i
nt heCALL st
atement
.FRMNUM (
$DD67)ist henusedtoevalua
tetheir st
expre
ssi
on.FRMNUMconveni
ent
lyleavesther
e s
ultintheFAC.S
incewewillwa nt
theirs
targumentintheARGregi
ste
r,MOVAF($EB63=MOVet oARGfrom FAC)is
the nus edtomov etheda ta.
Li ne2 5ag ainc allsCHKCOMt o“ gobbl e”thene x tcomma ,afterwhi chFRMNUM
isa gainus edtoe valua tethene xtv aluea ndpl aceitint heFAC.
Wewoul dnowe x pectther esul ttobei nt heFAC.Li ne3 1t akesc ar
eoft he
thir dc omma ,afterwhi chPTRGET( $DFE3)i nds( orc reat
e s
)t hev a ri
ableinwhi ch
wewa ntt heresultre turne d.
Att hispoint,e ve rythingha sbe e npr operlypla cedfort heus eoft heFADDT
rout inet oa ddtheFACa ndARGr e gisterstogether.Line2 8l oa dstheAc cumula t
or
wi thFACEXP( $9D)a st hee ntryrequi reme ntfort hene xtins t
r uction,whi chisthe
ac tual executi
onoft heFADDTr outine .
h eTAXonl i
ne3 3i sus eda fterPTRGETt omov ethel ow- or derby t
eoft he
va riableda taaddr essi nt otheX- Re g i
s t
er,afterwhi chMOVMF( $EB2B=MOVet o
Me mor yfrom FAC)i sus edt ocompl e tetheda tatrans f
er.
Not e:Ifyou’ reunf ami l
iarwi tht hef unda me ntalmov er out ines,youma y
wi sht og oba ckt ot hepr eviousc ha pter,whi chc ov er
edt he ses uppor ti
ngr ou-
tine s.
heni cepa rtaboutt hisroutinei showe asi
lythes etupf ort hea dditi
onr ou-
tinewa saccompl ishe d.Wi thal i
ttlet houg ht,thoug h,youma yr e al
iz
et hisisto
bee x pected.Afte ra ll,thei nterna lr outi
ne swe rec r
eatedi nt heir stpl ac
et o
pr oce ssda t
aea s
ilywi t
hi na nAppl esoftpr og r
a m.

Whyi
tDoe
sn’
tWor
k
her outinefail
sbe c
auseofFRMNUM. Alt
houghitwa smentione dthatFRMNUM
le
avesitsresultinFAC,wha tyouwe ren’tle
tinonwa sthefactthatita l
sous e
s
ARGd ur
ingi t
sc alc
ulati
ons.hi sme a
nst hatwhenwec al
lFRMNUMas econdtime
online26 ,
wea reunknowinglydestr
oy i
ngt heval
uewes etupi nARGi nl i
nes22
and2 3.
hes olution,then,istosavetheFACc ontent
sf r
om their stv a
luec al
cula
-
ti
oni nme mor yataplaceotherthanARG.
he rearet woa l
ter
na t
ive
s.heir s
ti st
ous esomeofAppl esoft’
sownt em-
porarynume ricr egi
ste
rs,whicha r
ec all
ed,cle
verlyenough,TEMP1( $93−$97),
TEMP2($98−$9C) ,andTEMP3( $8A−$8E).heonl 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 emporarys torag
ea re
a.
Fort hisnexte
xamplewe’
lldojus
ttha
t,usingthel
asthalfofthei nputbufe r
,
$280−$284.
He re
’stherevi
sedli
sti
ng,ca
ll
ed us
ingt hesameAppl esoft 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.hisstor
estheda
taf heir
ort s
tval
uesaf
el
yin
memory.
2
7.Fl
oat
ing
-Poi
ntMa
thRout
ine
s 2
69

hewor d“s af
ely”i
sus edwi t
hc e
rtai
nc av eats.hei nputbufe risaus ef
ul
areainwhi cht os t
oretempor aryda t
a,buty ous houl dbea wareoft hek indsof
condit
ionst hatwi l
loverwriteda taplacedt he r
e .DOSc omma ndsa ndi nput
stat
eme ntsa ret hemos tlikelyt hreat
s.Al s
o,c omma ndse xecute
df rom t he
imme diatemodec anov e
rwr i
tethei nputbufe r.hi sisi nf actwhyweus ed
$280−$284f ort het emporaryregiste.hi
r sa llowsy outot r
yt heroutinef rom
theimme di
a t
emode ,si
ncey oua reunlikel
yt ous emor et han1 27chara ctersas
yourcomma ndl inewhe ntest
ingther outi
ne.
Onc etheda t
aiss t
oreds af
elyinme mory ,line2 8e valuat
est henex tv al
ue,
leavi
ngt heresultintheFAC.Att hispointweus ea notherroutine,CONUPK( $E9E3
=CONv ert(?)a ndUnPa c
K) ,tomov etheda taf rom $280−$284 ba ckt oARG.
Re membe r,thisisne ce
ssarybec a
us eFRMNUMonl ine28ma kesitimpos sibleto
storet
hev alueforX1i nARG.
AfterCONUPKput stheda t
aba ckinARG, FADDT( $E7C1) addsFACt oARG.

ALi
tt
leMor
eFi
nes
se
Inthechar
tshowingthevari
ousma throuti
neentr
ypoints,y
ou’l
lremem-
be
rthatther
ewa sasetofrout
inesthatall
owf ordea
li
ngwi thdat
ainme mory
di
rec
tly
. Wecanusethes
etocreat
eas l
ightl
ysma l
le
rvers
ionofthepre
viouspr
o-
gr
am whichwil
leli
minateourhavi
ngtol oadARGdire
ctl
ypriortocal
li
ngFADDT.
Here
’sthei
mprovedlis
ti
ng:
1 ********************************
2 * *
3 * AL27-M.L. ADDITION SUBR 3 *
4 * *
5 * SYNTAX: CALL 768,X1,X2,RSLT *
6 * RSLT = X1 + X2 *
7 ********************************
8 *
9 * OBJ $300
10 ORG $300
11 *
12 CHKCOM EQU $DEBE
13 PTRGET EQU $DFE3
14 FRMNUM EQU $DD67
15 FACEXP EQU $9D
16 MOVMF EQU $EB2B
17 CONUPK EQU $E9E3
18 FADD EQU $E7BE
19 *
0300: 20 BE DE 20 ENTRY JSR CHKCOM
0303: 20 67 DD 21 X1 JSR FRMNUM ; FP -> FAC
22 *
0306: A0 02 23 LDY #$02
0308: A2 80 24 LDX #$80 ; $280
030A: 20 2B EB 25 JSR MOVMF ; FAC -> MEMORY
26 *

]
2
70 As
sembl
yLi
nes

030D: 20 BE DE 27 X2 JSR CHKCOM


0310: 20 67 DD 28 JSR FRMNUM ; FP -> FAC
29 *
0313: A0 02 30 ADD LDY #$02
0315: A9 80 31 LDA #$80 ; $280
0317: 20 BE E7 32 JSR FADD ; X1 + X2
33 *
031A: 20 BE DE 34 RSLT JSR CHKCOM
031D: 20 E3 DF 35 JSR PTRGET
0320: AA 36 TAX ; MOVE LO BYTE TO X
0321: 20 2B EB 37 JSR MOVMF ; FAC -> FP
38 *
0324: 60 39 RTS

heonl ydifer
encebetweenthisrout
ineandthepre
viousoneisthatline30
nows et
supt heY- Regi
sterandAc cumulat
orforadirec
tc a
lltoFADD( $E7BE)
.
hi sent
rypoi ntautomati
call
ytransfe
rsthecont
entsof$280−$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
ne s1 8a nd3 2oft hei mprovedl i
sti
ngt ouseFSUB( $E7A7), FMULT( $E97F),and
FDIV( $EA66) willcreatetherout i
nest ope r
formt hec orres
pondi ngfunc ti
ons.
Asi tha ppe ns,the r
e’salsoav a
rie t
yofot hers implef unc t
ionst hatcanbe
perfor me 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-
soft.he rea reaf ew not abl
es ourcesf ors uchinf ormation.heir stisina n
art
ic l
ebyJ ohnCr os sl
eyofAppl eComput ercalled“ Appl esoftInternalEntry
Point s,”whi ch ha sbe enr eprintedi n anumbe rofpl a cesi ncl
udi ngApp l
e
Orc ha rd,Ca l
l-
A. P.
P. L.E.,
andCa ll
-A.P.P.L.
E. I
nDe pth#1.
he reisalsoabookc all
edWha t’sWhe reint heAp ple?b yWi l
li
a m F.Lueb-
bertt ha tli
stsma nyoft hee ntrypoi ntstonotonl yAppl esoftbuta l
sot othe
Moni tor,DOS , IntegerBAS IC, andmor e.

Conc
lus
ion
Weha veseenhowt heFACandARGr e
gis
ter
sa reuseda sthecentr
alpoi
ntsin
almostal
lofAppl eoft’
s snumeri
ccalc
ulati
ons.
Ina ddit
ion( nopuni nte
nded),
we
haveseenhow t heindivi
dualmathrouti
nesar ecal
ledt ope r
formt hedesi
red
funct
ions.
he s
ene wr outi
nesshoul
d bev eryusefulinc reatingy ourown loat-
i
ng -
pointutil
it
ie
s .Youma ywishtotrytocreatearoutinet operfor
m asimple
funct
ionona nent i
rearr
ayasanexer
cisei
nusingthesene wt ec
hniques.
2
8
2
8. heBCD,
orBi
nar
yCo
dedDe
cimal
J
anua
ry1
983

hi schapt e
r’sdiscuss i
onc enter
sonal it
tle-me ntionedope rat
ionalmodeof
t
he6 5
02mi croprocessork 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
ionsoft henumbe rsinv olved.Suchope ra
tionsoften
r
e quir
eac e r
tainde greeofme nt a
ltransla ti
ont oproduc eade ci
ma lequival
e nt.
I
nt e
rmsofpr i
ntinganumbe ri nAS CI If orm,e venmor ediicultyi stobe
expecte
di fyou’reus ingy ourownc onve rsi
onr outine sratherthanthebui lt-i
n
f
unc ti
onsofDOS ,Apple soft,andI ntegerBAS IC.
heBCDmodeg reatlys i
mpl iiesthispr oc essbys toringnumbe r
sinoneor
mor ebytereg i
ste
rs( ei
the rme mor y,X,Y, ortheAc cumul a
tor)inade cimal-ori-
entedma nner .I
tdoe sthisbyus ingt wof our-bi tgroupsi ne achbytetor e
present
adigitinba seten.I nt hiswa ytwodi gitspe rby tec anbes t
ored,thusg iv
inga
t
ot a
lvaluer a
ng eof0t o9 9 ,versus0t o25 5us ingbi na ry.
hi stabl
epr ov i
desa ne x
a mpl eofhowt 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.hisisag 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 cht hehe xnumbe rr e
allyc anbes aidt oe qualthe
decima lvalue( thati s,thede cima 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 hepi cture?”
Sof ar,allweha vehe rei sapos sibl
es y s
temf orstoringde c i
ma lnumbe rsv i
a
ourus ualhe xby tes.heg oodne wsi sthatt he6502a ctuallysuppor t
st hismode
inthea dditiona nds ubt r
actionope rati
ons.
ha t’
sr i
g ht.hes ecrett oma k i
ngi twor kistot e
llthe6 5 02t ha ty ouwi s
ht o
operateint hismode . hi sisdonebyme ansoft heinstructionSED, whi c
hstands
forSEtDe cima lmode .Onc et hisinstructionha sbee ne xecut ed,a llf utur
ea dd
ands ubtractope rationswi llbedonei 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 ertentse t
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 l
earthede cima llag(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
lla
g,i
nthe
Appl
eROM t
heRESETc
odedoe
sis
sueaCLD.
CT]heor
2[ 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.heBCD,
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
edifer
-
entv
alue stoveri
fyitsopera
tion.
Forbot haddit
iona ndsubtra
ction,res
ult
soft heoperat
ions“wrapgr
ound”
i
nama nne rsi
milartothewa yhexadecimalcal
cul
ationsdo.ha tist
osaytha
t99
+1willg ivearesul
tof0 0(1
00l e
sstheleadi
ng1 )and0−1wi llg
ive9.

Li
mit
ati
ons
Likeeverythinge lsei nl if
e,BCD ha sit
st rade ofsa ndf ai
li
ng s.heir s
t
involv
esthatv a
g uereferenc ema dee a r
liertoev er
y t
hi ngwor kingwith“ l
e g
a l
v al
-
ues.”“Wha t
’sleg a
l?”youma yask.You’ l
lnot ethatc er
tainhe xvalues,sucha s
$0A,n ev
era ppea .hi
r si sbe causei nt heBCD modes uchav al
uei s“ i
lle
g al

becauseitusesadi g
itoutoft her angeof0t o9.I fy oua tte
mptt ous es ucha
val
uei ntheBCDmode ,you’llgetina ccurateresults
.
Toa ddt ot hef un,not ea lsot hatt heBEQ,BNE a ndINC,DEC f amilie
sof
inst
ructi
onsdon’ twor ka sex pectede it
her. heN- la g(si
g n/negat
ivela g)andZ-
lag( z
erola g)area l
llinke dt obina r
yope rat
ionsa ndnott oBCD. hus0 1+9 9
willyiel
d0 0,butN a ndZ r ema inuna fe c
ted,s i
nc et he“ t
rue”bi naryr esul
t
shouldha v
ebe en$9A.Al so,nopr ovisioni sma def orne gativ
enumbe rs(signed
ari
thme t
ic)
.How, then, dowet estforspe cialc
ondi ti
ons ?

TheCa
rryFl
ag
h ecar
ryla gistheonlydirectindic
ati
onofa r
ithmeti
cresul
tsinBCD.In
addit
ionoperat
ions,thecarrywillbesetiftheresultexcee
ds9 9(
ov e
rlow).I
n
s
ubtract
ion,t
hec ar
rywi l
lbecle
arediftheresul
tislessthan0(undelow)
r .
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.

CT]heor
3[ 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
rlow (r
esul
t>9 9)or
unde
rlow(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

CT]he
4[ 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 low(
r or
underlow),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.heBCD,
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
ilypr i
nte
dto
thes cre e
nordi sk.Whe nus inghex a
de c
ima lma t
h,somes ortofhe x-to-ASCII
stringde cima lconver
sionrout i
neisrequire
d. hi sist
henfollowedbyt hepr i
nt-
ingoft hedig it
sv i
asomes tringprintrouti
ne .I
nBCD,onl yami nima lconver-
sioni sne e
de d,andthepr i
ntingisdonef ai
rl
ye as
ily.
hee asiestwa ytoprintanumbe ristous eoneoft heMoni t
orr outi
nes.
PRBYTE( $FDDA=P RintBYTE) ,f
ore x
ampl e
, pri
ntsthecontentsoftheAc cumu-
latora sahe xby te
.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
i fe
ngdi r
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.heBCD,
orBi
nar
yCode
dDe
cima
l 2
77

8001: 18 14 CLC
8002: A5 06 15 LDA MEM1
8004: 65 08 16 ADC MEM2
8006: AA 17 TAX ; STORE RSLT IN X
8007: A5 07 18 LDA MEM1+1
8009: 65 09 19 ADC MEM2+1 ; RSLT+1 IN ACC
800B: B0 05 20 BCS ERR ; OVERFLOW
800D: D8 21 CLD ; CLR FOR PRNTAX
800E: 20 41 F9 22 JSR PRNTAX
8011: 60 23 DONE RTS
8012: 4C DD FB 24 ERR JMP BEEP

Itisi mpor tanttonot i


cet hatine achr outinet heCLDi sus edtoc l
e a
rt hede c-
ima lmodeb eforec all
ingPRBYTE orPRNTAX.hi sisbe c auset heMoni torne eds
thenor ma lbi narymodet oc alcula t
es creena ddressesa ndpos it
ionspr ope rl
y.If
youc allt heMoni torwi tht heBCD modes et,strang ething swi l
lha ppe nwhe n
thet e
x tr eac hest hee ndoft hel i
neort hes creenne edst obes crolleda ndt he
Moni torr out inesa tt
e mptt oc alculatewhe retoputt hene x tlineofte xt.
Ify oudon’ twa ntt ous etheMoni t
orby tepr intr outine sor,f orwha tever
reason,j us twa ntt oc reat
et heAS CI Icha ractersy our sel
f,t hec onv ersionsa re
str
aightfor wa rda ndCOUT ( $FDED = Ch aracterOUTput –us ual
lypr onounc ed
“C-out ”)c anbeus eddi re
ctl
y .
heonl yr ea lobs tac
lei show Lette r ASCI IVa lue* BCDVa lue
toc onve rtt heBCD di git
st ot he i
r 0 $B0 $00
AS CIIe qui v ale
nt s.Asi tha ppe ns, 1 $B1 $01
thi
si se ve ne asi
e rtodot ha ny ou 2 $B2 $02
mig hta tir sts uppos e.Cons ider 3 $B3 $03
thetablea tther ight. 4 $B4 $04
From l ook inga tt hetabl e,we 5 $B5 $05
cans eet ha tt hel owe rdigitoft he 6 $B6 $06
AS CIIv aluec or respondst ot he 7 $B7 $07
8 $B8 $08
digitenc ode di nt heBCD f orma t
9 $B9 $09
and,c oinc ide ntall
ye noug h,t ot he
*highbi tset
numbe ri tselft o be pr i
nt ed.I f
therewa sawa yofa dding$B0t o
thev al
uef ort hedi g
itt obepr i
nt ed,we ’dha v ejustt hev aluewene ededt osend
toCOUTt opr i
ntt hea ppropr iatec har a
cter.
Toa dd$B0t ot heBCDv al
ue ss hownwoul dnor ma ll
yr e
quiret heus ua lCLC,
ADCi nstruc tions.he reisamor ee legant( thati s
,s hor t
e r)wa y,howe v er.You
ma yr eme mbe rt hatt heORA( logica lORwi thAc cumul a tor)canbeus eda sa
ma sktope rfor ma nov erl
ay-likeope rati
on.
He re’showapos s
ibleORAope rationwoul da ppe ar:
Accumulator: 0000 0110 ($06 BCD)
ORA #$B0: 1011 0000
Result: 1011 0110 ($B6 = ASCII "6”)

]
2
78 As
sembl
yLi
nes

Whati
ftheupperBCDdigi
tisi
nvol
ved?hepr oc
edur
ethe
nisir
stt
os ft
hi
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?
hea ns
werher sir
ei sttos ftt
hi heuppernibbledownt othelowernibbl
e,aswas
j
ustshown,andtoprinttheAS CIIc
harac
terarri
v e
da t
.he ntheorigina
lval
ueis
rel
oadedint
otheAc cumul at
orandtheuppe rnibblei
sma skedout.hi 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

h 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.heBCD,
orBi
nar
yCode
dDe
cima
l 2
79

duri
ngt hea c
tuala dditionors ubtracti
onope ra
tions.Althought heBCD mode
wouldha v enoha rmf ule fe c
tont heAND a ndORA ope rati
ons,COUTwo ul
dc er-
tai
nlytakeofe nsea tbe ingc all
edwhi letheBCDmodewa ss t
il
line fect.
Lines1 2throug h1 6g ettheorigina l
BCDv a
luefrom me mor ya ndthe nshift
itl
eftfourtime stomov et heuppe rni bbl
etot helowe rposit
ion.Att hispointthe
ORA#$B0i sdonet oc onv ertthevaluei ntheAc cumulatortot hepr ope rASCI I
val
ue ,atwhi c hpoi ntt heJSR COUT onl ine1 8pr int
st heir stdi g
it.Line1 9
ret
rievestheor ig
ina lvaluea g
ain,afterwhicht heAND#$0Fc le
arstheuppe rdigit
to0a ndtheAS CIIc onv ersioniscompl eteda ndpr i
ntedasbe f
ore.
her emainde roft her outinei
si denti
ca ltotheprevi
ouse xampl epr ogra
m.

Conc
lus
ion
heBi 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
entici
i nst
rume nt
ation.
Anumbe rofpointsshouldbek epti
nmi ndwhe nusi
ngt heBCDmode :
1. hemodes houl
dbes etonlyfora r
it
hme t
icproce
sse
st hatuseBCDv alues
,
suchasaddit
ionands ubtr
a c
ti
on.
2
. Onlyle
galv
alue
sarea l
lowed:0−9 f
orea
ch di
gi
t.Va
lue
sout
si
det
he
e
xpe
cte
dra
ngewi
llge
ner
atei
naccura
tere
sul
ts
.
. heBCDmodes
3 houldbecle
aredassoona sposs
ibl
ewhenar
ithmeticoper
-
a
tionsa
rec
omplet
edsoastoavoi
dposs
iblecompl i
cat
ionswi
thot
hersoftwar
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. heNa ndZ-lagsar
eunrel
ia
bleasame ansofde
tec
ti
ngthere
sul
tsofc
om-
pa
ris
onsorofinc
rement/
decr
ementopera
tions.Onl
ytheca
r yla
r gshoul
dbe
us
edtodet
ectt
heres
ult
sofsuc
hopera
tions
.
. hec
6 a
rrylagwi
llbese
tforre
sul
tsg
rea
tert
han9
9(ov
erlow)a
ndc
lea
red
f
orr
esul
tsl
esst
han0(unde
rlow).
7
. BCDope
rat
ionsdo“
wra
par ”ha
ound. tis
,99+0
1=0
0a 0−0
nd0 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
hesearesomeg ener
a lr
ulest
ohelpinprogra
msusi
ngtheBCD modeof
t
he650
2.he reisonlyonenotabl
eexc
epti
onthatmayonoc
casi
onpr
oveuse
ful
.
hetes
tfor0(BNE,BEQ)canbeusedwhencounti
ngdowni
ntheBCDmode.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
lengeforyoutousetheinformationgi
venin
thi
schapte
randi nprevi
ouschapt
erstotrytowri
tearoutinethatwouldaddtwo
Appleofts
s t
ringstoget
herusi
ngtheBCD modea ndreturnther es
ultinathir
d
str
ing.hiswouldpr ovi
deawa yofextendi
ngthenorma lpre
c i
si
onofAppl e
soft
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 cat
ions
withtheoutsi
dewor ld.he i
rde s
ignisals
ooneoft hemoreint
eres
ti
nga s
pectsof
assembly-
languageprogramming .We ’
ll
spendthischa
pterandthenextle
arning
howt oint
ercepttheI/Ov ec
torsoftheAppleandimplementourownr outi
nes.
Itwil
lma k
et hene xtfew demonstrat
ionsmuc heasi
erifyoudi s
conne c
t
DOSf r
om theI/Os yst
em. ha t’
smos teas
il
ydonebyr unningthi
sshortApple-
softBASICprog ra
m:
10 IN#0: PR#0: END

ha
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.heg eneralproce-
durewast oloadt heAc cumulatorwitht heAS CIIvalueforthec hara
cteryou
wantedtopr i
ntandt hentodoaJSRCOUT.
Tos eewha tha ppensa t$FDED whe nyoudot his,ente
rt heMoni torby
meansoftheus ualCALL-151. he ntypein:FDEDL<RETURN>.
heir stinst
ructi
onl i
st
eds houldbeaJMP($0036). hi 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

hi stel
lsyout hatthejumpwi l
lbema deto$FDF0,whi chi nt hiscasehap-
penstobethene x
ti nstr
ucti
ona fte
rtheJMP($0036).$FDF0i scalledCOUT1a 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
teroutputS Wit
ch)isthena meg i
v e
nt otheby t
epa i
r$36,
$37.Apointersucha sthi
sisusua l
lycal
ledav ec
tor,i
nt hati
tdir ectsthelowof

]
2
82 As
sembl
yLi
nes

programc ont r
oltowha te
verroutine( thatis,wha teveraddressinme mory)is
appropriat
ea tthemome nt.
hec ha ngingoft heCSWv ectori swha tha ppenswhe nyoue xec
uteaPR#n
comma nd.CSWi spoint
e dtothea ddre s
sCn00,whe reni stheslotnumbergiven
inthePR#ns tatement.Ifnodeviceispr ese
nti nt heslot
,thennopr ogram wi
llbe
founda t$Cn00.hi sexplai
nswhyaBAS ICpr og ra
m ha ng swhena nimproper
PR#c omma ndi sgiven:thecomput eriswa i
tingf ortheina lRTSfrom anonexis
-
tentrouti
ne .Tov e
rif
yfory ours
elftha tt
heloc kupdoe s
n’toccurunti
lacharac
ter
isoutput,runt hisprogrami nAppl esoftBAS IC:
10 HOME
20 PR#5: REM OR SOME OTHER EMPTY SLOT
30 FOR I = 1 TO 20
40 POKE 1024 + I, 192 + I
50 NEXT I
60 PRINT "YOU WON’T SEE THIS”

Whe ny our unt hispr ogram, yous houl dseethelette


rsAt hr oug hTpr inte d
ont hes creen,butt hephr aseonl i
ne6 0s houldnota ppe a.hi
r ng sha ppent his
waybe causet hel ooponl ines2 0t hroug h4 0put stheda tadi rec t
lyi ntot he
scr
e enme mor ywi thoutg oingt hroug hCOUT. Re membe rthata l
lt hist i
meCSWi s
pointingt o$C500. It’
sonl ywhe nt heYc ha ract
erg et
ssenttoCOUTt hatthec om-
puterha ng s
.
IfDOSwe reins tal
leda ndl ine2 0saidPRINTCHR$(4);"PR#5", thepr og ram
woul dha ngont ha tstateme ntbe causeoft hec arri
ager e
turns enta tt hee ndof
thepr i
nts tat
eme nt. I
t’sinstructivetonot ethatthec ar
riagereturni snota ctually
neededf ort hePR#t owor k.Addi ngas e micolontot hepr i
nts tateme ntwoul d
res
toret hepr ogramt oi tsoriginalsemi -f
unc ti
onal s
tat
e.
Onewoul dthinkf rom t hepr ecedingt houghtsthathook i
ngupar outinet o
theout puthook swoul dbef a
irlys i
mpl e.hepr oblemi sthatmos toft het ime
you’llwa nttoha veDOSa c t
ive,andDOSha sbeenc l
everl
yde si
g nedt odoe v ery-
thi
ngpos si
bletok e
epi t
selfc onne c
ted.Whe nDOSi sinst
a l
led, CSW a ctually
pointst o$9EBD,apor ti
onofDOS ,andi t’sverydiic ulttoge titt opoi nte l
se-
whe re.
Spe ciical
ly,whe nev e
re i
the rinputorout putisdone ,bot hv ectorsa re
checke dtoma kesur eDOSi sstil
lhook edup. hi smea nsthat
, eve nt houghy ou
couldt empor aril
yc ha ngeCSW,a nyi nput -t
ypea cti
on woul dc a
us eDOSt o
res
torei tsel
ft otheout putlow.He re’sapr ogramt os howt his.You’ l
lne edt o
reconne ctDOS( pre s
singRESETwi l
ldot hat)tot r
yit:
10 HOME
20 PR#0
30 PRINT CHR$(4);"CATALOG”
40 INPUT "TURN THINGS BACK ON"; I$
50 PRINT CHR$(4);“CATALOG"
2
9.I
nte
rce
pti
ngOut
put 2
83

het heoryhe reisthatthePR#0 s etsCSWtopoi ntdire


c t
lyt o$FD0Cr at
her
thantoDOS .hi siswhyt heCATALOGdoe sn’
twor ki nli
ne3 0.Howe ver
,whe n
theinputisdone ,DOSi ssti
llhook edupt othei nputvect
or.Re a
li
z i
ngthatthe
outputconnec t
ionha sbee
nl ost,DOSt husreconnectsit
sel
f.Li ne4 0thenper-
for
msa sexpec t
ed.
Ingeneral,
DOSc anbedi s
c onne c
tedbye x
e cuti
ngbotha nIN#0a ndaPR#0
withinaBAS ICpr ogram,providedt hatoneisdonei mme di
a te
lya fte
rtheother
withnoi nputorout putinbe twee n.heone -
lineBAS I
Cpr ogram us e
da tthe
beginni
ngoft hischapte
rtodisconne ctDOSe mpl oysthi
sprinciple.
Pres
singRESET wi l
lhookt hingsba ckupa nyt
imey ouwa nt.Not i
cethat
thesear
enotdonea sDOSc omma ndss uchas:
10 PRINT CHR$(4);“IN#0": PRINT CHR$(4);“PR#0”

AnIN#0orPR#0a sadire
c tBASICcomma ndredi
rec
tsI/
Ot ot
heMonit
or.
hesa
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.hi 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
thegeneralowoft
l hi
ngs:
1. Wi ththeappropri
atevalueintheAccumulator,aJSRCOUT($FDED) isdone .
2
. At$FDED isaJMP t
otheaddresss
pec
iie
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, hi
$AA54. snormal
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 ?hi s
bas
ical
lydepe
ndsonwhe t
heras l
otisus
ed.Ifyouhappenedt obewr i
ting
ir
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 talocati
onotherthanthe$C000space,
anothera pproachi snee
ded.
hepr ocedurea ct
ual
lyisfairl
ys i
mple.Allyoune e
dt odoi ssetCSW t
o
whe r
ey ouwa nttheoutputtobeeventual
lysentandt
hencal 1
l$3EA.
Fore xa mple,let
’sputatrivi
alrouti
neat$300thatme re
lyjumpstoCOUT1
($FDF0).Goi nt
ot heMonitorandenter:
300: 4C F0 FD

I
fyoul
is
tthi
srout
iney
ous
houl
dge
t:
300L

0300- 4C F0 FD JMP $FDF0


0303- 00 BRK
0304- 00 BRK

Tohooki
tup,
typei
nthef
oll
owi
ngf
rom t
hei
mme
dia
temodeofAppl
esoft
POKE 54,0: POKE 55,3: CALL 1002

hissetsCSWt
opoi
ntto$300andthencal
ls$3EA.hes
amet
hingc
anbe
donef
rom wit
hina
nas
sembl
y-l
angua
geprogr
am wit
h:
LDA #$00
STA $36
LDA #$03
STA $37
JSR $3EA
RTS

Oncec onnect
edint hisway,ev
eryt
hingwills
til
llookthesameonthe
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

heMoni t
or,DOS ,a
ndBAS ICa l
lsendout
putviat
hejumpa tCOUT.hi s
s
ti
llpoint
st oDOS ,butnow DOSpoi ntsnott
oCOUT1 (
$FDF0)
,butt o$300.
here,ourrout
inedoesaj
umpt oCOUT1tocompl
et helow.
et

CT]het
1[ echni
quewoul fe
dbedi r
entf
orPr
oDOS
,whi
chdoe
sn’
tha
veahook
upr
ou-
t
ine
.Ins
tea
d,youcanmanual
lyc
hanget
heoutputvec
torat$BE30,$BE31topoi
ntt
o
y
ourout
putrout
ine
.Se
echapt
ers
eve
nofIns
idetheAp
ple//
e,byGaryB.Lit
tl
e.
2
9.I
nte
rce
pti
ngOut
put 2
85

Tov erifyt hatc ha ractersa reg oingt hr oug h$300,j ustt ypei nPOKE768,0.
Or ,from t heMoni tor ,type :300:0.
hec omput erwi lli mme di at
e lyha nga spr og ram low hi t
st he00 ( BRK
instruc t
ion)a t$300.heBRKr out inei 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 titselfinde ini tely.
Ani ntere st
ingpoi nthe rei st ha twhe nCOUTi st urne dof( f
ori nstance ,a
simpl eRTSa t$300wi l
ldot het r i
ck ),not hinga ppea rsont hes creende spitet he
facttha tt hec omput e riss til
lful lyf unc t
iona l.Ev ent houg hy ouc an’ts eewha t
you’ret ypi ng, youc oul dt ypei nCATALOGa ndt hedi skdr ivewoul dc omeon. he
la s
hi ngc ur sorwoul dr ema inont hes c
ree ns inceRDKEY( partoft hei nputr ou-
tinea t$FD1B)a ddr esse sthes cree ndi re c
tlyfort hec ur sor.
Toe xpe rime ntwi thCOUTs omemor e,let’
st ryar out i
net ha t’
sal it
tlemor e
interesting. Cont r
ol c ha r
a ctersar enor ma l
ly“ inv i
sible”i nthatt he y
’renots entt o
thes c reenbyCOUT1.I fwec ouldde tectthec ontr olc hara c
terbe forei tg ott o
COUT1a ndc oul dc ha ng eitt oadi fe r entva l
ue ,wec oul dha veitdi splaya sinv 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.ha ti s
,a l
l
va l
uesa r eg reatert ha n$80. Inv ersea ndla shingc ha rac t
ersa rec reatedbys e
nd-
ingc ha racterswi t
hav aluel esst han$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 rela shi ng. I
ng en-
eralwha tt hisme ansi st hat,ift hehi ghbi ti sc l
ea red,c ontrolc ha ra cte
r swi l
l
comeouti ni nv ersea nd“ standa rd”c ha ra
c tersinla shing .
hi si s,inf act,howt heFLASHa ndINVERSEc omma ndsofAppl esoftwor k.
her outinea tCOUT1i nclude sapor tiont 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 all
ed
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 si
redr e s
ults.
hef ollowi ngdi ag rami llustrate stheINVFLGma sk’sefe ctonout g oingc 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

hi 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 ontrolcha ra
cter,
though,at estisdonet os eeifi t’
sac a r
riagereturn.I fso,thattooispa sse
dt o
COUT1. Aftera 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
ercptCOUT,il
e t
ert hechara
ctersgoi
ngt hr
ough,
ande v
ent uall
yr et
urnc ontr
ol t
otheMonitorscre
e nrouti
neCOUT1.Ifweha dour
ownout putde vice,t
hiswoul dnotbenece
ssary.hepoi nthereist
ode monstra
te
thepossibili
tyofa l
ternateoutputdevi
ces
.Ul t
imatelythi
scouldincludeprint
er
cards
,termi na l
s,anal
ogde vi
cessuchasmot ors,andmor e.S uc
hpr ojec
tsare
rathe
rinv olved,howev er
,sofornowl e
t’
sjustse
ei fwec a
nwr it
eourownpr imi-
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,youcandire
ctl
ymodifyt
heoutputv
ect
orat$BE30,$BE31byr
unni
ng
t
hefol
lowingApple
softpr
ogr
am( donotr
unthi
sasadir
ectc
omma nd):
10 POKE 48688,0: POKE 48689,3
2
9.I
nte
rce
pti
ngOut
put 2
87

heba si
cmode l willbetos e
ta si
deonelineoft hes cree
na sourdispla
ywi n-
dowa ndt oa t
temptt oc ontrolte
xtout putwithi
nt ha twindow. Toa voidha vi
ng
toc r
ea t
ev erti
calscroll
ingr outi
nesa ndc ur
sorma nageme ntr outi
nes,we ’
lllimit
alloutputt othesingleli
nea nds croll
textonlytothel eftaseachne wc haracteris
displ
a yedont her i
ght.
Ifthi ssoundss uspiciouslysimilartoac a
lculatordi s
pla y,you’
r eri
g ht.It
shouldbee asynowt ose ewhy ,withlimite
dr esourcesofdi splayha r
dwa rea nd,
mor es i
g niicantl
y,limitedme mor yforma nageme ntr outi
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.
. her
3 ema
inde
roft
hel
inewi
lls
crol
ltot
hel
eftt
oma
ker
oom f
ore
achne
w
c
har
act
er.
4
. Noc
ont
rol
cha
rac
ter
swi
ll
bedi
spl
aye
d.
. hel
5 eft-
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,ba c
k s
pace,forwardc opy,andsoon)wi l
lbe
providedf or,exc
eptfornumbe r5a bov e.
Be f
orepr ocee
ding,let
’sdig
ressf oramome nttome nti
onthev a
lueofthelis
t
asapr ogrammi ngt e
chnique.Ify ouc an’tbringy oursel
ftolowc ha rt
,atleas
t
ma keal i
s ttoclar
ifyexactl
ywha ty ourpr ogr
a m willdo.hi she l
pstoor gani
ze
yourt houg ht
sinag ene r
alwaybe forey ouhavet oleapi nandc odethedetail
ed
part
s.Ev eni fyoua mendi tasthec odingprog res
ses,suchal i
stishelpful
.Now
backtoourr egul
arl
ys chedul
edpr ogr a
m. .
.
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

Afterthel i
sti
ngha sbeena ss
embled,ther out
ineishookedupt oCOUT,j ust
l
iketheot herr out
ine.Youwi llpr
obabl
ywa nttotypeinHOMEt ogiveyouac l
ear
scree
nf ory ourdi s
play.Onc eyourrout i
nei sinstal
le
d,e v
erythi
ngy out y
pe
shouldscroll ac
rossalineintheupperhalfofthescreen.Noti
cethatal
l expect
e d
outputfrom t heApplei snowdoneoni tsownc us
tom displ
ay.Youc a
nl istpro-
grams,c a
talogadi sk,ordoa nyoftheusua lopera
tions.Tryt
ypingint hiscom-
ma ndlineinAppl esoft:
FOR I = 1 TO 127: PRINT CHR$(I);: NEXT I

Whe ny oupr essr eturn,y ous houlds eeawhol es e r


ie sofc haractersg o
whi z
zingt hroug hthewi ndow,e ndingwi ththel owe r
ca sel ette r
s(althoug ht hey
ma ynotl ookqui ter ightify oudon’ thav ealowe rcas
edi spla yde vi
c e)
.Re me m-
ber,theleftarrowwi ll cl
ea rthedispla ywindow.
her out i
neitselfisfairlysimple .heonl yme mor yl oc ationsde ine da rethe
me mor yr angef ort hes c reenl i
nea t$700,at empora rys t orageby t
eus edby
COUT1, andourr outinet opr es
ervet hec ontent softheY- Re g ist
er.hepr ogram
alsoconta i
nss omei ns t
ruc ti
vepoi ntsofs tyl
e.
One ntry,theY- Re gisteriss aved.hi sisbe causet he“ oic i
al”out putr ou-
tine,COUT1,r eturnswi t
ha llregist
ers( A,X,a ndY)i nt a ctwhe nc a l
led.Ma ny
otherrout inesinBAS ICa ndDOSa ssumet ha talloutputwi llbedonea ssafely
,
sowemus thonort ha tconv entiona swe ll
.
Onc eYi ssaved,t hev aluepa s
s edtot hisr outinefr om t heAc c umul atoris
appropriatet otheAS CI Iv aluefort hec haracte rtobepr i
nt ed.Aswa sdonei n
thec ontrol-charact
e rdi splayr outine,ac hecki sdonef orc ontr
olc haracters
.
Re membe rthatinthi sprog rama llcont r
ol cha racte
rs,eve n<RETURN>, wi llbeil-
teredout .Ifac ontrolc ha racte
ri sde tec
ted,thec ompa r isononl i
ne1 2wi llfai
l
andac hec kwi l
lbema def orthel efta r
row( <CTRL>H) .I fthec harac t
eri snota
2
9.I
nte
rce
pti
ngOut
put 2
89

<CTRL>H,wewi
lli
mme
dia
tel
yex
itv
iaDONE1,whe
ret
heY-
Reg
ist
erwi
llbe
restor eda ndnoc hara cterwi llbedi spla yed.
Ifa<CTRL>Hi sde tecte d, theCLEARr out inec l
e arsthedi spla ywi 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 oindoutwhe nYr eached0 .
hepr oblemi stha t,whe nYr ea che d0 ,thebr a nc hwoul df allt hr oug ha ndwe
woul dnots toreas pa cea 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 fterr 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 .Afte 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 t
ec ha rac teri sde tec tedonl ine s1 2a nd1 3,c ont rollowst o
SCROLL, whi chma ke sroom f ort hene wc ha ractert obedi splay ed.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 tf orf utur eus e.
Att ha tpoi nt ,theY- Re g ist
e ri ss ett o$01i npr epa rationf ort heme mor y
mov et of ollow. Line2 6l oa dsac ha racterf rom onepos ition,a fterwhi chl ine2 7
wi l
ls toret hec ha racteri nt hepos iti
oni mme diate l
yt ot hel e ft.Fore x ampl e,on
their 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 fe re ntba sea ddr essesf ort hei nde x eda ddre ss
ing.
hi sa ll
owsust ous et hes amev a l
uei nt heY- Re gistertol oa da nds tor ea ttwo
dife renta ddr esses.hel oopi sr e pea tedunt i
lweha vemov eda llthec hara c
ters
onepos i
tiont ot hel eft.her out inet he nf all
si ntoPRINT.
PRINTi r s
tr etriev est hec ha rac t
e rtobepr i
nt edf rom t hes t
a c kbyme ansof
thePLAonl ine3 1.I tthe ns tor est hec ha r
a ctera t$727.hec 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 tofte ns een.
Afte rt hec ha racterha sbe ens tore da t$727,t heY- Re gi
s teri sr e 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 efti nt he i
ror igina lc ondi tionsr eg ardle ssofwhe t he rt heRTS i sdone
throug hDONE1orDONE2.S inc ewedi dn’tus et heX- Re gister,i ta lsowi l
lbepr e-
serve d.

]
2
90 As
sembl
yLi
nes

Conc
lus
ion
Her
earethemainpoi
ntsofourdis
cuss
ionontheout
putvec
tor
.
. hema
1 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
heir
ont s
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
ngalloftheinalout
put,
e ndtherouti
newi t
htheusua lRTS.
I
fyou’r
eme relyilter
ingorwa tc
hingtheout
put ,y
oumus teventual
lypasscon-
t
rolontowhe retheinaloutputwil
lbedone,usual
lyCOUT1($FDF0).
Inthenextc hapt
erwe ’
l
llookatthei
nputhook sa
nda thowt ouseyourown
r
outine
sont helist
eni
ngs i
deoftheApple.

CT]hee
3[ 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

It’
st i
met oe xami net hei nputs y s
te m oftheAppl e.Ma nypa rall
el
sc anbe
drawnbe tweeni ta ndt heout puts ystem,di s
c ussedi nt hepr e
viousc hapter
.
houg hnotr equired, somef a mil
iaritywi ththatc hapter’
sma j
orpointswi l
lhelp
youunde rs
tandourc urr e
ntt opic
.
hema i
nde mor out inesint hisi nstal
lme ntinv olv
el owercasetext;there
-
fore,it
’sstrongl
yr ecomme nde dtha tyoua cquirelowe rca
s edis
playha rdwa r
eif
youdon’ thavei talrea dy.Lowe rcasec hipsforAppl eswi threvis
ionnumbe r
s
greaterthan7c anbepur chasedfor$ 2 0to$ 30.Ea rl
ierAppl esrequir
emor ethan
as i
ng l
ec hi
p. Apple/ /edoe s
n’trequirea nya dditionalsoftwareorha rdwa re;t
he
l
owe r
casedi s
playcapa bi l
it
yi sbuiltin. Forse r
iouss tudya nde xpl
orati
onoft e
xt
input/outputme thods ,lowe rcas
ec apabi l
ityisessenti
a l
lyrequire
d.

TheI
nputVe
ctor
:KSW
heby 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 r
acteroutputS Wi t
c h)
,
KSWisusedt oswi tchi nputt oBAS ICandt heMoni torfrom dife r
ents our ce
s. As
isevi
dentfrom t hef acttha tanINPUTs tateme ntwi l
lr eadaDOSt e
x tilea ndt he
acti
onoft heEXECc omma ndont extil 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 ogramt hatne edsas in-
gle-
chara
c te
ri nputf r
om t heout si
dewor ld,theus ua lprocedur 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 tt
hatloc a
tion: 1

FD0C- A4 24 RDKEY LDY CH ; Get horizontal cursor


FD0E- B1 28 LDA (BASL),Y ; Get character from screen
FD10- 48 PHA ; Store it
FD11- 29 3F AND #$3F ; Clear bits 6,7
FD13- 09 40 ORA #$40 ; Set bit 6 (flash)
FD15- 91 28 STA (BASL),Y ; Put on screen

CT]hec
1[ odes
howni
sfora
nAppl
eIIorAppl
eIIPl
us.hec
odef
ora
nAppl
e//
eis
qui
teabi fe
tdi r
ent
,butt
hee
ntr
ypoi
nt tRDKEYa
sa ndKEYINa
ret
hes
ame
.

]
2
92 As
sembl
yLi
nes

FD17- 68 PLA ; Get the original character


FD18- 6C 38 00 JMP (KSW) ; To ‘real’ input
FD1B- E6 4E KEYIN INC RND ; RND = RND + 1
FD1D- D0 02 BNE KEYIN2
FD1F- E6 4F INC RND+1
FD21- 2C 00 C0 KEYIN2 BIT KBD ; Check for key
FD24- 10 F5 BPL KEYIN ; No, again
FD26- 91 28 STA (BASL),Y ; Restore old character
FD28- AD 00 C0 LDA KBD ; Get input character
FD2B- 2C 10 C0 BIT KBDSTRB ; Clear strobe
FD2E- 60 RTS ; Return with character

On e ntr
yt oRDKEY t heir 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 enposi
tion, y
oune e donl yloa
dt heY-
Re gi
sterwi t
ht hehorizonta lcursorpositi
on( CH=$24)a nda ddt sofs
hi ettothe
ba s
ea ddr es
sfort hecurr e
ntl ine.hi 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 fte
rt hei nput),thene xtthreeinstr
uc tionsha vethene t
efectofput tngala
i s
hi ngc ha rac
teront hescreene quiva
lentt ot hecharacte
r
thatwa son- s
creeninthec urrentcursorpos it
ion.
hea cti
onoft heANDsa ndORAsma ynotbei ntuiti
vel
yobv i
ous.Let
’scon-
siderthisexampl e:

He
x Bi
nar
y Charact
er
Or
igi
nalc
ha r
acte
r: $C1 %1100 0001 A(Norma l
)
AND: $3F %0011 1111 cl
earbit
s6,7
Fi
rstr
esul
t: $01 %0000 0001 A(I
nv e
rse)

ORA: $40 %0100 0000 se


tbit6
Fi
nal
res
ult
: $41 %0100 0001 A(Fl
ashi
ng)

Re membe rt hatthea ct
ionoft heANDi stoc l
ea ra nybitsintheAc cumul at
or
thata r
ema tche dbya0i nthema s
kv al
ue.Bi tsint heAc c
umul atorma t
che dby
1sint hema ska releftunchange d,whe t
hert heya r
e0 sor1 s.
AnORA,ont heot herhand,s et
sto1a 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
eleftunc hang ed.
Youmi ghtwonde ratirstwhyt woi nstruct
ions –t heANDf oll
owe dbyt he
ORA–we rene eded.Afte rall
,int hepreviousc hapt erdi dn’
twec hangec ontrol
character
stoi nve rs
ei njustones tep?Whynotj ustus eadi ferentma skvalueto
getla shi
ngc ha ract
e r?hea
s ns werliesint hedi fe rencesbetwe ent hebitpa t-
ter
nsf orinversea ndla 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 lea r( 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 er t
edt o
la s
hi ngt e mpor ar i
ly, wemus tnotonl yc leart hehi ghbi t(a tlea stfora ll“nor ma l

text),buta lsomus tonoc c
a sions etbi t6 .hi sc ombi na tionofas eta ndac l
ear
requi rest woope rations .
Onc eRDKEYha st husputala shingc ha ra cteron- scre ent os howt hec ur sor’
s
locat i
on,t hec ha racteror i
gina l
lyont hes cree ni sr e tri
e ve 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 ll
y ,the
ac t
ua lindi rec tj umpv iaKSWi sdone .
InCOUT( $FDED) ,t hej umpv i
aCSWwa sma dei mme di ately .hi se xtrapor -
tioni nRDKEYpr ecedi ngt hea c tua ljumpe xpla i
nst hepr ese nc eoft hec urs oron-
scree ndur ingat ext-iler ead. Al thoug hDOSi sha ndl ingt hei nputa tt hatpoi nt,
thec alliss tilldonev iaRDKEY,a ndt hust hepr es enceoft hec ur soriss til
ls 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 l
ot ,KSWwo uld
pointt o$Cnxx,whe ren i st hes l
otnumbe ra ndxxi st hei nputr out inee nt r
y
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.
KEYINi r sti ncre me ntsther andom- numbe rby tepa ir, $4E, $4F.hi si sapa rt
ofthel oopt ha twi llber epe atedunt i
l ak eyi spr e ssed. het he or yistha tt hepa s-
sageoft i
mebe twe e nk eypr ess esi sr andom.hi sby tepa iri sus edpr ima ri
lyby
Integ erBAS IC. Appl esoftha sit sownr andom- numbe rr egist ersa ndr out ines .
Afte ri nc re me nt i
ngt her andom by tepa ir, KEYIN2t he ndoe sthea 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 si
blebys ett
ing
thes ignla goft heS tatusRe g istere qua l tobi t7oft hec ha rac t
e rv al
uede tec tedat
thek eyboa r d( $C000) .BPLt husc anbeus e dtode tect(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 racterpr esuma bly
ther e,no tt hene wc ha ra cterinput .I fthec ha ra ctere nte redi sar ig
hta rrow,t his
signiie st ha twewa ntt omov et hec ur sorov ert hedi spla ye dc ha racterwi thout
cha ng ingt ha tc ha rac ter.heLDAKBDi swha tput st hei nputc ha r
ac teri ntot he
Ac cumul a t
or ,a twhi c hpoi ntt hes t
r obei sc le aredbya cc essing$C010a ndt he
ina lr e t
ur ni sdone .hec allingpr og ra mt he nha st heopt ionofpr i
nt ingt he
inputc ha rac tert ot hes c re e
n.

]
2
94 As
sembl
yLi
nes

Ot
herI
nputSour
ces
KSWdoe
snota
lwa
yspoi
ntt
oRDKEY.I
nfac
t,itdoe
sn’
tpoi
ntt
her
ewhe nDOS
i
sins
tal
le
d.Wit
hDOSboote
dandact
ive
,ent
ertheMonit
orandt
ypein:
38.39 AA55.AA56<RETURN>

Yous
houl
dge
t:
0036- 81 9E
AA55- 1B FD

You’llseet ha tKSWa c tuall


ypointst oDOSa t$9E81,whi cht heneventua l
ly
pointstoRDKEY( $FD1B)a t$AA55,$AA56.Li ketheout puts ystem,DOSi sr ather
perma nentl
yma depa rtoft heinputpa th.Anya ttempt stodi sconnec
tDOSby
modi f
y i
ngKSWdi rect
lywi llbeundonebyDOSi fa nyout puti sdone .DOSha sits
own i nt
e r
na li nputv ectora t$AA55,$AA56.I ta l
terst hisv e
ctor,notKSW,a s
neededt ogaina ccesstov ariousslot
s(ort odiskil es,asappr opr i
a t
e).
Youc ani ns tallyourownr outi
nei ntothei nputpa t
hbyme ansofapr oce-
duresimi l
artot heoneus edi nthepre v
iouscha ptert ointer c
e pttheoutputpa t
h.
Putthelow-a ndhi gh-orde rbytesofthede s
ti
na tiona ddre ssintoKSW( $38,$39=
56,57de cima l
)a nddoac allt
o$3EA( 1
0 02decima l
).hi sc aus esDOSt oc ha nge
it
sownv ectorsa t$AA55,$AA56t othea ddressspe ciied,a ndt he ntorest
or eKSW
sothatitpoint st oDOSa g ain,usual
lyat$9E81.2

InAppl esoftt hiswoul dt aketheform:


10 POKE 56, LB: POKE 57, HB: CALL 1002

I
nthi
sexampl
e,LBandHBaret
hel
ow-andhigh-
orderby
tesoft
hede
sti
na-
t
iona
ddre
ss.
Inass
emblyla
ngua
ge,i
twoul
dlookl
iket
his:
LDA #LB
STA $38
LDA #HB
STA $39
JSR $3EA

Justasout putha stwoba si


cclas
sesofr outines,t
herearetwoma intypesof
inputr outines–t hos
et hatint
erceptincomi ngc hara
cter
sa nddos omes ortof
processing,andt hosethatenti
rel
yr e
placethei nputrouti
nesalre
adybe i
ngus e
d.
Ifyoua redoi ngthelatt
er,thi
ngsarefair
lysimpl e.Onceinst
all
ed,yourrouti
neis
enti
relyi nc hargeofg ett
ingtheinputc hara c
ter;whent ha
tc hara
cteris“got,

yourr outinee ndswitha nRTSt opasscontrolba cktothecall
ingprogram.hi s
approa chissimilartoourc ust
om outputrout i
nesfrom t
hepr e
v i
ouschapter
.

2[
CT]J
ustl
iket
heout
putv
ect
or(
des
cri
bedi
ncha
pte
r29
),whe
nus
ingPr
oDOSy
ouc
an
dir
ect
lycha
ngethei
nputve
ctorat$BE32,$BE33topoi
ntt
oyouri
nputr
out
ine
.Se
e
chapt
ers
ixofI
nsi
det
heApp
le//e
,byGa r
yB.Litt
le
.
3
0.I
nte
rce
pti
ngI
nput 2
95

heir s
tcl
assofi
nputrout
ine
s,i
nwhichincomi
ngchar
act
ersaretobe
i
nte
rce
pte
d,mustbehandl
edsli
ght
l fe
ydi r
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 talowr 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 t
hi 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( asoppos edt oJSRs )
.You’ llrecal
lf rom ourout put
inte
rcepti
onf rom thepr ev i
ousc hapte
rt hattheina lexitfrom ther out i
newa sa
JMP$FDF0( orwhe rever)a fte
rthepr oce
s si
ngwa sdone .
Wi t
hinput ,
thes ecretistodoaJSRt oKEYIN( orwhe rever)ir standthendo
yourpr oces
sing,followedbya ne ventualRTSt ot hec al
lingpr ogr am.Forour
irstexperi
me nt,we ’
llt
rywr it
ingar 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 spl
ay edinlowe rc
a se
.
Nume ri
ca ndcontrol c
ha ract
e r
ss houl dbeuna fecte
d.her outineworksbyir s
t
cal
li
ngKEYIN, whichg etsac haracterf rom thek eyboar
da ndputsitintheAc cu-
mulator
.Att hatpointourr outinee nsur esthatwe ’
vegotac apit
alle
tter,rather
thananume ri
corc ontrolcha ract
e r.Ifwedon’ tha v
ea nalphabeti
cc haracter
val
uelessthan$C1,the nther outines kipst oDONE.
I
fwha tweha veisa na lphabeticc haract
er,theconvers
iont olowe rcas
ei s
donebyf orci
ngbit5oft heAS CI Ivaluet o1.hev al
uesofal
llowe r
casec harac-
ter
saree qualt
ot hev aluesoft hec orre spondinguppe r
casel
e t
terspl
us3 2 .hi s
me a
ns,asa nASCIIc hartshowi ngbi tv aluesrevea
ls,t
hatcapi
tallet
ter
sha vebi t5

]
2
96 As
sembl
yLi
nes

cl
earandlowe r
casel
ette
rshavebit5set
.Li
ne13ofourr outi
nese
tsbi
t5,t
hus
conve
rti
ngt hechara
ctertolower
case
.Fina
ll
y,l
ine1 4re
turnsustot
hecal
li
ng
prog
ram.
Ourr outi
neshouldworkfrom wi
thi
nApple
soft.
Trythi
s:3

6 INPUT “ENTER A STRING:”


10 POKE 56,0: POKE 57,3: CALL 1002
20 INPUT I$
30 PRINT I$
40 PRINT CHR$(4); "IN#0": REM DISCONNECT ROUTINE

Don’
tbes
urpr
ise
dift
hispr
ogram does
n’twor
k.4

Tr
ychang
ingl
ine20t
olookli
kethis
:
20 GET A$: PRINT A$;: IF A$ <> CHR$(13) THEN I$ = I$ + A$: GOTO 20

Now r un thepr og ram.her esultst hi


st imes hould bemor el ikey ou
expe cte
d. Line3 0i sus e dt oc onir mt hef acttha tthelowe rcaseda t
awet ypedin
onl ine2 0a ct
ua l
lyma dei tt oAppl esoft.
heque st
ionnowi s, whydi dn’tt heir stprog ram wor k?I nas e
ns ei tdid.If
youl ike,g oba cka ndr unt heir stpr og r
a m wi t
houtl i
ne4 0 .Whe nt hepr ogram
ends ,goi ntot heMoni tora ndc heckt heDOSi nputv ec
tora t$AA55,$AA56.I t
shoul dindic at
etha tourr out inea t$300i sbe i
ngus ed.
hepr obleml iesinAppl esoft’
sus eoft heGETLN( GETLi Ne )routinef orthe
INPUTs t
a te
me nt .hi sr out inei sus e dt oinpute nti
rel i
nesa tat ime .Al though
GETLNd oe sus et heRDKEYr out inet og etindi vi
dua lcharacte rs
,i tunf ortuna t
el
y
tampe rswi ththec hara cte rse nteredbe f
orei tret
ur nstheda tat oAppl e soft,DOS ,
ort heMoni tor.
Speciic al
ly,GETLNc onv e r
tsa nyl owe rcasec haracter
sc omi ngi nt ouppe r-
case. hus ,e ve
nt houg hourr outinec onv ertstheuppe rcasec harac t
ersc omi ngin
throug ht hek eyboa rdt ol owe r cas
e ,GETLN u ndoe se ver
yt hingbyc onv erti
ng
them ba ckbe foret hey ’
r ee ve ne choe dt othes creen.
Anot hera nnoy anc eofGETLN i st hati tconv ert
sc hara ct
e rsthaty ouc opy
from thes creenus i
ngt her i
g hta rrow.
her easont hepr og ram wor kswi tht hene wv e
rsionofl ine2 0i st hatthe
Appl esoftGETs tateme ntus esadi rectca l
ltoRDKEYa nddoe snotus eGETLN.
Onewa yt os olvet hepr oblem oft heINPUTs t
ateme ntnotwor kingi sby
wr i
tingy ourowni nputr out inei nsteadofus ingt heGETs eque nce.hee asi
est
thingt odohe rewoul dpr oba blybet oc opyt heGETLNr outinea nde l
imi na t
ethe
conv ersi
onpor ti
ons tartinga t$FD7E. 5

3[
CT]Unde
rPr
oDOS
,yous
houl
dcha
ngel
ine1
0toPOKE48690,0:POKE48691,3
4[
CT]Ona
nAppl
e//
e(unde
rDOS
)thi
spr
ogr
ama
ctua
ll
ywi
ll
wor
k.
5[
CT]I
ntheAppl
eIIa
ndAppl
eIIPl
ust
hec
odea
t$FD7Ec
hec
kswhe
the
rthec
har
act
er
s≥$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
’ss
eeifwecanimprov
eonthesi
mpleinputrouti
nejus
tshown,
ma
kingi
talit
tl
emorelexi
ble
,wit
houtre
wri
ti
ngtheGETLNrout
ine.

Some
thi
ngMor
eUs
eful
:Lowe
rca
seI
nput
Althoug hther outi
nejustgiveni
ll
ust
rat
est hec onceptofinte
rcepti
nginput,
i
t ’
snotr eal
lytha tusef
ulbecauseitpr
ovi
desnowa yofs witc
hingbe t
we enupper
-
caseandl owe rc
a sele
tte
rsatwill.
Whynotc reatea ninputroutinethatall
owsus
toshiftbetweenuppe r
caseandlowe r
cas
elet
tersa swei nputthem?Aswedi df
or
theoutputr outineinthepreviouschapt
er,
we ’
ll irstma kealis
tofwha twewant
theroutinetodo:
1. her outines houldall
ow nume ri
candc ont rolcharact
erstopa s
st hr
ough
unalt
e r
ed.
. her
2 out
ines
houldbes e
tupsucht
hatpr
ess
ing<ESCAPE>onc
ewhe
nint
he
l
owe
rca
semodewill
shiftonl
ythene
xtl
et
tert
oupper
case.
3
. Pr e
ssi
ng<ESCAPE>t
wicewheni
nthelowe
rcas
emodeshoulds fta
hi lls
uc-
c
ess
ivei
nputt
otheupperc
asemode(
thi
siss
ometi
mesc
all
ed“ca
pslock
”).
4. Pr e
ssing<ESCAPE>onc ewhe nintheuppe r
casemodes houldreturnthes ys
-
temt othel owe rcasemode .
hes y st
em ofus i
ng<ESCAPE>a sas hiftkeyi ssome wha tstandard.Be f
ore
goingont ot helisting,thoug h,let’
sthinkal i
tt
lemor ea boutwha tisne ededto
imple mentt hissyste m. Firstof, we ’
llneeds omewa yt or eme mbe rwhi c
hmode
(lowercaseoruppe r case)we ’r
ei n.hemos tdirectwa yofdoi ngt hisistous ea
la g
,whi c
hwe ’
llca llCSFLG ( Ca SeFLa G) .Toa v
oidaz ero-pag econlict,we ’
ll
reserveapl acefort hela ga tt hee ndofther outine.
Inor dert ofulill t
her e quireme ntst
a t
edi nitemt hr e
eonourl i
st,wene edto
storethev alueoft hel astc hara c
terinput–t hatis,thec ha ra
cterjustbeforethe
onec urrentlybe ingi nput ,ina notherstoragelocation.hi swillall
owust otel
l
whe n<ESCAPE>ha sbe enhi ttwiceinar ow. We ’l
lcallthisl ocati
onLSTCHR( LaST
CHa Ra cter
).
heg ener a
lpa tternwi llbet odos omebr ieftestse ac htimeac haracte
ris
inputa nd,i fnoc onv ersioni sne ces
sary,topa sstheuppe r
caseletterthrough
una l
tered.Onl ywhe na n<ESCAPE>s equenc eiscomi ngt hroug horwhe nwe ’r
e
inthel owe rcasemodewi llwee vera l
te
rt heinputc harac ter
.He re,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

Afterasse
mbl i
ngandi nst
al
li
ngthisrout
ineat$300,t
rytheApplesoftpro-
g
r a
m wi tht
healte
redli
ne2 0agai
n.histi
mey oushouldbeabl
etoe nt
erastri
ng
c
onta i
ningbothuppe r
caseandlowerc
a s
elet
ters
,withthe<ESCAPE>k e
yf unc-
t
ioningasdescr
ibedintherequi
rement
slis
t.
3
0.I
nte
rce
pti
ngI
nput 2
99

Not etheus eofEQUt ode ineESCi nl i


ne9 .hel a belESCi sus eda sav alue
rathe rt hanal oca ti
on. hi swa yy ouc a nc hang ethek eyus edf ors hi ftbyc ha ng-
ingt hev aluee qua tedi nl ine9 .
Al ooka tt hes our cel ist
ingr eve al
swha t’
sg oi ngon.Fi rs t,aJSRKEYINi s
donet og etac ha rac t
erf rom t hek eyboa rd. KEYINh a ndl esthela shi ngc ur sora nd
key boa rdha rdwa ref orus .Ne xt,t hei nputc ha racte ri spus he dont hes tacks o
we ’
ll bef reet ous et heAc cumul at orifne c essarywi thoutl osingt hei nputc ha rac-
ter.
Ne x t,at e s
ti sdonet os eewhe thert hec urrentc haracte risa n<ESCAPE>
cha racte r.Ifso,abr anchi sdonet ot he<ESCAPE>- ha ndl i
ngr out ine ,ESC1( line
34). heir s
tt hingdonea tESC1i st os eei fthel astc ha rac t
erwa sa n<ESCAPE>a s
we l
l, inwhi chc a seLOCK( li
ne3 8 )s etsc aps-lockmodebyput t
inga$80i nCSFLG.
Ifnot ,t he nCASE2( line4 8)c heck sCSFLGt os eewhe the rwe ’rec ur rent lyi nl ow-
erca seoruppe rca se.
Tos impl ifyt hist est,we ’veus edav alueof$00f orCSFLGt os ig nifyt hel ow-
erca semode .Av a l
ueof$80s igni iest heuppe r ca
s emodei noure x ampl e. he se
value swe rec hos ent oa ll
ow t heus eoft heBIT c omma nd.Be c aus et heBIT
instruc tionc ondi tionst hes ignla g( bit7 )oft heS ta tusRe gistera c cor dingt obi t
7oft heme mor yl oca t
ionr eferenc ed,wec a nt estt hes tatusofCSFLGwi thout
actua llyha vingt ol oa dt heAc cumul atorwi tha nythingt odot het est.
CASE2u sest heBITi nstructiont ot e
s tbit7ofCSFLG. Ifbit7i sc l
e ar, we ’rei n
lowe rc asemodea nda lltha tnee dst obedonei stopa sst hisir st<ESCAPE>c ha r-
actert hr oug ht oXFER, whe reitwi l
lbes toredi nLSTCHR.ha twa yt he<ESCAPE>
canbeus edtos ig nifyas hifttouppe rc as
ei fthene xtc ha ra c
teri sal e t
t er.
Ifbi t7i ss et,t henwe ’
rei nuppe rcase,a ndwene edt o“ unl oc k”t heuppe r-
casemode .UNLOCKd oe st hisbyput t
inga0v aluei nCSFLG. You’ llalsonot icet hat
thec ur rentc ha rac t
e risc ha ngedf rom a n<ESCAPE>t oanul l.hi si sdones ot hat
afterdown- shifting ,wec a nstil
lpr ess<ESCAPE>onc emor etoc apitaliz et hene xt
l
e tt
e r.Ifweha dn’ tc hang e dtha t<ESCAPE>t oanul lwhe nwedown- shi fted, we ’d
beba cki nc aps -l
oc kmode .
Fort hene x tpa sst hroug h,l et
’ss eewha tha ppe nswi t
hanon- <ESCAPE>
cha racte r.We ’llre sumet rac i
ngt her outiner ighta fterENTRYha sde cide dt ha tt he
curr entc ha racteri snota n<ESCAPE>c hara ct
e r
.
hene xts e ctioni sCHAR,whi chc he ckst os eewhe t
he rt hel astc ha r
a cter
throug hwa sa n<ESCAPE>c ha r
a cter. I
fs o, wene edt oma kes ur et hec ur rentl et-
teri sc apitalized,e v ent houg hwe ’repr esuma blyi nt hel owe rca semode .hi si s
easilydone ,thoug h:pr og r
a m low pr oce edsdi rectlyt oXFER.Re me mbe r,XFER
simpl ys t
or esthec ur renti nputc ha r
a cterinLSTCHRa ndt he nr etur nst ot hec a ll
-
ingpr og ram.I nt hisc a se,be caus ea llcha ractersg ene ratedbyKEYINa r ea lwa ys
uppe r case( exc e
ptont heAppl e/ /e),we ’
ll j
us tleavet hec apitalle tt
e rinput“ asi s”
andpa ssitthr oug h.

]
3
00 As
sembl
yLi
nes

Ifthela
stchara
cte
rwa snotan<ESCAPE>,progr
am low 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
eorl owe rcasemode .
CASEu sestheBITi nstruct
iontodot histest.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
ectlytot heXFERr outine.XFERr etri
eves
theor iginali nputc harac t
ers t
oredont hesta ck,upda t
esLSTCHR( sinc ethiswi l
l
nowbet he" l
astc ha racter”ont hene xtpa ssthr ough),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.CVERTi rstr etri
evestheinputc ha racterfrom
thes t
a cka ndt he nc hec kstos e
eifthec haracterha sanAS CIIv al
ueles stha nthat
ofthel etterA. I
fs o,thec ha rac
tercomi ngthr oughi 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 onvers i
onr outinetoline2 7,whi chput s
thec har act
e rba ckont hes tack(whe reXFERe xpectstoindi 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 onvertingt helett
ertol owerca se.Att hat
pointt hene wc ha racterisputont hes tackfort heXFERr outi
ne .

Conc
lus
ion
hi sisde ini telyoneoft hos epr og ra
mst ha ttakealowc 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,i tma 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 rstandt 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
ftous 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 teadi fe 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

hi 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 .
hedi 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 csefec t
s.

Te
xta
ndHi
-Re
sSc
ree
nMa
ppi
ng
heir stcons i
de r
a t
ioni nc rea
tingourc hara cte
rg ene rat
ori st het opicof
wha tactuall
ywi l
l ber equir e
dt oputac haracteront hehi -re sscreen. Inpr evious
chaptersweha v es eenhowe achdotont heg raphic sscreeni sr e
latedt oa ni ndi -
vidualbitwi thinaby t
eofme mor yassignedt othehi -resdi s
play.Ine ar
lierr ou-
tineswec r
eatedg raphicsbypl ott
ingdot susingt her outine sbuiltintoAppl e soft
BAS IC.hi stimet heappr oa chwi l
lbes ome wha tdife rent.
Toc r e
ateac ha rac
teront hehi -
ress c
reen,a ne nti
rea rra yofdot swi l
l hav eto
bet urnedon.Al thoug ht heHPLOTr out i
nesofAppl e
softc oul dbeus ed,itt urns
outt hereisamuc hs impl erwa ytoa chievethede siredr e sul
t.hi sme thodi s
ba s
edons imilaritiesbe t
we ent henor ma ltextdisplaypa gea ndt hehi -resg r
a ph-
icsdispl
a ypa ge.Tof ullyunde rst
andt histechnique ,thoug h,abr iefov erviewof
thes cr
eenme mor yma ppingwi l
lber equire
d.
Ont heAppl e,textdis pla
yi snorma l
lyconine dt owha tiscall
edt extdi splay
pa g
e1 .hi sdi splayc orrespondst oabl ockofme mor yi nt hea ddressr ang e$400
to$7FF( 1024to2 0 47de cimal).Ac ha ract
erispr i
nt edont hes creenbys toringa
singleby teint hisme mor yr ange.hec omput e rha rdwa r ethe nt akesc areof
conv er
tingt hiss toredc ha racterintoav i
deoi ma g eony ourmoni torort elevi-
sions e
t.

]
3
02 As
sembl
yLi
nes

Li
ne# Addr
ess(
hex
) Addr
ess(
dec
) heme 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.ha t
2 500 1280 istos a y,i fy ouwe ret oil lme mor ys e que n-
3 580 1408
4 600 1536
tial
lywi thac e rt
a inv alue,t hes c r
e eni ma ge
5 680 1664 woul dnotbec ha ng edi nal i
ne -by -
line ,c ha r
-
6 700 1792 acter-by -cha racterpa ttern.I nstead,ar ather
7 780 1920
8 428 1064 unus ua lpa tternwoul dbef ol lowe d.het able
9 4A8 1192 atl ftg
e ivest hea ddr essoft heir 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 pletec ha rt.
16 450 1104 Youma yr eca l
lf rom e arlierc ha pterst hat
17 4D0 1232
18 550 1360 itwa snotne ce ssaryt oc al
cul a t
et hebe ginni ng
19 5D0 1488 addr ess( some time sc alledt heb as ea ddre ss)of
20 650 1616
21 6D0 1744
eachl 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 a ll
ed,i tt ake st he
va l
ues tor edi nl ocation$24( ca l
ledCVf orCur -
sorVe rt
ic alpos ition)a ndc alculatestheba sea ddr essoft hel i
nec or r
e spondi ng
tot hatv e r
ticalpos iti
on. 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.
hi 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.
heba sea ddre ssi sreturne di naz ero-pa gepoi nte rca l
le dBASL, BASH( $28, $29=
Ba s
eAddr esSLowby teandHi ghby te).
Atir stg l
a nc e,the r
ema ys eemt obet oof ewhor izont alrowst or e
pr ese nta l
l
19 2l i
nes .Howe ve r,ifyoul ooka ttheig ureont hene x tpa g e,int hemi ddle
you’llseeabl ow- upofoneboxoft hema p. Ea chofe i
g htl ineswi thint heboxi s
l
a beledwi t
honeofe ightv alues.Wha tt hisme ansi st ha te achboxont hema in
char tactua ll
yr epr esentse i
g hts creenl 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.Toindt he
basea ddr essoft het hirds creenl ine,fori ns tanc e,youwoul da ddt hec orre cti
on
fort het hirdl inewi t
hinabox( $800)t ot heba sea ddr essf ort hepr ima rybox
($2000) tog e
tt hea ctualba sea ddress($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 i
zont aldotpos iti
ons .S evenbi tsi ne achby t
ea r eus e dt o
ma pt hes creendot s(7×4 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 equire dt oe v enb egint ok nowwhe ret os t
a r
t
drawi ngourc ha rac t
eron- screen.Ta kehe art,t houg h!I fy oug ivei tal ittl
e
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
03

thoug ht,y ous houl dbea blet os eear ema rkabl


es imi l
ari
tybe t
we enthehi -
res
pagea ndt het e
x tpageinr eg ardtot heirme mor yma pping.
heir stsi
mi lari
tyisint henumbe rofby t
esusedf oreachhor i
zontall
ineon
thes c
r e
e n. Inea chc a
se,4 0by tesa reuse dfora nentireline.Couldtherebee ven
mor es i
mi l
a ri
ty?Re adon!
Ify oul ooka ttheir stfourl inesoft het extpage ,t
heba seaddress
esar ethe
values$400,$480,$500,a nd$580.I fy oue xaminet heir stfourblocksofe i
ght
li
ne seac hont hehi -resscre e
n,t heba sea ddr e
ssesar e$2000,$2080,$2100,a nd
$2180.
You’ llnot icethatify oua ddt hev alue$1C00t oe achofthetext-s
cree
nv al
-
ues,you’ llg ett
hec orr
e s
pondi ngba sea ddressforthehi -resscre
en.hi spa t
tern
continue st hroug houtalltwe nty-fourt extscreenli
ne s.
Wha ta boutt hee ightl i
ne sfore achbl ock?Ea chs uccess
ivelinewithina
blockc anbec a
lculatedbya ddingt hev alue$400t ot headdr e
ssfortheli
nea bove
it.hi swi llturnoutt obej us tperfectforc reati
ngac haract
er.
Asi tha ppens ,achar acteront henor ma ltex
ts creenisma deupofdot sina
ma tri
xs ev endot shi hbyiv
g edot swi de. Aroundt hisma tr
ixthereisabounda ry

]
3
04 As
sembl
yLi
nes

ofonedotpos i
tionone ithe rs i
dea ndonedotpos it
iona l
ongt hebot tom.hi s
perma ne ntlye mpt yr eg i
oni ss etupt opr ovideag ua rantee dsepara t
ionbe twe en
chara cter swhe npr int edon- sc r
ee n.hus ,theina lma trixisa c
tuallys ev endot s
widebye i
g htdot shi g h. heig 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 eftope n. Att hispoi nt, alitt
lelightiny ourmi ndi sproba blys t
arti
ng
tog ow.hes
l ev endotpos itionsa crosse achc ha r
a cterc anc orrespondt ose ven
bitsine ac hoft he4 0hi -re ss creenby tesus edone a chl ine.hee ighthor i
zontal
rowswi l
lc or respondt ot hee ightby tesa ss
igne dt oe a chpr imaryboxde scri
be d
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
e a
ted.
Inabl oc kofe i
g hts eque nt i
a lby tesofme mor y, wec ans t
orea lloft hei nfor-
ma ti
onne ede dt oc rea teas i
ng lec ha racteront hes c
r een.Ea chby tewi llcorre-
spondt ooneoft hee ig htr owsi nt hema trix.Ea chbi twi t
hin aby t
ewi l
l
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 ll
us tratehowt hisr eallyf orms $22 %0010 0010
thele tterA,t ak eal ooka tt het a blet o $22 %0010 0010
$3E %0011 1110
ther ig htoft heig ur e ,whi chs hows $22 %0010 0010
theses amenumbe rsi nadi fe rentwa y . $22 %0010 0010
Int her ight-handc ol umni st he $00 %0000 0000

binaryf or m ofe achnumbe r.Youc an


seewhi chbi tsar eona ndwhi cha r eof. hi sr el
a tesdi rectlytohowt hec haracter
isdispl aye don- scre
e n. hebi tsa repl ottedinr everseor der–t hatis,wi thbi t0i n
thele ftmos tpos iti
on.Bi t7( thehi g hbi t)isne verdi spla yedon- screen.Atmos t
,
bit7c anbeus e donl yt os hifttheot he rdot sone -ha lfpos it
ion.S eet hee arl
ier
chapt ersonhi -resplot tingi fy oune edal itt
ler e
freshe ri nthisa r
e a
.

Th eCha
rac
terGe
ner
ator
Nowt oactuall
ydescri
bethecharac
tergener
atort
hatwil
lbeusedt
oputthe
a
ppr
opri
ateAS CIIchar
acteronthehi-
resscre
en.
hepr 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
olcha
ract
er,nospeci
alpr
oce
ssi
ngwi
llbedonea
nd
t
hec
harac
terwi
llbepas
sedontoCOUT1($FDF0)
.
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
05

3.I fthec harac


terisnotacontr
olcharacter
,anexaminati
onofCV($23=Cu r
-
sorVerti
calpos i
ti
on)andthec ur
renttex
t-pageaddres
swillbemade.Av a
lueof
$1C00willb eaddedtoBASL,BASH($28,$29)tocalc
ulat
ethebaseaddres
softhe
pri
ma r
yhi -resscree
nl i
ne.hec ont
entsofCH( $22=Cu r
sorHoriz
ontalposi
-
ti
on)willthenbea ddedtothisbaseaddresstocal
cul
atetheact
ualhi-
resscr
een
bytet
obemodi ied.
4. heAS CI Iva
lueoft hecha r
a c
tert
obepr i
ntedwil
lbeus e
dt odeterminethe
posit
ioninac har
act
e rdatatablefrom whic
hthee i
ghtbyte
sc onta
iningthedata
forthecha r
a ct
erwil
lber et
ri
ev ed.hepos i
ti
onc anbede t
erminedbyir stsub-
trac
ting32fr om t
heAS CIIvalue( t
oma keupforthemissi
ngc ontr
olcharact
ers
inthetable).her esult
ingv al
uei sthenmulti
pli
edbye ig
ht(foreightbytesper
charact
er)tode t
erminet hecor r
e c
tsta
rti
ngposit
ionofthedataforthatparti
cu-
l
a rchara
cter.heg ene r
al f
ormul a,t
hen,i
s:
Posi
tion=( ASCIIv alue−3 2)×8

5. hec ha
rac
terwil
lact
ual
lybeproducedbyst
oringtheirstby
teinthec
alc
u-
l
ate
dbaseaddres
s.hene xtsevenbyteswil
lthenbes tore
da ttheaddr
esse
s
de
termi
nedbysucce
ssi
vel
yaddingt
hev a
lue$400tothebaseaddre
ss.
6. Att hatpoi ntthepr i
ntingt ot hehi-resscreenwi llbec omplete.heor ig
ina l
charactertobepr intedwi llt henbes entt oCOUT1( $FDF0)s otha tt
heMoni tor
routinescanha ndlec arr
iag er e
turns,backspac es,ands oon.hi 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.
hi slas tpointma yneedabi tofexplanation. I
fwene versentac harac te
rt o
COUT1, wewoul dha v
et oha ndlethee ntir
es creenma nag ementour s
e l
ves .hi s
me ansthatwhe nweg ottot hee ndoft hel i
ne ,wewoul dha v
et odetecti ta nd
thena dvanc eCVa ndr ecalculateBASL,BASHa cc ordi
ng ly.Bypa ss
inge a
c hc hara c
-
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 t
a
printedtot hesc r
een.
husa llwene edt odoi slooka tBASL,BASH,CH,a ndCVf ore achc har a
c te
r
printedtoha vethehi -resscre e
npr operlymi mi cwha tisg oingonwi tht het e x
t
displ
a ypage .
He re,then, i
sthel i
sti
ngf orthehi -
r e
sc haractergene rat
or:
1 ********************************
2 * AL31-CHARACTER GENERATOR *
3 ********************************
4 *
5 * OBJ $300
6 ORG $300
7 *

]
3
06 As
sembl
yLi
nes

8 CSW EQU $36


9 BASL EQU $28
10 CH EQU $24
11 TABLE EQU $9000
12 POSN EQU $3C ; (BAS2)
13 SCRN EQU $3E ; (A4)
14 VECT EQU $3EA
15 COUT1 EQU $FDF0
16 *
0300: A9 0B 17 HOOK LDA #ENTRY ; PRODUCES LOW BYTE
0302: 85 36 18 STA CSW
0304: A9 03 19 LDA #>ENTRY ; #> PRODUCES HIGH BYTE
0306: 85 37 20 STA CSW+1
0308: 4C 3A FF 21 JMP VECT
22 *
030B: C9 A0 23 ENTRY CMP #$A0
030D: 90 51 24 BCC OUT ; CTRL CHARACTER
030F: 48 25 PHA ; STORE CHAR
0310: 29 7F 26 AND #$7F ; CLEAR HI BIT
0312: 85 3C 27 STA POSN
0314: A9 00 28 LDA #$00
0316: 85 3D 29 STA POSN+1
0318: 98 30 TYA
0319: 48 31 PHA ; SAVE Y
32 *
031A: 38 33 CALC1 SEC
031B: A5 3C 34 LDA POSN
031D: E9 20 35 SBC #$20
031F: 85 3C 36 STA POSN ; CHAR < 96
0321: 06 3C 37 ASL POSN ; *2 = CHAR < 192
0323: 06 3C 38 ASL POSN ; *4 < 384
0325: 26 3D 39 ROL POSN+1
0327: 06 3C 40 ASL POSN ; *8 < 768
0329: 26 3D 41 ROL POSN+1
42 *
43 * POSN = (ASC - $20)*8 BYTES PER CHAR
44 *
032B: 18 45 CLC
032C: A9 00 46 LDA #TABLE ; LOW BYTE
032E: 65 3C 47 ADC POSN
0330: 85 3C 48 STA POSN
0332: A9 90 49 LDA #>TABLE ; HIGH BYTE
0334: 65 3D 50 ADC POSN+1
0336: 85 3D 51 STA POSN+1 ; POSN = POSN + TABLE ADDR
52 *
0338: 18 53 CALC2 CLC
0339: A5 28 54 LDA BASL
033B: 65 24 55 ADC CH
033D: 85 3E 56 STA SCRN
033F: A5 29 57 LDA BASL+1
0341: 69 1C 58 ADC #$1C
0343: 85 3F 59 STA SCRN+1 ; SCRN = BASL + CH + $1C00
60 *
0345: A0 00 61 GETBYTE LDY #$00
0347: B1 3C 62 G1 LDA (POSN),Y
0349: 91 3E 63 STA (SCRN),Y
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
07

034B: C8 64 INC INY


034C: 18 65 CLC
034D: A5 3E 66 LDA SCRN
034F: 69 FF 67 ADC #$FF
0351: 85 3E 68 STA SCRN
0353: A5 3F 69 LDA SCRN+1
0355: 69 03 70 ADC #$03
0357: 85 3F 71 STA SCRN+1 ; SCRN = SCRN + $3FF
72 *
73 * $3FF TO MAKE UP FOR GROWING VALUE OF 'Y'
74 *
0359: C0 08 75 DONE? CPY #$08
035B: 90 EA 76 BCC G1
77 *
035D: 68 78 YES PLA
035E: A8 79 TAY ; RESTORE Y
035F: 68 80 PLA ; RESTORE CHAR
0360: 4C F0 FD 81 OUT JMP COUT1
0363: D8 82 CHK

her out i
nei sr el
a tivelys hor ta ndi splace da tl ocation$300( 768de c
ima l)
.
Whe nac a
llto$300i sdonebye it
he ra300Gf rom t heMoni tororaCALL768
from BAS IC,t her outinewi llsett 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 heir stthi ngt ha ti sc hec kedf ori st os eewhe t
he rthec haracter
be i
ngout putisac ont rolc ha ract
e r.Re me mb ert ha tatthi spoi ntt hehighbi twi l
l
bes etona lltextg oingt ot hes creen.he r
efor e,e v ent houg h$20i st hemor e
nor ma lAS CI Iv aluef oras pac 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- contr olc har act
er, it
’st henpus he dont ot hes t
ackin
l
ine2 5.hi sist os avet hec harac tert obepr inteds ot hati te ventua l
lycanbe
pa ss
edont oCOUT1.hene xtline ,2 6 ,thenc le arst hehi ghbi toft hec haracter
ands tor e
st her esultingv aluei nPOSN.
hi 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 llrequireat able
76 8($300) by teslong ,POSNwi llha vet obea blet oi nc l
udeat wo- bytevalue.hus
li
ne s2 8a nd2 9t ak et hisoppor tuni tyt os ett hehi ghby teofPOSNt o0nowi n
ant i
cipati
onoff ut ur ec alcula t
ions .
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 ts avet heY- Re gist
e rsoa st obea blet or e storeittoi t
s
or i
gina lcondi ti
onl aterone xit.Toa v oidha v ingt ous ea not he rz e ro- pageloca-
tionf ort his,we ’vede layeds avingt heY- Re gisterunt ilnows ot ha ti 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 atorwoul dha ve
erasedt hev aluef ort hec ha rac t
e rwewa nt edtopr int.
Nowf ort hec alculationpha se. heir ststepi st osub trac t3 2f rom t heAS CII
va l
uei npr epa ra t
ionf orc alcula tingt het ablepos i
tion.Li ne s3 3t hr oug h3 6do
this.hene xts tepist omul tiplyby8t og etther e l
ativepos iti
oni nt het able.For-
tuna t
e l
y ,8i sa ne a synumbe rbywhi cht omul tiply .Youma yr eme mbe rfrom
ea r
li
e rc ha pte rst ha tal e ft-shift ope rationi se qui valentt omul t iplyingby2 .
he refore,a llwene eddoi ss hiftl eftthr eetime st og etthee fe ctofmul ti
plying
by8( 2×2×2=8 ).
Nor ma l
ly, bec ausePOSNi sat wo- by tevalue ,eac hs hiftwoul 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 heir sts hiftc annotpos si
blyg i
v ear esultg rea t
e rt ha n2 56.In
lookinga tl i
ne s3 7t hroug h4 1 ,youc ans eetha tline3 7doe stheir stmul tipl
yby
2. I
ti sthe nl i
ne s3 8thr oug h4 1t ha tdot hetwo- bytes hiftst og e ttheina lr e
sult
.
Re me mbe ra lsot ha ta nASLput st hebi tpus he doutt hee ndi ntot hec arryla g.
ha ta l
lowsROLt opi ckupt hec arr ywhe ns hifti
ngt hehi gh- or derby t e.
Cons ide rt hee xa mpl ei nt het a blebe lowt os eehowt hes hi ftswor k.hel et
-
terAha sa nAS CI Iv alueof$41( 6 5de cima l).Afte rs ubtra cti
ng$20( 3 2dec i
ma l
)
we ’
llha vear e sul
tof$21( 3 3de c ima l).Aftermul 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
tiveofs 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 oade da t$9000
(andpresumablypr otectedbys etti
ngHIMEM:36864) .
Oncethet a
bl epositi
oni scalculat
ed, thescr
ee nbytetobemodi iedmus tbe
calc
ulat
eda swe l
l.hi si sdonebyCALC2.Li nes53t hrough59t ak et hec ontents
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.he refore,wea ddCHt oma keBASL, BASHt headdressoft heir sthi-res
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
09

scr
e enby tetobemodi ie d.Not et ha ta na dde da dvantag eofthisa ppr oa chist 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 n.hi
e sisdonei nanumbe rofs t
ag e.heir
s s
tstepi st os ettheY-
Reg i
stert o#$00t opr e pa retor e trievet heda taby tesf rom thet able.G1t he n
sta
r t
st her etr
ievalloopbyg ett
ingt heir s tbyteoft hec hara
c t
erf rom t het a ble
ands toringitont hehi -re sscreen.
Nowhe re’
swhe rei tg etsinte resting .Nor ma lly
,t hene xtste pswoul dbet o
increme ntYt og etthene xtchar acterf rom t het able
, andt oa l
soa dd$400t ot he
POSNv al
uet oa cc
e s
st hene xthor iz ont allineont hes creen.hepr oble mi stha t
,
ifYc ha ng e
s,wewon’ ta ccessthel inedi rectlybe l
owt heonewej us tmodi ie d,
butrat heroneby tetot her ightofwhe rewewa ntt obe .
hes ol
utionist oa dd$3FF, rathe rt ha n$400, toPOSN. ha twa yt hev alueof
POSNwi l
lg rowi nawa yc ompa ti
bl ewi tht hei ncreasedv a
lueoft heY- Re giste r
.
hi 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 .het
e echni quec a nbea 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 ra ct
ert obe
pri
nt eda ndt heY- Re g i
st ertoi tsor igina lvalue.hej umpt oCOUT1( $FDF0)i s
thendonet oc ompl etethepr i
ntingt ot henor ma ltextscr e
en. hea dv antage sof
thi
swe rediscussede arli
e r(ma inte na nceofBASL, BASH, CV,ands oon) .

AHi
-Re
sCha
rac
terSe
t
hewa yt ousethec haractergeneratoristoloa dtheassembledbinaryrou-
ti
nea t$300( 768decima l
).Ina nAppl esoftprogram, 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
.
he reis ,howe v
e r
,onemi norde t
ails t
il
lmi s
sing.ha 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.
hi sda ta,alt
houghl engthy ,wil
lpr ovideyouwi thac ompl et
echa r
acte
rs e
t
tobel oadeda t$9000.Al t
houg hi twill
tak eawhi l
etoe nt
ertheda ta
,itwil
lprob-
ablybeal ittleeasi
ertha ncrea ti
nge achc haract
erwi t
ha ne dit
or,al
thoughy ou
willhavethatoppor tunityinthene xtcha pter
.

]
3
10 As
sembl
yLi
nes

1 ********************************
2 * AL31-ASCII CHARACTER SET *
3 ********************************
4 *
5 ORG $9000
6 *
9000: 00 00 00 00 00 00 00 00 7 HEX 0000000000000000 ; SPACE
9008: 08 08 08 08 08 00 08 00 8 HEX 0808080808000800 ; !
9010: 14 14 14 00 00 00 00 00 9 HEX 1414140000000000 ; "
9018: 14 14 3E 14 3E 14 14 00 10 HEX 14143E143E141400 ; #
9020: 08 3C 0A 1C 28 1E 00 00 11 HEX 083C0A1C281E0800 ; $
9028: 06 26 10 08 04 32 30 00 12 HEX 0626100804323000 ; %
9030: 04 0A 0A 04 2A 12 2C 00 13 HEX 040A0A042A122C00 ; &
9038: 0B 00 06 00 00 00 00 00 14 HEX 0808080000000000 ; ’
9040: 08 04 02 02 02 04 08 00 15 HEX 0804020202040800 ; (
9048: 08 10 20 20 20 10 08 00 16 HEX 0810202020100800 ; )
9050: 08 2A 1C 08 1C 2A 08 00 17 HEX 082A1C081C2A0800 ; *
9058: 00 08 08 3E 08 08 00 00 18 HEX 0008083E08080000 ; +
9060: 00 00 00 00 00 00 08 04 19 HEX 0000000000000804 ; ,
9068: 00 00 00 3E 00 00 00 00 20 HEX 0000003E00000000 ; -
9070: 00 00 00 00 00 00 08 00 21 HEX 0000000000000800 ; .
9078: 00 20 10 08 04 02 00 00 22 HEX 0020100804020000 ; /
9080: 1C 22 32 2A 26 22 1C 00 23 HEX 1C22322A26221C00 ; 0
9088: 08 0C 08 00 08 08 1C 00 24 HEX 080C080808081C00 ; 1
9090: 1C 22 20 18 04 02 3E 00 25 HEX 1C22201804023E00 ; 2
9098: 3E 20 10 18 20 22 1C 00 26 HEX 3E20101820221C00 ; 3
90A0: 10 18 14 12 3E 10 10 00 27 HEX 101814123E101000 ; 4
90A8: 3E 02 1E 20 20 22 1C 00 28 HEX 3E021E2020221C00 ; 5
90B0: 18 04 02 1E 22 22 1C 00 29 HEX 1804021E22221C00 ; 6
90B8: 3E 20 10 08 04 04 04 00 30 HEX 3E20100804040400 ; 7
90C0: 1C 22 22 1C 22 22 1C 00 31 HEX 1C22221C22221C00 ; 8
90C8: 1C 22 22 3C 20 10 0C 00 32 HEX 1C22223C20100C00 ; 9
90D0: 00 00 08 00 08 00 00 00 33 HEX 0000080008000000 ; :
90D8: 00 00 08 00 08 08 04 00 34 HEX 0000080008080400 ; ;
90E0: 10 08 04 02 04 08 10 00 35 HEX 1008040204081000 ; <
90E8: 00 00 3E 00 3E 00 00 00 36 HEX 00003E003E000000 ; =
90F0: 04 08 10 20 10 08 04 00 37 HEX 0408102010080400 ; >
90F8: 10 22 10 08 08 00 08 00 38 HEX 1C22100808000800 ; ?
9100: 1C 22 2A 3A 1A 02 3C D0 39 HEX 1C222A3A1A023CD0 ; @
9108: 08 14 22 22 3E 22 22 00 40 HEX 081422223E222200 ; A
9110: 10 22 22 1E 22 22 1E 00 41 HEX 1E22221E22221E00 ; B
9118: 1C 22 02 02 02 22 1C 00 42 HEX 1C22020202221C00 ; C
9120: 1E 22 22 22 22 22 1E 00 43 HEX 1E22222222221E00 ; D
9128: 3E 02 02 1E 02 02 3E D0 44 HEX 3E02021E02023E00 ; E
9130: 3E 02 02 1E 02 02 02 00 45 HEX 3E02021E02020200 ; F
9138: 3C 02 02 02 32 22 3C 00 46 HEX 3C02020232223C00 ; G
9140: 22 22 22 3E 22 22 22 00 47 HEX 2222223E22222200 ; H
9148: 1C 08 06 08 08 08 1C 00 48 HEX 1C08080808081C00 ; I
9150: 20 20 20 20 20 22 1C 00 49 HEX 2020202020221C00 ; J
9158: 22 12 0A 06 0A 12 22 00 50 HEX 22120A060A122200 ; K
9160: 02 02 02 02 02 02 3E 00 51 HEX 0202020202023E00 ; L
9168: 22 36 2A 2A 22 22 22 00 52 HEX 22362A2A22222200 ; M
9170: 22 22 26 2A 32 22 22 00 53 HEX 2222262A32222200 ; N
9178: 1C 22 22 22 22 22 1C 00 54 HEX 1C22222222221C00 ; O
9180: 1E 22 22 1E 02 02 02 00 55 HEX 1E22221E02020200 ; P
9188: 1C 22 22 22 2A 12 2C 00 56 HEX 1C2222222A122C00 ; Q
3
1.Hi
-Re
sCha
rac
terGe
ner
ator 3
11

9190: 1E 22 22 1E 0A 12 22 00 57 HEX 1E22221E0A122200 ; R


9198: 1C 22 02 1C 20 22 10 00 58 HEX 1C22021C20221C00 ; S
91A0: 3E 08 03 08 03 08 08 00 59 HEX 3E08080808080800 ; T
91A8: 22 22 22 22 22 22 1C 00 60 HEX 2222222222221C00 ; U
91B0: 22 22 22 22 22 14 06 00 61 HEX 2222222222140800 ; V
91B8: 22 22 22 2A 2A 36 22 00 62 HEX 2222222A2A362200 ; W
91C0: 22 22 14 08 14 22 22 00 63 HEX 2222140814222200 ; X
91C8: 22 22 22 14 03 08 03 00 64 HEX 2222221408080800 ; Y
91D0: 3E 20 10 08 04 02 3E 00 65 HEX 3E20100804023E00 ; Z
91D8: 3E 06 06 0E 06 06 3E 00 66 HEX 3E06060606063E00 ; [
91E0: 00 02 04 06 10 20 00 00 67 HEX 0002040810200000 ; \
91E8: 3E 30 30 30 3D 30 3E 00 68 HEX 3E30303030303E00 ; ]
91F0: 00 00 08 14 22 00 00 00 69 HEX 0000081422000000 ; ^
91F8: 00 00 00 00 00 00 00 7F 70 HEX 000000000000007F ; _
9200: 04 08 10 00 00 00 00 00 71 HEX 0408100000000000 ; ’
9208: 00 00 1C 20 3C 22 3C 00 72 HEX 00001C203C223C00 ; a
9210: 02 02 1E 22 22 22 1E 00 73 HEX 02021E2222221E00 ; b
9218: 00 00 3C 02 02 02 3C 00 74 HEX 00003C0202023C00 ; c
9220: 20 20 3C 22 22 22 3C 00 75 HEX 20203C2222223C00 ; d
9228: 00 00 1C 22 3E 02 3C 00 76 HEX 00001C223E023C00 ; e
9230: 18 24 04 1E 04 04 04 00 77 HEX 1824041E04040400 ; f
9238: 00 00 1C 22 22 3C 20 1C 78 HEX 00001C22223C201C ; g
9240: 02 02 1E 22 22 22 22 00 79 HEX 02021E2222222200 ; h
9248: 08 00 0C 08 08 08 1C 00 80 HEX 08000C0808081C00 ; i
9250: 10 00 18 10 10 10 12 00 81 HEX 100018101010120C ; j
9258: 02 02 22 12 0E 12 22 00 82 HEX 020222120E122200 ; k
9260: 0C 03 08 0B 08 08 1C 00 83 HEX 0C08080808081C00 ; l
9268: 00 00 36 2A 2A 2A 22 00 84 HEX 0000362A2A2A2200 ; m
9270: 00 00 1E 22 22 22 22 00 85 HEX 00001E2222222200 ; n
9278: 00 00 1C 22 22 22 1C 00 86 HEX 00001C2222221C00 ; o
9280: 00 00 1E 22 22 1E 02 02 87 HEX 00001E22221E0202 ; p
9288: 00 00 3C 22 22 3C 20 20 88 HEX 00003C22223C2020 ; q
9290: 00 00 3A 06 02 02 02 00 89 HEX 00003A0602020200 ; r
9298: 00 00 3C 02 1C 20 1E 00 90 HEX 00003C021C201E00 ; s
92A0: 04 04 1E 04 04 24 18 00 91 HEX 04041E0404241800 ; t
92A8: 00 00 22 22 22 32 2C 00 92 HEX 0000222222322C00 ; u
92B0: 00 00 22 22 22 14 08 00 93 HEX 0000222222140800 ; v
92B8: 00 00 22 22 2A 2A 36 00 94 HEX 000022222A2A3600 ; w
92C0: 00 00 22 14 08 14 22 00 95 HEX 0000221408142200 ; x
92C8: 00 00 22 22 14 08 08 06 96 HEX 0000222214080806 ; y
92D0: C0 00 3E 10 08 04 3E 00 97 HEX 00003E1008043E00 ; z
92D8: 38 0C 0C 06 0C 0C 30 00 98 HEX 380C0C060C0C3800 ; {
92E0: 08 0B 03 08 08 08 08 08 99 HEX 0808080808080808 ; |
92E8: 0E 18 1B 30 18 18 0E 00 100 HEX 0E18183018180E00 ; }
92F0: 2C 1A 00 00 00 00 00 00 101 HEX 2C1A000000000000 ; ~
92F8: 7F 7F 7F 7F 7F 7F 7F 7F 102 HEX 7F7F7F7F7F7F7F7F ; CURSOR
9300: 6F 103 CHK

Asasi
denote,thi
sisanoddpr
ogr
aminthati
tdoes
n’tact
ual
lydoa ny
thi
ng.
I
tjus
tcre
atesadatatable
.Asse
mblei
tany
wa yandsa
vet heobj
ectcodeunder
t
henameAL31.ASCII.

]
3
12 As
sembl
yLi
nes

Totes
ta l
lofthi
sout,youcanus ethi
ss i
mpleAppl
esoftpr
ogram.1You
probabl
yshoul
dverif
ythaty
ouc a
na tl
eastge
tthismuc
htowor kbef
orediv
ing
inandtryi
ngtousetherout
ine
sfrom wit
hinyourownass
embly-l
anguag
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
Atthispointyous houl dfeelfa
irl
yc omfor t
ablewiththeideaofhowahi -
res
charact
erg enera
torwor ks.hei deaspr e
sentedhe rer e
lyheavilyonag eneral
degreeoffami l
iari
tywi thav ari
etyofte c
hni quesdiscuss
edi ne ar
li
erc hapter
s,
specic
i all
y,outputv ectorus eandi nte
rc e
ption,me moryma ppingoft hehi -
res
andt extscr
e e
ns,a ndofc ourseg ener
alt e
chni quesofa ss
embly -l
anguagepr o-
grammi ng.Ifyoua r
eha vi
ngdi ic ul
tyina nyoft hes
ea reas
,youma ywi sht o
revi
ewpr evi
ousc hapters.
Allinall,
yous houldindt hea pproachs hownhe retobemuc he asie
rtha n
youir stthought.hes imilari
ti
esbe t
we ent het extandhi -
ress c
reensg reat
ly
reducetheamountofdi ic ult
yi ncreat
ingac ha ra
ctergenerat
or.
Inthene xtchapter,we ’l
ldevelopac hara c
teredit
ort ocreateyourownhi -
rescharact
erfonts( t
het erm usedforthec haracterdesig
n),anda ls
otak eabr i
ef
l
ooka thow hi -resgraphicsi na r
cade-sty
leg ame scant akeadvantageoft hese
samet e
chniquestoc reateawi devarie
tyofe fe c
ts.

1[
CT]ForPr
oDOS
,wema
nua
ll
ycha
nget
heout
putv
ect
ora
t$BE30,$BE31t
opoi
ntt
o
ENTRY(
$30B)
.Se
efoot
not
e1i
ncha
pte
r29f
ormor
edi
scus
sion.
3
2
3
2. Hi
-Re
sChar
act
erEdi
tor
Ma
y19
83

Inthepr e viousc ha pte rwepr ese nt edal isti


ngf orahi -rescha ra cte rg ene ra
-
tora longwi tht het heor ybe hindi tsope ration.heg ene r
a t
orus eda ne x ist
ing
cha racterset, loade da tloc ation$9000i nme mor y,a ndc ont a
inedt heda t afor9 6
AS CI Icha racte r
s .
Toc rea tey ourownc ha racte rse t,a llthati sne ede di saut il
ityf ore ditingt he
ex i
stingc ha rac t
e rs etandc rea t
ingt hene wf ont ,orc ha ract
e rde sig n,t ha ty ou
de si
re .
Beforepr esent ingthel i
s t
ingf ort hec ha ractere ditor ,cons i
de rf oramome nt
thei nforma tiona ndt ec
hni que st ha tmus tbepr ov i
de df or .hi si sav e ryi mpor -
tantpa rtofs olvinga nypr obl em, prog rammi ngorot he rwi se,andi si nstr ume n-
talindi rectinga ndc l
ari
fy ingone ’sthoug htpr oce ss
e s.
Indi scus singt hec ha ra cters et,y ou’ llre calltha te achc hara cte ri sr epre-
sent e
dbyas er i
esofe ightby t
e si nt het able,a ndt ha te a chdoti nt hec ha ract
er
ima gei srepr e sentedbyabi twi thi noneoft hos eby tes.heir s
tt woc ons i
de ra
-
tions, t
he refore ,arehowt oa ddr esst hes eriesofby test ha tcor r
espondt oag iven
AS CI Icha racte ra ndhowt oi de ntifya nda lt
e rthebi tc orrespondi ngt ot hepa r
-
ticulardoti nt hec ha r
acteri ma get ha twewi s
ht omodi fy .
Ine dit
inge a chc harac ter, wewi llwa ntt obea blet ot urnag ive nbi tonorof
(setitto1or0 )a ndt omov eac ur sorf rom onebi ttoa not her.You’ lla l
s or ecal
l
from t hepr e viousc haptert ha te achby teoft hec ha ract er’
sda tac or re spondst o
onel ineofi tsima geont hes cree n. Wi t hine a chby te, sev enbi t
sa reus e dt oma p
thes evens cr eendot suse dt og ene rateag ivenl i
neofac ha ra c
ter.
Whe nwee ditt heindi vidua l scre endot s
, i
twoul dbeni ceifwec oul dus ethe
standa rda rrowk eyst omov et hec urs ora roundi naboxc ont ai
ni ngt hec ha r
ac-
terima g e.
Spea ki
ngoft hec ha r
a cte rbox ,s omet houg htwi llha vetobeg i
v ent ohow
thee ntirec ha racteri t
selfwi ll bedi s
pl ay ed. Wec ouldj us tpr i
ntthec ha racteron-
screene a cht imeamodi ic ationi sdone ,butbe ca useoft hes ma llsiz et hiswoul d
be comet e
di ousa fterawhi le, Abe ttera ppr oa chwoul dbet odispla yama g niied
ima geoft hec ha racter,uponwhi chourc ursorc a nbepos it
ione dtoe di ta nypa r
-
ticularbitint heov eral
lima g e.
Tous et hee ditor,we ’
lla lsoha v et obea blet os pe ci
fywhi chc ha ra cterwe
wa ntt oe dit,a ndt henl atert os ignifyt ha twea r edone .Tok eept hi ng ss impl e,

]
3
14 As
sembl
yLi
nes

we’l
lsel
e c
tac hara
cterbypre s
singtheequiv
ale
ntk eyandstor
ethecompl e
ted
imageba c
ki nthecha r
acte
rtablewhen<RETURN>ispres
sed.
Loa di
nga nds avingofthec omplet
etabl
eisnotpr ovi
dedforint
hee di
tor
butcanbea ccomplishedeasi
lyfrom t
heimme di
atemodewi t
hBLOADandBSAVE.
Moreont ha tl
a t
er.
He re,then,isthec omplet
el i
st
ing,whichwillbeexpl
aine
di ndet
ail
.1See
youatthebot t
om!
1 ********************************
2 * AL32-CHARACTER EDITOR *
3 * 2/7/1983 *
4 ********************************
5 ORG $8000
6 CSW EQU $36
7 BASL EQU $28
8 CV EQU $25
9 CH EQU $24
10 CR EQU $06
11 CC EQU $07
12 MASK EQU $08
13 CHR EQU $09
14 TABLE EQU $9000
15 POSN EQU $3C ; (BAS2)
16 SCRN EQU $3E ; (A4)
17 VECT EQU $3EA
18 COUT EQU $FDED
19 COUT1 EQU $FDF0
20 HGR EQU $F3E2
21 HCOLOR EQU $F6F0
22 HPLOT EQU $F457
23 HLIN EQU $F53A
24 X1 EQU $22 ; 34
25 X2 EQU $54 ; 84
26 Y1 EQU $17 ; 23
27 Y2 EQU $58 ; 88
28 VTAB EQU $FC22
29 RDKEY EQU $FD0C
30 BELL EQU $FBDD
31 B1 EQU %10101010
32 B2 EQU %01010101
33 *
34 CURDAT EQU $FFFF
35 *
8000: A9 81 36 HOOK LDA #HCOUT ; PRODUCES LOW BYTE
8002: 85 36 37 STA CSW
8004: A9 81 38 LDA #>HCOUT ; #> PRODUCES HIGH BYTE
8006: 85 37 39 STA CSW+1
8008: 4C DD FB 40 JSR VECT
41 *
800B: 20 E2 F3 42 ENTRY JSR HGR
800E: A9 00 43 LDA #$00
8010: 85 06 44 STA CR ; CR=0

1[
CT]Li
nes2
10a
nd2
18−2
22we
r ie
emodi dt
oal
lowy
out
opr
ess<CTRL>Qt
oqui
t.
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
15

8012: 85 07 45 STA CC ; CC=0


8014: EA 46 TITLE NOP
47 *
8015: A9 03 48 CHRLIST LDA #$03
8017: 85 25 49 STA CV
8019: 20 22 FC 50 JSR VTAB
801C: A2 20 51 START LDX #$20
801E: 8A 52 CH2 TXA
801F: 29 0F 53 AND #%00001111 ; 2^4 - 1
54 * RESULT = VALUE MOD 16
8021: D0 09 55 BNE CONT ; NOT MULT OF 16
8023: A9 8D 56 LDA #$8D
8025: 20 ED FD 57 JSR COUT ; PRINT RETURN
8028: A9 14 58 LDA #$14 ; MARGIN FOR NEW LINE
802A: 85 24 59 STA CH
802C: 8A 60 CONT TXA ; RESTORE CHAR
802D: 09 80 61 ORA #$80 ; SET HI BIT
802F: 20 ED FD 62 JSR COUT ; PRINT CHAR
8032: E8 63 NEXTC INX
8033: E0 80 64 CPX #$80
8035: 90 E7 65 BCC CH2
66 *
8037: A2 03 67 MATDSP LDX #$03
8039: 20 F0 F6 68 JSR HCOLOR
803C: A2 22 69 BOX LDX #X1 ; LOW BYTE
803E: A0 00 70 LDY #>X1 ; HIGH BYTE
8040: A9 17 71 LDA #Y1
8042: 20 57 F4 72 JSR HPLOT ; PLOT X1,Y1
8045: A9 54 73 LDA #X2
8047: A2 00 74 LDX #>X2
8049: A0 17 75 LDY #Y1
804B: 20 3A F5 76 JSR HLIN ; TO X2,Y1
804E: A9 54 77 LDA #X2
8050: A2 00 78 LDX #>X2
8052: A0 58 79 LDY #Y2
8054: 20 3A F5 80 JSR HLIN ; TO X2,Y2
8057: A9 22 81 LDA #X1
8059: A2 00 82 LDX #>X1
805B: A0 58 83 LDY #Y2
805D: 20 3A F5 84 JSR HLIN ; TO X1,Y2
8060: A9 22 85 LDA #X1
8062: A2 00 86 LDX #>X1
8064: A0 17 87 LDY #Y1
8066: 20 3A F5 88 JSR HLIN ; TO X1,Y1
8069: A9 03 89 MATD2 LDA #$03
806B: 85 25 90 STA CV
806D: 20 22 FC 91 JSR VTAB
8070: A0 00 92 GETROW LDY #$00
8072: A9 05 93 GR1 LDA #$05
8074: 85 24 94 STA CH
8076: B9 69 81 95 LDA MAT,Y
8079: A2 00 96 SCAN LDX #$00
807B: 4A 97 S1 LSR
807C: 48 98 PHA ; SAVE RESULT
807D: A9 A0 99 LDA #$A0 ; SPACE
807F: 90 02 100 BCC PRINTM

]
3
16 As
sembl
yLi
nes

8081: A9 FF 101 LDA #$FF


8083: 20 ED FD 102 PRINTM JSR COUT
8086: 68 103 PLA ; RESTORE ACCUM
8087: E8 104 NXTBIT INX
8088: E0 07 105 CPX #$07
808A: 90 EF 106 BCC S1
808C: A9 8D 107 LDA #$8D ; RETURN
808E: 20 ED FD 108 JSR COUT
8091: C8 109 NXTROW INY
8092: C0 08 110 CPY #$08
8094: 90 DC 111 BCC GR1
112 *
8096: 18 113 CURSOR CLC
8097: A5 06 114 LDA CR ; CURSOR ROW
8099: 69 03 115 ADC #$03
809B: 85 25 116 STA CV
809D: 20 22 FC 117 JSR VTAB
80A0: 18 118 CLC
80A1: A5 07 119 LDA CC ; CURSOR COLUMN
80A3: 69 05 120 ADC #$05
80A5: 85 24 121 STA CH
122 *
80A7: 20 C2 81 123 CURCALC JSR SCRNCALC
80AA: A4 06 124 STATUS LDY CR
80AC: B9 69 81 125 LDA MAT,Y
80AF: A6 07 126 LDX CC
80B1: 4A 127 ST1 LSR
80B2: CA 128 DEX
80B3: 10 FC 129 BPL ST1
80B5: 90 02 130 BCC CLEAR
80B7: B0 04 131 BCS SET
80B9: A9 00 132 CLEAR LDA #$00
80BB: F0 02 133 BEQ PRNTCURS
80BD: A9 08 134 SET LDA #$08
135 *
80BF: 18 136 PRNTCURS CLC
80C0: 69 71 137 ADC #CURSDATA ; LOW BYTE
80C2: 85 3C 138 STA POSN
80C4: A9 00 139 LDA #$00
80C6: 69 81 140 ADC #>CURSDATA ; HIGH BYTE
80C8: 85 3D 141 STA POSN+1
142 *
80CA: 20 D0 81 143 JSR PUTBYTE
80CD: 20 0C FD 144 CMD? JSR RDKEY
80D0: C9 A0 145 CMP #$A0
80D2: 90 12 146 BCC EDIT ; CTRL CHAR
80D4: 85 09 147 CHAR STA CHR
80D6: 20 9B 81 148 JSR POSNCALC
80D9: A0 07 149 LDY #$07
80DB: B1 3C 150 MOVE LDA (POSN),Y
80DD: 99 69 81 151 STA MAT,Y
80E0: 88 152 DEY
80E1: 10 F8 153 BPL MOVE
80E3: 4C 37 80 154 CHRX JMP MATDSP
155 *
80E6: C9 8D 156 EDIT CMP #$8D ; RETURN
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
17

80E8: D0 14 157 BNE E1


80EA: A5 09 158 ACCEPT LDA CHR
80EC: 20 9B 81 159 JSR POSNCALC
80EF: A0 07 160 LDY #$07
80F1: B9 69 81 161 XFER LDA MAT,Y
80F4: 29 7F 162 AND #$7F ; CLEAR BIT 7
80F6: 91 3C 163 STA (POSN),Y
80F8: 88 164 DEY
80F9: 10 F6 165 BPL XFER
80FB: 4C 15 80 166 XFX JMP CHRLIST
167 *
80FE: C9 9B 168 E1 CMP #$9B ; ESCAPE
8100: D0 18 169 BNE E2
8102: 38 170 TOGGLE SEC
8103: A6 07 171 LDX CC
8105: A9 00 172 LDA #$00
8107: 2A 173 SHFT ROL
8108: CA 174 DEX
8109: 10 FC 175 BPL SHFT
810B: 85 08 176 STA MASK
810D: A4 06 177 LDY CR
810F: B9 69 81 178 LDA MAT,Y
8112: 45 08 179 EOR MASK
8114: 99 69 81 180 STA MAT,Y
8117: 4C 37 80 181 TGX JMP MATDSP
182 *
811A: C9 8B 183 E2 CMP #$8B ; <CTRL>K
811C: D0 0B 184 BNE E3
811E: C6 06 185 UP DEC CR
8120: 10 04 186 BPL UPX
8122: A9 07 187 LDA #$07
8124: 85 06 188 STA CR
8126: 4C 37 80 189 UPX JMP MATDSP
190 *
8129: C9 8A 191 E3 CMP #$8A ; <CTRL>J
812B: D0 0F 192 BNE E4
812D: E6 06 193 DOWN INC CR
812F: A5 06 194 LDA CR
8131: C9 08 195 CMP #$08
8133: 90 04 196 BCC DX
8135: A9 00 197 LDA #$00
8137: 85 06 198 STA CR
8139: 4C 37 80 199 DX JMP MATDSP
200 *
813C: C9 88 201 E4 CMP #$88 ; <CTRL>H
813E: D0 0B 202 BNE E5
8140: C6 07 203 LEFT DEC CC
8142: 10 04 204 BPL LX
8144: A9 06 205 LDA #$06
8146: 85 07 206 STA CC
8148: 4C 37 80 207 LX JMP MATDSP
208 *
814B: C9 95 209 E5 CMP #$95 ; <CTRL>U
814D: D0 0F 210 BNE E6 ; [CT] CHECK FOR QUIT
814F: E6 07 211 RIGHT INC CC
8151: A5 07 212 LDA CC

]
3
18 As
sembl
yLi
nes

8153: C9 07 213 CMP #$07


8155: 90 04 214 BCC RX
8157: A9 00 215 LDA #$00
8159: 85 07 216 STA CC
815B: 4C 37 80 217 RX JMP MATDSP
815E: C9 91 218 E6 CMP #$91 ; [CT] <CTRL>Q TO QUIT
8160: D0 01 219 BNE ERR ; UNKNOWN CTRL CHAR
8162: 60 220 QUIT RTS ; [CT] QUIT PROGRAM
8163: 20 DD FB 221 ERR JSR BELL
8166: 4C CD 80 222 JMP CMD?
8169: 55 AA 55 223 MAT DFB B2,B1,B2,B1,B2,B1,B2,B1 ; WORKAREA
816C: AA 55 AA 55 AA
224 *
8171: 7F 225 CURSDATA DFB %01111111
8172: 41 226 DFB %01000001
8173: 41 227 DFB %01000001
8174: 41 228 DFB %01000001
8175: 41 229 DFB %01000001
8176: 41 230 DFB %01000001
8177: 41 231 DFB %01000001
8178: 7F 232 DFB %01111111
233 *
8179: 00 234 DFB %00000000
817A: 3E 235 DFB %00111110
817B: 3E 236 DFB %00111110
817C: 3E 237 DFB %00111110
817D: 3E 238 DFB %00111110
817E: 3E 239 DFB %00111110
817F: 3E 240 DFB %00111110
8180: 00 241 DFB %00000000
242 *
8181: C9 A0 243 HCOUT CMP #$A0
8183: 90 13 244 BCC OUT ; DON’T PRINT CTRL CHARS
8185: 48 245 PHA ; STORE CHAR
8186: 85 3C 246 STA POSN
8188: 98 247 TYA
8189: 48 248 PHA ; SAVE Y
249 *
818A: A5 3C 250 CALC1 LDA POSN ; GET CHAR
818C: 20 9B 81 251 JSR POSNCALC
252 *
818F: 20 C2 81 253 CALC2 JSR SCRNCALC
254 *
8192: 20 D0 81 255 PRINT JSR PUTBYTE
256 *
8195: 68 257 PLA
8196: A8 258 TAY ; RESTORE Y
8197: 68 259 PLA ; RESTORE CHAR
8198: 4C F0 FD 260 OUT JMP COUT1
261 *
819B: 29 7F 262 POSNCALC AND #$7F ; CLEAR HI BIT
819D: 85 3C 263 STA POSN
819F: A9 00 264 LDA #$00
81A1: 85 3D 265 STA POSN+1
81A3: 38 266 SEC
81A4: A5 3C 267 LDA POSN
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
19

81A6: E9 20 268 SBC #$20


81A8: 85 3C 269 STA POSN ; CHR < 96
81AA: 06 3C 270 ASL POSN ; *2 = CHR < 192
81AC: 06 3C 271 ASL POSN ; *4 < 384
81AE: 26 3D 272 ROL POSN+1
81B0: 06 3C 273 ASL POSN ; *8 < 768
81B2: 26 3D 274 ROL POSN+1
275 *
276 * POSN = (ASC - $20) * 8 BYTES PER CHAR
277 *
81B4: 18 278 CLC
81B5: A9 00 279 LDA #TABLE ; LOW BYTE
81B7: 65 3C 280 ADC POSN
81B9: 85 3C 281 STA POSN
81BB: A9 90 282 LDA #>TABLE ; HIGH BYTE
81BD: 65 3D 283 ADC POSN+1
81BF: 85 3D 284 STA POSN+1 ; POSN = POSN + TABLE ADDR
81C1: 60 285 RTS
286 *
81C2: 18 287 SCRNCALC CLC ; ENTER WITH BASL,CH SET UP
81C3: A5 28 288 LDA BASL
81C5: 65 24 289 ADC CH
81C7: 85 3E 290 STA SCRN
81C9: A5 29 291 LDA BASL+1
81CB: 69 1C 292 ADC #$1C
81CD: 85 3F 293 STA SCRN+1 ; SCRN = BASL + CH + $1C00
81CF: 60 294 RTS
295 *
81D0: A0 00 296 PUTBYTE LDY #$00 ; ENTER WITH POSN,SCRN SET UP
81D2: B1 3C 297 G1 LDA (POSN),Y
81D4: 91 3E 298 STA (SCRN),Y
81D6: C8 299 INC INY
81D7: 18 300 CLC
81D8: A5 3E 301 LDA SCRN
81DA: 69 FF 302 ADC #$FF
81DC: 85 3E 303 STA SCRN
81DE: A5 3F 304 LDA SCRN+1
81E0: 69 03 305 ADC #$03
81E2: 85 3F 306 STA SCRN+1 ; SCRN = SCRN + $3FF
307 * $3FF TO MAKE UP FOR GROWING VALUE
308 * OF 'Y'
309 *
81E4: C0 08 310 DONE? CPY #$08
81E6: 90 EA 311 BCC G1 ; NO
81E8: 60 312 YES RTS
81E9: E7 313 CHK

Afte
rass
embli
ngthelis
ti
ng,BLOADthecha
rac
tersetf
rom c
hapter31atl
oca-
ton$9000.he
i nBLOAD t
hecharact
eredit
orat$8000 (
donotBRUN)a ndtype
CALL32768from Apple
softor8000Gfr
om theMonitor(Appl
esoftmustbethe
sel
ecte
dl a
nguag
e)2
.

2[
CT]Ane
xampl
eBAS
ICpr
ogr
ami
sgi
vena
tthee
ndo
fthec
hapt
er.

]
3
20 As
sembl
yLi
nes

Whe nt hepr ogrami sc all


ed,t hes cre
enwi llcleara ndaboxwi thama tr
ix
patte
rni nsidei twi llappe ar ,al
ongwi t
ht hec ompl e t
ec haractersetloadeda t
$9000.Ifthec hara cte
rsa ppe arscr ambl e
d, r
echeckt oma kesurey ouhav el
oa ded
thecharacters etpr ope r
lya t$9000.
Tos el
ec tac ha r
actert oe dit
, simplypr es
sanynon- contr
olk ey.Ane nlarged
ima g
eoft ha tc ha racters houl da ppea rinthebox .Tomov ethee dit
ingc ursor
around,us et hel eftandr i
g htarr owst omov el efta ndr i
ght,a nd<CTRL>Ja nd
<CTRL>Kt omov eupa nddown.I fy ouha veanAppl e/ /
e,thef ourdirect
iona l
arrowswi l
la l
s owor k.Eve nonas tanda r
dAppl eI I
, youma yindi teasi
ertohol d
downt he<CTRL>k eywi tht helitt
leing erofyourle fthanda ndt henpresstheH,
U, J,
andKk ey swi thyourr i
g htha ndt omov earound.
Pressi
ng<ESCAPE>wi lltogg l
ebi tsi nthec hara ct
erona ndof.Tos avea
charact
erba ckt ot hetable,pr ess<RETURN>. I
fyouwa nttostartoverwithac ha r
-
acter
,si
mpl ypr esst heorig i
na lletterkeya gai
n.
Tos avet hea lteredtabl eba ckt odisk,si
mpl ypr ess<CTRL>Q, andthent ype:
BSAVE TABLENAME, A$9000, L$300

Youca
nre
pla
ceTABLENAMEwi
tha
nyna
mey
ouwi
sht
ogi
vet
hene
wcha
rac
-
t
ers
et.

Howi
tWor
ks
Alt
houghthel i
sti
nglooksr
athe
rlong ,don’tbedisc
ourag
ed.Asi thappens
,
muchofthelis
tingconsi
stsofr
outi
ne sthatwer epres
ente
dine a
rli
erc hapt
ers
.
Forex
ample,
lines2 4
3through313(HCOUT)aret hechar
act
ergene
ratorthatwas
des
cri
bedinchapter31.
Toseehow t heedit
orworks,l
etsir
’ stcons i
dert
hisover
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.
hisi
sal
sotheent
rypoi
ntf
orthee
dit
ingl
oopf
ore
achc
har
act
er.hi
sse
c-
t
ionc
anbebrok
endownasf
oll
ows:
heAppl
BOX: esofthi
-r
esr
out
ine
sar
eus
edt
odr
awab
oxwi
thf
ours
tra
ight
l
ine
s.hi
sfor
mst
hebounda
ryoft
hema
tri
xar
ea.
3
2.Hi
-Re
sCha
rac
terEdi
tor 3
21

GETROW:Ea
chby
teoft
hema
tri
xpa
tte
rni
sre
tri
eve
dhe
re fte
,a rwhi
chSCAN
wi
ll
proc
essa
nddi
spl
ayt
hei
ndi
vi
dua
lbi
ts
.
SCAN:hissec
ti
ons ftse
hi achbitoft
herowint
otheca
rrya
nd,
depe
ndi
ngon
whethe
rit’
sset
,di
spl
aysasol
idoranemptybl
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:hi
spa
rt,a
longwi
thPRNTCURS,de
ter
mine
swhe
the
rthebi
tatt
he
cur
sorpos
it
ionisse
t.I
fiti
sse
t,awhi
tec
urs
ori
spr
int
ed;i
fnot
,anout
li
ne
oft
hecurs
orisdi
spl
aye
d.
CMD?:Att
hispoi
ntwearerea
dyt
ogetac
omma ndfr
om thekeyboar
d.hegen-
e
ralt
heoryi
store
fres
hthes
cre
enwit
htherout
ine
si nMATDSPeac
hti
mea
c
ommandisent
erd.ha
e twa
ywedon’
tha
vet
oupda
teonl
ypa
rtoft
he
s
cre
ens
pec
iic
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
olkeyotherthant hele
galc
omma ndc
har
act
erswi
llg
ener
-
ateaBELLsound.I
na nycase
, afte
rak e
yisent
ere
d,aj
umpismadebac
kto
MATDSPtos
tartt
heproces
sove ra g
ain.

AndNowwi
tht
heMa
gni
fyi
ngGl
ass
hepr 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
entedherefortheir stti
me .Youma ywis
ht oref
erto
pr e
vioussecti
onsifsomepa r
tsseem diic ult
.Tohe lpyous canthr
oug htojus
t
thepa rt
sthatint
eres
tyou,eachsect
ioniske yedtotheprecedingove
rvie
w.

]
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
hi si
sthemainent
rypoi
nttot
heedit
or;
itse
rve
stocl
eart
hehi-
resscr
een
andini
ti
al
izet
hecolumnandrowposi
ti
onofthecur
sort
o0,0(uppe
r-l
eftc
or-
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 e
s t
heticpl aceme ntisde s
irable.hef 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 tini she danot herlineof1 6c haracters.
Asi thappens,t heANDi nstr uctionc anbeus edtope rf
ormt hee quival
entofa
modul of oranypowe rof2 .het echniquei st odoa nANDwi t
ht hev aluey ou
wa ntminus1 .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 enini she d,ac arria
g er 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.
hi sistoma keCOUTha ppy ,asi ta lwa ysexpe ctsthehi ghbi tt obes etonc hara c-
terstobepr i
nted.
MATDSP
hi ssecti
onbe ginsthepartthatc re
atesthema t
rixdisplayusedi ne di
ting
theindividualc haracte
rs.hi ssecti
onwi llbee xecutede achtimeac omma nd
char
a ct
erisente red.
heir stpa r
t,BOX,d r
awsaboxout lini
ngt hec haracterimag eusingt he
ApplesoftHLINr outines.
Onc etheboxi sdrawn,theindividualbytesmus tbedi s
playedwi ththest a-
tusofe achbi tindi c
a t
ed.hea 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

appr oachs houldwor k.( Not ethatifyoue di tthespa ceorther uboutc harac
ter
,
thema tr
ixpa tt
e r
nwi ll bea l
ter
eda c
cording l
y .)
he rea reatot alofe ightby t
estober etrievedanddi spla
y edfore achc hara
c-
ter.GR1i st hes e
c ti
ont ha tdoesthee quivale ntofanHTAB5( forpr ope rscr
een
placeme nt )andt henl oa dsaby t
efrom thewor kareaMAT( seeline2 25).Onc ea
bytei sretrieve
d, SCANu se stheLSRinstructiont oshiftabitintot hecar rylag.I
f
thec arryiss e
t,ar ubout( $FF) i
spri
nt e
d; othe rwis
eas pace($A0) isprinted.
Becaus etheAc cumul a
torwi l
lbeus edt oprintac haracterviaCOUT,t he
shiftedby teispreservedbypus hingitont ot hestackonl i
ne9 8a ndl aterpull
ing
itba ckofonl ine1 03.
Aftere ac
hs ev e
nbi tsa r
e“ pri
nted,
”ac arr i
agereturnisprinte donl ines107
and1 08a ndthel oopi sr epea t
eduntilal
l ei
g htby t
esha vebeendi spla
y ed.
CURSOR
Onc ethec hara cterma t
rixha sbee ndisplay
e d, wene edtodi s
playthec urs
or.
Lines115t hroug h1 23us ethec ursorr owa ndc ol umn( CRa ndCC)t ocalcul
a t
e
theHTAB, VTABp os i
tion. Reme mbe rtha ts
incewea remi rror
ingac t
ionstakenon
thetextpa gewec ana l
sous et het extpa geasaf r
a meofr ef
erenceforhi -
res
scre
enope r
ations.
STATUSi susedt or e
adthepa rt
icularbitthatc orrespondst othecurrentcur-
sorposit
ion. Not et hatCR( CursorRow)c onvenient lyisequaltowhi cheverby t
e
i
nt heindi vi
dua lc hara c
terdeini t
ionwewi l
lne edt or ead,andt hatCC( Cursor
Column)de termine show ma nybi tsne edt obes hiftedoutt oputt heoneof
i
nt e
restintot hec arryla g.De pendi ngonwhe the rthebi tisc l
earors et,the
Ac c
umul atorwi l
l bel oadedwi tha$00or$08, thepur poseofwhi chwi l
lbecome
i
mme diatel
yobv ious .
PRNTCURS
Si
nceCHa ndCV( $24,$25) hav ebe e nsetup, wec anuseas pec
ialform ofthe
HCOUTrouti
ne,calle
dPRNTCURS, topr intas ma ll
erblockorabl ockoutline.You’l
l
noti
cethatthehi-rescharacterg enera tora tHCOUTha sbeenmodi ieds l
ig
htlyto
usethepointe
rPOSN( $3C,$3D)t opoi ntt ot hedatat a
ble.Ouror i
gi
na lcharact
er
generat
oralwaysa s
sume dthatthet a blewoul dbea t$9000. Normall
yHCOUTs et
s
POSNtopointat$9000onl ines2 78t hr ough2 85
.
WithPOSNse tuptopoi ntatas pec ial
two- characte
rde ini
tiontableonlines
227through2 43,thePUTBYTEr outinewi lldot hee quiv
alentofprintingoneof
thetwone c
essa
ryspecialcharact
ersatthecur sorposi
tion.
Youma ywi shtoc ompa retheHCOUTr outi
nec ontainedint heedi
torwith
theprevi
ouschapter’
sc haract
ergeneratortos eewha tcha nge
sha vebeenma de
tofaci
li
tat
ethecall
ingbyt hePRNTCURSr outine.
An inte
rest
ing digressi
on:By a voidingCOUT a nd wr it
ingt othescreen
dir
e c
tl
y,wea r
eont heve r
geofbe i
nga bletodobl ocksha pes,
at e
chniqueuse
di n
ma nyhi-
resarca
de-typega mes.

]
3
24 As
sembl
yLi
nes

CMD?
hepr oce
ssingoft hec omma ndc ha r
a ct
ersisdonei nt hiss ection.hec har-
acte
ri sreadfrom t hek eyboardus i
ngt heMoni torr outineRDKEY( $FD0C).hi s
routi
newi llpl
acet heAS CI Ival
ueforthek e
ypr essedi ntot heAc cumul ator.
heir stma jordi s
tincti
ont obema dei swhe the rac ont r
olc haracte
rha s
beenpr es
sed.Lines1 45a nd1 46dot hi
s ,
forwa rdi
nga nyc ont rolcha racterstothe
EDITs ec
tion.
I
fanon- c
ont rolcharacterhasbe e
npr e
ssed,theus e
rwa nt stoe ditthatchar-
acte
r.CHARa ndMOVEus et heAS CIIvalueoft heke ypr ess
e dt oc al
c ulatethepos i
-
ti
onoft heda t
aoft hatcha ra
cteri
nt het abl
e, t
henmov et ha tdatai ntot hewor k
area,MAT.Afterthemov e,aj umpi sma deba c
kt oMATDSPt or efresht 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: hi simpl ie sthattheus erwa ntstoa cc
e ptthec ha r
actera sdi spl
a yed
andc opyi tba c ki ntot hec harac tert able. hi sisdonebye ssentiall
yr eversingt he
proc essus edbyCHARa ndMOVE( lines1 4 7t hrough1 53).
To ggle:If<ESCAPE>i spress ed, thea ppr opriat
ebi tpositi
onmus tbes wi t
che d
toitsoppos it
ec ondi ton–oft
i oonoront oof. hi sisdonebyc r
eatingama sk
bytewi tht hepr ope rbi tset.Todot hi s,thec arrylagi ssetandt heAc c umul ator
loade dwi tha0 .Whe na nROLi sdone ,t hissetbitwi llbes hiftedthr oug ht he
Acc umul ator.Bydoi ngt heROLag ivennumbe roft ime s(determi nedbyCC)we
cans e tag ivenbi tint heMASKby te($08) .
Onc ethema skha sbe enc rea ted, 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 cktoMATDSPt or efresht he
displaya ndg e tthene xtc haracte r.
Cur sorc o ntro l
:Tomov et hec ursora round,we l
lus et hef ourdi r
e ct
iona l
keysont heAppl e/ /ek eyboard.Ev eni fy oudon’ tha vea/ /e,y ouc 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 ’llus ewi l
lbe<CTRL>H,<CTRL>U,<CTRL>J,a nd
<CTRL>K, f
orl eft,right, down, a ndupr e spe c
tivel
y.
hec odeonl ine s1 85t hr oug h2 1 9i sfairl
ys tra
ightforwa rd.heupa nd
down mot ionsa redonebyi nc reme nt i
ngorde c r
eme ntingt hec ur sorr ow
count e r
;l efta ndr i
g htmot i
onsa redonebyi nc r
eme ntingorde c r
eme 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
hef ol
lowingAppl
esoftpr
ogra
mc anbeuse
dt oloa
dac har
act
ers
et,
runt
he
c
har
act
eredit
or,andt
hensavethecha
ract
ers
etafte
rexi
ting
.3

10 PRINT CHR$(21): REM 40-COLUMN


20 PRINT "CHAR TABLE FILE, <RETURN> FOR DEFAULT": INPUT A$
30 IF LEN (A$) = 0 THEN A$ = "AL31.ASCII"
40 PRINT CHR$ (4);"BLOAD ";A$
50 PRINT CHR$(4);"BLOAD AL32.CHAREDIT,A$8000"
60 VTAB 21
70 PRINT "PRESS A KEY TO EDIT THAT CHARACTER"
80 PRINT "<CTRL>H,U,J,K OR ARROW KEYS TO MOVE"
90 PRINT "<ESC> TO TOGGLE A BIT, <RETURN> TO SAVE"
100 PRINT "<CTRL>Q TO QUIT AND SAVE TO A FILE";
110 REM IF DOS 3.3 THEN SET UP CSW VECTOR
120 IF PEEK(1002) = 76 THEN CALL 32768: GOTO 150
130 REM IF PRODOS, SET UP OUTPUT LINK AT $BE30,31
140 POKE 48688,129: POKE 48689,129: CALL 32779
150 TEXT: PRINT: PRINT
160 PRINT "FILENAME TO SAVE, OR <RETURN> TO EXIT": INPUT A$
170 IF LEN(A$) > 0 THEN PRINT CHR$ (4);"BSAVE ";A$;",A$9000,L$300"
180 END

CT]heAppl
3[ esoftpr
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)hema
. 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 eneratelowe 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-
t .he<ESCAPE> a
or nd<SHIFT>func tionswi l
lcontinuet owor kproper
ly,pre-
suma bl
ywi thnoi l
lefectsontheeditorr outines
.
Itiswor thnot i
ngtha tt
hecharac t
ers etsuseda ndc reatedb ythi
sedit
ora r
e
ide
nticalinf orma ttotheDOSTo o
lKi tAni ma t
ri
xc 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
hisconcludesourdi sc
ussi
onofhi-rescharacte
rgenerat
ionande dit
ing;i
t
shoul
dprovideyouwi ththebasi
cprinci
plesofthesetec
hniques.heideaca 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. he6
5C0
2
J
une1
983

hi sl as
tc ha pt erde a l
swi thane wv e rsionofourbe lov ed6 5
0 2mi c ropr oc es-
sorknowna st he6 5C0 2. Althoug hthec hipha sj ustbe enr eleasedwi thint hel ast
few mont hsa ndha sy e ttoindi tswa yi nt ot hema ins tre a
m ofc omput e rs,it
seemsl ik el
yt ha twe ’l
lbehe ari
ngmor ea boutt hisitemi nt heupc omi ngy ea r.
Be forej umpi ngr i
ghti ntoi tsne wf unc t
ions ,thoug h,l et’sir s
tg etal it
tle
backgroundi nfor ma ti
onoutoft hewa y.
he6 502wa sde s
igne dbyChuc kPe ddl ea ndBi llMe ns chofMOSTe c hnol -
ogy(nowowne dbyCommodor eBus ine ssMa chines)a nd,a soft hepr e sent ,7 0
percentofi t
sus ei sbyAppl e,At ari
, andCommodor e.1he c
ur rentma nuf a ctur-
ersoft he6 50 2a reRoc kwe llInterna t
iona l,MOSTe c hnol ogy ,andS yne rtek .As
some t
ime sha ppe nswi t
ht hesething s,thoug h, someoft hek eype rsonsi nv olve d
witht he6 502we ntt owor ka tane wc ompa ny ,We ste rnDe si
g nCe nt er 2hi
. s
compa ny ,the n,i st heor i
ginals our ceoft hene w6 5 C0 2c hip.Butt hes tor y
doesn’te ndt he r e.We ste r
nDe signCe nt erha sl ice
ns e dt hede s
ignt oa tl east
threei nde pe nde ntma nuf act
ure r
s:Roc kwe l
lI nter
na tiona l
,GTE,a nd NCR.
he sec ompa ni est ookt hei nit
ial6 5C0 2de s i
g na nda dde dt heirowne nha nc e-
me nt
s .
hepi ctur ea tt hispoi ntisthate achoft hesec ompa ni eswi llbema rk etingi ts
ownv ersionoft he6 5C0 2. hec hipsa r
emor eorl essthes a me ,butt heRoc k we l
l
chipha st hela rg esti nst
r ucti
ons e t
.
“La rg e
sti nst ruc ti
ons e t
,”youa sk ?Ye s!hene w6 5C0 2ha sha dtheol d6 50 2
inst
ruc tions eta ppe nde dwi thav a ri
etyofne wi nstruc tions .Be causet heRoc k-
we l
lchi pa ppe a rst obeas upersetofa lloft heot he rchi ps ,thebul koft hisc ha p-
terwilla ssumet ha tit’
st heonet hat’
sbe ingus ed. Atthee ndoft hischa pt erwe ’l
l
descri
bedi fe renc e sa mongt hethre echi ps.
heRoc kwe llc hipha sat otaloft we lvene wi ns truc ti
onsa ndt wone w
address i
ngmode s.I na ddi t
ion,anumbe rofa ddressingmode snotpr e vious ly
avail
abl et oani ns truction( sucha st hei mme diatemodef ort heBITi nstr uc ti
on)
arenowa vailable . he rea r
eat otalof5 9a c t
ua lne wopc ode s.heme aningofa l
l
ofthesenumbe rswi llbe comec l
ears hortly .

1[
CT]Cor
rec
tedf
rom t
heor
igi
nala
rti
cl
e,whi
chl
is
tedCommodor
eBus
ine
ssMa
chi
nes
a
stheor
igi
nal
des
igne
r.
2[
CT]S
pec
iic
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 a rl
ierdiscuss
ionst hateach6 50
2i nst
ructi
onha supt os i
xaddress
ing
mode s.ha tnumbe risarri
v edatbycountingsomemode sasme revar
iat
ionsof
othersa ndnoti ncludingthev a
lue( r
ela
tiv
ea ddres
sing)associat
edwithbranch
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
heir stofthet wone wa ddressi
ngmode sisqui teeasytoe xpl
a i
nbe causeit
ise s
senti
allyanothe rvaria
tionofa ne xi
sti
ngmode .hene w modei si ndire c
t
addressi
ng.hi sma ys oundv eryfami l
iarbe causet hisiss i
mi lartot hei nstruc-
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 ddress
indicat
edbyt hez e
ro- pagepointertode t
ermi net hea ddressofi nterest
.
hi sisinef ora ddress
ingal ar
g etabl
eofda ta,butma nyt ime swea reinter-
estedinonl yoneby t
eofme morya ndmus tthe ng ot hroug htheobl i
gat oryLDY
#$00 topr operlyc onditi
ont heY- Re gis
ter
.( S
e ee ntrie
s5a nd6i nt het able
abov e
.)
hene wi nstr
uc ti
ona ll
owsust oignoret hec ont entsoft heY- Re gisterand
ga i
nac c
esstotheme mor ylocat
iondi rect
ly.hi sc ons erv
e stwoby tesofc odef or
ea c
hr ef
erence,becausetheY- Regist
e rdoesnotha 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)

hi
sne
waddr
ess
ingmodei
sav
ail
abl
efort
hei
nst
ruc
ti
onsl
is
tedbe
low.
3
3.he6
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
hes 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 ef
erenc ebefor eusingt he
sum oft hos enumbe r
st ode termi newhi chz ero-pa gepa irt ous e
.Fore xampl e,
theins t
r uc t
ionLDA($22,X), whe ret heX- Re g
isterhe ldthev a l
ue4 , woulda ctu-
all
yus eby tes$26, $27t og e
ttheina lde st
ina ti
ona ddr ess.
hi sdi fersf rom i ndi rectinde x ed, whic hwer eferredt oa spost- i
ndex i
ng .In
post-inde xing,t hev a
lueoft heY- Re gis
teri sa dde da fte
rt heba sea ddressi s
determi ne d.Fore xa mpl e ,i
nt hei nstruc t
ionLDA($22),Y,whe ret heY- Reg i
st e
r
holdst hev a l
ue4a nd$22,$23 poi ntt ol ocation$1000,t heme mor yloc ation
accessedwoul dbe$1004.
You’ l
l r
e c
a l
l alsot ha tpre-a ndpos t-i
nde xingwe r eli
mi tedint he i
ruseoft he
X-a ndY- Re gisters. Pre-inde xingc ouldus eonl yt heX- Registerandpos t
- i
nde x-
ingonl yt heY- Re gister.Be forey oug ettooe xcitedi na ntici
pa ti
ngt hepos sibili
-
tie
soft hene wi nst ruction, restrainy our sel
f :hi 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.hene wmodea l
lowsa nyt wo-by tev aluet obeus ed.hi sme anst hat
theX- Re g i
sterc anbea dde dt ot heba sea ddres sofat a
bleofme mor ypoi nters
thatpr e
v ious l
yc oul dha vebe enl oc atedonl yont hez eropa geofme mor y.

Addres
singMode CommonSy
nta
x
8.IndexedAbs
olut
eIndi
rec
t JMP ($1234,X)

Fore
xampl
e,suppos
eyouhadacomma ndinte
rpret
ert
ha ta
ccepte
dac om-
mandval
uebet
we e
n0a nd2.Withthe65C02,suc
ha nint
erpre
tercannowbe
use
dinconj
unc
tionwithaJMPt
ableloc
ate
danywhe r
einme mory,const
ruct
ed
asi
nthef
oll
owi
nge xa
mple:

]
3
30 As
sembl
yLi
nes

JMP DATA TABLE:


1200: 80 10
1202: A0 10
1204: CO 11

1 ********************************
2 * AL33-SAMPLE COMMAND PROCESSOR*
3 ********************************
4 XC ; MERLIN: ALLOW 65C02 OPCODES
5 ORG $1000
6 TABLE EQU $1200
7 *
1000: 20 00 40 8 ENTRY JSR GETCMD ; GET VALUE FROM 0-2
1003: 0A 9 ASL ; MULTIPLY BY 2
1004: AA 10 TAX ; PUT IN X-REGISTER
1005: 7C 00 12 11 GO JMP (TABLE,X) ; EXECUTE PROPER ROUTINE
12 *
13 * ...MORE CODE HERE...
1080: EA 50 CMD1 NOP ; FIRST ROUTINE
51 * ...MORE CODE HERE...
10A0: EA 100 CMD2 NOP ; SECOND ROUTINE
101 * ...MORE CODE HERE...
11C0: EA 150 CMD3 NOP ; THIRD ROUTINE
151 * ...MORE CODE HERE...

hisi
saveryf
astandefe
cti
vet
echnique
.hef
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
he rea reaf ewi nstructi
onst hathaveaddre s
si
ngmode sthatarenewj ustto
the m. Fore xampl e,twooft hemos texci
ti
ngone sareINCa ndDEC.
Previously ,a nyus esofINC a ndDECwe rel i
mitedtome mor yl oca
tions.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 ylocati
on.hes ur-
pr iseherei st hatINC a ndDECwi llnowwor kont heAc c
umul ator.hi sisnice
be causeyouc annowma i
ntainac ounterintheAc cumulator,
ore vendof udging
ofla gval
ue sa st heya rebe i
ngha ndledintheAc cumul a
tor.
Somef utur ea ssemble r
sma yrequir
ethe“ some whatusual”(ifnotinconve-
nie nt)useofDECAorINCAa stheyseemt opreferforLSR,ASL,andot herope r
a-
tionsont heAc cumul at
or.
heBITi ns tructiona ls
oa ll
owss omea ddit
ionaladdressi
ngmode sthatma y
pr oveus e
f ul.
Pr eviously,theBITi nstr
ucti
ons upportedonlyabsoluteaddressi
ng.
CT]heor
3[ 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.he6
5C0
2 3
31

hatist
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

hi sisuse
fulfortes
tingame morylocat
ionforagi
venbitpatt
ern,butnot
direc
tl
ys uit
abl
ef orte
sti
ngthebitpa
tter
noft heAc c
umul
ator.Formanyope ra-
tions
,thismeansy ouhavetorat
herart
iici
all
yloadsomeme morylocat
ionwi t
h
thevalueyouwa ntedtocomparetot
heAc cumulator
.
hene w65C0 2supportst
hreenewa ddres
si
ngmode sfortheBITi ns
truc-
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?”
heg reatthinga
boutthe65C02i
sthatnotonlyaremanyoftheoldinst
ruc-
ti
onsenhanced,butthereal
soareanumbe rofabsol
utel
yterr
iicnewi nst
ruc-
ti
ons–twelv
e,tobeex a
ct.henewinst
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

CT]heBBR,BBS,RMB,a
4[ 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
as sir
yone s
t.
..
PHX,
PHY,
PLX,
andPLY
Comma ndsf orpushi
ngaby teontothestac
ka ndpull
ingaby eofoft
t he
s
tackexis
tfort heAc cumula
torbutnotfortheX-a ndY-Regis
ter
sint he6502
.
Oneofthemor ecommonus e
sforthesta
ckistosaveal
lofthereg
ist
erspri
orto
goi
ngintoar outinesothateveryt
hingcanber e
stor
edjustpri
ortoe xi
ti
ng.
Ordi
naril
y,t
os a v
et heAccumula
tor,X-Regi
st
er,andY-Regi
ste
r,we’
dha vet
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

hepr obl
emi scompl
icate
de ve
nfurtherinprog
ramslik
ethechar
acte
rgen-
er
atorli
st
e dinc hapte
r31.he 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.he6
5C0
2 3
33

Unf ort
una tel
y,thisme answemus tforcesomela goft heS tat
usRe gi
st
e r
,
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 28byte
sa s
thema ximum br a
nc hingdi sta
nc e.
Itisworthme ntioning,int heinterestofprog r
ammi ngs tyl
e,thatmanype o-
pleindiscri
mi na t
elyus eaJMPt og oba ckt othet opofal oopwhe nabr a
nc h
inst
ructionwoul ddot het r
ick;t hi
sonl ya ddsonemor el i
mi t
ati
ont otheina l
codei nthepr ocess.Hope f
ully,thisne wbr anchinstruc t
ionwi llencourag
epe o-
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 .hi 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.hi siseasytodowhe ny ouhave
toloadt heAccumul ator,X-Reg is
ter,orY- Regi
st
erwi th0a ny way.hepr oblem
isthatonoc cas
iontheonl yreasononeoft heregis
tersisloadedwi th0isbe ca
use
ofthene edtozeroame mor yloca t
ion.
STZa ll
owsust oze r
oouta nyme moryby t
ewi thouta l
teri
ngc urr
entr egi
ste
r
contents.Nota l
loft hea ddressingmode susuall
ya vail
abletotheSTA,STX,or
STYins tr
ucti
onsa r
ea v a
il
ablewi thSTZ,t hough.hef 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

he r
ea r
etwoi nter
est
ingt hingstonotehere.heir stisthatforsomer ea-
sontheterm“ r
eset”isuse
di nsteadof“cle
ar”toindicat
et hezeroi
ngofag ive
n
bi.hes
t 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.5he 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 er
o-pagel ocati
ona ndt henbr anchde pend-
ingoni t
sc ondi t
ion.hi sinstruc ti
onwi l
lbev er
yus efulfortes t
ingla g
si npro-
gramst ha tne edt o pa ck la 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 .
he sei nstr
uctionsa r everys imilarinbot ha ppearancea ndus agetot heSMB
andRMBi nstructi
onsj us tdis cus sed.he y,t oo,us efour-chara c
termne moni cs
.
hedi fer ence,ofc ourse, istha twea retes
tingbi tsta
tusr atherthanc hangingit
.
he s
ea r
et hree-
by teins tructions ,their 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 heir 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 arryla 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

heequi
val
entc
annow bedonewi
t fe
houta c
ti
ngt
hec
aryla
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]hepr
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.he6
5C0
2 3
35

hi sals
ocoul
dbeusef
ulincrea
tingdri
v e
rsfort
henewApple//
e8 0-
col
-
umne xt
endedmemoryboa
rdsincet
hi sc
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.he seins
truct
ionsarerat
herli
keac ombi
nat
ionoftheBIT a
nd
AND/
ORAi nst
ruct
ionsofthe6 50
2.he ysee
m pri
maril
ydes
igne
dforcontr
oll
ing
I/Ode vi
cesbutma yhaveotheri
nteres
ti
ngappl
icat
ionsast
hingsdev
elop.
hea ct
ionofthesetwoinst
ructi
onsist
ouseama s
kstore
dintheAccumu-
lat
ort ocondit
ioname morylocat
ion.hema skintheAccumulat
orisunal
ter
ed,
butt heZ-lagoft heSta
tusRe g
ist
eriscondi
ti
onedb as
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

hi
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

he
sei
nst
ruc
ti
onsus
eonl
yabs
olut
eandz
ero-
pag
eaddr
ess
ing
.

Ot
herDi
fer
enc
es
he rea r
eanumbe rofothe rdi ferencesbetweenthe6 502a nd65C02,most
notabl
ythepowe rconsumpt i
on. hepowe ruseofthe6 5
C0 2isone-t
enththa
tof
the6 50
2,s othec hipr unscons i
de rablycooler.hel owerpowe rrequir
ement
opensnewpos si
bil
iti
esforportablec omput er
sa ndterminals.
Onepoi ntofi nter
estisthatt heol d65 02indi
rectjumppr obl
em hasbeen
ixed.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,t hepoi nter sus e da renott hos ea nt ici
-
pa t
e d. hi si sbe ca uset hehi ghby teoft hepoi nt era ddr e ssisnotpr ope rlyi nc re-
me nt edbyt hes ta nda rd6 502.
hi spr obl em ha sbe enix edi nt he6 5 C0 2. heonl ypos s i
bl epr obl em he reis
“clev er”pr ote ctions c
he me stha tus et hisbugt ot hr ow ofpe opl et ry ingt o
de codet hepr ote ctionme thod. Ot he r wise ,thiss houl dnotpr esenta nypr obl ems
toe xistings oftwa r e.
Ar et he rea nypr obl emst obea nticipa ted?I nt he ory ,no.hene w6 5 C0 2is
compa tiblepi nf orpi nwi tht heol done , anda lsoupwa rdlyc ompa tiblei nt erms
ofs oftwa re .S oftwa r efort heAppl e ,PET, At ari,orot he r6 50 2-ba se dmi c roc om-
put erss houl dwor kwi thoutpr obl emswi tht hene wc hi p.Ar et he rea nye x cep-
tions ?Unf or t
una tely ,yes.
heir stbi gpr oblemc oncer nsi nt erna lmi c ropr oc essort imi ngont heAppl e
IIa ndI IPl usc omput ers.heAppl eI Ia ndI IPl usdonotha ndl et hemi c ropr o-
ce s
sorc loc kc ycle sint hes a mewa yt heAppl e/ /edoe s .Ont hes ur fac e,the6 5C0 2
shoul ddi rec t
lyr epl acet he6 502 ;howe v er,be c aus et he6 50 C2i saf a sterc hip,
da t
ai snota va il
a blef ora sl onga ndbi tsc ang etl ost. Wha tthi sme a nsf ornowi 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 hipt ha twor ksont heAppl eI IorI I
Plus .I tc anbee x pe cted,t houg h,t ha tr ev is
ionswi llbema dei nt hene arf ut ure
thatwi lla llowt he6 5C0 2t obei mpl eme nte dint heol derma c hine s.
he rea lsoi sapos sibilit
yofpr obl emswi ths omee xisti
ngs oftwa re.As ma l
l
pe r
c ent ag eofs oftwa 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 t
iona sa nt i
c ipate dwi tht he6 5C0 2.
Fore x a mpl e ,ar e asona bleque stion mi ghtbe ,“ Whe r edi da llt hene w
opc ode sc omef rom?Afte ra ll
,wa sn’tt hec hipf ull?”Toa nswe rt his,c ons ider
howt hei ns tr
uc t i
onsweus enowa res truc t
ur ed.he6 5 02ope ra t
e sbys ca nni ng
me mor ya ndpe rfor mings pe cicope
i rationsba s edont hev alue st ha titindsi n
ea c
hme mor yl oc ation.Youwoul dt hene x pec tat otalof2 5 6pos s i
bl eopc ode s
.
Asi tha ppe ns ,a ll2 56pos s i
blev alue sa renotus e
d.I tist hi sg roupofunus ed
opc ode st ha ta ll
owsf ort hene wi ns truc tionsa nda lsoc reatest hepos sibilityofa
sma l
l pe rce nta geofdi ic ultieswi the xistingpr og rams .
Al thoug hr arelydoc ume nte d,t hepr ev i
ous 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 02i sl abe le
da sa na lterna teNOP. hi si soneof
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).
he rea reot he runus edc ode s ,thoug h,t ha tha v ec ombi na tione fe cts –us u-
allyofl ittleus e–s uc ha sl oadingt heAc c umul atora ndde creme nt i
ngar eg i
ster
atthes a met i
me . he irma ina ppl i
c ationi ssimi la rtot hei ndi rec tjumppr obl em:
crea ti
ngc odet ha tc a nnotbec asua llyint erpr eted. I
ft hes eins truc t
ionsha v ebe en
3
3.he6
5C0
2 3
37

usedi nex i
sti
ngs oftwa re ,proble msc oulda risewhe nt hatsoftwa r
eisr unont he
65C0 2.
Wi thsuc hdiic ul ties,t
he n, whybot he rtos ubstit
utethene w6 5C0 2intoa n
exist
ingAppl e?hea ns we rsarev a ri
ed.
Firstofa ll
,the6 5C0 2isl ikelyt oa ppe arinupc omi ngr el
e as
esofe xist
ing
comput ers( i
nane wr eleaseoft heAppl e/ /e,pe rhaps?),anda ssuchy ouc an
experime ntnowwi tht hene wes tversionoft hisversatil
ede vi
ce.
Second, therelikelywi l
lbes peciica ppl icat
ionswhe r
et hea dvantagesoft he
chipwi l
lma keitwor ths upplyingwi tht hes oftware ,si
ncethedi s
adv antagesare
practi
callynone x
istentf ort heAppl e/ /
ea ndAppl e/ /
/.Coder ewrittent otake
advantageoft hene wi nstructionsc anbee xpe ct
edt obe1 0t o1 5pe rcentsma ll
er
andr unpr opor t
iona ll
yf ast
er.I nc ertai
na pplications,eveng reat
e rimpr ove-
me ntscouldbee xpecte d.
Att hiswr i
t i
ng,t heRoc kwe l
lc hips eemst oha v ethelargests etofins t
ruc-
ti
onsoft hethre evar ietiesava il
able.heGTEa ndNCRc hipsla c
kt hebi t-ma-
nipulati
oni nstructi
onsbuta reot he r
wi seide ntic
a l
.
Ast oa ssembl e
rss uppor t
ingt hei nstructions,thec urrentv ersi
onofMe rl
in
suppor t
sa l
lthene wopc ode sinbot ht hea ssembl ya ndS ourcerorpor t
ionsoft he
produc t
.S -CS oftwarei sofe ringa6 5 C0 2c ross-as
s emblertor egist
e r
e downe rs
oft heS-CAs semb le
ra tar educ edr a te
.Ha y denwi lbeofe
l ri
nga nupda teto
ORCA t os uppor ttheGTEv er s
ionoft hec hip.Anya ssemble rthats uppor t
s
ma croca pabil
it
iesshoul dbea blet obeus edi mme diatel
ybyde iningt hepr oper
hexc odes.

]
3
38 As
sembl
yLi
nes

Anot
efr
om Rog
erWa
gne
r,J
une1
983
:

hi sinst
all
me ntma rksthelas
tinthisseri
es.Iwa nttothankthe
ma nyreadersofthiscolumnov erthelastseve
raly e
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 dalsoliketothank
Softa
lkforg iv
ingmet heoppor t
unitytosharethee xci
tementof
programmi ngwithitsreaders
,anda l
sothankBr ianBrit
tforhis
helpinresearc
hingthi
smont h’sar
tic
le.

Forbet
terorworse,t
houg h,you’
renotlikel
ytobecomple
tel
yrid
ofme.he rea
r er
umor sofothercol
umnsa ndpr
oje
cts
,andIlook
for
wardtobeingas mallpartoft
heS ofta
lkfami
lyf
orsomey 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
’sAssemb l
yLine s
begana ppeari
ngi nSoftal
k;t
hema gazinewa sonl
yonemont hold.
Intha tirstyear,Wag ner’
scolumne li
c i
te
dmor ema i
lfromS oft-
alk
’sr eadersthananyot herfeat
ure,a ndproperl
ys o:Itwa sthe
irsttimea ss
embl yl
ang uag
eha dbeene xpla
inedfrom st
epone .In
fac
t,inhi sir stcol
umn,Wa gnerdi dn’tevenintroduceac om-
ma nd.

Wit
ht hi
sissue,RogerWa gner
’sAss
emb l
yLi ne
se nds.heir s
t
y
ear
’scol
umnspl usappendix
esandrevi
sionshavebe enavai
la
ble
f
orsomet i
mei nAssemblyLine
s:TheBoo k.Vol
ume2 ,cove
ring
t
heres
tofthecolumns,wil
lbere
lea
sedshortl
ybyS ofta
lkBooks.

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,wechall
engedt hereader
sof
t
he“As s
embl yLines”col
umnt oac ontest
. Usi
ngthecomma ndsdi scuss
edinthe
col
umnf rom Oc tober1980t hroughMa rch1981(allmater
ialcove re
dthrough
cha
ptersixi nthisbook )
,c ontest
ant swe reas
kedt osubmitpr og ra
mswhi ch
wouldbej udgedbyt hestaf,thes hor t
estandmos tint
eres
tingpr ogram be
ing
t
hewi nner.Conte s
trule
sa rere pr
intedhe reasthe
yor i
ginal
lya ppearedinthe
Marchiss
ueofS oftal
k.

Cont
estRul
es

Createt
heshort
estposs
ibleprogram usingal
landonl ythec om-
mandspresent
edthusfarinthisser
iesthatdoessome t
hinginter
-
est
ing.hepr ogr
am mus tbee nti
rel
yi nma chinelanguage
,a nd
maynotc a
llanyrouti
nesinI nt
egerorAppl eoft.I
s tma ycallany
oftheMonit
orrouti
nesfrom $F800−$FFFF.

hepe 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 athersubject
ively
sel
ect
edpane lma deupofpe opleatSofta
lk,myse
lf,a
nda nyot her
haple
sspass er
sbywec anr opei nt
ot histhi
ng.Me mbe rsoft he
stfsofS
a oftal
ka ndSouthweste
rnDa t
aS ys
temsandpr ofes
sional
progr
amme rsarenoteli
gibl
etowi n.Entrie
sshouldbes ubmitted
nolate
rthanApr il15,1
98 1.
Tieswillbesett
le
dbyAppl e’
sr a
ndom
numbe rge
ne rat
or .
(Ipr
omi s
enott oseedit!
)

Conte
stres
ult
swer
ea nnouncedint
heJune1
981edi
ti
onofSofta
lk.he
wi
nningprog
ramfort
hecontesti
sli
st
edbeow.hec
l omment
arya
ccompa ni
es
t
heli
sti
ng.

Cont
estRe
sul
ts
Withtheus ualcomme ntsinmi nda bouthow ha r
di twast ode ci
deona
winner,Ihe r
ebya nnouncethewi nne rofthec ontestasStev en Morri
s,of
Queens,NewYor k.Hisprogr
amc ombi nesanumbe rofthepri
nc ipl
eswe ’
vedis
-
cuss
eds ofaranda l
sos howssomenicet ouche
sinpr ogramming .It
’saneleg
ant
useofallt
heg iv
e ncode s
.Ofpart
icularinte
res
tisas el
f-modif
yingpa rtwhere
in
theprogra
ma ctuall
yr e
wr i
te
sasma l
lpor ti
onofit
selfuponusercomma nd.

]
3
40 As
sembl
yLi
nes

Ithinki twi l
lbeofi nter
e s
t,anda lsoag oodr evi
ew,tog ot hr oug hMor ri
s’s
l
istingt ose ewha t
’sbe endone .Be f
oredoi ngt hat,howe ver,al it
tleba ckg round
ononemor ek indoft onerout i
neisi nor der.hi swillmak eMor ri
s’spr og ram
thatmuc hmor eunde rst
anda bl
e.
Incha ptere i
g ht,Idiscussedsimpl etoner out inesinwhi c
ht hes pe ake rwa s
accesseda tac onst a
ntr ateforag i
venl engthoft ime.he set wof actor sde ter-
mi nedt hepi tcha nddur at
ionoft het onepl aye d.Av ari
ati
onont hisist oha ve
thepi tchde cre
a seori ncreaseasthetonei spla yed, creati
ngefe ctsr athe rlikethe
sound us uall
ya ssoc iat
e d wi t
ha
falli
ng bomb or a r ising s i
ren,
respe cti
vely.hi sr equi rest hree
va riables
,andwi thoutg e tt
ingt oo
techni cal
, l
etmet akeamome ntto
il
lus trat
ewi t
ht hec ha rtatl eft.
he v ert
icala x i
sr epr esents
thef requencyoft het onebe i
ng
pl aye d.Put ting s eve ralt one s
tog etherintoas eriesov e rape ri
od
oft imec re
ates,int hi sc ase,ar is-
ings cale
.Ase acht onei spl ayed,
thepi tchisinc reased.Ea chindivi
dua ltonel astss omea r
bit
ra r
yt ime , T, andput
together,thes erieslastsanov eral
lti
mepe riod, labeledhe r
ea sDURATI ON.
Ifthepi tchi sde crea
sedbyac ert
aina mounte achtime ,t hepa tterni s
reversed.hi si ss ome ti
me sc al
le
dar ampt onepa tt
ern.Inpa r
ent he ses,Iha ve
indicatedhowe achoft hesev al
uesisde te
rmi ne di nMor r
is’
spr ogra m.
He r
eisal isti
ngoft heprog r
am:
1 ********************************
2 * ASSEMBLY LINES CONTEST WINNER*
3 * BY STEVEN MORRIS *
4 ********************************
5 * OBJ $302
6 ORG $302
7 *
8 PTCH EQU $300
9 DRTN EQU $301
10 SPKR EQU $C030
11 PREAD EQU $FB1E
12 PB0 EQU $C061
13 PB1 EQU $C062
14 GRSW EQU $C050
15 TXTSW EQU $C051
16 CLRSCR EQU $F832
17 *
0302: CA 18 LOOP DEX ; DEC THIS DELAY
0303: D0 06 19 BNE CYCLE ; DONE? NO = SKIP CLK
20 *
Appe
ndi
xA:
Cont
est 3
41

0305: AE 00 03 21 CLK LDX PTCH ; REFRESH X-REG


0308: AD 30 C0 22 LDA SPKR ; CLK SPKR
23 * SPKR CLKS ONLY ONCE
24 * FOR EVERY ($300) PASSES
25 *
030B: 88 26 CYCLE DEY ; # OF CYCLE CTR.
030C: D0 F4 27 BNE LOOP ; DONE?
28 * NO = KEEP GOING
030E: CE 01 03 29 DEC DRTN
0311: F0 06 30 BEQ CHKPDL ; DONE W/ RAMP?
31 * YES = CHK PDLS
0313: EE 00 03 32 RAMP INC PTCH
0316: 4C 02 03 33 JMP LOOP
34 *
0319: A2 00 35 CHKPDL LDX #$00
031B: 20 1E FB 36 JSR PREAD ; READ PDL(0)
031E: 8C 00 03 37 STY PTCH ; SET PTCH
0321: E8 38 INX
0322: 20 1E FB 39 JSR PREAD ; READ PDL(1)
0325: 8C 01 03 40 STY DRTN ; SET DRTN
0328: A0 7F 41 LDY #$7F
032A: CC 62 C0 42 CPY PB1 ; #1 PRESSED?
032D: 90 27 43 BCC TOGGLE ; BRCH IF YES
44 *
032F: C8 45 INY ; #$7F -> #$80; AN EXCUSE
0330: 98 46 TYA ; TO USE THESE
0331: AA 47 TAX ; COMMANDS.
0332: EC 61 C0 48 CPX PB0 ; #0 PRESSED?
0335: B0 CB 49 BCS LOOP ; BRCH IF NO
50 *
0337: 20 32 F8 51 SCREEN JSR CLRSCR ; CLR TOBLK
033A: 8D 50 C0 52 S1 STA GRSW ; SHOW GRAPHICS MODE
033D: 8D 51 C0 53 STA TXTSW ; SHOW TEXT MODE
0340: 4C 3A 03 54 JMP S1
55 *
0343: A8 56 SETDEC TAY ; USE UP THIS CODE
0344: A2 CE 57 LDX #$CE ; OPCODE FOR 'DEC'
0346: 8A 58 TXA
0347: CD 13 03 59 CMP RAMP ; IS IT 'DEC' NOW?
034A: F0 04 60 BEQ SETINC ; BRCH IF YES.
034C: 8D 13 03 61 STA RAMP ; NO. MAKE IT 'DEC'
034F: 60 62 RTS
63 *
0350: A2 EE 64 SETINC LDX #$EE ; OPCODE FOR 'INC'
0352: 8E 13 03 65 STX RAMP
0355: 60 66 RTS
67 *
0356: 20 43 03 68 TOGGLE JSR SETDEC
0359: 4C 02 03 69 JMP LOOP
70 *
035C: C0 71 CHK

I
’l
ltr
ytoexpl
ainea
c hpa
rtoft
heprog
r a
m,hope
full
ywit
haprope
rbal
ance
ofenoughdet
ailt
ojogyourmemorya
nde noug
hbrevi
tytok
eept
hing
sre
ason-
abl
yshort.

]
3
42 As
sembl
yLi
nes

Ifalloft hi sse emsov e rwhe lmi ng ,y ou’r et ryingt or eadt hroug hi tt oof 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 nt oone
pr og ram.Don’ twor ryi ft heinede tail
soft het oner out inee scapey ou.he
impor tantpa rti stoma k es ur et ha ty oua tl ea str ecallthee x i
stenc ea ndg e ne ra
l
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 r
e
thepa ddle sa rec he cke df orne wv a l
ue sa tthee ndofe ac hr a mps e ri
es( l
ine3 5at
addr ess$319) . heX- 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.ha 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 tor edi n
loc ation$300,l abe ledPTCH( “pi tch” ).heX- Re gisterv aluei sthe ni ncre me nted
from $00 t o$01onl i
ne3 8,a ndpa ddl e1r ea d.her etur ne dv a l
uei ss tor eda t
$301f ort hedur at
ionv alue .
Ifpa ddl epus hbut ton1i spr ess ed, location$C062wi llhol danumbe rg re ate
r
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 e
ar
(BCC)wi llbet a ken( Y- Re gis t
e r<me mor yl oc ation=c arryc l
e ar).We ’
lls eewha t
tha tdoe sla ter.
Ifthev a luei sl esstha n$7F,pr og ra me xe cutionwi llf allthroug ht ol ine4 5.
He ret he$7Fi sinc rea sedt o$80a ndt hatv a luepa ssedt ot heX- Re gis
terv iat he
Ac c umul ator .he ses tepsa rehe ret oe x erciset heINY,TYA,TAXc omma nds ,a nd
toa ll
owust ous et heCPXc omma ndne xttof ulil lthec ont e stre quireme nts .At
line4 8t hec ompa risoni sdone . Ift heX- Re gisteri sg reate r( reme mbe rithol dsa
$80h ere), thebut toni snotpr e sseda ndt heBr anc hCa rryS et( BCS)wi l
l bet a ken
(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 rbitrary
va lue s,butt heov era l
lt he or yi st ha t,sta r
tinga tCLKonl ine2 1 ,theX- Re gisteris
loa de dwi tht hepi t
c hv aluea ndt hes pea kerc licke donc e .Atl ine2 6theY- Re gi
s-
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 fe cti st ha tthepr 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.hi sc re ate
s
thede l
aybe twe enc licksne ede df orag i
v ent one .
hel e ng thoft ha tpa r ticula rt onei sde te rmi ne dbyt heY- Re gis
ter.Whe nit
re ache sav a lueof$00,t heBNE( Br a nc hNotEqua l)failsa ndt hec ounterf ort he
ov era lldur ationi sde cr eme nte d. Asl onga sthe re ’stimel e ft(tha tis,DRTN>$00) ,
thene xtt estf a i
ls( BEQ=Br anc hi fEqua lt oZe r o)a ndt hepi tchv al
uei si nc r
e-
me nte d.
Goi ngba c ktoLOOPpl ayst hisne xtnot eunt ilalloft henot e sint hes e ri
es
ha v ebe e npl ay ed.I nc reme ntingpi tc hg ivesade scendi ngnot epa t
tern.( Re cal
l
tha tt heg re atert hepi tc hv a l
ue , thel owe rthet onepl a y
ed. )
Appe
ndi
xA:
Cont
est 3
43

Whe nDRTNdoe srea ch0 ,t


hepr og ram br anche stot hepa ddlec he ckr outine
thatwes t
a rt
edi n. Let’sseewha tha ppe nswhe nabut toni spr essed.I fbut ton1i s
pressed,t heprog ra mg oesv iaTOGGLEt oSETDEC.hi sc leve rs e
c t
ion( ignor ethe
TAY)l oadst heX- Re gisterwi ththev alue$CE. hi sist heopc odef orDEC( DECr e-
me ntame moryl oc ati
on) .
Ifthec ompa r i
sonf ai
ls,thati s,the r
ei snota$CEc urre ntlythe re,the$CEi s
storeda tRAMP, theRTS( ReTur nf rom S ubr outine)re tur nst oTOGGLEa ndt heJMP
loops endse ve
r ythingba cki nt
ot het onel oop,t hist imewi thaDEC PTCHt here
inste
a d.hi sgivesa na scendi ngpi tchs erie
s .
Ifthec ompa r i
soni strue,itme anst ha ta$CEwa sputt he reear li
e r
,a ndt he
BEQ g oest oSETINC,whi chr estorest hec odef orINC a tRAMP( $313) ,a ndt hen
returnswi ththeRTS, JMPLOOPa sint hepr ev i
ousc ase.
he set woopt ionsg ivet hepr og ramt hea bi l
it
yt or e wr i
teitself,a ni nterest
-
ingandpowe rful i
de a.
Ifpa ddlebut ton0i spr essed, t
hebr ancha tline4 9f a il
sa ndt hepr og ramf all
s
intoa ni ninitel oopa tSCREEN( $337) .Int hisloop,t hes creeni sc learedt ot he
colorbl ackbyt heMoni torr outinea t$F832.
Loc ations$C050 a nd$C051 a res oft-
swi t
c hes:a cce ssingt he sec ha ng esthe
displ
a ymodeoft heAppl e.hes c r
e e
nc anbev i
e wede it
he ri nat extmodeori na
graphi c
smode . Ac cessing$C050onl ine5 2s et
st heg ra phi csmode , sot hes creen
appea rsbl ack
.Ac cessing$C051s etsthedi splayt ote xt, whi cha ppe ar sasi nv er
se
“@”s igns.
h eJMPS1r epea t
st hisc y
c leba cka ndf or thsof astt ha tyoudon’ ta ctual
ly
seetheli cker,justa ni nteresti
ngpa tternc reatedb yt hes cre enss witchi ngf ast
er
thany ours c
reenmoni torc andi splayt hem.
Att hi spointy ouha vetohi tRESETt oe nd.
he rewe reanumbe rofot he rex cell
ente nt r
ies.Honor ableme ntions hould
bema deofS t
ev eHa wley ,RayRa nsom, StephenGa g ola ,Jr.,andMa ttBr ook over
forthe i
re forts.

]
Ap
pendi
xB:
Ass
emb
lyCo
mmands
hi ss ectionma ywe llser
v ea sthemos tofte 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
iie d
task .hef 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 eir
r 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 al
da taont hei ns t
ruc t
iona ndofte nabr iefex ampl eofitsus ea swe ll
.
Ple asenot ethati ns omee xampl 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 hift
ins t
ruc ti
ons .Fore x ampl e,thef ol
lowi ngr 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
dife 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“ oic i
a l
”e x planati
onofa ddressing
mode s
.

ADC:
ADdwi
thCa
rry
Descr
iption:hi si
nstr
ucti
onaddsthecontentsofamemoryloca
tionori mme-
dia
tevaluetothec ont
entsoft
heAc c
umul a
t or,pl
ust
hec a
rrybi
t,ifitwasset
.
her esultisputbackintheAccumulator
.ADCwor ksforbot
hbina r
ya ndBCD
(Bi
naryCode dDe c
imal)modes
.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓ ✓ ✓

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,
ADCi tofte
smos 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
ha nofs
sa 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:hi si
ns t
ructi
ontakese ac
hbi toftheAccumulatorandperf
ormsa
l
ogic
alAND wi theachc orr
espondingbitoft hespec
iiedme moryl
ocat
ionor
i
mme dia
teval .her
ue esul
tisputba ckint heAccumulat .heme
or moryloca-
t
ionspec
iiedisunafecte
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
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

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.hema skisc reate
dbyputti
ng1 sineac
hbi t
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
(perhapsadiskile
).her 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.hisisthecas
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 fecti
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 fect
ed,rema i
ning0 sor1s,asinthei
rorigi
nalcondit
ion.
heMoni 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

he rea realsoatle as
ttwoot he rrat
herobscureusesfortheANDi nstruct
ion.
heir 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
on pr 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
heg eneralformul ais:Accumul at
orMOD2 =RESULT
hea ctualoperationi scar
r i
edoutbyus i
ngav al
ueof(2n−1 )asthema sk
val
ue .het 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

histechni
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

her es
ultoftheANDofa nynumbe rand$01wi l
la l
waysbeeit
her0or1
depe
ndi
ngonwhe therthenumberwasoddoreven.
het hi
rda ppl
icat
ionisindetermi
ningifag i
venbi tpatt
ernispres
ent
amongt
heotherdatainanumber.Forexampl
e,t
otestifbit
s0,3and7areon:
LDA MEM
AND #$89 ; %10001001
CMP #$89
BEQ MATCH
BNE NOMATCH

]
3
48 As
sembl
yLi
nes

hegener
altechniqueistoirs
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
ciie
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
econirmsthatal
lofthebi
tsofi
nte
res
tmatch.

ASL:
Ari
thme
ticShi
tLe
t
Desc
ripton:hi
i sinst
ruc
ti
onmov eseac hbi
toftheAccumulat
ororspec
iied
memorylocat
iononeposit
iontotheleft.A0isf
orcedatt
hebit0pos
iti
on,and
bi
t7(thehig
h-orderbi
t)f
all
sint
othec arryher
. es
ulti
slfti
e ntheAccumulat
or
ormemorylocat
ion.(S
eeal
soLSR, ROL,andROR.
)

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓ ✓ ✓

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
:hemostcommonus
eofASLi
sformul
ti
ply
ingbyapoweroft
wo.Youa re
al
rea
dyfami
li
arwit
ht fe
hee ctinba
sete 123 ×10 = 1230(
n: s
hiftl
eft)
.For
ex
ample
:
LDA MEM
ASL ; TIMES 2
ASL ; TIMES 2 AGAIN
STA MEM ; MEM = MEM*4 (4 = 2^2)

heotherus
eistoc
hec
kagive
nbitpos
it
ionbyc
ondi
ti
oni
ngt
hec
aryla
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:hi 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
ewershifts
needed).ROLals
oc anbeusedinst
eadofASLdependingonwhe t
herthedataisto
bepr es
erved.

BCC:
Bra
nchCa
rryCl
ear
Descri
pti
on:Execut
esabranc
hifthecaryla
r gi
sclear
.Ignoredifcar
ryisset.
Manya ss
emble
rshaveanequiva
lentpse
udo-opcal
ledBLT( Bra
nchLe sha
s n,
nottobeconf
usedwitht
hes a
ndwich)
,si
nceBCCisofte
nus edimme di
ate
lyfol-
l
owingac ompari
sontose
ewhe t
hertheAccumulat
orisle
s sthanthespecie
i d
val
ue.

Fl
ags&Re
gis
ter
sAfe
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BCC Address 90 FF

Not
e:hec
aryla
r 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 pecie
i dv a
l .heus
ue ua
la ppe
aranceoft
hec odeisl
ist
ed
bel
ow.Not ethati
nat wo-by
tecompa
risonthehi g
h-orde
rbyte
sa reche
cked
irs
t.

One
-By
teCompa
ris
on:
ENTRY LDA MEM
CMP MEM2
BCC LESS ; Goes to LESS if MEM < MEM2
BCS EQ/GRTR

Two-
Byt
eCompa
ris
on:
ENTRY LDA MEM+1
CMP MEM2+1
BCC LESS ; MEM,MEM+1 < MEM2,MEM2+1
BEQ CHK2 ; MEM+1 = MEM2+1

]
3
50 As
sembl
yLi
nes

BCS GRTR ; MEM,MEM+1 > MEM2,MEM2+1


CHK2 LDA MEM
CMP MEM2
BCC LESS ; MEM,MEM+1 < MEM2,MEM2+1
BCS EQ/GRTR ; MEM,MEM+1 >= MEM2,MEM2+1

BCS:
Bra
nchCa
rrySe
t
Descr
ipt
ion:Exec
utesabr
anchonl yi
ftheca
rrylagisse
t.Somea s
semble
rssup-
portt
hepseudo-
opBGT(“BranchGr e
aterhan”),
sincet
hiscomma ndisuse
dt o
te
stwhet
hertheAccumula
torisequalt
oorgreat
erthanthespe
ciiedval
ue.
Fl
ags&Re
gis
ter
sAfe
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BCS Address B0 FF

Not
e:hec
aryla
r g,uponwhi
cht
hisde
pends
,isc
ondi
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
ciiedv al
ue.BCSca
nbec ombine
dwithBEQt odet
ectagre
ater
-tha
nr el
at
ion-
shi
p.Not ethatinthetwo-by
tecompa
rison,thehig
h-orde
rby t
esarechecked
irs
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
esabra
nchi
ftheZ-lag(z
erolag
)iss
et,
indi
cat
ingt
hatt
he
re
sultofaprevi
ousoper
ati
onwas0 .Se
eBCStoseehowacompari
sonfort
he
Accumulat
orequalt
oagiv
enval
ueisdone.

Fl
ags&Re
gis
ter
sAfe
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BEQ Address F0 FF

Note:hezeola
r g,uponwhi c
ht hi
sde pe
nds ,i
sc ondit
ionedby :ADC,AND,ASL,
BIT,
CMP,
CPY,CPX,
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 t
hc ompareoperat
ions,BEQis
usedt otestwhetherther es
ul tofav a
r i
etyofotheroperat
ionswas0.hec om-
monc las
sesoftheseope ra
tionsa r
einc r
ementa nddecrement,l
ogic
aloperators
,
shifts
,andr egi
sterloads.Evene asi
ert oremembe risthegenera
lprinci
plethat
whe neveryou’vedones ome thingthatresul
tsin0,chancesaregoodthattheZ-
lagha sbe enset.Likewise
,a nynonz eroresul
tofa noperati
onisli
kelytoc l
ear
theZ- lag.Oneoft hemos tc ommoni nst
ancesiswhenc hecki
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 tsoft heAc c
umul atorandt he
contentsoftheme mor yloc
ation.heoppos i
teoft her esul
tisstoredi ntheZ-
lag.Wha tthisme ansi st
hatifanybi t
sseti
nt heAc cumul atorhappe ntoma tch
anys eti
nt hev al
ues pecie
i d,theZ- lagwillbec leared.Ifnoma tc
hi sfound,it
wil
l beset
.BNEi susedt odet
ectama tch,BEQd etectsano- ma tc
hc onditi
on.
Full
yunde r
standingthef unct
iona ndv ariousa pplic
a t
ionsoft hisins
truc-
ti
oni sas i
gnofha vi
nga rri
veda sana sse
mbl y-langua geprog r
amme rands ug-
gest
sy oua repr obablythehi tofpa rt
ies
,t hri
lli
ngy ourf r
iendsbydoi nghe x
ari
thme ti
ciny ourhea dandrec i
ti
ngAS CIIcode sonc omma nd.

]
3
52 As
sembl
yLi
nes

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

M7 M6 ✓

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.
het es
tma skcanbehe ldeitherint heAc c
umulator(iftest
ingame mory
l
oca
tion),oriname mor yloc
ati
on( whent e
stingtheAcc umulator)hema
. 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 →1→(opposi
t)→0 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 →0→(
oppos
it
e)→1 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-lags
, 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
ethatint hisex
a mple,noda t
aisactuall
yret
ri
evedfr
om thekeyboa
rd.
Onlyawa i
tisdoneunt iltheke y pre
ss.heBITSTROBEs tepintheexampleal
so
pr
ov i
desanill
ust
r at
ionofas e
c onda ppli
cat
ionofBIT,whichi
stoacce
ssahard-
war
el oca
tion(oftencal
ledas o ft-s
witch)withoutda
ma g
ingtheconte
ntsofthe
Accumulat
or.

BMI:
Bra
nchonMI
nus
Des
cript
ion:Ex
ecutest
hebranchonl
yiftheN-la
g(si
gnlag
)iss
et.heN- la
g
i
ssetbyanyoperat
ionproduci
ngaresul
tinthera
nge$80t
o$FF(i.
e.hi
ghbit
s
et)
.

Fl
ags&Re
gis
ter
sAfe
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BMI Address 30 FF

Note:hesi
gnla g,uponwhi c
ht hi
sde pe
nds ,i
sc ondit
ionedby :ADC,AND,ASL,
BIT,
CMP,
CPY,CPX,
DEC,DEX,DEY,EOR,INC,INX,INY,LDA,LDX,LDY,LSR,ORA,PLA,
PLP,
ROL,
TAX,
TAY,TXS,TXA,
a ndTYA.

Us e
s:BMI i
smostcommonlyus e
dt odete
ctnega
tiv
enumbe r
swhensi
gned
binarymat
hisus
ed,
butisal
soequal
lycommonintes
ti
ngf orase
thi
ghbi
t,s
uch
asinwatc
hingt
hekeyboa
rdforakeypr
ess
.(S
eeal
soBIT.)Forex
ampl
e:

]
3
54 As
sembl
yLi
nes

LOOP LDA KYBD


BMI PRESS ; DATA > $7F
BPL LOOP ; DATA < $80

BMIi
sal
sousef
ulf
ort
ermina
tingal
oopt
haty
ouwa
ntt
ore
ach0a
ndwhi
ch
ot
herwi
sewi
ll
sta
youtoft
he$80to$FFr
ange
:
ENTRY LDX $20 ; TO LOOP 33 TIMES
LOOP DEX
BMI DONE ; WHEN X = $FF
BPL LOOP ; WHILE X > $FF
DONE RTS

BNE:
Bra
nchNotEqua
l
Des
cri
ption:
Exe
cute
sthebr
anchi
ft la
heZ- g(ze
rola
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
sAfe
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BNE Address D0 FF

Note:hezeola
r g,uponwhi c
ht hi
sde pe
nds ,i
sc ondit
ionedby :ADC,AND,ASL,
BIT,
CMP,
CPY,CPX,
DEC,DEX,DEY,EOR,INC,INX,INY,LDA,LDX,LDY,LSR,ORA,PLA,
PLP,
ROL,
RTS,SBC,
TAX,TAY,
TXA, andTYA.

Uses:Oftenusedinl
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-la
g(si
gnlag)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
sAfe
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BPL Address 10 FF

Note:hesi
gnla g,uponwhi c
ht hi
sde pe
nds ,i
sc ondit
ionedby :ADC,AND,ASL,
BIT,
CMP,
CPY,CPX,
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
tisa
lso
use
di ngener
alt
ode t
ectt
hest
atusoft
hehighbi
t.Here
’sourf
amil
iarke
ypr
ess
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
sAfe
cte
d:None

Addre
ssingModes CommonSy nt
ax He
xCodi
ng
Rel
at
iveonly[
65C02
] BRA Address 80 FF

Uses:BRA( branchalways)isusef
ul f
orwrit
ingrel
ocat
ablecode.Normal
ly,i
fyou
hadal oopwi thaJMPba cktothet opyouwouldma kethisr
eloc
atabl
ebyforc
ing
abr a
nc h.hi swoul dinvolvesett
ingorclear
ingaS t
atusRegis
terlagandthen
is
suingt hea ppropri
atebr anc
hi nstr
uct
ion.Inst
ead,youc ansimplyi
ssueBRA
wit
houtc hangingtheStatusRegistrla
e gs.heonl yli
mitat
ionisthema x
imum
branchingdi s
tanceofplusormi nus128bytes
.

Ex
ampl
e:
8000: A9 12 2 LOOP LDA #$12
8002: EA 3 NOP ; MORE CODE HERE
8003: 80 FB 4 BRA LOOP

BRK:
BRe
aK(
sotwa
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
el
yf ol
lowedbytheS tat
usRe gi
st
er,inwhi chtheBRKbi thas
beenset
.hepr oces
sorthenjumpstot headdre
ssat$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)whichproduc estheoutput.

Fl
ags&Re
gis
ter
sAfe
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 nthepro-
gramc ome stoas creec
hingha l
t,onecane xa
mi nethes ta
tusofv ari
ousme mory
l
oc at
ionsa ndr egi
ster
st os e
eife ver
ythingisa syout hinki tshouldbe .hi s
processc anbef ormaliz
ed, a
ndhe nceconside
r ablyimpr ovedon, byusingasoft-
wa r
eut i
li
tycall
e dade buggerwhi c
ha l
lowsy out os t
ept hroug hapr og r
am one
i
ns t
ruc ti
ona tat i
me .Munc h-A-
Bug ,al
ongwi thot her
s ,providest hi
soption.On
Inte
ge rAppl es,apr imit
iveS t
epa ndTr acefunc ti
oni spr ovide daspa r
tofthe
Moni tor.

BVC:
Bra
nchonoVe
rlowCl
ear
Descr
ipt
ion:Exec
utesthebra
nchonlyiftheV-lag(ov
erlowla g
)iscl
ea.he
r
ove
rlow lagiscl
eare
dwhe nevert
heresultofanopera
tiondidnotent
ailt
he
ca
rryofabitfr
om posi
tion6topos
it
ion7. heoverlowla ga
lsocanbecl
ear
ed
wit
haCLVc ommand.

Fl
ags&Re
gis
ter
sAfe
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BVC Address 50 FF

Not
e:heov
erlowla
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 lowf
r 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.he
a
dvantag
eisthatthec
arryre
ma i fe
nsuna c
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
rlowSe
t
De scr
ipt
ion:Exe
cutesthebr
anchonlywhe ntheV-lag(ovelowla
r g)iss
ethe
.
ov e
rlowla giss
etonlywhenther e
s ul
tofanope rat
ioncaus
esac arr
yofabi t
from pos
iti
on6t opositi
on7.Notethatther
ei snotacomma ndtos pec
iica
ll
y
settheovelowla
r g(aswouldcor
respondtoaSECc omma ndforthecarr
y)but,
intheApple,t
heinst
ructi
onBIT$FF58isofte
nus edtosett
heovelowla
r g.

Fl
ags&Re
gis
ter
sAfe
cte
d:None

Addre
ssingMode
s CommonSy
nta
x He
xCodi
ng
Rel
at
iveonly BVS Address 70 FF

Not
e:heov
erlowla
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 lowf
r 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
sAfe
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
etheir
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
sAfe
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:hea r
it
hme t
icmodeoft he6502isanimport
antpointt
ok e
epinmind
whenusi
ngtheADC andSBCinst
ruc
tions
.Ifyouareinthewrongmodefr
om
whaty
oumi ghtass
ume ,ra
therunpredi
cta
blere
sult
scanoc c
ur.Seet
heSED
i
nst
ruc
ti
onentryf
ormor ede
tai
lsontheothe
rmode.

CLI:
CLe
arI
nte
rruptma
sk
De
scr
ipt
i hi
on: si
nst
ruc
ti
one
nabl
esi
nte
rrupt
s.
Fl
ags&Re
gis
ter
sAfe
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 :CLItell
sthe6 502t orecognizeincomingIRQ(Interr
uptReQue s
t)si
gnal
s.
heAppl e
’sdefaul
tist ohavei nte
r r
uptsenabl
edbut,afte
rtheirsti
nterr
upt,
all
s
uc ceedinginter
ruptsa redis
abledbyt he6502unti
laCLIi sre-i
ss
ued.Asama t-
t
e rofi nt
eres
t,ti
mi ng-dependentr outi
nesli
ketheDOSRWTS( Read/
Wr i
teTrack
Sector)routi
nedi s
ableinterr
upt swhileonandthena l
lowthema gai
nwi t
haCLI
atexit.

]
3
60 As
sembl
yLi
nes

CLV:
CLe
aroVe
rlowla
g
Des
cri
pti
on:hi
scl
ear
st la
heV- g(
ove
rlowla
g)bys
ett
ingt
heVb
itoft
heS
ta-
t
usRegi
st
ert
o0.

Fl
ags&Re
gis
ter
sAfe
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

Uses:Beca
usetheov erlowla gis
,infac
t,cl
ear
edbyanon- ovelowr
r e s
ultofan
ADCinst
ruct
ion,itusuall
yisnotneces
sar
ytoc l
ea hela
rt gpr
iort
oa na ddit
ion.I
t
is
,however,occa
s i
onall
yus edasarel
at
ivel
yunobtrus
ivewayoffor
cingabr anch
whenwr i
ti
ngr el
ocatablecode.
hisisdonei nama nnersi
mila
rtotheCLC,BCCorSEC,BCSpairsdiscuss
ed
inchapt
er15 heg
. eneralfor
mi s
:
CLV
BVC ADDRESS

hist
echni
quehasthea dvant
ageofnotafe
cti
ngt
hec
aryla
r g,s
houl
dthe
us
erwa
nttotes
tthec
arr
ya ftert
hefor
cedbrea
k.

CMP:
CoMPa
ret
oAc
cumul
ator
Descr
ipt
ion:CMP compare
st heAccumulatortoas pe
ciiedval
ueorme mory
l
ocati
on.heN- lag(si
gnlag )
,Z-la
g( z
erola g
),andC-lag(c
aryla
r g)a
recon-
di
ti
oned.A condi
ti
onalbranchisusuall
ythendonet odeter
minewhet
herthe
Accumula
torwaslesst
han,equal
to,orgre
atertha
nthedata.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓
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.her out
inetypi
cal
ly
dec
ideswhethertheresul
tisl
esst
han,equalto,org r
eatert
hanac r
it
ica
lval
ue.
heus ualpat
ter
ni s
:
BCC: Ac
cumul
at
or<v
alue
BCS: Ac
cumul
aor≥v
t a
lue
BEQ,
BCS: Ac
cumul
at
or>v
alue
S
eet
hes
ect
iononBCCt
hroug
hBCSf
ors
pec
iice
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 he
n. ref
ore
,theexampl
ecrea
tesanendles
sloopwhi
chwi
ll
ne v
erte
rminat
e.
Si
mila
rly
,iftheBCCisdoneirsti
twillneverbet
ake
nbecaus
etherei
sno
v
aluel
esst
han0totrig
gerit
.

CT]hi
2[ sshoul
dpr
oba
blys
tat
e“i
tshoul
dnotbeus
edwi
thBCSorBCC.
”I sinet
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
iie
d
va
lueormemor yl
ocat
ion.Se
eal
soCMP.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

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
ingusedastheofseti
ntheAbsolut
e,
Xa ddre
ssi
ngmode.heX- 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
iie
d
va
lueormemor yl
ocat
ion.Se
eal
soCMP.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

3[
CT]S
imi
la
rtoCMP,
i sinet
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
:heY- 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
evetheir
stiv e
by
tesofanAppleoftpr
s ogramli
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:hec onte
ntsoft
hespecie
i dmemoryloca
tionarede
cre
me nt
edby
one.I
ftheori
gi
nalcontent
swereequa
lto$00,t
hentheresul
twil
lwrapar
ound
andgi
vear e
sul
tof$FF.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓ ✓

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
anofse
t)orat
wo-byt
ememorypoi
nte
r.Herea
ret
hecommone
xampl
es:

4[
CT]S
imi
la
rtoCMP,
i sinet
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

Afte
rtheDECope ra
tion,theN-and/orZ-lagsoftenarechec
kedtose
eift
he
r
esul
twa snega
tiveorazero/nonze
rovalue,r
espect
ively.
het e
chniqueshownf orthetwo-byt
eDECope ra t
ionisnotnec
ess
ari
lyt
he
mos ic
te ient
.SeetheSBCe ntr
yforanalt
ernat
iveme thod.

DEX:
DEc
reme
ntt
heX-
Reg
ist
er
De
scr
ipt
ion:heX-
Reg
ist
eri
sde
cre
ment
edbyone
.Whe
ntheor
igi
nalv
aluei
s
$00,
ther
esul
twi
llwr
apa
roundt
ogi
vear
esul
tof$FF.
Seea
lsoDEC.

Fl
ags&Re
gis
ter
sAfe
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 DEX CA

Use
s:DEXofteni
suse
dinrea
dingada
tabl
ockv
iai
nde
xeda
ddr
ess
ing
,i.
e.Abs
o-
l
ute
,X.Her
ei sasi
mplee
xample:
ENTRY LDX #$05
LOOP LDA DATA-1,X
JSR PRINT
DEX
BNE LOOP
DONE RTS
DATA ASC "!TSET"

Note:herearesev
era
l poi
ntsofi
nter
estinthise
xample.
Besi
desthegenera
luse
oftheX-Regi
sterintheindexedaddress
ingmode ,not
icethatt
heloopr uns
bac
k wa
rdsfr
om $05to$01.hel oopisterminat
edwhentheX- Re
gis
terreac
hes
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
oopi hi
sDATA-1. sal
lowst
heuseoft
he$05to$01va
lue
soft
he
X-Regi
ste
r.

DEY:
DEc
reme
ntt
heY-
Reg
ist
er
Descr
ipt
ion:heY- 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
sAfe
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 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
escansofte
na 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:hev alueintheAc cumula
torisexcl
usiveOR’dwi t
hthes pecie
i d
dat
a.heN- lag(si
gnla g)andZ-lag(zeola
r g)a
rea l
socondit
ioneddepending
ontheresult
.her es
ultisputbackintheAccumulator.heme mor yloc
a t
ion(i
f
spe
ciied)isunafec
ted.
EORme a
nsthatifei
therbi
t,butnotbot
h,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
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

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
:
()hemos
1 tcommoni stoencodeda
tabydoi
nganEORwit
hanarbi
tr
aryone
-
byt
ekey.hedatama 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.he
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
ciiedposi
ti
onswillr
eve
rse
,i.
e.one
swil
lbecomezer
os,a
nd
vi
c ev
ers
a .
Seetheir
stexamplei
nthisent
rytover
if
ythi
sefe
ct.
(3)heN- lag(si
gnlag)canbeuse
dt odet
ecti
fbothmemoryandtheAcc
umu-
l
atorhavebit7se
t:
ENTRY LDA MEM
EOR MEM2
BMI MATCH ; BOTH SET
BPL NOMATCH ; BOTH NOT SET

(4)heZ-lag(
zeola
r g)la
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
ipton:hec
i onte
ntsofaspeciiedme mor
yloc
ati
ona r
einc
rement
edby
one.I
ftheorigi
nalval
ueis$FF,the
ni ncr
ementi
ngwil
lre
sul
tinawraparound
gi
vingar e
sultof$00.heN- la
g( s
ignla g
)andZ-lag(ze
rolag)ar
econdi-
ti
oneddependi
ngont heres
ult
.

]
3
68 As
sembl
yLi
nes

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓ ✓

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
susedmostoftenf
ori
ncre
ment
ingaone
-byt
eva
lue(
suc
ha nof-
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

Afte
rt heINC ope
rat
ion,theN-a nd/orZ-la
gsofte
na r
ec heck
edt
ose
e
whe
thertheres
ultwasneg
a t
iveoraz
ero/nonze
roval
ue,
respe
cti
vely
.

INX:
INc
reme
ntt
heX-
Reg
ist
er
De s
cri
pti
on:hec ont
ent
soft
heX-Regi
st
era r
eincr
emente
dbyone .
Iftheori
gi-
nalval
ueis$FF,
theni
ncr
ement
ingwil
lres
ultinawraparo
undg i
vi
ngar esul
tof
$00.heN- lag(si
gnlag
)andZ-lag(ze
rola g)a
recondi
tione
ddependingon
theres
ult
.

Fl
ags&Re
gis
ter
sAfe
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 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:hec ont
ent
soft
heY-Regi
st
erareinc
remente
dbyone .
Iftheori
gi-
nalval
ueis$FF,
theni
ncr
ement
ingwil
lres
ulti
nawr aparo
undg i
vi
ngar esul
tof
$00.heN- lag(si
gnlag
)andZ-lag(ze
rolag)arecondi
tione
ddependingon
theres
ult
.

Fl
ags&Re
gis
ter
sAfe
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 INY CB

Uses
:INY isuse
di nf or
ward-sc
a nni
ngloopswhichus etheindi
rec
tinde
x e
d
addr
ess
ingmode ,forexampleLDA($FF),Y.hi sisquit
ecommoni nrout
ines
whic
hpr oc
essst
ringsf
orc e
rtai
nc hara
cte
rs,s
earc
hr outi
nes
,et
c.Hereisar
ou-
t
inewhichsca
nstheinputbuferfortheirs
tcarr
iag
er et
urn:

]
3
70 As
sembl
yLi
nes

ENTRY LDY #$00


STA PTR
LDA #$02
STA PTR ; PTR,PTR+1 = $200
LDY #$00
LOOP LDA (PTR),Y
CMP #$8D ; CHR = <CR>?
BEQ FOUND
INY
BNE LOOP ; UNTIL Y = $00
DONE RTS
FOUND STY MEM
BEQ DONE ; (ALWAYS)

JMP:
JuMPt
oaddr
ess
De
scr
ipt
ion:
Caus
espr
ogr
ame
xec
uti
ont
ojumpt
othea
ddr
esss
pec
iie
d.

Fl
ags&Re
gis
ter
sAfe
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 :he6
e 502hasawe l
l-
docume nt
e dbugrega rdi
ngt heindirectjump.5Ifthe
jumps pecie
i 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 e
d.Inst
ead,
theaddressda t
awillberet
rieve
d( i
noure xample )fr
om loc a
tions$3FFa nd$300.
ha tistos a ythatthehigh-orderbyteisnotpr operl
yi ncrement e
da ndbot h
bytesareretrie
v e
df rom t
hes amepa geofme mor y.hi sshouldbet ake
ni nto
accountifsuchas i
tuati
oncanpos si
blyari
seiny ourroutine.

Uses
:Be s
idestheob vi
ousappli
cat
ion oftheus ua
la bs
oluteaddr
ess
edJMP
i
nst
ruct
ion,theindi
rectJMPi
susedwhenc rea
tingvect
ore
dj umps.heApple
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)

CT]hi
5[ six
sbugi 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 nofsetcanbec a
lcula
tedand
t
hevect
orssetups othattheJMPwillberel
ati
vetothec urre
ntlocat
ionofthe
c
ode.S
eechapter15formor ei
nfor
ma ti
onaboutthes
etechniques
.

JSR:
Jumpt
oSubRout
ine
De sc
r i
ption:hea ddre
ssoftheinst
ructionf
oll
owingtheJSRi
spus
hedontot
he
s
ta ck.hea ddre
ssfoll
owingt heJSR isthe
nj umpedto.WhenanRTS i
nthe
call
eds ubrout
ineisencounte
red,areturntotheloc
ati
ononthest
ack(t
heone
afte
rtheJSR)isdone.hi sisanal
ogoustoaGOSUBinBAS I
C.

Fl
ags&Re
gis
ter
sAfe
cte
d:None

Addres
singMode
s CommonSy
nta
x He
xCodi
ng
Absol
uteonl
y JSR $1234 20 34 12

Us e
s:JSR isoneoft hemos tc ommonl yus edi nstr
ucti
ons,beingusedt ocal
l
ofte
n-ne e
de dsubroutines.hedi sadvantageofthei nst
ruct
ioni st
hatiftheJSR
refe
rencesa na ddresswithinthec ode(a soppose dtoroutinesexte
rnaltothe
program,s ucha sint heMoni torROM) ,thecodec anbee x e
cutedonlyatthe
l
oc at
ionforwhi chthec odewa sor i
ginall
ya ss
embl ed.
Becausethec al
lingaddressi ssavedont hes tack
,aJSRt oak nownRTSc an
bedone ,andt heda tacanber etrievedtode ter
mi newhe r
einme morytherou-
ti
neisc urr
e nt
lybe i
nge xecut
ed.
Seecha pt
er1 5formor edeta i
lsaboutbothoft heset
opics.

LDA:
Loa
DAc
cumul
ator
Descr
iption:LoadstheAc
cumul
atorwit
heit
herthespecie
i dv a
lueort
hecon-
te
ntsofthede s
ignat
edmemoryl
ocaton.heN-
i lag(s
ignla g
)andZ-lag(
zero
lag)arecondit
ionedwhenaval
uewiththehi
ghbi ts
etisloade
d,orwhena0
val
ueisloaded.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

]
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.hevastmaj
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
ciiedva
lueorthec
onte
nts
ofthedesignat
edmemoryloca
tion.heN- lag(si
gnla g
)andZ-lag(ze
rolag)
areconditi
onedwhenav al
ueisl oa
dedt
ha thasthehighbi
tset,orwhena0
val
ueisloaded.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

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:hisi
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
ciiedva
lueorthec
ont
ent
s
ofthedesigna
tedmemoryloc
aton.heN-
i lag(si
gnla g
)andZ-lag(ze
rolag
)
arecondit
ionedwhenaval
uewitht
hehi
ghbitse
tisl
oaded,
orwhe na0val
uei
s
l
oa ded.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

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:hisist
hepr
imar
ywayi
nwhi
chda
tai
spl
ace
dint
otheY-
Reg
ist
er.
SeeLDX
f
oraddi
ti
onalc
omment
s.

LSR:
Log
ica
lShi
tRi
ght
Desc
ripton:hi
i sins
truc
ti
onmov ese a
c hbitoftheAc cumula
tororspecie
i d
memorylocat
iononeposi
tiontother i
ght.A0isforcedatthebi
t7posit
ion(
the
hi
gh-orde
rbit
),andbit0f
allsi
ntothec a
rry.her es
ultisl
efti
ntheAccumulat
or
ormemorylocat
ion.(
Seeal
s oASL,ROL,andROR.)

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

0 ✓ ✓ ✓ ✓

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.hec or
respondi
ngefecti
n
de
cimala
rit
hmeti
ciswe
llknown:123
/10=12.
3(s ftr
hi ight
).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
sAfe
cte
d:None

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly NOP EA

Uses:NOPisusedprimari
lytodisablepor t
ionsofc odewri
tte
nbyot herprogra
m-
me r
sthatyouha vedeci
dedy ouc anli
vewi thout.Acla
ssi
cexa mpleofthi
sisthe
plac
ingoft hre
eNOPsa tbytes$D3,$D4,a nd$D5onTr ack0,S ect
or9,ofas t
an-
dardDOS3 .3dis
kette
.Byt hestrate
gicpl aceme ntoft
heseNOPs,abootwillnot
for
ceac l
earofthelanguagecard,thusav oidingtherat
hermonot onousLOADING
LANGUAGE CARDme ss
ageone veryboot.6

Addit
ionall
y,NOPsma ybeus eddur i
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
ipton:hi
i sinstr
uctiontakeseachbi
toftheAc c
umulatorandpe r
formsa
l
ogic
alinclus
iveORwi t
he achcorres
pondingbitoft
hes pe
ciiedme moryloca-
t
ionorimme di
ateva
lue.her esulti
sputba c
kintheAccumulator
.heme mor y
l
ocat
ion,ifspecie
i d,isuna fe
cted.Condit
ionst
heN- lag(si
gnla g)andZ- lag
(
zerolag)dependingonther es
ult.(
Seeals
oANDa ndEOR.
)ORAme ansifei
theror
bot
hbitsare1thentheresulti
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
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓
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
ciie
db itposi
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
Des
cri
pton:hi
i spushe
sthecont
ent
softheAccumulat
orontot
hes
tac
k.he
Ac
cumulat
ora
ndStat
usRegi
st
erar
eunafe
cted.
(Seea
lsoPLA.
)

Fl
ags&Re
gis
ter
sAfe
cte
d:None

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly PHA 48

Uses:hisisoneofthemos tcommonwa ysoftempor ar


ilys
tori
ngab y
teortwo.
Iti
scombi nedwithPLAt oretr
iev
etheda ta.General
lyspeaki
ng,e
achPHAmus t
bema t
chedbyaPLAl aterintherouti
ne.OtherwisetheinalRTSofyourrout
ine
wil
ldel
iveryou,notbacktothec a
ll
ingBAS ICpr ogr
am orimme di
atemode,but
rat
herofintotheweeds,asthesayi
ngg oes.
He r
eisane xa
mpl eofasimplestor
e /
ret
riev
eope ra
tion:
ENTRY LDA #$80 ; TEST VALUE
PHA ; STORE IT
LDA #$FF ; DESTROY ACC.
PLA ; RETRIEVE VALUE
STA MEM ; SAVE IT TO LOOK AT
DONE RTS

Anot he
rmor eobs
cur
eus eofPHAistosetupa nar
tiici
alJMPbyexe
cut
ing
anRTS forwhichaJSRwa sneverdone.Provi
dingthattwoPHAsha vebeen
donepri
ortotheRTS,t
hepseudo-
jumpwi l
lbeexecut
ed.
S eechapt
er15f
ormore
det
ail
saboutthi
s.

PHP:
Pus
HPr
oce
ssors
tat
us
De
scr
ipton:hi
i spushestheSt
atusReg
ist
eront
othest
ackforla
terretr
iev
al.
heSt
atusReg
ist
eri
ts
elfi
suncha
ng e
dandnoneoft
here
gis
ter
sareafec
ted.

Fl
ags&Re
gis
ter
sAfe
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
tingforaspecic
i
condit
ion.hisishandyi
fy oudon’
twanttotetala
s gri
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
ignla
gor
ca
rrywhenit
’smostconv
eni
ent
.
ENTRY CLC ; CLR CARRY
PHP ; SAVE REG
SEC ; SET CARRY
PLP ; RETRIEVE REG
BCC DONE ; (ALWAYS!)
BRK ; (NEVER)
DONE RTS

ENTRY LDA #$00 ; SET Z-FLAG


PHP ; SAVE REG
LDA #$FF ; DESTROY
PLP ; RETRIEVE
BEQ DONE ; (ALWAYS!)
BRK ; (NEVER)
DONE RTS

Aswi t
ht hePHA i
nst
ructi
on,PHP a
lwaysshoul
dbeac
compani
edbya nequal
numberofPLPinst
ruc
tionstokeeptheAppleha
ppy
.Remember
:It
’snotni
cet
o
fool
thest
ack!

PHX:
Pus
HX-
Reg
ist
er[
65C02]
Desc
ript
ion:hispushest
hecont
ent
softheX-Re
gis
teront
othesta
ck(
65C0
2
onl
y)heX-
. Re
gis
tera
ndStat
usRe
gis
tera
r fe
euna ct
ed.(S
eeal
soPLX.
)

Fl
ags&Re
gis
ter
sAfe
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
torirst
.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:hispushest
hecont
ent
softheY-Re
gis
teront
othesta
ck(
65C0
2
onl
y)heY-
. Re
gis
tera
ndStat
usRe
gis
tera
r fe
euna ct
ed.(S
eeal
soPLY.
)

Fl
ags&Re
gis
ter
sAfe
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 heAccumulaorir
t st.I
tiscombinedwithPLY (
or
anot
herpul
linst
ruct
ion)toret
ri
evethedat
a.Se
ePHXforausa
geexample
.

PLA:
Pul
LAc
cumul
ator
Descri
pton:hi
i si
sthec onve
rseoft
hePHAinst
ruct
ion.PLAre
tri
eve
soneby t
e
fr
om thest
acka
ndpla
cesitintheAcc
umulat
or.hisaccordi
ngl
ycondit
ionst
he
N-lag(si
gnlag)a
ndZ- lag(zerola
g),j
ustast
houg hanLDAinst
ruct
ionhad
beendone.

Fl
ags&Re
gis
ter
sAfe
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 PLA 68

Use:hi
s si sc
ombinedwit
hPHAtore
tri
eveda t
af r
om thes ta
ck.SeePHAfora
n
ex
ampleoft hi
s.
Additional
ly
,PLAcanbeus
edtocancelac urrentRTS,muc hli
k eaPOPi
n
Appl
esoftBASIC.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:hi sisuseda fte
raPHPt ore
tri
evetheStat
usRegi
ste
rdat
afrom t
he
s
tack.heby t
eisputi nt heS ta
tusRegis
terandallofthelag
sa r
econdi
ti
oned
cor
respondingtot hest
atusofe ac
hbitinthebyt
eplacedthe
re.heAccumulat
or
andotherregi
st
er sareunafe ct
ed.(S
eePHP.)

Fl
ags&Re
gis
ter
sAfe
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 PLP 28

Uses
:PLPisusedt
or et
riev
et heSta
tusRegis
terafte
raPHPha sst
oredthelagson
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 er
ystra
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).hiscondit
ionst
heN-lag(
si
gnlag)a la
ndZ- g(z
erolag
),j
ustas
t
houghaLDXins
truc
ti
onha dbee
ndone.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

Addr
ess
ingModes CommonSy
nta
x He
xCodi
ng
I
mpli
edonly[
65C0
2] PLX FA

Use:hi
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).hiscondit
ionst
heN-lag(
si
gnlag)andZ-lag(z
erolag
),j
ustas
t
houghaLDYins
truc
ti
onha dbee
ndone.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

Addr
ess
ingModes CommonSy
nta
x He
xCodi
ng
I
mpli
edonly[
65C0
2] PLY 7A

Use:hi
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
t
Descr
ipt
ion:hi sinst
ruct
ionmov ese
ac hbitoftheAccumula t
ororthespe
ciied
memorylocat
iononepos i
tiont
ot heleft.
hec arr
ybitispushedintoposi
ti
on0
andisre
place
dbybi t7(thehigh-or
de rbit.heN-
) lag(signla g
)a ndZ-lag
(
z e
rolag)ar
ec ondit
ione
dde pe
ndingont here
sultoft
heshift.
(Seeals
oASL,LSR,
andROR.
)

ROL−Rot
ateOneBi
tLe
t ROR−Rot
ateOneBi
tRi
ght

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓ ✓ ✓

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
hbithe
.
di
sadvant
agetote
sti
ngforthehighbiti
nt hi
swayist
hatt
hec ont
entsmustt
hen
beres
tore
dwi t
hacorre
spondingRORinstr
ucti
on.
ROLisusedmoreofte
ni ncombinati
onwithASLinmult
iplyanddivi
derou-
ti
nes.

ROR:
ROt
ateRi
ght
Descript
ion:hisins
truct
ionmoveseachbitoft
heAc c
umul at
ororthes pecie
i d
me morylocat
iononeposit
iontot
her i
ght.hec a
rrybitispushe
dint oposit
ion
7(thehigh-or
derbit
),andisre
pla
cedbybi t0.heN- lag(sinla
g g
)a ndZ-lag
(ze
rola g)areals
oconditi
oneddependingonther e
sultoftheshift.(Se
ea l
so
ASL,LSR,andROL.
)

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓ ✓ ✓

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
ternat
ewayoftes
ti
ngf ortheodd/eve
nna t
ureofanum-
be.hec
r arr
yistes
tedafte
rthes
hiftt
ode t
ectwhe t
herthenumbe rwasoddor
ev
en.
RORindsgreat
erus
ewhe nc
ombinedwi t
ht heshiftoper
ati
onsincre
ati
ng
mult
iplya
nddivi
derout
ines.

RTI:
ReTur
nfr
om I
nte
rrupt
Desc
ript
ion:hisres
tore
sbotht heProgr
a m Counte
randtheSt
atusRegi
st
erin
pr
eparat
iontor
esumingtheroutinebei
nge x
e c
utedatt
hetimeofthei
nter
rupt
.
Al
llagsoftheS
tat
usRegist
erarerese
ttothei
rorigi
nalv
alue
s.

]
3
82 As
sembl
yLi
nes

Fl
ags&Re
gis
ter
sAfe
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 RTI 40

Us es:RTIi
susedi nmuc hthesamewa ytha tanRTSwoul dbeus e
di nre
turni
ng
from aJSR.Aftera ninte
rrupthasbee
nha ndl edandtheba ckgr
oundope r
ati
on
pe r
formed,thereturnisdonev i
atheRTIc omma nd.Us uall
ytheuserwil
lwant
tor est
oret
heAc cumul at
or,theX-Regi
st
er ,andtheY- Re g
ist
erpri
ortoretur
n-
ing.
RTI al
soise quival
enttoaPLPRTSi nt hattheStatusRegis
terisre
stor
ed
from thest
ackandar eturnisdonetot
hea ddr e
ssonthest a
ck.

RTS:
ReTur
nfr
om Subr
out
ine
Des
cri
pton:hi
i sres
torest
hePr
ogram Counte
rt othea ddr
essst
oredonthe
s
tac
k,us
uall
ytheaddr
essoft
hene
xtins
truc
ti
ona fte
rtheJSRthatc
all
edther
ou-
t
ine
.Anal
ogoustoaRETURNt
oaGOSUBi
nBAS IC.(Seeal
soJSR.)

Fl
ags&Re
gis
ter
sAfe
cte
d:None

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly RTS 60

Uses
:RTSi s
,sur
pri
singl
yenough,mos toftenus e
dt oreturnfrom subrouti
nes.
Onoc c
asionitcanbeus edtos i
mul at
e aJMP i nst
ruct
ion byus i
ngt woPHA
i
nstr
uct
ionstoputafal
sere
turnaddressont hesta
cka ndthenex e
cutingtheRTS.
Se
ethesecti
ononPHA,andals
ocha pt
er15f ormor edeta
il
s .
AnRTSc anbePOP’
donelevelbytheexe cut
ionoftwoPLAi nstructi
ons.

SBC:
SuBt
rac
twi
thCa
rry
De scri
pt i
on:S ubtr
act
st hec ont
ent
soft heme moryl ocat
ionort hes peciied
va l
uefrom theAc cumulator.heoppos i
teofthecarryisalsosubtra
cted,andi n
thisinstancethecarr
yisc al
le
dabor r
ow. heN- lag( s
ignla g),V-lag(overlow
la g)
,Z- lag(zerolag),
andC- la
g(caryla
r g)areallcondit
ionedbyt hisope r
a-
tion,andt heyofte
na r
eus edtodete
ctthenatureofther e
sultofthesubtract
ion.
her esultofthesubtr
actionisputbackintheAc cumul a
tor.heme mor yloca-
Appe
ndi
xB:
Ass
embl
yComma
nds 3
83

t
ion,i
fspe
ciie
d,i
sunc
hang
ed.
SBCwor
ksf
orbot
hthebi
nar
yandt
heBCDa
rit
h-
meti
cmodes.

I
mportant:AnSECshoul
dal
waysbedonebef
or heir
et s
tSBCopera
tion.hisi
s
e
quiva
lenttoc
lea
ringt
hebor
rowandisana
log
oust
otheCLCdonebef
or ea
nADC
i
nst
ruct
ion.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓ ✓ ✓

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
dmostoftenforsubt
ract
ingacons
tantormemoryv
aluef
rom
ei
the
raone
-by
teme
moryl oca
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
i hi
on: ss
etst
hec
aryla
r goft
heS
tat
usRe
gis
ter
.

Fl
ags&Re
gis
ter
sAfe
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.hec
i ar
ryisoccas
ional
ly
usedt hought oindicat
ee r
ror( orother)condit
ions,asi sdone byRWTS
(Read/Writ
eTr a
ckS e
ctor)inDOS .Inthes
eins
tancesSECisusedt
os ett
hec a
rry
toindicat
eane rr
or.hi swouldbede t
ect
edsome t
imelateri
nprog r
a mexecu-
ti
on, fte
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
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

Addr
ess
ingMode
s CommonSy
nta
x He
xCodi
ng
I
mpli
edonly SED F8

Uses:BCDmathisusedwhenagre
aterdeg
reeofprec
isi
onisre
qui
red.I
nthi
s
modee ac
hfourbi
tsofabyt
ere
pre
sentonedig
itofaba
se-t
ennumbe
r.Herei
sa
br
iefexampl
eofaBCDaddi
ti
onoper
ati
on:
ENTRY SED ; SET DEC MODE
CLC
LDA #$25 ; %00101001 = DECIMAL 25
ADC #$18 ; %00011000 = DECIMAL 18
Appe
ndi
xB:
Ass
embl
yComma
nds 3
85

STA MEM ; RSLT = %01000011 = DECIMAL 43


CLD ; CLR DEC MODE
DONE RTS

SEI:
SEtI
nte
rruptdi
sabl
e
Des
cript
ion:
SEIisus
edtodisa
blet
hei
nterr
uptre
spons
etoanIRQ(
amaska
ble
i
nte
rrupt
).hisdoesnotdi
sabl
ethere
sponsetoanNMI(Non-
Mask
abl
eInt
er-
r
upt=RESET).

Fl
ags&Re
gis
ter
sAfe
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 ect
orpa thf
rom
$FFFE,$FFFF to$3FE,$3FF.heus e
ri sexpe ctedtous eCLIt ore-enabl
einte
r-
ruptsupone ntrytohisorhe rowni nterruptr out
ine.DOSt y
pical
lydoesa
SEI/CLIo per
ationupone nt
rancet
oa nde xitfrom RWTSs othatinte
rruptsdonot
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
iiedmemory
l
ocaton.hec
i onte
ntsoftheAccumula
tora
renotcha
nge
d,norareanyoft
he
St
atusRegi
st
erlags.

Fl
ags&Re
gis
ter
sAfe
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
onst heina
oputt lr
esul
tint
oaspec
iie
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
ciiedmemor
y
l
ocat
ion.heX- Re
gis
teri
sunchang
edandnoneoftheSta
tusRe
gist
erla
gsar
e
afe
cted.

Fl
ags&Re
gis
ter
sAfe
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
soft’
sde t
ermi-
nati
onofs t
ringlengt
hs.Aftergetti
ngda t
afr
om theinputbufer($200t o$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
ciiedmemor
y
l
ocat
ion.heY- Re
gis
teri
sunchang
edandnoneoftheSta
tusRe
gist
erla
gsar
e
afe
cted.

Fl
ags&Re
gis
ter
sAfe
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
heir
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
erlag
sar
eafe
cted.

Fl
ags&Re
gis
ter
sAfe
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
fe
nota c
ttheAcc
umulat
or.

Fl
ags&Re
gis
ter
sAfe
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 TAX AA

Uses
:Mos tsi
mply,TAXisus
e dfortr
ansf
erri
ngdat
afr
om theAc
cumul
atort
othe
X-
Re g
ist
er.Equal
lyimporta
nt ,howeve
r,isit
scombi
nat
ionwit
hTYAtotra
nsf
er
da
tafrom theY-
Reg i
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
fe
nota c
ttheAcc
umulat
or.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓
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 tsi
mply,TAYi
susedfortr
ansf
erri
ngdat
afr
om theAc
cumul
atort
othe
Y-
Regist
er.Equal
lyimport
ant,howeve
r,isit
scombi
nat
ionwit
hTXAtotra
nsf
er
da
tafrom theX-
Reg i
st
ert
otheY- 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
rbitsinas pe
ciie
d
memorylocat
ion(65C02only.heAc
) cumul
a t
ori
suncha
nged,butt
heZ- la
g
(
zeola
r )la
g giscondit
ione
dbase
dont hev
alueoft
hos
eme morybit
spriort
o
t
heoper
ation.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓

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

hi
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
ciiedme m-
orylocati
on(65C0
2onl y
).heAc c
umulatori
sunchanged,butt
heZ- lag(z
ero
lag)la gisc
ondit
ionedbas
edonthev al
ueofthoseme morybi
tspriortothe
operat
ion.

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓

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:hisput
sthec
ont
entsoft
heS
tac
kPointeri
ntot
heX-Re
gis
ter
.he
N-lag(s
ignlag
)a ndZ-la
g(zeola
r g
)arecondi
ti
oned.heSta
ckPointe
ris
uncha
nged.
Fl
ags&Re
gis
ter
sAfe
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 TSX BA

Uses:hemos tobvi
ousus eofTSXi
sinpr es
ervingtheval
ueofthesta
ckatacer
-
tai
npoint.Si
milart
ot heuseofPLAswi thRTS,thiscoul
dbeus edtodupli
cat
e
BAS I
C’sPOPcomma nd–t hatist
os ay
,adi r
ectr e
turntoadiferentle
velt
han
theonewhichhadact
ua l
lycal
ledasubrout
ine.Forexample:
ENTRY LDA #$00 ; DUMMY OPERATION
TSX ; SAVE CURRENT RETURN PTR
JSR LEVEL1
RTS ; NORMAL EXIT, BUT IT WILL NEVER BE CALLED
LEVEL1 TXS ; PUT PTR TO 1ST RETURN BACK
DONE RTS ; EXIT TO MAIN CALLING PROGR
Appe
ndi
xB:
Ass
embl
yComma
nds 3
91

Not ethatthisi ss ome whatda ngerousint hatyoumus tbev eryc ertainast o


thea ctualc onte
nt soft hes ta
c k
,a ndint hek nowl e
dg ethatt hedataha snotbe en
chang edbyi nterme dia t
ePHAsa ndPLAsf ori nstance.Re me mbe rt hatt heS tack
Pointe risonl yap oint ert othesta c
ka nddoe snotpr es
e r
v ether e
tur na ddr es
sa s
such, butonl yitspos itioni nthes tack.
Anot herusef orTSXi sinretriev
ingda tafr om thest ackwi t
houtha vingt odo
aPLAi nstructi
on. Al thoug haPLA/ PHA/TAXs eque ncewoul dbet ranspar enttothe
stac
k ,a nda ccompl isht hes amer esult
s,TSXc anb eus edt or et
rieveinf orma ti
on
thatiso ic iall
ylosta tt ha tpoint.Wha tIa ma lludingtoi sr etr
ievingda tathatis
lowe rinme mor yt ha nt hec urrentS t
ackPoi nt er,andt hatwoul dbeov erwr i
tt
en
byt hene xtPHAi nstruc tion.Oneoft hepr imee x a
mpl esoft hisisinus ingaJSR
toak nownRTSi nt heMoni torfornoot herpur posetha ntobea bletoi mme di-
atel
yr e t
rievetheot he rwi setranspa r
entr et
urna ddress.hi sisdones ot hatrelo-
catablec odeha sawa yofindi ngoutwhe r
ei t
’sc urrentlyloc at
ed.S eec hapter1 5
forat hor oughe xplana t
ionoft het echnique.Forqui ckr ef
e rence,
he re’stheba sic
routine :
ENTRY JSR RETURN ; $FF58
TSX
LDA STACK,X ; $100,X
STA PTR+1
DEX
LDA STACK,X ; $100,X+1
STA PTR ; PTR,PTR+1 = ENTRY+2
DONE RTS

Cauti
on:MostS t
epa ndTr a
ceut i
li
ti
eswi l
lnotpr operl
yt r
acec odelikethis
beca
useofthesome whatil
le
galuseoft hestac
k .St
rict
lyspeaking,goodpr o-
gra
mmi ngpr
inci
plesdic
tat
ethatoncedataisoicial
lyoft hest
ack ,i
tiscounted
asbei
ngefec
tiv
elylost
.hisisespeci
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:hisputsthecont
ent
softheX-Reg
ist
eri
ntot
heAccumul
ator
,and
t
huscondit
ionstheSta
tusRegi
ste
rjus
ta si
fanLDAhadbeene
xecut
ed.heX-
Reg
ist
eri
suna fe
c t
edbytheope
rati
on.
(Seeal
soTAX.
)

Fl
ags&Re
gis
ter
sAfe
cte
d:
N V – B D I Z C Ac
c X Y Me
m

✓ ✓ ✓

]
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
rforappropr i
ate
processi
ngbyt heprogram.I nthecaseofstr
ing-
re l
at
edr outi
nes,t
hisisofte
nt he
l
e ngt
hoft hestri
ngjustenteredorscanned.heAc cumul at
orcantheng oa bout
thethingsi tdoessowe lli
nt er
msofput ti
ngt hev al
ueintothemos tusefulpart
ofme mor y .Not i
cethattherea r
emor ea ddres
singmode sava
ila
blet otheSTA
comma nd, nottome nti
ont heoveral
lpowe rsgr
a nt
edtheAc cumulatorinte r
ms
oflogi
c alope r
ators
.
Asdi scussedunde rTAY,TXAc anbec ombine dwit
hTAYt oform aTXY- l
ike
(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:hi sput
sthecont
entsoftheX-Re
gist
eri
ntotheSt
ackPoint
er.
NoneoftheSt
atusReg
ist
erla
gsar
ea fe
cted,
nori
stheX-
Regi
ste
rit
sel
fcha
nged.

Fl
ags&Re
gis
ter
sAfe
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
hereisno
TAS(Ac c
umul a
tortoS ta
ck)ore venTYS( Y-Regist
ertoS t
a c
k )
,thisi
stheonl y
waytog etaspecicby
i teintotheStackPointer
. hisus ual
lyisusedinconjunc-
ti
onwi t
hTSXt orest
oreprevi
ouslysaveddata.I
nt hecaseoftheAppl e
softs
tack-
ixprogram,itisuse
dt oavoidproble
mst hatotherwis
ewoul doc curi
faRESUME
werenotusedafte
rane 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:hisputsthecont
entsoft
heY-Reg
ist
erint
otheAc
cumulat
or,
and
t
huscondit
ionstheSta
tusRegi
ste
rjustasi
fanLDAha dbee
nexec
utd.heY-
e
Reg
ist
eri
suna fe
c t
edbytheope
rati
on.(S
eeal
soTAY.
)

Fl
ags&Re
gis
ter
sAfe
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 TYA 98

Uses:TYAp rovi
desawa yofretri
evi
ngt heva
lueintheY-Regis
terforappropr
iat
e
proce
ssingbyt heprog ram.hi 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
erle 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).heope 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
Manual
,cour
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
carry LDY Loa
dY- Regi
ste
rwithmemory
AND ANDme morywi thAc cumula t
or LSR S ftr
hi ightonebit(
memoryor
ASL Shiftl
eftonebi t( me mor yorAc cu- Ac c
umul at
or)
mul at
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 Rotateleftonebi t(me mor yor
BVC Branchonov erlowc lear Ac c
umul at
or)
BVS Branchonov erlows e t ROR Rotaterightonebi t(me mor yor
CLC Clearcarr ylag 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 erlowla g SBC Subtractme mor yf rom Ac cumula-
CMP Compa reme mor yandAc cumul a- torwithbor row
tor SEC Setcarryla g
CPX Compa reme mor yandX- Regis
ter SED Setdecima lmode
CPY Compa reme mor yandY- Regis
ter SEI Setinterruptdisablestatus
DEC Dec re
me ntme mor ybyone STA StoreAc cumul at
ori nme mor y
DEX Dec re
me ntX-Re gisterbyone STX StoreX- Regist
eri nme mor y
DEY Dec re
me 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
hef
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
tonebi
tle
t)a
ndLSR(
shi
tonebi
tri
ght
)

Fi
gur
eC-
2:ROL−Rota
teonebi
tle
t Fi
gureC-
3:ROR−Rot
ateonebi
t
(
memoryorAc
cumulat
or) ri
ght(me
moryorAc
cumul a
tor
)

]
3
98 As
sembl
yLi
nes

6502I
nst
ruc
tionCode
s
heTi meisgi
veninclockcycl
es(1μsat1MHz ).Forti
meswit
ha“ +”,add1ifapage
bounda
ryi
scross
ed.Forbranc
hinstr
uct
ionswit
ha“ *”,
add1ifthebranchist
aken,a
nd
add1moreift
hebranchcross
esapageboundar
y.Fortimeswi
tha“ d”,add1ifinde
ci-
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+C→ Immediat
e ADC #$FF 69 2 2d NZC---
AddAcc
umula
torto A,C ZeroPag
e ADC $FF 65 2 3d
memor
ywit
hcarr
y ZeroPag
e,X ADC $FF,X 75 2 4d
Absol
ute ADC $FFff 6D 3 4d
Absol
ute
,X ADC $FFff,X 7D 3 4d+
Absol
ute
,Y ADC $FFff,Y 79 3 4d+
(I
ndir
ect
,X) ADC ($FF,X) 61 2 6d
(I
ndir
ect
),Y ADC ($FF),Y 71 2 5d+

AND A∧M →A Immediat


e AND #$FF 29 2 2 NZ----
ANDAccumul
at
orwi
th ZeroPag
e AND $FF 25 2 3
memor
y ZeroPag
e,X AND $FF,X 35 2 4
Absol
ute AND $FFff 2D 3 4
Absol
ute
,X AND $FFff,X 3D 3 4+
Absol
ute
,Y AND $FFff,Y 39 3 4+
(I
ndir
ect
,X) AND ($FF,X) 21 2 6
(I
ndir
ect
),Y AND ($FF),Y 31 2 5+

ASL s
eeFi
gC-
1 Ac
cumulat
or ASL 0A 1 2 NZC---
S ftl
hi eftonebi
t(me
mor
yor Ze
roPage ASL $FF 06 2 5
Accumulato
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 M7→N Abs
olut
e BIT $FFff 2C 3 4
bi
tsinmemory M6→V

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
lla
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
erlow Br
anc
hV=0 Re
lat
ive BVC $FF 50 2 2* ------
cl
ear
BVSBr
anc
honov
erlows
etBr
anc
hV=1 Re
lat
ive BVS $FF 70 2 2* ------
CLC Cl
earc
aryla
r g5 0→C I
mpl
ie
d CLC 18 1 2 --C---
CLD Cl
earde
cima
lmode 0→D I
mpl
ie
d CLD D8 1 2 ----D-
CLICl
eari
nte
rruptdi
sa e 0→I
bl I
mpl
ie
d CLI 58 1 2 ---I--
CLV Cl
earov
erlowla
g 0→V I
mpl
ie
d CLV B8 1 2 -----V
CMP A←→M 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 X←→M 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 Y←→M 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 M−1→M 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 X−1→X I
mpl
ie
d DEX CA 1 2 NZ----
DEY De
cre
mentYby1 Y−1→Y I
mpl
ie
d DEY 88 1 2 NZ----
EOR Av M →A 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+1→M 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+1→X I
mpl
ie
d INX E8 1 2 NZ----
I
NY I
ncr
eme
ntYby1 Y+1→Y I
mpl
ie
d INY C8 1 2 NZ----
J
MP PC+1→PCL Absol
ute JMP $FFff 4C 3 3 ------
J
umpt
one
wloc
ati
on PC+2→PCH (I
ndir
ect
) JMP ($FFff) 6C 3 5/66

5[
CT]CLC,
CLD,
andCLVha
dthewr
ongS
tat
usRe
gis
terla
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+1→PCL
i
r
etur
na ddr
ess PC+2→PCH
LDA M →A 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 →X 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 →Y 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 Ac
cumulat
or LSR 4A 1 2 NZC---
S ftr
hi ightonebi
t(me
mor
y Ze
roPage LSR $FF 46 2 5
orAccumulat
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 →A 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 Ac
cumulat
or ROL 2A 1 2 NZC---
Rot
ateonebi
tlft(
e memor
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 Ac
cumulat
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+1→PC
SBC A−M−C→A 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
aryla
r g 1→C I
mpl
ie
d SEC 38 1 2 --C---
SED S
etde
cima
lmode 1→D I
mpl
ie
d SED F8 1 2 ----D-
SEIS
eti
nte
rruptdi
sabl
e 1→I I
mpl
ie
d SEI 78 1 2 ---I--
STA A→M 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 X→M 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 Y→M 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 A→X I
mpl
ie
d TAX AA 1 2 NZ----
TAY Tr
ans
ferAt
oY A→Y I
mpl
ie
d TAY A8 1 2 NZ----
TSX Tr
ans
fers
tac
ktoX S→X I
mpl
ie
d TSX BA 1 2 NZ----
TXA Tr
ans
ferXt
oA X→A I
mpl
ie
d TXA 8A 1 2 NZ----
TXSTr
ans
ferXt
ost
ack X→S I
mpl
ie
d TXS 9A 1 2 ------
TYA Tr
ans
ferYt
oA Y→A 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
heTi meisgiv
eninc l
ockcy
cle
s(1μsat1MHz )
.Fortimeswit
ha“ +”,a
dd1i fapage
boundar
yiscr
osse
d.Fortime
switha“d”,a
dd1i findeci
ma lmode.histabl
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+C→A,
rt C(I
ndi
rec
t) ADC ($FF) 72 2 5d NZC---
memorywi
thc
arr
y
AND ANDAcc
umul
aor A∧M →A
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 M7→N Zer
oPa g
e,X BIT $FF,X 34 2 4 NZ---V
M6→V 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 A←→M (
Indi
rec
t) CMP ($FF) D2 2 5 NZC---
andAc
cumul
ator
DEC De
cre
mentA M−1→M Ac
cumul
aor DEC
t 3A 1 2 NZ----
EOR Ex
clus
iveOR Av M →A (
Indi
rec
t) EOR ($FF) 52 2 5 NZ----
Acc
umulat
orwit
hmemor
y
I
NC I
ncr
eme
ntA M+1→M Ac
cumul
aor INC
t 1A 1 2 NZ----
J
MP J
umpt
one
wloc
aton PC+1→PCL (
i Abs
olute JMP ($FFff,X) 7C 3 6 ------
PC+2→PCH I
ndi
rec
t,
X)
LDA LoadAcc
umul
at
or M →A (
Indi
rec
t) LDA ($FF) B2 2 5 NZ----
wit
hme mor
y
ORA Log
ica
lOR A∨M →A (
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 A−M−C→A (
Indi
rec
t) SBC ($FF) F2 2 5d NZC--V
fr
om Awithbor
row
STA S
toreAc
cumul
at n A→M
ori (
Indi
rec
t) STA ($FF) 92 2 5 ------
memory
STZ S
tor
eze
roi
nme y 0→M
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 →M
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 →M 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 J
MP ADC ROR
6
x RTS (
zp,
x) zp z
p z
p
PLA
# A (
ind) a
bs a
bs
BVS ADC ADC S
TZ ADC ROR ADC JMP ADC ROR
7
x r
el (
zp)
,y (
zp) z
p,x z
p,x z
p,x
S
EI
abs
,y
PLY
(
abs
,x)a
bs,
xabs
,x
BRA STA S
TY S
TA S
TX BI
T S
TY STA S
TX
8
x r
el (
zp,
x) zp zp zp
DEY
#
TXA
abs a
bs abs
BCC STA STA S
TY STA STX STA S
TZ S TA S
TZ
9
x r
el (
zp)
,y (
zp) z
p,x z
p,x z
p,y
TYA
a
bs,
y
TXS
abs a
bs,
xabs,
x
LDY LDA LDX LDY LDA LDX LDA LDY LDA LDX
Ax # (zp,
x) # zp zp zp
TAY
#
TAX
a
bs abs abs
BCS LDA LDA LDY LDA LDX LDA LDY LDA LDX
Bx r
el (
zp)
,y (
zp) zp,
x zp,
x zp,
y
CLV
abs
,y
TS
X
abs
,x a
bs,
x a
bs,
y
CPY CMP CPY CMP DEC CMP CPY CMP DEC
Cx # (zp,
x) z
p z
p z
p
I
NY
#
DEX
abs abs abs
BNE CMP CMP CMP DEC CMP CMP DEC
Dx r
el (
zp)
,y (
zp) zp,
x zp,
x
CLD
abs
,y
PHX
abs
,xabs
,x
CPX S
BC CPX S
BC I
NC S
BC CPX S
BC INC
Ex # (z
p,x
) z
p zp zp
I
NX
#
NOP
abs abs a
bs
BEQ SBC SBC S
BC INC SBC SBC I
NC
Fx r
el (
zp)
,y (
zp) z
p,x z
p,x
S
ED
a
bs,
y
PLX
a
bs,
xabs,
x

Abbr
evi
ati
ons Addr
ess
ingMode
s
#=i mme di
ate a
bs,x=i nde xe
dbyX
A=Ac c
umul at
or a
bs,y=i nde x
edbyY
abs=absolute (
abs)=i ndirect
rel=rel
ativ
e (
abs,x)=i ndexedabs
oluteindir
e c
t
zp=z er
opa ge z
p,x=i nde xedbyX
x=X- Regist
er z
p,y=i nde xedbyY
y=Y- Regis
ter (
zp)=i ndi r
ect
(
zp,x)=i ndexedindi
rect(pre-
indexed)
(
zp),
y=i ndirec
tinde
xed( post
-index
e d)

]
Ap
pendi
xD:
Moni
torS
ubr
out
ine
s
Hereisal istofsomeus ef
ulsubrout
inesint heAppl e
’sMonitorand
Autosta
rtROMs .Tousethesesubrouti
nesf
rom a ss
embly -
langua
geprogra
ms,
l
oadt heproperme mor
yloc a
tionsor6502regis
tersasrequiredbythesubr
ou-
t
inea ndexecuteaJSRtot hesubrouti
ne’
sstart
ingaddress.Itwil
lper
formthe
f
unc t
ionandreturnwit
hthe6 502’
sregi
ste
rssetasdesc
ribed.

Out
putSubr
out
ine
s

$FDED COUT Outputac ha r


acter
COUT i
sthestandardchar
acteroutputsubroutine.hec hara
ctertobeoutput
shoul
dbei ntheAc cumulat
or.COUTcal
lsthec urrentchara
c t
eroutputsubr
ou-
ti
newhos eaddres
sisstor
edinCSW( loc
a t
ions$36a nd$37),usual
lyCOUT1(s
ee
be
low)
.
$FDF0 COUT1 Out
putt
oscr
een
COUT1di
spl
ayst
hec ha
rac
teri
ntheAc
cumul
at
orontheAppl
e’
ssc
ree
natt
he
curr
entoutputcurs
orpos iti
onanda dv
ancest
heoutputcurs
or.Ithandle
st he
cont
rolc har
act
ers,<RETURN>,li
nefe
ed,andbell
.Itret
urnswitha l
lregis
ters
int
act
.Cha r
act
ersinther angeof$00to$3Fcomeoutinver
se;char
acte
rsfrom
$40to$7Farelashi
ng ;c
haracte
rsfr
om $80t
o$FFa r
enormal.
$FE80 SETINV SetI
nversemode
Set
sInve
rsevi
deomodef orCOUT1.Al
loutputcha
rac
ter
swillbedispl
ayeda
s
bl
ackdot
sonawhi teback
ground.heY- Regi
st
erisse
tto$3F;allothe
rsar
e
uncha
nged.
$FE84 SETNORM SetNormalMode
Set
sNor malv
ide
omodef orCOUT1.Al
loutputcha
rac
ter
swillbedi
spl
ayeda
s
whitedot
sonablackbackground.heY- Regi
st
eriss
etto$FF;al
lothe
rsar
e
unchange
d.
$FD8E CROUT Gene
rat
ea<RETURN>
CROUTse
ndsa<RETURN>cha
rac
tert
othecur
rentout
putde
vic
e.
$FD8B CROUT1 <RETURN>withcle
ar
CROUT1c
lea
rst
hescre
enfrom t
hec
urrentc
urs
orpos
it
iontot
hee
dgeoft
het
ext
window,
thenc
all
sCROUT.
$FDDA PRBYTE Pri
ntahexade
cimalbyt
e
hissubr
outineoutputst
hecont
ent
softheAcc
umulatori
nhe x
ade
cimalont
he
c
urre
ntoutputdevi
c e
.hec ont
ent
soft
heAccumulat
oraresc
rambl
ed.
Appe
ndi
xD:
Moni
torS
ubr
out
ine
s 4
05

$FDE3 PRHEX Printahexa


deci
ma l
digit
hissubrouti
neoutput
sthelowerni
bbleoftheAccumula
torasas
ing
lehe
xa-
de
cimaldigi
t.hecontent
softheAcc
umul a
torar
esc
rambled.
$F941 PRNTAX Pri
ntAa ndXi nhexa
deci
ma l
hi soutputsthec ontent
soft heAccumulatorandX-Regi
st
erasaf our-
digi
t
hexadec
ima lval
ue.heAc cumulat
orcontai
nstheirs
tbyt
eoutput
;theX-Regis
-
te
rc ont
ainsthesecond.hec onte
ntsoftheAccumula
torar
eusual
l
ys c
rambled.
$F948 PRBLNK Print3spaces
Out
put
sthrees
pa c
ec hara
c t
erstothestandar
dout putde
vice
.Upone
xit
,the
Acc
umula
torus
uall
yc ont
ains$A0,t
heX- Regi
st
ercontai
ns0.
$F94A PRBL2 Pri
ntmanyspaces
Output
sfrom 1t o256s pac
ec harac
ter
stothesta
ndardout
putdev
ice.Upon
ent
ry,t
heX- Re
gis
tershouldconta
inthenumberofspa
cest
obeout
put.I
ftheX-
Regi
ste
ris$00,t
henPRBL2wi l
loutput256bl
anks
.
$FF3A BELL Out
puta“bel
l”char
act
er
Se
ndsabell
(<CTRL>G)cha
rac
tert
othec
urre
ntoutputde
vic
e.I
tle
ave
stheAc
cu-
mula
torhol
ding$87.
$FBDD BELL1 Beept
heAppl
e’ss
peake
r
Beepst
heApple’
sspeakerf
or0
.1s
econda
t1KHz.
Itscr
amble
stheAc
cumul
at
or
andY-Reg
ist
er.

I
nputSubr
out
ine
s

$FD0C RDKEY Getaninputcharac


ter
hisist
hestanda
r dchara
c t
erinputsubr
outine.I
tplac
esalashinginputcur
sor
onthesc
ree
na tthecurrentcursorposi
ti
ona ndjumpstotheinputsubrout
ine
whosea
ddres
sisstor
edinKSW( $38,$39)
,usuall
yKEYIN(se
ebelow).
$FD35 RDCHAR Getani
nputcha
ract
erores
capecode
RDCHARi
sana
lt
ernat
einputs
ubr
out
inewhi
chget
schar
acte
rsf
rom t
hesta
nda
rd
i
nputbuta
lsoi
sca
pabl
eofi
nte
rpr
eti
ngt
hee
lev
ene
sca
pec
ode
s.
$FD1B KEYIN ReadtheAppl e’
skey
board
hisi
sthek ey
boardinputsubrouti
ne.ItreadstheApple
’ske
yboar
d,wait
sfora
k
eypr
ess,andrandomizestherandom- numbers e
ed.Whenitget
sakeypre
ss,i
t
r
emovesthelashi
ngcursorandr e
turnswiththekeycodeint
heAccumula
tor.
$FD6A GETLN Getaninputli
newi t
hprompt
GETLNist
hesubroutinewhic
hg at
hersi
nputline
s.Yourprog
ramscanca
llGETLN
withtheprope
rpr omptc hara
cterinloca
tion$33;GETLNwil
lre
turnwit
ht he

]
4
06 As
sembl
yLi
nes

i
nputlineintheinputbufer(
beginni
nga
tloc
ati
on$200)a
ndt
heX-
Reg
ist
er
hol
dingthel
engt
hoft hei
nputl
ine
.
$FD67 GETLNZ Getaninputli
ne
GETLNZi
sanal
ter
na t
ee nt
rypoi
ntforGETLNwhichis
sue
sa<RETURN>t
othes
tan-
dardout
putbe
for
ef al
li
ngintoGETLN(s
eeabove)
.
$FD6F GETLN1 Geta
ni nputl
ine,
noprompt
GETLN1 i
sana
lt
ernateent
rypoi
ntf
orGETLN whi
chdoesnoti
ssueapr
ompt
bef
oreitga
the r
stheinputli
ne.
If,
howev
er,theuserca
ncelst
hei
nputli
ne(ei
ther
wit
htooma nyba c
kspacesorwit
ha<CTRL>X),thenGETLN1wi
lli
ssuethecon-
te
ntsofloc
ation$33a sapromptwhe
nitget
sa not
herli
ne.

Low-
ResGr
aphi
csSubr
out
ine
s

$F864 SETCOL Setl


ow-re
sgra
phi
csc
olor
hissubrout
ines
etsthecol
orus
edf
orpl
ott
ingont
hel
ow-r
esscr
eent
othec
olor
pa
sse
di ntheAcc
umul at
or.
$F85F NEXTCOL I
ncre
mentcol
orby3
hi
sadds3t
othecurre
ntcol
orus
edforl
ow-
resgr
aphi
cs.
$F800 PLOT Pl
otablockontheLow-
Re sSc
reen
hissubrouti
neplot
sas i
nglebloc
kont helow-r
esscr
eenofthepre-
spec
iied
c
olorhebl
. ock’
svert
ica
lposi
ti
onispasse
dintheAccumula
torandit
shori
zon-
t
alposi
ti
onintheY-Regis
ter
.PLOTret
urnswit
htheAccumulat
orscr
amble
d,but
t
heX-Regist
erandY-Regi
ste
rareunmolest
ed.
$F819 HLINE Dr awahorizontallineofblocks
hissubr
outinedrawsahor i
zontall
ineofblocksoft hepr e
-speciiedcoloron
t
helow-r
esscree
n.Yous houldc a
llHLINEwiththeve r
ticalc
oordina t
eoftheli
ne
i
ntheAc cumulat
or,theleftmosthorizont
alcoordinateintheY- Regi
st
er,and
t
her i
ght
mos thori
zontalc oordi
nateinl oca
tion$2C.HLINE r eturnswiththe
Acc
umulatorandY-Re g
ist
ers c
rambled,butwit
ht heX- Regi
ste
rint act
.
$F828 VLINE Drawav ert
icall
ineofblocks
hissubrouti
nedr awsavert
icalli
neofblocksofthepre-s
pe c
iiedc ol
oronthe
l
ow-re
ssc r
een.Yous houl
dca l
lVLINEwiththehorizont
alcoordinat
eoftheli
ne
i
ntheY- Regi
ste
r,thetopv e
r t
ic
alcoordi
nateintheAc cumulator,a
ndt hebot-
t
om vert
icalcoordinat
einl ocat
ion$2D.VLINE r
eturnswiththeAc c
umulator
s
cra
mbl e
d.
Appe
ndi
xD:
Moni
torS
ubr
out
ine
s 4
07

$F832 CLRSCR Cle


artheent
irel
ow-re
sscre
en
CLRSCR c
lea
rst
hee nti
relow-
resg
raphi
csscr
een.Ifyoucal
lCLRSCRwhi
let
he
v
ideodispl
ayisi
nTe x
tmode,i
twi
llil
lthes
cree
nwithi
nve
rse
-mode“@”c
har
-
a
cte
rs.CLRSCRde
str
oysthec
ont
ent
softheAcc
umula
tora
ndY-Reg
ist
er.
$F836 CLRTOP Clea
rthetopoft
helow-re
sScre
e n
CLRTOPi
sthes
amea sCLRSCR(
abov
e),e
xceptt
hati
tcl
earsonl
ythetop40r
ows
oft
hes
cre
en.
$F871 SCRN Rea
dthelow-ress
cree
n
hissubr
outi
nereturnsthecolorofas
ingl
eblockonthelow-r
esscr
een.Cal
lit
asyouwouldca
llPLOT( above
).heblock’
scolorval
uewi l
lbere
turnedint
he
Acc
umulator
.Noot herr
eg i
st
ersar
echang
ed.

Hi
-Re
sGr
aphi
csSubr
out
ine
s

$F3E2 HGR Hi-


respage1
hisisthee
ntr
ypoi ntfortheHGRcomma nd.I
tini
ti
al
iz
eshi
-r
espa
ge1
,the
n
c
lea
rsanddi
spl
aysthesc
reen.
$F3D8 HGR2 Hi-
respage2
hisisthee
ntr
ypoi ntf
ortheHGR2c
omma nd.I
tini
ti
al
iz
eshi
-r
espa
ge2
,the
n
c
lea
rsanddi
spl
aysthesc
ree
n.
$F3F2 HCLR Cl
eart
obl
ack
Cl
ear
sthec
urr
entscre
ent
obl
ack
1.
$F3F6 BKGND Cl
eartocolor
Cl
ear
sthec
urr
entscree
ntot
hel
astpl
ott
edHCOLOR.
$F6F0 HCOLOR Se
tcol
or
S
etst
hec
urr
entHCOLORt
othec
ont
ent
soft
heX-
Reg
ist
er(
0−7
).
$F411 HPOSN Posit
ionthec urs
or
Pos
it
ionst
hehi-
re s“cur
sor
”wi
thoutplott
ing
. EnterwithX,Y(l
ow, hi
gh)equa
l
tot
hehori
zont
alposit
ion,a
ndt
heAc cumulat
ore qualtothev
ert
ic
alposi
ti
on.
$F457 HPLOT Pl
ota tc
urs
or
I
dent
ic
alt
oHPOSN,butplot
scur
rentHCOLORatc
oor
dina
tesg
ive
n.
$F5CB HFIND Returnthecursorposit
ion
Returnsthecurrent“cursor”posi
ton.hi
i sisusef
ulafte
raDRAWtoindwhe re
you’v
ebe e
nleft.hec oordi
nate
sa r
er e
turnedin:$E0,$E1=h or
izont
al(
low,
high)
, $E2=vertic
al.

]
4
08 As
sembl
yLi
nes

$F53A HLIN Drawal i


ne
hissubr
outi
nedra
wsal i
nefr
om thepr e
viousplott
othepoi
ntgi
ven.
Oni nput
,
s
etA,X( l
ow,hi
gh)tothehor
izontalposi
tion,a
ndYe qualt
othever
ti
calposi
-
t
ion.
$F730 SHNUM Loadshapenumber
hi srout
ineputstheaddr
essoft
heshapenumbe ri
ndic
ate
dbyX-Regi
st
erinto
$1A,$1B.SHNUMretur
nswit
hX,Y( l
ow,hig
h)a l
sose
ttoaddr
essoft
hatshape
tabl
eentr
y .
$F601 DRAW Drawashape
Drawthesha
pepointedtobyX,Y(l
ow,hi
gh)i
nthecur
rentHCOLOR.Note
:X, Y
poi
nttothespe
ciicentr
y,nott
hebegi
nni
ngofthet
abl
e.Bes ur
et oca
llSHNUM
irs
t.
$F65D XDRAW Er a
s easha
pe(drawXOR)
Erase
sashapetha twasjus
tdra
wn(ifthere
)bydoingane xc
lusi
veORwitht
he
scr
eendat
a.Oni nput,l
oadX,Y(low,high)wit
ht headdressoft
hesha
peto
XDRAWorc
allSHNUMi rs
twit
htheX-
Re g
iste
requal
totheshapenumber.

Fl
oat
ingPoi
ntAc
cumul
ator

$EBAF ABS Abs ol


uteval
ue
hissubrout
inet
akesthea
bsol
utev
alueoftheFl
oat
ingPoi
ntAc
cumul
at
or(
FAC
=$9D−$A2).
$EC23 INT INTfunc
tion
heINTf unc
tionuse
sQINT(
$EBF2)t
oconve
rttheFACt
oint
ege
rfor
mandt
hen
backtoaloa
ting-
poi
ntnumberi
nFAC.
$EFAE RND Random number
hisisthes
ameast heRNDc
omma
nd.
Pr oduc
esa(
poorqua
li
ty)ps
eudo-
random
numberint
heFAC.
$EB82 SIGN Si
gnofFAC(inAcc
umulator)
S
etst
heAc
cumul
atorto$01,
$00,
or$FFi
ftheFACi
spos
iti
ve,
zero,
orne
gat
ive
.
$EB90 SGN SignofFAC(i
nFAC)
CalsSIGNir
l st,
thenset
sFACba
sedupontheAc
cumul
at
orva
lue
.
$EE8D SQR Squar
er oot
hi si
stheSQRcomma nd.I
tcompute
sthesquarer
ootofFACus
ingas
lowe
xpo-
nenti
ati
onmethod:X0.5.
$EF09 EXP Expone
nti
ati
on
hi sr
out
iner
ais
esetotheFACpowe
randle
ave
sthere
sul
tinFAC.
Appe
ndi
xD:
Moni
torS
ubr
out
ine
s 4
09

$E941 LOG Logari


thm ba
see
hi
scomput
est
helogar
it
hm (
bas
ee)ofFAC.
$EE97 FPWRT Rai
seARGt ot
heFACpowe r(bas
ee )
hiscompute
sARG tot heFAC powe
rusingthefor
mulaEXP(LOG(ARG)*FAC).
Be
for
ecal
li
ng,y
ous houl
dloadtheAccumula
torwi
thFACEXP(
$9D).
$EBB2 FCOMP CompareFACt ome mor y
Bef
orec
all
ing
,loadthememor yloc
ati
onintheY-Regist
erandAccumul
at
or.On
ex
it,A=$01ifthevaluea
ttheme moryloc
a t
ionisle
ssthanFAC;A=$00ifthe
memoryequal
sFAC;A=$FFi fthememoryisgrea
terthanFAC.
$EED0 NEGOP Mul
ti
plyby−1
hi sr
out
inet
ogg
lesthes
ignofFAC.
$E7A0 FADDH Add0.
5
hi
srout
inea
dds0
.5t
oFAC.
$EA55 DIV10 Di
videby10
hi
srout
inedi
vi
desFACby1
0.I
tre
tur
nsposi
ti
veva
lue
sonl
y.
$EA39 MUL10 Mult
iplyby10
hisrout
inemul
tipl
iesFACby1
0.I
twor
k sonbot
hpos
it
ivea
ndne
gat
ivenum-
be
rs.
$EFEA COS Cos
ine
hec
osi
nef
unc
ti
onofFAC.
$EFFA SIN Si
ne
hes
inef
unc
ti
onofFAC.
$EFF1 TAN Ta
nge
nt
het
ang
entf
unc
ti
onofFAC.
$F09E ATN Ar
cta
nge
nt
hea
rct
ang
entofFAC.
$ED34 FOUT Creat
eas t
ring
Cr
eateast
ri
ngatthest
artoft
hest
ack($100−$110)equi
val
entt
otheFACval
ue.
Onexitt
heY-Reg
ist
erandAccumula
torpointtothest
ri
ng.hestr
ingist
ermi-
na
tedbya$00.

]
4
10 As
sembl
yLi
nes

Ot
herSubr
out
ine
s

$FCA8 WAIT Dela


y
hi ssubrouti
nedel
aysf
oraspecica
i mountofti
me ,
the
nr e
turnstotheprog r
am
whichc a
lledi
t.hea mountofde
layi
sspecie
i dbytheconte
ntsoftheAc cumu-
l
atorA.hede l
ayisgiv
enby0 .
5102×( 26+2 7A +5A2)mi cr
oseconds.WAIT
re
turnswi t
htheAccumulat
orzer
oedandtheX-andY-Regis
tersundist
urbed.
$FB1E PREAD Rea
dagamec ont
rol
le
r
PREAD r
etur
nsanumberr
epr
ese
nti
ngthepos
it
ionofag amec
ont
rol
le
r.You
s dir
houl stpasst
henumberofthegamec
ont
rol
le
r(0to3)intheX-Regi
ste
r.I
f
thi
snumbe ri
snotva
li
d,st
rangethi
ngsma
yhappe
n.PREADret
urnswithanum-
berfr
om $00to$FFi
ntheY-Regis
ter
.heAcc
umulat
orissc
rambled.
$FF2D PRERR Pri
nt“
ERR”
Se
ndsthewor
d“ ERR” ,f
oll
owedbyabel
lcha
rac
ter
,tot
hes
tanda
rdout
put
de
vic
e.heAccumulatori
sscr
ambl
ed.
$FF4A IOSAVE Saveal
lre
g i
ste
rs
hec onte
ntsofthe6 502’
sint
erna
lreg
ist
ersaresav
edinl
ocat
ions$45t
hrough
$49intheorderA-X- Y-
P-S.heconte
ntsoftheAccumul
at
ora ndt
heX-Regi
s-
terar
echang
e d;t
hede ci
malmodeisc
lear
ed.
$FF3F IOREST Res
toreal
lre
gist
ers
hec ontent
softhe6 5
0 2
’si
nte
rnalreg
ist
ersar
el oa
dedf
rom l
oca
tions$45
t
hrough$49.
Ap
pendi
xE:
ASCI
IandS
cre
enChar
ts

YouGe
tWha
tYouASCI
IFor
..
.
hi sc hartshowsma nyoft hepos si
bleinterpretationsofaby tev aluei n
me mory .heir s
tthreecolumnss howt hehexv aluea ndi tsdecima la ndbi nary
equival
ents.hi sc anbeha ndywhe nc onvers
ionsa rene e ded.hene 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.
hes cre
enc olumns howswha tc haract
eri stobee xpecte
di ftha tv alueis
stor
e dinthesc r
eenme mor yarea
, $400−$7FF. Inversec ha ra
cter
sa res urr ounde d
bys quarebrackets[A],whilelashingc haract
ersa r
es urrounde dbya ng lebr ack-
ets>A<.
heAppl e oftc
s olumni ndic
a t
e showAppl esoftBAS ICi nt
erpre tst ha tbyte
whe ntok e
nizi
ngpr ograms.
Not ethatforcontrolcharac
t ers,t
he“ ^”sy mboli sus ed.husaCont rol
-A
woul dbeindicated^A.

He
x De
c Bi
nar
y Ke
y S
cre
en Appl
esot
$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
esot
$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
esot
$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
esot
$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
esot
$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
histabl
ewa sadaptedf
rom J
onBe t
tenc
ourt’
sApp
leIIInf
oArc
hiv
es,t
he
Appl
es
oftI
IBAS IC Prog
rammingRef
ere
nceMa nual
,andBeneat
hAppl
eDOS
(Wort
handLechner).

$0A−$0C JMPt oUSRrouti


ne $75,$76 Cu rr
entexe
cuti
onlinenumber
$18 Fi
rs tdat
atrac
k $77,$78 Linenumberwhe r
eENDorSTOP
$19 Fi
rs tdat
asect
or orBREAKoccurre
d
$1A,$1B S ha
pepointerf
orDRAW $79,$7A Ad dre
ssofl
inenumbe rbe
ing
$1C LastCOLORused e
xec
ute
d
$20 Leftedge(0−39/
79,def
aul
t0) $7B,$7C Cu rrentl
ine#ofDATA
$21 Wi dth(1−40or80,def
aul
t40, $7D,$7E Ne xtaddressofDATA
0c
ras
hesAppl
esoft) $7F,$80 Ad dressofINPUTorDATA
$22 Topma
rgi
n(−2
0 3
,de
faul
t0,
or $81,$82 Na meofl as
tusedv a
riable
2
0forg
raphi
cs) $83,$84 Ad dressoflas
tusedva ri
abl
e
$23 Bottom e dge(0−2 4,de f
ault24 ) $9B,$9C PointerforFNDLI N( $D61A)
$24 Ho rizontalcursorpos it
ion( 0−39/7
9) a
ndGETARYPT( $F7D9)
$25 Ve rti
calcursorpos i
tion( 0−23 ) $9D−$A3 Floating-
pointaccumul aorFAC
t
$26,$27 Ad dressofby t
ec ontainingX,Y $A5−$AB Floating-
pointargume ntARG
$28,$29 Ba sea ddressofc ursorpositi
on $AF,$B0 BAS ICpr ogra
me nda ddress
$2B BOOTs ot×1
l 6 $B1−$B6 Subr outi
netoincrea
set he
$2C Lo-resHLIN/ VLINe ndpoi nt s
tri
ngda
tapoi
nte
r
$30 COLOR × 1 7 $B7−$BE S
ubr
out
inet
ore
tur
nthes
tri
ng
$32 Tex tma sk($FF=No rma l,$3F= da
tapoi
nte
r’sc
har
act
er
Inve r
se,$7F=F las
hi ng) $C9−$CD Random numbe r
$33 Promptc haracter $D4 Err
or-codelag
$36,$37 Ad dressofoutputr out i
ne $D8−$DE ONERRp oi
nters
/scr
a t
ch
$38,$39 Ad dressofinputr outine $DF ERRSTKs t
ackpointe
r
$48,$49IOBa ddress $E0,$E1 Ho r
izont
alHPLOTc oordi
nat
e
$50,$51 Re sultofthec onv ersi
onoft he $E2 Vert
icalHPLOTcoordina
te
FACt oa1 6-
bitinteger $E4 HCOLOR( 0=0,
1=4 2
,2=85 ,
3=127,
$67,$68 Ad dressofbe gi
nni ngofBAS I
C 4
=12
8,5
=17
0,6
=21
3,7
=25
5)
prog r
a m( def
a ul
tis$0801) $E6 HGRp age
:HGR=$20, HGR2=$40
$69,$6A Ad dressofbe gi
nni ngofBAS I
C $E7 SCALEv al
ue(0=2 5 6)
v
ari
abl
es $E8,$E9 Addressofstartofshapeta
ble
$6B,$6C Addr
essofbe
ginni
ngofBAS
IC $EA DRAW/XDRAWc oll
is
ionc ounte
r
a
rra
ys $F1 SPEEDv al
ue(subtractedfr
om 256)
$6D,$6E Addr
essofe
ndofBAS
ICv
ari
- $F3 TextORma skforla shingte
xt
a
ble
s $F4−$F8 ONERRp ointe
rs
$6F,$70 Addr
essofst
artofst
ri
ngdat
a $F9 ROTvalue
$73,$74 Addr
essofhi
ghestBASIC $FF UsedbySTR$f unct
ion
me
mor
y+1
Appe
ndi
xF:
Zer
o-Pa
geMe
mor
yUs
age 4
19

Me
mor
yUs
ageTa
ble
hi sta
blecomesf
rom t hei
hecomp.sys.apple2FAQ. nfor
mati
onisdra
wn
fr
om theAppleI Ite
chni
calmanua l
s,Beneat
hAp pl
eDOS( DonWor t
ha nd
Pie
terLec
hner)
,andExpl
ori
ngAppleGS/OSa ndProDOS8(
Ga r
yB.Li
tt
le
).

Low Nibble of Address


Hi| 0 1 2 3 4 5 6 7 8 9 A B C D E F
0 | AP AP A A A A − − − − A A A A A A
1|A A A A A A A A A − − − − − − *
2 | M M M M M M MA3 MA3 M M M3 M3 MA3 MA3 M3 MA3
3 | M M M M M M3 M3B M3B M3B M3B MP MP MP MP M3P M3P
4 | M3P M3P M3P M3P M3P M3P M3P M3P M3P MP I3P I3P I3P I3P MP M
5 | MA MA MA MA MA MAI AI AI AI AI AI AI AI AI AI AI
6 | AI AI AI AI AI AI AI AI3 AI3 AI3 AI3 AI AI AI AI AI3
7 | AI3 AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI
8 | AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI
9 | AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI
A | AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI3
B | AI3 AI AI AI AI AI AI AI AI AI AI AI AI AI AI AI
C | AI AI AI AI AI AI AI AI AI AI AI3 AI3 AI3 AI3 I I
D | AI AI AI AI AI AI I I AI3 AI AI AI AI AI AI AI
E|A A A − A A A A A A A − − − − −
F|A A A A A A A A A A − − − − − −

M Moni tor;
* usedine a rl
yApple//
eROMs ,nowfree
A Apples oftBAS I
C
I Inte
ge rBAS IC
3 DOS3 .3
P ProDOS( $40−$4Ei
ssav
edbefor
eandr e
store
dafte
ruse)
B ProDOSBASIC.SYSTEM( al
sousesa
llAppleoftl
s ocat
ions
)
− Free;notus e
d

]
Ap
pendi
xG:
Beg
inne
r’sGui
det
oMe
rli
n
hi sse
c t
ionisadaptedfrom T.Pe ter
sen’s“Begi
nner’sGui det oUsingMe r
-
l
in,
”i nt heMe r
linProUs er’
sMa nua l
,Rog erWa gnerPubl ishing,19 8
4 .he
i
nstructi
onss houldworkonbot htheor igi
nalMe rl
inMa c r
oAs s
emb l
era ndthe
MerlinProMa cr
oAssemb l
er.
hepur poseofa na s
sembleri stot ransl
atehuma n-r
ea 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 sMerli
ny 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 .
heEx ecutiveCont rolModei st hema inme nuwhi cha ppearswhe nyou
startthepr ogram( seet hei magebe low) .hepr ompti si ndicate
dbyt he“ %”
character.heEx 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.
heEdi t
orCont rolModec ons i
s tsoft heEdi t
or ,theAs sembler,a ndthe
Link e.hepr
r ompti sindicate
dbyt he“ :”cha ract
er.heEdi torCont rolMode
l
e t
sy oue ntera ndmodi fyc ode,deinema cros,ass
embl ey ourcode,a ndl i
nkin
externaliles.

MERLIN-PRO 2.43
By Glen Bredon

C :Catalog
L :Load source
S :Save source
A :Append file
R :Read text file
W :Write text file
D :Drive change
E :Enter ED/ASM
O :Save object code
Q :Quit
Source: A$0901,L$0000
Drive: 2
%
Appe
ndi
xG:
Beg
inne
r’sGui
det
oMe
rli
n 4
21

Ge
tti
ngSt
art
ed
Asdi
scus
sedi
ncha
pte
rthr
ee,a
sse
mbl
ycodet
ypi
cal
lyha
sthef
oll
owi
ng
f
orm:
7 START JSR BELL ; RING THE BELL

Eachl inec onsist


sofs everalie lds:t
hel i
nenumbe r,a n optionallabel
(START),thecomma nd( JSR),theopera nd(usi
ngalabelsuc
ha sBELLoranum-
bersucha s$FBDD),anda nopt i
onalcomme nt
.InMe rl
in,t
hel i
nenumbe r
sa re
addeda utoma t
ical
lya ndc annotbee dited.Whenlist
ingora s
sembl i
ngt hepr o-
gram, a
llofthoseie l
dsa reseparatedbyt abchar
acte
rstoproduceni c
elyforma t-
ted output.Whe ni nputti
ngc ode,y ou needtypeonl yas ingle<SPACE> t o
advancefrom oneie ldtot hene xt
–y oudonotne e
dtoinsertt
absy ourself
.
Tog etstart
ed,trycreatingas hortprogramthatwillmakey ourAppl ebee p
once:
1. Booty ourMe rl
inorMe rli
nPr odisk .
2. Afte
rthemainExe
cuti
veCont
rolModeme
nua
ppe
ars
,ty
pe“
E”a
tthe“
%”
pr
omptt oent
ert
heEdi
torCont
rol
Mode.
3. Toe nteranew program,a tt
he“ :
”promptt ype“ A”(forAdd)andpr e
ss
<RETURN>.Yo ushoul
ds eea“ 1 ”appea
ra ndt hecur
s orshouldbeplac
e done
spacetother i
ghtofthatl
inenumbe r.Asy oue nt
ercodethelinenumberswil
l
advancea ut
oma t
ic
all
y.he selinenumbe r
sa reusedonlywhi l
eedit
ingcodein
theEditorandarenotpartofyouractua
lprogram.
4. Onl i
ne1 ,hit<CTRL>P.Alineofaster
isk
ss houlda ppea
r.Anast
eri
skasthe
ir
stchara
cterindicat
esac omme ntli
ne.Any t
hinga fte
rtheirsta
ste
ris
kwillbe
i
gnor
e dbythea s
sembler.Hit<RETURN>toacceptthelineandadva
ncetoli
ne2.
5
. Onl i
ne2 ,ty
peas ingl
e<SPACE>a ndthe nhit<CTRL>P.Youshoul
dnows ee
a
na st
eri
ska teit
herendoft heline .Spa
c eov e
raf ewc hara
cte
rsandthe
nt ype

DEMO PROGRAM 1”.Hit<RETURN>t oaccepttheli
ne.Youdonotne e
dtohavethe
c
ursoratthee ndoft hel i
newhe ny ouhi t<RETURN>–t heenti
reli
newillbe
a
ccept
ed,regardl
essofwherethec ursori
sl ocat
ed.
6
. Onl i
ne3,ag
ainhit<CTRL>Pandthen<RETURN>toini
shma
kingani
cel
y
f
orma
tte
dboxofas
ter
is
k scont
aini
ngyourpr
ogram name.
7. Onli
ne4,t
ypeasi
ngleast
eri
skandhit<RETURN>.From t
hispoi
nton,
itwi
ll
bea
ssumedt
hatyouhi
t<RETURN>tocomple
teeachline.
8. Onl i
ne5 ,hitt
he<SPACE>baronc
etoadvanc
etothecommandie l
d,ty
pe
ORG,hi
t<SPACE>a g
aintoadvanc
etotheope
randiel
d,the
nt y
pe$8000.Sofar
yourpr
ograms houl
dlookli
kethi
s:

]
4
22 As
sembl
yLi
nes

1 ********************************
2* DEMO PROGRAM 1 *
3 ********************************
4*
5 ORG $8000

h eORG deinestheor
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
useourir
stl
abe
l.Ty
pe:
BELL<SPACE>EQU<SPACE>$FBDD

hi sde inesthelabelBELLtobee qualtothehe xval


ueFBDD.Whe r
everyouuse
thelabe l
BELLi nane xpre
ssi
on,theas s
embl e
rwi l
lautomat
ic
all
yreplac
eitwi t
h
$FBDD.Wh ynots implyusethea ddress$FBDDe v
erywher
e?We l
l,usi
ngal abel
ma kesthec odeeasi
ert or
eadanda ls
oma kesiteas
iertoc
hangethelocat
ionin
thefuture.
10.Nowwene edtoringourbel
l.Onl i
ne7 ,t
ype:
START<SPACE>JSR<SPACE>BELL<SPACE>; RING THE BELL

Noti
cet
hatwest
art
edourcomme ntwit
has e
micol
on.
Anychar
act
erswit
hinthe
commentiel
dwil
lbei
gnored;us
ingthese
micol
onjus
tmak
e si
tcl
eart
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

Notethateachstr
ingofcha r
ac t
ersha
sbeenmov
edt ot
hec
orr
ectie
ld:
l
abe
ls,comma nds
,operands
,andc omme nt
s.I
nsummary
,whena
ddi
ngcode,
s
paceoncetoadvanc
etothenextield.

De
let
ingLi
nes
Ifyouma k
eami sta
k eornolongernee
dc ert
ainline
s,youc
andel
etel
ines
whil
eintheEdi
torControlMode.
Fore xa
mple:
1. Whi l
eyouareatthe“:”pr
ompt ,t
ypeD6<RETURN>.Nothi
ngcha
nge
sont he
sc
reen.
2
. Type“L”tol
is
tyourprog
ram.Noti
cet
hatt
heor
igi
nal
line6(
wit
htheBELL)
i
snowgoneandt
heremaini
ngli
nesha
vemovedup.
3
. Ty
peD5,6<RETURN>t
ode
let
ether
ang
eofl
ine
sfr −6
om 5 .
4
. Ty pi
ng“L”r
eve
alst
hatourpoorpr
ogr
am nowha
sonl
yonel
ineofc
odel
eft,
j
ustt
heRTS.

Caution:Noti
c etha
ttheautomati
crenumberi
ngca
usedthelinenumbe r
sto
s ftupwa
hi r
d.Ifyouinte
ndtodele
tese
v e
rall
ine
sinsuc
ces
sion,besuretos
tar
t
bydeleti
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
ie
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
oconir
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
ptthechangesandini
shedi
ti
ngtheli
ne.Ty
pe“L”to
li
styourprogramandconir
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 epeatst
eps1−4butskipt he<CTRL>I.
Notic
et hatwhenwedi d“ E8”a ndini s
hede diti
ngourl i
ne,wer et
urnedtoEdi -
torCont r
olMode .Youc ana l
s otypear angeofl ine
s,suchas“ E3,6.hi
” swi l
l
cal
lupe achlinefrom 3−6ins uccess
ion.Pr e
ssing<RETURN>willtakeyoutot 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
hene
xts
tepi
stoa
sse
mbl
eandr
unourc
ode
.Att
he“
:”pr
ompt
,ty
pe
ASM<RETURN>.
Ony
ours
cre
ens
houl
dappe
art
hef
oll
owi
ng:
UPDATE SOURCE (Y/N)?

Ty
pe“
N”a
ndy
ous
houl
dthe
nse
e:
1 ********************************
2 * DEMO PROGRAM 1 *
3 ********************************
4 *
5 ORG $8000
6 BELL EQU $FBDD
8000: 20 DD FB 7 START JSR BELL
8003: 60 8 DONE RTS

--End assembly, 4 bytes, Errors: 0

Symbol table - alphabetical order:


BELL =$FBDD ? DONE =$8003 ? START =$8000
Symbol table - numerical order:
? START =$8000 ? DONE =$8003 BELL =$FBDD
Appe
ndi
xG:
Beg
inne
r’sGui
det
oMe
rli
n 4
25

Ifthes y stem beepsa nddi splaysa nerrorme ssage,re


me mbe rthel i
nenum-
bertha twa sr efer
enceda ndpr es s<RETURN>unt i
lthea ss
embl ycompl etes.he n
goba ckt hroug hy ourpr ograma ndc ompa reitwiththel i
stingabov e.Us eyour
new-founde ditingskil
lstoc orr
e cttheline,thenre-assemblebyt ypingASM.
Tot hel e ftoftheli
nenumbe rswenows eethea ss
emble dma chinec ode.For
exampl e,theJSRBELLha sbe enc onve r
tedto20DDFB, wherethe20i st hehexa-
deci
ma lcodef orJSR andDDFBi stheBELLa ddressinr e
v ers
eby teor der.he
nextlinec ont ainsas i
ng l
eopc ode ,the60f ortheRTS, toret
ur nfrom ours ubrou-
ti
ne.Not i
c et ha tnoneoft hela bel
sorc omme ntsarewi t
hint hema chine-l
an-
guagec odeon t hel eft-hand side.Fi nall
y,wes e
et hatt hec odeha sbe en
ass
embl e da ta 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
. Att
he“
%”pr
ompt
,hi
t“S
”tos
avey
ours
our
cec
ode
. peail
Ty ena
mes
ucha
s
heil
DEMO1. ewi
ll
bes
ave
dwi
th“
.S”a
ppe
nde
dtoi
ndi
cat
etha
titi
sas
our
ceil
e.
3. Nowhi t“O”tosa
veyourobje
ctcode.Merli
nwil
ldi
spla
ythesmeil
a e
name
witha“?
”attheend.
Hit“Y”toacce
ptDEMO1a st
heobj
ectil
ename.Bec
auset
he
sourc
ecodeilehadt
he“.
S”appendedtoi
t,thetwoil
eswil
l onli
notc c
t.
4
. Nowhi t“Q”toqui
tMe r
lin.Ty
peCATALOGtover
if
ythatyourprogr
am was
s
ave he
d. nty
peBRUNDEMO1t
or unyourpr
ogram.Youshoul
dhe a
raBEEP!
Cong
rat
ulat
ions
!You’
vejustwr
itt
enyourirs
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
hepr og
ramsarecont
aine
dontwoloppydis
ks.
ForProDOSt heil
esa
rec
ontai
nedina
f
olde
rname dCODE.he.Ss uixi
ndi
cat
esaMe r
li
nsoureil
c e.he.Asuixindi
cat
esan
Appl
escr
iptil
e.Nameswit uixa
houtas recompi
ledobj
ectil
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
nchofs 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..
..
..
.
...
.
..
..
..
...
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.25
6,356
ABSs ubr outine .
.
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.
..
..
408 BRKv e
ctorlocati
on..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.35
6,370
absolutea ddr essing.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
18,45,12
8,328 BRUNc omma nd..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
.
.35
Accumul ator.
...
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
6,18 BSAVEc omma nd..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
113
ADC. ....
...
...
..
..
...
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
.66,274
,344 bufer
,me mory .
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
118
additi
oni na ssembl y.
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.6
5,265 BVC..
..
..
..
..
..
.
...
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.9
6,357
additi
on, two- byte.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
69 BVS..
.
..
..
..
..
..
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.9
6,358
addres s.
...
...
..
..
...
..
...
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
3,4
addres singmode s .
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
.18,4
5,328
C
ampe rs andv ect
or .
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
264 CALLc omma nd. .
...
..
..
...
.
...
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
6,15,1
43
AND. ..
...
..
....
.
...
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
92,278,32
2,345 caryla
r g.
...
....
......
...
...
..
...
.
...
..
..
..
.
..
..
..
..
..
..
.
..
38,66,90,2
73
Apple/ //.
...
..
..
..
...
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.336
,337 CATALOGc omma nd...
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.10
6
Apple/ /e.
...
..
..
...
..
...
.
..
..
..
..
13,94,291,
296,320,335-
337 CHl ocat
ion. ...
......
...
...
.
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.30
5
AppleI I.
..
....
..
..
..
..
...
..
..
..
.
..
..
..
..
..
.13
,53,94,296,33
6,411 CHKdi r
ectiv e.....
...
...
..
...
.
...
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
xii
i,
16
AppleI IPl us.
..
...
..
..
..
..
..
..
.
..
..
..
..
..
.5
,13,94,291,29
6,336 CHKCOM s ubr out i
ne .
..
. .
150,157,1
60,219,257,2
67
Apple softa rrayv ari
abl
es..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
154 CHKNUM s ubr out ine..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
158,163,2
19
Apple softBAS IC...
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
5,10,11,11
0,128 CHKS TRs ubr out ine .
.
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
159,1
65
Apple softv 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,31
3,411 CLI..
..
..
..
.
...
...
......
.....
..
...
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.35
9
ASL....
....
..
....
.
...
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
.89,186
,348 CLRS CRs ubr out ine ..
.
...
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.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.
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
.1
7,421 codelocat
ion, de termi ning.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.13
1
ATNs ubr outine..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
409 COLBYTEl oc a tion. .
..
..
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
198,211,2
13
COMBYTEs ubr out i
ne .
.
...
..
..
..
.
..
..
..
..
..
..
.
..
..
148,219,2
36
B comma ndie l
di na ssembly..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
.17,4
21
base16numbe rs.
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.4
,22 comme ntie 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,33
0,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
ttesoft-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 ft.
.....
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
.192,20
3,208
DOSbe l
lmodi ic ati
on..
...
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
88 hi
-r
esc olort a ble....
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.1
68,193
DOSbufe rpoi nt er.
..
..
..
...
.
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
85 hi
-r
ese nt rypoi nt s...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
168
DOSc at
alogk e
ymodi ic at
ion..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
87 hi
-r
esil l efe ct.
....
....
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
189
DOSdisk-vol umemodi icati
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..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
.
..
..
.9
7,365 HIMEM c omma nd. ..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.85,1
28,153
EQUdirecti
ve.
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
.
..
..
..
.
..
..
.1
7,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
lags
,Sta
tusRe g is
ter
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
22 i
mplie
da ddr e s
sing..
...
..
.
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.45
FLASHc omma nd...
..
.
...
.
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
285 I
NC...
..
..
..
......
...
..
..
..
..
...
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.23
,330,367
loat
ing-
poi nta ccumul at
or(FAC) .
..
..
.
..
..
150,236,
254 i
ncl
usiveOR. ..
.
...
..
..
...
..
..
..
.
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.97
lowofcont 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
ocat
ablecode..
..
.
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
128
J
MP..
..
..
..
.
..
..
...
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
28,139
,281,3
70 NOP..
..
..
.
..
..
.
..
..
..
..
..
..
..
..
..
..
.
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
.54,
374
J
MPs i
mulation..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
376,38
2 numeri
cregi
st
e r
s,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
yboadbufe
r r.
.
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
.42 opc
odes..
..
..
..
..
..
..
..
..
..
.
...
.
...
.
...
..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
.2,10
,17
ke
yboar
dinput..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
..
..
.
.42 OPENc omma nd..
.
..
..
..
..
..
...
.
..
..
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
125
ke
yboar
dinputswitc
h..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
291 ope
randie 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
abeliel
di na ssembly.
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
17,42
1 out
putv e
ctor.
..
..
..
..
..
..
..
.
...
.
...
..
..
.
...
..
..
.
..
..
..
..
.
..
.94,281
,370
LANGl ocation..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
.12
6 ove
rlowla 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....
..
...
..
..
..
..
..
..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
..
.91
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).
...
.
...
..
..
..
..
.
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
..
.47
mask,AND. .
..
...
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
93,96,292
,346 POKEc omma nd. .
...
.
...
..
..
..
..
.
..
...
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.14
4
mask,EOR. ..
..
...
..
..
...
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
366 POPc omma nd. .
...
...
.
...
..
..
..
..
.
..
...
.
..
..
..
..
..
.
..
..
..
..
.
..
..
136,37
8
mask,inve rsela g..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
285 POS Nloc ation. ...
..
...
..
..
..
..
..
..
..
.
...
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.323
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.
..
..
.
..
..
..
..
.
..
..
..
.
..
.93
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 .
...
..
..
...
..
..
..
..
.
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
..
.17
MOVMFs ubr outine.
..
..
..
..
.
..
..
..
..
..
.
..
..
163,219,25
8,267 pseudo-jump. ..
....
..
...
..
...
.
..
..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
376,38
2
MUL1 0s ubr outine .
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
409 PTRGETs ubr out ine .
..
..
...
157,161,163,21
9,260,26
7
mult
iplicationa nddi vis
ion.
..
..
..
..
..
..
.
..
..
..
..
..
90,265
,381
Munch- A- Bug ..
..
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.13
,357 R
RAM (
random a cc
essme mory)
..
..
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
.
..
.4
N ra
ndom numbe rgenera
tor.
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
..
.
.293
ne
gat
ivenumber
s..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
.72,
178 RDCHARs ubrout
ine.
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
..
.
.405
NEGOPs ubr
out
ine.
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
409 RDKEYs ubrouti
ne..
.
..
..
..
..
..
.
..
..
.11
8,291,324
,370,405
I
nde
x 4
31

READc omma nd...


..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
.
..
..
125 S
TA. .
.
..
..
...
..
..
..
.
..
...
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
.
..
18,38
5
rea
ding/wr i
tingda tailes
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
113 s
tac
k .
.
..
..
...
..
..
..
.
..
...
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
.61,
62,13
1
rea
ding/wr i
tingt e
xtiles.
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
120 S
tackPointer.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
.61,1
31,39
6
rea
lvari
a bles.
...
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
158,162
,252 S
tat
usRe gist
er.
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
21,22,37,1
30,39
6
reg
ist
ers,6 502..
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.6
,396 s
tri
ngv a
r i
ables
...
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
.159,1
64,38
6
rel
at
ivea ddressing.
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.46
,328 S
TX...
..
..
...
.
...
.
..
..
...
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
18,38
6
rel
ocat
abl ecode .
..
..
..
..
..
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.127
,371 S
TY...
..
..
...
.
...
.
..
..
...
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
18,38
7
REM comma nd...
..
.
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
17 S
TZ...
..
..
..
..
..
..
..
..
...
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.3
31,38
7
RESET....
..
..
..
...
..
...
.
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
385 s
ubtrac
tionina ss
embly..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
65,26
5
RMB. .
.
..
....
..
..
..
...
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
331 s
uperhi-resgraphic
s..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
.
.19
5
RNDs ubr outine..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
.
..
..
408
ROL...
..
..
...
..
..
...
..
..
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.91
,380 T
ROM ( r
e ad-onlyme mory).
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.
..
..
.
.4 TANs ubrouti
ne .
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
409
ROR...
..
...
...
.
..
....
.
...
.
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.91
,381 TAX..
..
..
..
..
..
..
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
34,388
rot
atecomma nds..
.
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
91 TAY..
..
..
..
..
..
..
..
.
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
34,388
RTI..
.
..
..
...
...
..
..
...
.
...
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
381 t
extsc
reenme mor yma p.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.19,3
02,416
RTS..
..
..
...
..
..
..
...
..
...
.
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.11,13
,382 t
ransf
ercomma nds.
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
34
RWTSe rrorc odes.
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
85 TRB..
.
...
.
..
...
.
..
..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
.331,3
35,389
RWTSs ubrout i
ne s
..
..
..
..
..
..
..
..
.78,8
1,83,359,384
,385 TSB.
..
..
..
..
.
...
..
..
.
..
...
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
.331,3
35,390
TSX.
..
..
..
..
..
..
..
..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
.131,1
32,390
S t
wo’scompl e
me nt.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.74,1
78,367
S
-CAs sembl er..
...
..
..
.
...
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
xvii
i,33
7 TXA..
..
..
..
..
..
..
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
34,391
S
AVDs ubr out ine..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.16
5 TXS.
..
..
..
..
..
..
..
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
..
..
392
S
BC. .
..
..
...
....
...
...
...
..
..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
72,274,38
2 TXTPTRl ocati
on..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.1
47,257
s
creenout put ..
..
...
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.3
0 TYA..
..
..
..
..
..
..
..
.
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
34,393
S
CRNs ubr out i
ne ..
..
.
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
219,40
7
S
EC. .
..
..
...
....
...
...
...
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
.72,38
4 U
S
ED. ..
.
...
...
...
....
..
....
.
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.272,38
4 UCMDl oc
ation.
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
.
..
..
.
.85
S
EI...
..
..
..
....
...
....
..
...
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.38
5 USLOTloc
ation.
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
84,90
s
elf
-modi f yingc ode.
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.13
7 USRcomma nd..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
.
..
..
..
.
..
..
..
255
S
ENDs ubr out i
ne ..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.22
3
S
ETCOLs ubr out i
ne...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.40
6 V
S
ETI NVs ubr out i
ne.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
.40
4 ve
ctor
..
..
...
..
.
...
..
.
....
.
..
..
..
..
.
..
..
..
..
.
.35,82,
94,281,291,
370
S
ETNORM s ubr outine.
..
..
..
..
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.40
4 VLINEs ubroutine.
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.4
06
S
GNs ubr out ine .
..
..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.40
8 VTABs ubroutine..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.119,
302
s
hapet able s.
....
..
...
..
..
.
...
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
.171,22
5 VTOC( VolumeTa bleofContents
).
..
..
..
..
.
..
..
..
..
.
..
..
..
.8
0
s
hiftopera tor s.
..
...
..
..
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.8
9
S
HNUM s ubr out i
ne...
..
..
.
..
..
..
..
..
..
..
..
.
..
..
..
..
169,172,40
8 W
s
ignbit..
...
....
...
...
...
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.7
3 WAITsubrout
ine.
.
..
..
..
.
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
172,183,4
10
s
ignla g..
....
...
...
....
.
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
.75,9
0 wa
rm-reent
ryvec
tor.
.
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.
..
..
.
..3
5
S
IGNs ubr out ine .
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.40
8 wr
aparoundofnumbers.
..
..
.
..
..
..
24,363-36
5,367-3
69
S
INs ubr out ine....
..
.
..
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.40
9 WRITEcomma nd.
..
..
..
.
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.
..
..
..
.1
25
S
MB. ..
..
...
....
...
...
....
.
..
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.33
1
s
neak e
r, we t..
...
...
..
..
..
...
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.4 X
oft-
s swi t
c hha rdwa relocati
on..
..
.
..
..
..
..
..
..
..
41,343,35
3 X-
Regi
st
er.
..
..
.
...
..
.
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
6,18
,396
s
ounddur ation....
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
.5
6 XDRAW comma nd..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
.
..
..
.
..
225
s
oundf rom pa ddleinput..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.5
9 XDRAW subrout
ine.
..
.
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
.1
69,229,
408
s
oundg ene ration...
..
...
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.5
3
s
oundpi t
c h....
..
....
..
.
..
...
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.5
6 Y
s
oundr out i
ne s..
...
..
..
...
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
.53,23
5 Y-
Reg
ist
er.
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
.6
,18
,39
6
s
ourcec ode ....
..
....
.
..
..
...
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.1
5
S
our cer
or ...
...
...
...
..
..
..
...
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
.29,33
7 Z
s
peak e
rs oft-s witch..
...
.
..
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
.5
3 z
erola
g..
.
..
..
.
..
..
.
..
..
.
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
22-2
4,9
0
S
QRs ubr out ine...
..
..
.
...
..
..
..
.
..
..
..
..
..
.
..
..
..
..
..
..
.
..
..
..
..
..
.
..
..
.40
8 z
eropa
geaddre
ssi
ng.
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
..
..
..
..
.
.45
,32
8
Qui
ckRe
fer
enc
e

Me
rli
nAs
sembl
er
Edit
ingComma nds
EditorCont rol Mode <CTRL>B−Be ginni ngofline
A−Ad dmode ,<RETURN>t oe xit <CTRL>CorX−Abor tEditmode
E−Ed i
ta l
lli
nes <CTRL>D−De letec ha r
acter
Emo rEm, n−Edi tlineorl i
ner ange <CTRL>Fc−F indc ha r
acte
r
E“ s
tri
ng”−Edi tli
nesc ont ainings t
ring <CTRL>I−I nse
r t;<RETURN>t oe xi
t
Im−I nsertli
ne sstartinga tm <CTRL>L−To gg lelowe rc
ase/upperc
ase
Dmo rDm, n−De l
etelineorl inerange <CTRL>N−En dofl ine
Rmo rRm, n−Re placel ineorl i
nerange <CTRL>O−I nse
r tspe ci
alcharacte
rs
L−L i
stsource <CTRL>P−F il
llinewi t
h*****
Lmo rLm, n−Li stspec iiclineorr ange <SPACE><CTRL>P−Bo r
derwi th* *
.(perod)−Li
i stf r
om pr eviousr ange <CTRL>Q−De leter estoftheline
P,Pn ,Pm, n−Li s
twi thoutl inenumbe rs <CTRL>R−Re storel inetoorigina
l
F“ s
tri
ng”−Fi ndt heg iv ens t
ring <RETURN>−Ne x tlineore xit
C“ s
tr1”“st
r2”−Cha ng es t
r1t ostr2
FW“ word”−Fi ndt heg iv enwo r
d Merl
inProFul lScr
eenEdi tor
CW“ s
tr1”“st
r2”−Cha ng es t
r1t ostr2 N−Got obe gi
nning/
e ndofsourc
e
EW“ word”−Edi tl
inesc ont ainingwo rd R−De l
eteorreplac
et hecurre
ntli
ne
COPYmTOn−Co pyl ine E−Ex c
ha nge(inda ndreplace)
COPYl,mTOn−Copyl ine s F−Findt ex
t
MOVEmTOn−Mo veline I−Ins
ertabl a
nkl i
ne
MOVEl,mTOn−Mov el ines L−Locatelabel,
ma rker
,orl i
nenumber
123o r$123−He x/De cima lc onvers
ion Q−Re t
ur ntoEditorContr olMode
V−Cl i
pboardcutandpa ste
ASM−Assembl ethesourcecode Y−S e
lectall
texttotheend
LEN−Sourcelengthandby tesremai
ning 8−Createalineofaster
isks
MON−ExittotheMoni tor 9−Createaboxofa ster
isks
NEW−Clearthecurrentsourcecode
PR#1−Sendout puttoprinteri
ns l
ot1 PseudoOpc ode s
VAL“e
xpres
sion”−Comput ee x
press
ion l
abelEQUe x pr
ession
VID3orVID0−Tur n8 0-columnson/of l
abelKBD−De inel abelduringa sse
mbly
Q−Qu i
tEditor,ret
urntoEx ecuti
ve ASC“ s
tring ”−De ineAS CIItext
CHK−Ad dac he cksum by te
Ex
pre
ssi
ons ERRe xpre s
sion−For ceerrorifnonz er
o
2*LABEL1-LABEL2+$231 HEXd ata−De inehe xda ta
1234+%10111 LSTONo rOFF−Ena ble
/disableli
sti
ng
“K”-“A”+1 LUP...--^−L oopa ndr epeato pcodes
“0”!LABEL(“0”EORLABEL) OBJe xpre s
sion−As sembl yaddr e
ss
LABEL&$7F(LABELAND$7F) ORGe xpre s
sion−Runa ddr e
ss
LABEL.$FFFF(LABELOR$FFFF) PUTi l
e na me−I nsertT.ilename
*-2(curr
entaddressminus2
) SAVi l
e na me−S avec urrentcode
DUM/DEND−Du mmys ectionofc ode
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 playdifere 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 2−7
Imme diate LDA #$FF 2 2
ZeroPa ge LDA $06 2 3−5
ZeroPa geIndexed, X LDA $06,X 2 4−6
Absolute LDA $C000 3 3−6
AbsoluteIndexed, X LDA $2000,X 3 4−7
AbsoluteIndexed, Y LDA $2000,Y 3 4−5
Indi
rectIndexed( post-i
nde
xed) LDA ($06),Y 2 5−6
IndexedIndire
ct( pre-
indexe
d) LDA ($06,X) 2 6
Rela
tive BCC $300 2 2−4
Indi
rectJump JMP ($0036) 3 5−6
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 e rWa gne rboug htoneoft hee ar l
yAppl e] [( note venPl us !
)c omput ers
in1 978a ndov e rthene xtf e wy earswr ot eamont hlypr ogrammi ngt utorialfor
Softal
k ma gazine .As semb lyLi nes:TheBo ok ,publ ishe di n1 98 2,wa st heir s
t
books pe ciicall
ya boutc re atinga s
sembl y -l
ang ua gepr og ramsont heAppl e] [
.
Rog e ralsowr oteAp pleI IGSMa c hineLa ng uagef orBe g
innersa ndnume rous
prog r
a ms i nc l
udi ngRo ger '
sEa se
l,ThePr ogra mme r’sUt i
li
tyPa ck,TheWr i
te
Cho ic
e, aswe lla sma jorc ont ributionst ot heRo utineMa chineAp ple
softLi br
a ry
.
Hewa st hede signe ra ndc r eatorofHy perStudi ofort heAp pleIIGS, amul t
ime dia
pack aget hatsoonbe camet hemos t-us eds oftwa reinK- 1
2c lass
rooms .
Rog e risapa tent-hol di ngi nvent or,ha sowne dar are-booka ucti
onhous e
,
andi sapr i
v a
tepi lotwi thl icens esint heUni tedS tates, Ca nada,andMe xico.
Te chno logy& Le a rni ngma ga
z inena me dRog e roneoft het op5“ Mos t
Impor tantEduc ati
ona lTe chnol ogyGur usoft hePa s tTwoDe ca des”a l
ongwi th
SeymourPa pe r
t, BillGa tes, Stev eWoz niak ,
a ndS t
eveJ obs.
Rog e rcoine dt hephr ase“ Copy rig htFr iendl y,
”apr edecess
ort ot heCr eat
ive
Commonsl icense ,andhewor k edwi tht heCr e at
iveCommonsor ganizationt o
developa utoma tica ttribut ions yste
ms .
Toda y,Rog ers e rve sont heBoa rdofDi r e
ctorsofCa l
if
or niaComput er-
UsingEduc ators( CUE)I nc .
,a ndi sa ne nergetica dv oc ateforena blings t
ude nts
toe fectiv e
lyus et ec hnol og y ,a swe lla sf ori ssue sre l
a ti
ngt odi g i
talciti
ze nship
ands tude ntpr i
v acy.
Formor einf orma tion, v i
sit“ http://rog erwa gne r
.c om. ”

You might also like