Professional Documents
Culture Documents
Assembly Lines Complete Wagner
Assembly Lines Complete Wagner
Assembly Lines Complete Wagner
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
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
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
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
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
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
]
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
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
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
]
2
0 As
sembl
yLi
nes
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
Andhe
rei
sthewa
yAppl
e’
sdi
sas
sembl
erwoul
dshowi
t:
*300L
0300- 20 58 FC JSR $FC58
0303- A2 FF LDX #$FF
0305- 8E 00 07 STX $0700
0308- CA DEX
0309- D0 FA BNE $0305
030B- 60 RTS
]
2
6 As
sembl
yLi
nes
getprint
ed–why ?)Tov eri
fyt hateachpassisbe ingexec
uted,repla
c etheSTX
$700inthes ourceli
sti
ngwi thaJSR$FBDD. Ifyoudon’ twanttohear2 55be e
ps,
tr
yc hangingtheiniti
alval
ueoft heX- Regi
st
e rinline10.Asbe f
ore,yous houl
d
beabletoc a
llthi
sprog r
amf rom theMonitorwi t
ha300G, orfrom BAS ICwi t
ha
CALL768.
Youma ya ls
owi shtotrythee qui
val
entve r
sionoftheprogram usingtheY-
Regi
s t
erorame mor ylocat
iona sthecounter.Iwoul dsugge
sttryi
ngt owr it
ea
program usi
ngINC, INX,orINYt odriv
ethecount erasapract
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
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
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
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
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
]
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
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
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
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
Whe
nlook
eda
tinme
mor
y,i
tshoul
dappe
arl
iket
his
:
*300L
0300- A2 00 LDX #$00
0302- BD 13 03 LDA $0313,X
0305- 20 ED FD JSR $FDED
0308- E8 INX
0309- E0 05 CPX #$05
030B- 90 F5 BCC $0302
030D- A9 8D LDA #$8D
030F- 20 ED FD JSR $FDED
0312- 60 RTS
0313- C1 D0 CMP ($D0,X)
0315- D0 CC BNE $02E3
0317- C5 00 CMP $00
]
5
0 As
sembl
yLi
nes
1 ********************************
2 * AL07-SCREEN CLEAR PROGRAM 1A *
3 ********************************
4*
5* OBJ $300
6 ORG $300
7*
8 PTR EQU $06
9*
10 ENTRY LDA #$04
11 STA PTR+1
12 LDY #$00
13 STY PTR
14 * SETS PTR (6,7) TO $400
15 START LDA #$A0
16 LOOP STA (PTR),Y
17 INY
18 BNE LOOP
19 NXT INC PTR+1
20 LDA PTR+1
21 CMP #$08
22 BCC START
23 EXIT RTS
Li
st
edf
rom t
heMoni
tor
,its
houl
dappe
arl
iket
his
:
*300L
0300- A9 04 LDA #$04
0302- 85 07 STA $07
0304- A0 00 LDY #$00
0306- 84 06 STY $06
0308- A9 A0 LDA #$A0
030A- 91 06 STA ($06),Y
030C- C8 INY
030D- D0 FB BNE $030A
030F- E6 07 INC $07
0311- A5 07 LDA $07
0313- C9 08 CMP #$08
0315- 90 F1 BCC $0308
0317- 60 RTS
Wes ta
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
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
]
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
]
6
2 As
sembl
yLi
nes
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:
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
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
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
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
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
]
8
4 As
sembl
yLi
nes
47 *
0300: A9 00 48 CLEAR LDA #$00
0302: 8D EB B7 49 STA VOL
50 *
0305: A5 09 51 LDA USLOT
0307: 8D E9 B7 52 STA SLOT
53 *
030A: A5 08 54 LDA UDRIV
030C: 8D EA B7 55 STA DRIV
56 *
030F: A5 06 57 LDA CTRK
0311: 8D EC B7 58 STA TRACK
59 *
0314: A5 07 60 LDA CSCT
0316: 8D ED B7 61 STA SECTOR
62 *
0319: A5 E3 63 LDA UCMD
031B: 8D F4 B7 64 STA CMD
65 *
031E: A5 0A 66 LDA BP
0320: 8D F0 B7 67 STA BUFR
0323: A5 0B 68 LDA BP+1
0325: 8D F1 B7 69 STA BUFR+1
70 *
0328: A9 B7 71 LDA #$B7
032A: A0 E8 72 LDY #$E8
032C: 20 D9 03 73 JSR RWTS
032F: 90 05 74 BCC EXIT
75 *
0331: AD F5 B7 76 ERRHAND LDA ERR
0334: 85 0C 77 STA UERR
78 *
0336: 60 79 EXIT RTS
80 *
0337: CD 81 CHK
4[
CT]$FFi
snotav
ali
dDOSv
olumenumbe
r.
1
1.DOSa
ndDi
skAc
ces
s 8
5
Code Condition
$10 Diskwrite-prot
ected,andc a
nnotbewr i
tte
nto.
$20 Volumemi smatche rr
or.Volumenumbe rfoundwa 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
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
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
ROL−Rot
ateOneBi
tLe
t ROR−Rot
ateOneBi
tRi
ght
]
9
2 As
sembl
yLi
nes
Log
ica
lOpe
rat
ors
Log i
c alope ratorsa re,totheuni nitiated,someoft hemor ee sotericoft he
assembl y-l
a nguag ec omma nds.Aswi t
he verythingwe ’vedonebe for e,thoug h,
withal it
tlee xplanationt hey’
llbe c
omequi teuse f
ul.
Le t
’ss tartwi thoneoft hemos tc ommonl yus edc omma nds , AND.Yo u’re
alreadyfa mi l
iarwi tht heba si
ci deaoft hisonef rom y ourda i
lys pe ech.Ift his
andt hata reac ertainwa y,t
henI ’l
l dos ome t
hing .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
]
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
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)
]
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
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
]
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.
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
]
1
02 As
sembl
yLi
nes
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'
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
]
1
08 As
sembl
yLi
nes
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
]
1
10 As
sembl
yLi
nes
a
ndt
hes
econdi
nas
sembl
yla
ngua
ge:
1 ********************************
2 * AL13-INPUT ROUTINE FP BASIC *
3 ********************************
4 *
5 * IN$="" MUST BE FIRST VARIABLE
6 * DEFINED IN PROGRAM!
7 *
8 * OBJ $300
9 ORG $300
10 *
11 GETLN EQU $FD6F
12 VARTAB EQU $69
13 BUFF EQU $200
14 *
15 *
0300: A2 00 16 ENTRY LDX #$00
0302: 20 6F FD 17 JSR GETLN
0305: A0 02 18 LDY #$02
0307: 8A 19 TXA
0308: 91 69 20 STA (VARTAB),Y
21 * STORE ’X-REG = LEN OF IN$’
22 * IN LEN BYTE OF IN$
23 *
030A: C8 24 INY ;Y=3
030B: A9 00 25 LDA #$00
030D: 91 69 26 STA (VARTAB),Y
1
3.I
/ORout
ine
s 1
11
Byloadi
ngtheY- Regi
ste
rwi t
h#$02, 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
]
1
16 As
sembl
yLi
nes
]
1
18 As
sembl
yLi
nes
229 *
61E1: 00 230 EOF BRK
231 *
232 *
61E2: 89 233 CHK
Tounde rst
andhowi twor ks,considerthesec ondit
ions:
Da t
awillbestoredint heareaf r
om $1000- $14FF.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
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
]
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
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
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
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
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
]
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).
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
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
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
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
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
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
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
FP S
imple Array S
tri
ng
Fr
ee DOS
Pr
ogra
m Var
iabl
es Var
iabl
es Data
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
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
:
]
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
]
1
58 As
sembl
yLi
nes
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
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
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
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
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
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
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
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
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
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
]
1
78 As
sembl
yLi
nes
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
81 *
6065: A5 0A 82 CHK2 LDA TX+1
6067: C9 01 83 CMP #$01
6069: 90 09 84 BCC CHK3
606B: A5 09 85 LDA TX
606D: C9 16 86 CMP #$16
606F: 90 03 87 BCC CHK3
6071: 20 AE 60 88 JSR RVRSX ; TX >= $116 (278)
89 *
6074: A5 0B 90 CHK3 LDA TY
6076: C9 02 91 CMP #$02
6078: B0 03 92 BCS CHK4
607A: 20 D6 60 93 JSR RVRSY ; TY < 2
94 *
607D: A5 0B 95 CHK4 LDA TY
607F: C9 9E 96 CMP #$9E
6081: 90 03 97 BCC ERASE
6083: 20 D6 60 98 JSR RVRSY ; TY >= $9E (158)
99 *
6086: A2 00 100 ERASE LDX #$00 ; BLACK = 0
6088: 20 F0 F6 101 JSR HCOLOR
608B: A6 E0 102 LDX X
608D: A4 E1 103 LDY X+1 ; GET X,X+1
608F: A5 E2 104 LDA Y ; GET Y
6091: 20 57 F4 105 JSR HPLOT ; ERASE POINT
106 *
6094: A2 03 107 PLOT LDX #$03 ; WHITE1 = 3
6096: 20 F0 F6 108 JSR HCOLOR
6099: A6 09 109 LDX TX
609B: A4 0A 110 LDY TX+1 ; GET TX,TX+1
609D: A5 0B 111 LDA TY ; GET TY
609F: 20 57 F4 112 JSR HPLOT ; PLOT POINT
113 *
60A2: A2 00 114 DELAY LDX #$00 ; PDL0
60A4: 20 1E FB 115 JSR PREAD
60A7: 98 116 TYA
60A8: 20 A8 FC 117 JSR WAIT
118 *
119 *
60AB: 4C 44 60 120 GOBACK JMP CALC
121 *
122 *
60AE: A5 07 123 RVRSX LDA XV+1
60B0: 30 12 124 BMI NEGPOSX
125 *
60B2: A5 06 126 POSNEGX LDA XV
60B4: 49 FF 127 EOR #$FF
60B6: 18 128 CLC
60B7: 69 01 129 ADC #$01
60B9: 85 06 130 STA XV
60BB: A5 07 131 LDA XV+1
60BD: 49 FF 132 EOR #$FF
60BF: 69 00 133 ADC #$00
60C1: 85 07 134 STA XV+1
60C3: 60 135 RTS ; XV -> -XV
136 *
]
1
82 As
sembl
yLi
nes
shortdelaybyget
tingavaluefr
om paddle0tobeusedbytheWAIT( $FCA8)rou-
ti
ne.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:
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
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
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
Fi
gur
e21-
1:Appl
esotCol
orMa
sks
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:
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
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
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
]
2
08 As
sembl
yLi
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
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
]
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
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
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
]
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
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
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
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
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
]
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
]
2
34 As
sembl
yLi
nes
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
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
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
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
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
]
2
44 As
sembl
yLi
nes
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
]
2
48 As
sembl
yLi
nes
]
2
50 As
sembl
yLi
nes
1
8.Goba
ckt
ost
ep1
5te
nti
mesf
ora
nex
cit
ing(
?)e
xpl
osi
on.
19.Re stor
eSCALEa ndthee xplos i
onc 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
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
)
]
2
54 As
sembl
yLi
nes
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 *
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
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
]
2
64 As
sembl
yLi
nes
t
hant
ous
e:
FOR I = 1 TO 100: A(I) = A(I) * 5: NEXT I
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
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
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
]
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”
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
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
<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
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
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)
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
I
nthi
sexampl
e,LBandHBaret
hel
ow-andhigh-
orderby
tesoft
hede
sti
na-
t
iona
ddre
ss.
Inass
emblyla
ngua
ge,i
twoul
dlookl
iket
his:
LDA #LB
STA $38
LDA #HB
STA $39
JSR $3EA
2[
CT]J
ustl
iket
heout
putv
ect
or(
des
cri
bedi
ncha
pte
r29
),whe
nus
ingPr
oDOSy
ouc
an
dir
ect
lycha
ngethei
nputve
ctorat$BE32,$BE33topoi
ntt
oyouri
nputr
out
ine
.Se
e
chapt
ers
ixofI
nsi
det
heApp
le//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
Don’
tbes
urpr
ise
dift
hispr
ogram does
n’twor
k.4
Tr
ychang
ingl
ine20t
olookli
kethis
:
20 GET A$: PRINT A$;: IF A$ <> CHR$(13) THEN I$ = I$ + A$: GOTO 20
3[
CT]Unde
rPr
oDOS
,yous
houl
dcha
ngel
ine1
0toPOKE48690,0:POKE48691,3
4[
CT]Ona
nAppl
e//
e(unde
rDOS
)thi
spr
ogr
ama
ctua
ll
ywi
ll
wor
k.
5[
CT]I
ntheAppl
eIIa
ndAppl
eIIPl
ust
hec
odea
t$FD7Ec
hec
kswhe
the
rthec
har
act
er
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
]
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
]
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
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
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
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
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
]
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
]
3
16 As
sembl
yLi
nes
]
3
18 As
sembl
yLi
nes
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
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
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
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
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
]
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:
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
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
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
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
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
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
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+
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
I
nputSubr
out
ine
s
]
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
Hi
-Re
sGr
aphi
csSubr
out
ine
s
]
4
08 As
sembl
yLi
nes
Fl
oat
ingPoi
ntAc
cumul
ator
]
4
10 As
sembl
yLi
nes
Ot
herSubr
out
ine
s
YouGe
tWha
tYouASCI
IFor
..
.
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).
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
).
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
]
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
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
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
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. ”