194
C O V E R S C O M M O D O R E B A S I C , V E R S I O N 7 . 0 !

Commodore 128 BASIC Programming Techniques

Embed Size (px)

Citation preview

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 1/194

C O V E R S

C O M M O D O

B A S I C ,V E R S I O N7 . 0 !

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 2/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 3/194

C O M M O1 2

B A SP R O G R A

T E C H N IM A R T I NH A R D

T A BT A BB O O K SI n c .

B l u e R i d g eS u m m i t ,P A 1 7 2 1 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 4/194

T h i sb o o ki s d e d i c a t e dt o m ym o t h e r ,w h ow i l ln e v e r u n d e r s t a n d c o m p u t e r s ;

t o m yf a t h e r ,w h oi s t r y i n g ;a n dt o m yw i f e ,J a n ,w h or o u t i n e l yp u t su p w i t h f o u r o rf i v ei n t h eh o u s ea t o n et i m e .

C o m m o d o r e ,C o m m o d o r e6 4 a n dC o m m o d o r e1 2 8a r et r a d e m a r k so f C o m m o d o r eB u s i n e s sM a c h i n e s .

A p p l ei s a t r a d e m a r ko f A p p l e C o m p u t e r ,I n c .

I B Mi s a t r a d e m a r ko f I n t e r n a t i o n a lB u s i n e s sM a c h i n e s .

C P M+ i s a t r a d e m a r ko f D i g i t a lR e s e a r c h ,I n c .

F I R S TE D I T I O N

F I R S T P R I N T I N G

C o p y r i g h t© 1 9 8 6b y T A BB O O K SI n c .

P r i n t e di n t h eU n i t e dS t a t e s , o fA m e r i c a

R e p r o d u c t i o no r p u b l i c a t i o no f t h e c o n t e n ti n a n y m a n n e r ,w i t h o u te x p r e s s

p e r m i s s i o no f t h ep u b l i s h e r ,i s p r o h i b i t e d .N ol i a b i l i t yi s a s s u m e dw i t hr e s p e c tt o

t h eu s eo f t h ei n f o r m a t i o nh e r e i n .

L i b r a r y o fC o n g r e s sC a t a l o g i n gi n P u b l i c a t i o nD a t a

H a r d e e ,M a r t i n .

C o m m o d o r e1 2 8B A S I C .

I n c l u d e si n d e x .

1 . C o m m o d o r e1 2 8 ( C o m p u t e r ) — P r o g r a m m i n g .

2 . B A S I C( C o m p u t e r p r o g r a ml a n g u a g e )I . T i t l e .

Q A 7 6 . 8 . C 6 4 5 H 3 71 9 8 6 0 0 5 . 2 6 5 8 6 - 1 4 3 7 3I S B N0 - 8 3 0 6 - 0 4 3 2 - 4

I S B N0 - 8 3 0 6 - 2 7 3 2 - 4( p b k . )

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 5/194

C o n t e n t s

I n t r o d u c t i o n v i i

1 A R e f r e s h i n gA p p r o a c ht o B A S I C 1

A M a g i c i a n ' sT r a d e1A W a l kD o w nM e m o r yL a n e 2

W h a t M a k e s B A S I CD i f f e r e n to n t h eC - 1 2 83U s i n gD O / U N T I LI n s t e a do f F O R. . . N E X T — F o r m a t t i n gN u m b e r s — I n s i d ea S t r i n g

W h a t ' sA h e a d5

2 W h a t Y o uH a v et o W o r kW i t h 7

T h eT h r e eS i d e so f t h eC o m p u t e r7

T h e6 4 S i d e — T h e1 2 8S i d e — C - 6 4 / C - 1 2 8C o m p a t i b i l i t y — C P / MP l u s

C o m m o d o r e1 2 8M e m o r y9W h a tM e m o r ya n d P o w e rM e a nt o P r o g r a m m e r s — V a r i a b l eM e m o r y — AB e t t e rT y p eo f M e m o r y

G r a p h i cS i d e so f t h eC o m p u t e r1 1

T h e4 0 - C o l u m nS i d e — T h e8 0 - C o l u m nS i d e — S w i t c h i n gB e t w e e nM o d e sE d i t i n gP r o g r a m o nt h eC o m m o d o r e1 2 8 1 4

G r a p h i c sS y m b o l s a n dU p p e r -a n dL o w e r c a s e L e t t e r s — E s c a p eC o m m a n d sU s e df o rE d i t i n g — H o wt o M a k ea L i s t i n gP a u s e — S p l i t t i n ga n dD u p l i c a t i n gP r o g r a mL i n e s — C l e a r i n gt h eS c r e e nf r o mt h e

K e y b o a r d — C h a n g i n gC o l o r s — R e s t o r i n gt h eS c r e e n

3 A Q u i c kT o u ro f D O SC o m m a n d s 2 3

P r e p a r i n g D i s k sf o rU s e 2 3

W h e nt o H E A D E Ra D i s kf r o mB A S I C — E n t e r i n gt h eH e a d e rC o m m a n dM a k i n gE x t r aC o p i e s2 5

S e e i n gW h a t ' so n a D i s k :D I R E C T O R Yo r C A T A L O G2 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 6/194

P r i n t i n ga D i r e c t o r y — T r i c k sw i t ht h eD I R E C T O R YC o m m a n dS a v i n gP r o g r a m so n D i s k 2 7

R e p l a c i n gE x i s t i n gP r o g r a mF i l e s — R e p l a c i n gF i l e so n A l m o s t - F u l lD i s k s

L o a d i n ga P r o g r a m 2 8

R u n n i n gO t h e rF i l e s2 9

D i s kD r i v e sa n dO t h e r D e v i c e s2 9

T w oU n i t s ,T w oZ e r o s — A c c e s s i n gD e v i c e s

S e n d i n gI n f o r m a t i o nt o D i f f e r e n tD e v i c e s3 1

U s i n gD u p l i c a t eF i l ea n dD e v i c eN u m b e r s — S e n d i n gF i l e st o D i s kD r i v e s — S e n d i n gF i l e st o t h eS c r e e n

4 S e a r c h i n gf o rI n f o r m a t i o n 3 5

H o wt o S e a r c h3 5

D i n n e ra t t h eC o m m o d o r eI n n 3 6

D a t aE n t r y — P u t t i n gt h eD a t ai n t oa n A r r a y — T r i c k sw i t ht h eA r r a y

I m p r o v i n gt h eS e a r c hR o u t i n e3 9

T h eE a s i e s tK i n do f T e s t :T r u e / F a l s e — A c c e n t u a t i n gt h eN e g a t i v e

S e a r c h i n gf o rR e l a t e dI n f o r m a t i o n4 1

S e a r c h i n g :W h a tt h eC o m p u t e rK n o w s4 2C o p i n gw i t hE x t r a - L o n gI F s

S e a r c h i n gf o r P a r t i a lI t e m s4 4

T o w a r dF a s t e rS e a r c h e s4 6

S e a r c h i n gf o r P a r t i a lM a t c h e s — D i f f i c u l t i e sw i t hB i n a r yS e a r c h e s

5 S t o r i n gY o u rD a t a 5 1H o wD i s kF i l e sW o r k5 1

O p e n i n g aD a t aF i l e 5 2

S t o r i n gD a t ai n a F i l e 5 3R e a d i n gI n f o r m a t i o nf r o ma F i l e 5 5

S e t t i n gY o u rR u l e sa n dS t i c k i n gt o T h e mT e l l i n gY o u rP r o g r a mW h a t ' si n a F i l e 5 6

A d d i n gI n f o r m a t i o nt o t h eE n do f F i l e s :A P P E N D5 8

W h e nN o t t oU s eA P P E N DC o m b i n i n gS e v e r a lF i l e s :T h eC O N C A TC O M M A N D5 9S o m eB a dN e w sA b o u tI N P U Ta n d aS o l u t i o n6 0

I n p u t t i n gw i t hQ u o t e s — A n o t h e rC u r v ei n t h eR o a d — U s i n gt h eG E T #C o m m a n d

6 R e l a t i v e( R a n d o mA c c e s s )F i l e s 6 6W h a tA r eR e l a t i v eF i l e s 6 6

O p e r a t i o n a lD i f f e r e n c e s

P l a n n i n gR e l a t i v eF i l eR e c o r d s6 7 *

P l a n n i n g I t e m si n a R e c o r d — R e c o r dS i z e — T h eN u m b e ro f R e c o r d s

A P r a c t i c a lE x a m p l e7 0O p e n i n gA s s i g n m e n t s — O p e n i n gt h e F i l e — C r e a t i n gB l a n kR e c o r d sA u t o m a t i c a l l y — T h eN u m b e ro f R e c o r d s — W r i t i n gt o t h eF i l e — T h eW r i t eR o u t i n e

R e a d i n g f r o mt h e F i l e 7 7A Q u i c k a n dD i r t yI n p u tR o u t i n e — O t h e rR o u t i n e sR e l a t e dt o I n p u t

G o i n gF u r t h e r8 2

7 P r o f e s s i o n a lI n p u tR o u t i n e s 8 3

O t h e rW a y st o I n p u tD a t a 8 3T h eL i m i t a t i o n s o ft h eI N P U TC o m m a n d — T h eS i l v e rL i n i n g

A C u s t o m i z e dI n p u tR o u t i n e8 4S e t t i n gU pV a r i a b l e s — H o wt h eE n t r yR o u t i n eW i l lW o r k — S c r e e n i n gC h a r a c t e rE n t r y — T h e B a c k

s p a c e a n dO t h e rR o u t i n e s - T i m eT r a v e l — W h i s t l eW h i l eY o uW a i t

C o m p l e t eL i s t i n g s9 2

U s i n gt h e E n t r yR o u t i n e sw i t hA r r a y s9 2

U s i n gt h eF u n c t i o na n dH e l pK e y s 9 6D e f i n i n g t h eP r e v i o u sE n t r y — R e a s s i g n i n gt h eH E L PK e y

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 7/194

8 S o r t i n g 9 8

W h a t ' sa S o r t 9 8

S o r t i n gT w o - d i m e n s i o n a lA r r a y s1 0 1

S o r t i n gN u m b e r s1 0 4

A F i n a lN o t eo n S o r t s1 0 4

9 P r o f e s s i o n a lP r o g r a mD e s i g n :A p p e a r a n c e 1 0 5

C r e a t i n gE n t r yF o r m s1 0 5U s i n gV a r i a b l e sw i t hC H A R — U s i n gC H A Rf o rO t h e rR e a s o n s

O l d - F a s h i o n e dM e n u s1 0 9O N . . . G O S U B

B a rM e n u s1 1 4

C o d i n gt h eB a rM e n u — R e a d i n gi n N e wI n f o r m a t i o n — T h eS c r e e nD i s p l a y — D e t e r m i n i n gW h a tK e yW a sP r e s s e d — G o i n gF u r t h e r

1 0 P r o f e s s i o n a lP r o g r a mD e s i g n :S p e e da n dR e a d a b i l i t y 1 1 9

W h a tW o r k s ,W h a tD o e s n ' t1 1 9

F O R. . . N E X TS t r u c t u r e s

M a k i n gt h eM o s to f S u b r o u t i n e s1 2 3

P r o g r a m sT h a tA r eR e m a r k a b l e1 2 4

O t h e rS p e e dH i n t s1 2 5

S p e e dU p sw i t hV a r i a b l e s — G a r b a g eC o l l e c t i o n

H o wt o T e l lI f a C h a n g eW i l lH e l p 1 2 7F a s tV e r s u s S l o w 1 2 7

1 1 P r o f e s s i o n a lD e s i g n :E r r o rT r a p p i n g 1 2 8

T h eT R A PC o m m a n d1 2 8

M a k i n gE r r o rM e s s a g e s M o r eR e a d a b l e 1 3 0

A S t e pF u r t h e r : L e t t i n gE r r o r sM a k eD e c i s i o n s1 3 0D i s kE r r o r s1 3 0

H o wt o T e l lW h e na n E r r o rO c c u r s

T e s t i n gf o rR U N / S T O P1 3 2A F i n a lN o t eo n E r r o r s1 3 5

1 2 D r a w i n gP i c t u r e s 1 3 6

A S i m p l e G r a p h i c sP r o g r a m1 3 6

I m p r o v i n g t h eP r o g r a m

S o m eN o t e sA b o u tC o l o r1 3 8

C h a n g i n gt h eS h a p eo f T h i n g s :S C A L E1 4 0

M o v i n g Y o u rP l o t t i n gP o i n t s1 4 0

1 3 A n i m a t i o n

W h a tI s a S p r i t e ?1 4 4

C r e a t i n gY o u rO w nS p r i t e s1 4 5C r e a t i n ga M o n s t e ro r a S p a c e s h i p

G e t t i n gD o w nt o W o r k1 4 5

A n i m a t i n gS p r i t e s :A V i d e oG a m eE x a m p l e1 4 6

U s i n gS P R I T Ea n dM O V S P R — U n t a n g l i n gS p r i t eC o o r d i n a t e s — A P r o g r a mE x a m p l e — S h o o t ' E mU p — B u m p sa n dC o l l i s i o n s — S o u n d ,F u e la n dA m m o — O t h e rC h a n g e sY o u C a n M a k e

1 4 M u s i ca n dS o u n d 1 5 4

M u s i ct h eE a s yW a y 1 5 4S o m eQ u i c k N o t e so n M u s i c1 5 5

P l a y i n gI n d i v i d u a lN o t e s1 5 5

T h eB l a c kK e y s

P l a y i n gi n H a r m o n y1 5 6

W h o l eN o t e s ,H a l fN o t e s1 5 7

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 8/194

D e f i n i n gI n s t r u m e n t s1 5 7V o l u m e1 5 8

M o r eo n H a r m o n y1 5 9

A B r a n dN e wI n s t r u m e n t1 6 2S o m eF i n a lN o t e s1 6 4

A p p e n d i xA : A R e f r e s h e rC o u r s ei n B A S I C 1 6 5H o wB A S I CW o r k s1 6 5

T a k i n ga N e wD i r e c t i o n1 6 6A n o t h e rW a yt o R e d i r e c tP r o g r a mC o n t r o l

B A S I Ca s a S e c o n dL a n g u a g e1 6 6

P R I N T — U s i n gV a r i a b l e st oT h e i rF u l l e s t — W o r d sW e ' r eN o t A l l o w e dt o U s e — D e c i s i o n s ,D e c i s i o n s —O Na n dO N. . . — F O R. . . N E X T — C r e a t i n gR e m a r k a b l e P r o g r a m s

M o v i n gA h e a d1 7 2

A p p e n d i xB : A C o m p l e t eR e l a t i v eF i l eP r o g r a m 1 7 3

I n d e x 1 7 9

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 9/194

I n t r o d u c t i o n

A c o m p u t e rc a nc h a n g ey o u rl i f e. . .

. . . o r i t c a ns i t i n t h ec o r n e r g a t h e r i n gd u s t .

A l lt o om a n yo f u s b o u g h to u rm a c h i n e sw i t h

h i g hh o p e s , o n l yt o h a v et h o s eh o p e s d a s h e db yc o m p l i c a t e di n s t r u c t i o nm a n u a l s ,h i g h - p r i c e ds o f t

w a r e ,a n da g e n e r a lf e e l i n go f h e l p l e s s n e s s .

W ea l lk n o wt h e r e ' sa t r e m e n d o u sa m o u n to f

p o w e ri n s i d ey o u rC o m m o d o r e1 2 8 .T h es e c r e ti s

h a r n e s s i n gi t ,a n dh a r n e s s i n gi t w i t h o u td e l v i n gi n t o

t h er e a l m so f m e m o r ys w a p s ,a s s e m b l yl a n g u a g e ,

o r c o m p l i c a t e da n dc o n f u s i n gp e e k sa n dp o k e s .

T h e r e ' sn oa s s e m b l yl a n g u a g ei n t h i sb o o k .I t ' sa l l

p u r eB A S I C .I n a d d i t i o n ,t h e r o u t i n e si n t h e s ec h a p

t e r sa r es t r u c t u r e da n dm o d u l a r i z e d ,s o t h e yc a nb e

t r a n s p l a n t e di n t oa n yB A S I Cp r o g r a m y o uw r i t e .

T h i sb o o kw i l ld i s p e lm a n yo f t h em y t h sy o uV e

h e a r da n da l l a ym a n yo f t h ef e a r sy o uh a v ea b o u t

d o - i t - y o u r s e l fp r o g r a m m i n g .C h a p t e r1 b e g i n sb y

e x a m i n i n gt h en e wc o m m a n d sa n df e a t u r e sa v a i l

a b l eu n d e rB A S I C7 . 0 .T h e s en e wc o m m a n d sw i l l

g i v ey o ua f r e s hw a yo fl o o k i n ga t t h et i r i n go l dB A

S I Cl a n g u a g e .

I n C h a p t e r2 , y o u ' l ls e eh o wt o t a k ea d v a n t a

o f t h eC o m m o d o r e ' sm e m o r ys e t u p ,e x a m i n eh o w

g r a p h i cs c r e e n s a r e h a n d l e d ,a n dl e a r na f e wt r i c k s

a b o u tt h ek e y b o a r d( i t ' sn o tq u i t ea s s i m p l ea s t y p

i n ga l e t t e r ) .C h a p t e r 3z e r o e si n o nd e v i c e s .Y o u ' l l l e a r nh o w

t o c o m m u n i c a t ew i t ht h e d i s kd r i v e s ,m o d e m s

p r i n t e r s ,m o n i t o r s ,a n do t h e rp e r i p h e r a l st h a tc a n

b e c o n n e c t e dt o t h eC o m m o d o r e1 2 8 .T h es i m p l

c o n c e p t si n t h i sc h a p t e r p r o v i d et h eb u i l d i n gb l o c k

f o rf i l eh a n d l i n g ,p r i n t i n g ,a n do t h e rc o m m o no p e r

a t i o n s .

O n eo f t h em o s ti m p o r t a n ta p p l i c a t i o n si n c o m

p u t i n gi s t h ea b i l i t yt o s e a r c hf o ri n f o r m a t i o n .C h a p

t e r4 s h o w sh o wt o d e s i g n s i m p l eB A S I Cr o u t i n e

t h a tw i l lf i n dt h e i n f o r m a t i o n y o u ' r e lf o r ,

w h e t h e ri t ' so n d i s k o ri n m e m o r y .

I n C h a p t e r5 , y o u ' l ls e eh o wt o c r e a t ep r o g r a m

t h a ts a v ea n dr e c a l ls t r e a m so f d a t at oa n d f r o md i s k

e t t e .T h em o d u l e si n t h i sc h a p t e rc a nb e e a s i l y

m o d i f i e da n di n s e r t e di n t oa n y p r o g r a mt h a tn e e d s

t o s a v ed a t a .

C h a p t e r6 f o c u s e so n r e l a t i v ef i l e s — t h ef i l i n g

v i i

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 10/194

m e t h o dm o s to f t e nu s e dt o s t o r en a m e s ,a d d r e s s e s ,

a c c o u n t i n gd a t a ,a n do t h e rb u s i n e s si n f o r m a t i o n .

I n C h a p t e r7 , y o u ' l ls e eh o wt o s c r e e na n dt r a p-

c h a r a c t e r sa s t h e ya r ee n t e r e df r o mt h ek e y b o a r d ,

g i v i n gy o uc o m p l e t ec o n t r o lo v e ro p e r a t o re n t r y .

T h e r ea r ee v e nr o u t i n e sf o rp e r f o r m i n ga u x i l i a r y

o p e r a t i o n s( p l a y i n gm u s i ca n dd i s p l a y i n g t h e t i m e )

w h i l e a w a i t i n gi n p u t .

S o r t i n g ,o n eo f t h em o s ti m p o r t a n tc o m p u t e r

c o n c e p t so fa l l ,i s d i s c u s s e di n C h a p t e r8 . T h e r e y o u

w i l lf i n d d e t a i l e de x a m p l e so f t h r e ed i f f e r e n ts o r t

i n gt e c h n i q u e s .

T h ea p p e a r a n c eo f t o d a y ' ss o f t w a r ei s r a d i c a l l y

d i f f e r e n tf r o m p r o g r a m sb e i n gw r i t t e nj u s ta f e w

y e a r sa g o .B a r m e n u s ,s c r e e n e n t r yf o r m s ,a n d

o t h e ru s e rf r i e n d l yd e v i c e sh a v eb e c o m es t a n d a r d -

i t ' ss e l d o my o u ' l ls e eq u a l i t ys o f t w a r ew r i t t e nw i t h

o u tt h e m . C h a p t e r9 s h o w sh o wt o d r e s su p y o u rp r o g r a m su s i n gt h e s ef e a t u r e s ,w i t ha l i t t l ef u s s .

B A S I C ' SA c h i l l e sh e e li s s p e e d :i f a B A S I Cp r o

g r a mi s n ' tw r i t t e np r o p e r l y ,i t c a ng r i n da l o n ga t

t h es p e e do f a c i t yb u s .C h a p t e r1 0 s h o w sh o wt o

k e e py o u rp r o g r a m sr u n n i n ga t p e a ke f f i c i e n c y ,

w h i l e a v o i d i n gs o m eo f t h ep i t f a l l st h a tc o m ef r o m

o v e r - o p t i m i z i n gp r o g r a m s( l a c ko fr e a d a b i l i t y ,e t c . ) .

C h a p t e r1 1 e x p l o r e s t h e" b u l l e t - p r o o f i

y o u rp r o g r a m s a n ds t e e r sy o ua r o u n ds o m eo f t h e

p o t h o l e s i n v o l v e di n t r a p p i n ge r r o r s .

G r a p h i c s ,t h em o s td a z z l i n g f e a t u r e o ft h eC o m

m o d o r e1 2 8 ,a r ec o v e r e di n C h a p t e r s 1 2a n d1 3 .

Y o u ' l ls e eh o wt o c r e a t e ,p o s i t i o na n dc o l o rp i c t u r e

a n dh o wt o d e s i g na n da n i m a t e s h a p e su s i n gb u i l t

i n B A S I C7 . 0c o m m a n d s .

W ee n du p w i t h ad i s c u s s i o no n m u s i ca n d

s o u n d .C h a p t e r 1 2c o n c e n t r a t e so nh o wt o p l a ym u

s i ca n dh a r m o n i e s .I t i n c l u d e sa b r i e fd i s c u s s i o

c r e a t i n gs o u n de f f e c t s .Y o u ' l la l s os e er e f e r e n c e t o

t h e C - 1 2 8 ' sm u s i c a n d s o u n dc o m m a n d st h r o u g

o u tt h et e x t .

A g a i n ,a l lo f t h ep r o g r a mr o u t i n e si n t h i sb o o k

a r es e l f - c o n t a i n e d ,s o t h e yc a ng e n e r a l l yb e m i x e

a n d m a t c h e da t w i l l .Y o u ' l ln od o u b tb eu s i n gt h e s e

c o n c e p t si n t h i sb o o ki n e v e r yn e wp r o g r a m

w r i t e .A n dy o um a yw e l lf i n dy o u r s e l fg o i n gb a c k

t o o l dp r o g r a m sa n de n h a n c i n gt h e mw i t ht h e s e

n e wf e a t u r e s .

v i i i

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 11/194

C h a p t e r1

A R e f r e s h i n gA p p r o a c ht o B A S I C

S l i c kp r o g r a m m i n gu s e dt o b e a t r i c k yb u s i n e s s .

S o u n da n dp i c t u r e s — t h ei n g r e d i e n t st h a tm a k e

e v e nb u s i n e s sp r o g r a m se n j o y a b l e — w e r en oj o ya t

a l lt o p r o d u c e .

I f y o uw a n t e dg r a p h i c s ,y o uw r o t ec o m p l i c a t e d

r o u t i n e si n a s s e m b l yl a n g u a g e — a n dt h e n s p e n t

h o u r st r y i n gt o d e b u gt h e m .G e t t i n ga f r o gt o

r e a l i s t i c a l l yd e v o u r a m o s q u i t oc o u l d t a k ed a y s .

S o u n dw a sj u s ta s c o m p l i c a t e d .E v e nt o d a y ,

g e t t i n gt w oo r t h r e ei n s t r u m e n t st o p l a yi n h a r m o n y

i s n e x tt o i m p o s s i b l eo n m a n yc o m p u t e r s .

D i s ks t o r a g e ,t h er e c a l lo f i n f o r m a t i o ns u c ha s

n a m e sa n da d d r e s s e s ,c o u l db e h a d ,b u tt h ep r i c e

w a sh o u r ss p e n t p o u r i n g o v e r c o m p l i c a t e dm a n u a l s

t h a to n l yh i n t e da t t h ep r o p e rp r o c e d u r e s .

W i t ht h ei n t r o d u c t i o no f p o w e r f u l ,l e s se x p e ns i v ec o m p u t e r sl i k et h eC o m m o d o r e1 2 8m a n yo f

t h e s ef r u s t r a t i o n sv a n i s h .F i r s t ,o p e r a t i o n st h a t

u s e dt o t a k eh o u r st o p r o g r a mb y h a n da r en o wi n

c o r p o r a t e di n t oB A S I Cc o m m a n d s ;i f y o uk n o wt h e

t r i c k s ,y o uc a nn o wd r a wa p e r f e c tc i r c l e ( o re l l i p s e ,

o r r e c t a n g l e )i n a m a t t e ro f s e c o n d s . G e t t i n ga f r o g

t o d e v o u r am o s q u i t oi s n o l o n g e rs o c o m p l i c a t e d .

I t c a nb ed o n er i g h tf r o mB A S I C ,u s i n gs p r i t e sa n d

t h ea i do f t h eC o m m o d o r e1 2 8 ' sb u i l t - i np i c t u r

e d i t o r .

A n dw h i l e d a t a - h a n d l i n g o p e r as u c ha s

n a m ea n da d d r e s ss t o r a g ea r es t i l ln o p i c n i c ,n e w

c o m m a n d sm a k et h e s eo p e r a t i o n sa g o o dd e a le a s

i e rt o c o m p r e h e n d .

A M A G I C I A N ' ST R A D E

T h e r e ' sa l o to f i l l u s i o nb u i l ti n t oc o m p u t e rp r o

g r a m s .I n s o p h i s t i c a t e dp r o g r a m s ,j u s t a s i n

s o p h i s t i c a t e dm a g i ct r i c k s ,m o s to f t h ew o r kt a k e s

p l a c eo u to fv i e w .O n c ey o uk n o wt h es e c r e t s ,h o w

e v e r ,p r o g r a m m i n g ,l i k ep e r f o r m i n gn o v e l t ym a g i

t r i c k s ,i s e a s y .U n f o r t u n a t e l y ,y o uw o n ' tf i n dm o s to f t h e s es e c r e t si n y o u rm a n u a l , b e c a u s em a n u a

a r ew r i t t e nb yp r o g r a m m e r sw h oa s s u m ee v e r y o

a l r e a d yk n o w sa l l t h et r i c k s .

T h i sb o o kw i l lu n r a v e l t h es e c r e t s o fC o m m

d o r e1 2 8B A S I C .Y o u ' l l l e a r nh o wt o s t o r ea n dr e

c a l ld a t a , e l e c t r o n i c a l l ys e a r c ht h r o u g hi n f o r m a t i

d e s i g ng r e a tg r a p h i c s ,p r o d u c er o u s i n g m u s i c c h o -

1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 12/194

r u s e s ,a n dc r e a t et h ek i n do f p r o f e s s i o n a l - l o o k i n g

s o f t w a r et h a th a sb e e nd i f f i c u l tt o c o m eb y o n

r e a s o n a b l y - p r i c e dc o m p u t e r s .

W e ' l lp a yp a r t i c u l a ra t t e n t i o nt o n e wB A S I C

c o m m a n d st h a tl e tp r o g r a m sr u nf a s t e ra n dm a k e

t h e mm u c he a s i e r t ou n d e r s t a n d .A r e a st h a ti n c l u d e

t r a d i t i o n a l l yt e c h n i c a ls u b j e c t s a r e w r i t t e ni n p l a i n

E n g l i s ha n da r er i c h l yi l l u s t r a t e d .

A W A L KD O W NM E M O R YL A N E

T h e C o m m o d o r e1 2 8d o e sa l o to f t h i n g s o t h e r

c o m p u t e r sd o n ' t .E v e nw h e ni t ' sp e r f o r m i n gm u n

d a n ep r o c e s s i n gt a s k s ,i t d o e ss om o r eq u i c k l ya n d

e l e g a n t l yt h a nc o m p a r a b l em a c h i n e s .

B e f o r ew ep e e ri n t ot h ef u l lc a p a b i l i t i e so fB AS I C7 . 0 ,p e r h a p si t i s a g o o di d e at o l o o ki n t ot h e

p a s tf o ra m o m e n t .T h ep r o g r a ml i s t e di n F i g .1 - 1w o u l dr u no n m o s to f t o d a y ' sp o p u l a rm i c r o c o m

p u t e r s .L i n e s 1 0a n d2 0 a r ec o m m e n t st o e x p l a i n

w h a tt h ep r o g r a md o e s .W h e nt h ep r o g r a mi s r u n ,

l i n e3 0 a c c e p t st h eu s e r sn a m ea n ds t o r e si t i n a

v a r i a b l ec a l l e dN A $ .B e c a u s en a m e sc o n t a i nl e t t e r s ,

a d o l l a rs i g n( $ )f o l l o w st h eN A ,i d e n t i f y i n gi t a s

a s t r i n gv a r i a b l e .L i n e4 0p r i n t sa l i n es p a c eo n t h e

s c r e e n ,a n dl i n e5 0 d i s p l a y sa m e s s a g et h a t i n c o

p o r a t e st h eu s e r ' sn a m e .N o tu n t i ll i n e6 0 d ow e

r e a l l yg e td o w nt o b u s i n e s s .T h eG O S U B2 0 0s t a t e

m e n ti n s t r u c t st h ec o m p u t e rt o p e r f o r ma s e p a r a t

s u b p r o g r a m ,o r s u b r o u t i n e ,w h i c hb e g i n sa t l i n e2 0 0 .

T h i ss u b r o u t i n ei s , i n f a c t ,t h e c o r eo f t h ew h o l e

p r o g r a m ,b e c a u s ei t c r e a t e sa n dp r i n t st h er a n d o m

n u m b e r s .W h e nt e nr a n d o mn u m b e r sa r eg e n e r

a t e d ,t h eF O R . .. N E X Tl o o pi s c o m p l e t e ,a n dt h e

c o m p u t e rR E T U R N Sf r o mt h es u b r o u t i n e .T h ep r o

g r a mw r a p st h i n g su pb ya s k i n gi f t h eu s e rd e s i r e s

a n o t h e rl i s t i n g .L i n e8 0a c c e p t st h e r e s p o n s e ,a n d

8 5m a k e sY $e q u a lt o t h el e f t m o s tc h a r a c t e ro f t h a t

r e s p o n s e( t r a n s l a t i n g , i ne f f e c t ,Y E Si n t oY ,a n dN Oi n t oN ) .O n l yi f t h es y s t e mr e c e i v e sa Y w i l li t

b r a n c hb a c kt o l i n e6 0 a n dp r i n tm o r en u m b e r sO t h e r w i s e ,a f a r e w e l lm e s s a g ei s d i s p l a y e da n dt h e

e n ds t a t e m e n ta t l i n e1 1 0h a l t st h ep r o g r a m .I f a n y

o f t h i ss e e m sf o r e i g n ,y o u ' l lp r o b a b l yw a n tt o s t u d y

A p p e n d i xA , " AR e f r e s h e rC o u r s ei n B A S I C . "I t ' s

l o c a t e da t t h eb a c ko f t h i sb o o k .

1 0

2 0

3 0

4 0

5 0

6 0

7 0

8 0

8 5

9 0

1 0 0

1 1 0

2 0 0

2 1 02 2 0

2 3 0

2 4 0

2 5 0

R E M P e r s o n a lR a n d o m N u m b e rG e n e r a t o r

R E M

I N P U T" W H A TI S Y O U RN A M E? " ; N A $

P R I N T

P R I N T" H E L L O" ; N A $ ; " !H E R E A R E1 0 R A N D O MN U M B E R S : "

R E M N o wG e n e r a t eN u m b e r s

G O S U B2 0 0

I N P U T" V I E WA D D I T I O N A LN U M B E R S( Y / N ) ? " ; Y $

Y $= L E F T $ ( Y $ , 1 )

I F Y $ = " Y "O RY $= " y "T H E NG O T O6 0

P R I N T" T H A N KY O UF O R A NE N J O Y A B L EG E N E R A T I O N "

E N D

R E M G e n e r a t ea n dP r i n tN u m b e r

F O R X= 1 T O 1 0R = R N D ( 0 ) * 1 0

P R I N TR

N E X T

R E T U R N

F i g .1 - 1 .P e r s o n a lr a n d o mn u m b e rg e n e r a t o r :a n e x a m p l eo f a p r o g r a mt h a tc o u l db e w r i t t e ni n m o s td i a l e c t so f B A S I C .

I t i s w o r k a b l e , b u th a r dt o r e a d .

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 13/194

1 0

2 0

3 0

4 0

5 0

6 0

6 5

7 0

8 0

8 5

9 0

1 0 0

1 1 0

1 2 0

2 0 0

2 0 5

2 1 0

2 1 5

2 2 0

2 3 0

2 4 0

2 5 0

R E M I m p r o v e dR a n d o mN u m b e rG e n e r a t o r

:

I N P U T" W H A TI S Y O U R N A M E? " ; N A M E $

P R I N T

P R I N T" H E L L O" N A M E S " !H E R E A R E1 0 R A N D O MN U M B E R S : "

R E MN o wG e n e r a t eN u m b e r sD OU N T I LY $ = " N "

G O S U B2 0 0

I N P U T" V I E W A D D I T I O N A LN U M B E R S( Y / N ) ? " ; Y $

Y $ =L E F T $ ( Y $ , 1 )

L O O P

P R I N T" T H A N KY O UF O RA N E N J O Y A B L E G E N E R A T I O N "

E N D

:

R E MG e n e r a t ea n dP r i n tN u m b e r

A T= 0 : L A S T= 1 0

D OU N T I LA T= L A S T

A T = A T+ 1

R = R N D ( 0 ) * 1 0

P R I N TU S I N G" # # ## . # # " ; R

L O O P

R E T U R N

F i g .1 - 2 .I m p r o v e dr a n d o mn u m b e rg e n e r a t o r ,a n e x a m p l eo f a p r o g r a mw r i t t e ni n C o m m o d o r e1 2 8B A S I C7 . 0 .I t i s m o r e

e l e g a n t ,e a s i e rt o r e a d ,a n di t w o r k sj u s ta s w e l l .

W H A TM A K E SB A S I C

D I F F E R E N TO NT H EC - 1 2 8

T h eC o m m o d o r e1 2 8w o u l dh a v e n ot r o u b l ea t

a l li n t e r p r e t i n gt h eB A S I Cl i s t i n gi n F i g .1 - 1 ;y o u

c o u l d t u r no n t h ec o m p u t e r ,t y p ei n t h ep r o g r a m ,

a n d b ea s s u r e dt h a ti t w o u l dr u nt h e f i r s tt i m e

t h r o u g h .T h e r ea r e ,h o w e v e r ,b e t t e rw a y st o d o

t h i n g so n t h eC o m m o d o r e1 2 8 .

T h em o s to b v i o u sc h a n g e m i g h ti n v o l v et h e

" v i e wa d d i t i o n a ln u m b e r s "s e c t i o n o ft h ep r o g r a m .

E v e nt h o u g hy o u ' r ef a m i l i a rw i t hB A S I C ,d o e s n ' t

i t s e e mc o m p l i c a t e d ?T h ep r o g r a mh a st o t e s tt h eu s e r ' se n t r ye a c ht i m et h r o u g h ,a n dt h e nm a k ea

d e c i s i o na s t o w h e t h e ro r n o tt o b r a n c hb a c kt o a

s p e c i f i cl i n en u m b e r .

I t ' sa s i f y o u rm a i lc a r r i e rk n o c k e d o nt h ed o o r

e v e r yd a y a n da s k e d" D oy o u w a n tm et o d e l i v e r

y o u rm a i lt o t h es a m ea d d r e s st o m o r r o w ,t o o ? " I n

s t e a d ,t h e p o s to f f i c eg e n e r a l l ya s s u m e sy o u w a n t

y o u rm a i ld e l i v e r e dt o t h es a m ep l a c eu n t i li t ' sn o t i

f i e dt h a ty o u ' r em o v i n g .I t ' sa m u c hs i m p l e r

r a n g e m e n t .

C o m m o d o r e1 2 8B A S I C7 . 0o f f e r sa s e t u p

s i m i l a r t ot h ep o s to f f i c e ' sc a l l e dD OU N T I L .I n

t h eC o m m o d o r e1 2 8B A S I Cr e w r i t eo f t h eR a n d o

N u m b e rG e n e r a t o ri n F i g .1 - 2 ,y o uc a ng e ta g o o d

i d e ao f h o wi t w o r k s .

T h ep r o g r a ms t i l lc h e c k sw i t ht h eu s e re a c h

t i m et o s e ei f a n o t h e rr a n d o mn u m b e rl i s t i n gi s

n e c e s s a r y ,b u tt h er e s ti s a u t o m a t i c .T h ec o m p u t

h a sb e e nt o l dt o p e r f o r ma l lo p e r a t i o n sb e t w e e nt h eD OU N T I La n dt h eL O O Pu n t i lY $i s e q u a lt o N .

I n t h i sc a s e ,t h e r e a r e o n l yt w ol i n e sb e t w e e nt h e

t w os t a t e m e n t s :T h e r ei s l i n e7 0 ,w h i c hc a l k t h

n u m b e r sd i s p l a y r o u t i n ea t l i n e2 0 0 ,a n dt h e r ei s

l i n e8 0 ,w h i c ha s k si f t h eu s e rw o u l dl i k ea n o t h

g o .T h e r ec a nb e a s m a n ys t a t e m e n t si n a l o o pa s

y o ul i k e ,b u ti t ' sm o s te f f e c t i v e t od e s i g n s m a l ll o o p

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 14/194

s o y o uc a nc l e a r l ys e e t h eb e g i n n i n ga n de n d . P R I N TU S I N G" $ # , # # # . # # " ; 1 2 3 4 .

U s i n gD O / U N T I LI n s t e a do f F O R. . . N E X T

W e ' r en o t f i n i s h e dw i t hR a n d o m N u m b e r

G e n e r a t o rj u s ty e t .T a k ea l o o ka t t h e h e a r to f t h e

p r o g r a m ,t h e" c o m p u t e a n dd i s p l a y " r o u t i n ea t l i n e

2 0 0 .F o rt h es a k eo f i l l u s t r a t i o n ,w e ' v er e p l a c e d t h eF O R . . .N E X Tl o o pw i t h a n o t h e rf o r mo f D O. . .

W H I L Es t r u c t u r e .I n t h i sc a s e ,t h ev a r i a b l eA Tk e e p st r a c ko fw h e t h e rt h ep r o g r a mi s p r i n t i n gt h e

f i r s tr a n d o mn u m b e r ,t h e s e c o n d , o r t h et h i r da n d

s o o n .A Tw o r k sl i k ea na m u s e m e n tp a r kt u r n s t i l e ,

a d d i n go n et o i t s e l fw i t he a c hp a s s .W h e nA Tf i

n a l l ye q u a l sL A S T( i nt h i sc a s e , 1 0 )t h el o o p e n d s .

G r a n t e d ,t h i sD O . . .U N T I Ll o o pc o n t a i n so n e

s t a t e m e n tm o r et h a nt h eo r i g i n a lF O R . . .N E X T

l o o pp r e s e n t e di n t h ef i r s tl i s t i n g( t h es t a t e m e n t

w h i c hb u m p su p t h ev a l u eo f A T ) ,b u tn o t i c eh o wm u c he a s i e rt h eD O . . .U N T I Ls t r u c t u r ei s t o r e a d .

I t ' sa l m o s ta s i f i tw e r ew r i t t e ni n E n g l i s h !

F o r m a t t i n gN u m b e r s

A n o t h e rc h a n g ew e ' v em a d ei s t o t h es t a t e m e n t

t h a tp r i n t st h er a n d o mn u m b e r s( l i n e2 3 0 ) .I f y o u ' v e

w o r k e dw i t hn u m e r i cv a r i a b l e so n a n o t h e rc o m

p u t e r ,y o uk n o wt h e y ' r eo f t e nd i s p l a y e dt o t h en i n t h

o r t e n t hd e c i m a lp l a c e .I t ' sg r e a tf o re n g i n e e r s ,b u t

i t g i v e sm o s to t h e rf o l k sa c a s eo f e y es t r a i n .P e op l eu s u a l l yl i k et o v i e wn u m b e r sr o u n d e dt o t w o

o r t h r e ed e c i m a lp l a c e s .

C o m m o d o r e1 2 8 B A S I C ' SP R I N TU S I N G

s t a t e m e n td o e sa l lt h er o u n d i n ga u t o m a t i c a l l y ,a n d

p r i n t st h en u m b e r ss o t h e y ' l ls t a c ku p i n t on i c en e a t

c o l u m n s :

W i t h o u tP R I N TU S I N GW i t hP R I N TU S I N G

2 . 3 4 3 2

3 . 1 2 6 31 2 1 . 9 7 2 3

2 . 3 4

3 . 1 21 2 1 . 9 7

P o u n ds i g n s( # )i n t h eP R I N T U S I N Gs t a t e

m e n tr e p r e s e n t t h en u m e r i cf o r m a td e s i r e d .Y o u

c a ne v e ni n c l u d ed o l l a rs i g n s ,c o m m a s ,a n do t h e r

s y m b o l st o m a k et h ef o r m a ts u i t a b l e f o rc h e c k sa n d

f i n a n c i a ls t a t e m e n t s :

Y o uc o u l du s et h ea b o v es t a t e m e n t ,f o ri n s t a n c e

t o d i s p l a y1 2 3 4 . 5 6 7i n a p r o p e rd o l l a rf o r m a

( $ 1 , 2 3 4 . 5 7 ) .

B A S I C7 . 0i s c h o c kf u l lo f s t a t e m e n t sl i k et h i s

t h a ta r eg u a r a n t e e dt o m a k ey o u rp r o g r a m m i n l e s s

t e d i o u sa n dy o u rp r o g r a m sm o r ep r o f e s s i o n a l .L e t ' s

t a k ea l o o ka t a n o t h e ro n e .

I n s i d ea S t r i n g

A n yp r o f e s s i o n a l l yd e s i g n e dp r o g r a m w o

s c r e e nk e y b o a r de n t r yf a rb e t t e rt h a ne i t h e ro f o u r

o r i g i n a ll i s t i n g sh a v ed o n e .W h a tw o u l d h a p p i f

a u s e ra c c i d e n t a l l yt y p e dT i n s t e a do f Y a t t h e

" V I E W A D D I T I O N A LN U M B E R S "q u e s t i o n ?O r

s i m p l yp r e s s e dr e t u r n w i t h o u t t y p i n ga n y t h i n ga t

a l l ?

I n b o t hc a s e s ,t h ep r o g r a m w o u l dd i s p l a ya

p l e a s a n tg o o d b y em e s s a g e a n de n di t s e l f .T h i ss o r t

o f u n e x p e c t e dd e f a u l ti s b e s tt o a v o i db e c a u s ei t

m a k e su s e r sf e e lt h e yc a n ' tm a k ea m i s t a k e .I f t h e y

d o ,t h e yc o u l d g e td u m p e do u to f t h ep r o g r a ma l l

t o g e t h e r .I t ' s a s i t u a t i o nt h a t m a k e su s e r s

i n s e c u r e — s o m e t h i n gt h a ts h o u l db e a v o i d e da t a l l

c o s t s !

A n o l dm e t h o df o rt a c k l i n gt h i ss o r to f p r o b

l e m w o u l dh a v e b e e nt o a d dn e wl o g i c a f t e rl i n e8 5

w h i c h w o u l ds c r e e n o u ta n yi n v a l i dr e s p o n s e :

8 0 I N P U T" V I E W A D D I T I O NN U MB E R S( Y / N ) ? " ; Y $

8 5 Y $ = L E F T $ ( Y $ , 1 )

8 7 I FY $ <> " Y "O R Y $ <> " y "O RY $ <> " N "O RY $ <> " n "G O T O8 0

W h i l el i n e8 7 i n d i s p u t a b l yr e d i s p l a y st h e q u e s t i

w h e nY $i s n ' te q u a lt o a v a l i dr e s p o n s e ,i t ' sa l i t t l e

t e d i o u s — t e d i o u sn o t o n l yt o t y p ei n o r i g i n a l l y ,b u tt o d e c i p h e rl a t e rw h e nr e v i e w i n gt h ep r i n t e dl i s t i n g

H e r e ' s ab e t t e rw a y ,a g a i n u s i n ga l o o ps t r u c

t u r ec a l l e dD O / W H I L Ea n da n e ws t a t e m e n tc a l l e d

I N S T R( i n s t r i n g ) :

7 4

7 6 D O W H I L EI N S T R ( " Y y N n " $= 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 15/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 16/194

O fc o u r s e ,t h e r e ' sa l o tm o r et h a tw ec o u l dh a v e w o u l ds a v et h er e s u l to fo u rr a n d o mn u m b e rg e n e r

a d d e dt o t h i sp r o g r a m .F o re x a m p l e , ar o u t i n et o a t i o ni n a f i l eo n d i s k .

p l a yr a n d o mn o t e s c o u l db ee a s i l yi n c o r p o r a t e d .W e I n t h er e m a i n d e ro f t h i sb o o k ,w e ' l le x p l o r

c o u l dh a v e a d d e da r o u t i n et o p r o d u c ec i r c l e sa n d t h e s ea d d i t i o n a lc o m m a n d sa n de x a m i n eh o wt o p u t

b o x e sa s a v i s u a la c c o m p a n i m e n tt o t h es o u n dr o u - t h e mt o p r a c t i c a l a p p l i c a t i o n ,

t i n e .W ec o u l de v e n h a v e a d d e da f e wl i n e st h a t

6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 17/194

C h a p t e r2

W h a tY o uH a v et o W o r kW i t h

T h eC o m m o d o r e1 2 8o f f e r sm a n yf e a t u r e s t h a t

m a k ei t a t t r a c t i v e t op r o g r a m m e r s .I t c a nf u n c t i o n

a s t h r e ed i f f e r e n tc o m p u t e r s ;i t p r o d u c e s b o t h4 0 -

a n d8 0 - c o l u m nd i s p l a y s ;i t g i v e sy o ua n a m p l e

a m o u n to f a v a i l a b l em e m o r y ;a n di t o f f e r sa h o s t

o f f e a t u r e st h a ts i m p l i f yt h e c h o r eo f p r o g r a m

e d i t i n g .

T H E T H R E ES I D E SO F T H EC O M P U T E R

A s y o u ' v e p r o b a b l ya l r e a d yf o u n do u tf r o m

y o u rm a n u a l ,a n d f r o mt h e a d v e r t i s i n gs a l e s p i t c h

t h a tp r e c e d e dy o u rp u r c h a s e , t h eC o m m o d o r e1 2 8

i s a c t u a l l yt h r e ec o m p u t e r si n o n ep a c k a g e .C o m

m o d o r el i k e s t or e f e rt o t h e s ec o m p u t e r sa s d i f f e r

e n t s i d e so f t h e m a c h i n e . F i g u r e2 - 1 l i s t st h e

a d v a n t a g e sa n dd i s a d v a n t a g e so f e a c hs i d eo f t h e

C - 1 2 8 .

T h e6 4 S i d e

T h e r e ' st h et r i e da n dt r u s t e dC o m m o d o r e6 4

s i d e ,w h i c hr u n sa l lp r o g r a m se v e rw r i t t e nf o rt h i s

v e r s a t i l ew o r kh o r s eo fa c o m p u t e r .I f y o us t a r tt h e

C o m m o d o r e1 2 8w h i l ea v i d e og a m eo r o t h e rc a r

t r i d g ep r o g r a mi s i n s t a l l e d ,t h es y s t e me n t e r s t

C - 6 4s i d eo f t h es y s t e ma u t o m a t i c a l l y .P r o g r a

f r o mc a s s e t t eo r d i s kc a nb e r u nb y t y p i n gG O6 4

f r o mt h eC o m m o d o r e1 2 8s i d e o ft h em a c h i n e ,a n d

t h e nf o l l o w i n g t h e l o a d i n gp r o c e d u r eo u t l i n e di n t h e

p r o g r a m ' sm a n u a l .A l lo f t h e s ep r o g r a m sw o r ke x

a c t l ya s t h e yw o u l do na C o m m o d o r e6 4 .T h es a m

i s t r u ef o rt h eB A S I Ca n da s s e m b l yl a n g u a g ep r o

g r a ml i s t i n g sy o us e ei n m a g a z i n e sa n db o o k s .

B u tt h eC o m m o d o r e6 4d o e s n ' t g i v em u c hb a n g

f o rt h eb u c kw h e ni t c o m e st o B A S I Cc o m m a n

I n f a c t ,o n c ey o ub e c o m ea c c u s t o m e dt o t h eC o m

m o d o r e1 2 8s i d e o ft h em a c h i n e ,y o um a yd o n op r o

g r a m m i n ga t a l l o n t h eC - 6 4s i d e .

T h e1 2 8S i d e

T h i si s t h e p a r to f t h es y s t e mw e ' l lb e d e a l i n

w i t hm o s te x t e n s i v e l yi n t h i sb o o k .I n a d d i t i o nt o

a s p e e d i e ra n dm o r ev e r s a t i l eB A S I C ,t h e1 2 8s i d e

o f t h em a c h i n eh a st w i c et h em e m o r yo f t h ea l r e a d

a m p l ye n d o w e dC o m m o d o r e6 4 .D i s ko p e r a t i o

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 18/194

P l u s e sa n dM i n u s e so f t h e i ro p e r a t i o n

T h eC o m m o d o r e6 4 S i d e

+ R u n sa l l C - 6 4s o f t w a r e

+ O p e r a t e sa l lC - 6 4d e v i c e si n c l u d i n gp r i n t e r s ,m o n i

t o r s ,j o y s t i c k s ,a n dc o n t r o l l e r s .

- L e s sm e m o r yt h a nt h eC - 1 2 8s i d eo f t h em a c h i n e

- O p e r a t e sw i t ha l e s se x p a n d e dB A S I Ct h a n t h eC - 1 2 8

s i d e .

T h eC o m m o d o r e1 2 8S i d e

+ T w i c et h em e m o r yo f t h eC - 6 4s i d eo f t h em a c h i n e

+ F a s t e ra n dm o r ev e r s a t i l eB A S I C

+ A d d i t i o n a lk e y b o a r df u n c t i o n s

- M a c h i n el a n g u a g ep r o g r a m sf o rt h eC - 6 4g e n e r a l l y

w o n ' t r u no n t h i ss i d e

T h e C P / MS i d e

+ R u n sm a n yp o p u l a rC P / Mp r o g r a m s ,s u c h a sW o r d

S t a r ,P e r f e c tW r i t e r ,a n dP e r f e c tC a l c

+ R e a d sC P / Md i s k so r i g i n a l l yc r e a t e do n o t h e r

m a c h i n e s s u c ha s K A Y P R Oa n dX e r o x

- M o s tc o m m a n d sa r ed i f f e r e n tf o r mt h o s eo ft h eC - 6 4

a n dC - 1 2 8s i d e s

- D i s k sf r o mC P / Ms i d ec a n n o tb er e a db yt h eC - 6 4a n dC - 1 2 8s i d e s

F i g .2 - 1 .T h et h r e ec o m p u t e r si n s i d ey o u rC o m m o d o r e1 2 8 .

a r ea l s om u c he a s i e r t oa c c o m p l i s ho nt h e1 2 8s i d e

o f t h e m a c h i n e ,b e c a u s ed i s kc o m m a n d sm o r e

n e a r l yr e s e m b l eE n g l i s hw o r d s .

C - 6 4 / C - 1 2 8C o m p a t i b i l i t y

A sy o ue x p e r i m e n t ,y o u ' l lf i n d t h a tm a n yB AS I Cp r o g r a m sw r i t t e nf o rt h eC o m m o d o r e6 4 w i l l

r u np e r f e c t l yo nt h eC o m m o d o r e1 2 8s i d e o ft h em a

c h i n e .A f t e ry o u ' v eb e e ns u i t a b l ys p o i l e db yC - 1 2 8

B A S I C7 . 0 ,h o w e v e r ,y o u ' l lf i n dy o u r s e l f s p i c i n g

u p t h e s e o l d e rp r o g r a m st o t a k ea d v a n t a g eo f t h e

a d d e dp o w e ro n t h e1 2 8s i d e .

M a c h i n el a n g u a g e o r a s s e m b l y l a n g u a g ep r o

g r a m sw r i t t e nf o rt h eC o m m o d o r e6 4 g e n e r a l

w o n ' t r u no n t h e1 2 8s i d eo f t h e s y s t e m ,b e c a u s

m a n y m e m o r yl o c a t i o n sh a v ec h a n g e d .T h es a m e

i s t r u ef o rB A S I Cp r o g r a m st h a tu s el o t so fP E E

a n dP O K Es t a t e m e n t s ,a l t h o u g hs o m eP E E K sa n d

P O K E sa r es t i l lt h es a m ea s o nt h eC o m m o d o r6 4 .

R e m e m b e ra g a i nt h a ta n y p r o g r a mw r i t t e nf o r

a C o m m o d o r e6 4 w i l lr u no n t h eC - 6 4s i d e o ft h e

s y s t e m ,r e g a r d l e s so f w h e t h e ri t r u n so n t h e1 2 8

s i d e .

C P / MP l u s

T h et h i r dp a r to f t h eC o m m o d o r e1 2 8i s c o m

8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 19/194

p r i s e do fa p r o c e s s o rd e s i g n e dt o r u nt h o u s a n d so f

a v a i l a b l ep r o g r a m sw r i t t e nf o rt h eC P / Mo p e r a t i n g

s y s t e m .L i k et h e 1 2 8 ' sb u i l t - i nd i s k o p e r a t i n g s y s

t e m s ,C P / Mh a sc o m m a n d sf o rs e e i n gw h a t ' so na

d i s k ,m a k i n gc o p i e s ,a n dr u n n i n gp r o g r a m s .B u t

h e r e ' s t h ec a t c h : A l lo f t h ec o m m a n d sa r ec o m

p l e t e l yd i f f e r e n t .C o m m a n d st h a tw o r ko n t h e1 2 8

s i d e o ft h es y s t e mw i l lh a v en oe f f e c tw h a t e v e ro n

t h eC P / Ms i d e( e x c e p tt o p r o d u c ea n e r r o rm e s

s a g e ) .T h eC P / MP l u ss e c t i o n o ft h eC o m m o d o r e

1 2 8i s l i k eo p e r a t i n ga c o m p l e t e l yd i f f e r e n tc o m

p u t e r .T h ec o m m a n d sa r e d i f f e r e n t .E v e nt h e

p r o c e d u r e sa r ed i f f e r e n t .F o re x a m p l e ,B A S I Ci s

a l w a y sa v a i l a b l ef o ru s eo n t h e6 4 a n d1 2 8s i d e s

o f t h em a c h i n e — i t ' sb u i l ti n t ot h es y s t e m .B u tw h e n

r u n n i n gu n d e rC P / M ,B A S I Cm u s tb e l o a d e df r o m

a C P / Md i s k .A n dm a n yo f t h eB A S I Cc o m m a n d s

a r ed i f f e r e n tf r o mw h a ty o u ' df i n di n C - 6 4B A S I Co r i n t h eC - 1 2 8s i d e ' sB A S I C7 . 0 .

E v e nw i t ha l lo f t h i sc o n f u s i o na n di n c o m p a t i

b i l i t y ,t h eC P / Ms i d e o ft h eC o m m o d o r e1 2 8s t i l l

o f f e r ss p l e n d i da d v a n t a g e st o t h o s ei n t e r e s t e di n

i n d u s t r i a l - s t r e n g t hb u s i n e s ss o f t w a r e .S o m eo f t h e

m o s tv e r s a t i l ew o r dp r o c e s s i n g ,f i l i n g ,a n da c c o u n t

i n gs o f t w a r ep a c k a g e s a r o u n dt o d a yh a v eb e e nw r i t

t e nf o rc o m p u t e r su s i n gC P / M .A n dt h i ss o f t w a r e

r u n so n t h eC o m m o d o r ej u s ta s w e l l a si t d o e so n

m o r ee x p e n s i v e b u s i n e s s s y s t e m s .B e s i d e s ,w h a t

o t h e rC P / Ms y s t e ml e t sy o up l a yD o n k e yK o n ga f

t e r y o u ' v ef i n i s h e dw i t hP e r f e c tW r i t e ro r

W o r d S t a r ?

C O M M O D O R E1 2 8M E M O R Y

T h e r e ' sa s a y i n gi n c o m p u t e r d o mt h a t" a n y

p r o g r a mw i l le x p a n dt o d e v o u ra l la v a i l a b l em e m

o r y . "S t i l l ,i t i s h a r dt o i m a g i n ea n yB A S I Cp r o

g r a mt h a tw o u l dc o m p l e t e l yf i l l u p t h e 1 2 8 ' s

e l e c t r o n i cg a st a n k .

T h e1 2 8s i d eo f y o u rC o m m o d o r e1 2 8h a sas t a g g e r i n ga m o u n to f m e m o r y :A s s h o w ni n F i g .

2 - 2 ,w h e ny o ut u r no n t h em a c h i n e ,m o r et h a n

5 8 - t h o u s a n db y t e s( c h a r a c t e r s )a r ea v a i l a b l et o h o l d

y o u rp r o g r a ma l o n e .G u s tt o g i v ey o ua n i d e a ,a 4 0 K

p r o g r a mw o u l dc o n t a i na l m o s t 2 , 0 0 0l i n e s — a n d

y o u ' ds t i l lh a v e1 8 Kf r e ef o rm o r ep r o g r a m ! )

I f y o uu s eg r a p h i c s ,p l a no n h a v i n g a b o u t9 K

l e s ss t o r a g ea r e af o ry o u r p r o g r a m ;w h e ny o uu s e

t h eh i g h - r e s o l u t i o no r m u l t i c o l o rg r a p h i c sm o d e s

t h eC o m m o d o r e1 2 8a u t o m a t i c a l l y t a k e so v e rt h i s

a m o u n to f m e m o r yf o rs c r e e nd i s p l a y . I n c i d e

a s y o u ' l ls e eo nt h es e c t i o nc o v e r i n gg r a p h i c s ,a s i m

p l ec o m m a n di s u s e df o re n t e r i n ga n dl e a v i n g t

g r a p h i c sm o d e s .T h e r e ' ss e l d o ma n yn e e dt o w o r r

a b o u tc o m p l e xm e m o r ym a n a g e m e n tw i t ht h eC o m

m o d o r e1 2 8 b e c a u s eB A S I Ch a n d l e st h e s ei n t e r n

a f f a i r sa u t o m a t i c a l l y .

V a r i a b l e s a r e s t o r e di n a s p e c i a ls e c t i o n

m e m o r ya n dc a n t a k eu p a n o t h e r6 4 K .T h a t '

e n o u g hs p a c et o s t o r ea l i s to f m o r et h a n1 , 3 0

i t e m sa n ds t i l lh a v er o o mf o ro t h e ri n f o r m a t i

T h ea m o u n to f v a r i a b l es p a c ea v a i l a b l ei s n o t

a f f e c t e db y t h es i z eo f y o u rp r o g r a m .I n f a c t ,t h e

p r o g r a ma n dv a r i a b l e sa r es t o r e di n t w os e p a r a

b a n k so f m e m o r y .

W h a tM e m o r ya n d

P o w e rM e a nt o P r o g r a m m e r s

I f y o u ' v ep r o g r a m m e di n B A S I Co no t h e rc o m

p u t e r s ,i t m a yb e n e c e s s a r yt o r e a d j u s ty o u rt h i n k

i n g .F i r s t ,o p e r a t i o n sy o um a yh a v es p l i ti n t o

s e v e r a lp r o g r a m s( s a y ,p r i n t i n ga n ds o r t i n g )c a n

n o wb e c o m b i n e di n o n e p r o g r a m .Y o uc a na l s oa d d

p r o g r a mf e a t u r e sw i t h o u tw o r r y i n ga b o u t r u n n

o u to f m e m o r y .M o r es p a c em e a n sy o uc a ns p r e a

o u tp r o g r a ms t a t e m e n t st h a ty o um i g h tb e t e m p t

t o s q u e e z ei n t oo n el i n eo n l e s s e rm a c h i n e s .T h i s

s t a t e m e n t :

1 0 F O RX = l T O1 0 : P R I N T " T E #

" ; X ; : N E X T : P R I N T

c a nn o wb e t y p e da s :

1 0 F O R X = l T O 1 0

2 0 P R I N T" T E S T# " ; X ;3 0 N E X T

4 0 P R I N T

A d d i t i o n a lv a r i a b l em e m o r ym e a n sn o to n l y

m o r es t o r a g es p a c e ,b u ta l s o f a s t e ro p e r a t i o nf o r

m a n yp r o g r a m s ,b e c a u s ei n f o r m a t i o nm a yb es t o r e

i n m e m o r yr a t h e rt h a n b e i n gw r i t t e na n dr e a dc o n -

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 20/194

V a r i a b l e sV a r i a b l e

S t o r a g e

( 6 4 . 2 )

( 6 5 , 5 3 5 )

( b a n k1 )

( 0 )

$ F F F F

$ 0 0

P r o g r a m /

G r a p h i c sP r o g r a ms t o r a g e

( 4 8 . 8 K )

O p t i o n a lg r a p h i c s

( 9 . 2 K )

I n t e r n a lf l a g s

( 6 5 , 2 8 0 )

( b a n k0 )

( 1 6 3 8 4 )

( 7 1 6 8 )

( 0 )

$ F F 0 0

$ 4 0 0 0

$ 1 C 0 0

$ 0 0

B A S I C .E a c hl i n eo f B A S I Cb e g i n sa s t h el o w e s ta v a i l a b l em e m o r yl o c a t i o n .T h u s ,l i n e s

a r es t o r e ds e q u e n t i a l l yi n m e m o r y .W h e ny o ua d d al i n et o a n e x i s t i n gp r o g r a m ,B A S I Cp u s h e s

a l lh i g h e r - n u m b e r e dl i n e su p i n m e m o r yt o m a k er o o mf o ry o u rn e wo n e . T h i si s w h yt h em a

c h i n ew i l lp a u s ef o ra s e c o n dw h e ny o up r e s sr e t u ma f t e ra d d i n ga l i n et o a v e r yl a r g ep r o g r a m

( g e n e r a l l yn o t i c e a b l ea t 2 0 Ko r m o r e ) .

V A R I A B L E S .V a r i a b l e sa r es t o r e di n a s e p a r a t eb a n ko f t h e 1 2 8 ' sm e m o r y .T h ec o m p u t e r

a l w a y sk n o w st h a tt h e s ev a r i a b l e sa r ei n t h i su p p e rb a n k ,s o t h e r e ' sn o n e e df o r m o s tB A S I C

p r o g r a m m e r st o b e c o n c e r n e dw i t hb a n ks w i t c h i n g .

S t r i n g v a r i a b l e sa r es t o r e df r o mt h e t o po f m e m o r yd o w n , a n dn u m e r i cv a r i a b l e sa r es t o r e d

f r o mt h eb o t t o mu p .W h e nt h et w os i d e sm e e t ,m e m o r yi s f u l la n da n " o u to f m e m o r y "m e s

s a g ew i l lb e d i s p l a y e d .

F i g . 2 - 2 .H o wm e m o r yi s b r o k e nu p .

t i n u a l l yf r o md i s k .

A g o o d e x a m p l ei s a n a m ea n da d d r e s sp r o

g r a m .O nc o m p u t e r sw i t hm o r el i m i t e dm e m o r y ,

y o um i g h ts t o r ee a c ha d d r e s ss e p a r a t e l yo n d i s k ,

u s i n gr a n d o ma c c e s sf i l e s .T h i sr e q u i r e sm o r ep r o

g r a m m i n gw o r ka n dp r o c e s s i n gt i m e t h a nt h ea l

t e r n a t i v e ,w h i c hi s t o s t o r ea l ln a m e sa n da d d r e s s

i n m e m o r y ,r e a d i n gt h e mo n l ya t t h eb e g i n n i n o f

t h ep r o g r a m ,a n ds a v i n gt h e mb e f o r ee x i t i n g .B e

c a u s et h eC o m m o d o r e1 2 8 ' se x p a n d e dm e m o r yc a n

s t o r eh u n d r e d so f n a m e sa n da d d r e s s e si n m e m o r

a t o n et i m e ,a c c e s st o t h ed a t ai s i n s t a n t ,m e a n i n

1 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 21/194

f a s t e rp r o g r a mo p e r a t i o n .

V a r i a b l eM e m o r y

B e c a u s ev a r i a b l em e m o r yi s s e p a r a t ef r o mp r o

g r a mm e m o r y ,t h es i z eo f y o u rp r o g r a mw o n ' ta f

f e c tt h ea m o u n to f v a r i a b l ew o r ks p a c ea v a i l a b l e .

O nm a n yc o m p u t e r s ,l a r g ep r o g r a m sc u td o w no nt h ea m o u n to f m e m o r ya v a i l a b l ef o rs t o r i n ga n d

p r o c e s s i n g .B u tt h eC o m m o d o r e1 2 8a l l o w sy o ut o

c r e a t ev e r yl a r g ep r o g r a m sw i t h o u tc o m p r o m i s i n g

y o u rv a r i a b l ew o r ks p a c e .

A B e t t e rT y p eo f M e m o r y

V a r i a b l e si n m e m o r ya r e c l e a r e di n o n l yt h r e e

w a y s :

1 . W h e ny o ut y p eN E Wt o c l e a rt h e c u r r e n tp r o

g r a mf r o mm e m o r y .

2 . W h e ny o uR U Na n o t h e rp r o g r a m .

3 . W h e ny o ut u r no f ft h ep o w e rt o t h eC - 1 2 8 .

N o t et h a t v a r i a b l e sa r e n ' te r a s e dw h e ny o u a d d

p r o g r a ml i n e s !I t ' sa l i t t l et h i n g ,p e r h a p s .B u ti t ' s

a c o n t r a s tt o t h ew a ym o s to t h e rc o m p u t e r so p e r a t e .

M o s to t h e rB A S I C ' Sh a v eb e e nd e s i g n e ds o

t h a ta d d i n go r e v e ne d i t i n ga p r o g r a ml i n ew o u l d

w i p eo u ta l lv a r i a b l e ss t o r e di n m e m o r ya t t h et i m e .

S ow h e ny o u w e r et r y i n g t od e b u ga p r o g r a m ,y o uc o u l d n ' tc h a n g ea l i n ea n dr e s u m er u n n i n g ,b e c a u s e

a l ld a t ai n m e m o r yw o u l db e g o n e !

B e c a u s et h eC - 1 2 8r e t a i n sa l lv a r i a b l e sd u r i n g

p r o g r a me d i t i n g ,y o u ' r ef r e e t om a k ec h a n g e st o

a l i n ea n d r e s u m et h ep r o g r a mw i t ha G O T Oa n d

a l i n en u m b e r .

T h e C o m m o d o r e1 2 8 ' sm e m o r ya r r a n g e m e n t

p r o v i d e sp h e n o m e n a lf r e e d o mf o rd e v e l o p i n gy o u r

p r o g r a m su n d e rB A S I C .

G R A P H I CS I D E SO FT H EC O M P U T E R

U n d o u b t e d l yo n eo f t h em o s te x c i t i n ge l e m e n t s

o f t h eC o m m o d o r e1 2 8i s t h a t o fv i d e od i s p l a y .I t

i s a l s oo n eo f t h em o s tc o n f u s i n g .T h eC - 1 2 8b o a s t s

f o u rg r a p h i c s c r e e n sa n ds e v e r a lg r a p h i cm o d e s :

t h e r e ' sy o u re i g h t yc o l u m nt e x t .T h e r e ' sy o u rf o r t y

c o l u m nt e x t .T h e r e ' sa h i g h - r e s o l u t i o ng r a p h i c s

s c r e e n ,w h i c ha l l o w sy o ut o d e s i g np i c t u r e so fv e r y

h i g hd e t a i la n dl i m i t e dc o l o r .O rt h e r e ' sm u l t i c o

h i g h - r e s o l u t i o n s c r e e n ,w h i c ha l l o w sp i c t u r e so f

m o d e r a t ed e t a i la n da l l o w sm o r ec o l o r .T oc o n f u s

t h i n g se v e nm o r e ,y o uc a n h a v es c r e e n st h a ta r e

h a l fg r a p h i c sa n dh a l ft e x t ,a n dy o uc a nc h a n g et h e

n u m b e ro f l i n e s i nt h i sm i xo f t e x ta n dg r a p h i c

T h e r e ' se v e na w a yt o m i xc h a r a c t e r sa n dg r a p h i

t h r o u g h o u tt h es c r e e n .F i g u r e2 - 3i l l u s t r a t e sa n d

e x p l a i n s t h e s em o d e s .

I t ' s a l l v e r yc o n f u s i n g ,e v e nw h e ny o u ' v

w o r k e dw i t ht h es y s t e mf o ra w h i l e .B u tr e w a r df o r

s t i c k i n gt o i t a n dl e a r n i n ga b o u tt h ei n sa n do u t s

o f t h e s e g r a p h i c sm o d e si s t h a ty o u ' l lb e a b l et o

g r e a t l ye n h a n c e y o u rp r o g r a m sw i t hp r o f e s s i o

g r a p h i c s .

T h e4 0 - C o l u m nS i d e

I f y o u ' r eu s i n ga t e l e v i s i o n s e ta s a m o n i t o r ,

i f y o ub o u g h ty o u rc o m p u t e rf o rb o t hg a m e sa n d

b u s i n e s s ,c h a n c e sa r ey o u rs y s t e mi s o p e r a t i n i n

a f o r t yc o l u m n m o d e .T e l e v i s i o ns e t sa n df o r t y

c o l u m nm o n i t o r sd i s p l a y4 0 - c h a r a c t e r s - p e r

a n dg r a p h i c sf r o mb o t hh i g h - r e s o l u t i o ng r a p h i

m o d e s .S o u n di s d e l i v e r e de i t h e rt h r o u g ht h em o n

i t o rs p e a k e ro r t h es p e a k e ri n y o u rt e l e v i s i o ns e t .

S u r p r i s i n g l y ,t h es o u n df r o ma T Vs e ti s e v e r yb i t

a s g o o da s s o u n d f r o mt h em o r ee x p e n s i v em o n it o r .B u tt h ep i c t u r eq u a l i t yo f 4 0 - c o l u m nm o n i t o

i s d e f i n i t e l yc r i s p e ra n db r i g h t e r .A f t e ra l l ,c o m

p u t e rm o n i t o r sw e r ed e s i g n e de x p r e s s l yf o rc o m

p u t e r s ,a n dt h a t ' sw h e r et h e ys h i n e .

T h e8 0 - C o l u m nS i d e

T h ee i g h t y - c o l u m ns i d eo f t h em a c h i n ei s a n

o t h e rw o r l de n t i r e l y .I n f a c t ,t h e C o m m o d

e i g h t y - c o l u m nm o n i t o re v e nh a si t so w np l u g ,l a

b e l e dR G B I( i t s t a n d sf o r r e d ,g r e e n ,b l u e ,a n di n d i g o — ar e p r e s e n t a t i o n o ft h em a i nc o l o rc o m b

n a t i o n sb e i n gp o r t e dt o t h i sm o n i t o r ) .T h ee i g h t y

c o l u m nd i s p l a yi s e s p e c i a l l y w e l l - s u i t e f o rw o r d

p r o c e s s i n g , s i n c ei t a l l o w st y p i s t s t ov i e wa l e t t e

e x a c t l ya s i t w i l la p p e a ro n t h ep r i n t e dp a g e( s e e

F i g .2 - 4 ) .F o r t y - c o l u m nw o r dp r o c e s s o r sg e n e r a l

a r r a n g et e x ta u t o m a t i c a l l ya n dd o n ' tr e a l l ya l l o w

1 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 22/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 23/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 24/194

y o ut o s e eh o wi t w i l ll o o ku n t i lt h ed o c u m e n ti s

p r i n t e d .

T h ee i g h t y - c o l u m ns i d e o ft h em a c h i n ec a nd i s

p l a yi t so w nb r a n do f h i g h - r e s o l u t i o n g r a p h i c s ,b u t

d r a w i n gp i c t u r e so n t h ee i g h t y - c o l u m ns i d ei s d i f

f i c u l tf r o mB A S I C .I n f a c t ,t h e s c r e e n o u t p u tt o

e i g h t y - c o l u m n sc o m e sf r o ma c o m p l e t e l yd i f f e r e n t

c o m p u t e rc h i p .A ni n t e r e s t i n g s i d ee f f e c to ft h i sd e

s i g ni s t h a ty o uc a nw a t c ha p r o g r a mr u n n i n go n

t h ef o r t yc o l u m ns i d e o ft h ec o m p u t e r ,a n da t t h e

s a m et i m e s e et h ep r o g r a ml i s t i n go n t h ee i g h t y -

c o l u m ns i d e .I n f o r m a t i o nl e f to n o n es c r e e ns t a y s

t h e r ee v e nw h i l et h eo t h e rs c r e e ni s o p e r a t i n g .I f

y o u ' r ev e r ys e r i o u sa b o u tp r o g r a m m i n g ,a n dy o u r

u n c l eW a l d ol e f ta b i gw a do f b i l l sf o rC h r i s t m a s ,

y o um i g h tc o n s i d e rb u y i n gt w om o n i t o r s :o n ew i t h

e i g h t yc o l u m n sf o rp r o g r a me d i t i n g ,a n dt h eo t h e r

w i t hf o r t yc o l u m n sf o rw a t c h i n gt h ep r o g r a mo p e r

a t e .T h o s ew h od o n ' th a v ea n u n c l eW a l d oc a n

g e n e r a l l yg e t a l o n gf i n ew i t hj u s ta s i n g l em o n i t o r ,

o r b y c o m b i n i n ga n e i g h t y - c o l u m nm o n i t o ra n da

T Vs e t .

S w i t c h i n gB e t w e e nM o d e s

T h a t4 0 / 8 0D I S P L A Ys w i t c ha t t h e t o po fy o u r

k e y b o a r d( s e e F i g .2 - 5 )i s a l i t t l ed e c e p t i v e .Y o u ' d

t h i n kt h a tp r e s s i n gi t w o u l di n s t a n t l ys w i t c h v i d e o

m o d e s .Y o um i g h ta s s u m et h a tw h e nt h es w i t c hi s

u p , t h eC - 1 2 8w i l ld i s p l a yi n f o r m a t i o no n t h ef o r t y

c o l u m ns i d e ;a n dt h a tw h e nt h es w i t c hi s d e p r e s s e d ,

a l ld i s p l a yw o u l di n s t a n t l yg ot o t h ee i g h t y - c o l u m n

s i d e .

Y o u ' db e w r o n go n b o t ha c c o u n t s .

Y e s ,t h e r ei s a k e y b o a r dc o m m a n dt o s w i t c h

i n s t a n t l yb e t w e e nt h e4 0 -a n d8 0 - c o l u m ns i d e s o f

t h em a c h i n e s , b u ti t h a sn o t h i n gw h a t s o e v e rt o d o

w i t ht h e4 0 / 8 0D I S P L A Yb u t t o n .

T h e4 0 / 8 0D I S P L A Yk e yd e t e r m i n e st h ec u r

r e n tg r a p h i c sm o d eo n l yw h e nt h em a c h i n ei s f i r s ts w i t c h e do n( p o w e r e du pa s t h e y s a yi n t h ec o m p u t e r

m a n u a l s ) .S o i f t h ek e yi s d o w n w h e ny o ut u r n t h e

p o w e ro n ,t h ec o m p u t e rw i l lb e i n t h ee i g h t y - c o l u m n

m o d e .O t h e r w i s e , t h es y s t e mw i l ld i s p l a ya l lt e x t

a n dg r a p h i c so n t h e f o r t y - c o l u m ns i d e .

T h e r e ' sa d i f f e r e n tc o m m a n df o r s w i t c h i n g

m o d e so n c et h ec o m p u t e ri s o p e r a t i n g :

< E S C >

Y o uc a n u s e< E S C >< X >e n d l e s s l yt o t o g

g l eb e t w e e nf o r t y -a n de i g h t y - c o l u m nm o d e s ,a s i n

d e e dy o up r o b a b l yw i l li f y o u r s y s t e mi s e q u i p p e

w i t h b o t hm o n i t o r s .< E S C >< X >i s a l s ot h ef i r s t

c o m m a n dt o t r yi f y o u rc o m p u t e ra p p e a r st o h a v e

" g o n eo f f t on e v e r - n e v e rl a n d . "A f r o z e n o rb l a n kv i d e od i s p l a yi s o f t e n t h er e s u l t o ft h eC - 1 2 8b e i n g

a c c i d e n t a l l yt h r o w ni n t ot h ew r o n gg r a p h i c sm o d e

I f y o ud o n ' th a v ea m o n i t o rf o rt h eo t h e rs i d e o ft h e

v i d e od i s p l a y ,y o ur e a l l yh a v en ow a yo f k n o w i n

w h e t h e rt h i sh a s h a p p e n e d .I s s u i n ga n < E S C < X >c o m m a n di s a l w a y sw o r t ha t r y( a l s om a k e

i t s t a n d a r d o p e r a t i n gp r o c e d u r et o c h e c kt h a tt h e

m o n i t o ri s t u r n e do n , t h a tt h ev i d e oc o r di s p l u g g e

i n t ob o t ht h ec o m p u t e ra n dm o n i t o r ,a n dt h a tt h e

m o n i t o rp o w e rc o r di s p l u g g e di n t ot h ew a l ls o c k e t )

E D I T I N GA P R O G R A MO NT H EC O M M O D O R E1 2 8

W h i l ey o u rC o m m o d o r eo p e r a t o r ' sg u i d ec o n

t a i n ss o m ee x c e l l e n tt i p so nh o wt o e d i ta p r o g r a

t h e r e a r ea f e wa d d i t i o n a lt r i c k so fw h i c hy o us h o u l

b e a w a r e .T h ef i r s tt h i n gt o k e e pi n m i n di s t h a t

w h e ny o u ' r e n o tr u n n i n g ap r o g r a mw h e ny o u ' r

i n t h ee d i tm o d e :p r e s s i n gR E T U R Np l a c e s t h ec u r

r e n tl i n eo n t h es c r e e ni n t om e m o r y .T h i sm a k e

t h e s t e p sf o rc h a n g i n g ap r o g r a ml i n ev e r ye a s y :

1 . L i s tt h el i n ei n q u e s t i o n( e x .L I S T2 0 0 ) .

2 . M o v et h e c u r s o r u s i n g t h ek e y ss h o w ni n F i g .

2 - 6a n dt y p ei n t h ec h a n g e .

3 . P r e s sR E T U R Nw h i l et h eb l o c kc u r s o ri s s t i l l

o n t h a tp r o g r a ml i n e .

T h ef i r s tt h i n gt o k e e pi n m i n di s t h a tw h e n

y o u ' r en o tr u n n i n ga p r o g r a m ,p r e s s i n gR E T U R

p l a c e s t h el i n ea t t h e c u r s o rp o s i t i o n i n t om e m o r T h i sm a k e si t e a s yt o l i s t a p r o g r a m ,t y p ei n

c h a n g e so n a ne x i s t i n gl i n e ,a n ds i m p l yp r e s sR E

T U R Nt o s a v et h ei n f o r m a t i o n .A n e wl i n ew i l lr e

p l a c e t h eo l do n e .

I t a l s om e a n st h a ti f y o ud o n ' tl i k ec h a n g e

y o u ' v em a d eo na g i v e np r o g r a ml i n e ,y o uc a nk e e p

t h el i n ei n t a c ts i m p l yb y m o v i n gt h ec u r s o ro f fo f

1 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 25/194

E S C

T A B

A L T

C A P S

L O C K

H E L P

L I N E

F E E D

4 0 / 8 0

D I S

P L A

Y

N O

S C R O L L

1

C O N T R O L

R U N

S T O P

G

Q

S H I F T

L O C K

n 2

W

A

S H I F T

# 3

$ 4

E

S

Z

5

R

D

X

C

& 6

T

F

G

r v

7

Y

8

U

H

B

J

N

9

I

K

M

0

0

P

]

L

'

<

~

@

1

>

*

7

t

S H

I F T

C L R

H O M E

I N S T

D E L

R E S T O R E

R E T U R N

C R S R

C R S R

F 1

F 3

F 5

F 7

7 4 1 0

8 5 2

9 6 3

9

+ E N T E R

F i g .2 - 5 .T h eC o m m o d o r e1 2 8k e y b o a r d .

o i

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 26/194

L J

C R S R

. » J

( l lC R S R

T h ec u r s o rk e y sa t t h e

o l dC o m m o d o r e6 4 s e t

p r e s st h eS H I F Tk e ya l

q u i r e d ,m a n yu s e r sf i n e

a t t h et o p .

T h ea r r o wk e y sa t t h e t o po f t h ek e y b o a r dm o v et h e c u r s o rI n t h ed i r e c t i o ni n d i c a t e d( a tl e a s t

i nB A S I Ca n di nm a n y C o m m o d o r e1 2 8p r o g r a m s ! ) .T h e s ek e y sd on o tf u n c t i o ni nt h eC o m m o

d o r e6 4 m o d e .

b o t t o mr i g h t - h a n dc o r n e ro f t h em a i nk e y b o a r da r ea r e m n a n to f t h e

. I no r d e rt o m o v et h ec u r s o ru po r t o t h el e f tw i t ht h e s ek e y s ,y o um u s tt h es a m et i m ey o u ' r eu s i n g t h ea r r o wk e y .D e s p i t et h i se x t r ae f f o r tr e -

\ t h e s el o w e r c u r s o rk e y sm o r ec o n v e n i e n t l yp l a c e d t h a nt h ea r r o wk e y s

F i g .2 - 6 .T h et w os e t so f a r r o wk e y s .

t h a tl i n eb e f o r ep r e s s i n gR E T U R N ,o rb yc l e a r i n g

t h es c r e e n( m o r ea b o u tt h i sl a t e r ) .

I f y o u ' r e n o tf u l l ya w a r eo fh o wt h i s" R E T U R Nt o r e p l a c e "f e a t u r ew o r k s ,y o uc o u l db e i n f o rt r o u

b l e .I f y o uj u s tk n o wy o u ' v em a d ea c h a n g et o a

l i n e ,b u tl a t e rf i n dt h a tt h ec h a n g ei s n ' tt h e r e ,y o u

p r o b a b l yf o r g o t t op r e s sR E T U R Na f t e rt h ee d i t s

t o t h a tl i n e .I t ' sa c o m m o nm i s t a k e .

I f y o u ' v ew o r k e dw i t ho t h e rc o m p u t e r st h a tu s e

f i l ee d i t o r s t oc h a n g ep r o g r a ml i n e s ,i t ' se a s yt o f o r

g e tt o p r e s sR E T U R Nt o i n c o r p o r a t ey o u rc h a n g e s .

B u t r e m e m b e r :o nt h eC o m m o d o r e1 2 8 ,t h en e w l y

e d i t e dl i n ei s n ' t" i n "t h ep r o g r a mu n t i ly o up r e s s

R E T U R N .I t ' sa l s ol o g i c a lt o t h i n kt h a tt h el i n e

s h o u l db e i n t h ep r o g r a me x a c t l ya s i t a p p e a r so nt h es c r e e n .B u ty o uc a nm a k ea l lt h ec h a n g e sy o u

w a n t :I f y o ud o n ' tp r e s sR E T U R N ,t h el i n e w i l lr e

m a i ni n m e m o r ye x a c t l y a si t w a s ,a n d n oc h a n g e

w i l lb e i n s t a l l e d .

S o m eo t h e rc o m p u t e r sr e q u i r et h a ty o ut r a c e

o v e rt h ee n t i r ee d i t e dl i n eu s i n gt h e c u r s o r k e y s .O n

a nA p p l e ,f o re x a m p l e ,y o u ' l ll o s ep a r to f y o u rl i n e

u n l e s s t h e c u r s o ri s s i t t i n ga t t h ev e r ye n do f i t .T h i s

t r a c i n go v e ri s n o t n e c e s s a r yo n t h eC o m m o d o r e

1 2 8 ,a l t h o u g hi t u s u a l l yw o n ' th u r ta n y t h i n g .

A n o t h e rr u l ei s t o c a r e f u l l yc h e c kt h el i n eb ef o r ep r e s s i n gR E T U R N .O n c ey o up r e s sR E

T U R N ,e v e r y t h i n go n t h e c u r r e n tl i n ei s r e a di n t o

m e m o r y ,e v e ni f t h e r e a r es t r a yc h a r a c t e r so n t h e

s c r e e na t t h a tl i n e .F a i l u r et o c h e c kt h el i n ec a u s e s

p r o b l e m sm o r eo f t e nt h a ny o u ' dt h i n k .F o ri n s t a n c e ,

a s s h o w ni n F i g .2 - 7 ,i f y o u ' r ei n c e r t a i n e d i t i n g

m o d e s ,i t ' se a s yt o w i n d u pw i t ht h ew o r d" r e a d y "

a t t h ee n do f y o u rp r o g r a ml i n e s .A n di t ' sa p r o b

l e my o u ' l lu s u a l l ym i s su n t i lt h ep r o g r a ms c r e a m

" ? S Y N T A XE R R O R "d u r i n gt h em i d d l eo f a r u n .

S t r a ym e s s a g e s( s u c ha s " r e a d y " )c a nb e e a s i l

e l i m i n a t e db y p o s i t i o n i n gt h e c u r s o ro n es p a c et o

t h er i g h ta n dp r e s s i n gt h eD E Lk e ya f e wt i m e st o

b a c ko v e rt h eo f f e n d i n gw o r d .

T h e r e f o r ec h e c ka l i n e c a r e f u l l y ,a n de l i m i n a

a n ye x t r a n e o u sc h a r a c t e r su s i n gt h ed e l e t ek e y .

W h e nt h el i n ea p p e a r se x a c t l ya s y o u w a n ti t , i t ' s

s a f et o p r e s sR E T U R N .

I t s h o u l db e n o t e dt h a tw h e nw e s a yl i n e ,w e

m e a na p r o g r a ml i n e ,e v e ni f i t o c c u p i e ss e v e r a

4 0 - c o l u m nh o r i z o n t a ll i n e so n t h e s c r e e n .F o r e x

a m p l e ,e v e n t h o u g hi t s t r e t c h e so nf o rs e v e r a ll i n e s

t h e f o l l o w i n gi s c o n s i d e r e do n el i n ef o r e d i t i n

p u r p o s e s :

6 2 0 0 0 I FA N S W E R = " Y E S "T H E NP R I N

" V E R YG O O D !T H A T ' SR I G H T

E L S E P R I N T" S O R R YT H A T

T H EW R O N GA N S W E R "

1 0 4 0 i fq $ ( a t ) > r e q u e s t st h e n

h i g h = a t r e a d y .

F i g . 2 - 7 .A l i n ew i t hR E A D Y ,a t t h e e n d .T h ew o r dR E A D w a sp i c k e du pw h e nt h i sl i n ew a sl i s t e dw h i l ei n t h eE S C - Aa u t o m a t i ci n p u tm o d e .T h ep r o g r a m m e ra p p a r e n t l yp r e s s e

R E T U R Nw i t h o u t n o t i c i n gt h ee x t r aw o r d .I n m a n yc a s e s ,

t h i sm i s t a k ew i l lc r e a t eb u g so r c a u s et h ep r o g r a mt o c r a s h .

T h e r e ' so n er e m e d y :p a ya t t e n t i o n t ow h a ty o u ' r ed o i n g !

1 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 27/194

P r e s s i n gR E T U R Na t a n yp o i n t( e v e ni f t h ec u r

s o ri s a t t h ew o r d" w r o n g "o r " g o o d " )w o u l dp l a c e

t h ee n t i r ep r o g r a ml i n ei n t om e m o r y .

G r a p h i c sS y m b o l s a n d

U p p e r -a n dL o w e r c a s eL e t t e r s

T h eC o m m o d o r e( C * ) K e ya n dK e yb o a r dM o d e s . T h eC o m m o d o r e1 2 8o f f e r st w o

t e x tm o d e st h a td e t e r m i n ew h e t h e rt e x to n t h e

s c r e e nw i l la p p e a ra s u p p e r c a s el e t t e r sa n dg r a p h i c s

s y m b o l s ,o r s i m p l y a su p p e r c a s ea n dl o w e r c a s e

l e t t e r s .

A s a h i s t o r i c a ln o t e ,t h i sd u a lm o d ef o rt e x t

t a k e si t so r i g i n sf r o mt h eC o m m o d o r e6 4 ,a n df o l

l o w se x a c t l y t h es a m er u l e s .W h e nt h em a c h i n ei s

t u r n e do n ,i t i s i n t h eu p p e r c a s ea n dg r a p h i c sm o d e .

U n s h i f t e dl e t t e r sa p p e a ri n u p p e r c a s e .S h i f t e dl e tt e r sa p p e a ra s g r a p h i c sc h a r a c t e r s .A s s h o w ni n

F i g .2 - 8 ,t h e g r a p h i c sc h a r a c t e r sp r o d u c e db y

S H I F Ta r et h o s es h o w no n t h er i g h t - f r o n t s i d eo f

t h e k e y .

T h el o w e r c a s ea n du p p e r c a s em o d ei s t h eo n e

u s e db y w o r dp r o c e s s o r sa n do t h e rp r o f e s s i o n a lp r o

g r a m s ,s u c ha s t h eo n e sy o u ' l lb e d e s i g n i n gi n t h i s

b o o k .

I n e i t h e rm o d e ,t h en u m b e rk e y sf u n c t i o nj u s t

a s t h e yw o u l do n a r e g u l a rt y p e w r i t e r ;a 5 i s s t i l l

a 5 i n e i t h e rm o d e , a n da s h i f t e d5 p r o d u c e sa p e r

c e n ts i g n( % ) ,j u s ta s y o uw o u l de x p e c t .

S w i t c h i n gf r o m o n em o d et o t h eo t h e ri s e a s y :

S i m p l yp r e s sa n d h o l dt h e S H I F Tk e y w h i l e

m o m e n t a r i l yp r e s s i n gt h eO ( C o m m o d o r e )k e y .A l l

t h et e x to n t h es c r e e nw i l li n s t a n t l ys w i t c ht o t h e

o t h e rm o d e .

O n ee x c e p t i o nt o a l l o f t h i si s t h e C - 1 2

8 0 - c o l u m ns c r e e nm o d e ,w h i c h ,a s y o u ' l ls e e

t h r o u g h o u tt h i sb o o k ,h a si t so w ns p e c i a l s e to fr u l e s

a n dp r i v i l e g e s .I n t h e8 0 - c o l u m nm o d e ,c h a n g i

t h et e x tm o d ea f f e c t so n l y c h a r a c t e r st h a th a v ey e t

t o b e t y p e d ;t e x ta l r e a d yo nt h es c r e e nr e m a i n su n

a f f e c t e d .T h i sm e a n ss h i f t i n g i n t ot h el o w e r c a

m o d ew o n ' ti n s t a n t l yt u r na l lu p p e r c a s el e t t e r so n

t h e s c r e e ni n t ol o w e r c a s e ,a s i t d o e si n t h e

4 0 - c o l u m ns c r e e nm o d e .A s i m i l a rs e to fr u l e sh o l d s

t r u ew h e ny o ua r ew o r k i n gi n a n yo ft h ec o m p u t e

g r a p h i c sm o d e s .

B y p a s s i n gK e y b o a r dC o n t r o l .T h e r ei s a

w a yt o h a v e y o u rc o m p u t e r p r o g r a m ss w i t c hm o d e

a u t o m a t i c a l l y ,w i t hn on e e dt o p r e s st h eC sk e y .Y o uc a ne v e nu s ea p r o g r a mc o m m a n dt o t e m p o r a r

l o c kt h em a c h i n ei n t ot h e c u r r e n tm o d e ,s o u s e r

c a n ' t a c c i d e n t a l l yc h a n g ei n t ot h ew r o n gk e y b o a

m o d e .

W h a ti f y o uw a n tt o c o m b i n el o w e r c a s e ,u p p e r

c a s e ,a n dg r a p h i c sc h a r a c t e r sa l l o n t h e s a m e

s c r e e n ?T h e r ea r ew a y st o d o t h a t , t o o .W e ' l le x

p l o r ea l lo f t h e s ep r o g r a mc o n t r o lf e a t u r e si n t h e

c h a p t e ro n p r o f e s s i o n a la p p e a r a n c e .

R u l e sf o rK e y i n gi n a P r o g r a m .U s u a l l y

t h o u g hn o t a l w a y s ,y o uw i l lb e t y p i n gi n y o u rp r o

g r a mw h i l et h e c o m p u t e ri s i n t h e u p p e r

c a s e / g r a p h i c sm o d e .T h i sm e a n st h ec o m m a n d sa n d

s t a t e m e n t so ny o u rp r o g r a ml i n e sw i l la p p e a ri n a l l

H e r ea r et h r e eo f m a n yk e y b o a r d k e y st h a tm a yb e u s e df o rg r a p h i c s .T h es y m b o lo n t h e

l e f to f t h eF k e yw o u l db e s e l e c t e db yp r e s s i n gO a n dF . I n t h eu p p e rc a s e / g r a p h i c sm o d e ,

p r e s s i n gS H I F TF w o u l d p r o d u c et h e c h a r a c t e ro n t h er i g h ts i d eo f t h eF k e y .

F i g . 2 - 8 .O n eo f t h eC o m m o d o r e1 2 8 ' sm a n yt e x t / g r a p h i c sk e y s .

1 7

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 28/194

c a p s .P r e s s i n gS H I F Tt o g e t h e rw i t ha na l p h a b e t i c

k e yw i l lp r o d u c ea g r a p h i c s c h a r a c t e ri n t h i sm o d e .

W h e ny o u ' r ei n t h ec o m p u t e r sl o w e r c a s e / u p

p e r c a s em o d e , b es u r ey o u rp r o g r a ml i n e sa p p e a r

i n l o w e r c a s e .I f y o ut y p et h ep r o g r a mw i t ht h e

S H I F T - L O C Ko r C A P S - L O C Kk e yd o w n ,t h el i n e s

w i l la p p e a ri n u p p e r c a s e — t h e y ' l l l o o kj u s tf i n e .B u t

w h e ny o uR U Nt h ep r o g r a m ,t h eC - 1 2 8w i l li n t e r

p r e ta l lt h ec o m m a n d sa s g r a p h i cs y m b o l s .T h ep r o

g r a mw i l li m m e d i a t e l yc r a s h .

I t e m se n c l o s e di n q u o t e sa r ee x e m p tf r o mt h e

u p p e r c a s e / l o w e r c a s er u l e ,b e c a u s eB A S I Ci n t e r

p r e t se v e r y t h i n gi n q u o t e sl i t e r a l l y .S o t h i s w i l l

w o r ki n t h el o w e r c a s em o d e :

1 0 p r i n t" H e l l ot h e r eS a l l y ! "

B u tt h i sw o n ' t ,b e c a u s es o m eo ft h ec o m m a n d sc o n t a i nu p p e r c a s el e t t e r s :

1 0 r e mT h i si s a T e s t

2 0 P r i n tT a x e s

I f y o u ' r ea t a l lc o n f u s e db y t h ed i f f e r e n tk e y

b o a r dm o d e s ,t r yt y p i n g t h e s et w os h o r tp r o g r a m s

i n f r o mt h el o w e r c a s em o d e .T h e nR U Nt h e m .I t ' s

t h ee a s i e s tw a yt o s e eh o wi t a l lw o r k s .

E s c a p eC o m m a n d sU s e df o rE d i t i n g

A ss h o w ni n F i g .2 - 9 ,t h eE S Ck e yc a nb eu s e d

t o i s s u es p e c i a lc o m m a n d st h a tc a ng r e a t l ys p e e d

e d i t i n g o fy o u r p r o g r a m .

I n s e r t i n g T e x t . T h e r ea r em a n yt i m e sw h e n

p r o g r a m m e r s w a n tt o i n s e r ta d d i t i o n a li t e m so f i n

f o r m a t i o ni n t oa p r o g r a ml i n e .Y o um i g h t ,f o re x

a m p l e ,w a n tt o a d d a na d d i t i o n a lp r i n ts t a t e m e n t

o r l e n g t h e nt h en a m eo f a v a r i a b l e .B y p r e s s i n g

S H I F Ta n dI N S E R T / D E L E T Ea t t h es a m et i m e ,

y o uc a ni n s e r ta d d i t i o n a ls p a c e si n t oa l i n e ,a n dt h e nf i l li n t h e s es p a c e sw i t hn e wp r o g r a mc o m m a n d s

o r o t h e ri n f o r m a t i o n .B u tt h e r ea r et w od r a w b a c k s .

F i r s t ,y o uh a v et o c o u n to u tt h en u m b e ro f

s p a c e sy o u ' l ln e e df o rt h ei n s e r t ,a s s h o w ni n F i g .

2 - 1 0 .I f y o uw a n t e dt o a d dt h ew o r dP R I N Tt o a

l i n e ,y o uw o u l di n s e r t f i v es p a c e sf o rt h ec o m m a n d

a n do n es p a c et o f o l l o wi t . C o u n t i n gt h es p a c e sr e

q u i r e df o rl o n gs e t s o fs t a t e m e n t sb e c o m e sl a r g e l

a m a t t e ro f g u e s sw o r k .

T h es e c o n dd r a w b a c ki s t h a tt h ec u r s o r( a r r o w

k e y sb e c o m ei n a c t i v eo v e rt h ei n s e r t e d s p a c eF o r

e x a m p l e ,i f y o um a d ea m i s t a k ei n i n s e r t i n

P R I N T ,a n ds t a r t e dt h ew o r dw i t hL i n s t e a d ,p r e s s

i n g t h el e f ta r r o w k e yw o u l dp r o d u c ea n i n v e r s

c h a r a c t e ra n dm o v et o t h er i g h t — n o ta t a l l t h e

d e s i r e dr e s u l t .

T h eC o m m o d o r e1 2 8 ' sa u t o m a t i ci n s e r tm o d ei s

t h ep e r f e c ta l t e r n a t i v et o m a n u a l l yi n s e r t i n gs p a c e

o n ea t a t i m e .T h i sm o d ea u t o m a t i c a l l yi n s e r t

s p a c ef o rc h a r a c t e r sa s t h e ya r ee n t e r e d ,a n da l s o

a l l o w sf u l lu s eo f t h ec u r s o rk e y s .E s p e c i a l li f

y o u ' r e p l a n n i n gt o d o a g o o dd e a l o fe d i t i n go n a

l i n eo r g r o u p so f l i n e s ,t h ea u t o m a t i ci n s e r tm o d

i s p e r f e c t .

T h ea u t o m a t i ci n s e r tm o d ei s a c t i v a t e db ys i m

p l y p r e s s i n g< E S C >a n dt h e nt h el e t t e r< A > F r o mt h e no n — a n du n t i ly o ua c t u a l l yr u nt h ep r o

g r a mo r G O S U Bt o a r o u t i n e — a n y t h i n gi n

a l i n e w i l lb e a u t o m a t i c a l l yi n s e r t e d ,p u s h i n gt h e

s t a t e m e n t sf o l l o w i n gt h e mo n ep o s i t i o n t ot h er i g h t

T h ei n s e r tm o d em a yb e c a n c e l e db yp r e s s i

< E S C> a n dt h e n< C > ( n o t e t h a tt h e s et w ok e y s

s h o u l db ep r e s s e ds e p a r a t e l y — i t ' sd i f f e r e n tf r o mu s

i n gS H I F To r C O N T R O Li n c o n j u n c t i o nw i t ha n

o t h e rc h a r a c t e r ) .

T h e s e t w oc o m m a n d sa r ee a s yt o r e m e m b

< E S C >< A >a s i n " a u t o m a t i ci n s e r t "

< E S C >< C >a s i n " c a n c e l "

I n s e r t i n gC h a r a c t e r sW h i l e aP r o g r a

i s R u n n i n g .B e c a u s et h ea u t o m a t i ci n s e r tm o d

i s t u r n e do f fa s s o o na s a p r o g r a mr u n s ,i t i s n o t

p o s s i b l et o u s et h i sm o d et o e d i te n t r yo f i n f o r m

t i o n w i t h i na n I N P U Ts t a t e m e n t .B u t t h e< I N S E R T> a n d< D E L E T E >k e y s b o t hf u n c t i

w i t h i na p r o g r a ma n dm a yb e u s e da l m o s ta n y t i m

y o u ' r e e n t e r i n gi n f o r m a t i o n .

C l e a r i n gt o t h eE n do f a L i n e .T h e E S C

c o m m a n di s u s e dt o e r a s ea l lc h a r a c t e r st o t h er i g h t

o f t h e c u r s o rp o s i t i o n .F o re x a m p l e ,l e t ' ss a yy o u

w a n tt o e l i m i n a t ea r e m a r ka t t h ee n do f a l i n e :

1 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 29/194

T h ef o l l o w i n gE S Cc o m m a n d sa r eu s e f u lf o re d i t i n gB A S I Cp r o g r a ml i n e s .T h e yg e n e r a l l y

d o n o t f u n c t i o no u t s i d eo f B A S I C .

L I S T

L I S T2 0 0

L I S T2 0 0 - 2 5 0

E S C

E S C

E S C

E S C

E S C

E S C

E S C

E S C

E S C +

E S C

E S C

E S C

E S C

E S C

E S C+

u

E

F

L i s t sa l l p r o g r a ml i n e s

L i s t sl i n e2 0 0

L i s t s l i n e s2 0 0 - 2 5 0 ,i n c l u s i v e

A u t o m a t i ci n s e r tm o d e

C a n c e l sa u t o m a t i ci n s e r tm o d ea n dq u o t em o d e

D e l e t e s t h ec u r r e n tl i n e

I n s e r t sa l i n ea t t h e c u r s o rp o s i t i o n

M o v e sc u r s o rt o b e g i n n i n go f l i n e

M o v e sc u r s o rt o e n do f c u r r e n tl i n e

E r a s e se v e r y t h i n gf r o mb e g i n n i n go f t h el i n et o c u r s o r

E r a s e sf r o mc u r s o rt o e n do f l i n e

C l e a r sf r o mc u r s o rt o e n do f s c r e e nw i n d o w

O t h e rE S C C o m m a n d s

S e t sb o t t o mr i g h tc o r n e ro f w i n d o w

S e t st o pl e f tc o r n e ro f w i n d o w

C h a n g e sc u r s o rt o a s o l i db l o c k

C h a n g e sc u r s o rt o a n u n d e r l i n e

T u r n so f f c u r s o rf l a s h i n g

M a k e sc u r s o rf l a s h

F i g . 2 - 9 .S t e p sf o ri n s e r t i n gt e x tu s i n gt h eI N Sk e y .

1 9

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 30/194

I n s e r t i n gW o r d sw i t ht h e

1 . P o s i t i o nt h ec u r s o ra t t h eb e g i n n i n go f t h ew o r df o l l o w i n g :

T H R E EI M I C E

2 . P r e s sa n dh o l d t h e| S H I F Tj k e yw h i l e p r e s s i n gt h e | l N S |k e yt h e r e q u i r e dn u m b e ro ft i m e s

( o n c ef o re a c hl e t t e r i nt h en e ww o r d ,p l u sa n a d d i t i o n a lt i m ef o ra s p a c e ) :

T H R E E M I C E

\T h ew o r dM I C Ei s m o v e dt o

t h er i g h te a c h

t i m eI I N SI i s p r e s s e d .

3 . T y p e as p a c ea n dt h e nt h ew o r dt o b e i n s e r t e d :

T H R E EB L I N DI M I C E

T h eI I N SI k e yi s e s p e c i a l l yu s e f u lf o rm a k i n gq u i c ke d i t st o p r o g r a ml i n e s .

F i g .2 - 1 0 .U s e f u le d i t i n gc o m m a n d s .

1 0 P R I N T " H E L L O ": R E MT H I S L I N E

P R I N T SH E L L O

O n ew a yw o u l d b et o " s p a c e o v e r "t h er e m a r k .

B u tt h i sw o u l dt a k es o m et i m e .E S C - Qp r o v i d e sa n

e a s i e ra l t e r n a t i v e .U s i n gt h ea r r o wk e y s ,y o uw o u l d

s i m p l yp o s i t i o nt h e c u r s o rt o t h el e f to f t h e c o l o n( : ) ,p r e s s t h eE S Ck e y ,a n dt y p eQ . T h er e m a i n d e r

o f t h el i n ew o u l dv a n i s h .Y o uc a nr e m e m b e rt h i s

c o m m a n db y s i m p l yt h i n k i n go f E S C - Qa s " q u i t

t i n g " t h el i n ea t t h e c u r r e n t c u r s o rp o s i t i o n .N o t e

t h a ti t ' ss t i l ln e c e s s a r yt o p r e s s r e t u r nf o rt h ee d i t

t o b e i n s e r t e di n t oy o u rp r o g r a m .

M o v i n gt o t h eE n do f a L i n e .T h eE S C - K

c o m m a n di s u s e dt o m o v et h e c u r s o rt o t h ee n do f

a p r o g r a ml i n e ,a n di s g e n e r a l l yu s e dt o a d ds t a t e

m e n t st o t h ee n d o na l i n e .E S C - Ki s m o s tu s e f u

w h e ny o uw i s ht o a d da s t a t e m e n tt o a l o n g p r

g r a ml i n ea n dy o ud o n ' tw a n tt o t r a c eo v e r2 0 o r

3 0 s p a c e st o g e tt o t h e e n d .R e m e m b e rt o t y p ea

c o l o n( : )t o s e p a r a t e t h en e wp r o g r a ml i n ef r o mt h eo l d .

H o wt o M a k ea L i s t i n gP a u s e

I f y o u ' v eu s e do t h e rB A S I C ' S ,y o uk n o wt h a t

t h eL I S T c o m m a n dc a nb e u s e dt o d i s p l a y s e

o f a p r o g r a mo n t h es c r e e n :

2 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 31/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 32/194

&

G r n

L G g n

E x a m p l eo f a k e y b o a r d k e ys h o w i n gc o l o r s .G r e e ni s s e l e c t e db y p r e s s i n gC O N T R O La n d6 .

L i g h tg r e e ni s o b t a i n e db y p r e s s i n gO a n d6 .

F i g .2 - 1 1 .A k e yw i t hc o l o r .

e a s yi t i s t o u s et h ed u p l i c a t i o na n ds p l i tf e a t u r e s

o f t h eB A S I Ce d i t o r .

C l e a r i n gt h eS c r e e nF r o mt h eK e y b o a r d

W h i l et h eS C N C L Rc o m m a n ds h o w ni n t h el a s t

e x a m p l ei s f i n e f o ru s ei n p r o g r a m s ,t h e r e ' sa f a r

e a s i e rw a yt o c l e a rt h es c r e e nw h e ny o u ' r ew o r k

i n gd i r e c t l yf r o mt h ek e y b o a r d .B yp r e s s i n gS H I F T

a n dC L E A R / H O M Ea t t h es a m et i m e ,y o uc a nc l e a r

a n y t h i n gt h a t ' so nt h es c r e e n .T h i sf e a t u r ei s e s p e

c i a l l yu s e f u lw h e ny o u ' v eb e e np e r f o r m i n gs e v e r a l

p r o g r a mo p e r a t i o n so n t h es c r e e na n d w o u l dl i k e

t o r e d u c et h ec l u t t e rt h a tc o m e sw i t ht h e m .P r e s si n g t h eC L E A R / H O M Ek e yw i t h o u td e p r e s s i n g

S H I F Ta t t h es a m et i m ep u t s t h e c u r s o ra t t h eu p

p e rl e f th o m ep o s i t i o n o ft h es c r e e n ,b u td o e sn o t

c l e a rt h es c r e e n .

C h a n g i n gC o l o r s

O n ew a yt o m a k ep r o g r a ml i n e sa n do t h e ri n

f o r m a t i o ne a s i e rt o r e a di s t o c h a n g et h ec o l o r o f

c h a r a c t e r sa p p e a r i n go n t h es c r e e n( t h ef o r e g r o u n d

c o l o r ) .Y o u ' v ep r o b a b l ya l r e a d yn o t i c e d t h a te a c ho f t h en u m e r i c k e y sa t t h e t o po f t h ek e y b o a r di s

l a b e l e dw i t ht h en a m e so ft w oc o l o r s( s e eF i g .2 - 1 1 ) .

B yp r e s s i n gt h eC O N T R O Lk e ya l o n g w i t ht h e s e

n u m b e r s ,t h e t o ps e to f c o l o r sm a yb e s e l e c t e d .

P r e s s i n gt h eO ( C o m m o d o r e ) k e yc a u s e st h eb o t

t o mc o l o ro n t h ek e yt o b e s e l e c t e d .T h e s ec o l o r

c h a n g e sa f f e c to n l y t h ec o l o r o fc h a r a c t e r sa n d

c h a r a c t e rg r a p h i c s ,a n dd o n o tc h a n g et h eb a c k

g r o u n dc o l o ro nt h es c r e e n .T h eC O L O Rc o m m a n

d i s c u s s e di n C h a p t e r8 , i s u s e dt o a l t e rb a c k g r o u

b o r d e r ,a n dc h a r a c t e rc o l o r s .A l t h o u g hy o um a y

c h a n g ec o l o r sw i t h i ny o u rp r o g r a m su s i n gt h e

C O N T R O L - n u m b e rk e ya n d C £ - n u m b e rk e yc o m

m a n d s( w i t h i nq u o t e s ) ,i t i s g e n e r a l l yr e c o m m e n

t h a ty o uu s et h eC O L O Rc o m m a n d ,a n dl e a v e t h

n u m b e rk e yc o l o rc o m m a n d sf o ru s ew h e ny o u w a n

t o e f f e c tq u i c kc h a n g e si n c o l o ro nt h es c r e e nw h i l e

e d i t i n g .N o t et h a tt h ek e y si n t h en u m e r i ck e y p a

d o n o tw o r ki n t h i sm a n n e r .

R e s t o r i n g t h eS c r e e n

I f t h i n g se v e rg e to u to f h a n d — i fy o ue v e ri n

v o k ec o l o r st h a ta i r eu n r e a d a b l e ,o r t h ec o m p u t

" h a n g s "f o rn oa p p a r e n tr e a s o n — y o uc a no f t e nb a i l

o u t o f t h i n g sb y p r e s s i n g t h eR E S T O R Ea n d

R U N / S T O Pk e y sa t t h es a m et i m e .R E S T O R

R U N / S T O Pb r i n g s t h ec o m p u t e rs c r e e nb a c kt o i t s

o r i g i n a l s t a t ew h e nt h ep o w e rw a st u r n e do n .W h i l e

R E S T O R E - R U N / S T O Pc l e a r st h e s c r e e na n dc a u s e sm a n yp r o g r a m st o h a l t ,t h i ss p e c i a lk e y

b o a r dc o m m a n dw i l ln o t e r a s eB A S I Cp r o g r a m

f r o mm e m o r y .( W e ' l lt a l ka b o u th o wt o t r a pt h e

R E S T O R E - R U N / S T O Pc o m m a n di n t h ec h a p t e

o n e r r o rt r a p p i n g . )

2 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 33/194

C h a p t e r3

A Q u i c kT o u ro f D O SC o m m a n d s

W h i l et h e D O SS h e l lc o n t a i n e do n y o u r 1 5 7 1

T e s t / d e m od i s kw i l lp e r f o r mm o s td i s kp r e p a r a t i o n

a n dm a i n t e n a n c ef u n c t i o n sa u t o m a t i c a l l y ,t h e r ea r e

a f e wc o m m a n d sa n dc o n c e p t si t ' ss t i l li m p o r t a n t

t o k n o wa b o u t .T h e yc a nm a k et h ed i f f e r e n c eb e

t w e e ny o u rb e i n ga c o o l ,s o p h i s t i c a t e du s e ro r a

f r u s t r a t e db e g i n n e r .

P R E P A R I N GD I S K SF O R U S E

T h ef i r s tt h i n gt o r e a l i z ei s t h a ta l ln e wd i s k s

m u s tb e s p e c i a l l yp r e p a r e df o ru s eo n y o u rC o m

m o d o r e1 2 8 .I f y o u ' v ej u s tt a k e na n e wd i s kf r o m

i t sb o x ,o r t h e d i s kh a sb e e nu s e do na d i f f e r e n tt y p e

o f c o m p u t e r( s u c ha s a n A p p l e ,I B MP C ,o r R a d i o

S h a c k ) ,t h eH E A D E Rc o m m a n dm u s tb ei s s u e db ef o r et h e d i s kc a nb e u s e d .H E A D E Re r a s e sa l li n

f o r m a t i o nt h a tm a yh a v eb e e ns t o r e do na d i s k .A t

t h es a m et i m e ,t h eH E A D E Rc o m m a n ds e t su pt h e

d i s kf o ru s eo n t h eC o m m o d o r e1 2 8b y c r e a t i n g

e l e c t r o n i ct r a c k s( s e e F i g .3 - 1 )a n da na l l o c a t i o nm a p

t h a tt e l l st h ec o m p u t e rw h e r ee v e r y t h i n go n t h i s

d i s kw i l lb es t o r e d .T h i s p r o c e d u r ei s k n o w na sf o r

m a t t i n g .

B e c a u s ef o r m a t t i n g e r a s e s e v e r yo n t h e

d i s k ,y o us h o u l db ev e r yc a r e f u lw i t ht h eH E A D

c o m m a n d .I t i se a s i l yp o s s i b l et o w i p ec l e a na d i s k

c o n t a i n i n gi m p o r t a n ti n f o r m a t i o n .O n c et h e

H E A D E Rp r o c e s sb e g i n s ,y o u ' v el o s te v e r y t h

t h a tm a yh a v eb e e no n t h ed i s k .

T h ed i s km a yb e f o r m a t t e de i t h e rf r o mt h e

D O SS h e l l( a v a i l a b l eb yi n s e r t i n gt h e1 5 7 1d i s ki n t o

y o u rd r i v ea n ds t a r t i n gt h es y s t e m )o r b y e x e c u

i n g t h eH E A D E Rc o m m a n df r o mB A S I C .

W h e nt o H E A D E Ra D i s kf r o mB A S I C

W h e n e v e rp o s s i b l e ,f o r m a ty o u rd i s k sf r o mt h e

D O SS h e l l .I t ' sj u s ta s f a s t ,a n d y o ud o n ' th a v et o

r e m e m b e ra l lo f t h ew h y sa n dw h e r e f o r e so f t h e

H E A D E Rc o m m a n d ,w h i c hc a ng e tr a t h e rc o m p l

c a t e d .I t ' sa g o o di d e a ,i n f a c t ,t o u s et h eF O R M

o p t i o nf r o mt h eD O Ss h e l lw h e n e v e r y o ub u ya b o x

o fn e wd i s k s .S i m p l yf o r m a ta l l1 0d i s k sa t t h es a m e

2 3

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 34/194

T r a c k

i S e c t o r( b l o c k )

F o r m a t t e dd i s k sa r em a d eu p o f a s e r i e so f m a g n e t i ct r a c k s .D a t ai s s t o r e do n s e c t o r s o r

b l o c k s ,w i t h i ne a c ht r a c k .A t r a c km a yc o n t a i nb e t w e e n1 7 a n d2 1 s e c t o r sd e p e n d i n g o ni t s

p o s i t i o n .A d i s k ' s d i r e c t o r yi s l o c a t e do n t r a c k1 8 .

F i g . 3 - 1 .D a t at r a c k so n a m a g n e t i cd i s k .

t i m e .T h e ns t o r ea l l y o u rf o r m a t t e dd i s k si n a

c l e a r l yl a b e l e db o x .I f y o uf o l l o wt h i sp r o c e d u r e ,

y o u ' l ln e v e rw a n tf o rf o r m a t t e dd i s k s .

T h e r ea r et i m e sw h e nu s i n gt h eD O SS h e l lf o r

f o r m a t t i n gi s i m p r a c t i c a l .Y o um a yn e e dt o f o r m a t

a d i s kf r o mw i t h i na p r o g r a m ,o ry o um a ys u d d e n l y

r e a l i z ey o u ' r e o u to f f o r m a t t e dd i s k s .

L e t ' ss a yy o u ' v ej u s tw r i t t e na n e l e g a n t l yd e

s i g n e dh o m ef i l i n gp r o g r a m( p e r h a p st y p e df r o m

t h ep a g e so f t h i sb o o k ) ,a n dy o ur e m e m b e rt h a t

t h e r e a r en of o r m a t t e dd i s k sa n y w h e r e .Y o uc a n ' t

b o o tt h eD O SS h e l l ,s i n c ed o i n gs o w o u l d w i p eo u t

t h ep r o g r a my o uj u s tt y p e d .T h i si s w h e ny o ut y p e

t h eH E A D E Rc o m m a n dd i r e c t l yf r o mB A S I C .I f

H E A D E Rs t i l ls e e m sa b i tc o n f u s i n ga f t e ry o u ' v e

r e a dt h e f o l l o w i n gp a r a g r a p h s ,s i m p l y f o r m a ty o u rd i s k sb yt y p i n gt h eH E A D E Rc o m m a n de x a c t l ya s

i t a p p e a r si n t h i sb o o k .A sl o n g a sy o u rb l a n kd i s k

i s i n d r i v ez e r o( y o u rf i r s to r o n l yd r i v e ) ,t h i sc o m

m a n dw i l lw o r kf l a w l e s s l y .

E n t e r i n gt h eH E A D E R C o m m a n d

H E A D E Ri s o n eo f t h et r i c k i e rB A S I Cc o m

m a n d st o t y p e ,b e c a u s ei t i n c l u d e sf o u rp a r t s( s e e

F i g .3 - 2 )t h a tm u s ta l w a y sb e s p e c i f i e dt o c r e a t ea

n e wd i s k :

H E A D E R" W O R K D I S K " , I 3 5 , D

F o l l o w i n gt h eH E A D E Rc o m m a n di t s e l fi s t h e d i s

t i t l e ,w h i c hi s a l w a y se n c l o s e di n q u o t e sa n dm a y

b e u p t o 1 6 c h a r a c t e r si n l e n g t h .T h en e x ti t e mi s

t h eI D c o d e ,c o m p r i s e do f t h el e t t e rI f o l l o w e db y

a n yt w o - c h a r a c t e rI D c o d e .A n yl e t t e ro r n u m b e

i s a c c e p t a b l e ,b u tg r a p h i c sc h a r a c t e r s ,s u c ha s t h o s e

o b t a i n e db yu s i n gt h eO ( C o m m o d o r e )k e y ,w o n ' t

w o r k .F i n a l l y ,a d r i v en u m b e rm u s tb e s p e c i f i e

t h e l e t t e rD a n d a d r i v en u m b e rh a v et o b e

i n c l u d e d — e v e ni f y o uh a v eo n l yo n ed r i v e .I f y o u

h a v ea s i n g l ed r i v es y s t e m ,t h ed r i v ed e s i g n a t

i s D O( f o rd r i v ez e r o ) .O na s y s t e mw i t hd u a ld r i v e s

( s u c ha s t h eC o m m o d o r e1 5 7 2d r i v eu n i t ) ,t h e d e s i

n a t i o nc a nb e e i t h e rD Oo r D l , d e p e n d i n go n w h i c

d r i v ey o u ' r e u s i n gf o rt h e f o r m a t .

S o m e t i m e sy o u ' l lh a v ed i f f e r e n td r i v eh o u s i n g

c a l l e du n i t s ,c o n n e c t e dt o y o u rc o m p u t e r .I f y o u

h a v et w os e p a r a t ed r i v e st h a ta r e a s s i g n e dd i f f e r

2 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 35/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 36/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 37/194

T r i c k sw i t ht h eD i r e c t o r yC o m m a n d

I f y o u rd i s ki s c h o c kf u l lo f f i l e s( y o uc a ns t o r e

u p t o 1 4 4p r o g r a m so r d a t af i l e so n a d i s k )a c o m

p l e t ed i r e c t o r y o ft h ed i s kw o u l df i l lt h es c r e e n

s e v e r a lt i m e so v e r .T h ee n t r i e sa t t h eb e g i n n i n g

q u i c k l ys c r o l lo u to f v i e w ,p e r h a p sb e f o r ey o u ' v e

h a dt i m et o g e ta g o o dl o o ka t t h e m .F o r t u n a t e l y ,t h e D I R E C T O R Yc o m m a n d

o f f e r ss e v e r a lw a y st o " n a r r o wd o w n "t h en a m e s

o f t h ef i l e sy o uw a n tt o s e e .I f y o uw a n t e dt o c o n

f i r mt h a tt h ef i l eL E T T E Rw a so n t h e d i s ki n t h e

d e f a u l td r i v e ,y o uc o u l dt y p e :

D I R E C T O R Y" L E T T E R "

Y o uc o u l da l s ov i e wa d i r e c t o r yo fa l lf i l e ss t a r t

i n gw i t ht h el e t t e r sL E ,b yu s i n ga g l o b a l f e a t u r e :

D I R E C T O R Y" L E * "

T h ea s t e r i s k( * )i s a w i l d c a r ds y m b o lt h a tt e l l st h e

c o m p u t e r ," S h o wa n yf i l e n a m e sb e g i n n i n gw i t hL E ,

r e g a r d l e s so f h o wt h e y e n d . "S u c ha c o m m a n d

w o u l ds h o wt h ef o l l o w i n gf i l e n a m e s ,i f t h e yw e r e

o n t h ed i s k :

L E T T E R

L E D

L E T T O R

L E T SG OF I S H

A n o t h e rw i l d c a r dc h a r a c t e r ,t h eq u e s t i o nm a r k ,

i s u s e dt o r e p r e s e n ta s i n g l el e t t e r .T h ec o m m a n d :

D I R E C T O R Y" L E T T P R "

W o u l dp u l lu p o n l y t h e f o l l o w i n gf i l e n a m e sf r o m

t h el i s ta b o v e :

L E T T E R

L E T T O R

T h ew i l d c a r df e a t u r e sc a nb ev e r yu s e f u lw h e n

y o ua r es o r t i n gt h r o p g hl o t so fd i s k sc o n t a i n i n gl o t s

o f d i f f e r e n tp r o g r a m sa n dd a t af i l e s .

S A V I N GP R O G R A M SO ND I S K

O n c ey o u ' v ew r i t t e na p r o g r a m — i n d e ep e r i

o d i c a l l yw h i l e y o u ' r e w r i t i n gi t — y o us h o u l ds t o r e

t h ep r o g r a m o nd i s k .U n t i la p r o g r a mi s s t o r e do n

d i s k ,t h e o n l yc o p y y o u h a v ei s i n t h ec o m p u t e r

r a n d o ma c c e s sm e m o r y ,a v o l a t i l et e m p o r a r ys t o r a g

a r e at h a t f o r g e t se v e r y t h i n gi t k n o w sa s s o o na s t h ep o w e rg o e so u t o ry o ut u r no f ft h em a c h i n e .

F r e q u e n ts a v i n go f a p r o g r a me n s u r e st h a ta

p o w e rg l i t c ho r s o m eo t h e re l e c t r o n i cm i s h a pw o n ' t

w i p eo u ty o u rw o r k .

S a v i n gp r o g r a m so nd i s ki s a s i m p l ep r o c e d u

g e n e r a l l y .H e r e ' sa n e x a m p l eo f t h e D S A V

c o m m a n d :

D S A V E" S T O C KM K TS Y S T E M "

T h e n a m eo f t h ef i l e ( p r o g r a m ) t ob e s a v e ds h o u l

a l w a y sb e e n c l o s e di n q u o t e sa n dc a nb e u p t o s i x

t e e nc h a r a c t e r si n l e n g t h .T h ef i l e n a m em a yn o t

s t a r tw i t ha n u m b e r ,b u tn u m b e r sm a yb e i n c l u d e

a f t e rt h ef i r s tc h a r a c t e r( s e e F i g .3 - 4 ) .

T h e r ea r es o m es p e c i a l r u l e s t ob e a w a r eo f i f

y o uw a n tt o e n s u r et h a ty o u ' v es a v e d ap r o g r a

p r o p e r l y .I f y o up e r f o r mt h eD S A V Ep r o c e d u r ei n

c o r r e c t l y ,i t ' sp o s s i b l e t ot h i n kt h a ta p r o g r a mh a s

b e e ns a v e dw h e ni t r e a l l y h a s n ' t .I t i s a l s o p o s s i

t o a c t u a l l yd a m a g ei n f o r m a t i o no n a d i s ka t t h e

s a m et i m e .

R u l e# 1 .A l w a y su s et h eD I R E C T O R Yc o m

m a n db e f o r es a v i n ga p r o g r a m .T h ef i r s tr e a s o ni s

t h a ti t ' sa l w a y s ag o o di d e at o c o n f i r mt h a ty o u ' r e

s a v i n gi n f o r m a t i o no n t h er i g h td i s k .T h es e c o n

r e a s o ni s e v e nm o r ei m p o r t a n t :i f y o u ' v es w a p p e

d i s k s ,t h ec o m p u t e rc a ns o m e t i m e sb e c o m ec o n

f u s e d ,t h i n k i n gt h eo l dd i s ki s s t i l li n t h ed r i v e .Y o u r

p r o g r a mc a ns o m e t i m e sb e w r i t t e no u tt o a r e a so f

t h e d i s kt h a ta r e a l r e a d ys p o k e nf o r ,n o t o n l yg a r

b l i n gt h ep r o g r a mt h a ty o u ' r et r y i n gt o s a v e ,b u tm a k i n gm i n c em e a to u to fe x i s t i n gf i l e so nt h en e w

d i s k .T h eD I R E C T O R Yc o m m a n df o r c e st h ec o m

p u t e rt o r e a d t h e a r e ao f t h e d i s kt h a tt e l l sw h a t

s p a c ei s a v a i l a b l e .

I f y o u ' r eu s i n go l d e rC o m m o d o r ed r i v e s ( s u

a s t h em o d e l4 0 4 0 ) ,i t ' sa l s oa g o o di d e a t o t y p

2 7

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 38/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 39/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 40/194

T w os e p a r a t eu n i t s

A s i n g l eu n i t ,t w od r i v e s

F o u rd r i v e s ,t w ou n i t s

d r i v e0 , u n i t8

d r i v e0 , u n i t9

d r i v e0 , u n i t8

d r i v e1 , u n i t8

d r i v e0 , u n i t8

d r i v e l ,u n i t8

d r i v e0 , u n i t9

d r i v e l ,u n i t9

F i g . 3 - 7 .D i f f e r e n t d r i v ea r r a n g e m e n t s .

g i v e s t h eC o m m o d o r e1 2 8a d i f f e r e n tw a yt o r e f e r

t o s p e c i f i c d r i v e s .

I f y o uh a v eo n l yo n ed i s k d r i v ea n dp l a nt o a d d

n om o r e , t h e nr e f e r r i n g t oi n d i v i d u a ld r i v e si s r a r e l y

a p r o b l e m .T h eo n l yc o m m a n dt h a tw i l lr e q u i r ey o u

t o s p e c i f ya d r i v en u m b e ri s t h eH E A D E Rc o m

m a n d , w h i c hi s u s e dt o p r e p a r ea d i s k ,a n d w h i c h

w e ' l l t a l ka b o u ta f e wp a g e sf r o mn o w .T h et w o

d r i v en u m b e r sa v a i l a b l ea r ez e r o( f o rt h ef i r s td r i v e )

a n do n e( f o rt h es e c o n dd r i v e ) .I t g e t sm o r ec o m

p l i c a t e d .

I f y o uh a v ea d u a ld r i v es y s t e m( s u c h a s t h e

1 5 7 2 )i n w h i c ht w od r i v e sa r eb u i l t i n t oo n ep h y s i

c a lh o u s i n g ,t h e nt h es e c o n dd r i v ei s r e f e r r e dt o a

d r i v e o n e .B o t hd r i v e s a r e p a r to f u n i tn u m b e r8 .

E i g h ti s t h e d e v i c en u m b e rt h a tC o m m o d o r el o n g

a g oa s s i g n e df o ri t s f i r s td r i v eu n i t .

T w oU n i t s ,T w oZ e r o e s

D r i v ea n du n i tn u m b e r sw o r ks o m e w h a td i f f e r

e n t l yw i t ht w oi n d i v i d u a ld r i v e s .I f y o uh a v et w o

s e p a r a t ed r i v eu n i t s ,b o t ho f t h e mw i l lb e r e f e r r e

t o a s d r i v ez e r o .T h e s ed r i v e sa r ei d e n t i f i e d ,t h e n

n o tb yd r i v en u m b e r ,b u tb yu n i tn u m b e r .T h ef i r s t

d i s kd r i v ew i l lb ed r i v ez e r o ,u n i t e i g h t( a b b r e v i ai n c o m m a n d sa s D 0 , U 8 ) ,a n dt h es e c o n dd r i v ew i l l

b e r e f e r r e dt o a s d r i v ez e r o , u n i tn i n e( D 0 , U 9 )

C o m m o d o r ed i dn o t d e s i g nt h es y s t e mt o b e

c o n f u s i n g — i tj u s tt u r n e do u tt h a tw a y .B u te v e n

t u a l l y ,y o u ' l lg e tu s e dt o t h e i d e at h a ts i n g l ed i s k

d r i v eu n i t sa r e r e f e r r e dt o a s u n i t e i g h ta n dn i n e

a n dt h a td u a ld r i v es e t u p sh a v et w od r i v e s( D Oa n d

D l )t h a ta r e p a r to f o n eu n i t( U 8 ,U 9 ,U 1 0 , a n ds o

o n ) .

G e n e r a l l y ,t h ef i r s td r i v ec o n n e c t e dt o t h e s y

t e mi s d r i v e0 , u n i te i g h t .Z e r oa n do n ea r e t h et w od r i v en u m b e r sa v a i l a b l eo no n ed u a l - d r i v ed e v i c eo r

u n i tE i g h ti s o n eo f s e v e r a ld e v i c en u m b e r st h a t

d r i v e sc a nb e a s s i g n e d .

T o m a k em a t t e r se v e nm o r ec o m p l e x ,s o m

c o m m a n d sw o r kw h e nb o t hd r i v e sa r ei n o n eu n i t

( s u c h a s t h ed u a l - d r i v e1 5 7 2 )b u td o n ' tw o r ki f y o u

h a v et w os e p a r a t ed r i v eu n i t s ,s u c ha s 1 5 7 1 so r

1 5 4 1 s .F o r t u n a t e l y ,C o m m o d o r eh a si n c l u d e dt h e

D O SS h e l lo n i t ' s T e s t / D e m od i s k ;i t p e r f o r

m a n yd i s kc o m m a n d sa n dh a n d l e sa l lt h eu n i t / d r

c o n f u s i o nf r o mb e h i n dt h es c e n e s .I f y o u ' r eg o i n

t o b e p e r f o r m i n gc e r t a i nd i s ko p e r a t i o n s ,i t ' sm u c

e a s i e r t ou s et h eD O SS h e l l .F o re x a m p l e ,t h eD O SS h e l li s t h eo n l yw a yt o t r a n s f e rf i l e sb e t w e e ns e p

a r a t e d r i v eu n i t s .

A c c e s s i n gD e v i c e s

I t m a yh e l pt o t h i n ko f d i s kd r i v e sa s s i m p l

a t y p eo f d e v i c et h a tt h ec o m p u t e rc o n t r o l s .W h i l

t h ed r i v e sa r er e f e r r e dt o a s u n i t e i g h to r n i n e ,o t h e

p e r i p h e r a l sa t t a c h e dt o t h ec o m p u t e ra r er e f e r r

t o a s d e v i c e s .I n f a c t ,a s f a ra s t h ec o m p u t e ri s c o n

c e r n e d ,u n i t sa n dd e v i c e sa r e t h es a m et h i n g .F o r

e x a m p l e ,c o m m a n d st h a t r e l a t e t ot h ep r i n t e rg e n e

a l l yr e f e rt o d e v i c ef o u ro r f i v e .T h es c r e e nc a nb e

r e f e r r e dt o b ya d e v i c en u m b e r( z e r o ) ,a l t h o u g hi n

f o r m a t i o na u t o m a t i c a l l yg o e st o t h es c r e e nu n l e s

y o ud i r e c ti t s o m e w h e r ee l s e .E v e nt h ek e y b o a

3 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 41/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 42/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 43/194

t h es c r e e nt o t h ep r i n t e r .A n dt h e r e a r et w om o r e

t h a tt u r no f fo r c l o s eo u tt h ep r i n t e r .Y o uc a n u s e

t h i st y p eo f s t r u c t u r ea n yt i m ey o uw i s ht o p r i n t

i n f o r m a t i o n . J u s t p l a c ea s m a n yp r i n ts t a t e m e n t s

a s n e c e s s a r yb e t w e e nt h et w os e t so f c o m m a n d s .

I f y o u w a n tt o p r i n ts o m el i n e so n t h e s c r e e na n d

s o m eo nt h ep r i n t e r ,s i m p l yt u r n t h ep r i n t e ro na n d

o f fu s i n gt h eO P E N ,C M D ,P R I N T * ,a n dC L O S E

c o m m a n d s .

U s i n gD u p l i c a t eF i l ea n dD e v i c eN u m b e r s

M a n yu s e r sl i k et o u s et h es a m ef i l ea n dd e

v i c en u m b e r s ,s o t h e yc a ne a s i l yr e m e m b e rw h i c h

f i l e( c h a n n e l )i s a s s i g n e dt o w h a td e v i c e .U s i n gt h i s

a p p r o a c h ,t h ec o m m a n d sw o u l dl o o kl i k et h i s :

O P E N4 , 4

C M D4P R I N T " H E L L O "

P R I N T # 4

C L O S E4

N o t et h a tt h e s ec o m m a n d sc a nb e i n c l u d e di n .

a p r o g r a mo r t y p e dd i r e c t l yf r o mt h ek e y b o a r d .

S e n d i n gF i l e st o D i s k D r i v e s

T h eO P E Nf e a t u r ei s u s e di n a s l i g h t l y d i

e n tw a yw h e na p r o g r a mm u s ts e n di n f o r m a t

i n t oa d a t af i l e .T h es e c t i o n so f t h i sb o o ko nS e q u e

t i a lF i l e sa n dR a n d o mF i l e sc o v e rt h eC o m m o d

1 2 8 ' sa b i l i t i e si n t h i sa r e ae x t e n s i v e l y .

S e n d i n gF i l e st o t h eS c r e e n

A so d da s i t s e e m s ,t h e r e a r es o m eo c c a s i o

w h e ny o um i g h tw a n tt o t r e a tt h e s c r e e n a sa f i l e .

F o re x a m p l e ,w h a ti f y o u w a n t e dt o l e tt h eu s e rd e

c i d ew h e t h e ri n f o r m a t i o ns h o u l db e d i s p l a y e do n

t h es c r e e no r s e n tt o a p r i n t e r .I f y o us i m p l yp r e d e

f i n ef i l e1 b a s e do n t h eu s e r ' sr e s p o n s e , t h eC M D1 c o m m a n dc a nb e u s e di n y o u rp r o g r a mt o d i r e c

o u t p u tt o t h er i g h t p l a c e :

1 0 I N P U T " T OS C R E E NO R P R I N T ( S / P ) " ; A N S $

1 5 :

2 0 I F A N S $= " P "T H E NO P E N1 , 4 :E L S E

O P E N1 , 3

2 5 :

3 0 C M D 1

1 . M a k es u r et h ed i s kh a sb e e np r e p a r e d( i ft h ed i s ki s n e w ,u s et h eH E A D E Rc o m m a n d ,

o r f o r m a ti t u s i n g t h eD O Ss h e l l ) .

2 . T y p eD I R E C T O R Yt o v i e wt h ed i s k ' sc o n t e n t s .

3 . T y p et h eD S A V Ec o m m a n d :

I f s a v i n gf o rt h ef i r s tt i m e :

D S A V E" M YP R O G R A M "

I f s a v i n gt o r e p l a c e :

D S A V E" @ M YP R O G R A M "

4 . C h e c kt h ed r i v el i g h tt o m a k es u r et h a ti t i s n o tf l a s h i n ga f t e rt h eR E A D Ym e s s a g ea p p e a r s .

5 . T y p et h eD I R E C T O R Yc o m m a n dt o c o n f i r mt h a tt h ef i l ew a sp l a c e do n t h ed i s k .

6 . P e r f o r ma D V E R I F Yt o e n s u r et h a tt h ep ' r o g r a mw a ss a v e dc o r r e c t l y :

D V E R I F Y " M YP R O G R A M "

F i g .3 - 1 0 .S t e p sf o rs a v i n ga p r o g r a m .

3 3

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 44/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 45/194

C h a p t e r4

S e a r c h i n gf o rI n f o r m a t i o

A p p l i c a t i o n sf o rs e a r c h i n ga r ew i d er a n g i n gi n d e e d .

T h e yi n v o l v em u c hm o r et h a n t h e s i m p l el o c a t i o n

o fa n a m eo ra na d d r e s s .F o re x a m p l e ,y o uc a n u s e

s e a r c h e st o h a v ey o u rp r o g r a m sa u t o m a t i c a l l yl o o k

u pa n dp r o c e s si n f o r m a t i o n .I n a d d i t i o nt o p e r f o r m

i n gc a l c u l a t i o n sa n ds t o r i n gi n f o r m a t i o n ,o n eo fy o u r

c o m p u t e r ' sp r i n c i p a lf u n c t i o n si s t o s e a r c hf o ri n

f o r m a t i o n .

H O WT OS E A R C H

W h i l et h e r e ' sn o " s e a r c h "c o m m a n dt h a tw i l l

f i n d i n f o r m a t i o n ,t h e r e a r em a n ym e t h o d so fs e a r c h

i n gf o rd a t aw i t h a c o m p u t e r .

E s s e n t i a l l y ,a l ls e a r c h i n gm e t h o d si n v o l v ef i n d

i n ga m a t c h - u pb e t w e e n t w oo r m o r ei t e m s .I n f a c t ,

y o u ' r ep r o b a b l yf a m i l i a r

w i t ht h e s i m p l e s tt y p eo f

s e a r c h ,e v e ni f y o u ' v en e v e rw r i t t e na s e a r c hr o u

t i n eb e f o r e .

T a k ea l o o ka t t h eH A P P YH O M E M A K E Rp r o g r a m ,l i s t e di n F i g .4 - 1 ,w h i c hc o n t a i n s a ss i m

p l ea s e a r c ha s y o u ' r el i k e l yt o f i n da n y w h e r e .A f

t e ra c c e p t i n ge n t r i e sf r o mt h eh u s b a n d a n dw i f e ,

t h ep r o g r a ml o o k sf o ra m a t c hb e t w e e nt h ew o r d

Y E Sa n dt h e c o n t e n t so ft h eH U S B N D $a n dW F E v a r i a b l e s .I f e i t h e ro f t h e s ev a r i a b l e si s e q u a lt o t h e

w o r d Y E S ,l i n e9 0 i n s t r u c t st h ec o m p u t e rt o p r i n t

" G o o d ,I ' l lm a k es o m et h e n . "

T h ec o m p u t e rh a si n e f f e c tl o o k e df o ra m a t c

w i t h i nt w ov a r i a b l e s( H U S B N D $a n dW F E $ )f o r

t h ew o r dY E S ,t a k i n ga d i f f e r e n ta c t i o nd e p e n d i

o n w h e t h e ro r n o ti t i s f o u n d .

I f y o u ' r ea l i t t l eu n s u r eo f h o wt h ep r o g r a

w o r k s ,t r yt y p i n gi t i n a n dr u n n i n gi t . A f e wt r i a l

r u n ss h o u l dm a k ec l e a rw h a t ' sh a p p e n i n gi n t e r n a l

a s y o ug i v ed i f f e r e n tr e s p o n s e st o t h et w oq u e s t i o n

T h ep r o c e d u r ei n t h eH a p p yH o m e m a k e p r o

g r a mw o r k ss p l e n d i d l yw i t ht w og u e s t s .B u tw h a t

a b o u t al i s to f f i v eg u e s t s ?O rt e n ?T h es a m ep r o c e

d u r ec o u l db e r e p e a t e di n a n e x p a n d e df o r m ,b u tl o o ka t h o wc o m p l i c a t e dt h i n g sb e c o m ew i t hf i v e

v a r i a b l e sb e i n gu s e d :

9 0 I F A $ = " Y E S "O R B $= " Y E S "O RC $ = " Y E S "O R D $ = " Y E S "O RE $= " Y E S "T H E NP R I N T" G O O D ,I ' L L

M A K ES O M E "

3 5

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 46/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 47/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 48/194

i s f o u n d , t h ep r o g r a mp r i n t s" Y e s ,w eh a v et h a t ! " s c r e e nn u m b e r .T h e r ea r em a n yd i f f e r e n td i s p l a

e x i t st h el o o p ,a n dt h e nr e t u r n sf r o mt h es u b r o u t i n e ,s c r e e n so n t h eC o m m o d o r e1 2 8 ,a n dm a n yd i f f e r

A f e wl i n e sa t t h et o pa r en e e d e dt o r u nt h ep r o - e n tw a y st o c l e a rt h e m ,a l l o f w h i c hw i l lb e d i s -

g r a m ,w h i c hi s s h o w na s a w h o l ei n F i g .4 - 6 . c u s s e dt h o r o u g h l yi n t h ec h a p t e r so ng r a p h i c sa n d

L i n e 1 0d i m e n s i o n s t h ev a r i a b l eF O O D $ ,w h i c h p r o f e s s i o n a lp r o g r a md e s i g n . )

i s a n o t h e rw a yo f s a y i n gt h a tt h ep r o g r a mi s r e s e r v - L i n e s3 0 ,4 0 ,a n d5 0 c a l lt h e s u b r o u t i n ei n d i -

i n gi n m e m o r yf o rt h i sl i s to f i n f o r m a t i o n .W h i l e c a t e di n e a c hR E M a r ks t a t e m e n tt o t h er i g h t .L i n e

i t ' sn o tr e q u i r e dt h a ty o ud i m e n s i o na n a r r a yt h a t 7 0 e n d st h ep r o g r a m .

w i l lh a v ef e w e rt h a n t e ni t e m s ,u s i n gt h eD I Ms t a t e - A g a i n ,a l lt h el i n e sc o n t a i n i n gs o l i t a r yc o l o n

m e n ti s a g o o dp r a c t i c e t og e ti n t o ,b e c a u s em o s t ( : )a r e i n c l u d e ds i m p l yf o ra p p e a r a n c e ;t h e yd i v i d

o f y o u rl i s t sw i l lc o n t a i na t l e a s tt e ni t e m s . u p t h el i n e s ,b u td o n ' ta f f e c tp r o g r a mo p e r a t i o

L i n e2 0 c l e a r st h e4 0 - c o l u m ns c r e e n u s i n gt h e E v e nt h o u g hy o u ' r e s e a r c h i n gt h r o u g ha l o n g e

S C N C L R0 c o m m a n d .( Z e r oi s t h e 4 0 - c o l u m nl i s to f i n f o r m a t i o nt h a ni n t h ef i r s te x a m p l e( w h e r

5 r e m : s i m p l ef o o d :

1 0 d i m f o o d $ ( 5 )

2 0 s c n c l r0

3 0 g o s u b 5 0 0 0: r e m g e te n t r y

4 0 g o s u b6 0 0 0 0 : r e m l i s t i n t om e m o r y

5 0 g o s u b 1 0 0 0: r e md o s e a r c h

6 0 :

7 0 e n d

8 0 :

1 0 0 0r e m : s e a r c h r o u t i n e :

1 0 0 5f o u n d = 0 : a t = 0

1 0 1 0d o u n t i la t = l a s t

1 0 2 0: a t = a t + 1

1 0 4 0: i f r e q u e s t $ = f o o d $ ( a t )t h e np r i n t " y e s ,w e h a v e t h a t ! " : e x i

1 0 6 0l o o p

1 0 8 0 r e t u r n

1 0 9 0:

2 0 0 0:

5 0 0 0r e m : i n p u t r o u t i n e :

5 0 2 0p r i n t " w h a tw o u l dy o u l i k e "

5 0 4 0 i n p u t " f o rd i n n e rN ; r e q u e s t s

5 0 6 0:

5 0 7 0r e t u r n

6 0 0 0 0 r e m : l o a du p l i s t :

6 0 0 1 0f o o d $ ( 1 ) » " c h i l id o g "

6 0 0 2 0f o o d $ ( 2 ) = " h a m b u r g e r "

6 0 0 4 0f o o d $ ( 3 ) = " p i z z a "

6 0 0 6 0f o o d $ ( 4 ) * « p a e l l a "6 0 0 8 0f o o d $ ( 5 ) * " f i l e tm i g n o n "

6 0 0 9 0l a s t = 5 : r e m t h e r ea r e 5 i t e m s

6 0 1 0 0r e t u r n

F i g . 4 - 6 .A c o m p l e t el i s t i n go f t h eS i m p l eF o o dp r o g r a m .

3 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 49/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 50/194

u s eo fa v a r i a b l ef l a gh a sg i v e nu s g r e a t e rf l e x i b i l

i t y ,b e c a u s et h ep r o g r a m" k n o w s " — a ta n yp o i n t

a f t e rt h es e a r c h — w h e t h e rt h er e q u e s t e di t e mh a s

b e e nf o u n d ,a n di t r e m e m b e r st h r o u g h o u tt h er e s t

o f t h ep r o g r a m .

L o o kw h a te l s eo u rF O U N Df l a gh a sb o u g h tu s !

A D O . . .U N T I Ll o o pt h a tw o r k so nF O U N Da n d

l e t st h e p r o g r a mc o n t i n u e r e q u e s t i n gl u n c h e o n

o r d e r su n t i la f o o d i t e mi s l o c a t e d .N o to n l yh a st h e

f l a gn e a t e n e du p t h ep r o g r a m a n dm a d ei t m o r e

r e a d a b l e ;i t h a sa l s oa l l o w e du s t o b em o r ef l e x i b l e

w i t hw h a tw ed oo u t s i d eo f t h es e a r c hs u b r o u t i n e .

T h eE a s i e s tK i n do f T e s t :T r u e / F a l s e

T h e r e ' sa n o t h e rt r i c kw ec a nu s et o m a k et h e

p r o g r a me v e nm o r er e a d a b l e .A k i n do f p r o g r a m

m i n gs h o r t h a n dt h a te n a b l e sy o ut o r e m o v et h e

» - 1 p a r to f t h et e s ti s a v a i l a b l ew h e ny o ua r eu s

i n gf l a g s .

H e r e ' sa n e x a m p l e :

I F F O U N D = - 1 T H E NP R I N T" F O U N D

Y O U RI T E M ! "

a n d

I F F O U N DT H E NP R I N T" F O U N DY O U R

I T E M ! "

p e r f o r me x a c t l yt h es a m ef u n c t i o n .B u tt h es e c o n d

s t a t e m e n ti s m o r er e a d a b l e , r e q u i r e sf e w e rk e y

s t r o k e s ,a n da c t u a l l ye x e c u t e so n t h eC o m m o d o r e

1 2 8 as p l i t - s e c o n d f a s t e rt h a nt h ef i r s ts t a t e m e n t .

T h es a m et h i n gc a nb e d o n ew i t h aD O. . .

W H I L Eo r D O. . . U N T I Ll o o p :

D OU N T I LF O U N D = - 1

b e c o m e s :

D OU N T I LF O U N D

T h e r e ' so n l yo n es m a l lh i t c h :t h i st r i c kw o r k s

o n t h eC o m m o d o r e1 2 8o n l yw h e ny o uu s e af l a g

s e ta t ( e q u a lt o )n e g a t i v eo n e( - 1 ) .I f F O U N D= 0 ,

f o re x a m p l e ,t h ep r o g r a m w o u l ds t i l lc o n s i d e r

i t e mN O TF O U N D .S e t t i n g t h eF O U N Dv a r i a b

e q u a lt o o t h e rn u m b e r ss u c ha s + 1 , + 5 ,o f - 3

y i e l d sc o n f u s i n gr e s u l t s .N o wy o uk n o ww h yw e

u s e d- 1 t o s e tt h ef l a g i no u rp r e v i o u se x a m p l

V a r i a b l e st h a ta r e u s e di n t h i sm a n n e ra r e

c a l l e dB o o l e a nv a r i a b l e so r s i m p l yB o o k a n s ,a f t e rt h e

E n g l i s hl o g i c i a nG e o r g eB o o l e ,w h of i g u r e do u t

m o r et h a na h u n d r e dy e a r sa g ot h a tm a t h e m a t

v a r i a b l e sc a nb e u s e dt o l a b e lt h i n g sa s T R U Eo r

F A L S E .

T ot h eC o m m o d o r e1 2 8 ,a v a r i a b l ee q u a lt o - 1

i s a l w a y sc o n s i d e r e dT R U E( w h e nu s e di n t h et y p e

o f t e s ta b o v e ) ,a n da v a r i a b l ee q u a lt o z e r oi n t h i

t y p eo f t e s ti s a l w a y sc o n s i d e r e dF A L S E .

A c c e n t u a t i n g t h e N e g a t i v e

T h e r e ' so n eo t h e rt r i c kw e c a np e r f o r mw i t h

B o o l e a n s — o n et h a ta g a i nw i l lm a k ey o u rp r o g r a

e a s i e rt o r e a d .L e t ' ss a yt h a ti n s t e a d o fu s i n ga D O. . . U N T I Ll o o pa s i n t h ee x a m p l ea b o v e ,y o ud e

c i d e t ou s ea D O. . . W H I L Es t r u c t u r e .Y o u ' v

p r o b a b l ya l r e a d yf i g u r e do u tt h a tD O. . . W H I La n dD O. . . U N T I La r eo p p o s i t ew a y so f a c c o m

p l i s h i n gt h es a m et h i n g .I n o u rf i r s te x a m p l e :

D OU N T I LF O U N D = - 1

a n d

D OW H I L EF O U N D= 0

w o u l dh a v ee x a c t l y t h es a m ee f f e c t( b e c a u s ew h e

F O U N Di s s e t t o- 1 , i t c a nn o l o n g e r e q u a lz e r o

a n dt h e l o o pw i l le n d ) .I t p r o b a b l yw o n ' ts u r p r i

y o ut o k n o wt h a tt h e r e ' sa s h o r t c u tt o t h i sn e g a

t i v ew a yo f l o o k i n ga t t h i n g s , t o o .A sw em e n t i o n

p r e v i o u s l y ,t o t h eC o m m o d o r e1 2 8a f l a ge q u a lt o

z e r oi s c o n s i d e r e dN O T T R U E( i no t h e rw o r d sF A L S E ) .I f t h i si s t h ew a ya t e s tf o rT R U Ew o r k s

D OU N T I LF O U N D

h o wd o y o ut h i n ka t e s tf o rN O T T R U Ew o u l

w o r k ?

4 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 51/194

T ot e s tf o rN O T T R U E( F A L S E )f l a gs e t t i n g ,

s i m p l yp l a c eN O Ti n f r o n t o ft h ev a r i a b l en a m e :

D OW H I L EN O TF O U N D

i s t h es a m ea s :

D OW H I L EF O U N D= 0

w h i c hi s t h es a m ea s :

D OU N T I LN O TF O U N D

A s w e u s e t h e s eB o o l e a nr e p r e s e n t a t i o n s

t h r o u g h o u tt h i sb o o k ,y o u ' l ls e eh o wm u c hc l e a n e r

a n dm o r e' ' E n g l i s h - l i k e ' 't h e ym a k et h ep r o g r a m s

a p p e a r .G e n e r a l l y ,t h i sb o o kw i l ln o tu s et h en e g a

t i v ea p p r o a c hs h o w na b o v e ,s i n c ei t so p e r a t i o nc a nb e c o m ec o n f u s i n go n t h e C - 1 2 8 .W ew i l li n c o r

p o r a t e t h eB o o l e a nF O U N Dt e s ti n t oo u r n e x t

s e a r c he x a m p l e ,w h i c hi l l u s t r a t e sh o wr e l a t e db i t s

o f i n f o r m a t i o nm a yb e d i s p l a y e da f t e ra s e a r c h .

S E A R C H I N GF O RR E L A T E DI N F O R M A T I O N

S o f a rw e ' v eb e e nu s i n gr o u t i n e st h a tp l u c k a

g i v e ni t e mf r o ma l i s tb a s e do n a p o s i t i v em a t c h .

T h e r ea r em a n yt i m e s ,h o w e v e r ,w h e ny o u ' l lw a n t

y o u rp r o g r a m st o d o m u c hm o r et h a nt o s i m p l y

v e r i f yw h e t h e ra p a r t i c u l a ri t e me x i s t si n a l i s t .F o r

e x a m p l e ,i n a n a d d r e s sl i s t i n g ,y o um i g h tw a n tt h e

c o m p u t e rt o d i s p l a ya n i n d i v i d u a l ' sa d d r e s sa n d

t e l e p h o n en u m b e r ,i n a d d i t i o n t os i m p l y c o n f i r m

i n gt h a tt h ep e r s o ni s o n t h el i s t .

L e t ' sg o b a c kt o o u rC o m m o d o r eI n ne x a m p l e ,

w h e r ew ef i n d t h a tt h ec h e fh a sa d d e da n e wf e a

t u r et o t h eb i l lo f f a r e .N o w ,i n a d d i t i o n t ot h em a i n

c o u r s e( s u c ht h a ti t i s ) ,a c u s t o m e rg e t st w os i d e

d i s h e s .F o ri n s t a n c e ,w i t ha h a m b u r g e r y o un o wg e t

f r e n c hf r i e sa n da s a l a d .W i t ha c h i l id o g ,y o ug e t

s o u pa n d c r e a m e dc o r n .

T h e s et y p e so fa d d i t i o n a li t e m sa r ee a s yt o r e p

r e s e n to n t h ec o m p u t e r .Y o u ' l lr e m e m b e rt h a to u r

l a s te x a m p l eu s e d as i n g l el i s to ff i v ei t e m s s t a c

i n a s i n g l ec o l u m n .W h a ti f w e a d d e dt w om o r e

c o l u m n st o r e p r e s e n t t h et w os i d e d i s h e s ?W e ' d

t h e nh a v et h r e ec o l u m n so f i n f o r m a t i o n ,f i v er o w s

d e e p ,a s s h o w ni n F i g .4 - 8 .

I n f o r m a t i o na r r a n g e di n t h i sw a yi s c a l l e da t w o -

d i m e n s i o n a la r r a y .T h e f i r s td i m e n s i o n( r o w )

r e p r e s e n t so n eo ft h ef i v el u n c h e o ns e l e c t i o n sa v a i l

a b l ea t t h eC o m m o d o r eI n n .T h es e c o n d d i m e

( c o l u m n )i s u s e dt o r e f e r t o e i t h e rt h em a i nc o u r s ( c o l u m n1 ) o r t h et w os i d ed i s h e s( c o l u m n s2 a n d3 ) .

T h i st y p eo f a r r a n g e m e n ti s q u i t eu s e f u lw h e n

y o u w a n tt h ec o m p u t e rt o s t o r ea n dr e t r i e v es e t s

o f r e l a t e d i n f o r m a t i o n .

I n o u rl a s tt w ot r i p st o t h eC o m m o d o r eI n n ,w e

u s e ds t a t e m e n t ss u c ha s :

F O O D $ ( 2 )= " H A M B U R G E R "

t o r e f e ri t e m si n t h el i s t .W ea l s os u b s t i t u t e da v a r i

a b l ef o rt h en u m e r i cc o n s t a n tt o c o m eu p w i t h :

I F R E Q U E S T $= F O O D $ ( A T )T H E N. . .

T h i st r i c ka l l o w e dt h e c o m p u t e rt o r e f e r t

d i f f e r e n ti t e m sb ys i m p l yc h a n g i n gt h ev a l u eo fA T .

B u th o wd ow eh a n d l er o w so f i t e m st h a th a v em o r e

C O L U M N1

R O W

1 . C h i l iD o g2 . H a m b u r g e r

3 . P i z z a

4 . P a e l l a

5 . F i l e tM i g n o n

C O L U M N2

S o u pF r e n c hF r i e s

M o o nP i e

P l a n t a i n s

R i c eP i l a f

C O L U M N3

C r e a m e dC o r n

S a l a d

S a l a d

B l a c kB e a n s

S a l a d

F i g .4 - 8 .A t h r e e - c o l u m nm e n ua r r a y .

4 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 52/194

6 0 0 0 0r e m : l o a du p l i s t :

6 0 0 1 0f o o d $ ( 1 , 1 ) = H c h i l id o g H : f o o d $ ( 1 , 2 ) = t t s o u p t f : f o o d $ ( 1 , 3

c o r n "

6 0 0 2 0f o o d $ ( 2 , 1) = t l h a m b u r g e r t f : f o o d $ ( 2 , 2 ) = M f r e n c hf r i e s H : f o o d $ ( 2 , 3 )

• • s a l a d "

6 0 0 4 0 f o o d $ ( 3 , 1 ) = t l p i z z a H : f o o d $ ( 3 f 2 ) = M m o o np i e " : f o o d $ ( 3 , 3 ) = " s a l a6 0 0 6 0f o o d $ ( 4 > 1 ) = f f p a e l l a t t : f o o d $ ( 4 t 2 ) = l t p l a n t a i n s " : f=

" b l a c kb e a n s "

6 0 0 8 0f o o d $ ( 5 , 1 ) = " f i l e t m i g n o n " : f o o d $ ( 5 l 2 ) = " r i c ep i l a f " : f o o d $ ( 5 , 3

" s a l a d "

6 0 0 9 0l a s t = 5 : r e m t h e r e a r e5 i t e m s

6 0 1 0 0 r e t u r n

F i g .4 - 9 .L o a d i n ga t w o - d i m e n s i o n a la r r a yi n B A S I C .

t h a no n e c o l u m n ?T h es o l u t i o ni s t o a d da n o t h e r

n u m b e rw i t h i nt h e p a r e n t h e s e s :

F O O D $ ( 2 , 1 )= " H A M B U R G E R ":

F O O D $ ( 2 , 2 )= " F R I E S " :

F O O D $ ( 2 , 3 )= " S A L A D "

H e r e ,t h ef i r s tn u m b e r( i np a r e n t h e s i s )r e f e r s t ot h e

r o w( a l li t e m sa r ei n r o wt w o ) .T h es e c o n dn u m b e r

r e f e r s t ot h ec o l u m n .A s y o uc a ns e e ,t h em a i n

c o u r s ei s l i s t e du n d e rr o wo n e ,a n d

t h et w os i d e

d i s h e s a r el i s t e da s b e i n gu n d e rr o w st w oa n dt h r e e .

A r r a y sp r o v i d ea v e r yf a s ta n de f f i c i e n tw a yo fh a n

d l i n gr o w sa n d c o l u m n so f i n f o r m a t i o no n a

c o m p u t e r .

N o wt h a tw e ' r eu s i n ga t w o - d i m e n s i o n a la r r a y ,

o u rl o a d - u p - l i s tr o u t i n ew i l lc h a n g e s o m e w h a t ,a s

s h o w ni n F i g .4 - 9 .

W e ' l lh a v et o c h a n g ea f e wo t h e rl i n e st h a t

p r e v i o u s l yr e f e r r e dt o a s i n g l ea r r a ya n dn o wm u s t

c o n t e n dw i t ha d o u b l e ( t w od i m e n s i o n a l ) o n e .T h e

f i r s tl i n ef o r e d i t i n gi s 1 0 , w h i c hm u s tn o wb ec h a n g e dt o a d o u b l e - d i m e n s i o n :

1 0 D I MF O O D $ ( 5 , 3 )

W h e ny o u ' r ew o r k i n gw i t ha r r a y s ,i t ' si m p o r

t a n tt o r e m e m b e rt h a ta v a r i a b l ec a n n o tb e t u r n e d

i n t os i n g l ea n dd o u b l e d i m e n s i o n sw i t h i n t h es a m e

p r o g r a m .F o re x a m p l e ,t h es t a t e m e n ti n l i n e2 0b e

l o w w o u l dg i v ea n e r r o r ,b e c a u s eA $h a da l r e a d

b e e nd e f i n e d a sa n a r r a y :

1 0 D I MA $ ( 1 0 ): R E MG O O DS T A T E

M E N T2 0 D I MA $ ( 1 0 , 1 0 ): R E MB A DS T A T E

M E N T( S e c o n dd i m ! )

T h e r e ' sn o t h i n gw r o n gw i t ht h el i n e2 0 s t a t e

m e n ti t s e l f .I t ' sj u s tt h a tt h ea r r a yi s a l r e a d y" s p o

k e nf o r , "s i n c ea s i n g l e - d i m e n s i o na r r a yh a sa l r e a d y

b e e nd e f i n e da t l i n e1 0 .

I f w ec h a n g e dt h es e c o n dv a r i a b l et o B $ , e v e r

t h i n gw o u l d b eO Ka g a i n ,b e c a u s eB $h a sn o t y e t

b e e nd i m e n s i o n e d .Y o uc o u l da l s oh a v ea s e p a r a t

n o n d i m e n s i o n e dB $( a B $w i t hn op a r e n t h e s e s )v a r i

a b l e ,w h i c h w o u l dc a u s en o c o n f l i c t s .A sy o uw o r k

w i t hB A S I C ,y o u ' l lf i n dt h eo n l yc o n f l i c t st h a ta r i s e

a r eb e t w e e nd i f f e r e n tt y p e so f s t r i n ga r r a y su s i n g

t h es a m ev a r i a b l e ,a s i n t h eA $ e x a m p l ea b o v e .

T h en e x tl i n et o c h a n g ei s 1 0 4 0 ,w h e r ew ea c

t u a l l yt e s tf o rt h ei t e m b e i n gs e a r c h e d . I nt h i sc a s e ,

t h ev a r i a b l e

F O O D $ ( A T )

h a sb e e nc h a n g e dt o

F O O D $ ( A T , 1 )

4 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 53/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 54/194

W i t ha l ls t a t e m e n t st u c k e dn e a t l yo u to f s i g h t a t

l i n e7 0 0 ,t h e r e ' sn o q u e s t i o nt h a tt h i sa p p r o a c h

w o r k s .Y e ti t a d d sy e ta n o t h e rs u b r o u t i n et o t r a c k

t h r o u g h .P e r h a p sa b e t t e rs o l u t i o ni s o n et h a tC o m

m o d o r eh a sa d d e dt o i t sB A S I C :t h ea b i l i t yt o p e r

f o r ms t a t e m e n t sb a s e do n a t e s t ,e v e ni f t h et e s t

i s s e v e r a ll i n e sa b o v e .T h eI F . . . B E G I Ni n F i g .

4 - 1 0l e t su s a d dt h r e ep r o g r a ml i n e st h a tw i l lo n l y

b e e x e c u t e di f F O U N Di s t r u e .T h eB E N D( p r o

n o u n c e dB — E N D )a t l i n e5 1 8t e l l st h ep r o g r a mt h a t

t h i ss p e c i a li f l o g i ci s e n d e d . A f t e ra B E N D ,t h e

c o m p u t e rc o n t i n u e se x e c u t i n ge a c hs t a t e m e n ti n t h e

n o r m a l m a n n e r .

Y o uc a ns e eh o wt h i st y p eo f p r o g r a m ,s h o w n

i n i t se n t i r e t yi n F i g .4 - 1 1 ,i s j u s ta s h o r ts t e pa w a y

f r o mo n et h a ts e a r c h e st h r o u g ha d d r e s sl i s t so r a

d a i l ys c h e d u l e .

S E A R C H I N GF O RP A R T I A LI T E M S

T h ep r o g r a me x a m p l e sw e ' v eu s e ds o f a rh a v e

o n er e s t r i c t i o n :t h e yo n l yw o r kw i t he x a c tm a t c h e s .

S oi f y o ur e q u e s t e da H A M B U R Go rB U R G E R ,t h e

. w a i t e rw o u l d n ' tk n o ww h a t y o uw e r et a l k i n ga b o u t .

T h ep r o g r a mw o u l dc o m eu p e m p t ya n da s kf o ra n

o t h e rs e l e c t i o n .

T h es o l u t i o n ,o f c o u r s e ,i s t h eC o m m o d o r e ' s

I N S T Rf u n c t i o n ,w h i c hw a sb r i e f l yr e v i e w e di n

C h a p t e r1 . I n t h a te x a m p l e ,w e u s e dI N S T Rt o

v e r i f yt h a tt h ei n p u tv a r i a b l eY $c o n t a i n e dc e r t a i n

c h a r a c t e r s( Y o r y o r N o r n ) :

D OW H I L EI N S T R ( " Y y N n " , Y $ ) = 0

I n e f f e c tw ew e r ea s k i n gt h ec o m p u t e rt o s e a r c

t h ef i r s ts t r i n g( a l i t e r a le n c l o s e di n q u o t e s ) , f o a n

o c c u r r e n c eo f t h es e c o n ds t r i n g ( Y $ ) .I f t h e r e ' sa

m a t c h a n y w h e r ei n t h ef i r s ts t r i n g ,t h eI N S T Rf u n c

t i o nw i l lb e e q u a lt o t h ep o s i t i o no f t h ec h a r a c t e

a s s h o w ni n F i g . 4 - 1 2 .F o re x a m p l e ,i f Y $c o n t a i n

t h el e t t e r< N > ,t h i ss t a t e m e n tw o u l dr e t u r nt h e

v a l u et h r e e .

I N S T Ri s a l i g h t e n i n g - f a s ta n di m m e n s e l yv a l u

a b l ef u n c t i o nt h a ti s e a s i l yi n c l u d e di n s e a r c hr o u

t i n e s ,s u c ha s t h eo n ei n o u rC o m m o d o r I n n

p r o g r a m s .

T h ef o r m a to f I N S T Ri s a l w a y st h es a m e :

X - I N S T R( s t r i n g l , s t r i n g 2 , p o s i t i o

S t r i n g li s t h es t r i n gt o s e a r c ht h r o u g h .S t r i n g 2i s

t h es t r i n g t os e a r c hf o r .P o s i t i o nr e p r e s e n t s c h a r a c t e rp l a c ei n s t r i n g lw h e r et h es e a r c h s h o

b e g i n( I N S T Ra l w a y ss c a n sf r o ml e f tt o r i g h t ) .A s

w e ' l ls e ei n t h es e c t i o no n d a t as t o r a g e ,t h i sp o s i

t i o nf e a t u r e sh a sr e a la d v a n t a g e sw h e ny o u ' r es c a n

n i n gf o ra l e t t e ro r g r o u po f l e t t e r st h a tm a yo c c u r

m o r et h a no n c ei n a s t r i n g .F o rn o w ,b ec o n t e n tt h a t

t h ep o s i t i o n i n gf u n c t i o nw o r k si f y o u n e e d i t

w o n ' tb e u s i n gi t i n t h en e x tf e we x a m p l e s .

I f n o s t a r t i n gp o s i t i o ni s s p e c i f i e d ,I N S T Ra u

t o m a t i c a l l ys t a r t st h e s e a r c ha t t h e b e g i n n i n g W e ' l

b e u s i n gt h i sd e f a u l ts e t t i n gi n t h e f o l l o w i ne x

a m p l e s .

T r a n s l a t i n gs t r i n g la n d s t r i n g 2i n t ol i t e r a

s t r i n g s( c h a r a c t e r se n c l o s e di n q u o t e s ) ,s h o w sh o w

5 0 0

5 1 0

5 1 2

5 1 4

5 1 65 1 8

5 2 0

5 3 0

5 4 0

5 5 0

r e m : p r i n t

i f f o u n dt h e n

: p r i n t M t h a t

: p r i n t M w i t h

m e s s a g er o u t i n e :

b e g i n

• s v e r yg o o dt o d a y ! 1 1

t h e M f o o d $ ( a t , 1 ) "y o u

: p r i n t f o o d $ ( a t , 2 ) Ma n d " f o o d $ ( a tb e n d

i f n o t f o u n d1:

r e t u r n■•

t h e np r i n tf l s o r r y ,w e '

g e t "

, 3 ) " . "

r e a l lo u t t o d a y 1 *

F i g .4 - 1 0 .B a s i n gt h er e s p o n s eo n a f l a g .

4 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 55/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 56/194

T h eC o m m o d o r e1 2 8 ' sI N S T Rc o m m a n dm a yb e u s e da n y t i m ey o uw i s ht o l o o kf o rc h a r a c t e r s .

T h em o s tc o m m o na p p l i c a t i o n sa r es e a r c h i n ga n di n p u ts c r e e n i n gr o u t i n e s .T h e c o m m a n d ' s

f o r m a t ,I N S T R ( A $ , B $ ) ,t r a n s l a t e sa s " s e a r c ht h ec o n t e n t so f A $f o rt h eg r o u po f c h a r a c t e r s

c o n t a i n e di n B $ . "N a t u r a l l y , e i t h e r o ft h es t r i n g sm a yb e al i t e r a lg r o u po f c h a r a c t e r se n c l o s e d

i n q u o t e s , s u c h a s" H e l l o "o ^ ' V y N n " .I f n o m a t c hi s f o u n d ,I N S T R ( A $ , B $ ) e q u a l s0 . I f t h e r ei s

a m a t c h ,t h i sf u n c t i o nr e t u r n st h ep o s i t i o nw h e r et h em a t c hw a sf o u n d :

V a l u eR e t u r n e d

I N S T R ( " Y y N n " , Y $ )= 1

I N S T R ( l 4 Y y N n " , Y $ )= 2

I N S T R C Y y N n " , Y $ )= 3

I N S T R ( l l Y y N n " , Y $ )= 4

I N S T R ( " Y y N n M , Y $ )= O

Y $

" N "

" n "

n o m a t c h

( Y i s t h e 1 s t p o s i t i o n )

( y i s t h e2 n dp o s i t i o n )

( N i s t h e 3 r dp o s i t i o n )

( n i s t h e4 t hp o s i t i o n )

( A n y t h i n go t h e rt h a n w h a t ' s c o n t a i n e d

i n t h ef i r s ti t e m )

T h i sp a r t i c u l a rI N S T Rt e s tw o u l db e a f a s ta n de f f i c i e n tw a yo f c h e c k i n gt h eu s e r ' sa n s w e rt o

a q u e s t i o nt h a ts h o u l do n l yh a v ea y e so r n o a n s w e r .S e et h et e x tf o ra p r o g r a me x a m p l e .

F i g .4 - 1 2 .E x a m p l e so f I N S T R .

I N S T Rm i g h t b eo f u s ei n t h eC o m m o d o r eI n n

p r o g r a m :

X = I N S T R ( " H A M B U R G E R " ," B U R G E R " )

T h i se x a m p l ei s a l i t t l eu n r e a l i s t i c ,b e c a u s e

I N S T Ru s u a l l yw o r k sw i t hs t r i n gv a r i a b l e s( A $ ,B $ ,

F 0 0 D $ ( A T , l ) ,e t c . ) ,i n s t e a do f l i t e r a l s .A f t e ra l l ,

y o uc a nj u s t l o o k a tt h e s et w os t r i n g sa n dk n o w

t h e r e ' sa p a r t i a lm a t c h .B u ty o uc a ns e ei m m e d i

a t e l yt h ea p p l i c a t i o n s t ow h i c ht h i sc o m m a n dc a n

b e p u t .I n t h ea b o v ec a s e ,I N S T Rw o u l df i n dt h e

w o r dB U R G E Rw i t h i nH A M B U R G E R ,a n dm a k e

X e q u a lt o 4 . I n t h i sc a s e ,w eo n l yc a r et h a tX > 0( i t e mw a sf o u n d )o r X = 0( i t e mw a s n ' tf o u n d ) ,b u t

o t h e ra p p l i c a t i o n s o fI N S T Rm a k eg o o du s eo f t h e

v a l u er e t u r n e db y t h i sf u n c t i o n .

I f w ew e r et o s u b s t i t u t e v a r i a b l e s( F O O D $f o r

w h a t ' so n t h em e n ua n dR E Q U E S T $f o rt h ei t e m

r e q u e s t e d ) ,t h ea p p e a r a n c e f o r m u l aw o u l dc h a n g e ,

b u tt h er e s u l tw o u l db e t h es a m e .

X = I N S T R ( F O O D $ , R E Q U E S T $ )

T a k ea m o m e n tn o wt o l o o ka t t h eD O U B F O O Dp r o g r a mi n F i g .4 - 1 1 .A t w h a tl i n ew o u l

y o ui n s t a l la n I N S T Rs e a r c h ?A n dh o ww o u l d y

h a n d l et h ef a c tt h a tF O O D $i n t h i sp r o g r a mi s a

t w o - d i m e n s i o n a la r r a y ?

B o t ha n s w e r sc o m ei n o n en e a tp a c k a g e :Y o u

w o u l dr e p l a c et h ec u r r e n tm a t c h - u pt e s t( l i n e1 0 4 0

w i t ht h i sn e wl i n e :

1 0 4 0 I FI N S T R $ ( F O O D $ ( A TR EQ U E S T S) > 0 T H E NF O U N D=

- 1 : E X I T

P o pt h i s l i n ei n t ot h eD O U B L E F O p r o

g r a m ,a n dy o u ' l lb e a b l e t ot y p eH A M B U Ro rB U R G E Rf o ra h a m b u r g e r a n dD O Gf o ra c h i l id o g .

I f y o u ' r ea f r a i d t h a tt h ec u s t o m e rm i g h te n t e r

" I W O U L DL I K EA H A M B U R G E RP L E A S E

y o uc o u l dr e v e r s e t h e t e s t — t h i st i m el o o k i n gf o r

F O O D $ ( A T , 1 )w i t h i nR E Q U E S T $ .B ya d d i n ga n

o t h e rl i n et o t h ep r o g r a m y o uc o u l de v e n c h e c kf o r

b o t hm a t c h e s :

4 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 57/194

1 0 4 0I F I N S T R $ ( F O O D $ ( A T , 1 )

, R E Q U E S T $) > 0 T H E NF O U N D= - l : E X I T

1 0 4 2I F I N S T R $ ( R E Q U E S T $, F O O D $

( A T , 1 ) )> O T H E NF O U N D=

- 1 : E X I T

w h i c ht r a n s l a t e sa s :T h ei t e mi s f o u n di f t h ec u s

t o m e re n t e r s p a r to f t h en a m e ,o r i f t h ec u s t o m e r

r e q u e s ti s f o u n da s a p a r t i a lm a t c h ,f o l l o w i n ga s c a n

o f t h ef o o di t e ml i s t .

T O W A R DF A S T E RS E A R C H E S

T h et y p eo f s e a r c h e sy o u ' v e s e e ns o f a ra r e

f a i r l yq u i c k — a sl o n g a s y o u ' r e o n l yd e a l i n gw i t h

l i s t so f a f e wd o z e ni t e m s .T h er e a lw o r l di s a l i t t l e

d i f f e r e n t .W h a ta b o u tl i s t so f a f e wh u n d r e di t e m s ?

T h e s ec o u l dc l e a r l yt a k ea l o tl o n g e r .B e c a u s et h es e a r c hr o u t i n e sw e ' v e b e e nd e a l i n gw i t hs o f a r

c h e c ke v e r yi t e m ,y o uc a nr e a d i l ys e eh o wa s e a r c h

o f a b i gl i s tc a nb e q u i t et i m ec o n s u m i n g .

O fc o u r s e ,l o n g b e f o r ec o m p u t e r sc a m ea l o n g

f o l k sw e r eg r a p p l i n gw i t ht h ep r o b l e mc r e a t e db y

l o n gl i s t s .T h e yf o u n dt h e a n s w e rl o n gb e f o r

C h a r l e sB a b b a g ei n v e n t e d ,t h ef i r s ta n a l y t i c a le n

g i n ea n dt h ec o m p u t e ra g ew a sb o r n .

T h i n kf o ra m o m e n ta b o u tt h et i m e - t e s t e dw a y s

o f s t o r i n ga n dl o o k i n gu p i n f o r m a t i o no np a p e r :d i c

t i o n a r i e s ,e n c y l o p e d i a s ,a d d r e s sd i r e c t o r i e s ,a n d

l a t e r ,t e l e p h o n e b o o k s .T of i n di n f o r m a t i o ni n a n y

o n eo f t h e s e ,y o uc a ns i m p l yo p e nt o t h em i d d l ea n d

s t a r tl o o k i n g .I f y o u rl o o k i n gf o rP e t e ' sP l u m b i

S u p p l ya n dt h ef i r s te n t r yy o ur u na c r o s si s C a r

s o n ' sC a n a r yE m p o r i u m , y o uc a ni n s t a n t l ye l i m i

n a t ea l lt h ep a g e so nt h el e f ts i d e o ft h eo p e nb o o k ;

t h e yw o n ' tc o n t a i nw h a ty o u ' r e l o o k i n gf o r .N e x t ,

y o ut a k et h e i n c ho r s o o fp a g e st h a t ' sl e f t ,a n ds p l i t

i t ,f i g u r i n gt h a to n eo ft h eh a l v e sw i l lc o n t a i nP e t e ' s

T h i st i m ey o uo p e nt o S t e l l a ' sS a n d w i c hS h o p p e

a n d y o ui n s t a n t l yk n o wt h a tP e t e ' si s t h ef i r s th a l fo f t h e p a g e s .T h i sm i s s i o no f s e a r c ha n de l i m i n a

w o u l dc o n t i n u ea f e wm o r et i m e su n t i ly o uh i t

P e t e ' sP l u m b i n gS u p p l yr i g h to nt h eb u t t o n .T h e r e

a r ea l w a y sa f e wm i s s e st h i sw a y ,b u ti t ' sa l o tm o r e

R E C #

B I L L $

C A R S O N \

G O M E Z I

H E L E N 2 r

J A M E S

6

7

8

9

J A N

R O B E R T

Y O L A N D A

Z I G G Y

A d i a g r a mo f a b i n a r y s e a r c h .S e a r c ha r e a s( s h o w nb y s h a d i n g ) a r er e d u c e dw i t he a c h

s u c c e s s i v et e s t .A b i n a r ys e a r c ha s s u m e se l e m e n t si n t h ef i l eo r a r r a yh a v eb e e ns o r t e d .

F i g .4 - 1 3 .L o o k i n gf o rY o l a n d a :A b i n a r ys e a r c h .

4 7

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 58/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 59/194

p r a c t i c a lt h a ng o i n g t h r o u g ht h ee n t i r ep h o n ed i r e c

t o r y ,n a m eb y n a m e ,t o f i n d P e t e ' s .

T h ek e yt o t h i ss y s t e m ,o fc o u r s e ,i s t h a ta l lt h e

n a m e si n t h ep h o n eb o o ka r e s t o r e di n a l p h a b e t i

c a lo r d e r .P r o v i d e dy o us a v ey o u rc o m p u t e r i z e di n

f o r m a t i o ni n t h es a m ew a y ,t h eC o m m o d o r e1 2 8

w i l lh a v en o t r o u b l ep e r f o r m i n gt h es a m et r i c k .

P r o g r a m m e r sr e f e r t ot h i st y p eo f p r o c e d u r e

a s a b i n a r ys e a r c hb e c a u s ei t d i v i d e st h el i s ti n t o

p r o g r e s s i v e l y s m a l l e ra n ds m a l l e rp a r t s .F i g u r e

4 - 1 3 ," L o o k i n gf o rY o l a n d a , "i l l u s t r a t e sh o wa l i s t

w o u l db e d i v i d e du p f r o ms t a r tt o f i n i s h ,w i t ht h e

p r o g r a ms p l i t t i n gt h el i s tf i r s ta t J A M E S ,t h e na t

R O B E R T ,a n df i n a l l yl o c a t i n gY o l a n d aa s i t e m

n u m b e re i g h t .

F i g u r e4 - 1 4t r a n s l a t e st h i sd e s i g ni n t oa s e a r c h

r o u t i n e .I f y o u ' r ed e a l i n gw i t hb i gl i s t s ,t h i sr o u t i n e

c o u l de a s i l yr e p l a c et h ep r e v i o u se x a m p l e s w e ' v eu s e da t l i n e s1 0 0 0 - 2 0 0 0 .A n dh e r e ' s t h em a g i c :b i

n a r ys e a r c h e ss t i l lr e q u i r eo n l ya f e ws a m p l e so f

t h e d a t a( s a y ,1 0 t e s t s )w h e nd o i n g1 , 0 0 0i t e m s .

S e a r c h i n gf o rP a r t i a lM a t c h e s

B e c a u s ei t r e l i e ss o h e a v i l yo n a l p h a b e t i ca r

r a n g e m e n t ,b i n a r y s e a r c h e sc a n n o tb eu s e dt o f i n d

o n es t r i n g t h a ti s p a r to f a n o t h e r ; t h eI N S T Rf u n c

t i o nu s e di n o u rl a s te x a m p l es i m p l yw o n ' tw o r k .

I f y o u ' r ew i l l i n g t oa d da f e wl i n e st o y o u rr o u t i n e ,h o w e v e r ,y o uc a nd e s i g na p r o g r a mt h a tw i l ls e a r c h

f o ri t e m sb a s e do n t h ef i r s tf e wc h a r a c t e r s .T h i s

t y p eo f s e a r c h e n a b l e sy o ut o f i n dJ A M E Sb y t y p

i n gJ A ,R O B E R Tb yt y p i n gR , a n ds o o n .T h ef i r s t

s t e pi s t o f i g u r et h en u m b e ro f c h a r a c t e r s t h eu s e r

h a st y p e d ;t h i si s a v a i l a b l et h r o u g hB A S I C ' SL E Nf u n c t i o n :

5 0 6 0L R = L E N ( R E Q U E S T $ )

Y o uc a nt h e nl o c a t et h er e c o r db ym a t c h i n gt h ef i r s t

c h a r a c t e r so fe a c h i t e mi n t h ea r r a yw i t ht h ec h a r a c

t e r sb e i n gs e a r c h e df o r :

1 0 3 5T E M P $ =L E F T $ ( A $ ( A T ) , L R ): R E M

L O O KF O RS H O R T E RA $ ( A T )

1 0 4 0 I FT E M P $> R E Q U E S T ST H E NH I G H= A T

1 0 5 0 I FT E M P $ <R E Q U E S T ST H E L O W = A T

1 0 6 0 I FT E M P $= R E Q U E S T ST H E NF O U N D= - l : E X I T

L i n e1 0 3 5s i m p l yl o p so f fp a r to f t h ea r r a y

s t r i n gs o i t c a nb e p r o p e r l ym a t c h e dw i t ht h ei t e m

r e q u e s t e d .I f t h eu s e r t y p e sJ Aa s a s e a r c hr e q u e s t

T E M P $w i l lr e f l e c ta t w o - c h a r a c t e rv e r s i o no fe a c h

i t e mi n t h eA $a r r a y( B I ,C A , G O ,H E . . . ) . T h e

r e m a i n d e ro f t h er o u t i n ew o r k si n t h es a m ew a ya s

b e f o r e .

O f c o u r s e ,i t ' s a l w a y sp o s s i b l e t h a ts e v e r a

i t e m si n t h el i s tb e g i n w i t ht h es a m el e t t e r( J A M E

J A N ) ,s o i t se s s e n t i a l t h a ta r o u t i n eb e i n c l u d e dt o

" b a c k s c a n "f o ro t h e rp o s s i b l em a t c h e s :

0 ) 0 ) 0 )

a a a

F i g .4 - 1 5 .A d i a g r a mo f b a c k w a r ds e a r c h i n g .

4 9

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 60/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 61/194

C h a p t e r5

S t o r i n gY o u rD a t a

T h e r e ' sn o t h i n gm o r es a t i s f y i n gt h a nc r e a t i n ga n d

u s i n gy o u ro w nf i l e s .W h e ny o uc a ns u c c e s s f u l l y

s t o r ea n dr e c a l ly o u ro w nd a t ao n d i s k ,y o uh a v e

c l e a r l yl e f tt h er a n k so f t h e b e g i n n e r s .Y o u ' r es t a r t

i n gt o e x t r a c ts o m er e a lp e r f o r m a n c ef r o my o u rs y s t e m .

A l t h o u g hm o s tc o m p u t e ro w n e r sg e t t h ej i t t e r s

w h e ni t c o m e st o w r i t i n gt h e i ro w nf i l i n gp r o g r a m s ,

o r e v e ns i m p l ef i l er o u t i n e s ,f i l eh a n d l i n gi s n o t t h e

q u a g m i r eo fc o m p l e xa c t i o n s t h a tm a n ym a k ei t o u t

t o b e .I n f a c t ,o n c ey o uu n d e r s t a n dt h ec o n c e p t ,

y o u ' l lw o n d e rw h yy o u rp r o g r a m sh a v e n ' tm a d e

f u l l e ru s eo ff i l e si n t h ep a s t .T h o s ew h oc h o o s en o t

t o u s ef i l e sa r em i s s i n go u to n o n eo f t h eC o m m o

d o r e1 2 8 ' sm o r es a v o r yi n g r e d i e n t s .

H O WD I S KF I L E SW O R K

S e n d i n gi n f o r m a t i o ni n t oa d i s kf i l ei s m u c hl i k e

p r i n t i n gi t o na s c r e e n .T h eo n l yd i f f e r e n c ei s t h a t

p r i n t i n gi n f o r m a t i o ni n t of i l e sm u s tb e d o n ee s p e

c i a l l yc a r e f u l l y ;w h e nt h ep r o g r a mg o e st o r e t r i e v e

f i l ei n f o r m a t i o n ,i t m u s tk n o we x a c t l yh o wt h a ti n

f o r m a t i o nw a ss t o r e do r i g i n a l l y .

T a k ea l o o ka t F i g .5 - 1 t os e eh o wt h eb i l l - o f

f a r ei n f o r m a t i o nu s e de a r l i e rc o u l db e p r i n t e do n

t h es c r e e n .T h i sl i s t i n gw o u l dd i s p l a ya l lo f t h ei n

f o r m a t i o ni n t h eb i l l - o f - f a r e ,a s s u m i n gt h i sd a t a h a

p r e v i o u s l yb e e np l a c e di n m e m o r y .T h el i s t i n gi t

s e l fw o u l db e u n f o r m a t t e d — j u s ta s i m p l ep r i n t o u

o f i n f o r m a t i o nt h a tw o u l db e g i nl i k et h i s :

C H I L ID O GS O U P

C R E A M E DC O R NH A M B U R G E RF R E N C HF R I E S

S A L A D

T h i si s e x a c t l yt h ew a yi n w h i c hi n f o r m a t

i s s t o r e di n a f i l e .I t m a ya t f i r s ta p p e a rc o n f u s i n

b e c a u s et h e r e a r en o c o l u m nh e a d i n g so r o t h e rk e y s

t o t h ei n f o r m a t i o n .B u ti f t h ep r o g r a mi s w r i t t e

p r o p e r l y ,t h ec o m p u t e rw i l lr e c a l lt h i si n f o r m a t

i n e x a c t l y t h er i g h to r d e re v e r yt i m e .I f y o uw a n t e

5 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 62/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 63/194

S o f a r ,w e ' v eo p e n e du p a f i l e ,b u tv e r yl i t t l e

a c t i o nh a st a k e np l a c e .T h en a m eo f t h eg a m ew h e n

u s i n gd i s kf i l e s i ss t o r i n ga n dr e c a l l i n gi n f o r m a t i o n ,

a n dt h a t ' st h en e x ts t e pi n o u r p r o g r a m .

S T O R I N GD A T AI N A F I L E

A f e wp a g e sb a c kw es a i dt h a ts e n d i n gd a t ai n t o

a f i l e i sq u i t e s i m i l a r t o p r i n t i n gi t o n t h e s c r e e n .

A s w e p r o c e e d ,y o u ' l ln o t i c e t h a ta l lo f t h ec o m

m a n d su s e df o rd a t a h a n d l i n ga r es t r i k i n g l ys i m i

l a r t o t h o s ee m p l o y e df o r s c r e e nd i s p l a ya n d

k e y b o a r di n p u t .T h ec o m m a n dt o w r i t e ,o r p r i n t ,

i n f o r m a t i o ni n t of i l en u m b e ro n ei s :

P R I N T # 1

P R I N T # 1c a nb e u s e dw i t hv a r i a b l e s ,l i t e r a l s

( i t e m si n q u o t e s ) ,o r a c o m b i n a t i o no f t h et w o :

P R I N T # 1 , A $

P R I N T # 1 , " H E L L OT H E R E "

P R I N T # 1 , A $+ " H E L L OT H E R E "

H e r e ,B A S I Cf i l en u m b e r i n gs y s t e mb e c o m e se s

s e n t i a l ,b e c a u s ei t n o to n l yt e l l sD O St h a ty o u w a

t o o u t p u ti n f o r m a t i o nt o a f i l e :i t d e s i g n a t e sw h i c

f i l e .R e m e m b e r ,o n c ea f i l e i so p e n ,B A S I Ca l w a y

r e f e r s t ot h ef i l ea s a n u m b e r .I n a p r o g r a mt h a t

u s e ss e v e r a lf i l e ss i m u l t a n e o u s l y ,y o uc a np r i n td a t a

i n t o a l t e r n a t ef i l e sa s e a s i l ya s a c o n d u c t o rp o i n t

t o m e m b e r so f a n o r c h e s t r a :

6 0 3 0P R I N T # l , " H e r ea r et h i sy e a r ' sB e l m o

S t a k e s j o c k e y s "

6 0 3 5P R I N T # 2 , " H e r ea r et h eh o r s e sf o rt h e

B e l m o n tt h i sy e a r "

T h eh e a d i n g sa b o v ew o u l de a c hb e p l a c e di n

D O P E N# V @ T A XI N F O R M A T I O N " ^

D O P E N# 1 , " T A XI N F O R M A T I O N "

D O P E N# 5 , " G A M ES C O R E S "

o p e n s u p af i l en a m e dT A XI N F O R M A T I O N .T h i sf i l ew i l l

b e w r i t t e nt o ( i n f o r m a t i o nw i l lb e s a v e di n t oi t ) .T h i sf i l e

w i l lb e r e f e r r e dt o a s # 1 .T h ea t s y m b o l( @ )t e l l sD O S

t h a t e x i s t i n gi n f o r m a t i o ni n t h ef i l es h o u l db e r e p l a c e d ,

o p e n su p t h eT A XI N F O R M A T I O Nf o rr e a d i n g( l o a d i n g )

o f i n f o r m a t i o n( t h e r e ' sn o , W a t t h ee n d ) ,

o p e n s u p af i l en a m e dG A M ES C O R E Sf o rr e a d i n ga s

f i l en u m b e r

f i v e( n o t et h a tt h e r e

D ON O Th a v et ob e

f o u r

p r e v i o u sf i l en u m b e r ss e l e c t e d ;a f i l en u m b e rm a yb e a n y

y o uc h o o s e ) .

B e c a u s ei t p e r f o r m ss e v e r a lo p e r a t i o n sa t o n c e ,t h ew o r k i n g so f D O P E Nc a ns o m e t i m e s

b e d i f f i c u l tt o g r a s p .I t m a yh e l pt o t h i n k o fD O P E Na s d o i n ga l l t h et h i n g st h a tn e e db e i n g

d o n eb e f o r ey o u rp r o g r a mc a nr e a d( r e c a l l )o r w r i t e( s t o r e )i n f o r m a t i o nt o a f i l e :

1 . D O P E Na s s i g n sa f i l en u m b e r , w h i c h y o uc h o o s e .T h i sn u m b e ri s u s e dt o r e f e rt o f i l eo p e r

a t i o n sa s l o n ga s t h ef i l er e m a i n so p e n .Y o um a yc h o o s e a n yn u m b e rf r o m1 t o 2 5 5 .

2 . D O P E Nn a m e st h ef i l e .T h en a m ec a nb e a n y y o uc h o o s e ,u p t o s i x t e e nc h a r a c t e r sl o n g .

I t m a yn o tb e g i nw i t ha n u m b e r .I f y o u u s ea l i t e r a ln a m e( T A XI N F O ) ,t h en a m em u s tb ee n c l o s e di n q u o t e s .I f t h en a m ei s a v a r i a b l e ,s u c ha s A $ ,i t m u s t b ee n c l o s e di n p a r e n t h e s e s .

3 . T h eD O P E Nc o m m a n dt e l l st h ec o m p u t e rw h e t h e rt h ef i l ew i l lb e u s e df o rw r i t i n go r r e a d

i n g .A f i l em a yb e o p e nf o rr e a d i n go r w r i t i n g ,b u tn e v e rb o t ha t t h es a m et i m e .I f y o u w a n t

t o r e a df o r ma f i l et h a ti s o p e nf o r w r i t i n g ,o r v i c ev e r s a ,s i m p l y c l o s e t h ef i l ei n t h ec u r r e n t

m o d e , a n dr e o p e ni t i n t h en e wm o d e .

F i g . 5 - 2 .E x a m p l e so f t h eD O P E Nc o m m a n d .

5 3

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 64/194

6 0 0 0 0r e m : l o a d u pl i s t :

6 0 0 1 0 f o o d $ ( 1 , 1 ) = " c h i l id o g ": f o o d $ ( 1 , 2 ) = t f s o u p " : f o o d $ ( 1 , 3 )

c o r n 1 1

6 0 0 2 0f o o d $ ( 2 f 1 ) x f f h a m b u r g e r l f : f o o d $ ( 2 > 2 ) = t f f r e n c hf r i e s " : f o o d $ ( 2 , 3 )

" s a l a d "

6 0 0 4 0f o o d $ ( 3 , 1 ) = " p i z z a " : f o o d $ ( 3 , 2 ) = " m o o n p i e " : f o6 0 0 6 0f o o d $ ( 4 l 1 ) = " p a e l l a " : f o o d $ ( 4 , 2 ) = " p l a n t a i n s " : f

b e a n s "

6 0 0 8 0f o o d $ ( 5 , 1 ) = " f i l e t m i g n o n " : f o o d $ ( 5 , 2 ) = " r i c e p

" s a l a d "

6 0 0 9 0l a s t = 5 : r e m t h e r e a r e5 i t e m s

6 0 1 0 0 r e t u r n

F i g . 5 - 3 .D a t ai n a t w o - d i m e n s i o n a la r r a y .

t w od i s t i n c t l ys e p a r a t ef i l e s( p r e s u m a b l yo n ef i l ef o r

j o c k e y sa n dt h eo t h e rf o r h o r s e s ) .I f t h ep r o g r a m

w r i t e rd e s i r e d ,t h e r ec o u l db e a d d i t i o n a lP R I N T #

s t a t e m e n t st o s e n di n f o r m a t i o nt o s t i l lo t h e rf i l e s .

Y o u ' v ep r o b a b l ya l r e a d yg u e s s e dt h a tt h e

P R I N T #c o m m a n dm a ya l s ob e u s e dw i t ha r r a y s ,

s u c ha s t h eo n ey o u ' v e e n c o u n t e r e dm a n yt i m e sb e

f o r e :t h eb i l l - o f - f a r ea t t h eC o m m o d o r eI n n .F i g

u r e5 - 3s h o w st h a tb y s u b s t i t u t i n gP R I N T # 1f o r

P R I N Ti n t h e s c r e e nd i s p l a yl o o pa f e wp a g e s

e a r l i e r ,y o uc a ne a s i l yc o n v e r tt h i sr o u t i n ef r o md i s

p l a y i n g i n f o r m a t i o no nt h es c r e e nt o w r i t i n gi t i n t o

a f i l e .N o t et h a tt h eo n l yl i n et h a th a sc h a n g e di s 6 0 3 0 ,

w h i c hp r i n t st h ea r r a yi n t oa f i l ei n s t e a do f o n t o

t h es c r e e n .T w on e wl i n e sh a v e b e e na d d e d :6 0 0 5

( w h i c ho p e n st h ef i l ef o rw r i t i n ga s # 1 ) ,a n d6 0 6 0

( w h i c hC L O S E St h ef i l e ) .

T h eD O P E Nc o m m a n di n 6 0 0 5h a ss o m e t h i n g

n e w :a n a t s y m b o l( @ ) ,w h i c hi n s t r u c t sD O St o r e

p l a c et h i sf i l ei f i ta l r e a d ye x i s t s .I t ' st h es a m es y s

t e mu s e dw h e ny o uw i s ht o r e p l a c ea B A S I C

p r o g r a mt h a ta l r e a d ye x i s t so n t h ed i s k .T h e@s y m b o li s i m p o r t a n tt o i n c l u d ei n p r o g r a m st h a tw i l l

w r i t eo v e ra ne x i s t i n gf i l e .I f t h i ss y m b o li s n o t p r e s

e n tw h e ny o ut r y t or e p l a c ea f i l e ,t h e d r i v el i g h t

w i l lb e g i nf l a s h i n g ,a n e r r o rw i l lo c c u r ,a n dn o n e

o f t h en e wi n f o r m a t i o nw i l lb e p l a c e di n t ot h ef i l e .

T h eD C L O S Es t a t e m e n ti n 6 0 6 0i s j u s ta s i m

p o r t a n ta s a D O P E N ,b e c a u s ei t t e l l st h ec o m p u t e r

y o u ' r ed o n ew i t ht h i sf i l ef o rt h em o m e n t .

W h e ny o ua r ew r i t i n gi n f o r m a t i o n o n t a f i l e ,

a s i n t h i se x a m p l e ,D C L O S Ei s v i t a l ,b e c a u s ei t

f o r c e st h ec o m p u t e rt o c o u g hu p a n ys t r a yb i t so f

i n f o r m a t i o nt h a tw e r eP R I N T e dt o t h ed i s kf i l eb u t

t h a tm a yn o th a v er e a c h e dt h e d i s ki t s e l f .C l o s i n

a f i l ei s n e c e s s a r yb e c a u s et h eC o m m o d o r e1 2 8( a n d

m o s to t h e rc o m p u t e r s )s e n df i l ed a t at o a t e m p o r

h o l d i n gb u f f e rb e f o r et h ei n f o r m a t i o ni s a c t u a l

w r i t t e nt o t h ef i l e .W h e ne n o u g hd a t ai s s t o r e di n

t h eb u f f e r ,D O Sd u m p se v e r y t h i n gf r o mt h eb u f f e

o n t ot h ed i s k ,e x a c t l yi n t h eo r d e ri t w a sw r i t t e

C l o s i n gs i m p l yf o r c e s t h eC o m m o d o r e1 2 8t o s e n d

a n yr e m a i n i n gd a t a o u tt o d i s k .T h ec o m p u t e rc o n

s i d e r sw o r ko n t h i sf i l ec o m p l e t ef o rt h et i m eb e

i n g .O n c ea f i l eh a sb e e nc l o s e di n t h i sm a n n e r i t

c a n n o tb e r e f e r r e dt o a g a i nb y t h ep r o g r a mu n t i l

i t i s r e o p e n e d .

M a n yp r o g r a m m e r sl i k et o a d da g e n e r a lc l o s e

a l l - f i l e ss t a t e m e n ta t t h ee x i t t o t h e i rp r o g r a m s b y

i n c l u d i n gD C L O S Ew i t h o u ta n yn u m e r i cr e f e r e n c

D C L O S E

T h i se n s u r e sa n yf i l ea c c i d e n t a l l yl e f to p e nd u r

i n gp r o g r a mo p e r a t i o nw i l lb e c l o s e du p o ne x i t i n

G e n e r a l l y ,i t ' sa g o o dp r a c t i c et o c l o s ea f i l ea s s o o n

a s y o u rf i l er e a d i n go r w r i t i n gr o u t i n ei s d o n ew i t h

i t .T h e r e ' sn o t h i n gw o r s et h a nl o s i n gd a t ab e c a u

t h ep o w e rg o e so u ta n dd a t ai s s t i l ls i t t i n gi n a f i l e

b u f f e r .

5 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 65/194

7 0 0 0

7 0 0 5

7 0 1 0

7 0 2 0

7 0 3 0

7 0 4 0

7 0 5 0

7 0 6 0

7 0 7 0

r e m : r e a df r o mf i l e :

d o p e n# 1 , " f o o df i l e "

f o r r a w = 1t o

: f o r c o l = 1

: i n p u t # 1 ,

: n e x tc o l

n e x t r o w

d c l o s e# 1

r e t u r n

5

t o 3

f o o d $ ( r o w , c o l )

F i g . 5 - 4 .R e a d i n gd a t a f r o ma f i l e .

R E A D I N GI N F O R M A T I O NF R O M AF I L E

G e n e r a l l y ,a r o u t i n et h a tr e a d sa f i l ew i l la p p e a r

a l m o s ti d e n t i c a lt o t h e r o u t i n et h a tc r e a t e da n d

w r o t et h ef i l ei n t h ef i r s tp l a c e .F i g u r e5 - 4s h o w s

t h er o u t i n ew h i c hw o u l db e u s e dt o r e c a l lt h e d a t a

s t o r e di n o u rF O O DF I L E . O n l yt h es t a t e m e n t so n

l i n e s7 0 0 5a n d7 0 3 0 h a v ec h a n g e d .T h er e s to f t h e

p r o g r a mr e m a i n se s s e n t i a l l yt h es a m e .F i r s t ,s i n c e

t h ef i l e i sb e i n go p e n e df o rr e a d i n g ,t h eo p e nc o m

m a n dm u s tc h a n g e :

7 0 0 5D O P E N# 1 , " F O O D F I L E "

T h ec o m m a n dt o o p e na f i l ef o rr e a d i n gi s s i m p l e r

t h a nt h eo n e u s e df o r w r i t i n g .B e c a u s et h ef i l ew i l lb e r e a d( i n s t e a do f b e i n g

w r i t t e nt o ) t h e , W i s n o l o n g e ra p p r o p r i a t e ;

r e m e m b e r ,t h ed e f a u l ti n D O P E Ni s f o rr e a d i n g .

W e ' v ea l s oe l i m i n a t e d t h e@ r e p l a c es y m b o l ,s i n c e

i t i s n ' tn e e d e di n a r e a do p e r a t i o n .

T h eo t h e rc h a n g ei s t o t h el i n et h a tP R I N T s

d a t ai n t ot h ef i l ei n o u rp r e v i o u se x a m p l e .T h i sr o u t

i n gn o wr e a d si n f o r m a t i o n .A n I N P U Tc o m m a n d

r e p l a c e sP R I N T :

7 0 1 0I N P U T # 1 , F O O D $ ( R O W , C O L )

T h es t a t e m e n tI N P U T # 1f o l l o w s t h es a m ef o r

m a ta s P R I N T # 1 — t h en u m b e rs i g na n dt h ew o r d

I N P U Tm u s tn o tb e s e p a r a t e d .

O p e r a t i o n a l l y ,I N P U T # 1i s s i m i l a r t oB A S I C ' S

k e y b o a r d - o r i e n t e dI N P U Ts t a t e m e n t ,e x c e p tt h a t

I N P U T # 1r e t r i e v e si n f o r m a t i o nf r o ma f i l e .L i k e

i t sc o u s i n ,I N P U T # 1p r e s e n t ss o m ed r a w b a c ki n

m o r ed e m a n d i n g p r o g r a ms i t u a t i o n s .I n a f e wm o

m e n t sw e ' l lt a l km o r ea b o u tt h e s ed i f f i c u l t i e sa n d

w h a t y o uc a nd ot o s t e e r c l e a ro f t h e mi n y o u rd i s k

i n p u tr o u t i n e s .

S e t t i n gY o u rR u l e sa n dS t i c k i n g t oT h e mI n o u re x a m p l e ,t h e r e a r e1 5 i t e m s — f i vr o w s

o f t h r e ec o l u m n se a c h .T h ep r o g r a mm u s tb e s t r u c

t u r e di n a w a yt h a tr e a d sa l lo f t h e s ei t e m s —

r e a d st h e mi n t h ec o r r e c t o r d e r .F o re x a m p l e ,i f t h e

d a t aw e r es t o r e di n c o l u m n si n s t e a d o fb y r o

( y i e l d i n gr e c o r d so f t h r e er o w sb y f i v ec o l u m n s

t h ef i l ew o u l d b em a r k e d l yd i f f e r e n tf r o mt h ee x

a m p l ew e ' v e b e e n w o r k i n gw i t hs o f a r .( S e eF i g .

5 - 5f o ra n e x a m p l eo fh o wt h i sw o u l dw o r k . )A l lf i v e

m a i nc o u r s e sw o u l d b el i s t e dt o g e t h e r ,f o l l o w e db y

t e ns i d ed i s h e s .I f t h ef i l ei n f o r m a t i o nw e r er e a d

b a c ki n a n yo t h e r o r d e r( a r o w b yc o l u m no r d e r

f o re x a m p l e ) ,t h er e s u l tw o u l db e v e r yc o n f u s i n

T h u s ,i t ' si m p o r t a n tt o r e a da n dw r i t ey o u rd a t ai n

e x a c t l y t h es a m eo r d e re a c ht i m e .

A T a b l e

# I T E M

1 p e n c i l s

2 p e n s

3 c l i p s

L i s t e db y c o l u m nw i t h i nr o w

p e n c i l s

5 . 2 3

R A B B ' s

p e n s

7 . 1 5

D A N ' s

c l i p s

2 . 8 1

s t a t i o n s

o f D a t aI t e m s

C O S T

5 . 2 3

7 . 1 5

2 . 8 1

L I

V E N D O R

R A B B ' s

D A N ' S

s t a t i o n ' s

s t e db y r o ww i t h i nc o l u m n

p e n c i l s

p e n s

c l i p s

5 . 2 3

7 . 1 5

2 . 8 1

R A B B ' s

D A N ' S

s t a t i o n s

F i g .5 - 5 .S t o r i n gd a t ab yr o wa n d c o l u m no r c o l u m na n dr o w .

5 5

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 66/194

T E L L I N GY O U R

P R O G R A MW H A T ' SI N A F I L E

Y o u ' l lr e m e m b e rw em e n t i o n e dt h a to n eo f t h e

d i f f i c u l t i e si n w o r k i n gw i t hf i l e si s t h a ty o uc a n n o t

s e ew h a t ' sg o i n gi n t ot h e m .A n o t h e rd i f f i c u l t yi s

t h a ty o ud o n ' ta l w a y sk n o ww h a ti t e m sa r ec o n

t a i n e di n t h ef i l e .W h e ny o u rp r o g r a m

i s r e a d i n g

i n f o r m a t i o n ,t h i sb e c o m e so fc r i t i c a li m p o r t a n c e ,b e

c a u s et h ec o m p u t e rm u s tk n o wh o wm a n yi t e m so f

i n f o r m a t i o ni t i s r e a d i n g .

I f t h ef i l ec o n t a i n sf i v es e t so f i t e m sa n dt h e

c o m p u t e rr e a d ss i x ,t h el a s tg r o u pi n t h ea r r a yo f

b e s tw i l lb e b l a n k .A t w o r s t ,t h e r ew i l lb e a ne r r o r

t h a tw i l lb r i n gy o u rp r o g r a mt o a n a b r u p th a l t .

I f t h ef i l ec o n t a i n sf i v es e t so f d a t aa n dt h ec o m

p u t e ri s o n l yi n s t r u c t e dt o r e a df o u r ,y o uw i l lh a v e

l o s to n eo f y o u rr e c o r d s .I t w i l ls t i l lb e o nt h ed i s k ,

b u tt h ed a t af r o mt h ef i n a lr e c o r dw o n ' th a v eb e e nr e a di n t om e m o r y .I n p r o g r a m st h a tu s ef i l e s ,i t i s

v e r yi m p o r t a n tt o k n o wh o w m a n yr e c o r d st h e r e

a r e .T h e r ea r e s e v e r a lw a y st o d o i t :

S o l u t i o n1 : B u i l dt h en u m b e ro f r e c o r d si n t ot h e

p r o g r a m .

S o l u t i o n2 : S t o r et h en u m b e ro f r e c o r d si n t h e

f i l e i t s e l f .

S o l u t i o n3 : A d da n e n d - o f - f i l em a r k e r .

E a c ho f t h e s es o l u t i o n sw o r k s , b u ts o m ea r e b e t t e r

i n c e r t a i nc i r c u m s t a n c e s .

R e a d i n ga f i l ei s l i k ed r i v i n gd o w nl o n e l yr o a d

w i t h o u ta m a p . Y o u ' v eg o tt o c a r e f u l l yp a ya t t e n

t i o n t ot h el a n d m a r k so r y o u ' l ln e v e rg e tt o y o u r

d e s t i n a t i o n .

" T e l lm e , "s a y st h e s t r a n g e r a sh e p e e r si n t o

t h ee y e so f t h eg a ss t a t i o na t t e n d a n t ." H o wd o y o u

g e tt o t h eC o m m o d o r eI n n ? "

" W e l l ,i t ' s l i k e t h i s .Y o ug o d o w ns e v e n

m i l e s — i t ' l lb e o ny o u rl e f t . "" N o o o o ! "T h em a nw h oh a sb e e nq u i e t l yr o c k

i n gi n f r o n t o ft h ei c em a c h i n es i t su p a n dt a k e sn o

t i c e ." N o ,t h a t ' sn o ti t !D o n ' tt e l lh i mt a g ot h a tw a y .

L o o k ,j e s tt a k eh e rd o w nt i ly a s e et h e e n d - o f - r o a d

m a r k e r .T h a t ' l lb e t h eC o m m o d o r eI n nt h e r eo ny e r

l e f t . "

A c t u a l l y ,b o t ht h eo l dt i m e r sa r er i g h t .T h e y ' r e

s i m p l yg i v i n gd i f f e r e n td i r e c t i o n s f o rt h e s a m e

t h i n g .R e a d i n gt o t h ee n do f f i l e sw o r k si n t h es a m e

w a y .

S o l u t i o n1 : P l a c et h e N u m b e ro f

R e c o r d sR i g h ti n t h eP r o g r a m .O n es o l u t i o

i s t o " h a r d w i r e "i n t oy o u rp r o g r a mt h en u m b e

o f i t e m st o b e s t o r e d .W e ' v ed o n et h i s i no u rp r e v i

o u se x a m p l e sb ys p e c i f y i n g5 a st h ee n do f t h eF O R

. . . N E X Tl o o p .T h i s a p p r o a c hw o r k sw e l le n o u g

a s l o n g a st h en u m b e ro f r e c o r d sd o e s n ' tc h a n g

B u tu s u a l l yy o u ' l lh a v e af l u i dn u m b e ro f i t e m si n

y o u rf i l e s .A n dw i t ht h e mm e t h o d ,u n l e s sy o u

m o d i f yt h ep r o g r a me a c h t i m ea n i t e mi s a d d e do r

d e l e t e d , t h e r e ' sn o w a yf o rt h ep r o g r a mt o k n o w

f r o mo n er e a d i n gt o t h en e x t ,e x a c t l yh o w m a

r e c o r d se x i s t .

S o l u t i o n2 : S t o r e t h eN u m b e ro f

R e c o r d si n t h eF i l eI t s e l f .O n eo f t h em o s tc o mm o na p p r o a c h e si s t o w r i t e t h en u m b e ro f r e c o r d

r i g h ta t t h eb e g i n n i n go f t h ef i l e .T h ep r o g r a mc a n

r e a dt h i sn u m b e rf i r s t ,a n di n s t a n t l yk n o w h

m a n yr e c o r d st o c l i c ko f f i nt h eF O R. . . N E X

l o o p :

7 0 0 7I N P U T # 1 , L A S T: R E MG E T# O F

L A S TR E C O R D

7 0 1 0F O RR O W = 1T O L A S T: R E S L I G H T L Y M O D I F I E F O R. . .

N E X T

A sl o n ga s t h ep r o g r a mh a sw r i t t e n t h en u m b e ro f

r e c o r d si n t ot h ef i l e ,t h i sn u m b e rc a nb e u s e da s

a p o i n t e rt o t h el a s ti t e mi n t h ef i l e .Y o uw i l ln o t e

t h a tt h en u m b e ro f r e c o r d s( L A S T )m u s tb e p l a c e

a t t h ev e r y b e g i n n i n go f b o t hf i l er o u t i n e s .O t h e r

w i s e ,y o u rp r o g r a mw i l lb e c o m ec o n f u s e da n de v

e r yi t e mw i l lb e r e a di n t ot h ew r o n gs l o t i nt h e

a r r a y .R e m e m b e rt h a tt h ef i l er e a da n dw r i t er o u

t i n e sm u s th a n d l ed a t ai n e x a c t l y t h es a m eo r d e r F i g u r e5 - 6i s a c o m p l e t ep r o g r a ml i s t i n gt h a t

i n c o r p o r a t e sa l lt h ef i l eh a n d l i n gt r i c k sw e ' v ec o

v e r e ds o f a r .

L i n e s 1 0 0 - 4 0 0m a k eu p t h eM a i nC o n t r o lR o u

t i n et h a tc a l l st h ea v a i l a b l es u b r o u t i n e s .F i r s t ,t h e

i n i t i a ld a t ai s p l a c e di n t ot h ea r r a y u s i n gt h e o l dr o u

t i n ea t l i n e6 0 0 0 0 . N e x t ,t h i si n f o r m a t i o ni s w r i t

5 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 67/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 68/194

1 . O p e nt h ef i l e .

2 . W r i t e t h ed e s i r e di n f o r m a t i o ni n t ot h ef i l e .B e s u r et o i n c l u d et h en u m b e ro f r e c o r d s .

3 . B e s u r et o D C L O S Et h ef i l et o u p d a t et h ed i s kd i r e c t o r y .

F i g . 5 - 7 .S t e p sf o rw r i t i n ga f i l e .

w i t hf a i l u r et o c l o s et h ef i l e sy o uh a v e o p e n e d( e s

p e c i a l l yw h e ny o u ' r ew r i t i n gi n f o r m a t i o n ) .F i g u r e

5 - 7s h o w st h e s t e p sf o rw r i t i n ga f i l e ,a n dF i g .5 - 8

s h o w st h es t e p sf o rr e a d i n ga f i l e .

S o l u t i o n3 : U s i n ga n E n do f F i l eM a r k .

A n o t h e rm e t h o do f r e a d i n gf i l e si n v o l v e ss i m p l y

p l a c i n ga m a r k e ra t t h ee n do f t h ef i l e ,t o s i g n a lt h e

f i l e ' se n d .A n a p p r o p r i a t e e n d - o f - t h e - r o a dm a r k

m i g h tb e t h r e ed o l l a rs i g n s ,o rs o m es e l d o m - u s e d

g r a p h i c sc h a r a c t e r .W h e nt h ec o m p u t e re n c o u n t e r s

t h i sm a r k e r ,t h ep r o g r a mc a nb e s e t t os t o pr e a d

i n gt h ef i l e .N a t u r a l l y ,t h i sc a l l sf o rs o m et y p eo f

i n t e r n a lc o u n t e rt o k e e pt r a c ko f h o w m a n yr e c o r d s

h a v e b e e nr e a d .A t y p i c a ls u b r o u t i n ew o u l dl o o k

l i k et h i s :

7 0 0 0R E M : R E A DF I L ER O U T I N E

7 0 0 5R O W = 1 : R E MI N I T I A L I Z ER O W7 0 1 0D OW H I L EA $ ( R O W , C O L )< > " $ $ $ "

7 0 2 0 : F O R C O L = 1 T O 3

7 0 3 0 : I N P U T # 1 , A $ ( R O W , C O L )7 0 3 5 : I F A $ ( R O W , C O L ) = " $ $ $ "T H E N

E X I T

7 0 4 0 : N E X TC O L

7 0 1 5 : R O W= R O W + 1 : R E M: B U M P E D

U PO N L YI F N O TE N D

7 0 5 0L O O P7 0 6 0R E T U R N: R E M: E N DO F R O U T I N E

N o t et h a tb e c a u s et h e$ $ $c o d em i g h tb e e n c o u n

t e r e dw i t h i nt h eC O LF O R . . .N E X Tl o o p( t h o u g h

i t s h o u l d n ' tb e i n a p r o p e r l yw r i t t e n - o u tf i l e ) ,i t i s

a g o o di d e a t o i n c l u d ea n I Fs t a t e m e n tt e l l i n gt h e

p r o g r a mt o E X I Tt h eD O W H I L Eo p e r a t i o n .T h e

E X I Tc o m m a n dt e l l st h ec o m p u t e rt o g od i r e c t l

t o t h en e x ts t a t e m e n tf o l l o w i n g t h e l o o p( i nt h i s

c a s e ,a R E T U R Nf r o mt h es u b r o u t i n e ) .

E i t h e rm e t h o dw o r k sp e r f e c t l yw e l l .T h ef i r s t

( p r i n t i n gt h en u m b e ro f r e c o r d si n t h ef i l e )l e t sy o u r

p r o g r a mk n o wi n s t a n t l yh o w m a n ys e t so f i t e m

t h e r ea r e .T h es e c o n d( u s e o fa ne n d - o f - f i l em a r k e

l e t sy o uc r e a t e s e q u e n t i a lf i l e st h a td o n ' th a v e a n

e x t r a n e o u sc h a r a c t e r su n t i lt h e e n d .

A D D I N GI N F O R M A T I O NT OT H E E N DO FF I L E S :A P P E N D

T h e r ea r et i m e sw h e ni t ' su s e f u lt o t a c ka d d i

t i o n a li t e m s o n t ot h ee n do fa ne x i s t i n gf i l e .F o r e x

a m p l e ,a c o n s u l t a n tw h ow o r k sb y t h eh o u rm i g h

w a n tt o b u i l da f i l et h a td o c u m e n t e dt h e l e n g t ha n d

c o n t e n to f b u s i n e s s - r e l a t e dp h o n ec o n v e r s a t i

D a t aa b o u te a c hn e wc o n v e r s a t i o nc o u l db e a pp e n d e dt o t h ee n do f t h ef i l e .T h e n ,a t t h ee n do f

t h em o n t h ,t h e c o n s u l t a n tc o u l dl o a d t h ef i l ei n t o

a w o r dp r o c e s s o ra n dp r o c e e dt o t y p ec o m m e n

o n e a c hi t e m .

N a t u r a l l y ,y o uc o u l dr e a dt h ee n t i r ef i l ei n t oa n

a r r a y ,a d do n em o r ei t e m ,a n dt h e nr e w r i t e t h ee n

t i r ef i l e .B u tt h e r e ' sa n e a s i e rw a y :t h eA P P E N

c o m m a n d .F i g u r e5 - 9s h o w sh o wA P P E N Dw o r k s

W h a ti f y o uc o u l dg l u e i n f o r m a t i o nt o t h ee n d

o f t h ef i l e ?T h a t ' sw h a tA P P E N Di s a l la b o u t .T h e

1 . O p e nt h ef i l e .

2 . R e a di n t h e i n f o r m a t i o nu s i n gI N P U To r o t h e rc o m m a n d s :

• D a t am u s tb e r e a di n e x a c t l yt h es a m eo r d e ri t w a sw r i t t e n

• I n p u tt h en u m b e ro f i t e m sf i r s t ,i f i t w a ss o w r i t t e n

3 . D C L O S Et h ef i l et o " f r e eu p "t h ef i l en u m b e r .

F i g . 5 - 8 .S t e p sf o rr e a d i n ga f i l e .

5 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 69/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 70/194

m a i no nt h ed i s ku n t o u c h e d ,b u tt h en e ws i z eo fI N

J U R I E Sw o u l d b ee q u a lt o t h ec o m b i n e ds i z eo f t h e

o r i g i n a lI N S U L T Sa n dI N J U R I E Sf i l e s .

C O N C A Ti s o n eo f t h o s ec o m m a n d st h a ti m

m e d i a t e l ys e n d sc o n t r o lo f t h ec o m p u t e rb a c kt o

y o u .W h i l et h ed i s ki s w h i r r i n ga l o n g ,y o uc a np e r

f o r mo t h e r n o n d i s k - r e l a t e dp r o c e d u r e ss u c ha s l i s t

i n gy o u rp r o g r a mo r t y p i n gi n f o r m a t i o n .

S O M EB A DN E W SA B O U TI N P U TA N DA S O L U T I O N

A sw e ' v es e e nt h e ms o f a r ,r e a da n dw r i t er o u

t i n e sw o r kp r e t t yw e l l .B u tw eh a v e n ' ta d d e da n y

o f t h ed a i l yc o m p l i c a t i o n sy o u ' r e s u r et o e n c o u n

t e r i n t h er e a lw o r l d .

O n c ey o uh a v e aw o r k i n gw r i t e - t o - f i l er o u t i n e

t h a ti s p l a c i n gi n f o r m a t i o ni n t ot h ef i l e ,a n do n c e

y o u rr e a d - t h e - f i l er o u t i n ei s i n p u t t i n g t h a ti n f o r m a

t i o n ,t h em o s tc o m m o np r o b l e my o u ' l le n c o u n t e r

i s o n eo f m i s s i n gi n f o r m a t i o n :

M I S S I O N :I M P O S S I B L E

F r i e n d s ,R o m a n s , c o u n t r y m e n

w i l lb e c o m e :

M I S S I O N

F r i e n d s

T h er e a s o nf o rt h i si s t h a tb o t ht h eI N P U Ta n d

I N P U T #c o m m a n d st y p i c a l l yi g n o r ea n yi n f o r m a

t i o nt h a t f o l l o w sc o m m a so r c o l o n so n a l i n e .T h e

m o r es o p h i s t i c a t e dy o u rd a t ah a n d l i n ga n ds t o r a g e

r e q u i r e m e n t s , t h em o r el i k e l yy o ua r et o n e e dt h e s e

c h a r a c t e r s .C o u l dy o ui m a g i n e aw o r dp r o c e s s o r

t h a td i d n ' ta l l o wc o m m a so r c o l o n s ?W h a ta b o u ta n

a d d r e s sl i s tp r o g r a mt h a t d i d n ' ta l l o wy o ut o p l a c e

a c o m m ab e t w e e nc i t ya n ds t a t e ?

S o m ec o m p u t e r si n c l u d ea L I N EI N P U Tc o mm a n di n t h e i rB A S I C s .I n t h e s ev e r s i o n so fB A S I C ,

L I N EI N P U Ta l l o w s e n t r yw i t hc o m m a s ,c o l o n s ,

q u o t e s ,a n do t h e rs y m b o l st h a tc a u s et r o u b l eu s i n g

t h er e g u l a rI N P U Tc o m m a n d .T h eC - 1 2 8h a s n o t h

i n gl i k ei t .

T h eg o o dn e w si s t h a tt h e r e a r ea t l e a s tt w o

B A S I Cp r o g r a m m i n gs o l u t i o n s t ot h i sd i l e m m a .

T h e ya l l r e q u i r ea l i t t l ee x t r aw o r k .

I n p u t t i n gw i t hQ u o t e s

T ou n d e r s t a n dt h ef i r s tw a ya r o u n do u rc o m m

a n dc o l o np r o b l e m ,i t h e l p st o k n o wa s e c r e ta b o u

t h eI N P U Tc o m m a n d :I N P U Ta c c e p t sa n e n t i r

l i n eo f i n f o r m a t i o n( c o m m a sa n dc o l o n s i n c l i f t h el i n eb e g i n sw i t ha q u o t a t i o nm a r k .S ow h i l

i n p u t t i n g

M I S S I O N :I M P O S S I B L E

p r o d u c e so n l yM I S S I O N ,t y p i n g" M I S S I O N I M

P O S S I B L Ew o r k sj u s tf i n e .A q u o t a t i o nm a r ki n

f r o n t o fa n yl i n ew o r k sa s a f l a g ,t e l l i n gI N P U Tt o

a c c e p tt h ei t e ml i t e r a l l y ,a n dt o i n c l u d e t h ec o m m

a n dq u o t a t i o nm a r k sa s p a r to f t h es t r i n g .

Y o u ' l l n o t i c er i g h ta w a yt h a tt h i sr o u t i n eh a s

a s p e c i a lr e q u i r e m e n t .A l li n f o r m a t i o ni n t h ef i l e

m u s tb e p r e c e d e db y q u o t a t i o nm a r k s .

B u th o wd o y o us e tq u o t a t i o nm a r k si n f r o n

o f e a c hf i l ei t e m ?F i r s t ,y o ua n d y o ua l o n ec o n t r

h o wt h ed a t ai s w r i t t e ni n t oa f i l et o b e g i nw i t h .S o

y o uc a nd e s i g nt h ep r o g r a mt o p l a c ea q u o t a t i

m a r ki n f r o n to fe a c hl i n ea s t h a tl i n ei s w r i t t e ni n t o

t h ef i l e .

A l o n gc o m e sa n o t h e rh i t c h :y o uc a n ' t d i r e

t e l lt h ec o m p u t e rt o

p r i n ta q u o t a t i o nm a r k ,e i t h e

o n t ot h es c r e e no r i n t oa f i l e :

6 0 3 0P R I N T# 1 , " A $: R E MT H I SW O N ' W O R K

T h ea b o v el i n ew o n ' tp r i n ta q u o t a t i o nm a r k , n

w i l li t p l a c et h ev a l u eo fA $i n t h ef i l e .I n s t e a d ,t h e

c o m p u t e rw i l lt a k et h el i n el i t e r a l l ya n dp r i n tt h e

l e t t e rA a n da d o l l a rs i g n( r e m e m b e rt h a ta n y t h i

f o l l o w i n ga q u o t a t i o nm a r ki n B A S I Ci s p r i n t e

c h a r a c t e rf o r c h a r a c t e r ) .

F o r t u n a t e l y ,t h e r e ' sa n o t h e rw a yt o p r i n tq u o

t a t i o nm a r k sa n do t h e rs y m b o l s :A S C I Ic o d e s( s e e

F i g .5 - 1 0 ) .E a c hc h a r a c t e rt h a tc a nb e d i s p l a y e o r

t y p e di n c a nb e r e p r e s e n t e db y s p e c i a lc o d e su s i n

t h eC H R $ ( x )f u n c t i o n ,w h e r ex i s t h eA S C I In u m

b e r .M o s tc o m p u t e ru s e r s a r ea w a r et h a te a c hk e y

b o a r dc h a r a c t e rh a s as p e c i a lc o m p u t e rc o d e .B u t

6 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 71/194

3 2

3 3

3 4

3 5

3 6

3 7

3 8

3 9

4 0

4 1

4 2

4 3

4 4

4 5

4 6

4 7

4 84 9

5 0

5 1

5 2

5 3

5 4

5 5

5 6

5 7

5 8

5 9

S P A C E

!

n

#

$

%

&

i

(

)

*

+

-

.

/

01

2

3

4

5

6

7

8

9

*

6 0

6 1

6 2

6 3

6 4

6 5

6 6

6 7

6 8

6 9

7 0

7 1

7 2

7 3

7 4

7 5

7 67 7

7 8

7 9

8 0

8 1

8 2

8 3

8 4

8 5

8 6

8 7

<

=

>

?

@A

B

C

D

E

F

G

H

|

J

K

LM

N

0

P

Q

R

S

T

U

V

w

8 8

8 9

9 0

9 1

9 2

9 3

X

Y

Z

[

£

J

6 0 3 0

F i g .5 - 1 0 .A p a r t i a ll i s t i n go f A S C I Ic h a r a c t e rc o d e s .

m a n yp r o g r a m m e r ss h ya w a yf r o me m p l o y i n gt h e s e

c o d e si n t h e i rp r o g r a m sb e c a u s et h en u m b e r sa r e

d i f f i c u l tt o r e m e m b e ra n dp r o g r a ml i n e su s i n gt h e m

a p p e a rc o m p l i c a t e d .A nA S C I Ic o d ei s j u s tw h a tt h e

d o c t o ro r d e r e dw h e ny o u ' r et r y i n gt o p l a c ea q u o

t a t i o nm a r k( " )a t t h eb e g i n n i n go fa l i n e .A q u o t a

t i o nm a r ki s A S C I Ic o d e3 4 ,a n dt h e c h a r a c t e r

( C H R $ ( x ) )f u n c t i o ni s u s e dt o p r i n ti t o nt h es c r e e n

o r t o a f i l e :

1 0 P R I N TC H R $ ( 3 4 )

I t i s a l s op o s s i b l et o d e f i n ea s t r i n gv a r i a b l ea s

a q u o t e ,a n dt h e nt o u s et h i sv a r i a b l ew h e n e v e ra

q u o t ei s r e q u i r e d :

Q U O T E $ = C H R $ " ( 3 4 ): R E MA S

S I G NQ U O T E V A L U E T

P R I N T # 1 , Q U O T E $ + A

T f f l SW O R K SB E A U T I F U L

E s s e n t i a l l y ,l i n e6 0 3 0a d d sa q u o t et o t h el e f to f t h e

s t r i n g .B A S I Cp r i n t sb o t ht o g e t h e ri n t h ef i l e .A f i l e

w r i t t e nb y t h i sr o u t i n em i g h tl o o kl i k et h i s :

" F a m o u sq u o t a t i o n s :

" F r i e n d s ,R o m a n s ,c o u n t r y m e n :l e n dm ey o u r

e a r s

" I c a m e ,I s a w ,I c o n q u e r e d

B e c a u s eo ft h e l e a d i n g q u o t a t i o nm a r k s ,B A S I C

I N P U T #f u n c t i o nw o u l dr e a c he a c ho f t h e s el i n e s

w i t hc o m m a sa n dc o l o n si n t a c t .T h eq u o t a t i

m a r k sa r ed r o p p e da u t o m a t i c a l l yf r o mt h eb e g i n

n i n g .A m a j o ra d v a n t a g ei s t h a tt h ei n p u tf i l er o u

t i n ew ec o v e r e de a r l i e rw o u l dr e m a i nc o m p l e t

u n c h a n g e d .

A n o t h e r C u r v ei n t h eR o a d

T h eq u o t a t i o nm a r ka p p r o a c hw o r k sw e l l ,a s

l o n ga s y o ud o n ' ta c t u a l l yw a n tt o p l a c e q u o t a

m a r k sw i t h i na l i n e .E x c e p tf o rt h eu s eo f q u o t e a t t h eb e g i n n i n go fa l i n e ,B A S I C ' SI N P U T #i s n o t

c a p a b l eo f r e a d i n gq u o t a t i o nm a r k s ;i n f a c t ,

I N P U T #h i c c u p sw h e n e v e ri t h i t sa q u o t ei n t h e

b o d yo f t h el i n e .T h er e s u l ti s e i t h e ra " b a dd a t a

i n f i l e "m e s s a g e ,o r a c o m p l e t e l ys c r a m b l e dv e r

s i o n o ft h el i n ey o u ' r e t r y i n gt o r e a df r o md i s k .

A sa l w a y s ,t h e r e ' sa f a i r l ys i m p l es o l u t i o n t

c a nb e c a l l e da s a s u b r o u t i n er i g h tb e f o r e d a t i s

w r i t t e nt o o r r e a df r o my o u rf i l e .T h et r i c ki n t h i s

c a s ei s t o t e m p o r a r i l yt r a n s f o r mq u o t a t i o nm a r k s

i n t os o m e t h i n ge l s e — s a y ,a s e l d o m - u s e dg r a p h i

s y m b o l .T h ed a t ac a nb e d e c o d e da s s o o na s i t i s

r e a do u to f t h ef i l ea g a i n .F r o ma p r o g r a m m

p o i n t o fv i e w ,i t ' sa l i t t l em o r ec o m p l i c a t e d

s o m eo t h e rs o l u t i o n sw e ' l lt a l ka b o u t ,b u tt h ee n

c o d e / d e c o d ea p p r o a c hi s v e r y ,v e r yf a s t .T h er o u

t i n e i nF i g .5 - 1 1u s e sB A S I C ' Sl i g h t e n i n g - q

I N S T Rf u n c t i o nt o s c a nt h el i n ef o rq u o t e s .W h e n

6 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 72/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 73/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 74/194

c S T A R T

E X I T

A D D T OS T R I N G

W r i t ee a c hp a s st h r o u g ht h el o o p ,a n a d d i t i o n a lc h a r a c t e ri s r e a da n d a d d e dt o t h es t r i n g :

P A S S #

1

2

3

4

S T R I N G

T

T E

T E S

T E S T

F i g .5 - 1 3 .H o wa s t r i n gi s b u i l to n ec h a r a c t e ra t a t i m ei n a G E T K E Yl o o p .

7 3 1 0G E T # 1 , G T $

7 3 2 0L O O P

7 3 3 0R E T U R N

: R E MG E TS I N

G L EC H A R A C T E R

: R E MK E E P

G O I N G

: R E ME N DO FR O U T I N E

T h eC H R $ ( 1 3 )i n l i n e7 3 0 5i s t h eA S C I Is y m

b o lf o ra c a r r i a g er e t u r n ,w h i c ha l w a y ss i g n a l st h e

e n do fa l i n ei n s e q u e n t i a lf i l e s .T h i si s t h eo n l yw a yt o t e s tf o ra c a r r i a g e r e t u r n .W h e nu s i n gt h eI N

P U To r I N P U T #c o m m a n d si n B A S I C ,y o um a y

h a v es e e nc o n s t r u c t i o n su s e dt o t e s t f o rc a r r i a g e

r e t u r n ss u c ha s :

I N P U TA $ : I FA $= " " T H E NP R I N T" Y O UP R E S S E DR E T U R NO N L Y "

W i t hI N P U T ,a ne m p t ys t r i n g( A $= " " ) i n d i c a t

a c a r r i a g er e t u r n .T h i sd o e sn o tw o r k , h o w e

w i t hG E T # .B e c a u s eG E T #i s p u l l i n g i n d i v

c h a r a c t e r sf r o mt h ef i l e ,e a c hG T $i n t h i se x a m p

w i l lc o n t a i na c h a r a c t e rc o d e — e v e ni n v i s i b l ec h a r a

t e r ss u c ha s c a r r i a g er e t u r n s .

F i g u r e5 - 1 3s h o w sh o wa r e a dc h a r a c t e r

c h a r a c t e rr o u t i n ew o u l dw o r k .T h eI N $s t r i n gw i l l

c o n t a i na n e n t i r el i n eo f t e x tw h e nt h eo p e r a t i

i s f i n i s h e d .T h ec o n t e n to f G T $ ,a s i n g l ec h a r a t e r ,i s s i m p l ea d d e dt o t h er i g h ts i d e o fI N $a t e a c h

p a s s .N o t et h a tI N $i s b u i l tb e f o r et h e r o u t i n es e t s

t h en e x tc h a r a c t e r ,e n s u r i n gt h a ta c a r r i a g er e t u r

i s n e v e rp l a c e da t t h ee n do f I N $( b e c a u s ew h e n

a c a r r i a g er e t u r ni n e n c o u n t e r e d ,t h e l o o pa u t o m a

i c a l l ye n d s ) .I t ' si m p o r t a n tt h a ta c a r r i a g e r e

n o tb e p l a c e da t t h ee n do f a s t r i n g ,b e c a u s et h i s

6 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 75/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 76/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 77/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 78/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 79/194

R e c o r d1

R e c o r d2

s

T

E

V

E

S

M

1

T

H

3

0

5

5

5

5

-

1

2

1

2

<J

O

H

N

F i r s tn a m e

1

L a s tn a m e

( 5 c h a r a c t e r s )

^

( 7 c h a r a c t e r s )

C a r r i a g er e t u r n( d i v i d e rb y t e )

P h o n e

( 1 2c h a r a c t e r s )

D i v i d

e rb y t e

f

F i r s t

n a m e

( 5c h a r a c t e r s )

D i v i d e rb y t e

D i v i d e rb y t e

I nar e l a t i v ef i l e ,a l lf i e l d s

a r es t o r e di ns p a c e so fp r e d e f i n e dl e n g t h s .N o t et h a tS t e v ea n d

J o h nb o t hr e q u i r ef i v eb y t e s ,e v e nt h o u g hJ o h ni so n l yaf o u r - c h a r a c t e rn a m e .S m i t ht a k e su p

s e v e ns p a c e s ,s i n c es e v e nw a st h ep r e - d e t e r m i n e dl e n g t ho ft h eL a s tN

a m ef i e l d .Ad i v i d e r b y t e

s e p a r a t e se a c hf i e l di nt h ef i l e .T h i sd i v i d e ri si n c l u d e dt oa c c o m m o d a t ec a r r i a g er e t u r n s .

F i g .6 - 2 .H o wr e l a t i v er e c o r d sa r ea r r a n g e d .

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 80/194

1 . D e f i n e df i l en a m e sa n dl e n g t h .

2 . A d du p a l l l e n g t h s .

3 . R o u n dr e c o r ds i z eu p w a r d ,i f d e s i r e d .

4 . D e t e r m i n et h en u m b e ro f r e c o r d sp e rs e c t o ra n d a d do n e( y o uc a nd ot h i si n t h ep r o g r a m ) .

5 . D O P E Nt h ef i l e ,a n da t t e m p ta " r e a d "o f t h er e c o r dn u m b e rd e t e r m i n e di n S t e p4 .

6 . I f t h e r e c o r dd o e sn o te x i s t ,c r e a t et h ef i l eb y p r i n t i n ga n u l li n t h el a s tr e c o r dy o ua n t i c i

p a t eu s i n g( r e c o r d1 0 0 , 1 5 0 ,2 0 0 ,e t c . ) .

7 . U p d a t et h et r a c k i n gr e c o r d( # 1 )w i t ha z e r o ,s i n c en od a t ac u r r e n t l y e x i s t si n t h i sn e wf i l e .

8 . D C L O S Et h ef i l et o e n s u r et h a tt h e0 w i l lb e w r i t t e n t or e c o r d1 .

9 . R e o p e nt h ef i l e . a n dr e t u r n t on o r m a lp r o g r a mo p e r a t i o n .

F i g .6 - 3 .S t e p st o c r e a t ea r e l a t i v ef i l e .

d a t ai s s t o r e dt o g e t h e ro nt h ed i s k .T h ed r i v eh e a d

h a st o d o m u c hl e s sm o v i n ga r o u n d .

A P R A C T I C A LE X A M P L ET h ee a s i e s tw a yt o l e a r ns o m e t h i n gi s b y d o

i n gi t .I n t h en e x tf e wp a g e s ,w e ' l lc o n s t r u c ta s i m

p l er e l a t i v ef i l ep r o g r a mu s i n gt h e t e l e p h o n el i s t

o u t l i n e d p r e v i o u s l y .F i g u r e s6 - 3 , 6 - 4 ,a n d6 - 5o u t

l i n et h e s t e p s r e q u i r e dt o c r e a t e ,w r i t et o a n dr e a d

r e l a t i v ef i l e so n t h e C o m m o d o r e1 2 8 .T h r o u g h

r e l a t e dp r o g r a ml i s t i n g s ,y o u ' l lb e a b l et o s e eh o w

e a c ho f t h e s e o p e r a t i o n sw o r k s .

O p e n i n gA s s i g n m e n t s

W i t h o u ta d o u b tt h eh a r d e s tp a r to fu s i n gr e l a

t i v ef i l e si s t h es e t u p p r o c e s si t s e l f .T h eo p e n i n g

v a r i a b l ea s s i g n m e n t st h a tg u i d e t h ec r e a t i o na n d

u s eo f t h ef i l er e q u i r es o m es i m p l ea d d i t i o ne x e r

c i s e sa n ds o m ef o r e t h o u g h t .I f y o u ' v ea l r e a d

p l a n n e do u t t h ef i l e ,t h o u g h , w r i t i n gt h i sp a r t o

y o u rp r o g r a mc a nt a k el e s st h a nf i v em i n u t e s

F i g u r e6 - 6s h o w sa t y p i c a ls u b r o u t i n eu s e dt o

a s s i g n t h ev a r i a b l e sr e q u i r e dt o f u e la r e l a t i v ef i l e

T h ef i r s ta r r a yo fv a r i a b l e s ,d e s i g n a t e db yt h en a m

H E A D $ ,i s i n t e n d e dt o c o n t a i nf i e l dh e a d i n g sf o r

e a c ho f t h ei t e m si n a r e c o r d .T h e yd o n ' th a v et o

b ep l a c e di n t h i sp a r t i c u l a rs p o ti n t h ep r o g r a m ,b u t

s i t u a t i n gt h e mh e r e ,a t t h et o po f t h i sr o u t i n e ,h e l p

t o c l e a r l y i d e n t i f yt h ep u r p o s eo f e a c hi t e mi n t h e

r e c o r d .T h e s eh e a d i n g sc o u l db e u s e da s o n - s c r

p r o m p t sd u r i n g r e c o r d e n t r ya n dc o u l da l s os e r v

a s h e a d i n gl a b e l so n p r i n t o u t s .

F i e l dL e n g t h sa n dI t e mS e c t i o n s .T h en e x ts e t o fv a r i a b l ea s s i g n m e n t sh a n d l e st h ej o bo f

l e t t i n gt h es y s t e mk n o wa b o u tt h e l e n g t ho f e a c

i t e ma n dt h e i rb e g i n n i n gp o s i t i o n si n a r e c o r d

T h eP L A C Ev a r i a b l e sm a r kt h ee x a c ts t a r t i

1 . O p e nt h ef i l eu s i n gD O P E N # ,a n ds p e c i f y i n gt h el e n g t hw i t ht h eL p a r a m e t e r .

2 . R e a di n t h e" N u m b e ro f r e c o r d s "f r o mr e c o r d1 .

3 . P o s i t i o nf o rf i l ep o i n t e ra t t h ep r o p e rr e c o r d ,u s i n gR E C O R D #c o m m a n d .

4 . I s s u et h eR E C O R Dc o m m a n da g a i n ,p o s i t i o n i n gt h ef i l ep o i n t e ra t t h es p e c i f i e dr e c o r d

a n di t e mn u m b e r .

5 . W r i t e t h ei n f o r m a t i o nf o rt h e i t e mu s i n gP R I N T # .

6 . C o n t i n u et h i sp o s i t i o n - a n d - w r i t ep r o c e d u r eu n t i la l li t e m si n t h e r e c o r dh a v eb e e nw r i t t e n .

7 . U p d a t et h eN u m b e r - o f - R e c o r d sr e c o r d ,i f a p p l i c a b l e .

N o t e :T h ef i r s tt w os t e p sn e e do n l yb e p e r f o r m e dw h e nt h ep r o g r a mi s f i r s tr u n .

F i g . 6 - 4 .S t e p st o w r i t e t oa r e l a t i v ef i l e .

7 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 81/194

1 . O p e nt h ef i l eu s i n gD O P E N #a n ds p e c i f y i n gt h el e n g t h w i t ht h eL p a r a m e t e r .

2 . R e a di n t h en u m b e ro f r e c o r d sf o r mr e c o r d1 .

3 . P o s i t i o nt h ef i l ep o i n t e ra t t h ep r o p e rr e c o r d ,u s i n g t h eR E C O R D #c o m m a n d .

4 . I s s u e t h eR E C O R Dc o m m a n da g a i n ,p o s i t i o n i n gt h ef i l ep o i n t e ra t t h es p e c i f i cr e c o r da n d

i t e mn u m b e r .

5 . R e a dt h ei n f o r m a t i o nf o re a c hi t e m u s i n gI N P U T #o r a G E T #l o o p .

6 . C o n t i n u et h i sp o s i t i o n - a n d - r e a dp r o c e d u r eu n t i la l l i t e m si n t h er e c o r dh a v eb e e nr e a d .

N o t e :T h ef i r s tt w os t e p sn e e do n l yb e p e r f o r m e dw h e nt h ep r o g r a mi s f i r s tr u n .O n l yS t e p

5 d i f f e r sf r o mt h ep r o c e d u r et o r e a da r e c o r d .A l lo t h e rs t e p s a r e t h es a m ef o rb o t hr e a do p e r a

t i o n sa n dw r i t eo p e r a t i o n s .

F i g . 6 - 5 .S t e p st o r e a da r e l a t i v ef i l e .

p o i n t sf o re a c hf i e l d .T h i sP L A C Ea r r a yw i l lb e

u s e da s t h ep o s i t i o np a r a m e t e ri n t h eR E C O R D #c o m m a n dw h e ny o ua r ew r i t i n go r r e a d i n gr e c o r d s .

T h eL Ea r r a ys t o r e st h ea c t u a ll e n g t ho f e a c h

i t e m a si t s h o u l db e e n t e r e df r o mt h ek e y b o a r d .

T h i sn u m b e rc o r r e s p o n d st o t h e n u m b e ri n t h e

o r i g i n a lf i l el a y o u td i s c u s s e de a r l i e r ,w h e r et h ef i r s t

n a m ei s t e nc h a r a c t e r s ,t h el a s tn a m ei s t w e n t y ,a n d

t h et w ot e l e p h o n en u m b e r sa r e a s s i g n e dt h i r t e c h a r a c t e r s e a c h .T h eL Ev a r i a b l ec a nb e u s e dn o t

o n l yi n s c r e e n i n gi n p u t ,b u ti n p o s i t i o n i n gi t e m so n

p r i n t e dl i s t sa n do n t h e s c r e e n .

N o t i c eh o wt h e P L A C Ev a r i a b l e sa r e c o m

p u t e d ;t h e ya r et h er e s u l to f t h el a s tP L A C Ev a l u e

6 0 0 0 0

6 0 0 1 0

6 0 0 2 06 0 0 3 0

6 0 0 4 0

6 0 0 5 0

6 0 0 6 0

6 0 0 7 0

6 0 0 8 0

6 0 0 9 0

6 0 1 0 0

6 0 1 1 0

6 0 1 2 0

6 0 1 2 5

6 0 1 4 0

6 0 1 4 5

6 0 1 4 8

6 0 1 5 0

6 0 1 9 0

r e m o p e n i n g a s s i g n m e n t s

h e a d $ ( 1 ) = " f i r s t Mh e a d $ ( 2 ) = M l a s t l f

h e a d $ ( 3 ) = M h o m ep h o n e "

h e a d $ ( 4 ) * w o f f i c ep h o n e 1 *

p l a c e ( 1 ) = 1

p l a c e ( 2 ) = 1 2

p l a c e ( 3 ) = 3 3

p l a c e ( 4 ) = 4 7

l e ( 2 ) = 2 0

l e ( 3 ) = 1 3

l e ( 4 ) = 1 3

l r = 6 4

f i e l d s = 4

f i l e $ x M t e s t "

q u o t e $ s c h r $ ( 3 4 )

e l $ = c h r $ ( 2 7 ) + " q "■

r e t u r n

r e m l e n g t ho f r e c

F i g .6 - 6 .O p e n i n ga s s i g n m e n t sf o ra r e l a t i v ef i l i n gp r o g r a m .

7 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 82/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 83/194

1 4 0 1 0

1 4 0 2 0

1 4 0 3 0

1 4 0 3 2

1 4 0 3 5

1 4 0 3 7

1 4 0 4 0

1 4 0 5 0

1 4 0 6 0

1 4 0 6 5

1 4 0 7 0

1 4 0 7 5

1 4 0 8 0

1 4 0 9 9

1 4 1 0 0

r e m

••

d o p e n # 1 ,■■

n o r m a lo p e n

C f i l e $ ) . l ( l r )

r c r d = 2 5 6 / l r + 1 : r e m n e x t s e c t o r•

r e c o r d # 1

i f d s = 5 0

r e c o r d # 1■•

i n p u t # 1 ,i

r e t u r na . . . . . . .■ • ■ . . ■ • •

, r c r d : r e c o r d# 1 fr c r d

t h e ng o s u b1 4 1 0 0

, 1 : r e c o r d # 1 , 1 : r e m# o f r e c s

i r

• • ■ ■ ■ ■ • • ■ ■ ■ • ■ ■ • ■ ■ ■ ■ ■ ■ ■ ■ ■ ■• • ■ ■ ■ ■ ■ ■ • • ■ ■ ■ • • • ■ • • ■ • ■ ■ a .

F i g . 6 - 7 .A no p e nr o u t i n ef o ra r e l a

t i v ef i l i n gp r o g r a m .N o r m a l l y ,t h i s

r o u t i n ei s c a l l e do n l yo n c e .T h ec a l l

t o 1 4 1 0 0o c c u r so n l yi f t h ef i l ed o e s

n o t y e te x i s t .( T h a t r o u t i n ec r e a t e

t h ef i l e . )

C r e a t i n ga N e wF i l e .T h a n k st o l i n e s1 4 0 3 5 ,

1 4 0 4 0 ,a n d1 4 0 6 0 ,t h eo p e nr o u t i n ea p p e a r sm o r e

c o m p l e xt h a ni t r e a l l yi s .T r a n s l a t e di n t oE n g l i s h

t h e s el i n e sw o u l dr e a d :

1 4 0 3 5 .F i g u r eo u th o wm a n yr e c o r d sf i ti n a

s e c t o ra n da d do n es ow e l lh a v et h en u m b e ro f t h e

f i r s tr e c o r di n t h es e c o n ds e c t o r o ft h ef i l e .S t o r e

t h en u m b e ri n a v a r i a b l ec a l l e dR C R D .

1 4 0 4 0 .P o s i t i o nt h ef i l ep o i n t e rf o rf i l en u m

b e ro n e( o u r c u r r e n t l yo p e nr e l a t i v ef i l e )t o t h e

p r e v i o u s l ys e l e c t e dr e c o r dn u m b e r ,w h i c hi s s t o r e d

i n R C R D . D oi t t w i c ef o rg o o dm e a s u r e .

1 4 0 6 0 .I f a d i s ke r r o r# 5 0o c c u r s( m e a n i n g

t h a tt h er e c o r dd o e s n ' te x i s ty e t ) ,a s s u m et h a tt h i s

i s a b r a n dn e wf i l ea n dc a l lt h er o u t i n ef o rc r e a t i n g

t h en e c e s s a r yn e wr e c o r d s .

A l l o ft h i sh a sp r o b a b l yr a i s e ds o m eq u e s t i o n s .

W h yi s i t n e c e s s a r yt o c r e a t en e wr e c o r d s ?W h y

c a n ' tw ej u s tr e a dt h ef i r s tr e c o r dt o s e ei f t h e r e ' s

a n y t h i n gt h e r e ?W h yd o e st h ep r o g r a mu s et h e

R E C O R D #c o m m a n dt w i c e ?

T h ea n s w e r sa l lh a v et o d o w i t ht h ep e c u l i a r

w a yi n w h i c hD O So p e r a t e s .

W e ' v ea l r e a d y d i s c u s s e dt o s o m ee x t e n tt h e

r e a s o n sf o rc r e a t i n g e x t r ar e l a t i v er e c o r d s :y o u ' r e

e n s u r e dt h a ta c c e s sw i l lb e f a s t e ra n dt h e r e ' sa

g u a r a n t e et h a tt h ep r o g r a mw o n ' tr u no u to f r o o m

a s i t a d d sn e wi n f o r m a t i o n( t h er e c o r d sa r ea l r e a d

t h e r e ) .T h et i m et o c r e a t et h e s eb l a n kr e c o r d si s

w h e nt h ef i l eh a sf i r s tb e e n o p e n e da n d n od a t ah a s

y e tb e e ns t o r e d ,b u th o wc a ny o uk n o wi f t h i si s

a n e wf i l eo r o n et h a th a sb e e np r e v i o u s l yo p e n e d

T h ee a s i e s tw a yi s b y f o r c i n ga d i s ke r r o rb y t e l l

i n g t h ec o m p u t e rt o d o s o m e t h i n gt h a ti s p h y s i c a

i m p o s s i b l e — r e a d i n ga r e c o r dt h a td o e sn o ty e t

e x i s t .

E a r l i e ri t w a sm e n t i o n e dt h a te a c hb l o c ko nad i s kc a nh o l d2 5 6c h a r a c t e r so f i n f o r m a t i o n .T h e

f i l eb u f f e ri n y o u rd i s kd r i v es t o r e st h es a m en u m

b e r .W h e na r e l a t i v ef i l ei s f i r s tc r e a t e d ,D O St a k e s

t h el i b e r t yo f m a k i n ga s m a n yb l a n kr e c o r d s a sw i l l

f i t i n o n es e c t o r ,s i n c ea t l e a s to n es e c t o rw i l lb e

u s e df o rt h i sf i l e .

I n s h o r t ,r e a d i n gt h ef i r s tf e wr e c o r d so f a f i l e

w o n ' tw o r k — t h e r ew o n ' tb e a ne r r o rb e c a u s et h e s e

r e c o r d sh a v el i k e l yb e e nc r e a t e d a l r e a d yw h e nt h e

r e c o r dw a so p e n e d .T of o r c ea n e r r o r( i ft h i si s i n

d e e da n e wf i l e ) ,w em u s tp o s i t i o nt h ef i l ep o i n t e

a t a c u r r e n t l yv i r g i ns e c t o r .

L i n e1 4 0 3 5b r i n g su s t o t h e q u e s t i o nt h a ty o u

p r o b a b l yd i d n ' ta s ko u t l o u db u tw e r en o d o u b t

t h i n k i n g .W h yd o e st h ef o r m u l a( R C R D= 2 5 6/ L R+ 1 ) l o o k s o c o m p l i c a t e d ?

A c t u a l l y ,i t ' sq u i t es i m p l e .I t d i v i d e s t h en u m

b e ro f b y t e si n a s e c t o rb yt h en u m b e ro f b y t e si n

7 3

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 84/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 85/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 86/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 87/194

c o m m a n d .T h em a i nr e a s o ni s t h a ti n r e a d i n g d a t a

f r o ma d i s k ,R E C O R D *s o m e t i m e sf a i l st o w o r k

p r o p e r l y u n l e s s i s s u e dt w i c e .I t ' se a s i e rt o w r i t ea l l

y o u rr o u t i n e su s i n g d o u b l ec o m m a n d s ,t h a nt o t r y

t o r e m e m b e rw h e nR E C O R D *m u s tb e i s s u e d

t w i c e ,a n dw h e ni t n e e dn o tb e .

F o rt h eF O R. . . N E X Tl o o p ,w e l lu s et h e

s a m ec o u n t e rv a r i a b l ew e ' v ec a l l e du p o np r e v i

o u s l y :I T E M .B e c a u s eo f t h ew a yi t i s u s e d ,t h i s

v a r i a b l ew i l la l w a y sc o n t a i n t h en u m b e ro f t h ei t e m

c u r r e n t l yb e i n gr e a df r o mt h ef i l e .I t w i l ls e r v e a s

a m a r k e rf o rt h e c u r r e n t p l a c ei n t h eA $ ( x )a r r a y .

L i n e1 2 0 4 5p o s i t i o n st h e f i l ep o i n t e rt o t h e

s p e c i f i cp l a c ei n t h er e c o r dw h e r ei t s h o u l db e ,a n d

l i n e1 2 0 4 8p r i n t st h ed a t ai n t ot h i ss l o ti n t h er e c o r d .

L i n e s1 2 0 6 0 - 1 2 0 7 5p r i n ta n u p d a t e dm e s s a g e

o nt h es c r e e n a se a c hi t e mi s w r i t t e nt o t h ef i l e .L i n e

1 2 0 8 0f i n i s h e st h el o o p .N e wR e c o r d .N a t u r a l l y ,t h e r eh a st o b e s o m e

w a yt o d e t e r m i n ew h e na r e c o r di s n e w ,s o t h a tt h e

n u m b e r - o f - r e c o r d s r e c o r dc a nb eu p d a t e da p p r o p r i

a t e l y .I f a n e x i s t i n gr e c o r d h a sb e e nr e w r i t t e nw i t h

n e wi n f o r m a t i o n ,w ed o n ' tw a n tt o w r i t e t h en u m

b e ro fr e c o r d st o r e c o r d# 1 ,b e c a u s ei t ' sr e d u n d a n t ;

t h ep r o g r a ms h o u l d a l r e a d yh a v et h ec u r r e n tn u m

b e ro f r e c o r d so n f i l e .

O nt h eo t h e rh a n d ,i f t h er e c o r di s a n e wo n e ,

y o u m o s td e f i n i t e l ys h o u l du p d a t et h en u m b e r - o f -

r e c o r d sr e c o r d .

T h eA D D E Dv a r i a b l es e r v e st h i sp u r p o s ew e l l .

I n e v e r yf i l i n gp r o g r a m ,t h e r es h o u l db e a n a d d - a -

r e c o r dr o u t i n et h a td o e st h ef o l l o w i n g :

1 . A c c e p t se n t r yo f i n f o r m a t i o nf r o mt h e

k e y b o a r d .

2 . S e t s t h eA D D E Df l a g t o- 1 ,i n d i c a t i n g t h a ta

r e c o r di s b e i n g a d d e d .

3 . B u m p su p t h en u m b e ro fr e c o r d sv a r i a b l e( N R )

b y o n e .

O u rw r i t e r o u t i n ea s s u m e sa l lo f t h e s e t h i n g s

h a v ea l r e a d yb e e nd o n e ,i f a p p r o p r i a t e .T h eo n l y

t h i n gw e ' l lc h e c kf o ri s w h e t h e rt h eA D D E Df l a g

i s s e t . I fi t i s ,t h ep r o g r a mw i l lv i s i tt h e s u b r o u t i n e

t h a tu p d a t e st h en u m b e ro fr e c o r d s( s t a r t i n ga t l i n e

1 2 3 0 0 ) .I f t h eA D D E Df l a gi s s e t t o z e r o ,t h ep r o

g r a mw i l ls k i p t h eu p d a t er o u t i n e .

U s i n gQ u o t e sa n dO t h e rT r i c k s .P r e v i o u

c h a p t e r sh a v ed i s c u s s e d t h eu s eo f q u o t a t i o nm a r k

f o r e n c l o s i n gd a t at h a tw i l lc o n t a i nc o m m a so r

c o l o n s .I f y o up l a nt o l e a de a c hs t r i n gw i t h aq u o

t a t i o nm a r k ,s i m p l yd e f i n eQ U O T E $ = C H R $a t

t h es e t u pp o r t i o n o fy o u rp r o g r a m .T h e n ,a d dt h i s

q u o t a t i o nm a r kv a r i a b l et o t h el i n et h a tw r i t e s t

d a t a :

1 2 0 4 8: P R I N T# 1 , Q U O T E $ + A $

I f y o ua n t i c i p a t eu s i n gq u o t a t i o n sm a r k si n y o u r

f i l e s ,y o u ' l lp r o b a b l yw a n tt o u s et h ec o n v e r s i o nr o u

t i n ed i s c u s s e di n C h a p t e r5 . I fy o uu s eq u o t a t i

m a r k s ,b e s u r et o a d do n em o r eb y t ep e ri t e mw h e n

c o m p u t i n gr e c o r dl e n g t h .

R E A D I N GF R O MT H EF I L E

A sy o uc a ns e ef r o ma q u i c kg l a n c ea t F i g .6 - 1 0 ,

t h e r o u t i n et o r e a di n f o r m a t i o nf r o ma r e c o r df o l

l o w st h es a m eg e n e r a lp r o c e d u r e sa s o u rw r i t er o u

t i n e .T h et w od i f f e r e n c e sa r et h a td a t ai s b e i n g r e

u s i n gI N P U T #( i n s t e a do f b e i n gw r i t t e nu s i n g

P R I N T # ) ,a n dt h a tt h e r ei s n ou p d a t et o t h en u m

b e ro f r e c o r d s , s i n c en o t h i n gi s b e i n ga d d e d .

A Q u i c ka n dD i r t yI n p u tR o u t i n e

A f i l i n gp r o g r a mw o u l d b en o t h i n g w i t h am e a n so f e n t e r i n g i n f o r m a t i o nf r o mt h ek e y b o a r

T h ei n p u tr o u t i n ep i c t u r e di n F i g .6 - 1 1i s n o t h i n

f a n c y ,b u ti t w i l ls u f f i c en i c e l y .

I n a d d i t i o nt o a l l o w i n gi n p u t o fi n f o r m a t i o n t h i s

r o u t i n ec h e c k sf o ro v e r - l e n g t hv a r i a b l e s ( t h o s

w o u l dn o tf i ti n t ot h er e c o r d ' si t e mb o u n d a r i e s ) a n d

r a t h e re l e g a n t l yh a n d l e st h e i r r e - e n t r y .

T h i sr o u t i n e a l s ot a k e sa d v a n t a g eo f s o m eo f

B A S I C7 . 0 ' sh i g h - o c t a n ec o m m a n d s ,s u c ha s

D O / U N T I L / L O O Pa n dC H A R .I t a l s ou s e ss o m e

o t h e rt r i c k s ,s u c ha s p r i n t i n gk e y b o a r de s c a p ec o d e s

t o c l e a ra l i n eo n t h es c r e e n .

W h i l et h i sr o u t i n eu s e sa l o to f f l a g sa n do t h e r

v a r i a b l e s ,i t ss t r u c t u r em a k e si t p r e t t ye a s yt o d e

c i p h e r .T h ef o l l o w i n gv a r i a b l e sc o m ef r o mo t h e r

p l a c e si n t h ep r o g r a mo r a r eu s e db y o t h e rp a r t s

o f t h ep r o g r a ma f t e rt h i sr o u t i n ef i n i s h e sw i t h t h e

7 7

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 88/194

1 3 0 0 0

1 3 0 1 0

1 3 0 1 5

1 3 0 2 0

1 3 0 3 0

1 3 0 4 0

1 3 0 4 51 3 0 4 8

1 3 0 5 0

1 3 0 6 0

1 3 0 7 0

1 3 0 7 5

1 3 0 8 0

1 3 2 9 9

• • • ■ • • • • ■

r e m

r c r d = r e c + 1

r e a df r o m f i l e

r e c o r d # 1 , r c r d

f o r i t e m s 1t o f i e l d s

: r e c o r d # 1 , r c r d , p l a c e ( i t e m ): i n p u t # 1 , a $ ( i t e m )

: c h a r , 1 , 1 0 :r e m p o s i t

: p r i n t" r e a d i n g i t e m #*

: p r i n t a $ ( i t e m ) N

n e x t

r e t u r n

c u r s o r

i t e m 1 1f r o mn

r e c o r d #" r c r d

F i g .6 - 1 0 .A r o u t i n e t or e a da r e c o r df r o ma r e l a t i v ef i l e .

A s i n t h e p a s t ,w e ' l l r e f e r t ot h e s ea s e x t e r n a l

v a r i a b l e s :

A D D — d e t e r m i n e sw h e t h e ro r n o tt h es c r e e ns h o u l d

c l e a r( i s t h i sa n a d d e di t e m w i t hn o e x i s t i n g

d a t a ? ) .

F I E L D S — y o u ' v ec e r t a i n l ys e e nt h i so n eb e f o r e ;i t ' s

t h en u m b e ro f f i e l d s i nt h ea r r a y .

H E A D $ ( x ) — t h eh e a d i n g( o rt i t l e )f o re a c hi t e mi n

t h ea r r a y .T h e s ei t e mt i t l e sw e r ed e f i n e di n t h e

o r i g i n a ls e t u pr o u t i n ef o rv a r i a b l e s .

A $ ( x ) — t h ev a r i a b l ew h i c hs t o r e sa r r a ye l e m e n t sa s

t h e ya r ee n t e r e d .T h i si s a l s ot h ev a r i a b l eu s e d

t o w r i t ea n dr e a dd a t at o a n d f r o mt h ef i l e .

M u c hm o r en u m e r o u sa r e t h er o u t i n e si n t e r n a l

v a r i a b l e s ,w h i c hs e r v e a sf l a g s ,o r s w i t c h e s , t om o n

i t o rt h e p r o g r e s so fd i f f e r e n te v e n t s .T h e r ea r ef l a g s

t o t e l lt h ec o m p u t e ri f t h e i n f o r m a t i o ne n t e r e di s

c o r r e c t .T h e r ea r e f l a g st o t e l l t h e c o m p u t e r

w h e t h e rt h el a s ti t e me n t e r e dw a st o ol o n g .T h e r e ' s

e v e n af l a gt h a tl e t st h em a c h i n ek n o wi f t h i si s t h e

f i r s tt i m et h e i t e mi s b e i n ge n t e r e d ,o r i f i t ' sa r e t r y

a f t e ra t o o - l o n ge n t r y .

A l lt h e s ef l a g ss e r v ea p u r p o s e :t h e ya l l o w t h e

r o u t i n e t ob e d r i v e nw i t hD O / U N T I L / L O O Ps t r u c

t u r e si n s t e a do f m o r e c u m b e r s o m eG O T O

b r a n c h e s .

T h ei n t e r n a lv a r i a b l e sf o rt h i sr o u t i n ei n c l u d e

t h ef o l l o w i n g :

O K — t h i sf l a g i n d i c a t e s t h a tt h ea n s w e rt o t h e" c o r

r e c t ? "p r o m p tw a s" y "o r " y e s . "I t d r i v e st h e

D O / U N T I Ll o o pt h a tk e e p st h ep r o g r a ma s k

i n g" i s t h i sc o r r e c t ? "u n t i lt h eu s e ra n s w e

I T E M — t h es a m ev a r i a b l ew e ' v eu s e db e f o r et o

k e e pt r a c k o f c u r r e n ti t e mi n a n a r r a y .

F R S T — i n d i c a t e sw h e t h e rt h i si s t h ef i r s tt i m et h i s

i t e m h a sb e e ne n t e r e d( o nt h i sp a s s ) .T h i sv a r i

a b l em a k e ss u r et h a tt h ep r o g r a md o e s n ' ts k i p

t h e D O / W H I L El o o pf o r v e r i f y i n g v a l e n g t h .

B I G — w a st h el a s ti t e mt y p e dt o oB I G ?I f i t w a st o o

l o n g ,t h e p r o g r a mw i l l c o n t i n u ei n t h e

D O / W H I L El o o pf o r l e n g t h ,u n t i lt h e l e n g t

d e c r e a s e s .

Y $ — t h ei n p u tv a r i a b l e t h a t s t o r e st h eu s e r ' s r

s p o n s et o t h e" i st h i sc o r r e c t ? "p r o m p t .

T h e s ev a r i a b l e ss h o u l dg i v ey o us o m ei n s i g

i n t ot h ew o r k i n g so f t h e i n p u tr o u t i n e .O fc o u r s

s o m eo f t h e t r i c k se m p l o y e dh e r eb e a rc l o s e

s c r u t i n y .

H o wt h eI n p u t R o u t i n eW o r k s .T h ei n

p u tr o u t i n ei n F i g .6 - 1 1i s b a s i c a l l yt h r e e b i g l

w i t hs o m ef a n c ys t u f f i n g si n s i d e .

T h ef i r s ti s t h eD OU N T I LO Kl o o p ,w h i c

k e e p sa c c e p t i n g e n t r yo fn a m e sa n dp h o n en u m b e

u n t i lt h eO Kv a r i a b l ei s e q u a lt o - 1 ( w h i c hh a p

7 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 89/194

p e n st o b e w h e nt h eu s e ra n s w e r s" Y "t o t h e" i s

t h i sc o r r e c t ? "p r o m p ta f e wl i n e sd o w ni n t h e

p r o g r a m ) .

I n s i d et h i sf i r s tl o o pi s t h eF O R . . .N E X Tl o o p ,

w h i c hb e g i n sa t l i n e4 0 6 0 .I t ' sp r e t t y s t a n d a r d .T h e

I T E Mv a r i a b l es t a r t so u ta t o n ea n dc o n t i n u e su n

t i lr e a c h i n gt h e v a l u eo f F I E L D S ,w h i c hr e p r e s e n t s

t h en u m b e ro f f i e l d s i na r e c o r d .O fc o u r s e ,i n b e

t w e e nt h eF O Ra n dt h eN E X Ta r ea g r o u po fc o m

m a n d sd e s i g n e dt o a c c e p ta n dv e r i f yk e y b o a

e n t r y .

C o m m a n d sI n s i d et h eF O R. . . N E X L o o p .T h es e to f c o m m a n d sn e s t l e di n s i d et h e

F O Ra n dN E X Ts t a t e m e n t si s p e r f o r m e dw i t he a c h

p a s st h r o u g ht h el o o p .

T h ef i r s tc o m m a n di n s i d et h e l o o pi s :

C H A R, 0 , 2 3 , E L $

4 0 0 0

4 0 1 0

4 0 2 0

4 0 3 0

4 0 4 0

4 0 5 0

4 0 6 0

4 0 7 0

4 0 8 0

4 0 9 0

4 1 0 0

4 1 1 0

4 1 2 0

4 1 3 0

4 1 4 0

4 1 5 0

4 1 6 0

4 1 6 5

4 1 7 0

4 1 8 0

4 1 8 5

4 1 8 8

4 1 9 0

4 2 0 0

4 3 0 0

4 3 1 0

4 3 2 0

4 3 3 0

4 3 4 0

4 3 5 0

4 3 6 0

4 3 7 0

4 3 8 0

4 3 9 0

4 4 0 0

r e m i n p u ti t e m s

i f a d d t h e ns c n c l r

o k = 0 :d o u n t i lo k

c h a r , 0 , 1

f o r i t e m = 1t o f i e l d s

c h a r , 0 , 2 3 , e l $

f r s t = - 1

d o w h i l eb i g o r f r s t

c h a r , 0 , i t e m: r e m p o s i t e m

p r i n t h e a d $ ( i t e m ) ;

i n p u t a $ ( i t e m )

g o s u b4 3 0 0 : r e mc h e c kl e n g t h

f r s t = 0 :r e m n o l o n g e r1 s t

l o o p

n e x t

c h a r , 0 , 2 3 , 9 1 $ + " i s t h i s c o r r e c t "

i n p u ty $

i f i n s t r ( M Y y " , l e f t $ ( y $ , 1 ) )> 0 t h e no k = - 1 : e l s eo k = 0••

l o o p

r e t u r n

r e m c h e c k l e n g t h

i f l e n ( a $ ( i t e m ) )> l e ( i t e m )t h e nb e g i n

p l a y" s c e g "

c h a r , 0 , 2 3 , " i t e mt o o l o n g— p l e a s er e - e n t e r 1 1

c h a r , 0 , i t e m , e l $

b i g * - 1 : r e m r e d o

b e n d : e l s e b i g = 0

r e t u r n

F i g .6 - 1 1 .A s i m p l er o u t i n ef o rt h e e n t r yo f r e c o r d s .

7 9

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 90/194

T h i sc o m m a n dp o s i t i o n st h e c u r s o ra t c o l u m nz e r o ,

l i n e2 3 ,a n dc l e a r st h i sl i n ew i t ha n E S C - Q .T h e

c l e a r - t o - e n d - o f - l i n ei s n e c e s s a r yb e c a u s es o m e

p r o m p t s a n d m e s s a g e sw i l lb e p l a c e do n t h i sl i n e

d u r i n go p e r a t i o n .I t ' si m p o r t a n tt o c l e a rt h e s em e s

s a g e sa s s o o na s t h e y ' r e o b s o l e t e ,s o a s n o tt o c o n

f u s et h ep r o g r a mo p e r a t o r .F o re x a m p l e ,e v e ni f t h e

l a s ti t e mw a st o ol o n g ,y o uw o u l d n ' tw a n tt h es a m e

" i t e mt o ol o n g "m e s s a g et o c o n t i n u et o a p p e a rf o r

e n t r yo f t h en e x ti t e m ;i t s h o u l db e c l e a r e d .S ot h e

C H A Rc o m m a n da t l i n e4 0 7 0c l e a r st h i sl i n ef o r

g o o dm e a s u r e — e v e ni f t h e r ew a s n ' ta n y t h i n gt h e r e .

N e x t ,t h eF R S Tv a r i a b l ei s s e t t o- 1 , m e a n

i n gt h a tt h i sf i r s t - t i m e - t h r o u g h f l a gi s o n .O n c et h e

i t e m h a sb e e ne n t e r e df r o mt h ek e y b o a r do n c e ,t h e

F R S Tf l a gw i l lb e s e t t oo f f( 0 ) .S e t t i n g t h eF R S T

v a r i a b l ei n t h i sw a y m a k e ss u r e t h a t t h e

D O / W H I L El o o pi n t h en e x tl i n ew o n ' tj u m pt o t h ee n do f e n t r y p r e m a t u r e l y .

T h eD O / W H I L El o o pi n l i n e4 0 9 0t e l l st h e s y s

t e mt o k e e pa s k i n gf o rt h i si t e m w h i l ei t i s t o oB I G ,

o rw h e ni t i s t h eF R S Tt i m et h r o u g h .T h eB I Gv a r i

a b l ew i l lb e s e to no r o f fi n a s u b r o u t i n et h a ti s c a l l e d

f r o mw i t h i nt h i sl o o p .

T h eC H A Rc o m m a n di n l i n e4 1 0 0p o s i t i o n st h e

c u r s o ra t c o l u m nz e r oo n t h e l i n es p e c i f i e db y

I T E M .T h i sm e a n st h a tt h ei t e mo n ew i l lb e i n p u t

o nl i n eo n e ,i t e mt w ow i l lb e i n p u to nl i n et w o ,a n d

s o o n . I t ' s a p o o rm a n ' s a n s w e rt o s c r e e n

f o r m a t t i n g — o n ew ed i s p e n s ew i t hi n t h ec h a p t e ro n

p r o f e s s i o n a lp r o g r a ma p p e a r a n c e .F o rn o w ,i t ' l lb e

f i n e .

T h eP R I N Ts t a t e m e n ti n 4 1 1 0p r i n t st h eh e a d

i n ga t t h e c u r r e n tc u r s o rp o s i t i o n ,w h i c hw a ss p e c i

f i e db yt h eC H A Rc o m m a n da t l i n e4 1 0 0 .I f y o u ' r e

o b s e r v a n t ,y o u ' l ln o t i c et h a tt h ep r o g r a mc o u l dh a v e

c o m b i n e dl i n e s4 1 0 0 a n d 4 1 1 0i n t oa c o m m a n dt h a t

l o o k e dl i k et h i s :

C H A R, 0 , I T E M , H E A D $ ( I T E M )

T h et w o - l i n ea p p r o a c hi n o u rp r o g r a mr o u t i n e

i s s i m p l y a n o t h e ri l l u s t r a t i o nt h a tt h es a m ee f f e c t

c a nb e o b t a i n e di n m a n yd i f f e r e n tw a y s .

L i n e4 1 2 0a s k sf o rt h e i t e m .B e c a u s et h e r e ' s

a s e m i c o l o na t t h ee n do ft h el a s tP R I N Ts t a t e m e n t ,

t h e c u r s o ri s s t i l ln e x tt o t h i st i t l e ;t h ei n f o r m a t

t y p e do n t h ek e y b o a r dw i l la p p e a rd i r e c t l yt o t h e

r i g h t o ft h ei t e mt i t l e .

F i n i s h i n gu pt h eF O R . . .N E X Tl o o p ,w eh a v e

a c a l lt o t h es u b r o u t i n ea t l i n e4 3 0 0 ,w h i c hd e t e r

m i n e si f t h ei n p u ts t r i n gi s t o ol o n g ,a n ds e t st h e

B I Gv a r i a b l ea c c o r d i n g l y .F R S Ti s r e s e tt o z e r o

s i n c ea t t h i sp o i n ti n t h ep r o g r a mw ec a n g u a r

t e e t h a tt h i ss t r i n gh a sb e e ne n t e r e da t l e a s to n c e

C o n f i r m a t i o n . A tt h et a i le n do f t h ei n p u

r o u t i n ei s a p r o m p tt h a ta s k st h e q u e s t i o n" i st h i s

c o r r e c t "( b e c a u s et h eI N P U Ts t a t e m e n tp r o v i d

i t so w nq u e s t i o nm a r k ,t h e r e ' sn o n e e df o ro n ei n

t h eC H A Ro p e r a t i o na t l i n e4 1 7 0 ) .

F i n a l l y ,l i n e4 1 8 5s t r i p sd o w nt h ea n s w e rt o t h e

l e f t m o s tc h a r a c t e re n t e r e da n dl o o k sf o ra " Y y

m a t c h u p .I f a m a t c hi s f o u n d ,O Ki s t u r n e do n( - 1 )

a n dt h ec o n d i t i o nf o rt h eb e g i n n i n gl o o p( D OU NT I L O K )i s s a t i s f i e d .I f t h i n g s a r e n o tO K ,t h er o u

t i n eg o e sb a c kt o t h e t o pf o r r e - e n t r y o ft h en a m

a n dt e l e p h o n en u m b e r s .

O t h e rR o u t i n e sR e l a t e dt o I n p u t

T h er o u t i n ei n F i g .6 - 1 1d o e s al o ti n a s h o r

s p a c e ,b u tt h e r e ' su s u a l l ym o r er e q u i r e df o rf i l er o u

t i n e s .U s u a l l y ,s e p a r a t e a d d - a - r e c o r da n dc h a n g

a - r e c o r dr o u t i n e sw i l lc a l lt h i sm a i ne n t r yr o u t i n

T h i sa l l o w s t h ep r o g r a mt o d i s p l a yd a t aw h e r

n e c e s s a r ya n dt o s e t d i f f e r e n t f l a g sd e p e n d i no n

w h e t h e rt h er e c o r dw a sa d d e do r i s m e r e l yb e i n

c h a n g e d .F i g u r e s6 - 1 2a n d6 - 1 3s h o wt y p i c a lr o u

t i n e s .T h e s er o u t i n e su s em a n yo f t h es a m ep r o c

d u r e sc a l l e du p o na l r e a d yi n o u ri n p u tr o u t i n e

A d d i n ga R e c o r d . T h ea d d - a - r e c o r dr o u t i

i n F i g .6 - 1 2p e r f o r m st h e f o l l o w i n gf u n c t i o n s

1 . I ti n i t i a t e sa l o o pt o c o n t i n u ea d d i n gr e c o r

u n t i li t i s n o l o n g e r n e c e s s a r y .

2 . I t s e t st h eA D Df l a gs o t h e s c r e e nw i l lb ec l e a r e db e f o r ei n p u t .

3 . I t c a l l st h ei n p u tr o u t i n ea n da c c e p t se n t r y

4 . I t b u m p su p t h e r e c o r d c o u n t e rb yo n e ,a n da s

s i g n st h i sn e w( h i g h e s t )r e c o r dn u m b e ra s t h

c u r r e n tr e c o r d .

5 . I t c a l l st h er o u t i n et h a tw r i t e st o t h er e c o r d

a n dc o n t i n u e sw i t ht h eD O / U N T I Ll o o p .

8 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 91/194

T h i sp r o c e s sc o n t i n u e su n t i lt h eu s e ra n s w e r s

" N "o r " N O "t o t h e" a d da n o t h e r ? "p r o m p t .

C h a n g i n ga R e c o r d .A ss h o w ni n F i g . 6 - 1 3 ,

c h a n g i n g ar e c o r di s a b i tm o r ec o m p l i c a t e d ,b e

c a u s ei n f o r m a t i o nh a st o b er e t r i e v e df r o mt h e d i s k

a n dt h e nd i s p l a y e df o rr e v i e w .

W es t a r ta t l i n e3 4 0 0 ,w h e r ea r o u t i n ea c c e p t

a r e c o r dn u m b e rf r o mt h ek e y b o a r d ,a n dt h e nc a l l s

t h et r u ee d i tr o u t i n e ,w h i c hd o e sm o s to f t h ew o r k

T h i sp r o c e s sc o n t i n u e su n t i lt h eu s e ri n d i c a t e s

h e d o e s n ' tw i s ht o c h a n g ea n o t h e rr e c o r d .

T h ee d i tr o u t i n es t a r t i n g a tl i n e3 4 0 0p e r f o r

3 0 0 0

3 0 1 0

3 0 1 2

3 0 1 5

3 0 2 0

3 0 3 0

3 0 4 0

3 0 5 0

3 0 6 0

3 0 7 0

3 0 8 03 0 9 0

3 1 0 0

3 1 1 0

3 1 1 5

3 1 2 0

3 1 3 0

3 1 3 5

3 1 4 0

3 1 5 0

3 1 6 0

3 1 6 53 1 7 0

3 4 0 0

3 4 1 0

3 4 2 0

3 4 3 0

3 4 4 0

3 4 5 0

3 4 6 0

3 4 7 0

3 4 8 0

3 4 8 53 4 9 0

3 4 9 1

3 4 9 2

3 4 9 4

3 4 9 6

3 4 9 8

r e n t

a g a i n = - 1

d o u n t i ln o t a g a i n

a d d = - 1 : r e m

a d d a r e c o r d

a d d f l a g

g o s u b 4 0 0 0: r e m i n p u tr t n

n r = n r + 1 : r e m b u m p #o f r e c s

r e c = n r

g o s u b1 2 0 0 0 :r e m w r i t er e c o r d

c h a r, 0 , 2 3 > e l $ + f f a d da n o t h e r "

i n p u t y $

i f i n s t r ( t t Y y M , l e f t $ ( y $ , 1 ) )> 0 t h e na g a i n * - 1: e l s ea g a i n = 0l o o p

a d d e d = 0r e t u r n

■ ■ ■ ■

r e m

i ■ ■ ■ ■

a g a i n = - 1

c h a n g ea r e c o r d

d o u n t i ln o t a g a i n

r e c = 0

d o u n t i lr e c > 0 a n d r e c < = n r

s c n c l r : i n p u t H r e c o r d( # ) H ; r e cl o o p

g o s u b3 5 0 0 : r e m e d i t r e cc h a r , 0 , 2 3 , e l $ + l f c h a n g ea n o t h e r "

i n p u t y $

i f i n s t r ( " Y y H , l e f t $ ( y $ f 1 ) )> 0 t h e na g a i n * - 1 : e l s e a g a i n =l o o p

r e t u r n

F i g .6 - 1 2 .R o u t i n e st o a c c e p ta n e n t r ya n da d do r c h a n g ea r e c o r d .

8 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 92/194

e d i t a r e c o r d

3 5 0 0: : : :

3 5 1 0r e m

3 5 2 0

3 5 3 0

3 5 4 0g o s u b1 3 0 0 0 : r e m r e a d r e c

3 5 5 0

3 5 6 0g o s u b 3 8 0 0: r e m d i s p l yd a t a

3 5 7 0

3 5 8 0g o s u b 4 0 0 0: r e m i n p u tr t n

3 5 9 0

3 6 0 0g o s u b1 2 0 0 0 :r e m w r i t er e c o r d

3 6 1 0

3 6 2 0

3 6 3 0

3 6 4 0 r e t u r n

F i g .6 - 1 3 .A r o u t i n et o e d i ta r e c o r d .

t h e f o l l o w i n gs i m p l es t e p s :

1 . R e a d st h es p e c i f i e dr e c o r db y c a l l i n gt h er e a d

r o u t i n e .

2 . D i s p l a y st h ed a t ar e a df r o mt h ef i l e ,b yc a l l i n g

a d i s p l a yr o u t i n e .

3 . I n p u t s c h a n g e s ,b y c a l l i n gt h ei n p u tr o u t i n e .

T h i se d i tr o u t i n ec o u l db e c a l l e db y a n u m b e r

o f p r o g r a mo p t i o n s .F o re x a m p l e ,i f y o ui n c l u d e d

a r o u t i n et o s e a r c hf o rr e c o r d s ,y o uc o u l dc a l lt h i sr o u t i n ew h e nt h er e c o r d sw e r el o c a t e d .

G O I N GF U R T H E R

T h ec o m p l e t ep r o g r a mi n A p p e n d i xB p u l l s

s o m ed i f f e r e n ti d e a st o g e t h e ri n t oa m o r es o p h i s t

c a t e dr e l a t i v e - f i l e sp a c k a g e .I t e m p l o y ss o m eo f t h e

c o n c e p t sd i s c u s s e di n o t h e rc h a p t e r s ,s u c ha s t h e

u s eo f m o r ea e s t h e t i cs c r e e nf o r m a tr o u t i n e sa n d

m e n u s .

8 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 93/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 94/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 95/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 96/194

V a r i a b l e sU s e dI n s i d et h e I n p u tR o u

t i n e .T h e r ea r ef e we x t e r n a lv a r i a b l e su s e di n t h i s

r o u t i n e ,o t h e rt h a nt h es e t u pv a r i a b l e sa l r e a d yd i s

c u s s e d .T h ev a r i a b l eI N $r e t u r n sw h a t e v e rh a s

b e e ne n t e r e df r o mt h ek e y b o a r d .V , H , a n dL E

h a v eb e e np r e v i o u s l yd i s c u s s e d .

T h e r ea r ea l o to f i n t e r n a l v a r i a b l e s ,b e c a u s e

t h e r e ' sa l o tt o k e e pt r a c ko f . I n m a i np r o c e s s i n g

r o u t i n e( F i g .7 - 2 ) ,t h e f o l l o w i n gv a r i a b l e sa r e u s e d :

B K S T h i sf l a gi s s e tb y a s u b r o u t i n ea n yt i m ea

b a c k s p a c ec h a r a c t e ri s p r e s s e d .

D U NI n d i c a t e s t h a ty o ua r ed o n ew i t he n t r yo f t h i s

l i n e .T h i sf l a gi s s e t i n t h i sr o u t i n ei f y o u

p r e s sR E T U R No r E S C a p e .

C H $ H o l d st h el a s tc h a r a c t e rt y p e d .T h i sc h a r a c

t e ri s t h e n a n a l y z e da n dp l a c e di n t h es t r i n g

I N $ ,i f a p p r o p r i a t e .C H T h i sv a r i a b l ec o n t a i n s t h eA S C I Iv a l u eo f

t h et y p e dc h a r a c t e r .T h er e a s o nw ec o n v e r t

t h es t r i n gC H $i n t oa n u m e r i cv a r i a b l ei s

t h a to p e r a t i o n s i n v o l v i n gn u m e r i cv a r i a b l e s

a r e m u c hf a s t e r ;i t s p e e d su p t h e e n t

r o u t i n e .

I N A f l a gu s e dt o i n d i c a t ew h e t h e rt h ec h a r a

t e rt y p e ds h o u l db e i n c l u d e di n t h ee n t e r

s t r i n g ( I N $ ) .I f R E T U R N ,E S C a p e ,o r a

b a c k s p a c ec h a r a c t e rw a st y p e d ,I N w i l lb e

z e r o .

L N T h i sv a r i a b l ei s s e t i na s u b r o u t i n ea n dc o n

t a i n st h e c u r r e n t l e n g t h o fI N $ .I t i s u s e

t o t e s tt o m a k es u r et h a tt h e e n t r yh a se x

c e e d e dt h e a l l o w a b l el e n g t h( s t o r e di n L E )

T h e s ev a r i a b l e sa r e t h ec o r eo f t h ee n t r yr o u

t i n e .A s y o uc a ns e ef r o mF i g .7 - 3 ,a l m o s te v e r

l i n eo f t h er o u t i n ei n v o l v e sa t l e a s to n eo f t h e m

L e t ' st a k ea m i n u t et o r e v i e wt h et w o m a

c o m p o n e n t so f t h i ss u b p r o g r a m .O n ei s a m a i

p r o c e s s i n gr o u t i n e ,w h i c hc o n t a i n s t h e l oa n db u i l d st h e I N $e n t r yv a r i a b l ea s c h a r a c t e r

t y p e d .T h eo t h e rm a j o rr o u t i n es c r e e n s e n t ra n d

s e t sf l a g sb a s e do n t h e c h a r a c t e r t y p e d

T h eM a i nP r o c e s s i n gR o u t i n e .T h em a i

5 0 0 0

5 0 2 0

5 0 4 0

5 0 5 0

5 0 6 05 0 8 0

5 0 8 5

5 0 9 0

5 1 0 0

5 1 2 0

5 1 3 0

5 1 4 0

5 1 6 0

5 1 8 0

5 1 9 0

5 2 0 05 2 1 0

5 3 0 0

5 3 9 0

5 3 9 2

5 3 9 5

r e m i n p u t r o u t i n e

t e m p o 2 5 5

g o s u b 5 9 0 0 : r e m c l r b u f f e ri n $ = M r t: c h $ = M M : i n = 0 : d u n = 0

i f l e > 2 4 0 t h e n l e = 2 4 0

d o u n t i ld u n

c h a r . h . v . c a s e S + i n S + c s r S + r e s e t S

g e t k e yc h $

c h = a s c ( c h $ )

g o s u b5 4 0 0 : r e m t e s t c h

i f d u nt h e ne x i t

i f b k s t h e nb k s = 0 : g o s u b5 9 5 0

i f i n t h e n i n $ = i n $ + c h $

l o o p

c h a r , h , v , c a s e $ + i n $ + H H + r e s e t $

r e t u r n

F i g . 7 - 2 .T h em a i ne n t r yr o u t i n e .

8 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 97/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 98/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 99/194

w a n tt o g e t o u to f t h e c u r r e n tr o u t i n e .T h i sf l a gw i l l

r e m a i ns e tw h e nt h ee n t r yr o u t i n e r e t u r n st o o t h e r

p a r t s o ft h ep r o g r a m .T h e s eo t h e rs e c t i o n s o ft h e

p r o g r a mc a nt e s tf o rE S C ,a n dt a k ea p p r o p r i a t ea c

t i o ni f i ti s s e t t o- 1 .

B a c k s p a c e s .Y o u ' v ep r o b a b l yn o t i c e d t h e

l i n e sg e t t i n gl o n g e ra n dl o n g e r .T h en e x to n e ' sa

r e a lt h i c k e to f d e c i s i o nm a k i n g .I t t e s t s f o ra l lc u r

s o r c h a r a c t e r sa n do t h e rk e y s t r o k e st h a tm i g h t c o n

f u s e t h er o u t i n e .T h es y m b o l sa n dt h e i rA S C I I

c o d e sa r e n o tr e a d i l ya p p a r e n tf r o mt h i sl i n e ,s o

h e r e t h e ya r e :

( C H > 1 4 4 A N D

C H < 1 4 9 )

C H = 1 5 7

C H = 9 5

( C H < 3 2 )

T e s t sf o r d o w na r r o w ,

R V SO F F ,C L RH O M E ,

a n dI N S .

T e s t sf o rt h ec u r s o r - l e f t

k e y .

T e s t sf o r t h e l e f t w a r d -

a r r o wk e ya t t h eu p p e r

l e f ts i d eo f t h ek e y b o a r d .

T e s t sf o r a n yi n v i s i b l e

c o n t r o lc h a r a c t e r s .

I f a n yo f t h e s ek e y sw a st h eo n ep r e s s e d ,B K S

i s s e tt o o n .I n e f f e c t ,t h i sl i n ec o n v e r t sa l lo f t h e s e

k e y s t r o k e st o b a c k s p a c e s .B e c a u s e y o uw o u l d n ' t

w a n ta b a c k s p a c ec h a r a c t e rt o p r i n t ,I N i s s e tt o

z e r o .S i n c en o f u r t h e rt e s t sa r en e c e s s a r yb e f o r e

a c c e p t i n gt h e n e x tc h a r a c t e r ,t h i s l i n eg o e st o

5 5 3 0 — t h ee n do f t h es u b r o u t i n e .

C o n v e r t i n gG r a p h i c sC h a r a c t e r s .L i n e

5 5 0 0p e r f o r m st h ef u n c t i o n s o fc o n v e r t i n gs h i f t e d

k e y b o a r dc h a r a c t e r s ,w h i c hw o u l dn o r m a l l ya p p e a r

a s g r a p h i c s y m b o l s ,i n t on o r m a ll e t t e r s .I t d o e st h i s

b ys h i f t i n gd o w nt h eA S C I Ic o d eb ya v a l u eo f 1 2 8 ,

w h i c hi s t h eg a ps e p a r a t i n gl e t t e r sf r o mg r a p h i

c h a r a c t e r s .T h i n ko f i t a s s i n g i n gt h es a m es o n gi n

a l o w e ro c t a v e .

I f y o u rp r o g r a m w o r k e di n t h el o w e r c a s e /

p e r c a s em o d e ,a s i m i l a rr o u t i n ec o u l db e u s e dt o

l o c ko u tl o w e r c a s ec h a r a c t e r sb y s h i f t i n gt h e mu p

w a r d b y1 2 8 .Y o u ' df i r s th a v et o d e t e c tw h e t h e rt h e

c h a r a c t e rw a sa l p h a b e t i cl o w e r c a s ei n o r d e rt o e n

s u r ey o ud i d n ' ts t e po n a n yo t h e rt e s t s i nt h er o ut i n e .T h u s :

( C H > 6 4 A N D C H < 9 1 )

w o u l dr e p l a c e t h eC H >9 6 t e s ti n t h e c u r r e n tl i n e .

T e s t i n gf o rN u m b e r s .B a c ki n t h e S e t u p

V a r i a b l e ss e c t i o nw em e n t i o n e da v a r i a b l en a m e d

N M B R ,w h i c hi s s e tt o - 1 w h e nt h e e n t r y s h o

a c c e p tn u m b e r so n l y .N M B Rc o m e si n t op l a ya t l i n e

5 5 0 5 .

T h et e s to n t h i s l i n et a k e sa d v a n t a g ea g a i no f

5 4 0 0:

5 4 1 0d u n = 0 : i n = - 1 : b k s = 0 : e s c = 0

5 4 1 5:

5 4 2 0r e m t e s tc h a r a c t e r

5 4 3 0l n = l e n ( i n $ ) : r e mc u r l e n

5 4 4 0i f c h = 1 3 t h e n d u n = - 1 : i n = 0 : g o s u b5 8 0 0 :r e m c r

5 4 6 0i f c h = 2 7t h e nd u n = - 1 : i n = 0 : e s c = - 1 : r e me s c

5 4 8 0i f ( c h > 1 4 4 ' a n d c h < 1 4 9 ) o r c h = 1 5 7 o r c h = 9 5o r ( c h < 3 2 ) t h e nb k s = - 1: i n

= 0 : g o t o5 5 3 0

5 5 0 0i f c h > 9 6 t h e n c h = c h - 1 2 8 : c h $ = c h r $ ( c h ) : g o t o 5 5 2 0

5 5 0 5i f n m b r a n d ( c h > 5 7 o r c h < 4 5 ) t h e n p l a y b l e e p $ : i

5 5 2 0i f l n = > l et h e n p l a y b l e e p $ : i n = 0

5 5 3 0r e t u r n

F i g . 7 - 4 .A t e s tc h a r a c t e rr o u t i n e .

8 9

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 100/194

A S C I I ' sg r o u p i n gr e l a t e dc h a r a c t e r st o g e t h e r .

S i n c et h en u m b e r s0 - 9a r el o c a t e di n t h eA S C I It a

b l eb e t w e e n4 5 a n d5 7 ,t h e r o u t i n ea s s u m e st h a t

a n y t h i n gb e l o wo r a b o v et h a tr a n g ei s n o ta n u m

b e r .A b l e e pi s s o u n d e d .I N i s s e t t o z e r o ,s i n c e

w h a t e v e rw a st y p e ds h o u l d n ' tb e p l a c e di n t h ee n

t r y s t r i n go r d i s p l a y e do nt h es c r e e n .T h e nt h el i n e

b r a n c h e st o 5 5 2 0 .

I f y o u re y e sa r es h a r p ,y o u ' v e p r o b a b l yn o t i c e d

t h a tt h eG O T O5 5 2 0h e r ei s u n n e c e s s a r y ;5 5 2 0i s

t h ev e r yn e x tl i n e .T h eG O T Ob r a n c hi s i n c l u d e d

h e r ea s a s a f e t yf e a t u r e , t oe n s u r et h a t a d d i t i o n a l

l i n e st h a ta r el a t e ra d d e dw o n ' tg u mu pt h ew a yt h i s

r o u t i n ew o r k s .

T e s t i n gf o rL e n g t h .I t ' sa l w a y sb e s tt o c a t c h

a ne r r o rb e f o r ei t h a p p e n s .L i n e5 5 2 0d o e sj u s tt h a t .

I f t h ec u r r e n te n t r y l e n g t h( L N )i s a b o u tt o e x c e e d

t h ea l l o w e dl e n g t ho f e n t r y( L E ) ,t h eB L E E Pi sp l a y e d ,a n dI N i s s e tt o z e r o .

C u s t o m i z i n ga S c r e e nT e s t .S o m e t i m e si t ' s

n o te n o u g hj u s tt o s c r e e n e n t r y l e n g t ho r t o t e s tf o r

n u m b e r sv e r s u sl e t t e r s .S o m e t i m e s y o un e e dt o

b l o c ke n t r yo f a l lb u ta s m a l lg r o u po f c h a r a c t e r s .

T a k ea b u d g e t p r o g r a m ,w h e r ea n e x p e n s e

m i g h t b el a b e l e dw i t ha o n e - l e t t e rc o d et o d e s i g n a t e

d a i l y ,w e e k l y , b i w e e k l yo r m o n t h l y .T h ep r o m p t o n

t h es c r e e nw o u l dl o o kl i k et h i s :

P L E A S EE N T E RC O D E( D / W / B / M ) :-

T h ep r o g r a mu s e rw o u l ds i m p l y t y p et h ec o d ea n d

p r e s sR E T U R N .

N a t u r a l l y ,y o uc o u l dt e s tt h eu s e r ' s i n p u ta f t e r

t h ef a c tt o d e t e r m i n ei f o n eo f t h e f o u rv a l i dc o d e s

h a d b e e ne n t e r e d .B u tw o u l d n ' ti t b e m o r ee l e g a n t

t o s i m p l yn o t a l l o w e n t r yo f i n c o r r e c tc h a r a c t e r st o

b e g i nw i t h ?E s p e c i a l l yi f y o uc o u l dd o i t w i t ho n l y

o n es h o r tl i n eo f c o d e ?

T h es e c r e tc o m m a n di s o u ro l d f r i e n dI N S T R .H e r e ' sw h a ts u c ha s c r e e n i n gl i n ew o u l dl o o kl i k e

a t l i n e5 5 1 0 :

5 5 1 0I F I N S T R ( " D W B M " , C H $ )< 1 T H E NP L A Y B L E E P $ : I N= 0

T h i sl i n es i m p l yl o o k sf o r o n eo f t h e f o u r

c h a r a c t e r s .I f o n eo f t h e f o u ri s n o ts t o r e di n C H $

t h el i n eb l e e p sa p r o t e s ta n ds e t sI N t o z e r o ,b a r

r i n g t h e c h a r a c t e rf r o me n t r yi n t oI N $ .

T o m a k et h el i n em o r eg e n e r i c ,s o i t c o u l db e

c h a n g e dd e p e n d i n go nt h e c h a r a c t e r sn e e d i n gt o b e

s c r e e n e d ,a s t r i n gv a r i a b l es h o u l db e s u b s t i t u t ef o r

D W B M :

5 5 1 0I F I N S T R ( A L L O W E D $ < 1

T H E NP L A YB L E E P $ : I N= 0

A n o t h e rv a r i a b l es h o u l db e i n c l u d e d ,s o t h a tt h e

l i n e w i l lb e e x e c u t e do n l yi f t h i sp a r t i c u l a rt y p eo f

t e s ti s d e s i r e d :

5 5 1 0I F A L L O WT H E NI F

I N S T R $ ( A L L O W E D $ , C< 1 T H E

P L A YB L E E P $ :I N

T h eA L L O Wf l a gs h o u l db e r e s e t t oz e r oa t t h e

e n do f e n t r y ,p r e f e r a b l yj u s tb e f o r e t h eR E T U

s t a t e m e n tg o e sb a c kt o t h em a i np r o g r a m ,a s s h o w

b e l o w :

5 3 9 5A L L O W= 0 : R E T U R N

T h eI N S T Ro p e r a t i o n sw e ' v e b e e nd i s c u s s

a r ei n c r e d i b l yf a s t .I ' v ep l a c e dt h ee n t i r ea l p h a b

i n t oA L L O W E D $a n ds t i l lw i t n e s s e dn od e c l i n ei n

e n t r ys p e e d .O fc o u r s e ,i f y o uu s et h i sr o u t i n e ,d o n '

f o r g e tt o a c t i v a t ei t w i t hA L L O W ,a n dd o n ' tf o r

g e tj t op l a c et h ep r o p e rc h a r a c t e r si n A L L O W E

O t h e r w i s e ,t h e r o u t i n ew i l lb e s e a r c h i n gf o rb l a n k

a n dt h ep r o g r a mw o n ' ta l l o w e n t r yo f a n y t h i n a t

a l l .

T h eB a c k s p a c e a n dO t h e r R o u t i n e s

W e ' v et a k e nt h e s e *p a g e st o c o v e rt h e

p e r i p h e r a l r o u t i n e sb e c a u s et h e y ' r ea l ls h o r ta n d

s i m p l e .T h ec l e a r - t o - e n d - o f - f i e l dr o u t i n ea t l i n e5 8 0

i n F i g . 7 - 5d e t e r m i n e st h er e m a i n i n ge m p t ys p a c

i n a f i e l d( L E - L N ) ,a n dp r i n t st h a tn u m b e ro f

s p a c e s — o b l i t e r a t i n g a n y t h i n gt o t h er i g h to ft h e c u

s o r( t ot h eb o r d e ro f t h ee n t r y ,a n y h o w ) .

T h ec l e a r - b u f f e rr o u t i n ea t 5 9 0 5u s e sa n e i g h

9 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 101/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 102/194

5 7 1 0 r e m d i s l a yt i m e5 7 2 0c h a r , 0 , 2 4 , l e f t $ ( t i $ , 2 ) + " : M + m i d $ ( t i $ > 3 , 2 ) + M :

5 7 3 0 r e t u r n

F i g . 7 - 6 .D i s p l a y i n gt h et i m e .

F i g .7 - 6 .T h i sr o u t i n et a k e sf u l la d v a n t a g eo f t h e

O 1 2 8 ' sb u i l t - i nc l o c k .W i t ha l i t t l eh e l pf r o mB AS I Cs t r i n gh a n d l i n gc o m m a n d s ,l i n e5 7 2 0p l a c e st h e

c u r r e n tt i m eo nt h el o w e rr i g h tc o r n e ro ft h es c r e e n .

T h ev i s u a le f f e c ti s o fa c l o c kt i c k i n ga w a yt h e

s e c o n d s , w h i l e t h ec o m p u t e ra w a i t sk e y b o a r de n

t r y .I t ' s a l ls o f a s tt h a te v e nw h e ny o u ' r et y p i n ga t

f u l ls p e e d , t h et i m ep i e c er e m a i n su n d i s t u r b e d

. . . a s s t e a d ya s B i gB e n .

S e t t i n gt h eT i m e .T ou s et h i sr o u t i n ep r o p

e r l y ,y o us h o u l ds e tt h et i m ea t t h eb e g i n n i n go f

y o u r p r o g r a m .T h et i m ev a r i a b l eT I $i s s e t i nt h e

f o l l o w i n gm a n n e r ,a c c o r d i n gt o a 2 4 - h o u rc l o c k :

8 : 3 0 : 0 0a m

1 2 : 0 0 : 2 0p m

2 : 4 0 : 1 0p m

T I $= " 0 8 3 0 0 0 "

T I $= " 1 2 0 0 2 0 "

T I $ =" 0 2 4 0 1 0 "

T h el e a d i n gz e r o e si n t h e s e e q u a t i o n s a r em a n d a

t o r y .I f y o ud o n ' t i n c l u d ea l ls i xd i g i t s i nt h et i m e

c o m m a n d ,t h ec o m p u t e rw i l ls l a py o uw i t ha n e l e c

t r o n i ct r a f f i ct i c k e t :" I L L E G A LQ U A N T I T Y

E R R O R . "

W h i s t l eW h i l eY o uW a i t

O fc o u r s e , d i s p l a y i n g t h et i m ei s n ' tt h eo n l yo p

t i o n .W h a ti f y o uc o u l dp l a ym u s i c ?F i g u r e7 - 7

s h o w sa r o u t i n e t h a tp l a y sa n o t ef r o ma M U S I C $

v a r i a b l ee a c ht i m ei t i s c a l l e d .S i m p l yc h a n g et h e

G O S U B5 7 0 0t o G O S U B5 6 0 0a t l i n e5 1 4 0 ,a n d

y o u ' l lh a v ea r e a d y - m a d er e p r i s e o fS h a v e a n da

H a i r c u t ,a d n a u s e u m .T h em u s i cv a r i a b l em u s tb e

s e tu p i n t h e6 0 0 0 0 ' sl i k et h i s :

6 0 0 9 3M U S I C $= " W F R R . C R . C R . D R .

C R R R . E R R F R R R R " : L M T = L E N

( M U S I C $ )

A g a i n ,d o n ' tw o r r yi f y o u ' r e n o t y e tu p o n t h e

P L A Yc o m m a n d .I t ' sc o v e r e di n a n o t h e rc h a p t e r

T h ep o i n to f t h e s ee x e r c i s e si s t h a ty o uc a nd o a l

m o s t a n ys i m p l e t a s k w h i l et h ec o m p u t e ri s a w a i t

i n gk e y b o a r de n t r y .

C O M P L E T EL I S T I N G S

B e c a u s et h e e n t r yr o u t i n e sd i s c u s s e di n t h i

c h a p t e ri n c l u d es om a n ys c a t t e r e d s u b r o u t i t h e y

a r er e p r o d u c e di n f u l la t t h ee n do f t h i sc h a p t e r

i n F i g s .7 - 8a n d7 - 9 .I f y o u ' v eh a dt r o u b l e f o l

i n ga n yp o r t i o no f t h e s er o u t i n e s ,y o uc a n p r o b

b l yg e ta b e t t e ru n d e r s t a n d i n go f t h e mn o wb y

e x a m i n i n gt h e s el i s t i n g s .

B o t ho f t h e s er o u t i n e si n c l u d ed u m m ym a i n

p r o c e s s i n gr o u t i n e sw h i c he n a b l et h e mt o b e e a s

i l yt e s t e d .

U S I N GT H EE N T R YR O U T I N E S W I T H A R R A Y

T h ee n t r yr o u t i n e sd i s c u s s e di n t h e c h a p t e r

v e r s a t i l ee n o u g ht o b e u s e di n a l m o s ta n yt y p eo f

p r o g r a m y o ud e s i g n .O f t e ny o u ' l lw a n tt o u s et h i s

r o u t i n ei n c o n j u n c t i o nw i t ha r r a y s .I t ' sj u s ta m a t

t e r o f s e t t i n gu p t h ep r o p e rb e g i n n i n gv a r i a b l e

c a l l i n gt h e e n t r yr o u t i n e ,a n df i n a l l y ,a s s i g n i

I N $ — t h ev a r i a b l er e t u r n e df r o mt h e e n t r yr o u t i n e

5 6 0 0 c t r = c t r + 1

5 6 0 5 t e m p o1 0 05 6 1 0 p l a yM o 2 M + m i d $ ( m u s i c $

5 6 2 0i f c t r = l m tt h e n c t r = 0

5 6 2 5 t e m p o2 5 5

5 6 3 0 r e t u r n

F i g . 7 - 7 .P l a y i n g w h i l ew a i t i n g .

9 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 103/194

5 r e m n m b r = - 1

1 0 h = 0

2 0 v = 1 0

2 5 l e = 1 5

3 0 s c n c l r

3 5 c h a r . h , v f " x x x x x x x x x x x x x x x x x x "

4 0 g o s u b 6 0 0 0 0

5 0 g o s u b 5 0 0 0

6 0 r e m p r i n t i n $

6 5 r e m i f e s c t h e np r i n t" e s c a p ew a s p r e s s e d * 1

7 0 e n d

5 0 0 0: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :

5 0 2 0 r e m i n p u t r o u t i n e

5 0 4 0:

5 0 5 0t e m p o 2 5 5

5 0 6 0 g o s u b 5 9 0 0 : r e m c l rb u f f e r

5 0 8 0i n $ = l l l f : c h $ = H l f : i n = 0 : d u n = 0

5 0 8 5

5 0 9 0i f l e > 2 4 0t h e nl e = 2 4 0

5 1 0 0

5 1 2 0 d o u n t i l d u n

c h a r , h , v , c a s e $ + i n $ + c s r $ + r e s e t $

g e t k e yc h $

c h = a s c ( c h $ )

g o s u b5 4 0 0 : r e m t e s tc h

i f d u n t h e ne x i t

5 1 3 0

5 1 4 0

5 1 6 0

5 1 8 0

5 1 9 0

5 2 0 0

5 2 1 0

5 3 0 0

i f b k s t h e nb k s = 0 : g o s u b5 9 5 0

i f i n t h e ni n $ = i n $ + c h $

5 3 9 0 l o o p

5 3 9 2: c h a r , h , v , c a s e $ + i n $ + "" + r e s e t $

5 3 9 5 r e t u r n

5 4 0 0

5 4 1 0d u n = 0 : i n = - 1 : b k s = 0 : e s c = 0

5 4 1 5

5 4 2 0 r e m t e s tc h a r a c t e r5 4 3 0 l n = l e n ( i n $ ) :r e m c u rl e n

5 4 4 0i f c h = 1 3t h e n d u n = - 1 : i n = 0 :g o s u b5 8 0 0 :r e m c r

5 4 6 0i f c h = 2 7 t h e nd u n = - 1 : i n = 0 : e s c = - 1 : r e me s c

5 4 8 0i f ( c h > 1 4 4a n d c h < 1 4 9 ) o r c h = 1 5 7 o r c h = 9 5 o r( c h < 3 2 ) t h e nb k s =- 1 : i n = 0 : g o t o 5 5 3 0

5 5 0 0i f c h > 9 6t h e nc h = c h - 1 2 8 : c h $ = c h r $ ( c h ) : g o t o5 5 2 0

5 5 0 5i f n m b r a n d ( c h > 5 7 o r c h < 4 5 )t h e np l a y b l e e p $: i n = 0 : g o t o5 5 3 05 5 2 0i f l n = > l e t h e n p l a y b l e e p $ : i n * 0

5 5 3 0 r e t u r n

5 8 0 5r e m c l e a rt o e n d o f f i e l d

F i g . 7 - 8 .A n e n t i r eI n p u tl i s t i n g .

9 3

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 104/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 105/194

5 1 8 0

5 1 9 0

5 2 0 0

5 2 1 0

5 3 0 0

5 3 9 0 l o o p

5 3 9 2

g o s u b5 4 0 0 : r e m t e s tc h

i f d u n t h e ne x i t

i f b k s t h e n b k s = 0 : g o s u b 5 9 5 0

i f i n t h e n i n $ = i n $ + c h $

c h a r l h , v , c a s e $ + i n $ + f fn + r e s e t $

5 3 9 5 r e t u r n

5 4 0 0

5 4 1 0d u n = 0 : i n = - 1 : b k s = 0 : e s c = 0

5 4 1 5

5 4 2 0 r e m t e s t c h a r a c t e r

5 4 3 0 l n = l e n ( i n $ ) :r e mc u r l e n

5 4 4 0i f c h = 1 3t h e n d u n = - 1 : i n = 0 :g o s u b5 8 0 0 :r e mc r

5 4 6 0i f c h = 2 7t h e nd u n = - 1 : i n = 0 : e s c = - 1 : r e me s c

5 4 8 0i f ( c h > 1 4 4 a n d c h < 1 4 9 ) o r c h » 1 5 7 o r c h = 9 5 o r( c h < 3 2 ) t h e n

b k s = - 1 : i n = 0 : g o t o5 5 3 0

5 5 0 0i f c h > 9 6 t h e n c h = c h - 1 2 8 : c h $ = c h r $ ( c h ) : g o t o 5 5 2

5 5 0 5i f n m b ra n d ( c h > 5 7 o r c h < 4 5 ) t h e n p l a y b l e e p $ : i n

5 5 2 0i f l n = > l et h e n p l a y b l e e p $ : i n = 0

5 5 3 0 r e t u r n

5 6 0 0 c t r * c t r + 1

5 6 0 5t e m p o1 0 0

5 6 1 0p l a yw o 2 t f + m i d $ ( m u s i c $ , c t r > 1 )

5 6 2 0i f c t r = l m tt h e nc t r = 0

5 6 2 5t e m p o 2 5 5

5 6 3 0 r e t u r n

5 7 0 0: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :

5 7 1 0 r e m d i s l a y t i m e

5 7 2 0c h a r , 0 , 2 4 , l e f t $ ( t i $ , 2 ) + " : " + m i d $ ( t i $ , 3 , 2 ) + " : "5 7 3 0 r e t u r n

5 8 0 0: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :

5 8 0 5r e m c l e a rt o e n d o f f i e l d

5 8 1 0: c h a r , h , v , c a s e $ + i n $ + l e f t $ ( s p a c e $ , l e - l n ) + r e 5 8 2 0:

5 8 3 0r e t u r n

5 9 0 0:

5 9 0 5 r e m c l e a rb u f f e r

5 9 1 0f o r z z = 1t o 8 : g e t c h $ : n e x t

5 9 2 0 r e t u r n

5 9 5 0:

5 9 6 0: r e m b a c k s p a c er t n

5 9 7 0i f l n = 0t h e nt e m p o2 5 5 :p l a y " sv 1 o 5c v 2 o 6c v 3 o 4 c N : g o t o5 9 9 55 9 8 0i n $ = l e f t $ ( i n $ , l n - 1 )5 9 9 5 r e t u r n

6 0 0 0 0:

6 0 0 9 3 m u s i c $ s " w f r r . c r . c r . d r . c r r r . e r r f r r r r " : l m t = l 6 0 0 9 4 b l e e p $ = M s v 1 o 5g v 2 o 6g v 3 0 4g N

9 5

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 106/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 107/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 108/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 109/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 110/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 111/194

8 8 3 0 f o rr e c = 1t o l a s t

8 8 3 5i f r e c = f o lo r r e c = l e a dt h e nr v = 1 : e l s er v = 0

8 8 3 6i f r e c s l e a dt h e np l a yn q v 1 c v 2 e N

8 8 3 7i f r e c = f o l t h e np l a y H q v 1 ev 2 g w

8 8 4 0: c h a r , 0 , r e c , l e f t $ ( a $ ( r e c ) + s p a c e $ , 1 5 ) , r v

8 8 5 0n e x t

8 8 6 0r e t u r n6 0 0 0 0 r e m : l o a du p l i s t :

6 0 0 0 5s p a c e $ = " n

6 0 0 1 0a $ ( 1 ) = " c h i l id o g "

6 0 0 2 0a $ ( 2 ) = " h a m b u r g e r "

6 0 0 4 0a $ ( 3 ) = M p i z z a "

6 0 0 6 0 a $ ( 4 ) = w p a e l l a "

6 0 0 8 0a $ ( 5 ) = M f i l e tm i g n o n "

6 0 0 8 2a $ ( 6 ^ " s u b m a r i n e "6 0 0 8 4 a $ ( 7 ) = " d o gf o o d "

6 0 0 8 6a $ ( 8 ) = " s c r a m b l e de g g s ' *

6 0 0 8 8 a $ ( 9 ) = " e g g sb e n e d i c t "

6 0 0 8 9a $ ( 1 0 ) = " c o u n t r ys a l a d "

6 0 0 9 0l a s t = 1 0 :r e m t h e r e a r e1 0 i t e m s

6 0 1 0 0r e t u r n

s w a p p e d .W h e nt h ep r o g r a mg e t st o t h e e n d ,a l l

i t e m sw i l lb e i n o r d e r .

B o t ho f t h e s es o r t sa r e e a s yt o u n d e r s t a n d .

M o r ec o m p l i c a t e ds o r t s ,s u c ha s q u i c k s o r t ,s h o w n

i n F i g .8 : 3 ,a r eh a r d e rt o f o l l o w ,b u ta r em u c hm o r e

e f f i c i e n tb e c a u s e t h e yp e r f o r m f e w e rs w a p st o g e t

t h ed a t ai n o r d e r .

T h eq u i c k s o r t r o u t i n ec a nb e u s e di n p l a c eo f

t h es l o w e rb u b b l es o r tr o u t i n es h o w ni n F i g .8 - 1 .

S O R T I N G T W O - D I M E N S I O N A LA R R A Y S

T h eo n l yt h i n gt h a tc h a n g e si n t h es o r to fa b i -

d i m e n s i o n a la r r a yi s t h et e s ta n dt h es w a p .L e t ' s

s a y y o u w a n t e dt o s o r t a n a r r a yI T E M $ -

( R E C , F I E L D )t h a t ' sb r o k e nd o w nl i k et h i s :

I T E M $ ( R E C , 1 )

I T E M $ ( R E C , 2 )

I T E M $ ( R E C , 3 )

r e f e r s t ol a s tn a m e

r e f e r st o c i t y

r e f e r s t o z i pc o d e

A f t e rd e c i d i n gw h i c hf i e l dw o u l dd r i v et h es o r t

( d oy o u w a n tt o s o r to nl a s tn a m e ?c i t y ?z i p c o d e ? ) ,

y o uw o u l ds i m p l yh a r dc o d et h i sn u m b e ri n t ot h e

t e s t ,l i k es o :

8 0 4 0 : I F I T E M $ ( L E A D , 2 )-

> I T E M $ ( F O L , 2 )T H E NB E G I N

L E A Da n dF O Ls t i l lr e f e rt o r e c o r d si n t h ea r r a y

T h eo n l yt h i n gt h a t ' sc h a n g e ds o f a ri s t h a ty o u ' v

a d d e da n o t h e r d i m e n s i o n .Y o u ' r es o r t i n go n t h e

v a l u ei n t h es e c o n df i e l d( c i t y )o f e a c hr e c o r d .

T h en e x ts t e pi s t o f i xt h es w a pr o u t i n e .I f t h e r e

a r en o wt h r e ee l e m e n t si n e a c hr e c o r d ,e a c hm u s t

b e s w a p p e d :

F O R C T R = 1 T O 3T $= I T E M $ ( L E A D , C T R )

I T E M $ ( L E A D , C T R ) = I T I T E M $ ( F O L , C T R )= T $

N E X T

I f t h e r ei s a s u b s t a n t i a ln u m b e ro ff i e l d s i ne a c h

1 0 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 112/194

0 : g o t o1 0

2 : q u i c k s o r t p r o g r a m

3 :

1 0 d i m i t e m $ ( 5 0 ) v s t a c k ( 2 0 , 2 )

1 5 t r u e = 1 : f a l s e = 0

1 7 :

2 0 s c n c l r02 2 l i m i t = 1 0: r e ms c r e e nd i s p l a yl i m i t

2 5 i t e m = 1 :r e m s e t f i r s t i t e m

2 6 :

3 0 g o s u b 4 0 0 0: r e m g e t e n t r y

4 0 g o s u b8 0 0 0 : r e m s o r t

5 0 g o s u b 2 0 0 0: r e m d i s p l a y

5 5 :

6 0 :

7 0 e n d

8 0 :

2 0 0 0: r e md i s p l a y2 0 1 0:

2 0 1 5 s c n c l r

2 0 2 0f o r i = 1 t o i t e m

2 0 2 5:

2 0 3 0: p r i n t i t e m $ ( i )

2 0 4 0: c t r = c t r + 1

2 0 4 5:

2 0 5 0: i f c t r = l i m i tt h e nb e g i n

2 0 6 0: c t r » 1

2 0 6 5: c h a r , 1 0 , 2 3 , " p r e s sa k e y 1 *

2 0 7 0: g e t k e ya $2 0 7 5: s c n c l r

2 0 8 0: b e n d

2 0 8 5:

2 0 9 0n e x t

2 1 0 0 r e t u r n

4 0 0 0: r e m i n p u tr o u t i n e

4 0 1 0:

4 0 1 5p r i n f e n t e ri t e m s ,r e t u r nw h e nd o n e . 1 *

4 0 1 6p r i n t

4 0 2 0d o

4 0 3 0s p r i n t" i t e m# : N ; i t e m ;4 0 4 0: i n p u t i t e m $ ( i t e m )4 0 5 0: i f i t e m « 5 0 o ri t e m $ ( i t e r n ) ■ " "t h e ni t e m * i t e m - 1 : e x i t

4 0 6 0: i t e m x i t e m + 1

4 0 7 0 l o o p

4 0 7 5:

4 0 8 0r e t u r n

F i g . 8 - 3 .

1 0 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 113/194

4 0 9 0

8 0 0 0

8 0 0 5

8 0 0 6

8 0 0 7

8 0 1 0

8 0 2 0

8 0 3 0

8 0 4 0

8 0 5 0

8 0 6 0

8 0 7 0

8 0 8 0

8 0 9 0

8 1 0 0

8 1 1 0

8 1 2 0

8 1 3 0

8 1 4 0

8 1 5 0

8 1 6 0

8 1 7 0

8 1 8 0

8 1 9 0

8 2 0 0

8 2 1 0

8 2 2 0

8 2 3 0

8 2 4 0

8 2 5 0

8 2 6 0

8 2 7 0

8 2 8 0

: r e ms o r t r o u t i n e

s c n c l r : c h a r, 1 5 , 1 0 , " n o w s o r t i n g "

p n t r = 1 : s t a c k ( 1 , 1 ) = 1 : s t a c k ( 1 , 2 ) = i t e m

d o u n t i lp n t r = 0

s i = s t a c k ( p n t r , 1 )

s j = s t a c k ( p n t r , 2 )

p n t r = p n t r - 1

d o w h i l es i < s j

l e a d = s i

f o l = s j

t e s t = t r u e

d o w h i l el e a d < f o l

i f i t e m $ ( l e a d ) > i t e m $ ( f o l )t h e n b e g i n

: t $ = i t e m $ ( l e a d )

: i t e m $ ( l e a d ) = i t e m $ ( f o l )

: i t e m $ ( f o l ) = t $

: t e s t s - t e s t

b e n d

i f t e s t = t r u e t h e n l e a d = l e a d + 1 : e l s e f o l = f o l - 1

l o o p

i f l e a d + K s jt h e n b e g i n

: p n t r = p n t r + 1

: s t a c k ( p n t r , 1 ) = l e a d + 1

: s t a c k ( p n t r , 2 ) = s j

b e n d

s j = l e a d - 1

l o o p

l o o p

r e t u r n

e a c hr e c o r d ,t h i sk i n do f s w a p p i n gc a nr e a l l ys l o w

d o w na s o r t ,s oa l o to fp r o g r a m sc o n c e n t r a t eo n l y

o nt h ef i e l db e i n gs o r t e d ,b ya s s i g n i n gi t i n t oi t so w n

s i n g l e - d i m e n s i o na r r a y ,a n ds e t t i n gu p a r e c o r dp o i n t e rt h a ti s s o r t e di n p a r a l l e l .A s s u m i n gt h e

s i n g l e - d i m e n s i o na r r a yw e r es e tu pa s A $ ( I T E M ) ,

t h et e s ta n ds w a pw o u l dl o o kl i k et h i s :

I f A $ ( L E A D ) => A $ ( F O L )T H E NB E G I NT $= A $ ( L E A D )

A $ ( L E A D )= A $ ( F O L )

A $ ( F O L )= T

T = R C ( L E A D )

R C ( L E A D )= R C ( F O L )R C ( F O L )= T

B E N D

T h eR Ca r r a yw o u l dt h e ns e r v ea s a s o r t e

p o i n t e r t o r e c o r d s .E v e nt h o u g ht h er e c o r dt h e m

s e l v e sa r e n o ti n s o r t e d o r d e r ,t h e yc a nb e p r i n t e

1 0 3

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 114/194

t h a tw a yb y r e f e r r i n g t ot h e mw i t hR C :

F O R1 = 1 T OI T E M

F O R J= 1 T O 3P R I N TI T E M $ ( R C ( I ) J )

N E X T

P R I N T

R E MR E C O R D S

R E MF I E L D S

R E MS P A C E

B E T W E E NR E C O R D S

N E X T

T h eo n l yr e a ld r a w b a c kt o t h i sa p p r o a c hi s t h a tt h e

r e c o r d sa r en e v e rs o r t e d .I f y o uw a n tt o w r i t et h e m

b a c kt o t h ed i s ki n s o r t e do r d e r ,y o uh a v et o i n c l u d e

t h eR Cv a r i a b l ei n y o u rw r i t e - t o - d i s kr o u t i n e .

S O R T I N GN U M B E R S

B A S I Cd o e sa f i n ej o bo f s o r t i n gn u m b e r st h a t

a r e s t o r e di n a n u m e r i ca r r a y .I t d o e ss p l e n d i d l y

w i t hw o r d so r p h r a s e sw i t h i na s t r i n g a r r a y .B u t

B A S I Cf a l l ss h o r tw h e ni t c o m e st o s o r t i n gn u m

b e r sw i t h i ns t r i n g s .T h er e a s o ni s t h a ty o u rc o m

p u t e rt r e a t sn u m b e r sw i t h i ns t r i n g sa s t h o u g ht h e y

s h o u l db e i n a l p h a b e t i c a lo r d e r :

1

1 0 0 0

2

2 2

3

T h i s a p p r o a c hm a k e sp e r f e c ts e n s et o t h ec o m

p u t e r ,b e c a u s ei t d e a l sw i t hs t r i n g sf r o mt h el e f t

t o r i g h t .S i n c eo n ec o m e sb e f o r e t w o ,w h i c hc o m e s

b e f o r et h r e e ,t h ec o m p u t e rp l a c e s1 , 1 0 0 0 ,2 , 2 2 ,

a n d3 i n t h a t o r d e r .

T h e r ea r ea c o u p l eo f w a y sa r o u n dt h i sp r o b

l e m .T h ef i r s ti s o b v i o u s :s t o r ea n ds o r ty o u rn u m

b e r si n a r r a y s .I f y o uc a n ' td ot h a t ,y o uc a nl e f tp a d

t h en u m b e rw i t hz e r o e s d u r i n g e n t r y( 0 0 0 1 ,0 0 0 2 ,

e t c ) ,o r l e f tp a ds t r i n g s t h a ts t a r tw i t hn u m b e r sa f

t e r e n t r y :

I F V A L ( A $ ( I T E M ) > 0T H E NB E G I N

L = L E N ( A $ ( I T E M ) )

F O R Z Z= l T O 1 0 - LA $ ( I T E M= " " + A $ ( I T E M )

N E X T

B E N D

T h er o u t i n ea b o v ea s s u m e st h a tt h e m a x i m l e n g t ho f t h ef i e l di s 1 0 .Y o uc a nc h a n g et h ev a l u

1 0 t o w h a t e v e rn u m b e ri s a p p r o p r i a t e .

A F I N A LN O T EO NS O R T S

T r yn o tt o l o o ke x c l u s i v e l ya t s o r t sa s r o u t i n

t o a l p h a b e t i z ep h o n el i s t so r a l b u mc o l l e c t i o

T h e y ' r er e a l l ym u c hm o r e .W i t hs o r t s ,y o uc a n

g r o u pi n f o r m a t i o ni n a v a r i e t y o fw a y s .F o re x a m

p l e ,a s i m p l e s u b r o u t i n ec a nb e a d d e dt o a d i s p l

r o u t i n et h a tw i l lp r o v i d ec o u n t sa n ds u b t o t a l so n

r e l a t e d r e c o r d s :

A L A B A M AM O N T .A L A B A M AS E L M A

T O T A LF O RA L A B A M A :

F L O R I D A J A X

F L O R I D AT A M P A

$ 1 0 0

$ 5 0

$ 1 5 0

$ 9 0

$ 4 0

D u r i n gt h ep r i n tr o u t i n e ,t h ep r o g r a ms i m p l

r e m e m b e r st h el a s ti t e ma n dc o m p a r e st h ec u r r e n

o n et o i t . I f t h e r e ' sa m a t c ht h ep r o g r a mt a l l i e su p

a n e wd o l l a ra m o u n t .I f t h e r ei s n om a t c h ,t h ep r o

g r a mp r i n t sa t o t a la n dm o v e so n .

A n o t h e ra p p l i c a t i o n o fs o r t si s t h e b i n a r

s e a r c h ,w h i c hw et o u c h e do n i n C h a p t e r4 . I f y o u

d a t ai s k e p ts o r t e d ,y o uc a nu s e t h e s eb i n a r

s e a r c h e sa n dg r e a t l ys p e e da c c e s st o y o u ri n f o r

m a t i o n .

F i n a l l y ,s o r t sc a nh e l py o ug r o u pr e l a t e di t e m

t o g e t h e r . S o r t i n gr e c o r d sb yd i f f e r e n tf i e l d sl e t sy o u

l o o ka t y o u rd a t ai n n e ww a y s ,a n do f t e n p o i n to u t

p a t t e r n sy o un e v e rw o u l d h a v eo t h e r w i s en o t i c e

T h es o r tr o u t i n e si n t h i sc h a p t e r s h o u l db e c o m

p a r t o fy o u rp e r m a n e n tp r o g r a ml i b r a r y .

1 0 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 115/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 116/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 117/194

T h e r ea r es e v e r a lw a y so f l o c a t i n gi t e m so nt h e

s c r e e ni n C o m m o d o r e1 2 8B A S I C .B yf a rt h ee a s i

e s t t ou n d e r s t a n da n d m o s tv e r s a t i l e ,h o w e v e r ,i s

t h eC H A Rc o m m a n d .I t ' ss h o r tf o rc h a r a c t e r ,b u t

i t ' sp r o n o u n c e d " c h a r "a s i n c h a r c o a l .T h eC H A Rc o m m a n dd o e st h ef o l l o w i n g :

• D e f i n e so n w h i c hg r a p h i c s c r e e n t h e c h a r a c t e r

w i l lb e d r a w n .

• D e f i n e sc o l u m n a n dr o wc o o r d i n a t e s .

• P r i n t st h e t e x ta n d c h a r a c t e r g r a p h i c sy o u

s p e c i f y .

• D e t e r m i n e sw h e t h e rt h ec h a r a c t e r sw i l lb e d i s

p l a y e di n r e v e r s e o rn o r m a lf o r m .

T h ef o r m a to f t h eC H A Rc o m m a n di s a l i t t l e

d i f f i c u l tt o f o l l o wa t f i r s t ,b u ty o u ' l ls o o nb e c o m

f o r t a b l ew i t hi t .

C H A R, 1 0 , 2 0 , " H E L L O " , 1

T h ef i r s tc o m m aa f t e rC H A Rt e l l st h ec o m p u t e rt o

u s et h ed e f a u l t( s k i p ,i f y o uw i l l )o p t i o n a lc o l o r s o u r c e .

N e x t c o m e st h eg r a p h i cs c r e e n ,t h ec o l u m ny o u

w i s ht o p r i n ta t , a n dt h er o wy o uw i s hi t t o s t a r t

a t , f o l l o w e db y t h es t r i n gy o uw i s ht o a p p e a r ,a n d

w h e t h e rr e v e r s ei s o n o r o f f .F i g u r e9 - 3s h o w ss o m e

e x a m p l e so f t h eC H A Rc o m m a n d .

C H A Rh a ss e v e r a la d v a n t a g e so v e rt h eP R I N T

c o m m a n d :

• I t c a nb e u s e do na l ls c r e e n s ,i n c l u d i n gg r a p h i c

a n d8 0 - c o l u m nm o d e s .

• I n t h eg r a p h i c sa n d8 0 - c o l u m n m o d e s ,i t e n a b l e

y o ut o p r i n t t ot h ev e r yl a s tc o l u m no n a l i n e ,

w i t h o u ta d v a n c i n gt o t h en e x tl i n e .

• I t l e t sy o ur e v e r s e c h a r a c t e r sw i t h o u tw o r r y i n

a b o u tt h es o m e t i m e st r o u b l e s o m eR V Sc h a r a c

t e r t h a ti s r e q u i r e di f y o u ' r e u s i n g t h eP R I N

c o m m a n d .

C H A Rc a nd i s p l a ya n yt e x t t h a tc a nb e p l a c e

o n t h es c r e e n u s i n gP R I N T .I n s o m ec a s e s ,h o w

e v e r ,a f e wt r i c k sa r er e q u i r e d .C H A Rc a n n o tb e

u s e dw i t ht h e p r i n t e r — i ta l w a y s g o e sd i r e c t l yt o t h e

s c r e e n .

A n o t h e rr u l e t ok e e pi n m i n di s t h a tC H A Ra l

w a y su s e ss t r i n g s( e i t h e rv a r i a b l e sl i k eA $o r l i t e r a l

l i k e" H E L L O " ) .A n u m b e ru s e dw i t h i nC H A m u s tt h e r e f o r eb e e x p r e s s e da s a s t r i n g :

C H A R, 5 , 8 / 1 0 "

T h ea b o v ee x p r e s s i o nw o u l dp r i n tt h en u m b e

1 0a t c o l u m nf i v e ,r o we i g h t o f t h es c r e e n .T h e1 0

i s e n c l o s e di n q u o t e sb e c a u s ei t i s b e i n gt r e a t e da s

a s t r i n g .I f y o uw i s ht o d i s p l a ya n u m e r i cv a r i a b l

u s i n gC H A R ,t h a t v a r i a b l em u s tb e c o n v e r t e di n t o

a s t r i n gu s i n g t h eS T R $f u n c t i o n .

H e r ea r e s e v e r a le x a m p l e s ,a l lo f w h i c hp r i n t

t h en u m b e rs e v e n :

C H A R , 1 0 , 1 , "H E L L OW O R L D "

( d i s p l a y sH E L L OW O R L Da t c o l u m n

C H A R . 1 0 , 1 , " H E L L OW O R L D ' M

( d i s p l a y sH E L L OW O R L Da t c o l u m n

A $ = § ' H E L L O " :B $ = " W O R L D " :

( d i s p l a y sH E L L OW O R L Da t c o l u m n

1 0 ,

1 0 ,

r o w

r o w

1 )

1 — i nr e v e r s e )

C H A R , 1 0 , 1 , A $ + "" + B

1 0 , r o w1 , u s i n gs t r i n gv a r i a b l e s )

F i g . 9 - 3 .D i f f e r e n te x a m p l e so f t h eC H A Rc o m m a n d .

1 0 7

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 118/194

1 0 C H A R, 0 , 1 0 / 7 ": R E MN U M B E RI N

Q U O T E S

1 5 :

2 0 N U M B E R = 3+ 4

3 0 C H A R, 0 , 1 0 , S T R $ ( N U M B E R ): R E M

U S I N GT H ES T R $C O M M A N D3 5 :

4 0 C H A R, 0 , 1 0 , C H R $ ( 5 5 ): R E MD I R E C T

A S C I IC O D EF O R" 7 "

5 0 :

6 0 E N D

A l lt h er u l e st h a ta p p l yt o s t r i n g sa n ds t r i n g

v a r i a b l e s a l s oa p p l yt o t h eC H A Rc o m m a n d .F o r

e x a m p l e :

C H A R, 1 0 , 1 5 , L E F T $ ( " H E L L OT H E R E " , 5 )

w o u l dp r i n tH E L L Oa t c o l u m n1 0 ,r o w1 5 o f t h e

s c r e e n .H , E , L , L ,a n dO a r e t h el e f t m o s tf i v ec h a r a c

t e r so f t h a ts t r i n g .Y o uc a ne v e na d ds t r i n g s t o

g e t h e r w i t h i nt h eC H A Rc o m m a n d ,s u c ha s i n t h i s

e x a m p l e ,w h i c hp r i n t st h ew o r d s" G O O DM O R NI N GR O B E R T " :

1 0 A $= " M O R N I N G " : N A M E $=

" R O B E R T "

2 0 C H A R9 0 , 1 0 9 " G O O D" + A $+ N A M E

I n f a c t ,a n yo f t h ep a r a m e t e r si n t h eC H A Rc o m

m a n dc a nb e s p e c i f i e da s v a r i a b l e s .T h i sm e a n s

e v e r - c h a n g i n gv a r i a b l e sa n ds w i t c h e si n y o u rp r o

g r a mc a nd e t e r m i n e :

• W h a tt e x tw i l lb e p r i n t e d( a s i n t h ep r e v i o u s

e x a m p l e ) .

• W h e r et h et e x tw i l la p p e a ro n t h es c r e e n .

• W h e t h e rt e x tw i l la p p e a ri n i n v e r s eo r n o r m a l ,

e t c .

U s i n gV a r i a b l e s w i t hC H A R

L e t ' ss a yy o u w a n tt o p u ta n i t e mh e a d i n go n

t h e s c r e e nf o rF I R S TN A M Ea n da c c e p te n t r yo f

t h en a m eu n d e r n e a t ht h i si t e mh e a d i n g .T h eC H A c o m m a n df i t st h i sa p p l i c a t i o np e r f e c t l y .J u s tt o k e e p

t h i n g si n t e r e s t i n g ,w e ' l la s s i g nv a r i a b l e sf o re v e r

o p e r a t i o n .

R « 0

C = 5

H E A D $= " F I R S TN A M E "R V S F L A G= 1

T h ec o m m a n dw o u l dl o o kl i k et h i s :C H A , C , R , H E A D $ , R V S F L A G .Y o uc a ne a s i l ys e eh o w

t h ev a r i a b l e sm i g h tb e c h a n g e df o rd i s p l a y o fd i f f e r

e n t i n f o r m a t i o na t d i f f e r e n tp l a c e s .F i g u r e9 - 4

s h o w st h i sa p p r o a c ht o t h eu s eo f C H A R .A s w e w i l ls e ei n a f e wp a g e s ,t h ea b i l i t yo f

C H A Rt o u s ev a r i a b l e si n t h i sm a n n e rg i v e sy o u

a g r e a td e a lo f f l e x i b i l i t y .

U s i n gC H A Rf o rO t h e rR e a s o n s

I n t h e4 0o r 8 0 c o l u m nt e x tm o d e s ,C H A Rc a n

p e r f o r ma n o t h e r i m p o r t a n tf u n c t i o n :i t c a np o s i t i o

t h e c u r s o r b e f o r eP R I N To r I N P U Ta r e u s e d .Y o u

c a na c t u a l l yu s et h eC H A Rc o m m a n da n dP R I N

t o g e t h e r ,a n dt h e r e a r et i m e sw h e ny o u ' l lw a n tt o .

C e r t a i np r o g r a m sm a ya l r e a d yb ew r i t t e na n dw o r k

i n gp e r f e c t l yi n t h e4 0 -o r 8 0 - c o l u m nt e x tm o d eu s

i n g t h eP R I N Tc o m m a n d .T h e r e ' sn o n e e dt o

c o n v e r ta l lP R I N Ts t a t e m e n t st o C H A Rs t a t e m e

w h e ny o us i m p l yw a n tt o r e l o c a t et h et e x t .I t sa b i l

i t yt o p o s i t i o nt h e c u r s o rf o ra P R I N Ts t a t e m e

i s o n eo f t h eu n s u n gf e a t u r e s o f t h eC H A Rc o m

m a n d ;i t w i l lc o m ei n q u i t eh a n d yw h e ny o uw a n t

t o l o c a t et e x t .

C o n t i n u i n g w i t ht h e" h e a d i n g "c o m m a n dj u s t

1 0 : c x 0

2 0 : r e m c o l

5 0 :

4 0 c h a r , c , r , t e x t $ , r v s

r = 1 0 :t e x t $ « N h e l l ow o r l d " :r v s * 1

r o w t e x t r e v e r s eo n / o f f

F i g . 9 - 4 .A r o u t i n eu s i n gC H A R .

1 0 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 119/194

e x p l a i n e d ,l e t ' ss a yy o u w a n tt o a c c e p ti n p u tf r o m

t h e k e y b o a r d o n el i n eb e l o wt h e h e a d i n gj u s t

p r i n t e d .Y o uc a nd os ob ys i m p l y u s i n gC H A Rw i t h

a ne m p t ys e to f q u o t e s , t op o s i t i o nt h ec u r s o r .T h e n

s i m p l ya c c e p tt h ei n p u t .B e c a u s et h e c u r s o rh a s

b e e nr e p o s i t i o n e d ,i n p u to f t h en a m ew i l lb e g i na t

c o l u m nf i v e ,r o wo n ei n t h i se x a m p l e :

1 0 C H A R, 5 , 1 , ""

2 0 I N P U TN A M E $

N o t et h a tt h ec o l u m nr e m a i n st h es a m ea s i n t h e

p r e v i o u se x a m p l e ;o n l y t h el i n e n u m b e rh a s

c h a n g e d .I n f a c t ,w ec o u l dr e w r i t et h e s et w ol i n e s

t o t a k ea d v a n t a g eo ft h ev a r i a b l e s s e te a r l i e r i nt h e

p r o g r a m :

1 0 C H A R , C , R+ 1 , " "

2 0 I N P U TN A M E

T h es t a t e m e n t sa b o v ep o s i t i o nt h e c u r s o ro n el i n e

b e l o wt h eN A M Eh e a d i n g .

N o t ea l s o t h a tt h e d e f a u l t( n o r m a l )f o r mo f

C H A Ri s f o rn o r m a l , n o tr e v e r s e ,p r i n t ,s o i t i s n o t

n e c e s s a r yt o p l a c ea , 0 n e x tt o t h ee m p t yq u o t e s .

I n a d d i t i o nt o u s i n gt h i st r i c kw h e nr e w r i t i n g

e x i s t i n gp r o g r a m s ,y o um a yw a n tt o u s ei t i n r o u

t i n e si n w h i c hi n f o r m a t i o nw i l lb e s e n tt o a p r i n t e r .

T h ep r i n t e rw i l li g n o r et h eC H A R ,b u tw i l lp r i n t

t h ed a t at h a ti s p a r t o fP R I N T .T e x tt h a ti s p a r t

o fa C H A Rw i l la l w a y sa p p e a ro n t h es c r e e n .T e x t

t h a t ' sp a r t o fa P R I N Tm a yb e r e d i r e c t e dt o t h e

p r i n t e r ,a d i s kf i l e ,o r o t h e rd e v i c e s .

N o wt h a ty o u ' v es e e nh o wC H A Rc a nb e u s e d

t o p o s i t i o nc h a r a c t e r so n t h es c r e e ni n o n ee x a m

p l e ,l e t ' ss e eh o wa na r r a yo f v a r i a b l e sc o u l db e u s e d

t o p o s i t i o ns u c h h e a d i n gf o r f i v eo r s i x d i f f e r e n t

i t e m s .I f y o u ' r es t o r i n gy o u ri n f o r m a t i o ni n a n a r

r a y ,t h ec o l u m na n dr o wl o c a t i o n sc a nb e s t o r e di n

s e p a r a t eb u tr e l a t e d a r r a y s ,a s s h o w ni n l i n e s2 0

t h r o u g h4 0 i n F i g .9 - 5 .

H e r ea r et h e s t e p sr e q u i r e dt o u s et h e s eh e a d

i n g s p r o p e r l y :

• C a l la s u b r o u t i n et o " d r a w "a l lh e a d i n g sa t o n c e .

• C a l la s u b r o u t i n et o " d r a w "a l le n t r yu n d e r l i n e s .

• C a l la r o u t i n et o i n p u ti n d i v i d u a li t e m s .

T h er o u t i n et o l o c a t et h e s ei t e m sp r o p e r l yi s s h o w n

i n l i n e s2 4 0 0t h r o u g h2 4 4 0i n F i g .9 - 5 .

L e t ' se x a m i n eh o wt h ew h o l el i s t i n gw o r k s

F i r s t ,t h ed i m e n s i o n e dv a r i a b l eH E A D $ ( x )i s u s e d

t o s t o r eh e a d i n gn a m e s .T h ev a r i a b l eR ( x )i s u s e d

t o s t o r et h er o w ,a n dC ( x )i s u s e dt o r e p r e s e n t

c o l u m n .S o t h i ss t a t e m e n t :

H E A D $ ( 1 ) = " F I R S TN A M E " : Q 1 ) = O :

a s s i g n s" F I R S TN A M E "a s t h ef i r s ti t e mh e a d i n g

I t w i l la p p e a ri n c o l u m nz e r o( t h ef i r s tc o l u m n ) ,r o w

f i v e .A s t h e l o o pc o u n t e ri s b u m p e du p w a r dw i t h

e a c hs u c c e s s i v ep a s s ,t h ev a l u e si n H E A D $ ( x ) ,R ( x ) ,

a n dC ( x )w i l lc h a n g ea n dd i f f e r e n th e a d i n g sw i l lb e

d i s p l a y e da t v a r i o u sp l a c e so n t h es c r e e n .T h i si s t h em e t h o du s e db ym a n yp r o f e s s i o

f i l i n gs y s t e m si n o r d e rt o m a k ed a t ae n t r ye a s i e r .

N o wt h a ty o u ' v e s e e nh o wh e a d i n g sa n di n p u t

s t a t e m e n t sa r ep o s i t i o n e d ,w ec a nl o o ka t t h es u b

j e c to f i n s t a l l i n ga s e p a r a t er o u t i n ef o ra c c e p t i n

a n dp o s i t i o n i n gd a t ao n r e c o r d st o b e a d d e d .B a s i

c a l l y ,t h i sr o u t i n es h o u l dd o t h ef o l l o w i n g :

• B u m pu pt h ei t e mc o u n t e ro f r e c o r d si n m e m o r y

• C a l la s u b r o u t i n et h a t p o s i t i o n sa n da c c e p t si n

p u ta n dk e e p st r a c ko f w h i c hf i e l di s c u r r e n t .

• A l l o w y o ut o c h a n g et h er e c o r do n c ei t h a sb e e n

e n t e r e d .

T h i sl i s ti s a t a l lo r d e r ;i t r e a l l yc a l l sf o rt w o

s e p a r a t er o u t i n e s :o n et o a d dt o t h e r e c o r dc o u n t e

a n dt h e o t h e rt o d o a l lt h er e a lw o r k :s c r e e np o s i

t i o n i n g ,a c c e p t i n gi n p u t ,a n ds o o n .

W ec a nu s et h i sp o s i t i o na n di n p u tr o u t i n ei n

o t h e ra r e a s ,s u c ha s i n a s e a r c ha n dc h a n g er o u t i n e

t h a tc o u l de a s i l yb e a d d e dt o t h ep r o g r a ma t a l a t e r

t i m e .Y o uc a ne v e nr e d e s i g nt h es e a r c hr o u t i n ee x

p l a i n e di n C h a p t e r4 . T h i sm o d u l a ra p p r o a c ht o

s o f t w a r e ,w h e r er o u t i n e sc a n b em i x e da n d

m a t c h e d ,i s p a r ts t r u c t u r e dp r o g r a m m i n g .

O L D - F A S H I O N E DM E N U S

W e ' v ea l ls e e ns i m p l em e n ur o u t i n e s t h a ta l -

1 0 9

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 120/194

0 :

2 : r e m r o u t i n et o d i s p l a yh e a d i n g s

3 :

5 s c n c l r

1 0 n f * 4

2 0 h e a d $ ( 1 ) x «3 0 h e a d $ ( 2 ) x "

4 0 h e a d $ ( 3 ) = w

4 5 :

4 6 r e m a b o v e

5 0 :

6 0 g o s u b 2 4 0 0

7 0 :

7 5 c h a r ,0 , 2 2

8 0 e n d

2 4 0 0: r e m

2 4 0 5:2 4 1 0: f o r x « 1

f i r s tn a m e " : c ( 1 ) = 0 : r ( 1 ) = 1l a s t n a m ew : c ( 2 ) = 2 0 : r ( 2 ) = 1

a d d r e s s M : c ( 3 ) = 0 : r ( 3 ) = 4

l i n e s a s s i g n h e a d i n g s

: r e md i s p l a yh e a d i n g s

l o c a t e h e a d i n g sr t n

t o n f

2 4 2 0: c h a r , c ( x ) , r ( x ) , h e a d $ ( x ) , 1

2 4 3 0: n e x t

2 4 3 5:

2 4 4 0: r e t u r n

F i g . 9 - 5 .R o u t i n e st o s t o r ea r r a y s

a n dd i s p l a yh e a d i n g s .

l o wy o ut o s e l e c ta n u m b e ra n dp r e s sR E T U R N .

M o s to f t h e s em e n u sa r e s i m p l ei n s t r u c t u r e ,u s

i n ge i t h e ra n I F o r a n O NG O S U Bs t r u c t u r e .W h a t

e v e rp r o g r a m m i n ga p p r o a c ht a k e n ,t h e m e n u sg e n e r a l l yl o o kl i k et h eo n ei n F i g .9 - 6 .T h e ya l l o w

t h eu s e rt o s e l e c tf r o ma n u m b e ro f i t e m sa n dt h e n

b r a n c ht o t h ea p p r o p r i a t es e c t i o n o f t h ep r o g r a m .

F i g u r e s9 - 7a n d9 - 8 s h o wh o wI F a n dO NG O S U Bm e n u sa r ep r o g r a m m e d .

O N. . . G O S U B

I f y o u ' r e n o tf a m i l i a rw i t hi t ,t h eO NA G O S U B

f u n c t i o no f C o m m o d o r e1 2 8B A S I Cl e t st h ep r o

g r a mb r a n c ht o a s p e c i f i cs u b r o u t i n eb a s e do n t h e

v a l u eo f A . I f A = 1 , t h ec o m p u t e rg o e st o t h ef i r s t

s u b r o u t i n ei n t h el i s t .I f A =3 , t h ec o m p u t e rg o e s

t o t h et h i r ds u b r o u t i n el i s t e d ,a n ds o f o r t h .I f A i s

g r e a t e rt h a nt h en u m b e ro f l i n e sl i s t e d ,t h ec o m

p u t e rp r o c e e d st o t h en e x ts t a t e m e n t .I n t h eO N

. . . G O S U Be x a m p l ei n F i g .9 - 8 ,t h eG O T Of u n c

t i o n e f f e c t i v e l yi n t e r c e p t si n c o r r e c t l ye n t e r e d

n u m b e r s .

T h i st y p eo fm e n uw o r k ss m o o t h l y ,a n dc a nb e

e a s i l yi n s t a l l e d i na p r o g r a m .I n f a c t ,b e c a u s ei t s

m o d u l a r ,y o uc a ni n s t a l li t a s t h ev e r yl a s tt h i n gy o u

d o ,s i n c ei t s i m p l yc a l l so t h e rs u b r o u t i n e s .M a n u s e r sl i k et o p l a c e t h em e n ua t t h eb o t t o mo f t h e

p r o g r a m ,s o t h a ti t d o e sn o tc l u t t e ru p t h ea p p e a

a n c eo f t h e l i s t i n g .B u ti t r e a l l yd o e s n ' tm a t t e

w h e r e y o up u ty o u rm e n u .A sl o n g a si t ' sm o d u l a

i t w i l lw o r ka n y w h e r e .

M o d u l a r i t ya l s om e a n st h a tt h e s em e n u sc a n

b e e a s i l yr e p l a c e d ,s ow h e ny o ur e a l l yw a n tt o d r e s

u p a m e n u ,y o uc a ni n s t a l la f a n c i e rv e r s i o nw i t h

o u tm e s s i n gu p t h er e s to f y o u rc o d e .

1 . A D DA R E C O R D

2 . E D I TA R E C O R D

3 . S A V EF I L E

4 . R E T R I E V EF I L E

5 . E X I T

Y O U RC H O I C E( # ) : _

F i g .9 - 6 .A t y p i c a lp r o g r a mm e n u .

1 1 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 121/194

g o t o 5 0 0 0 0

m e n u u s i n gi f

0

2

3 :

4 :

1 0 0 p r i n t : p r i n t " 1s e l e c t e d " : s l e e p1 :

2 0 0 p r i n t : p r i n t " 2s e l e c t e d * 9 : s l e e p1 :

3 0 0 p r i n t : p r i n t * 1 3s e l e c t e d " : s l e e p1 :

4 0 0 p r i n t : p r i n t ss e l e c t e d 1 * : s l e e p1 :

5 0 0 :

6 0 0 :

5 0 0 0 0: r e mm e n uu s i n gi f

5 0 0 0 5 d ou n t i le s c

r e t u r n

r e t u r n

r e t u r n

r e t u r n

s c n c l r

c h a r , 0 , 8

p r i n tN 1 . a d d a

p r i n tW 2 .

p r i n t" 3 . s e a r c h t h i sf i l e 1 1

p r i n tW 4 . s o r t i t e m s "

p r i n tH 5 . e n d p r o g r a m "

r e m i n i t i a l i z e

o r c h o i c e> 5

5 0 0 1 0

5 0 0 1 5

5 0 0 2 0

5 0 0 3 0

5 0 0 4 0

5 0 0 5 0

5 0 0 6 0

5 0 0 6 5

5 0 0 7 0

5 0 0 7 5

5 0 0 8 0

5 0 0 8 2

5 0 0 8 5

5 0 0 9 0

5 0 0 9 5

5 0 0 9 6

5 0 0 9 75 0 1 0 0

5 0 1 1 0

5 0 1 2 0

5 0 1 3 0

5 0 1 3 2

5 0 1 3 3

5 0 1 3 5l o o p

5 0 1 4 0p r i n t : p r i n t w b y e ! "

5 0 1 5 0 e n d

r e c o r d "

d e l e t ea r e c o r d "

5 .

c h o i c e = 0

d o w h i l ec h o i c e< 1

c h a r , 0 , 1 4

i n p u t" c h o i c e ( # ) : " ; c h o i c e $c h o i c e = v a l ( c h o i c e $ )

l o o p

r e mo n l yt h i s c h a n g e sf r o mo n . . . g o s u b

: i f

: i f

: i f

: i f

: i f■ ■ ■ ■ • •

c h o i c e = i

c h o i c e = 2

c h o i c e = 3

c h o i c e = 4

c h o i c e = 5

t h e n

t h e n

t h e n

t h e n

t h e n

g o s u b

g o s u b

g o s u b

g o s u b

e s c = 1

1 0 0

2 0 0

3 0 0

4 0 0

F i g . 9 - 7 .A m e n uu s i n gI F b r a n c h e s .

B A RM E N U SM a n yc o m m e r c i a l p a c k a g e sn o wu s ea m e n u

f o r mk n o w na s a b a rm e n u .A s t h en a m ei m p l i e s ,

a h i g h l i g h t i n gb a ri s u s e dt o p o i n tt o d i f f e r e n to p

t i o n so n t h e s c r e e n . T y p i c a l l y ,t h e o p e r a t o r p r e s s e s

a r r o w k e y so r u s e sa m o u s et o m o v et h eh i g h l i g h t

i n g b a r t o d i f f e r e n t o p t i o n s .W h e nt h e o p t i o

d e s i r e db e c o m e s" l i tu p / 't h er e t u r nk e yo r a b u t

t o no n t h em o u s ec a n b ep r e s s e dt o s e l e c ti t . F i g

u r e9 - 9s h o w sa n e x a m p l eo f a b a rm e n uw i t ha

h i g h l i g h t e ds e l e c t i o n .

S u r p r i s i n g l y ,t h el o g i cr e q u i r e df o ra b a rm e n u

1 1 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 122/194

i s n o tt e r r i b l ym u c hm o r ec o m p l i c a t e d t h a nt h el o g i c

f o ra s i m p l e" p r e s s t h ei t e mt o s e l e c t "t y p eo fm e n u .

T h ea d d i t i o n a lr o u t i n e sn e e d e da r e :

1 . A r o u t i n et o r e a dp o s s i b l em e n us e l e c t i o n sf r o m

a f i l eo r a l i s to f d a t as t a t e m e n t s .

2 . A r o u t i n et o d i s p l a yt h em e n u ,h i g h l i g h t i n gt h e

c u r r e n to p t i o n .

3 . A r o u t i n et o G E Ta s i n g l ek e y s t r o k ea n dt o d e

t e r m i n ew h e t h e rt h eb a rs h o u l dm o v eu p o r

d o w n .

U s u a l l y ,a r r o wk e y sa r eu s e dt o m o v et h eb a r :

A d o w na r r o wp u s h e st h eb a rd o w n w a r da n dt h e

u p a r r o w m o v e s .S o m e t i m e st h er i g h ta r r o wa n d

l e f ta r r o wk e y sa r ea l s ou s e d ,w i t hr i g h tp u s h i n

t h eb a rd o w n w a r d ,a n da l e f ta r r o wk e y s t r o k em o v

i n gi t u p w a r d .

g o t o5 0 0 0 0

m e n u u s i n go n . . . g o s u b

0

2

3 :

4 :

1 0 0 p r i n t : p r i n t " !s e l e c t e d " : s l e e p1 :

2 0 0 p r i n t : p r i n t N 2 s e l e c t e d " : s l e e p1 :

3 0 0 p r i n t : p r i n t " 3 s e l e c t e d " : s l e e p1 :4 0 0 p r i n t :p r i n t 1 1 4s e l e c t e d " :s l e e p1 :

5 0 0 :

6 0 0 :

5 0 0 0 0: r e mm e n uu s i n go n . . . g o s u b

5 0 0 0 5d o u n t i le s c

s c n c l r

c h a r , 0 , 8

p r i n tH 1 . a d d

p r i n t" 2 .

r e t u r n

r e t u r n

r e t u r nr e t u r n

a r e c o r d "

d e l e t ea r e c o r d "

p r i n t" 3 . s e a r c h t h i sf i l e "

p r i n t" 4 . s o r t i t e m s "e n d p r o g r a m "

: r e m i n i t i a l i z e

o r c h o i c e> 5

5 0 0 1 0

5 0 0 1 5

5 0 0 2 0

5 0 0 3 0

5 0 0 4 0

5 0 0 5 05 0 0 6 0

5 0 0 6 5

5 0 0 7 0

5 0 0 7 5

5 0 0 8 0

5 0 0 8 2

5 0 0 8 5

5 0 0 9 0

5 0 0 9 5

5 0 0 9 6

5 0 0 9 7

5 0 1 0 0o n c h o i c eg o s u b1 0 0 , 2 0 0 , 3 0 0 , 4 0 0

5 0 1 3 2: i f c h o i c e = 5t h e ne s c = 1

5 0 1 3 3: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :

5 0 1 3 5l o o p

5 0 1 4 0p r i n t : p r i n t " b y e ! "

5 0 1 5 0 e n d

p r i n t" 5 .

c h o i c e = 0

d o w h i l ec h o i c e

c h a r , 0 , 1 4

i n p u t " c h o i c e ( # ) : " ; c h o i c e $

c h o i c e ^ v a l ( c h o i c e $ )

l o o p

r e mo n l yt h i s c h a n g e sf r o mi f

F i g .9 - 8 .A m e n uu s i n gO N . . . G O S U Bb r a n c h e s .

1 1 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 123/194

T h eo p t i o nt o e s e l e c t e di s h i g h l i g h t e du s i n g t h ea r r o wk e y s .O n c et h ed e s i r e di t e mi s h i g hl i g h t e d ,i t c a n b ee x e c u t e d b yp r e s s i n gR E T U R N .

A D DA R E C O R D

E D I TA R E C O R D

S A V EF I L E

R E T R I E V EF I L E

E X I TP R O G R A M

A D DE f lS A V ER E T R I E V EE X I T

B a rm e n u sc a na l s ob e l a i do u th o r i z o n t a l l y .

F i g . 9 - 9 .A t y p i c a lb a rm e n u .

I d e a l l y ,t h eb a rm e n ur o u t i n es h o u l da d v a n c e

t h eb a rt o t h et o po f t h em e n ui f t h eb a ri s a t t h e

b o t t o mw h e nt h ed o w na r r o wi s p r e s s e d .T h eb a r

s h o u l dm o v et o t h eb o t t o m r u n gw h e ni t h a sb e e n

r e s t i n ga t t h e t o pa n dt h eu p a r r o wi s p r e s s e d .

C o d i n gt h eB a rM e n u

L e t ' sb e g i nw i t ht h ef i r s ts t e p ,w h i c hi s t h er o ut i n et o a s s i g nm e n ui n f o r m a t i o n .T h e r ea r el o t so f

w a y st h i sc a nb e d o n e .Y o uc o u l da s s i g nm e n uo p

t i o n sd i r e c t l y :M N $ ( 1 ) =" P R I N TI N F O R M AT I O N *\ Y o uc o u l dr e a dt h e s eo p t i o n sf r o ma f i l e ,

o r y o uc o u l dr e a dt h e mf r o mD A T As t a t e m e n t s .

Y o u ' l l n o t i c et h a ts o f a ri n t h i sb o o kw e h a v e

n o tu s e dd a t a s t a t e m e n t s .T h i si s b e c a u s ed a t a

s t a t e m e n t sc a nb e c o m et r o u b l e s o m e .I f t h e r e a r e

s e v e r a ld i f f e r e n tt y p e so f d a t at o b e r e a d ,t h ep r o

g r a mh a st o " s k i p "u n n e e d e di n f o r m a t i o nu n t i li t

c o m e st o t h er i g h tp l a c ei n t h ed a t as t a t e m e n tl i s t .

A l lt h i ss k i p p i n gc a l l sf o re x t r ac o d ea n dc r e a t e s

g e n e r a lc o n f u s i o n .F o rm u l t i p l ea p p l i c a t i o n s ,d a t a

s t a t e m e n t sa r en o tr e a l l yw o r k a b l e .

D a t as t a t e m e n t s ,h o w e v e r , c a nb e i m m e n s e l y

u s e f u li f y o um a k ea s t a r t i n gd e c i s i o n — a n ds t i c k

t o i t — t h a tt h e yw i l lb e u s e df o ro n ea p p l i c a t i o no n l y .

D a t as t a t e m e n t sa r ee s p e c i a l l yh e l p f u lw h e ny o u ' r e

d e s i g n i n gm e n u s , b e c a u s et h e yw o r ka t l i g h t e n i

s p e e da n de n a b l ey o ut o e a s i l yc h a n g et h en a m e

o f m e n uo p t i o n s .I f y o u ' v ed o n e a n ys e r i o u sp r o

g r a m m i n gy o ua l r e a d yk n o wh o wo f t e nm e n uo p

t i o n sc a nc h a n g e .

T h ed a t as t a t e m e n t sw eu s ef o rt h em e n uw i l l

c o n t a i n :

1 . T h en a m e so f m e n uo p t i o n s ,e x a c t l y a st h e y

s h o u l da p p e a ro n t h es c r e e n .

2 . A ne n d - o f - n a m e sm a r k e ra t t h ee n dt o s i g n i

t h a tt h e r e a r en o a d d i t i o n a le n t r i e s .

T h es y s t e mw i l lu s et h em a r k e ra s a n i n d i c a

t i o n t h a ti t s h o u l ds t o p r e a d i n g .H e r e ' sw h a tt h e

d a t as t a t e m e n tm i g h tl o o kl i k e :

1 0 0 0 0D A T A" A D DI N F O R M A T I

' ' C H A N G EI N F O R M A T I " S O R TI N F O R M A T I O N " ,

N o t et h a tm e n uo p t i o n sa r e e n c l o s e di n q u o t e

a s a m a t t e ro f c o n v e n t i o n ;t h e yd o n ' ta c t u a l l yh a v e

t o b e i n t h i sc a s e ,b e c a u s et h e y ' v e g o tn o c o m m a

o r c o l o n s .G e n e r a l l y ,t h o u g h ,p r o g r a m m e r sw i l le n

c l o s ea n ya l p h a n u m e r i cd a t ai t e m sw i t h i nq u o t e s

1 1 3

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 124/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 125/194

0

1

2

3

4

g o t o1 0

m e n u u s i n g o n . . . g o s u b

r e m d e f i n i t i o n s2 02 5 :

2 6 s c n c l r

3 0 d w n $ = c h r $ ( 1 7 )

4 0 u p $ = c h r $ ( 1 4 5 )

5 0 r e t r n $ = c h r $ ( 1 3 )

6 0 e s c $ = c h r $ ( 2 7 )

8 0 :

9 0 g o t o5 0 0 0 0

1 0 0 p r i n t : p r i n f ' 1 s e l e c t e d " : s l e e p 1 : r e t u r n

2 0 0 p r i n t : p r i n t " 2 s e l e c t e d " : s l e e p1 : r e t u r n3 0 0 p r i n t : p r i n t " 3 s e l e c t e d " : s l e e p1 : r e t u r n

4 0 0 p r i n t : p r i n t M 4 s e l e c t e d " : s l e e p1 : r e t u r n

5 0 0 :

6 0 0 :

5 0 0 0 0: r e mm e n uu s i n go n . . . g o s u b

5 0 0 0 1: r e a d i n d e x $: r e mi n d e xt o k e y s

5 0 0 0 2: d o w h i l es e l e c t $ ( c t r ) < > " # # # "

5 0 0 0 3: c t r x c t r + 1

5 0 0 0 5: r e a dc o l ( c t r ) , r o w ( c t r ) , s e l e c t $ ( c t r )

5 0 0 0 6: l o o p

5 0 0 0 7: n o = c t r - 1 : r e mn b r o fi t e m s5 0 0 0 9: d o u n t i l e s c

5 0 0 1 0: g o s u b 5 2 0 0 0: r e md i s p l a yf u l l

5 0 0 1 2: c h o i c e = l : l a s t = n o

5 0 0 1 5: g o s u b5 4 0 0 0 : r e md i s p l a y h i g h l g t

5 0 0 1 7: r e t r n = 0 : e s c = 0

5 0 0 2 0: d o u n t i l r e t r no r e s c

5 0 0 3 0: g o s u b 5 3 0 0 0: r e mg e t k e y

5 0 0 4 0: g o s u b 5 4 0 0 0: r e md i s p l a y p a r t i a l

5 0 0 5 0: l o o p

5 0 0 9 0:

5 0 0 9 5c h a r , 0 , 2 2

o n c h o i c e g o s u b 1 0 0 , 2 0 0 , 3 0 0 , 4 0 0

i f c h o i c e = 5t h e ne s c = 1

5 0 1 0 0

5 0 1 3 2

5 0 1 3 3

5 0 1 3 5l o o p

5 0 1 4 0p r i n t :p r i n f b y e !

5 0 1 5 0e n d

F i g .9 - 1 1 .A c o m p l e t eb a rm e n up r o g r a m .

1 1 5

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 126/194

5 2 0 0 0

5 2 0 1 0

5 2 0 2 0

5 2 0 3 0

5 2 0 4 0

5 2 0 5 0

5 2 0 6 0

5 3 0 0 0

5 3 0 0 5

5 3 0 1 0

5 3 0 1 2

5 3 0 1 5

5 3 0 2 0

5 3 0 3 0

5 3 0 3 5

5 3 0 4 0

5 3 0 4 5

5 3 0 4 7

5 3 0 4 8

5 3 0 5 0

5 3 0 6 0

5 4 0 0 0

5 4 0 1 0

5 4 0 2 0

5 4 0 3 0

5 4 0 3 5

5 4 0 4 0

5 4 0 5 0

6 0 0 0 0

6 0 0 1 5

6 0 0 2 0

6 0 0 3 0

6 0 0 4 0

6 0 0 5 0

6 0 0 6 0

6 0 2 0 0

: r e m d i s p l a ya l l o p t i o n s•

: f o r c t r = 1t o n o

c h a r , c o l ( c t r ) , r o w ( c t r ) , s e l e c t s ( c t r ) , 0

n e x t

r e t u r n

r e m k e y p r e s s

l a s t = c h o i c e

g e t k e ya $

i f i n s t r ( i n d e x $ , a $ )t h e nc h o i c e = i n s t r ( i n d e x $ , a $ )

i f a $ = u p $ t h e n c h o i c e = c h o i c e - 1

i f a $ = d w n $t h e nc h o i c e = c h o i c e + 1

i f a $ = e s c $t h e ne s c = 1 : c h o i c e = n o

i f a $ = r e t r n $ t h e nr e t r n = 1

i f c h o i c e < 1 t h e nc h o i c e = n o

i f c h o i c e > n ot h e nc h o i c e = 1

r e t u r n

r e m d i s p l a yb a r s

c h a r , c o l ( l a s t ) , r o w ( l a s t ) , s e l e c t s ( l a s t ) , 0

c h a r , c o l ( c h o i c e ) , r o w ( c h o i c e ) , s e l e c t s ( c h o i c e

c h a r , 9 , 2 2 , " e s c t oe n d 1 1

r e t u r n

:

: r e m m e n ud a t a

d a t a " a d s i e ": r e mf i r s tl t r i n d e x

d a t a 5 , 1 0 , "a d d a r e c o r d N

d e l e t ea r e c o r d H

s e a r c h t h i s f i l e"

i n d e xi t e m s "

e n d p r o g r a m "

: r e m e n d m a r k

d a t a 5 , 1 1 , "

d a t a 5 , 1 2 , "

d a t a 5 , 1 3 , "

d a t a 5 , 1 4 , "

d a t a 0 , 0 , " # # # "

3 . S e tt h eb a r - u po r b a r - d o w nf l a g .

4 . O r s e to t h e rf l a g s .

5 . R E T U R Nf r o mt h e s u b r o u t i n ei n o r d e rt o h i g h

l i g h tt h e s e l e c t e di t e m .

T h ep r o g r a mw i l lc o n t i n u et o a c c e s st h ek e y

p r e s sa n dd i s p l a y - b a r s r o u t i n e sf r o mt h e l o o pi n

l i n e s5 0 0 2 0t h r o u g h5 0 0 5 0u n t i lt h e u s e r p r e s s e s

R E T U R N ,a t w h i c ht i m ea s p e c i a lf l a gw i l lb e s e ta n dt h es y s t e mw i l l '' f r e e z e ' 'a l ls e l e c t e d v a r

e x a c t l yw h e r et h e ya r e .

G o i n gF u r t h e r

O n c ey o uh a v ey o u rb a rm e n uw o r k i n g ,y o uc a n

a d db e l l sa n dw h i s t l e s .F o re x a m p l e ,y o um i g h

1 1 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 127/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 128/194

w a n tt o i n t e r c e p t t h eh e l pk e y( o rt h ef i r s tl e t t e r F i n a l l y ,y o um i g h tw a n tt o i n c l u d ea r o u t i n ef o r

o f i t , H ) t o p r o v i d ee x p l a n a t i o n so f m e n uo p t i o n s ,s o m ef a n c yh e a d i n g s , s ot h a ta l ly o u rm e n u sc a n

A sd o n ei n L i s t i n g9 - 1 1 ,t h eE S Ck e ym i g h t b e h a v ea s t a n d a r dp o l i s ht o t h e m .F i g u r e9 - 1 3s h o w

i n t e r c e p t e d ,a l l o w i n g u s e r st o q u i c k l ye x i tt h e t h es t e p si n v o l v e di n r e v i s i n ga m e n u ,

p r o g r a m .

1 1 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 129/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 130/194

g r a ma t a n yt i m e .( I nc a s ey o u ' r ec u r i o u s ,t h em e m

o r yl o c a t i o n t h a tp o i n t st o t h eb e g i n n i n g a d d r e s so f

t h ep r o g r a mi s 4 5 2 8d e c i m a l , o r$ 2 Dh e x . )

U s u a l l y ,B A S I Ch a sn or e a s o nt o b e c o n c e r n e d

a b o u tw h e r et h ep r o g r a ms t a r t s .P R I N T ,I N P U T ,

a n d m o s to t h e rc o m m a n d so p e r a t ei n t h es a m ew a y

n om a t t e rw h e r et h e ya r ei n t h ep r o g r a m .B u tt h e r e

a r ec e r t a i ns t a t e m e n t st h a tf o r c et h ec o m p u t e rt o

g ot o t h e t o po f t h ep r o g r a m m o s to ft h et i m e .W h e n

t h ep r o g r a ms e e k sa l i n es u c ha s t h i s :

2 0 0 G O T O1 0 0

y o up r o b a b l yt h i n k t h ec o m p u t e ri n s t a n t l yk n o w s

w h e r el i n e1 0 0i s a n dj u m p st o i t i m m e d i a t e l y .N o t h

i n g c o u l db e f u r t h e rf r o mt h et r u t h .A c t u a l l y t h e

C - 1 2 8j u m p st o t h e t o po f t h ep r o g r a m a n dc o m

m e n c e sa l o n g p r o c e s so f c o u n t i n ga h e a dd o w nt o

l i n e1 0 0 .

E v e r yl i n eb e t w e e nt h eb e g i n n i n go f t h ep r o

g r a ma n dt h ef i n a ld e s t i n a t i o nm u s tb e e x a m i n e d

f o ra m a t c h .I f t h ep r o g r a md o e s n ' tf i n dt h el i n e ,

o f c o u r s e ,a n U N D E F ' DS T A T E M E N Te r r o ro c

c u r s ,a n dt h ep r o g r a ms t o p s .

B e c a u s eo f t h ew a yl i n e sa r es t o r e di n t e r n a l l y

( i nt h em o s tc o m p a c t f o r ma v a i l a b l e t ot h ec o m

p u t e r ) ,t h e r e ' sn o i n d e xt o t e l lt h em a c h i n ew h e r e

t o f i n da l i n en u m b e r .S o i t d u t i f u l l ye x a m i n e sa n d

s k i p sl i n en u m b e r su n t i li t a r r i v e s a ti t sd e s t i n a t i o n .

T h e nt h es t a t e m e n ti s e x e c u t e d .

B A S I Ci s i t s e l fw r i t t e ni n s t r a i g h tm a c h i n el a n

g u a g e ,o fc o u r s e ,s o t h i si s a p r e t t yq u i c kp r o c e d u r e .

B u tt h et i m ec a na d du p .I f B A S I Ci s s k i p p i n ga

l a r g en u m b e ro f l i n e s( s a y1 , 0 0 0 ) ,y o uc a na c t u a l l y

c o u n t ab e a to r t w ob e f o r e t h ep r o g r a mr e a c h e st h e

d e s i r e dl i n ea n dr e a c t s .

H e r e ' sa n o t h e rw a yt o p i c t u r et h es i t u a t i o n .

I m a g i n ef o ra m o m e n ta nu n u s u a lH a l l o w e e n g a m e .

W h e nc h i l d r e ns t o pa t a h o u s ef o rc a n d y ,t h e ya r eg i v e nt h ea d d r e s so ft h en e x th o u s et h e ym u s tv i s i t .

I f t h en e wa d d r e s si s u pt h er o a d( g r e a t e rt h a n )t h e i r

c u r r e n t a d d r e s s ,t h e yc a np r o c e e d .B u ti f i ti s d o w n

t h er o a d ,o v e rp a v e m e n tt h e y ' v ep a s s e da l r e a d y ,

t h e y ' l lh a v et o g o t o t h eb e g i n n i n go f t h er o a db e

f o r et h e yc a nh e a df o rt h en e wh o u s e .I t ' sa s i l l y

g a m e ,b u ti t ' se x a c t l yh o wB A S I Cm o v e st h r o u

p r o g r a m s .

B A S I C ' Sw a yo f d o i n gt h e s et h i n g sa d d su p t o

a c o m p l i c a t i o nw ed o n ' t u s u a l l yp l a nf o ri n d e s i g

i n go u rp r o g r a m s :r o u t i n e sp l a c e da t t h eb o t t o mo f

t h ep r o g r a mt h a tu s eG O T O se x t e n s i v e l yc a nb e

m u c hs l o w e rt h a ni f t h e ya r ep l a c e da t t h et o po f

t h ep r o g r a m .T h es o l u t i o n ,h o w e v e r ,i s n o tt o r e a r

r a n g ey o u re n t i r ep r o g r a m .T h e r ea r el o t so f a l t e r

n a t i v e st o G O T O s .

Y o ua l r e a d yk n o wa b o u tt h r e eo f t h e m

D O / W H I L E ,D O / U N T I L ,a n dF O R. . . N E X T

L e t ' sl o o ka t w h a th a p p e n si n a t y p i c a lD O / U

T I Ll o o p .F i g u r e s1 0 - 1a n d1 0 - 2i l l u s t r a t e dt h a tt h i s

o p e r a t i o n , w h i c hc o u n t sf r o m1 t o1 0 0 0 , c o u lb e

d o n ee f f e c t i v e l yw i t he i t h e rG O T Oo r D O / U N T

T h eG O T Os t y l eo f l o o p ,h o w e v e r ,t a k e s a p p r

b l y l o n g e ri f t h e r ei s a g o o dn u m b e ro f p r o g r al i n e sa b o v ei t .

H e r e ' sw h y :W h e na G O T Ot h a t d i r e c t t h e

c o m p u t e rt o a p r e v i o u sl i n en u m b e ri s e n c o u n t e

t h em a c h i n em u s tg o t o t h e t o po f t h ep r o g r a m

c o u n td o w nt o t h en u m b e rs p e c i f i e d .I n F i g .1 0 -

t h ec o m p u t e rh a sn o w a yo f k n o w i n gt h a tt h el i n e

i t w a n t s( t h el i n et o G O T O )i s o n l yt h r e e o r f

l i n e sa b o v e ,B A S I Cm u s tg o t o t h eb e g i n n i n go f t h e

p r o g r a ma n dc o u n td o w nt o l o c a t et h ep r o p e rl i n e

a s i l l u s t r a t e d i nF i g .1 0 - 3 .

N o w ,a s s h o w ni n F i g .1 0 - 4 ,h e r e ' sw h a th a pp e n si n a D O / U N T I Ll o o p :w h e na D O / U N T

D O / W H I L Eo r F O R. . . N E X Tl o o pi s e x e c u t

B A S I Cm a k e sa m e n t a ln o t eo f t h el o o p ' s s t a

1 0 :

2 0 :

1 0 0 0

1 0 1 0

1 0 2 0

1 0 3 01 0 4 0

1 0 5 0

1 0 5 5

1 0 6 0

1 0 7 0

r e m l o o pw i t h g o t o

c t r = c t r + 1■■

p r i n t" p a s s# M ; c t r•■

i f c t r < = 9 0 0t h e n1 0 0 0•

e n d■

F i g .1 0 - 1 .A l o o p u s i n gG O T O .

1 2 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 131/194

1 0 :

2 0 :

1 0 0 01 0 0 5

1 0 1 01 0 2 0

1 0 3 0

1 0 4 0

1 0 5 0

1 0 5 5

1 0 6 0

1 0 7 0

r e m l o o pw i t hd o / u n t i l

d o u n t i lc t r = 9 0 0••

c t r = c t r + 1

p r i n t " p a s s# H ; c t r••

l o o p■■

e n d■

F i g .1 0 - 2 .A l o o p u s i n gD O / U N T I L .

( F i r s tt i m eo n l y )X

E x e c u t e dw i t h

e a c hp a s s

X

X

X

L C

LL1

5

1 0

2 0

3 0

4 0

5 0

1 0 0

1 1 0

1 2 0

G O T O1 0 0

R E M

R E M

R E M

R E M

R E M

P R I N T" H E L L OW O R L D "

C T R * C T R+ 1

I F C T R< 1 0 0T H E N1 0 0

1 3 0 E N D

M J T h ep r o g r a ms t a r t sb y c o u n t i n gu p t h el i n e su n t i li t r e a c h e s1 0 0 .

[ 2 J O n c el i n e s1 0 0a n d1 1 0a r ee x e c u t e d ,t h ec o m p u t e ri s i n s t r u c t e dt o b r a n c hb a c kt o 1 0 0 .

E v e nt h o u g hl i n e1 0 0i s j u s tt w ol i n e sa w a y ,t h ec o m p u t e r m u s t g ot o t h et o pa n db e g i n

" c o u n t i n gu p "a g a i n .

N o t e :O n l yl i n e sm a r k e dw i t ha n X a r ee x e c u t e d .A l lo t h e r sa r e s k i p p e do v e r .

F i g .1 0 - 3 .D i a g r a mo f l o o p o p e r a t i o n u s i n gG O T O .

1 2 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 132/194

( F i r s tt i m eo n l y )X

E x e c u t e dw i t h

e a c hp a s s

5 G O T O1 0 0

1 0 R E M

2 0 R E M

3 0 R E M

4 0 R E M5 0 R E M

1 0 0 D OU N T I LC T R >1 0 0

1 1 0 P R I N T" H E L L OW O R L D "

1 2 0 C T R= C T R+ 1

1 3 0 L O O P

1 4 0 E N D

T h ep r o g r a ms t a r t sb y c o u n t i n gu p t h e l i n e su n t i li t r e a c h e s1 0 0 .

O n c el i n e s1 0 0 , 1 1 0 a n d1 2 0a r ee x e c u t e d ,t h ec o m p u t e ri s i n s t r u c t e dt o l o o pb a c kt o t h e

b e g i n n i n g .

B e c a u s et h eD O / U N T I La n dD O / W H I I Es t r u c t u r e sr e m e m b e r w h e r et h el o o ps t a r t s ,t h e

c o m p u t e r g o e sd i r e c t l y t ol i n e1 0 0a n dc o n t i n u e s .F O R. . . N E X Tw o r k s u n d e rt h es a m e

p r i n c i p l e s .

N o t e :O n l yl i n e sm a r k e dw i t ha n X a r ee x e c u t e d .A l lo t h e r sa r es k i p p e do v e r .

F i g .1 0 - 4 .D i a g r a mo f l o o p o p e r a t i o n u s i n gD O / U N T I L .

l i n e .W h e ni t e n c o u n t e r st h eL O O Po r N E X Tc o mm a n d ,i t s i m p l yj u m p sb a c kt o t h em e m o r yl o c a

t i o ni t n o t e da t t h eb e g i n n i n go f t h el o o p .S i n c et h i s

l o o p - s t a r ta d d r e s si s a d i r e c tm e m o r yl o c a t i o n ,

t h e r e ' sn o c o u n t i n gi n v o l v e d .T h ec o m p u t e r ' sm e n

t a ln o t et e l l si t i m m e d i a t e l yw h i c hl i n et o g o b a c kt o .

I t ' sa s i f o u rH a l l o w e e nk i d sc o u l dg o b a c kt o

a h o u s ei m m e d i a t e l y ,a s l o n ga s t h e y ' dp l a n t e da

f l a gi n t h ef r o n ty a r d .

Y o uc a ns e ew h yD O / U N T I L ,D O / W H I L E ,a n d

F O R. . . N E X Tl o o p sc a nb e m u c hf a s t e rt h a n

G O T O s ,e v e ni f t h e y ' r ee s s e n t i a l l yp e r f o r m i n gt h e

s a m eo p e r a t i o n s .N o to n l ya r e t h e l o o ps t a t e m e n t s

e a s i e r t o r e a d ;t h e ya r ea l s oe a s yt o e x e c u t e — ac a s e

o f h a v i n gy o u r c a k ea n de a t i n gi t t o o .

I f y o u ' r er e a l l ya s p e e dd e m o n ,h e r e ' sa n o t h e r

i t e mf o r y o u rp r o g r a m m i n gd e s i g nl i s t :D Oa n d

F O R. . . N E X Tl o o p sa l s ow o r ka t s l i g h t l y d i f f e r

e n ts p e e d s .W h i c ho n e d oy o ut h i n ki s f a s t e r ?

F O R. . . N E X TS t r u c t u r e sT h r o u g h o u tt h i sb o o kw e ' v e u s e dD O / L O

s t r u c t u r e se x t e n s i v e l y .P a r t l yt h a t ' sb e c a u s et h e y ' r

a t t r a c t i v ea n de a s yt o r e a d .P a r t l y t o oi t ' sb e c a u s

i t i s f u nt o p l a yw i t hn e wt y p e so f s t a t e m e n t s .G i v e

p r o g r a m m e r sa n e ws e to f c o m m a n d s ,a n dt h en e w

c o m m a n d sw i l lb e a p p l i e di n e v e r ys i t u a t i o

r e g a r d l e s so f h o wa d v i s a b l et h a tm a yb e .

B u tl e t ' sn o tf o r g e ta b o u t o u ro l df r i e n dF O R

. . . N E X T .I t ' sb e e ni n B A S I Cf r o mt h eb e g i n n i n

a n da s y o u ' v es e e ni n t h ep r e v i o u se x a m p l e ,i t s

o p e r a t i o no n t h eC o m m o d o r e1 2 8i s q u i t ef a s t .

I n f a c t ,f o ra l o o pt h a ti n v o l v e ss i m p l e c o u

i n g ,F O R . . .N E X Tb e a t st h eD O / L O O Ps t r u c t u r

h a n d s d o w n .F i g u r e s1 0 - 1a n d1 0 - 2s h o wt w os u c h

l o o p s ,a n dy o uc a nr e a d i l ys e ew h yr e w r i t i n g

l o o p u s i n gF O R. . . N E X Tw o u l dh a v et h ee d g e .

T h e r e ' sa n e x t r as t a t e m e n tr e q u i r e df o r t h e

D O / L O O Ps t r u c t u r e :C T R = C T R + 1 .W h e nm u l -

1 2 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 133/194

t i p l i e dt h o u s a n d so f t i m e s ,t h a ts i m p l e s t a t e m e n t

i s l i k e r u n n i n go n r e g u l a rv e r s u sh i - t e s t — t h e

D O / L O O Pl o s e se v e r yt i m e .I t ' sn o tm u c ho f a

d i f f e r e n c e ,b u ti f y o u ' r eu s i n ga l o o kf o rt h o u s a n d s

o f p a s s e sy o us h o u l dc e r t a i n l yk e e pi t i n m i n d .I f

y o ut a k et h e t r o u b l et o t i m e - t e s tt h e s er o u t i n e s ,

y o u ' l lf i n d t h a tt h eF O R. . . N E X Ts t r u c t u r ei s

a b o u tt e n p e r c e n tf a s t e r .

M A K I N GT H EM O S TO FS U B R O U T I N E S

W h a ta b o u ts u b r o u t i n e s ?W o u l da p r o g r a mt h a t

m a k e se x t e n s i v eu s eo f t h e mb e s l o w e dd o w n ,o r

n o t ?

T h ef a c t sm i g h ts u r p r i s ey o u .I f t h i n g sa r eh a n

d l e dp r o p e r l y ,p r o g r a m sw i t hl o t so f s u b r o u t i n e s

t e n dt o b e al o tf a s t e rt h a np r o g r a m sw i t h o u ta l o t

o f s u b r o u t i n e s .

R u l e1 . A l w a y sc a l ly o u rs u b r o u t i n e sf r o m

a b o v e :

1 0 0

1 1 0

G O S U B1 5 0

G O S U B1 2 0

T h e C o m m o d o r e1 2 8a l w a y sr e c o g n i z e s t h e

c u r r e n tl i n en u m b e r ,s o i t c a nq u i c k l ys e a r c hf o r

w a r df o ra l i n ew i t h o u ts t a r t i n ga l o n gt r i pf r o mt h e

t o po f t h ep r o g r a m .I f a d e s t i n a t i o ni s 1 0 l i n e s

a h e a d ,B A S I Cw i l lo n l yh a v et o e x a m i n e1 0 l i n e s

t o f i n da m a t c h . T h i sm e a n st h a ts e t t i n g t oa l i n e

w i t hG O S U Bw o r k si n m u c ht h es a m ew a yi t d o e s

w i t hG O T O .

1 0 :

2 0 :

3 0 :

4 0 :

9 0 0

9 5 0

9 6 0

9 7 0

9 8 0

9 9 0

1 0 0 0

1 0 1 0

g o s u b1 0 0 0■■

e n d•■

••

•a

p r i n t M h e l l o H

r e t u r n

R u l e2 . G r o u pf r e q u e n t l yu s e ds u b r o u t i

d i r e c t l yu n d e rt h er o u t i n et h a tc a l l st h e m .

L e t ' st a k ea l o o ka t h o wt h i sw o r k s .

F i g u r e s1 0 - 5a n d1 0 - 6s h o wt w om e t h o d so f

c a l l i n ga s u b r o u t i n e .O n ep l a c e s t h e s u b r

d i r e c t l yb e l o wt h er o u t i n et h a ti s c a l l i n gi t . T h e

o t h e rp l a c e s t h er o u t i n es o m e w h e r ei n t h em i d d l

o f t h ep r o g r a m .N o w ,l e t ' sg o t h r o u g hw h a t h a

p e n sw h e ne a c ho f t h e s ep r o g r a m si s e x e c u t e d

I n t h ef i r s te x a m p l e ,w h e nt h ec o m p u t e rh i t s

t h eG O S U Bi t i m m e d i a t e l yd e t e r m i n e st h a tt h el i n e

n u m b e ri s g r e a t e rt h a nt h e c u r r e n tp o s i t i o n —

l i n en u m b e ri s f u r t h e rd o w ni n t h ep r o g r a m .S o i t

s k i p sd o w nt e no r t w e n t yl i n e st o t h a tr o u t i n ev e r y

q u i c k l y .

I n t h es e c o n d e x a m p l e ,t h ec o m p u t e rd e t e r

m i n e st h a tt h el i n en u m b e ri s a b o v ei t . S i n c eB A

S I Cc a n ' tc o u n tb a c k w a r d s ,i t d o e st h eo n l yt h i n gi t k n o w sh o wt o d o .I t g o e st o t h et o po f t h ep r o

g r a ma n db e g i n s c o u n t i n gd o w n ,p e r h a p ss k i p p i n

o v e ra s m a n ya s o n e h u n d r e dl i n en u m b e r sb e f o r

l i g h t i n go n t h e d e s i r e dr o u t i n e .T h i s a p p r o ai s

v e r ys l o w .

A ss h o w ni n F i g s .1 0 - 7a n d1 0 - 8 , i nb o t hc a s e s

t h ep r o g r a mg o e sb a c kt o t h eo r i g i n a lr o u t i n ei m

m e d i a t e l y( w i t h o u tc o u n t i n gl i n e s ) ,b e c a u s et h e

5 g o t o 2 0 0 0: r e mm a i n p r o c e s s

1 0 :

2 0 :

3 0 :

4 0 :

1 0 0 0p r i n ^ h e l l o "1 0 1 0r e t u r n

2 0 0 0

2 0 1 0

2 0 2 0

2 0 3 0

2 0 4 0 g o s u b1 0 0 0

2 0 5 0

2 0 6 0 e n d

2 0 7 0

2 0 8 0

2 0 9 0

F i g .1 0 - 5 .C a l l i n ga s u b r o u t i n et h a t ' sb e l o w . F i g . 1 0 - 6 .C a l l i n ga s u b r o u t i n et h a t ' sa b o v e .

1 2 3

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 134/194

L

5 G O T O1 0 0

1 0 R E M

2 0 R E M

3 0 R E M

4 0 R E M

5 0 R E M

1 0 0 R E M

2 0 0 G O S U B 5 0 0

3 0 0 R E M

4 0 0 E N D

5 0 0 P R I N T" H E L L OW O R L D " : R E T U R N

f ^ \ O n c et h ec o m p u t e ra r r i v e sa t l i n e2 0 0 ,i t i n t e r p r e t sa n i n s t r u c t i o n t op e r f o r mt h er o u t i n e

a t l i n e5 0 0 .

( 2 ) B e c a u s el i n e5 0 0i s g r e a t e rt h a n2 0 0 ,t h eC - 1 2 8s i m p l yc o u n t sa h e a dt h r e el i n e s .

T h ec o m p u t e re x e c u t e st h er o u t i n ea t l i n e5 0 0 ,a n dr e t u r n sd i r e c t l y t ol i n e2 0 0( i t r e m e m

b e r sw h e r et h e s u b r o u t i n ec a l lo r i g i n a t e d ) .

N o t e : O n l yl i n e sm a r k e dw i t ha n X a r ee x e c u t e d .A l lo t h e r s a r es k i p p e do v e r .

F i g .1 0 - 7 .C a l l i n ga r o u t i n et h a t ' sa t a h i g h e rl i n en u m b e r .

C o m m o d o r e1 2 8 k e e p s as t a c ko f o r i g i n a t i n gp o s i

t i o n s f o rG O S U B s .L i k ea h o m i n gp i g e o ns e tf r e e

f a rf r o mb a s e ,t h ep r o g r a ma l w a y sk n o w se x a c t l y

w h e r et o g o w h e ni t e n c o u n t e r sa R E T U R N

s t a t e m e n t .

B o t ha p p r o a c h e s w o r k ,b u tt h ef i r s ti s m u c h

f a s t e r ,e s p e c i a l l yw h e nt h e r o u t i n ei s b e i n gc a l l e d

s e v e r a lt i m e s .T h ek e yi s p l a c i n g t h er o u t i n eb e i n g

c a l l e da s c l o s ea s p o s s i b l et o t h em a i nr o u t i n e .I f

t h e r o u t i n e sb e i n gc a l l e dw e r ef a rb e l o w ,g e t t i n g

t o t h e mc o u l d t a k ej u s ta s l o n g .A sa l w a y si n c o m

p u t i n g ,t h es p l i ts e c o n d sd o a d du p .

I n r e v i e w ,D O / L O O Pa n dF O R. . . N E X T

s t a t e m e n t sa r e t h ef a s t e s tw a yt o g o ,a n dG O S U B s

s h o u l dc a l ll i n e sd e e p e ri n t ot h ep r o g r a m( l i n e sw i t h

h i g h e rn u m b e r s ) ,w h e n e v e rp o s s i b l e .T h e s e t w o

t r i c k s w i l ls a v el o t sa n dl o t so f t i m e .

P R O G R A M ST H A T A R ER E M A R K A BO n eo f t h eo t h e rc o m m o nf a l a c i e so f s p e e dp r o

g r a m m i n gi s t h a tt h ea d d i t i o n o fR E M a r k sw i l la p

p r e c i a b l ys l o wd o w nt h ep r o g r a m .A c t u a l l y ,i f y o u

u s et h et r i c k so u t l i n e ds o f a r ,R E M ss h o u l d c a

y o u n ot r o u b l ea t a l l ,a n dt h e y ' r ee s s e n t i a l f o p r o

g r a mr e a d a b i l i t y .

I f y o u ' r et r u l yc o n c e r n e dw i t he x t r a c t i n g

l a s to u n c eo f s p e e df r o my o u rm a c h i n e ,r e m o v i

s o m et y p e so f R E M a r k sc a nh e l pa l i t t l e .R E M

t h a td o n ' t r e q u i r ea s e p a r a t el i n en u m b e ro b v i o u

m e a no n el e s sl i n en u m b e rf o rB A S I Ct o s k i po v e

w h e ni t ' sh u n t i n gd o w na r o u t i n e .T h e r e ' s aw a y

t o r e m o v et h eR E M sw i t h o u ts a c r i f i c i n g r e a d

W h i l eR E M so n s e p a r a t el i n e sc a ns l o wd o w

a p r o g r a m ,R E M sp l a c e da t t h ee n do f e x i s t i n i n

f o r m a t i o nd on o t .I t ' sa l s oi m p o r t a n tt o r e a l i z et h a t

1 2 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 135/194

B A S I Cs k i p sa l li n f o r m a t i o nf o l l o w i n ga r e m a r k .S o

i f y o up l a c ea R E Ma t t h ee n do fa l i n et h a tw o u l d

h a v ee x i s t e da n y w a y ,y o u ' r en o ts l o w i n gt h i n g s

d o w n .A f t e r e x e c u t i n ge v e r y t h i n g b e f o r et h eR E M ,

t h eC o m m o d o r e1 2 8s k i p st o t h en e x tl i n e :

2 0 P R I N T" H E L L OT H E R E" ; A $ :R E MA $ I S N A M E

3 0 P R I N T :R E MC O N T I N U EW I T HP R O G R A M

T h e r e f o r e ,i f y o ua r ev e r y ,v e r y c o n c e r n e d a b o u t

s p e e d ,s i m p l yp l a c ea l ly o u rR E M sa t t h ee n do f

e x i s t i n gs t a t e m e n t so n e x i s t i n gl i n e s .

O T H E RS P E E DH I N T S

H e r ei s w h e r ew e r e a l l ys a c r i f i c e .Y o um a y

r e a l l yw a n tt o r e s e r v et h ef o l l o w i n gp r o g r a mt u r b o -

c h a r g e r sf o rt h e r o u t i n e sy o uu s em o s to f t e n —

w h e r es p e e dr e a l l yi s c r u c i a l .M o s to f t h e s et r i c k

s a c r i f i c ep r o g r a mr e a d a b i l i t y f o rf a s t e ro p e r a t i o

O n eo f B A S I C7 . 0 ' ss t r o n g e rf e a t u r e si s t h a t

y o uc a n u s e l o n g ,d e s c r i p t i v e v a r i a b l n a m e s

Y o u ' v es e e ni t a l o ti n t h i sb o o k .O n l yt h ef i r s tt w o

c h a r a c t e r sc o u n t ,s o S A V I N G Sa n dS A Lw o u l dr e

f e r t ot h es a m ev a r i a b l e .S t i l l , i t ' sn i c et o u s ev a r i

a b l en a m e st h a th a v es o m ec h a r a c t e r .

U n f o r t u n a t e l y ,l o n gv a r i a b l en a m e ss l o wd o w

y o u r c o m p u t e rl i k ea l o a d o fe x c e s sb a g g a g e .B e

c a u s eB A S I Ci n t e r p r e t se a c hc h a r a c t e ri n d i v i d u a

l o n gv a r i a b l en a m e st a k e l o n g e rt o p r o c e s st h a n

s h o r tv a r i a b l en a m e s .A g a i n ,i t b e c o m e sa c o m

p r o m i s eb e t w e e ns p e e da n dr e a d a b i l i t y :

1 0 S A V I N G S= I N C O M E - C O S T

c o u l db e w r i t t e na s :

1 0 S A = I N - C O

( F i r s tt i m eo n l y )X

X

X

1 W ^ 5

^ ^ 1 0^ \ 2 0^ ^ 3 0^ L - 4 0

C - £ D5 0P C I )1 0 0

L y 2 0 0

3 0 0

4 0 0

5 0 0

G O T O1 0 0

R E M

R E M

R E M

R E M

P R I N T" H E L L OW O R L D " : R E T U R N

R E M

G O S U B5 0

R E M

R E M

E N D

M J O n c et h ec o m p u t e ra r r i v e sa t l i n e2 0 0 ,I t i n t e r p r e t sa n i n s t r u c t i o n t op e r f o r mt h er o u t i n e

a t l i n e5 0 .

( ? )B e c a u s el i n e5 0i s l o w e r t h a n 2 0 0 , t h eC - 1 2 8m u s tr e t u r nt ot h e t o po ft h ep r o g r a ma n d

b e g i n c o u n t i n gu p w a r d .

( 3 )T h ec o m p u t e re x e c u t e st h er o u t i n ea t l i n e5 0 ,a n dr e t u r n sd i r e c t l yt o l i n e2 0 0( i tr e m e m

b e r sw h e r et h e s u b r o u t i n ec a l lo r i g i n a t e d ) .

N o t e :O n l yl i n e sm a r k e dw i t ha n X a r ee x e c u t e d .A l lo t h e r sa r es k i p p e do v e r .

F i g .1 0 - 8 .C a l l i n ga r o u t i n et h a t ' sa t a l o w e rl i n en u m b e r .

1 2 5

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 136/194

E v e nt h o u g ht h ep e r f o r m a n c ed i f f e r e n c ei s n o t

g r e a t ,i f y o ud o n ' tm i n ds a c r i f i c i n g r e a d a b i l i t y ,y o u

c a ns h o r t e nv a r i a b l en a m e si n s o m eo f t h em o r e

s p e e d - c r i t i c a la r e a so f y o u r p r o g r a m .

R e m e m b e rt h a tb o t ht h ef i r s ta n ds e c o n dl e t

t e r so f v a r i a b l en a m e sa r es i g n i f i c a n t .S Ai s n o t t h e

s a m ea s S , b u tS Aa n dS A V I N G Sw i l lc o n t a i nt h e

s a m ev a l u e s .

H e r e ' s a n o t h e rs a c r i f i c ey o u c a n m a k e .

T h r o u g h o u tt h i sb o o ky o u ' v e s e e nt h et r i c ko f i n

d e n t i n gp r o g r a ml i n e sb y s t a r t i n gt h e mw i t ha

c o l o n :

1 0 : R E M: T H I SI S A T E S T :

2 0 F O R X = l T O 1 0

3 0 :

4 0 : P R I N T" T H I SI S N U M B E R "

5 0 : P R I N TX

6 0 :

7 0 N E X T

T h i si n d e n t a t i o nm a k e sl o o p sa n da f e wo t h e r

p r o g r a ms t r u c t u r e sm u c he a s i e r t o r e a d .I t a l s oe n a

b l e sy o ut o s e tR E M sa p a r tf r o mt h em a i n b o d yo f

t h ep r o g r a m .B u ti n d e n t a t i o na d d ss p a c e s ,a n d

s p a c e s t a k e t i m et o d e c o d e ,e v e nt h o u g ht h e yc o n

t a i nn ou s e f u l i n f o r m a t i o n .R e m o v i n gs p a c e sf r o m

p r o g r a m sm a k et h e mo p e r a t ea t a f a s t e rc l i p( a n d

s a v e ss t o r a g es p a c et o b o o t ) .

I f y o uw a n tt o r e m o v et h ee x t r as p a c ec a u s e d

b y i n d e n t a t i o ni n s u c hl i n e s ,s i m p l yd e l e t et h ec o

l o n .B A S I Cw i l le l i m i n a t el e a d i n gs p a c e so n p r o

g r a ml i n e sa u t o m a t i c a l l y ,B y t h ew a y ,t h e e x t r a

l i n e sc r e a t e db y c o l o n st h a tf l ys o l o( l i n e s3 0 a n d

6 0 )a l s od e l a yt h ep r o g r a mf o rt h es a m er e a s o na s

s o l i t a r yR E M sd o .I f y o ur e a l l yw a n tt o t r a v e ll i g h t ,

t h e s e s h o u l db e e l i m i n a t e d a sw e l l .

S p e e dU p sw i t h V a r i a b l e s

O n eo f t h eo l d e rt r i c k si n g i v i n gB A S I Ca l i t t l e

e x t r ak i c ki s t o r e p l a c ec o m m o n l yu s e dw o r d sa n d

n u m b e r sw i t hv a r i a b l e s .W h e nB A S I Ce n c o u n t

a p r i n to r p r o c e s s i n gs t a t e m e n ts u c ha s :

A = 7 * 3

I t m u s ti n t e r p r e ta n dt r a n s l a t et h e7 a n d3 i n t oi t s

o w nb i n a r yn u m b e rs y s t e m .B o t ho p e r a t i o n st a k et i m e .H o w e v e r ,w h e nt h ec o m p u t e re n c o u n t e r t h e

s a m ev a l u e si n v a r i a b l e s :

A = B * C

B a n dC h a v i n gb e e na s s i g n e d a s 7a n d3 r e s p e

t i v e l y ,p r o c e s s i n gt i m ed i m i n i s h e s ,b e c a u s eB A S I

a u t o m a t i c a l l yk n o w st h ev a l u e so f t h ev a r i a b l

w h i c ha r ek e p ti n a s p e c i a l v a r i a b l e t a i n t h e

m e m o r y .T h eC o m m o d o r e1 2 8a l w a y sk n o w

w h e r et h o s ev a r i a b l e sa r el o c a t e da n dt h e i rv a l u eW h a t ' sm o r e ,t h e v a l u eh a sa l r e a d yb e e nt r a n s l a

i n t ot h ec o m p u t e r ' sn a t i v eb i n a r yn u m b e rs y s t e m

T h i sf a c tm a k e sA = B * Cf a s t e rt h a n t h ep e r h a

m o r er e a d a b l ea l t e r n a t i v e ,A = 7 * 3 .

T h es a m eh o l d st r u ef o rs t r i n g s :

I F Y $ = A $T H E N1 0 0 0

i s f a s t e rt h a n :

I F Y $= " Y E S "T H E N1 0 0 0

T h e s ev a r i a b l e ss h o u l db ea s s i g n e dn e a rt h et o p

o f t h e p r o g r a m ,b e c a u s et h e c o m p u t e ra c c e s s

v a r i a b l e si n t h eo r d e ri n w h i c ht h e yw e r ed e f i n e

P l a c i n gd e f i n i t i o n s a tt h e t o p( o ri n a s u b r o u t

t h a t ' s c a l l e df r o mt h et o p )e n s u r e st h a tB A S I Cw i l l

f i n dt h ev a r i a b l em o r eq u i c k l y .

G a r b a g eC o l l e c t i o n

M a n yp r o g r a m sw r i t t e nf o ro t h e rc o m p u t

1 0 r e m : c o m p a c t e de x a m p l eo f p e r f e c t h o s t e s sp r o g r a m :

2 0 p r i n t M w o u l d e i t h e r o fy o u l i k e m o r ec o f f e e ? 1 1 :

i n p u f ' h u s b a n d ' sr e s p o n s e 1 1; h $ : i n p u t M w i f e ' s r e s p o n s e " ; w $

9 0 i f h $ = w y e s H o r w $ = M y e s " t h e n p r i n t l l i f l lm a k es o m et h e n 1 1

F i g .1 0 - 9 .A n e x a m p l eo f a c o m p a c t e dp r o g r a m .

1 2 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 137/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 138/194

C h a p t e r1 1

P r o f e s s i o n a lD e s i g n :E r r o rT r a p p i n g

C r a s hi s t h em o s td r e a d e dw o r di n p r o g r a mo p e r a

t i o n .F o ru s e r si t m e a n st h a ta p r o g r a mh a sh a l t e d

a b r u p t l y ,a n dt h e r e ' sn o e a s yw a yt o g e tb a c ki n .

C r a s h e s c a nc o m ea b o u ti n h u n d r e d so f d i f f e r e n t

w a y s .F r o mi m p r o p e rk e y b o a r de n t r yt o r e a de rr o r so n b a dd i s k s ,t h e y ' r ea p a r to f l i f e .F o r t u n a t e l y ,

t h eC - 1 2 8i s w e l l - e q u i p p e dt o i n t e r c e p t e r r o r sb e

f o r et h e yb e c o m ea p r o b l e mf o ry o u rp r o g r a m sa n d

t h e i ru s e r s .

W e ' v ea l r e a d ys e e nh o wc e r t a i n e r r o r sc a nb e

t r a p p e db e f o r et h e y ' r e a b l et o d oa n yd a m a g e . F o r

e x a m p l e ,i n t h es e c t i o no n i n p u t ,y o uw e r es h o w n

h o wt o s c r e e ne a c hc h a r a c t e r a si t i s e n t e r e df r o m

t h e k e y b o a r d ,e l i m i n a t i n gc h a r a c t e r st h a tc o u l d

c a u s ep r o b l e m si n f i l er o u t i n e so r t h a ts h o u l dn o t

b e a c c e p t e db y t h ep r o g r a m .

T h i si s t h em o s ti m p o r t a n tf o r mo f e r r o rt r a p

p i n g .I f y o u rp r o g r a m sa r eg a l v a n i z e dw i t hr o u t i n e s

t h a tr e j e c ti m p r o p e re n t r y ,a n di f t h e ya n t i c i p a t e

t h eu s e r ' sq u e s t i o n sa n dm i s t a k e s , y o u ' r e9 0 p e r

c e n to f t h ew a yt o w a r de l i m i n a t i n ga l lc r a s h e s .

T h e r ea r e ,h o w e v e r ,c e r t a i nt y p e so f e r r o r st h a t

k e y b o a r ds c r e e n i n ga n di n s t r u c t i o n s t ot h eu s e r

s i m p l y c a n n o t t a k ec a r eo f .W h a th a p p e n si f t h e

o p e r a t o r p r e s s e s t h eR U N / S T O Pk e y( w h i c hh a l t

t h ep r o g r a m )i n t h em i d d l eo f a c r u c i a ls e c t i o n

t h ep r o g r a m ?W h a th a p p e n si f t h eu s e ro p e n st h e

d r i v ed o o r ?O rr e m o v e st h e d i s kw h e nt h ep r o g r ai s t r y i n gt o r e a di n f o r m a t i o nf r o ma f i l e ?

H o wd oy o uh a n d l e? S Y N T A X E R R Oi n t h e

p r o g r a m ?S h o u l do p e r a t i o nc o m et o a h a l t ?O r

s h o u l dy o ui n d i c a t et h el i n en u m b e ro f t h ee r r o r

d i s p l a yi t , a n dt h e nr e t u r nt o t h em a i nm e n u ?

T h ea n s w e ri s t h a ty o u w a n tt o s c r e e na l l o f

t h e s ep o s s i b l e e r r o r sa n ds t i l la l l o wt h eu s e rt h e

g r e a t e s tf r e e d o mi n o p e r a t i o ny o u rs o f t w a r e .T h e

T R A Pc o m m a n dp r o v i d e s t h em e a n s b yw h i c hy o u

c a na c c o m p l i s ht h e s eg o a l s .

T H ET R A PC O M M A N D

T h es t a r to f t h es h o wi s t h eC o m m o d o r e1 2 8 '

T R A Pc o m m a n d ,w h i c hi n s t r u c t st h ec o m p u t e t o

t r a pa n ye r r o re n c o u n t e r e d .W h e nT R A Pi s a c t i v e

a n dt h ec o m p u t e re n c o u n t e r sa n e r r o r ,t h eo p e r a

t i o nj u m p st o a s p e c i f i cr o u t i n ed e s i g n e de x p r e s s

1 2 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 139/194

t o h a n d l ee r r o r s .

1 0 T R A P3 0 0 0 : R E MG O T O3 0 0 0

A N Y T I M ET H E R E ' SA NE R R O R

2 0 P R I N T " H E L L O "

3 0 :

4 0 : R E MR E S TO F

P R O G R A M5 0 :

3 0 0 0 : R E ME R R O RT R A P P I N GR O U T I N E

3 0 5 0P R I N T" Y O UH A V EA NE R R O R "3 0 6 0G E T K E Y A $

3 0 7 0R E S U M E

T h eT R A Pc o m m a n dw o r k su n d e ra p r i n c i p l e

t h a tp r o g r a m m e r sr e f e r t oa s e v e n tt r a p p i n g .I t

w o r k sl i k et h i s :i n s t e a d o f t e s t i n gf o rs o m e t h i n ga t

e a c hl i n e ,y o uc a n s i m p l ys a y , " I ft h e r ei s a n e r r o r

a n y w h e r ei n t h ep r o g r a m ,g o t o t h er o u t i n ea t l i n e

3 0 0 0 a n dp e r f o r mt h er o u t i n e s t h e r e . "

O t h e rB A S I Cc o m m a n d sa r eu s e di n c o n j u n c

t i o nw i t hT R A P .T h e r ea r ec o m m a n d st o d e t e r m i n e

w h a te r r o rw a se n c o u n t e r e da n dt o c o n t i n u eo p e r

a t i o nw h e r et h ep r o g r a ml e f t o f f ,a n dt h e r e ' se v e n

a c o m m a n dt o r e s u m eo p e r a t i o na t a c o m p l e t e l y

d i f f e r e n t p l a c e .I f y o ud o i t r i g h t ,y o uc a nh a n d l e

a n ye r r o rw i t h o u tm i s s i n ga b e a t .

B e c a u s et h eC o m m o d o r e1 2 8a l w a y s" k n o w s "w h a te r r o rh a so c c u r r e d( t h r o u g hs p e c i a lv a r i a b l e s

t h a tc o n t a i ne r r o rc o d e s ) ,i t i s p o s s i b l et o p r o v i d e

v e r ys p e c i f i cm e s s a g e sa b o u tw h a te r r o rw a s

t r a p p e da n dt o d i s p l a y f u r t h e ri n s t r u c t i o n s .Y o u r

p r o g r a m sc a nb e m a d et o s e e mi n t e l l i g e n t ,t o s a y

t h i n g sl i k e" P l e a s er e i n s e r ty o u rd i s k ! "

I n a d d i t i o nt o t h em e s s a g ey o ud e v i s ey o u r s e l f ,

t h eC o m m o d o r e1 2 8a l s oh a st w os p e c i a l s t r i n gv a r i

a b l e s ,E R R $a n dD S $ ,w h i c hd i s p l a ym e s s a g e sf o r

t h e e r r o re n c o u n t e r e d .

I n t h ep r e v i o u se x a m p l e ,y o us a wh o wT R A Pi s u s e dt o i n t e r c e p te r r o r s .H e r e ' sa n o t h e re x a m

p l e ,u s i n gt h eE R R $ ( x )f u n c t i o n( l i n e3 0 6 0 )t o d i s

p l a ya n e r r o rd e s c r i p t i o n :

1 0 T R A P3 0 0 0 : R E MG O T O3 0 0 0

A N Y T I M ET H E R E ' SA NE R R O R2 0 P R I N T " H E L L O "

3 0 :

4 0 : R E MR E S T O FP R O G R A

5 0 :

3 0 0 0 : R E ME R R O RT R A P P I

R O U T I N E

3 0 5 0P R I N T" Y O UH A V E A NE R R O R

3 0 6 0P R I N TE R R $ ( E R ): R E MS H O WE R R O RM S G

3 0 6 5G E T K E Y A $

3 0 7 0R E S U M E

L e t ' st a k e am o m e n tt o e x a m i n et h i sp r o g r a m

F i r s t ,a t l i n e1 0 w e ' v ei n s t r u c t e dt h ec o m p u t e rt o

b r a n c ht o t h ee r r o r r o u t i n ea n y t i m e ap r o b l e mi s

e n c o u n t e r e d .W eh a v ea l s oi n c l u d e da n e r r o rr o u

t i n et h a tn o to n l yt r a p s t h ee r r o r ,b u tp r i n t sw h a t

i s w r o n g .A f t e rp a u s i n gf o rt h eu s e r ' si n p u t ,t h i s

r o u t i n er e s u m e sw i t hc u r r e n tp r o g r a mo p e r a t i o N om a t t e rw h a tt y p eo f e r r o rt r a p p i n g y o

d o i n g ,y o u ' l lp r o b a b l yu s eR E S U M Ea t l e a s to n c e

i n y o u re r r o rr o u t i n e .Y o uc a nt e l lt h ec o m p u t e rt o

r e s u m ea t a s p e c i f i cl i n en u m b e r :

R E S U M E1 9 7 0

o r e v e nt o r e s u m eo p e r a t i o na t t h es t a t e m e n tf o l

l o w i n gt h eo n et h a tc a u s e dt h eo r i g i n a l e r r o r

T h ee r r o rm e s s a g ed i s p l a y e dw h e ny o u ' v e

d i v i d e da n u m b e rb y a z e r ov a l u ew o u l dl o o kl i k e

t h i s :

D I V I S I O NB YZ E R O

I t ' sp r e t t ys t r a i g h t f o r w a r d ,e v e ni f i t d o e sl e a v ea

l i t t l et o t h e i m a g i n a t i o n .D i s ke r r o rm e s s a g e sa r e

s o m e w h a tm o r em y s t e r i o u s :

6 2 , F I L EN O TF O U N D , 0 0 , 0 0

T h es t a t e m e n tt e l l sa s t o r y ,a s l o n g a s y o

g o o da t r e a d i n gc o d e ;D i s ke r r o rn u m b e r6 2 h a s

o c c u r r e d — t h ef i l ew a sn o tf o u n d .T o a b e g i n n i

u s e r ,t h i sm e s s a g ec a nb e q u i t ec o n f u s i n g .W h y

w a s n ' tt h ef i l ef o u n d ?W h a ts h o u l dt h eo p e r a t o rd o ?

F o r t u n a t e l y ,t h e r e ' sa s i m p l es o l u t i o n ,b e c a u s ey o u

c a ns t r i pt h e s ec o d e sa w a ya n de l a b o r a t ef u r t h e r

1 2 9

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 140/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 141/194

N u m b e r

1

2

5

67

1 0

1 2

1 3

1 6

1 8

1 9

2 0

2 2

3 5

M e s s a g e

T O O M A N YF I L E S

F I L EO P E N

D E V I C EN O T P R E S E N T

N O TA NI N P U TF I L E

N O TA N O U T P U TF I L E

N E X T W I T H O U TF O R

R E T U R N W I T H O U TG O S U B

O U TO F D A T A

O U TO F M E M O R Y

B A DS U B S C R I P T

R E D I M ' DA R R A Y

D I V I S I O NB YZ E R O

T Y P EM I S M A T C H

N OG R A P H I C SA R E A

A c t i o n

C l o s eo n eo f t h ef i l e s i ny o u rp r o g r a m .

C l o s ef i l e sa f t e ru s i n gt h e m ;u s ea d i f f e r e n tf i l e# .

T u r no n p r i n t e r ,d i s kd r i v e ,e t c .b e c e r t a i ni t i s p r o p e r l y

c o n n e c t e d .

C h a n g et h eO P E Ns t a t e m e n tt o a l l o wa r e a d .

C h a n g et h eO P E Ns t a t e m e n tt o a l l o wa w r i t e .

T r a c et h r o u g hl o g i co f y o u r p r o g r a m ;l o o kf o rG O T O

b r a n c h e s .

B e c e r t a i ny o u rp r o g r a mf i n i s h e sw i t ha n E N Di f t h e r e

a r e s u b r o u t i n e sb e l o wt h em a i ns e c t i o no f y o u rp r o g r a m .

C o u n t u pt h eD A T Ai t e m s — t h e ys h o u l dm a t c ht h en u m

b e ro f R E A D s .B es u r ea l ll i n e so f d a t aa r ep r e c e d e db y

a D A T As t a t e m e n t .A l w a y si s s u ea R E S T O R Eb e f o r e

r e r e a d i n gt h es a m ed a t ai n a p r o g r a m .

D e t e r m i n ea v a i l a b l em e m o r yu s i n gF R E ( O )a n dF R E ( 1 ) .

I F F R E ( O )i s l o w ,i s s u ea G R A P H I CC L Rc o m m a n d .I f

F R E ( 1 )i s l o w ,r e d u c et h es i z eo f d i m e n s i o n e dv a r i a b l e s .

A l s oc h e c kf o ra l a r g en u m b e ro f n e s t e dD O , F O R ,o r

G O S U Bs t a t e m e n t s .

D i m e n s i o nt h i sa r r a yt o a l a r g e rs i z e .R e m e m b e rt h a td o u

b l ed i m e n s i o n e da r r a y sc a n n o tb e r e f e r r e dt o a s s i n g l e

d i m e n s i o n e d ,a n dv i c e v e r s a .

D o n o td i m e n s i o na n a r r a ym o r et h a no n c e .

I f t h ed i v i s o ri sz e r o ,s k i pt h ef o r m u l ai n q u e s t i o n :I FA = 0

T H E NC = 0 : E L S EC = B / A .

C h e c kt h es t a t e m e n tc a r e f u l l y .B es u r et o u s eS T R $w h e n

c o n c a t e n a t i n gn u m b e r st o s t r i n gv a r i a b l e s .

I s s u ea G R A P H I Cc o m m a n db e f o r e u s i n gD R A W ,C I R

C L E ,o r B O X .Y o um u s t u s eG R A P H I C1 , 2 , 3 o r 4 f o r

t h i sc o m m a n d .

F i g .1 1 - 1 .T y p e so f e r r o r sa n dt h em o s td e s i r a b l er e s p o n s e s .

w a yo f t e s t i n gw h e t h e rt h ee r r o re n c o u n t e r e dw a s

a d i s ke r r o r o rs o m eo t h e rt y p eo f m i s h a p .

B e c a u s et h eT R A Pc o m m a n dd o e sn o ti n t e r

c e p tm o s td i s ke r r o r s ,y o u rp r o g r a m ss h o u l dt e s t

f o rd i s ke r r o r sa f t e re a c hr o u t i n e t h a ta c c e s s e s t h e

d i s k( r e a d ,w r i t e , d i r e c t o r y ,a n ds o o n ) .R e m e m b e r

t h a tm o s td i s ke r r o r sw o n ' ts t o py o u rp r o g r a ml i k e

o t h e rp r o g r a mp r o b l e m sw i l l( d i s ke r r o r ss i m p l y

c a u s et h ed r i v el i g h tt o f l a s h ) .T h e r e f o r e ,y o uc a n

t e s tf o ra d i s ke r r o ra t a n yt i m ea f t e ra d i s k - r e l a t e d

o p e r a t i o nh a sb e e np e r f o r m e d .

T h e r e ' sn o e n dt o t h et r e a t m e n t st h a tc o u l db e

a p p l i e dt o d i s ke r r o r s .Y o uc o u l dd i s p l a y i n s

t i o nm e s s a g e s o nt h es c r e e n ,a t t e m p tt o r e s a v ei n

f o r m a t i o n ,o r e v e nf o r m a t ad i s k — a l ld e p e n d i n o n

t h e t y p eo f e r r o r t h a th a so c c u r r e d .

P e r h a p st h es i m p l e s ta p p r o a c ht o d i s k e r r

i s t o s i m p l yd i s p l a ya s t a t e m e n ti n s t r u c t i n gt h eu s e r

t o c h e c kt h ed r i v e :

T H E R EI S A D I S KE R R O R !

P L E A S EC H E C KY O U RD R I V E

P R E S SR E T U R N T OC O N T I N U E

D e p e n d i n go n t h ep r o g r a m ' sd e s i g na n dp u r p o s

1 3 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 142/194

N u m b e r M e s s a g e

2 0 - 2 4 ,2 7

2 5

2 6

2 9

5 1

6 0

6 1

6 2

7 0

7 2

7 4

R E A D E R R O R

W R I T EE R R O R

W R I T EP R O T E C TO N

D I S KI D M I S M A T C HO V E R F L O WI N R E C O R D

W R I T EF I L EO P E N

F I L EN O T O P E N

F I L EN O TF O U N D

N OC H A N N E L

D I S KF U L L

D R I V EN O T R E A D Y

A c t i o n

R e t r yo r r e t u r nt o m e n u ,s w i t c ht o b a c k u pd i s k s .

R e t r yw r i t e ;r e w r i t ef i l eo n a d i f f e r e n t d i s k .

D i s p l a ym e s s a g et o r e m o v ew r i t e p r o t e c tt a b .

R e t r yo r i n i t i a l i z e( H e a d e r )d i s k .

S c r e e nt h e i n f o r m a t i o nb e i n g e n t e r e d( t oa v o i ds t r i n g s t h a t

a r e t o ol o n g ) ;o r e x p a n ds i z eo f r e c o r d sa n dr e c r e a t ef i l e .

D C L O S Et h ef i l ea n d o p e ni t f o rr e a d i n g .

C h e c kt h a tt h ef i l eh a sn o tb e e nc l o s e db e f o r et h i so p e r

a t i o n ;b e s u r et h ef i l ew a so p e n e dt o b e g i nw i t h .

R e s p e c i f yt h ef i l en a m e ;s w i t c hd i s k s .

C l o s ea f i l et e m p o r a r i l y .

C l o s ea l lf i l e s .T h e nS C R A T C Ho b s o l e t ep r o g r a m sa n d

d a t af i l e s ,o r H E A D E Ra n e wd i s k .

B e s u r ea d i s kh a sb e e ni n s e r t e da n dt h ed r i v el e v e ri s

c l o s e d .

F i g .1 1 - 2 .D i s ke r r o r sc o d e sa n dt h em o s td e s i r a b l er e s p o n s e s .

i t c a ne i t h e rr e r u nf r o mt h eb e g i n n i n go r r e s u m e

a t a c e n t r a lp r o g r a mr o u t i n e( s u c ha s a m a i nm e n u ) .

H o wt o T e l lW h e na n E r r o rO c c u r s

T h ed i s k e r r o rv a r i a b l eD Sc a nt e l ly o uw h e t h e r

o r n o ta d i s k e r r o rh a so c c u r r e d ,a n di f s o ,w h a tt y p eo f e r r o ri t w a s .W h e n D Si s g r e a t e rt h a no r e q u a l

t o 2 0 ,y o uc a nb e a s s u r e dt h a ts o m et y p eo f d i s k

e r r o rh a so c c u r r e d .

S e v e r a ld i s ke r r o r sa r ea l s or e f l e c t e di n t h eE R

v a r i a b l ea s w e l l ,s o i t ' sp o s s i b l et o h a v ea f i l e - n o t -

f o u n de r r o rt h a tc a nb e i n t e r c e p t e db yb o t hD Sa n d

E R .( I n c l u d eD Si n y o u rp r o g r a m so n l yw h e ny o u

k n o wt h e r ew i l lb e a d i s kd r i v ec o n n e c t e d ,b y t h e

w a y ,o r y o u ' l lg e ta n e r r o r t r y i n gt o u s et h i sv a r i a

b l e .I t ' sd i r e c t l yd e p e n d e n to n t h e d i s kd r i v e . )

O fc o u r s e ,t h em o r es p e c i f i cy o u rt e s t i n g f o re r

r o r s ,t h em o r ee f f i c i e n ta n dh e l p f u lt h ep r o g r a mc a n

b e .F o re x a m p l e ,i f t h ew r i t e - p r o t e c tn o t c hi s c o

v e r e d ,y o um i g h tw a n tt o d i s p l a ya s e p a r a t e

m e s s a g e :

P L E A S ER E M O V ET H EW R I T E - P R O T E C T

L A B E LF R O M Y O U RD I S K .

T H E NR E I N S E R TT H ED I S K

A N DP R E S SR E T U R N .

I f t h ed i s kw e r ef u l l ,y o uc o u l d p r o v i d es p e c i a

i n s t r u c t i o n s f o rf o r m a t t i n ga n e wd i s k .Y o um i g h

e v e nw a n tt o i n c l u d ea s p e c i a lr o u t i n et h a tw o u l

h e a d e rt h ed i s k — o fc o u r s ef i r s tc h e c k i n gt h a tt h e

d i s kd i dn o tc o n t a i n i n f o r m a t i o n .T h i sc o u l db ed o n e

b y u s i n gy e ta n o t h e re r r o rc o d e ,w h i c h w o u lt r y

t o o p e na t e m p o r a r yf i l et o s e ei f t h ed i s kh a db e e n

f o r m a t t e d( i f i th a s n ' tb e e n ,y o u ' l lg e ta d r i v e - n o

r e a d ye r r o ra n dt h ep r o g r a mc a np r o c e e d w i t f o r

m a t t i n g . )

T h eb e s ts o l u t i o nf o re r r o rh a n d l i n gi s a s e t o f

s u b r o u t i n e sw h i c hg r o u p se r r o r sa c c o r d i n gt o t h e i r

t y p ea n ds e v e r i t y .F i g u r e1 1 - 3s h o w sa c o m p l e

e r r o r - t r a p p i n gr o u t i n e .

T E S T I N GF O RR U N / S T O P

T h e r e ' sn o t h i n gw o r s et h a nh a v i n ga p r o g r a

s t o pb e c a u s ey o u ' v ea c c i d e n t a l l yh i tt h ew r o n gk e y .

A n dR U N / S T O Pi s e a s yt o h i t ,n e s t l e da s i t i s n e x t

t o C O N T R O L ,S H I F TL O C Ka n dS H I F T .

N o r m a l l y ,a n y t i m et h e R U N / S T O Pk e yi s

1 3 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 143/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 144/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 145/194

r e s t a r ta p r o g r a m( w i t hR U No r C O N T )o n c e

t h e y ' v es t o p p e di t u s i n gR U N / S T O P .

P r e s s i n gR U N / S T O Pa c t u a l l yc a u s e sa n e r r o r ,

w h i c hi n t u r nc r e a t e sa n e r r o rc o d e — s of a r ,s og o o d

. . . B u tt o t r a pt h eR U N / S T O Pk e ys u c c e s s f u l l y

r e q u i r e ss p e c i a la t t e n t i o n t op r o g r a ms t r u c t u r e ,b e

c a u s et h eC - 1 2 8 c a ne a s i l yb e c o m ec o n f u s e do ns u c

c e s s i v eR U N / S T O Pa t t e m p t s .F i r s t ,t h e T R A P

s t a t e m e n ta n dT R A Pr o u t i n es h o u l db e t h ev e r y

f i r s ti t e m si n y o u r p r o g r a m :

1 0 T R A P1 2

1 1 G O T O2 0 : R E MS T A R TO F P R OG R A M

1 2 T R A P1 2 : R E ME R R O RT R A P P I N G

1 4 I F E R< > 3 0 T H E NG O S U B4 5 0 0 0:

R E MD OT E S T SA N DD I S P L A Y S

1 6 R E S U M E : R E S U M E

1 8 :

2 0 : : R E MS T A R TO FP R O G R A M

Y o u ' r er i g h ti f y o ut h i n kt h i sl o o k sa l i t t l ec o n

v o l u t e da n du n s t r u c t u r e d .B u ti t w o r k s .A n di t ' st h e

o n l yw a yt o b e a s s u r e do f t r a p p i n g t h eR U N / S T O P

k e ys u c c e s s f u l l y .T ou n d e r s t a n db e t t e rw h a t ' sg o

i n go n i n t h i ss p a g h e t t i - p l a t ea p p r o a c ht o p r o g r a m

m i n g ,l e t ' st a k ea l o o ka t e a c hs t e p .

L i n e 1 0i s e a s y ;i t t e l l st h ec o m p u t e rt o g o t o

l i n e1 2 a n y t i m ea n e r r o ri s e n c o u n t e r e d .T h en e x t

s t a t e m e n t ,l i n e1 1 ,b r a n c h e sa r o u n dt h e e r r o rr o u

t i n e( f r o ml i n e s1 2 - 1 8 )a n dg o e sd i r e c t l y t ot h em a i n

b o d yo f t h ep r o g r a m .

W h y ,y o ua s k ,s h o u l dt h e e r r o rr o u t i n eb e

t h r o w nr i g h ti n t h em i d d l eo f e v e r y t h i n g ?W h yn o t

p l a c ei t a t t h eb o t t o mo f t h ep r o g r a m ?A n d w h yi s

t h e r ea T R A P1 2 s t a t e m e n ta t l i n e1 2 ,t h ev e r yl i n e

t h a tb e g i n st h ee r r o rr o u t i n e ?A l la r eg o o dq u e s

t i o n s ,t o b e s u r e .T h ea n s w e rl i e sl o c k e di n t h ew a y

B A S I Ct r a p se r r o r s ,a n di n t h ew a yi t f a i l st o t r a p

t h eR U N / S T O Pk e yu n d e rc e r t a i nc i r c u m s t a n c e s .T h ef i r s tt h i n gt o r e a l i z ei s t h a tB A S I Cd o e s n ' t

a u t o m a t i c a l l yk n o ww h e r ee v e r yp r o g r a ml i n ei s l o

c a t e d .E a c ht i m ea n e r r o ri s e n c o u n t e r e d ,a n dt h e

c o m p u t e r m u s tr e d i r e c ti t s e l ft o a n e r r o r - h a n d l i n g

r o u t i n e ,B A S I Cg o e st o t h ev e r yt o po ft h ep r o g r a m

a n db e g i n ss c a n n i n gd o w n w a r df o rt h e a p p r o p r i a t e

l i n e .S oi f t h e r ea r eo n e - t h o u s a n dl i n e sb e t w e e nt h e

t o po f t h ep r o g r a m a n dt h ee r r o rt r a p p i n gr o u t i n e

B A S I Cw i l lc h e c ke a c ho ft h o s el i n e sb e f o r eh i t t i n g

t h ee r r o r r o u t i n ea n de x e c u t i n gi t .

I n t h eb r i e fn a n o s e c o n d sB A S I Ci s t r a c k i n

d o w nt h e e r r o rr o u t i n e ,e r r o rt r a p p i n gi t s e l fi s

t u r n e do f f .T h es h i e l d sa r ed o w n .T h ef o r c ef i e l d

i s o u to f o r d e r .

S o w h a th a p p e n si f t h e R U N / S T O Pk e yi s

p r e s s e dw h i l eB A S I Ci s h u n t i n gd o w nt h ee r r o rr o u

t i n e ' sl i n en u m b e r ?Q u i t es i m p l y ,t h e p r o g r a

c r a s h e st o a h a l t .T h i sk i n do f c r a s hi s q u i t e p o s

b l ei n a l a r g ep r o g r a mi n w h i c ht h ee r r o rt r a p p i n

r o u t i n eh a sb e e np l a c e da t t h eb o t t o m ,b e c a u s

t h e r ei s a s i g n i f i c a n tl a gt i m ew h i l e t h ec o m p u t

l o o k sf o r t h a t r o u t i n e .

N o w ,t h i n kt h r o u g ht h es a m es c e n a r i ow i t he r

r o rt r a p p i n gp l a c e da t t h et o p .B A S I Cg o e sr i g h t

t o t h e t o po f t h ep r o g r a m ,a n de n c o u n t e r st h e e rr o r t r a p p i n gr o u t i n eo n t h e s e c o n dl i n ei t h i t s !

T h e r e ' sv i r t u a l l yn ot i m e t a k e ni n s e a r c h i n gf o rt h e

e r r o rt r a p p i n gr o u t i n e ,a n d h e n c ei t ' sa l m o s ti m p o s

s i b l et o b r e a ko u to ft h ep r o g r a mw i t hR U N / S T O

T h eT R A P1 2 s t a t e m e n ti n l i n e1 2 m a ys e e m

a l i t t l er e d u n d a n t ,b u ti t s e r v e s t h es a m ep u r p o s e

C o m m o d o r e ' sm a n u a l st e l lu s t h e r e ' sn ow a yt o t r a p

a ne r r o rw i t h i n t h ee r r o rt r a p p i n gr o u t i n ei t s e l f .B u t

a c t u a l l yt h e r ei s a w a y :y o uc a nm a k et h ee r r o r t r a

p i n gr o u t i n et u r ni t s e l fo n .I t ' sw h yt h e r e a r et w o

R E S U M E Sa t t h ee n do f t h er o u t i n e — o n ei s t o t a k e

c a r eo f t h eT R A P1 2 s t a t e m e n ta t t h eb e g i n n i

o f t h er o u t i n e .

J u s tb e c a u s et h eb e g i n n i n go f t h er o u t i n ei s a t

t h e t o pd o e s n ' tm e a ny o uh a v et o c l u t t e ru pt h eb e

g i n n i n g so f y o u rp r o g r a m sw i t ha l lt h ee r r o rt e s t s

a n d r e s p o n s e sp o s s i b l e .S i n c ee r r o r3 0 ( R U N

/ S T O P )i s t h eo n l yo n et h a t ' st i m ed e p e n d e n t ,w e

c a ns a f e l yt u c kt h er e s to f t h ee r r o r d e c i s i o nm a k

i n gd o w na t t h eb o t t o m .

A F I N A LN O T EO NE R R O R S

T h eb e s tw a yt o a p p r o a c he r r o r si s t o t r e a t

t h e ma s a l l i e s .E r r o r c o d e sc a na l e r ty o ut o t r o u

b l e so n c o n d i t i o n s t h a ty o u ' dn e v e ro t h e r w i s eb e

a b l et o t e s tf o ro r s c r e e no u t .U s e dc r e a t i v e l y ,t h e y

c a nh e l py o uc a t c hm i s t a k e sb e f o r et h e yh a v ea

c h a n c et o d o a n yr e a ld a m a g e .

1 3 5

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 146/194

C h a p t e r1 2

D r a w i n gP i c t u r e s

M a k ef r i e n d sw i t ha p i e c eo f g r a p hp a p e r .G r a p h

p a p e ri s t h e o n l yv e h i c l et h r o u g hw h i c hy o uc a ne a s

i l yu n d e r s t a n dg r a p h i c so n t h eC o m m o d o r e1 2 8 .

W i t ha g r i dt o l o o ka t , g r a p h i c sl a y o u tb e c o m e sa

b r e e z e .W i t h o u ta g r i d ,i t ' sa n i g h t m a r e .

L e t ' ss t a r t r i g h t o f fb y d r a w i n g s o m e t h i n gu s

i n gg r a p h i c ss c r e e no n e .

A S I M P L EG R A P H I C SP R O G R A M

W h i l et h eC - 1 2 8o f f e r ss e v e r a ld i f f e r e n tg r a p h i c

m o d e s ,t h eo n ew e ' l lu s em o s ti n o u re x a m p l e si s

g r a p h i c s c r e e no n e ,w h i c hi s f o rs t a n d a r db i t - m a p

g r a p h i c s .I n t h i sm o d e ,p i c t u r e sa r el i m i t e dt o o n e

f o r e g r o u n da n d o n e b a c k g r o u n dc o l o rf o r e a c h

s q t c a r eo n t h e s c r e e n .Y o uc a nu s ea l l 1 6 C - 1 2 8

c o l o r so n g r a p h i c ss c r e e no n e ,b u tt h ed i f f e r e n t

c o l o r sc a n n o to v e r l a p w i t h i na s i n g l es q u a r e .T h e

a d v a n t a g eo f t h i ss t a n d a r d b i t - m a pm o d ei s t h a tt h e

p i c t u r e sc a nb e m u c hm o r ed e t a i l e d .I n t h eC - 1 2 8 ' s

o t h e rm o d e ,t h em u l t i c o l o rb i t - m a pm o d e ,y o u rp i c

t u r e sc a n s q u e e z em o r ec o l o r s i n t os m a l l s p a c e s ,

b u tt h ed e t a i li s s o m e w h a tc o m p r o m i s e d .

G r a p h i c ss c r e e no n ei s w e l ls u i t e df o rp i c t u r

o f b a l l o o n s ,o r o t h e rl a r g e - i t e mp o r t r a i t s .T h em u l

t i c o l o rm o d ei s b e s tf o r c o l o r f u ls c e n e s ,s u c ha s t h

c h e r r yb l o s s o m si n a p a r ka t s p r i n g t i m e .

O u rp r o g r a m ,l i s t e d i nF i g .1 2 - 1 ,d r a w sa p i c

t u r eo f a f l o p p yd i s k .T h ep i c t u r eh a ss o m ea r t i s t i

w e a k n e s s e s ,w h i c hw e ' l lf i xi n a m o m e n t .T h em a i n

t h i n gi s t h a ti t i l l u s t r a t e st h eu s eo f s e v e r a li m p o

t a n tg r a p h i c sc o m m a n d s .

T h ef i r s tc o m m a n di s a t l i n e t e n .G R A P H I 1 , 1

s e l e c t sg r a p h i c s c r e e no n ea n dc l e a r st h a ts c r e e

o f a n yp r e v i o u sp i c t u r e s .T h ec o m m a n dG R A P H

1 , 0w o u l ds e l e c tt h es c r e e n ,b u tl e a v ea n yp r e v i

o u sd r a w i n g si n t a c t .

T h eG R A P H I Cc o m m a n da l s od o e ss o m e t h i

t h a ti s i n v i s i b l e ,b u tc o u l da f f e c ty o u rp r o g r a mi f

t h e y ' r ev e r y ,v e r yl a r g e .T h ef i r s tt i m ei t ' su s e d

G R A P H I Ch e l p si t s e l ft o a 9 Kc h u n ko f y o u rp r o

g r a mm e m o r y .N o r m a l l yt h i si s n ' ta p r o b l e m ,s i n c

y o u r c o d ew o u l d h a v et o t a k eu p a t l e a s t4 8 Kt o

c r e a t ea n yk i n do fg r a p h i c sc o n f l i c t .I t i s ,h o w e v e

a g o o di d e at o i s s u et h eG R A P H I C C L Rc o m m a

1 3 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 147/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 148/194

a n o t h e r2 a t t h i sp l a c ei n t h es t a t e m e n t .

N o wR U Nt h ep r o g r a m . T h i sd i s kl o o k s b e t

t e r a l r e a d y ,b u t n o tq u i t ea s g o o da s i t w i l l .

L e t ' sf i n i s hu p w i t ht w oc o m m a n d s .O n ew i l l

f i l lt h ed i s kw i t hw h i t e ,a n dt h e o t h e rw i l la d da

c h a r a c t e r i s t i cw r i t e - e n a b l e n o t c h .

T h ef i r s tc o m m a n di s P A I N T :

7 6 P A I N T, 9 1 , 5 1

T h eP A I N Tc o m m a n df i l l st h eb o u n d a r i e so f

a n yf i g u r ew i t hc o l o r .T h ec o o r d i n a t eg i v e nf o r

P A I N Tm a yb e a n y w h e r ei n s i d et h ef i g u r e ,b u ti t

m a yn o tb e o na b o u n d a r yl i n ei t s e l f .I f t h ec o o r

d i n a t e& o na b o u n d a r yl i n eo f t h ef i g u r e ,P A I N T

w i l ls i m p l yn o tw o r k .

N o wt h a ty o u ' v e s e e nh o we a s i l yP A I N T

w o r k s ,h e r e ' sa n o t h e rs e c r e t :y o uc a ni n c l u d eap a i n tp a r a m e t e ri n t h eB O Xc o m m a n d .N o r m a l l y ,

a b o xi s d r a w nu n f i l l e d ,a s i t w a si n t h i sp r o g r a m ,

b u tb y i n c l u d i n ga n e x t r ap a r a m e t e ra t t h ee n do f

t h eB O Xc o m m a n dy o uc a nd r a wa s o l i db o xi n t h e

d e f a u l tc o l o r .Y o uc a ne v e nc r e a t e v o i d sb yc h a n g

i n gt o t h eb a c k g r o u n dc o l o ra n dt h e nd r a w i n g ab o x :

7 8 B O X0 , 1 5 5 , 6 0 , 1 6 0 , 6 3 , , l: R E MN O T C

T h e, , 1a t t h ee n di n s t r u c t st h ec o m p u t e rt o f i l lt h i s

b o xa s i t i s d r a w n .Y o uw i l ln o t i c et h a tt h es t a t e

m e n tb e g i n sw i t h a0 , w h i c hi n d i c a t e s t h a tt h eb o x

s h o u l db e d r a w ni n t h eb a c k g r o u n dc o l o ri n s t e a

o f i n t h ec u s t o m a r yf o r e g r o u n dc o l o r .F i g u r e1 2 - 3

s h o w sa l i s t i n go f t h ef i n i s h e dp r o g r a m , c o m

w i t ha n a d d i t i o n a l c o l o rc o m m a n da t l i n e5 .

W e ' l lb e m a k i n g s o m ef u r t h e re n h a n c e m

t o t h i sp r o g r a ml a t e ri n t h ec h a p t e r .

S O M EN O T E SA B O U T C O L O R

L e a r n i n ga b o u tt h eC - 1 2 8 ' sc o l o rs c h e m e sf o r

t h ef i r s tt i m ei s a c h o r ef o re v e nt h em o s ts e a s o n

p r o g r a m m e r .H e r e ' sw h y :t h e r e a r es e v e nd i f f e r

0 1 0 2 0 3 0 4 0

O i l | I I

1 0 -

2 0 -

3 0 -

4 0 -

5 0 -

6 3 -. 7 0 -

9 0 -

1 0 0 *

1 1 0 '

1 2 0 '

1 3 0 '

1 4 0 *'

1 5 0 >-

1 6 0 *>

1 7 0 »•

1 8 0 -•

1 9 0 -.

7 0 8 0

H - H -9 0 1 0 0 1 1 01 2 0 ( 1 3 01 4 0 1 5 0

i i i i ; i i i

9 0 , 5 0

1 6 01 7 01 8 0 1 9 02 0 02 1 0 2 2 02 3 0 2 4 02 5 0 2 6 02 7 0 2 8 0 2 9 0 3 0 0 3 1 0 3

! I I I I I I I I I 1 I I I i I

, 6 0 . 5 0

. 6 0I1 5 5 . 6 0I 1 6 0 . 6 0

1 5 5 . 6 3

1 2 2 . 1 1 6

1 6 0 . 6 3

9 0 . 1 2 0 1 6 0 , 1 2 0

F i g .1 2 - 2 .L a y o u tf o ra p i c t u r e o fa f l o p p yd i s k .

1 3 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 149/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 150/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 151/194

0 g o t o5

2 : d i s kp r o g r a m( 2 )

3 : b y m a r t i n h a r d e e

4 :

5 c o l o r1 , 2

1 0 g r a p h i c1 , 1

1 2 s c a l e1 , 3 2 0 , 2 5 01 5 :

2 0 d r a w, 9 0 , 5 0t o 1 6 0 , 5 0t o 1 6 0 , 6 0t o 1 5 5 , 6 0 t o 1 5 5 , 6 5t o 1 6 0 , 6 5

t o 1 6 0 , 1 2 0t o 9 0 , 1 2 0t o 9 0 , 5 0

2 5 :

3 0 c i r c l e , 1 2 5 , 8 5 , 1 0 , 9

3 5 :

4 0 c i r c l e, 1 2 5 , 9 9 , 2 , , 2 7 0 , 9 0

5 0 c i r c l e, 1 2 5 , 1 1 6 , 2 , , 9 0 , 2 7 05 5 :

6 0 d r a w, 1 2 7 , 9 9t o 1 2 7 , 1 1 6

7 0 d r a w, 1 2 2 , 1 1 6t o 1 2 2 , 9 9

7 5 :

7 6 p a i n t, 9 1 , 5 1

8 0 g e t k e y a $ : g r a p h i c0

F i g .1 2 - 4 .A ne v e nb e t t e rd i s kp r o g r a m .

0 g o t o5

2 : p o r t a b l ed i s k( 1 )

3 : b y m a r t i n h a r d e e

4 :

5 c o l o r1 , 2

1 0 g r a p h i c1 , 1

1 2 s c a l e 1 , 3 2 0 , 2 5 0

1 5 :

2 0 d r a w, 0 , 0t o 7 0 , 0t o 7 0 , 1 0 t o 6 5 , 1 0 t o 6 5 , 1 5 t o 7 0 , 1 5 t

t o 0 , 7 0t o 0 , 0

2 5 :

3 0 c i r c l e, 3 5 , 3 5 , 1 0 , 9

3 5 :

4 0 c i r c l e , 3 5 , 4 9 , 2 , , 2 7 0 , 9 0

5 0 c i r c l e, 3 5 , 6 6 , 2 , , 9 0 , 2 7 0

5 5 :

6 0 d r a w, 3 7 , 4 9t o 3 7 , 6 6

7 0 d r a w, 3 2 , 6 6t o 3 2 , 4 9

7 5 :

7 6 p a i n t, 2 , 2

7 7 c o l o r 1 , 1 2

8 0 g e t k e ya $ : g r a p h i c0

F i g .1 2 - 5 .A p r o g r a mt h a td r a w sd i s k sa n y w h e r e .

1 4 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 152/194

c t r = c t r + 1

c o l o r 1 , c t r

g o s u b1 0 0 0: r e md r a wd i s k

0 g o t o5

2 : p o r t a b l e d i s k( 2 )

3 : b y m a r t i n h a r d e e

4 :

5 c o l o r1 , 2 : r e m f o r e g r o u n dw h i t e

6 c o l o r0 , 1 : r e m b a c k g r o u n db l a c k

1 0 g r a p h i c1 , 1

1 2 s c a l e 1 , 3 2 0 , 2 5 0

1 5 :

1 7 c t r = 3: r e mf i r s t c o l o r

2 0 f o r x = 1 t o 2 4 5 s t e p8 0

2 5

3 0 : f o r y = 1 t o 1 7 5 s t e p8 0

3 5

3 7

3 8

4 0

4 55 0 : n e x t

5 5

6 0 n e x t

6 5

7 0 g e t k e y a $ : g r a p h i c0

7 5

8 0 e n d

8 5

8 6

1 0 0 0d r a w, x + 0 , y + 0t o x + 7 0 , y + 0 t o x + 7 0 , y + 1 0 t o x + 6 5 , y + 1

x + 6 5 , y + 1 5t o x + 7 0 , y + 1 5t o x + 7 0 , y + 7 0 t o x + 0 , y + 7 0 t o x +1 0 1 0:

1 0 2 0c i r c l e, x + 3 5 , y + 3 5 , 1 0 , 9

1 0 3 0:

1 0 4 0c i r c l e , x + 3 5 , y + 4 9 > 2 , , 2 7 0 , 9 0

1 0 5 0c i r c l e , x + 3 5 , y + 6 6 , 2 , , 9 0 , 2 7 0

1 0 6 0:

1 0 7 0d r a w, x + 3 7 , y + 4 9 t ox + 3 7 , y + 6 6

1 0 8 0d r a w, x + 3 2 , y + 6 6t o x + 3 2 , y + 4 9

1 0 9 0:

1 1 0 0p a i n t, x + 2 , y + 2

1 1 2 0r e t u r n

F i g .1 2 - 6 .A ni m p r o v e dp r o g r a mf o rr e l o c a t a b l ed i s k s .

t h e s ev a l u e s ,a s s h o w ni n F i g . 1 2 - 6 .I n t h i sl i s t i n g ,

t h ep i c t u r eh a sb e e nt u r n e di n t oa s u b r o u t i n e .F o r

p u r p o s e so f e x a m p l e , aF O R. . . N E X Tl o o ph a s

b e e n a d d e dt h a tw i l ld r a wa s e to f t w e l v ed i s k so n

t h es c r e e n .A C T Rv a r i a b l ei s i n c l u d e dt o c h a n g

t h ec o l o r o fe a c hd i s k .S i n c e t h ep r o g r a mi s w r i t

t e nf o rt h e b i t - m a pm o d e ,t h ed i s k sh a v eb e e ns e p a

r a t e ds l i g h t l ys o t h a tt h e c o l o r sf r o ma d j a c e n

1 4 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 153/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 154/194

C h a p t e r1 3

A n i m a t i o n

A n i m a t i o ni s o n eo f t h e b e s tt h i n g sg o i n go n t h e

C o m m o d o r e1 2 8 .C - 1 2 8B A S I Ch a sb e e nd e s i g n e d

t o h a n d l e a l m o s te v e r y t h i n g ,f r o md r a w i n gt h e

s h a p e st o b e a n i m a t e d ,t o m o v i n gt h e ma r o u n d ,t o

a c c o m m o d a t i n gc o l l i s i o n sa n dd i s p l a yc o n f l i c t s .I n t h i sc h a p t e r ,y o u ' l ls e eh o wt o c r e a t ea s i m

p l ev i d e og a m et h a ti n c l u d e sa s p a c e s h i p ,m o n s t e r s ,

m i s s i l e s t h a tc a nb e f i r e d ,a n de x p l o s i o n s .A l l o f

t h e s ee f f e c t sw i l lb e a c h i e v e dt h r o u g hc o m p u t e r

i z e d p i c t u r e sk n o w na s s p r i t e s .S p r i t e sa r e t h e c o r

n e r s t o n eo f c o m p u t e ra n i m a t i o n .

W H A TI S A S P R I T E ?

S p r i t e s a r ea v e r yn i m b l e ,m a n i p u l a b l e k i n do f

f i g u r e t h a tc a nb e d r a w nv e r yf a s t .S p r i t e sa r ea l s o

k n o w no n s o m ec o m p u t e r sa s s h a p et a b l e s .

T of u l l ya p p r e c i a t ew h a ts p r i t e s d o ,i t ' sn e c e s

s a r yt o t h i n ka b o u tw h a tl i f ew o u l d b el i k ew i t h o u t

t h e m .I n C h a p t e r1 2y o us a wh o wa p i c t u r eo fa d i s k

o r o t h e ro b j e c tc a nb e d r a w na t a n yp l a c eo n t h e

s c r e e n .W h a tw o u l di t t a k et o m o v et h ed i s ka r o u n d

t h e s c r e e n ?P r i m a r i l y ,y o u ' db e d r a w i n gt h e d i s k

o v e ra n do v e ra g a i na t a d j a c e n ts p o t s .B u tt o a v o i

s i m p l yc r e a t i n ga l o n gw h i t es t r e a ka c r o s st h e

s c r e e n ,e a c hn e wm o v eo f t h e d i s kw o u l dr e

q u i r e t h a ty o ua l s oe r a s e t h ea r e aw h e r et h e d i

h a dp r e v i o u s l yb e e nd i s p l a y e d .N o to n l yw o u l di tb e t e d i o u st o p r o g r a m ;i t w o u l db e s l o w ,b e c a u

t h e d i s kw o u l dh a v et o b e r e d r a w nw i t he a c hm o v e

I f y o ue v e rw a n t e dt o r e m o v et h e d i s kf r o mv i e w

y o u w o u l dh a v et o r e d r a wi t a t i t s l a s tp o s i t i o ni n

t h eb a c k g r o u n dc o l o r .

S p r i t e sd oa l lo f t h i sw o r kw i t ht w os i m p l ec o m

m a n d s . W h a t ' sm o r e ,o n c ey o u ' v es t a r t e da C - 1 2

s p r i t em o v i n g ,y o u rp r o g r a mc a np r o g r e s st o o t h e

t h i n g s .M o v e m e n ti s a u t o m a t i c , r e q u i r i n n o l o o p s

r e p o s i t i o n i n g ,o r o t h e rt r i c k s .

P e r h a p st h e b e s t t h i n ga b o u ts p r i t e si s t h a t

t h e y ' r es o e a s yt o c r e a t e .A p i c t u r e t h a tw o u l dr e

q u i r ef i f t e e no r t w e n t yl i n e so f c o d eu s i n gB O X

C I R C L Ea n dD R A Ws t a t e m e n t sc a nb e d e s i g n

a n ds a v e do nd i s ki n a m i n u t eo r t w ou s i n gt h eC o m

m o d o r e1 2 8 ' sv e r s a t i l es p r i t ed e f i n i t i o n( S P R D E

c o m m a n d .

T h i si s n o tt o s a yt h a tt h e r ea r e n ' ts o m ed r a w

1 4 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 155/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 156/194

b b b i

■ B B I

b b m

m m b

■ b b i

■ B M

■ B B

B B B

■ ■ M i

B B

B B

■ M B

B B

B B

B B I

■ ■ ■ ■

■ ■ ■ ■

■ I ^ H I

B B I

B B I

;̂ ■ B B

B B M B

■ H

M M M I

■ B B I

1

■ M M B B B

H ^ M M

;b b b M

■ ■ ■ M B

- b - - 1

■ M B *

b b b b 1

b b b b 1

1■ ■ ■ i i

I M B B i

M H M

■ ■ ■ M

H H B I

■ ■ ■

■ M M i

■ ^ ^

M B M i

■ ■ ■ ■

M M

H B B i

■ ■ ■ ■

m a

■ ■ M B

■ a m

■ ■ i

■ H H i

■ M M

■ ■ H

■ ■ ■ ■ 1

■ M m

M B

■ ■ 1

■ M B H

■ M i

M M I

M M M

M M M

M M M

M H M I

w a m m m

M B

■ ■ ■ ■

M M M ]

M B

■ M

■ B B B

B B B

M B

B H B B

B B B B

■ ■ ■ ■

B S M B

■ ■ ■ ■

M M M

■ M M i

M M

M M M

■ ■ M i

■ ■

■ I B M

B B I

■ ■ ■ ■

B ^ ^

M B B i

M B

■ B B S

B B I

M M M

M M M !

M M M

■ M

B B I

■ ■ ■ B l

B B B B

B B B B

B B B B

B H B H

B B B B

B B B H

B B B B

M M

M B )

B B B B

B B B B

B B B M

■ M M

M M B

■ B B I

■ B B B

■ B B i

B M

■ B B 1

M M M

M M M

M M M

M l

M M B I

M M M

M M M

B M M

B B I

M M B

M M B

B B B B

B B

■ M M

M M

^ M B I

B B B 1

■ M

B B B I

B B M

B B I

B B B I

■ B B i

■ ■ ■ I

■ B B J

■ ■ B

w m m

■ B B l

■ ■ ■

■ B B I

M B

■ M M J

M M

■ M M I

■ M M

M M B

■ B B

w m u m

F i g .1 3 - 1 .A s p a c e s h

s p r i t e .T h i ss p r i t em a yb e r e c r e a t e du s i n g

S P R D E F .

s h a p e st h a tw i l lb e u s e di n t h ev i d e og a m el a t e ri n

t h i sc h a p t e r .D r a wa n ds a v et h e ma c c o r d i n gt o t h e

f o l l o w i n gs t e p s :

1 . E n t e rS P R D E F .

2 . S e l e c t t h ec o r r e c ts p r i t en u m b e rf o r e a c h

s h a p e ,p l o ti t o u t ,a n ds a v ei t i n m e m o r yu s i n g

S H I F T - R E T U R N .T h e n ,c o n t i n u ew i t ht h e

n e x ts p r i t e .

3 . W h e na l lt h r e es h a p e sh a v eb e e nd e s i g n e da n d

s a v e di n m e m o r y ,e x i tS P R D E Fb y p r e s s i n g

R E T U R N .

4 . S a v et h es p r i t e sw i t ht h ec o m m a n d :

B S A V E" T E S TS P R I T E " , B 0 , P 3T O

P 4 0 9 6

B ec e r t a i n t h a ty o ud r a wa l lt h r e es p r i t e s ,a n d

t h a ty o us p e c i f y t h e c o r r e c tn u m b e r .A l s om a k

s u r ey o uh a v eB S A V E dt h es p r i t e su n d e p t h en a m

T E S TS P R I T E .T h i si s t h ef i l e n a m eu s e di n t h e

e x a m p l ep r o g r a m .

A N I M A T I N GS P R I T E S :A V I D E OG A M EE X A M P L E

T h ej o bo f d i s p l a y i n ga n da n i m a t i n gs p r i t

1 4 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 157/194

f a l l so n t h es h o u l d e r so f t w oc o m m a n d s :S P R I T E

a n dM O V S P R .

T h eS P R I T Ec o m m a n dt u r n so nt h ed i s p l a yo f

a s p r i t e ,s e t st h es p r i t e sc o l o r ,a n dd e t e r m i n e sw h a t

h a p p e n sw h e nt w os p r i t e s v i e f o rt h es a m es p o to n

t h es c r e e n .I t m a ya l s ob eu s e dt o e n l a r g ea s p r i t e ,

j u s ta s y o ud i dw i t ht h eX a n dY k e y sw h e nu s i n g

S P R D E F .

T h eM O V S P Rc o m m a n di s u s e dn o t o n l yt o

m o v ea s p r i t e ,b u ta l s o t o p o s i t i o ni t o n t h e s c r e e n .

T h e r ea r et w om o d e st o M O V S P R .I n o n em o d e ,

t h ec o m m a n di s u s e ds i m p l yt o p l a c ea n o n m o v i

s p r i t e a ta p a r t i c u l a rp l a c eo n t h es c r e e n .I n t h e

o t h e rm o d e ,M O V S P Ra c t u a l l ym o v e st h es p r i t ea t

a s p e c i f i e da n g l ea n ds p e e d .T h es p r i t ew o n ' ts t o p

m o v i n go r c h a n g ev e l o c i t yu n t i li t e n c o u n t e r sa n

o t h e rM O V S P Rc o m m a n d .W h e nu s i n gt h i ss e c o n

m o d e ,i t ' sg e n e r a l l ya g o o di d e at o p o s i t i o nt h e

s p r i t ea h e a do f t i m e w i t ht h ef i r s tM O V S P Rm o d e

I f y o ui s s u et h es e c o n dt y p eo f M O V S P Rw i t h o u

f i r s tp o s i t i o n i n gt h es p r i t e ,y o u ' l ln e v e rb e s u r ej u s t

w h e r et h es p r i t e w i l ls t a r ti t s p a t ho n t h es c r e e n

F i g .1 3 - 2 .A m i s s i l es p r i t e .

■ ■ M

■ ■ M i

■ H

— .

■ ■ ■ ■

■ m m

M M i

m m i

M M B

m m m m

H i

■ M B

■ H

m m m t

■ ■ ■ ■

m m b

m m b

m m b

— -

■ M m m .

■ M M I

■ ■ ■ ■

■ M B

M B H

■ ■ M B

M M B

m m m

m m m

m m m m

m m m m

v A

■ m m

m m .

■ ■ ■ ■

M S B

■ M M M V J

m m m

i J

m m m m

■ m m

m i m

M M B

■ ■ H f f B f f l

m m m m

■ ■ ■ ■ m I

■ ■ ■ H

■ ■ ■ ■ I

■ ■ ■ ■ ■

M M

■ ■ ■ ■ ■

• ■ M i

■ ■ - ■ ■ ■

m m m

■ ■ ■ ■ f f j

- ■ ■ ■ ■ I

■ « ■ ■ ■

m m m m

■ ■ B B

■ ■ ■ ■

■ ■ ■

■ ■ ■ ■ ■

- M H

■ ■ ■ « ■ ■

« « ■ ■ ■

■ ■ ■ ■ 1

■ ■ ■ ■

M M

■ ■ ■ ■ ■

m ^ m w

■ * * ■ ■ ■ ■

■ ■ ■ ■ ■

■ ■ ■ ■ ■

M M H

■ M

m m m m

■ ■ ■ ■ ■

■ ■ ■ B - l

■ M a s a i

^ ^ ^ ^

M M

■ M M

a a a a a i

a a a a a i

a a a a B i

m m m m

a a a a a i

m m m m

B B B B a i

■ H B M !

■ a a a a

■ ■ ■ ■ ■ 1

■ ■ H B

B M B B H

_

■ a a a a i

■ a n a *

a a a a a i

■ H U H

■ ■ ■ ■ ■

■ M l

M M i

M M B B M

a a a a a a

■ • M a

■ ■ ■ ■ ■

■ B M

■ M M M

B M a a i

■ ■ M a i

a i a a M

■ ■ ■ ■ ■

• ■ M M I

a a a a a :

M M I

■ ■ ■ ■

■ ■ ■ i

■ ■ ■ ■

■ ■ ■ ■

■ ■ ■

M M I

M M

1 4 7

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 158/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 159/194

d e g r e ea n g l e( l e f tt o r i g h t )a n da t t o p s p e e d :

M O V S P R3 , 9 0# 1 5

T h i sc o m m a n du s e st h e s e c o n dm o d eo f

M O V S P Ry o ur e a da b o u te a r l i e r .T h es p r i t ew i l l

k e e pm o v i n gu n t i ly o ui s s u ea n o t h e rM O V S P Rc o m m a n dw i t ha n a n g l eo f z e r o ,h i t R E S T O R E -

R U N / S T O P ,o r t u r no f ft h ec o m p u t e r .T h es p r i t e

w i l lk e e pm o v i n ga t t h es a m er e l a t i v ea n g l ee v e n

i f y o ur e p o s i t i o ni t o nt h es c r e e nw i t ht h ef i r s ts t a t i c

m o d eo f M O V S P R .

U n t a n g l i n gS p r i t eC o o r d i n a t e s

I t ' si m p o r t a n tt o n o t et h a ts p r i t e sw o r ku n d e r

a d i f f e r e n tX a n dY p l o t t i n gs c h e m et h a ny o u ' l lf i n d

o n a n yo f t h eC - 1 2 8 ' sg r a p h i cs c r e e n s .F o re x a m

p l e ,y o u ' d e x p e c tt h eu p p e rl e f tc o r n e rt o h a v e a

c o o r d i n a t eo f 0 , 0 — j u s tl i k eG R A P H I C1 , 2 , 3a n d4 .

I n s t e a d ,t h eu p p e rl e f ts p r i t ec o o r d i n a t ei s 2 4 , 5 0 .

Y o uc a np l o ta s p r i t e a t0 , 0 ;i t s i m p l y w o n ' ts h o w

u p .

T h el a s tv i s i b l ec o o r d i n a t ef o ra s p r i t e( l o w e r

r i g h tc o r n e r )i s 3 4 4 , 2 5 0 .

H e r e ' st h e g o o dn e w s :N o m a t t e rw h a t4 0 -

c o l u m no r g r a p h i c ss c r e e ny o u ' r eo n ,y o u rs p r i t e s

w i l ll o o kt h es a m ea n dw i l lm a i n t a i nt h es a m es c a l e .

T h eo n l yp l a c es p r i t e sd o n ' tw o r ki s t h e8 0 - c o l u m n

m o d e .

A P r o g r a m E x a m p l e

Y o u n o wk n o we n o u g ha b o u ts p r i t e st o w r i t e

y o u ro w ns i m p l e v i d e og a m e .W e ' l ls t a r tw i t h a p r o

g r a mt h a ts i m p l ym o v e ss p r i t e sa r o u n dt h e s c r e e n .

T h i sp r o g r a mw i l ld o t h ef o l l o w i n g :

• M o v ea s p a c e s h i pf r o ml e f tt o r i g h ta t t h eb o t

t o mo f t h es c r e e n .

• P u tt h em o n s t e ri n m o t i o n ,m o v i n gi n t h eo p p o

s i t ed i r e c t i o na c r o s s t h e t o po f t h es c r e e n .

• F i r ea m i s s i l ef r o mt h e s p a c e s h i pw h e nt h es p a c e

b a ri s p r e s s e d .

T h el i s t i n g i nF i g .1 3 - 4d o e sa l l o f t h i su s i n g

t h eS P R I T Ec o m m a n d sy o ua l r e a d yk n o w .L e t ' s

l o o ka t h o wi t w o r k s .

L i n e1 0 s t a r t so f fb y m a k i n gs u r e t h ec o r r e c

s p r i t e sa r ei n m e m o r y ;i t l o a d s t h eb i n a r yT E S T

S P R I T Ef i l e .A n ys p r i t e sc u r r e n t l yi n m e m o r yw i l l

b e o v e r w r i t t e nw i t ht h en e ws p r i t e sf r o mt h i sf i l e .

N e x t ,t h ep r o g r a ms w i t c h e st o G R A P H I 1 ,

s e t sa t e s t i n gv a r i a b l ef o ra s p a c e ,a n ds e t sa s p e e d

v a r i a b l ea t l i n e7 0 .T h ew a yt h ep r o g r a mi s d e

s i g n e d ,c h a n g i n gt h i sv a r i a b l ew i l lc o r r e s p o n d i

i n c r e a s e o rd e c r e a s et h es p e e do f t h e s h i pa n dt h e

m o n s t e r .

T h es t a t e m e n ta t l i n e8 0 u s e st h ef u l lc o m p l

m e n to f o p t i o n s a v a i l a b l ew i t ht h es p r i t ec o m m a n

S P R I T E1 , 1 , 2 , 0 , 1 , 1 , 0

I n E n g l i s h ,t h el i n er e a d s :

S e l e c t t h es p a c e s h i p( s p r i t e1 ) .

T u r ni t o n ( s oi t ' sv i s i b l e ) .

S e l e c tw h i t ea s t h ec o l o r( c o l o r2 ) .

G i v et h es p r i t et o pp r i o r i t y( i tw i l lo b s c u r eo t h e r

s p r i t e st h a tc o m ei n t oc o n t a c tw i t hi t ) .

E x p a n di t i n a h o r i z o n t a l d i r e c t i o n( X e x p a n

= 1 —o n ) .

E x p a n di t i n a v e r t i c a ld i r e c t i o n( Y e x p a n d=

1 - o n ) .

T h es p r i t ei s s t a n d a r d( 0 = n o tm u l t i c o l o r

I t ' s h e l p f u lt o w o r kw i t ht h e m a n u a lw h e n

y o u ' r e t y p i n gi n n e wS P R I T El i n e s ,s i n c et h e r e a r

s o m a n yp a r a m e t e r s .

L i n e9 0 p o s i t i o n st h es p a c e s h i pa t t h eb o t t o

o f t h e s c r e e n ,a n dl i n e1 1 0s t a r t si t m o v i n gf r o m

l e f tt o r i g h t .

L i n e s1 2 0 , 1 3 0 ,a n d1 4 0t u r no n t h em o n s t e

p o s i t i o ni t a t t h et o pp a r to f t h es c r e e n ,a n ds t a r t s

i t m o v i n gf r o mr i g h tt o l e f t .

L i n e1 5 0s t a r t sa s h o r t l o o pt h a t c o n t r o l sm o v e

m e n to f t h es p a c e s h i pa n dc h e c k st o s e ei f t h es p a c e

b a rw a sp r e s s e d .

Y o u ' r ep r o b a b l yw o n d e r i n gw h yy o u ' dn e e da

r o u t i n et o m o v et h es p a c e s h i p ,s i n c ea M O V S c o m m a n dp r e v i o u s l ys e ti t i n m o t i o n .I t c o m e sd o w n

t o t h i s :w h e nw e f i r et h em i s s i l e ,w e ' l lw a n ti t t o

c o m ed i r e c t l yf r o mt h en o s eo f t h e s p a c e s h i pU n

f o r t u n a t e l y , t h e r e ' sn o w a yt o k n o wt h ee x a c tp o -

1 4 9

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 160/194

2 :

3 : m e r c u r y ' s i n v a d e r s( 1 )

4 : b y m a r t i n h a r d e e

5 :

6 :

7 :

1 0 b l o a d" t e s ts p r i t e 1 13 0 g r a p h i c1 , 1

4 5 :

4 6 :

6 0 t $ * c h r $ ( 3 2 ): r e mf o r s p a c e b a r t e s t

7 0 a d j » 4 : r e ms p e e d

7 5 :

7 6 :

8 0 s p r i t e 1 , 1 , 2 , 0 , 1 , 1 , 0:

9 0 m o v s p r1 , 2 0 0 , 1 5 0 :

9 5 :

1 1 0 :

1 2 0 :

1 3 0 :

1 4 0 :

1 5 0 (

2 1 0 i

2 2 0 ,

2 4 0

2 6 0 ]

2 6 5

3 3 0 i

3 3 53 4 0

m o v s p r1 , 9 0# a d js p r i t e3 , 1 , 8 , 0 , 1 , 1

: m o v s p r3 , 2 0 0 , 6 0

: m o v s p r3 , 2 7 0# a d j

l o

: x = r s p p o s ( 1 , 0 ) : y =

: m o v s p r1 , x + a d j , y

: g e t a $ : i f a $ = t $

L o o p

* n d

: r e m s h o o t

3 5 0 s p r i t e 2 , 1 , 2 , 1 , 1 , 1:

3 6 0 m o v s p r 2 , x + a d j + 5 , y:

3 7 0 m o v s p r2 , 3 6 0# 1 5 :

4 0 0 r e t u r n

r e m *

r e m

: r e m: r e m

: r e m

: r e m

t u r no n s h i p

p o s i t i o n s h i p

s t a r t s h i pm o v i n gt u r n o n m o n s t e r

p o s i t i o n m o n s t e r

s t a r tm o n s t e r m o v i n g

r s p p o s ( 1 , 1 )

t h e n

r e m

r e m

r e m

g o s u b 3 4 0 : r e m f i r em i s s l e

t u r n o n m i s s l e

p o s i t i o n

l a u n c h

F i g .1 3 - 4 .T h eM e r c u r y1 p r o g r a m :a s i m p l ev i d e og a m e .

s i t i o no fa m o v i n gs p r i t e .T h eb e s tw ec a nd oi s k e e p

a l e a s ho n o u rm o v i n gs p a c e s h i pb y h a v i n gi t h o p

a c r o s st h es c r e e n .B e c a u s et h es p r i t ei s a l r e a d yi n

m o t i o n ,y o uw o n ' tn o t i c et h e s e h o p s ;t h es p r i t ei s

m o v i n ga t j u s tt h er i g h ts p e e dt o c a t c hu p t o t h e

n e x th o p p i n gp o i n tj u s ta s t h ec o m m a n di n l i n e2 2 0

r e p o s i t i o n si t t h e r e .

T h et w oR S P P O Sf u n c t i o n so n l i n e2 1 0r e t u r n

t h ep r e v i o u sh o p p i n gp o i n t o ft h es p r i t e .R S P P O S

r e t u r n s t h el a s ts p r i t ep o s i t i o n s e tu s i n gt h ef i r s t

m o d eo f M O V S P R — t h es t a t i cm o d ew h e r ea s p r i t

i s s i m p l y p o s i t i o n e do n t h es c r e e n .T h i sf u n c t i

c o m e si n h a n d yw h e ny o u ' r em o v i n gd i f f e r e

s p r i t e se v e r yw h i c hw a ya n dw a n tt o k e e pt r a c ko f

w h e r et h e ya r e .T h ef i r s tp a r a m e t e rr e f e r s t ot h e

s p r i t en u m b e r .T h es e c o n dr e f e r s t ot h ea x i s( 0 f o r

X a n d1 f o r Y ) .

L i n e2 4 0p e r f o r m st h e" f i r ea m i s s i l e 1 's u b r o

t i n ew h e n e v e rt h e s p a c eb a ri s p r e s s e d .

T h ep r o g r a mk e e p sr u n n i n gu n t i ly o up r e s

1 5 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 161/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 162/194

2

3 : m e r c u r y ' si n v a d e r s

4 : b y m a r t i nh a r d e e

5

6

7

1 0 b l o a d M t e s t s p r i t e "3 0 g r a p h i c1 , 1

4 0 c h a r, 0 , 0 , l t f t

4 5 :

4 6 :

5 0 b r d e r = 2 : f u e l = 2 0 0 0 : a m m o = 8 0

6 0 t $ = c h r $ ( 3 2 ): r e mf o r s p a c e b a r t e s t

7 0 a d j= 4 : r e ms p e e d

7 5 :

7 6 :

8 0 s p r i t e1 , 1 , 2 , 0 , 1 , 1 , 0

9 0 m o v s p r 1 , 2 0 0 , 1 5 09 5 :

1 0 0 d o w h i l ef u e l > 0a n d a m m o > 0m o v s p r1 , 9 0# a d j : r e ms t a r ts h i pm o v i n g

s p r i t e3 , 1 , 8 , 0 , 1 , 1

m o v s p r 3 , 2 0 0 , 6 0

: r e mt u r no n s h i p

: r e m p o s i t i o ns h i p

1 1 0

1 2 0

1 3 0

1 4 0

1 5 0

1 6 0

1 7 0

1 8 0

1 9 02 0 0

2 1 0

2 2 0

2 3 0

2 4 0

: r e mt u r n o n m o n s t e r

: r e mp o s i t i o nm o n s t e r

m o v s p r3 , 2 7 0# a d j : r e ms t a r t m o n s t e r m o v i n g

b a n g = 0

d o u n t i l b a n go r f u e l< 0 o r a m m o < 0

f u e l = f u e l - ( 5 * a d j )

g o s u b5 1 0 : r e ms h o w s c o r e

i f b u m p ( 2 ) = b r d e rt h e n s p r i t e 2 , 0c o l l i s i o n1 , 4 1 0: r e m ah i t !

x = r s p p o s ( 1 , 0 ) : y = r s p p o s ( 1 , 1 )

m o v s p r1 , x + a d j , y

i f b u m p ( 2 ) = b r d e rt h e n s p r i t e 2 , 0

g e t a $ : i fa $ = t $ t h e n g o s u b3 4 0 : r e m f i r e m i s s l e

l o o p2 5 0

2 6 0 l o o p

2 6 5

2 7 0 c h a r , 1 5 , 1 0 , " g a m e o v e r "

2 8 0 s p r i t e1 , 0 : s p r i t e 2 , 0 : s p r i t e3 , 0

2 8 5 :2 9 0 d ou n t i la $ = c h r $ ( 2 7 ): r e m e s c a p e

3 0 0 : g e t k e ya $

3 1 0 l o o p

3 2 0 g r a p h i c0

3 3 0 e n d

3 3 5 :

F i g .1 3 - 5 .A m o r ec o m p l e xv i d e og a m e .

1 5 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 163/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 164/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 165/194

i n s t r u m e n t so r m o o d s ,b u t w i t ht h et r i c k s y o u ' l l

l e a r ni n t h i sc h a p t e r ,y o u ' l lb e a b l et o c r e a t ea n a s

t o u n d i n gv a r i e t y o fs o u n d .

S O M EQ U I C KN O T E SO NM U S I C

L e t ' ss t a r tw i t hw h a tw e a l l k n o w :M u s i ci s

m a d eu p o f n o t e s .T h eo r d e ro f n o t e sd e t e r m i n e st h em e l o d y ,w h i l et h e l e n g t ho f t h ei n d i v i d u a ln o t e s

h e l p st o d e t e r m i n ew h e t h e ri t ' sa w a l t zo r a s a m b a .

T e m p od e f i n e st h es p e e da t w h i c ht h em u s i ci s

p l a y e d . . .a n dt h a t ' sa b o u ti t .W h i l et h e r ei s a g r e a t

d e a lt o t h es t u d yo f m u s i c , t h eb a s i c sa r e p r e t t y

s i m p l e .

T h e C o m m o d o r e1 2 8s t i c k sf a i t h f u l l yt o t h o s e

b a s i c s .F o re x a m p l e ,m u s i c i a n sh a v ei n v e n t e da

c o n c e p tc a l l e dt i m et o h e l pt h e mm e a s u r et h el e n g t h

o f t h en o t e st h e y ' r ep l a y i n g .Y o u ' v eu n d o u b t e d l y

h e a r do f 4 / 4t i m e ,o r 3 / 4t i m e ,o r 6 / 8t i m e .Y o uw i l l

f i n dn ot i m ec o m m a n do nt h eC o m m o d o r e1 2 8 ,b e

c a u s et h ec o m p u t e rd o e s n ' tn e e di t . T i m ei s s i m

p l ya m e a s u r i n gd e v i c et o h e l ph u m a n sc o m p o s e

a n dp l a yt h e i rm u s i c .

T h eC o m m o d o r ei s o n l yc o n c e r n e dw i t ht h e

l e n g t ho f t h en o t e sy o ug i v ei t t o p l a y .T h et i m e

o f t h em u s i cw i l ls i m p l yf a l li n t o p l a c e , j u s ta s i t

d o e sa s n o t e sa r ep l a y e do na n yo t h e r i n s t r u m e n t .

W h i l ei t ' sa l w a y sh e l p f u li f y o uc a nr e a ds h e e t

m u s i c , t h eC o m m o d o r e ' sP L A Yc o m m a n di s r e a l l y

d e s i g n e df o rt h o s eo f u s w h oc a n ' t .I f y o uc a ns i n g

i n t h es h o w e r , y o u ' v e p r o b a b l y g o te n o u g h m u s i

c a lt a l e n t t og e ts t a r t e do n t h eC o m m o d o r e1 2 8 .

P L A Y I N GI N D I V I D U A LN O T E S

H e r e ' so n eo f t h e f i r s tt h i n g sy o up r o b a b l y

p l a y e do n y o u rt o yx y l o p h o n eo r b a b yp i a n o .I t ' s

a p a r t i a ls c a l e :

P L A Y" C D E F G "

T h a ts e to f n o t e sw i l lp r o b a b l ys o u n dl i k ei t

c a m ef r o ma ne l e c t r i cp i a n o .P r o d u c i n ga r e c o g n i z

a b l et u n ei s a m a t t e ro f t e l l i n gt h eC - 1 2 8w h a tn o t e s

t o p l a y .H e r e ' st h eb e g i n n i n go fM a r yH a da L i t t l e

L a m b :

P L A Y " A G F G A A A "

T h eP L A Ys t a t e m e n ti s n o tl i m i t e d t oj u s ta

f e wn o t e s .I n f a c t ,y o uc a ns p e c i f yu pt o 1 6 0c h a r a c

t e r si n a p l a yc o m m a n d .H e r e ' sa r e n d i t i o n o t h e

s o n gi n i t s e n t i r e t y :

P L A Y' ' A G F G A A A G G G A AA G FG A AA A G G A G F "

W h e ny o ui s s u et h i sc o m m a n do n t h e C - 1 2

y o u ' l ln o t i c es o m e t h i n gr i g h ta w a y :t h em u s i cw i l l

s o u n df l a ta n du n i n s p i r e d .T h a t ' sm a i n l yb e c a u s

w e ' r en o tc h a n g i n go c t a v e st o t a k ea d v a n t a g o f

h i g h e rn o t e s ,a n db e c a u s e w e ' r en o tp a u s i n gb e

t w e e nc l a u s e s o ft h e m u s i c .H e r e ' sa s l i g h t l yi m

p r o v e dv e r s i o n :

P L A Y" O 4 A G F G A A A R G G

0 4 R A G F G A A A A G G A G F "

E v e nt h o u g ht h i s l i n ea d d so n l yt w on e wf e a

t u r e s ,t h e r e ' sa b i gd i f f e r e n c ei n t h ew a yi t s o u n d s

F i r s t ,y o uw i l ln o t i c ea n e wl e t t e r ,R , w h i c ht e l l s

t h ec o m p u t e rt o r e s tf o ra n o t e .T h eR a c c o u n t sf o r

t h ep a u s ei n p l a y .S e c o n d ,w eh a v ed e f i n e d t h a n

o c t a v ea t t h eb e g i n n i n go ft h eP L A Ys t a t e m e n t A n

o c t a v ei s s i m p l yu s e dt o d e f i n ea c e r t a i nl e v e lo f

h i g h n e s so r l o w n e s sf o rt h e n o t e sb e i n gp l a y e d .O c

t a v e4 , d e f i n e dh e r eb y0 4 ,i s t o w a r dt h eu p p e re n d

o f t h em u s i c a ls p e c t r u mo n t h e C - 1 2 8 ,a n di s t h e

m a c h i n e ' sd e f a u l to c t a v e .

O u rP L A Ys t a t e m e n ta l s ou s e st h e0 p a r a m

t e rt w i c em o r e — o n c et o s w i t c ht o o c t a v e5 ( a n d p l

t w on o t e s ) ,a n do n c em o r et o s w i t c hb a c kt o o c t a v

4 . H a dw en o ts w i t c h e du p t o o c t a v e5 — b u ts t i l le n

t e r e dt h et w oC n o t e s — t h em e l o d yw o u l dd i pd o w n

t o a l o w e rC i n o c t a v e4 , w h i c hw o u l d n ' tb e t h e

d e s i r e dr e s u l t a ta l l .E a c hn e wo c t a v es t a r t sa t C ,

j u s ta s o n a p i a n o .

T h eB l a c kK e y s

E v e ni f y o u ' v en e v e rs t u d i e dm u s i c ,y o u ' v en o

d o u b th e a r d a b o u ts h a r p sa n df l a t s .T h e s ea r e t h

b l a c kk e y so nt h ep i a n o , t h a tr e p r e s e n ta n o t et h a t '

h a l f w a yi n b e t w e e nt w on a t u r a lk e y s .F o re x a m

p l e ,D s h a r pi s o n e - h a l fa t o n a ls t e pb e t w e e nD a n d

E .

1 5 5

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 166/194

T h et e r ms h a r pi s u s e dt o d e s c r i b ea n o t et h a t ' s

h a l f w a ya b o v ea n a t u r a ln o t e ,a n dt h et e r mf l a ti s

u s e df o ra n o t et h a t ' sa h a l f - s t e pb e l o w . T h e r e f o r e ,

D s h a r pa n dE f l a ta r er e a l l yt h es a m en o t e .

O nt h eC o m m o d o r e1 2 8 ,a p o u n ds i g n( # )p r e

c e d e sn o t e st o b ep l a y e da s s h a r p s ,a n da d o l l a rs i g n

( $ )p r e c e d e sn o t e st o b e p l a y e da s f l a t s .I n t h e

P L A Ys t a t e m e n t ,D s h a r pw o u l db e r e p r e s e n t e d

l i k et h i s :# D .T h en o t eE f l a tw o u l db e p l a y e dl i k e

t h i s :$ E .A g a i n ,s i n c eb o t hn o t e sa r er e a l l yt h e

s a m e ,t h e y ' ds o u n di d e n t i c a l .

H e r e ' st h eb e g i n n i n go f S k i pt o M 'L o u , p l a y e d

i n t h ek e yo f G . I t i n c l u d e sa n F s h a r p :

P L A Y" 0 4B B G G B B0 5 D R 0 4 A A# F # F

A A0 5 C "

S h a r p sa n df l a t sa r ev e r yi m p o r t a n t .I f y o ut a k e

t h es h a r ps i g na w a y ,y o u ' l ln o t i c ea d r a m a t i cc h a n g e

i n t h et o n a l i t yo f t h es o n g .I t w i l ls o u n dl i k es o m e

o n ei s h i t t i n gt h ew r o n gn o t e s .

P L A Y I N GI N H A R M O N Y

B e c a u s ei t h a s t h r e e s e p a r a t ev o i c e s ,y o u r

C - 1 2 8 c a nd oa g o o db i tm o r et h a n s i m p l y p i c k o u t

s i m p l et u n e s ;i t c a np r o v i d eb a c k g r o u n d a c c o m p a n i

m e n tt o a n ym e l o d y .

O n ee x a m p l eo f h o wh a r m o n yc a nb e u s e di s

a c h o r d .H e r e ' sa C c o r dp r o d u c e du s i n gt h eV( v o i c e )p a r a m e t e r :

P L A Y" V IC V 2E V 3 G "

T h i sc h o r ds o u n d sm u c hf u l l e rt h a na n ys i n g l en o t e

c o u l d .T h en o t e sh e r e ,p l a y e ds i m u l t a n e o u s l y ,c o m

p l e m e n te a c ho t h e r .

T h eV p a r a m e t e ri s u s e dt o s e l e c tv o i c e s ,a n d

s h o u l dp r e c e d ea n yo t h e r i n f o r m a t i o nf o rt h ev o i c e ,

s u c ha s a n o c t a v es e t t i n go r n o t e s .P e r h a p st h es i m

p l e s tf o r mo f h a r m o n yi s s i m p l yt o p l a y t h es a m e

n o t e s i m u l t a n e o u s l ya t d i f f e r e n to c t a v e s .H e r e ' sa n

e x a m p l eo f t h r e e C ' sp l a y e ds i m u l t a n e o u s l y :

P L A Y" V 1 0 4C V 2 O 5C V 3 O 6C "

Y o u ' v ep r o b a b l y n o t e dt h a tp a r a m e t e r si n a l l

t h eP L A Ys t a t e m e n t ss o f a rh a v eb e e nb r o k e nu p

b ys p a c e s .T h i si s o n l yt o m a k et h es t a t e m e n t sm o r

r e a d a b l e .A s t a t e m e n ts u c ha s t h e f o l l o w i n gw o u l

w o r kj u s ta s w e l l ,b u ti t i s n e a r l y i m p o s s i b t o d e

c i p h e r :

P L A Y" V 1 O 4 C V 2 O 5 C V 3 O 6 C

T h u sf o r t h e m o s tp a r t , y o u ' l ls e et h e e x t

w h i t e s p a c ei n s e r t e di n t om u s i c a lp r o g r a ml i n e s

L a t e ri n t h i sc h a p t e r ,w e ' l l t a l ka b o u to t h e rw a y

t o m a k et h eP L A Ys t a t e m e n te a s i e rt o u s ea n dc o m

p r e h e n d .

B a c kt o h a r m o n y :o n eo f t h e b e s tk n o w ne x

a m p l e so f t w o - n o t eh a r m o n yi s t h es o n g" C h o

s t i c k s " —k n o w no n t h ep i a n ob y e v e r yp r e c o c i

s e v e ny e a ro l d .F i g u r e1 4 - 1s h o w sh o wt h eb e g i n

n i n go f " C h o p s t i c k s "w o u l db e p l a y e db yt h eC o m

m o d o r e1 2 8 .I t ' sj u s ta s s p i n e s h a t t e r i n

v e r s i o np l a y e do n a b a b yg r a n d .

T h ep r o g r a ms t a r t s o f fw i t ha n e v e r - e n d i D O

. . . L O O Pt h a tk e e p st h es o n gp l a y i n gi n p e r p e t u i

Y o uc a n b r e a ko u t o f t h e s o n gb y h i t t i n gt h e

R U N / S T O Pk e y .

L i n e2 0 s e t st h eT E M P Oa t w h i c ht h em u s i

i s p l a y e d .A l t h o u g ht h em o d e r a t et e m p oo f s e v e n

s p e c i f i e dh e r ei s b e s tf o ra s o n gl i k eC h o p s t i c

y o uc o u l ds p e c i f ya l o w e rs p e e d( d o w nt o a t e m p

o f o n e ) ,o r a f a s t e rs p e e d( u pt o a t e m p oo f 2 5 5 ) A ta t e m p oo f 2 5 5 ,T E M P Os p e e d sa l o n ga t t i c k e

t a p e s p e e d .I f y o uc h a n g et h eT E M P Os e t t i n g

o n e ,t h ep a u s eb e t w e e nn o t e sw i l lb e a n u n b

a b l e3 0 s e c o n d s( y o uc a ne s c a p ef r o mt h i st o r t u r

b y p r e s s i n gR E S T O R Ea n dR U N / S T O Ps i m u

t a n e o u s l y ) .

S i n c et h em a i nb o d yo f C h o p s t i c k si s p l a y e

t w i c ei n t h es o n g ,t h i sm e l o d yh a sb e e np l a c e di n

a s u b r o u t i n e ,w h i c hs t a r t sa t l i n e1 5 0 .

T h ef i r s ts t a t e m e n ti n t h i sr o u t i n eu s e st h e

P L A Yc o m m a n dt o s e tv o i c eo n ea t o c t a v ef o u ra n d

v o i c et w oa t o c t a v ef i v e .T h e r ea r en on o t e si n t h i

P L A Ys t a t e m e n t ;i t ' so n l yu s e dt o s e tt h ed e f a u

o c t a v e sf o rt h ev o i c e s .

T h en e x tt h r e el i n e sc o n t a i nF O R. . . N E X

l o o p st h a tp l a ye a c hs e t o f n o t e ss i xt i m e s .T h e

P L A Ys t a t e m e n ti n l i n e1 7 0s t a r t sb yp l a y i n gF u n -

1 5 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 167/194

1 0 d o

' 2 0 t e m p o7 0

3 0 g o s u b 1 5 0

4 0 p l a yM v 1 o 5c v 2 o 6c r w m n

5 0 p l a yN v 1 o 5c v 2 o 6c h r w m "

6 0 p l a yv ' v 1 o 5c v 2 o 6c h r w m "

7 0 p l a y H v 1 o 4b v 2 o 5d h r w m l f8 0 p l a y l v v 1 e v 2 a h r w m "

9 0 g o s u b1 5 0

1 0 0 p l a yM v 1 cv 2 o 6 c h r wr m l f

1 1 0 p l a yM v 1 o 5 fv 2 o 5 g h rw m l f

1 2 0 p l a yM v 1 o 5c v 2 o 6 c h r wm 1 1

1 3 0 p r i n t " c h o p s t i c k s ! "

1 4 0 s l e e p1 : l o o p

1 5 0 r e m : m a i n s u b r o u t i n e

1 6 0 p l a y N v 1 o 4v 2 o 5 "

1 7 0 f o r x = 1 t o 6 : p l a yM v 1

1 8 0 f o r x = 1 t o 6 : p l a yM v 11 9 0 f o r x = 1 t o 6 : p l a yM v 1

2 0 0 r e t u r n

f v 2 g h r w m " : n e x t

e v 2 g h r w m " : n e x td v 2 b h r w m " : n e x t

F i g .1 4 - 1 .C h o p s t i c k s .

d e rv o i c eo n ea n dG u n d e rv o i c et w o .B e c a u s et h e

n o t e s a r eu n d e rd i f f e r e n t v o i c e s ,t h e y ' l lb e p l a y e d

s i m u l t a n e o u s l y ,p r o d u c i n gh a r m o n y .T h en e x tp a

r a m e t e r ,H R ,s e t st h en o t ed u r a t i o nt o a h a l f n o t e ,

a n dt h e np l a y sa r e s t ,p a u s i n gf o r h a l fa m e a s u r e

t o p r o v i d es e p a r a t i o nb e t w e e nt h en o t e s .I f y o ur e

m o v e dt h eH R ,t h en o t e sw o u l ds e e mt o p l a yf a s t e r

a n dw o u l dr u nt o g e t h e r .

T h el a s ts e c t i o n ,W M ,r e s e t sv o i c e2 f o rw h o l e

n o t e s( a f u l lf o u rb e a t s )a n di n s t r u c t st h ec o m p u t e r

t o f i n i s hp l a y i n g t h e c u r r e n tl i n eb e f o r em o v i n go n .

I f t h e M p a r a m e t e rw e r en o t i n c l u d e d ,v o i c e1

w o u l db e g i np l a y i n g t h en e x tn o t e w h i l ev o i c e2 w a s

s t i l lr e s t i n g .T h eM p a r a m e t e ra l l o w sy o ut o p l a c e

a l lv o i c e so n h o l dw h i l et h ec u r r e n t n o t e s o rr e s t s

f i n i s hp l a y i n g .I t ' s a c o n v e n i e n ta l t e r n a t i v e t o

s p e c i f y i n g s e p a r a t er e s t s f o re a c hv o i c e .O n c et h er o u t i n ei s f i n i s h e d ,t h ep r o g r a mc o n

t i n u e sa t l i n e4 0 b y p l a y i n ga t w o - o c t a v eC c h o r d

a n dr e s t i n gf o ro n ef u l lm e a s u r eb e f o r ec o n t i n u i n g .

T h em a i nb o d yo f t h et u n ei s c a l l e da g a i na t l i n e

9 0 ,f o l l o w e db y a t h r e e - n o t ef i n a l e .A t t h ee n do f

t h e s o n g , t h ew o r d" C H O P S T I C K S ! "i s d i s p l a y e d

o n t h es c r e e n ,t h ec o m p u t e rp a u s e sf o ro n es e c o n d

( t h a n k st o t h eS L E E Pc o m m a n d )a n dt h e nL O O P

b a c kt o t h et o p ,w h e r ei t b e g i n st h en e v e r - e n d

c y c l ea g a i n .

W H O L EN O T E S ,H A L FN O T E S

U pu n t i ln o w ,m o s to f t h ee x a m p l e sy o u ' v e s eh a v eu s e d" w h o l e "n o t e s :a s i n g l en o t ep e rm e a

s u r e( i n4 / 4t i m e ) .W h o l en o t e sa r e t h ed e f a u l to n

t h eC o m m o d o r e1 2 8 . I na f e we x a m p l e s ,w e ' v ei n

c l u d e dh a l fn o t er e s t sa n do t h e r d e v i c e st h a tc h a n g

t h et i m i n go f t h en o t e sb e i n gp l a y e d .F i g u r e1 4 - 2 ,

t h ec h o r u sf r o mt h es o n gB u f f a l oG a l s ,e m p l o y

n o t e so f v a r y i n gd u r a t i o n s .T h ep r o g r a ms t a r t so f f

w i t ht w os i x t e e n t hn o t e s( I G G ) ,f o l l o w e db y t w o

q u a r t e r n o t e s( Q ]E ] , f o l l o w e db y t w os i x t e e n

n o t e s( I D D ) .F i g u r e1 4 - 3s h o w sa c o m p l e t ec h a r t

o fn o t ed u r a t i o n sa v a i l a b l eo n t h eC o m m o d o r e1 2 8 ,

a l o n g w i t ht h e i rs h e e tm u s i cc o u n t e r p a r t s .

D E F I N I N GI N S T R U M E N T S

T h e C o m m o d o r e1 2 8c o m e sw i t h 1 0i n s t r u

m e n t sb u i l ti n .T h e ya r e t h ep i a n o ,a c c o r d i o n ,c a l

l i o p e ,d r u m ,f l u t e ,g u i t a r ,h a r p s i c h o r d , o

1 5 7

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 168/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 169/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 170/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 171/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 172/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 173/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 174/194

2 9 0

3 0 0

3 1 0

3 2 0

3 3 0

3 4 0

3 5 0

3 6 0

3 7 0

3 8 0

a S s ^ g "

a $ = n # Q n

a $ = f f b l f :

a $ = " c " :

a $ = " # c M

a $ = " d " :

a $ = " # d "

a $ = l f e t i :

a $ = " f " :

: a d j = 0 : r e t u r n

a d j = 0 : r e t u r n

: a d j = 0 : r e t u r n

a d j = 0 : r e t u r n

a d j = 1 : r e t u r n

: a d j = 1 : r e t u r n

a d j = 1 : r e t u r n

: a d j = 1 : r e t u r n

a d j = 1 : r e t u r n

a d j = 1 : r e t u r n

T h er o u t i n es t a r t i n ga t l i n e1 5 0d o e s an u m b e r

o f t h i n g s :

• I t g e t sa k e y s t r o k e

• I t b r a n c h e st o o n eo fm a n yr o u t i n e si f o n eo f t h e

k e y so n t h eb o t t o mt w o r o w sw a sp r e s s e d

• I t s e t sa m u s i c a l n o t ei f o n eo f t h e s ek e y sw a s

p r e s s e d

• I f e s c a p ew a sp r e s s e d ,i t s e t st h eO U Tf l a g

T h eh e a r to f t h e r o u t i n ei s t h eO N . . .G O T O ,

w h i c hb r a n c h e st o s t a t e m e n t sb a s e do n t h ep o s i

t i o no f A $w i t h i n t h eI N S T Rf u n c t i o na t l i n e1 7 0 .

I f Z w a sp r e s s e d ,t h ep r o g r a mb r a n c h e st o 2 1 0 ,

w h e r ea n o t eo f C i s s e t .I f a n S w a sp r e s s e d , t h e

p r o g r a mb r a n c h e st o 2 2 0w h e r ea n o t eo f C s h a r p

i s s e t ,a n ds o o n .W h yd i d n ' tw ej u s tu s e al o n gs e

q u e n c eo f I F s t a t e m e n t s ?B e c a u s et h i sO N . . .

G O T Oa p p r o a c h w i t hI N S T Ri s m u c h ,m u c hf a s t e r .

O n c et h en o t eh a sb e e ns e t ,i t i s p l a y e di n t h e

c u r r e n tv o i c e ,w h i c hi s d e t e r m i n e db yt h ev a l u eo f

C T R( 1 ,2 , o r 3 ) .W i t he a c hk e y s t r o k e ,a d i f f e r e n t

v o i c ei s s e l e c t e d ,l e n d i n ga ne c h o - l i k es o u n dt o t h e

m u s i c .T h em u l t i p l e - v o i c ea p p r o a c ha l s oe n s u r e s

t h a tn o t e sw o n ' tg e tb a c k e du p o r b e s k i p p e db e

c a u s ey o uh a p p e nt o b e p r e s s i n gt h ek e y sf a s t e r

t h a nt h ec o m p u t e rc a nc o n v e r ta n dp l a y t h en o t e s .I f y o up r e s sk e y si n q u i c ks u c c e s s i o n ,y o uc a ne v e n

f o r mc h o r d si n t h i sm a n n e r( X ,C , a n dB p l a y e di n

r a p i d s u c c e s s i o nc r e a t ea p l e a s a n ts o u n d i n gC

t r i a d ) .

I f y o uh o l dk e y sd o w nf o ra n yt i m e ,t h e n o t

w i l lr e p e a ta n df e e di n t oi t s e l f ,s o u n d i n g s o m

l i k ea m a n d o l i n .

T h eh i g h e ro c t a v e s ,o b t a i n e db y p r e s s i n gt h e

4 o r 5 k e y s ,s o u n dl i k eb e l l s .T h el o w e ro c t a v e

s e tb yp r e s s i n g1 o r 2 , a r em o r ea k i nt o a m a j e s t

p i p eo r g a n .

I n t i m e ,y o um a ye v e nw a n tt o e x p e r i m e n tw i t h

t h ee n v e l o p e s t a t e m e n tt o l e n dd i f f e r e n ts h a d e so f

s o u n dt o t h i sn e wi n s t r u m e n tw e ' v ei n v e n t e d

S O M EF I N A LN O T E S

T h e r e ' so n el a s tn o t ew h e nu s i n gt h eC - 1 2 8

m u s i cc o m m a n d s :t h eC - 1 2 8i s s l i g h t l yo u to ft u n e

I f i t s e e m sa s t h o u g ht h eC o m m o d o r en o t e sa r eo f f

p i t c hf r o ma p i a n oo r a c c o r d i o no r s o m eo t h e ri n

s t r u m e n tt h a t ' ss u p p o s e dt o s t a yi n t u n e ,d o n ' t

b l a m et h e i n s t r u m e n t .T h eC - 1 2 8i s a p p r o x i m a

1 1 / 2s t e p so f ff r o mp e r f e c tp i t c h .

T h eC o m m o d o r e1 2 8 c a np r o v i d ey o uw i t hl o t s

o fg o o dm u s i c a l e n t e r t a i n m e n ta n dl e a r n i n g .I f y o u

e n j o yw o r k i n gw i t ht h em a c h i n e ' sm u s i c a lc o m

m a n d s ,y o um a yw a n tt o c o n s i d e ri n v e s t i n gi n s o m

s h e e tm u s i ct h a tc a nb e t r a n s c r i b e di n t oP L A s t a t e m e n t s .I t w i l lb e r e p r o d u c e da s f a i t h f u l l a s

i f y o u w e r er u n n i n ga p l a y e rp i a n o .I f y o u ' r ei n t e r e s t e di n e x p l o s i o n s ,p h a s e rs h o t s

a n do t h e rs u c h s o u n de f f e c t s ,y o u ' l la l s ow a n tt o

e x p e r i m e n tw i t ht h eS O U N Dc o m m a n d ,w h i c hi s

c o v e r e de x t e n s i v e l yi n y o u r C - 1 2 8m a n u a l .

1 6 4

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 175/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 176/194

c o m p u t e rw i l la u t o m a t i c a l l yp l a c e t h ep r o g r a ml i n e s

i n o r d e r( a se a c hn e wl i n ei s a d d e d ) .

N o ri s i t n e c e s s a r yt o p l a c el i n en u m b e r so n e

a p a r t .I n f a c t ,p r o g r a ml i n en u m b e r sa r eu s u a l l y

s p a c e d1 0 o r 2 0 a p a r t :

1 0 P R I N T " H E L L O "

2 0 P R I N T" H O W "

3 0 P R I N T" A R E "

4 0 P R I N T" Y O U "

L e a v i n gp r o g r a ml i n e ss o m ee l b o wr o o ma l l o w s

s p a c ef o ro t h e rl i n en u m b e r st o b e i n s e r t e dl a t e r

i n t h es a m e .

T A K I N GA N E WD I R E C T I O N

N o r m a l l yt h ec o m p u t e rt a k e sp r o g r a ml i n e so n e

a t a t i m ea n di n o r d e r .T h e r ea r es e v e r a lB A S I C

c o m m a n d s ,h o w e v e r ,t h a ta l l o wy o ut o s k i po v e r

c e r t a i nl i n e so r r e d i r e c tt h ec o m p u t e r ' sa t t e n t i o n

t o a c o m p l e t e l yd i f f e r e n t s e c t i o n o ft h ep r o g r a m .

I n f a c t ,i t ' sa g o o d i d e a — o n c ey o u ' r ec o m f o r t a b l e

w i t hs i m p l ep r o g r a m m i n g — t os e tu p s p e c i a lr o u

t i n e st h a tt h ec o m p u t e rp e r f o r m ss e p a r a t e l yf r o m

t h ep r o g r a ma s a w h o l e .H e r e ' sa n e x a m p l e :

1 0 P R I N T " H E L L O "

2 0 G O S U B 2 0 0

3 0 P R I N T" G O O D B Y E "4 0 E N D

2 0 0 P R I N T" I ' MG L A DT OM E E TY O U "

2 1 0 R E T U R N

I n t h i se x a m p l e , as u b r o u t i n ep r i n t st h ew o r d s" I ' m

g l a dt o m e e ty o u . "P r o g r a m m e r sr e f e rt o t h eo p e r

a t i o na t l i n e2 0 a s a c a l l" L i n e2 0 c a l l st h er o u t i n e

a t l i n e2 0 0 , "y o u ' l lh e a rt h e ms a y .T h eR E T U R Nc o m m a n da t l i n ei n s t r u c t st h ec o m p u t e rt o r e t u r n

t o i t sp r e v i o u sp l a c ei n t h ep r o g r a m( j u s ta s i f i t h a d

k e p t ab o o km a r ka t l i n e 2 0 ) .B e c a u s et h er o u t i n ei s c a l l e db e t w e e nl i n e s1 0 a n d3 0 ,t h es e n t e n c ea t

l i n e2 0 0w i l la p p e a ro n t h es c r e e na f t e rt h ew o r d

H E L L Oa n db e f o r et h ew o r dG O O D B Y E :

H E L L O

I ' MG L A DT OM E E T Y O UG O O D B Y E

T h ea d v a n t a g eo f u s i n gt h i sr o u t i n ei s t h a ti t

c a nb e c a l l e ds e v e r a lt i m e sd u r i n gt h ep r o g r a

w i t h o u t h a v i n gt o r e p e a tl i n e2 0 0 .F o re x a m p l e ,y o u

c o u l dp u ta G O S U B2 0 0b e f o r el i n e1 0 ,a n da f t e r

l i n e3 0( s a y ,a t l i n e s5 a n d3 5 ) .W i t ht h ea d d i t i o

c a l l s ,t h es c r e e nd i s p l a yw o u l dl o o kl i k e t h i sw h e

t h ep r o g r a mi s r u n :

I ' MG L A DT OM E E T Y O UH E L L OI ' MG L A D T OM E E T Y O UG O O D B Y E

I ' MG L A D T OM E E T Y O U

S u b r o u t i n e sa r e e s p e c i a l l yu s e f u lw h e ny o u

p l a nt o r e p e a tc o m p l i c a t e do r b u l k yp r o g r a mf u n c

t i o n ss e v e r a lt i m e s d u r i n ga p r o g r a m .T h e ym a k

y o u rp r o g r a me a s i e rt o f o l l o wa n dl e s sb u l k y ,s i n c o f t e n - u s e dr o u t i n e s d o n ' th a v et o b e r e p e a t e d

t h i sb o o kw e ' l lb eu s i n gs u b r o u t i n e s e x t e n f o r

k e y b o a r de n t r y ,f i l i n g ,s o r t i n g ,a n dd i s p l a ya p p l i

c a t i o n s .

A n o t h e rW a yt o R e d i r e c tP r o g r a mC o n t r o

A n o t h e rc o m m a n d ,G O T O ,i s a l s ou s e dt o

r e d i r e c tt h ec o m p u t e rt o a d i f f e r e n tl i n e .I n t h i se x

a m p l e ,G O T Oi s u s e dt o s k i po v e rl i n e3 0 :

1 0 P R I N T" H E L L OT H E R E "

2 0 G O T O4 0

3 0 P R I N T" T H I SW I L LN E V E RA P P E A

4 0 P R I N T" G O O D B Y E "

T r u et o i t sp r o m i s e ,t h es t a t e m e n ti n l i n e3 0 w i l l

n e v e rs h o wu p o n t h es c r e e n .

N a t u r a l l y ,t h i st y p eo fp r o g r a ms t r u c t u r es e r v e

n ou s e f u lp u r p o s ea s i t s t a n d s ,b u tw h e nu s e dw i t h

d e c i s i o n - m a k i n go p e r a t i o n s ( s u c h a sw e ' l ld i s c u

l a t e ri n t h i s s e c t i o n ) ,G O T Ob e c o m e ss u b s t a n t i m o r ed y n a m i c .

B A S I CA SA S E C O N D L A N G U A

H e r ea r e s o m eB A S I Cc o m m a n d si n p r o c e

d u r e st h a ty o uw i l l l i k e l ye n c o u n t e rd u r i n gy o u rf i r s

e x p e r i e n c e sw i t ht h en e wl a n g u a g e .

1 6 6

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 177/194

P r i n t

Y o u ' v ea l r e a d ys e e ns e v e r a le x a m p l e so f t h e

P R I N Tc o m m a n da n dp r o b a b l yr e a l i z eb yn o wt h a t

i t i s u s e dt o d i s p l a y i n f o r m a t i o n .I f y o u ' v ep e e k e d

a t y o u rC o m m o d o r e1 2 8U s e r ' s G u i d e ,y o um a ya l s o

h a v ed i s c o v e r e dt h a tt h e q u e s t i o nm a r k( ? )c a nb e

u s e da s s h o r t h a n df o rt y p i n gP R I N Te a c ht i m ey o uw a n tt o u s et h ec o m m a n d .

T h eP R I N Tc o m m a n dh a ss e v e r a lf o r m s ,a l lo f

w h i c hp r i n ti n f o r m a t i o n o n t o t h es c r e e n ,t o a

p r i n t e r ,o r i n t oa f i l e .P R I N Tm a yb e u s e dt o d i s

p l a yn u m b e r s ,t e x t ,a n dc a l c u l a t i o n s — o ra c o m b i

n a t i o n o ft h e m :

P R I N T" H E L L O "

P R I N T2

P R I N T 2 * 5

I n t h el a s te x a m p l e ,t h ea s t e r i s k( * )i s u s e da s a m u l

t i p l i c a t i o ns y m b o l .

A n yt e x tt o b e d i s p l a y e dm u s tb e e n c l o s e di n

q u o t e s ,b u tt e x ta n dn u m b e r sa r ee a s i l yb l e n d e d

i n t ot h es a m eP R I N Ts t a t e m e n t

P R I N T" H E L L O " ; 2* 5

T e x tm u s tb e e n c l o s e di n q u o t e sb e c a u s eg r o u p s

o f l e t t e r s( s u c h a sH E L L O )a r ei n t e r p r e t e da s v a r i

a b l e sw h e nt h e ya r e n ' ti n s i d eq u o t e s .W h e nH E L L O

i s p l a c e di n q u o t e s ,i t w i l la p p e a ra s a w o r d .

T h eq u i c k ,s i m p l ea p p l i c a t i o n o f v a r i a b l e si s

o n eo f B A S I C ' S k e yt a l e n t s .B A S I Ca l l o w sy o ut o

d e t e r m i n ev a l u e sm u c ha s i s d o n ei n a na l g e b r af o r

m u l a .T h i sm a k e sB A S I Ca m a z i n g l yf l e x i b l e ,b e

c a u s ev a r i a b l e sm a yr e p r e s e n td i f f e r e n tv a l u e sa t

d i f f e r e n tt i m e s ,e v e ni n t h es a m ef o r m u l a .

A ne a s ye x a m p l et o p i c t u r ei s a s i m p l eh o u s e

h o l d b u d g e t . P a r to f t h ef o r m u l af o rd e t e r m i n i n g

t h ea m o u n to f m o n e ys a v e de a c hm o n t hm i g h tb e :

S A V D= P A Y- X P E N S E S

I n t h i se x a m p l e ,t h ec o m p u t e rf i g u r e st h e v a l u eo f

t h ev a r i a b l eS A V E Db a s e do n t h es t a t e df o r m u l a .

I f t h ea m o u n to f P A Yo r X P E N S E Sc h a n g e s ,t h e

v a l u eo f S A V Dw i l ll i k e w i s eb e a d j u s t e d .A n o t h e r

t y p eo f v a r i a b l e , c a l l e da s t r i n g v a r i a b l eb e c a u s ei t

c o n t a i n ss t r i n g so f c h a r a c t e r s ,i s u s e dt o s t o r eb o t h

t e x ta n d n u m b e r s .

N A M E $= " J A N "

W h e n e v e ra s t r i n gv a r i a b l es u c ha s N A M E i s

p r i n t e di n a p r o g r a m ,t h ec h a r a c t e r s c o n t ai n

t h a t s t r i n gw i l lb e d i s p l a y e d .T h u st h e s el i n e s :

1 0 N A M E $= " J A N "

1 5 P R I N TN A M E $

w o u l dd i s p l a y :

J A N

N a t u r a l l y ,i f N A M E $i s a s s i g n e da d i f f e r e

v a l u e( c o l l e c t i o no f c h a r a c t e r s )s o m e w h e r ed o w n

t h e r o a d , t h a td i f f e r e n tv a l u e( s a y ," M I K E "o r

" C A R L A " )w o u l db e d i s p l a y e di n s t e a d .

T h e r ea r es o m el i m i t st o B A S I C ' Sv e r s a t i l i

Y o uc a n ' t ,f o r i n s t a n c e ,p e r f o r mt h es a m et y p e so f

m a t h e m a t i c a lo p e r a t i o n sw i t hs t r i n gv a r i a b l e sa s

w i t hn u m e r i co n e s ,b u tt h e r e a r ew a y st o a d d ,o r

c o n c a t e n a t e , s t r i n gv a r i a b l e st o g e t h e r ( s u c h a d d

i n ga f i r s ta n dl a s tn a m et o g e t h e r ) ,a n dw e c o v e

t h e s em e t h o d se x t e n s i v e l yi n t h e b o o k .

U s i n gV a r i a b l e st o T h e i rF u l l e s t

B A S I Cc a nd om u c hm o r et h a n s i m p l ya s s i g n

i n gv a l u e sw i t h i na p r o g r a m ;i t c a nl e ty o uo r o t h e

u s e r sd e t e r m i n et h ev a l u e s w h i l et h ep r o g r a mi s a c

t u a l l yc h u g g i n ga l o n g .T h i sf e a t u r eg i v e sy o u rv a r i

a b l e sa n df o r m u l a st r u l yu n l i m i t e df l e x i b i l i t y .

T o r e a s s i g na v a r i a b l e ' sv a l u e w h i l e t hp r o

g r a mi s r u n n i n g ,w e n e e da w a yt o a c c e p t e n

f r o mt h ek e y b o a r da n ds t o r ei t i n t h ev a r i a b l e .W eh a v ea l lo f t h i s i nt h eI N P U Tc o m m a n d ,w h i c ha c

c e p t s i n p u tf r o mt h ek e y b o a r do r f r o ma d i s kf i l e .I f y o uw a n t e dt o f i n do u ta u s e r ' sn a m e , a n ds t o r e

t h a tn a m ei n t h eN A M E $v a r i a b l e ,t h e f o l l o w

l i n ew o u l dd o t h et r i c k :

1 0 I N P U TN A M E $

T h i si s t h e s i m p l e s tf o r mo f t h eI N P U Tc o m -

1 6 7

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 178/194

m a n d , a n da s s u c hi t h a s d r a w b a c k s .F i r s to f a l l ,

t h eu s e rw i l lo n l yk n o wt h a tt h ec o m p u t e ri s r e

q u e s t i n ga n e n t r yb y a s i n g l eq u e s t i o nm a r kt h a t

a p p e a r so n t h es c r e e n .T h e r e w o u l db e n o c l u ea s

t o w h a tt h ep r o g r a mr e a l l yw a n t s .C a ny o ut h i n k

o f a w a ya r o u n dt h i sp r o b l e m ?O n ew a yw o u l db e

t o a d da p r i n ts t a t e m e n tb e f o r el i n e1 0 ,g i v i n g t h e

p r o g r a mo p e r a t o rs o m ei n s t r u c t i o n s :

5 P R I N T" P L E A S EE N T E RY O U RN A M E " ;

1 0 I N P U TN A M E $

T h es e m i c o l o nf o l l o w i n gt h e P R I N Tl i n ei s

v e r yi m p o r t a n t .I t s i g n a l st h ec o m p u t e rt o l e tt h e

c u r s o rr e m a i nw h e r ei t i s , i n s t e a do f a d v a n c i n ga

l i n e .S i n c et h ec u r s o rp o i n t s t ot h ep o s i t i o no f t h e

n e x tc h a r a c t e rt o b e d i s p l a y e d ,t h en e x tc h a r a c t e r

t y p e dw i l la p p e a rt o t h er i g h to f " P L E A S EE N T E RY O U RN A M E . "W i t h o u tt h es e m i c o l o n ,t h en e x t

c h a r a c t e rp r i n t e d( w h i c hw o u l db e a ? i n t h i sc a s e )

w o u l da p p e a ro n t h en e x tl i n e .B u tt h es e m i c o l o n

e n s u r e st h a tt h e? w i l lb e p l a c e da t t h ee n do f t h e

s e n t e n c e .H e r e ' sw h a ti t w o u l dl o o kl i k ew h e nt h e

p r o g r a mi s r u n :

P L E A S EE N T E RY O U RN A M E ?

T h ef l a s h i n gc u r s o rw o u l d b ep o s i t i o n e dt o t h e

r i g h to f t h e q u e s t i o nm a r k ,a n dt h er e s p o n s ew o u l d

b e p r i n t e dt o t h er i g h to f t h ep r o m p ta n dq u e s t i o n

m a r ka s t h e c h a r a c t e r sa r e t y p e df r o mt h e

k e y b o a r d .

A n o t h e rw a yt o a c c o m p l i s ht h es a m es o r to f

t h i n gi s t o c o m b i n et h e" P L E A S EE N T E RY O U RN A M E "p r o m p tw i t h i nt h e i n p u tl i n ei t s e l f :

1 0 I N P U T " P L E A S EE N T E RY O U R

N A M E " ;N A M E $

A sy o uc a ns e e ,t h i sa p p r o a c hc o m b i n e sa f u n c t i o n

o f P R I N Tw i t h i nI N P U T ,a n dk e e p sb o t ho p e r a

t i o n s t o g e t h e r .

T h es a m et r i c kc a nb eu s e dw i t hn u m e r i cv a r i

a b l e s .H e r e ' s as h o r tp r o g r a mt h a tp u t sa l lo f t h e s e

c o n c e p t st o w o r k :

1 0 I N P U T " P L E A S EE N T E RY O U N A M E " ; N A M E $

2 0 I N P U T" P L E A S EE N T E RY O UP A Y " ; P A Y

3 0 I N P U T" P L E A S EE N T E RY O U RE X

P E N S E S "; X P E N S E S

4 0 S A V D= P A Y- X P E N S E S

5 0 P R I N T" Y O U RS A V I N G SA R E :" ;

S A V D

N a t u r a l l y ,t h e r e a r em a n ym o r et h i n g sw ec o u l

d ow i t ht h i sl i s t i n g .F o re x a m p l e ,w ec o u l di n s t r u

t h ec o m p u t e rt o p r i n ta s p e c i a lm e s s a g ei f t h e r ew a s

n om o n e ys a v e d( S A V D= 0 ) ,o r t o p r i n ta d i f f e r e

m e s s a g ei f s a v i n g sw e r en e g a t i v e( S A V Di s l e s s

t h a nz e r o ) .

W o r d sW e ' r eN o t A l l o w e dt o U s e

Y o u ' l l n o t i c e t h a tt h eS A V Dv a r i a b l ed o e sn o t

c o n t a i n t h el e t t e rE , b e c a u s et h ew o r dS A V Ei n B AS I Ci s r e s e r v e df o ra s p e c i a lc o m p u t e ro p e r a t i

( s a v i n gi n f o r m a t i o n ) .B A S I Cw o n ' ta l l o wy o ut o u s e

s u c hr e s e r v e dw o r d sf o rv a r i a b l e ,a n dw i l lg e n e

a l l yp r o d u c e aP S Y N T A XE R R O Ro r s o m eo t h e

c o m p u t e re q u i v a l e n tt o t h eB r o n xc h e e r .X P E N S

f o l l o w st h es a m er u l e ,b e c a u s eE X Pi s a r e s e r v

w o r di n C - 1 2 8B A S I C .

R e s e r v e dw o r d si n c l u d ea l lB A S I Cc o m m a nI n s h o r t ,d o n ' ta s s i g na v a r i a b l e t h es a m en a m ea s

a B A S I Cc o m m a n d .I t s i m p l yc o n f u s e s t h ec o m

p u t e ra n dc a u s e sa n e r r o r .

D e c i s i o n s ,D e c i s i o n s

C l o s et o t h et o po f t h el i s to f s e r v i c e a b l eB A

S I Cc o m m a n d si s t h eI F f u n c t i o n .L e t ' ss a yy o u

w a n tt o p r i n ta m e s s a g e , s u c ha s t h eo n e sw e d i s

c u s s e da b o v e .I f t h ea m o u n ts a v e de q u a l sz e r o ,y o u

m i g h ts a y" N OM O N E YH A SB E E NS A V E DT h es t a t e m e n tw o u l dl o o kl i k et h i s :

4 2 I F S A V D= 0 T H E NP R I N T" N O

M O N E YH A S B E E NS A V E D "

T h ea b o v es t a t e m e n ti n s t r u c t st h ec o m p u t et o

d o t w ot h i n g s :

1 6 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 179/194

1 . I t t e s t st o s e ei f S A V Dh a s av a l u eo f z e r o .

2 . I fS A V De q u a l sz e r o ,i t p r i n t st h em e s s a g et h a t

f o l l o w s t h et e s t .

I f S A V Dd o e sn o te q u a lz e r o ,t h ep r o g r a ms k i p s

t o t h en e x t s t a t e m e n t w i t h o u tp r i n t i n ga n y t h i n g .

T h u s ,t h eo n l yt i m et h i sm e s s a g ea p p e a r so n t h es c r e e nw i l lb ew h e nn om o n e yh a sb e e ns a v e d .H o w

a b o u t a n o t h e rs t a t e m e n t — o n et h a tw i l lh a n d l en e g a

t i v eS A V Dv a l u e s( i no t h e rw o r d s ,a s t a t e m e n tf o r

h a n d l i n gb u d g e td e f i c i t s ) .I f y o u ' l lt h i n kb a c kt o

y o u rl a s tb r u s hw i t h c l a s s r o o m m a t h e m a t i c s ,y o u ' l l

r e m e m b e rt h a tt h el e s st h a ns i g nl o o k sl i k et h i s< .

S o w e c a nd o a n o t h e rt e s t t h a t ' sv e r ys i m i l a r :

4 4 I F S A V D< 0 T H E NP R I N T" T H E R E

I S A B U D G E TD E F I C I T "

( I fy o uw e r ea p o l i t i c i a n ,a n dt h i sw e r ea g o v e r n

m e n tb u d g e t ,y o uc o u l dc h a n g et h e m e s s a g et o

r e a d :" T H E R EI S A R E V E N U ES H O R T F A L L " )

T h e r e ' s o n em o r es t e py o um i g h tw a n tt o t a k e

t o m a k et h ep r o g r a mr e a l l y r a d i a t e .R e m e m b e rt h e

l i n et h a tp r i n t st h ea m o u n ts a v e d ?W ec o u l db l o c k

t h a tl i n es o i t i s n ' td i s p l a y e do n t h e s c r e e ni f e i t h e r

o f t h e s et w o c o n d i t i o n se x i s t s( S A V D= 0 o r

S A V D< ) .L o g i c a l l y ,t h e o n l yt i m et h eS A V Dv a l u e

s h o u l db e d i s p l a y e di s w h e nS A V Di s g r e a t e rt h a n

z e r o — w h e nt h e r ew a sa c t u a l l ym o n e ys a v e d :

5 0 I F S A V D> 0 T H E NP R I N T" Y O U R

S A V I N G SA R E :" ; S A V D

N o w ,t h ev a l u ei n S A V Di s o n l yp r i n t e di f a c e r

t a i nc o n d i t i o ni s s a t i s f i e d( S A V Di s g r e a t e rt h a n

z e r o ) .

T h e r ea r e o t h e rc o m b i n a t i o n so ft h e s et e s t st h a t

a r ea l s oh a n d yt o k n o w .W h e ny o uw i s ht o t e s tf o r

a v a r i a b l et h a ti s N O TE Q U A Lt o a g i v e nn u m b e r ,a l e s st h a na n dg r e a t e rt h a ns i g na r eu s e dt o g e t h e r :

5 2 I F S A V D< > 0 T H E NP R I N T" S A V DI S N ' TE Q U A L T OZ E R O "

I n m a t h ,o f c o u r s e , t h es y m b o li s s i m p l ya n

e q u a ls i g nw i t h as l a s ht h r o u g hi t . C o m p u t e r sa r e

n o te q u i p p e dw i t ha l lt h es y m b o l sm a t h e m a t i c

h a v ea v a i l a b l e t ot h e m .I f y o uw i s ht o t e s t f o rs o m e

t h i n gt h a ti s " g r e a t e rt h a no r e q u a lt o "o r " l e s st h a n

o r e q u a lt o " ,a g a i ny o uc a nc o m b i n et h et w os i g n s

t o g e t h e r ,i n a n yo r d e r :

5 4 I F S A V D= > 0 T H E NP R I N T" S A V I S L E S ST H A NO R E Q U A L T O0 "

5 6 I FS A V D< = 0 T H E NP R I N T" S A V

I S G R E A T E RT H A NO RE Q U A LT O0 "

S u c hd e c i s i o n - m a k i n gi s o n e o f t h e r e a l

p r o v i n c e so fc o m p u t e r s .D u r i n gt h i sb o o k ,w e ' l l t a

a b o u th o wt o m a k et h em o s to f d e c i s i o n - m a

t e s t s ,a n dh o wt o u s et h e ma s e f f i c i e n t l ya s p o s s i b l

O n a n dO n . . .

A n o t h e rw a yt o l e t t h ec o m p u t e rm a k ed e c i

s i o n si s b y u s i n gt h eO NG O T Oa n dO NG O S Uc o m m a n d s ,w h i c he x a m i n ea p a r t i c u l a rv a r i a b l ea n d

b r a n c ht o a l i s to f l i n en u m b e r sb a s e do n t h e v a l u

o f t h ev a r i a b l e .H e r e ' sa n e x a m p l e :

4 0 O NA G O S U B1 0 0 , 2 0 0 , 3 0 0 , 4 0 0

I n t h ep r o g r a mc o n t a i n i n gt h i sl i n e ,t h ev a r i

a b l eA w o u l dp r o b a b l yh o l dt h ev a l u eo f a u s e r ' s

s e l e c t i o nf r o ma m e n u .I n t h i sl i n e ,i f A = 1 t h e nt h e

c o m p u t e rp e r f o r m st h es u b r o u t i n ea t l i n e1 0 0 ;i f

A = 2t h e s u b r o u t i n ea t 2 0 0i s p e r f o r m e d ,a n ds o o n .

T h el i n e sd o n ' th a v et o b e i n o r d e r ;t h e f o l l o

i n gv a r i a t i o ni s p e r f e c t l y a c c e p t a b l e :

4 0 O NA G O S U B2 0 0 , 3 0 0 , 1 0 0 , 4 0 0

L i n e4 0 w o u l dp e r f o r mt h e s u b r o u t i n ea t l i n e

2 0 0i f A = l , t h e s u b r o u t i n ea t 3 0 0i f A = 2 ,t h e

s u b r o u t i n ea t 1 0 0i f A = 3 ,a n dt h es u b r o u t i n ea t 4 0 0

i f A= 4 .

I f A i s g r e a t e rt h a nt h en u m b e ro f c h o i c e s ,o r

i s e q u a lt o z e r o ,t h ec o m p u t e rd o e sn o t h i n g ;i t s i m

p l y c o n t i n u e so n t o t h en e x tl i n e .

F O R. . . N E X T

A n o t h e rf e a t u r ey o u ' l lb e u s i n gq u i t ea b i t i s

1 6 9

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 180/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 181/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 182/194

M a n yp r o g r a m m i n gb o o k sa n da r t i c l e sr e c o m

m e n du s i n gR E M ' ss p a r i n g l y ,s i n c ei t i s c l a i m e d

t h a tt h e ys l o wd o w na p r o g r a m ,b u ta s y o u ' l ls e e

i n t h e s e c t i o no np r o g r a ms p e e d ,i t ' sr e a l l yt h ew a y

i n w h i c hR E M sa r e u s e d ,r a t h e rt h a nt h eR E M ' s

t h e m s e l v e s ,t h a ti s r e s p o n s i b l ef o r p u t t i n gt h e

b r a k e so n o t h e r w i s es p e e d ys o f t w a r e .

M O V I N GA H E A D

T h e C o m m o d o r e1 2 8o f f e r sm a n yo t h e rB A S I

c o m m a n d st h a tw ec a n n o t b e g i nt o c o v e rh e r e .I n

f a c t ,t h e r e a r em o r et h a n1 0 0B A S I Cc o m m a n

a v a i l a b l e .W e ' l lt o u c ho nm a n yo ft h e s ei n t h eb o d y

o f t h i sb o o k .T h eo t h e r sa r eu p t o y o u .N e v e rb e

a f r a i dt o e x p e r i m e n t .I t ' st h ek e yt o l e a r n i n g .

1 7 2

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 183/194

A p p e n d i xB

A C o m p l e t eR e l a t i v eF i l eP r o g r

T h i sp r o g r a mi s a n e x a m p l eo fa r e l a t i v e( r a n d o m )

f i l ep r o g r a mu s i n ga p r o f e s s i o n a l - l o o k i n gb a rm e n ua n d f o r ml o c a t i o n sf o re n t r y .

A d d i t i o n a lr o u t i n e sf r o mt h ep a g e so ft h i sb o o k

( t os e a r c hf o ra n ds o r ti n f o r m a t i o n )c a n b ee a s i l ya d d e d .T op e r f o r m ag l o b a ls e a r c ht h r o u g ha l lf i e l d s

i n t h ef i l ey o u w o u l dd e s i g na r o u t i n et h a tw o u l dd o t h ef o l l o w i n g :

1 . R e a de a c hf i e l di n e a c hr e c o r d ,o n er e c o r da t

a t i m e .

2 . C o m p a r et h ec o n t e n t so f e a c hf i e l dw i t ht h e

s e a r c hi t e m .

3 . S e t aF O U N Df l a ga n dr e t u r ni f t h ei t e mi s

f o u n d .

F o ra s p e c i f i cs e a r c ht h r o u g h as i n g l ef i e l dy o u

w o u l d d ot h ef o l l o w i n g :

1 . R e a da s i n g l ef i e l di n e a c hr e c o r d ,o n er e c o r d

a t a t i m e .T h i sr e a dw i l ln a t u r a l l yb e b a s e do n

t h eP L A C Ea r r a y( i fF I E L Di n d i c a t e dt h ef i e l d

t h r o u g hw h i c hy o uw e r es e a r c h i n g ,t h ef i l e

p o i n t e rw o u l db e p o s i t i o n e da t P L A C E

( F I E L D ) ) .

2 . C o m p a r et h e c o n t e n t so f e a c hf i e l dw i t ht h e

s e a r c hi t e m .

3 . S e ta F O U N Df l a ga n dr e t u r ni f t h ei t e mi s

f o u n d .

T h i sr e l a t i v ef i l i n gs y s t e mu s e sr o u t i n e sa l r e a d

c o v e r e di n d e t a i lt h r o u g ht h i sb o o k .T h eo n l y

c h a n g ei s t h e i n p u tr o u t i n e ,w h i c hh a sb e e nm o d

i f i e d s l i g h t l yf o rt h i sa p p l i c a t i o n .

1 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :

2

3

4

5

6

7

1 0

2 0

3 0

r e m r a n d o mf i l ee x a m p l e

o p e ne r r o rr e mt r a p2 0 0 0 0 r e m

g o s u b6 0 0 0 0 :r e mv a r i a b l e s

c o l o r 5 , 2 : r e ma s s u m e st e x tg r p h i c s

s c n c l r

g o s u b 1 4 0 0 0: r e m o p e n

g o s u b 5 0 0 0 0: r e mm e n u

1 7 3

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 184/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 185/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 186/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 187/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 188/194

6 0 1 5 0:

6 0 1 5 2s c n c l r

6 0 1 5 5d w n $ = c h r $ ( 1 7 )

6 0 1 6 0u p $ = c h r $ ( 1 4 5 )

6 0 1 6 5r e t m $ = c h r $ ( 1 3 )

6 0 1 7 0e s c $ = c h r $ ( 2 7 )

6 0 1 7 1:

6 0 1 7 2c ( 1 ) = 0 : r ( 1 )= 1

6 0 1 7 4c ( 2 )= 1 5 : r ( 2 ) = 1

6 0 1 7 6

6 0 1 7 8

6 0 1 8 0

6 0 1 8 2

6 0 1 9 0

6 0 1 9 9

6 1 0 0 0

c ( 3 ) = 0 : r ( 3 )= 4

c ( 4 )= 1 5 : r ( 4 ) = 4

d i ma $ ( 4 )

o p e n2 , 0 :r e mo p e nk e y b o a r d ,a l l o w i n gi n

p u tw i t h o u t? m a r k

r e t u r n

r e t u r n

r e t u r n

1 7 8

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 189/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 190/194

d e v i c e s

a c c e s s i n g ,3 0

s e n d i n gi n f o r m a t i o nt o d i f f e r e n t ,

3 1

d i r e c t o r y

p r i n t i n ga , 2 6

d i r e c t o r yc o m m a n dt r i c k sw i t ht h e ,2 7

d i s k

w h e nt o H E A D E Rf r o mB A S I C ,2 3d i s kd r i v e s ,2 9

d i s ke r r o r s ,1 3 0

d i s k s

p r e p a r i n gf o ru s e ,2 3

r e p l a c i n gf i l e so n a l m o s t - f u l l ,2 8

D O / U N T I Li n s t e a do f F O R . . .N E X Tu s i n g ,4

D O Sc o m m a n d s , 2 3D O SS h e l l ,2 3d r u m ,1 5 7

d u p l i c a t ef i l e

u s i n g ,3 3

Ee i g h t yc o l u m ns i d e ,1 1

e n do f a l i n e

c l e a r i n g t ot h e ,1 8

m o v i n gt o t h e ,2 0

e n do f f i l em a r k

u s i n ga n ,5 8

e n t r y

d e f i n i n gt h ep r e v i o u s ,9 7

e n t r yf o r m s

c r e a t i n g ,1 0 5

E N V E L O P Ec o m m a n d ,1 5 8

e r r o rm e s s a g e s

m a k i n gm o r er e a d a b l e ,1 3 0e r r o rt r a p p i n g ,1 2 8

e r r o r s

d i s k ,1 3 0

e s c a p ec o m m a n d su s e df o r e d i t i n g ,

1 8

e s c a p eh a t c h ,8 8

e v e n tt r a p p i n g ,1 2 9

F

f i e l dl e n g t h s ,7 0

f i l e

o p e n i n gt h e ,7 2r e a d i n gi n f o r m a t i o nf r o ma , 5 5

f i l ep r o g r a m

r e l a t i v e ,1 7 3

f i l e s

r e l a t i v e ,6 6r u n n i n go t h e r ,2 9

f i l e st o d i s k d r i v e s

s e n d i n g ,3 3

f i l e st o t h e s c r e e n

s e n d i n g ,3 3

f l a g ,3 9

f l u t e ,1 5 7F O R. . . N E X Ts t r u c t u r e s ,1 2 2

f o r m a t t i n g ,2 3

f o r m a t t i n gn u m b e r s ,4f o r m s ,1 0 5

f o r t y - c o l u m ns i d e ,1 1

f u n c t i o nk e y s

u s i n gt h e ,9 6f u n c t i o n s

B U M P ,1 5 1

Gg a r b a g ec o l l e c t i o n ,1 2 6g r a p h i c s

s t a n d a r db i t - m a p ,1 3 6

g r a p h i c s c h a r a c t e r s

c o n v e r t i n g ,8 9

g r a p h i c ss y m b o l s ,1 7

g u i t a r ,1 5 7

HH A P P YH O M E M A K E Rp r o g r a m ,3 5

h a r m o n y

p l a y i n gi n , 1 5 6

h a r p s i c h o r d ,1 5 7H E A D E Rc o m m a n d

e n t e r i n gt h e ,2 4

h e l pk e y

r e a s s i g n i n gt h e ,9 7

h e l pk e y s

u s i n gt h e ,9 6

1

I F s

c o p i n gw i t he x t r a - l o n g ,4 3i n r l A vR OI I I U w A j\ J \ J

i n i t i a l i z e5I I I l l l G l l l j b W fw

I N P U Tc o m m a n dl i m i t a t i o n so f t h e ,8 4

i n D u t r o u t i n e

c u s t o m i z e d ,8 4d i r t y ,7 7

q u i c k ,7 7

v a r i a b l e su s e di n s i d et h e ,8 6

i t e ms e c t i o n s ,7 0

i t e m si n a r e c o r d

p l a n n i n g ,6 7

Kk e y b o a r db u f f e r ,8 3

k e y b o a r dc o n t r o l

b y p a s s i n g ,1 7

k e y i n gi n a p r o g r a m

r u l e sf o r ,1 7

k e y s

b l a c k ,1 5 5

L

l a n g u a g e

a s s e m b l y ,8

m a c h i n e ,8

l e n g t h

t e s t i n gf o r ,9 0

l e n g t h s

f i e l d ,7 0

l e n g t h

r e c o r d ,7 2

l o o p

r e s to f t h e ,8 8

l o w e r c a s el e t t e r s ,1 7

Mm a c h i n el a n g u a g e ,8

m e m o r y

C o m m o d o r e1 2 8 ,9v a r i a b l e ,1 1

m e n u s

a d - f a s h i o n e d ,1 0 9

b a r ,1 1 1

m o d ea u t o m a t i ci n s e r t ,1 8

m o d e m s ,3 1

m o d e s , 1 7

m o v i n gt o t h ee n do f a l i n e ,2 0

M O V S P Ru s i n g ,1 4 8

m u s i c ,1 5 4

Nn u l lc h a r a c t e r ,7 4

n u m b e r sf o r m a t t i n g ,4s o r t i n g ,1 0 4

t e s t i n gf o r ,8 9

OO N . . . G O S U B ,1 1 0

o p e r a t i o n a ld i f f e r e n c e s ,6 7o p t i o n a lc o l o r s o u r c e ,1 0 7

o r g a n ,1 5 7

Pp a r a m e t e r

p o s i t i o n ,7 1

p a r t i a li t e m s

s e a r c h i n gf o r ,4 4

p a r t i a lm a t c h e ss e a r c h i n gf o r ,4 9

p i a n o ,1 5 7

p i c t u r e s

d r a w i n g ,1 3 6P L A Ys t a t e m e n t ,1 5 8

p l o t t i n gp o i n t s ,1 4 0

p o i n t e r ,1 0 3

p o i n t s

p l o t t i n g ,1 4 0p o s i t i o np a r a m e t e r ,7 1

p r o g r a m

H A P P YH O M E M A K E R3 5

i m p r o v i n gt h e ,1 3 7

l o a d i n ga , 2 8

p r o g r a mc o n t r o l

a n o t h e rw a yt o r e d i r e c t ,1 6 6

p r o g r a md e s i g n ,1 0 5

p r o g r a mf i l e s

r e p l a c i n ge x i s t i n g ,2 8

p r o g r a ml i n e s

1 8 0

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 191/194

d u p l i c a t i n g ,2 1

s p l i t t i n g ,2 1

p r o g r a m m i n g

p r o f e s s i o n a l ,1 1 9p r o g r a m s

r e m a r k a b l e ,1 2 4

p r o g r a m s o nd i s k

s a v i n g ,2 7

Qq u o t em o d e , 8 5

q u o t e s

i m p u t t i n gw i t h ,6 0

Rr a n d o ma c c e s sf i l e s ,1 0r e c o r d

a d d i n ga , 8 0c h a n g i n ga , 8 1

r e c o r dl e n g t h ,7 2

r e c o r d s ,1 0 5

t r a c k i n gt h en u m b e ro f ,7 4r e c u r s i o n ,1 7 0

r e l a t e di n f o r m a t i o n

s e a r c h i n gf o r ,4 1r e l a t i v ef i l ep r o g r a m ,1 7 3

r e l a t i v ef i l er e c o r d s

p l a n n i n g ,6 7

r e m m e do f f ,8 7

R E M s ,1 7 1

r o u t i n e

m a i np r o c e s s i n g ,8 6

w r i t e - t o - f i l e ,7 5r o u t i n e s

b a c k s p a c e ,9 0o t h e r ,9 0

p r o f e s s i o n a li n p u t ,8 3r u n / s t o p

t e s t i n g ,1 3 2

SS C A L Ec o m m a n d ,1 4 0

s c r e e n

r e s t o r i n gt h e ,2 2

s c r e e nd i s p l a y ,1 1 4

s c r e e n f r o mt h ek e y b o a r d

c l e a r i n gt h e ,2 2s c r e e nt e s t

c u s t o m i z i n ga , 9 0

s e a r c h

h o wt o , 3 5

s e a r c hr o u t i n e

i m p r o v i n gt h e ,3 9

s e c t i o n s

i t e m ,7 0

s e t u pv a r i a b l e s ,7 2

s h a p et a b l e s ,1 4 4s i d e

4 0 - c o l u m n ,1 1

6 4 , 7

s i d e1 2 8 ,7s o r t ,9 8

b u b b l e ,9 8

s o r t i n g ,9 8

s o u n d ,1 5 4

s p e e du p sw i t hv a r i a b l e s ,1 2 6S P R D E F ,1 4 4S P R I T E

u s i n g ,1 4 8

s p r i t ec o o r d i n a t e s

u n t a n g l i n g ,1 4 9

s p r i t e d e f i n i t i o nc o m m a n d ,1 4 4

s p r i t e s ,1 4 3

a n i m a t i n g ,1 4 6

c r e a t i n gy o u ro w n ,1 4 5

s t a n d a r db i t - m a pg r a p h i c s ,1 3 6s t a t e m e n t

P L A Y ,1 5 8

s t r i n g

i n s i d ea , 4s t r u c t u r e s

F O R. . . N E X T ,1 2 2

s u b r o u t i n e s

m a k i n gt h em o s to f , 1 2 3

s w i t c h i n gb e t w e e n m o d e s ,

Tt a b l e s

s h a p e ,1 4 4

T e s t / d e m od i s k

1 5 7 1 ,2 3

t e s t :t r u e / f a l s e ,4 0

t e x ti n s e r t i n g ,1 8

t i m et r a v e l ,9 1

t r a pc o m m a n d ,1 2 8t r a p p i n g

e r r o r ,1 2 8

e v e n t ,1 2 9

t r u m p e t ,1 5 8

t w o - d i m e n s i o n a la r r a y ,4 1

Uu n i tn u m b e r8 , 3 0

u n i t s ,2 4

u p p e r c a s el e t t e r s ,1 7

U s e r ' sG u i d e

C o m m o d o r e1 2 8 , 1 6 5

v a r i a b l em e m o r y ,1 1

v a r i a b l e s ,3 6

B o o l e a n ,4 0s e t t i n gu p ,8 4

s e t u p ,7 2

s p e e d u p sw i t h ,1 2 6

V O Lc o m m a n d ,1 5 8

Ww i l d c a r ds y m b o l ,2 7w r i t es t r i n g ,3 2

w r i t e - t o - f i l er o u t i n e ,7 5

x y l o p h o n e ,1 5 8

E d i t e db y M a r i l y nL J o h n s o n 1 8 1

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 192/194

O t h e rB e s t s e l l e r sF r o mT A B

D 1 0 0 1T H I N G ST OD OW I T HY O U RC O M M O D O R E ®1 2 8 — S a w u s c ha n dP r o c h n o w

E x p e r ts o f t w a r ed e v e l o p e ra n d c o m p u t e rw r i t e rD a v eP r o c h n o wh a se x p a n d e d a n di m p r o v e do n t h e e x c e p t i o n a l

w o r kd o n eb yc o m p u t e rp i o n e e rM a r kS a w u s c ht o p r o d u c e

a c o l l e c t i o no f a p p l i c a t i o n sa n dp r o g r a m st h a te x p l o r e s t h e

o u t e rl i m i t so f C - 1 2 8c a p a b i l i t i e s .P a c k e dw i t hi n g e n i o u sa n di n n o v a t i v ei d e a sf o r p u t t i n gy o u rc o m p u t e rt o w o r k -i n c l u d i n gl o t so f w a y st o s a v et i m ea n dm o n e y , e v e nw a y s

t o u s ey o u rc o m p u t e rt o m a k em o n e y — i t ' s a b o o kt h a t ' ss u r e

t o i n s p i r ey o ut o c o m eu pw i t hs t i l lm o r ei d e a so f y o u ro w n .

2 0 8p p . ,1 6 7i l l u s .( 7 " x 1 0 " ) .P a p e r$ 1 2 . 9 5 H a r d$ 1 8 . 9 5

B o o kN o .2 7 5 6

□ C O M M O D O R E6 4 ™ / 1 2 8 ™G R A P H I C SA N D S O U N DP R O G R A M M I N G - 2 n dE d i t i o n - K r u t e

H e r e ' sa l l t h e h a n d s - o n ,l e a r n - b y - d o i n g i n f o r m a t i o n

y o u ' l ln e e dt o s t a r tt a k i n gf u l la d v a n t a g eo f y o u rC o m m od o r e ' s e x c e p t i o n a lg r a p h i c s p o w e r s — s p r i t e ,c h a r a c t e r ,a n db i t - m a p p e dg r a p h i c s .P l u s ,y o u ' l lf i n do u th o wt o u t i l i z ea l l

o f y o u rm a c h i n e ' sa d v a n c e dt h r e e - v o i c em u s i cs y n t h e s i z e r

c h i p .B e s to f a l l ,y o u ' l lf i n da w h o l ec o l l e c t i o n o f r e a d y - t o -

r u np r o g r a m st o d e m o n s t r a t eh o we a c hc o n c e p tw o r k so nb o t ht h eC - 6 4a n dt h eC - 1 2 8 !2 7 2p p . ,1 5 7i l l u s .( 7 "x 1 0 " ) .

P a p e r $ 1 4 . 9 5 H a r d $ 2 2 . 9 5

B o o kN o .2 6 4 0

D C O M M O D O R E6 4 ™E X P A N S I O NG U I D E — P h i l l i p sF a rm o r et h a nj u s ta p r o d u c tl i s t i n go r a r e h a s ho f

m a n u f a c t u r e r ' ss a l e s b r o c h u r e s ,t h e s e a r et h e b e s to f t h e

h u n d r e d so f h a r d w a r ea c c e s s o r i e sc u r r e n t l yo n t h em a r k e t

. . . e a c ho n ec h o s e nf o rv a l u ea n dp e r f o r m a n c ea f t e re xh a u s t i v et e s t i n ga n de x a m i n a t i o n .Y o u ' l lf i n di n - d e p t hb a c k

g r o u n do n e a c ht y p eo f d e v i c e — p r i n t e r s ,d i s kd r i v e s ,m o d e m s ,m o n i t o r s ,a n dp h o t o g r a p h i cd e t a i l s .2 8 8p p . ,3 1

i l l u s .7 " x 1 0 " .P a p e r$ 1 6 . 9 5 H a r d$ 2 2 . 9 5

B o o kN o . 1 9 6 1

□ T H EC O M P U T E RF U R N I T U R EP L A NA N DP R O J E C TB O O K — W i l e y

N o w ,e v e na n o v i c ec a nb u i l dg o o dl o o k i n g , f u n c t i o n a l ,

a n dl o w - c o s tc o m p u t e rf u r n i t u r e t h a t ' sc u s t o m - d e s i g n e df o r

y o u ro w ns p e c i a ln e e d s — t a b l e s ,s t a n d s , d e s k s ,m o d u l a ro r

b u i l t - i nu n i t s ,e v e n ap o s t u r e s u p p o r t i n gk n e e l i n gc h a i r !

C r a f t s m a nJ a c kW i l e yp r o v i d e sa l lt h es t e p - b y - s t e pg u i d a n c e ,

d e t a i l e dp r o j e c tp l a n s ,s h o w - h o wi l l u s t r a t i o n s ,a n dp r a c t i c a l

c u s t o m i z i n gd e v i c e .2 8 8p p . ,3 8 5i l l u s .7 " x 1 0 " .P a p e r$ 1 5 . 9 5 H a r d$ 2 3 . 9 5

B o o kN o .1 9 4 9

* P r i c e s s u b j e c tt o c h a n g ew i t h o u tn o t i c e .

□ T H EI L L U S T R A T E D D I C T I O N A R O F M I C R O C O M P U T E R S — 2 n dE d i t i o n — H o r d e s k

L i t t l em o r et h a na d e c a d ea f t e rt h ei n t r o d u c t i o no f t h e

f i r s tm i c r o p r o c e s s o r s ,m i c r o c o m p u t e r sh a v em a d ea m a j o r

i m p a c to ne v e r ya r e ao f t o d a y ' s b u s i n e s s ,i n d u s t r y ,a n dp e r

s o n a ll i f e s t y l e s .T h er e s u l t :a w h o l en e wl a n g u a g eo f t e r m s

a n dc o n c e p t sr e f l e c t i n g t h i sr a p i d l yd e v e l o p i n g t e c h

. . . a n d av i t a ln e e df o rc u r r e n t ,a c c u r a t e e x p l a n a o f

w h a tt h e s et e r m sa n dc o n c e p t sm e a n .M i c h a e l H o r d e sh a s

p r o v i d e dj u s tt h a ti n t h i sc o m p l e t e l yr e v i s e da n dg r e a t l ye xp a n d e dn e ws e c o n de d i t i o n o fT h eI l l u s t r a t e dD i c t i o n a r y

M i c r o c o m p u t e r s *3 6 8p p . ,3 5 7i l l u s .( 7 " x 1 0 " ) .

P a p e r$ 1 4 . 9 5 H a r d$ 2 4 . 9 5

B o o kN o .2 6 8 8

□ P R A C T I C A LI N T E R F A C I N GP R O J E C T SW I T HT H EC O M M O D O R E ™C O M P U T E R S

H a n d s - o nt e c h n i q u e sf o rt r a n s f o r m i n gy o u rC - 6 4 , C - 1

P l u s / 4 ,V I C - 2 0o r t h en e wC - 1 2 8i n t oa n a c c u r a t ec o n t r o l l

f o rs c i e n c e , e n g i n e e r i n g , o rh o m ea n d h o b b ye l e c t r o n i c sa p

p l i c a t i o n s .I n c l u d e so v e r8 0 d i f f e r e n ts o f t w a r ep r o g r a m T h i si s a s o u r c e b o o kt h a tw i l lh a v ey o uu s i n gy o u rC o m m od o r ec o m p u t e ri n s o m et r u l ye x c i t i n gn e ww a y s .2 5 6p p . ,

2 5 6i l l u s .7 " x 1 0 " .P a p e r$ 1 6 . 9 5 H a r d$ 2 4 . 9 5

B o o kN o .1 9 8 3

D 1 0 1P R O G R A M M I N GS U R P R I S E SA N DT R I C K SF O RY O U RC O M M O D O R E6 4 C O M P U T E R

T h i se x c i t i n gn e wc o l l e c t i o n o fg a m e s ,n o v e l t i e s ,a n dp r o g r a m m i n gm a r v e l si s f r e s h ,l i t e r a t e ,a n dp a c k e dw i t ha l l

k i n d so fd o w n r i g h ta m a z i n gw a y st o h a v ef u n w i t hy o u rC - 6 4 .

A n du n l i k eo t h e rp r o g r a m m i n gb o o k s ,i t m a k e sn o a t t e m p

t o i n s t r u c ty o u — i n s t e a d ,t h eo b j e c ti s t o e n t e r t a i na n d b e

e n t e r t a i n e d .2 2 4p p . ,1 2 i l l u s .7 " x 1 0 " .P a p e r$ 1 1 . 9 5 H a r d$ 1 8 . 9 5

B o o kN o . 1 9 5 1

D C O M M O D O R E6 4 ™A D V A N C E DG A M ED E S I G NS c h w e n k

P r o f e s s i o n a lg a m ed e s i g n e r sG e o r g e a n d N S c h w e n kr e v e a lt h e i rw i n n i n gf o r m u l af o rc r e a t i n gs t i m u l a t

i n g ,p r o f e s s i o n a l - q u a l i t ym i c r o c o m p u t e rg a m e sf o rf a m i l yf u n

a n de v e np r o f i t !U s i n gt h r e ef u l l y - d e v e l o p e dC - 6 4g a m e st o

i l l u s t r a t eg a m ed e s i g n ,t h i si n n o v a t i v et u t o r i a lp r o v i d e sa n

i n f o r m a t i v ea n dp r a c t i c a ll o o ka t t h ec o n c e p t u a la n di mp l e m e n t a t i o nt e c h n i q u e si n v o l v e d .1 4 4p p . ,1 4 i l l u s .7 " x

1 0 " .P a p e r$ 1 0 . 9 5 H a r d $ 1 5 . 9 5

B o o kN o . 1 9 2 3

L o o kf o rt h e s ea n do t h e rT A Bb o o k sa t y o u rl o c a lb o o k s t o r e .

T A BB O O K SI n c .

P . O .B o x4 0B l u eR i d g eS u m m i t ,P A 1 7 2 1 4

S e n df o rF R E ET A Bc a t a l o gd e s c r i b i n go v e r1 2 0 0c u r r e n tt i t l e s i np r i n t .

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 193/194

8/6/2019 Commodore 128 BASIC Programming Techniques

http://slidepdf.com/reader/full/commodore-128-basic-programming-techniques 194/194

C o m m o d o r e1 2 8 "B A S I C - .P r o g r a m m i n gT e c h n i q u e s

M a r t i nH a r d e e

T h eo n es o u r c eo n C - 1 2 8B A S I Ct h a ty o u ' l lt u r nt o

a g a i na n da g a i nf o r i n f o r m a t i o na n di n s p i r a t i o n !

Y o ud o n ' th a v et o s p e n da f o r t u n eo n a c o m p u t e ri n o r d e rt o g e tt h e

p r o g r a m m i n g p o w e ry o un e e d !N o w ,w i t ht h e h e l po f t h i su s e r - f r i e n d l yg u i d e ,

y o uc a ng e t u n b e l i e v a b l ep r o g r a m m i n gp r o d u c t i v i t ya n d v e r s a t i l i t yu s i n g

B A S I Co n y o u rC - 1 2 8 !Y o u ' l ll e a r nd i s kc o m m a n d st h a tw i l la l l o wy o ut o

s t o r ea n d r e t r i e v ed a t a ,t e l e p h o n ec o m m u n i c a t i o n st e c h n i q u e st h a tw i l l

b r o a d e ny o u rh o r i z o n s ,a n ds o u n da n dg r a p h i cc o m m a n d st h a tw i l lm a k ey o u rp r o g r a m sm o r ee n j o y a b l ea n df r i e n d l y .

L e ta n e x p e r to n t h eC - 1 2 8s h o wy o ua h o s to f c o m m a n d sa n dt r i c k s

t h a tm a k ep r o g r a m m i n gi n B A S I Co n t h eC - 1 2 8e a s i e ra n df a s t e rt h a ny o u

e v e r t h o u g h tp o s s i b l e .A p a r t o ft h et e a mt h a td e v e l o p e dt h e " m a n u a lo nd i k " i l d d fh C d1 2 8 k H d k