Page 1
c o m m o d o r e 6 4
m a c h i n e c o d e m o s t e r
A l i b r a r y o f m a c h i n e c o d e ro u ti n e s
d a v i d l a w r e n c e & m a r k e n g l a n d
Page 2
Page 3
c o m m o d o r e 6 4
m a c h i n e c o d e m a s t e r
A l i b r a r y o f m a c h i n e c o d e r o u t i n e s
Page 4
Fir st p u b l i s h e d 1 9 8 3 by :
S u n s h in e B o o k s (a n im p rin t o f S c o t P re s s L t d .)
H o b h o u s e C o u r t,
1 9 W h it c o m b S tre et,
L o n d o n W C 2 7 H F
C o p y ri g h t © D a v i d L aw re n c e an d Mar k E n g l a n d
R e p r i n t e d D e c e m b e r 1 9 8 3
I S B N 0 94 6 4 0 8 05 X
A l l righ ts rese rv ed . N o p a r t o f th is p u b l i c a t i o n m a y b e r e p r o d u c e d , s t o r e d
i n a r e tr i ev a l s y s t e m , o r t r a n s m i t t e d i n a n y f o r m o r b y a n y means, elec
tronic, me ch anic al, p h o t o c o p y i n g , r e co r d in g a n d / o r o th erw ise , w i t h o u t
th e p r i o r wri tte n p e r m i s s i o n o f th e Pu bl ish ers .
C ov e r d es i g n b y G ra p hi c D e s i g n L td .
I ll u s tr at io n b y Stuart H u g h e s .
T y p e s et an d p rin te d in E n g l a n d b y C o m m e r c i a l C o l o u r P r e s s , L o n d o n E 7 .
2
Page 5
C O N T E N T S
P a g e
F o r e w o r d 7
P a r t i
1 M a s t e r c o d e M o n i t o r 1 1
2 M a s t e r c o d e D i s a s s e m b l e r 27
3 M a s t e r c o d e F i l e E d i t o r 4 1
4 M a s t e r c o d e A s s e m b l e r 5 7
P a r t 2
5 T h e B A S I C E x t e n d e r 97
6 T h e B A S I C / M a c h i n e C o d e P a t c h 1 0 3
7 B A S I C E x t e n d e r P r o g r a m I I 1 1 9
8 S i m p l e B A S I C A c t i o n K e y w o r d s 1 2 3
9 T h e P r o b l e m o f P a r a m e t e r s 1 3 7
1 0 B A S I C F u n c t i o n s 1 6 7
1 1 B r e a k in g N e w F r o n t i e r s 1 7 3
A p p e n d i c e s
A C h e c k s u m G e n e r a t o r 1 7 7
B M a s t e r c o d e U s e r G u i d e 1 8 1
C T a b l e o f V a r ia b le s 1 8 5
D T a b l e o f S u b r o u t i n e F u n c t i o n s 1 8 7
E R O M R o u t i n e s C a l l e d 1 8 9
F T a b l e o f C o n t r o l C h a r a c t e r s 1 9 1
3
Page 6
Page 7
C o n t e n t s in de ta i l
C H A P T E R 1
M as ter cod e M o n it o r
E xam in ing m e m o ry c o n t e n t s , alt ering t h e m , sav ing an d l o a d i n g m a c hi n e
c o d e prog rams.
C H A P T E R 2
M as ter cod e D isa sse m ble r
Tr anslating th e 6 4 R O M or o u r o w n m a c h i n e c o d e pr ograms i n t o a ss em bl y
l ang u age .
C H A P T E R 3
M as te rc od e File e d it o r
E nt eri ng as sembly la ng u ag e pr ograms an d sa vi ng an d l o a d i n g th e m .
C H A P T E R 4
M as te rc od e A ss em b le r
Crea ting m ac h in e c o d e pr ograms fr om as sem b ly l a n g u a g e, i n c lu d in g
au to m a ti c e rr o r c h ec k in g .
C H A P T E R 5
T h e B A S IC E x te n d er
H o w t o tran sfer t h e c o n t e n t s o f t h e 6 4 ’s B A S I C Interp rete r t o user
me mor y.
C H A P T E R 6
T h e B A S I C / M a c h i n e C o d e P a t c h
A l l th e m ac h in e c o d e te c h n i q u e s n e e d e d f o r e x t e n d i n g t h e B A S I C
l ang u age .
5
Page 8
M a ch in e C o d e M a s te r
C H A P T E R 7
T h e B A S IC E xt en d er I I
T h e final c h a n g e s t o lin k e x t e n d e d B A S I C t o th e exi sti ng in ter pr ete r.
C H A P T E R 8
Simp le B A S I C A c t i o n Ke ywords
U n d e a d , S u b e x an d Rk ill.
C H A P T E R 9
T h e P r o b l e m o f P arameters
P i c k i n g u p pa ram eters fr o m a B A S I C program. Ke yw ord s: D O K E ,
P L O T , D E L E T E , B S A V E , B L O A D , B V E R I F Y , M O V E , FI L L and
R E S T O R E .
C H A P T E R 1 0
BASIC Fu n cti on s
E x te n d in g t h e 6 4 ’ s m at he m a tic a l f u n c t i o n s . Key wo rds: D E E K , V A R P T R ,
Y P O S .
C H A P T E R 1 1
Brea king N e w Frontiers
T h e F A S T co m m a n d th a t w a s n ’ t.
A P P E N D I C E S :
A ) C h e c k s u m Gen erator: te s t y o u r M a s t er c o d e P ro gr a m a s y o u en te r it.
B) A User G u i d e t o t h e M a s t er c o d e pr ogram.
C) A c o m p l e t e ta b le o f t h e va riables f o u n d in t h e M a s t er c o d e pr ogram
an d th eir u s es .
D ) A qui ck g u id e t o t h e p u r p o s e o f ea c h s u b r o u t in e in th e M a s te rc o de
progr am.
E ) T h e R O M r ou tin es ca ll ed u p o n b y e x t e n d e d B A S I C an d wh at th e y
d o .
F) R e pr es en ta tio n o f c o n t r o l ch aracters in t h e M a s t er c o d e pr ogram.
6
Page 9
F O R E W O R D
T h i s is n o t y e t a n o t h e r m a c h i n e c o d e b o o k f o r a p o p u l a r m i c r o w h i c h
s p e n d s h a l f its p a g e s e x p l a i n i n g a l l t h e 6 5 0 2 / 6 5 1 0 m a c h i n e c o d e in s t r u c
t i o n s a n d th eir v a r i o u s a d d r e s s i n g m o d e s , w i t h a cr ud e l o a d e r p r o g r a m a n d
a nu m be r o f m a c h i n e c o d e r o u t i n e s o f d u b i o u s u s e f u l n e s s ta c k e d o n t o th e
e n d . T h e i n t e n t i o n o f th i s b o o k is t o p r o v i d e its reade rs w i t h a s o l i d B A S I C
p ro gr a m f o r t h e e n tr y o f m a c h i n e c o d e a n d a s s e m b l y l a n g u a g e r o u ti n e s
a n d , a l o n g w i th i t , a s e l e c t i o n o f m a c h i n e c o d e p r o g r a m s t h a t a re w o r t h
h a v in g .
T h e B A S I C p r o g r a m i s c a l l e d M a s t e r c o d e a n d i t is j u s t a b o u t a c o m p l e t e
m a c h i n e c o d e p r o g r a m m i n g t o o l , c o n t a i n i n g a M o n i t o r t o a l l o w y o u t o
e x a m in e an d c h a n g e t h e c o n t e n t s o f m e m o r y , a D i s a s s e m b l e r w h i c h tr a n s
l a te s m a c h i n e c o d e p r o g r a m s i n t o a s s e m b l y l a n g u a g e f o r m a t , a n d a F i l e
E d i t o r a n d A s s e m b l e r w h i c h t o g e t h e r a l l o w a s s e m b l y la n g u a g e p ro g ra m s
t o b e d e v e l o p e d a n d t r a n s f o r m e d i n t o m a c h i n e c o d e .
F o r t h e m a c h i n e c o d e r o u t i n e s i n t h e s e c o n d h a l f o f t h e b o o k w e h a v e
a d o p t e d a n e w a p p r o a c h . W h a t y o u w i l l f i n d th e r e is a c o l l e c t i o n o f
ro u ti n e s w h i c h y o u c a n u s e t o e x t e n d t h e B A S I C la n g u a g e o n y o u r 6 4 w i t h
1 4 n e w c o m m a n d s . A p a r t f r o m i n c r e a s i n g t h e p o w e r o f B A S I C i t w i l l a l s o
h el p y o u t o le a r n t h e t e c h n i q u e s o f e f f e c t i v e m a c h i n e c o d e p r o g r a m m i n g
a n d th e w a y s i n w h i c h t h e m a c h i n e c o d e p ro g ra m m er c a n m a k e u s e o f th e
r o u ti n e s a lr ea d y b u i l t i n t o t h e 6 4 ’ s B A S I C Inter pre ter .
T h e b o o k is n o t i n t e n d e d t o b e a prime r i n m a c h i n e c o d e . T h a t is n o t t o
s a y t h a t it c a n n o t b e u s e d b y b e g i n n e r s . It i s s i m p l y t h a t w e h a v e a s s u m e d
t h a t y o u w i l l p o s s e s s , o r c a n g e t h o l d o f , s o m e o t h e r b o o k w h i c h e x p l a i n s
ea c h 6 5 0 2 / 6 5 1 0 i n s t r u c t i o n i n d e t a i l . W e d e c i d e d t h a t w e c o u l d o f f e r b et t er
v a l u e b y c o n c e n t r a t i n g o n t h e p r o g r a m s th e m s e l v e s a n d o n e x p l a i n i n g t h e
t e c h n i q u e s t h a t w e n t i n t o t h e m . I f y o u h a v e t o l o o k u p t h e o d d in s t r u c t i o n
i n y o u r 6 5 0 2 t e x t - b o o k i t w i l l b e a s m a l l p r i c e t o p a y .
A l l t h e p ro g ra m s i n t h e b o o k h a v e b e e n t e s t e d — in fa c t t h e M a s t e r c o d e
p ro gr am i t s e l f h a s b e e n t e s t e d t o e x h a u s t i o n ( o u r s ) . T h e m a c h i n e c o d e
r o u ti n e s i n th i s b o o k h a v e a l l b e e n d e v e l o p e d o n t h e M a s t e r c o d e p r o g r a m
b e c a u s e o n l y i n t h a t w a y c o u l d w e b e sur e t h a t y o u w o u l d b e a b le t o d o t h e
s a m e . I f y o u run i n t o b u g s i n a n y t h in g y o u f i n d i n th i s b o o k t h e n t h e b la m e
i s o n u s b u t w e v e n tu r e t o s u g g e s t t h a t t h e y w i l l n o t b e m a j o r a f t e r t h e
t e s t i n g t h a t h as b e e n p u t i n .
T h e b o o k is a c o o p e r a t i v e v e n t u r e b e t w e e n t w o p e o p l e w i t h v e r y d i f
fe r e n t b a c k g r o u n d s . D a v i d L a w r e n c e w r ite s m a i n l y B A S I C p ro g ra m s an d
7
Page 10
M a chi ne C o d e M a s te r
is th e a u th o r o f seve ral b o o k s o n m i c r o c o m p u t i n g . H i s in te re st in c o m p u te r
hardw are is m i n i m a l . Mark E n g l a n d s tu d ie s e l e c t r o n i c e n g i n e e r i n g , ju g g l e s
wi th s i l ic o n c h i p s a s i f it we re s e c o n d n a tu re an d l ea rn ed t o wr ite m a c h i n e
c o d e w h en th e B A S I C R O M o n his m i c r o b ur ne d o u t . H e d is c o v e r e d he
d i d n ’ t ne ed it a n y w a y .
T h e i de a c a m e f r o m D a v i d L a w r e n c e , w h o w a s ti re d o f b u y i n g m a c h i n e
c o d e b o o k s th a t ne ver s e e m e d t o p r o v i d e a n y t h in g o f i nt ere st w h e n he
a c t u a l l y w o r k e d t h r o u g h t h e m . Mar k E n g l a n d did t h e v a s t m a j o r i t y o f th e
p r o g r a m m i n g , t h o u g h n o t w i t h o u t h e c k l i n g . T h e f i n a l f o r m o f th e b o o k
arises o u t o f Mark E n g l a n d ’ s n ee d t o e x p l a i n t o his c o - a u t h o r j u s t w h a t th e
p rog ram s wer e a b o u t an d h a v e it tr a n s l a te d f o r l es ser m o r t a l s . In th a t
p ro ce s s o f e x p l a n a t i o n a n d d i s c u s s i o n a s ty l e o f c o m m e n t a r y an d e x p l a
n a t i o n has d e v e l o p e d w h i c h , w e b e l i e v e , d o e s j u s t i c e t o th e p ro g ra m s and
t o th e re ad e r’ s n ee d t o u n d e r s ta n d t h e m .
T h e o th er partn er in th e w r it in g o f th i s b o o k h as b e e n th e C o m m o d o r e
64 . T h e b o o k w o u l d n o t h a v e b e e n a p o s s i b i l i t y o n m a n y o th e r h o m e
m i c r o s . I t i s o n l y b e c a u s e o f C o m m o d o r e ’ s p h i l o s o p h y o f o p e n i n g u p thei r
m a c h i n es t o th e p ro g r a m m e r , p r o v i d i n g a c c e s s t o a h o s t o f fa c i l i t i e s an d
r o u ti n e s w i th i n th e interp re ter th a t o th e r s s e e m t o d o their b e s t t o l o c k a w a y ,
th a t we h a v e b ee n a b le t o t a k e 6 4 B A S I C a pa rt an d p u t it t o g e t h e r a g a i n .
W e h a ve ha d s o m e a rg u m e n t s w i th th e 6 4 , b u t it re m ai ns an i n v a l u a b le
m a c h i n e f o r t h o s e w h o w i s h t o g o b e y o n d B A S I C .
F i n a ll y , ou r t h a n k s are d u e t o th e s t a f f o f th e M i c r o c h i p s s h o p s in
W in c h e s te r a n d S o u t h a m p t o n , w h o p r o v i d e d i n v a l u a b le h e l p w h e n it c a m e
t o s ee k in g o u t e q u ip m e n t a n d s u p p l i e s . T h a n k s a re a l s o d u e t o J a n e L a w
rence w h o re gu la rly , o n he r w a y t o b e d as w e p o u n d e d th e k e y b o a r d i n t o
th e early h o u r s , t o l d us w h a t a w o n d e r f u l j o b w e were d o i n g . W e h o p e she
w as righ t.
8
Page 11
P a r t i
Page 12
Page 13
C H A P T E R 1
M a s t e r c o d e M o n i t o r
Ever y c o m p u te r p r o g r a m , re ga rd les s o f th e l a n g u a g e in w h i c h it is w r it te n ,
b eg in s its l i f e as a ser ies o f i n s t r u c t i o n s s to r e d in a c o d e d f o r m w i th i n th e
c o m p u t e r ’ s m e m o r y . In th e c a s e o f m o s t l a n g u a g e s , th e i n s t r u c ti o n s w h i c h
m ak e up th e p ro g ra m a re q u i te m e a n i n g l e s s t o th e ce nt ral p r o c e s s in g u ni t
or C P U , th e c o m p u t e r - w i t h i n - a - c o m p u t e r w h i c h w il l e v e n t u a l l y b e ca l l ed
o n t o e x e c u te th e ta s k s d ic ta te d b y th e p r o g r a m . T o o v e r c o m e th is p r o b
l e m , s ta n d in g in b e t w e e n th e p r o g r a m en te re d b y th e u ser an d th e C P U wi ll
be ye t an o th e r p r o g r a m , m o s t o f t e n b u i l t in t o th e m a c h i n e a t th e t i m e o f it s
m a n u f a c t u r e , w h i c h t a k e s t h e u s e r ’ s p ro g ra m an d tr a n s l a te s it i n t o a f o r m
w h ic h th e C P U is a b le t o u n d e r s ta n d
T h e p e r m a n e n t , ‘ b u i l t i n ’ p r o g r a m , h o w e v e r , p e r f o r m s a n o th e r f u n
c t i o n , f o r w i t h o u t its h el p it w o u l d b e i m p o s s i b l e f o r th e use r t o ente r
in s t r u c ti o n s in th e fir s t p la c e . F r o m th e m o m e n t t h a t th e c o m p u t e r is
s w it c h e d o n , th e bu i l t in p ro g ra m be g i n s its ta s k o f s c a n n in g th e k ey b o a r d
t o d et e ct an i n p u t f r o m th e o u t s i d e w o r ld . I t t h e n ta k e s t h o s e in p u ts an d
s tor es th e m in th e m e m o r y in s u ch a w a y th a t t h e y ca n la ter b e ‘ i n t e r p r e te d ’
f o r th e C P U . T h e u ser w h o w rit es p ro g ra m s in B A S I C w il l s e l d o m be
awar e o f th is p r o c e s s . P r o g r a m l i n e s w il l b e en te r e d , th e return k ey p res se d
and th e li n e w il l b e c o m e part o f th e p ro g ra m — p r o v i d e d th a t th e co r re c t
gram ma r o f B A S I C h a s b e e n o b s e r v e d . N o r eal e f f o r t o r t h o u g h t is
required t o inse rt a n e w i n s t r u c t i o n i n t o th e p r o g r a m , ei th er at th e en d or
e m b e d d e d in th e m i d d l e , f o r th e c o m p u t e r ’ s m e m o r y is a u t o m a t i c a l l y re
arranged t o m a k e s p a c e f o r th e n ew i n p u t.
W h e n w e tu rn t o p r o g r a m m i n g in m a c h i n e c o d e , th e s i t u a t i o n is n o t
q u it e as s i m p le . Th e re a re n o fa c i l i t i e s b u i l t i n t o th e c o m p u t e r t o a l l o w a
new i n s t r u c ti o n t o be s i m p ly e n te re d f r o m th e k e y b o a r d i n th e c o n f i d e n c e
th a t it w il l a u t o m a t i c a l l y b e en te re d i n t o th e c o m p u t e r ’ s m e m o r y a n d th e
p res ent c o n t e n t s rearran ged t o m a k e r o o m f o r i t . T h e fi rst ta s k o f a
m a c h i n e c o d e p r og ra m m er i s , t h e r e f o r e , t o d e v i s e a m e t h o d o f en te ri n g
i n s t r u c ti o n s , e x a m in i n g m e m o r y a n d re arr an gi ng it t o s u it th e d e v e l o p i n g
n ee d s o f th e p ro g ra m t h a t is b e i n g e n te r e d . T h i s is tr ue w h et h er th e
m a c h i n e c o d e i n s t r u c t i o n s a re b e i n g e n te re d d ir ec t l y i n t h e f o r m o f n u m
ber s (w h ic h is th e e v e n t u a l f o r m in w h i c h t h e y m u s t b e p re s e n t ed t o th e
C P U ) or b y m e a n s o f a s p e c i a l l a n g u a g e c a l l e d ‘ a s s e m b l y l a n g u a g e ’ w h i c h
m ak es m a c h i n e c o d e p ro g ra m s ea si er t o en te r a n d u n d e r s ta n d . T h e s i m
1 1
Page 14
M a chi ne C o d e M a s te r
p les t t o o l w h ic h a l l o w s t h e n e c e s s a r y m a n a g e m e n t o f th e m e m o r y t o ta k e
p la c e is ca l l e d a ‘ M o n i t o r ’ an d in th is ch a p te r we s h a ll b u i l d up a fl e x i b l e
M o n i t o r p ro g ra m w h ic h w il l a l l o w y o u t o e x a m in e in d i v i d u a l b y te s o f
m e m o r y or e x t e n s iv e c h u n k s an d t o m o d i f y thei r c o n t e n t s at w i l l .
S E C T I O N 1 : I n i t i a l i s a t i o n a n d M e n u
M O D U L E 1 . 1
1 0 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3 .0 02 0 R E M G E N E R A L I N I T I A L I S A T I O N
3 . 0 0 3 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3 . 0 0 3 3 . B A S E = 16
1 0 0 3 2 I F L E N ( F T R T ) + L E N (ET ) < > 2 5 5 T H E N C L . R
: G 0 3 U B 1 9 0 0 0
1 0 0 3 5 D E V = 1
3 .0 04 0 D E F F N H E X ( X ) = ( X A N D 1 5 ) + 4 8 - < ( X A
N D 1 5 ) > 9 ) * 7
3 .0 050 D E F F N D E C ( X ) = X - 4 8 + < X > 5 7 ) * 7
1 0 0 6 0 F A L S E = 0 ; T R U E = -1
1 0 0 7 0 P O K E 5 3 2 8 1 , 1 : P O K E 5 3 2 8 0 , 1 5
T h e p u r p o s e o f th is m o d u l e is t o se t u p a n um be r o f v a r i a b l e s w h i c h wi ll
b e u se d later in th e p r o g r a m . T h e f u n c t i o n o f t h e v a ri a b l es is e x p l a i n e d
b r i e fl y in th e ta b l e g i v e n i n A p p e n d i x b u t f u l l u n d e r s ta n d i n g w i l l o n l y
c o m e as s u b s e q u e n t s e c t i o n s o f th e p r o g r a m are e n te re d a n d th e va ri a b le s
a c t u a l l y u s e d . A t th i s s t a g e it is e n o u g h s i m p l y t o en te r th e m o d u l e c o r r e c tl y
— th e o n l y v i s i b l e e f f e c t o f R U N n i n g it w i l l b e t o c h a n g e t h e s cr een c o l o u r .
C H E C K S U M T A B L E
1 0 0 0 0 1 2 3
1 0 0 3 1 3 . 1 6
1 0 0 4 0 1 8 2
1 0 0 2 0 2 3 8
1 0 0 3 2 . 1 . 6 4
3 . 0 0 5 0 3 . 3 2
1 0 0 3 0 1 2 3
1 0 0 3 5 2
3 . 0 0 6 0 21
1 0 0 7 0 2 2 0
T h i s ta b l e o f c h e c k s u m s is he re t o h e l p y o u en s u re th a t y o u m a k e n o
e rr or s in th e en tr y o f w h a t w i l l b e a l o n g a n d c o m p l e x p r o g r a m . F o r an
u n d er s ta n d i n g o f h o w t o u s e th e ta b l e s e e A p p e n d i x A , w h i c h g i v e s th e
l i s t i n g o f th e ch e c k s u m p ro g ra m t o b e a d d e d t o th e en d o f t h e M a s t e r c o d e
p ro gr am w h e n y o u sta rt , e n a b l i n g y o u t o g e n e r a te y o u r o w n ch e c k s u m
ta b l e s f o r c o m p a r i s o n w i th t h o s e g i v e n in t h e b o o k .
1 2
Page 15
C h a p te r 1 M a s te r c o d e M o n i t o r
M O D U L E 1.2
1 9 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 9 0 0 1 R E M T E M P O R A R Y L I N E S
1 9 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 9 0 1 0 R E T U R N
1 9 0 1 1 R E M * * * E N D O F M O N I T O R P R O G R A M * * *
T h i s t e m p o r a r y m o d u l e is p l a c e d i n t o t h e p ro g ra m at th i s p o i n t t o a l l o w
f o r t h e f a c t t h a t t h e i n i t i a l i s a t i o n r o u t i n e c a l l s u p a la te r s e c t i o n o f th e M a s
te r c o d e p ro g ra m w h i c h wi ll n o t b e en te re d y e t . T h e l i n e s c o n t a i n e d i n th is
m o d u l e wil l b e ov e r w r i tt e n w h e n s u b s e q u e n t s e c t i o n s o f t h e M a s t e r c o d e
p ro g ra m are e n te r e d . T h e f o r m a t is n o t c r it i c a l , s o n o c h e c k s u m t a b l e is
n ee d e d .
M O D U L E 1.3
mr
1 0 1 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 0 1 0 1 R E M C O N T R O L R O U T I N E F O R M O N I T O R
1 0 1 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 0 1 1 0 D A T A E X I T T O B A S I C , M E M O R Y M O D I F Y , M
E M O R Y D U M P , M A C H I N E C O D E E X C U T E
1 0 1 1 1 D A T A L O A D M A C H I N E C O D E F I L E , S A V E M
A C H I N E C O D E F I L E
1 0 1 2 0 D A T A D I S A S S E M B L E R
1 0 1 3 0 D A T A F I L E E D I T O R
1 0 1 4 0 D A T A A S S E M B L E R
1 0 1 9 0 D A T A E N D
1 0 2 0 0 R E S T O R E
1 0 2 2 0 X = 0
1 0 2 3 0 P R I N T " [ B L U E 3 C C L R 3
E C O D E M O N I T O R
- - - - - - - - - -
- - - - - - - - - -
M A C H IN
C G R E E N 3 C C D 3 "
1 0 2 5 0 R E A D T $
1 0 2 6 0 IF T * < > " E N D " T H E N P R I N T T A B ( 5 ) X "
) " T $ : X = X + l s G O T O 1 0 2 5 0
1 0 2 6 5 I F X < 1 5 T H E N F O R Y = X T O 1 5 s P R I
N T s N E X T
1 0 2 7 0 P R I N T " C O M M A N D ( 0 X - l " ) : " ;
: I N P U T T
1 0 3 0 0 IF T < 0 O R T > X T H E N 1 0 1 0 0
1 0 3 0 5 IF T = 0 T H E N P R I N T " C C L R 3 C 4 * C D 3
C R V S O N 3 B Y E C R V S O F F 3 C 4 * C D 3 "
: C L O S E 1 : E N D
1 0 3 1 0 O N T G O S U B 1 3 1 0 0 , 1 3 3 0 0 , 1 3 5 0 0 , 1 4 3 0 0
, 1 4 1 0 0 , 1 5 8 0 0 , 2 4 8 0 0 , 2 0 0 0 0
1 0 3 2 0 G O T O 1 0 1 0 0
1 3
Page 16
M a chi ne C o d e M a s te r
Ever y c o m p l e x p ro g ra m n e e d s t o p r o v i d e th e u ser w it h a m e a n s o f
s el ec ti n g w h ic h o f its m a n y f u n c t i o n s is t o be u se d n e x t . S uc h a f a c i l i t y is
c a ll ed a ‘ c o n t r o l r o u t i n e ’ o r , m o r e s i m p ly , a ‘ m e n u ’ . T h e m e n u g i v e n her e
is m o r e c o m p l e x t h a n it n e e d str ic tly h a v e b e e n f o r th e current p ro g ra m .
T hi s is b e c a u s e th e M o n i t o r p ro g ra m is d e s i g n e d s o th a t it ca n la ter be
ex t e n d e d by a d d i n g s u b s e q u e n t s e c t i o n s o f th e o v e r a ll M a s t e r c o d e A s s e m
bler. Rather th a n h a v in g t o en te r ne w p r o g r a m lin e s t o ta k e a c c o u n t o f th e
ext ra f u n c t i o n s th a t wi ll b e p r o v i d e d , th e m e n u wi ll a u t o m a t i c a l l y ex t en d
i t s e l f t o t a k e a c c o u n t o f n ew o p t i o n n a m e s e n te re d i n t o th e d at a
s ta te m e n ts .
C H E C K S U M T A B L E
1 0 1 0 0 1 2 3 1 0 1 0 1 1 0 1
1 0 1 10 1 8 0 1 0 1 1 1
1 0 1 3 0 1 7 0 1 0 1 4 0
6 2 1 0 1 2 0
6 5 1 0 1 9 0
1 0 2 0 0 1 4 0 1 0 2 2 0 1 2 2
1 0 2 5 0 3 1 1 0 2 6 0 2 1 0
1 0 2 7 0
2 3 4
1 0 : 3 0 0 8
1 0 1 0 2 1 2 3
1 0 2 3 0
1 0 2 6 5
/ I 0 3 0 5
1
1 9 2
1 6 0
1 0 3 1 0 1 9 9 1 0 3 2 0 1 5 5
S E C T I O N 2 : O u t p u t o f M e m o r y C o n t e n t s t o S cr e en
In this s e c t i o n o f th e p ro g ra m w e sh al l e x a m in e t h o s e s e c t i o n s o f th e p r o
gram w h ic h ar e n ec e s s a ry t o e n a b le us t o pri nt o u t , in an ord er ly f a s h i o n ,
th e c o n t e n t s o f a s p e c i f i e d are a o f m e m o r y . T h e m o d u l e s c o m m e n t e d o n
her e m ay appear v er y i n s i g n i f i c a n t an d y o u m a y w o n d e r w h y it is t h a t t h e y
ha v e n o t b ee n ru n t o g e t h e r t o m a k e o n e m o d u l e . A s y o u c o n t i n u e t h r o u g h
th e M a s t e r c o d e p r o g r a m , h o w e v e r , y o u w il l see th a t in d i v i d u a l m o d u le s
m ay a ct u a ll y b e ca l l ed up f o r u s e fr o m m a n y d i f f e r e n t parts o f th e p r o
gram . K eep in g th e m o d u l e s t o o n e p art icu la r f u n c t i o n a n d o n e o n l y wi ll
e n a b le us t o s av e o n th e e v e n t u a l n um be r o f p ro gr am li n e s e m p l o y e d ra the r
th a n h a v e t o d u p l i c a te th e s a m e li n e s later in a n o th e r s e c t i o n o f th e p r o
gra m.
1 4
Page 17
C h a p te r 1 M a s te r c o d e M o n i t o r
M O D U L E 1.4
1 1 ( 3 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 1 0 0 1 R E M C O N V E R T D E C I M A L T O H E X
1 1 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 1 0 1 0 T = H s H $ = " "
1 1 0 2 0 H * = C H R * ( F N H E X < T - I N T < T / 1 6 > * 1 6 ) ) + H
* s T = I N T ( T / 1 6 ) : I F T > 0 T H E N 1 1 0 2 0
1 1 0 5 0 R E T U R N
T h is thre e li n e m o d u l e t r a n s f o r m s a d e c i m a l n um be r i n t o a h e x a d e c i
m al n um be r, th a t is o n e w i th a b a s e o f 1 6 ra the r th a n a b a s e o f 1 0 .
M a c h i n e - c o d e pr o gr a m m er s a l m o s t u n i v e r s a l ly u se h e x a d e c i m a l n u m
ber s, f o r th e s i m p le re a s o n th a t t h e y c o n f o r m m u c h m o r e l o g i c a l l y t o
th e s ys te m o f b in ar y a ri th m e ti c u s ed b y a c o m p u t e r .
T h e h e x a d e c i m a l n u m b e ri n g s y s te m h a s 1 6 , rath er th a n 1 0 d ig i t s , a s
f o l l o w s : 01 2 345 67 89A B CD E F. M o s t m o d e r n c o m p u te r s
st o r e n um be rs in u ni ts o f 2 5 6 (0 - 2 5 5 ) , a n d th e r e a s o n th a t h e x a d e c i
ma l is s o c o n v e n i e n t is t h a t w it h a t w o d ig i t h e x a d e c i m a l n u m b e r , th e
m a x i m u m v a l u e w h i c h ca n b e e x p re s s ed is a l s o 255 (1 5 * 1 6 f o r th e h ig h
d ig it an d 1 5 f o r th e l o w ) . U s i n g h e x a d e c i m a l m e a n s th a t a m u c h m o r e
or d er ly re p r e s e n ta ti o n o f th e v a l u e s s to r e d in m e m o r y ca n b e m a d e . In
a d d i t i o n , th e b in ar y s y s t e m u s e d b y th e c o m p u t e r m e a n s t h a t very
o f t e n a p p a r en tl y s i g n i f i c a n t n um be rs in h e x a d e c i m a l l i k e 100 0 (or 4 0 9 6
in d e c i m a l ) ar e a l s o s i g n i f i c a n t in te rm s o f th e o p e r a t i o n o f th e
c o m p u t e r . B e g i n n i n g t o t h i n k in h e x a d e c i m a l is an i m p o r t a n t aid t o
b eg i n n i n g t o u n d er s ta n d th e w o r k i n g s o f th e m i c r o .
C o m m e n ta r y
11 0 2 0 : T h e o p e r a t i o n o f th i s l i n e is b$st ex p l a i n e d b y u s e o f a n e x a m
p le . A s s u m e th a t th e d e c i m a l n u m b e r 48 75 ha s b e e n s to r ed i n th e
var ia b le H . T o c o n v e r t th a t v a l u e i n t o h e x a d e c i m a l , w e n ee d t o first
r e c o g n is e t h a t it is m a d e u p o f 1 * 1 6 |3 ( 1 6 | 3 = 4 0 9 6 ) + 3 * 1 6 | 2
( 1 6 | 2 = 2 5 6 ) + 0 * 1 6 | 1 ( 1 6 j l = 1 6 ) + l l * 1 6 t 0 ( 1 6 | 0 = 1 ) . T h i s li n e i s o l a t e s
ea c h o f t h e s e u n i ts o f d i f f e r e n t p o w e r s o f 1 6 an d t h e n tr a n s la te s t h e m
i n t o a ch ara ct er w h i c h re pr ese n ts th e a p p r o p r ia t e h e x a d e c i m a l d ig i t ,
u s in g th e user d e f i n e d f u n c t i o n F N H E X (se e l in e 10 040) t o s el ec t th e
co rr ec t ch a ra ct er . In th e c a s e o f 48 7 5 th e h e x a d e c i m a l n um be r w il l be
130B. F o r u n i ts w i th a v a l u e f r o m 0 t o 9 , F N H E X s i m p ly returns th e
v a l u e o f th e ap p r o p r ia t e ch ar a c te r 0 - 9 (char act er c o d e s 48 - 5 7 ). I f th e
v a l u e o f th e u n i t is f r o m 1 0 - 1 5, t h e n a fu rt he r 7 is a d d e d t o th e c h a
ra ct e r c o d e v a l u e t o ta k e it i n t o th e ra ng e A - F in th e 6 4 ’ s ch ara ct er
s et .
1 5
Page 18
M ach in e C o d e M a s te r
C H E C K S U M T A B L E
1 1 0 0 0 1 2 3
1 1 0 1 0 1 7 0
M O D U L E 1.5
1 1 0 0 1 1 6 7
1 1 0 2 0 7 4
1 1 0 0 2 1 2 3
1 1 0 5 0 1 4 2
1 1 9 5 1 R E M C O N V E R T H E X I N H * T O D E C I N H
1 1 9 5 2 R E M * * * * * * * * * * * * * * * - * * * * * * * * * * * * * * *
1 1 9 7 5 E R R = F A L S E : H = 0 s IF L E N < H * > = 0
T H E N 1 2 0 3 0
1 1 9 8 0 F O R X = 1 T O L E N ( H T )
1 1 9 9 0 T = F N D E C < A S C ( M I D * < H * , X , 1 > ) ) : H =
H * B A S E + T
1 2 0 1 0 IF T > B A S E — 1 D R T < 0 T H E N E R R = T R U E
1 2 0 2 0 N E X T X
1 2 0 3 0 R E T U R N
R E A D Y .
A l t h o u g h it is m o r e s e n s i b l e t o i n p u t n um be rs in h e x a d e c i m a l , w o r k i n g
in B A S I C d o e s m e a n th a t t h e y h a v e t o b e tr a n s la te d i n t o o rd in a r y d e c i m a l
f o r u s e b y th e p r o g r a m . T h i s is a c c o m p l i s h e d b y th e cu rrent m o d u l e .
C o m m e n ta r y
1 19 75 : T h r o u g h o u t th e p r o g r a m th e v a r i a b l e E R R (err or) w il l b e u s ed t o
i n d i c a t e th a t a n e rro r h a s b e e n d i s c o v e r e d . T h e n o r m a l v a l u e o f E R R w il l
be 0 , w h i c h i s th e v a l u e a s s i g n e d t o t h e v a r i a b l e F A L S E in th e i n i t i a l i s a t i o n
r o u t i n e . W h en ev er a n e rro r is d e t e c t e d E R R is reset t o th e v a l u e o f T R U E ,
w h i ch is m in u s o n e . T h e p o i n t b e h S h d u s in g t h e s e ‘ t r u t h * v a l u e s is t h a t it
a l s o a l l o w s E R R t o b e s e t b y a s t a t e m e n t s u ch as E R R = ( A > 5 0 ) . T h e
e x p r e s s io n in b ra ck et s h as a v a l u e a c c o r d in g t o w h et h er it is tr ue or fa l s e . I f
f a l s e it wil l ta k e t h e v a l u e z e r o , i f true it w i l l h a v e th e v a l u e m i n u s o n e . B y
th i s m e a n s E R R c a n b e se t t o s h o w th a t s o m e t h i n g is w r o n g m u c h m o r e
e c o n o m i c a l l y th a n u s in g s t a t e m e n t s su ch as IF A > 5 0 T H E N E R R = -1.
1198 0-1 202 0: E x a m i n i n g e a c h ch a ra c te r o f t h e str in g H $ i n tu rn , th is l o o p
ex tr a ct s th e d e c i m a l v a l u e o f th e h e x a d e c i m a l ch ara ct er u s in g th e user
d e f i n e d f u n c t i o n F N D E C (l i n e 10 050 ). S in c e t h e l o o p w o r k s f r o m th e l e f t ,
th e result o b t a i n e d s o fa r m u s t b e m u l t i p l i e d b y 1 6 f o r e a c h s u b s e q u e n t
h e x a d e c i m a l d ig i t . I f a c h ar a c te r o u t s i d e th e ra n ge 0 - F is i n p u t , th e E R R
va ri ab le is s et t o m i n u s o n e as a w a r n in g t o s u b s e q u e n t m o d u l e s .
1 6
Page 19
C h a p t e r 1 M a s t e r c o d e M o n i t o r
C H E C K S U M T A B L E
1 1 9 5 0 1 2 3 V 1 1 9 5 1 2 3 0 - 1 1 9 5 2 123 -
1 1 9 7 5 1 4 2 / 1 1 9 8 0 1 2 8 7 1 1 9 9 0 4 0 -
1 2 0 1 0 2 0 8 V 1 2 0 2 0 2 5 0 1 1 2 0 3 0 1 4 2 7
M O D U L E 1.6
1 2 0 5 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * ' * * * * * *
1 2 0 5 1 R E M I N P U T S T A R T A D D R E S S
1 2 0 5 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 2 0 5 7 H * = /
1 2 0 6 0 I N P U T " S T A R T A D D R E S S ( IN H E X ) :
H * : G O S U B 1 1 9 5 0
1 2 0 8 0 I F E R R O R H < 0 O R H > 6 5 5 3 5 T H E N 1 2 0 6
0
1 2 0 9 0 A D = H : R E T U R N
W h e n p ri nt in g t h e c o n t e n t s o f an are a o f m e m o r y t o t h e s cr ee n , it is
n ec e s s a ry t o s p e c i f y t h e sta rt p o i n t i n m e m o r y . T h i s is d o n e i n h e x a d e c i m a l ,
and th e in p u t is t h e n tr a n s l a te d i n t o d e c i m a l b y th e p r e v i o u s m o d u l e .
C H E C K S U M T A B L E
1 2 0 5 0 1 2 3 1 2 0 5 1 19 1 2 0 5 2 1 2 3
1 2 0 5 7 1 6 2 / 1 2 0 6 0 5 0 1 2 0 8 0 1 2 8
1 2 0 9 0 1 9 9
M O D U L E 1.7
1 1 8 5 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 1 8 5 1 R E M A S K C O N T I N U E ■ ?
1 1 8 5 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 1 8 5 8 T $ = " "
1 1 8 6 0 I N P U T " C O N T I N U E ( Y / N ) : " ; T $
1 1 8 7 0 IF T $ = " Y " T H E N C O = T R U E : G O T O 1 1
8 9 5
1 1 8 8 0 I F T * < > " N " T H E N P R I N T " C C U 3 " ; : G O
T O 1 1 8 5 0
1 1 8 9 0 C O = F A L S E
1 1 8 9 5 R E T U R N
W h e n an are a o f m e m o r y is d u m p e d t o t h e sc r e e n , th i s m o d u l e is ca l l ed
t o e n q ui re w h e th e r th e us er w i s h e s t o c o n t i n u e w i th a n o t h e r .
1 7
Page 20
M a ch in e C o d e M a s te r
C H E C K S U M T A B L E
1 1 8 5 0
1 1 8 5 8
1 1 8 8 0
M O D U L E 1.8
1 1 1 0 0
1 1 1 0 1
1 2 3 1 1 8 5 1
1 7 4 1 1 8 6 0
2 3 5 1 1 8 9 0
R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
R E M B Y T E I N T O
1 1 4
1 1 8 5 2 1 2 3
3 4 1 1 8 7 0 7 2
T < y
1 1 8 9 5
1 4 2
H E X
1 1 1 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 1 1 1 0
1 1 1 2 0
1 1 1 3 0
1 1 1 4 0
1 1 1 5 0
T h i s m o d u l e d o e s th e a c t u a l w o r k o f t a k i n g a v a l u e f r o m a l o c a t i o n i n th e
m e m o r y s p e c i f i e d b y th e v a r i a b l e A D . M o d u l e 2 is t h e n c a l l e d t o t r a n s f o r m
th e va lu e i n t o h e x a d e c i m a l f o r m — s i n g l e fi g u r e h e x a d e c i m a l n u m b e rs a r e
‘ p a d d e d o u t ’ w i th a l e a d i n g z e r o in o rd er t o en s u re a s ta n d a r d is e d fo r m a t
o f t w o d ig it s per b y t e o f m e m o r y . F i n a ll y t h e h e x a d e c i m a l n u m b e r is a d d e d
t o 0 2 $ , w h i c h w i l l b e u s e d t o d i s p l a y t h e c o n t e n t s o f t h e m e m o r y t o t h e
scr ee n .
C H E C K S U M T A B L E
H = P E E K ( A D ) : A D
G O S U B 1 1 0 0 0
I F L E N ( H $ ) < 2
T H E N H $ = " 0 " + H $
0 2 $ = 0 2 $ + H $
R E T U R N
= A D + 1
1 1 1 0 0 1 2 3 1 1 1 0 1 6 6
1 1 1 1 0
1 1 1 4 0 8 2
M O D U L E 1.9
3 5
1 1 1 2 0
1 5 9
1 1 1 5 0 1 4 2
1 1 1 0 2
1 1 1 3 0
1 2 3
1 3 3 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 3 3 0 1 R E M D U M P M E M O R Y T O S C R E E N
1 3 3 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 3 3 1 0 G O S U B 1 2 0 5 0
1 3 3 2 0 P R I N T " C C L R D " : F O R X I = 1 T O 1 8 :
H = A D : G O S U B 1 1 0 0 0
1 3 3 4 0 0 2 # - = " " s 0 1 $ = H i P : 0 3 $ = " "
1 3 3 5 0 F O R X 2 = 0 T O 7
1 3 3 6 0 G O S U B 1 1 1 0 0 : 0 2 $ = 0 2 $ + " "
1 3 3 7 5 IF H > 3 1 A N D H < 9 5 T H E N 0 3 $ = 0 3 $ + C H
R $ ( H ) : G O T O 1 3 3 8 0
1 3 3 7 7 0 3 $ = 0 3 $ + " . "
1 8
Page 21
C h a p t e r 1 M a s te r c o d e M o n i t o r
1 3 3 8 0 N E X T X 2
1 3 3 9 0 P R I N T 0 1 * T A B ( 5 ) 0 2 * T A B (31 ) 0 3 $
1 3 4 0 0 N E X T X I
1 3 4 1 0 P R I N T : G O S U B 1 1 8 5 0 : I F C O T H E N 1
3 3 2 0
1 3 4 4 0 R E T U R N
W e h a v e n o w e n te re d all t h e m o d u l e s w h i c h are n ec e s s a ry t o d e f i n e a
start ad dr ess a n d t o p ic k u p d a t a f r o m th e m e m o r y . W e c a n n o w p r o c e e d t o
th e pa r t o f th e p ro g ra m w h i c h a c t u a l l y d o e s s o m e t h i n g . H a v i n g d e f i n e d th e
st art p o i n t , th is m o d u l e pr int s o u t t h e c o n t e n t s o f an area o f m e m o r y t o th e
scr een .
C o m m e n ta r y
13 3 2 0 : T h e X I l o o p w il l b e u s ed t o pri nt o u t 1 8 l i n e s , ea c h w ith ei g h t v a l u e s
ta k e n f r o m th e m e m o r y , sta r tin g at th e ad dr es s n o w s to r e d in A D .
13350 -133 80: T h e h e x a d e c i m a l v a l u e s re tu rn ed f r o m th e p r e v i o u s m o d u le s
ar e s to r ed in th e str ing 0 2 $ . I f th e v a l u e c o n t a i n e d in th e p art icu la r
m e m o r y l o c a t i o n is th e c o d e o f an A S C I I l ett er or d ig i t , th a t ch ara ct er is
s to r ed in t h e str ing 0 3 $ , f o r d is p l a y n ex t t o th e v a l u e s c o n c e r n e d . In m o s t
c a s e s , th e ch ara ct er s d is p l a y e d w il l m a k e n o s e n s e , s in c e th e f a c t t h a t th e
c o d e is th a t o f a p ri n t a b le ch ar a c te r w il l b e p u r el y c h a n c e . H o w e v e r , w h e n
e x a m in i n g areas o f m e m o r y su ch as th e v a r i a b l e s area o f th e 6 4 , o r th e
str ucture o f th e B A S I C p ro g ra m i t s e l f , o r a m a c h i n e c o d e p ro g ra m w h i c h
c o n t a i n s s tr in gs , th i s f a c i l i t y w il l b e i n d i s p e n s a b l e in g e t t i n g a p ic tu re o f
w h at an area o f m e m o r y c o n t a i n s , si n c e a n y strin gs h e l d th er e w il l be
d is p l a y e d .
C H E C K S U M T A B L E
1 3 3 0 0 1 2 3 1
1 3 3 1 0
1 6 5 1 3 3 2 0 2 4 6 1
1 3 3 5 0 1 0 4 1
1 3 3 7 7 9 0
1 3 4 0 0 4 3
3 3 0 1 1 2 9 1
3 3 6 0 1 0 0 1
1 3 3 8 0 4 4 1
1 3 4 1 0
8 6
3 3 0 2 1 2 3
3 3 4 0
1 7 3
3 3 7 5 1 7 7
3 3 9 0 8 9
13 4 4 0 1 4 2
R e v i e w
H a v i n g en te red th i s s e c t i o n y o u h a v e th e w o r k i n g b a s is o f th e p r o g r a m as a
w h o l e . In th e s e c t i o n s w h i c h f o l l o w y o u w i l l f i n d th a t m a n y o f t h e m o d u le s
e m p l o y e d ar e t h o s e w h i c h are a lr ea d y en te r e d , s in c e f u n c t i o n s su ch as
tr a n s la ti n g i n t o h e x a d e c i m a l a re c o m m o n t o t h e m a l l . B e f o r e m o v i n g o n t o
ente r th e r es t o f th e p r o g r a m , f a m il i a r i s e y o u r s e l f w i th th e o p e r a t i o n o f th e
p ro gr am s o fa r . E x a m i n e th e a re a o f m e m o r y w h i c h c o n t a i n s th e sta rt o f
1 9
Page 22
M a ch in e C o d e M a s t e r
t h e p ro g ra m i t s e l f (s ta r ti n g at 8 0 1 h e x ) an d t h e v a r i a b l e s a re a . T h i s s e c t i o n
o f t h e M o n i t o r , o n its o w n , is a p o w e r f u l t o o l i n u n l o c k i n g t h e sec ret s o f th e
6 4 ’ s m e m o r y .
MO NITOR: Out put o f M emory f r o m 8 0 1 he x
8 0 1
2 5 0 8
10
2 7 8 F 2 A
2 A 2 A
. 1 . * * *
8 0 9
2 A
2 A 2 A 2 A
2 A
2 A 2 A 2 A
* * * * * * * *
8 1 1 2 A 2 A
2 A 2 A 2 A 2 A 2 A 2 A
* * * * * * * *
8 1 9
2 A 2 A
2 A
2 A 2 A 2 A
2 A
2 A
* * * * * * * *
8 2 1
2 A 2 A 2 A 0 0 4 2 0 8
2 4
2 7 * * * . B . $ ■ '
8 2 9
8 F 2 0
4 7
4 5
4 E 4 5
5 2
41 . G E N E R A
8 3 1
4 C 2 0 4 9 4 E 4 9 5 4 4 9
41
L I N I T I A
8 3 9
4 C 4 9 5 3 41 5 4 4 9
4 F 4 E
L I S A T I O N
8 4 1
0 0 6 6 0 8 2 E
2 7
8 F 2 A 2 A . . . . ' . * *
8 4 9
2 A 2 A 2 A 2 A 2 A
2 A 2 A 2 A
* * * * * * * *
8 5 1
2 A 2 A 2 A 2 A 2 A 2 A
2 A
2 A
* * * * * * * *
8 5 9
2 A 2 A 2 A
2 A
2 A
2 A 2 A 2 A
* * * * * * * *
8 6 1 2 A 2 A 2 A
2 A
0 0 7 4
0 8 2 F
• # * • # * . . . /
8 6 9
2 7 4 2 41 5 3 4 5 2 0 B 2 2 0 ' B A S E .
8 7 1 3 1
3 6
0 0 9 B 0 8
3 0
2 7
8 B
1 6 . . . 0 ' .
8 7 9
2 0 C 3 2 8
5 0
5 4 5 2 2 4
2 9
. < P T R $ >
8 8 1
A A
C 3 2 8 4 5
2 4 2 9
B 3 B1 . . < E * > . .
8 8 9
3 2 3 5 3 5 2 0
A 7
2 0 9 C 2 0 2 5 5 . .
C O N T I N U E ( Y / N ) :
S E C T I O N 3: M o d i f y i n g t h e M e m o r y
H a v i n g l ea rn ed h o w t o e x a m i n e t h e m e m o r y w e n o w p r o c e e d t o t h e n e x t
s t a g e , w h i c h is al te r i n g its c o n t e n t s . In th i s s e c t i o n w e p re s e n t t w o m o r e
m o d u l e s w h i c h w i l l a l l o w y o u t o s te p th r o u g h t h e m e m o r y , f o r w a r d s o r
b a c k w a r d s , d is p l a y i n g t h e c o n t e n t s o f i n d i v i d u a l b y t e s a n d , i f y o u w i s h ,
al te r in g t h e c o n t e n t s o f t h e b y t e cu rr en tl y d i s p l a y e d .
M O D U L E 1 . 1 0
1 3 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * ' * * * ' #
1 3 0 0 1 R E M G E T 1 B Y T E
1 3 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 3 0 0 7 H $ ■ " "
1 3 0 1 0 I N P U T " B Y T E ( IN H E X ) : " ; H $
1 3 0 3 0 G O S U B 1 1 9 5 0
1 3 0 4 0 IF E R R O R H < 0 O R H > 2 5 5 T H E N P R I N T
" C C U I " : G O T O 1 3 0 0 0
1 3 0 5 0 R E T U R N
2 0
Page 23
C h a p te r 1 M a s te r c o d e M o n i t o r
O n th e b a s is o f p r e v i o u s m o d u l e s y o u s h o u l d h a v e l i t t l e d i f f i c u l t y in d is c er
n in g th at th i s m o d u l e a c c e p ts a h e x v a l u e in th e ra n ge 0 - F F ( 0 - 2 5 5 ) , c a l l s u p
a t r a n s l a t i o n i n t o d e c i m a l a n d returns th a t v a l u e t o th e ne x t m o d u l e , fr o m
w h i c h it is c a l l e d .
C H E C K S U M T A B L E
1 3 0 0 0 1 2 3
1 3 0 0 7 1 6 2
1 3 0 4 0 1 9 2
M O D U L E 1 .1 1
1 3 0 0 1 5 2
1 3 0 1 0 1 7 1
1 3 0 5 0 1 4 2
1 3 0 0 2 1 2 3
1 3 0 3 0 1 7 3
1 3 1 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 3 1 0 1 R E M M E M O R Y M O D I F Y
1 3 1 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 3 1 1 0 G O S U B 1 2 0 5 0
1 3 1 2 0 H = A D : G O S U B 1 1 0 0 0 : P R I N T H $ T A
B (6) "/" ; : 0 2 $ = " "
1 3 1 4 0 G O S U B 1 1 1 0 0 : A D = A D - 1 : P R I N T H $
S P C ( 6 ) ;
1 3 1 5 0 T $ = " "
1 3 1 6 0 I N P U T " + I , E s T $
1 3 1 7 0 IF T $ ~ “ + " A N D A D < 6 5 5 3 5 T H E N A D = A
D + l : G O T O 1 3 1 2 0
1 3 1 8 0 IF T A N D A D > 0 T H E N A D = A D - 1
: G O T O 1 3 1 2 0
1 3 1 9 0 I F T $ = " E " T H E N R E T U R N
1 3 2 0 0 I F T $ < > " I " T H E N P R I N T " L ' 2 * 0 1 3 " : G O
T O 1 3 1 2 0
1 3 2 1 0 G O S U B 1 3 0 0 0 : P O K E A D , H : G O T O 1 3 1
2 0
T h e p u r p o s e o f th i s m o d u l e is t o a l l o w th e user t o s te p t h r o u g h th e
m e m o r y fr o m a c h o s e n start ad dr es s a n d t o m o d i f y t h e c o n t e n t s o f i n d i
v id u a l b y t e s . T h e m a j o r part o f t h e m o d u l e is c o n c e r n e d w i th o u t p u t t i n g
th e v a l u e s in e a c h b y t e t o th e s cr ee n in a c o m p r e h e n s ib l e fo r m a t a n d t o
m o v i n g t h r o u g h t h e m e m o r y . C h a n g e s t o m e m o r y c o n t e n t s a r e a c c o m
p li s h ed b y th e la st l i n e , i n c l u d i n g a ca ll t o t h e p r e v i o u s m o d u l e
2 1
Page 24
M ac hin e C o d e M a s te r
C o m m e n ta r y
131 20-13140: H a v i n g o b t a i n e d th e st art a d d r e s s , th e a d dr ess o f th e curre nt
b y te is p rin te d o u t , to g e t h e r w it h th e v a l u e w h ic h it c o n t a i n s .
131 60-13210: F o u r p r o m p ts a re u s ed b y th e m o d u l e . ‘ + ’ m e a n s m o v e o n t o
th e n ext b y t e , * - * m e a n s m o v e b a c k o n e b y te and ‘ E * q u i ts th e m o d u l e . T h e
rem ai ni n g p r o m p t is T , w h i c h ca ll s u p th e p r e v i o u s m o d u l e an d a l l o w s a
ne w v a lu e t o be p la c e d i n t o th e curr ent b y t e .
C H E C K S U M T A B L E
1 3 1 0 0 1 2 3
1 3 1 0 1
1 1 2
1 3 1 1 0 1 6 5 1 3 1 2 0 2 2 0
1 3 1 5 0
1 3 1 8 0 1 1 6 1 3 1 9 0
1 7 4
1 3 1 6 0 1 9 2
2 1 1
1 3 1 0 2 1 2 :
1 3 1 4 0
1 7
1 3 1 7 0 7 5
1 3 2 0 0
2 0
1 3 2 1 0 2 2 9
M O D U L E 1.1 2
1 3 5 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 3 5 0 1 R E M M A C H I N E C O D E E X E C U T E
1 3 5 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 3 5 1 0 G O S U B 1 2 0 5 0 s SY S A D s R E T U R N
S h o u l d y o u w is h t o u s e th e m o n i t o r t o en te r m a c h i n e c o d e pr o g ra m s
dir ect ly i n t o th e m e m o r y in h e x a d e c i m a l f o r m , th is o n e li n e r o u ti n e wi ll
a l l o w y o u t o ca ll up th e m a c h i n e c o d e r o u t i n e w i t h o u t h a v in g t o q ui t th e
pr og ra m . I t w o u l d be w i s e n o t t o r un a n y m a c h i n e c o d e p ro g ra m b e f o r e
en su rin g th a t th e p ro g ra m s o far en te re d ha s b e e n s a v e d .
C H E C K S U M T A B L E
1 3 5 0 0 1 2 3 1 3 5 0 1 1 8 1 3 5 0 2 1 2 3
1 3 5 1 0 1 0 6
S E C T I O N 4 : S a v i n g a n d L o a d i n g F i l e s
N o w th at y o u h a v e th e a b il i t y t o en te r n e w v a l u e s i n t o th e m e m o r y an d
h en ce t o d e v e l o p a m a c h i n e c o d e p r o g r a m , y o u n ee d t o b e a b le t o s a v e th e
pr ogr am s th a t y o u wi ll e v e n t u a l l y d e v e l o p an d en te r. Y o u a l s o n e e d t o be
a b le t o re cla im t h o s e pr o gr a m s f r o m d is c or t a p e , d e p e n d i n g o n w h er e y o u
w is h t o s to re t h e m . T h e f o u r s h o r t r o u t i n e s w h i c h f o l l o w a r e d e s i g n e d t o
m ak e th is p o s s i b l e .
2 2
Page 25
C h a p te r 1 M a s te r c o d e M o n i t o r
M O D U L E 1.13
1 1 2 5 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 1 2 5 1 R E M I N P U T F I L E N A M E
1 1 2 5 2 G O S U B 2 5 5 0 0 : IF D E V = 4 T H E N 1 1 2 9 0
1 1 2 5 5 I N * = " "
1 1 2 6 0 I N P U T " F I L E N A M E s I N * : T = L
E N (IN*)
1 1 2 8 0 I F T > 1 6 O R T < 0 T H E N P R I N T " C C D 3 F I L
E N A M E I N V A L I D " s G O T O 1 1 2 6 0
1 1 2 9 0 R E T U R N
W h e n s a v i n g a b l o c k o f i n f o r m a t i o n o n ta p e o r d is c , th i s i s d o n e in th e
f o r m o f a ‘ f i l e ’ , a n a m e d l o c a t i o n w h i c h m u s t fi rst b e ‘ o p e n e d ’ b e f o r e
i n f o r m a t i o n is s e n t t o it a n d t h e n ‘ c l o s e d ’ w h e n al l t h e n e c e s s a r y i n f o r
m a t i o n h as b ee n s t o r e d . W h e n t h e i n f o r m a t i o n is re c a l l e d , t h e n a m e o f th e
f i l e n e e d s t o b e s p e c i f i e d . T h i s m o d u l e a l l o w s t h e n e c e s s a r y f i l e n a m e t o b e
i n p u t.
C H E C K S U M T A B L E
1 1 2 5 0 1 2 3 1 1 2 5 1 1 9 2 1 1 2 5 2 1 1 9
1 1 2 5 5 2 4 1 1 1 2 6 0 1 3 7 1 1 2 8 0 2 1 6
1 1 2 9 0 1 4 2
M O D U L E 1 . 1 4
1 1 2 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 1 2 0 1 R E M I N P U T F I N I S H A D D R E S S
1 1 2 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 1 2 0 5 H $ = " "
1 1 2 1 0 I N P U T " F I N I S H A D D R E S S ( IN H E X ) :
H * : G O S U B 1 1 9 5 0
1 1 2 3 0 I F E R R O R H < 0 O R H > 6 5 5 3 5 T H E N 1 1 2 0
0
1 1 2 4 0 E A = H s R E T U R N
T h e m a c h i n e c o d e p r o g r a m s w h i c h y o u w i l l e v e n t u a l l y d e v e l o p w i t h th e
a i d o f t h e p r o g r a m s i n th i s b o o k w i l l b e c o n t a i n e d i n b l o c k s o f m e m o r y . T o
s a v e t h e m , t h e p r o g r a m m u s t b e g i v e n t w o p ie c e s o f i n f o r m a t i o n , n a m e l y
w h er e t h e b l o c k star ts a n d w h e r e it f i n i s h e s . W e a l r e a d y h a v e a r o u t i n e
w h i c h o b t a i n s t h e start a d d r e s s , t h i s o n e p e r f o r m s t h e s a m e f u n c t i o n f o r
t h e f i n i s h a d d r e s s .
2 3
Page 26
M ach in e C o d e M a s t e r
C H E C K S U M T A B L E
1 1 2 0 0 1 2 3 1 1 2 0 1 7 0 1 1 2 0 2 1 2 3
1 1 2 0 5 1 6 2 1 1 2 1 0 1 0 . 1 1 1 2 3 0 1 2 3
1 1 2 4 0 2 0 0
M O D U L E 1.15
1 4 1 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 4 1 0 1 R E M M A C H I N E C O D E S A V E
1 4 1 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 4 1 1 0 G O S U B 1 1 2 5 0 ; G O S U B 1 2 0 5 0 : G O S U B
1 1 2 0 0
1 4 1 1 5 T * = " N " : I F D E V = 8 T H E N I N P U T " O V
E R W R I T E E X I S T I N G F I L E ( Y / N ) : T *
1 4 1 1 6 I F T * = " Y " T H E N I N * ~ " @ 0 : " + I N *
1 4 1 2 0 I F D E V = 8 T H E N I N * = I N $ + “ , S , W "
1 4 1 2 5 I F S A > E A T H E N 1 4 1 9 0
1 4 1 3 0 O P E N 2 , D E V , 2 , I N * : P R I N T # 2 , A D : P
R I N T # 2 , E A
1 4 1 5 0 F O R X = A D T O E A : P R I N T # 2 , P E E K < X
) : N E X T : P R I N T # 2 : C L O S E 2
1 4 1 9 0 R E T U R N
N o w th at w e c a n g i v e a n a m e t o t h e f i l e in w h i c h t h e i n f o r m a t i o n c o n
ta in ed in an area o f m e m o r y is g o i n g t o b e s to r ed a n d c a n s p e c i f y t h e s tart
p o i n t an d e n d p o i n t , w e c a n p r o c e e d t o en te r th i s m o d u l e , w h i c h wi l l s to re
th e i n f o r m a t i o n o n ta p e or d is c .
C o m m e n ta r y
1 41 25 : T hi s li n e s i m p ly c h e c k s t h a t th e user has n o t d e f i n e d a b l o c k o f
m e m o r y w h o s e e n d p o i n t i s b e f o r e its star t.
1 4 1 3 0 : A fi l e is o p e n e d , in th i s c a s e an ‘ o u t p u t ’ f i l e , w i th t h e d e s t i n a t i o n o f
th e i n f o r m a t i o n b e i n g d ic t a t e d b y th e v a l u e o f t h e v a r i a b l e D E V (d e v i c e ) .
In th e l i s t i n g o f th i s p ro g ra m it is set a t 1 (l i n e 1 0 03 5 ), w h i c h dir ec ts th e
o u t p u t t o a c a s s e t t e re co rd er . I f y o u are u s i n g a d is c d ri v e, th e n D E V s h o u l d
b e set t o 8 in li n e 10 035. O n c e t h e o u t p u t f i l e is o p e n e d , t h e fi rst t w o p ie ce s
o f i n f o r m a t i o n t o b e s to r e d in it a r e th e star t ad dr es s ( A D ) a n d th e en d
addr ess ( E A ) . L ate r in t h e p r o g r a m , a f a c i l i t y wi ll b e a d d e d t o a l l o w y o u t o
c h a n g e th e curr en t d e v i c e n u m b e r at w i l l .
1 4 1 5 0 : T h e c o n t e n t s o f ea c h b y te in t h e b l o c k o f m e m o r y t o b e s a v e d ar e
n o w s to r ed o n e b y o n e in t h e f i l e . A t th e e n d o f t h e l o o p th e f i l e is c l o s e d .
2 4
Page 27
C H E C K S U M T A B L E
C h a p te r 1 M a s te r c o d e M o n i t o r
1 4 1 0 0 1 2 3
1 4 1 1 0 2 2 4
1 4 1 2 0 1 7 9
1 4 1 5 0 1 6 1
M O D U L E 1.1 6
1 4 1 0 1 4 6
1 4 1 1 5 3 2
1 4 1 2 5 9 2
1 4 1 9 0 1 4 2
1 4 1 0 2 1 2 3
1 4 1 1 6 8 9
1 4 1 3 0 1 0 8
1 4 3 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 4 3 0 1 R E M M A C H I N E C O D E L O A D
1 4 3 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 4 3 1 0 G O S U B 1 1 2 5 0 : I F D E V = 8 T H E N I N * =
I N * + " , S , R "
1 4 3 2 0 O P E N 2 , D E V , 0 , I N * : I N P U T # 2 , S A , E A
: I F S T T H E N C L O S E 2 : R E T U R N
1 4 3 5 0 F O R X = S A T O E A : I N P U T # 2 , T : P O
K E X ,T : N E X T : C L O S E 2 : R E T U R N
T h i s m o d u l e is s i m p ly th e mirror i m a g e o f th e l a s t o n e . I n s te a d o f p la c i n g
i n f o r m a t i o n i n t o a f i l e , th is m o d u l e t a k e s p r e v i o u s l y s to r ed i n f o r m a t i o n
f r o m th e f i l e a n d p la c e s it b a c k i n t o th e c o m p u t e r ’s m e m o r y .
C H E C K S U M T A B L E
1 4 3 0 0 1 2 3 1 4 3 0 1 3 1 1 4 3 0 2 1 2 3
1 4 3 1 0 2 0 6 / 1 4 3 2 0 8 4 1 4 3 5 0 5 0
S u m m a r y
H a v i n g en te red t h e w h o l e o f th e M o n i t o r y o u are n o w fre e t o p la y a b o u t
w it h i t, t h o u g h its f u l l p o w e r wi ll o n l y b e re a lis e d o n c e t h e rest o f t h e M a s
te r c o d e p ro gr a m is e n te r e d . Try en te ri n g a n e w line : 0 A = 1 3 . C a l l u p th e
m e n u o p t i o n w h i c h a l l o w s t h e m e m o r y t o b e c h a n g e d a n d alter t h e c o n t e n t s
o f b y te 80 5 h e x t o 8 F (1 4 3 ) . L i s t t h e p ro g ra m t o 1 a n d y o u w i l l see th a t y o u r
first l i n e h as c h a n g e d t o a R E M s ta tm e n t (1 43 re pr ese n ts R E M in t h e p r o
gra m f i l e ) . U n l e s s y o u ar e v er y su re o f w h a t y o u ar e d o i n g it w o u l d b e w i s e
n o t t o try t o c h a n g e t o o m a n y o t h e r m e m o r y l o c a t i o n s a t p r e s e n t , an d
c e rt ai n ly n o t b e f o r e y o u h a v e p r o p e r l y s a v e d y o u r f i n a l v e r s io n o f th e
m o n i t o r . I f y o u d o w a n t t o m e s s a b o u t , try m o d i f y i n g s o m e o f t h e c o l o u r
a tt ri b u te b y te s f r o m D 8 0 0 - D B F F h e x , t h e c o l o u r a tt ri bu te s m e m o r y o f th e
s cr ee n . M i s ta k e s he re are n o t l i k e l y t o b e d i s a s t r o u s .
2 5
Page 28
Page 29
C H A P T E R 2
M a s te r c o d e D i s a s s e m b l e r
H a v i n g n o w e n te re d th e M o n i t o r p r o g r a m , w h i c h a l l o w s y o u t o e x a m in e
areas o f m e m o r y a n d t o c h a n g e thei r c o n t e n t s , we n o w m o v e o n t o th e n ex t
s ta g e , w h i c h is t o e n a b le y o u t o tr a n s l a te th e c o n t e n t s o f an are a o f m e m o r y
w h ic h c o n t a i n s a m a c h i n e c o d e p ro g ra m i n t o a m o r e u n d e r s ta n d a b l e f o r m .
T h is m o r e ‘ r e a d a b l e ’ f o r m f o r a m a c h i n e - c o d e p ro g ra m is k n o w n a s
‘ a s s e m b l y l a n g u a g e ’ .
T h e a d v a n t a g e o f w o r k i n g w i th a s s e m b l y l a n g u a g e is t h a t w h ile
P O K E i n g n um be rs d ir ec t ly in t o m e m o r y d o e s p erm it a m a c h i n e c o d e p r o
gra m t o b e e n te re d , th er e is n o e a s y c o r r e s p o n d e n c e b e t w e e n t h e n um be rs
b ei n g en te red i n t o th e m e m o r y o r read b a c k f r o m it a n d th e o p e r a t io n s
w h i ch th e m a c h i n e c o d e p ro g ra m w il l carry o u t . T h e p ro g ra m is m er ely a
li s t o f n um be rs an d v er y f e w p ro gr a m m er s are ev er c a p a b le o f re a di n g a
pro gr am in t h a t f o r m w i t h o u t c o n s t a n t re fe r e n c e t o ch art s c o n t a i n i n g th e
re lev an t c o d e s an d their m e a n i n g . A s s e m b l y l a n g u a g e p r o v i d e s a m e a n s o f
in p u tt i n g i n s t r u c ti o n s w h i c h w il l b e b o t h c o m p r e h e n s ib l e t o th e user (w it h a
li tt l e pra ct ic e) an d y e t re pre sen t eve ry m a c h i n e - c o d e in s t r u c t i o n in th e p r o
gram e x a c t l y . In o th e r w o r d s th e a s s e m b l y l a n g u a g e p ro g ra m c o n s i s t s o f a
seri es o f i n s t r u c t i o n s , or m n e m o n i c s , w h i c h c o r r e s p o n d t o in d i v i d u a l
m a c h i n e - c o d e o p e r a t i o n s w h i c h th e 6 5 0 2 / 6 5 1 0 c h ip is c a p a b le o f r e c o g
n is in g an d car ryi ng o u t .
I n s tr u ct i o n s in a s s e m b l y l a n g u a g e w il l n o r m a l l y be in t w o pa r ts :
1 ) A n ‘ o p e r a t i o n c o d e ’ ( o p c o d e ) w h i c h s p e c i f i e s th e t y p e o f o p e r a t i o n
w h ic h th e 6 5 0 2 / 6 5 1 0 c h i p is b e i n g a s k e d t o carry o u t , su ch as m o v e a n u m
ber f r o m o n e p la c e i n m e m o r y t o a n o t h e r , c o m p a r e t w o v a l u e s o r per
f o r m an a ri th m e ti c o p e r a t i o n o n a v a l u e .
2) H a v i n g d e f i n e d th e ty p e o f o p e r a t i o n w h i c h m u s t be p e r f o r m e d it is n o w
n ec e s s ar y t o d e f i n e th e n u m b e r o n w h i c h th e o p e r a t i o n is t o b e p e r f o r m e d .
T h is p art o f th e i n s t r u c t i o n is k n o w n as t h e ‘ o p e r a n d ’ a n d m a y c o n s i s t o f a
n um ber w h ic h w il l b e a c t e d u p o n d ir ec t ly o r th e a ddr ess in m e m o r y o f a
n um be r w h ic h is t o b e o p e r a t e d o n .
A t y p i c a l m a c h i n e c o d e i n s t r u c t i o n , u p o n w h i c h th e a s s e m b l y l a n g u a g e
t r a n s l a ti o n is b a s e d , w il l t h e r e f o r e n o r m a l l y c o n s i s t o f o n e b y t e s p e c i f y i n g
th e ‘ o p c o d e ’ an d o n e o r t w o b y te s w h i c h are u s e d t o de ri v e th e n um be r t o be
op e r a t e d u p o n . S o m e t y p e s o f in s t r u c t i o n n e e d o n l y o n e b y t e , th a t s p e c i f
2 7
Page 30
M a chi ne C o d e M a s te r
y in g th e o p c o d e i t s e l f , si n c e th e y in v a r i a b l y i m p l y th a t th e v a l u e t o be
op er a t ed u p o n is at a fi x e d l o c a t i o n w h i c h d o e s n o t n ee d t o be s p el l ed o u t .
In order t o tr a n s la te a m a c h i n e c o d e p ro g ra m in th e c o m p u t e r ’ s m e m o r y
i n t o a s s e m b l y l a n g u a g e , a p ro g ra m is n e e d e d w h ic h wi ll be c a p a b le o f
id e n t i f y i n g an o p c o d e an d th e n o f d e c i d i n g h o w m a n y o f th e s u c c e e d in g
b yt es o f m e m o r y (0,1 or 2) a r e par t o f th e o p e r a n d a s s o c i a t e d w it h th a t
o p c o d e . A p ro gr a m w h i c h i s c a p a b le o f d o i n g th is is k n o w n as a ‘ d i s a s s e m
b le r ’ . I t s e f f e c t is t o t a k e th e i n c o m p r e h e n s i b l e n um be rs w h i c h m e m o r y
n o r m a l l y c o n t a i n s an d t o tr a n s l a te th e m i n t o s o m e t h i n g w h ic h (w it h a
li tt l e p rac tic e) ca n b e r ead a n d u n d e r s t o o d b y th e u ser.
T h e b r i e f an d m u ch s i m p l i f i e d e x p l a n a t i o n g i v e n a b o v e wi ll bear s o m e
st u d y i f th is is th e first t i m e y o u h a v e b e e n i n t r o d u c e d t o th e id e a o f a d i s a s s
em bl er . O n c e th e c o n c e p t is s tr ai gh t in y o u r m i n d , y o u s h o u l d h a v e li tt l e
tr o u b l e in u n d e r s ta n d i n g th e b a s is o n w h i c h th e f o l l o w i n g s e c t i o n o f th e
M a s t e r c o d e p ro g ra m w o r k s . B y m e a n s o f a ser ies o f ta b l es s to r ed in
str in gs , th e pr o gr am i s c a p a b l e o f i d e n t i f y i n g m a c h i n e c o d e in s t r u c ti o n s i n
a s p e c i fi e d are a o f m e m o r y an d o f p ri n t in g o u t th e t y p e o f o p e r a t i o n s and
their o p er a n d s in a s s e m b l y l a n g u a g e . T h e p ro g ra m ca n be u se d in at le a s t
t w o way s:
a ) Fo r th e user w h o is d e v e l o p i n g p r o g r a m s in a s s e m b l y l a n g u a g e , th e
D i s a s s e m b l e r a l l o w s th e p ro g ra m in m e m o r y t o be m o r e e a s i l y c h e c k e d
du rin g th e p r o c e s s o f en te r i n g an d d e b u g g i n g .
b) Fo r t h o s e w h o w is h t o g o furthe r in th eir e x p l o r a t i o n o f th e m e m o r y o f
th e C o m m o d o r e 6 4 , th e p r o g r a m a s l i s te d is q u it e c a p a b le o f g i v i n g a
c o m p l e t e t r a n s l a t i o n o f th e m a c h i n e ’ s R O M , th e p e r m a n e n t b u i l t - i n p r o
gram w h ic h a c t u a l l y runs th e m a c h i n e . In t h i s w a y a be tt er u n d e r s ta n d i n g
o f the 6 4 ’ s i nt ern al w o r k i n g s c a n be b u i l t u p an d it i s p o s s i b l e t o e x a m in e
w ay s in w h ic h in d i v i d u a l r o u ti n e s w i th i n th e R O M c a n b e u s ed e f f e c t i v e l y
w i th i n th e u s e r ’ s o w n p r o g r a m s .
S E C T I O N 1 : S e t t i n g u p T a b l e s
M O D U L E 2.1
1 2 2 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 2 2 0 1 R E M H E X L O A D E R
1 2 2 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 2 2 1 0 T l $ = " "
1 2 2 2 0 F O R XI = 1 T O L . E N ( T $ ) S T E P 2
1 2 2 3 0 T l * = T l f + C H R T ( F N D E C ( A S C ( M I D * ( T * , X
1 , 1 ) ) ) * 1 6 + F N D E C ( A S C < M I D $ ( T $ , Xl + 1 , 1 ) ) ) )
1 2 2 6 0 N E X T X I
1 2 2 7 0 R E T U R N
T h e r ea l p u r p o s e o f t h i s m o d u l e w i l l n o t b e c o m e a p pa r en t u n t il t h e ta b l e s
in th e f o l l o w i n g m o d u l e h a v e b e e n e x p l a i n e d . Its f u n c t i o n is t o t a k e v a l u e s
2 8
Page 31
C h a p t e r 2 M a s t e r c o d e D is a s s e m b l e r
f r o m t h e t a b l e s a n d t o c o m p a c t t h e m i n t o s tr in g s . T h e t a b l e v a l u e s h a v e
b e e n set o u t i n t h e f o r m o f t w o d ig i t h e x a d e c i m a l v a l u e s (ie n u m b e rs in th e
rang e 0 -2 5 5 d e c i m a l ) . T h i s m o d u l e c o n v e r t s a p a ir o f h e x a d e c i m a l v a l u e s
i n t o a s i n g l e A S C I I c h a ra c te r . T h e ch a ra c te r s t h u s f o r m e d c a n b e e c o n o
m i c a l l y s to r e d i n a s tr in g ( T l $ ) .
C H E C K S U M T A B L E
1 2 2 0 0 1 2 3 1 2 2 0 1 1 0 7 1 2 2 0 2 1 2 3
' U 1 2 2 1 0 2 2 3 1 2 2 2 0 2 1 6 1 2 2 3 0 1 5 4
1 2 2 6 0 4 3 1 2 2 7 0 1 4 2
M O D U L E 2 . 2
1 9 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 9 0 0 1 R E M I N I T A L I S E D E C O D E R T A B L E S
1 9 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 9 0 0 5 B A S E = 1 6
1 9 0 0 7 D E F F N D E C ( X ) = X - 4 8 + ( X > 5 7 ) * 7
1 9 0 1 0 D I M T A t < 4 >
1 9 0 1 1 T t = " 0 A 2 2 3 8 3 8 3 8 2 2 0 2 3 8 2 4 2 2 0 2 3 8 3
8 2 2 0 2 3 8 "
1 9 0 1 2 T t = T t + " 0 9 2 2 f 3 8 3 8 3 8 2 2 0 2 3 q 0 D 2 2 , 3 8 3 8 3
8 2 2 0 2 3 8 " 1
1 9 0 1 3 T t = T * + " 1 C 0 1 3 8 3 8 0 6 0 1 2 7 3 8 2 6 0 1 2 7 3 8 0
6 0 1 2 7 3 8 "
1 9 0 1 4 T t = T t + " 0 7 0 1 3 8 3 8 3 8 0 1 2 7 3 8 2 C 0 1 3 8 3 8 3
8 0 1 2 7 3 8 "
1 9 0 1 5 T t = T t + " 2 9 1 7 3 8 3 8 3 8 1 7 2 0 3 8 2 3 1 7 2 0 3 8 1
B 1 7 2 0 3 8 "
1 9 0 1 6 T t = T t + " 0 B 1 7 3 8 3 8 3 8 1 7 2 0 3 8 0 F 1 7 3 8 3 8 3
8 1 7 2 0 3 8 "
1 9 0 1 7 T t = T t + "2 A 0 0 3 8 3 8 3 8 0 0 2 8 3 8 2 5 0 0 2 8 3 8 1
B 0 0 2 8 3 8 "
1 9 0 1 8 G O S U B 1 2 2 0 0 : T A t < 0 > = T i t
1 9 0 1 9 T t = "0 C 0 0 3 8 3 8 3 8 0 0 2 8 3 8 2 E 0 0 3 8 3 8 3
8 0 0 2 8 3 8 "
1 9 0 2 0 T t - = T t + " 3 8 2 F 3 8 3 8 3 1 2 F 3 0 3 8 1 6 3 8 3 5 3 8 3
1 2 F 3 0 3 8 "
1 9 0 2 1 T t = T t + " 0 3 2 F 3 8 3 8 3 12 F 3 0 3 8 3 7 2 F 3 6 3 8 3
8 2 F 3 8 3 8 "
1 9 0 2 2 T t = T t + " 1 F 1D 1E 3 8 1 F 1 D 1 E 3 8 3 3 1D 3 2 3 8 1
F 1 D 1 E 3 8 "
1 9 0 2 3 T t = T t + " 0 4 1 D 3 8 3 S 1 F 1 D 1 E 3 8 1 0 1 D 3 4 - 3 8 1
2 9
Page 32
M a ch in e C o d e M a s te r
F 1 D 1 E 3 8 " ;
1 9 ( 3 2 4 I t - = T $ + " 1 3 1 1 3 8 3 8 J 3 1 1 1 4 3 8 j . A J . 1 1 5 3 8 1
3 1 1 1 4 3 8 " 1
1 9 0 2 5 T * = T $ + " 0 8 1 1 3 8 3 8 3 8 1 1 1 4 3 8 0 E 1 1 3 8 3 8 3
8 1 1 1 4 3 8 "
1 9 0 2 6 S O S U B 1 2 2 0 0 : T A $ < 0 > = T A * < 0 > + T 1 *
1 9 0 2 7 T $ = ■ - " 1 2 2 B 3 8 3 8 1 2 2 B : l 8 3 8 ; l 9 2 B 2 1 3 8 1
2 2 B 1 8 3 8 " , , !
1 9 0 2 8 T $ = T 4 - + " 0 5 2 B 3 8 3 8 5 8 2 B 1 8 3 8 2 D 2 B 3 8 3 8 3
8 2 B 1 S " 1
1 9 0 2 9 G O S U B 1 2 2 0 0 : i T A t - , ( 0 ) - - T A $ < 0 ) + T 1 *
1 9 0 3 0 T $
" 1 7 1 1 1 1 6 6 1 1 2 0 1 1 C C 1 3 8 1 1 1 4 4 1 1
B11 1 A A1 "
1 9 0 3 1 J - $
B i l l A A 1 "
1 9 0 3 2 T *
T $ + ” C 7 1 1 6 6 6 1 1 2 0 1 C C C 1 3 8 1 1 1 4 4 1 1
T $ + " 1711 | l 6 6 1 1 2 0 1 C C C 1 3 8 1 1 1 4 4 1 1
B 11 1 A A 1 "
1 9 0 3 3 T - f -
T * + " 1 7 1 l ! l 6 6 1 1 2 0 1 9 C C 1 3 81 1, 144 11
B1U A A1 "
1 9 0 3 4 T $
T * + "171 l l 6 6 6 1 : 1 1 1 1 C C C 1 3 8 1 1 4 4 5 1 1
B 1 1 1 A 1 1 "
1 9 0 3 5 T #
T $ + " 2 7 2 1 6 6 6 1 , 1 2 1 1 C C C 1 3 8 1 1 4 4 5 1 1
B 1 1 A A B 1 "
1 9 0 3 6 T * » T J ; + " 2 7 1 1 6 6 6 1 1 2 1 1 C C C 1 3 8 1 1 1 4 4 1 1
B 1 1 1 A A 1 "
1 9 0 3 7 T * = T $ + " 2 7 1 1 6 6 6 1 1 2 1 1 C C C 1 3 8 1 1 1 4 4 1 1
B i l l A"
1 9 0 3 8 G O S U B 1 2 2 0 0 : T A * ( 1 > = T l $ + C H R t ( 1 6
0 )
1 9 0 4 0 T A * < 2 ) = " A B C A N D A S L B C C B C S B E
Q B I T l B M I B N E B P L B R K B V C B V S "
1 9 0 4 1 T A * ( 2 > = T A # ( 2 ) + " C L C C L D C L I C L V C M F ' C F '
X C P Y D E C D E X D E Y E O R I N C I N X "
1 9 0 4 2 T A * < 2 ) = T A ^ ( 2 > + " I N ' y j J M F ; J S R j _ D A j _ . D X / . . D
Y , L S R J M O f ^ O R A p H A f H F ; P L A P L P "
1 9 0 4 3 T A $ ( 2 ) = T A $ ( 2 ) + " R O L * R O R k T I R T S B B C f e E
C S E D S E I S T A S T X S T Y T A X T A Y "
1 9 0 4 4 t a $ < 2> = T A $ ( 2 ) + " T S X T X A T X S T Y A ? ? ? "
1 9 0 4 6 R E T U R N
R E A D Y .
3 0
Page 33
C h a p te r 2 M a s te r c o d e D is a s se m b l e r
T h e s e s e e m i n g l y d a u n ti n g t a b l e s ar e in fa c t r e m a rk a b ly s i m p le i f th e g e n
e r a l e x p l a n a t i o n o f th e w o r k i n g o f a d is a s s e m b le r g i v e n a b o v e h as b ee n
u n d e r s t o o d .
T h e thre e s e c t i o n s o f th e t a b l e d e f i n e d b e t w e e n l i n e s 1 9 0 1 1 an d 19 029 a r e
used t o cr ea te , v i a ca ll s t o th e p r e v i o u s m o d u l e , a li n e in th e ar ray T A $ ,
c o n t a i n i n g char ac ter s w h o s e c o d e s are in th e ra ng e 0 - 5 6 . T h e s e v a l u e s p o i n t
t o a s u b s e q u e n t t a b l e w h i c h c o n t a i n s th e n a m e s , in a s s e m b l y l a n g u a g e , o f
t h e 5 6 o p c o d e ty p e s th a t a r e a v a i l a b l e w h e n a m a c h i n e c o d e i n s t r u c t i o n is
ex p re ss ed i n a s s e m b l y l a n g u a g e , p lu s o n e c o d e w h i c h s h o w s th a t an i n v a l i d
o p c o d e ha s b ee n f o u n d . T he re a re o v e r 1 50 o p c o d e s a v a i l a b l e in 6 5 0 2 / 6 5 1 0
m a c h i n e c o d e , s o w h y o n l y 56 r e p r e s e n ta ti o n s (o r m n e m o n i c s ) in a s s e m b l y
lan gu ag e? T h e a ns we r t o th e q u e s t i o n is t h a t m a c h i n e c o d e o p c o d e s fa ll
i n t o g r o u p s , su ch a s t h o s e w h i c h l o a d th e a c c u m u l a t o r w i th a v a l u e , and
such g ro u p s h a v e a c o m m o n m n e m o n i c . W i t h i n ea c h g r o u p , h o w e v e r ,
ther e ar e w i d e d i f f e r e n c e s b e t w e e n th e o p e r a n d s ie th e w a y in w h i c h th e
v a lu e t o be w o r k e d u p o n is o b t a i n e d . T h u s ea c h o p c o d e wi ll h a v e a u n i q u e
o p er a n d ty p e a s s o c i a t e d w ith it b u t a m n e m o n i c m a y b e c a p a b le o f be i n g
a s s o c i a t e d wi th seve ral d if f e r e n t t y p e s o f o p e r a n d w h e n t h e m a c h i n e c o d e
pro gr am is tr an s la te d i n t o a s s e m b l y l a n g u a g e .
T h u s , an o p c o d e w i th a v a l u e o f 1 2 7 w o u l d ha v e an en tr y at p o s i t i o n 1 2 7
in T A $ ( 0 ) . T h e A S C I I c o d e o f th e ch ara ct er at th a t p o s i t i o n w i l l b e u s e d t o
g iv e a v a l u e b e tw e e n z e ro an d 5 6. T h i s v a l u e w i l l th e n b e u s e d t o p o i n t t o
three charac ter s in th e s e c t i o n o f th e t a b l e w h i c h is d e f i n e d b e t w e e n 19 940
and 19944. T h e s e f i v e lin e s o f t e x t , w h e n b r o k e n up i n t o un it s o f th re e re p
resent all th e a v a i l a b l e 6 5 0 2 / 6 5 1 0 a s s e m b l y l a n g u a g e m n e m o n i c s fo r
o p c o d e ty p e s .
T h e re m ai ni n g s e c t i o n o f th e t a b l e s , d e f i n e d by li n e s 19 030 t o 19 03 7 g iv e
th e ty p e o f o p er a n d w h i ch is a s s o c i a t e d w it h th a t p art icu la r o p c o d e . T h e
ty p e s o f o p er a n d w il l b e e x p l a i n e d m o r e fu l l y s u b s e q u e n t l y .
S E C T I O N 2 : O p e r a n d s a n d t h e ir T y p e s
A s w as m a d e clear in th e f o r e w o r d , th er e is n o i n t e n t i o n in th is b o o k t o
p r o v i d e an i n t r o d u c t i o n t o 6 5 0 2 / 6 5 1 0 m a c h i n e c o d e . I t is a s s u m e d th a t
t h o s e w h o wil l w i s h t o u s e th e b o o k w il l ei th er a lr ea d y b e fa m il i a r t o s o m e
ex te n t w ith th e c o n c e p t s th a t li e b e h in d m a c h i n e c o d e an d a s s e m b l y l a n
g u a g e p ro g ra m m i n g or t h a t th e b o o k w il l b e u s ed in c o n j u n c t i o n w i th a
gene ra l 6 5 0 2 / 6 5 1 0 a s s e m b l y l a n g u a g e p rimer w h i c h w il l e x p l a i n th e
v a r i o u s f u n c t i o n s a v a i l a b l e o n th e 6 5 0 2 / 6 5 1 0 c h i p . I t i s , h o w e v e r , n e c e s s
ar y f o r th e u n d e r s ta n d i n g o f th e p ro g ra m at th i s s ta g e , t o p r o v i d e s o m e
b r i e f e x p l a n a t i o n o f th e m an n er in w h i c h th e 6 5 0 2 / 6 5 1 0 c h i p u n d er s ta n d s
o p e r a n d s , th a t is t o s ay t h e v a l u e s o r m e m o r y l o c a t i o n s o n w h i c h it is c a p
a b le o f p e r f o r m i n g its 56 ty p e s o f o p e r a t i o n .
3 1
Page 34
M ac hin e C o d e M a s te r
T h e 6 5 0 2 / 6 5 1 0 c h ip is c a p a b l e o f r e c o g n is i n g 1 1 d is t in c t m e t h o d s ,
k n o w n as ad d r es s in g m o d e s , b y w h i c h th e v a l u e w h i c h is t o b e o p e r a t e d
u p o n is o b t a i n e d f r o m a m a c h i n e c o d e p r o g r a m . E a c h i n d i v i d u a l o p c o d e
req uir es th e u s e o f o n e o f th e s e 1 1 d i f f e r e n t m e t h o d s . T h e d is a s s e m b le r
pro gra m m u s t be c a p a b le o f r e c o g n is i n g th e o p c o d e and th e n o f e x tr a ct in g
fr o m th at o p c o d e th e ty p e o f a d d r e s s i n g w h i c h is t o be u s e d .
T h e t w o s i m p le s t f o r m s o f a d d r es s in g a re a c c u m u la t o r a d d r es s in g an d
i m p l ie d addres si ng:
1 ) A c c u m u l a t o r ad dr ess in g: s o m e o p c o d e s s p e c i f y , w i t h o u t th e n ee d f o r
an y further sp el l in g o u t o f a v a l u e o r m e m o r y a d d r es s , th a t th e o p e r a t i o n t o
be p e r f o r m e d is t o be car ried o u t o n th e c o n t e n t s o f th e a c c u m u l a t o r reg
is te r w it h in th e C P U . A n e x a m p l e o f th i s t y p e o f a d d r es s in g w o u l d b e ‘ s h if t
l e f t a c c u m u l a t o r ’ ( S L A in a s s e m b l y l a n g u a g e ) , w h ic h w o u l d s h i f t b its 0 - 6
in th e a c c u m u l a to r o n e p la c e t o th e l e f t , e f f e c t i v e l y m u l t i p l y i n g th e v a l u e
re presente d b y t h o s e b its b y 2 . N o furt he r re fe r en ce is n e e d e d w h e n an
o p c o d e o f th is ty p e i s s p e c i f i e d and o n l y o n e b y te o f m e m o r y is n e e d e d t o
represent an i n s t r u c ti o n o f th is k ind in a m a c h i n e c o d e p ro g ra m .
2) Im p lie d addr ess ing : a c c u m u l a t o r a d d r es s in g is a s p ec ia l c a s e o f th is
ad dr es s in g m o d e . Th e re are o th e r o p c o d e s w h i c h i m p l y w i th i n t h e m s e l v e s
th e p la c e w h er e th e v a l u e t o b e o p e r a t e d u p o n is t o b e f o u n d . A n e x a m p l e o f
th is w o u l d be ‘ tr a ns fe r a c c u m u l a t o r t o Y r e g is t er ’ ( T A Y ) . T h e e f f e c t o f th is
o p e r a t io n is e x a c t l y w h a t it s ays an d th er e is n o fu rt he r n ee d t o sp ell o u t
wher e th e v a l u e t o b e tr a ns fe rr e d is o b t a i n e d or w h er e it w il l b e p la c e d . T his
ag a in is a o n e b y te i n s t r u c t i o n in m a c h i n e c o d e .
3) I m m e d ia t e ad dre ss ing : o p c o d e s w h i c h e m p l o y th i s t y p e o f ad d r es s in g
req uir e th a t th e v a l u e t o b e a ct ed u p o n is s p e c i f i e d a l o n g w ith th e o p c o d e
i t s e l f , su ch v a l u e s b ei n g in th e ran ge 0 - 2 5 5 , or th e p o s s i b l e c o n t e n t s o f a
si n g l e b yt e o f m e m o r y . A n e x a m p l e o f t h i s ty p e o f o p c o d e w o u l d b e ‘ l o a d
a c c u m u l a to r i m m e d i a t e ’ ( L D A ) . A n in s t r u c t i o n i n v o l v i n g th i s o p c o d e
m ig h t be L D A # 1 2 7 . T h e e f f e c t o f th is i n s t r u c t i o n w o u l d be t o l o a d th e
ac c u m u l a to r w i th th e v a l u e 1 27. W h e n p u t i n t o m a c h i n e c o d e th i s t y p e o f
i n s t ru ct io n requires o n e b y t e o f m e m o r y t o s p e c i f y th e o p c o d e an d a fu r
ther o n e b y te t o s p e c i f y th e v a l u e t o b e o p e r a t e d u p o n .
4) R e la t iv e ad dr ess ing : th i s is e m p l o y e d w h e n j u m p s a re t o b e m a d e in a
pro gra m and a v a l u e is requ ired t o s p e c i f y th e p o i n t in m e m o r y t o w h i c h th e
e x e c u t i o n o f th e p ro gr a m wi ll j u m p . A s w i th th e p r e v i o u s ad d r es s in g t y p e ,
this v a lu e is in th e range 0 - 2 5 5 b u t th is ra ng e i s s p lit i n t o a p o s i t i v e and
n e g a ti v e h a l f , w ith v a l u e s f r o m 0 - 1 2 7 i m p l y in g a p o s i t i v e j u m p an d v a lu e s
fr o m 1 2 8 - 255 s p e c i f y i n g a n e g a t i v e j u m p (127 is s u bt ra ct e d f r o m th e
v a l u e ). T h e j u m p is m e a s u r ed re la ti ve t o th e a ddr ess o f th e b y te f o l l o w i n g
th e ju m p in s t r u c t i o n . A n e x a m p l e o f th i s t y p e o f o p c o d e w o u l d b e ‘ b ran ch
n o n - z e r o ’ ( B N E ) . A n in s t r u c t i o n i n v o l v i n g th is o p c o d e m ig h t t a k e th e
fo r m B N E 1 2 7 - th e e f f e c t o f th e in s t r u c t i o n w o u l d be th a t i f th e p r e v i o u s
o p e r a t io n p e r f o r m e d b y th e p ro gr a m h a d n o t re su lte d in a z e r o , a j u m p
3 2
Page 35
C h a p te r 2 M a s t e r c o d e D is a s se m b l er
fo r w a rd w o u l d b e m a d e s p a n n i n g 1 2 7 b y te s o f th e p ro g ra m b e f o r e e x e
c u t i o n c o m m e n c e d a g a i n . R e la t iv e a d d r e s s i n g , li k e th e p r e v i o u s t y p e ,
e m p l o y s o n e b y te f o r th e o p c o d e a n d o n e b y te f o r th e o p e r a n d .
B e f o r e d is c u s s i n g th e re m a in in g ty p e s o f a d d r e s s i n g it is n ec e s s a ry t o
u nd er sta n d t w o ty p e s w h i c h a re n o t im p l e m e n t e d in a pur e fo r m b u t w h i c h
fo r m th e b as is f o r oth er s:
a) In de xe d ad dr ess in g: th is m e t h o d e m p l o y s o n e o f t w o regist ers in th e
6 5 0 2 / 6 5 1 0 ch ip k n o w n a s th e ‘ i n d e x re g is t e r s ’ . T h i s ty p e o f o p c o d e u s es an
o p er a n d w h ic h s p e c i f i e s an a d dr ess in m e m o r y b u t , b e f o r e th is a d dr ess is
u s e d , it or i t s c o n t e n t s a r e m o d i f i e d by th e a d d i t i o n o f th e pr es e nt c o n t e n t s
o f o n e o f th e i n d e x reg ist ers . T h u s an in s t r u c t i o n us in g in d e x e d ad d r es s in g
req uires th at
i) th er e b e a v a l u e in th e i n d e x re gister
ii ) t h a t th e op e r a n d s p e c i f y a n ad dr es s in m e m o r y .
b) Z ero p a ge ad dr ess in g: th i s ref er s t o th e f a c t th a t t h o u g h th e 6 5 0 2 / 6 5 1 0
ch ip h as o n l y f i v e regist ers ( l o c a t i o n s w i t h i n th e c h i p in t o w h i c h v a l u e s ca n
be p la c ed an d e a s i l y a c t e d u p o n ) a c c e s s i b l e t o t h e m a c h i n e - c o d e p r o g r a m
me r, th is l i m i t a t i o n c o m p a r e d t o o th e r p o p u l a r C P U c h i p s is o v e r c o m e by
reg ard ing th e w h o l e o f th e m e m o r y f r o m ad dr es s z e r o t o a d dr ess 25 5 a s
b ei n g a series o f 1 2 8 t w o - b y t e regis te rs w h i c h c a n b e c a l l e d u p o n t o st o r e
v a l u e s f o r th e C P U t o o p e r a t e u p o n . Z er o p a g e a d d r e s s i n g is th e a d d r es s in g
m o d e b y w h ic h th i s are a o f m e m o r y is a c c e s s e d .
G o i n g b a ck n o w t o th e m a i n a d d r es s i n g m o d e s p r o v i d e d o n th e
6 5 0 2 / 6 5 1 0 ch ip w e fin d:
5 ) Z e r o - p a g e i n d e x e d ad dr es si ng : in th i s f o r m o f a d d r es s i n g th e t w o m o d e s
g i v e n a b o v e ar e c o m b i n e d . In an i n s t r u c t i o n o f th is t y p e th e v a l u e c o n
ta in ed in th e i n d e x re gister m i g h t b e s e v e n , in w h i c h c a s e th e t w o b y te
o p er a n d w o u l d re fer t o an ad dr es s in z e r o p a g e m e m o r y (0 - 2 5 5 ) t o w h i ch
w o u l d b e a d d e d th e c o n t e n t s o f th e s p e c i f i e d i n d e x re gister p lu s .
6) Indire ct ad dr ess ing : h ere th e o p e r a t i o n s p e c i f i e d b y th e o p c o d e w il l be
p er f o rm e d u p o n an ad dr es s w h i c h is n o t d ir ec t ly st a te d in th e a s s em b le r
in s t r u c ti o n b u t i s c o n t a i n e d in th e t w o b y te s w h o s e add re ss is p o i n t e d t o b y
th e t w o b y te o p e r a n d . A n e x a m p l e o f an i n s t r u c t i o n o f th i s t y p e w o u l d be
‘ j u m p ’ ( J M P ) . T h is o p c o d e w o u l d b e f o l l o w e d b y a t w o b y te o p e r a n d . T h e
op er a n d i s n o t i t s e l f th e a d dr ess in m e m o r y t o w h i c h p ro g ra m e x e c u t i o n
s h o u l d j u m p , rath er th e t w o - b y t e s b e g i n n i n g at th e a ddr ess s p e c i f i e d b y th e
op er a n d co nt ain an a d dr es s . I t is th is s e c o n d a d dr ess t o w h i c h th e j u m p
s h o u l d b e m a d e . T h u s J M P ( $ A A A A ) w o u l d n o t s p e c i f y a j u m p t o a ddr ess
$A A A A bu t t o th e a ddr ess re pr es en te d b y th e v a l u e st o r ed in th e t w o by te s
at $ A A A A and $ A A A B in th e m e m o r y .
T he re a r e t w o furt he r f o r m s o f i nd ir e ct a d d r es s i n g a v a i l a b l e o n th e
6 5 0 2 / 6 5 1 0 ch ip w h i c h u s e th e c o n c e p t o f i n d e x i n g d es cr ib ed b ef o r e:
7) P r e -i n d e x e d ad dr ess in g: as w i th n o r m a l i nd ir e ct a d d r e s s i n g , o p e r a n d s o f
th is ty p e c o n t a i n ad dr es s es at w h i c h w il l b e f o u n d v a l u e s t o be o p e r a t e d
3 3
Page 36
M a chi ne C o d e M a s te r
u p o n . B e f o r e o b t a i n i n g t h a t firs t a d d r e s s , h o w e v e r , p r e- in d ex ed o p e r a n d s
a r e ad de d t o th e c o n t e n t s o f th e C P U X regis te r. T h u s i f th e X regi ster
c o n t a i n s $1 0 0 a n d th e o p e r a n d is $ 1 0 0 , th e n th e a d dr ess at w h ic h th e
des ired v a l u e w il l be s o u g h t is $ 2 00 .
8 ) P o s t - i n d e x e d a d dr ess in g : h ere th e o p e r a n d s p e c i f i e s a l o c a t i o n in
m e m o r y , an d th e c o n t e n t s o f th a t l o c a t i o n ar e first o b t a i n e d , th e n th e c o n
te nt s o f th e C P U Y register are a d d e d t o th a t v a l u e . T h e re su lt is an addr ess
u p o n w h o s e c o n t e n t s th e o p e r a t i o n is t o b e p e r f o r m e d .
9) A b s o l u t e ad dr ess in g: in th i s t y p e , th e t w o b y t e o p e r a n d s p e c i f i e s an
addr ess in m e m o r y at w h i c h w il l be f o u n d th e v a l u e t o b e o p e r a t e d u p o n .
T h u s th e i n s t r u c t i o n ‘ l o a d a c c u m u l a t o r ’ , w h e n u s in g th is ty p e o f
a d d r es s in g , m i g h t h a v e th e f o r m L D A $ A A A A , w h i c h w o u l d res ult in th e
l o a d i n g o f th e a c c u m u l a t o r w i th th e v a l u e s to r ed in b y t e $ A A A A in th e
m e m o ry .
1 0 an d 1 1 ) A b s o l u t e X an d a b s o l u t e Y ad dr es si ng : in t h e c a s e o f th e s e t w o
ty p es th e ad dr ess s p e c i f i e d in th e t w o b y t e o p e r a n d is a d d e d t o th e c o n t e n t s
o f eithe r th e X or th e Y reg ist er t o arrive at th e fi n a l ad dr es s o f th e v a l u e t o
b e op e r a t e d u p o n . T h u s i f t h e c o n t e n t s o f reg ister X is $ 5 and th e o p e r a n d is
$ A A A A , th e n th e ad dr ess o f th e v a l u e t o be o p e r a t e d u p o n f o r an i n s t r u c
t i o n su ch as L D A $ A A A A , X w o u l d b e t o l o a d th e a c c u m u l a t o r w i th th e
c o n t e n t s o f th e b y te at $ A A A F in th e m e m o r y .
H a v i n g gi v e n th is b r i e f e x p l a n a t i o n o f th e d i f f e r e n t ty p e o f o p e r a n d s
w h ic h th e 6 5 0 2 / 6 5 1 0 c h i p is c a p a b le o f u n d e r s t a n d i n g , y o u s h o u l d n o w
fi n d th e s e c t i o n s o f th e p ro g ra m w h i c h d ea l w ith t h e c r e a t i o n o f a s s e m b l y
l a n g u a g e i n s t r u c ti o n s o u t o f th eir m a c h i n e - c o d e e q u iv a l e n t s ea si er t o
u nd er s ta n d w i t h o u t t o o m u c h fu rt he r c o m m e n t a r y . In t h e m o d u l e s th a t
f o l l o w , w h e n an o p c o d e is p ic k e d up f r o m m e m o r y , th e p ro g ra m wi ll
o b t a i n th e cor re c t ty p es o f a d d r es s in g f o r th e o p c o d e b y a c c e s s i n g th e
ta b l es s to r ed in th e p r e v i o u s s e c t i o n , o b t a i n i n g a v a l u e w h i c h it w il l recor d
in th e v ar ia b le O P (O P e r a n d ) . T h e v a l u e o f O P w h e n tr a n s la te d i n t o an
ad dr es si ng m o d e is g i v e n i n th e t a b l e b e l o w an d y o u w il l fi n d it u s e f u l t o
refer t o th i s w h e n f o l l o w i n g th e p ro g ra m m o d u l e s f o r th e D i s a s s e m b l e r .
V A L U E O F ‘ O P ’
0
1
2
3
4
5
6
7
8
3 4
A D D R E S S I N G M O D E
A c c u m u l a t o r
I m p lie d
I m m e d i a t e
R e la t iv e
Z e r o - p a g e i n d e x e d , X
Z e r o - p a g e i n d e x e d , Y
Z er o p a g e
P r e - i n d e x e d i nd ir e ct ( X )
P o s t - i n d e x e d i n d i re ct (Y)
Page 37
C h a p t e r 2 M a s te r c o d e D is a s se m b le r
9
1 0
1 1
1 2
C H E C K S U M T A B L E
1 9 0 0 0
1 9 0 0 5
190 1 1
123
1 1 6
1 36
19 0 1 4 9 0
1 90 1 7
9 3
1 9 0 2 0 1 3 0
1 9 0 2 3 19 3
1 9 0 2 6
1 9 0 2 9
1 9 0 3 2
1 9 0 3 5
1 9 0 3 8
2 1 3
2 1 3 1 9 0 3 0 162
1 1 a
J . 4 7 1 9 0 3 6 125
7 5
1 9 0 4 2 1 0
1 9 0 4 6 14 2
" / . 1 '
M O D U L E 2 . 3
A b s o l u t e ind ir e ct
A b s o l u t e i n d e x e d , X
A b s o l u t e i n d e x e d , Y
A b s o l u t e
19 00 1
6 6 1 9 0 0 2
1 9 0 0 7 132
1 9 0 1 2 8 9
1 9 0 1 5
1 9 0 1 0
19 02 1
9 1
241
154
1 9 0 2 4 6 1
1 9 0 2 7 18 9
1 9 0 3 3
1 9 0 4 0
1 9 0 4 3
108
8 4
8 2
* i
12 3
1 9 0 1 0
1 90 1 3
1 901 6
1 9 0 1 9
1 9 0 2 2
2 2 8
7 8
1 1 6
1 6 4
. c : . c d
1 9 0 2 5 8 7
1 9 0 2 8
5 8
1 9 0 3 1 1 4 1
1 9 0 3 4
1 9 0 3 7
1 1 1
1 1
1 9 0 4 1 175
1 9 0 4 4 2 3 8
1 5 4 5 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 4 5 1 . R E M A C C U M U L A T 0 R < OP =0>
1 5 4 5 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 4 6 0 0 1 * = 0 1 * + " A "
1 5 5 0 0 R E M I M F ' L I E D <0P =1>
1 5 5 1 0 R E T U R N
T h is b r i e f m o d u l e d e a l s w i th th e t w o si m p le s t ty p es o f a d d r es s in g m o d e:
a) A c c u m u l a t o r a d dr es si ng : al l th a t is requ ired f o r t h e d i s a s s e m b ly o f th is
t y p e is th e a d d i t i o n o f ‘ A ’ t o th e st a n d a rd o p c o d e .
b) Im p lie d ad dr es si ng : he re th e o p c o d e i t s e l f i m p l ie s its o w n o p e r a n d an d
n o fu rther a c t i o n is n e e d e d .
C H E C K S U M T A B L E
1 5 4 5 0 12 3 15 4 5 1 7 7 1 5 4 5 2 12 3
1 5 4 6 0 1 0 5 . 1 . 5 5 0 0 4 9 1 5 5 1 0 14 2
3 5
Page 38
M a ch in e C o d e M a s te r
M O D U L E 2 . 4
1 5 5 5 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 5 5 1 R E M I M M E D I A T E < O P = 2 )
1 5 5 5 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 5 6 0 GO SI JB 1 1 1 0 0
1 5 5 7 0 0 1 * = 0 1 $ + " # $ " + H $
1 5 5 8 0 R E T U R N
T hi s m o d u l e d e a l s w ith i m m e d i a t e a d d r e s s i n g . T h e b y t e f o l l o w i n g th e
o p c o d e is ta k e n t o b e an o p e r a n d in th e ra n g e 0 -2 5 5 .
C H E C K S U M T A B L E
1 5 5 5 0 1 2 3 1 5 5 5 1 1 8 9 1 5 5 5 2 1 2 3
1 5 5 6 0 1 6 0 1 5 5 7 0 1 3 3 1 5 5 8 0 1 4 2
M O D U L E 2 . 5
1 5 6 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 6 0 1 R E M R E L A T I V E ( 0 P = 3 )
1 5 6 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 6 1 0 G O S U B 1 1 1 0 0
1 5 6 2 0 I F H > 1 2 7 T H E N H = H- -2 56
1 5 6 3 0 H = H + A D
1 5 6 4 0 G O S U B 1 1 0 0 0
1 5 6 5 0 0 1 $ = 0 1 $ + " $ " + H $
1 5 6 6 0 R E T U R N
T hi s m o d u l e d ea ls w i th r e la t iv e a d d r e s s i n g a n d tr a n s la te s t h e b y te
f o l l o w i n g th e o p c o d e i n t o a n u m b e r i n t h e ra n g e - 1 2 8 t o + 1 2 7 .
C H E C K S U M T A B L E
1 5 6 0 0 1 2 3 1 5 6 0 1 1 3 9 1 5 6 0 2 1 2 3
1 5 6 1 0 1 6 0 1 5 6 2 0 2 3 9 1 5 6 3 0 1 7 7
1 5 6 4 0 1 5 9 1 5 6 5 0 9 8 1 5 6 6 0 1 4 2
M O D U L E 2 . 6
1 5 3 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 3 0 1 R E M A D D O P E R A N D I N O P T O 0 1 $
1 5 3 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 3 1 0 O N O P + 1 G O T O 1 5 4 5 0 , 1 5 5 0 0 , 1 5 5 5 0 , 1 5 6
0 0
3 6
Page 39
C h a p t e r 2 M a s t e r c o d e D is a s s e m b l e r
1 5 3 3 0 I F D P > 6 A N D O P < 1 0 T H E N G I T = 0 1 * + "
< "
1 5 3 4 0 G O S U B 1 1 1 0 0
1 5 3 5 0 0 1 * = 0 1 * + " * " : T * = H *
1 5 3 6 0 IF O P < 9 T H E N 1 5 3 9 0
1 5 3 7 0 G O S U B 1 1 1 0 0
1 5 3 8 0 0 1 * « = 0 1 * + H *
1 5 3 9 0 0 1 * = 0 1 * + T $
1 5 4 0 0 IF O F - 9 O R O P = 8 T H E N 0 1 * = 0 1 * + " ) "
1 5 4 1 0 I F O P - I N T < O P / 3 > * 3 = 1 T H E N 0 1 * = 0 1 *
+ " , X "
1 5 4 2 0 I F O P - I N T ( O P / 3 ) * 3 = 2 T H E N 0 1 * = 0 1 *
+ " , Y "
1 5 4 3 0 IF 0 P = 7 T H E N 0 1 * = 0 1 * + " ) "
1 5 4 4 0 R E T U R N
T h i s s i m p le s e c t i o n o f IF s t a t e m e n t s f o r m a t s th e a s s e m b l y l a n g u a g e
i n s t r u c ti o n s a c c o r d in g t o th e d i f f e r e n t a d d r e s s i n g m o d e s . T h e b e s t w a y t o
u nd er s ta n d th e s e c t i o n is t o c o m p a r e w h a t it d o e s t o th e o p e r a n d , o n th e
b as is o f th e v a l u e o f O P , g i v e n in th e ta b l e p r e v i o u s l y .
C H E C K S U M T A B L E
1 5 3 0 0
1 5 3 1 0 1 1 0
t l 5
1 5 3 8 0
1 5 4 1 0
1 2 3
3 5 0 1 5 6
8 0 1 5 3 9 0
2 0 7
1 5 3 0 1 1 5 8 1 5 3 0 2 1 2 3
1 5 3 3 0 1 0 1 5 3 4 0 1 6 0
1 5 3 6 0 31 1 5 3 7 0 1 6 0
1 5 4 2 0
c p ' P
2 0 9 y
1 5 4 0 0 2 3 0
1 5 4 3 0
1 0 7
1 5 4 4 0 1 4 2
S E C T I O N 3 : D i s a s s e m b l y o f M e m o r y
W e h a ve n o w en te re d th e s e c t i o n s o f th e p ro g ra m w h ic h e n a b le th e tr a n s l a
t i o n t o be m a d e f r o m m a c h i n e c o d e i n t o a s s e m b l y l a n g u a g e . I t n o w
re ma ins t o ad d t h o s e m o d u l e s w h i c h a l l o w th e p ro g ra m t o pi c k u p th e c o n
te nt s o f a s p e c i f i e d are a o f th e 6 4 ’ s m e m o r y s o t h a t t h a t th e m a c h i n e - c o d e
i n s t r u c ti o n s it c o n t a i n s m a y be d is a s s e m b le d an d p ri nt ed t o th e scr een .
M O D U L E 2 . 7
1 5 7 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 7 0 1 R E M D I S A S S E M B L E I N S T R U C T I O N
1 5 7 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3 7
Page 40
M a ch in e C o d e M a s te r
1 5 7 1 0 0 2 $ = " "
1 5 7 1 5 G O S U B 1 1 1 0 0 t H = H + l
1 5 7 2 0 I F H > 2 5 5 T H E N H = 3
1 5 7 3 0 T = A S C ( M I D $ ( T A T ( 0 ) , H ,1 ))
1 5 7 5 0 O 1 $ = M I D $ ( T A T ( 2 ) , T * 3 + 1 , 3 ) + " "
1 5 7 6 0 O P = A S C ( M I D $ < T A T ( 1 ) , I N T ( ( H + 1 ) / 2 ) ,
1 ) )
1 5 7 7 0 I F ( H A N D 1 ) = = 1 T H E N O P = O P / 16
1 5 7 8 0 O P = O P A N D 1 5
1 5 7 9 0 R E T U R N
C H E C K S U M T A B L E
1 5 7 0 0 1 2 3 1 5 7 0 1 9 3
1 5 7 1 0
1 5 7 3 0
1 5 7 7 0
T h i s m o d u l e c o n s tr u c ts th e a s s e m b l y l a n g u a g e i n s t r u c t i o n o u t o f th e
i n f o r m a t i o n p ic k e d up f r o m m e m o r y .
C o m m e n ta r y
15715-1 572 0: T h e o p c o d e b y t e h a v in g b e e n o b t a i n e d , its v a l u e is p la c e d
i n t o th e va ri a b l e ‘ H ’ .
1 5 7 3 0 : T h e o p c o d e is u s ed t o o b t a i n a p o in t e r v a l u e f r o m T A $ ( 0 ) w h i c h wi ll
i n d i c a t e t h e p o s i t i o n in T A $ ( 2 ) o f t h e th re e l ett er a s s e m b l y - l a n g u a g e
f o r m a t o f t h a t o p c o d e .
1 5 7 5 0 : A s p ac e is a d d e d a ft e r t h e o p c o d e t o c o n f o r m w i th st a n d a rd a s s e m
b ly l a n g u a g e f o r m a t .
15760-15 780 : T h e a d d r es s in g m o d e w h i c h is a s s o c i a t e d w ith th e o p c o d e i s
o b t a i n e d fr o m th e ta b l e at T A $ ( 1 ) .
M O D U L E 2 . 8
2 1 ; 9
1 3 1
1 4 6 1 5 7 8 0 1 3 3
1 5 7 1 5 1 1 9
1 5 7 5 0 1 4 8
1 5 7 0 2
1 5 7 2 0
1 2 3
1 4 8
1 5 7 6 0 2 2 4
1 5 7 9 0
1 4 2
1 5 8 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 8 0 1 R E M D I S A S S E M B L E M E M O R Y A R E A
1 5 8 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 5 8 1 0 G O S U B 1 2 0 5 0
1 5 8 2 0 P R I N T " C C L R ] " : F O R I - 1 T O 2 0
1 5 8 2 5 H « A D : G O S U B 1 1 0 0 0 : P R I N T H $ T A
3 8
Page 41
C h a p te r 2 M a s t e r c o d e D is a s se m b l e r
B ( 6 ) ;
1 5 8 3 0 G D S U B 1 5 7 0 0 : G O S U B 1 5 3 0 0
1 5 8 5 0 P R I N T 0 2 # T A B (14 ) 0 1 $
1 5 8 6 0 N E X T I
1 5 8 6 5 P R I N T
1 5 8 7 0 G O S U B 1 1 8 5 0
1 5 8 8 0 IF C O T H E N 1 5 8 2 0
1 5 8 9 0 R E T U R N
C H E C K S U M T A B L E
1 5 8 0 0 1 2 3
1 5 8 0 1 1 3
1 5 8 1 0 1 6 5 1 5 8 2 0 9 3
1 5 8 3 0 2 0 2
1 5 8 6 5 1 5 3
1 5 8 5 0 1 1 5
1 5 8 7 0 1 7 2
1 5 8 0 2
1 5 8 2 5
1 5 8 6 0
1 5 8 8 0
1 2 3
2 4 4
2 3 5
3 6
1 5 8 9 0 1 4 2
T hi s is th e c o n t r o l m o d u l e w h i c h f o r m a t s th e a s s e m b l y l a n g u a g e i n s t r u c
t i o n s o b t a i n e d b y th e p r e v i o u s m o d u l e s . F o r an e x p l a n a t i o n o f th e v a r i o u s
s u b r o u t in e c a l l s , see th e T a b l e o f S u b r o u ti n e F u n c t i o n s in th e A p p e n d i x .
S u m m a r y
E v en i f y o u ar e w o r k i n g w i th th is b o o k in c o n j u n c t i o n w i th a g o o d
6 5 0 2 / 6 5 1 0 prim er it w il l b e w o r t h , at th i s s t a g e , s p e n d i n g s o m e ti m e p l a y
ing w ith y o u r a s s em b l er an d m o n i t o r . Try d is a s s e m b li n g s o m e o f th e
ro u ti n es w i th i n th e 6 4 R O M a n d try in g t o u n d e r s ta n d a l i t t l e o f h o w t h e y
f u n c t i o n . S o m e in t er e s ti n g a d d r es s es t o b e g i n d is a s s e m b l y a re g i v e n b e l o w ,
to g e th e r w it h th e p u r p o s e s o f th e r o u ti n e s at th a t p o s i t i o n .
Be w ar n e d , h o w e v e r , th a t a n y d is a s s e m b le r is o n l y as g o o d as th e sta rt in g
p o s i t i o n in m e m o r y th a t it is g i v e n . I f y o u start th e d i s a s s e m b ly o f m e m o r y
a t a p o i n t w h i c h is in f a c t h a l f w a y th r o u g h a m a c h i n e - c o d e i n s t r u c t i o n th e n
th e fir st fe w b y t e s , at l e a s t , o f th e d is a s s e m b l y l i s t i n g w il l b e g a r b a g e , s in c e
parts o f op e r a n d s w i l l b e tr a n s l a te d as o p c o d e s . E v e n t u a l l y , a ft e r re je c tin g
a n um be r o f ap p a r e n tl y s p u r io u s in s t r u c t i o n s a n d p er h a p s l i s t i n g s o m e
n o n s e n s e i n s t r u c t i o n s , th e d is a s s e m b le r w i l l g e t i t s e l f i n t o s y n ch w i th th e
m e m o r y . A f t e r th i s it w il l b e d ist u r b e d o n l y b y t a b l e s c o n t a i n e d in th e
m e m o r y , w h i c h it w il l a g a i n a tt e m p t t o tr a n s l a te as i f t h e y we re m a c h i n e
c o d e in s t r u c t i o n s . W h e n y o u ru n u p a g a i n s t su ch p r o b le m s th e o n l y
s o l u t i o n is t o m o v e th e start ad dr es s a l o n g u n t il y o u cle ar th e ta b l e an d
m e a n i n g f u l i n s t r u c ti o n s a re d is c o v e r e d f r o m t h e sta rt o f th e d is a s s e m b le d
l i s ti n g .
3 9
Page 42
M a chi ne C o d e M a s te r
G iv en b e l o w i s a s p e c i m e n d is a s s e m b ly o f an area o f th e 6 4 ’ s interpreter
sta rt ing at th e a ddr ess o f a r o u ti n e w h o s e f u n c t i o n is t o a c c e p t th e i n p u t o f
a new B A S I C li n e u s in g v a r i o u s s u b r o u t in e s in th e 6 4 ’ s m o n i t o r and
‘ k e r n a l \
S P E C I M E N D I S A S S E M B L Y : Sta rt A d d r e s s A 4 8 0 he x
A 4 8 0
A 4 8 3
6 C 0 2 0 3 J M P
2 0 6 0 A 5 J S R
A 4 8 6 8 6 7 A
A 4 8 8
8 4 7 B
A 4 8 A 2 0 7 3 0 0
A 4 8 D
A A
S I X - * 7 A
S T Y * 7 B
J S R
T A X
( * 0 3 0 2 )
* A 5 6 0
* 0 0 7 3
A 4 8 E F 0 F 0 B E Q * A 4 8 0
A 4 9 0 A 2 F F
L D X # * F F
A 4 9 2 8 6 3 A S I X * 3 A
A 4 9 4
A 4 9 6
A 4 9 9
A 4 9 C
A 4 9 F
A 4 A 2
A 4 A 4
A 4 A 7
A 4 A 9
A 4 A B
A 4 A D
9 0 0 6 B C C * A 4 9 C
2 0 7 9 A 5 J S R
* A 5 7 9
4 C E 1 A 7 J M P * A 7 E 1
2 0 6 B A 9 J S R
* A 9 6 B
2 0 7 9 A 5 J S R *A5'79
8 4 0 B S T Y * 0 B
2 0 1 3 A 6 J S R * A 6 1 3
9 0 4 4 B C C * A 4 E D
A 0 0 1
B 1 5 F
8 5 2 3
L D Y # * 0 1
L D A
( * 5 F ) , Y
S T A * 2 3
C O N T I N U E ( Y / N ) :
4 0
Page 43
C H A P T E R 3
M a s te r c o d e F ile E d i t o r
B e f o r e p r o c e e d in g t o th e m a i n par t o f th e A s s e m b l e r p ro g ra m w e sh a ll
e x a m in e th e F i l e E d i t o r , w h i c h a l l o w s a s s e m b l y l a n g u a g e p ro g ra m s t o be
ente red in a c o n v e n i e n t f o r m an d p r a c t ic a l ly ed i te d .
I n d is c u s s i n g th e D i s a s s e m b l e r w e h a v e a lr ea d y n o t e d th e fo r m a t o f s o m e
o f th e i n d i v i d u a l i n s t r u c ti o n s w h i c h wi ll b e u s ed in a s s e m b l y l a n g u a g e p r o
gra m s. I f y o u h a v e u s ed th e D i s a s s e m b l e r t o tr a n s l a te part o f th e 6 4 ’ s RO M
th en y o u wil l a l s o h a v e s ee n th e fo r m a t in w h i c h a s s e m b l y l a n g u a g e p r o
gr am s ar e n o r m a l l y p re s e n t e d , c o n s i s t i n g o f th re e it em s o f i n f o r m a t i o n fo r
eve ry a s s e m b l y l a n g u a g e in s t ru ct i o n :
1 ) T h e m e m o r y a ddr ess at w h i c h th e in s t r u c t i o n is t o b e f o u n d .
2) T h e c o n t e n t s , in H e x , o f th e b y te s i n v o l v e d .
3 ) T h e as s e m b l y l a n g u a g e f o r m o f th e i n s t r u c t i o n .
W h en e n te ri ng an a s s e m b l y l a n g u a g e p ro gr a m t o an as s e m b l e r , o n l y th e
a s s e m b l y l a n g u a g e i n s t r u c ti o n s ar e n e e d e d . T he re are , h o w e v e r , s o m e
p r o b le m s wi th s i m p ly en te ri n g a l o n g li s t o f a s s e m b l y l a n g u a g e i n s t r u c
t i o n s . W h a t, f o r i n s t a n c e , i f w e h a v e en te re d a l o n g a s s e m b l y l a n g u a g e p r o
gram and th en d is c o v e r th a t it ne ed s a f e w m o r e i n s t r u c t i o n s s o m e w h e r e in
th e m i d d l e or th a t s o m e i n s t r u c t i o n s n ee d t o b e d e l e t e d . D o e s th e w h o l e
th i n g h a v e t o en te re d a g a i n in th e righ t or der? O b v i o u s l y th e i d e a l m e t h o d
w o u l d be s o m e t h i n g li k e th a t p r o v i d e d b y th e 6 4 ’ s B A S I C interp re ter —
n um ber ed li ne s w h i c h a r e a u t o m a t i c a l l y d e l e te d or ins e rt e d in th e co rr ec t
p la c e , wi th th e a b il i ty t o alter l i n e s a n y w h er e in th e p ro g ra m at w i l l . I t is th e
f u n c t i o n o f th e F il e E d i to r t o p r o v i d e th a t f a c i l i t y , t h o u g h th e p ro g ra m
s e c t i o n g iv en h ere g o e s fu rt her t h a n t h a t , a l l o w i n g re nu m b er in g o f th e p r o
gram and th e sa v i n g (or l o a d i n g ) o f th e a s s e m b l y l a n g u a g e f i l e b e f o r e th e
A s s e m b l e r p rop er g o e s t o w or k o n it an d tr an s la te s it i n t o m a c h i n e c o d e .
I t s h o u l d b e str ess ed th a t th e F il e E d i t o r is n o t g e n u in e l y par t o f th e
A s s e m b l e r in th a t it m a k es n o te s t o f th e m a t er ia l b e i n g en te r e d , it is p ur ely
ther e t o a l l o w n u m b e re d li n e s o f te x t to b e in s e rt e d i n t o a f i l e . N o t h i n g wi ll
be c h ec k e d or p r o c e s s e d u n t il th e A s s e m b l e r i t s e l f i s en te re d an d run.
S E C T I O N 1 : S e t t i n g U p
M O D U L E 3.1
24E 100 R E M * * * * * * * * * * * * * * * * * * * * * * ' * - * - * * * * * *
2 48 0. 1 R E M F I L E E D I T O R M E N U
4 1
Page 44
M ac h in e C o d e M a s te r
2 4 8 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 8 2 0 P R I N T
I L E E D I T O R -
2 4 8 3 5 P R I N T
I I
C C L R 3 C G R E E N ]
I I
— C B L U E 3 C C D ] "
0 )
E X I T F R O M F I L E E D I
- - - - - - - - - - - - - -
F
T O R "
2 4 8 4 0 P R I N T
2 4 8 5 0 P R I N T
2 4 8 6 0 P R I N T
2 4 8 7 0 P R I N T
2 4 8 8 0 P R I N T
2 4 8 9 0 P R I N T
2 4 9 0 0 P R I N T
2 4 9 1 0 P R I N T
I I
I I
I I
I I
I I
I I
I I
I I
1 ) I N P U T L I N E ( S > "
L I S T L I N E ( S ) "
2 )
D E L E T E L I N E ( S > "
3 )
4 ) R E N U M B E R F I L E "
5 )
I N I T I A L I S E F I L E "
6 ) L O A D F I L E "
7 )
S A V E F I L E "
8 )
A D D M A C H I N E C O D E T
0 F I L E "
2 4 9 1 5 P R I N T
I I
9 )
C H A N G E D E V I C E N U M B
E R C 5 * C D ] “
2 4 9 2 0 I N P U T
I I
C O M M A N D ( 0 - 9 ) : C O
2 4 9 4 0 I F C O = 0 T H E N R E T U R N
2 4 9 5 0 IF C O > 0 T H E N O N C O G O S U B 2 4 6 0 0 , 2 4 4
0 0 , 2 4 5 0 0 , 2 4 7 0 0 , 2 4 3 0 0 , 2 3 6 0 0 , 2 3 7 0 0 , 2 5 0 0 0
2 4 9 6 0 I F C O > 8 T H E N O N C O - 8 G O S U B 2 5 5 0 0
2 4 9 7 0 G O T O 2 4 8 0 0
A s tr a ig h tf o r w a r d m e n u m o d u l e .
C H E C K S U M T A B L E
2 4 8 0 0 1 2 3 2 4 8 0 1
2 4 8 2 0 1 2 5 2 4 8 3 5
2 4 8 5 0 9 6 2 4 8 6 0
2 4 8 8 0 1 0 2
2 4 9 1 0
2 4 9 4 0
2 4 9 7 0
M O D U L E 3 . 2
9 0 2 4 9 1 5 2 4 3
1 4 8
1 6 7
2 4 8 9 0 1 5 6
2 4 9 5 0
1 1
2 3 5
2 1 6
3 0
2 4 8 0 2 1 2 3
2 4 8 4 0
1 7 9
2 4 8 7 0 2 1 8
2 4 9 0 0
1 7 2
2 4 9 2 0 1 8 2
2 4 9 6 0 2 5 2
2 4 3 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 3 0 1 R E M I N I T A L I S E F I L E
2 4 3 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 3 1 0 P T R * = 1 1 " : E $ = " " : F O R X - 0 T O
2 5 4 : E * = E $ + C H R * ( X > : N E X T s R E T U R N
T h i s m o d u l e set s up t h e v ar i a b l e s n e c e s s a r y f o r h a n d l i n g a n e w f i l e —
c a l l i n g th i s o p t i o n w h e n a f i l e is a lr ea d y i n m e m o r y w i l l re su lt in t h e l o s s o f
t h e ex i s ti n g f i l e . T h e t w o m a i n v a r i a b l e s are P T R $ , w h i c h w i l l in d i c a t e th e
4 2
Page 45
C h a p te r 3 M a s te r c o d e File E d i t o r
p o s i t i o n o f en tr ie s i n t h e f i l e i n t h ei r co r r e c t o rd er , an d E $ , w h i c h w il l
re cor d th e p o s i t i o n o f s p a c e s f o r n ew en tr ie s . T h e u s e o f P T R $ w i l l b e
d es cr ib ed under M o d u l e 6.
C H E C K S U M T A B L E
2 4 3 0 0 1 2 3 2 4 3 0 1 1 4 5 2 4 3 0 2 1 2 3
2 4 3 1 0 2 1 7
M O D U L E 3 . 2 A
1 9 9 8 0 D I M F I * < 2 5 4 ) s G O S U B 2 4 3 0 0
T hi s m o d u l e is a c t u a l l y a part o f th e m a i n i n i t i a l i s a t i o n r o u ti n e f o r th e
D i s a s s e m b l e r t a b l e s . Its f u n c t i o n is t o set u p t h e m a i n f i l e ar ray (F IS) w h e n
th e p ro gr a m is fi rst run. O n c e t h e p r o g r a m is r u n ni n g t h e ar ray is r e -i n it ia
l i s e d b y c a l l i n g t h e p r e v i o u s m o d u l e .
C H E C K S U M T A B L E
1 9 9 8 0 1 0 1
S E C T I O N 2 : I n p u t t i n g L i n e s
M O D U L E 3 .3
2 4 6 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 6 0 1 R E M I N P U T L I N E ( S )
2 4 6 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 6 1 0 P R I N T " C C L R 3 1 1
2 4 6 2 0 I N * = " " : I N P U T I N * : B O S U B 2 4 0 0 0
: I F L „ N = - 6 5 5 3 6 T H E N 2 4 6 6 5
2 4 6 5 0 G O S U B 2 3 9 0 0 : I F L E N ( I N * ) = 0 T H E N 2
4 6 8 0
2 4 6 6 0 G O S U B 2 3 1 0 0 : I F N O T E R R T H E N 2 4 6 2
0
2 4 6 6 5 R E T U R N
2 4 6 8 0 G O S U B 2 3 0 2 0 : I F N O T E R R T H E N G O S U
B 2 3 3 0 0
2 4 6 9 0 G O T O 2 4 6 2 0
T h i s is th e m o d u l e w h i c h , w h e n a li n e is i n p u t , a l l o c a t e s t h e n ec e s s a ry
t a s k s t o th e F i l e E d i t o r ’ s v a r i o u s r o u t i n e s . O th er th a n d is t r i b u ti n g w o r k
ar o u n d o th er m o d u l e s , it s o n l y f u n c t i o n s are t o a l l o w t h e i n p u t o f t h e l i n e in
4 3
Page 46
M ac h in e C o d e M a s te r
th e f o r m o f I N $ a n d t o d e te r m i n e w h e th e r a li n e n u m b e r w i t h o u t a l in e
a tt a c h e d is b e i n g en te re d i e a d e l e t i o n .
C H E C K S U M T A B L E
2 4 6 0 0 1 2 3
2 4 6 1 0 1 4 4
2 4 6 6 0 9 4
2 4 6 0 1 4 3
2 4 6 2 0 2 5 1
2 4 6 6 5 1 4 2
2 4 6 0 2 1 2 3
2 4 6 5 0 1 0 8
2 4 6 8 0 6
2 4 6 9 0 1 6 7
M O D U L E 3 . 4
2 4 0 0 0 R E M * * * * * * * * * * * * * * * * * * - * * * * * * * * * * * *
2 4 0 0 1 R E M G E T L I N E N U M B E R
2 4 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 0 1 0 L N = - 6 5 5 3 6
2 4 0 2 0 I F L E N ( I N # ) = 0 O R I N # < " 0 " O R L E F T # (
I N # , 1 ) > " 9 " T H E N 2 4 0 9 0
2 4 0 3 0 F O R T = 1 T O L E N ( I N # )
2 4 0 4 0 I F M I D # ( I N # , T , 1 ) < = " 9 " A N D M I D # ( I N #
,T , 1 ) > = " 0 " T H E N N E X T T
2 4 0 8 0 L N = V A L ( L E F T # (I N # , T - l ) ) : I N # * M
I D # < 1 N # , T )
2 4 0 9 0 R E T U R N
H a v i n g o b t a i n e d a n i n p u t in t h e f o r m o f I N S , a lin e n u m b e r is o b t a i n e d
fr o m th e b e g i n n i n g o f th e s tr in g . T h e str in g is ex a m in e d char ac te r b y c h a
r ac te r t o f i n d th e first o n e w h i c h is o u t s i d e th e ra n g e 0 - 9 , a n d th e n t h e V A L
o f th e st ring u p t o th a t p o i n t is o b t a i n e d . S tr in g s w h i c h d o n o t b e g i n w i th a
li n e n um be r res ult in t h e li n e n u m b e r ( L N ) b e i n g s e t t o - 6 5 5 3 6 , t h u s
f l a g g i n g an error , o t h e r w i s e t h e li n e n u m b e r is s to r ed in L N an d t h e c h a
ract ers c o n t a i n i n g th e l i n e n u m b e r c h o p p e d o f f t h e o r i g i n a l str in g .
C H E C K S U M T A B L E
2 4 0 0 0 1 2 3 2 4 0 0 1 1 9 2 2 4 0 0 2 1 2 3
2 4 0 1 0 6 4 2 4 0 2 0 9 9 2 4 0 3 0 2 0 3
2 4 0 4 0 1 5 0 2 4 0 8 0 7 9 2 4 0 9 0 1 4 2
M O D U L E 3 .5
2 3 9 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 9 0 1 R E M R E M O V E L E A D I N G S P A C E S
2 3 9 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 9 1 0 F O R T = 1 T O L E N ( I N # )
2 3 9 2 0 I F M I D # ( I N # , T , 1 ) = " " T H E N N E X T 7
2 3 9 5 0 I N # = M I D # ( I N # , T ) : R E T U R N
4 4
Page 47
C h a p te r 3 M a s te r c o d e File E d i t o r
T h e re su lti n g I N S , str ip p ed o f its l i n e n u m b e r m a y n o w b e g i n w i th o n e o r
m o r e s p a ces — th i s m o d u l e r e m o v e s th e m .
C H E C K S U M T A B L E
2 3 9 0 0 1 2 3 2 3 9 0 1 1 1 2 2 3 9 0 2 1 2 3
2 3 9 1 0 2 0 3 2 3 9 2 0 8 1 2 3 9 5 0 1 1
M O D U L E 3 . 6
2 1 3 1 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 0 0 1 R E M F I L E E D I T O R
2 3 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 0 1 0 R E M F I L E E D I T O R
2 3 0 2 0 R E M F I N D L I N E N U M B E R I N 'L N' I N FI
L E
2 3 0 3 0 T = L E N < P T R 4 ; ) + 1 : T 2 = -1
2 3 0 4 0 T = T - l : I F T < = 0 T H E N G O T O 2 3 0 8 0
2 3 0 5 0 T 1 = A S C ( M I D * ( P T R $ , T , 1 ) >
2 3 0 6 0 T 2 = A S C ( M I D $ ( F I $ ( T 1 ) , 1 , 1 ) ) + 2 5 6 * A S
C < M I D $ ( F I $ ( T 1 > , 2 , 1 ) )
2 3 0 7 0 I F T2 >L .N T H E N 2 3 0 4 0
2 3 0 8 0 E R R » N O T ( T 2 = L N ) : I F E R R T H E N T =
T + l
2 3 0 9 0 R E T U R N
B e f o r e w e p r o c e e d t o th e m o d u l e w h i c h a c t u a l l y in s e rt s a li n e i n t o th e
f i l e , w e m u s t d ea l w i th th i s o n e , w h o s e f u n c t i o n is t o d et e rm i n e t h e c o r re c t
p o s i t i o n f o r t h e n ew li n e ( i f it h a s a v a l i d l i n e n u m b e r ). In e x a m i n i n g th e
m o d u l e w e sh a ll lea rn s o m e t h i n g o f t h e u s e o f P T R $ .
C o m m e n ta r y
23 03 0: In s ea r ch in g f o r th e co r re c t p o s i t i o n t o inse rt a li n e w e sh a ll m a k e
u s e o f th e string w e h a v e c a l l e d P T R $ , s h o r t f o r ‘ p o in t e r s t r in g ’ . A p o in te r
string is a sta n d ar d m e t h o d o f o v e r c o m i n g t h e p r o b le m s o f in s e rt in g n ew
li ne s in m u l t i - l i n e arr ays. I t is n o t th a t t h i s is d i f f i c u l t , it is s i m p ly t h a t t o
insert a ne w li n e at th e b e g i n n i n g o f w h a t is p o t e n t i a l l y a n ar ray o f s o m e 2 5 0
li ne s i n v o l v e s s h i f t i n g all t h e curr ent l i n e s , a t a s k w h i c h c a n b e t i m e c o n
s u m in g an d c a n a l s o cr eat e p r o b le m s w i th g a rb ag e c o l l e c t i o n , s l o w i n g
th i n g s d o w n e v e n m o r e . U s i n g a p o in t e r str in g th i s c a n b e o v e r c o m e , s i n c e
th e c o n t e n t s o f t h e array n e e d ne ver be s h i f t e d a t a l l . A l l t h a t n e e d s t o b e
d o n e is t o m a n i p u l a t e a s i n g l e s tr in g . I n s te a d o f fi n d i n g t h e co r re c t p l a c e in
th e ar ra y an d t h e n s h i f t i n g e v e r y th i n g e l s e t o m a k e r o o m f o r th e n e w li n e ,
wh at w e sh al l d o is t o fi n d w h a t s h o u l d b e th e co r re c t p o s i t i o n (as d ic t a t e d
b y t h e li n e n u m b e r ), p la c e t h e l i n e t o be en te re d in th e fir s t em p t y s p a c e w e
4 5
Page 48
M ac hin e C o d e M a s te r
fi n d and th en p ut an i n d i c a t i o n o f its a c t u a l p o s i t i o n in th e right p la c e in th e
p o in te r str ing.
T h u s th e p o in te r strin g m i g h t c o n t a i n a seri es o f b y te s w ith v a l u e s o f
3 4 , 7 6 , 2 3 3 , 1 7 6
is t o be f o u n d at p o s i t i o n 3 4 , th e s e c o n d l i n e is at p o s i t i o n 7 6 , th e third a t
p o s i t i o n 23 3 an d s o o n . T o a c c e s s th e ar ray o f lin e s in o rd er w e m u s t first
l o o k at P T R $ , ta k e f r o m th a t t h e p o s i t i o n o f th e firs t l i n e , th e n l o o k at the
s e c o n d ch ara ct er o f P T R $ t o fi n d th e p o s i t i o n o f th e s e c o n d li n e . B e c a u s e
we h a ve a c c e p te d th e arbitrary li m i t o f 255 li ne s f o r a n y o n e f i l e all th e
p o in te rs ca n b e he ld in th e f o r m o f si n g l e ch ara ct er s in P T R $ — si n g l e c h a
r ac te rs ca n h a v e an A S C I I v a l u e o f 0 - 2 5 5 . T o inse rt a n ew e n tr y , all th a t wi ll
be n ec e s s a ry is t o spli t P T R $ i n t o t w o a n d p la c e a n ew i n d i c a t o r in th e
m id d le o f it — a c o n s i d e r a b l e s a v i n g o f t i m e . In th is pa rti cu la r li n e th e m a i n
search v ar ia b le (T) is set t o L E N ( P T R $ ) + 1 , s o t h a t th e sear ch wi ll b e g i n at
th e en d o f P T R $ .
2305 0: T h e v a l u e o f ch ara ct er T in P T R $ is th e p o s i t i o n o f w h a t s h o u l d be
l in e T in th e array ( n o t t h e l i n e w i th line n u m b e r T b u t p o s i t i o n T i f w e
c o u n t e d f r o m th e b e g i n n i n g o f th e f i l e ) .
2306 0: T his o b t a i n s th e li n e n u m b e r o f th e l i n e s to r ed in FI S at p o s i t i o n T 1 .
23070: T h e sear ch c o n t i n u e s u nt il a l i n e n um be r is f o u n d w h i c h is greater
th an th at o f t h e l i n e b ei n g e n te re d ( L N ) .
. . . . . . .
W h a t th i s w o u l d m e a n is th a t th e tr ue firs t li n e in th e list
2308 0: N o t e th a t E R R i s set i f th e li n e n u m b e r b e i n g e n te re d is n o t th e s a m e
a s o n e alrea d y in th e f i l e . T h i s is s o t h a t th e n e x t m o d u l e w il l k n o w wh et h er
a l in e is b ei n g ins e rt e d or o v e r w r i tt e n .
C H E C K S U M T A B L E
; 0 i 0 1 8 2
1 04 0 1 7
; 0 7 0 9 2
M O D U L E 3 . 7
2 3 0 2 0 3 . 8 3
2 3 0 5 0 1 6 0
2 3 0 8 0
1 6
2 3 0 3 0 2 9
2 3 0 6 0
2 3 0 9 0
8 6
1 4 2
2 3 1 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 1 0 1 R E M A D D L I N E T O F I L E
2 3 1 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 1 0 5 I F L N < 0 O R L N > 6 5 5 3 5 T H E N 2 3 2 1 5
2 3 1 1 0 G O S U B 2 3 0 2 0
2 3 1 2 0 I F N O T E R R T H E N T 1 = A S C ( M I D * ( P T R *
, T , 1 ) ) s G O T O 2 3 1 5 0
2 3 1 3 0 I F E * = , , H T H E N E R R = T R U E s G O T O 2 3
4 6
Page 49
C h a p t e r 3 M a s t e r c o d e File E d i t o r
2 2 0
2 3 1 4 0 T 1 = A S C ( E * > : E * = M I D * ( E * , 2 )
2 3 1 5 0 T 2 = I N T ( L N / 2 5 6 )
2 3 1 6 0 F I * < T 1 > - C H R $ ( L N - T 2 * 2 5 6 ) + C H R * < T 2 >
+ I N *
2 3 1 7 0 I F N O T E R R T H E N 2 3 2 2 0
2 3 1 8 0 T * = " " : T l $ =
2 3 1 9 0 IF T > 1 T H E N T * = L E F T * ( P T R * , T - l )
2 3 2 0 0 I F T O L E N ( P T R - T ) T H E N 7 1 * = M I D * ( P T
R * , T )
2 3 2 1 0 P T R * = T * + C H R * ( T 1 ) + T 1 *
2 3 2 1 5 E R R = F A L S E
2 3 2 2 0 R E T U R N
T hi s is th e m o d u l e w h i c h a c t u a l l y a c c o m p l i s h e s th e i n s e r t i o n o f t h e l in e
i n t o th e f i l e .
C o m m e n t a r y
231 05: U s i n g t w o b y t e s , 0 - 6 5 5 3 5 is t h e m a x i m u m ra n g e o f p o s s i b l e l i n e
n um be rs .
23120: I f an erro r is return ed f r o m t h e p r e v i o u s m o d u l e , al l it m e a n s i s th a t
a n ew li n e n u m b e r is b e i n g e n te r e d . I f th er e is n o e rro r t h e n t h e li n e b e i n g
en te red will s i m p l y o v e r w r i te a n e x i s t i n g l i n e a n d P T R $ d o e s n o t n ee d t o b e
al ter ed at all.
23 13 0: T o s p e e d u p t h e p r o c e s s o f en tr y e v e n m o r e , a s e c o n d str in g (E $ ) is
u s ed t o rec ord al l t h e e m p t y s p a c e s in th e f i l e . R a th er t h a n s c a n n in g f o r th e
first a v a i l a b l e e m p t y s p a c e , t h e li n e wi ll b e in s e r te d in t h e p o s i t i o n in d i c a t e d
b y th e fi rst cha ra c te r i n E$ — th is ch ar a c te r is n o w l o p p e d o f f s i n c e it w il l
n o l o n g e r b e e m p t y .
23 15 0 -2 3 1 6 0 : T h e l i n e n u m b e r b y t e s a r e cr e a te d f r o m L N a n d t h e ne w li n e
in s e r te d . N o t e t h a t h a v in g p u t t h e h ig h b y t e i n t o t h e v a ri a b l e T 2
( = L N / 2 5 6 ) , t h er e is n o n e e d t o m a k e a n o t h e r v a ri a b l e e q u a l t o
L N - 2 5 6 * I N T ( L N / 2 5 6 ) . S im p l y p u t t i n g L N i n t o A S C I I f o r m w i l l l o s e
an y th in g a b o v e 25 5 as i f L N h a d b e e n A N D e d w i th 2 5 5 .
23 17 0 -2 3 2 1 0 : I f w e are d e a l i n g w i th a n e w l i n e n u m b e r t h e n P T R $ m u s t
h a v e a cha ra c te r a d d e d t o it . T h e p o s i t i o n o f t h e ch ar ac te r is i n d i c a t e d b y T
4 7
Page 50
M a ch in e C o d e M a s te r
an d all th a t is n ec e s s a ry is t o t a k e L E F T $ ( P T R $ , T - 1 ) a n d M I D $ ( P T R $ , T )
th e n t o ad d th e n ec e s s a ry ch a ra c te r b e t w e e n t h e m .
C H E C K S U M T A B L E
; i 0 0 1 2 3 2 3 1 0 1 1 9 5 2 3 1 0 2 1 2
1 0 5 7 9
2 3 1 1 0
1 3 0 4 0 2 3 1 4 0 2 0 6 2 3 1 5 0
1 6 4 2 3 1 2 0 1
9 8
1 6 0 8 4 2 3 1 7 0 6 0 2 3 1 8 0 7
; i 9 0 1 9 3 2 3 2 0 0 2 0 1 2 3 2 1 0
2 1 5 7 0
M O D U L E 3 . 8
2 3 2 2 0 1 4 2
3 0
2 3 3 00 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 3 0 1 RE M D EL ETE LINE P O I N T E D A T B Y T
23 3 02 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 3 10 T * « , , H : Tl * = " “
2 3 3 20 I F T > 1 THEN T $ = L E F T * ( P T R * , T - l )
23 3 3 0 I F T< LE N< PT R*> THEN Tl * - M ID *( PT R
$ , T + 1 >
23 3 40 E S = E$+ MID *< P T R$ ,T ,1 >
2 3 3 50 PTR* * T*+ T1*
2 3 3 60 R ET URN
T h is m a y s ee m a s tr a ng e m o d u l e t o d is c u s s un d er th e h e a d in g o f i n p u t o f
l i n e s , s in c e its p u r p o s e is t o d e l e t e th e m . T h e re a s o n w e ta l k a b o u t it h ere is
t h a t , w h e n i n p u t t i n g l i n e s , i f y o u i n p u t a li n e n u m b e r w i t h o u t a l in e
a tt a c h e d , t h e l i n e w i th th a t n u m b e r is d e l e t e d in th e sa m e w a y t h a t it w o u l d
be in B A S I C . T h e m o d u l e is t h e r e f o r e c a l l e d fr o m th e m a i n c o n t r o l m o d u l e
f o r in p u t. T h e p r o ce d u re f o l l o w e d is a mirror i m a g e o f th a t i n v o l v e d in
in s e r t i o n , w i th a p o in t e r ch a ra c te r b e i n g r e m o v e d f r o m P T R $ a n d th e
l i n e ’ s p o s i t i o n b e i n g re co rd ed as a s p a c e in E $ . N o t e th a t th er e is n o n ee d t o
a c t u a l l y r e m o v e th e c o n t e n t s o f th e l i n e — it is still th er e b u t th e F i l e E d i t o r
d o e s n o t r e c o g n is e its e x i s t e n c e a n y l o n g e r a n d w il l ov e r w r i te it w h e n a n ew
li n e is en te re d .
C H E C K S U M T A B L E
2
0 0 1 2 3 2 3 3 0 1
1 0 7 2 3 3 2 0
4 0 1 2 8 2 3 3 5 0
1 9 3 2 3 3 0 2
1 9 3
2 1 5
2 3 3 3 0 2 4 2
2 3 3 6 0
1 2 3
1 4 2
4 8
Page 51
C h a p te r 3 M a s te r c o d e File E d i t o r
S E C T I O N 3 : L i s t i n g a n d D e l e t i n g
M O D U L E 3 . 9
2 4 2 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 2 0 1 R E M F I R S T A N D L A S T L I N E S
2 4 2 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 2 0 5 I N * = " " : I N P U T " F I R S T - L A S T L I N
E S : I N *
2 4 2 1 0 S L = 0 : F L = 6 5 5 3 5 : T 3 = 0 : E R R
= F A L S E
2 4 2 2 0 I F L E N ( I N * ) = 0 T H E N 2 4 2 9 5
2 4 2 3 0 G O S U B 2 4 0 0 0
2 4 2 4 0 I F L N > = 0 T H E N S L = L N : G O T O 2 4 2 6 0
2 4 2 5 0 I F L N > - 6 5 5 3 6 T H E N F L = - L . N : G O T O
2 4 2 9 5
2 4 2 6 0 G O S U B 2 3 9 0 0 : I F L E N ( I N * ) = 0 T H E N F
L - ■ S L : G O T O 2 4 2 9 5
2 4 2 7 0 I N * = M I D * ( I N * , 2 ) : G O S U B 2 3 9 0 0
2 4 2 9 0 I F L E N ( I N * ) > 0 T H E N G O S U B 2 4 0 0 0 : F
L = L N
2 4 2 9 5 E R R = S L < 0 O R S L > 6 5 5 3 5 O R F L < 0 O R
F L > 6 5 5 3 5 O R E R R : R E T U R N
T hi s m o d u l e is u s ed i n l i s t i n g a n d b l o c k d e l e t i o n t o g e t a pair o f l i n e
n um ber s i n p u t in t h e fo r m a t ‘ 1 0 0 -3 3 0 ’ .
C o m m e n ta r y
24 2 10 -2 42 20 : T h e star t li n e (S L ) a n d fi n i s h l i n e (F L ) are set t o th e en d s o f
t h e p er m is s ib le ra n ge . I f t h e user s i m p ly pr ess es return w h e n th e p ro m p t
ap pe ar s, th e w h o l e o f th e f i l e w il l b e li s te d f r o m sta rt t o f i n i s h .
2 4 2 30 -2 42 5 0: T h e fi rst li n e n um be r is o b t a i n e d th r o u g h t h e s u b r o u t in e at
2 4 0 0 0 . I f it is gre ater th a n z e r o th e n SL is s e t e q u a l t o i t. I f ‘ - 3 0 0 ’ we re i np u t
th is wil l b e retu rned as m i n u s 3 0 0 . I n th i s c a s e S L w i l l re m ai n a t z e ro b u t F L
will b e set t o 3 0 0 a n d th e f i l e w i l l b e li s te d up t o li n e 3 0 0 .
24 260: A n y l e a d i n g sp a ce s a re str ip p ed f r o m w h a t re m a in s o f INS a f t e r th e
fi rst numb er ha s b ee n r e m o v e d . I f th er e is n o t h i n g l e f t th e n FL is se t eq u al
t o SL an d o n l y o n e l i n e is l i s te d .
2 4 2 70 -2 42 9 0: INS is str ip p ed o f t h e b e f o r e t h e s e c o n d n u m b e r , a n y l e a d
i n g s p ac es are r e m o v e d an d t h e s e c o n d v a l u e o b t a i n e d .
C H E C K S U M T A B L E
2 4 2 0 0 1 2 3 2 4 2 0 1 5 7 2 4 2 0 2 1 2 3
2 4 2 0 5 1 6 8 2 4 2 1 0 1 7 0 2 4 2 2 0 7 3
Page 52
M ac hin e C o d e M a s te r
2 4 2 3 0 1 6 3 2 4 2 4 0 1 1 2 4 2 5 0 1 3 1
2 4 2 6 0 1 8 0 2 4 2 7 0 6 2 4 2 9 0 1 2 5
2 4 2 9 5 3 8
M O D U L E 3 . 1 0
2 3 4 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 4 0 1 R E M L I S T L I N E S P O I N T E D A T B Y T
2 3 4 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 4 1 0 P R I N T A S C ( M I D $ < F I $ ( T > , 1 , 1 ) ) + 2 5 6 * A S
C < M I D $ ( F I * ( T > , 2 , 1 ) ) T A B ( 6 ) ;
2 3 4 2 0 P R I N T M I D * ( F I # ( T ) , 3 )
2 3 4 3 0 R E T U R N
T hi s m o d u l e pr int s a l i n e w h o s e p o s i t i o n is i n d i c a t e d b y th e v a ri a b l e T.
T h e lin e nu m be r is o b t a i n e d f r o m th e fi rst t w o ch a ra c te r s o f th e li n e , th e n
th e r es t o f th e li n e is p ri n t ed .
C H E C K S U M T A B L E
2 3 4 0 0 1 2 3 2 3 4 0 1 1 5 7 2 3 4 0 2 1 2 3
2 3 4 1 0 1 7 8 2 3 4 2 0 1 3 9 2 3 4 3 0 1 4 2
M O D U L E 3 . i l
2 3 5 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 5 0 1 R E M S T A R T A N D F I N I S H P O I N T E R S
2 3 5 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 5 1 0 L N = S L : G O S U B 2 3 0 2 0
2 3 5 2 0 S P = T
2 3 5 3 0 L N = F L : G O S U B 2 3 0 2 0
2 3 5 4 0 F F ' = T
2 3 5 4 5 I F E R R T H E N F P - F P - 1
2 3 5 5 0 I F F P > L E N ( P T R $ > T H E N F P = L E N t P T R *
)
2 3 5 6 0 R E T U R N
U s i n g th e sta rt a n d fi n i s h li n e n u m b e r s , th i s m o d u l e p ic k s u p f r o m P T R $
th e p o in te rs t o t h e first an d la s t li n e s t o b e l i s t e d an d st o r e s th e m i n S P an d
F P .
5 0
Page 53
C H E C K S U M T A B L E
C h a p t e r 3 M a s te r c o d e Fi le E d i t o r
2 3 5 0 0 1 2 3
2 3 5 1 0 7 3
2 3 5 4 0 2 2 0
5 0 1 1 6 5
: 5 2 0 2 3 3
5 4 5 1 1 7
2 3 5 0 2 1 2 3
2 3 5 3 0 6 0
2 3 5 5 0 1 8 9
2 3 5 6 0 1 4 2
M O D U L E 3 . 1 2
2 4 4 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 4 0 1 R E M L I S T L I N E S
2 4 4 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 4 1 0 G O S U B 2 4 2 0 0 : I F E R R T H E N 2 4 4 6 0
2 4 4 2 0 P R I N T " C C L R 3 ' 1 : G O S U B 2 3 5 0 0 : I F F
P < S F ' O R F F ' = 0 T H E N 2 4 4 6 0
2 4 4 3 0 F O R T 1 = S P T O F P : T = A S C ( M I D * ( P
T R $ , T 1 , 1 ) ) s G O S U B 2 3 4 0 0 : N E X T s P R I N T
2 4 4 5 5 I F P E E K ( 1 5 2 ) = 0 T H E N G E T T $ : I F T *
= " " T H E N 2 4 4 5 5
2 4 4 6 0 R E T U R N
U s i n g th e sta rt a n d f i n i s h p o in te r s d e te r m i n e d b y th e p r e v i o u s m o d u l e ,
th is m o d u l e n o w c al ls u p th e pri nt m o d u l e t o li st th e li n e s t o th e scr een . T h e
str an ge l o o k i n g l i n e a t 2 44 55 c h e c k s t o s e e w h e th e r t h e li n e s are a c t u a l l y
b e i n g li s te d t o a d e v i c e s u ch as t h e t a p e re co rd er o r a pr int er. I f n o t , th e
l i s ti n g w i l l b e d is p l a y e d o n t h e s cr een u n t il a k ey is p re s s e d .
C H E C K S U M T A B L E
2 4 4 0 0 1 2 3 2 4 4 0 1 1 3 4 2 4 4 0 2 1 2 3
2 4 4 1 0 1 5 4 2 4 4 2 0 2 4 1 2 4 4 3 0 1 2 6
2 4 4 5 5 2 1 4 2 4 4 6 0 1 4 2
M O D U L E 3 .1 3
2 4 5 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 5 0 1 R E M D E L E T E L I N E ( S )
2 4 5 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 5 1 0 G O S U B 2 4 2 0 0 : I F E R R T H E N 2 4 4 6 0
2 4 5 2 0 G O S U B 2 3 5 0 0 : I F F P C S P T H E N 2 4 5 6 0
2 4 5 3 0 T = S P : F O R T 1 = S P T O F P : G O S U B
2 3 3 0 0 : N E X T
2 4 5 6 0 R E T U R N
T hi s is th e b l o c k d e l e t e m o d u l e . It is in c l u d e d at th is p o i n t b e c a u s e it s s o l e
f u n c t i o n is t o ca ll u p m o d u l e s p r e v i o u s l y e n te r e d , t h e la rg es t o f w h i c h is th e
5 1
Page 54
M ac hin e C o d e M a s te r
‘ g et first an d la st l i n e s ’ r o u t i n e . I n s te a d o f l i s ti n g th e l i n e s s p e c i f i e d , th e
l in e d el et e m o d u l e is c a l l e d up f o r ea c h li n e in tu rn . N o t e th a t b e c a u s e
P T R $ is b ei n g sh o r te n e d w i th ea c h d e l e t i o n , th e ch ara ct er d e l e te d fo r
ea c h ite r a ti o n o f th e l o o p is a l w a y s at th e s a m e p o s i t i o n .
C H E C K S U M T A B L E
2 4 5 0 0 1 2 3 2 4 5 0 1 7 8 2 4 5 0 2 1 2 3
2 4 5 1 0 1 5 4 2 4 5 2 0 1 6 0 2 4 5 3 0 1 7 9
2 4 5 6 0 1 4 2
S E C T I O N 4 : L o a d i n g a n d S a v i n g
M O D U L E 3 . 1 4
2 3 7 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 7 0 1 R E M S A V E F I L E T O D E V I C E
2 3 7 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 7 0 5 G O S U B 1 1 2 5 0
2 3 7 1 0 IF D E V — 8 T H E N I N * = I N * + " „ S , W "
2 3 7 1 5 T * = " N " s I F D E V = 8 T H E N I N P U T " O V
E R W R I T E E X I S T I N G F I L E ( Y / N ) s " ; T *
2 3 7 1 6 I F T * — " Y M T H E N I N * = " 6 0 : " + I N *
2 3 7 2 0 0 P E N 2 , D E V , 2 , I N * s C M D 2
2 3 7 3 0 S L = 0 : F L . * 6 5 5 3 6
2 3 7 5 0 G O S U B 2 4 4 2 0 : P R I N T # 2 , " E N D "
2 3 7 6 0 P R I N T # 2 s C L O S E 2
2 3 7 8 0 R E T U R N
T hi s m o d u l e a l l o w s a f i l e t h a t y o u h a v e cr ea te d t o b e sa v ed o n t o ta p e
or d is c , or o u t p u t t o a p rinter.
C o m m e n ta r y
23705: T h e r o u ti n e f r o m th e M o n i t o r w h i c h re qu es ts a f i l e n a m e .
2 37 10 -2 37 16 : T h e s e li ne s a r e i n c l u d e d f o r th e b e n e f i t o f t h o s e u s in g d isc
u ni ts fo r s to r a g e . T heir e f f e c t is t o a l l o w th e user t o o v er w ri te an e x i s ti n g
fi l e o n drive z e ro wi th a s e q u e n ti a l f i l e o f th e c o n t e n t s o f FIS . T h e li ne s
a r e o n l y a c c e s s e d i f D E V is set t o 8 (d i s c dr iv e).
2 3 72 0- 23 76 0: A f i l e is o p e n e d t o th e s p e c i f i e d d e v i c e an d th e C M D 2
in s t r u c ti o n s p e c i f i e s th a t all fu rt her o u t p u t w il l b e sen t t o th a t d e v i c e . A l l
th at rem ains is t o u se th e n o r m a l li s ti n g r o u ti n e s t o print all th e li n e s o f
th e f i l e , te rm in a te th e m w ith ‘ E N D ’ as a marker an d fi n a l l y c l o s e th e
f i l e .
5 2
Page 55
C H E C K S U M T A B L E
C h a p t e r 3 M a s t e r c o d e File E d i t o r
2 3 7 0 0
2 3 7 0 5
2 3 7 1 6
2 3 7 5 0
M O D U L E 3 . 1 5
1 2 3
1 6 6
8 9
1 1 6
2 3 7 0 1
1 7 7
2 3 7 1 0 1 7 9
2 3 7 2 0 1 3 8
2 3 7 6 0 5 4
2-3702 1 2 3
2 3 7 1 5
2 3 7 3 0
T V ?
2 0 0
2 3 7 8 0 1 4 2
2 3 6 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 6 0 1 R E M L O A D F I L E F R O M D E V I C E
2 3 6 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 3 6 1 0 G O S U B 1 1 2 5 0
2 3 6 1 5 I F D E V = 8 T H E N I N * = I N * + H , S , R H
2 3 6 3 0 0 P E N 2 , D E V , 0 , 1 N $
2 3 6 3 5 I N P U T # 2 , I N * : I F S T T H E N G O T O 2 3
6 5 0
2 3 6 4 0 I F I N * < > ”E N D " T H E N G O S U B 2 4 0 0 0 : G
O S U B 2 3 9 0 0 : G O S U B 2 3 1 0 0 : G O T O 2 3 6 3 5
2 3 6 5 0 C L O S E 2
2 3 6 6 0 R E T U R N
T h e mir ror i m a g e o f th e p r e v i o u s m o d u l e . N o t e t h a t w h e n l o a d i n g th e
f i l e b a ck f r o m t a p e o r d is c , al l t h e n o r m a l in p u t ro u ti n e s h a v e t o b e u s e d .
T h i s is b e c a u s e th e l i n e s w er e l i s t e d in fu l l w i th t h ei r li n e n u m b e r s , n o t th e
t w o b y te f o r m o f th e l i n e n um be rs th a t is n o r m a l l y st o r ed in F I $ . T h e c a s s
e t t e s a v i n g / l o a d i n g s y s te m h a s d i f f i c u l t y in s a v i n g n o n - p r in t a b l e A S C I I
charac ter s and s i m p ly sa v in g th e c o n t e n t s o f F IS w o u l d res ult i n t h e c o r ru p
t i o n o f s o m e o f th e l i n e n um be rs o n r e lo a d i n g .
C H E C K S U M T A B L E
2 3 6 0 0 1 2 3 2 3 6 0 1 5 1
2 3 6 1 0 1 6 6 2 3 6 1 5 1 7 4
2 3 6 3 5 5 7 2 3 6 4 0 2 1 5
2 3 6 0 2 1 2 3
2 3 6 3 0 3 1
2 3 6 5 0 2 4 2
2 3 6 6 0 1 4 2
M O D U L E 3 . 1 6
2 5 5 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 5 5 0 1 R E M C H A N G E D E V I C E N U M B E R
2 5 5 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 5 5 1 0 P R I N T S P C (19 ) D E V
2 5 5 2 0 I N P U T " C C U 3 N E W D E V I C E N U M B E R : D E V
2 5 5 3 0 R E T U R N
5 3
Page 56
M a chi ne C o d e M a s te r
T h e p u r p o s e o f th i s m o d u l e is t o a l l o w o u t p u t t o b e m a d e t o c a s s e t t e , d isc
or printer. N o t e th a t tr yi n g t o o u t p u t t o , or i n p u t f r o m , a d e v i c e w h i c h is
n o t pr es e nt , or t o i n p u t f r o m a d e v i c e w h i c h is n o t c a p a b le o f g i v i n g an
i n p u t (su ch as th e pr inter ) m a y res ult in th e p ro g ra m s t o p p i n g . D a t a wi ll
n o t be l o s t p r o v i d e d t h a t y o u sta rt th e p r o g r a m w i th G O T O 1000 0 rath er
th a n R U N . B e f o r e d o i n g s o it w o u l d b e w i s e t o en su re th a t f i l e 2 is c l o s e d b y
en te rin g P R I N T ^ 2 : C L O S E 2 i f y o u w er e s a v i n g w h e n th e p ro g ra m
s t o p p e d or s i m p ly C L O S E 2 i f y o u we re l o a d i n g . T h i s w i l l a v o i d th e
p o s s i b i l i t y o f "FI LE A L R E A D Y O P E N " err or s b e i n g g i v e n .
C H E C K S U M T A B L E
2 5 5 m 1 2 3 2 5 5 0 1 1 4 2 5 5 0 2 1 2 3
2 5 5 1 0 2 4 1 2 5 5 2 0 1 1 3 2 5 5 3 0 1 4 2
S E C T I O N 5 : R e n u m b e r i n g
M O D U L E 3 . 1 7
2 4 7 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 7 0 1 R E M R E N U M B E R F I L E I N S T E P S O F 1 0
2 4 7 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 4 7 1 0 L N = 1 0 : E R R « F A L S E
2 4 7 2 0 I F L E N ( P T R * ) < 1 T H E N 2 4 7 8 0
2 4 7 3 0 F O R T = 1 T O L E N t P T R * )
2 4 7 3 5 7 1 = A S C ( M I D * ( P T R * , T , 1) >
2 4 7 4 0 F I $ ( T 1 > = C H R $ ( L N - I N T ( L N / 2 5 6 ) * 2 5 6 )
+ C H R * ( L N / 2 5 6 ) + M I D * ( F I * < T 1 ) , 3 )
2 4 7 5 0 L N = L N + 1 0 : N E X T
2 4 7 8 0 R E T U R N
H a r d l y w o r t h a s e c t i o n in its o w n ri g h t, b u t t h e m o d u l e d o e s p e r f o r m
i n d e p e n d e n t l y o f ev e ry th i n g e l s e y o u h a v e e n te re d s o fa r . Its p u r p o s e is t o
renu mb er y o u r fi l e i n st e p s o f 1 0 . T h i s is d o n e b y s tr ip p in g e a c h en tr y in th e
f i l e o f its fir s t t w o ch ara ct er s a n d t h e n re cre a tin g t h e m f r o m L N , w h i c h i s
in c re m en te d b y 1 0 f o r ea c h l i n e .
C H E C K S U M T A B L E
2 4 7 0 0 1 2 3 2 4 7 0 1 2 3 5 2 4 7 0 2 1 2 3
2 4 7 1 0 1 7 3 2 4 7 2 0 1 6 9 2 4 7 3 0 4 2
2 4 7 3 5 1 6 0 2 4 7 4 0 9 4 2 4 7 5 0 4 5
2 4 7 8 0 1 4 2
5 4
Page 57
C h a p te r 3 M a s t e r c o d e File E d i t o r
M O D U L E 3 . 1 8
2 5 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 5 0 0 1 R E M A D D T O F I L E F R O M M E M O R Y
2 5 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 5 0 1 0 6 0 S U B 1 2 0 5 0 : G O S U B 1 1 2 0 0 : G O S U B
2 4 2 0 0
2 5 0 5 0 F O R X Y ■ A D T O E A S T E P 15
2 5 0 6 0 I N * * " B Y T " : L N » S L s S L = S L +
5
2 5 0 7 0 F O R X Z ■ 0 T O 1 4 : 0 2 * - " "
2 5 0 8 0 G O S U B 1 1 1 0 0 : I N * » I N * + " * " + H *
2 5 1 0 0 I F X Z < 1 4 A N D A D O E A T H E N I N * = I N *
+ • ' . " : N E X T X Z
2 5 1 1 0 G O S U B 2 3 1 0 0 . ■ N E X T X Y : R E T U R N
W e c o n f e s s t h a t t h i s m o d u l e i s a b i t o f a n a f t e r t h o u g h t , b u t a n ic e o n e f o r
all t h a t . Its r e le v a n c e re a ll y w o n ’ t b e c o m e cl ea r u n t il y o u h a v e ent er ed th e
A s s e m b l e r b u t w h a t it d o e s is a l l o w y o u t o s p e c i f y an ar e a o f m e m o r y and
th e n p la c e it i n t o an a s s e m b l y l a n g u a g e p r o g r a m H i e i n t h e f o r m o f ‘ b y t e
d ir e c t i v e s ’ — t h e c o n t e n t s o f e a c h m e m o r y l o c a t i o n are s p e c i f i e d i n th e
a s s em b le r f i l e . N o a u t o m a t i c a d j u s t m e n t is m a d e t o i n s t r u c t i o n s w h i c h
a c c e s s ad d r es s es in t h e a re a f r o m w h i c h t h e c o d e w a s o r i g i n a ll y l i f t e d . Su ch
i n s t r u c ti o n s w i l l st i l l re fer t o t h e o r i g i n a l are a o f m e m o r y .
C H E C K S U M T A B L E
2 5 0 0 0 1 2 3 2 5 0 0 1 2 0 0 2 5 0 0 2
2 5 0 1 0 2 2 3 2 5 0 5 0 1 3 0
2 5 0 7 0 1 9
2 5 0 8 0
1 7 0 2 5 1 0 0 1 3
2 5 0 6 0 7 8
1 2 3
2 5 1 1 0 1 2 0
S u m m a r y
N o w t h a t y o u h a v e en te r e d t h e F i l e E d i t o r it w o u l d b e w i s e t o p la y w ith it
f o r a w h i le b e f o r e g o i n g o n t o en te r t h e A s s e m b l e r . T h i s wi l l h el p t o a v o i d
th e u p s e t o f en te r i n g a l o n g a s s e m b l y l a n g u a g e f i l e a n d t h e n h a v in g it
s p o il e d b e c a u s e y o u m i s u s e t h e F i l e E d i t o r . Y o u c o u l d , i f y o u w i s h , en te r
o n e o r t w o o f t h e a s s e m b l y l a n g u a g e p r o g r a m s t o b e f o u n d la te r in t h i s
b o o k , s a v i n g t h e m t o d is c o r t a p e a n d t h e n r e lo a d i n g t h e m t o c h e c k th a t
y o u h a v e th e p r o c e d u r e o f f pa t.
5 5
Page 58
Page 59
C H A P T E R 4
M a s t e r c o d e A s s e m b l e r
H a v i n g en te re d th e F i l e E d i t o r , w e c a n n o w b e g i n o n th e p r o c e s s o f
en te ri ng th e m o s t i m p o r ta n t and c o m p l e x par t o f th e M a s t e r c o d e p ro g ra m ,
th e A s s e m b l e r . It s p u r p o s e is t o a l l o w y o u t o ente r p ro g ra m s in a s s e m b l y
l a n g u a g e , t o g e t h e r w i th a v a r i e t y o f fe a tu re s w h i c h m a k e su ch p r o g r a m
m in g ea s ie r , an d th e n t o se e th e m a u t o m a t i c a l l y tr a n s la te d i n t o a m a c h i n e
c o d e p ro gr am . T h e p rice th a t h a s b e e n p a id f o r th e fl e x i b i l i t y an d p o w er o f
th is part o f th e p ro g ra m is th a t it is i m m e n s e l y c o m p l e x . En t er in g it w il l be a
l o n g j o b f o r y o u , an d n o d o u b t th er e wi ll b e m a n y e rro rs a l o n g th e w a y ,
her e y o u m u st rely o n th e C h e c k s u m T a b l e s t o g u id e y o u . A t th e en d o f th e
p ro ce s s y o u w il l h a v e th e s a m e p ro g ra m t h a t w e u s ed t o d e v e l o p all ou r
m a c h i n e c o d e r o u ti n e s f o r th i s b o o k . T h e pr o gr am w o r k s a n d th a t is s u f f i
c ie n t j u s t i f i c a t i o n f o r th e e f f o r t t h a t it wi ll i n v o l v e .
S E C T I O N 1 : I n i t i a l i s a t i o n
M O D U L E 4.1
1 9 0 4 6 T A * ( 2 ) ■ = T A $ ( 2 ) + " B Y 7 / W R D D B Y ' E N D 0 R 6 P R
T S Y M " !
1 9 0 4 7 T * = 1 1 6 1 2 1 0 6 9 0 B 0 F 0 2 4 3 0 D 0 1 0 0 0 5 0 7
0 1 8 D 8 5 8 "
1 9 0 4 8 T $ = T * + " B 8 C D E C C C C E C A 8 8 4 D E E E 8 C 8 4 C 2
0 A D A E A C "
1 9 0 4 9 T * = T - $ + " 4 A E A 0 D 4 8 0 8 6 8 2 8 2 A 6 A 4 0 6 0 E D 3
8 F 8 7 8 8 D "
1 9 0 5 0 T $ = T $ + " 8 E 8 C A A A 6 B A 8 A 9 A 9 8 "
1 9 0 5 1 GOS LI B 1 2 2 0 0 : T A $ ( 3 > = T l *
1 9 0 5 2 T * = "F F 1 1 F F F F F F 0 9 0 A F F F F 1 D 0 E F F F
F 0 5 1 E F F "
1 9 0 5 3 T $ = T $ ■ + " F F 15 F F F F F F F F F F F F F F 0 1F F F F F
F 1 9 1 6 F F "
1 9 0 5 4 T - t = I ■ $ : + " F F 2 D F F F F 2 C 2 9 3 E F F F F 3 D 2 E F F F
F 2 5 2 6 F F "
1 9 0 5 5 T $ ■ = T * + ”F F 3 5 F F F F F F F F F F F F F F 3 1 F F F F F
F 3 9 3 6 F F "
1 9 0 5 6 T $ = " F F 5 1F F F F F F 4 9 5 E F F F F 5 D 4 E F F 6
5 7
Page 60
M a ch in e C o d e M a s te r
C 4 5 4 6 F F "
1 9 0 5 7 T T = T T + " F F 5 5 F F F F F F F R F F F F F F 4 - l l F F F F F
F 5 9 5 6 F F "
1 9 0 5 8 T T = T 7 + " F r F 6 D F F F F F F 6 9 , 7 E F F F F 7 D 6 E F F F
F 6 5 6 6 F F "
1 9 0 5 9 B O S U B 1 2 2 0 0 : T A T - ( 4 ) > = T I T
1 9 0 6 0 T T = " F F 7 5 F F F F - F F F F F F F F ' F F J T F F F F F
F 7 9 7 6 F F "
1 9 0 6 1 T T = T 7 + " F F 9 1F F F F 9 4 9 D 9 6 F F F F F F F F F F 8
4 8 5 8 6 F F "
1 9 0 6 2 T T = T $ ■ + " F F 9 5 F F F F F F F F F F F F F F 8 1 F F F F F
F 9 9 F F F F "
1 9 0 6 3 T * = T t + " B C B 1 B E F F A 0 A 9 A 2 F F F F B D , F F A F A
4 A 5 A 6 F F "
1 9 0 6 4 T *
4 B 9 B 6 F F "
1 9 0 6 5 T $ ■
= T $ + " F F B 5 F F F F F F F F F F F F F F A 1F F F F B
= T t + " F F D l F F F F C 0 C 9 - D E F F F F D e | f F F F C
4 C 5 C 6 F F "
1 9 0 6 6 T $
_ T $ + „ f p d s f f F F F F F F F F F F F F C 1 F F F F F
F D 9 D 6 F F "
1 9 0 6 7 B O S U B 1 2 2 0 0 : T A * ( 4 ) = T A * ( 4 ) + T 1 $
1 9 0 6 8 T # = ■ ' F F F 1 F F F F E 0 E 9 F E F F F F F D F F F F E
4 E 5 E 6 F F "
1 9 0 6 9 T : $ = 7 ' $ + " F F F 5 F F F F F F F F F F F F F F E 1 F F F F F
F F 9 F 6
1 9 0 7 0
1 9 0 8 0
1 9 1 0 1
1 9 1 0 3
I I
B O S U B 1 2 2 0 0 : T A T ( 4 ) = T A T ( 4 ) + 7 1 7
S M - 5 0 : S E = : 0 s D I M S T A B L E T ( S M )
D I M E R R T ( 1 8 )
E R R T ( l ) = “ S I N G L E B Y T E O U T O F R A N G
E "
1 9 1 0 4
E R R T ( 2 ) = " D O U B L E B Y T E O U T O F R A N G
E "
1 9 1 0 5
E "
1 9 1 0 6
1 9 1 0 7
1 9 1 0 8
I I
1 9 1 0 9
1 9 1 1 0
1 9 1 1 2
1 9 1 1 3
1 9 1 1 4
E R R T < 3 ) = " I N V A L I D O P R A N D O R O F : ' C OD
E R R T ( 4 ) = " I N V A L I D O P E R A T O R "
E R R T ( 5 ) = " I N D E X I S N O T X O R Y "
E R R T ( 6 ) = " L A B E L N O T A L P H A - N U M E R I C
E R R T ( 7 ) = " I N C O R R E C T N U M B E R B A S E "
E R R T ( 8 ) = “ L A B E L D E F I N E D T W I C E "
E R R T (1 0) = " B R A N C H O U T O F R A N G E "
E R R T (11) = " U N D E F I N E D L A B E L "
E R R T (12 ) = " O N L Y S I N G L E C H R . E X P E C
T E D "
5 8
Page 61
C h a p t e r 4 M a s t e r c o d e A s s e m b l e r
1 9 1 1 6 E R R * (1 4) =
1 9 . 1 . 1 7 E R R * (1 5) =
1 9 1 2 0 E R R * ( 1 . 8 ) =
A I L B L . E W I T H T H I S
1 9 9 8 0 D I M F I * ( 2 5
" O U T O F S Y M B O L . S P A C E
" D I V I S I O N B Y Z E R O "
" A D D R E S S I N G M O D E N O T
O P C O D E "
1 ) : GO SL JB 2 4 3 0 0
A V
1 , 9 9 9 0 R E T U R N
R E A D Y .
I f y o u h a ve b e e n ta k i n g n o t e o f w h a t y o u h a v e en te red s o far y o u wi ll
i m m e d i a te l y re ali se th a t w h a t y o u ar e a b o u t t o en ter he re is n o t a m o d u l e
th a t sta n d s a l o n e in its o w n righ t b u t an a d d i t i o n t o a n e x i s t i n g m o d u l e ,
n a m e l y th e i n i t i a l i s a t i o n m o d u l e f o r th e ta b l e s o f o p c o d e s an d o p er a n d
ty p es u p o n w h i c h th e D i s a s s e m b l e r w o r k s . In th e c a s e o f th e A s s e m b l e r th e
s am e ta b l es wil l be u s e d , b u t in th e re verse d ir e c t i o n . I n s te a d o f f i n d i n g a
v a lu e in t h e m e m o r y an d t h e n l o o k i n g up an a p p r o p r ia t e o p c o d e m n e m o
nic and ad d r es s in g m o d e , th e a s s e m b l e r w il l s ca n th e f i l e s en te re d th r o u g h
th e F i l e E d i to r an d try t o c o n s t r u c t th e m a c h i n e - c o d e e q u iv a l e n t o f ea c h
l in e — either th a t or rejec t th e l i n e a s an i n v a l i d in s t r u c t i o n .
I f y o u th in k a b o u t i t, th is requ ires s o m e m o r e i n f o r m a t i o n f o r , i n s t ea d
o f b ei ng ab le t o read a v a l u e an d th e n c h o o s e a f o r m a t b a s e d u p o n th a t
o p c o d e , th e A s s e m b l e r m u s t , o n fi n d i n g an i n s t r u c t i o n li k e ‘ l o a d ’ at th e
b eg i n n i n g o f a li n e li k e ‘ L D A S A A A A . X ’ , b e a b le t o s c a n t h r o u g h all th e
p o s s i b l e fo r m a ts f o r a ‘ l o a d ’ in s t r u c t i o n t o s ee w h et h er or n o t th e p res en t
in s t r u c ti o n is a p e r m is s i b l e o n e . In ord er t o a c h i e v e th i s , t w o m o r e ta b le s
ar e a d de d t o t h o s e al re ad y s to r ed in th e p r o g r a m . B e tw e e n 1 9 047 an d 19 050
is s to r ed a t a b l e o f t w o - c h a r a c t e r h e x n um be rs c o r r e s p o n d i n g t o ea c h o f th e
p o s s i b l e o p c o d e s s to r ed in T A $ ( 2 ) f o r th e D i s a s s e m b l e r . T h e s e s h o w , f o r
ea c h o p c o d e , th e fir st o p e r a n d t y p e w h i c h m a y b e u s e d . L in e s 19 0 52 -1 90 6 7
c o n s i s t o f furthe r o p e r a n d ty p e s f o r ea c h p art icu la r g r o u p o f o p c o d e s .
Later in th e p ro g ra m w e s h a ll s e e h o w ea c h p o s s i b l e o p e r a n d t y p e i s
c o m p a r e d w ith w h a t is a c t u a l l y in th e a s s em b l er i n s t r u c t i o n c o n t a i n e d
w i th i n a l in e in FIS. F o r th e m o m e n t y o u w il l d o w e ll t o s i m p ly u nd er s ta n d
th a t o n d e te c ti n g an i n s t r u c t i o n b e g i n n i n g w i th A D C (t h e fir s t th re e c h a
r ac te r o p c o d e i n T A $ ( 2 ) , th e A s s e m b l e r wi ll g o t o T A $ ( 3 ) . I t wi ll th e n
d is c o v e r th a t thi s m a y p o s s i b l y b e an in s t r u c t i o n i n v o l v i n g o p c o d e 6 1 (h ex )
and w il l e x a m in e th e f o r m a t o f th e a s s em b l er i n s t r u c t i o n t o see w h et h er it
fi ts th e fo r m a t requ ired b y o p c o d e 6 1 h e x (e g A D C ( $ 5 0 , X ) . I f th e f o r m a t
o f th e l in e b e i n g e n te re d d o e s n o t c o n f o r m t o th a t n e e d e d b y o p c o d e 6 1 h e x ,
th e n th e v a l u e o f 6 1 h e x (97 d e c i m a l ) , w i l l b e u s e d t o f i n d th e n ex t p o s s i b l e
o p c o d e in T A $ ( 4 ) . T h i s w il l b e f o u n d in th e 9 8 th ch a ra c te r pair o f T A $ ( 4 )
(n u m b er in g a l w a y s star ts f r o m z e r o ) a n d th e o p c o d e th er e is 6 D s i g n i f y i n g
a n o th e r i n s t r u c ti o n i n v o l v i n g A D C , b u t th i s t i m e t a k i n g a fo r m a t su ch a s
A D C $ A A A . T h e v a l u e 6 D is t h e n u s ed t o f i n d th e n ex t o p c o d e in th e ta b le
w h ic h w o u l d p r o d u c e a n i n s t r u c t i o n b e g i n n i n g w i th A D C .
5 9
Page 62
M a chi ne C o d e M a s te r
In the c a s e o f A D C th er e ar e ei g h t p o s s i b l e o p c o d e s a n d i f a ft er e x a m i n
ing each o n e a g a i n s t th e a c t u a l fo r m a t o f th e in s t r u c t i o n in th e li n e in F IS
n o n e o f t h em f i t , th e la s t p o s s i b l e o p c o d e wi ll c o n t a i n th e v a l u e F F , i n d i
c a t in g th at th e en d o f th e c h a i n o f p o s s i b l e A D C i n s t r u c ti o n s ha s b ee n
reach ed and th a t n o p e r m is s i b l e o p c o d e c o n f o r m s t o w h a t is a c t u a l l y in th e
l in e . I f y o u care t o w o r k t h r o u g h th e t a b l e s w ith a n y th re e lett er o p c o d e
ty p e y o u care t o c h o o s e , firs t o f all fi n d i n g its p o s i t i o n in T A $ , th e n f i n d i n g
th e st ar t o f th e c o r r e s p o n d i n g c h a i n o f o p c o d e v a l u e s in T A $ ( 3 ) an d
f o l l o w i n g th e c h a i n t h r o u g h T A $ ( 4 ) y o u s h o u l d q u i c k l y b e a b le t o se e w h at
is h a p p e n i n g . T h e o n e r eal a d d i t i o n t o th e ta b l e s se t u p al re a d y b y th e
d is as s e m b le r is th a t m a d e b y li n e 19 046 . T h i s a p p a r e n tl y ad ds s ev e n ne w
o p c o d e ty p es t o th e li s t w h i c h th e D i s a s s e m b l e r w o r k e d u p o n . T h e s e a re th e
a ss em b ler d ir e c t i v e s , s e v e n i n s t r u c t i o n s w h i c h a re n o t a c t u a l l y a s s e m b l y
l a n g u a g e in s t r u c ti o n s b u t r athe r i n s t r u c t i o n s t o th e A s s e m b l e r t o b e h a v e in
a certa in w a y w h i le it is p r o c e s s in g o n th e a s s e m b l y l a n g u a g e p ro g ra m . T h e
se ve n d ir ec t iv es , B Y T , W R D , D B Y , E N D , O R G , P R T an d S Y M wi ll be
e xp la in e d f u l l y later in th e p ro g ra m .
F ro m 19100 t o 19120 y o u w il l fi n d th e v a r i o u s e rro r m e s s a g e s th e A s s e m
bler is ca p a b le o f g e n e r a ti n g w h e n it c o m e s a cr o s s in v a l i d i n s t r u c ti o n s or
o m i s s i o n s fr o m th e p r o g r a m . T h e s e t o o wi ll be e x p l a i n e d m o r e f u l l y in d ue
c o u rs e .
C H E C K S U M T A B L E
1 9 0 4 6 1 9 3
1 9 0 4 9
1 9 0 5 2
* - > c ;
u L u J
2 5 1 1 9 0 5 3
1 9 0 5 5 1
1 9 0 5 8 2 3 8
1 9 0 6 1 1 9 8
1 9 0 6 4 3 8
1 9 0 6 7 2 2 1
1 9 0 7 0
2 2 1
1 9 1 0 3 5 3
1 9 1 0 6
1 9 1 0 9
19 1 1 3
9 1
1 5 2
8
1 9 1 1 7 1 2 2
1 9 9 9 0
M O D U L E 4 . 2
1 4 2
1 9 0 4 7
1 9 0 5 0
17 1
1 7 0
2 4 8
1 9 0 5 6
1 9 0 5 9
1 8 7
2 4 5
1 9 0 6 2 5 3
1 9 0 6 5
1 9 0 6 8
13
91
1 9 0 8 0 2 7
1 9 1 0 4 4 7
1 9 1 0 7
1 9 1 1 0
1 9 1 1 4
1 9 1 2 0
1 9 2
2 1 5
1 8 4
3 7
1 9 0 4 8 1 8 9
1 9 0 5 1 2 4 4
1 9 0 5 4 1 8 7
1 9 0 5 7 8
1 9 0 6 0
1 9 0 6 3
7 9
2 1 6
1 9 0 6 6 6 8
1 9 0 6 9
1 9 1 0 1
1 9 1 0 5
1 9 2
1 0 9
7 7
1 9 1 0 8 1
1 9 1 1 2 2 5 3
1 9 1 1 6 4 0
1 9 9 8 0 1 0 1
2 0 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 0 0 0 1 R E M G E N E R A T E A S S E M B L Y L I S T I N G
2 0 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
6 0
Page 63
C h a p te r 4 M a s t e r c o d e A s s e m b l e r
2 0 0 0 5 S E = 0 : F M A X = L E N ( P T R T ) : S Y = F
A L . S E
2 0 0 1 0 I N P U T " E R R O R O N L Y L I S T I N G ( Y / N )
: “ ; T T
2 0 0 2 0 E O = L E F T T ( T T , 1 > = " Y "
2 0 0 2 5 I N P U T " A S S E M B L E T O M E M O R Y ( Y / N )
: " ; T T
2 0 0 2 9 A M = L E F T T ( T T , 1 ) = " Y "
2 0 0 3 0 A D ■ - 0 : R E M S E T S T A R T A D D R E S S
2 0 0 4 0 F O R Q = 1 T O F M A X
2 0 0 5 0 I N T = F I L E T ( A S C ( M I D T ( F ' T R T , Q , 1 ) ) ) :
O T = " "
2 0 0 6 0 G O S U B 2 6 4 0 0
2 0 0 7 0 I F E X I T T H E N G M F M A X + 1
2 0 0 8 0 N E X T Q
2 0 0 8 5 T = F R E ( X )
2 0 0 9 0 A D = 0 : E C = 0 : P R I N T " A D D . D A T
A S O U R C E C O D E "
2 0 1 0 0 F O R Q = 1 T O F M A X
2 0 1 1 0 I N T = F I L E T ( A S C ( M l ' D T (F'TRT ,Q , 1 > ) ) :
O T = " "
2 0 1 2 0 Q 1 = A D
2 0 1 3 0 G O S U B 2 7 6 0 0
2 0 1 4 0 I F E R R T H E N 2 0 2 5 0
2 0 1 4 5 IF E O T H E N 2 0 2 2 2
2 0 1 5 0 H = Q 1 : G O S U B 1 1 0 0 0
2 0 1 6 0 Q T = H T
2 0 1 8 0 Q 2 = 3 : I F L E N ( O T ) < Q 2 T H E N Q 2 = L
E N ( O T )
2 0 1 8 5 Q 1 T = " " : I F O T = " " T H E N 2 0 2 2 1
2 0 1 9 0 F O R Q 3 = 1 T O Q 2
2 0 2 0 0 H = A S C ( M I D T ( O T , Q 3 ,1 ) ) : G O S U B 1 1 0
0 0
2 0 2 1 0 IF L E N ( H T ) = 1 T H E N H T = " 0 " + H T
2 0 2 2 0 Q 1 T = Q 1 T + H T : N E X T Q 3
2 0 2 2 1 P R I N T Q T S P C ( 6 - L . . E N (QT ) > Q 1 T S P C ( 8 -
L E N ( Q I T ) ) ; : G O S U B 2 8 1 0 0
2 0 2 2 2 I F N O T A M O R O T * " " T H E N 2 0 2 5 0
2 0 2 2 5 F O R X = 1 T O L E N ( O T ) : P O K E Q l + X - 1
, A S C ( M I D T ( O T , X ,1 )) : N E X T
2 0 2 5 0 I F E X I T T H E N Q = F M A X + 1 : R E M L E A V
E L O O P
2 0 2 6 0 N E X T Q
2 0 2 7 0 P R I N T : P R I N T " T O T A L E R R O R S I N F I
6 1
Page 64
M ac hin e C o d e M a s te r
L E
- - -
" E C : P R I N T
2 0 2 8 0 I F S Y T H E N G G S U B 2 6 9 0 0
2 0 2 9 0 IF P E E K ( 1 5 2 ) < > 0 T H E M P R I N T # 2 : C
L 0 S E 2 : G O T O 2 0 3 0 0
2 0 2 9 5 G E T T T : I F T $ = " " T H E N 2 0 2 9 5
2 0 3 0 0 R E T U R N
R E A D Y .
E X AM P L E ER R OR S : E r r o r on ly listin g
A D D . D A T A S O U R C E C O D E
5 0 L B L 0 0 0 L D Q * A 0 0 0
L A B E L D E F I N E D T W I C E E R R O R
6 0 J S R ( $ = 3 0 0 )
A D D R E S S I N G M O D E N O T A V A I L B L E W I T H T H I
S O P C O D E E R R O R
7 0 L . D A # L B L 0 0 0 / H
D I V I S I O N B Y Z E R O E R R O R
8 0 L D X # L B L 0 0 0 - L B L 0 0 0 / 2 5 6
@ 2 5 6
I N V A L I D O P E R A T O R E R R O R
1 4 0
B C C L B L 0 0 0
B R A N C H O U T O F R A N G E
1 5 0 J M P L. BL 00 1
U N D E F I N E D L A B E L E R R O R
1 5 0 J M P LB L. 00 1
U N D E F I N E D L A B E L E R R O R
1 6 0 L B L 0 0 0 R T S
L A B E L D E F I N E D T W I C E E R R O R
T O T A L E R R O R S I N F I L E
H 0
L B L 0 0 0 C 8 0 0
T O T A L N U M B E R O F S Y M B O L S
6 2
E R R O R
- - -
0
8
- - -
2
Page 65
C h a p te r 4 M a s te r c o d e A s s e m b l e r
EX A M P L E E RR O R S : F u U li sti n g
A D D . D A T A S O U R C E C O D E
0 1 0 P R T
0 2 0 S Y M
0 3 0 O R G $ C 8 0 0
C 8 0 0 4 0 H = 0
5 0 L B L 0 0 0 L D Q $ A 0 0 0
L A B E L D E F I N E D T W I C E E R R O R
C 8 0 0 5 0 L B L 0 0 0 L D Q $ A 0 0 0
6 0 J S R ( $ 3 0 0 )
A D D R E S S I N G M O D E N O T A V A I L B L E W I T H T H I
S O P C O D E E R R O R
7 0 L D A # L B L 0 0 0 / H
D I V I S I O N B Y Z E R O E R R O R
8 0 L D X # L B L 0 0 0 - L B L 0 0 0 / 2 5 6
@ 2 5 6
I N V A L I D O P E R A T O R E R R O R
C 8 0 4
C 8 0 6 8 6 4 2
C 8 0 8 6 0
C 8 0 9
C A 0 0 1 8
8 5 4 3 9 0 S T A S < 1 0 3
1 0 0
S T X S c 1 0 2
1 1 0
R T S
1 2 0
O R G $ C A 0 0
1 3 0 C L C
1 4 0 B C C L B L 0 0 0
B R A N C H O U T O F R A N G E E R R O R
1 5 0 J M P L B L 0 0 1
U N D E F I N E D L A B E L E R R O R
1 5 0 J M P L B L 0 0 1
U N D E F I N E D L A B E L E R R O R
1 6 0 L B L 0 0 0 R T S
L A B E L D E F I N E D T W I C E E R R O R
C A 0 6 1 6 0 L B L 0 0 0 R T S
T O T A L E R R O R S I N F I L E
- - -
8
H 0
L B L 0 0 0 C 8 0 0
T O T A L N U M B E R O F S Y M B O L S
- - -
2
6 3
Page 66
M ac hin e C o d e M a s te r
In p re vi ou s s e c t i o n s o f th e o ve r a ll p ro g ra m w e h a v e a d o p t e d th e
a p pr oa ch o f firs t ex p l a i n i n g all th e m o d u l e s w h i c h ar e n ec e s s a ry t o m a k e a
c o n t r o l m o d u le w o r k b e f o r e e n te ri ng th e c o n t r o l m o d u l e i t s e l f . T o d o tha t
in th e ca s e o f th e A s s e m b l e r w o u l d res ult in s co r es o f p a g e s o f e x p l a n a t i o n s
b e f o r e an y pic tu re c o u l d b e b u i l t up o f w h a t th e p ro g ra m is s et ti n g o u t t o
d o . T h e sheer c o m p l e x i t y o f th e A s s e m b l e r d ic ta te s t h a t w e a d o p t a ‘ t o p
d o w n ’ a p p r o a c h and a tt e m p t t o w or k o u r w a y f r o m a s i m p le e x p l a n a t i o n
o f th e w o r k i n g o f th e p ro gr a m t o a d e t a i l e d e x a m i n a t i o n o f th e f u l l li s t i n g ,
fi l l in g in d et a il s all th e t i m e . It is f o r th a t re a s o n th a t w e b e g i n o u r c o m m e n
tary o n th e m ai n part o f th e A s s e m b l e r w i th th i s m a i n c o n t r o l m o d u l e . T h e
m o d u l e o n its o w n is t o t a l l y h e l p l e s s , it d o e s a l m o s t n o w o r k i t s e l f bu t
s im p ly a l l o c a t e s w or k b e t w e e n v a r i o u s o th e r s e c t i o n s o f th e p ro g ra m . N e v
er th el es s , c o m m e n t i n g o n it at th i s ea rly s ta g e wi ll h e l p t o g i v e us a m u ch
n ee d ed o v er v ie w o f th e A s s e m b l e r ’ s f u n c t i o n i n g .
C o m m e n ta r y
2 0 0 1 0- 20 02 9: T h e A s s e m b l e r is c a p a b le o f c o m p i l i n g a m a c h i n e c o d e p r o
gram in f o u r d i f f e r e n t w a y s . It c a n p r o v i d e a fu l l l i s t i n g o f th e a s s e m b l y
l a n g u a g e i n s t r u c t i o n s , t o g e t h e r w i th a n o t i f i c a t i o n o f a n y e rro rs p re s e nt or
it ca n sk ip th e l i s t i n g an d p r o v i d e o n l y th e errors. A n e x a m p l e o f b o t h o f
t h e s e w as p r o v i d e d at th e e n d o f th i s m o d u l e . It c a n a l s o b e d ir ec te d t o p la c e
th e m a c h in e c o d e p ro gr a m re s u lt in g f r o m th e a s s e m b l y i n t o m e m o r y o r it
ca n be t o l d t o r un t h r o u g h t h e p ro gr a m b u t l e a v e t h e m e m o r y u n t o u c h e d .
I f , f o r i n s t a n c e , y o u wi s h t o p la c e a m a c h i n e c o d e r o u t i n e in a s p e c i f i c area
o f m e m o r y b e tw e e n a s p e c i f i e d sta rt p o i n t an d a s p e c i f i e d f i n i s h p o i n t ,
w i t h o u t co r ru p t in g an y o f th e m e m o r y o u t s i d e t h o s e p o i n t s , y o u w o u l d d o
w e ll t o as k fi rst f o r a fu l l l i s t i n g w i t h o u t th e p ro g ra m b e i n g p la c e d i n t o
m e m o r y . T h i s wi ll s h o w e x a c t l y wh er e th e a s s e m b l e d m a c h i n e c o d e w o u l d
h a v e b ee n p la c e d in m e m o r y b e f o r e a n y t h in g i r r e v o c a b l e is d o n e .
2 0 0 3 0- 20 08 5: B e f o r e s ta r tin g w o r k o n th e a s s e m b l y l a n g u a g e p ro g ra m th e
v a ri ab le A D is se t t o z e r o , s i g n i f y i n g th a t th e add re ss at w h i c h th e e v e n t u a l
m a c h i n e c o d e p ro g ra m w i l l start is z e r o . D u r i n g th e c o u r s e o f th e a s s e m b l y
l a n g u a g e p ro gr am th i s start p o i n t wi ll in a l m o s t ev e ry c a s e b e re set t o s o m e
o th er p o i n t in th e m e m o r y u s in g th e a s s e m b l e r d ir ec t iv e ‘ O R G ’ ( o r ig i n ).
T h e as se m b le r w il l n o w w o r k t h r o u g h th e p ro g ra m t w i c e in w h a t a re ca l l ed
‘ p a s s e s ’ . T hi s l o o p ca ll s u p th e s e c t i o n o f th e p ro g ra m w h i c h p e r f o r m s
‘ P a s s 1 ’ . D u ri n g P a s s 1 a n y v a ri a b le s (i n c l u d i n g a sp e c i a l k in d o f v a ri ab le
c a ll ed a ‘ l a b e l ’ , w h i c h d e f i n e s th e co r re c t d e s t i n a t i o n in th e m e m o r y f o r a
j u m p i n s t r u c ti o n ) a r e e x a m in e d an d p la c e d , t o g e t h e r w i th thei r a s s o c i a t e d
v a l u e s , i n t o a t a b l e k n o w n as th e ‘ s y m b o l t a b l e ’ w h i c h w i l l b e u s e d in th e
lat er a s s e m b l y o f th e p ro g ra m .
6 4
Page 67
C h a p te r 4 M a s te r c o d e A s s e m b l e r
E a c h li n e o f t h e a s s e m b l y l a n g u a g e p r o g r a m is o b t a i n e d in INS b e f o r e
P a s s 1 is e x e c u te d u p o n it . O n re tu rn in g f r o m t h e e x e c u t i o n o f P a s s 1
o n an y pa rti cul ar l i n e , a te s t is m a d e o f th e v a r i a b l e E X I T , w h i c h is s et t o
T R U E i f th e a s s em b l er d ir ec t iv e E N D is m e t w ith du r in g th e e x a m i n a t i o n
o f th e p ro gr am . A t th is p o i n t th e a s s e m b l y w il l c e a s e , e v e n i f th e en d o f th e
f i l e in F IS ha s n o t b e e n r e a ch ed . A t th e c o n c l u s i o n o f th e l o o p th e F R E
f u n c t i o n is c a l l e d , th u s e n s u ri n g t h a t g a r b a g e c o l l e c t i o n is d o n e an d th er e
a re n o d an ge rs o f r u n ni n g o u t o f m e m o r y .
2 0 0 9 0- 20 3 00 : T h i s is th e l o o p w h i c h c o n t r o l s t h e s e c o n d p as s t h r o u g h th e
pro gr am t o b e a s s e m b l e d . T h e r o u t i n e f o r th e s e c o n d p as s is c a l l e d u p at
li n e 2 0 1 3 0 . D u r i n g th e s e c o n d p a s s th e p r o g r a m w i l l a c t u a l l y b e a s s e m b l e d ,
w ith eac h v a l i d i n s t r u c ti o n b e i n g tr a n s l a te d i n t o th e b y te s ne c e s s a r y t o rep
resent th e o p c o d e a n d o p e r a n d in m a c h i n e c o d e , i n c l u d i n g t h e tr a n s l a t i o n
o f va ri ab le s i n t o v a l u e s a n d th e a s s i g n m e n t o f v a l u e s t o l a b e l s u s ed f o r
ju m p s . O n re tu rni ng f r o m th e r o u ti n e a te s t is m a d e o f th e v a ri a b l e E R R
w h i ch re cor ds w h et h er an err or h a s b e e n d e t e c t e d . I f s o , n o furt her
p r o c e s s in g is d o n e o n th e i n s t r u c t i o n . I f a n err or o n l y l i s t i n g h a s b ee n s p e
c i f i e d (E O is s et t o T R U E ) th e pri nt r o u t i n e is o m i t t e d . In l i n e s 2 0 1 50 -2 02 2 1
th e i n f o r m a t i o n retu rne d f r o m th e s e c o n d p a s s is p rin te d o u t in a f o r m a t t e d
l a y o u t . I t i n c l u d e s t h e a d dr ess at w h i c h an i n s t r u c t i o n w il l b e p la c e d in th e
m e m o r y i f th e p ro g ra m is in f a c t a s s e m b l e d t o m e m o r y , th e h ex re pr ese n
t a t i o n o f th e 1, 2 o r 3 b y te s th a t wi ll b e p la c e d th er e an d t h e o r i g i n a l a s s e m
b ly l a n g u a g e i n s t r u c t i o n . T h e b y te s o f th e n e c e s s a r y m a c h i n e c o d e in s t ru c
t i o n ar e c o n t a i n e d in O S an d in l i n e s 2 0 2 2 2 - 2 0 2 2 5 . P r o v i d e d th a t A M is
T R U E , t h e s e b y te s are p la c e d i n t o m e m o r y b e g i n n i n g a t th e a p p r o p r ia t e
l o c a t i o n . A te s t is m a d e th a t th e E N D d ir ec t i v e h a s n o t b e e n f o u n d and th e
nex t li n e o f th e p ro gr a m is p ic k e d up i f n o t . F i n a l l y , i f th e as s em b le r
dir ec ti ve S Y M h a s b e e n en c o u n t e r e d du r in g t h e c o u r s e o f th e p ro g ra m th e
va ri ab le SY wi ll be se t t o T R U E a n d th e s y m b o l ta b l e w il l b e pri nt ed o u t a t
th e en d o f th e l i s ti n g o f th e a s s e m b l e d p ro gr a m
C H E C K S U M T A B L E
2 0 0 0 0 1 2 3
2 0 0 0 5
2 0 0 2 5
3
1 9 7 2 0 0 2 9 1 8 9 2 0 0 3 0
2 0 0 4 0 3 7
2 0 0 7 0 2 1 4
- 2 0 0 9 0
2 4 4
2 0 1 2 0 2 4 9
20.14 5 3 0
2 0 1 8 0 1 0 9
2 0 2 0 0
5
2 0 0 0 1 . 1 8 0
20 00 .2 1 2 3
2 0 0 1 0 2 2 7 2 0 0 2 0 1 9 5
1 4 4
2 0 0 5 0 1 3 9 2 0 0 6 0
2 0 0 8 0 2 4 3 2 0 0 8 5
1 6 9
1 6 7
2 0 1 0 0 3 7 2 0 1 . 1 . 0 1 3 9
2 0 1 3 0
2 0 . 1 . 5 0
2 0 1 8 5 4 0 2 0 1 9 0 1 7 5
2 0 2 1 0
1 7 2
2 1 3 2 0 1 6 0 2 1 . 1
' " t ’ ~ i - j
2 0 1 4 0 i 16
2 0 2 2 0 . 2 4 4
6 5
Page 68
M a chi ne C o d e M a s te r
2 0 2 2 1 8 6
2 0 2 5 0 6
2 0 2 8 0 2 3 6
2 0 2 2 2 5 8
2 0 2 6 0 2 4
2 0 2 9 0 6 3
2 0 3 0 0 1 4 2
S E C T I O N 2 : P a s s O n e R o u t i n e s
M O D U L E 4 . 3
2:6 400 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 4 0 1 R E M D O P A S S 1 A S S E M B L Y O N I N *
2 6 4 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 4 0 5 P R I N T " C H O M E 3 C 2 2 * C D 3 L ' 4 0 S P A C E S ]
2 6 4 0 6 P R I N T
2 6 4 0 7 P R I N T " C H O M E 3 C 2 2 * C D 3 " G O S U B 2 8 1
0 0
2 6 4 1 0 P A S S = 1 : E X I T = F A L S E : P T R = 2
2 6 4 2 0 G O S U B 2 8 8 5 0
2 6 4 3 0 I F N O T E R R T H E N 2 6 5 4 0
2 6 4 4 0 I F T = 5 8 A N D L E N < H $ > = 0 T H E N 2 6 4 2 0
2 6 4 5 0 I F T = 5 9 O R T = - - l T H E N R E T U R N
2 6 4 6 0 G O S U B 2 8 7 0 0
2 6 4 8 0 G O S U B 2 8 8 5 0
2 6 4 9 0 I F N O T E R R T H E N 2 6 5 4 0
2 6 5 0 0 I F T = 5 8 A N D L . E N ( H t ) = 0 T H E N 2 6 4 2 0
2 6 5 2 0 R E T U R N
2 6 5 4 0 I F P O > 5 5 T H E N G O S U B 2 6 6 0 0 : G O T O 2
6 5 5 6
2 6 5 5 0 G O S U B 2 6 1 0 0
2 6 5 5 2 G O S U B 2 6 3 0 0 : I F E R R A N D O P > 3 A N D
O P < 7 T H E N O P = O P + 6 : G O T O 2 6 5 5 2
2 6 5 5 5 G O S U B 2 6 5 6 0
2 6 5 5 6 I F L . E N ( I N $ > > P T R A N D N O T E X I T T H E N
2 6 4 2 0
2 6 5 5 7 R E T U R N
H a v i n g e x a m i n e d t h e o v e r a l l c o n t r o l m o d u l e f o r t h e t w o pa s s e s w e n o w
tu rn t o t h e c o n t r o l m o d u l e f o r P a s s 1 . O n c e a g a i n w e sh a ll l e a v e u nt il
later a n e x p l a n a t i o n o f h o w t h e s p e c i f i c t a s k s n e c e s s a r y are a c t u a l l y
a c c o m p li s h e d a n d c o n c e n t r a t e o n a c h i e v i n g a n o v e r v ie w o f w h a t t h e p a s s
a c t u a l l y d o e s .
6 6
Page 69
C h a p te r 4 M a s t e r c o d e A s s e m b l e r
C o m m e n ta r y
2 6 4 0 0- 26 40 7: T h e s e li n e s clear th e b o t t o m t w o li n e s o n th e scre en an d print
ther e th e a s s e m b l y l a n g u a g e i n s t r u c t i o n cu rr en tly b e i n g p r o c e s s e d , p ur ely
t o gu id e th e user a s t o h o w far th e p a s s h a s p r o g r e s s e d .
26410: T h e v a ri ab le P T R i n d i c a t e s w h er e th e e x a m i n a t i o n o f th e cu rrent
li ne wil l b e g i n . I t is set t o 2 in order t o sk ip o v e r th e t w o b y te s c o n t a i n i n g th e
li ne nu m be r.
26420: T h e r o u ti n e w h ic h i d e n t i f i e s th e m n e m o n i c u s in g th e t a b l e in
T A $ ( 2 ) is c a l l e d . T h e r o u ti n e w il l sca n I N $ f r o m th e ch ar ac te r a ft er th a t
in d i c a t e d b y P T R u nt il it fi n d s a ch ar a c te r w h i c h is n o t a lett er or a d ig i t ,
th en retur n.
2 6 43 0- 26 52 0: O n retu rn f r o m th e p r e v i o u s G O S U B , H $ w il l c o n t a i n a
string o f ch ara ct er s w h i c h w er e te r m i n a te d b y a s p a c e , c o l o n or a n y o th er
ch aracter w h ic h is n o t a l ett er o r d ig i t . I f , o n retu rn f r o m th e p r e v i o u s
G O S U B a v a li d m n e m o n i c f o r an o p c o d e ha s n o t b ee n f o u n d an d p la c ed
i n t o H $ , li ne 2 6 4 4 0 te s ts w h et h er a n y ch a ra ct er s at a ll wer e f o u n d b e f o r e
th e c o l o n s ep ar a to r b e tw e e n i n s t r u c t i o n s o n th e s a m e li n e (or a n y o th er
chara ct er w h ic h i s n o t a lette r o r d ig i t ) . I f n o t , th e c o l o n is i g n o r e d a n d th e
s u b r o u t in e t o f i n d a m e n o m o n i c is c a l l e d a g a i n , sta rt in g a ft e r th e c o l o n .
In 2 6 5 4 0 a te s t is m a d e t o see w h et h er a s e m i - c o l o n or th e en d o f l i n e has
b ee n re ac h ed . I f s o n o furt he r a c t i o n is t a k e n in r e sp ec t o f th e current li n e .
C o m m e n t s m a y th u s be en te red in a p ro g ra m w i t h o u t c o n f u s i o n , p r o v i d e d
th a t th e y c o m m e n c e w ith a s e m i - c o l o n . I f H $ d o e s c o n t a i n ch ara ct er s th e n ,
s in ce th e y h av e n o t b e e n i d e n t i f i e d as a m n e m o n i c t h e y a re a s s u m e d t o be a
la be l o f s o m e kin d a n d th e n a m e is en te re d i n t o th e s y m b o l ta b l e b y th e
s u b r o u t in e at 2 8 7 0 0 . O n retu rn f r o m p la c i n g th e a s s u m e d l a b e l i n t o th e
s y m b o l ta b l e th e p ro g ra m g o e s b a c k t o s ea r ch in g f o r th e o p c o d e m n e m o n i c
w h ic h s h o u l d f o l l o w th e la b e l .
26 540: A t th is p o i n t in th e p ro g ra m a v a l i d m n e m o n i c m u s t h a v e b ee n
d e te c te d . I f its p o s i t i o n in th e t a b l e s , a s i n d i c a t e d b y th e v a ri a b le P O , is
gr eater th an 5 5 t h e n it is an a s s e m b l e r d ir ec t iv e an d th e s u b r o u t in e at 2 6 6 0 0
is c al le d up t o e v a l u a t e it.
26550: H a v i n g f o u n d a v a l i d o p c o d e m n e m o n i c at th is p o i n t , th e s u b r o u
ti n e w h ic h ev a l u a te s th e o p e r a n d t y p e is c a l l e d .
26552: W e n o w h a v e a v a l i d m n e m o n i c a n d , h o p e f u l l y , a v a l i d o p e r a n d
ty p e . H o w e v e r th er e is n o g u a r a n te e th a t th e p art icu la r o p e r a n d t y p e i s
a p p r op r ia t e t o th e pa rti cu la r o p c o d e . T h i s l i n e ca ll s th e s u b r o u t in e w h ic h
es t a b l i s h e d w h et h er th e y d o in f a c t m a k e a v a l i d pa ir. T h e s u b r o u t in e , i f it
c o m e s ac r o s s an a d dr ess w h i c h c o u l d a c c e s s e d b y a z e ro p a g e a d dr es s in g
m o d e , w il l a s s u m e t h a t z e ro p a g e a d d r es s i n g m o d e is b e i n g u s e d , ev e n
t h o u g h th is m a y re sult in a n err or b e i n g f l a g g e d b e c a u s e th e parti cul ar
6 7
Page 70
M a chi ne C o d e M a s te r
o p c o d e c a n n o t use ze ro p a g e a d d r e s s i n g . O n return f r o m th e s u b r o u t in e , i f
th e ad dr es s in g m o d e s e l e c te d is ze ro p a g e a n d a m i s m a t c h is i n d i c a t e d , th e
op er a n d ty p e re pr ese n te d b y t h e v a ri a b l e O P is in c r e m e n te d b y 6 t o tr a n s
fo r m it i n t o an a b s o l u t e a d d r es s i n g m o d e .
26 555: I n a s s e s s i n g th e a s s e m b l y l a n g u a g e i n s t r u c t i o n th er e is a lw a y s a n ee d
t o keep a c o u n t o f h o w m a n y b y te s th e a s s e m b l e d i n s t r u c t i o n wi ll require s o
th at th e n ext i n s t r u c t i o n w il l c o m m e n c e at th e co rr ec t p la c e in m e m o r y .
T hi s is a c c o m p l i s h e d b y th e s u b r o u t in e at 2 6 5 6 0 .
26556: I f th e li n e is n o t e x h a u s t e d b y w h a t ha s s o far b e e n a n a ly s e d and
E N D has n o t b ee n d e t e c t e d , th e r es t o f th e li n e is p r o c e s s e d in th e s a m e w a y .
C H E C K S U M T A B L E
2 6 4 0 0
2 6 4 0 5
1 2 3 2 6 4 0 1
' - - i C j
2 6 4 0 6 5 6
2 6 4 1 0 2 5 5 2 6 4 2 0 1 8 0 2 6 4 3 0
2 6 4 4 0 8 4
2 6 4 5 0
2 6 4 8 0 1 8 0 2 6 4 9 0
1 0 2
6 8
2 6 5 2 0 1 4 2 2 6 5 4 0 3 4 2 6 5 5 0
2 6 5 5 2 1 9 0 2 6 5 5 5 1 7 6
2 6 4 0 2 1 2 3
2 6 4 0 7
2 6 4 6 0
2 6 5 0 0
' ? ~ 7
6 8
1 7 4
8 4
1 6 6
2 6 5 5 6 2 4 7
2 6 5 5 7 1 4 2
M O D U L E 4 . 4
2 8 1 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 1 0 1 R E M P R I N T I N * T O T H E S C R E E N
2 8 1 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 1 2 0 P R I N T 2 5 6 * A S C ( M I ( I N # , 2 , 1 ) ) + A S C ( M
I D * ( I N * , 1 , 1 ) ) M I D * ( I N * , 3 )
2 8 1 4 0 R E T U R N
T hi s m o d u l e s i m p ly pr int s o u t t h e cu rrent li n e o f th e a s s e m b l y la n g u a g e
p ro g ra m , i n c l u d i n g its l i n e n u m b e r. T h is is p rin te d t o th e b o t t o m o f th e
scre en o n th e first p a s s . In th e s e c o n d p a s s th e a s s em b l er d ir ec ti v e P R T can
b e in c lu d ed in th e p ro g ra m t o se n d th e o u t p u t t o a print er.
6 8
Page 71
C h a p te r 4 M a s t e r c o d e A s s e m b l e r
C H E C K S U M T A B L E
2 8 : 1 . 0 0 1 2 3 2 8 1 0 1 1 8 7 2 8 1 0 2 1 2 3
2 8 1 2 0 8 0 2 8 1 4 0 1 4 2
M O D U L E 4 . 5
2 8 1 5 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -
2 8 1 5 1 R E M S Y M B O L T O M O N - L E T T E R / D I G I T
2 8 1 5 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 1 6 0 H * = " " : T = -1
2 8 1 6 5 R T F : = P T R + 1
2 8 1 7 0 I F P T R > L E N < I N * ) T H E M 2 8 2 1 0
2 8 1 8 0 T = A S C ( M I D * ( I N * , R T F: , 1 ) )
2 8 1 8 5 I F T = 3 2 A N D L E N ( H $ > = 0 T H E N 2 8 1 6 0
2 8 1 9 0 I F T < 4 8 O R T > 9 0 O R < I " > 5 7 A N D T < 6 5
) T H E M 2 8 2 1 0
2 8 2 0 0 H * = H * + C H R * ( T ) : G O T O 2 8 1 6 5
2 8 2 1 0 R E T U R N
T hi s s im p le m o d u l e s can s t h e l i n e i n INS f r o m th e p o i n t in d i c a t e d b y th e
v ar ia b le P T R , m a k i n g up a s tr in g , H $ , f r o m w h i c h a n y l e a d i n g s p a ce s a r e
str ipp ed an d w h i c h e n d s w h en ev e r a ch ar a c te r w h i c h is n o t a l ett er or d ig it i s
e n c o u n te r e d . T h e m o d u l e is u s e d b y t h e f o l l o w i n g m o d u l e t o return a string
w h ic h m a y c o n t a i n an o p c o d e m n e m o n i c or a l a b e l .
C H E C K S U M T A B L E
2 8 1 5 0 1 2 3 2 8 1 5 1
2 4 0
2 8 1 6 0 6 2 2 8 1 6 5 1 8 5
2 8 1 8 0 1 7 8 2 8 1 8 5
7 9 2 8 1 9 0
2 8 1 5 2
2 8 1 7 0 5
1 2 3
1 7 9
2 8 2 0 0 9 2 8 2 1 0 1 4 2
M O D U L E 4 . 6
2 8 8 5 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 8 5 1 R E M T E S T F O R O R C O D E / D I R E C T I V E
2 8 1 3 5 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 8 6 0 ( 3 0 S U B 2 8 1 5 0
2 8 8 7 0 E R R = F A L S E
2 8 8 9 0 F T R = R T R .1
2 8 8 9 5 IF L E N < h i * ) < >3 T H E N 2 8 9 4 0
2 8 9 0 0 P O = - - 2
2 8 9 1 0 P O F ' 0 - 1 - 3
6 9
Page 72
M ach in e C o d e M a s t e r
2 8 9 2 0 IF H T = M I D T ( T A T ( 2 ) , P 0 , 3 ) T H E N 2 8 9 5 0
2 8 9 3 0 I F ( F ' O + 3 ) < = L E N ( T A T ( 2 ) > T H E N 2 8 9 1 0
2 8 9 4 0 E R R = T R U E
2 8 9 5 0 P O = ( P 0 - l ) / 3
2 8 9 6 0 E R R = < P 0 = 5 6 > O R E R R
2 8 9 7 0 I F P O > 5 6 T H E N P O = P O - - 1
2 8 9 8 0 R E T U R N
R E A D Y .
H a v i n g o b t a i n e d a str ing o f ch a ra ct er s w h i c h m a y o r m a y n o t c o n t a i n a
v a li d o p c o d e or l a b e l w e n o w b e g i n th e p r o c e s s o f a c t u a l l y e v a l u a ti n g w h at
we h a v e f o u n d . T h e r o u g h m e t h o d o f s ea r ch in g f o r th e co rr ec t o p c o d e wa s
d esc rib ed un d er M o d u l e 1 a n d th e p r o c e s s b e g i n s w i th th is m o d u l e .
C o m m e n t a r y
28 89 0: I f th e str ing re tu rn ed i n H $ is n o t th re e ch ar a ct er s l o n g t h e n it
c a n n o t b e a v a l i d o p c o d e a n d th er e is n o p o i n t in s ea r ch in g th e t a b l e s .
288 95: P T R is reset t o p o i n t t o th e l a s t ch ara ct er o f H $ in th e li n e fr o m
w h i ch it w as d ra wn .
2 8 9 0 0- 28 9 40 : T h i s is a l o o p w h i c h s ca n s T A $ ( 2 ) — w h i c h c o n t a i n s th e v a l i d
thre e letter m n e m o n i c s — t o c o m p a r e ea c h set o f ch ar a ct er s w ith w h a t has
b e e n p ic k e d up in H $ . T h e p o in t e r f o r th i s p u r p o s e is first se t t o -2 in order
th a t o n th e fi rst i t e r a t i o n o f th e l o o p , w h e n 3 is a d d e d , th e sear ch star ts a t
o n e . I f th e w h o l e l o o p is e x e c u t e d a n d l i n e 2 8 9 4 0 re a c h e d , it ca n o n l y be
b e c a u s e th e th re e ch ara ct er s d o n o t c o n f o r m t o a n y o f th e m n e m o n i c s fo r
o p c o d e s s p e c i f i e d in th e t a b l e .
28 950: T h e ad dr ess in T A $ ( 2 ) , w h i c h w a s in c r e m e n te d in s te ps o f th r e e , is
n o w a d ju s te d s o t h a t , f o r i n s t a n c e , th e th re e ch ara ct er s at p o s i t i o n 1 9 - 2 1
a r e n o w i d e n t i f i e d in P O as m n e m o n i c 7.
2896 0: T hi s str an ge l o o k i n g li n e i s h ere t o t a k e a c c o u n t o f th e fa c t t h a t th e
D is a s s e m b l e r ta b l e s o n w h i c h th e A s s e m b l e r w o r k s c o n t a i n th e t h re e c h a
r ac te rs ‘ ? ? ? ’ t o c o p e w it h ti m e s w h e n th e D i s a s s e m b l e r c a n n o t p r o v i d e a
v a li d o p c o d e f o r w h a t is in t h e m e m o r y . W i t h o u t t h i s c h e c k y o u w o u l d be
a b le t o ent er ‘ ? ? ? 1 ’ i n t o a n a s s e m b l y l a n g u a g e p ro g ra m a n d t h r o w th e w h o l e
t h i n g i n t o c o n f u s i o n w h e n it w a s f o u n d as a v a l i d m e n o m o n i c . T h e li n e
fl a g s an e rro r i f H $ c o n s i s t s o f ‘ ? ? ? \
2897 0: T hi s li n e is a l s o th er e t o t a k e a c c o u n t o f t h e * ? ? ? ’ i n th e t a b l e . T h e
as se m b le r d ir ec ti ve s f a l l a f t e r th e q u e s t i o n ma rk s a n d s o , w h e n n u m b e ri n g
t h e m f o r th e p u r p o s e s o f th e a s s e m b l e r , th eir p o s i t i o n is re du ce d b y o n e .
7 0
Page 73
C H E C K S U M T A B L E
C h a p t e r 4 M a s t e r c o d e A s s e m b l e r
2 8 8 5 0
2 8 8 6 0 1 7 3
2 8 8 9 5 1 7 6 2 8 9 0 0
2 8 9 2 0 5 7
2 8 9 5 0
1 2 3 2 8 8 5 1 1 5 8 /
2 8 8 7 0 7 0
1 1 0
2 8 9 3 0 2 4
2 8 9 6 0
1 9 3
2 8 8 5 2
2 8 8 9 0
2 8 9 1 0
1 2 3
1 8 6
1 3
2 8 9 4 0 2 7
2 8 9 7 0
8 9
2 o 9 8 0 61 1 4 2
M O D U L E 4 . 7
2 8 7 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 7 0 1 R E M A D D S Y M B O L T O S Y M B O L T A B L E
2 8 7 0 2 R E M * * * * * * * • * * • * * * * * ■ * • * • * • * • • * * # • # ■ * • * * * • * * * *
2 8 7 1 0 IF B E > ~ S M T H E M E X I T « T R U E : P A S S
■ 2 : E M « 1 4 s G O T O 2 8 0 0 0
2 8 7 2 0 G O S U B 2 8 2 5 0 ; I F N O T E R R T H E N 2 8 8 3
0
2 8 7 4 0 T * = L E F T * ( H * + " " , 6 )
2 8 7 4 5 T B = P T R
2 8 7 5 0 G O S U B 2 8 1 5 0 : R E M D O E S « F O L L O W
2 8 7 6 0 IF T < > 6 1 T H E N P T R - T B : R E = A D :
G O T O 2 8 7 8 0
2 8 7 7 0 7 0 - T ; G O S U B 2 8 6 0 0
2 8 7 8 0 E N ■ 0
2 8 7 9 0 IF R E < 0 O R R E > 6 5 5 3 5 ' T H E N S T * ( S E > = T
* + C H R * ( 0 ) + C H R * ( 0 ) + C H R * ( 2 ) n G O T O 2 8 8 1 0
2 8 8 0 0 S T * ( S E ) = T * + C H R * ( R E - 1 N T ( R E / 2 5 6 ) * 2
5 6 ) + C H R * < I N T ( R E / 2 5 6 ) )
2 8 8 1 0 S E = S E + 1
2 8 8 2 0 R E T U R N
2 8 8 3 0 I F P A S S 5 5 1 A N D L E N ( S T * ( 7 1 ) ) < 9 T H E N
S T * ( T 1 ) = S T * ( T 1 ) + C H R * ( 8 )
2 8 8 3 5 IF P A S S 0 2 T H E N 2 8 8 4 0
2 8 8 3 6 T A = P T R : G O S U B 2 8 1 5 0 : I F 7 0 6 1
T H E N P T R = T A : G O T O 2 8 8 4 0
2 3 8 3 7 G O S U B 2 6 0 0 0 : R E M S C A N P A S T « S I G N
( I F P R E S E N T ) O N P A S S 2
2 8 8 4 0 IF P A S S O - 2 O R L E N ( S T * ( T 1 ) ) < 9 T H E N
R E T U R N
2 8 8 4 5 E N = A S C ( M I D * ( S T * ( T 1 ) , 9 , 1 ) ) : G O T O
2 8 0 0 0
R E A D Y .
T hi s is th e c o n t r o l m o d u l e f o r e v a l u a t i n g va ri a b l es a n d l a b e l s a n d p l a c
ing t h e m in t o t h e s y m b o l ta b l e i f t h e y h a v e n o t a lr ea d y b e e n d e f i n e d . E ar-
71
Page 74
M a chi ne C o d e M a s te r
H e r , under M o d u l e 2 , a l a b e l w as d es cr ib ed as a kin d o f v a ri a b le f o r th e
s ak e o f s i m p li c it y . In fa c t a la b e l is a c o n s t a n t w h ic h i d e n t i f i e s a p o i n t in a
m a c h in e c o d e p ro gr a m t o w h i c h a j u m p m a y be m a d e . B y u s in g l a b e l s it
b e c o m e s p o s s i b l e t o a s s e m b l e a m a c h i n e c o d e pr o g ra m t o a d i f f e r e n t p la c e
in th e m e m o r y w i t h o u t h a v in g t o re c a l c u l a te th e a d dr es s es t o w h ic h j u m p s
wil l be m a d e . T h e A s s e m b l e r wi ll a u t o m a t i c a l l y i d e n t i f y th e p o s i t i o n in
m e m o r y o f a l a b e l le d i n s t r u c t i o n and re pl a ce a j u m p t o th e l a b e l w ith a
j u m p t o th a t ad dr es s .
C o m m e n ta r y
28710: I f th er e ar e m o r e s y m b o l s t h a n th e s y m b o l t a b l e is set up f o r (5 0 ) ,
th en a s s e m b l y o f th e p ro g ra m ce a s e s i m m e d i a t e l y an d th e a p p r o p r ia t e
er r or i s f l a g g e d .
28720: T h e l ab e l in H $ is n o w s en t t o t h e n e x t m o d u l e , w h i c h ex a m in e s th e
s y m b o l ta b l e (STS) t o s e e w h et h er it is a lr ea d y p re s e n t . I f it is p re s e nt t h e n
E R R is returned a s f a l s e , an d an e rro r ‘ l a b e l d e f i n e d t w i c e ’ wi ll b e f l a g g e d .
T hi s reve rs e u s e o f err or m a y se e m c o n f u s i n g bu t is n ec e s s a ry s i n c e a later
u se o f th e m o d u l e a t 2 8 2 5 0 w il l require an er r o r t o be f l a g g e d i f a la b e l is
n o t in th e s y m b o l ta b l e .
28740: H $ is p a d d e d o u t t o si x ch ara ct er s i f it is s h ort er th a n th a t. Six c h a
r a cte rs i s th e m a x i m u m f o r a n y l a b e l .
2 8 74 5- 28 76 0: T h e c o n t e n t o f H $ , i f it is v a l i d at a ll , m a y be either a v a r i a
b le or a la b e l . I f it is a la b e l th e n all th e A s s e m b l e r w il l n ee d t o k n o w is th e
ad dre ss o f th e in s t r u c t i o n s o l a b e l le d . I f it is a v a ri a b l e it wi ll be f o l l o w e d
by an ‘ = ’ t o set its v a l u e . T h e s c a n n in g m o d u l e at 2 8 1 5 0 is ca l l e d up t o ge t
th e next cha ra c te r . I f th e n e x t ch ar a c te r a ft er th e v a ri a b le (i g n o r i n g
s p a ce s ) is n o t an e q u a ls s ig n th e n it m u s t b e a l a be l a n d RE ( R E s u l t) is us ed
t o s to r e th e curr ent a d dr ess o f th e i n s t r u c t i o n . P T R is b a c k e d up t o th e
en d o f th e la b e l a g a i n u s in g th e te m p o r a r y v a ri a b l e T B .
28870: I f th e c o n t e n t s o f H $ d o c o n t a i n a v a ri a b l e t h e n th e ‘ e x p r e s s io n
e v a l u a t o r ’ s e c t i o n o f th e p ro g ra m is n o w ca l l e d u p . N o a tt e m p t w il l be
m a d e t o ex p l a i n th e w o r k i n g o f t h e e x p r e s s io n e v a l u a t o r u n t il th e en d o f
th e p ro gra m s i n c e it w o u l d i nt err upt o u r a tt e m p t t o f o l l o w t h r o u g h th e
m ai n w o r k i n g s o f th e A s s e m b l e r . F o r th e m o m e n t all t h a t y o u n ee d t o
a cc ep t is th a t a ca ll t o th e e x p r e s s io n e v a l u a t o r f o r a li n e l ik e
V A R = 25 6* B Y T E 1 - I - 1 5 w o u l d return in t h e v a ri a b l e RE th e res ult o f th e
ri ght h an d part o f th e e q u a t i o n . A l l w il l u l t i m a t e l y b e m a d e cl ea r!
7 2
Page 75
C h a p te r 4 M a s te r c o d e A s s e m b l e r
28790: I f RE is le s s th a n z e ro o r greater th a n 6 5 53 5 (t h e m a x i m u m v a l u e
wh ic h ca n be d e a l t w it h b y th e C P U in o n e in s t r u c t i o n ) t h e n t o th e v a ri ab le
n am e or l ab e l in th e s y m b o l t a b l e a re a d d e d t w o ch ara ct er s w h i c h repr esen t
a result o f 0 an d a n o th e r w h i c h f l a g s an err or ‘ d o u b l e b y t e o u t o f r a n g e ’ .
28800: I f R E is a v a li d n um be r th e n th e n um be r is a d d e d in t w o b y te f o r m t o
th e en d o f th e va ri a b l e or l a be l n a m e in ST $ . S in c e th e n a m e has b ee n set t o
6 ch arac ters in ev er y c a s e i t w il l be s i m p le t o re co ve r th e v a l u e o f RE f o r a ny
var ia b le or la b e l .
28830: T hi s l in e is o n l y a c c e s s e d i f th e curr ent n a m e is a lr ea d y in th e s y m b o l
ta b l e . P r o v i d e d t h a t an e rro r c o d e is n o t a lr ea d y a tt a c h e d t o th e n a m e , it
has e rr or c o d e 8 a d d e d t o i t, i n d i c a t in g ‘ la b e l d e f i n e d t w i c e ’ .
2 8 8 3 5- 28 83 7: T h i s m o d u l e i s a l s o u s ed b y th e s e c o n d pa s s t h r o u g h th e p r o
gra m . O n pas s 2 th e n a m e w il l a l re a d y be in th e s y m b o l t a b l e (p l a c e d th er e
by p as s o n e ) an d s o t h e first part o f th e m o d u l e w il l n o t b e carried o u t . On
pass 2, h a v in g o b t a i n e d th e n a m e in H $ , i f th e n ex t ch ara ct er is an e q u a ls ,
th en th e result o f th e e x p r e s s io n has a lr ea d y b e e n o b t a i n e d an d th e m o d u l e
a t 2 6 0 0 0 , w h ic h f i n d s th e end o f an e x p r e s s io n or l i n e , is c a l l e d t o sk ip o v er
th e r es t o f th e e x p r e s s io n .
28840: Er r or m e s s a g e s a r e o n l y p ri nt ed o n p a ss 2 a n d , fa i rl y o b v i o u s l y ,
o n l y i f there is an err or c o d e a tt a c h e d t o th e en d o f a n a m e in th e s y m b o l
ta b l e .
C H E C K S U M T A B L E
2 8 7 0 0 1 2 3
2 8 7 1 0 2 3 9
2 8 7 4 5
1 2 6
2 8 7 7 0 2 4 1
2 8 7 0 1
2 8 7 2 0
2 8 7 5 0
2 8 7 8 0
1 7 5
1 1 2
81
18 i .
2 8 8 0 0 2 5 1 2 8 8 1 0 2 5 3 2 8 8 2 0
2 8 8 3 0
2 8 8 3 7
M O D U L E 4 . 8
1 0 6
1 6 0
2 8 8 3 5
2 8 8 4 0
10 1
1 2 6
2 8 7 0 2 1 2 3
2 8 7 4 0 31
2 8 7 6 0
2 2 7
2 8 7 9 0 4 0
1 4 2
2 8 8 3 6
2 8 8 4 5
1 1 7
5 5
2 8 2 5 0 R E l M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 2 5 1 R E M F I N D L A B E L I N S T *
2 8 2 5 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 2 6 0 E R R = F A L S E s H = 0 : T1 = 0
2 8 2 7 0 IF L E N ( H * > < 6 T H E N H * = H * + " " : G O
T O 2 8 2 7 0
2 8 2 8 0 IF T 1 = S E T H E N E R R = T R U E s R E T U R N
2 8 2 9 0 I F M I D * < S T * <T1> , 1 , 6 ) O H * T H E N T 1 =
T 1 + i s G O T O 2 8 2 8 0
7 3
Page 76
M ach in e C o d e M a s te r
2 8 2 9 5 H = A S C ( M I D 4 > < S T T ( T 1 ) , 8 , 1 ) ) * 2 5 6 + A S C
( M I D * ( S T * ( T 1 ) , 7 , 1 ) ) : R E T U R N
T h i s a st r a ig h tf o r w a r d m o d u l e w h i c h is c a l l e d b y th e p r e v i o u s o n e t o
d et e r m i n e w h e th e r a v a r i a b l e o r l a b e l n a m e is al r e a d y in t h e s y m b o l t a b l e .
T h e m o d u l e returns an err or f l a g o f 1 o r 0 t o i n d i c a t e th e p re s e n c e o r
a b s e n c e o f t h e va ri a b l e or l a b e l . A l s o re tu rn ed , i f p re s e n t is th e res ult c o n
ta i n e d in ch ara ct er s 7 a n d 8 .
C H E C K S U M T A B L E
2 8 2 5 0 1 2 3 2 8 2 5 1 2 4 2 2 8 2 5 2 1 2 3
2 8 2 6 0 7 5 2 8 2 7 0 2 4 9 2 8 2 8 0 1 3 2
2 8 2 9 0 2 1 9 2 8 2 9 5 9 2
M O D U L E 4 . 9
2 8 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 0 0 1 R E M A S S E M B L E R E R R O R R O U T I N E
2 8 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 0 0 5 I F P T R > = 3 0 0 O R P A S S < > 2 T H E N 2 8 0 5 0
: R E M S U P R E S S S E C O N D A R Y E R R O R S I N L I N E
2 8 0 1 0 P R I N T S F ' C (1 4) ; : G O S U B 2 8 1 0 0
2 8 0 1 5 E C = E C + 1
2 8 0 2 0 F O R X = - 1 3 T O P T R : P R I N T " = "
N E X T X : P R I N T " E C U 3 "
2 8 0 3 0 P R I N T " " E R R * ( E N > " E R R O R "
2 8 0 4 0 P T R = 3 0 0 : E R R * T R U E
2 8 0 5 0 R E T U R N
T h i s m o d u l e , w h i c h is c a l l e d b y li n e 2 8 8 4 0 in M o d u l e 4 . 7 , pr int s o u t a n
erro r m e s s a g e w h er e an err or i s f l a g g e d .
C o m m e n t a r y
28 005: I f an err or h a s a l r e a d y b e e n n o t i f i e d f o r t h e curr en t l i n e , t h e v a ri a
b le P T R is se t t o t h e i m p o s s i b l e v a l u e o f 3 0 0 (m a x im u m l e n g t h o f a str in g is
2 55 ) a n d n o s u b s e q u e n t error m e s s a g e s are p rin te d f o r t h a t l i n e . Errors a re
n o t p rin te d o n p a s s 1 .
280 10: O n t h e s e c o n d p a s s , th e m e m o r y a ddr ess a n d b y t e d a t a t a k e 1 4
s p ac es o n th e l i n e . W h e n a li n e is p ri n t ed a n d a n err or is t o b e f l a g g e d th e
ad dr ess a n d d a ta are n o t p ri n t e d .
28 0 20 -2 8 0 3 0 : T h e erro r is n o t o n l y f l a g g e d b y a n err or m e s s a g e in th e
o u t p u t , a p o in t e r is p ri n t ed at t h e a p p r o x i m a t e p o s i t i o n i n th e li n e w h er e
t h e err or h as b e e n d e t e c t e d , as i n d i c a t e d b y th e v a l u e o f P T R .
7 4
Page 77
C H E C K S U M T A B L E
C h a p te r 4 M a s t e r c o d e A s s e m b l e r
2 8 0 0 0
2 8 0 0 5
2 8 0 2 0
1 2 3 2 8 0 0 1 6 1 2 8 0 0 2
c p r ?
1 2
2 8 0 1 0 1 0 6 2 8 0 1 5
2 8 0 3 0 1 2 4 2 8 0 4 0
2 8 0 5 0 1 4 2
M O D U L E 4 . 1 0
2 6 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 0 0 1 R E M S Y M B O L U P T O C O L O N E T C .
2 6 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 0 1 0 H $ = " " : T 1 = L E N U N $ )
2 6 0 2 0 P T R = P T R + 1
2 6 0 3 0 IF T 1 < P T R T H E N 2 6 0 6 0
2 6 0 4 0 T = A S C ( M I D * ( I N * , P T R , 1) >
2 6 0 4 5 I F T = 3 2 T H E N 2 6 0 2 0
2 6 0 5 0 I F T < > 5 8 A N D T < > 5 9 T H E N H * = H * + C H
R $ < T ) : G O T O 2 6 0 2 0
2 6 0 6 0 R E T U R N
T hi s m o d u l e is s im il a r t o t h e m o d u l e a t 2 8 1 5 0 ( M o d u l e 5 ), its p u r p o s e
b e i n g t o de te rm in e t h e en d p o i n t o f an a s s e m b l e r i n s t r u c t i o n . It i s d i f f e r e n t
fr o m M o d u l e 5 in t h a t it d o e s n o t return o n fi n d i n g a n o n - l e t t e r / n o n - d i g i t
ch ara ct er b u t o n l y o n fi n d i n g a d el i m i te r s u ch as a c o l o n , s e m i - c o l o n o r th e
en d o f li n e , d r o p p i n g a n y s p a c e s w h i c h are pr es e nt i n t h e o r i g i n a l l i n e .
C H E C K S U M T A B L E
2 6 0 0 0
1 ^ ~ : ' I
2 6 0 0 1
2 6 0 1 0 9 8 2 6 0 2 0 1 8 5
2 6 0 4 0 1 7 8 2 6 0 4 5 2 4 7 2 6 0 5 0
2 1 0
2 6 0 0 2
1 2 3
2 6 0 3 0 1 9 0
2 0 1
2 6 0 6 0 1 4 2
M O D U L E 4 . i l
2 6 6 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 6 0 1 R E M C A L C U L A T E D I R E C T I V E L E N G T H
2 6 6 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 6 1 0 T 1 = L E N ( I N $ )
2 6 6 2 0 I F P 0 = 5 6 T H E N 2 6 7 2 0 : R E M B Y T D I R E
C T I V E
2 6 6 2 5 I F P O = 6 0 T H E N G O S U B 2 8 6 0 0 : A D = R
E S U L T : R E M D E A L W I T H O R G D I R E C T I V E
7 5
Page 78
M a chi ne C o d e M a s te r
2 6 6 2 7 I F P 0 = 5 9 T H E N E X I T = T R U E
2 6 6 3 0 I F P C ) > 5 8 T H E N R E T U R N : R E M E N D & 0
R G D I R E C T I V E S
2 6 6 4 0 R E M F I N D L E N . O F W R D & D B Y
2 6 6 5 0 A D = A D + 2
2 6 6 6 0 P T R = P T R + 1
2 6 6 7 0 I F P T R > T 1 T H E N R E T U R N
2 6 6 8 0 T = A S C ( M I D T ( I N T , P T R , 1 ) )
2 6 6 9 0 I F T = 5 8 O R T = 5 9 T H E N R E T U R N
2 6 7 0 0 I F T < > 4 6 T H E N 2 6 6 6 0
2 6 7 1 0 G O T O 2 6 6 5 0
2 6 7 2 0 R E M L E N G T H F O R B Y T .
2 6 7 3 0 A D = A D + 1
2 6 7 4 0 P T R = P T R + 1
2 6 7 5 0 I F P T R > T 1 T H E N R E T U R N
2 6 7 6 0 T = A S C ( M I D T ( I N T , P T R , 1 ))
2 6 7 7 0 I F T = 5 8 O R T = 5 9 T H E N R E T U R N
2 6 7 8 0 I F T < > 4 6 T H E N 2 6 7 4 0
2 6 7 9 0 G O T O 2 6 7 3 0
T h i s m o d u l e i s u s ed ex c l u s i v e l y b y P a s s 1 a n d a ct s u p o n t h o s e a s s em b l er
d ir ec ti ve s w h i c h are re le v a n t t o t h a t p art o f t h e p ro g ra m e x e c u t i o n , t h e s e
b e i n g B Y T , W R D , D B Y , E N D , O R G . T h e m o d u l e is c a l l e d f r o m M o d u l e 3
w h en ev e r a n a s s em b le r d ir e c t i v e i s d e t e c t e d .
C o m m e n ta r y
26 620: T hi s d ea ls w i th th e B Y T d ir ec t iv e a n d w il l b e d is c u s s e d un d er 2 6 7 2 0 .
2 6 6 2 5 : 6 0 is t h e c o d e f o r t h e O R G d ir e c t i v e , w h i c h is u s e d t o set t h e a d dr ess
in m e m o r y o n w h i c h s u b s e q u e n t a s s e m b l y w i l l b e b a s e d . O R G wi ll b e
f o l l o w e d o n th e l i n e b y an e x p r e s s io n a n d th e ‘ e x p r e s s io n e v a l u a t o r ’ ( n o t
ex p l a i n e d y e t ) is c a l l e d t o g e t t h e des ir ed add r es s f r o m t h e e x p r e s s i o n . A D ,
th e ad dr ess at w h i c h t h e n ex t m a c h i n e c o d e b y t e w il l b e p la c e d is t h e n set
eq u al t o t h e res ult o f th e e x p r e s s io n .
26 62 7: I f t h e E N D d ir ec t iv e is e n c o u n t e r e d t h e v a r i a b l e E X I T is s e t t o
T R U E .
26 630: O ther t h a n O R G a n d E N D , n o n e o f t h e d ir ec t i v es w i th a c o d e
gre ater t h a n 5 8 ( S Y M , P R T ) a f f e c t p ro g ra m e x e c u t i o n o n t h e fi r s t p a s s .
26 64 0 -2 6 7 1 0 : A t th i s p o i n t i n t h e p r o g r a m e x e c u t i o n , w h a t h a s b e e n
en c o u n te r e d m u s t b e o n e o f t h e t w o d ir ec t i v es W R D an d D B Y . T h e s e t a k e
th e f o r m in th e a s s e m b l y l a n g u a g e pro gra m:
W R D (or D B Y ) $ A A A A . $ B B B B . $ C C C C ie t h e d ir ec t iv e f o l l o w e d b y a
series o f tw o b y t e v a l u e s w h i c h w i l l b e p la c e d d ir ec t l y i n t o m e m o r y — t h u s
7 6
Page 79
C h a p te r 4 M a s t e r c o d e A s s e m b l e r
a l l o w i n g a ta b l e t o b e d e f i n e d .
T h e d if f e r e n c e b e t w e e n th e t w o d ir ec t i v es is t h a t W R D w i l l ta k e t h e t w o
b yt es s p e c i f i e d b y , f o r i n s t a n c e $ A B C D a n d s to r e t h e m in th e m e m o r y in
th e order C D , A B w h i le D B Y w i l l s to r e th e m in th e o rd er A B , C D . T h e
C P U c h i p n o r m a l l y w o r k s u p o n t w o b y t e n u m b e rs w h e r e th e le a s t
s i g n i f i c a n t b y te (C D in o u r e x a m p l e ) c o m e s fi rs t . T h e p r o b le m w ith t h e s e
t w o d ire ct iv es o n th e fi rst p a s s is t h a t , as w e h a v e p r e v i o u s l y n o t e d , a
re cord m u s t b e k ep t o f th e le n g th in b y te s o f e a c h i n s t r u c t i o n in orde r th a t
l a b e ls m ay b e g i v e n th eir co r re c t a d dr es s es in th e m e m o r y w h e n t h e y ar e
d e f i n e d o n th e fir st p a s s . B Y T a n d W R D ca n h a v e an y n u m b e r o f t w o b y te
v a lu e s af t er th e m up t o t h e m a x i m u m stri n g l e n g t h o f 2 5 5 . T h i s l o o p th e r e
f o r e sca n s t h e l i n e , c o u n t i n g t h e n u m b e r o f t w o b y t e v a l u e s an d
i n c re m en ti n g t h e ad dr es s c o u n t e r A D b y t w o f o r ea c h v a l u e f o u n d .
26 72 0 -2 6 7 9 0 : T h e B Y T i n s t r u c t i o n s p e c i f i e s s i n g l e by te v a l u e s t o b e p la c e d
i n t o m e m o r y . T h i s l o o p p e r f o r m s th e s a m e f u n c t i o n a s t h e p r e v i o u s o n e
b u t o n l y in c re m en ts t h e ad dr es s c o u n t e r b y o n e f o r e a c h v a l u e s p e c i f i e d .
C H E C K S U M T A B L E
2 6 6 0 0
1 2 3 2 6 6 0 1
2 6 6 1 0 7 0
2 6 6 2 7
1 8 9 2 6 6 3 0 6 1 2 6 6 4 0 4
2 6 6 2 0 3 8
f ’ ? ' ~ ?
2 6 6 0 2 1 2 3
2 6 6 2 5 4 0
2 6 6 5 0 2 1 6 2 6 6 6 0 1 8 5 2 6 6 7 0 7 6
2 6 6 8 0
2 6 7 1 0
2 6 7 4 0
1 7 8 2 6 6 9 0 2 4 7
1 7 2
1 8 5 2 6 7 5 0 7 6 2 6 7 6 0 1 7 8
2 6 7 7 0 2 4 7
M O D U L E 4 . 1 2
2 6 7 2 0 18 1 2 6 7 3 0
2 6 7 8 0
1 8 3 2 6 7 9 0
2 6 7 0 0 1 8 4
2 1 5
1 7 1
2 6 : 1 . 0 0 R E M * * * * * * * * * * * * * * * * * * . * * * * * * * * * * * *
2 6 1 0 1 R E M O P E R A N D T Y P E T O B E U S E D
2 6 1 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 1 1 0 T 6 = P T R : G O S U B 2 6 0 0 0
2 6 1 2 0 E R R = F A L S E
2 6 1 3 0 IF L E N < H $ > = 0 T H E N O P = 1 : R E T U R N
2 6 1 4 0 IF H $ = " A " T H E N O P = 0 : R E T U R N
2 6 1 4 5 IF A S C ( H $ ) = 3 5 T H E N O P = 2 : R E T U R N
2 6 1 7 0 O P = 1 2
2 6 1 8 0 IF L E F T * ( H I T , ! ) = " ( " T H E N O P = O P - 3
2 6 1 9 0 T = 1 : T 1 = L E N ( H T )
2 6 2 0 0 T 2 = A S C ( M I D $ ( H : T , T , 1 ) >
2 6 2 1 0 IF T 2 < > 4 6 A N D T C T i T H E N T = T + i :
G O T O 2 6 2 0 0
7 7
Page 80
M a ch in e C o d e M a s t e r
2 6 2 2 0 IF T 2 < > 4 6 T H E N 2 6 2 7 5
2 6 2 3 0 T = T + l : I F T > T 3 . T H E N 2 6 2 7 0
2 6 2 4 0 T 2 = A S C ( M I D * < F I T , T , 1 ) )
2 6 2 5 0 I F " T 2 = 8 9 T H E N O P = O P - 1 : G O T O 2 6 2
7 5
2 6 2 6 0 I F T 2 — 8 8 T H E N O P « * O P - 2 : G O T O 2 6 2
7 5
2 6 2 7 0 R E M N O T A V A L I D I N D E X
2 6 2 7 2 E N « 5 s G O T O 2 8 0 0 0
2 6 2 7 5 I F ( O P = : l . 2 ) A N D ( ( P O > 2 A N D P 0 < 6 ) O R ( P O > 6 A
N DF 'O C 1 0 ) Q R P 0 = 1 2 0 R P 0 = 1 1 ) T H E N O P = 3
2 6 2 8 1 R E M Z E R O P A G E O P R A N D S
2 6 2 8 2 I F O P < 1 0 T H E N R E T U R N
2 6 2 8 4 T ' 7 = P T R : P T R = T 6
2 6 2 8 6 G O S U B 2 8 6 0 0
2 6 2 8 8 IF E R R O R R E S U L T > 2 5 5 T H E N 2 6 2 9 2
2 6 2 9 0 O P = O P - 6
2 6 2 9 2 P T R « T 7
2 6 2 9 4 R E T U R N
F o l l o w i n g o n t h e d e v e l o p m e n t o f P a s s 1 , a s l a id d o w n b y th e c o n t r o l
m o d u l e , w e n o w c o m e t o t h e t w o r o u ti n e s w h i c h d et e r m i n e t h e ty p e o f
op er a n d t o b e u s e d an d w h e th e r t h a t o p e r a n d t y p e a c t u a l l y f i t s t h e o p c o d e
t y p e , r e m em be ri ng th a t n o t ev e ry a d d r e s s i n g m o d e c a n b e u s e d b y eve ry
o p c o d e t y p e . T h e p u r p o s e o f t h e p r e s e n t m o d u l e is t o d et e rm i n e th e
op er a n d ty p e w h i c h fi ts t h e f o r m a t l a id d o w n i n t h e i n s t r u c t i o n .
C o m m e n ta r y
261 10: A t th i s p o i n t P T R is i n d i c a t in g t h e cha ra c te r a f t e r th e o p c o d e
m n e m o n i c an d t h e s u b r o u t i n e a t 2 6 0 0 0 is ca l l e d t o o b t a i n t h e o p e r a n d par t
o f th e i n s t r u c t i o n , s tr ip p ed o f its s p a c e s .
261 30: I f th e o p e r a n d h a s z e r o l e n g th t h e n th e a d d r e s s i n g m o d e m u s t be
i m p l ie d , a n d th e v a l u e o f O P is z e r o .
261 40: I f t h e o p e r a n d is ‘ A ’ t h e n t h e a d d r es s in g m o d e is a c c u m u l a t o r
a d d r e s s i n g , O P = 1 .
261 45: I f th e fi r s t ch ar a c te r o f t h e o p e r a n d is ‘ th e n th e a d d r es s in g m o d e
is i m m e d i a t e , O P = 2.
26 1 50 -2 6 1 6 0 : T h e t e m p o r a r y a s s u m p t i o n is n o w m a d e th a t th e o p er a n d
t y p e is 3 , r e la t iv e a d d r es s in g m o d e . T h e f o l l o w i n g s u b r o u t i n e is c a l l e d t o
s ee i f th e ta b l e s i n T A $ in d i c a t e t h a t th i s is p o s s i b l e f o r t h e o p c o d e t h a t h a s
b e e n derive d (ie t h e o p c o d e m u s t b e a b ra n ch o f s o m e k in d ) . T h e m e t h o d b y
7 8
Page 81
C h a p te r 4 M a s te r c o d e A s s e m b l e r
w h i ch th is i s d o n e w il l be d es cr ib ed un d er th e n ex t m o d u l e . T h e re fe r en ce s
t o 0 $ a r e o n l y re le v a nt t o P a s s 2 . T h e s u b r o u t in e at 2 6 3 0 0 w il l p la c e i n t o 0 $
th e b yt e w h o s e v a l u e repr ese nt s th e re le va nt o p c o d e .
A t th is p o i n t , h o w e v e r , w e a re o n l y u s in g th e s u b r o u t in e t o d et e rm in e
wh eth er th e o p e r a n d ty p e is 3 — w e d o n o t w is h t o i n c re m en t 0 $ a t th is
p o in t s o we t a k e a n o t e o f its l e n g th b e f o r e th e s u b r o u t in e is c a l l e d a n d th e n
r ese t it t o th e s a m e l e n g th o n ret ur n. I f an e rro r is fl a g g e d o n retu rn fr o m
th e s u b r o u t in e a t 2 6 3 0 0 th e n o p e r a n d ty p e th re e d o e s n o t fi t th e o p c o d e we
h av e an d th e s u b r o u t in e c o n t i n u e s .
2 6 1 7 0- 2 62 60 : T h e s e li n e s a r e a mirr or i m a g e o f th e li n e s in th e D i s a s s e m b l e r
wh ich wo rk o u t th e fo r m a t o f th e o p e r a n d f r o m th e v a l u e o f th e o p er a n d
b yt e in a m a c h i n e c o d e i n s t r u c t i o n . I n th i s c a s e w e w o r k o u t th e o p e r a n d
ty p e f r o m e x a m in i n g th e f o r m a t .
2 6 27 0- 26 28 0: I f a fu l l s t o p is d e t e c t e d an d th e fo r m a t d o e s c o n f o r m t o th a t
fo r in d ex e d a d d r e s s i n g , e rr o r 5 is fl a g g e d — ‘ i n d e x is n o t X o r Y \
2 6 28 2- 26 29 4: T h e s e li n e s t e s t w h et h er it is p o s s i b l e t o p e r f o r m t h e i n s t r u c
t i o n w it h a ze ro p a g e a d d r e s s i n g m o d e — th e f o r m a t f o r a b s o l u t e
ad dr es s in g an d z e r o - p a g e a d d r e s s i n g is th e s a m e an d th e a s s u m p t i o n has
b ee n m a d e up t o th i s p o i n t th a t o p e r a n d s w h i c h c o u l d b e ei th er a r e in f a c t
a b s o l u t e a d d r es s es . T h i s is o n l y p o s s i b l e w ith o p e r a n d ty p e s o f 1 0 and
a b o v e , re pr ese n tin g th e a b s o l u t e a d d r e s s i n g m o d e s . T h e P T R is reset t o th e
end o f th e o p c o d e and th e o p e r a n d r e -e v a l u a te d b y th e E x p r e s s i o n E v a l u a
to r . I f th e re sult fa l l s in th e ra ng e 0-2 55 t h e n it is p o s s i b l e t o u s e t h e fa ste r
ze ro p ag e ad d r es s in g m o d e an d th e o p e r a n d t y p e O P is re du ce d b y 6 t o
tr a n s fo r m th e ad d r es s in g m o d e i n t o z e ro p a g e a d d r es s in g o f s o m e k in d .
C H E C K S U M T A B L E
2 6 1 0 0 1 2 3
2 6 1 1 0 1 4 5
2 6 1 4 0 2 5 4
2 6 1 8 0 1 6 0
2 6 2 1 0 2 4 3
2 6 2 4 0 2 4 3
2 6 2 7 0 4 1
2 6 2 8 1 9 9
2 6 2 8 6 1 7 3
2 6 2 9 2 1 1 5
2 6 1 0 1 2 1 3
2 6 1 2 0 7 0
2 6 1 4 5 2 5 0
2 6 1 9 0 2 3 2
2 6 2 2 0 2 3 6
2 6 2 5 0 1 1 2
2 6 2 7 2 2 1 5
2 6 2 8 2 2 1 1
2 6 2 8 8 1 5 6
2 6 2 9 4 1 4 2
2 6 1 0 2 1 2 3
2 6 1 3 0 1 9 0
2 6 1 7 0 2 4 4
2 6 2 0 0 2 4 3
2 6 2 3 0 1 2
2 6 2 6 0 1 1 2
2 6 2 7 5 2 1 3
2 6 2 8 4 9 5
2 6 2 9 0 17
7 9
Page 82
M ach in e C o d e M a s te r
M O D U L E 4 . 1 3
2 6 3 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 3 0 1 R E M E V A L U A T E O P C O D E
2 6 3 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 3 1 0 T 1 = 3 s T = P O
2 6 3 2 0 T = A S C < M I D * < T A * < T 1 > , T + 1 , 1 > >
2 6 3 3 0 I F 1 = 2 5 5 T H E N E R R = T R U E s R E T U R N
2 6 3 4 0 T 1 = 4 s T 2 = A S C ( M I D * < T A $ < 1 > , I N T <
T / 2 + 1 ) , 1 ))
2 6 3 5 0 IF ( 1 A N D T ) = 0 T H E N 7 2 = I N T ( T 2 / 1 6
)
2 6 3 5 5 T 2 = T 2 A N D 1 5
2 6 3 6 0 I F T 2 O 0 P T H E N 2 6 3 2 0
2 6 3 7 0 D * = 0 * + C H R * < T >
2 6 3 8 0 E R R = F A L S E
2 6 3 9 0 R E T U R N
A t this p o i n t w e ag a i n m a k e f u l l u s e o f th e n ew ta b l e s w h i c h w ere ad d e d
t o T A $ in th e firs t m o d u l e o f th e A s s e m b l e r . T h e p u r p o s e o f th e m o d u l e i s
t o m a t ch th e o p c o d e t h a t ha s b e e n o b t a i n e d w ith t h e o p e r a n d t y p e an d see
wh et h er th e y ar e in f a c t c o m p a t i b l e . I f n o t an err or m u s t b e f l a g g e d .
C o m m e n ta r y
2632 0: T has b ee n set e q u a l t o P O , th e p o s i t i o n o f th e o p c o d e m n e m o n i c in
T A $ ( 2 ) an d t h u s th e str ing e q u a t i o n in t h i s l i n e p o i n t s t o a pair o f b y te s in
T A $ ( 3 ) . T A $ ( 3 ) c o n t a i n s , f o r ea c h o f th e p o s s i b l e o p c o d e s t y p e s , th e first
o f th e p o s s ib l e b y te f o r m s t h a t th e o p c o d e c a n ta k e . T h e v a l u e is a l s o th e
firs t l in k in th e c h a i n o f p o s s i b l e b y te f o r m s o f th e o p c o d e .
2633 0: I f , o n s u b s e q u e n t i t e r a t i o n s , th e v a l u e f o u n d in th e ta b l e (T A $ ( 4 )
s u b s e q u e n tl y ) is F F h e x , t h e n th er e ar e n o m o r e f o r m s o f th e o p c o d e a v a i l a
b le an d an e rro r is f l a g g e d .
2 6 34 0- 26 35 5: H a v i n g f o u n d th e p o s s i b l e o p c o d e , it is n o w c o m p a r e d w it h
th e n ec e s s a ry a d d r es s in g m o d e in T A $ ( 1 ) . T h e a d d r e s s i n g m o d e s f o r ea c h
o p c o d e a r e s to r e d in T A $ ( 1 ) , t w o t o a ch a ra c te r . A s i n g l e ch ara ct er c a n be
u s ed t o s to re t w o n um be rs in t h e ran ge 0 - 1 5 , s i m p ly b y m u l ti p l y i n g o n e o f
th e n um ber s b y 1 6 a n d t h e n a d d i n g t h e m t o g e t h e r . T h u s th e a d dr es s in g
m o d e f o r o p c o d e o n e in th e ta b l e o f o p c o d e s w il l be f o u n d in th e fi rst c h a
r ac te r o f T A $ ( 1 ) , as w il l th e ad d r es s i n g m o d e f o r o p c o d e t w o . L in e s 26 3 5 0
an d 263 55 ex tr ac t th e n e c e s s a r y h a l f o f th e ch a ra c te r v a l u e (0-1 5 and
16 -25 5) . I f th e o p c o d e p o s i t i o n ( P O ) in th e t a b l e is o d d , th e n th e h ig h h a l f
o f th e b y te is u s ed ( T 2 / 1 6 ) a n d i f P O is e v e n th e l o w h a l f o f th e b y te is u se d
(T2 A N D 1 5 ) .
8 0
Page 83
C h a p te r 4 M a s te r c o d e A s s e m b l e r
26 360: I f th e re s u lt in g ad d r es s in g m o d e is n o t t h e s a m e as t h a t o b t a i n e d b y
e x a m in i n g th e o p e r a n d o f t h e i n s t r u c t i o n in a s s e m b l y l a n g u a g e , t h e n th e
s u b r o u t in e retur ns t o 2 6 3 2 0 a n d p ic k s u p th e n ex t p o s s i b l e f o r m o f th e
o p c o d e , t o g e t h e r w i th i ts a s s o c i a t e d ad d r es s in g m o d e a n d s o o n u n t il there
are n o fu rther f o r m s o f t h a t o p c o d e m n e m o n i c .
26 37 0: I f p ro gr a m e x e c u t i o n h a s re a ch ed th i s p o i n t it is b e c a u s e a n
ad dr es s in g m o d e h a s b ee n f o u n d in t h e ta b l e s w h i c h c o n f o r m s t o th e
f o r m a t o f th e o p e r a n d p ic k e d up f r o m t h e a s s e m b l y l a n g u a g e in s t r u c ti o n .
T h e co rr ec t o p c o d e f o r t h e o p e r a n d a n d th e o p c o d e t y p e is a d d e d t o 0 $ ,
w h i c h is b e i n g u s e d t o s to r e w h a t w i l l e v e n t u a l l y b e p la c e d i n t o m e m o r y ,
t h o u g h th is is o n l y re le v a n t o n P a s s 2.
C H E C K S U M T A B L E
2 6 3 0 0 1 2 3
2 6 3 1 0 9
2 6 3 4 0 1 6 7
2 6 3 6 0 2 4
2 6 3 0 1 2 2 4
2 6 3 2 0 1 9 1
2 6 3 5 0 8 1
2 6 3 7 0 2 3 8
2 6 3 0 2 1 2 3
2 6 3 3 0 8 7
. 2 6 3 5 5 . 8 3
2 6 3 8 0 7 0
2 6 3 9 0 1 4 2
M O D U L E 4 . 1 4
2 6 5 6 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 5 6 1 R E M B Y T E L E N G T H
2 6 5 6 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 5 6 5 A D = A D + 1
2 6 5 7 0 I F 0 P > 1 T H E N A D = A D + 1
2 6 5 8 0 I F O P > 8 T H E N A D = A D + 1
2 6 5 9 0 R E T U R N
W e f i n i s h f o l l o w i n g t h r o u g h th e w or k o f th e A s s e m b l e r o n P a s s 1 w it h
th is s h o r t m o d u l e . I t s i m p ly u s es t h e o p c o d e t y p e t o d et e rm i n e h o w m a n y
b y te s th e a s s e m b l e d i n s t r u c t i o n w i l l require w h e n it is fi n a l l y p la c e d i n t o
m e m o r y o n P a s s 2 . T h i s is in o rde r t h a t t h e va ri a b l e A D m a y b e co r re c tly
u p d a te d f o r t h e p u r p o s e o f d e f i n i n g la b e l s .
C H E C K S U M T A B L E
2 6 5 6 0 1 2 3 2 6 5 6 1 1 9 7 2 6 5 6 2 1 2 3
2 6 5 6 5 2 1 5 2 6 5 7 0 2 3 4 2 6 5 8 0 2 4 1
2 6 5 9 0 1 4 2
S E C T I O N 3 : P a s s T w o R o u t i n e s
M O D U L E 4 . 1 5
2 7 6 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 6 0 1 R E M D O P A S S 2 A S S E M B L Y
81
Page 84
M ac hin e C o d e M a s te r
2 7 6 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 6 0 5 P A S S = 2
2 7 6 1 0 0 $ = " "
2 7 6 2 0 E X I T = F A L S E : E R R = F A L S E
2 7 6 2 5 F ' T R = 2
2 7 6 3 0 G O S U B 2 8 8 5 0
2 7 6 4 0 I F N O T E R R T H E N 2 7 7 2 0
2 7 6 5 0 I F T = 5 8 A N D L E N < H $ ) = 0 T H E N 2 7 6 3 0
2 7 6 6 0 I F T ~ 5 9 O R T = - l T H E N E R R = F A L S E :
R E T U R N
2 7 6 6 5 G O S U B 2 8 7 0 0
2 7 6 7 0 G O S U B 2 8 8 5 0
2 7 6 8 0 I F N O T E R R T H E N 2 7 7 2 0
2 7 6 9 0 I F T = 5 8 A N D L E N ( H $ > * 0 T H E N 2 7 6 3 0
2 7 6 9 5 I F T = 5 9 O R T = - l T H E N E R R » F A L S E :
R E T U R N
2 7 7 0 0 E N = 3 : G O T O 2 8 0 0 0
2 7 7 2 0 I F P O > 5 5 T H E N G O S U B 2 7 2 0 0 s G O T O 2
7 7 4 5
2 7 7 2 3 T 5 = F ' T R : G O S U B 2 6 1 0 0 : 7 8 * F ' T R
: F ' T R = T 5
2 7 7 2 5 G O S U B 2 6 3 0 0 : I F N O T E R R T H E N 2 7 7 3
0
2 7 7 2 7 I F 0 P < 7 A N D O P > 3 T H E N O F ' = 0 F ' + 6 :
P T R = T 5 : G O T O 2 7 7 2 5
2 7 7 2 8 E N = 1 8 : G O T O 2 8 0 0 0
2 7 7 2 9 R E M T H I S B I T A T T E M P S T O M A T C H A B S L
O U T E A D D M O D E T O O P C O D E I F Z P H A S F A I L E D
2 7 7 3 0 G O S U B 2 6 5 6 0
2 7 7 4 0 I F N O T E R R A N D L E N ( O # ) > 0 T H E N G O S U
B 2 7 0 0 0 : P T R = 7 8
2 7 7 4 5 I F L E N (I N $ ) > F ' T R A N D N O T E X I T T H E N
2 7 6 3 0
2 7 7 5 0 R E T U R N
H a v i n g w o r k e d t h r o u g h P a s s 1 , w e n o w tu rn o u r a t t e n t i o n t o P a s s 2. A s
w ith M o d u le 4 . 3 , th i s is th e c o n t r o l m o d u l e o f th e P a s s .a n d w e sh a ll f o l l o w
th r o u g h th e P a s s in o u t l i n e b e f o r e e x a m in i n g it in d e t a i l .
C o m m e n ta r y
2 7 60 5- 27 70 0: A p a r t f r o m s e t t i n g t h e o u t p u t str in g (OS ) t o e m p t y a n d P A S S
e q u a l t o 2 , t h e s e l i n e s are si mi la r t o th e fi rst pa rt o f th e P a s s 1 m o d u l e in
th eir e f f e c t . A t e s t is m a d e f o r a n o p c o d e , i f th i s f a i l s it is as s u m e d t h a t th e
first par t o f t h e l i n e is a la b e l o r v a r i a b l e . F o l l o w i n g th i s a n o t h e r s ea r ch is
8 2
Page 85
C h a p te r 4 M a s te r c o d e A s s e m b l e r
m ad e fo r an o p c o d e an d i f th e l i n e is n o t s e t ti n g a v a ri a b l e e q u a l t o
s o m e t h i n g an d th er e is n o o p c o d e p re s e n t , err or 3 ‘ i n v a l i d o p e r a n d or
o p c o d e ’ is f l a g g e d .
27 720: I f th e o p c o d e t y p e is gre ater th a n 5 5 t h e n an as s em b le r d ir ec ti v e has
be en e n c o u n te r e d an d th e r e le v an t m o d u l e is c a l l e d up t o e v a l u a te it .
27723: T h e s u b r o u t in e at 2 6 1 0 0 is n o w ca l l e d t o e v a l u a te th e o p e r a n d .
2 7 7 25 -2 77 2 8: T h e s u b r o u t in e at 2 6 3 0 0 ex a m in e s th e m a t ch b e tw e e n th e
o p c o d e and ad d r es s in g m o d e s o far o b t a i n e d , t h e n a tt e m p ts t o m a t ch th e m
in a b s o l u t e m o d e , f l a g g i n g err or 1 8 , ‘ ad d r es s in g m o d e n o t a v a i l a b l e wi th
th is o p c o d e ’ i f th e m a t c h is n o t co r re c t.
27730: T h e b y te c o u n t e r A D is i n c re m en te d b y th is c a ll .
277 40: I f n o e rro r ha s b e e n f o u n d an d th er e is s o m e t h i n g in O S th e n th e
op er a n d pa rt o f th e i n s t r u c ti o n is a c t u a l l y e v a l u a t e d . T8 is a va ri a b le u se d
t o m o v e th e p o in te r p a s t an i n d e x su ch as ‘ . X ’ at th e en d o f th e o p e r a n d ,
s in c e th e o p er a n d e v a l u a t o r w i l l n o t s ca n p a s t th e s e . T8 w a s set in l in e
2 7 7 2 3 , w h en retur nin g f r o m th e r o u ti n e w h i c h ev a l u a te s th e o p er a n d ty p e ,
wh ich sc an s t o th e ver y en d o f th e o p e r a n d , in c l u d i n g a n y in d e x a tt a c h e d .
27 745: T hi s l in e a l l o w s m u l t i p l e s ta te m e n ts o n th e s a m e li n e t o be e v a l u
ate d.
C H E C K S U M T A B L E
2 7 6 0 0
1 2 3 2 7 6 0 1 1 0 7
2 7 6 0 5 9 1
2 7 6 2 5
2 7 6 5 0
2 7 6 7 0
2 7 6 9 5
2 6
8 8
1 8 0 2 7 6 8 0 6 9
3 8 2 7 7 0 0
/ 2 7 7 2 3 1 3 8
2 7 7 2 8
1 1
/ 2 7 7 4 0 4 6
M O D U L E 4 . 1 6
2 7 6 1 0 1 6 9
2 7 6 3 0 1 8 0
2 7 6 6 0 3 8
2 7 7 2 5 1 0 4
/ 2 7 7 2 9
2 7 7 4 5 2 5 1
2 1 3
2 4 8
2 7 6 0 2 1 2 3
2 7 6 2 0
8 7
2 7 6 4 0 6 9
2 7 6 6 5
2 7 6 9 0
2 7 7 2 0
2 7 7 2 7
2 7 7 3 0
2 7 7 5 0
1 7 4
8 8
~ r
1 7 8
1 7 6
1 4 2
27 20 G) R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 2 0 1 R E M D I R E C T I V E O P E R A N D E V A L U A T O R
2 7 2 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 2 0 5 E R R = F A L S E
2 7 2 1 0 IF P O = 6 0 T H E N G O S U B 2 8 6 0 0 : A D = R E
S U L . T
2 7 2 1 4 I F P 0 = 6 2 T H E N S Y = T R U E
8 3
Page 86
M achine C o d e M a s te r
2 7 2 1 5 I F P 0 - - 6 1 T H E N O P E N 2 , 4 : C M D 2 : P
P I N T " [ . C D ] A D D . D A T A S O U R C E C O D E L C D I
I I
2 7 2 2 0 I F P 0 = 5 9 T H E N E X I T = T R U E
2 7 2 3 0 I F P 0 > 5 8 T H E N R E T U R N
2 7 2 4 0 I F P 0 = 5 6 T H E N 2 7 3 3 0
2 7 2 5 0 R E M D B Y ? • < W R D D I R E C T I V E S
2 7 2 6 0 G O S U B 2 8 6 0 0
2 7 2 7 0 I F R E S U L . T < 0 O R R E S U L T > 6 5 5 3 5 T H E N E
N = 2 t G O T O 2 8 0 0 0
2 7 2 8 0 I F P 0 = 5 8 T H E N R E S U L T = I N T ( R E S U L T /
2 5 6 ) + 2 5 6 * ( R E S U L T - I N T ( R E S U L T / 2 5 6 ) * 2 5 6 )
2 7 2 8 1 R E M 2 7 2 8 0 R E V E R S E S H I . 2 < L O . B Y T E S
I F D I R E C T I V E I S D B Y
2 7 2 9 0 T 1 = T : G O S U B 2 7 1 0 0 : A D = A D + 2
2 7 3 0 0 I F T 1 = 3 2 T H E N G O S U B 2 8 1 5 0
2 7 3 1 0 IF T 1 = 4 6 T H E N 2 7 2 6 0
2 7 3 2 0 R E T U R N
2 7 3 3 0 R E M B Y T D I R E C T I V E
2 7 3 4 0 G O S U B 2 8 6 0 0
2 7 3 5 0 I F R E S U L T C 0 O R R E S U L T > 2 5 5 T H E N E N
= 1 : G O T O 2 8 0 0 0
2 7 3 6 0 G O S U B 2 7 1 4 0 : A D = A D + 1
2 7 3 7 0 I F T = 3 2 T H E N G O S U B 2 8 1 5 0
2 7 3 8 0 I F T = 4 6 T H E N 2 7 3 4 0
2 7 3 9 0 R E T U R N
In P a s s 1 w e e x a m in e d a m o d u l e w h i c h d e te r m i n e d a n y a c t i o n s
n ece ss ar y w h e n an a s s em b le r d ir ec t iv e w a s e n c o u n t e r e d a n d a l s o th e
le n g th o f t h e d ir ec ti ve i f it w as B Y T , W R D or D B Y . T h is m o d u l e is
simi lar ex c ep t t h a t it p e r f o r m s su ch a c t i o n s a s o p e n i n g a ch a n n e l t o
th e prin ter f o r o u t p u t , f l a g s SY t o pri nt o u t th e s y m b o l ta b l e or p la c es
th e d a ta f r o m a B Y T , W R D or D B Y d ir ec ti ve i n t o th e o u t p u t str in g ,
o $ .
C o m m e n ta r y
27 21 0: I f an O R G d ir ec t iv e is e n c o u n t e r e d , its o p e r a n d is e v a l u a t e d
and th e b y te c o u n te r A D s e t e q u a l t o th e res ult .
27 214: E nt er in g S YM in th e p ro g ra m f l a g s S Y t o pri nt th e s y m b o l
ta b l e at th e en d o f th e l i s t i n g o f s o u r c e c o d e .
27 215: E nt er in g P R T in th e p ro g ra m o p e n s an o u t p u t c h a n n e l t o th e
prin ter f o r th e li s ti n g o f s o u r c e c o d e , o t h e r w i s e o u t p u t is t o th e scr ee n .
8 4
Page 87
C h a p te r 4 M a s te r c o d e A s s e m b l e r
27 220 : E N D re s ul ts in t h e p a s s f i n i s h i n g at t h i s p o i n t .
2 7 25 0- 27 32 0: T h e v a l u e o f a t w o b y te d ir ec t i v e ( D B Y , W R D ) is o b t a i n e d
u s in g th e E x p r e s s i o n E v a l u a to r a n d th e t w o b y te s reversed i f th e d ir ec ti ve i s
D B Y . T hi s i s b e c a u s e th e r o u t i n e at 2 7 1 0 0 , w h i c h is n o w c a l l e d , p la c e s th e
t w o b y te s i n t o t h e str in g 0 $ i n L O / H I ord er . A D is i n c re m en te d b y 2 . L in e
2 7 3 0 0 sca n s p a s t a n y le a d i n g s p a c e s and t h e su b r o u t in e l o o p s b a c k t o p ick
up a n o th e r d o u b l e b y te i f a f u l l s t o p is e n c o u n t e r e d .
2 7 34 0- 27 38 0: T h e s a m e r o u t i n e as a b o v e , but f o r th e s i n g l e b y te dir ec ti v e
B Y T .
C H E C K S U M T A B L E
~ J f - y
0 0
‘ 7 * " J 7
0 5
X 2 7 2 1 5
2 7 2 4 0 7 7
2 7 2 7 0
7 ' 7 7 >
9 0 3 0
2 0
2 7 3
2 7 3 5 0 6 7
2 7 3 8 0
1 2 3 2 7 2 0 1
7 0
1 5 4
2 7 2 1 0
2 7 2 2 0 1 8 9
V 2 7 2 5 0
1 7 6
2 7 2 8 0 1 5 0
2 7 3 0 0
1 4 2
2 7 3 3 0 9 3 2 7 3 4 0 1 7 3
2 7 3 6 0
2 7 3 9 0
7 4
2 7 2 0 2 1 2 3
1 6 6 2 7 2 1 4 4 1
2 7 2 3 0
9 9 J
2 4 3 2 7 2 6 0
2 7 2 8 1
2 1 9 V 2 7 3 1 0
o « = * ' * ; >
2 7 3 7 0 1 7 0
31
5 2
1 4 2
1 7 3
M O D U L E 4 . 1 7
2 7 0 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 0 0 1 R E M E V A L U A T E O P E R A N D
2 7 0 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 0 1 0 E R R = F A L S E
2 7 0 2 0 I F O P < 2 T H E N R E T U R N
2 7 0 3 0 I F 0 P = 3 T H E N 2 7 5 0 0
2 7 0 4 0 I F Q P = 2 T H E N 2 7 4 0 0
2 7 0 5 0 G O S U B 2 8 6 0 0
2 7 0 6 0 I F E R R O R L E N ( O t ) = 0 T H E N R E T U R N
2 7 0 7 0 I F ( R E S U L T C 0 O R R E S U L T > 2 5 5 ) A N D O P
< 9 T H E N E N = 1 : G O T O 2 8 0 0 0
2 7 0 8 0 I F R E S U L T S 0 O R R E S U L T > 6 5 5 3 5 T H E N E
N = 2 : G O T O 2 8 0 0 0
2 7 0 9 0 I F O F ' < 9 T H E N 2 7 1 4 0
2 7 1 0 0 T = I N T ( R E S U L T / 2 5 6 )
2 7 1 1 0 R E S U L T = R E S U L . T - T * 2 5 6
2 7 1 2 0 G O S U B 2 7 1 4 0
2 7 1 3 0 R E S U L T ■ T
2 7 1 4 0 0 $ = O ^ + C H R S ( R E S U L T )
2 7 1 5 0 R E T U R N
8 5
Page 88
M ac hin e C o d e M a s te r
T hi s m o d u l e e v a l u a te s an o p e r a n d w h o s e t y p e ha s a lr ea d y b ee n
d et e rm in ed , p la c i n g th e re su lt in o n e or t w o b y te f o r m i n t o 0 $ .
C o m m e n ta r y
27020: I f O P is les s th a n t w o th e n th er e w il l b e n o o p e r a n d .
27 030: I f th e O P is 3 t h e n re lat iv e a d d r es s in g m o d e is re quired an d th e
ro u ti n e at 2 7 5 0 0 is ca l l e d u p .
27040: I f O P is t w o th e n th e a d d r e s s i n g m o d e is im m e d i a t e an d th e ro u ti n e
at 2 7 40 0 is ca l l e d u p .
2 7 05 0- 27 08 0 : F o r all o th e r v a l u e s o f O P , th e E x p r e s s i o n E v a l u a t o r is u se d
t o o b ta i n a res ult an d th is is te s te d a g a i n s t th e re qu ir em en ts o f th e
a d dre ss ing m o d e f o r 1 or 2 b y t e s .
27 09 0- 27 14 0: T h e s e a r e th e t w o r o u ti n e s th a t p la c e th e result o b t a i n e d
th ro u gh th e E x p r e s s i o n e v a l u a t o r i n t o b y te f o r m in 0 $ . N o t e th a t t w o b yt e
numbe rs ar e p la c e d i n t o 0 $ w i th th e h ig h b y t e s e c o n d .
C H E C K S U M T A B L E
2 7 0 0 0
2 7 0 1 0
2 7 0 4 0
2 7 0 7 0
2 7 1 0 0
2 7 1 3 0
M O D U L E 4 . 1 8
1 2 3
7 0
1 8
1 4
1 1 7
3 7
2 7 0 0 1 4 7
2 7 0 2 0 1 6 4 2 7 0 3 0
2 7 0 5 0 1 7 3
2 7 0 0 2
1 2 3
2 0
2 7 0 6 0 9 8
2 7 0 8 0 1 4 4 2 7 0 9 0 2 7
2 7 1 1 0
2 7 1 4 0
2 4 8
1 2 1
2 7 1 2 0 1 7 1
2 7 1 5 0 1 4 2
2 7 5 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 5 0 1 R E M E V A L U A T E R E L A T I V E E X P R E S S I O N
2 7 5 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 5 1 0 G O S U B 2 8 6 0 0
2 7 5 2 0 I F L E N ( 0 $ ) = 0 O R E R R T H E N R E T U R N
2 7 5 3 0 R E S U L T = R E S U L T - A D
2 7 5 4 0 I F R E S U L T C 0 T H E N R E S U L T = R E S U L T + 2
5 6
2 7 5 6 0 IF R E S U L T S 2 5 6 A N D R E S U L T > = 0 T H E N 2
7 1 4 0
2 7 5 7 0 E N = 1 0
2 7 5 8 0 G O T O 2 8 0 0 0
T hi s m o d u l e e v a l u a te s th e o p e r a n d o f an in s t r u c t i o n u s in g re la ti v e
ad dr es s in g, ie w h er e a j u m p is s p e c i f i e d f r o m th e curr en t addre ss up t o 1 2 7
p o s i t i o n s p o s i t i v e l y in th e m e m o r y or 1 2 8 n e g a t i v e l y .
8 6
Page 89
C h a p t e r 4 M a s te r c o d e A s s e m b l e r
C o m m e n ta r y
27 530: B e c a u s e w e ar e t a l k i n g a b o u t a rela ti v e ad d r es s , a j u m p is s p e c i f i e d
first o f all as b e i n g t o an ad dr es s a n d t h e n th e rela ti v e j u m p i s c a l c u l a t e d b y
s u bt ra ct in g t h e curr ent a d d r e s s , re co rd ed in A D .
27 540: N e g a t i v e j u m p s c a n n o t b e p la c e d str ai g h t i n t o t h e m a c h i n e c o d e
p ro g ra m , th e y m u s t b e tr a n s f o r m e d i n t o w h a t is k n o w n a s ‘ t w o ’ s c o m p l e
m e n t ’ f o r m , wh er e a n y n e g a t i v e v a l u e h a s 2 5 6 a d d e d t o i t , s o t h a t it e n d s u p
a s a p o s i t i v e nu m be r i n th e ra n g e 12 8 -2 55 . T h u s j u m p s w i th a v a l u e a b o v e
1 2 8 are in f a c t n e g a t i v e j u m p s a n d th eir v a l u e c a n be o b t a i n e d b y su b t ra c
ti n g 25 6 .
C H E C K S U M T A B L E
2 7 5 0 0 1 2
2 7 5 1 0 1 7 :
2 7 5 4 0 7 5
2 7 5 0 1 1 7 8
2 7 5 2 0 9 8
2 7 5 6 0 3 2
2 7 5 0 2 1 2 3
2 7 5 3 0 2 2 4
2 7 5 7 0 2 3 0
2 7 5 8 0 1 6 :
M O D U L E 4 . 1 9
2 7 4 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 4 0 1 R E M E V A L U A T E I M M E D I A T E E X P R E S S I O N
2 7 4 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 7 4 1 0 T 5 = P T R : G O S U B 2 6 0 0 0
2 7 4 2 0 I F A S C ( H * > < > 3 5 T H E N 2 7 4 8 0
2 7 4 3 0 I F MID$(H*,2,1)=“ T H E N 2 7 4 5 0
2 7 4 4 0 P T R = T 5
2 7 4 4 2 I F P T R > L E N ( I N $ ) T H E N 2 7 4 4 6
2 7 4 4 4 I F A S C ( M I D $ < I N * , P T R , 1 ) ) < > 3 5 T H E N P
T R = P T R + 1 s G O T O 2 7 4 4 2
2 7 4 4 6 O P = 8 : G O S U B 2 7 0 5 0 : O P = 2
2 7 4 4 8 R E T U R N
2 7 4 5 0 R E M S I N G L E C H R . E X P E C T E D
2 7 4 6 0 I F L E N ( H $ ) < > 3 T H E N 2 7 4 8 0
2 7 4 7 0 0 $ = 0 : $ + M I D * < H : * , 3 , 1 > i R E T U R N
2 7 4 8 0 E N = 1 2
2 7 4 9 0 G O T O 2 8 0 0 0
T h i s m o d u l e o b t a i n s th e v a l u e o f a n o p e r a n d f o r an i n s t r u c t i o n i n v o l v i n g
im m e d i a t e ad d r e s s i n g , ie wh er e a register is l o a d e d d ir ec tl y w ith a v a l u e in
th e rang e 0 - 2 5 5 .
8 7
Page 90
M ac hin e C o d e M a s te r
C o m m e n ta r y
27 41 0- 27 42 0: T h e o p e r a n d is o b t a i n e d in H $ b y th e u s e o f th e r o u t i n e at
2 6 0 0 0 . I f it d o e s n o t b e g i n w ith a ‘ t h e n a n e rro r wi ll b e f l a g g e d .
27 430 : I f the s e c o n d ch ar a c te r o f t h e o p e r a n d is a s i n g l e q u o t a t i o n ma rk ,
th e n th e r o u ti n e w il l e x p e c t a s i n g l e ch ar a c te r f o l l o w i n g q u o t e , w h o s e
A S C I I v a lu e w i l l b e th e v a l u e o f t h e o p e r a n d . A s e c o n d q u o t e s h o u l d n o t be
u s e d .
274 4 2- 27 44 4: T h e s e tw o li n e s sk ip o v e r a n y sp a ce s in INS t o th e h a s h s ig n .
27446: Sin ce im m e d i a t e a d d r es s in g a l w a y s i n v o l v e s a s i n g l e b y t e , th e
ro u ti n e at 2 7 0 5 0 is u sed t o e v a l u a t e th e o p e r a n d as i f it were ad d r es s in g
m o d e 8 (indi re ct Y) an d t o p la c e th e b y t e i n t o O S . T h i s is s i m p ly a s h o r t - c u t .
274 6 0- 27 47 0: T h e s e t w o li n e s d ea l w ith s i n g l e ch a ra c te r s in q u o t e s . I f th e
le n g th o f H $ is n o t 3 th e n th e f o r m a t is i n v a l i d a n d an e rro r is f l a g g e d . I f H $
i s th ree charac ter s l o n g th e n th e m i d d l e ch ar a c te r is t a k e n as th e o n e w h o s e
A S C I I v a l u e is t o b e th e o p e r a n d .
C H E C K S U M T A B L E
2 7 4 0 0
2 7 4 1 0 1 4 4
2 7 4 4 0 1 1 3 2 7 4 4 2
2 7 4 4 6
2 7 4 6 0 1 7 4
1 2 3
4 3
2 7 4 0 1 2 2 9 2 7 4 0 2 1 2 3
, 2 7 4 2 0 2 3 0 2 7 4 3 0
15 V 2 7 4 4 4
2 7 4 4 8
2 7 4 7 0
1 4 2 2 7 4 5 0
2 0 5
2 7 4 8 0
" ? 4 3
1 6 9
1 4
2 3 2
2 7 4 9 0 1 6 3
M O D U L E 4 . 2 0
2 6 9 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 9 0 1 R E M D U M P S Y M B O L T A B L E T O S C R E E N
2 6 9 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 6 9 1 0 I F S E C 1 T H E N 2 6 9 7 5
2 6 9 1 5 P R I N T
2 6 9 2 0 F O R X = 0 T O S E - 1
2 6 9 3 0 P R I N T L E F T * ( S T * ( X ) , 6 ) T A B ( 1 0 ) ;
2 6 9 4 0 H = A S C ( M I D $ ( S T * < X > , 8 ) ) * 2 5 6 + A S C ( M I
D i M S T ' f - ( X ) , 7 ) )
2 6 9 5 0 G O S U B
1 1 0 0 0
2 6 9 6 0 P R I N T H *
2 6 9 7 0 N E X T X
2 6 9 7 5 P R I N T "C C D 3 T O T A L N U M B E R O F S Y M B O L
S
- - -
" S E
2 6 9 8 0 R E T U R N
8 8
Page 91
C h a p te r 4 M a s te r c o d e A s s e m b l e r
T hi s m o d u l e is n o t tr ul y part o f P a s s 2 : it s i m p ly o u t p u t s th e s y m b o l
ta b l e at t h e en d o f P a s s 2 i f th e S Y M d ir ec t iv e w a s pr es e nt i n th e a s s e m b l y
l a n g u a g e p ro gr am .
C H E C K S U M T A B L E
2 6 9 0 0 1 2 3
2 6 9 1 0 2 7
2 6 9 3 0 8 0
2 6 9 6 0 3 7
2 6 9 0 1
2 6 9 1 5 1 5 3 2 6 9 2 0
2 6 9 4 0
2 6 9 7 0
6
2 6 9 0 2 1 2 3
6 4 2 6 9 5 0
2 5 0
2 6 9 7 5 2 4 8
1 1 5
1 5 9
2 6 9 8 0 1 4 2
S E C T I O N 4 : T h e E x p r e s s i o n E v a l u a t o r
U p t o n o w w e h a v e h a d sev er a l r e f er en ce s t o t h e s h a d o w y b e a s t k n o w n as
t h e E x p r e s s i o n E v a l u a t o r , t a k i n g it o n tr u s t th a t th er e w a s s u c h an an im a l
a n d , m o r e i m p o r t a n t l y , t h a t it d o e s th e n e c e s s a r y j o b . It w o u l d , o f c o u r s e
b e q u i te p o s s i b l e t o w rit e an a s s e m b l e r w h i c h requ ired al l v a l u e s t o b e
s p el le d o u t in e it h er d e c i m a l o r h e x a d e c i m a l b u t a gre at d ea l o f ti m e an d
e f f o r t is s av ed i f th e u ser c a n en ter v a r i a b l e s , j u m p t o a p o s i t i o n six b yt es
a ft er a pa rti cul ar l a b e l or c a l c u l a t e b y te s b y e n te ri n g s o m e t h i n g li k e L D A
j f V A L / 2 5 6 . T h e E x p r e s s i o n E v a l u a t o r m a k e s al l o f th i s p o s s i b l e , a s y o u
w i l l d is c o v e r w h e n y o u m o v e o n t o en te r o u r m a c h i n e c o d e r o u ti n e s .
M O D U L E 4 .2 1
2 8 3 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 3 0 1 R E M E V A L U A T E L A B E L O R N U M B E R
2 8 3 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 3 2 0 G O S U B 2 8 1 5 0
2 8 3 2 5 I F T = 4 0 A N D L E N ( H $ ) = 0 T H E N G O S U B 2
8 1 5 0
2 8 3 3 0 T 1 - L E N ( H $ >
2 8 3 3 5 I F ( T = — l O R T = 3 2 O R T = 5 8 O R T = 5 9 0
R T = 4 1 O R T = 4 6 ) A N D T 1 = 0 T H E N R E T U R N
2 8 3 4 0 I F T 1 = 0 T H E N 2 8 3 9 0
2 8 3 5 0 I F A S C < H * ) < = 5 7 T H E N H = V A L ( H $ > :
G O T O 2 8 4 9 2
2 8 3 6 0 G O S U B 2 8 2 5 0 : R E M F I N D L A B E L I N S Y
M B O L T A B L E
2 8 3 7 0 I F E R R T H E N E N = 1 1 s H = 0 : G O T O
2 8 0 0 0
2 8 3 8 0 G O T O 2 8 4 9 2
2 8 3 9 0 R E M H E X , O C T A L O R B I N A R Y N U M B E R S E V
A L U A T E
8 9
Page 92
M a chi ne C o d e M a s te r
2 8 4 0 0 T 2 = T : G O S U B 2 8 1 5 0
2 8 4 1 0 I F L E N ( H $ ) = 0 T H E N 2 8 4 5 0
2 8 4 2 0 I F T 2 = 3 6 T H E N 2 8 4 7 0
2 8 4 3 0 I F T 2 = 3 7 T H E N B A S E = 2 : G O T O 2 8 4 7
0
2 8 4 4 0 I F T 2 = 3 8 T H E N B A S E = 8 : G O T O 2 8 4 7
0
2 8 4 5 0 R E M I N V A L I D L A B E L
2 8 4 6 0 H = 0 : E N = 6 : G O T O 2 8 0 0 0
2 8 4 7 0 R E M T E S T I F V A L I D N U M B E R
2 8 4 7 5 G O S U B 1 1 9 5 0
2 8 4 8 0 B A S E = 1 6 : R E M D E F A U L T B A S E
2 8 4 9 0
I F E R R T H E N H = 0 :
E N = 7 : G O T O
2 8 0 0 0
2 8 4 9 2 P T R =
P T R - 1 :
G O S U B 2 8 1 5 0
: R E M G E
T N E X T O P E R A T O R
2 8 4 9 5
C H E C K S U M T A B L E
R E T U R N
2 8 3 0 0
1 2 3
2 8 3 0 1
4 8
2 8 3 0 2 1 2 3
2 8 3 2 0 1 7 3 2 8 3 2 5 2 5 0 2 8 3 3 0 2 4 7
' 2 8 3 3 5 1 9 8 2 8 3 4 0 2 5 5
2 8 3 5 0 2 0 6
2 8 3 6 0 1 7 3 2 8 3 7 0 9 9 2 8 3 8 0 1 7 8
2 8 3 9 0
2 8 4 2 0 5 6
2 8 4 5 0 5 4
2 8 4 7 5 1 7 3
2 8 4 9 2 2 1 3
T h i s m o d u l e is t h e c o r e o f t h e E x p r e s s i o n E v a l u a t o r , it s j o b b e i n g t o
ex tr ac t th e v a l u e o f n u m b e r o r la b e l , n u m b e rs b e i n g p e r m it te d i n d e c i m a l ,
h e x a d e c i m a l , o c t a l ( b a s e 8 ) o r b in a ry ( b a s e 2 ) fo r m . T h e s u b r o u t in e returns
a v a l u e in t h e v ar ia b le H a n d t h e p er m is s i b l e ra n g e o f v a l u e s is in te g er s
f r o m 0 t o 6 5 5 3 5 .
C o m m e n ta r y
28320: th e n u m b e r or l a b e l is o b t a i n e d i n H $ .
283 25: I f t h e fi rst ch ar a c te r e n c o u n t e r e d is an o p e n b r a c k e t ‘ ( ‘ , t h e n th e
ro u ti n e is c al le d a g a i n t o o b t a i n th e a c t u a l n u m b e r.
28835: O n e n te ri n g t h e r o u t i n e a t 2 8 1 5 0 , t h e v a r i a b l e T is set t o m i n u s o n e .
I f it re main s at t h a t v a l u e t h e n n o ch ar a c te r s o f a n y s i g n i f i c a n c e h a v e b e e n
f o u n d . T h e o th e r v a l u e s f o r T i n d i c a t e t h a t a s p a c e , c o l o n , s e m i - c o l o n ,
1 4 0 2 8 4 0 0
2 8 4 3 0
2 8 4 6 0
2 8 4 8 0
2 8 4 9 5
2 4 3 2 8 4 1 0
1 5 5 2 8 4 4 0 1 6 2
1 8 8 2 8 4 7 0
2 2 1 2 8 4 9 0 5 6
1 4 2
2 4 7
9 0
Page 93
C h a p te r 4 M a s te r c o d e A s s e m b le r
c l o s e b rac k et o r fu l l s t o p h a v e b e e n f o u n d . I f a n y o f t h e s e is c o m b i n e d w i th
an H $ o f le n g th z e r o th e n th er e is n o n u m b e r o r l a b e l in t h e i n s t r u c t i o n an d
th e p ro g ra m returns f r o m t h e s u b r o u t in e .
28 34 0: I f n o n e o f t h e d el i m i te r s t e s t e d f o r i n t h e p r e v i o u s l i n e is p re s e n t i t is
as s u m e d t h a t th e n u m b e r h a s a ‘ $ \ ‘ 9 o ’ o r i n f r o n t o f i t , in d i c a t in g
h e x a d e c i m a l , o c t a l o r b in a r y a n d e x e c u t i o n g o e s t o t h e r o u t i n e w h ic h
ex tr a ct s a d e c i m a l nu m be r f r o m th e s e .
28 350: I f th e fi rst cha rac ter is a d ig i t th e n t h e V A L o f H $ is t a k e n — v a ri a
b le s m u s t t h e r e f o r e n o t b e g i n w i th a n u m b e r , s i n c e th e v a l u e o f t h e n um be r
wil l b e p ic k e d u p a n d t h e re st o f t h e v a ri a b l e n a m e i g n o r e d .
2 8 36 0- 28 38 0: I f t h e fi rst ch ara ct er is n o t a di g i t th e n w h a t h a s b e e n pic k e d
up is a s s u m ed t o b e a l a b e l a n d it is se n t t o t h e r o u t i n e at 2 8 2 5 0 i n orde r t o
o b t a i n its v a l u e .
2 8 39 0- 28 49 0: T h e p o in t e r re pr es en te d b y T h a s n o w p a s s e d a chara ct er
w h ic h is a s s u m e d at th i s p o i n t t o i n d i c a t e a d i f f e r e n t n u m b e r b a s e . T h i s
a s s u m p t i o n is n o w t e s t e d . I f a d i f f e r e n t b a s e is s p e c i f i e d , th e va ri a b le
B A S E is alt er ed t o t a k e a c c o u n t o f it a n d t h e ro u t i n e in t h e M o n i t o r w h ic h
c o n v e r ts n o n - d e c i m a l n um be rs t o d e c i m a l is c a l l e d u p . I f t h e ch aracter
in d i c a t e d b y T 2 is n o t o n e o f t h e b a s e c h a n g in g i n d i c a t o r s t h e n it i s in v a l i d
a n d th e ‘ l ab e l is n o t a l p h a n u m e r i c ’ err or is f l a g g e d . I f a d i f f e r e n t b a s e has
b ee n s p e c i f i e d b u t t h e re p r e s e n ta ti o n is in c o rr e ct (eg b in a r y 10 1012) th e n
an e rro r ‘ in c o rr e ct n um be r b a s e ’ w il l b e f l a g g e d a ft er t h e re turn f r o m th e
r o u ti n e at 11950.
28 49 2: T h e m a i n p o i n t e r , P T R , w h i c h is i n d i c a t in g th e char ac te r a ft er th e
en d o f th e curr en t o p e r a t o r , is n o w b a c k e d up o n e i n o rd er t h a t th e
p ro ce du re m a y b e re -e x e c u te d o n t h e rest o f t h e li n e .
M O D U L E 4 . 2 2
2 8 5 0 0 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 5 0 1 R E M E V A L U A T E T E R M W I T H * O R /
2 8 5 0 2 R E M * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 8 5 1 0 B O S U B 2 8 3 0 0 : T E R M = H
2 8 5 2 0 IF P T R > L E N ( I N * > T H E N R E T U R N
2 8 5 3 0 IF T = 4 2 T H E N G O S U B 2 8 3 0 0 s T E R M =
I N T < T E R M * H > s G O T O 2 8 5 2 0
2 8 5 5 0 I F T< > 4 7 T H E N R E T U R N
2 8 5 6 0 G O S U B 2 8 3 0 0
2 8 5 7 0 IF H = 0 T H E N T E R M - 0 : E N = 1 5 : G
O T O 2 8 0 0 0
2 8 5 8 0 T E R M = I N T ( T E R M / H )
2 8 5 9 0 G O T O 2 8 5 2 0
91
Page 94
M ac hin e C o d e M a s te r
O n e o f th e p r o b le m s o f e v a l u a t i n g an e x p r e s s io n is t h a t o f p r e c e d e n c e , ie
wh ich pa r t o f an e x p r e s s io n su ch a s A * B / C + D / E * F m u s t be e v a l u a te d
firs t. T h e E x p r e s s i o n E v a l u a t o r ca n d ea l w it h th e p r e c e d e n c e b e tw e e n ‘ + ’ ,
and 7 * bu t n o t w it h p r e c e d e n c e fo r c e d b y th e u se o f b ra ck et s .
Bracket s w o u l d a n y w a y m a k e it m o r e d i f f i c u l t t o a s s es s th e o p e r a n d ty p e .
T his particu lar m o d u l e d ea ls w it h th e t w o hig h p re c e d e n c e o p e r a t i o n s ,
m u lt ip ly and d iv i d e .
C o m m e n ta r y
28510: A v a l u e is p ic k e d up u s in g th e p r e v i o u s m o d u l e an d s to r ed in th e
va ria bl e T E R M .
28530: I f th e charac ter p o i n t e d t o b y T is a m u l ti p l y si gn th e n th e n ex t v a l u e
i s i m m e d i a te l y p ic k e d up an d m u l ti p l i e d b y T E R M .
28 5 50 -2 85 80 : I f th e ch ara ct er i n d i c a t e d b y T is a V ’ , re pr es en tin g a d iv i s i o n
s i g n , th en a te s t i s m ad e th a t th e di v i s o r i s n o t ze ro — i f it is th e ‘ d i v i s i o n b y
z e r o ’ is f l a g g e d . T E R M is n o w d iv i d e d b y th e v a l u e j u s t o b t a i n e d in H .
C H E C K S U M T A B L E
2 8 5 0 0 1 2 3
2 8 5 1 0 1 5 0
2 8 5 5 0 6 7
2 8 5 8 0 9 3
M O D U L E 4 . 2 3
2 8 5 0 1 2 0
2 8 5 2 0 1 5 0
2 8 5 6 0 1 7 0
2 8 5 9 0 1 7 0
2 8 5 0 2 1 2 3
2 8 5 3 0 1 6 2
2 8 5 7 0 1 5 2
2 8 6 0 0 R E l v i * • * • * ¥ . ■ * - » • # • * * * ■ *
2 8 6 0 1 R E M E V A L U A T E E X P R E 8 S I 0 N
2 8 6 0 2 R E L ' M * * * * * • * : * * * * * ¥ .■ * : • * *
2 8 6 0 5 E R R = = F A L S E
2 8 6 1 0 G O S U E * 2 B 5 0 0 : R E S U L T • • = T E R M
2 8 6 2 0 I F T -
.
1 O R T = 3 2 O R T = 5 8 O R T = 5 9 O R 7
= 4 1 O R T = 4 6 O R P T R > L E N < I N $ ) T H E N R E T U R N
2 8 6 3 0 IF T — 4 3 T H E N G O S U B 2 8 5 0 0 . : R E S U L T
= I N T ( E E S U L T + T E R M ) : G O T O 2 8 6 2 0
2 8 6 4 0 IF T = 4 5 T H E N G O S U B 2 8 5 0 0 : R E S U L T
= I N T ( R E S U L T - T E R M ) : G O T O 2 8 6 2 0
2 8 6 5 0 R E S U L T = 0 : : E N = 4 : G O T O 2 8 O 0 0
R E A D Y
T hi s m o d u l e e v a l u a te s th e l o w e r p r e c e d e n c e o p e r a t io n s ad d and
su bt rac t.
9 2
Page 95
C h a p te r 4 M a s te r c o d e A s s e m b l e r
C o m m e n ta r y
2861 0: T h e m o d u l e d o e s n o t d ir ec t ly ca ll th e m a i n m o d u l e a t 2 8 3 0 0 , bu t
r ath er it ca ll s th e h ig h p r e c e d e n c e o p e r a t o r m o d u l e . T h i s en su res th a t
b e f o r e a n y v a l u e s a r e retu rned t h e y h a v e b e e n t e s t e d t o se e w h et h er th e y
s h o u l d firs t h a v e b ee n d iv i d e d o r m u l ti p l i e d b y s o m e t h i n g e l s e . T h u s i f th e
e x p r e s s io n be i n g e v a l u a t e d were A * B + C , A * B w o u l d b e e v a l u a te d b e f o r e
th e result was re turned t o th i s m o d u l e .
28620: I f o n e o f th e d e l i n e a t o r s is e n c o u n t e r e d a ft e r th e o p e r a n d th e n there
is n o t h i n g m o r e t o e v a l u a t e .
2 8 6 3 0- 2 86 40 : I f a p lu s or m i n u s s ig n is in d i c a t e d b y T as f o l l o w i n g th e v a l u e
o b t a i n e d s o far t h e n th e a p p r o p r ia t e c a l c u l a t i o n is m a d e .
2865 0: I f th e ch ara ct er i n d i c a t e d b y T is n eit h er a p lu s n o r a m in u s sig n
(m u l ti p l y or d iv i d e w o u l d h a v e b e e n d e a l t w it h b y th e p r e v i o u s m o d u le )
th en th e ‘ in v a l i d o p e r a t o r ’ err or is fl a g g e d .
C H E C K S U M T A B L E
2 8 6 0 0 1 2 3 2 8 6 0 1 5 4 2 8 6 0 2 1 2 3
2 8 6 0 5 7 0 2 8 6 1 0 4 7 2 8 6 2 0 5
2 8 6 3 0 2 2 6 2 8 6 4 0 2 2 9 2 8 6 5 0 81
S u m m a r y
I t ’ s fi n i sh e d ! O r at l e a s t i t ’ s en te r e d . W h et h er y o u h a v e th e he art t o g o o n
an d d e v e l o p th e M a s t e r c o d e p ro g ra m fu rt he r d e p e n d s o n y o u r o w n
s ta m i n a — it is o n e o f th e la rg es t si n g l e p ro g ra m s ev er p u b l i s h e d in b o o k
fo r m f o r a p o p u l a r m i c r o . In th e res t o f th e b o o k w e e x a m in e a se ries o f
m a c h i n e c o d e r o u ti n e s w h i c h c a n be en te re d u s in g th e A s s e m b l e r . I f y o u
h a v e o th e r b o o k s o n 6 5 0 2 p r o g r a m m i n g t h e n y o u m a y f i n d in th e m u s e f u l
s u b r o u t in e s w h i c h y o u c a n en te r. It is n o t a b a d i d e a t o ente r o n e or t w o
s m al l ro u ti n es b e f o r e y o u g o o n t o e x t e n d i n g th e 6 4 * s B A S I C w i th t h e re st
o f t hi s b o o k , i f o n l y t o f a m il i a r i s e y o u r s e l f w i th th e w o r k i n g o f th e
p ro gr am . T h e u s u a l w o r d o f c a u t i o n a p p l i e s , h o w ev e r: d o m a k e sure th a t
th e p ro gra m is sa v ed b e f o r e y o u try t o d o a n y th in g w ith m a c h i n e c o d e .
A n y o n e ca n m a k e a m i s t a k e — a n d reg re t it i f th e p ro g ra m is l o s t .
9 3
Page 96
Page 97
P a r t 2
9 5
Page 98
Page 99
C H A P T E R 5
T h e B A S I C E xt e nd e r
In order t o a c h i e v e o u r a im o f pr a ct ic al m a c h i n e c o d e r o u ti n e s t o ex t e n d
th e B A S I C l a n g u a g e o n th e 6 4 it is first o f all n e c e s s a r y t o u n d er s ta n d a
l it tl e h o w B A S I C a c t u a l l y w o r k s . H o w is a n o r m a l B A S I C c o m m a n d
p ic ke d up an d ac t ed u p o n , le t a l o n e th e c o m m a n d s w e w i s h t o add t o th e
la n g u a g e .
C o n s i d e r fir st a s tr a ig h tf o r w a r d B A S I C c o m m a n d s u ch as a l i n e re adi ng
1 G O T O 1 0 . W h e n y o u press R E T U R N t o ente r t h e l i n e , it is sc a n n e d b y th e
B A S I C int erpreter a n d th e f a c t th a t it c o n t a i n s a B A S I C k ey w o r d is
d e te c te d . T hi s k ey w o r d is t h e n ‘ c r u n c h e d ’ , th a t is t o s a y sh ru n k d o w n t o a
s in gl e b y te in th e p ro g ra m f i l e . A l l th e B A S I C k e y w o r d s h a v e s u c h b y t e s , or
‘ t o k e n s ’ , wi th v a l u e s in t h e ra n g e 12 8-2 02 (p lu s 255 f o r P I ) . In t h e c a s e o f
G O T O th e t o k e n is 1 37 .
W h en th e p ro g ra m c o n t a i n i n g th i s li n e is R U N th e B A S I C interpreter
s can s th e l i n e , s k ip p i n g th e l i n e n um be r an d fi n d s th e t o k e n , w h i c h it r e c o g
nis es as s uch s in c e it is a b o v e 1 2 8 an d n o t c o n t a i n e d w i t h i n q u o t e s . T h e
t o k e n i n d i c a t e s a p o s i t i o n in a ta b l e an d at t h a t p o s i t i o n is t h e add re ss o f a
m a c h i n e c o d e r o u ti n e w h i c h w i l l e x e c u t e th e c o m m a n d y o u se e as G O T O
1 0 . T h e int erpreter n o w e x e c u te s th i s m a c h i n e c o d e r o u ti n e w h i c h firs t
s can s th e li n e f o l l o w i n g t h e G O T O t o k e n f o r a li n e n u m b e r . H a v i n g
o b t a i n e d th is f r o m th e B A S I C l i n e t h e res t o f th e m a c h i n e c o d e r o u t i n e f o r
this parti cu la r c o m m a n d is d e v o t e d t o f i n d i n g t h e p art icu la r l i n e n um be r
ref erred t o an d al te r in g a n u m b e r o f s y s te m v a r i a b l e s s o th a t pr o g ra m e x e
c u t i o n j u m p s t o th a t p o i n t . I f n o li n e n um be r is f o u n d a l o n g s i d e th e G O T O
th en a s y n ta x err or is i n d i c a t e d . I f t h e l i n e n u m b e r is f o u n d b u t is n o t in th e
pro g ra m th en an u n d e f i n e d li n e err or is g i v e n . A s s u m i n g th a t ev e ry th in g
has g o n e a c c o r d in g t o p l a n , c o n t r o l o f th e p ro g ra m n o w returns t o t h a t part
o f th e B A S I C interpr ete r w h o s e j o b is t o s ee k o u t th e n e x t t o k e n in th e
pro g ra m .
F ro m all th is w e lea rn th a t a n um be r o f a c t i o n s a re n ec e s s a ry f o r th e
e x e c u t i o n o f a B A S I C k ey wo rd :
1 ) T h e int erpreter m u s t r e c o g n is e it as a k e y w o r d a n d b e a b le t o cr unc h it
d o w n t o th e f o r m o f a t o k e n .
2) T h e int erpreter m u s t b e a b le t o r e c o g n is e t h e t o k e n o n c e th e p ro g ra m is
ru n.
3 ) T he re m u s t b e a t a b l e in th e m e m o r y s o m e w h e r e f r o m w h i c h th e i n t er
9 7
Page 100
M ac hin e C o d e M a s te r
pr e te r can draw th e sta rt ad dr ess o f a m a c h i n e c o d e r o u ti n e t o p e r f o r m th e
c o m m a n d .
4) T h e m a c h i n e c o d e r o u ti n e m a y h a v e t o b e a b le t o p ic k u p furthe r i n f o r
m a t io n fo r th e c o m m a n d (eg th e ‘ 10 ’ f o r G O T O 1 0 ) .
5 ) T here m u s t b e p r o v i s i o n t o r e c o g n is e an d n o t i f y e rro rs w h ic h pr ev e nt th e
e x e c u t i o n o f th e r o u ti n e .
H a v i n g R U N th e p ro g ra m o n e m o r e re qu ir em en t is d is c o v e r e d w h e n it is
L I S T e d . I t is n o u s e tr yi n g t o pri nt o u t th e t o k e n f o r th e k e y w o r d . T h e
in terpreter m u s t a l s o h a v e a t a b l e w h i c h a l l o w s it t o l o o k up th e w o r d w h ic h
co r r e s p o n d s t o th e t o k e n s o t h a t it m a y b e p rin te d in a l i s t i n g o f t h e p r o
gram .
In order t o ente r n e w k e y w o r d s w e m u s t ta k e a c c o u n t o f all t h e s e
re qu ire m en ts. Ou r k e y w o r d s m u s t b e p la c e d i n t o th e i nt erp re ter , t o k e n s
m u st be s p e c i f i e d fo r t h e m , r o u ti n e s t o e x e c u t e th e m m u s t b e p r o v i d e d a n d ,
m o s t i m p o r ta n t o f a l l , th e i nt erp et er m u s t b e p er s u a d ed t o re c o g n is e and
act u p o n th e i n f o r m a t i o n g i v e n . A l l th is w il l cle a r ly i n v o l v e alte rin g th e
B A S I C interpreter an d th a t i t s e l f p res en ts th e first p r o b le m s i n c e , as y o u n o
d o u b t a lre ad y k n o w , th e int erpreter is n o t in a par t o f m e m o r y th a t w e can
c h o o s e t o alt er ( R A M ) bu t rath er in c u s t o m - b u i l t ch i p s w h o s e c o n t e n t s a r e
fi x e d at th e t i m e o f thei r m a n u f a c t u r e . A l l th a t i s tr ue b u t f o r t u n a t e l y it is
n o t th e w h o l e o f th e tr ut h.
W h en y o u s w itc h th e 64 o n its 6 4K o f m e m o r y is t a k e n up (r o u g h l y ) b y
8 K o f m e m o r y f o r w h a t is k n o w n as th e Ke rnal (a set o f u s e f u l m a c h i n e
c o d e ro u tin es c o m m o n t o m o s t C o m m o d o r e m a c h i n e s ) , 8 K fo r th e B A S I C
interpreter, I K f o r s y s te m v a ri ab le s ( l o c a t i o n s w h ic h th e 64 u s es t o s to r e
im p o rt a n t v a l u e s and a d dr es s es f o r its o p e r a t i o n ) , 4 K o f R A M w h ic h
c a n n o t be u s ed b y B A S I C an d 4 K f o r r u n ni n g o th er d e v i c e s such a s th e VIC
ch i p , d is c s , t a p e , pr inters e t c . H o w e v e r it i s . p o s s i b l e t o s w itc h cert ai n
s e c ti o n s o f th is m e m o r y o v e r t o user c o n t r o l ( R A M ) — in f a c t th er e is a
c o m p l e t e 64K o f R A M a v a i l a b l e , p r o v i d e d t h a t y o u a re w i l l in g t o sw itc h
eve ry th in g e ls e in th e m a c h i n e o f f , m e a n i n g n o B A S I C , n o c o m m u n i c a t i o n
wi th th e o u t s i d e w o r ld an d s o o n .
T h e B A S I C interp re ter , a p pe ars t o o c c u p y th e 8 K o f m e m o r y fr o m
4 0 9 6 0 on w a r d s . In fa c t th e 6 4 c h e a t s b y f o o l i n g th e C P U i n t o t h i n k i n g th a t
th e ent ire ly sep ar at e B A S I C interpr ete r c h i p o c c u p i e s th a t p o s i t i o n . T h e
ac tu al 8 K o f R A M a t t h a t p o i n t is t o t a l l y u n u s e d f o r th e s i m p le r e a s o n t h e
6 5 0 2 / 6 5 1 0 ch ip ca n o n l y see 64K o f m e m o r y at o n e ti m e , s o th a t i f it is t o see
th e B A S I C int erpreter it m u s t ce a s e t o se e 8 K o f th e a c t u a l user m e m o r y .
T h e i m p o r ta n c e o f th is fo r o u r p u r p o s e s is th a t th er e is 8 K o f m e m o r y g o i n g
u n u s e d , e x a c tl y th e right a m o u n t o f s p a ce t o h o l d th e B A S I C inte rpreter i f
it were t o b e h eld in R A M a n d n o t in R O M , a n d in e x a c t l y th e p la c e wher e
th e C P U w o u l d ex p e c t t o f i n d th e B A S I C in terp reter.
Ou r first ste p in alter in g B A S I C , t h e r e f o r e , is t o c o p y th e c o n t e n t s o f th e
B A S I C interpreter i n t o th a t are a o f R A M . T h i s ha s its d ra w b a ck s — th e
9 8