Top Banner
8/6/2019 Commodore 64 Assembly Language Programming http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 1/243
243

Commodore 64 Assembly Language Programming

Apr 08, 2018

Download

Documents

CubemanPDX
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 1/243

Page 2: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 2/243

Page 3: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 3/243

This cassette was provided with the book.

I had access to 2 copies and dumped both toverify the files. Two programs were on thetape. The counter went from 000 to 127.

“C64 ASSEMBLER” Start 000 End 073

“BIN/HEX/DEC TUTO” Start 073 End 121

Page 4: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 4/243

“C64 ASSEMBLER”Version 2.04 / 14-11-83 

“BIN/HEX/DEC TUTO”Version 2.02 / 14-11-83 

Page 5: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 5/243

C o m m o d o r e 6 4 A s s e m b l y L a n g u a g e P r o g r a m m i n g

Page 6: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 6/243

Page 7: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 7/243

D r W a t s o n C o m p u t e r L e a r n i n g S e r i e s

C o m m o d o r e 6 4 A s s e m b l y L a n g u a g e

P r o g r a m m i n g

D e r e k B u s h a n P e t e r H o l m e s

Page 8: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 8/243

A l p r o g r a m s i t h i s b o o k a n t h a c c o m p a n y i n g s o f t w a r e h a v e b e e nw r i t t e n e x p r e s s l y t o i l l u s t r a t e s p e c i f i c t e a c h i n g p o i n t s . T h e y a r n o w a r r a n t e d a b e i n g s u i t a b l e f o a n p a r t i c u l a r a p p l i c a t i o n . E v e r y c a r eh a b e e n t a k e n i t h w r i t i n g a n p r e s e n t a t i o n o t h i s b o o k b u n r e s p o n s i b i l i t y i a s s u m e d b t h a u t h o r o p u b l i s h e r f o a n e r r o r so o m i s s i o n s c o n t a i n e d h e r e i n .

C o m m o d o r e 6 i a t r a d e m a r k o f C o m m o d o r e B u s i n e s s M a c h i n e s ,I n c , a n d D r W a t s o n i a t r a d e m a r k o G l e n t o p P u b l i s h e r s L t d . , b o t ho w h i c h a r n o a f f i l i a t e d w i t h H a y d e n B o o k C o m p a n y , I n c .

C o p y r i g h t 1 9 8 3 , 1 9 8 4 b D e r e k B u s h a n P e t e r H o l m e s , A l r i g h t sr e s e r v e d . N p a r t o t h i s b o o k m a b r e p r i n t e d , o r e p r o d u c e d , o ru t i l i z e d i a n f o r m o b y a n y e l e c t r o n i c , m e c h a n i c a l , o r o t h e r m e a n s ,n o k n o w n o h e r e a f t e r i n v e n t e d , i n c l u d i n g p h o t o c o p y i n g a n r e c o r di n g , o i a n i n f o r m a t i o n s t o r a g e a n r e t r i e v a l s y s t e m , w i t h o u t p e m i s s i o n i w r i t i n g f r o m t h P u b l i s h e r .

P r i n t e d i t h U n i t e d S t a t e s o f A m e r i c a

1 2 3 4 5 6 7 8 9 P R I N T I N G

Page 9: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 9/243

C O N T E N T SI N T R O D U C T I O NC H A P T E R

C H A P T E R

C H A P T E R

C H A P T E R

C H A P T E R

C H A P T E R

C H A P T E R

C H A P T E R

G e t t i n g S t a r t e dU n d e r s t a n d i n g a s s e m b l y l a n g u a g e a n d m a c h i n e c W r i t i n g a n u n d e r s t a n d i n g a s s e m b l y l a n g u a g ei n s t r u c t i o n s .J u m p i n g , B r a n c h i n g , F l a g sU n c o n d i t i o n a l j u m p s , c a l c u l a t i n g a d d r e s s e s .T h p r o g r a m c o u n t e r . C o n d i t i o n a l j u m p s( b r a n c h e s ) . T h 6 5 1 0 f l a g s .

M o r e I n s t r u c t i o n s , A d d r e s s i n g , S c r e e n O u t p u t t i nS c r e e n d i s p l a y s . T i m i n g o f p r o g r a m s .M o d e s o a d d r e s s i n g .

M a t h e m a t i c a l , L o g i c a l O p e r a t o r sH e x a d e c i m a l i n p u t s . M u l t i p l i c a t i o n , d i v i s i o n ,b i n a r y - c o d e d d e c i m a l a r i t h m e t i c . L o g i c a lo p e r a t i o n s . B i n a r y m u l t i p l i c a t i o n .

A d v a n c e d F u n c t i o n s o f t h A s s e m b l e rL a b e l s , m e m o r y l a b e l s a n M a c r o s , i n s e r t i n g a n m o v i n g c o d e , c r e a t i n g D A T A s t a t e m e n t s , p r i n t i na a s s e m b l y l i s t i n g .

W i t h o u t t h A s s e m b l e rM a c h i n e c o d e i B A S I C . T h e M a c h i n e L a n g u a g eM o n i t o r . P r o t e c t i n g p r o g r a m s i m e m o r y .M a c h i n e - c o d e p r o g r a m s w i t h c o l o u r a n s o u n d .

B u i l t - i n S u b r o u t i n e sU s i n g t h 6 5 1 0 R O s u b r o u t i n e s . T h s t a c k .

I n t e r r u p t i n g t h e 6 5 1 0 , V a r i a b l e sI n t e r r u p t s . T h o v e r f l o w f l a g . N u m e r i c a ls c r e e n o u t p u t , t h e U S c o m m a n d , s i g n e d a n f l o a t i n g - p o i n t n u m b e r s a n d b u i l t - i n

Page 10: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 10/243

A P P E N D I X

A P P E N D I X

A P P E N D I X

I N D E X

B i n a r y , B i n a r y - C o d e d D e c i m a l , H e x a d e c i m a lN o t a t i o n s

T a b l e sT a b l e 1 T h 6 5 1 0 i n s t r u c t i o n s e t .T a b l e 2 C h a r a c t e r s e t .T a b l e 3 H e t d e c i m a l c o n v e r s i o n .T a b l e 4 A S C I I c h a r a c t e r s s e t .T a b l e 5 6 5 1 0 s t a t u s f l a g g u i d e .

M a p s a n V e c t o r sM e m o r y m a p s , k e r n a l v e c t o r t a b l e .

Page 11: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 11/243

I N T R O D U C T I O NT h i s b o o k i s e l f - p a c e d c o u r s e o m a c h i n e - c o d e / a s s e m b l y l a n g u a g e p r o g r a mm i n g b a s e d o t h 6 5 1 0 m i c r o p r o c e s s o r . G e t t i n g g o i n g i a s s e m b l y l a n g u a g ei s a i d t o b m a j o r p r o b l e m f o t h e b e g i n n e r . H o w e v e r , i t ' s p r o b l e m t h a tw t r u s t w i l l d i s a p p e a r w h e n t h b e g i n n e r ' s h a n d s g e o t h i s b o o k . B p l o tt i n g c a r e f u l r o u t e t h r o u g h t h e i n s t r u c t i o n s e t , w h a v e p r o d u c e d t e x t t h a tw i l l t a k e t h r e a d e r s t a g e b s t a g e t h r o u g h 6 5 1 0 a s s e m b l y l a n g u a g e m a i n t a i ni n i n t e r e s t a n u n d e r s t a n d i n g a a l t i m e s . I o r d e r t o v e r c o m e t h e u s u a ld i f f i c u l t y a s s o c i a t e d w i t h a s s e m b l y l a n g u a g e s , t h M n e m o n i c s u s e d a r t o t a ll u n i f o r m t h r o u g h o u t t h b o o k a n t h i n s t r u c t i o n s e u s e d i t h o n e m p l o y e db t h a s s e m b l e r a n d w a s w r i t t e n e s p e c i a l l y t a c c o m p a n y t h i s t e x t . M o s t o t h e x a m p l e s a r d e s i g n e d t b l o a d e d v i t h i s a s s e m b l e r a n a l a r d e s i g n e dt r u o t h C o m m o d o r e 6 4

I o r d e r t e n s u r e t h a t t h v i t a l f i r s t f e m a c h i n e c o d e p r o g r a m s r u n , t h o p e ni n c h a p t e r g o e s i g r e a t d e t a i l t h r o u g h t h s t a g e s o e n t e r i n g , a s s e m b l i n g ,l i s t i n g a n r u n n i n g t h e s e p r o g r a m s . T h o s e w h b r i n g t t h b o o k p r e - e x i s t i n gs k i l l a r a s k e d t b e a r w i t h t h t e x t i n t h e s e e a r l y s t a g e s . N o t w i t h s t a n d i n g t h g e n t l e p a c e o f t h o p e n i n g , t h r e a d e r s h o u l d f i n d h i m s e l f / h e r s e l f a c c e l e r a t i n ga l o n g w i t h t h t e x t . B t h e n o c h a p t e r e i g h t h e / s h e w i l l h a v e c o v e r e d t h w h o l e 6 5 1 0 i n s t r u c t i o n s e t .

J u s t t t e s t y o u r u n d e r s t a n d i n g a e a c h s t a g e t h t e x t i i n t e r s p e r s e d w i t hn u m e r o u s e x e r c i s e s . I a l c a s e s , t h s o l u t i o n s t o t h e s e a r p r o v i d e d a n d , i m a n y c a s e s , d e t a i l e d e x p l a n a t i o n s a r g i v e n t a i y o u r u n d e r s t a n d i n g . A y o u ' v e p a i d f o t h e s e e x e r c i s e s , d o n ' t w a s t e y o u r m o n e y — h a v e g a t h e m .T h e s e t o a r c a r e f u l l y g r a d e d t e x p a n d y o u r u n d e r s t a n d i n g o 6 5 1 0 .

A f u r t h e r a i d , t h s o f t w a r e s u p p l i e d w i t h t h i s b o o k c o n t a i n s B i n a r y B C H e x a d e c i m a l T u t o r . A p p e n d i x p r o v i d e s f u l l e x p l a n a t i o n o h o t u s t h i sp r o g r a m a n s h o u l d e n s u r e t h a t y o u n d e r s t a n d b i n a r y , b i n a r y - c o d e d d e c i m a la n h e x a d e c i m a l r e a l l y w e l l . f u r t h e r f e a t u r e o t h i s p r o g r a m t h e x e r c i s e s

w i l l e n a b l e y o t t e s t j u s t h o w e l l y o d u n d e r s t a n d . I y o u ' r e s h y , j u s tt r t h e m w h e n n o n e ' s l o o k i n g !

W e l l t h a t ' s a l t h p r o p o g a n d a w e ' v e d o n e w h a t w c a t m a k e l e a r n i n ga s s e m b l e r f u t h r e s t i u t y o u . E n j o y y o u r s e l f .

Page 12: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 12/243

Page 13: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 13/243

C H A P T E RG e t t i n g S t a r t e d

H a v i n g d r i v e n c o m p u t e r b e f o r e y o a r p r o b a b l y w e l l a w a r e t h a t' s o m e t h i n g 1 e x i s t s c a l l e d " m a c h i n e c o d e " a l o n g w i t h t h a t o t h e rt h i n g k n o w n a " a s s e m b l e r " . Q u i t e s i m p l y , m a c h i n e c o d e i s t h el a n g u a g e t h a t t h m i c r o - p r o c e s s o r c h i p i n y o u r c o m p u t e ru n d e r s t a n d s . A s a n e x a m p l e t a k e s i m p l e a d d i t i o n s u a d d i n ga n 2

I E n g l i s h y o u w o u l d s a y : -

A d o n t t w o , w h a t ' s t h a n s w e r ?

I n B A S I C y o u m i g h t s a y s o m e t h i n g l i k e : -1 L E T A2 L E = 3 4 P R I N T

I 6 5 1 0 m a c h i n e c o d e y o c o u l d s a y : -

A 0 8 8 4 0 3A 9 0 26 8 0 8 8 0 6

T h i s i p r e t t y w e l l u n i n t e l l i g i b l e , i s n ' t i t W e l l , t h a t ' s w h w u s a s s e m b l y l a n g u a g e . T h e s a m e p r o b l e m i g i v e n b e l o w i a s s e m b l yl a n g u a g e a l o n g w i t h b r i e f c o m m e n t o e a c h l i n e .

L o a d r e g i s t e r w i t h ' 1 S t o r e t h c o n t e n t s o r e g i s t e r i 9 0 L o a d t h A c c u m u l a t o r w i t h ' 2 A d t t h a c c u m u l a t o r t h c o n t e n t s o fm e m o r y l o c a t i o n 9 0

S T 9 0 S t o r e c o n t e n t s o f a c c u m u l a t o r i m e m o r yl o c a t i o n 9 0

R T R e t u r n f r o m m a c h i n e - c o d e s u b - r o u t i n e

I t ' s m u c h e a s i e r t r e a d t h a t t h a n t h m a c h i n e c o d e , i s n ' t i t W i t h a a s s e m b l e r y o u c a e n t e r y o u r p r o g r a m i a s s e m b l y l a n g u a g e

L D X I MS T L D A I MA D

9 0 9 0

Page 14: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 14/243

T h h e a r t o t h 6 5 1 0 i t h a c c u m u l a t o r ( A t h r o u g h w h i c h a l m o s ta l y o u r d a t a h a s t f l o w . I i b a s i c a l l y a e i g h t b i t s t o r e t h a tc a s t o r e n u m b e r u t 2 5 5 -

6 5 1 0 i n s t r u c t i o n s a l l o w y o u t w r i t e d i r e c t l y i n t o t h i s s t o r e u s i n gt h i n s t r u c t i o n " L o a D n u m b e r i n t o t h e A c c u m u l a t o r u s i n g I m m e d i a t eM o d e " . T h e M n e m o n i c f o t h i s i L D A I M .

L D A I M ^ L o a D A c c u m u l a t o r u s i n g I m m e d i a t e M o d e ( i . e .w i t h t h v a l u e s p e c i f i e d } .

A n o t h e r i n s t r u c t i o n a l l o w s y o t t r a n s f e r n u m b e r f r o m t h i s s t o r et a n s p e c i f i e d m e m o r y l o c a t i o n . I f t h i s m e m o r y l o c a t i o n i b e t w e e n 1 0 2 4 a n d 2 0 2 3 t h e n t h e n u m b e r t a k e n f r o m t h s t o r e w i l l b d i s p l a y e d o n t h e s c r e e n . T h i n s t r u c t i o n i s : -

S T A S T o r e c o n t e n t s o A c c u m u l a t o r i t h a d d r e s ss p e c i f i e d .

N o t e t h a t t h e T R A N S F E R c o m m a n d s y o w i l l m e e t s h o u l d r e a l l y b t h o u g h t o a C O P Y c o m m a n d s a t h e y t a k e c o p y o t h d a t a a n c r e a t e S E C O N D c o p y o f t h a t d a t a , l e a v i n g t h o r i g i n a l u n a l t e r e d .

L e t ' s h a v e a g t h e n a r u n n i n g m a c h i n e - c o d e p r o g r a m !

W w i l l c r e a t e p r o g r a m w h i c h w i l l p u n u m b e r i n t o t h ea c c u m u l a t o r a n t h e n t r a n s f e r i t t h t o p l e f t p o s i t i o n o t h s c r e e n , i . e . l o c a t i o n 1 0 2 4 .

A c o u p l e o p o i n t s a b o u t t h a s s e m b l e r : w h e n y o s t a r t t w r i t ep r o g r a m t h a s s e m b l e r n e e d s t b e t o l d w h e r e y o w a n t t h p r o g r a mt b p l a c e d i c o m p u t e r m e m o r y . T h e C o m m o d o r e 6 h a p l e n t y o m e m o r y a v a i l a b l e w h e r e w e c a n p u t m a c h i n e c o d e p r o g r a m ( i t h e o r y , w e c o u l d u s e t h w h o l e 6 4 k ) . H o w e v e r , f o r s h o r t p r o g r a m st h c a s s e t t e b u f f e r , w h i c h e x t e n d s f r o m 8 2 t 1 0 1 9 , p r o v i d e sc o n v e n i e n t 1 9 b y t e s . s a y c o n v e n i e n t a p r o g r a m s w h i c h a r s t o r e d t h e r e a r e u n l i k e l y t g e m i x e d u w i t h a n y B A S I C p r o g r a m s .S o i n o u e a r l y p r o g r a m s w e w i l l m a k e u s e o t h c a s s e t t e b u f f e ra n d t e l l t h a s s e m b l e r t s t a r t t h p r o g r a m s t h e r e .

s e c o n d p o i n t a b o u t t h a s s e m b l e r i t h a t i n i t i a l l y w w i l l o n l yu s d e c i m a l f o r m a t f o r n u m b e r s . L a t e r o n w e s h a l l s e h o o t h e rn u m b e r f o r m a t s m a y b e u s e d .

L a s t l y , t h a s s e m b l e r m u s t b t o l d w h e n w h a v e f i n i s h e d e n t e r i n gt h p r o g r a m . T h u s , t h f i r s t a n d l a s t l i n e s o t h a s s e m b l yp r o g r a m a r e : -

Page 15: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 15/243

N . B . T h u n d e r l i n i n g o S T A R T A D D R E S S ? i n t h e p r o g r a m i i n t e n d e d t i n d i c a t e t h a t t h i s h a b e e n t y p e d b t h c o m p u t e r . T h i s c o n v e n t i o n i u s e d t h r o u g h o u t t h i s b o o ka n s h o u l d m a k e i e a s i e r f o y o t i n t e r p r e t t h e x a m p l e s . R e m e m b e r , i i i u n d e r l i n e d , t h e n t h c o m p u t e r w i l l t y p e t h i s ; i f i i s n ' t u n d e r l i n e d , t h e nt h i s i t h e b i t t h a t y o w i l l h a v e t t y p e i y o u r s e l f .

T h e f i r s t a n d l a s t l i n e s s h o w n a b o v e h a v e n o t h i n g t d w i t h t h m a c h i n e c o d e p r o g r a m . T h e y s i m p l y p r o v i d e i n f o r m a t i o n t t h a s s e m b l e r . ( T h e w o r d E N D i c a l l e d P S E U D O - C O D E ) .W h e n y o h a v e p u i y o u r m a c h i n e - c o d e p r o g r a m y o u m a y r u i b u s i n g t h R U f a c i l i t y p r o v i d e d b t h a s s e m b l e r ( o b u s i n g S Y c a l l t t h s t a r t o f t h m a c h i n e c o d e - p r o g r a m i . e . S Y 8 2 i n t h i c a s e ) . E i t h e r w a y y o u m u s t t e l l t h p r o g r a m t r e t u r n f r o m a c h i n e - c o d e t B A S I C o m o n i t o r . T h e c o m m a n d t h a t d o e s t h i s i R e T u r n f r o m m a c h i n e - c o d e S u b r o u t i n e o R T S .

R i g h t , t p u t h a t i n t o t h e p r o g r a m w m u s t :

1 T e l l t h a s s e m b l e r t h a t t h e S T A R T A D D R E S S ? i 8 2 8 .

2 ^ L o a D n u m b e r ' 0 i n t o a c c u m u l a t o r . u s i n g j t a m e d i a t eM o d e . T h e M n e m o n i c f o r t h i s i L D A I M f o l l o w e d b y t h n u m b e r t b l o a d e d L D A I M

3 j S T o r e i s p e c i f i c a d d r e s s t h c o n t e n t s o t h A c c u m u l a t o r , t h M n e m o n i c i s S T A . A f t e r t h i s w e m u s tt e l l t h e 6 5 1 0 w h a t t h a d d r e s s i S T 1 0 2 4

4 J I T o r e i a n o t h e r s p e c i f i c a d d r e s s t h c o n t e n t s o t h A c c u m u l a t o r S T 5 5 2 9 6 . w i l l e x p l a i n w h w d t h i s l a t e r .

5 J R e T u r n f r o m t h S u b r o u t i n e t B A S I C — R T

6 T e l l t h a s s e m b l e r ( n o t t h 6 5 1 0 ) t E N o

P R O G R A M 1 .

S T A R T A D D R E S S ? 8 2 L D A I M 0S T 1 0 2 4S T A 5 5 2 9 6

Page 16: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 16/243

Page 17: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 17/243

N o s e l e c t ' L t l i s t t h p r o g r a m .

T h p r o g r a m t h e n a s k s y o f o t h S T A R T A D D R E S S ?

T y p e i " 8 2 8 " < r e t u r n >T h s c r e e n d i s p l a y s : -

A D D R E S SD E

8 2 8 3 8 3 8 3 e t

H E

O 3 3 CO 3 3 E0 3 4 10 3 4 4

M A C H I N EC O D E

A 9 0 08 D 0 0 0 48 0 D 6 e t c .

A S S E M B L Y C O D EP R O G R A M

L D A I MS T 1 0 2 4S T A 5 5 2 9 6R T e t c .

Y o u r m a c h i n e w i l l s h o w w h o l e s c r e e n o d a t a b u a n y t h i n gb e l o w t h e l i n e b e g i n n i n g 8 3 w i l l b i g n o r e d a t h 6 5 1 0 r e a d st h R T S ( R e T u r n f r o m m a c h i n e - c o d e S u b - r o u t i n e ) a n r e t u r n s t B A S I C .

W h a t t h a b o v e s h o w s i s t a k i n g t h f i r s t l i n e o p r o g r a m 1 .

A D D R E S SD E

8 2

H E

O 3 3 C

M A C H I N EC O D E

A 0 0 ^ ^

A S S E M B L Y C O D EP R O G R A M

L D A I M

. . M e m o r y l o c a t i o no f f i r s t b y t e o c o m m a n d i . e ." L D A I M "

I n s t r u c t i o ni m a c h i n e c o d ea n d v a l u e t b e n t e r e d •

T h u s t h l i s t i n g i b o t h c h e c k o w h a t y o e n t e r e d a n a l s og i v e s y o t h f u l l m a c h i n e - c o d e p r o g r a m o t h o b j e c t p r o g r a m .R e a d i n g t h i s o f f f r o m b e l o w t h a s s e m b l y l a n g u a g e l i s t i n g t h i si s : -

A 9 0 08 0 0 0 48 0 0 D 8

Page 18: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 18/243

T h e r e i o n p o i n t t h a t m i g h t h a v e b e e n p u z z l i n g y o u w h i c hw i l l d e a l w i t h b e f o r e i n t r o d u c e a n y t h i n g e l s e . W h d i w s t o r e z e r o i 5 5 2 9 6 T h a n s w e r t t h i s l i e s i t h w a t h C o m m o d o r e 6 p u t s c o l o u r o t h s c r e e n . I f w e s t o r ev a l u e d i r e c t l y i n t o t h s c r e e n ( a w d i w h e n w e s t o r e d t h z e r o i n 1 0 2 4 ) , t h e n w a l s o n e e d t t e l l t h c o m p u t e r t h c o l o u rt b u s e d f o t h d i s p l a y . I f w e d o n ' t t h e n t h r e s u l t i l i k e l y t b d i s p l a y e d i t h s a m e c o l o u r a s t h e s c r e e nb a c k g r o u n d , a n l i g h t g r e y c h a r a c t e r o l i g h t g r e yb a c k g r o u n d c a n ' t b s e e n a a l l . Y o w o u l d n e v e r k n o w w h e t h e ry o u r v e r y f i r s t p r o g r a m w a s u c c e s s o n o t .

S t o r i n g z e r o i n t h e f i r s t b y t e o t h c o l o u r R A M ( i . e . 5 5 2 9 6 )e n s u r e d t h a t t h e c h a r a c t e r s t o r e d i n t h e f i r s t b y t e o f t h es c r e e n ( i . e . 1 0 2 4 ) w o u l d b s h o w n u a s b l a c k c h a r a c t e r .S i m i l a r l y , i w e h a d s t o r e d v a l u e i n t o 1 0 2 5 ( t h e s e c o n dc h a r a c t e r o t h s c r e e n ) t h e n w n e e d t s t o r e c o l o u r c o d ei n t o 5 5 2 9 7 e t c . W c o u l d h a v e s t o r e d a n y v a l u e f r o m t o 1 5 i 5 5 2 9 6 a n d w e w o u l d h a v e g o o n o f t h e s i x t e e n c o l o u r s t h a t t h c o m p u t e r c a n d i s p l a y . O n e v a l u e w d o n ' t w a n t t s t o r e i t h c o l o u r R A i t h n u m b e r r e p r e s e n t i n g t h c o l o u r o t h s c r e e n b a c k g r o u n d , o c o u r s e . I n t h e c a s e o f t h e a s s e m b l e r ,t h s c r e e n b a c k g r o u n d i l i g h t g r e y w h i c h i g i v e n b t h v a l u e1 5

N o l e u l o o k a a n o t h e r i n s t r u c t i o n a n d u s e t h i s i n p r o g r a m . A s t a t e d e a r l i e r , t h a c c u m u l a t o r i t h r e p o s i t o r yo m o s t " a n s w e r s " a n t h n e i n s t r u c t i o n A D C " d o e s s u m " a n l o a d s t h a n s w e r i n t o t h a c c u m u l a t o r .

A D A D d . w i t h £ a r r y c o n t e n t s o s p e c i f i e d m e m o r yl o c a t i o n t t h a c c u m u l a t o r .

T d t h i s , h o w e v e r , w e m u s t f i r s t a d d t w l i n e s t f r o n t o f t h ep r o g r a m . T h e s e l i n e s s i m p l y g e t h 6 5 1 0 r e a d y t d s o m e a d d i n g .D o n ' t w o r r y w h a t t h e y m e a n f o n o j u s t t y n e ' e i n a n f o l l o w t h i n s t r u c t i o n s .

O n o t h e r p o i n t a b o u t t h j a r g o n ! T h e t e r m I N S T R U C T I O N i u s e d t d e s c r i b e a e x e c u t a b l e m a c h i n e c o d e s t a t e m e n t . T h u s i c o u l dc o n s i s t o L D A I M o r j u s t R T S . H o w e v e r , t h t e r m i a l s o u s e d t r e f e r t t h m n e m o n i c a l o n e , a w h e n o n e s a y s t h 6 5 1 0 i n s t r u c t i o ns e t . I t h i s b o o k , t h e t e r m C O M M A N D i u s e d t r e f e r t t h m n e m o n i c p a r t o f a n i n s t r u c t i o n w h e n t h i s p r e c i s i o n i r e q u i r e d .F o i n s t a n c e , i n t h e i n s t r u c t i o n L D A I M 0 t h L D A I M p a r t m a b

Page 19: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 19/243

P R O G R A M 1 .

S T A R T A D D R E S S ? 8 2 8 G i v e s a d d r e s s f o b e g i n n i n g o

C L C L L D A I M

S T 1 0 2 4

S T A 5 5 2 9 6

L D A I M

A D C 1 0 2 4

S T 1 0 2 6

S T A 5 5 2 9 8

R T E N

p r o g r a m .

G e t s t h 6 5 1 0 r e a d y f o r a d d i n g .L o a D " 1 i n t o t h A c c u m u l a t o r i I M m e d i a t e m o d e .S T o r e t h c o n t e n t s o A c c u m u l a t o r( 1 i 1 0 2 4 .S T o r e t h c o n t e n t s o A c c u m u l a t o r( 1 i 5 5 2 9 6L o a D " 2 i n t o t h A c c u m u l a t o r i I M m e d i a t e m o d e .A D C o n t e n t s o 1 0 2 4 ( t t h c o n t e n t s o t h a c c u m u l a t o r ( 2 ) .S T o r e t h c o n t e n t s o t h A c c u m u l a t o r ( 3 i n 1 0 2 6 .S T o r e t h c o n t e n t s o t h A c c u m u l a t o r ( 3 i n 5 5 2 9 8 .R e T u r n f r o m m a c h i n e - c o d e S u b r o u t i n e E n d a s s e m b l y .

R i g h t t h e n , l e t ' s t y p e i i n

I y o u m a k e m i s t a k e b e f o r e p r e s s i n g r e t u r n , y o m a y c o r r e c t t h m i s t a k e u s i n g t h c u r s o r k e y s w i t h t h I N S T / D E L k e a n o r m a l .H o w e v e r , i y o p r e s s r e t u r n b e f o r e y o u n o t i c e t h m i s t a k e j u s tt y p e i n " E N D " a n d s t a r t a g a i n .

1 R u t h A s s e m b l e r p r o g r a m i n o a l r e a d y r u n n i n g .2 S e l e c t " E t b e g i n e n t e r i n g a s s e m b l y p r o g r a m .3 T e l l a s s e m b l e r w h e r e t s t a r t , i . e . t y p e i " 8 2 8 " < r e t u r n >4 T y p e i n " C L D " < r e t u r n > ( p r e s s < r e t u r n > a f t e r e a c h e n t r y ) .5 T y p e i " C L C "6 T y p e i " L D A I M 1 7 T y p e i n " S T A 1 0 2 4 "8 T y p e i n " S T A 5 5 2 9 6 "9 T y p e i " L D A I M 2 1 0 T y p e i n " A D C 1 0 2 4 "1 1 T y p e i " S T A 1 0 2 6 "1 2 T y p e i n " S T A 5 5 2 9 8 "1 3 T y p e i " R T S "1 4 T y p e i n " E N D "1 5 S e l e c t " R " t o r u p r o g r a m a n d t h e n g i v e s t a r t a d d r e s s

Page 20: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 20/243

A n o t h e r w a o l o o k i n g a t t h e t h r e e l i n e s

L D A I MS T 1 0 2 4 .S T A 5 5 2 9 6

i a w a o p u t t i n g " 1 i n t o m e m o r y o r o f p r i n t i n g A S C I I " I i w h i t e ( o a " A " ) o t h s c r e e n .

T h e 6 5 1 0 h a s t w i n d e x r e g i s t e r s i a d d i t i o n t i t a c c u m u l a t o r a n t h e s e a r e r e f e r r e d t a I n d e x r e g i s t e r s a n Y a n d e a c h c a n s t o r eo n 8 - b i t n u m b e r . T h a r r a n g e m e n t o t h e s e o r a t h j a r g o n h a si t t h A R C H I T E C T U R E o f t h e 6 5 1 0 i s h o w n b e l o w ( i p a r t ) i F i g .1 . 1 .

D a t a

B u

A c c u mu l a t o r

A L

F i g . 1 .

I n t h i s f i g u r e t h a n d Y r e g i s t e r s a r s h o w n i d e n t i c a l l y ,a l t h o u g h t h e y d d i f f e r s l i g h t l y . N e v e r t h e l e s s , t h e y a r e b o t hi n d e x r e g i s t e r s . T h e r e a l a d v a n t a g e o i n d e x r e g i s t e r s i t h a t w ec a i n c r e m e n t ( i n c r e a s e b y 1 o d e c r e m e n t ( d e c r e a s e b y 1 t h v a l u e w h i c h t h e y c o n t a i n a n d , i n a d d i t i o n , w e c a n u s t h e m t ' s t e pt h r o u g h 1 m e m o r y , s t h e y a r v e r y p o w e r f u l a w s h a l l s e e l a t e r .T t h r i g h t o f t h f i g u r e i t h ' A L U ' o A r i t h m e t i c a n L o g i cU n i t w h i c h i u s e d b y t h 6 5 1 0 f o r a l a r i t h m e t i c a n l o g i c a lo p e r a t i o n s w h i c h i n e e d s t c a r r y o u t . T h e A L U h a t w i n p u t s f o t h d a t a t h a t i m a n i p u l a t e s a n d o n e o u t p u t w h i c h f e e d s t h r e s u l to f t h e o p e r a t i o n i n t o t h a c c u m u l a t o r . N o t i c e t h a t a l m o s t a l d a t af l o w s t h r o u g h t h a c c u m u l a t o r a n t h i s m a k e s t h a c c u m u l a t o r k e f e a t u r e o t h 6 5 1 0 . D a t a f l o w s b e t w e e n t h v a r i o u s r e g i s t e r sa l o n g t h ' D a t a B u s * w h i c h i c o m m o n p a t h w a y f o c o m m u n i c a t i o nw i t h i n t h e 6 5 1 0 . F o r t a l k i n g t d e v i c e s b e y o n d t h c h i p t h i s d a t ab u i e x t e n d e d t a c c e s s m e m o r y a l s o .

Page 21: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 21/243

L D J L o a j D i n d e x r e g i s t e r w i t h d a t a f r o m t h s p e c i f i e d a d d r e s s , i . e .

L D 9 0 m e a n s J L o a J D i n d e x r e g i s t e r 3 w i t h t h d a t a i m e m o r y l o c a t i o n 9 0 0 .

L D X d i f f e r s f r o m t h e a r l i e r " L D A I M 1 1 ( a p a r t f r o m o n l o a d i n g t h A c c u m u l a t o r a n d o n e r e g i s t e r ) i t h a t t h L D A I M c o m m a n d i a I m m e d i a t e M o d e c o m m a n d . W h e n t h 6 5 1 0 s e e s t h i s i l o o k s f o rw h a t ' s i m m e d i a t e l y f o l l o w i n g t h i n s t r u c t i o n a n d l o a d s t h a t a d a t a i n t o t h A c c u m u l a t o r . W i t h t h n e w c o m m a n d a b o v e " L D X " t h 6 5 1 0 l o o k s f o w h a t f o l l o w s a n d t h i s s p e c i f i e s t h A D D R E S S o t h d a t a . T h u s w i t h t h i n s t r u c t i o n : -

L D 9 0

t h 6 5 1 0 g o e s t m e m o r y l o c a t i o n 9 0 t f i n d t h e d a t a w h i c h i l o a d s i n t o t h e X - r e g i s t e r . T h i s i n s t r u c t i o n ( a a r e a l l t h r e g i s t e r i n s t r u c t i o n s ) i r e a l l y C O P Y a s t h e d a t a p u i n t o t h X - r e g i s t e r i C O P I E D f r o m l o c a t i o n 9 0 0 . T h a t i s t o s a y , t h d a t ao r i g i n a l l y s t o r e d i n m e m o r y l o c a t i o n 9 0 0 r e m a i n s t h e r e .

T r e c o v e r t h d a t a w m a u s t h i n s t r u c t i o n : -

S T S I T o r e t h c o n t e n t s o f r e g i s t e r X i t h s p e c i f i e d a d d r e s s .

S T 1 0 2 4 m e a n s S T o r e c o n t e n t s o 3 r e g i s t e r i n t om e m o r y l o c a t i o n 1 0 2 4 .

H e r e ' s t h

P R O G R A M 1 .

p r o g r a m !

S T A R T A D D ? 8 2 8L D A I MS T 1 0 2 4

S T

L D

5 5 2 9 6

1 0 2 4

L o a d ' 1 i n t o t h a c c u m u l a t o rS t o r e t h c o n t e n t s o f a c c u m u l a t o r i 1 0 2 4S t o r e t h c o n t e n t s o a c c u m u l a t o r i 5 5 2 9 6 t g i v e t h s c r e e n d i s p l a yc o l o u r ( g i v e s w h i t e )L o a d i n t o X - r e g i s t e r , c o n t e n t s o m e m o r y l o c a t i o n 1 0 2 4 ( i . e . " 1 " )

Page 22: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 22/243

A M e n u s e l e c t ' R t r u t h p r o g r a m .

T h s c r e e n s h o u l d d i s p l a y " A " , " s p a c e " , " A ( b o t h A ' s c o l o u r e dw h i t e ) a t t h e t o p l e f t - h a n d p o s i t i o n .B n o y o u s h o u l d b a b l e t w r i t e s i m p l e p r o g r a m s s o , a s a e x e r c i s e t r y t h e f o l l o w i n g : -

E x e r c i s e 1 .

L o a d t h A c c u m u l a t o r d i r e c t l y w i t h ' d i s p l a y t h i s ( s c r e e n' A 1 ) i 1 0 2 4 . A n s w e r o p a g e 9 - 1 .

R e m e m b e r t p u " 1 i n t o 5 5 2 9 6 t g i v e t h d i s p l a y t h c o l o u rw h i t e .

D o n ' t f o r g e t t p u i n t h e " R T S " a t t h e e n d . I y o d f o r g e t t h e nt h 6 5 1 0 w i l l r u o t s e w h a t i c a f i n d a n d t r y t e x e c u t et h i s . I y o a r l u c k y , t h e n t h 6 5 1 0 m a s i m p l y r e t u r n t B A S I C .H o w e v e r , w i t h y o u r l u c k , i w i l l p r o b a b l y f i n d s o m e t h i n g t h a tc r a s h e s t h s y s t e m . T h c r a s h m a b r e c o v e r a b l e , i w h i c h c a s ep r e s s i n g t h R U N / S T O P k e t o g e t h e r w i t h t h R E S T O R E k e y m a y r e t u r nt h c o m p u t e r t B A S I C . I f p r e s s i n g t h R U N / S T O P a n R E S T O R E k e y st o g e t h e r d o e s n o t r e s t o r e y o t B A S I C t h e n t h c r a s h i n o r e c o v e r a b l e a n i w i l l b n e c e s s a r y t s w i t c h t h c o m p u t e r o f a n o a g a i n , r e - l o a d t h a s s e m b l e r a n s t a r t a g a i n .

E x e r c i s e .

W r i t e y o u r n a m e i n t h e t o l e f t h a n d c o r n e r o t h s c r e e n . O n ep o s s i b l e a n s w e r o p a g e 9 -

E x e r c i s e .

P u t a n ' X i e a c h o t h f o u r c o r n e r s o t h s c r e e n . A n s w e r o p a g e 9 - 2 .

Y o u w i l l n e e d t k n o w t h a t t h s c r e e n o t h c o m p u t e r n o r m a l l yo c c u p i e s t h 1 0 0 0 m e m o r y l o c a t i o n s f r o m 1 0 2 4 t 2 0 2 3 . T h e 1 0 0 0m e m o r y l o c a t i o n s i n e e d e d t p r o v i d e d 2 r o w s e a c h o f 4 0c h a r a c t e r s . A s w e h a v e s e e n , 1 0 2 4 i s t h e t o p l e f t h a n d p o s i t i o n o t h s c r e e n , a n d s i n c e 1 0 2 6 w a t h t h i r d p o s i t i o n . .

* *

Page 23: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 23/243

L D Y I M

S T

L o a D r e g i s t e r Y I m m e d i a t e ! f o d e .

S T o r e t h d a t as p e c i f i e d a d d r e s s .

w i t h

i

d a t a

t h

s p e c i f i e d

^ - r e g i s t e r

i

a

T h e s e i n s t r u c t i o n s a r s h o w n b e l o w i n t h e d i a g r a m s : -

1 0 2 5

Page 24: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 24/243

Y o u s h o u l d n o k n o w , o r b a b l e t i n t e r p r e t t h f o l l o w i n g ; -R T L D

L D A I MS T A D

L D L D X I MS T

L D L D Y I MS T

F o m a n y o p e r a t i o n s , B U N O A L L , t h e X a n d Y r e g i s t e r s c a b t r e a t e d i n t e r c h a n g e a b l y ; f o i n s t a n c e p r o g r a m 1 . c o u l d b w r i t t e n : -

P R O G R A M 1 .

S T A R T A D D ? 8 2 L D A I MS T S T L D S T S T R T E N

o

P R O G R A M . 3

1 0 2 45 5 2 9 61 0 2 41 0 2 65 5 2 9 8

S T A R T A D D ? 8 2 8L D A I MS T S T L D S T S T R T E N

1 0 2 45 5 2 9 61 0 2 41 0 2 65 5 2 9 8

B e c a u s e o t h i s i n t e r c h a n g e a b i l i t y a n t h n e e d t s w a p d a t ar a p i d l y b e t w e e n r e g i s t e r s d u r i n g t h r u o p r o g r a m s e v e r a li n s t r u c t i o n s e x i s t t o d o t h i s a u t o m a t i c a l l y . T h e y a r t y p i f i e db y : -

T A T r a n s f e r t h c o n t e n t s o t h A c c u m u l a t o ri n t o t h i n d e x r e g i s t e r 3

U s i n g t h i s c o m m a n d i p r o g r a m 1 . ( t p r o d u c e p r o g r a m 1 . 4 ) , m a k e s

Page 25: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 25/243

P R O G R A M 1 . S T A R T A D D ? 8 2 L D A I MS T 1 0 2 4S T A 5 5 2 9 6T A S T 1 0 2 6S T 5 5 2 9 8R T E N

W h e n t h i s p r o g r a m i r u t h s c r e e n s h o u l d d i s p l a y t w w h i t e " A " s ,o n e i n 1 0 2 4 a n d o n e i n 1 0 2 6 .

D e s c r i p t i o n s g i v e n o f t h e c o d e s s f a h a v e b e e n s p e l l e d o u i d e t a i l . H o w e v e r , a y o a r g e t t i n g m o r e u s e d t t h j a r g o n , i t i sr e a s o n a b l e t b e g i n t a b b r e v i a t e . P r o m n o w o n i n s t e a d o " t h ec o n t e n t s o t h X r e g i s t e r " , w e w i l l j u s t r e f e r t a n s i m i l a r l ys w i t h t h Y r e g i s t e r ( Y a n a c c u m u l a t o r ( A ) . T h u s , s u m m a r y o t h t r a n s f e r i n s t r u c t i o n s i s : -

T A

T X

T r a n s f e r

t r a n s f e r

A i n t o X

T A

i n t o / t

T Y

t r a n s f e r

T r a n s f e r

A i n t o £

i n t o A

E x e r c i s e 1 .

W r i t e p r o g r a m t h a t l o a d s " Z i n t o t h a c c u m u l a t o r a n a " A i n t o t h e X r e g i s t e r . T h e n , w i t h o u t u s i n g a n f u r t h e r I m m e d i a t eM o d e c o m m a n d s , s w a p s t h e s e o v e r a n d p r i n t s t h " Z o t h f i r s ts c r e e n m e m o r y l o c a t i o n a n t h " A o t h l a s t .

p o s s i b l e a n s w e r o p a g e 9 - 3 *

E x e r c i s e 1 .

W r i t e p r o g r a m t h a t : L o a d s d i a m o n d i n t o t h a c c u m u l a t o r , a na s t e r i s k i n t o a n a " E i n t o Y T h e n , w i t h o u t u s i n g a n y f u r t h e rI m m e d i a t e M o d e c o m m a n d s , m o v e s t h " E i n t o A t h d i a m o n d i n t oa n t h a s t e r i s k i n t o Y P r i n t t h d i a m o n d i t h s c r e e nb o t t o m l e f t , t h e a s t e r i s k i n t h e b o t t o m r i g h t a n t w M E " s , o n e i n

Page 26: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 26/243

t h b o t t o m l e f t h a n d c o r n e r a n t h c o r r e s p o n d i n g ; m e m o r y -l o c a t i o n s f o t h c o l o u r s .

Page 27: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 27/243

C H A P T E RJ u m p i n g , B r a n c h i n g , F l a g s

F e r e a l l i f e p r o g r a m s p r o c e e d a l o n g s m o o t h u n i n t e r r u p t e d p a t hw i t h o u t j u m p i n g o b r a n c h i n g a s o m e s t a g e . T h i s c h a p t e r l o o k s a t h o s e c o m m a n d s a n t h e i r u s e s a n t h e n e x a m i n e s t h f l a g s t h a te n a b l e t h b r a n c h e s t b c o n t r o l l e d .

U n c o n d i t i o n a l J u m p sT h e s e t e l l t h e p r o g r a m t j u m p w i l l y - n i l l y n c o n d i t i o n s . O n l yt w s u c h 6 5 1 0 i n s t r u c t i o n s e x i s t ; t h f i r s t t b c o n s i d e r e d i s : -

J M J u M P t t h s p e c i f i e d a d d r e s s .

F o i n s t a n c e , J M P 8 3 4 m e a n s j u m p t m e m o r y l o c a t i o n 8 3 4 .

P u i p r o g r a m a n i w i l l l o o k l i k e t h i s : -

L D A I M

J M 8 3

» S T

S T

J M

1 0 2 4

5 5 2 9 6

8 3

F i g . 2 .

S u c h j u m p r o u t i n e d o e s n ' t r e a l l y a c h i e v e l o b u i c o u l d , f o ri n s t a n c e , b u s e d t p a t c h p i e c e o f c o d e i n t o p r o g r a m . I

Page 28: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 28/243

N o t h i s c a n b e t y p e d i

P R O G R A M 2 .

S T A R T A D D R E S S ?L D A I MJ M R T S T S T J M E N

8 3

1 0 2 45 5 2 9 68 3

O n c e a g a i n , i c a b r u b s e l e c t i n g ' R o t h m e n u a n t h e ns t a r t i n g t h p r o g r a m a 8 2 8 . W h e n r u n , i s h o u l d g i v e w h i t e " A i t h e t o p l e f t h a n d c o r n e r o t h s c r e e n .

W h e n t h j u m p s a r e u s e d i t h i s w a i t ' s n e c e s s a r y t t e l l t h p r o g r a m e x a c t l y w h e r e t j u m p t o i . e . t g i v e a n A D D R E S S , h e n c eJ M 8 3 4 . C a l c u l a t i n g t h e s e a d d r e s s e s i q u i t e s t r a i g h t f o r w a r d a l o n g a - i i d o n e s y s t e m a t i c a l l y . F o r i n s t a n c e , a l t h c o m m a n d so " S O U R C E - C O D E S " : R T S , L D A I M , J M P , e t c . , t a k e u p o n b y t e o m e m o r y , t h u s , t j u m p o v e r R T i n F i g . 2 . 2 a w j u m p f r o m 8 3 t 8 3 4 , o v e r 8 3 w h i c h c o n t a i n s R T S .

- P S E U D OO P E R A T I O N

8 2 8 3 8 3 8 3 8 3 8 4

L D A I MJ M R T S T S T J M

8 2 8 3 1 / 2 8 3 8 3 5 / 68 3 8 / 98 4 1 / 2

1 0 2 45 5 2 9 68 3

P S E U D O O P F i g . 2 . 2 a

T h p a r t f o l l o w i n g t h s o u r c e c o d e i k n o w n a t h O P E R A N D a n c a l c u l a t i n g i t l e n g t h i b i m o r e c o m p l i c a t e d !

T h e a s y w a y i s i m p l y t l o o k i u p i n A p p e n d i x H e r e y o w i l lf i n d c o m p l e t e l i s t o a l t h s o u r c e c o d e s a v a i l a b l e o t h 6 5 1 0m i c r o p r o c e s s o r . F o r i n s t a n c e , a t h b o t t o m o p a g e - A 2 - 1 9 y o w i l lf i n d t h e n t r y f o r t h R T i n s t r u c t i o n . U n d e r t h h e a d i n g N O B Y T E S O P E R . ( n u m b e r o b y t e s i o p e r a n d ) y o u w i l l f i n d t h v a l u e

Page 29: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 29/243

t c o n f i r m t h i s . O t h e r i n s t r u c t i o n s t h a t y o h a v e m e t r e q u i r et w b y t e s a t h e i r o p e r a n d s a r e g r e a t e r t h e n 2 5 5 . F o r i n s t a n c e ,J M h a s a n e n t r y o p a g e A 2 - 1 2 s h o w i n g t h a t i h a t w b y t eo p e r a n d , a n s o n B y t h e w a y , k n o w t h l i s t o f s o u r c e c o d e s i r a t h e r f r i g h t e n i n g , b u D O N ' T P A N I C , t a k e t h e m a t h e y c o m e .F i g u r e 2 . 2 a s h o w s t h l o c a t i o n o f t h v a r i o u s i n s t r u c t i o n s a n o p e r a n d s f o p r o g r a m 2 . 1 .

L D A I M t a k e s u p t w o b y t e s o n e f o r i t s o b j e c t c o d eA 9 a n d o n e f o r t h n u m b e r t b l o a d e d i n t ot h a c c u m u l a t o r . R e m e m b e r t h a c c u m u l a t o ri o n l y o n b y t e l o n g , s o i c a o n l y h o l dn u m b e r u t 2 5

J M 8 3 4 t a k e s t h r e e b y t e s o n e f o r J M ( 4 C ) a n d t w of o t h a d d r e s s h e r e 8 3

R T t a k e s o n l y o n b y t e ( 6 0 ) i h a n o p e r a n da s d o T A X , T X A , e t c .

S T 1 0 2 4 T h i s t a k e s t h r e e b y t e s o n e f o r S T ( 8 D )a n t w f o 1 0 2 4

S T 5 5 2 9 6 T h i s t a k e s t h r e e b y t e s o n f o S T ( 8 D )a n t w f o r 5 5 2 9 6

J M P 8 3 3 t a k e s t h r e e b y t e s o n f o J M P ( 4 C ) a n d t w of o t h a d d r e s s h e r e 8 3 3

T h i s c a n a l s o b s e e n b y u s i n g t h " L I S T " c o m m a n d o n t h a s s e m b l e rM E N U . R e t u r n t M E N U a n t y p e " L f o L I S T , t h e n t e l l t h a s s e m b l e r w h e r e t s t a r t l i s t i n g , i . e . t y p e i n " 8 2 8 " •T h s c r e e n w i l l d i s p l a y : -

8 2 8 3 8 3 8 3 8 3 8 4

O 3 3 CO 3 3 E0 3 4 10 3 4 20 3 4 50 3 4 8

A 4 6 8 8 4

0 4

0 0 4

0 0 D 0

L D A I MJ M R T S T S T J M

8 3 1 0 2 45 5 2 9 68 3

F i g . 2 . 2 b

A F i 2 . 2 b s h o w s , t h a s s e m b l e r b r e a k s t h i n s t r u c t i o n s a n o p e r a n d s d o w n i n t o o n e - b y t e c h u n k s . V c a c a l c u l a t e t h t o t a l

Page 30: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 30/243

J S j J u m p t & u b - R o u t i n e .

T h i s i a n o t h e r j u m p c o m m a n d w h i c h i u s e d a l o n g w i t h R T S a n t o g e t h e r t h e s e a r l i k e G O S U B . . . R E T U R N i B A S I C .

1 B A S I CG O S U B 2 0 0

A S S E M B L E R8 3 J S R 8 3 4

2 0 R E M * * * S U B - R O U T I N E3 0 R E T U R N

8 3 S T 1 0 2 4

8 4 R T

W e c a n m o d i f y p r o g r a m 2 . t u s t h i s i n s t r u c t i o n i n s t e a d o t h s t r a i g h t J M P u s e d t h e r e . T h p r o g r a m t h e n b e c o m e s : -

F i g . 2 .

P R O G R A M 2 .

S T A R T A D D R E S S ? 8 2 L D A I M

J S 8 3 R T

S T 1 0 2 4S T A 5 5 2 9 6

P s e u d o - c o d eL o a d A c c u m u l a t o r i i m m e d i a t e m o d ew i t hJ u m p t t h s u b r o u t i n e a 8 3 R e t u r n f r o m s u b r o u t i n e ( i . e . b a c k t B A S I C )S t o r e c o n t e n t s o a c c u m u l a t o r i 1 0 2 4S t o r e a c c u m u l a t o r i 5 5 2 9 6

Page 31: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 31/243

2 . w e h a d t p u i n t h e J M 8 5 t h a v e t h s a m e e f f e c t a R T i t h i s p r o g r a m . T h e 6 5 1 0 d o e s t h i s t r i c k b u s o t h

P R O G R A M C O U N T E R ( P C )

T h i s i 1 6 - b i t r e g i s t e r w h i c h c o n t a i n s t h a d d r e s s o t h n e x tc o m m a n d w h i c h i s t o b e x e c u t e d . I r e a l i t y a l i t i s i s t w 8 - b i tm e m o r i e s , o n f o r e a c h b y t e , w h i c h i b u i l t i n t o t h 6 5 1 0 c h i p .W h e n y o u s e l e c t ' R a t t h e a s s e m b l e r M E N U a n d t h e n t y p e i n 8 2 8 ,t h i s g e n e r a t e s c o m m a n d t h a t s e t s P C t 8 2 a n s t a r t s e x e c u t i o nf r o m t h e r e . A t h P f e t c h e s e a c h b y t e f r o m m e m o r y i i i n c r e m e n t e d b t h u s a l w a y s p o i n t i n g t t h n e x t m e m o r y l o c a t i o nc o n t a i n i n g t h r e q u i r e d d a t a .

T a k e t h e f i r s t t h r e e l i n e s o f p r o g r a m 2 . 2 f o r i n s t a n c e :

S T A R T 8 2 L D A I MJ S 8 3

s u m m a r y o f t h e P C c o u n t e r c o n t e n t s d u r i n g e x e c u t i o n o f t h i s i g i v e n i F i g . 2 . 3 -

P R O G R A M

S T A R T 8 2 8L D A I M

J S 8 3

P B E F O R EE X E C U T I O N

8 2

8 3

P A F T E RE X E C U T I O N

8 2 8 3

8 3 F i g . 2 .

T h i s f i g u r e i l l u s t r a t e s h o w t h P s t e p s t h r o u g h t h p r o g r a m u n t i li c o m e s t t h e J S R c o m m a n d . I t h e n t a k e s t h j u m p c o m m a n d a n s e t s t h P t t h a d d r e s s s p e c i f i e d , i . e . 8 3 4 - A i t i s o n l y t w b y t e s l o n g a n d c a n t h u s o n l y s t o r e o n a d d r e s s , t h P u s e s s o m ee x t e r n a l m e m o r y w h e n i n e e d s t r e m e m b e r m o r e t h a n o n e . T h i s a r e ao m e m o r y , t h e s t a c k , i d i s c u s s e d o p a g e 7 - E x e r c i s e 2 .

W r i t e p r o g r a m t p u i t h a c c u m u l a t o r . T h p r o g r a m i s t os t a r t a t 8 2 8 , t h e n j u m p t s u b - r o u t i n e a 9 0 w h i c h a d d s 3 t t h

a l r e a d y i A r e t u r n t o r i g i n a l r o u t i n e a n p r i n t t h a c c u m u l a t o r s u o n t o t h t o l e f t - h a n d c o r n e r o t h s c r e e n .

Page 32: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 32/243

C o n d i t i o n a l J u m p sW h a v e a l r e a d y l o o k e d a u n c o n d i t i o n a l j u m p s b u a n p r o g r a m t h a tn e e d s t t e s t f o c o n d i t i o n s n e e d s C O N D I T I O N A L J U M P S . I B A S I C ,t h a n a l o g y i w i t h t h I F . . . T H E N c o m m a n d ,

1 0 I F X = Y T H E N 5 0

I n t h i s l i n e t h v a l u e s X a n d Y w h i c h h a v e b e e n s t o r e d i m e m o r y ,a r c o m p a r e d .

T h 6 5 1 0 c a r r i e s o u t h i s o p e r a t i o n i s e v e r a l d i f f e r e n t w a y s o n o t h e s e i b u s i n g s p e c i a l r e g i s t e r k n o w n a t h S T A T U SR E G I S T E R ( S R ) , s o m e t i m e s k n o w n a t h P R O C E S S O R S T A T U S W O R D . T h S i a e i g h t b i t r e g i s t e r l i k e t h a c c u m u l a t o r a n a n r e g i s t e r , b u i t i s u s e d q u i t e d i f f e r e n t l y f r o m t h e s e . W h e r e a s t h o t h e r r e g i s t e r s a r e u s e d t s t o r e a n m a n i p u l a t e b y t e s , t h e S R i t r e a t e d a s i f i c o n t a i n e d e i g h t i n d i v i d u a l b i t s w h i c h a r u s e d a s i g n a l s o f l a g s . T h 6 5 1 0 n o r m a l l y o n l y h a n d l e s o n s t a t u s f l a g( a t h e y a r k n o w n ) a t i m e , e i t h e r s e t t i n g t h b i v a l u e t ' 0 o ' o t e s t i n g t h s t a t u s f l a g t d e t e r m i n e w h e t h e r i t i s s e ( T o c l e a r e d ( ' 0 ' ) .O n e x a m p l e o f t h e s t a t u s f l a g s i t h f l a g o r t h e Z E R O f l a g .W h e n e v e r a a r i t h m e t i c p r o c e s s ( o j u s t m o v e ) i c a r r i e d o u t t h a tp r o d u c e s r e s u l t o z e r o i n t h e a p p r o p r i a t e r e g i s t e r ( A o Y t h e n t h f l a g i s e t ' 1 * . I f o t h o t h e r h a n d t h r e s u l t o t h p r o c e s s i n o n - z e r o t h e n t h e f l a g i s e t ' 0 * . L o o k a i t h i s w a y : i f t h e f l a g i S E ( t ' 1 ' ) t h e n t h c o n d i t i o n ( o s t a t u s ) i T R U E , i t h f l a g i C L E A R ( t ' 0 ' ) t h e n t h s t a t u s i F A L S E . T h f l a g i c o n c e r n e d w i t h z e r o c o n d i t i o n o s t a t u s , s

f l a g s e m e a n s t h a t i i t r u e t h a t t h e c o n d i t i o n i z e r o .

S e v e r a l d i f f e r e n t i n s t r u c t i o n s c a n s e t t h i s f l a g , o n e o f t h e s eb e i n g : -

D E X D E c r e m e n t t h c o n t e n t s o t h 3 r e g i s t e r .T h s e g m e n t o P r o g r a m 2 . b e l o w d e m o n s t r a t e s t h i s i u s e .

P R O G R A M 2 . ( I n P a r t )

S T A R T A D D R E S S ? 8 2 L D X I M 1 0 D E

Page 33: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 33/243

t h f l a g a n b r i n g s a b o u t b r a n c h d e p e n d e n t u p o n w h e t h e r o n o i i s e t . S u c h a i n s t r u c t i o n i s

B E j B r a n c h i r e s u l t w a s E Q u a l t z e r o . ( i . e .i f t h e Z E R O f l a g i s e t T )

T h i s c h e c k s o t h s t a t u s o f t h f l a g a n i i i s e ( t 1 ) b r a n c h e s a s p e c i f i e d . T h e o p e r a n d i t h i s c a s e i o n l y o n e b y t el o n g , s o n l y t 2 5 c a b a c c o m m o d a t e d . A t h e s e 2 5 n u m b e r sa r n e e d e d f o b r a n c h i n g i b o t h d i r e c t i o n s 0 t 1 2 a r e a s s i g n e dt f o r w a r d j u m p s , f o e x a m p l e f 6 0 f g i v i n g f o r w a r d j u m p o 6 s t e p s w h i l e 1 2 t 2 5 a r u s e d f o b a c k w a r d s t e p s . I n t h e c a s e o t h l a t t e r b r a n c h i n s t r u c t i o n o f s a y , 2 0 g i v e s b a c k w a r d s t e po f 2 5 6 2 0 0 o r 5 s t e p s .

T h e B E Q i n s t r u c t i o n i i l l u s t r a t e d i P r o g r a m 2 . b e l o w , w h e r e i c h e c k s t h c o n d i t i o n o f t h e f l a g , a n d , i s e t , b r a n c h e s f o r w a r db y t e s .

P R O G R A M 2 .

S T A R T A D D R E S S ? 8 2 8L D X I M 1 0 D E B E J M 8 3 S T 1 0 2 4S T X 5 5 2 9 6R T E N

W h e n r u n t h i s p r o g r a m p r i n t s b l a c k " @ i 1 0 2 4 .A w i t h m a n y X - r e g i s t e r i n s t r u c t i o n s , D E X h a s c o r r e s p o n d i n gr e g i s t e r i n s t r u c t i o n : -

D E D E c r e m e n t t h c o n t e n t s o t h _ r e g i s t e r .E x e r c i s e 2 .

W r i t e p r o g r a m t c a r r y o u t t h e s a m e o p e r a t i o n a P r o g r a m 2 . b u u t i l i s i n g t h r e g i s t e r . A n s w e r o n p a g e 9 - 4 .

s e c o n d i n s t r u c t i o n t h a t a l s o c h e c k s t h f l a g i s

Page 34: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 34/243

P R O G R A M 2 .

S T A R T A D D R E S S ? 8 2 L D X I M 1 0 D E B N E . 2 5 3S T X 1 0 2 4S T X 5 5 2 9 6R T E N

W h e n r u n , t h p r o g r a m i i d e n t i c a l i e f f e c t t P r o g r a m 2 . 3 a n dp u t s b l a c k " @ i 1 0 2 4 .

T h i n d e x r e g i s t e r s h a v e b e e n i n d e x e d d o w n w a r d s b t h D E a n D E c o m m a n d s . A y o m i g h t e x p e c t , t h e y c a n a l s o b i n d e x e d u p w a r d s .T h i s i d o n e b m e a n s o f : -

I N

I N

I N c r e m e n t

I N c r e m e n t

t h

t h

c o n t e n t s

c o n t e n t s

o

o

b b

o n e .

o n e .

I n s t r u c t i o n s t c o m p a r e v a l u e s

N a t u r a l l y , w h e n i n c r e m e n t i n g , s t r a i g h t c h e c k f o r z e r o i n o p o s s i b l e s t h r e g i s t e r s m u s t b c o m p a r e d a g a i n s t v a l u ep r e v i o u s l y s e s o m e w h e r e a n d t h e 6 5 1 0 p o s s e s s e s t h r e e i n s t r u c t i o n st d o t h i s . T h e f i r s t o f t h e s e i n s t r u c t i o n s t b e x a m i n e d i s : -

C P C o m P a r . e t h c o n t e n t s o t h s p e c i f i e d m e m o r ya d d r e s s w i t h t h r e g i s t e r .

T h i s i a c t u a l l y d o n e b s u b t r a c t i n g t h m e m o r y c o n t e n t s f r o m a n c a t h u s g i v e p o s i t i v e , n e g a t i v e o r z e r o v a l u e . T h u s , t h i n s t r u c t i o n C P X 9 0 0 d o e s t h e f o l l o w i n g : -

1 R e a d c o n t e n t s o m e m o r y l o c a t i o n 9 0 0 .2 S u b t r a c t t h e s e c o n t e n t s f r o m t h o s e o t h e X r e g i s t e r .3 S e f l a g i a n s w e r O . ( A l s o s e t s o t h e r f l a g s n o t y e t

c o n s i d e r e d . )N O T E :

N e i t h e r t h c o n t e n t s o t h m e m o r y l o c a t i o n n o t h r e g i s t e r a r c h a n g e d d u r i n g t h i s o p e r a t i o n .

Page 35: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 35/243

L o a d 9 0i n t o

S t o r ei 8 9

L o a d " 0 i n t o X r e g i s t e r

J f I n c r e m e n t

r e g i s t e rb

C o m p a r e8 9 0 w i t h

r e g i s t e r

J u m p b a c kt

i n c r e m e n tY E

R e t u r nt

B A S I C

S t o r ei 1 0 2 4

F i g . 2 . 5

W r i t t e n i n t o p r o g r a m i l o o k s l i k e t h i s :

P R O G R A M 2 .

S T A R T A D D R E S S ? 8 2 8■ L D A I M 9

S T A 8 9 0

L D X I MI N C P X 8 9 0

B E

J M 8 3 S T 1 0 2 4L D A I MS T 5 5 2 9 6R T

E N

L o a d 9 ( d i a m o n d ) i n t oA c c u m u l a t o r .S t o r e c o n t e n t s o A c c u m u l a t o ri n 8 9 0 ,L o a d ' 0 i n t o r e g i s t e rI n c r e m e n t r e g i s t e rC o m p a r e v a l u e i n r e g i s t e rw i t h t h a t i n 8 9 ( i . e . 9 0 )B r a n c h f o r w a r d t h r e e b y t e s i C P X a n s w e r = 0J u m p t m e m o r y l o c a t i o n 8 3 S t o r e c o n t e n t s o i 1 0 2 4L o a d o n i n t o A c c u m u l a t o r "S t o r e i t i n c o l o u r R A M t g i v e

w h i t e i m a g e o t h s c r e e nR e t u r n f r o m m a c h i n e " c o d e t B A S I C ,

Page 36: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 36/243

N o t e t h a t a l t h o u g h X i l o a d e d w i t h ' 0 f , t h i s i i m m e d i a t e l yi n c r e m e n t e d t ' 1 a t I N X .

L o o pN u m b e r

e t c .8 8 9

A c c u m u l a t o rc o n t e n t s

9 9 9

9 9 9

X - r e g i s t e rc o n t e n t s

8 8 9

Z - f l a g

F i g . 2 . 6

A s t a g e 9 0 B E i a c t i v a t e d a n p r o g r a m j u m p s b y t e s t S T 1 0 2 4a n t h e n R T S .

N o t y p e i t h p r o g r a m a n d r u n i t I s h o u l d d i s p l a y d i a m o n d i 1 0 2 4 .T h c o m p a r e i n s t r u c t i o n C P h a c o r r e s p o n d i n g i n s t r u c t i o n f o r t h e

r e g i s t e r :

C P C o m p a r e t h c o n t e n t s o s p e c i f i e d l o c a t i o n w i t ht h o s e i t h r e g i s t e r .

I t o p e r a t i o n c o r r e s p o n d s e x a c t l y w i t h t h a t f o r C P X .

E x e r c i s e 2 . R e - w r i t e P r o g r a m 2 . t u s t h Y - r e g i s t e r r a t h e r t h a n t h a n o c o m p l e t i o n o t h l o o p p r i n t o u p u r p l e h e a r t a 1 0 3 4( y o u n e e d a i c o l o u r R A M t o b t a i n p u r p l e )A n s w e r o p a g e 9 - 4 -

T h t h i r d c o m p a r e i n s t r u c t i o n i s

C M £ o M P a r e t h c o n t e n t s o f t h e s p e c i f i e d m e m o r y w i t ht h A c c u m u l a t o r .

T h i s i p a r t i c u l a r l y u s e f u l a s t h e r e s u l t s o f a l l a r i t h m e t i co p e r a t i o n s a r d e p o s i t e d i n t h e A c c u m u l a t o r a n C M a l l o w s d i r e c t

Page 37: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 37/243

P R O G R A M 2 .

S T A R T A D D R E S S ? 8 2 L D X I ML D A I M 8 I N S T X 9 0 0C M 9 0 B N E 2 4 7S T 1 0 2 4L D A I MS T 5 5 2 9 6

R T E N

L o a d ' 0 i n t o X L o a d a n ' 8 3 ' ( h e a r t ) i n t oI n c r e m e n tS t o r e i 9 0 C o m p a r e w i t h 9 0 B r a n c h i N o E q u a lS t o r e i n 1 0 2 4L o a d ' 1 i n t o t h A c c u m u l a t o rS t o r e i c o l o u r R A M t g e w h i td i s p l a yR e t u r n f r o m S u b r o u t i n e

L e t u s h a v e a n o t h e r l o o k a p r o g r a m 2 . 5 » I t u r n s o u t h a t u s i n gC P t p u i n t o t h f l a g w a s b i l i k e u s i n gs l e d g e h a m m e r t c r a c k r a t h e r s m a l l n u a t h f l a g i v e r yr e a d i l y s e t . I n f a c t , t h f l a g i s e t f 1 w h e n e v e r z e r o i p a s s e d f r o m m e m o r y t t h A c c u m u l a t o r , t t h X o t t h r e g i s t e r s o w h e n z e r o a n s w e r i o b t a i n e d t a a r i t h m e t i cp r o c e s s . T h u s , t h p r o g r a m 2 . c o u l d h a v e b e e n w r i t t e n o m i t t i n gt h C P 8 9 i n s t r u c t i o n .

T d e m o n s t r a t e t h i s , p r o g r a m 2 . i r e - w r i t t e n u s i n g t h r e g i s t e ra n D E Y a n d t e s t i n g w i t h t h c o m m a n d B N E , a s h o w n i f i g u r e 2 . b e l o w : -

L o a d4

i n t o.

L o a d1 0 i n t o D e c r e m e n t

A z Y e \ = \

. N

P r i n to u

4

F i g . 2 .

Page 38: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 38/243

A n t h p r o g r a m : -

P R O G R A M 2 .

S T A R T A D D R E S S ? 8 2 L D A I M 4 L D Y I M 1 0 D E B N E 2 5 3S T A 1 0 2 4L D A I MS T 5 5 2 9 6R T E N

T h i n s t r u c t i o n B N 2 5 g i v e s b r a n c h o 2 5 6 - 2 5 3 . T h i s i b a c k w a r d b r a n c h o t a k i n g t h p r o g r a m b a c k t D E a s t h e s t e p i c o u n t e d f r o m t h b e g i n n i n g o f t h e n e x t i n s t r u c t i o n w h e t h e r t h b r a n c h i f o r w a r d o r b a c k w a r d .

I y o u h a v e n ' t r u p r o g r a m 2 . b n o w , h a v e a g o , i s h o u l d p u w h i t e a s t e r i s k i 1 0 2 4 !

E x e r c i s e 2 .

N o w w r i t e t h p r o g r a m f o t h f l o w d i a g r a m b e l o w : -

S t a r t )L o a d

" 0 i n t o

L o a d" 8 0 "i n t o

I n c r e m e n tC o m p a r eR e g i s t e r

w i t h

' A r e

^ e q u a l

N

P r i n to u

v a l u eo

Y e

F i g . 2 .

I y o r u i n t o d i f f i c u l t yf l o w - d i a g r a m o p a g e 9 - s h o u l du n t i l y o u ' v e h a a t r y , t h o u g h .

w i t hh e l p .

t h i s , t h m o r e d e t a i l e dP L E A S E d o n ' t l o o k a t t h i s

Page 39: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 39/243

T h 6 5 1 0 F l a g sP r o g r a m 2 . u s e d t h Z - f l a g w h i c h i o n l y o n o f l a g s a v a i l a b l eo t h 6 5 1 0 . A s t h e s e e a c h o n l y c o n t a i n o n B I o f d a t a , i . e . a' 0 o T t h e y c a a l b s t o r e d i o n b y t e o m e m o r y t h

P r o c e s s o r S t a t u s R e g i s t e r . T h u s t h f l a g s a r e c o n t a i n e d i t h S a s h o w n b e l o w i F i g u r e 2 . 8 .

B i n u m b e r

F l a gF i g . 2 .

I t i s n o p r o p o s e d t g i v e f u l l d e s c r i p t i o n o a l t h e s e n o w ;t h e y w i l l , h o w e v e r , b d e s c r i b e d a n i l l u s t r a t e d w h e n t h e y a r ee n c o u n t e r e d . T h e Z - f l a g t h Z e r o f l a g h a s , o f c o u r s e , a l r e a d yb e e n m e t a n u s e d i p r o g r a m s a n e x e r c i s e s .

T h f u n c t i o n o t h f l a g s i s u m m a r i s e d b e l o w : -

N e g a t i v e f l a g . S e w h e n a a r i t h m e t i c o p e r a t i o nr e s u l t s i n e g a t i v e r e s u l t . T h e f l a g i c o n t r o l l e db t h i n s t r u c t i o n A D C , A N D , A S L , B I T , C M P , C P Y , C P X ,D E C , D E X , D E Y , E O R , I N C , I N X , I N Y , L D A , L D X , L D Y , L S R ,O R A , P L A , P L P , R O L , R O R , T A X , T A Y , T X A , T Y A .

O v e r f l o w f l a g . S e w h e n a a r i t h m e t i c o p e r a t i o nr e s u l t s i a o v e r f l o w f r o m b i t 7 a n d t e l l s t h a tr e s u l t w i l l b w r o n g u n l e s s o v e r f l o w a l l o w e d f o r . T h f l a g i c o n t r o l l e d b t h i n s t r u c t i o n A D C , B I T , C L V ,P L P , R T I , S B C .

B r e a k f l a g . S e w h e n p r o g r a m m e d i n t e r r u p t i b r o u g h t a b o u t b B R i n s t r u c t i o n .

D e c i m a l f l a g . S e w h e n a r i t h m e t i c o p e r a t i o n s a r t b c a r r i e d o u i n d e c i m a l . T h e f l a g i c o n t r o l l e d b t h i n s t r u c t i o n s C L D , P L P , R T I , S E D .

I n t e r r u p t f l a g . S e w h e n a i n t e r r u p t s e q u e n c e i i o p e r a t i o n . T h f l a g i c o n t r o l l e d b t h i n s t r u c t i o n sB R K , C L I , P L P , R T I , S E I .

Z e r o f l a g . S e t w h e n , a a r i t h m e t i c o p e r a t i o n r e s u l t si z e r o a n s w e r . T h e f l a g i c o n t r o l l e d b t h

Page 40: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 40/243

l o s s o a b i t . T h e f l a g i c o n t r o l l e d b A D C , A S L ,C L C , C M P , C P X , C P Y , L S R , P L P , R O L , R O R , R T I , S B C , S E C .

T h e N - F l a gT h e N - f l a g , w h i c h i b i o F i g . 2 . 8 , i t h N e g a t i v e f l a g t h a t i s e t ' 1 w h e n t h a n s w e r t a o p e r a t i o n i n e g a t i v e . I c a b t e s t e d b y t w i n s t r u c t i o n s , o n o w h i c h i s : -

B M I S r a n c h o M l i n u s .A i n s t r u c t i o n s u c h a B M 2 w o u l d t e s t t h f l a g a n i f s e tb r a n c h t h p r o g r a m f o r w a r d 2 7 b y t e s .A e x a m p l e o t h u s o B M i g i v e n i p r o g r a m 2 . 8 , w h e r e t h c o n t e n t s o f Y a r e i n c r e m e n t e d u n t i l C P c o m m a n d n l o n g e r g i v e sm i n u s r e s u l t a n c o n s e q u e n t l y c e a s e s t b r a n c h .

P R O G R A M 2 . 8

S T A R T A D D R E S S ? 8 2 8L D A I M 9 S T 9 0

L D Y I MI N C P Y 9 0 0

B M 2 5

S T 1 0 2 4L D A I MS T A 5 5 2 9 6

R T E N

L o a d 9 0 i n t oS t o r e A c c u m u l a t o r c o n t e n t si n 9 0 0 ,L o a d ' 0 i n t o ' YI n c r e m e n tC o m p a r e c o n t e n t s o 9 0 w i t hc o n t e n t s o Y B r a n c h o m i n u s , i . e . t e s tf l a gS t o r e c o n t e n t s i 1 0 2 4L o a d A c c u m u l a t o r w i t hS t o r e i c o l o u r R A M t g e w h i t e d i s p l a yR e t u r n f r o m s u b r o u t i n e

O n r u n n i n g , t h i s p r o g r a m s h o u l d p u w h i t e d i a m o n d i 1 0 2 4 .

I a d d i t i o n t t h B M I c o m m a n d , t h e N f l a g i a l s o t e s t e d b t h c o m m a n d : -

B P I J r a n c h o n P L u s .

Page 41: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 41/243

b r o u g h t a b o u t b r a n c h . A s t h e Y r e g i s t e r i d e c r e m e n t e d , h o w e v e r ,t h C P t e s t y i e l d s m i n u s a 9 9 a n t h B r a n c h o P L u s ( B P L )t e s t f a i l s a l l o w i n g t h p r o g r a m t r u t h r o u g h t t h e n d .

P R O G R A M 2 .

S T A R T A D D R E S S ? 8 2 8L D A I M 9 S T 9 0 L D Y I M 1 0 D E C P Y 9 0 0B P L 2 5 0S T 1 0 2 4L D A I MS T A 5 5 2 9 6R T E N

W h e n r u n , t h i s p r o g r a m w i l l p r i n t 9 ( d i a m o n d ) c o l o u r e d y e l l o w i 1 0 2 4 .

E x e r c i s e 2 .

W r i t e p r o g r a m u s i n g B P t b r a n c h w h e n r e g i s t e r g o e s n e g a t i v eh a v i n g b e e n d e c r e m e n t e d f r o m 9 0 t h e n p r i n t o u t h c u r r e n t v a l u eo X

p o s s i b l e a n s w e r o p a g e 9 - 5 .

Page 42: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 42/243

Page 43: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 43/243

C H A P T E RA d d r e s s i n g , S c r e e n O u t p u t t i n g

O n o t h a d v a n t a g e s o m a c h i n e c o d e p r o g r a m s i t h e i r s p e e d o o p e r a t i o n a n d t h i s n a t u r a l l y f a c i l i t a t e s s c r e e n d i s p l a y s .A n i m a t i o n , f o r i n s t a n c e , c a b a c h i e v e d b m e a n s o c o m m a n d s s u c ha s : -

S T A X S I T o r e t h c o n t e n t s o t h A c c u m u l a t o r i n t h es p e c i f i e d a d d r e s s i n d e x e d w i t h t h j r e g i s t e r .

T h u s , i f X c o n t a i n s 1 0 a n t h a c c u m u l a t o r 9 0 t h i n s t r u c t i o n

S T A X 1 0 2 4w i l l p u d i a m o n d i n t o ( 1 0 2 4 + 1 0 0 ) . W h e n u s e d a l o n g w i t h t h i n c r e m e n t i n s t r u c t i o n , t h i s e n a b l e s t h l o c a t i o n o t h s c r e e n t b i n d e x e d . P r o g r a m 3 * d e m o n s t r a t e s t h i s .

P R O G R A M 3 .

S T A R T A D D R E S S ? 8 2 L D X I M 1 0 L D A I M 9 S T A X 1 0 2 3L D A I M - S T A X 5 5 2 9 5D E B N 2 4 R T E N

L o a d 1 0 i n t oL o a d 9 ( d i a m o n d ) i n t oO u t p u t d i a m o n d a ( 1 0 2 3 + X )L o a d i n t oE n s u r e c o l o u r i w h i t eD e c r e m e n t v a l u eB r a n c h o n o t e q u a l

W h e n r u n , t h i s p r o g r a m p u t s d i a m o n d i t h f i r s t 1 0 l o c a t i o n s o t h s c r e e n . O n c e a g a i n , t h ' X c o m m a n d h a c o r r e s p o n d i n g ' Y c o m m a n d :

S T A Y S T o r e t h c o n t e n t s o t h A c c u m u l a t o r i t h s p e c i f i e d a d d r e s s i n d e x e d w i t h t h Y r e g i s t e r .

E x e r c i s e 3 »

M o d i f y p r o g r a m 3 - t u s t h r e g i s t e r r a t h e r t h a n t h r e g i s t e r , u s i n g o n l y d i r e c t P O K E c o m m a n d s .

Page 44: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 44/243

E x e r c i s e 3 .

P r i n t a a s t e r i s k i n t h e f i r s t 1 0 s c r e e n l o c a t i o n s u s i n g a n I N Xc o m m a n d t i n c r e m e n t .

p o s s i b l e a n s w e r o p a g e 9 - 6 .

I e x e r c i s e 3 * t h b r a n c h i n s t r u c t i o n w a a c t i v a t e d b z e r og e n e r a t e d b c o m p a r e c o m m a n d . H o w e v e r , i f t h e o r e g i s t e r i i n c r e m e n t e d f r o m 2 5 5 , i c l o c k s b a c k t z e r o a n d r e s e t s t h f l a g .I c a n t h u s b u s e d t b r a n c h w i t h o u t c o m p a r e i i i i n i t i a l l ys e t t h a p p r o p r i a t e v a l u e . P r o g r a m 3 . p e r f o r m s s i m i l a rf u n c t i o n t 3 . b u u s e s I N r a t h e r t h a n D E X . I i n c r e m e n t s f r o m2 1 t 2 5 5 a n d o t h e 3 9 l o o p s t h r o u g h , p l u s t h n e x t l o o p , p r i n t so u t a n a s t e r i s k . O n c e a 2 5 t h 8 b i t r e g i s t e r i f u l l o T a n d t h e a d d i t i o n o o n m o r e ' 1 r i p p l e s t h r o u g h e a c h b i t o f t h s t o r e , r e s e t t i n g t h e m a l t ' O ' s . T h 6 5 1 0 d o e s n o t i c e t h i ss w i t c h i n g o v e r a n d s e t s f l a g t r e m e m b e r t h e v e n t ( d i s c u s s e do p a g e 2 - 1 3 ' ) .T h i s p r o g r a m o f f e r s n a d v a n t a g e o v e r 3 * o i t o w b u m a i p a r t i c u l a r c o n t e x t b a d v a n t a g e o u s .

P R O G R A M 3 .

S T A R T A D D ? 8 2 L D X I M 2 1 6L D A I M 4 S T A X 8 0 L D A I MS T A X 5 5 0 8 0I N B N E 2 4 3R T E N

A w i t h B A S I C p r o g r a m s c h a r a c t e r c a b m o v e d a c r o s s t h s c r e e nb f i l l i n g t h s c r e e n w i t h t h c h a r a c t e r w h i l e P O K I N G b l a n k o n s p a c e b e h i n d i t P r o g r a m 3 - d e m o n s t r a t e s t h i s t y p e o r o u t i n e .

Page 45: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 45/243

P R O G R A M 3 -

S T A R T A D D R E S S ? 8 2 L D X I ML D Y I M 3 S T Y 9 0 0L D A I M 9 S T 9 0 S T A X 1 0 2 4L D A I MS T A X 5 5 2 9 6T Y S T A X 1 0 2 3L D 9 0 I N B N E 2 3 8R T E N

W h e n r u n , t h p r o g r a m r u n s w h i t e d i a m o n d a c r o s s t h s c r e e n t 1 2 7 9 - Y o u m a y b q u i t e a h o m e w i t h t h i s g e n e r a l t e c h n i q u e o a n i m a t i o n ; i s p l e a s e i g n o r e F i g u r e 3 - 1 • I n o t , y o u s h o u l d s t e pt h r o u g h t h i s s t a g e b s t a g e .

C o m m a n d

L D X I ML D Y I M 3 S T Y 9 0 0L D A I M 9 S T 9 0 S T A X 1 0 2 4L D A I MS T A X 5 5 2 9 6T Y S T A X 1 0 2 3L D 9 0 I N B N 2 3 S T A X 1 0 2 4T Y S T A X 1 0 2 3

e t c .S T A X 1 0 2 4L D A I MS T A X 5 5 2 9 6T Y

A c

9 9 9

3 3 9 9 9 9 3 3

X - R e g

u n t i l9

3

2 5 2 5 2 5 2 5

Y - R e g

3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

3 3 3 3

9 0

3 3 3 3 3 3 3 3 3 3 3 3 3 3

3 3 3 3

9 0

9 9 9 9 9 9 9 9 9 9 9 9

9 9 9 9

S c r e e nm e m .

1 0 2 41 0 2 41 0 2 41 0 2 41 0 2 31 0 2 31 0 2 31 0 2 31 0 2 51 0 2 51 0 2 4

1 2 7 91 2 7 91 2 7 91 2 7 9

C o n t e n t

9 9 9 9 3 3 3 3 9 9 3

9 9 9 9

Z - f l a g

Page 46: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 46/243

A w r i t t e n , p r o g r a m 3 - i b n m e a n s t h o n l y w a y o d o i n g t h j o b a n d a y o n d o u b t o b s e r v e d , i t i s l o n g w a f r o m b e i n g t h b e s t ; n e v e r t h e l e s s i d o e s f l a s h t h d i a m o n d t w h e r e i t ' sr e q u i r e d . L a t e r o i t h i s c h a p t e r w w i l l d e v e l o p m o r ea c c e p t a b l e v e r s i o n o f t h e p r o g r a m , b u i o r d e r t d t h i s w m u s tf i r s t l o o k a t h p r o b l e m s a s s o c i a t e d w i t h . . .

T h T i m i n g o P r o g r a m sP r o g r a m 3 « h i g h l i g h t s o n o t h p r o b l e m s o m a c h i n e c o d e f e p a g e s a g i w a s a n a d v a n t a g e ! S P E E D . W h e r e a s i B A S I C i t ' s n o to f t e n n e c e s s a r y t s l o w t h i n g s d o w n , t h a t ' s n o s i m a c h i n e c o d e .T h 6 5 1 0 c h i p t a k e s i t o p e r a t i n g s p e e d f r o m a i n t e r n a l c l o c kd r i v e n f r o m c r y s t a l o s c i l l a t o r , w h i c h i t h C o m m o d o r e 6 c o m p u t e rr u n s a M H ( t w o M e g a H e r t z ) o t w m i l l i o n c y c l e s p e r s e c o n d .T h u s e a c h c y c l e t a k e s 1 m i l l i o n t h o s e c o n d a n s p e e d s o o p e r a t i o n o t h v a r i o u s i n s t r u c t i o n s w i l l b r e f e r r e d t b t h n u m b e r o f c y c l e s t h a t i t a k e s f o t h e m t b c a r r i e d o u t , o E X E C U T E D . S o m e o f t h e s e o p e r a t i o n s t a k e p l a c e e n t i r e l y w i t h i n t h 6 5 1 0 a n d a r c a r r i e d o u m u c h q u i c k e r t h a n t h o s e w h i c h a r r e q u i r e dt r e t r i e v e d a t a f r o m m e m o r y . T h e i n s t r u c t i o n T A X , f o r i n s t a n c e ,t a k e s t w o c y c l e s t e x e c u t e w h i l e S T A X t a k e s s i x .

C l e a r l y , k n o w l e d g e o t h t i m e t a k e n f o t h i n s t r u c t i o n s t b e x e c u t e d i i m p o r t a n t a i i t h i s t h a t d e t e r m i n e s t h s p e e d o o p e r a t i o n o t h p r o g r a m a n a l s o a l l o w s u s t b m a d e o t h 2 M H z c l o c k f o t i m i n g l o o p s a n d d e l a y s .L o o k i n g b a c k a p r o g r a m 3 * 3 , t h t i m e t h a t c h a r a c t e r r e m a i n s o t h s c r e e n p r i o r t " m o v i n g o n c a b c a l c u l a t e d . T h u s , t h ed i a m o n d a p p e a r s a S T A X 1 0 2 4 , a n t h n e x t f e w s t a g e s a r t a b u l a t e db e l o w : -

C o m m a n d

S T A X 1 0 2 4

L D A I M

S T A X 5 5 2 9 6

T Y

S T A X 1 0 2 3

L D 9 0

E x e c u t i o n t i m e( c y c l e s )

E l a p s e d T i m e( c y c l e s )

1

1

Page 47: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 47/243

L D A I M

S T A X 5 5 2 9 6

T Y

S T A X 1 0 2 3

2 3

3

4

S o f r o m d i a m o n d a p p e a r i n g t i t b e i n g o v e r w r i t t e n b b l a n k i 4 c y c l e s o 2 0 . 5 m i c r o - s e c o n d s . O v e r a l l , t h 2 5 d i a m o n d s a r w r i t t e n i a b o u t 5 2 4 8 m i c r o - s e c o n d s o 5 - m i l l i - s e c o n d s , m u c f a s t e r t h a n t h e e y e c a n f o l l o w . I n d e e d , s i n c e t h t e l e v i s o n s c r e e i o n l y s c a n n e d o n c e e v e r y 2 m i l l i - s e c o n d s ( E u r o p e a n P A L s y s t e m o 1 6 . 7 m i l l i - s e c o n d s ( U S A N T S C s y s t e m ) t h e n t h i s i m u c h f a s t e t h a n y o u r t e l e v i s i o n s c r e e n c a f o l l o w .

T a t t a i n m o r e l e i s u r e l y p r o g r e s s a c r o s s t h s c r e e n d e l a y c o u l b p r o g r a m m e d i t a l l o w t h d i a m o n d t s t a y i v i e w l o n g e r . S u c

d e v i c e i s i m p l e i p r i n c i p l e b u m a y n e e d s o m e c a r e w h e i m p l e m e n t i n g i a c t u a l a p p l i c a t i o n . P r o g r a m 3 . b e l o w s h o w ss i m p l e d e l a y l o o p .

P R O G R A M 3 .

S T A R T A D D R E S S ? 8 2 L D X I M 2 5 c y c l e sD E c y c l e sB N 2 5 c y c l e sR T

T h i s g i v e s ( i g n o r i n g L D X I M ' s c y c l e s ) d e l a y o c y c l e s p e l o o p , o 2 5 0 x 5 = 1 2 5 0 c y c l e s p e e x e c u t i o n . E v e n w h e n r u r i g h tt h r o u g h , d e l a y o o n l y 6 2 m i c r o - s e c o n d s i o b t a i n e d a n t h i sm u s t b a u g m e n t e d b n e s t i n g t h i s l o o p w i t h i n a n o t h e r o n a p r o g r a m 3 * 5 s h o w s .

P R O G R A M 3 .

S T A R T A D D R E S S ? 8 2 L D Y I M 2 0 0

► L D X I M 2 5 r - ^ D E X1 — B N E 2 5

D E B N E 2 4 8R T

c y c l e sc y c l e sc y c l e sc y c l e sc y c l e sc y c l e s

Page 48: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 48/243

I w w i s h t u s t h c o m p u t e r f o p r e c i s i o n t i m i n g , t h e n w c a c l e a r l y n o i g n o r e t h o d t w m i c r o - s e c o n d s h e r e a n d t h e r e a n d w em u s t s t e p v e r y c a r e f u l l y t h r o u g h t h p r o g r a m t e n s u r e t h a t w ea c c o u n t f o r a l l p a r t s o f t h p r o g r a m . I n p a r t i c u l a r w m u s t w a t c ht h b r a n c h i n s t r u c t i o n s . F o i n s t a n c e , t h B N i p r o g r a m 3 - n o r m a l l y t a k e s t h r e e c y c l e s , i . e . w h e n t h b r a n c h s u c c e e d s .H o w e v e r , w h e n t h b r a n c h f a i l s a n d t h p r o g r a m r u n s p a s t . t h ei n s t r u c t i o n i t a k e s o n l y t w c y c l e s . U n d e r o t h e r c o n d i t i o n s , i t h b r a n c h t a k e s t h p r o g r a m i n t o a n o t h e r s e c t i o n o m e m o r y ( i . e .a n o t h e r " p a g e " , s e e p a g e 3 - 9 f o r d i s c u s s i o n ) , t h i n s t r u c t i o n t a k e sa e x t r a t w c y c l e s !

A n o t h e r p r o b l e m p e c u l i a r t t h C o m m o d o r e 6 4 i s t h a t t h e V I c h i p w h i c h c o n t r o l s t h s c r e e n d i s p l a y c a i n t e r f e r e w i t h t i m i n g .W h a t w i t h i t S p r i t e s a n d a l l , t h V I C o h i p h a s m u c h w o r k t d t h a t e v e r y s o f t e n i n e e d s t t a k e o v e r c o n t r o l o t h m e m o r y a n w h e n i d o e s t h 6 5 1 0 s i m p l y h a t w a i t . T h i s m e a n s t h a t i f y o r e a l l y n e e d t u s t h 6 5 1 0 f o r a c c u r a t e t i m i n g , i t i s n e c e s s a r y t s t o p t h V I C c h i p f r o m d i s p l a y i n g w h i l e y o u r u t h t i m i n g c o d e ,a n r e i n s t a t e t h d i s p l a y a f t e r w a r d s n o t v e r y d i f f i c u l t t d o e s p e c i a l l y i t h A s s e m b l e r .

H o w e v e r , w e w e r e p r i n c i p a l l y i n t e r e s t e d i d e l a y s i o r d e r t s l o wd o w n o u a n i m a t i o n s l e t ' s t r y p u t t i n g s o m e d e l a y s i n t o p r o g r a m( 3 . 3 ) t c h e c k t h a t t h e y r e a l l y d w o r k !

P r o g r a m 3 . u s e s 3 - 3 a b a s i s a n i n s e r t s t h d e l a y l o o pi l l u s t r a t e d i 3 . p u t t i n g 0 . 6 m i l l i s e c o n d s b e t w e e n t h a p p e a r a n c ea n d i s a p p e a r a n c e o d i a m o n d .

P R O G R A M 3 .

S T A R TL D Y I ML D A I MS T L D A I MS T L D A I MS T L D X I ML D S T A YL D S T A YD E B N

A D D R E S S ? 8 2

9 9 0

9 0 3 9 0 2 5 9 0 1 0 2 49 0 5 5 2 9 6

2 5

S e u d i a m o n d

S e u c o l o u r w h i t e

S e u b l a n k

S e t u p f o r d e l a yL o a d a n D i s p l a yd i a m o n dL o a d c o l o u r w h i t e

D e l a yl o o p

Page 49: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 49/243

T r y e n t e r i n g t h i s p r o g r a m a n r u n n i n g i t D i s a p p o i n t i n g i s n ' t i t T h e t r u t h o f t h e m a t t e r i t h a t 0 . m i l l i - s e c o n d s i m p l y i s n ' t l o n e n o u g h . T h t e l e v i s i o n s c r e e n i o n l y r e f r e s h e d e v e r y i / 5 0 t s e c o n d ( E u r o p e a n P A L ) o r i / 6 0 t h s e c o n d ( U S A N T S C ) s o i t t a k e r o u g h l y 1 6 - 2 0 m i l l i s e c o n d s f o t h s c r e e n t b s c a n n e d . I f o u l i t t l e w h i t e d i a m o n d i o n l y o n t h e s c r e e n f o a p p r o x i m a t e l y o n t h i r t i e t h o f t h a t t i m e , t h c h a n c e s a r e t h a t o n l y o n e i n t h i r t y o o u r d i a m o n d s i g o i n g t b s e e n ( i . e . o n e o r t w o l i n e ) . I f y o l o o k c a r e f u l l y , a n y o h a v e t l o o k c a r e f u l l y , y o u w i l l d e t e c t h a t t h s i t u a t i o n i s b i b e t t e r t h a n t h a t , r o u g h l y t h r e e o r f o u

l i n e . M e n g i n e e r i n g f r i e n d s t e l l m e t h a t t h i s i b e c a u s e o s o m e t h i n g c a l l e d ' i n t e r l a c e ' a n h a v e t b e l i e v e t h e m . I a n c a s e , i a p p e a r s t h a t t h e d e l a y p r o d u c e d b p r o g r a m 3 * i t h e r e f o r e a b o u t f i f t e e n t i m e s t o s h o r t .

H o t h e n c a n w e i n c r e a s e t h d e l a y ? T h r e g i s t e r c a o n l y h o l dm a x i m u m o f 2 5 5 a n d w a r a l r e a d y u s i n g c o u n t o 2 5 0 , s t h e r i s n ' t m u c h s c o p e f o r i n c r e a s i n g t h n u m b e r o f t i m e s a r o u n d t h l o o u n l e s s w e u s e t h d o u b l e l o o p i l l u s t r a t e d i p r o g r a m 3 . 5 . B u t h e x t e n t o f t h e d e l a y d e p e n d s o n t h e t i m e u s e d b y t h l o o p a w e l l a t h n u m b e r o f t i m e s r o u n d t h e l o o p . T h D E X / B N E l o o p u s e s c y c l e sf o e a c h l o o p , c o u l d w m a k e i u s m o r e ? T h a n s w e r i y e s . I t h B N b r a n c h a t t h e e n o t h d e l a y l o o p w e n t b a c k t t h L D 9 0 i n s t r u c t i o n i w o u l d i n c r e a s e t h n u m b e r o c y c l e s c o n s u m e d b t h l o o p t 2 3 T h i s w o u l d m e a n t h a t e a c h o f t h d i a m o n d s w o u l da p p e a r o t h s c r e e n f o m i l l i - s e c o n d s , s t i l l n o t e n o u g h t i m e f o t h 1 6 - 2 0 m i l l i - s e c o n d s c a n t s e e v e r y d i a m o n d . T r y m o d i f y i n gt h p r o g r a m s t h a t t h B N o t h d e l a y l o o p g o e s b a c k t t h e L D A9 0 0 . W h e n y o r u i a g a i n y o u s h o u l d s e r o u g h l y o n i t h r e e o t h d i a m o n d s .

W e n e e d t i n c r e a s e t h d e l a y b f a c t o r o f i f t e e n i t h o r i g i n a l p r o g r a m ( o f a c t o r o t h r e e i n t h e m o d i f i e d v e r s i o n ) .O n p o s s i b l e s o l u t i o n i t u s t h d o u b l e l o o p o p r o g r a m 3 . 5 .T h p r o b l e m i t h a t p r o g r a m 3 - u s e s t h e Y r e g i s t e r f o i n d e x i n gt h c h a r a c t e r a c r o s s t h s c r e e n a n d w e c a n n o t t h e r e f o r e u s t h r e g i s t e r f o t h o u t e r l o o p o t h d o u b l e l o o p , o r c a n w e ? W e l l w c a n , o f c o u r s e . I t i s a l w a y s p o s s i b l e t s a v e t h v a l u e i n t h er e g i s t e r i m e m o r y p r i o r t e n t e r i n g t h d e l a y l o o p a n d t h e nr e t r i e v e i a f t e r t h e l o o p . W h e n u s e d i t h i s w a y , t h e l a c k o m o r e t h a n t w r e g i s t e r s i n t h e 6 5 1 0 c a n b o v e r c o m e q u i t e r e a d i l ya t t h e e x p e n s e o l i t t l e m o r e c o d i n g .

Page 50: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 50/243

P R O G R A M

S T A R TL D Y I ML D A I MS T L D A I MS T L D A I MS T L D S T A YL D S T A YS T L D Y I ML D X I MD E B N D E B N L D L D S T A YI N B N R T E N

. 6

A D D R E S S ? 8 2 8

9 9 0

9 0 3 9 0 9 0 1 0 2 49 0 5 5 2 9 69 0 1 2 5

2 5 2 4 9 0 9 0 1 0 2 4

2 1

S e t u p d i a m o n d

S e t u p c o l o u r w h i t e

S e u s p a c e

L o a d d i a m o n dD i s p l a y i o n t h e s c r e e nL o a d c o l o u r w h i t eS t o r e i c o l o u r R A S a v e r e g i s t e r d u r i n g . . .S e o u t e r l o o pS e i n n e r l o o p

C o u n t d o w n 2 5 0 t i m e s

C o u n t d o w n 1 R e s t o r e r e g i s t e r a s c r e e n i n d e xL o a d b l a n kD i s p l a y b l a n kS e t u p f o r n e x t s c r e e n l o c a t i o nL o o p u n l e s s a l l d o n e

E n t e r t h i s p r o g r a m a n d r u n i t B r i l l i a n t i s n ' t i t I r e a l l yd e m o n s t r a t e s w h y c o m p u t e r g a m e s w h i c h a r w r i t t e n i m a c h i n e c o d ea r s m u c h b e t t e r t h a n t h o s e w r i t t e n i n B A S I C . B e a r i m i n d t h a tw e s l o w e d t h m a c h i n e c o d e d o w n b a d d i n g 4 0 0 0 d e l a y c y c l e s o t e a c h 4 u s e f u l w o r k c y c l e s a n y o c a i m a g i n e h o w m u c hm a n i p u l a t i o n c o u l d b c a r r i e d o u t u s i n g m a c h i n e c o d e .

M o d e s o A d d r e s s i n gW h e n m o v i n g c h a r a c t e r a c r o s s t h s c r e e n w h a v e u s e d t h i n s t r u c t i o n s S T A X a n d S T A Y a t h e s e a r a b l e t i n d e x a l o n g w i t ht h r e l e v a n t r e g i s t e r . I e a r l i e r w o r k w e u s e d t h S T i n s t r u c t i o no n i t s o w a n d t h i s i c l e a r l y r e l a t i v e o f S T A X , S T A Y , e t c . T h d i f f e r e n c e b e t w e e n t h t w t y p e s o f i n s t r u c t i o n l i e s i t h e i r m o d e so a d d r e s s i n g a n c l e a r l y t h X a n d Y a r e p a r t o f t h i s .

Page 51: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 51/243

T h a d d r e s s e s s e n t i a l l y p o i n t s t h 6 5 1 0 t l o c a t i o n i m e m o re i t h e r d i r e c t l y o i n d i r e c t l y , t h w a i d o e s t h i s b e i nd e t e r m i n e d b t h p a r t i c u l a r m o d e o a d d r e s s i n g u s e d . A d d r e s s i ni u n i f o r m t h r o u g h o u t t h 6 4 K o f p o s s i b l e m e m o r y e x c e p t f o r t h e 2 5l o c a t i o n s f r o m t o 2 5 5 - T a d d r e s s t h e s e l o c a t i o n s , o n l y o n b y ti n e e d e d w h e r e a s a l o t h e r l o c a t i o n s n e e d t w o . H e n c e , t h i s a r eo m e m o r y i g i v e n s p e c i a l n a m e Z E R O P A G E a n s p e c i a l m o do a d d r e s s i n g . I f a c t , t h e w h o l e o t h m e m o r y t h a t t h 6 5 1 0 c a a d d r e s s i d i v i d e d u i n t o p a g e s o 2 5 b y t e s a n d a i n s t r u c t i ot h a t c a u s e s o p e r a t i o n o v e r t h b o u n d a r y b e t w e e n p a g e s t a k e s a e x t r a c y c l e t b e x e c u t e d .

I n t h i s b o o k , a d d r e s s e s i Z e r o p a g e m e m o r y a r i n d i c a t e d b t h u s o ' Z o t h i n s t r u c t i o n , e . g . S T A Z . H o w e v e r , t h i s i o n l

c o n v e n t i o n c h o s e n h e r e a n i b n m e a n s u n i v e r s a l o n e . A l t h a t r e a l l y m a t t e r s i t h a t t h z e r o p a g e S T A c o m m a n d h a t h o p - c o d e o 8 5 o 1 3 3 < i o - T h i s i a c h i e v e d w i t h t h a s s e m b l ep r o v i d e d , b y t h p o s i t i o n o S T A Z i n t h e D a t a s t a t e m e n t o f 1 3 32 )

I m p l i e d A d d r e s s i n gT h i s m o d e , s o m e t i m e s a l s o k n o w n a i n h e r e n t a d d r e s s i n g , i p r o b a b lt h e a s i e s t t o u s e , a s t h 6 5 1 0 d o e s a l t h w o r k f o y o u !

S e v e r a l i n s t r u c t i o n s h a v e a l r e a d y b e e n u s e d a s T Y A , T X A , R T S , i w h i c h t h 6 5 1 0 i t s e l f c a l c u l a t e s t h a d d r e s s . B a s i c a l l y t h e y f o rt w s e p a r a t e g r o u p s , o n i w h i c h t h w h o l e i n s t r u c t i o n i e x e c u t e w i t h i n t h 6 5 1 0 i t s e l f , i . e . T Y A , t r a n s f e r t A a n d t h e o t h eg r o u p w h e r e a e x t e r n a l r e f e r e n c e i n e c e s s a r y , e . g . R T S r e t u rf r o m s u b r o u t i n e .

T h m e m b e r s o t h f i r s t g r o u p a l r e a d y c o n s i d e r e d a r e : D E X , D E YI N X , I N Y , T A X , T A Y , T X A , T Y A , w h i l e t h o s e y e t t b d i s c u s s e d a r e C L C , C L D , C L I , C L V , N O P , S E C , S E D , S E I . T h o s e i t h s e c o n d g r o ua r R T S ( a l r e a d y u s e d ) a n d B R K , P H A , P H P , P L A , P L a n R T I .

A b s o l u t e A d d r e s s i n gI n s t r u c t i o n s u s i n g t h i s m o d e a r a m o n g t h e a s i e s t t u n d e r s t a n d a t h e i r o p e r a n d i t w b y t e n u m b e r t h a t ' d e f i n e s t h a d d r e s a b s o l u t e l y . T h u s i p r o g r a m 3 - 6 , p a g e 3 - 6 , t h i n s t r u c t i o n S T 9 0 t e l l s t h r e g i s t e r e x a c t l y w h e r e t s t o r e i t c o n t e n t s . L a t e r i t h s a m e p r o g r a m L D f o l l o w s t h s a m e p a t t e r n .

I n s t r u c t i o n s t h a t u t i l i s e t h i s f o r m o a d d r e s s i n g a r e t h o sa l r e a d y m e t ; A D C , C M P , C P X , C P Y , J M P , J S R , L D A , L D X , L D Y , S T A , S T X

Page 52: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 52/243

Z e r o - P a g e A d d r e s s i n gT h i s f o r m o a d d r e s s i n g i r e a l l y s u b - s e t o a b s o l u t e a d d r e s s i n gb u i r e s t r i c t e d i n s i z e o f i t o p e r a n d t 2 5 T h m a j o ra d v a n t a g e o f t h i s m o d e i t h a t i e x e c u t e s i o n l y t h r e e c y c l e sc o m p a r e d w i t h t h f o u r r e q u i r e d f o t h a b s o l u t e m o d e s . I n t h i sb o o k t h o s e i n s t r u c t i o n s u s i n g z e r o - p a g e m o d e s a r s u f f i x e d w i t h' Z 1 . B e c a u s e o t h f a s t e r e x e c u t i o n t i m e s w h e n u s i n g p a g e z e r o

a d d r e s s e s , p a g e z e r o o f t h c o m p u t e r i u s e d b y t h B A S I C i n t e r p r e t e ra n i s t h u s , n o r e a d i l y a v a i l a b l e f o m a c h i n e c o d e p r o g r a m s .f e o t h e s e z e r o p a g e l o c a t i o n s a r e n o t u s e d b B A S I C a n m a b u s e d b y t h m a c h i n e c o d e p r o g r a m m e r , n o t a b l y 2 5 t 2 5 4 . W h e n y o g e t k n o w y o u r w a y a r o u n d t h B A S I C i n t e r p r e t e r t h e n y o u w i l ld i s c o v e r t h a t y o c a u s l o m o r e . F i n a l l y , i i p o s s i b l e t u s t h z e r o p a g e b y r e l o c a t i n g t h i s p a g e t a n o t h e r l o c a t i o n i R A b u t t h i s i f e l t t b p r o c e d u r e b e y o n d t h s c o p e o t h i sb o o k .

H o w e v e r , e v e n w h e n y o u c a n n o t w r i t e t p a r t i c u l a r z e r o p a g el o c a t i o n b e c a u s e i w o u l d i n t e r f e r e w i t h t h B A S I C i n t e r p r e t e r , y o uw i l l o f t e n f i n d i u s e f u l t r e a d t h i n f o r m a t i o n s t o r e d t h e r e b B A S I C . T h r e e o t h l o c a t i o n s i p a g e z e r o t h a t a r e u s e f u l a r 1 6 t o 1 6 2 , w h i c h c o n t a i n t h j i f f i e c l o c k t h a t i n c r e m e n t s e v e r y 1 / 6 0 t ho s e c o n d . P r o g r a m 3 * i v e r y s i m p l e p r o g r a m t h a t l o a d s o n b y t e o t h i s i n t o t h a c c u m u l a t o r a n t h e n p r i n t s i o n t o t h s c r e e n .

P R O G R A M 3 -

S T A R T A D D R E S S ? 8 2 L D A Z 1 6 L o a d a c c u m u l a t o r i Z e r o - p a g e

m o d e w i t h c o n t e n t s o 1 6 S T 1 0 2 4 S t o r e c o n t e n t s o i 1 0 2 4L D A I M L o a d a c c u m u l a t o r w i t hS T 5 5 2 9 6 S t o r e i c o l o u r R A M -R T R e t u r n f r o m m a c h i n e c o d e

s u b r o u t i n e

B e c a u s e o f t h e i r l i m i t e d r a n g e o u s e , n o z e r o - p a g e i n s t r u c t i o n sh a v e b e e n u s e d t d a t e i t h i s b o o k . T h o s e t h a t a r a v a i l a b l e f o u s a r e : A D C Z , A N D Z , A S L Z , B I T Z , C M P Z , C P X Z , C P Y Z , D E C Z , E O R Z ,I N C Z , L D A Z , L D X Z , L D Y Z , L S R Z , O R A Z , R O L Z , R O R Z , S B C Z , S T A Z , S T X Z ,S T Y Z .

I m m e d i a t e A d d r e s s i n gT h i s m o d e o a d d r e s s i n g a l l o w s n u m b e r t b l o a d e d i m m e d i a t e l y ,

Page 53: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 53/243

N o r m a l - i n s t r u c t i o n # o p e r a n di n s t e a d o f L D A I M 9

L D # 9 M a n y e x a m p l e s o i m m e d i a t e m o d e a d d r e s s e s h a v e b e e n s e e n a l r e a d y ,f o e x a m p l e i p r o g r a m 3 » 6 , p a g e 3 - 6 . I n t h i s p r o g r a m , t h A c c u m u l a t o r w a s l o a d e d d i r e c t l y u s i n g L D A I M 3 2 a n i o t h e rp r o g r a m s b o t h t h e X r e g i s t e r a n t h r e g i s t e r h a v e b e e n l o a d e d i t h s a m e w a y . H o w e v e r , m a n y o t h e r i n s t r u c t i o n s c a b u s e d i n t h ei m m e d i a t e m o d e , r e s u l t i n g i n e a t e r p r o g r a m s . P r o g r a m 3 « d e m o n s t r a t e s t h u s e o f : -

C P Y I M £ o m P a r e w i t h v a l u e

P R O G R A M 3 . S T A R T A D D R E S S ? 8 2 L D Y I MT Y I N S T A Y 1 0 2 3L D A I MS T A Y 5 5 2 9 5C P Y I M 1 0 B N E 2 4 2R T

s p e c i f i e d i i m m e d i a t e M o d e .

L o a d w i t h ' 0 T r a n s f e r t I n c r e m e n tS t o r e c o n t e n t s o i 1 0 2 3 + YL o a d w i t hS t o r e i 5 5 2 9 5 + YC o m p a r e w i t h 1 0 B r a n c h i f l a g n o s e R e t u r n f r o m m a c h i n e c o d es u b r o u t i n e

O n r u n n i n g , t h p r o g r a m p r i n t s t h f i r s t 1 0 c h a r a c t e r s o t h c h a r a c t e r s e t i n t h e f i r s t 1 0 s c r e e n l o c a t i o n s .

I n d e x e d A d d r e s s i n gI t h i s m o d e , a n a d d r e s s i c a l c u l a t e d u s i n g t h c o n t e n t s o r e g i s t e r a d d e d t o s p e c i f i e d a d d r e s s . I h a b e e n u s e d f r e q u e n t l yt p r i n t c h a r a c t e r s a c r o s s t h s c r e e n i n t h e f o r m S T A X , S T A Y . I p r o g r a m 3 - 8 , S T A Y w a s u s e d i t h i s w a y w i t h t h i n s t r u c t i o n S T A Y1 0 2 4 . I s o m e a s s e m b l e r s , t h r e g i s t e r w h i c h i n d e x e s t h c o m m a n di a p p e n d e d a f t e r t h o p e r a n d ; t h u s t h c o m m a n d

S T A X 1 0 2 4

w o u l d b w r i t t e n :

S T A X 1 0 2 4 , X

Page 54: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 54/243

T h o s e i n d e x e d w i t h a r e : A D C X , A N D X , A S L X , C M P X , D E C X , E O R X ,I N C X , L D A X , L D Y X , L S R X , O R A X , R O L X , R O R X , S B C X , S T A X .T h o s e i n d e x e d w i t h a r e : A D C Y , A N D Y , C M P Y , E O R Y , L D A Y , L D X Y ,O R A Y , S B C Y , S T A Y .

N . B . E x c e p t i o n s t t h g e n e r a l i t y a r t h a t S T C A N N O T b i n d e x e d w i t h X

A S L , D E C , L S R , R O L , R O C A N N O T b i n d e x e d w i t hY

T h s o u r c e c o d e s t h a t c a n b u s e d i z e r o - p a g e m o d e h a v e t h s u f f i x' Z a d d e d t t h i s t i d e n t i f y t h r e g i s t e r u s e d . T h u s t h i n s t r u c t i o n A D b e c o m e s A D C Z X . T h e t o t a l l i s t o z e r o - p a g e ,X - i n d e x e d c o m m a n d s i s A D C Z X , A N D Z X , A S L Z X , C M P Z X , D E C Z X , E O R Z X ,I N C Z X , L D A Z X , L D Y Z X , L S R Z X , O R A Z X , R O L Z X , R O R Z X , S B C Z X , S T A Z X ,S T Y Z X .

I i s o f c o u r s e , N O p o s s i b l e t u s t h e t w o r e g i s t e r i n d e xc o m m a n d s S T X a n d L D X w i t h r e f e r e n c e t i t s e l f !

R e l a t i v e A d d r e s s i n gM a n y p r o g r a m s u s e d s f a h a v e u t i l i s e d r e l a t i v e a d d r e s s i n g i w h i c h b r a n c h h a b e e n d e f i n e d r e l a t i v e t t h c u r r e n t p o s i t i o n o t h p r o g r a m , i . e . t h e o p e r a n d e x p r e s s e s t h d e s i r e d d i s p l a c e m e n t .I p r o g r a m 3 . t h i n s t r u c t i o n B N 2 4 2 w a s u s e d t t e s t t h s e t t i n go f t h e f l a g a n d t b r a n c h w h e n i w a n o t s e t . A l t h b r a n c hi n s t r u c t i o n s u s e d s f a u t i l i s e r e l a t i v e a d d r e s s i n g a n i i t h i sg r o u p a w h o l e t h a t u s e s t h i s m o d e . T h e g r o u p c o n s i s t s o f : -B C C , B C S , B E Q , B M I , B N E , B P L , B V C , B V S .

I n d i r e c t A d d r e s s i n gT h i s i b f a t h m o s t c o m p l i c a t e d a n t h m o s t v e r s a t i l e o a l t h a d d r e s s i n g m o d e s . I g e t s t h " i n d i r e c t " i t h n a m e f r o m t h f a c t t h a t t h o p e r a n d i p o i n t e r a n d n o t a a d d r e s s . I t i s t h i sp o i n t e r t h a t d i r e c t s t h 6 5 1 0 t o w a r d s t h m e m o r y l o c a t i o n t h a tc o n t a i n s t h a d d r e s s .

H o w e v e r , o n c e a g a i n , t h e X a n d Y i n d e x i n g m e c h a n i s m s d i f f e rc o n s i d e r a b l y i o p e r a t i o n a n g i v e r i s e t d i f f e r e n t s u b - m o d e s o a d d r e s s i n g . A l t h i n s t r u c t i o n s t h a t u t i l i s e t h i s k i n d o a d d r e s s i n g a r r e c o g n i s a b l e i n t h e a s s e m b l e r a t h e y c o n t a i n e i t h e ra ' I X 1 o r a n ' I Y 1 a n h a v e o n b y t e o p e r a n d . B e c a u s e o f t h i s

Page 55: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 55/243

U s i n g t h X - R e g i s t e rW i t h i n d i r e c t a d d r e s s i n g u s i n g t h r e g i s t e r , t h o p e r a n d i i n d e x e d w i t h ( i . e . a d d e d t o t h c o n t e n t s o t h r e g i s t e r t p r o d u c e p o i n t e r . T h i s l o c a t i o n a n t h e o n e a f t e r i a r e t h e e x a m i n e d a n t h e i r c o n t e n t s p r o v i d e a d d r e s s e s f o r t h e r e q u i r e d d a t i t h o r d e r l e a s t s i g n i f i c a n t b y t e , m o s t s i g n i f i c a n t b y t e .

T h t e c h n i q u e i u s e f u l f o e x a m i n i n g o n p a r t i c u l a r e l e m e n t i t a b l e , t h e a c t u a l p o s i t i o n i n t h e t a b l e b e i n g s e t b y t h v a l u e i t h e X r e g i s t e r . W i t h t h l a c k o f a v a i l a b i l i t y o f s p a r e z e r o - p a g s p a c e o n t h e c o m p u t e r t h a d d r e s s i n g m o d e i o l i m i t e d u s e .H o w e v e r , j u s t t d e m o n s t r a t e o n i n s t r u c t i o n i u s e , p r o g r a m 3 . u s e s : -

L D A I X j L o a D A I n d i r e c t l y i n d e x e d w i t h X

T h i s i u s e d t r e t r i e v e f o u r b y t e s s t o r e d i p a g e z e r o f r o m 8 8 8 T h i s i a a r e a o R A u s e d f o r n u m e r i c s t o r a g e i p r o g r a m3 - 9 .P R O G R A M 3 . 9

S T A R T A D D R E S S ? 8 2 8L D X I ML D A I X 8 S T A X 1 0 2 4L D A I MS T A X 5 5 2 9 6I N C P X I MB N 2 4 R T

L o a d i m m e d i a t e w i t h l 0 L o a d i n d i r e c t 8 4 + XS t o r e i 1 0 2 4 + XL o a d w i t hS t o r e i c o l o u r R A I n c r e m e n tC o m p a r e i m m e d i a t e w i t hB r a n c h i n o e q u a l

" 4

W h e n r u n , t h p r o g r a m w i l l p r i n t f o u r c h a r a c t e r s i n t h e f i r s t f o u rs c r e e n l o c a t i o n s . W h a t t h e s e a r w i l l d i f f e r e v e r y t i m e d e p e n d i n gu p o n w h a t B A S I C h a b e e n d o i n g e a r l i e r . W h e n u s e d i p r o g r a mt h e s e w o u l d f o r m t w a d d r e s s e s i n t h e o r d e r :

c h a r a c t e r A d d r e s s l e a s t s i g n i f i c a n t b y t e L S c h a r a c t e r A d d r e s s m o s t s i g n i f i c a n t b y t e M S c h a r a c t e r A d d r e s s l e a s t s i g n i f i c a n t b y t e L S Bc h a r a c t e r A d d r e s s m o s t s i g n i f i c a n t b y t e M S

T h i s t y p e o a d d r e s s i n g i k n o w n a I n d e x e d I n d i r e c t A d d r e s s i n g , o p e r h a p s m o r e c l e a r l y a P R E - I N D E X E D I N D I R E C T A D D R E S S I N G . A t h

Page 56: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 56/243

U s i n g t h Y - R e g i s t e rI n d i r e c t a d d r e s s i n g u s i n g t h r e g i s t e r o p e r a t e s s o m e w h a td i f f e r e n t l y , a t h o p e r a n d i n s t r u c t i o n p o i n t s d i r e c t l y t z e r o - p a g e m e m o r y l o c a t i o n . T h i s c o n t a i n s t h l e a s t s i g n i f i c a n tb y t e o f t h a d d r e s s , t h n e x t m e m o r y l o c a t i o n c o n t a i n i n g t h m o s ts i g n i f i c a n t b y t e . F i n a l l y , t h i n d e x r e g i s t e r c o n t e n t s a r a d d e dt t h i s a d d r e s s t f o r m t h f i n a l i n d e x e d a d d r e s s . N o ts u r p r i s i n g l y , t h i s f o r m o a d d r e s s i n g i r e f e r r e d t o a sP O S T - I N D E X E D I N D I R E C T A D D R E S S I N G a t h i n d e x i n g i c a r r i e d o u a f t e r t h a d d r e s s i r e t r i e v e d . T h B A S I C i n t e r p r e t e r a n C o m m o d o r e6 o p e r a t i n g s y s t e m m a k e e x t e n s i v e u s o f t h i s i n s t r u c t i o n . W h e ny o u h a v e b e c o m e a e x p e r i e n c e d A s s e m b l e r u s e r y o w i l l n d o u b tw i s h t l o o k a t h w a t h B A S I C i n t e r p r e t e r w o r k s a n y o w i l ls e h o w u s e f u l t h e s e i n s t r u c t i o n s a r e .

C o m m a n d s t h a t u s e t h i s t y p e o f a d d r e s s i n g a r e : - A D C I Y , C M P I Y ,E O R I Y , L D A I Y , O R A I Y , S B C I Y , S T A I Y .

I n d i r e c t A b s o l u t e A d d r e s s i n gT h i s m o d e o a d d r e s s i n g i u s e d b y o n e i n s t r u c t i o n o n l y :

J M P I A _ J u M P i n d i r e c t l y A d d r e s s e d .

I i a a b s o l u t e i n s t r u c t i o n i t h a t t h o p e r a n d i s t w b y t ea d d r e s s a n d c a n t h u s a d d r e s s a n y l o c a t i o n i m e m o r y . H o w e v e r , i i i n d i r e c t i n t h a t , a t h a t l o c a t i o n a n t h s u b s e q u e n t o n i f i n d s t h a d d r e s s ( L S B f i r s t t h e n M S B ) f o r t h j u m p i n s t r u c t i o n .I n t h e D W a t s o n a s s e m b l e r i i r e c o g n i s e d b t h m n e m o n i c J M P I Ab u s o m e o t h e r a s s e m b l e r s m a y r e p r e s e n t t h i s w i t h J M P f o l l o w e d b t h o p e r a n d i b r a c k e t s , e . g . J M ( 8 4 4 ) , w h i c h i J u m p t 8 4 t f i n dL S B a n d 8 4 5 t f i n d M S o j u m p a d d r e s s .

P u t t i n g t h i s i n t o p r o g r a m u s i n g J M P I A 8 4 4 y i e l d s :

P R O G R A M 3 . 1 0 ( P a r t )D E 8 4 8 4 8 4 8 4 8 4 8 4 8 4 8 4

H E 0 3 4 80 3 4 BO 3 4 CO 3 4 DO 3 4 EO 3 4 F0 3 5 00 3 5 1

J M P I A 8 4 4N O 5 0 N O N O N O S T 1 0 2 4

( 8 i ( 3 i O )

Page 57: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 57/243

p r o g r a m j u m p s t 8 4 t e x e c u t e t h p r o g r a m s t o r e d t h e r e . A p r o g r a m 3 * 1 0 ( P a r t ) s t a n d s i i n o p o s s i b l e t p r o g r a m i n t h e 5 a n 0 3 i n t o 8 4 a n d 8 4 5 a t h e s e h e x n u m b e r s d n o r e p r e s e n t v a l i do p - c o d e s . T h e a s s e m b l e r w i l l s i m p l y r e j e c t t h e m . T h e y c a b m o s te a s i l y p u i b l o a d i n g t h e m i n t o r e g i s t e r a n t h e n t r a n s f e r r i n gi n t o m e m o r y a i s h o w n i p r o g r a m 3 - 1 0 ( W h o l e ) .P R O G R A M 3 . 1 0 ( W h o l e )

8 2 L D A I X 9 8 3 0 L D X I M $ 0 0 5 8 3 2 S T X 8 4 8 3 L D X I M $ 0 0 8 3 S T 8 4 8 4 J M P I A 8 4 8 4 N O 8 4 N O 8 4 N O 8 4 N O 8 4 N O 8 4 N O 8 4 S T 1 0 2 48 5 L D A I M8 5 S T A 5 5 2 9 68 5 7 R T S

W h e n r u n , t h i s f i r s t l o a d s t h a d d r e s s $ 0 3 5 1 i n t o l o c a t i o n s 8 4 4 a n 8 4 5 . T h e J M P I A t h e n r e t r i e v e s t h e s e a n d j u m p s t $ 0 3 5 1 w h e r e i e x e c u t e s t h r o u t i n e s t o r e d t h e r e . T h i s , h o w e v e r , i r o u t i n e a n n o s u b r o u t i n e a i w a s e n t e r e d w i t h J M P I A a n n o J S R , s t h R T a 8 5 r e t u r n s t h p r o g r a m t t h B A S I C p r o g r a m . O n c e r u n ,t h i s p r o g r a m w i l l h a v e m o d i f i e d i t s e l f a n d p u t t h e 0 3 a n d 5 i n t o8 4 4 a n d 8 4 5 .

A d d r e s s i n g G e n e r a l l yT h w h o l e s u b j e c t o a d d r e s s i n g i c l e a r l y c o m p l e x o n a n o n t b a p p r o a c h e d o n l y w i t h c a r e . b a s i c r u l e m u s t b t c h e c kc a r e f u l l y i A p p e n d i x 2 b e f o r e u s i n g a n a d d r e s s i n g o w h i c h y o ua r e n o t a b s o l u t e l y c e r t a i n . T s o m e e x t e n t , t h a s s e m b l e r w i l la s s i s t i w e e d i n g o u i n s t r u c t i o n s t h a t d o n ' t e x i s t b u i c a n ' tw r i t e y o u r p r o g r a m s !

Page 58: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 58/243

Page 59: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 59/243

C H A P T E RM a t h e m a t i c a l , L o g i c a l O p e r a t o r s

O n p a g e 1 - 1 , a e a r l y a p r o g r a m 1 . 1 , a p r o g r a m w a w r i t t e n t a d t o g e t h e r t w s i m p l e n u m b e r s a n d i s p l a y t h e s e . T h " s i m p l i c i t y " o t h n u m b e r s a r i s e s f r o m t h f a c t t h a t t h e y w e r e o n l y s i n g l e d i g i t sa n t h a t t h e i r a n s w e r g a v e n c a r r i e s . W h e n l a r g e r n u m b e r s a r a d d e d a n c a r r i e s a r i s e , t h e 6 5 1 0 h a n d l e s t h e s e b y u s e o f i t s c a r r yo f l a g .

T h i s i s e a u t o m a t i c a l l y w h e n a a d d i t i o n o p e r a t i o n i c a r r i e d o u t h a t b r i n g s a b o u t c a r r y b e t w e e n t h e t w o b y t e s o t w b y t en u m b e r c l e a r , e h I f n o t , p l e a s e r e a d o n

U s i n g o n e b y t e i t ' s o n l y p o s s i b l e t c o u n t u p t 2 5 5 , t h u s i w w i s h t c o u n t b e y o n d t h i s w h a v e t u s t w b y t e s . T h e s e 1 b i t st h e n a l l o w u t c o u n t u p t 6 5 5 3 6 . I f y o u h a v e n o t y e t r e a d t h s e c t i o n o b i n a r y a n h e x a d e c i m a l ( p a g e A 1 - 1 ) t h e n i w o u l d b w e l lt d o i t n o w ! N a t u r a l l y i w o u l d b p o s s i b l e t h o l d a l a r g en u m b e r a t h e r e a r b y t e s f r e e ; h o w e v e r , w w i l l n o c o n s i d e r t h u s o t w b y t e s w h i c h i r a t h e r f a n c i l y d e s c r i b e d a D O U B L EP R E C I S I O N o p e r a t i o n .

I t w o m o r e b y t e s a r t b s u c c e s s f u l l y u t i l i s e d t r e p r e s e n ts i n g l e n u m b e r t h e n t h e y m u s t b l i n k e d f r o m t h f i r s t t t h s e c o n db y t e b y s o m e m e c h a n i s m . T h i s i t h f u n c t i o n t h a t t h c a r r y f l a gp e r f o r m s a n d i t s o p e r a t i o n i t e s t e d b t h i n s t r u c t i o n : -

B C I 3 r a n c h o £ a r r y C l e a r .T h i s t e s t s f o r t h e c a r r y f l a g b e i n g c l e a r , i . e . s e t 0 a n e x e c u t e s b r a n c h i t h i s i s o O n p r e c a u t i o n i a l w a y s b e s to b s e r v e d w h e n t e s t i n g f o r t h i s f l a g , h o w e v e r . T h a t i s t o e n s u r et h a t t h e f l a g i i t h e x p e c t e d s t a t e p r i o r t t h o p e r a t i o n t h a tm a y m o d i f y i t T h i n s t r u c t i o n w h i c h p e r f o r m s t h i s t a s k i s : -

C L C L e a r t h C a r r y f l a g .

T h i s s e t s t h c a r r y f l a g t ' c l e a r 1 o ' 0 a n i u s e d p r i o r t t h p r o c e s s t h a t m a r e s e t t h i s i n p r o g r a m 4 . 1 :

P R O G R A M 4 . N O T E :A y o a r w e l l a w a r e b

C L n o w , t h e a s s e m b l e r m u s tL D A I M b g i v e n s t a r t a d d r e s s ,

Page 60: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 60/243

W h e n r u n , t h i s p r o g r a m p r o g r e s s i v e l y i n c r e a s e s t h a c c u m u l a t o rc o n t e n t s b u n t i l a 2 5 5 t h e A D C I M i n s t r u c t i o n f l i p s t h e i g h tT o v e r t e i g h t ' O ' s a n s e t s t h c a r r y f l a g t 1 T h u s w h e nt h a c c u m u l a t o r i d i s p l a y e d w i t h t h i n s t r u c t i o n S T 1 0 2 4 , i i s e e n t c o n t a i n ' 0 ( i . e . a w h i t e o n t h e s c r e e n ) .

T h 6 5 1 0 h a s e c o n d t e s t i n s t r u c t i o n f o t h c a r r y f l a g , t h i sb e i n g : -

I B C S B r a n c h o C a r r y S e t .T h i s t e s t s f o t h c a r r y f l a g b e i n g ' s e t 1 , i . e . c o n t a i n i n g ' 1 ' ,a n i f t h e t e s t i p o s i t i v e , e x e c u t e s b r a n c h . P r o g r a m 4 . i l l u s t r a t e s t h i s i n s t r u c t i o n i n u s e :

P R O G R A M 4 . C L L D A I MA D C I MB C J M 8 3 S T A 1 0 2 4L D A I MS T A 5 5 2 9 6R T

O n c e a g a i n , t h i s p r o g r a m p r o g r e s s i v e l y f i l l s a l l e i g h t b i t s o t h a c c u m u l a t o r w i t h ' 1 ' s a n d ' f i n a l l y , o f l i p p i n g t h e s e o v e r t ' O ' ss e t s t h e c a r r y f l a g a n t e r m i n a t e s t h p r o g r a m . A t h e e n d t h a c c u m u l a t o r c o n t a i n s a l ' O ' s a n t h e r e f o r e w h i t e * @ i d i s p l a y e d o n t h e s c r e e n .

L e t ' s h a v e g o a t a d d i n g t o g e t h e r t w n u m b e r s l a r g e r t h a n 2 5 6 ,w e ' l l t a k e 1 2 5 7 . F i r s t w e h a v e t c a l c u l a t e t h M S a n L S a n t d o t h i s , o f c o u r s e , w e h a v e t c o n v e r t t h n u m b e r t o h e x a d e c i m a l f o r m a t . T h u s : -I N ( 1 2 5 7 / 4 0 9 6 ) t h e r e f o r e R i g h t - m o s t c h a r a c t e rI N T ( 1 2 5 7 / 2 5 6 ) t h e r e f o r e 2 n c h a r a c t e rI N ( 1 2 5 7 - 4 x 2 5 6 ) / 1 6 ) = t h e r e f o r e 3 r c h a r a c t e r( 1 2 5 7 - 4 x 2 5 6 - 8 x 1 6 ) = t h e r e f o r e 4 t c h a r a c t e r = T h u s :

H e x 1 2 5 7 1 0 0 4 8 5 1 6

Page 61: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 61/243

T a d t o g e t h e r t w 1 2 5 7 1 0 ' s w m u s t f i r s t a d d t h e L S B ' s , c h e c k i t h e r e i s c a r r y a n t h e n a d t h M S B ' s t a k i n g i n t o a c c o u n t t h n e c e s s i t y ( o o t h e r w i s e ) o c a r r y , i . e . a d L S B ' s .

8 + 8 p l u s c a r r y O 1 6 , 1 0 = c a r r y O

T h e n a d t h M S B ' s

0 + 0

0

N e x t a d d i n t h e c a r r y0 c a r r y 0

I n t h e e x p l a n a t i o n w h a v e g l i b l y s a i d " + c a r r y " a n i t i s t h i so p e r a t i o n t h a t t h C - f l a g d o e s f o t h p r o g r a m m e r . T h f l a g i s e t ' 1 w h e n a o p e r a t i o n i c a r r i e d o u t t h a t l e a d s t " c a r r y " .T h n e x t o p e r a t i o n t h a t i c a r r i e d o u t h e n t a k e s a c c o u n t o t h i sc a r r y a n a d d s o t t h n e x t " a d d " c a r r i e d o u t .

W i t h0

f l a g0

" 0 0

W i t h0

f l a g0

" 1 0

T h u s t h a n s w e r t t h e x a m p l e i s i H e x 0 9 0 A , o r i n d e c i m a l9 x 2 5 6 + 1 0 2 3 1 4 1 O -N o w l e t ' s t r d o i n g t h a t t h e l o n g w a u s i n g t h c o m p u t e r !

W e c a n r e l y o t h 6 5 1 0 h a n d l i n g t h c a r r y b u t w e c a n ' t r e l y o i k n o w i n g w h e n t e x e c u t e c a r r y ! A l d o u b l e p r e c i s i o n w o r k i c a r r i e d o u l e a s t s i g n i f i c a n t b y t e ( L S B ) f i r s t a s i t i d u r i n g t h i sa d d i t i o n o p e r a t i o n t h a t t h c a r r y a r i s e s a n i i t h e n s t o r e d u r e a d y f o t h m o s t s i g n i f i c a n t b y t e ( M S B ) a d d i t i o n w h e n t h i s i d o n e . Y o u m a y r e m e m b e r t h a t w h e n t h 6 5 1 0 i u s i n g i n d i r e c ta d d r e s s i n g c o m m a n d s i s t o r e s t h L S o t h a d d r e s s f i r s t a n t h M S B s e c o n d t h i s i t h o r d e r t h a t t h e y a r u s e d w h e n t h i n d e x i a d d e d o t t h e ' p o i n t e r ' a d d r e s s . W c o u l d , i w s w i s h e d ,s t i c k t t h i s o r g a n i s a t i o n o u r s e l v e s w h e n w e a r e s t o r i n g ' n u m b e r s '( a d i s t i n c t f r o m a d d r e s s e s ) . B u t s i n c e w e a r d o i n g t h o r g a n i s i n g o f t h e w a i w h i c h t h t w b y t e s a r a d d e d ( n o t t h e6 5 1 0 ) , t h e r e i n r e a l a d v a n t a g e i s t o r i n g ' n u m b e r s ' e i t h e r w a y

Page 62: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 62/243

F i r s t , w m u s t w o r k o u t h v a l u e o M S a n L S i d e c i m a l , a b o t h m e t h o d s o p u t t i n g d a t a i n t o m e m o r y t h a t w e h a v e d i s c u s s e d t d a t e r e q u i r e t h i s . F o t h L S B , i t d e c i m a l v a l u e i

1

a n f o M S i t i s

1 4 1 N o t w r i t e t h p r o g r a m b u b e f o r e w d t h a t , l e t ' s j u s ti n t r o d u c e n e i n s t r u c t i o n : -

N O N O P e r a t i o n

W h e n t h 6 5 1 0 m e e t s i t i d o e s n o t h i n g f o r t w o c y c l e s o i t o p e r a t i o n .W e ' l l s e w h w e p u t i i s h o r t l y ; f o r n o w , t y p e i n t h e p r o g r a m .

P R O G R A M 4 .

C L C L L D A I M 1 3 A D C I M 1 3 S T 1 0 2 6L D X I MS T X 5 5 2 9 8N O L D A I MA D C I MS T 1 0 2 4S T 5 5 2 9 6R T

E N

C l e a r c a r r y f l a gE x p l a i n e d l a t e rL o a d w i t h L S BA d w i t h c a r r y 2 n d L S BS t o r e s u m o L S B ' s i n 1 0 2 6L o a d f o w h i t e d i s p l a yS t o r e i i c o l o u r R A D n o t h i n gL o a d w i t h M S B . .A d w i t h c a r r y 2 n M S S t o r e s u o M S B ' s i 1 0 2 4S t o r e i c o l o u r R A R e t u r n f r o m m a c h i n es u b r o u t i n e

c o d e

H a v i n g d o n e t h i s y o m a r u t h p r o g r a m ; i s h o u l d p u o t h s c r e e n .

T h o u t p u t m e a n s : o r 9 ^ q a & o 1 0 - j q

i . e . 9 O A 1 6 2 3 1 4 1 0

Page 63: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 63/243

S t e p p i n g t h r o u g h t h a t p r o g r a m , t h v a r i o u s s t a g e s a r e :

( s i g n i f i e s R a n d o m v a l u e . )S t a g e

S T A R T 8 2 8C L C L L D A I M 1 3 A D C I M 1 3 S T 1 0 2 6L D X I MS T X 5 5 2 9 8N O L D A I MA D C I MS T 1 0 2 4S T 5 5 2 9 6R T

A c c u m u l a t o r

1 3 1 3 1 3 1 3 1 3 1 3

R e 1 0 2 6

1 1 1 1 1 1 1 1 1

1 0 2 4 F l a g

F i g . 4 .

A t h a b o v e c h a r t s h o w s , a t h A D C I M 1 3 3 , a c a r r y i g e n e r a t e da n t h f l a g i s e t ' 1 a n d t h i s c a r r y a f f e c t s t h s u b s e q u e n tA D C . T h o t h e r t h i n g t n o t i c e i t h a t a t t h e A D C I M f u r t h e rc a r r y i n o g e n e r a t e d a n t h f l a g i t h e r e f o r e c l e a r e d t ' 0 f .I y o u w a n t t c h e c k t h i s y o c o u l d r e p l a c e t h N O P c o m m a n d w i t hC L w h i c h w o u l d c l e a r t h f l a g a f t e r i h a b e e n s e a n n o t i c et h a t t h a n s w e r t h a t y o g e w o u l d b ' i n c o r r e c t 1 .

T h i s c a n b d o n e b y P O K E i n g i n t o 8 4 t h c o d e f o C L C , i . e . 2 4

P R O G R A M 4 - P O K E 8 4 2 , 2 4

N o w , w h e n r u n , p r o g r a m 4 . m o d i f i e d b 4 . 4 w i l l g i v e d i s p l a y o f : - r

I n t h i s r u n , t h v a l u e o h a s b e e n c o m p u t e d a n w h e n i t v a l u e o 2 6 o v e r f l o w e d , 2 5 w a s c a r r i e d , t h e c a r r y b i t s e t a n d 1 w a s t o r e d i t h a c c u m u l a t o r . H o w e v e r , t h i s 2 5 6 w a l o s t w h e n t h c a r r y b i w a l a t e r c l e a r e d b C L ( P r o g r a m 4 - 4 ) .

U s i n g H e x a d e c i m a l I n p u t s

Page 64: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 64/243

i h e x a d e c i m a l , p r o v i d i n g i i p r e c e d e d b ' $ s i g n . U t i l i s i n gt h i s i p r o g r a m 4 . 3 , t h i s b e c o m e s :

P R O G R A M 4 - 3 a

C L O L L D A I M $ 8 A D C I M $ 8 S T 1 0 2 6L D X I MS T 5 5 2 9 8N O L D A I M $ 0 A D C I M $ 0 S T 1 0 2 4S T 5 5 2 9 6R T E N

W h e n p r o g r a m 4 . 3 a i r u n , i g i v e s e x a c t l y t h s a m e r e s u l t a p r o g r a m 4 - 3 , i - e . I J S e l e c t t h l i s t o p t i o n a n y o u w i l l s e t h a t t h e a s s e m b l e r h a c o n v e r t e d t h $ 8 5 i n t o d e c i m a l v a l u e a n P O K E d t h i s i n t o m e m o r y .

E x e r c i s e 4 *

U s i n g h e i n p u t s , a d d t o g e t h e r 1 8 0 7 ^ a n d 2 A F A 1 5 » V e r i f y y o u rp r o g r a m a f t e r w a r d s i b a s e 1 0 A n s w e r p a g e 9 - 7 -

T r e c a p t h u s e o f t h f l a g ; i s t o r e s t h f a c t t h a t a n a d d i t i o no p e r a t i o n h a o c c u r r e d t h a t y i e l d e d c a r r y . I t h f l a g i l e f ts e b m i s t a k e t h e n t h i s c a r r y w i l l b a d d e d o t t h n e x t A D o p e r a t i o n w h e t h e r y o u w a n t i o r n o t . F o t h c a r r y t b p a s s e df r o m t h L S t t h M S B y o u h a v e t a r r a n g e t h A D C ' s t b d o n e i t h a p p r o p r i a t e o r d e r .

T h 6 5 1 0 , o f c o u r s e , h a s a n i n s t r u c t i o n t h a t c a r r i e s o u s u b t r a c t i o n w i t h c a r r y ; t h i s i s : -

S B S u B t r a c t f r o m t h a c c u m u l a t o r w i t h £ a r r y , t h d a t aa t h s p e c i f i e d m e m o r y l o c a t i o n .

e . g . S B 8 9

m e a n s l o o k i m e m o r y l o c a t i o n 8 9 a n s u b t r a c t t h n u m b e r t h a t y o f i n d t h e r e f r o m t h n u m b e r i t h a c c u m u l a t o r .

Page 65: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 65/243

S E C S E t t h C a r r y b i t T L e t ' s l o o k a t h a t i n s i m p l e p r o g r a m t t a k e f r o m 4 F o c h a n g e w e w i l l u s e t h i m m e d i a t e m o d e t l o a d t h e d a t a .

P R O G R A M 4 .

S E L D A I MS B C I MS T 1 0 2 4L D A I MS T A 5 5 2 9 6R T E N

B n o y o s h o u l d b w e l l e n o u g h e q u i p p e d t t r d o u b l e p r e c i s i o ns u b t r a c t i o n o y o u r o w s h a v e g o a t t h e e x e r c i s e s b e l o w i c a s e y o u h a v e p r o b l e m e x e r c i s e 4 - i e x p l a i n e d i f a i r d e t a i l t d e m o n s t r a t e t h c a r r y o p e r a t i o n .

E x e r c i s e 4 *

W r i t e p r o g r a m t s u b t r a c t 6 0 0 f r o m 8 0 0 u s i n g A B S O L U T E A D D R E S S I N G .S t o r e d a t a i m e m o r y l o c a t i o n s 8 9 0 o n w a r d s . D i s p l a y t h a n s w e r i 1 0 3 4 .A n s w e r o n p a g e 9 - 8 .

E x e r c i s e 4 .

W r i t e p r o g r a m t s u b t r a c t 5 0 0 1 0 f r o m t h s u m o f 3 0 0 ^ 0 a n d 4 0 0 ^ 0 -D i s p l a y t h a n s w e r i 1 0 4 0 / 1 i t h o r d e r L S B / M S B .

A n s w e r o p a g e 9 - 8 .

M u l t i p l i c a t i o nT h e * a r i t h m e t i c i n s t r u c t i o n s a v a i l a b l e f o t h 6 5 1 0 a l l o w a d d i t i o n sa n s u b t r a c t i o n s t b c a r r i e d o u t b u t n p r o v i s i o n i m a d e f o m u l t i p l i c a t i o n . T h i s h a t b c a r r i e d o u t , t h e r e f o r e , b s e r i e so r e p e a t e d a d d i t i o n s . F o r e x a m p l e , t h s u m 2 x 3 c a n b e x p r e s s e da 2 + 2 + 2 , a n d i t h u s r e l a t i v e l y s i m p l e t e v a l u a t e . T h e p r o c e s si t h u s o n e o f a d d i n g t t h a c c u m u l a t o r t h r e e t i m e s a n d r e q u i r e st h t h r e e t b e s e t u p i n l o o p t d e f i n e t h n u m b e r o p a s s e st h r o u g h t h i s . O c o u r s e , t h a c c u m u l a t o r s h o u l d c o n t a i n z e r o

Page 66: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 66/243

T h p r o c e s s i i l l u s t r a t e d i F i g . 4 . b e l o w , w h e r e n u m b e r o p a s s e s t h r o u g h t h l o o p ( i n t h i s c a s e 3 ) a n d A c u r r e n t s u m .

4 .

I i p u t i n t o p r o g r a m 4 . 5 b e l o w :

P R O G R A M 4 . C L L D Y I ML D A I MA D C I MD E B N S T L D A I MS T R T

2 5 1 0 2 4

5 5 2 9 6

W h e n r u n , t h e p r o g r a m g i v e s a f F ( C o m m o d o r e 6 f 6 f ) i 1 0 2 4 .W i t h i n t h i s p r o g r a m , t h k e y s e g m e n t i

P R O G R A M 4 . 5 a

• A D C I MD E - B N E 2 5

T h i s s m a l l l o o p t h a t d o e s t h w o r k i k n o w n a a n A L G O R I T H M . O n l i m i t a t i o n o t h i s s i m p l e a l g o r i t h m i t h a t i c a o n l y h a n d l e a a n s w e r u t 2 5 a f t e r t h a t , i g e n e r a t e s c a r r y a n c l o c k s t h a c c u m u l a t o r b a c k t o z e r o . i f n o a c c o u n t i t a k e n o f t h i s c a r r y ,t h e n t h a n s w e r h a s l o s t 2 5 6 f o r e a c h l o o p ! W h e n e l a b o r a t e ds o m e w h a t , t h i s a l g o r i t h m c a h a n d l e d o u b l e p r e c i s i o nm u l t i p l i c a t i o n . T h i s c a n b a c h i e v e d b y c h e c k i n g f o c a r r y a f t e re a c h a d d i t i o n a n i f c a r r y i g e n e r a t e d , a d d i n g o n e o n t o t h e M S B .O n w a o c a r r y i n g o u t h i s i n c r e m e n t i n g i b y m e a n s o t h i n s t r u c t i o n : -

I N I N C r e m e n t t h c o n t e n t s o t h s p e c i f i e d m e m o r yl o c a t i o n .

Page 67: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 67/243

P R O G R A M 4 . L D Y I MS T L D Y I ML D A I M/ " " i t

A D C I MB C I N

B N S T L D X I MS T L D S T S T R T

9 0 1

1

9 0

2 4 1 0 2 6

5 5 2 9 89 0 1 0 2 45 5 2 9 6

S e u 9 0 t r e c e i v e c a r r y b i t s

S e u n u m b e r o f t i m e s a r o u n d l o o pC l e a r a c c u m u l a t o r t r e c e i v e s u o L S B ' sC l e a r c a r r y p r i o r t a d d i n g .A d 1 6 t o o n c eI n c a r r y g e n e r a t e d t h e n s k i p . . .I n c r e m e n t c a r r y r e c o r d

C h e c k i f d e c r e m e n t e d t z e r oD i s p l a y L S s u L o a d i f o w h i t e d i s p l a yS t o r e i i c o l o u r R A D i s p l a y M S B

i w h i t e

W h e n r u n , t h i s s h o u l d d i s p l a y w h i t e A P o r 2 5 6 + 1 6 , o r 1 6 x 1 7 .

I t h e x a m p l e u s e d 1 6 x 1 7 i m a d e l i t t l e d i f f e r e n c e w h e t h e r t h a l g o r i t h m w a a r r a n g e d t a d 1 s e v e n t e e n t i m e s o t a d 1 s i x t e e n t i m e s . I f t h s u m 2 x 1 0 0 w a s a t t e m p t e d , h o w e v e r , t h e n i w o u l d o b v i o u s l y b m u c h q u i c k e r t a d 1 0 t w i c e r a t h e r t h a n t a d

o n h u n d r e d t i m e s . T h i s c o u l d b t a c k l e d b y w r i t i n g s h o r ts u b r o u t i n e t h a t e n s u r e s t h a t t h m u l t i p l i e r i p r o v i d e d w i t h t h s m a l l e s t o t h e t w o v a l u e s , a n d t h u s t h a l g o r i t h m i c y c l e d t h l e a s t p o s s i b l e n u m b e r o f t i m e s . H o w e v e r , o p a g e 4 - 1 9 a n o t h e rm e t h o d o m u l t i p l i c a t i o n w i l l b i n t r o d u c e d b i n a r y m u l t i p l i c a t i o n

a n t h i s k e e p s t h n u m b e r o i t e r a t i o n s d o w n t t h m i n i m u m .

D i v i s i o nD i v i s i o n u s i n g t h 6 5 1 0 i s p r o c e s s o f r e p e a t e d s u b t r a c t i o n i t h s a m e w a t h a t m u l t i p l i c a t i o n i o n e o f r e p e a t e d a d d i t i o n . I i i l l u s t r a t e d i p r o g r a m 4 . 6 a i w h i c h 3 i d i v i d e d b 2 I n t h i st h a c c u m u l a t o r i u s e d t s t o r e t h r u n n i n g r e m a i n d e r , i . e . s t a r t sw i t h 3 a n d p r o g r e s s i v e l y d e c l i n e s t z e r o ( 3 0 , 2 8 , 2 6 . . . 4 , 2 , 0 ) . T h

r e g i s t e r i u s e d t l o a d t h d i v i s o r i n t o m e m o r y w h i l e t h r e g i s t e r c o u n t s t h n u m b e r o f t i m e s t h a t t h s u b t r a c t i o n c a b m a d e .

Page 68: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 68/243

P R O G R A M 4 . 6 aL D Y I ML D X I MS T 9 0 L D A I M 3

r — - ► S E CS B C I MI N C M P 9 0 0B C S 2 4 7S T 1 0 2 4L D X I MS T 5 5 2 9 6S T A 1 0 2 6S T X 5 5 2 9 8R T

W h e n r u n , t h i s d i s p l a y s t h q u o t i e n t 1 ( a w h i t e l e t t e r 0 i 1 0 2 4 a n d t h r e m a i n d e r ( a a @ i 1 0 2 6 .

B i n a r y C o d e d D e c i m a l A r i t h m e t i cI a d d i t i o n t n u m b e r s b e i n g r e p r e s e n t e d i b i n a r y a n d e c i m a ln o t a t i o n h y b r i d o m i x e d n o t a t i o n , b i n a r y c o d e d d e c i m a l o B C D ,a l s o e x i s t s . T h u s a g e a n f o r m a t o f t h i s i d e s c r i b e d i A p p e n d i x1 p a g e A 1 - 7 - B C f o r m s b r i d g e b e t w e e n t h t w n o t a t i o n s a n i m a n y c a s e s g r e a t l y f a c i l i t a t e s o u t p u t . F o r t u n a t e l y f o u s , t h e6 5 1 0 c h i p c a h a n d l e B C D a r i t h m e t i c a n i t u r n e d o t t h B C h a n d l i n g m o d e b t h i n s t r u c t i o n : -

S E S I 3 t _ D e c i m a l m o d e o o p e r a t i o n .

T h i s i n s t r u c t i o n s e t s t h e D f l a g a u t o m a t i c a l l y t o a n t h e r e a f t e r , a r i t h m e t i c i d o n e i B C D . W h e n t h d e c i m a l m o d e o o p e r a t i o n i n l o n g e r r e q u i r e d i i c l e a r e d w i t h t h i n s t r u c t i o n : -

C L C J L e a r t h D e c i m a l f l a g .

T h i s s e t s t h f l a g b a c k t ' 0 a n t h e r e a f t e r , a r i t h m e t i c i d o n ei b i n a r y . Y o m a r e m e m b e r t h a t w e h a v e u s e d t h C L i n s t r u c t i o no n u m b e r o f o c c a s i o n s , p r i o r t u s i n g t h e A D C a n S B i n s t r u c t i o n s . I s h o u l d b c l e a r t y o n o t h a t w e w e r e e n s u r i n gt h a t t h e 6 5 1 0 c a r r i e d o u b i n a r y a r i t h m e t i c .

Page 69: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 69/243

W h e n r u n , p r o g r a m 4 . p u t s f C i 1 0 2 4 . I i u s u a l l y c o n s i d e r e dt b g o o d p r a c t i s e t c l e a r t h d e c i m a l f l a g a f t e r d o i n g a n y B C Da r i t h m e t i c , s i n c e m o s t a r i t h m e t i c i d o n e i b i n a r y h e n c e t h C L j u s t b e f o r e t h e R T S .

T h e x a m p l e g i v e n i p r o g r a m 4 - i i d e n t i c a l i e f f e c t t t h a r i t h m e t i c t h a t w e h a v e d o n e t d a t e , w i t h t h e x c e p t i o n t h a t i B C t h c a r r y o c c u r s a f t e r e a c h h a l f - b y t e e x c e e d s 9 T h i s i d e m o n s t r a t e d b e l o w i p r o g r a m 4 . 8 , w h i c h a d d s t o g e t h e r t w o 6 ' s . I fp r o g r a m 4 - i s t i l l i 8 2 t h e n 4 . 8 c a n b e P O K E d i b y

P R O G R A M 4 .

S E C L L D A I MS T L D A I MA D S T L D X I MS T C L R T

9 0

9 0 1 0 2 45 5 2 9 6

P O K E 8 3 1 , 6P O K E 8 3 6 , 6

P R O G R A M 4 .

S E C L L D A I MS T L D A I MA D S T L D X I MS T C L R T

9 0

9 0 1 0 2 4

5 5 2 9

W h e n r u n , p r o g r a m 4 . 8 p u t s w h i t e R i n 1 0 2 4 , w h i c h i i t w a o s a y i n g 1 2 T h i s c o m e s f r o m t h w a t h a t B C i s t o r e d i m e m o r y a N Y B B L E S ( n y b b l e i h a l f b y t e o u c h ! ! d o n ' t b l a m e m e d i d n ' t i n v e n t t h w o r d ) . T h ' R i t s e l f c o m e s f r o m t h c o m p u t e r ' sp o k e c o d e o f 1 8 w h i c h i b i n a r y i s

1 8 1 0 0 0 0 1 0 0 1 0 2H o w e v e r , t h m e m o r y l o c a t i o n i s t o r i n g t w n y b b l e s r a t h e r t h a n o n b y t e a n d h e n c e :

| o | o | o | i | o | o | i | r e a l l y | o o 1 m e a n s

3 = 1 1 0 - 2 1 0F i g . 4 .

T h n u m b e r r e p r e s e n t s 1 x 1 0 + 2 x 1 , o 1 2 ^ q .

Page 70: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 70/243

I w e l o o k a t h a n s w e r t p r o g r a m 4 . 8 , i w h i c h t h a n s w e rc o n s i s t e d o f t w o n y b b l e s h e l d w i t h i n o n e b y t e , w s e e o n e o f t h p r o b l e m s o b i m a n i p u l a t i o n . W h a t w a s n e e d e d i t h i s c a s e w a st e c h n i q u e f o m o d i f y i n g i n d i v i d u a l b i t s w i t h i n b y t e . I n o r d e r t e x t r a c t t h l o w e r o r d e r n y b b l e f r o m t h b i n a r y n u m b e r i t i s o n l yn e c e s s a r y t e r a s e t h h i g h e r o r d e r n y b b l e , i . e . t f i l l i w i t h' O ' s . T h i s c a n b d o n e w i t h a i n s t r u c t i o n : -

A N P e r f o r m l o g i c a l A N b e t w e e n t h a c c u m u l a t o r

A A N i l o g i c a l o p e r a t o r t h a t c o m p a r e s t w l o g i c s t a t e s a n p r o d u c e s a o u t p u t b a s e d o n t h e c o m p a r i s o n . I w e x a m i n e l o g i cA N g a t e s u c h a i u s e d i e l e c t r o n i c c i r c u i t r y , i m a k e s t h A N f u n c t i o n c l e a r e r .

A E l e c t r o n i c A N

F i g . 4 .

F i g u r e 4 . 4 s h o w s a A N D g a t e w i t h t w i n p u t s a n B a n d a n o u t p u tC I t f u n c t i o n i s u c h t h a t i f b o t h i n p u t s , A N B a r e s e t a T t h e n i t o u t p u t , C i ' 1 ' . H o w e v e r , i e i t h e r o b o t h o i t i n p u t s A N a r ' 0 t h e n i t o u p u t i ' O ' .T h i s i n o r m a l l y e x p r e s s e d i w h a t i k n o w n a T R U T H T A B L E , t h a tf o t h A B A N g a t e i f i g . 4 - b e i n g s h o w n i f i g . 4 . 5 .

T r u t h T a b l e f o r E l e c t r o n i c A N D

F i g . 4 . T u s t h t a b l e , t h e v a l u e o C t h o u t p u t i r e a d o f f o t h a p p r o p r i a t e i n p u t s o a n B T h u s , t a k i n g a i n p u t o ' 0 a n

i n p u t o ' 0 t h o u t p u t i 0

E x e r c i s e 4 *

U s i n g t h t r u t h t a b l e , f i g . 4 * 5 , w o r k o u t h l o g i c o u t p u t ( c o b t a i n e d f o t h f o l l o w i n g i n p u t s : -

Page 71: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 71/243

W h e n a A N i p e r f o r m e d b t h 6 5 1 0 , i o p e r a t e s o a l e i g h t b i t si n t h e a c c u m u l a t o r s i m u l t a n e o u s l y . T h u s i 2 5 i A N D e d w i t ht h e n :

2 5 5 1 O 1 1 1 1 1 1 1 1 2

• 1 0 0 0 0 0 0 0 1 .

i . e . O w n - * E q u a l s — ^

F i g . 4 .

T h r e s u l t o t h o p e r a t i o n i t h a t a l l t h b i t s A N D e d w i t h ' 0 h a v e b e e n s t r i p p e d o f l e a v i n g o n l y t h f i r s t b i t .

E x e r c i s e 4 .

W h a t i t h r e s u l t w h e n H 9 1 0 i A N D e d w i t h 5 2 1 O ?I y o h a v e p r o b l e m s , w o r k i o u t b i t b y b i t u s i n g t h t r u t h t a b l e .

A n s w e r o p a g e 9 - 9 •

A s t h e a b o v e e x e r c i s e s h o w s , t h e A N i n s t r u c t i o n c a b u s e d t s t r i p B I T S f r o m n u m b e r a n c o u l d b u s e d t c o n v e r t p a r t o f t h B C 1 f r o m p r o g r a m 4 . 8 . T h i s B C ' 1 2 ' w a s t o r e d a t w n y b b l e si o n b y t e . I f t h e M o s t S i g n i f i c a n t N y b b l e ( M S N ) c o u l d b c h a n g e di n t o f o u r z e r o s t h e n t h b y t e w o u l d r e a d o u d i r e c t l y a t h v a l u eo t h L e a s t S i g n i f i c a n t N y b b l e . S u c h m a s k i n g o u o f b i t s c a n b d o n e b u s i n g a n A N D c o m m a n d , a a n ' 1 i t h A N D i n g n u m b e r w i l ll e a v e T i n t h e n u m b e r A N D e d . A l s o f 0 f s i n t h e A N D i n g n u m b e rw i l l s w i t c h a n y ' 1 ' s i n t h e A N D e d n u m b e r t ' O ' s .

L e t ' s t r y t h a t w i t h t h B C 1 2

B C 1

B I N A R Y 1

B I N A R Y

► A N D —

F i g . 4 .

B A N D i n g t h B C n u m b e r w i t h 0 0 0 0 1 1 1 1 2 ( b i n a r y 1 5 i o ) » t h e f o u rm o s t s i g n i f i c a n t b i t s h a v e b e e n e r a s e d a n d t h n u m b e r c o n v e r t e d

Page 72: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 72/243

P R O G R A M 4 .

L D X I M 1 S T X 9 0 0L D A I M 1 A N 9 0 S T A 1 0 2 4L D X I MS T X 5 5 2 9 6R T

L o a d w i t h ' 1 5 'S t o r e X i 9 0 L o a d w i t h ' 1 8 'A N w i t h 9 0 S t o r e A i 1 0 2 4L o a d f o w h i t e d i s p l a yS t o r e i c o l o u r R A M

W h e n r u n , t h i s w i l l d i s p l a y w h i t e ' B i 1 0 2 4 .U s i n g i m m e d i a t e a d d r e s s i n g , A N D I M , p r o g r a m 4 . c a b r e - w r i t t e n a b e l o w :

P R O G R A M 4 . 9 a

L D A I M 1 A N D I M 1 S T 1 0 2 4L D X I MS T 5 5 2 9 6R T

W h e n d e f i n i n g t h t r u t h t a b l e o f t h A N i n s t r u c t i o n i t i s w r i t t e s o m e w h a t d i f f e r e n t l y f r o m t h a t u s e d i e l e c t r o n i c s . T h i s i b e c a u s e t h r e s u l t o f t h e A N o p e r a t i o n i d e p o s i t e d b a c k i t h a c c u m u l a t o r , i . e . t h i s f o r m s b o t h p a r t o t h i n p u t a n d t h e o u t p u t .T h t r u t h t a b l e f o r A N i g i v e n i f i g u r e 4 . b e l o w :D a t a f o r "A N D i n g

A c c u m u l a t o r *c o n t e n t s

T r u t h t a b l e f o r A N

F i g . 4 .

T h 6 5 1 0 c h i p a l s o u s e s t w o t h e r l o g i c a l o p e r a t o r s , o n o t h e s eb e i n g a O f u n c t i o n . W i t h t h M n e m o n i c s u s e d i t h i s b o o k i i r e f e r r e d t a s O R A .

Page 73: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 73/243

I t i s d e f i n e d f o r m a l l y a s : -

O R P e r f o r m l o g i c a l i n c l u s i v e O b e t w e e n t h A c c u m u l a t o r a n t h d a t a s p e c i f i e d .

I e l e c t r o n i c c i r c u i t r y , t h e O R i d e p i c t e d a s h o w n i P i g .4 . 9 .

\ A N

A n E l e c t r o n i c O

F i g . 4 .

I t m o d e o o p e r a t i o n i t h a t i i p r e s e n t o n A O Rt h e n t h o u t p u t i s e t ' I 1 , T h i s i b i t l i k e a A N i r e v e r s e t h A N g i v e s o n l y i b o t h i n p u t s a r ' I 1 ,w h i l e t h O g i v e s ' 0 o n l y i b o t h i n p u t s a r e ' 0 * . T h t r u t h t a b l e f o t h 6 5 1 0 ' s O R A c o m m a n d i g i v e n b e l o w i f i g u r e4 . 1 0 .

T r u t h t a b l ef o r O R

P i g . 4 . 1 0

I a c t i o n , t h O R c o m m a n d h a t h f o l l o w i n g e f f e c t :

B i n a r y o 1 4 9 1 0B i n a r y o 5 2 1 0

B i n a r y o f 1 8 1 1 0

P i g . 4 . 1 1

P u t t i n g t h a t i n t o p r o g r a m g i v e s :

Page 74: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 74/243

W h e n r u n , t h i s p r o g r a m w i l l p u 1 8 ( w h i t e r e v e r s e - f i e l d 5 i 1 0 2 4 .

A w i t h A N D , O R h a s e v e r a l m o d e s o a d d r e s s i n g t s u i td i f f e r e n t a p p l i c a t i o n s .

T h i r d a m o n g t h l o g i c a l o p e r a t o r s i s : -

E O P e r f o r m l o g i c a l E x c l u s i v e O b e t w e e n t h a c c u m u l a t o r a n d t h e d a t a s p e c i f i e d .

T h i s o p e r a t i o n i p r o b a b l y t h l e a s t e a s y t u n d e r s t a n d a n i b e s ti l l u s t r a t e d b y m e a n s o f t h e t r u t h t a b l e , f i g . 4 . 1 0 .

T r u t h t a b l e f o r E O

F i g . 4 . 1 2

O n w a o e x p r e s s i n g t h f u n c t i o n i t h a t t h o u t p u t w i l l b i e i t h e r o t h i n p u t s i b u n o b o t h . U s i n g t h i si n s t r u c t i o n w i t h t h a b o v e e x a m p l e ( 1 4 9 6 1 0 E O R e d w i t h 5 2 ) :

B i n a r y o 1 4 9 i oB i n a r y o 5 2 1 0

B i n a r y o f 1 6 1 1 OF i g . 4 . 1 3

T h p r o g r a m t d e m o n s t r a t e t h a t i b e l o w : '

P R O G R A M 4 . 1 1

L D A I M 1 4 E O R I M 5 S T 1 0 2 4L D X I MS T X 5 5 2 9 6

Page 75: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 75/243

E O R , l i k e t h o t h e r l o g i c a l o p e r a t o r s , h a s e v e r a l m o d e s o a d d r e s st f a c i l i t a t e i t u s i p r o g r a m s .

E x e r c i s e 4 .

C a l c u l a t e t h r e s u l t o f t h e f o l l o w i n g l o g i c a l o p e r a t i o n s : -

( i 1 0 0 1 0 A N D e d w i t h 8 7 k ) -( i i ) 7 5 1 O O R e d w i t h 2 7 1 0 -

( i i i ) 9 9 ! 0 E O R e d w i t h 5 7 1 O -( i v ) 9 4 ! 0 E O R e d w i t h t h r e s u l t o

1 0 0 1 0 A N D e d w i t h 8 7 1 0 »

W r i t e p r o g r a m t v e r i f y e a c h o p e r a t i o n .

A n s w e r s o p a g e 9 - 9 -

O t h e r F o r m s o B i M a n i p u l a t i o nO t h e r 6 5 1 Q i n s t r u c t i o n s e x i s t t h a t e n a b l e o n t m a n i p u l a t e b i t sw i t h i n b y t e a n a g r o u p t h e s e l e a d t t h m o v e m e n t o b i t s t t h r i g h t o l e f t w i t h i n t h b y t e i t s e l f .

I n t h e e a r l i e r e x a m p l e u s i n g B C a r i t h m e t i c , t h A N D i n s t r u c t i o nw a s a b l e t i s o l a t e t h L S N f r o m t h b y t e . I w a n o p o s s i b l e ,t h o u g h , t e x t r a c t t h M S N u s i n g t h a v a i l a b l e l o g i c . T h i s d o e sb e c o m e p o s s i b l e , h o w e v e r , u s i n g o n e o f t h b i m a n i p u l a t i o nc o m m a n d s : -

L S l o g i c a l S h i f t o t h s p e c i f i e d c o n t e n t s o n b i t t h R i g h t .

W h e n t h i s i d o n e , t h b i t s a r e m o v e d a l o n g o n p l a c e t t h r i g h tw i t h t h r i g h t - m o s t b i f a l l i n g o f f i n t o t h c a r r y a n ' 0 b e i n gp u i n t o t h l e f t - m o s t b i t . T h u s t h L S c o m m a n d o p e r a t i n g o

4 9 - j o S i v e s t h f o l l o w i n g :

1 4 9 1 0 1 F i g . 4 . 1 4

Page 76: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 76/243

U s i n g t h A c c u m u l a t o r m o d e L S R A t p r o v e t h e x a m p l e i s

P R O G R A M 4 . 1 2

L D A I M 1 4 L S R AS T 1 0 2 4L D X I MS T 5 5 2 9 6R T

W h e n r u n , t h i s w i l l p u 7 4 - j o ( w h i t e Q i 1 0 2 4 )B u s i n g f o u r s u c h r i g h t s h i f t s t h M S N n y b b l e i b y t e c a b m o v e d i n t o t h p l a c e o t h l e a s t s i g n i f i c a n t n y b b l e a n t h l e f tm o s t f o u r b i t s f i l l e d w i t h O ' s . T h i s e n a b l e s o n t i s o l a t e t h M S i B C D c a l c u l a t i o n .

T h i s i d e m o n s t r a t e d i p r o g r a m 4 . 1 3 , t h i s t i m e u s i n g L S i n i t sa b s o l u t e m o d e .

P R O G R A M 4 . 1 3

L D Y I M 1 \ L o a d ' 1 8 ' i n t o 9 0 0S T Y 9 0 0L D Y I M S e l o o p c o u n t e rL S R 9 0 0D E B N E 2 5 0L D A 9 0 0S T 1 0 2 4L D X I MS T X 5 5 2 9 6R T

S h i f t 9 0 f o u rp l a c e s t r i g h t ,

' P r i n t o u c o n t e n t so f 9 0 0i w h i t e

W h e n r u n , t h e p r o g r a m w i l l p r i n t ( w h i t e f A f ) i n 1 0 2 4 .E x e r c i s e 4 *

S u p p o s e t h a t t h a n s w e r t p r o b l e m i B C i 8 6 - j q .

W r i t e m a c h i n e - c o d e p r o g r a m t d e c o d e t h i s a n d d i s p l a y t h a n s w e ri d e c i m a l ( P O K E f o r m , i . e . c h a r a c t e r s ) i 1 0 2 4 a n 1 0 2 5 .

p o s s i b l e a n s w e r o n p a g e 9 - 1 1

f u r t h e r 6 5 1 0 i n s t r u c t i o n m i r r o r s L S i n t h a t i m o v e s t h b i t s t

Page 77: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 77/243

T h l e f t - m o s t b i i s h i f t e d i n t o t h c a r r y b i t a n d b i t i l o a d e dw i t h ' O 1 , A A S L i n s t r u c t i o n o p e r a t i n g o 1 4 g i v e s : -

A S o 1 4 9 - j 0C A R R Y

H 9 1 0 I T T 0 I 0 I 1 1 0 1 1 l O F i g . 4 . 1 5

U s i n g t h v a r i o u s m o d e s o a d d r e s s i n g , A S L c a o p e r a t e o d a t a i d i f f e r e n t l o c a t i o n s , e . g .

A S L A m e a n s A r i t h m e t i c S h i f t L e f t o d a t a i A A S 9 0 m e a n s A r i t h m e t i c S h i f t L e f t o d a t a i 9 0 0 .

U s i n g t h a c c u m u l a t o r m o d e t t e s t t h a b o v e e x a m p l e i s P R O G R A M 4 . 1 4

L D A I MA S L AS T L D X I MS T R T

1 4

1 0 2 45 5 2 9 6

W h e n r u n , t h i s p u t s 4 ( w h i t e a s t e r i s k ) i n 1 0 2 4 .

B i n a r y M u l t i p U c a t i o nW h a v e s e e n f r o m p r o g r a m s 4 . 5 a n 4 . t h a t m u l t i p l i c a t i o n c a b c a r r i e d o u u s i n g r e p e t i t i v e , o R E - I T E R A T I V E , p r o c e s s b u t w eh a v e a l s o s e e n t h a t t h i s i s l e n g t h y p r o c e s s a n a t i m e s v e r yt r i c k y . H o w e v e r , w t a c k l e d t h p r o b l e m v e r y m u c h f r o m p o i n t o v i e w o c o n v e n t i o n a l a r i t h m e t i c a l p r o c e s s e s a n b i n a r y h a i t o w w a o d o i n g t h e s e t h i n g s ! A t h 6 5 1 0 i t s e l f t h i n k s i b i n a r y a n h a n u m b e r o i n s t r u c t i o n s f o m a n i p u l a t i n g t h b i t s w i t h i n i t b y t e s , b i n a r y a r i t h m e t i c h a s l o t o f f e r .

F i r s t , l e t * 8 e x a m i n e o u w a o d o i n g d e c i m a l ( c o n v e n t i o n a l )a r i t h m e t i c . T a k e t h s u m 1 3 x 1 4 . W d e f i n e 1 a s t h e M U L T I P L I C A N Da n 1 a s t h e M U L T I P L I E R a n l a t h m u l t i p l i c a t i o n o u a b e l o w : -

1 M u l t i p l i c a n d1 M u l t i p l i e r5

Page 78: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 78/243

I n t h i s c o n v e n t i o n a l f o r m a t , w f i r s t m u l t i p l y t h m u l t i p l i c a n d b t h l o w e s t d i g i t o t h m u l t i p l i e r a n s t o r e t h i s a s t h e f i r s tp a r t i a l p r o d u c t , i . e . 4 x 1 3 = 5 2 . N e x t , w e m u l t i p l y t h m u l t i p l i c a n db t h s e c o n d d i g i t o t h m u l t i p l i e r , i . e . 1 x 1 3 , a n t h e n m u l t i p l yt h i s b 1 0 t o o b t a i n t h s e c o n d p a r t i a l p r o d u c t , i . e . 1 3 x 1 0 = 1 3 0 .T h u s t h t o t a l a n s w e r i t h s u o t h e t w o p a r t s , i . e . 5 2 + 1 3 0 = 1 8 2 .A c t u a l l y , i t h s e c o n d s t a g e i w o u l d h a v e b e e n m o r e c o r r e c t t s a y t h a t w m u l t i p l i e d t h m u l t i p l i c a n d b t h s e c o n d d i g i t o f t h m u l t i p l i e r a n t h e n m u l t i p l i e d t h r e s u l t b t h B A S E ( w h i c hh a p p e n e d t b 1 0 ) . T h e t o t a l a n s w e r w a t h e n t h s u o t h t w p a r t i a l p r o d u c t s .

I t i s q u i t e p o s s i b l e t p e r f o r m t h s a m e m u l t i p l i c a t i o n p r o c e s su s i n g n u m b e r s i b i n a r y f o r m a t .

F o e x a m p l e , t m u l t i p l y i b i n a r y ,

5 1 0 1 0 1 a n 7 1 0 1 1 1 ( w o r k i n go n l y t

b i t s )i . e .

P a r t i a l P r o d u c tP a r t i a l P r o d u c tP a r t i a l P r o d u c tP a r t i a l P r o d u c t

A N

0 1 1 10 1 0 1

0 1 1 10 0 0 0 0

0 1 1 1 0 00 0 0 0 0 0 0

C u r r e n t t o t a l 0 1 1 1C u r r e n t t o t a l 0 1 1 1C u r r e n t t o t a l 0 1 1 0 1 1C u r r e n t t o t a l 0 1 1 0 1 1

1 0 0 0 1 1 = 3 2 + 2 + 1 3 5 1 O

F i g . 4 . 1 7W i t h a l d i g i t s i t h m u l t i p l i e r e q u a l t o n e , a l t h s i g n i f i c a n tb i t s i n t h e p a r t i a l p r o d u c t s p a r t s h a v e t h s a m e p a t t e r n o d i g i t sa s t h e m u l t i p l i c a n d , i . e . " 1 1 1 " . T h u s t h e m u l t i p l i c a t i o n p r o c e s s i b i n a r y r e d u c e s t o n o s u c c e s s i v e a d d i t i o n f o l l o w i n g m o v e m e n t t t h l e f t o f m u l t i p l i c a n d .

8 - B i t M u l t i p U c a t i o nT h b l o c k d i a g r a m f o r t h i s p r o c e s s i g i v e n i f i g u r e 4 . 1 8 , w h e r eA n s w e r - A N S , D = M u l t i p l i c a n d , R = M u l t i p l i e r , N = c u r r e n t b i n u m b e r ,L S B = l e a s t s i g n i f i c a n t b i o m u l t i p l i e r .

Page 79: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 79/243

S e

|

S t a r t

A N t

R e m o v eL S

N

F i n a lA n s w e r

A N

N

S h i f tb

( N - 1 )B i t s

t L E F T

A d S h i f t e d

o n t oA N

Y e

F i g . 4 . 1 8

W o r k i n g t h r o u g h t h i s f l o w d i a g r a m f o t h e x a m p l e o 2 - j q 2 - j q

0 0 0 0 0 0 1 0 2S u c c e s s i v e p a s s e s p a s t a r r e f e r r e d t o a s A 1 A 2 e t c . F o t h i v e r y s i m p l e e x a m p l e w i t h n c a r r i e s , o n l y f o u r p a s s e s t h r o u g h t h l o o p w i l l b m a d e ( t w o w o u l d s u f f i c e a o n l y t h e t w o r i g h t - m o s d i g i t s a r s i g n i f i c a n t ) .

S T A R TA A

A N S N1 1 s L S

= 2 n L S S h i f t ( N - 1 ) b i t s ( i . e . 1 t l e f t ,i . e . 0 0 1 0 2 — ^ 0 1 0 0 2a d t A N S , i . e . A N S = 0 + 0 0 1 0 2 = 0 0 1 0 2

Page 80: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 80/243

P u t t i n g t h i s i n t o p r o g r a m g i v e s :

P R O G R A M 4 . 1 5L D X I ML D Y I MS T 9 0 S T X 9 0 2L D A I M

I n i t i a l i s a t i o n

L S C h e c k i m u l t i p l i c a t i o n n e c e s s a r yB C 4

• M u l t i p l y 1 i f n e c e s s a r y

C h e c k i a l b i t s c h e c k e d

C L A D 9 0

- ► A S L 9 0 D E

— B N E 2 4

S T A 1 0 2 4L D X I MS T X 5 5 2 9 6R T

W h e n r u n , t h e p r o g r a m w i l l p r i n t w h i t e ( 4 i 1 0 2 4 . I m a b c h e c k e d b y c h a n g i n g t h L D X I M i n s t r u c t i o n t i n p u t d i f f e r e n tm u l t i p l i c a n d a n m u l t i p l i e r .

E x e r c i s e 4 .

R e - w r i t e p r o g r a m 4 . 1 5 s o a s t m u l t i p l y t w d i f f e r e n t n u m b e r st o g e t h e r .

O n p o s s i b l e a n s w e r o p a g e 9 - 1 2 .

U n f o r t u n a t e l y , p r o g r a m 4 . 1 5 i o n l y r e a l l y h a l f t r u e a a n e i g h t b i tm u l t i p l i c a t i o n r o u t i n e a n w o r k s o n l y o v e r r a n g e o f s m a l l n u m b e r sf o t h m u l t i p l i e r a n m u l t i p l i c a n d . I t h f u l l r o u t i n e , t h A S i n s t r u c t i o n m u l t i p l i e s t h m u l t i p l i c a n d b t h b a s e e i g h t t i m e s .T h u s , b t h e i g h t h s h i f t t h r i g h t - m o s t b i w o u l d h a v e f a l l e n o f t h l e f t - h a n d e n o t h r e g i s t e r . I f a c t , w i t h t h n u m b e r 2 1 w o r k e d t h r o u g h i n t h e e x a m p l e , t h s e c o n d b i ( r e p r e s e n t i n g 2 ^ o 2 ^ q ) w o u l d b l o s t a f t e r s e v e n s h i f t s . H o w e v e r , t h i s d i d n o t

Page 81: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 81/243

o l e f t - m o s t b i t s w o u l d b s i g n i f i c a n t a n t h a n s w e r o b t a i n e w o u l d b e w r o n g . F o r t u n a t e l y , w h e n t h l e f t - m o s t b i t f a l l s o u t o fr e g i s t e r d u r i n g a n A S L i d o e s n o t j u s t f a l l i n t o s p a c e b u t i c a u g h t i t h c a r r y . T h u s t h p r o b l e m i o n o r e t r i e v i n g t h ia n t r a n s f e r r i n g i t t h M S o f t h e a n s w e r . T h i s c a n b e a c h i e v e b m e a n s o t h c o m m a n d : -

R O R O t a t e J L e f t t h c o n t e n t s o t h s p e c i f i e d a d d r e s s .

I n t h i s o p e r a t i o n , a l t h b i t s o t h s p e c i f i e d a d d r e s s a r r o t a t e d l e f t , w i t h t h c a r r y b i b e i n g l o a d e d i n t o t h r i g h t - m o s b i t a n d t h l e f t - m o s t b i b e i n g t r a n s f e r r e d t t h c a r r y .

T h e R O L O p e r a t i o n

F i g . 4 . 1 9

A t h r o t a t i o n i n v o l v e s t h c a r r y b i t , i t i s k n o w n a 9 - b i r o t a t i o n a n p r o v i d e s m e a n s o f p i c k i n g t h c a r r y b i b a c k u a g a i n . U s i n g R O L e n a b l e s o n t w r i t e a n e i g h t b i t m u l t i p l i c a t i o a c c o m m o d a t i n g t h c a r r y b u t p r o d u c e s r a t h e r c o m p l e x p r o g r a m S u c h p r o g r a m i l i s t e d i c h a p t e r ( p a g e 5 - 7 ) a s , t o m a k e i c l e a r e r t f o l l o w , b o t h l a b e l s a n m e m o r y l a b e l s h a v e b e e n u s e d .

T h i n s t r u c t i o n R O L h a r i g h t - h a n d e d c o l l e a g u e : -

R O R R O t a t e R i g h t t h c o n t e n t s o t h s p e c i f i e d a d d r e s s .

B o t h t h e s e i n s t r u c t i o n s c a b a d d r e s s e d i s e v e r a l w a y s a n d t a kf o r m s a s : -

R O L A

R O R A

R O t a t e

R O t a t e

^ e f t

R i g h t

t h

t h

c o n t e n t s

c o n t e n t s

o

o

t h

t h

A c c u m u l a t o r .

A c c u m u l a t o r .

S u c h f o r m s w i l l b d e s c r i b e d w h e n u t i l i s e d .

O n o t h e r i n s t r u c t i o n i a v a i l a b l e f o b i m a n i p u l a t i o n : -

Page 82: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 82/243

W h i l e B I p e r f o r m s t h s a m e l o g i c a l f u n c t i o n a A N D , i d i f f e r s i t h a t i l e a v e s b o t h a c c u m u l a t o r a n m e m o r y a t h e y a r e . I d o e s ,h o w e v e r , m o d i f y t h r e l e v a n t f l a g s i n t h e P S i t h f o l l o w i n g w a y :

T h f l a g i s e i t h r e s u l t o t h A N D i n g i z e r o ( a n dc l e a r e d , o f c o u r s e , i f t h r e s u l t i n o t z e r o ) .T h e N f l a g i a f f e c t e d a f o l l o w s : - b i o t h l o c a t i o n b e i n gt e s t e d i c o p i e d t t h P r o c e s s o r S t a t u s r e g i s t e r ( b i t o t h P S b e i n g t h N f l a g ) . T h i s i s v e r y c o n v e n i e n t w a o t e s t i n gw h e t h e r t h c o n t e n t s o p a r t i c u l a r l o c a t i o n a r p o s i t i v e o n e g a t i v e w i t h o u t t h n e c e s s i t y o l o a d i n g t h v a l u e i n t o o n e o f t h er e g i s t e r s .

T h e V f l a g ( w h i c h w e h a v e n ' t r e a l l y d i s c u s s e d y e t ) i b i o t h P r o c e s s o r S t a t u s r e g i s t e r . T h e B I T i n s t r u c t i o n a l s o c o p i e s b i o t h l o c a t i o n b e i n g t e s t e d t b i o t h P S R . T h i s i s n ' t q u i t es u s e f u l a t h f l a g , a b i d o e s n ' t n o r m a l l y s i g n i f y a n y t h i n gv e r y s p e c i a l . H o w e v e r , i y o l o o k a s o m e o f t h c l e v e r m a c h i n ec o d e p r o g r a m m i n g u s e d i t h B A S I C i n t e r p r e t e r a n o p e r a t i n gs y s t e m , y o w i l l o c c a s i o n a l l y f i n d s o m e v e r y n e a t u s e s o t h B I i n s t r u c t i o n o p e r a t i n g o t h f l a g .

U s i n g t h e s e b i n a r y i n s t r u c t i o n s p r o c e s s a n a l o g o u s t b i n a r ym u l t i p l i c a t i o n c a b c a r r i e d o u t .

8 - B i t B i n a r y D i v i s i o nT h i s p r o c e s s i a n a l o g o u s t t h b i n a r y m u l t i p l i c a t i o n r o u t i n e ,n e e d i n g o n l y r e - i t e r a t i o n s t h a n d l e a 8 - b i t n u m b e r . I t i si l l u s t r a t e d i p r o g r a m 4 . 1 5 A , w h e r e t h d i v i d e n d ( i n t h i s c a s e 3 1 i s t o r e d i l o c a t i o n 9 0 0 a n t h d i v i s o r ( 2 ) , i 9 0 1 . T h r e g i s t e r i u s e d a t h l o o p c o u n t e r t e n s u r e t h a t 8 p a s s e s a r m a d e t h r o u g h t h a l g o r i t h m . B m e a n s o f a n A S a n R O L Ai n s t r u c t i o n , t h r e m a i n d e r i b u i l t u i n t h e a c c u m u l a t o r .

Page 83: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 83/243

P R O G R A M 4 . 1 5 A

L D X I MS T L D X I MS T L D Y I ML D A I M

► A S LR O L AC M

S B I N

B N L D S T L D Y I MS T S T S T R T

5 9 0

9 0

9 0

9 0

9 0 9 0

2 3 9 0 1 0 2 4

5 5 2 9 61 0 2 65 5 2 9 8

W h e n r u n , t h i s w i l l d i s p l a y t h q u o t i e n t 1 ( a 0 i n 1 0 2 4 a n t h r e m a i n d e r ( a a A i 1 0 2 6 .

Page 84: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 84/243

Page 85: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 85/243

C H A P T E RA d v a n c e d F u n c t i o n s o t h A s s e m b l e r

L a b e l sT h u s o l a b e l s e n a b l e s p r o g r a m t b d i r e c t e d t n a m e di n s t r u c t i o n w i t h o u t t h n e c e s s i t y o c a l c u l a t i n g b r a n c h e s o j u m pa d d r e s s e s . f a n c i e r t e r m f o r l a b e l i S Y M B O L I C L A B E L a t h l a b e li t s e l f i s y m b o l i c o l o c a t i o n i m e m o r y . F o i n s t a n c e , t h i n s t r u c t i o n

B N L 0 0 P 1

i n s t r u c t s t h a s s e m b l e r t c r e a t e t h m a c h i n e c o d e t h a t t e l l s t h 6 5 1 0 t b r a n c h t a i n s t r u c t i o n l a b e l l e d L 0 0 P 1 . T h u s , L 0 0 P 1 S T A X1 0 2 4 c r e a t e s l a b e l c a l l e d L 0 0 P 1 , w h o s e a d d r e s s i t h s a m e a s t h e" S T A X " i S T A X 1 0 2 4 . I o r d e r t t e l l t h a s s e m b l e r t h a t l a b e li l a b e l a n d n o t a n i n s t r u c t i o n , i t i s p r e c e d e d b a a s t e r i s k( * ) . T h i s r u l e i o n l y c o n v e n t i o n t h a t h a b e e n c h o s e n w h e nw r i t i n g t h i s p a r t i c u l a r a s s e m b l e r . T h u s , t h b e g i n n i n g o L 0 0 P 1w o u l d b e n t e r e d :

* L 0 0 P 1 S T A X 1 0 2 4

F u r t h e r c o n v e n t i o n s m u s t b o b s e r v e d w h e n u s i n g l a b e l s ,p a r t i c u l a r l y t h o s e c o n c e r n i n g s p a c e s . T h e a s t e r i s k , f o i n s t a n c e ,m u s t b f o l l o w e d i m m e d i a t e l y b t h l a b e l ( n s p a c e b e t w e e n ) . I m a b e a s l o n g a r e q u i r e d b u m u s t N O c o n t a i n a n s p a c e s . T h e r ei n p a r t i c u l a r l y t e c h n i c a l r e a s o n f o t h i s , i t i s s i m p l y t h a t t h a s s e m b l e r l o o k s f o r s p a c e i o r d e r t w o r k o u h o l o n g t h l a b e li s F o t h i s r e a s o n t h l a b e l m u s t b f o l l o w e d b y s p a c e a n d t h e n

n o r m a l i n s t r u c t i o n . T h e s e r u l e s m a s o u n d l i t t l e f o r m i d a b l eb u d o n ' t w o r r y , t h a s s e m b l e r w i l l p i c k u a n y e r r o r s a n l e y o k n o w w h a t i w r o n g w i t h a n y p a r t i c u l a r l i n e . W h e n r e f e r r i n g t l a b e l i a i n s t r u c t i o n i t i s o n l y n e c e s s a r y t r e p l a c e t h o p e r a n dw i t h t h l a b e l i t s e l f .T s u m m a r i s e : L A B E L S

( i l a b e l i d e f i n e d b y t h a s t e r i s k ( * t h a t p r e c e d e s i t ( i i ) T h e l a b e l m a b e o f a n l e n g t h b u i t i s a s w e l l t s t i c k

t a b o u t s i x c h a r a c t e r s .

( i i i ) T h e r e m u s t b e n o g a b e t w e e n t h a s t e r i s k a n t h l a b e l .

Page 86: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 86/243

I a l s o u n d s l i t t l e c o m p l i c a t e d s l e t ' s s e i i l l u s t r a t e d i p r o g r a m . T h i s u s e s t w l o o p s , c a l l e d " L O O P 1 " a n d " L 0 0 P 2 " a n d o e ss o m e u n n e c e s s a r y b r a n c h i n g a n j u m p i n g b y w a y o i l l u s t r a t i o n .

P R O G R A M 5 . ( I A s s e m b l y l a n g u a g e )L D X I M 1 6 L D Y I MJ M L 0 0 P 2

* L 0 0 P 1 L D A I M 8 S T A X 1 1 8 3T Y S T A X 5 5 4 5 5D E B N L 0 0 P 1J M E N

* L 0 0 P 2 L D A I M 9 S T A X 1 0 2 3T Y S T A X 5 5 2 9 5D E B N L 0 0 P 2L D X I M 1 2 D E J M L 0 0 P 1

* E N D R T E N

A l t h o u g h t h i s p r o g r a m j u m p s a b o u t s o m e w h a t , i i s t i l l r e l a t i v e l ye a s y t f o l l o w . I s t a r t s b y i n i t i a l i s i n g a n Y t h e n j u m p s t l o o p a n o c o m p l e t i o n r e i n i t i a l i s e s a n a n t h e n j u m p s b a c kt l o o p a n f r o m t h e r e t t h e E N D . N o t e t h a t * E N D w i l l n o ti n d i c a t e t h e n d o f a s s e m b l y ; t h " * E N D " i l a b e l w h e r e a s " E N D "o i t o w ( w i t h o u t t h a s t e r i s k ) i t h p s e u d o - c o d e t h a tt e r m i n a t e s t h a s s e m b l y p r o c e s s . O n c e t h i s p r o c e s s i c o m p l e t e ,t h p r o g r a m w i l l r e s i d e i m e m o r y i e x a c t l y t h s a m e f o r m a t a a n o t h e r p r o g r a m t h a t h a b e e n e n t e r e d . T c h e c k t h i s , l i s t t h ep r o g r a m f r o m 8 2 a n d t h e f o l l o w i n g s h o u l d a p p e a r :

P R O G R A M 5 . ( i d i s a s s e m b l e d a s s e m b l y l a n g u a g e )I A s s e m b l y I d i s a s s e m b l e dL a n g u a g e A s s e m b l y L a n g u a g eL D X I M 1 6 L D X I M 1 6 L D Y I M L D Y I MJ M P L 0 0 P 2 J M P 8 5 L D A I M 8

Page 87: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 87/243

* L 0 0 P 2 L D A I M 9 L D A I M 9 S T A X 1 0 2 3 S T A X 1 0 2 3T Y T Y S T A X 5 5 2 9 3 S T A X 5 5 2 9 5D E D E B N L 0 0 P 2 B N E 2 4 L D X I M 1 2 L D X I M 1 2 D E D E J M L 0 0 P 1 J M 8 3

* E N D R T R T E N

P r o g r a m 5 . c a e x i s t i v a r i e t y o f f o r m s , t h r e e o w h i c h a r r e a d i l y a v a i l a b l e . I i t o r i g i n a l f o r m i w a w r i t t e n i a s s e m b l yl a n g u a g e w i t h l a b e l s a n d t h i s w a c o n v e r t e d b t h a s s e m b l e r i n t om a c h i n e c o d e a n s t o r e d i m e m o r y i t h i s f o r m . W h e n t h a s s e m b l e ri t h e n a s k e d t l i s t t h i s p r o g r a m i r e a d s t h m a c h i n e c o d e f r o mm e m o r y a n c h a n g e s t h i s b a c k i n t o a s s e m b l y l a n g u a g e . W e r e t h i sp r o c e s s t b c a r r i e d o u i m m e d i a t e l y a f t e r a s s e m b l y t h e n i w o u l db p o s s i b l e t r e - l a b e l t h l a b e l p o i n t s b e d i t i n g t h a s s e m b l e rb u a c u r r e n t l y w r i t t e n t h i s i n o s o i c o m m o n w i t h o t h e ra s s e m b l e r s . M o r e o v e r , o n c e t h B A S I C p r o g r a m h a b e e n r e - r u n , t h v a r i a b l e s , i . e . t h L A B E L S a n L A B E L R E F E R E N C E S , w i l l h a v e b e e nl o s t . R e - c r e a t i n g t h a s s e m b l y p r o g r a m f r o m m a c h i n e c o d e i k n o w na D I S A S S E M B L Y , i . e . t h e ' l i s t ' c o m m a n d c o u l d b r e - t i t l e d a ' d i s a s s e m b l e 1 a n d t h i s p r o c e s s c a n n o t r e - c r e a t e l a b e l s .

W h e n r u n , t h i s p r o g r a m w i l l p r i n t f o u r r o w s o f w h i t e d i a m o n d s a n t h r e e o b l a c k h e a r t s i t h t o s e v e n s c r e e n r o w s .

E x e r c i s e 5 *

A d f u r t h e r l o o p l o o p 3 a f t e r l o o p i p r o g r a m 5 . 2 .R e - w r i t e t h p r o g r a m t r u l o o p f i r s t , f o l l o w e d b y l o o p a n t h e n l o o p 2 L o o p s h o u l d p u t w r o w s o f r e d a s t e r i s k s o t h s c r e e n b e l o w t h d i a m o n d s .

p o s s i b l e a n s w e r i g i v e n o n p a g e 9 - 1 2 .

M e m o r y L a b e l sI a d d i t i o n t l a b e l l i n g i n s t r u c t i o n s , t h a s s e m b l e r a l s o a l l o w sm e m o r y l o c a t i o n s t b g i v e n l a b e l s . O n c e a g a i n , t h a s s e m b l e rn e e d s t b t o l d w h a t t e x p e c t a n d t h e p r e s e n c e o m e m o r y l a b e li i n d i c a t e d b a " @ " a t t h b e g i n n i n g o l i n e . I t i s f o l l o w e di m m e d i a t e l y b y t h n a m e a s s i g n e d t t h a t l o c a t i o n a n t h e n , a f t e r

Page 88: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 88/243

P r o g r a m 5 - i l l u s t r a t e s t h u s o m e m o r y l a b e l s i d o u b l ep r e c i s i o n a d d i t i o n i a d d s t o g e t h e r t w 1 b i n u m b e r s : -

N u m b e r 2 7 6 0 1 0N u m b e r 2 9 4 8 1 0

m a d e u p o f L S B 1 a n M S B 1 , a n d L S B 2 a n d M S B 2 ; t h e a n s w e r s a r e s t o r e di A N S L S B 1 a n d A N S M S B 2 .

P R O G R A M 5 . © L S B 1© M S B 1© L S B 2© M S B 2

9 0 9 0 9 0 9 0

© A N S L S B 9 0 © A N S M S B 9 0

L D A I MS T L D A I MS T L D A I MS T L D A I MS T

C L

A D S T S T

L D A D S T S T

L D X I MS T S T R T E N

1 M S B 12 0 L S B 1

M S B 2

D e f i n e m e m o r yD e f i n e m e m o r yD e f i n e m e m o r yD e f i n e m e m o r yD e f i n e m e m o r yD e f i n e m e m o r y

S t o r e M S B 1 i

S t o r e L S B 1 i n

S t o r e M S B 2 i

1 8 S t o r e L S B 2 i L S B 2

L S B 1A N S L S B1 0 2 5M S B 1M S B 2A N S M S B1 0 2 4

5 5 2 9 65 5 2 9 7

l o c a t i o nl o c a t i o nl o c a t i o nl o c a t i o nl o c a t i o nl o c a t i o n

m e m o r y

m e m o r y

m e m o r y

m e m o r y

f o L S B 1f o M S B 1f o r L S B 2f o r M S B 2f o A N L S f o r A N S M S

C l e a r c a r r y p r i o r t a d d i t i o nA d d L S B ' s t o g e t h e r , s t o r e a n s w e ri A N S L S B a n

L o a d M S B 1 a n

p r i n t o

a d w i t h

s c r e e n

c a r r y t M S B 2 , s t o r e a n s w e r i A N S M S B a n p r i n t o s c r e e n

L o a d i n t o c o l o u r R A i r e

t p r i n t

W h e n r u n , p r o g r a m 5 - w i l l d i s p l a y r e ( 1 4 ) i n 1 0 2 4 a n d r e ( 1 2 4 ) i 1 0 2 5 .A p r o m i s e d ( ! i c h a p t e r 4 l i s t i n g i g i v e n b e l o w o f a n e i g h t

Page 89: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 89/243

A b b r e v i a t i o n s u s e d i t h i s p r o g r a m a r e :M P M P T E M PR E S L S BR E S M S BA L G ON O C A R R Y

P R O G R A M 5 .

© M P D

© T E M P© R E S L S B© R E S M S B

* A L G O

♦ N O C A R R Y

M u l t i p l i e r 2 5 M u l t i p l i c a n d 2 5 T e m p o r a r y l o c a t i o n t s t o r e c a r r y b i R e s u l t L e a s t s i g n i f i c a n t b i R e s u l t M o s t s i g n i f i c a n t b i S t a r t o m u l t i p l i c a t i o n a l g o r i t h mJ u m p t p o i n t i n c a r r y a r i s e s

9 0 L D A I MS T 9 0 L D A I MS T 9 0 9 0 9 0 L D A I MS T S T S T L D Y I ML S B C L D C L A D S T L D A D S T A S R O D E B N L D S T L D X I MS T L D S T S T R T

2 5 M P

2 5 M P

T E M PR E S L S BR E S M S B

M P N O C A R R YR E S L A B

M P R E S L S BR E S M S BT E M PR E S M S BM P T E M P

A L G OR E S L S B1 0 2 5

5 5 2 9 7R E S M S B1 0 2 45 5 2 9 6

D e f i n e m e m o r y l o c a t i o n f o m u l t i p l i e r , l o a d a n d s t o r em u l t i p l i e rD e f i n e m e m o r y l o c a t i o n f o m u l t i p l i c a n d , l o a d a n d s t o r em u l t i p l i c a n dD e f i n e m e m o r y l o c a t i o n s f o t e m p o r a r y s t o r e , a n d L S B / M S Bo f r e s u l tI n i t i a l i s e ( b l o a d i n g i z e r o ) t h t e m p o r a r y a n r e s u l t s t o r e

S e t l o o p c o u n t e r t C h e c k i r i g h t - m o s t b i t o fm u l t i p l i e r = 0 ; b r a n c h i s C a l c u l a t e c u r r e n t p a r t i a lp r o d u c t a n d a d d i n t c u r r e n t p a r t i a l s u

A d c u r r e n ts u

c a r r y i n t o M S

C u r r e n t P a r t i a l O , s e u n e x t l o o p

D i s p l a y r e s u l t o f L S a n M S

Page 90: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 90/243     

         

      

      

      

          

      

      

      

      

      

      

      

      

      

           

      

      

      

   

      

      

      

  

      

      

      

     

      

      

      

     

      

      

      

Page 91: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 91/243

M a c r o I n s t r u c t i o n sO n f u r t h e r f e a t u r e o y o u r a s s e m b l e r i t h a t i e n a b l e s y o u t u s M A C R O i n s t r u c t i o n s . T h e s e a r b l o c k s o c o d e t h a t y o u w i s h t r e p e a t a n d a r e t h u s g i v e n l a b e l . W h e n y o u w i s h t i n s e r t t h e s ei n t o y o u r p r o g r a m y o u s i m p l y n e e d t t y p e i n t h e l a b e l a n d t h i sa u t o m a t i c a l l y i n s e r t s t h w h o l e m a c r o . T h u s m a c r o i v e r ys i m i l a r t o s u b r o u t i n e e x c e p t t h a t t h a s s e m b l e r w r i t e s i i e v e r y t i m e i i c a l l e d r a t h e r t h a n u s i n g J S i n s t r u c t i o n s . A a e x a m p l e , t a k e s h o r t r o u t i n e t h a t c l e a r s t h s c r e e n a n p u t s i b o r d e r c a l l e d C L S B D R . A s s u m i n g t h a t t h p r o g r a m h a h i e r a r c h i c a lm e n u s t r u c t u r e a n c a l l s t h i s r o u t i n e t i m e s , i c o u l d b w r i t t e ni a ( i b e l o w o F i g . 5 . 2 i n w h i c h t h r o u t i n e i c a l l e d a s s u b r o u t i n e , o r a s i n ( i i ) i w h i c h t h r o u t i n e i w r i t t e n i t i m e s a m a c r o .

( i J S C L S B D R .

p r i n t m e n u

J S C L S B D R

— — p r i n t m e n u

J S R C L S

p r i n . t m e n u

S U B R O U T I N EC L E A R S C R E E NP R I N T B O R D E RR T S -

( i i )M A C R O

M E N U

M A C R O

M E N U

M A C R O

M E N U

C L S B D R

C L S B D R

C L S B D R

Page 92: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 92/243

r e a d s m o r e l o g i c a l l y a n i e a s i e r t f o l l o w . T h e y t a k e u p m o r es p a c e t h a n s u b r o u t i n e , w h i c h i o n l y w r i t t e n o n c e , b u a t h e yd o n ' t u s u p r o c e s s o r t i m e j u m p i n g a b o u t , t h e y r u n m o r e q u i c k l y .J u s t - w h e t h e r m a c r o o s u b r o u t i n e i m o r e p r e f e r a b l e i a n p a r t i c u l a r c a s e i . l e f t t t h i n d i v i d u a l p r o g r a m m e r .

T i d e n t i f y m a c r o o n t h i s p a r t i c u l a r a s s e m b l e r i t i s p r e c e d e d b a " + " s i g n ( a t h m a c r o i l a t e r ' a d d e d ' i n t o t h p r o g r a m ) . A w i t h o t h e r f e a t u r e s , t h s i g n m u s t i m m e d i a t e l y p r e c e d e t h m a c r o ' sn a m e w h i c h m a b a n y l e n g t h a n d t h i s s h o u l d b e t h e o n l y e n t r y o t h a t p a r t i c u l a r l i n e , e . g . . " + M A C R 0 1 " T h f i r s t o c c u r r e n c e o t h m a c r o i t h e n t y p e d i n T h e e n d o f t h m a c r o i s i g n a l l e d b " + s i g n f o l l o w e d b " E N D " . T o i n c l u d e f u r t h e r c o p y o t h m a c r oi n t h e p r o g r a m a l a t e r p o i n t , a n o t h e r M A C R O 1 s a y , j u s t t h l i n e+ M A C R O 1 n e e d s t b e n t e r e d i n t o t h p r o g r a m a t t h e a p p r o p r i a t ep o i n t s e F i g . 5 » 3 «

+ M A C R O 1

L D A I M 9 L D X I M 4 0S T A X 1 0 2 4D E B N 2 5

F i r s t o c c u r r a n c e o f M A C R 0 1i n s e r t e d i n t o p r o g r a m a t h i s p o i n t . I i d e f i n e dh e r e a t t h e f i r s t o c c u r r a n c e

+ E N D S i g n a l s t h e e n d o t h m a c r od e f i n i t i o n

+ M A C R 0 1 A s s e m b l e r i n s e r t s s e c o n dc o p y o t h m a c r o h e r e ,

R T E N

F i 5 -

W h e n u s i n g l a b e l s , l a b e l r e f e r e n c e s a n m a c r o s , i s h o u l d b b o r n ei m i n d t h a t t h e a s s e m b l e r n e e d s t s t o r e t h n a m e s a n l o c a t i o n sd u r i n g t h a s s e m b l y p r o c e s s . T h e v e r s i o n o a s s e m b l e r s u p p l i e d o t h t a p e w i l l a c c e p t u p t o m a x i m u m o 2 l a b e l s o r m e m o r y l a b e l s ,2 l a b e l r e f e r e n c e s a n 1 m a c r o s . . - - M a c r o s w h i c h c o n t a i n l a b e l s o r e f e r e n c e s c r e a t e n e l a b e l s a n d r e f e r e n c e s a n d t h e s e m u s t a l s o b t a k e n i n t o a c c o u n t . T h u s , r e p e a t i n g m a c r o w i t h l a b e l a n dr e f e r e n c e s c r e a t e s a n e x t r a l a b e l s a n r e f e r e n c e s o n m a c r o .

Page 93: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 93/243

a r d i m e n s i o n e d i l i n e n o 7 o t h a s s e m b l e r p r o g r a m( t e n a b l e 3 l a b e l s t b u s e d ) L i n e 7 s h o u l d b a l t e r e dt c o n t a i n :7 D I F $ ( 3 1 ) , F ( 3 1 ) .

b L a b e l r e f e r e n c e s a r s t o r e d i K $ ( X ) , K % ( X ) a n d K 1 # ( X )w h i c h a r d i m e n s i o n e d i l i n e n o 8 0 ( t o e n a b l e 4 0 l a b e lr e f e r e n c e s t b u s e d ) . L i n e 8 0 s h o u l d c o n t a i n :8 D I K $ ( 3 9 ) , K * ( 3 9 ) , K 1 * ( 3 9 ) .

c M a c r o s a r s t o r e d i M $ ( X ) , U % ( x ) a n M 1 # ( X ) w h i c h a r d i m e n s i o n e d i l i n e n o 8 ( t e n a b l e 1 m a c r o s t b u s e d ) . L i n e 8 s h o u l d c o n t a i n :8 D I M $ ( 1 5 ) , M * ( 1 5 ) , M 1 J K ( 1 5 ) .

W a r n i n g : T h e A s s e m b l e r f o r t h e C o m m o d o r e 6 i n c l u d e ss e c t i o n w r i t t e n i m a c h i n e c o d e w h i c h i i n i t i a l l y l o a d e di m e m o r y i m m e d i a t e l y f o l l o w i n g t h B A S I C p r o g r a m . T h i sm a c h i n e c o d e p r o v i d e s t h f a c i l i t i e s f o r t h e M a c h i n eL a n g u a g e M o n i t o r ( M L M ) w h i c h a r d i s c u s s e d i n t h e n e x tc h a p t e r . T h e f i r s t t i m e t h a t t h a s s e m b l e r i r u n , t h i sm a c h i n e c o d e s e c t i o n i m o v e d i n t o t h e b l o c k o R A a n t h s p a c e f o l l o w i n g t h B A S I C p r o g r a m i r e l e a s e d .

T h e r e i r i s k t h a t e d i t i n g t h B A S I C A s s e m b l e r p r o g r a mc a d i s t u r b t h m a c h i n e c o d e s e c t i o n . I f y o d m a k e a n c h a n g e s t t h d i m e n s i o n s i l i n e s 7 5 8 o 8 ( o a n o t h e r c h a n g e s f o r t h a t m a t t e r ) b s u r e t c a r r y o u t t h ef o l l o w i n g p r o c e d u r e :( i s w i t c h t h c o m p u t e r o f f a n t h e n o t s t a r t w i t h

c l e a n m e m o r y .( i i ) l o a d t h a s s e m b l e r i n t o m e m o r y , b u t d n o r u n ,( i i i ) m a k e t h r e q u i r e d c h a n g e s , b u t d n o r u n .( i v ) i y o u w i s h t r e t a i n c o p y o f t h n e ( c h a n g e d )

a s s e m b l e r , s a v e i n o w , b u d o n ' t o v e r w r i t e t h o l a s s e m b l e r , j u s t i c a s e ,

( v d t e s t r u o t h n e a s s e m b l e rI t h t e s t f a i l s , c h e c k y o u r a m e n d m e n t s c a r e f u l l y . I fy o a r c o n f i d e n t t h a t t h e s e h a v e b e e n e n t e r e dc o r r e c t l y , t r y a g a i n , f r o m ( i a b o v e .

2 A y o a r e a w a r e , t h c o m p u t e r i b l e s s e d w i t h l a r g e m e m o r y ,a n a s c o n s e q u e n c e i i e x t r e m e l y u n l i k e l y t h a t s p a c ep r o b l e m s a r l i k e l y t o c c u r w i t h t h a s s e m b l e r . H o w e v e r ,f o t h r e c o r d :a E a c h o c c u r r e n c e o t h l a b e l s u i t e o v a r i a b l e s P $ ( X ) a n

F ( X ) r e q u i r e s 8 b y t e s .

Page 94: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 94/243

F u r t h e r O p t i o n s o t h A s s e m b l e rO n e o f t h o p t i o n s w h i c h i o f f e r e d o n t h e m a i n M E N U i ' O t h e rf u n c t i o n s 0 ' I t h i s i s e l e c t e d , t h s c r e e n w i l l d i s p l a y a a l t e r n a t i v e M E N U w h i c h p r o v i d e s y o u w i t h a d d i t i o n a l f u n c t i o n s f o i n s e r t i n g o m o v i n g c o d e ( i ) , o a c c e s s i n g t h M a c h i n e L a n g u a g eM o n i t o r ( M ) , o c o n v e r t i n g m a c h i n e c o d e p r o g r a m t B A S I C D A T As t a t e m e n t s ( D ) , o o b t a i n i n g a a s s e m b l y l i s t i n g o t h P r i n t e r ,i n s t e a d o t h s c r e e n ( P ) , o r f i n a l l y , t h o p t i o n t r e t u r n t t h m a i n M E N U ( X ) . T h ' M o n i t o r M o p t i o n w i l l b d e a l t w i t h i t h n e x t c h a p t e r , t h o t h e r f u n c t i o n s a r d e s c r i b e d b e l o w .

I n s e r t i n g o M o v i n g C o d eT h I N S E R T f u n c t i o n i a c c e s s e d b s e l e c t i n g a ' 0 a t h m a i nM E N U , a ' I a t h s e c o n d M E N U , a n r e s p o n d i n g ' I w h e n a s k e d" I n s e r t . . I M o v e . . M " .T h i s f a c i l i t y w i l l b f o u n d t b o m o s t u s w h e n y o u n e e d t i n s e r t n e s e c t i o n o c o d e i n t o a e x i s t i n g p r o g r a m . T h e n e e df o t h i s c a a r i s e t h r o u g h d e s i g n o a c c i d e n t ( p l a n n e d e x p a n s i o n ,o m i s s e d l i n e s ) . T h I N S E R T f a c i l i t y a l l o w s y o t o p e n u g a i n t h e p r o g r a m , i n t o w h i c h y o u c a e n t e r n e w c o d e ( u s i n g t h o p t i o n o t h m a i n M E N U ) .I n t h e e x a m p l e s h o w n b e l o w , s i b y t e g a i o p e n e d u p i a e x i s t i n g p r o g r a m w h i c h e x t e n d s f r o m 8 2 t o 8 4 2 .

i E n t e r s t a r t a d d r e s s o f c o d e t b e n t e r e d , i . e . t h a d d r e s so t h s t a r t o f t h e g a p , w h e n a s k e d :

S t a r t A d d r e s s f o I n s e r t i o n ? 8 3

i i E n t e r t h a d d r e s s o f t h e e n d o f t h c u r r e n t p r o g r a m w h e na s k e d :

E n o C u r r e n t P r o g r a m ? 8 4

i i i ) E n t e r l e n g t h o f t h e c o d e t b i n s e r t e d , i n b y t e s , w h e na s k e d :

L e n g t h o I n s e r t i o n ?

i v W h e n s p a c e h a s b e e n c r e a t e d f o t h n e c o d e , t h e s c r e e nw i l l s a y :

O K S p a c e I n s e r t e d

Page 95: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 95/243

T a c c e s s t h M O V E f u n c t i o n , m u c h t h s a m e p a t h i f o l l o w e d a f o t h I N S E R T f u n c t i o n , b u s e l e c t i n g ' M w h e n a s k e d " I n s e r t . . IM o v e . . M "

I t h e x a m p l e " b e l o w , m a c h i n e c o d e p r o g r a m o r i g i n a l l y w r i t t e ni n t o 8 2 t 8 4 i s t o b e m o v e d t t h s t a r t o f t h e b l o c k o R A M ,S C O O O o n w a r d s .

i E n t e r t h s t a r t a d d r e s s o t h p r o g r a m t b m o v e d w h e na s k e d :

O l S t a r t A d d r e s s o B l o c k ? 8 2 8

i i E n t e r t h e n a d d r e s s o t h p r o g r a m w h e n a s k e d :O l d E n d A d d r e s s o B l o c k ? 8 4

i i i ) E n t e r t h a d d r e s s w h e r e t h p r o g r a m i s t o s t a r t , a f t e rm o v i n g , w h e n a s k e d :N e S t a r t A d d r e s s o B l o c k

. i . e . a f t e r m o v e $ C 0 0 0i v W h e n t h b l o c k h a b e e n m o v e d , t h s c r e e n w i l l d i s p l a y :

O K - B l o c k M o v e d

v P r e s s a n k e t r e t u r n t t h m a i n M E N U .

C o n v e r t i n g P r o g r a m t B A S I C D A T A S t a t e m e n t sc o n v e n i e n t m e t h o d o a t t a c h i n g m a c h i n e c o d e p r o g r a m t B A S I C

p r o g r a m i s t o c o n v e r t t h p r o g r a m t s e r i e s o n u m b e r s i D A T As t a t e m e n t . T h e B A S I C p r o g r a m a l s o h a t i n c l u d e s i m p l e l o o pw h i c h w i l l p o k e t h n u m b e r s i n t h e D A T A s t a t e m e n t b a c k i n t o t h a p p r o p r i a t e m e m o r y l o c a t i o n s . T h i s l a t t e r t e c h n i q u e i d e s c r i b e di p r o g r a m 6 . 3 o f t h n e x t c h a p t e r . T h e f i r s t p r o b l e m , t h a t o . c o n v e r t i n g t h p r o g r a m t D A T A s t a t e m e n t s , i m o s t e a s i l y h a n d l e db l e t t i n g D R W a t s o n ' s a s s e m b l e r d i f o r y o u .B e c a u s e o f t h e l i m i t a t i o n s b r o u g h t a b o u t b y t h w a y t h a t t h a s s e m b l e r u s e s t h s c r e e n i o r d e r t c a r r y o u t t h i s f u n c t i o n ,m a x i m u m o 1 2 b y t e s m a y b c o n v e r t e d t D A T A s t a t e m e n t s a a n o n t i m e . T h D A T A s t a t e m e n t s w h i c h a r c r e a t e d c o n t a i n m a x i m u m o 1 v a l u e s ( r e p r e s e n t i n g 1 b y t e s o f t h p r o g r a m ) , h e n c e m a x i m u mo e i g h t D A T A s t a t e m e n t s a r c r e a t e d e a c h t i m e . T h i s r e s t r i c t i o n

Page 96: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 96/243

I t h e x a m p l e s h o w n b e l o w , m a c h i n e c o d e p r o g r a m c r e a t e d i t h c a s s e t t e b u f f e r b e t w e e n 8 2 a n 9 7 ( 1 5 0 b y t e s l o n g ) i s t o b ec o n v e r t e d i n t o B A S I C D A T A s t a t e m e n t s .

' 0 h a s b e e n s e l e c t e d a t t h e m a i n M E N U a n ' D a t t h e s e c o n d a r yM E N U .

i E n t e r s u i t a b l e l i n e n u m b e r f o t h f i r s t B A S I C D A T As t a t e m e n t t b c r e a t e d w h e n a s k e d :

L i n e N o f o 1 s D A T A s t a t e m e n t ? 2 0 0 0 0T h n u m b e r 2 0 0 0 0 h a b e e n s e l e c t e d t e n s u r e t h a t t h D A T As t a t e m e n t s w i l l n o t g e t m i x e d u w i t h t h a s s e m b l e r . T h D A T A s t a t e m e n t s w i l l b n u m b e r e d 2 0 0 0 0 2 0 0 1 0 2 0 0 2 0 e t c .

i i E n t e r t h s t a r t a d d r e s s o f t h e p r o g r a m w h i c h i t b c o n v e r t e d t D A T A s t a t e m e n t s w h e n a s k e d :

S t a r t A d d r e s s o f D a t a ? 8 2 8

i i i ) E n t e r t h a d d r e s s o t h e n o t h p r o g r a m , u n l e s s i i b i g g e r t h a n 1 2 b y t e s . I t h i s c a s e , t h p r o g r a m i 1 5 b y t e s s w w i l l h a v e t h a v e t w b i t e s a t t h e b y t e s( s o r r y ! ) . S o w h e n a s k e d :

E n d A d d r e s s o f D a t a ? 9 5 i v T h a s s e m b l e r w i l l i m m e d i a t e l y c o n v e r t t h p r o g r a m t D A T A

s t a t e m e n t s w h i c h w i l l a p p e a r b r i e f l y o y o u r s c r e e n . I t h i s c a s e t h e r e w i l l b e i g h t D A T A s t a t e m e n t s w h i c h w i l l b a d d e d t t h a s s e m b l e r p r o g r a m a 2 0 0 0 0 2 0 0 7 0 . T h s c r e e nw i l l t h e n d i s p l a y t h m e s s a g e :

D A T A S t a t e m e n t s n o E n t e r e da n d y o u m a p r e s s a n y k e t r e t u r n t t h m a i n M E N U .

v A s t h e r e a r f u r t h e r 2 b y t e s t c o n v e r t , s e l e c t t h f 0 o p t i o n o n t h e m a i n M E N U , ' D o t h s e c o n d a r y M E N U a n d .r e p e a t t h p r o c e s s . O b v i o u s l y , w m u s t s p e c i f y l i n en u m b e r g r e a t e r t h a n 2 0 0 7 0 , w h e n a s k e d , o t h e r w i s e t h p r o g r a m ' s b y t e s w i l l b o u o f o r d e r ; 2 0 0 8 0 w i l l d n i c e l y .T h e s t a r t a d d r e s s a n t h e n a d d r e s s t h i s t i m e w i l l b 9 5 a n 9 7 7 , w h i c h a r t h b y t e s r e m a i n i n g t b c o n v e r t e d .

S a v i n g t h n e w l y a c q u i r e d D A T A s t a t e m e n t s c a b p r o b l e m . S i m p l e

Page 97: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 97/243

H o w e v e r , i y o h a v e n ' t a c c e s s t s u c h u s e f u l t o o l s , t h e n h e r e i o n e a s y m e t h o d o s a v i n g t h D A T A s t a t e m e n t s . T h i s t e c h n i q u e w o r k sw e l l , p r o v i d e d a l t h D A T A s t a t e m e n t s w i l l f i o t t h s c r e e n :

a E x i t f r o m t h a s s e m b l e r , a n d l i s t t h D A T A s t a t e m e n t s o t h s c r e e n ( e . g . L I S T 2 0 0 0 0 - )

b T y p e N E W t r e m o v e t h a s s e m b l e r f r o m m e m o r y . O K k n o w t h a ta t h i s m o m e n t y o h a v e l o s t , n o o n l y t h a s s e m b l e r , b u t a l s ot h e D A T A s t a t e m e n t s b u w o r r y y n o t .

c M o v e t h c u r s o r t t h f i r s t o f t h D A T A s t a t e m e n t s d i s p l a y e do t h s c r e e n a n p r e s s t h R E T U R N k e y . T h e f i r s t D A T As t a t e m e n t h a b e e n r e - e n t e r e d i n t o m e m o r y . P r e s s t h R E T U R Nk e a g a i n a n t h n e x t D A T A s t a t e m e n t h a s b e e n r e c o v e r e d .R e p e a t f o e a c h o f t h e D A T A s t a t e m e n t s .

d Y o c a n o e i t h e r s a v e t h p r o g r a m i m e m o r y , w h i c h c o n s i s t so t h D A T A s t a t e m e n t s o n l y , o r y o c o u l d s t a r t t t y p e t h B A S I C p r o g r a m a r o u n d t h e m .

P r i n t i n g t h e A s s e m b l y L i s t i n g o P r i n t e rT h ' L o p t i o n o t h m a i n M E N U a l l o w s y o u t d i s p l a y t h a s s e m b l yl i s t i n g o t h s c r e e n . I y o o w p r i n t e r h o w e v e r , y o w i l lw a n t t p r i n t y o u r l i s t i n g s . T h ' P o p t i o n o n t h e s e c o n d a r y M E N Up r o v i d e s t h i s f a c i l i t y . T h e p r i n t e d o u t p u t w i l l b e x a c t l y t h s a m e a t h s c r e e n l i s t i n g a n w i l l b p r i n t e d i d o u b l e w i d t hc h a r a c t e r s .

A t h m a i n M E N U s e l e c t ' 0 ' , t h e n s e l e c t ' P a t t h e s e c o n d a r y M E N U .M a k e s u r e t h a t y o u r p r i n t e r i c o n n e c t e d a n s w i t c h e d o n o c o u r s e . N o w p r o v i d e t h s t a r t a d d r e s s o f t h e p r o g r a m a n t h e e n da d d r e s s w h e n a s k e d t d s o a n t h p r i n t e r w i l l p r o d u c e y o u rl i s t i n g s . W h i l e t h p r i n t e r i l i s t i n g , t h s c r e e n o t h c o m p u t e rw i l l b b l a n k e d . T h i s i t a v o i d t h c h a n c e o f t h e p r i n t e rh a n g i n g u p w h i c h c a o c c a s i o n a l l y h a p p e n i y o a r u s i n gp r i n t e r w i t h t h o l V I 2 0 R O M . D o n ' t p a n i c , w h e n t h l i s t i n g i f i n i s h e d , t h s c r e e n w i l l r e t u r n b e a r i n g t h m e s s a g e :

O K - P r o g r a m L i s t e d

a n y o c a p r e s s a n y k e y t r e t u r n t t h m a i n M E N U .

Page 98: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 98/243

Page 99: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 99/243

C H A P T E RW i t h o u t t h A s s e m b l e r

S f a a l t h m a c h i n e c o d e p r o g r a m s c o n s i d e r e d h a v e b e e n e n t e r e dv i t h a s s e m b e r . H o w e v e r , a d i s c u s s e d i n t h e l a s t c h a p t e r , a l t h a s s e m b l e r d o e s i s t o m a k e i e a s y t P O K E d a t a i n t o m e m o r y .T h a s s e m b l e r i s o c o u r s e , n o t h o n l y w a y o p u t t i n g m a c h i n ec o d e p r o g r a m i n t o m e m o r y , o n e o t h e r w a i s t o P O K E i i d i r e c t l y .P r o g r a m 6 . i e x p r e s s e d i n t h i s d i r e c t P O K E f o r m .

P R O G R A M 6 .

P O K E 8 2 8 , 1 6 0P O K E 8 2 9 , 1P O K E 8 3 0 , 1 6 2P O K E 8 3 1 , 0P O K E 8 3 2 , 1 6 9P O K E 8 3 3 , 9 0P O K E 8 3 4 , 1 5 7P O K E 8 3 5 , 0P O K E 8 3 6 , 4P O K E 8 3 7 , 1 5 2P O K E 8 3 8 , 1 5 7P O K E 8 3 9 , 0P O K E 8 4 0 , 2 1 6P O K E 8 4 1 , 2 3 2P O K E 8 4 2 , 2 0 8P O K E 8 4 3 , 2 4 4P O K E 8 4 4 , 9 6

W c a n n o t e n t e r t h i s i n t o m e m o r y , o f c o u r s e , w h i l e w a r r u n n i n gt h a s s e m b l e r . S f i r s t o f a l l , s e l e c t t h o p t i o n o n t h e m a i nm e n u t e x i t . T h c o m p u t e r w i l l r e p o r t R E A D Y a n d w e c a e n t e r t h d i r e c t P O K E c o m m a n d s . O n e a s y w a t d o t h i s i t c l e a r t h s c r e e nf i r s t a n d t y p e i P O K E 8 2 8 , 1 6 2 f r o m 1 0 2 4 o n w a r d . A f t e r e a c h< r e t u r n > , H O M E w i l l p u t t h e c u r s o r b a c k o t h P i n P O K E . I i t h e n s i m p l e m a t t e r t e d i t t h e l i n e , c h a n g i n g t h n e c e s s a r yd i g i t s . W h e n t h p r o g r a m i i n i m a b r u b t h S Y 8 2 c o m m a n d a n i w i l l p u 2 5 w h i t e d i a m o n d s o n t o t h s c r e e n . C a y o u i m a g i n e t h p r o g r a m t h a t t h e s e P O K E s h a v e c r e a t e d ?F o r t u n a t e l y , i t ' s n o t n e c e s s a r y t i m a g i n e i t a s t h e a s s e m b l e rw i l l d i s a s s e m b l e t h c o d e f r o m m e m o r y f o y o a n d i s p l a y i t F i r s t R U N t h a s s e m b l e r b t y p i n g R U N , t h e n s e l e c t ' L a t h M E N Ua n t y p e i t h a d d r e s s 8 2 8 .

Page 100: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 100/243

T h a s s e m b l e r w i l l t h e n r e v e a l t h s o u r c e p r o g r a m f o 6 . 1 a w h i c hw a s :

P R O G R A M 6 . 1 a ( i A s s e m b l y L a n g u a g e )

L D Y I ML D X I ML D A I M

. S T A XT Y S T A XI N B N R T

9 1 0 2 45 5 2 9 6

2 4

A w e l l a e n t e r i n g p r o g r a m s w i t h o u t t h a s s e m b l e r i i a l s op o s s i b l e t r u n t h e m d i r e c t l y o f r o m B A S I C p r o g r a m . F o ri n s t a n c e , t r u t h p r o g r a m t h a t c u r r e n t l y s t a r t s a m e m o r yl o c a t i o n 8 2 8 i i o n l y n e c e s s a r y t p u a 8 2 i n t o t h p r o g r a mc o u n t e r . I p r o g r a m 6 . i s t i l l i m e m o r y t h i s c a b u s e d f o d e m o n s t r a t i o n o f t h i s i i i n o t s t i l l i m e m o r y t y p e i i n N e x t e x i t f r o m t h a s s e m b l e r a n g e b a c k t B A S I C w i t h t h R E A D Yc u r s o r .

N o t y p e i S Y 8 2 a n t h p r o g r a m w i l l r u n , p u t t i n g ' i n t h ed i a m o n d s .

N o r u t h i s f r o m B A S I C . P u i t h i s f o l l o w i n g p r o g r a m :

P R O G R A M 6 .

2 0 0 0 0 P R I N T " c l e a r " C l e a r s c r e e n2 0 0 1 0 S Y 8 2 8 D i r e c t P t 8 2 2 0 0 2 0 P R I N T " A L L O V E R "

N o t y p e i n " R U N 2 0 0 0 0 " < r e t u r n > a n d t h B A S I C p r o g r a m s h o u l d r u i t h f o l l o w i n g w a y : -

L i n e 2 0 0 0 0 T h e B A S I C p r o g r a m c l e a r s t h s c r e e n .L i n e 2 0 0 1 0 H a n d s c o n t r o l t t h m a c h i n e c o d e p r o g r a m a

8 2 8 . C o n t r o l i h a n d e d b a c k t B A S I C w h e n t h f i n a l R T S i e n c o u n t e r e d .

L i n e 2 0 0 2 0 B A S I C p r i n t s o u t h m e s s a g e " A L L O V E R " .R u n n i n g p r o g r a m d i r e c t l y i r e l a t i v e l y e a s y b u t t h e d i r e c t m o d eo e n t r y o p r o g r a m i o b v i o u s l y t e d i o u s w a o e n t e r i n g l o n gp r o g r a m s f u r t h e r , m o r e e a s i l y e n t e r e d o p t i o n i o f f e r e d b y t h

Page 101: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 101/243

P R O G R A M 6 . F O 0 T 1 R E A DP O K E ( 8 2 8 + X ) , AN E X T X : R E S T O R ED A T A 1 6 0 , 1 , 1 6 2 , 0 , 1 6 9 , 9 0 , 1 5 7 , 0 , 4D A T A 1 5 2 , 1 5 7 , 0 , 2 1 6 , 2 3 2 , 2 0 8 , 2 4 4 , 9 6E N

T h i s i r u n a s f o n o r m a l B A S I C p r o g r a m w i t h R U c o m m a n d .O n c e r u n , t h D A T A w i l l h a v e b e e n l o a d e d i n t o m e m o r y a n d l i n e s t

m u s t b l o o p e d o u t a n d t h s t a t e m e n t s R E M e d o u t .

1 G O T O 1 0 : F 0 R X = T 1 5 R E M D A T A 1 6 0 , 1 , 1 6 26 R E D A T A 1 5 2 , 1 5 7 , 0

O n c e t h i s i d o n e , t h m a i n a s s e m b l e r c a b r u a n d t h e p r o g r a mR U f r o m t h M E N U i t ' s l o c a t e d a t 8 2 8 . O c o u r s e , o n c e t h d a t ai l o a d e d i n t o m e m o r y , t h p r o g r a m c a n a l s o b r u n b y S Y S 8 2 8c o m m a n d .

T h A s s e m b l e r f o t h C o m m o d o r e 6 o f f e r s f u r t h e r m e t h o d o e n t e r i n g m a c h i n e c o d e v i a :

T H M O N I T O R O p t i o n M ( o ' o t h e r f u n c t i o n s 1 )T h i s f e a t u r e o f f e r s r e a d y f a c i l i t y f o e x a m i n i n g a n m o d i f y i n gm e m o r y . T e n t e r M a c h i n e L a n g u a g e M o n i t o r ( M L M ) t y p e " M " a t t h M E N U . T h c o m p u t e r w i l l t h e n d i s p l a y :

c P

. ; E 4 1 6S B

A F

X 2

Y 0

S E

a n t h c u r s o r w i l l b f l a s h i n g m e r r i l y a w a y i n t h e p o s i t i o ni m m e d i a t e l y f o l l o w i n g t h e l a s t d o t ( " . " ) . T h M L o t h D r W a t s o n A s s e m b l e r i f u l l i m p l e m e n t a t i o n o f t h e C o m m o d o r e M a c h i n eL a n g u a g e M o n i t o r a u s e d o t h e i r P E T r a n g e o f m i c r o c o m p u t e r s . I y o h a v e u s e d t h M a c h i n e L a n g u a g e M o n i t o r o P E c o m p u t e r , t h e ny o u s h o u l d b i m m e d i a t e l y a h o m e w i t h t h e D r . W a t s o n M o n i t o r .H o w e v e r , i y o a r n o f a m i l i a r w i t h M L M , t h e n y o a r p r o b a b l yw o n d e r i n g w h a t o e a r t h t h d i s p l a y m e a n s . H a v e p a t i e n c e , a l l w i l lb r e v e a l e d b e l o w .

Page 102: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 102/243

T a k e c a r e w h e n d o i n g t h i s t p u t i n a l t h s p a c e s , o t h e r w i s eM o n i t o r w i l l b c o n f u s e d . T h i s c o m m a n d s h o u l d , a s s u m i n g p r o g r a m6 . i s t i l l l o a d e d , r e s u l t i t h d i s p l a y b e l o w :

. O 3 3 C O 3 4 C. : O 3 3 C A 0 A 2 0 0 A 9 5 A 9 0 . : O 3 4 4 0 4 9 8 9 D 0 0 D 8 E D F . : O 3 4 C 6 0 0 0 0 0 0 0 0 0 0 0 0

F i g . 6 .

T h c o m m a n d r e q u e s t s t h m o n i t o r t l i s t , i h e x a d e c i m a l , t h b l o c k o f m e m o r y d e f i n e d , i . e . i n t h i s c a s e f r o m O 3 3 C t 0 3 4 C( a d d r e s s e s i h e x ) . T h e b l o c k w h i c h i l i s t e d a b o v e i s i n f a c t ,t h p r o g r a m l o a d e d b y p r o g r a m 6 . a n t h l i s t i n g t e l l s t h a t O 3 3 Cc o n t a i n s A O O 3 3 D c o n t a i n s 0 a n s o t O 3 4 C w h i c h i l o a d e dw i t h 6 0 T h v a l u e s t h a t y o w i l l s e d i s p l a y e d i O 3 4 D t 0 3 5 3m a n o b 0 a s h o w n i f i g u r e 6 . s i n c e t h e s e m a c o n t a i n v a l u ew h i c h w e r e n o c h d n g e d w h e n y o u l o a d e d p r o g r a m 6 . 1 . Y o c a n l i s ta n y p a r t o m e m o r y u s i n g t h c o m m a n d , o f c o u r s e .

L o o k a t h b l o c k l i s t e d b y m o n i t o r a g a i n . N o t i c e t h a t e a c h l i n es t a r t s w i t h t h d o ( i n d i c a t i n g t h a t m o n i t o r i a w a i t i n g c o m m a n d )f o l l o w e d b y c o l o n ( " : " ) . T h i s c o l o n i s i f a c t , o n o m o n i t o r ' s c o m m a n d s : t h c o m m a n d t c h a n g e m e m o r y . O n e o f t h e m o s tu s e f u l f a c i l i t i e s o f f e r e d b y t h m o n i t o r i t h a b i l i t y t e d i tm a c h i n e c o d e p r o g r a m s a i t h e y w e r e B A S I C p r o g r a m s . F o i n s t a n c e , w h i l e p r o g r a m 6 . i l o a d e d , g i n t o m o n i t o r a n d l i s tO 3 3 C t O 3 4 CT y p e : . O 3 3 C O 3 4 C < r e t u r n >

A t t h i s s t a g e t h c u r s o r s h o u l d b f l a s h i n g i m m e d i a t e l y b e l o w t h c o l o n o f t h e O 3 4 C l i n e o t h d i s p l a y . N o m o v e t h c u r s o r o v e rt h f i r s t o 0 a O 3 3 D a n r e p l a c e t h i s w i t h * 6 u s i n g t h n o r m a l e d i t i n g t h a t y o u s i B A S I C . A f t e r c h a n g i n g t h i s , e n t e ri i n t o m e m o r y b p r e s s i n g R E T U R N . Y o w i l l b a b l e t s e t h c h a n g e t h a t y o m a d e q u i t e e a s i l y s i n c e m o n i t o r o r i g i n a l l yd i s p l a y e d t h l i n e i o r a n g e a n d y o u r c h a n g e w i l l b e n t e r e d i l i g h t b l u e . N o m o v e t h c u r s o r d o w n a g a i n a n t y p e :

. O 3 3 C O 3 4 C < r e t u r n >

M o n i t o r w i l l s h o w y o u t h b l o c k a g a i n a n y o w i l l b a b l e t c o n f i r m t h a t t h c o n t e n t s o O 3 3 D h a v e i n d e e d b e e n c h a n g e d t 0 6 T h c o m m a n d t m o n i t o r h a b e e n o b e y e d . N o r e p e a t t h p r o c e s st r e p l a c e t h 5 i 0 3 4 1 w i t h a 5 f o l l o w e d b R E T U R N t a c t i o n

Page 103: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 103/243

. O 3 3 C O 3 4 C. : O 3 3 C A 0 A 0 A 9 5 8 9 D 0 0. : O 3 4 4 0 4 9 8 9 0 0 - D 8 B 8 D F . : O 3 4 C 6 0 0 0 0 0 0 0 0 0 0 0

F i g . 6 .

Y o c a n o f c o u r s e m a k e a m a n y c h a n g e s a y o l i k e i o n l i n e .W h e n y o u p r e s s t h R E T U R N k e y , a l l t h e c h a n g e s w i l l b m a d e . Y o uc o u l d i y o u w i s h e d t b u i l d t h c h a n g e l i n e y o u r s e l f , t y p e i n t h ec o l o n f o l l o w e d b y t h a d d r e s s f o l l o w e d b u t e i g h t h e v a l u e s .H o w e v e r , m o s t u s e r s f i n d i m u c h e a s i e r t u s t h M c o m m a n d t d i s p l a y t h e l i n e a n d t h e n u s t h c o m m a n d w h i c h m o n i t o r h a s e u f o t h e m i t h d i s p l a y .

I o r d e r t e x i t f r o m t h m o n i t o r t h c o m m a n d i u s e d f o l l o w e d b r e t u r n . T r t h i s a n t h m a c h i n e w i l l r e t u r n t t h M E N U .

T h A s s e m b l e r p r o g r a m m a t h e n b R U a n o L I S T i n g t h L D Y I Ma n L D A I M 9 i n s t r u c t i o n s w i l l b s e e n t h a v e b e e n c h a n g e d t L D A I M a n L D A I M 8 8 O r u n n i n g , t h w h i t e d i a m o n d s w i l l b r e p l a c e d b y b l u e c l u b s .

N o o n l y c a p r o g r a m s b e d i t e d i m o n i t o r , t h e y c a a l s o b e n t e r e d . T r t h i s w i t h t h f o l l o w i n g e x e r c i s e , u s i n g t h m o n i t o rt e n t e r t h f o l l o w i n g c o d e :

i E n t e r m o n i t o r ,i i L i s t m e m o r y c o n t e n t s

T y p e O 3 3 C 0 3 4 E r e t u r n ,i i i ) R e p l a c e c o n t e n t s w i t h :

P R O G R A M 6 .

. O 3 3 C 0 3 5 8. : O 3 3 C A 5 A 4 9 F 0 9 • : O 3 4 4 9 0 A 9 4 E 9 9 4 F 0 4 8 . : O 3 4 C D O F O A 9 0 A O F O 9 F

: O 3 5 4 D 8 D F A 6 0 8 8 8

N o t e t h a t t h f i n a l " 8 8 " f s i n t h i s p r o g r a m a r r e a l l y i r r e l e v a n t a t h e y o c c u r a f t e r t h 6 0 o r R T S . T h u s a n y v a l u e s c o u l d b p u t i n t ot h e s e l o c a t i o n s .

T h m a c h i n e c o d e p r o g r a m i n o e n t e r e d i n t o m e m o r y a n m a b R U e i t h e r f r o m t h A s s e m b l e r o b S Y 8 2 8 . I t c a a l s o b

Page 104: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 104/243

v e r y s i m p l y a c h i e v e d b c h a n g i n g t h a d d r e s s d i s p l a y e d o t h e m o n i t o r l i s t i n g a n p r e s s i n g r e t u r n . F o r i n s t a n c e , t h p r o g r a m i O 3 3 C o n w a r d s c a b c o p i e d t s t a r t a 9 0 0 1 0 ( 0 3 8 4 1 6 ) o b t h f o l l o w i n g p r o c e d u r e :

i E n t e r M O N I T O Ri i L i s t O 3 3 C t 0 3 5 8 b y

i i i ) . O 3 3 C 0 3 5 8 < r e t u r n >i v R e - p o s i t i o n c u r s o r t t h O 3 3 C d i s p l a y l i n e

v R e p l a c e t h O 3 3 C b 0 3 8 4 a n d p r e s s < r e t u r n >v i R e p e a t f o r t h e 0 3 4 4 , O 3 4 C a n 0 3 5 4 l i n e s , a

b e l o w :

0 3 4 4 g o e s t 0 3 8 C0 3 4 C g o e s t 0 3 9 40 3 5 4 g o e s t O 3 9 C

T h p r o c e d u r e h a s m a d e c o p y o t h O 3 3 C p r o g r a m i 0 3 8 4 o n w a r d s .I c a b r u f r o m t h a s s e m b l e r o b S Y 9 0 0 .

T h p r o g r a m c a n a l s o b e r u n b m o n i t o r c o m m a n d " G " , w h i c h i s h o r t f o r " G O ! " I r u n s m a c h i n e - c o d e p r o g r a m d i r e c t l y f r o mm e m o r y . N a t u r a l l y t h c o m p u t e r h a t b t o l d w h e r e t h p r o g r a ms t a r t s a n s t h t o t a l c o m m a n d r e a d s :

. 0 3 8 4 < r e t u r n >

P r o t e c t i n g M a c h i n e C o d e i M e m o r yT w m o n i t o r c o m m a n d s e n a b l e m a c h i n e - c o d e p r o g r a m s t b S A V E d o n t ot a p e a n d L O A D e d b a c k f r o m t h i s . H o w e v e r , o n s l i g h t p r o b l e ma r i s e s i d o i n g t h i s a a l m a c h i n e - c o d e p r o g r a m s s f a h a v e b e e ns t o r e d i t h c a s s e t t e b u f f e r a n d t h i s i u s e d w h e n L O A D i n g a n S A V E i n g p r o g r a m s .

I o r d e r t p r o t e c t p r o g r a m s f r o m b e i n g o v e r - w r i t t e n , t h e y m u s t b s t o r e d e l s e w h e r e i m e m o r y .

O t h c o m p u t e r , B A S I C p r o g r a m s a r n o r m a l l y s t o r e d b e t w e e n 2 0 4 8( 0 8 0 0 ^ ) a n 4 0 9 5 9 ( 9 F F F ^ g ) . H o w e v e r , s t r i n g s g e n e r a t e d b p r o g r a m a r w r i t t e n f r o m t h e t o p o f m e m o r y d o w n w a r d s . T h e c o m p u t e rk n o w s w h e r e t s t a r t s t o r i n g t h i s i n f o r m a t i o n , a w h e n t h m a c h i n ei t u r n e d o i c h e c k s w h a t t h h i g h e s t a v a i l a b l e m e m o r y l o c a t i o n sa r e a n d s t o r e s t h i s i n f o r m a t i o n i n t h e z e r o - p a g e l o c a t i o n s 5 a n 5 6 I m a c h i n e - c o d e p r o g r a m i s t o r e d a t t h e t o p o m e m o r y ,t h e n i w i l l b w r i t t e n o v e r w h e n a n y B A S I C p r o g r a m i r u s u c ha t h a s s e m b l e r w h e n t h i s g e n e r a t e s s t r i n g s . H o w e v e r , b e f o r e

Page 105: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 105/243

I m m e d i a t e l y a f t e r s w i t c h i n g o t h c o m p u t e r t h a d d r e s s 4 0 9 6 0 ^ 0( A 0 0 0 1 6 ) i s t o b f o u n d i 5 L S a n 5 M S B . I n o r d e r t P r o t e c t 1 0 0 b y t e s , t h i s a d d r e s s w o u l d b l o w e r e d t 4 0 8 6 0 ^ q

9 F 9 C 1 g ) . T h e r e a r n u m b e r o w a y s o a c h i e v i n g t h i s . F o i n s t a n c e , b m e a n s o d i r e c t p r o g r a m m i n g :

P O K E 5 5 , 1 5 6 ( 9 C 1 6 )P O K E 5 6 , 1 5 9 ( 9 F 1 6 )

f o l l o w e d b y C L

T h C L c o m m a n d i r e q u i r e d t p e r s u a d e t h c o m p u t e r t a d j u s t a l t h o t h e r p o i n t e r s t h a t i m a i n t a i n s . T h e y p o i n t t t h e n d o f t h s p a c e i m e m o r y w h i c h i u s e d t s t o r e t h v a r i a b l e s , t h a r r a y sa n b o t t o m o t h s t r i n g s p a c e . T h e s e n e e d t b c l e a r e d f o l l o w i n ga a d j u s t m e n t o t h t o p o f m e m o r y p o i n t e r , o t h e r w i s e t h 6 c o u l dg e i t ' s l i n e c r o s s e d s o m e w h a t c o n s i d e r w h a t w o u l d h a p p e n i f a na t t e m p t w e r e m a d e t a c c e s s t h s t r i n g s w h i c h a r n o i t h p r o t e c t e d p a r t o m e m o r y .

L o w e r i n g t h t o p o f m e m o r y c o u l d a l s o b a c h i e v e d b y i n c l u d i n gl i n e i n t h B A S I C p r o g r a m w h i c h i a s s o c i a t e d w i t h t h m a c h i n e c o d ep r o g r a m . T h l i n e s h o u l d r e a d :

1 P O K E 5 5 , 1 5 6 P O K E 5 6 , 1 5 9 C L

A n o t h e r m e t h o d i s t o m a k e u s e o f t h M o n i t o r . T h l o c a t i o n sa s s o c i a t e d w i t h t h e t o p o f m e m o r y a r 3 7 ^ 6 a n d 3 8 i g . I t h e s a m ew a t h a t w e a l t e r e d t h p r o g r a m i . e . u s i n g t h c o m m a n d t d i s p l a yt h l o c a t i o n s , e d i t i n g t h l i n e a n d t y p i n g r e t u r n , s i m i l a r l y , w c a a l t e r t h h i g h m e m o r y v a l u e s i 3 7 ^ 6 a n 3 8 < | 6 «

H o w e v e r , t h e r e m a b p r o b l e m a s s o c i a t e d w i t h l o w e r i n g t h e t o p o fm e m o r y . M e m o r y m a y h a v e b e e n l o w e r e d a l r e a d y b a n o t h e r p r o g r a m ,p e r h a p s o n e o f y o u r s . A l t h o u g h A 0 0 0 w i l l b f o u n d i 3 \ a n 3 8 ^ 6a s w i t c h o n t i m e , t h e a d d r e s s w h i c h w i l l b f o u n d l a t e r o m a w e l l h a v e b e e n l o w e r e d . I n g e n e r a l , t h s a f e s t w a t l o w e r m e m o r yi s t o c a l c u l a t e t h v a l u e s f r o m t h c u r r e n t v a l u e s s t o r e d i 5 5 \ q( 3 7 ^ ) a n 5 6 ^ o ( 3 8 i g ) . T h f o l l o w i n g B A S I C p r o g r a m m i n g l o w e r s t h t o o m e m o r y b y 2 0 0 b y t e s .

1 0 M 2 5 6 * P E E K ( 5 6 ) + P E E K ( 5 5 ) R E G e O L a d d r e s s1 1 M M 2 0 R E M C a l c u l a t e N E W a d d r e s s1 2 M I N T ( M N / 2 5 6 ) R E M C a l c u l a t e N E W h i - b y t e1 3 M M 2 5 6 * M H R E M C a l c u l a t e N E W l o - b y t e1 4 P O K E 5 6 , M H P O K E 5 5 , M L C L R E M L o w e r m e m o r y

Page 106: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 106/243

n o r e a l l y w a s t e o f t i m e , s i n c e C 0 0 0 t C F F F m i g h t b u s e d a w o r k i n g s p a c e b y c a r t r i d g e s w h i c h a r p l u g g e d i n t o t h c o m p u t e r .A l t h o u g h t h i s i n o v e r y l i k e l y , i i a w e l l t k n o w h o t p r o t e c t y o u r p r o g r a m i y o h a v e t p l a c e i i B A S I C m e m o r y .

T h A s s e m b l e r f o t h C o m m o d o r e 6 d o e s u s e f e h u n d r e d o t h 4 0 9 6 b y t e s a v a i l a b l e i t h C 0 0 0 t C F F F a r e a a t t h e t o p o f t h a r e a , b u t h e r e i s g r e a t d e a l o s p a c e w h i c h y o m a s a f e l y u s e .T h a d d r e s s o t h f i r s t b y t e i t h i s a r e a w h i c h i u s e d b y t h A s s e m b l e r m a b f o u n d i 5 3 2 4 6 ( C F F E - j g ) a n 5 3 2 4 7 ( C F F F ^ g ) i f y o n e e d t k n o w t h i s . A e v e r , t h e f o r m a t i l o b y t e f o l l o w e d b h i g h b y t e

T h O 3 3 C p r o g r a m c a b c o p i e d t C 0 0 0 o n w a r d t e , u s i n g M o n i t o r ' sc o m m a n d r a n l i s t e d b y m e a n s o t h c o m m a n d . T h e l i n e s d i s p l a y e dw i l l s t a r t w i t h t h a d d r e s s e s C 0 0 0 , C 0 0 8 , C 0 1 0 a n C 0 1 8 . O n c es t o r e d a h e i g h t t h L O A D a n S A V E r o u t i n e s o t h M o n i t o r c a b u s e d .

S a v eF o p r o g r a m t b S A V E d , t h m o n i t o r n e e d s t k n o w w h e r e i s t a r t s a n w h e r e i e n d s a n d , a w i t h o t h e r p r o g r a m s f i n e e d sn a m e . T h u s t h f u l l c o m m a n d b e c o m e s

. " p r o g r a m - n a m e " , 0 1 , s t a r t - a d d r e s s , e n d - a d d r e s s + 1 < r e t u r n >i s a v i n g t t a p e , a n

. " p r o g r a m - n a m e " , 0 8 , s t a r t - a d d r e s s , e n d - a d d r e s s + 1 < r e t u r n >i s a v i n g t d i s k .

N o t i c e t h p e c u l i a r i t y t h a t t h e e n d a d d r e s s i o n l a r g e r t h a n t h h i g h e s t a d d r e s s o f t h e p r o g r a m b e i n g s a v e d .

T r a n s l a t i n g t h i s t s a v e t h c h e c k , p r o g r a m t t a p e , i b e c o m e s :

" . . . . " C H E C K " , 0 1 , C 0 0 0 , C 0 1 D < r e t u r n >A f t e r t h R E T U R N , t h e c o m p u t e r w i l l r e s p o n d w i t h " P R E S S R E C O R DP L A Y O N T A P E " ( i y o a r s a v i n g t t a p e ) a n w h e n t h i s h a b e e nd o n e , a " O K " . A y o w i l l e x p e c t , t h e s c r e e n w i l l g b l a n k d u r i n gt h s a v i n g p r o c e s s . F i n a l l y , w h e n t h r e c o r d i n g i c o m p l e t e , t h

c o m p u t e r w i l l r e s p o n d w i t h a " . " a n t h f l a s h i n g c u r s o r o t h M O N I T O R a i a w a i t s f u r t h e r i n s t r u c t i o n s . S h o u l d y o h a v e m a d em i s t a k e i y o u r t y p i n g , m o n i t o r w i l l d i s p l a y r e q u e s t i o n m a r k

Page 107: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 107/243

L o a dP r o v i s i o n i m a d e o t h m o n i t o r f o r e - l o a d i n g m a c h i n e c o d ep r o g r a m f r o m t a p e a n d s t o r i n g t h i s b a c k i n t o t h l o c a t i o n f r o mw h i c h i w a o r i g i n a l l y s a v e d . I d o e s t h i s b s a v i n g t h s t a r ta d d r e s s o t t h t a p e o r d i s k s t h a t i k n o w s w h e r e t l o a d t h p r o g r a m b a c k . A s t h i s a d d r e s s i k n o w n , i d o e s n ' t n e e d t b s p e c i f i e d ( a n d s h o u l d n o b s p e c i f i e d ) i t h L O A D c o m m a n d i . e .

. " p r o g r a m - n a m e " , 0 1 < r e t u r n >

t l o a d f r o m t a p e , a n

. " p r o g r a m - n a m e " , 0 8 < r e t u r n >

f r o m d i s k .

T h u s. " C H E C K " , 0 1 < r e t u r n >

w i l l r e c o v e r t h C H E C K p r o g r a m f r o m t a p e .

I f a c t , t h e c o m m a n d c a b g r e a t l y s i m p l i f i e d i u s i n g c a s s e t t et a p e . T h 0 c a b o m m i t t e d , a n M o n i t o r w i l l a s s u m e y o m e a nt a p e , a n i t h n a m e i o m m i t t e d a l s o , t h e n M o n i t o r w i l l l o a d t h f i r s t p r o g r a m f r o m t a p e t h a t i f i n d s . T h u s , t h s i m p l e s t f o r m o t h l o a d c o m m a n d i s

. L < r e t u r n >

F o l l o w i n g t h r e t u r n , t h c o m p u t e r w i l l r e s p o n d w i t h " P R E S S P L A Y O T A P E " a n a n " O K " w h e n p r e s s e d . . A u s u a l w i t h t h C o m m o d o r e 6 t h s c r e e n w i l l g b l a n k d u r i n g t h l o a d i n g p r o c e s s , c l e a r i n g w h e np r o g r a m i f o u n d e t c . W h e n t h p r o g r a m i l o a d e d t h c o m p u t e r w i l lr e s p o n d w i t h " . a n d t h e f l a s h i n g c u r s o r o t h M o n i t o r a i t a w a i t s f u r t h e r i n s t r u c t i o n s .

R e g i s t e r D i s p l a y D e b u g g i n gY o u m i g h t r e m e m b e r t h a t p r o m i s e d t e x p l a i n t h d i s p l a y p r e s e n t e db M o n i t o r w h e n i t i s f i r s t e n t e r e d . s h a l l n o k e e p m p r o m i s e .T h d i s p l a y l o o k e d r a t h e r l i k e t h f o l l o w i n g :

* P S A X Y R S P

. ; E 1 4 6 3 1 0 0 F

Page 108: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 108/243

( A C ) , X - R e g i s t e r ( X R ) , Y - R e g i s t e r ( Y R ) a n f i n a l l y S t a c k P o i n t e r( S P ) . I p r a c t i c e , t h e s e v a l u e s a r e o f a c a d e m i c i n t e r e s t o n l y f o

C A L L e n t r y , t h e y r e a l l y c o m e i n t o t h e i r o w i t h e n t r y i m a d ea c o n s e q u e n c e o B R E A K .

O n o t h 6 5 1 0 i n s t r u c t i o n s t h a t h a s n o t y e t b e e n d e a l t w i t h i B R w h i c h h a h e v a l u e o 0 0 ^ . I t h 6 5 1 0 e n c o u n t e r s B R i n s t r u c t i o n t h e n t h n o r m a l f l o w o t h p r o g r a m i ' i n t e r r u p t e d 1 ( f u l l d e s c r i p t i o n o t h B R c o m m a n d a n d o f m a c h i n e ' i n t e r r u p t s i g i v e n i c h a p t e r 8 b u t b r i e f t r e a t m e n t i g i v e n h e r e ) . I fM a c h i n e L a n g u a g e M o n i t o r h a n o b e e n i m p l e m e n t e d , t h e n t h r e s u l tw i l l s i m p l y b t h a t t h s c r e e n w i l l b c l e a r e d a n y o u w i l l r e t u r nt t h B A S I C R E A D Y s t a t e . I f M L h a b e e n i m p l e m e n t e d , h o w e v e r ,t h e n a l l s o r t s o f n i c e t h i n g s h a p p e n . T h e v a l u e s s t o r e d i t h P r o g r a m C o u n t e r a n t h R e g i s t e r s h a v e b e e n s a v e d d u r i n g t h B R i n t e r r u p t a n d o n e o t h f u n c t i o n s o t h M o n i t o r i t r e c o v e rt h e s e a n d p r e s e n t t h e m t t h p r o g r a m m e r . H e n c e t h e d i s p l a y .S o m e t i m e s , t h r e a s o n f o t h B R E A K i s i m p l y t h a t t h p r o g r a m h a g o n e b e s e r k a n d i e x e c u t i n g d a t a a i f t h e y w e r e i n s t r u c t i o n st h e r e a r a l w a y s l a r g e n u m b e r o f z e r o s h a n g i n g a r o u n d i c o m p u t e r ' s m e m o r y a n t h e s e w i l l b i n t e r p r e t e d a B R i n s t r u c t i o n s . T h i s i g o o d , s i n c e y o u w i l l ( a h a v e r e g a i n e dc o n t r o l o y o u r r u n a w a y p r o g r a m a n ( b k n o w w h a t p a r t o m e m o r y i w a r u n n i n g a w a y i n

H o w e v e r , p r o g r a m b e h a v i n g l i k e r o g u e e l e p h a n t i n o t t h e o n l yr e a s o n f o B R E A K e n t r y t M o n i t o r . m u c h b e t t e r r e a s o n i t h a ty o y o u r s e l f o r g a n i s e d t h B R K . S u p p o s e t h a t y o u h a v e m a c h i n ec o d e p r o g r a m w h i c h i n o d o i n g w h a t y o e x p e c t , a n y o a r u n a b l et d e t e r m i n e w h a t i g o i n g w r o n g i s p i t e o f b r i n g i n g a l y o u ri n t e l l e c t u a l p o w e r t b e a r o n t h e p r o b l e m . D o n ' t g i v e u p t h e r e i

w a f o r w a r d .

Y o c a i n s e r t B R i n s t r u c t i o n i n t o y o u r p r o g r a m a n d r u n i t W h e n ( i f ) t h e B R i n s t r u c t i o n i e x e c u t e d y o u w i l l a r r i v e i M o n i t o r w i t h t h v a l u e s o a l l t h e r e g i s t e r s s h o w n t y o u . Y o c a n o u s t h c o m m a n d t l o o k a v a r i o u s v a l u e s s t o r e d i t h o s ep a r t s o m e m o r y w h i c h y o h a v e b e e n m e s s i n g a b o u t , a n d t h i s ,h o p e f u l l y , m a g i v e y o t h e c l u e t h a t y o n e e d . I . y o u d o n ' ta r r i v e i M o n i t o r , b t h w a y , t h a t i i t s e l f i v i t a l i n f o r m a t i o n ,s i n c e p r e s u m a b l y y o u p l a c e d t h B R i n s t r u c t i o n i p a r t o f t h p r o g r a m t h a t y o e x p e c t e d t b e x e c u t e d s e i t h e r t h p r o g r a mc a n ' t g e t t h a t f a o i i t a k i n g a n u n p l a n n e d r o u t e .

Y o c a s e e d y o u r p r o g r a m w i t h a m a n y B R i n s t r u c t i o n s a y o w i s h . T h e r e i n p r o b l e m o i d e n t i f i c a t i o n a M o n i t o r t e l l s y o

Page 109: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 109/243

t h a d d r e s s o f t h e i n s t r u c t i o n w h i c h f o l l o w s t h B R K . T h i s i u s e f u l , s i n c e a c o m m a n d t M o n i t o r w i t h o u t a a d d r e s s ( f o l l o w e db R E T U R N ) w i l l m a k e M o n i t o r a u t o m a t i c a l l y p i c k u t h v a l u e o t h P a n r e g i s t e r s a s h o w n a n r e c o m m e n c e e x e c u t i o n .

L e u h a v e l o o k a d i s p l a y , t h i s t i m e f r o m B R E A K e n t r y .♦

P C S R A X Y S . ; O 3 5 C 3 0 0 0 0 2 0 3 F 4

N o t i c e t h s e m i c o l o n ( " ; " ) a t t h s t a r t o t h t h i r d l i n e . I t i s i f a c t , a n o t h e r c o m m a n d t M o n i t o r . T h i s o n e s a y s ' c h a n g e t h r e g i s t e r s 1 t t h e s e v a l u e s . Y o c a n e d i t t h s e m i c o l o n l i n e , ( i nt h s a m e w a y a y o u e d i t e d t h c o l o n l i n e r e s u l t i n g f r o mc o m m a n d ) a n t y p i n g r e t u r n w i l l c a u s e a n y c h a n g e s w h i c h y o u h a v em a d e t b r e m e m b e r e d b y M o n i t o r . I y o u r e - e n t e r y o u r p r o g r a mu s i n g t h c o m m a n d , t h e n t h p r o g r a m w i l l r e s t a r t w i t h t h n e v a l u e s s t o r e d i n i t s r e g i s t e r s . N a t u r a l l y , i y o c h a n g e t h v a l u eo t h P C t h e n y o u w i l l r e - e n t e r t h p r o g r a m a d i f f e r e n t p o i n tf r o m t h B R e x i t .

T h e r e i o n o t h e r c o m m a n d t M o n i t o r w h i c h w h a v e n o y e t d e a l tw i t h . T h c o m m a n d ( f o l l o w e d b y R E T U R N ) w i l l c a u s e M o n i t o r t d i s p l a y t h r e g i s t e r s , j u s t l i k e t h d i s p l a y y o s e w h e n y o e n t e rM o n i t o r , b u w i t h o u t t h * o * l i n e . T h e m a i n u s e o t h i sc o m m a n d i w h e n y o u a r d e b u g g i n g a n w i s h t r e - e n t e r t h p r o g r a m ,b u y o w a n t t c h a n g e r e g i s t e r , s a y . T h c h a n c e s a r e t h a t t h o r i g i n a l d i s p l a y o t h r e g i s t e r s h a l o n g s i n c e s c r o l l e d o f y o u rs c r e e n . T h e c o m m a n d w i l l p u t h e m b a c k t h e r e , a n y o c a c h a n g et h r e g i s t e r b e f o r e h i t t i n g t h G

Y o m a f e e l t h a t p u t t i n g e x t r a B R K c o m m a n d s i n t o y o u r p r o g r a m i b i o n u i s a n c e , e s p e c i a l l y i f t h e p r o g r a m i l o n g . I t h i sc a s e , y o c o u l d u s t h a n c o m m a n d s t c h a n g e t h v a l u e o f a e x i s t i n g i n s t r u c t i o n t 0 0 ^ , t h e r e b y c h a n g i n g i t B R c o m m a n d .I y o d o t h i s , y o m u s t r e m e m b e r b e f o r e r e - e n t e r i n g t h p r o g r a mt o

( a d e c r e m e n t t h P r o g r a m C o u n t e r b ( i t i s p r o b a b l y p o i n t i n gt t h f i r s t b y t e o f t h e a d d r e s s p a r t o f t h e i n s t r u c t i o n t h a ty o B R K e d , a n

( b r e s t o r e t h v a l u e o t h i n s t r u c t i o n t h a t w a r e p l a c e d ( t h et r i c k h e r e i s t o m a k e n o t e o t h h e x v a l u e B E F O R E y o c h a n g e i t z e r o ) .

Page 110: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 110/243

o m e m o r y , o u t o f t h e r e a c h o f B A S I C , i r e m a i n s i m e m o r y a f t e ry o h a v e s t o p p e d u s i n g t h a s s e m b l e r . I w i l l o n l y b d e s t r o y e d b s w i t c h i n g t h c o m p u t e r o f f , o r i f y o o v e r w r i t e i w i t h m a c h i n ec o d e p r o g r a m , o f c o u r s e . T h e r e f o r e , i c a b u s e d w h e n e v e rr e q u i r e d .

T e n t e r M o n i t o r , y o u s i m p l y t y p e :

S Y 1

a n y o w i l l e n t e r M o n i t o r v i B R c o m m a n d . T h e r e a s o n i n g b e h i n dt h i s i t h a t l o c a t i o n 1 o t h c o m p u t e r a l w a y s c o n t a i n s z e r o , a n da w k n o w t h i s i t h o p c o d e f o r t h B R K . S o , a S Y 1 t e l l s t h 6 t e x e c u t e t h B R c o m m a n d s t o r e d a 1 0 b i n g o , w e e n t e rM o n i t o r . W h y , h e a r y o a s k , s h o u l d w a n t t e n t e r M o n i t o r , i a m n o t w o r k i n g w i t h m a c h i n e c o d e p r o g r a m s ? Y o u w i l l b s u r p r i s e dh o o f t e n y o u w i l l f i n d t h i s c o n v e n i e n t . F o e x a m p l e , t a d j u s t t h p o i n t e r s t b o t t o m a n t o p o f m e m o r y , t c h a n g e c o l o u r s o t h s c r e e n ( i t i s e a s i e r t r e m e m b e r t h h e x a d d r e s s e s o t h r e g i s t e r sD 0 2 0 a n D 0 2 1 t h a n t h e i r d e c i m a l e q u i v a l e n t s ) a n s o a n s o n

O n w o r d o w a r n i n g h o w e v e r . I f y o n e e d t p r e s s S T O P / R E S T O R E a a n t i m e , t h e n t h B R K l i n k t M o n i t o r w i l l b b r o k e n ( n p u i n t e n d e d ) , a n S Y 1 w i l l s i m p l y t a k e y o b a c k t t h B A S I C R E A D Ys t a t e . T h e t r i c k i t P E E K a t h e t w o l o c a t i o n s 7 9 a n 7 9 1 ,i m m e d i a t e l y a f t e r l e a v i n g a s s e m b l e r , a n d m a k e n o t e o t h v a l u e st h a t y o f i n d t h e r e . T h e s e l o c a t i o n s c o n t a i n t h l i n k a d d r e s s t M o n i t o r f o t h B R c o m m a n d . Y o u w i l l f i n d t h a t t h e y f o r m t h L O / H I a d d r e s s o s o m e w h e r e i t h b l o c k . I y o u n e e d t h i S T O P / R E S T O R E a a n t i m e , p o k i n g t h e s e v a l u e s b a c k i n t o 7 9 0 a n d 7 9 w i l l r e s t o r e a c c e s s t M o n i t o r .

S u m m a r y o M o n i t o r C o m m a n d s

M < l o - a d d r e s s > < h i - a d d r e s s > D i s p l a y M e m o r y< a d d r e s s > < b y t e 1 > < b y t e 2 > . . . C h a n g e M e m o r y

D i s p l a y R e g i s t e r s< p r o g - c o u n t e r > < s t a t u s - r e g > e t C h a n g e R e g i s t e r s

< " f i l e n a m e " > , < d e v i c e > , < l o - a d d > , < h i - a d d 1 S a v e b l o c k o m e m o r y

< f l f i l e n a m e " > , < d e v i c e > L o a d f r o m t a p e / d i s k< a d d r e s s > G r u

E x i t f r o m M o n i t o r

Page 111: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 111/243

C o l o u r D i s p l a y s o t h 6 O n o t h m a j o r f e a t u r e s o y o u r C - 6 M c o m p u t e r i i t a b i l i t y t p r o d u c e c o l o u r d i s p l a y s . T h i s f a c i l i t y i j u s t a r e a d i l y u s ef r o m m a c h i n e - c o d e a f r o m B A S I C .

C o n t r o l l i n g t h C o l o u rP r o g r a m 6 . s h o w s h o t h s c r e e n / b o r d e r c o m b i n a t i o n c a b d e m o n s t r a t e d u s i n g s h o r t m a c h i n e c o d e p r o g r a m .

P R O G R A M 6 . L D Y I M 1 L D X I M 1

♦ s c r e e n s t $ D O 2 1 . ■ ♦ B O R D E R S T $ D 0 2 0

D E B P B O R D E RD E B P S C R E E NR T

U n f o r t u n a t e l y , w h e n t h i s i r u a i s t a n d s t h e r e i l i t t l e t s e a t h w h o l e p r o g r a m r u n s t h r o u g h i a b o u t 2 6 0 0 c y c l e s , o 1 3 0m i c r o s e c o n d s ! I o r d e r t e n a b l e t h c h a n g e s t b v i s i b l e ,d e l a y m u s t b i n s e r t e d t h o l d e a c h ' " c h a n g e o t h s c r e e n l o ne n o u g h f o t h e y e a n d b r a i n t p e r c e i v e i t

I p r e v i o u s c h a p t e r s w u s e d c o u n t i n g l o o p s t p r o d u c e t h e d e l a y s o m e t i m e s n e s t e d l o o p s . T h e n e x t p r o g r a m d o e s t h i n g s t h e a s y w ab m a k i n g u s o t h t h r e e b y t e j i f f y c l o c k a 1 6 0 - 1 6 2 ( A 0 - A 2 1 g ) T h i s i b i n a r y c o u n t e r w h i c h c o u n t s j i f f i e s ( i / 6 0 t h s o s e c o n d ) , 1 6 ( A 2 1 6 ) i i n c r e m e n t e d b y e v e r y j i f f y , a n d r o l l s o v ei n t o 1 6 ( A 1 1 6 ) e v e r y 2 5 j i f f i e s , 1 6 1 ( A 1 1 6 > r o l l s o v e r i n ti 6 O ( A 2 1 g ) e v e r y 6 5 5 3 6 j i f f i e s . T h i s g i v e s a m p l e s c o p e - f o r d e l a y s P r o g r a m 6 . l o a d s 2 4 i n t o 1 6 a n w a i t s f o i t t o b e c o m e p o s i t i v w h i c h w i l l h a p p e n i a p p r o x i m a t e l y 1 / s e c o n d .P R O G R A M

♦ B O R D E R♦ S C R E E N

♦ L O O P

. L D Y I MS T L D X I MS T L D A I MS T L D B M

1 $ D 0 2 01 $ D 0 2 12 4 1 6 1 6 L O O P

Page 112: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 112/243

I n d i v i d u a l c h a r a c t e r s o t h s c r e e n c a n a l s o b c o n t r o l l e d b y u s eo t h s c r e e n c o l o u r c o d e s ( i f a c t w e h a v e b e e n u s i n g t h i s f r o mo u r e a r l i e s t p r o g r a m s ) . I f m e m o r y l o c a t i o n 5 5 2 9 6 ( 0 8 0 0 ^ ) c o n t a i n s

" 2 " , t h e n t h t o l e f t m e m o r y l o c a t i o n , i . e . 1 0 2 4 ( 0 4 0 0 ^ 5 ) w i l lp r i n t o u o n t h e s c r e e n i r e d . T h e c o m p l e t e c o l o u r s e i g i v e nb e l o w i f i g . 6 . 3 .

C o d e

1 1 1 1 1 1

C o l o u r

B l a c kW h i t eR e C y a nP u r p l eG r e e nB l u eY e l l o wO r a n g eB r o w nL i g h t R e D a r k G r e yM i d G r e yL i g h t G r e e nL i g h t B l u eL i g h t G r e y

P i g . 6 . P r o g r a m 6 . s h o w s h o t h c o l o u r o b l o c k s o s c r e e n c a b d e f i n e db m e a n s o t h s c r e e n c o l o u r c o d e s .P R O G R A M 6 .

L D X I M 2 0 ♦ L O O P L D A I M 8

S T A X $ D 7 F PL D A I M 1 6 S T A X 1 0 2 3D E B N E L O O PR T

T h i s p r o c e s s i t a k e n s t a g e f u r t h e r J . p r o g r a m 6 . w h e r e t h s i x t e e n d i f f e r e n t c o l o u r s a r c y c l e d t h r o u g h , w i t h d e l a y b e t w e e ne a c h .

Page 113: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 113/243

P R O G R A M

♦ L O O P 1♦ L 0 0 P 2

6 . L D A I MS T

L D X I ML D A I MS T A XL D S T A XD E B N

1 0 1 92 0 1 6

1 0 2 31 0 1 9$ D 7 F FL 0 0 P 2

S e u f i r s t c o l o u r a n s a v ea t o p o f t a p e b u f f e r

S e c o u n t e r f o 1 r o w sL o a d r e v e r s e d s p a c e f o s c r e e n

P r i n t b l o c k o n t o s c r e e n

L D A I M 1 8 S T 1 6 D e l a y o 6

♦ D E L A Y L D 1 6 j i f f i e s ( s e c )B M D E L A Y JL D A 1 0 1 9A D C I MC M P I M 1 S T 1 0 1 9B N L 0 0 P 1R T

p M o v e t n e x t c o l o u ra n c h e c k i f a l l d o n e

Page 114: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 114/243

Page 115: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 115/243

C H A P T E RB u i l t - i n S u b r o u t i n e s

A s t h e C o m m o d o r e 6 i t s e l f u s e s t h 6 5 1 0 , i h a s s t o r e d i n s i d e i t o R O ( R e a d O n l y M e m o r y ) , m a c h i n e c o d e r o u t i n e s t h a t c o n t r o l t h 6 5 1 0 . T h e s e e n a b l e t h c o m p u t e r t d e a l w i t h t h B A S I C c o m m a n d st h a t a r e p u t i n t o p r o g r a m s , a l i n p u t a n o u t p u t a n d a l l t h s t a n d a r d r o u t i n e s w h i c h a r n e e d e d t k e e p t h c o m p u t e r a l i v e a n w e l l . T h e R O M w h i c h h a n d l e s a l t h B A S I C s t a t e m e n t s i l o c a t e d i m e m o r y b e t w e e n $ A 0 0 0 a n d $ B F F F . T h R O M w h i c h l o o k s a f t e r a l t h n o n - B A S I C r o u t i n e s i c a l l e d t h K e r n e l b C o m m o d o r e , a n l i e sb e t w e e n $ E 0 0 0 a n d $ F F F F .

I a d d i t i o n t t h i s u s o f m e m o r y , b o t h B A S I C a n t h K e r n e l m a k eu s e o f t h R A m e m o r y i t h b o t t o m f o u r p a g e s b e t w e e n $ 0 0 0 0 a n $ 0 3 F F , t h m o s t f r e q u e n t l y u s e d l o c a t i o n s b e i n g i t h z e r o p a g e$ 0 t $ F F . S o m e o f t h i s u s a g e i f o t h s t o r a g e o t r a n s i e n td a t a s u c h a s f o r i n s t a n c e , t h j i f f y c l o c k ( $ A 0 t $ A 2 ) t h a t t i c k sa w a y a l t h t i m e t h c o m p u t e r i t u r n e d o n S o m e o f t h e R A i u s e d f o r m o r e p e r m a n e n t d a t a , s u c h a t h p o i n t e r s s t o r e d i n 4 3 t 5 ( $ 2 B t $ 3 8 ) w h i c h i n d i c a t e t h a r e a o m e m o r y u s e d b t h B A S I Cp r o g r a m , a n d i t s d a t a a r e a s . S o m e o f t h e s e u s e s t a k e u o n l y o n e ,t w o t h r e e b y t e s , o t h e r s u s e m u c h m o r e , s u c h a t h c a s s e t t e t a p eb u f f e r w h i c h u s e s 1 9 b y t e s w h e n t r a n s f e r r i n g d a t a b e t w e e n m e m o r ya n t h c a s s e t t e t a p e .

T h m o s t d i f f i c u l t a s p e c t i u s i n g t h b u i l t - i n s u b r o u t i n e s i t k n o w w h e r e t h e y o b t a i n t h e i r d a t a f r o m a n w h e r e t h e y d e p o s i t t h d a t a t h a t t h e y h a v e g e n e r a t e d . T h i s i e s p e c i a l l y t r u e o t h r o u t i n e s w h i c h m a k e u p t h B A S I C R O M . F o r t u n a t e l y , C o m m o d o r e h a v eb e e n v e r y h e l p f u l i t h d e s i g n o t h K e r n e l r o u t i n e s . T h s t a r t i n g p o i n t s a n t h s o u r c e s o t h d a t a f o r t h e m a j o r K e r n e lr o u t i n e s a r v e r y w e l l d e f i n e d . A n o t h e r h e l p f u l f e a t u r e i t h a tt h s t a r t i n g p o i n t s o t h K e r n e l r o u t i n e s w i l l b k e p t t h s a m ew h e n n e w / b e t t e r v e r s i o n s o t h R O a r d e v e l o p e d a n t h i s w i l le n a b l e m a c h i n e c o d e p r o g r a m s w h i c h u s o n l y t h e s e r o u t i n e s t r e t a i n t h e i r u s e f u l n e s s f o t h f o r s e e a b l e f u t u r e .

F i r s t l e t ' s h a v e l o o k a t t h e c o n t e n t s o t h a c c u m u l a t o r u s i n gC o m m o d o r e 6 K e r n e l s u b r o u t i n e . W e h a v e a l r e a d y d i s p l a y e d t h a c c u m u l a t o r b y u s i n g S T c o m m a n d t m o v e c o p y o t h a c c u m u l a t o r t s c r e e n l o c a t i o n , e . g . S T 1 0 2 4 . b e t t e r / e a s i e rm e t h o d , h o w e v e r i s t o u s t h K e r n e l s u b r o u t i n e w h i c h i c a l l e dC H R O U T a n i l o c a t e d a 6 5 4 9 0 ( $ F F D 2 ) . T h i s w i l l o u t p u t t h a c c u m u l a t o r t t h e s c r e e n , s t a r t i n g f r o m t h c u r r e n t c u r s o r

Page 116: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 116/243

P R O G R A M 7 .

L D A I M 4 L o a d a c c u m u l a t o r w i t h ' 4 2 '( a a s t e r i s k )

S T 1 5 2 4 S t o r e a c c u m u l a t o r i t h m i d d l e o t h s c r e e n ( 1 5 2 4 ) -L D X I M L o a d w i t h f o c o l o u r w h i t e ,

S T 5 5 7 9 6 S t o r e i i c o l o u r R A J S $ F F D 2 J u m p t c o m p u t e r C H R O U T s u b r o u t i n eR T E N

W h e n r u n , t h i s d i s p l a y s t w a s t e r i s k s . I t h m i d d l e o f t h e s c r e e na 1 5 2 4 w h i t e a s t e r i s k w h i c h w p l a c e d d i r e c t l y . H o w e v e r , w a l s o h a v e a n o t h e r a s t e r i s k , p r p b a b l y i t h t o p l e f t h a n d c o r n e r o t h s c r e e n a n p r o b a b l y c o l o u r e d l i g h t b l u e . . T h i s e x t r a a s t e r i s kw a s . p l a c e d t h e r e b y t h R O s u b r o u t i n e . N o t i c e t h a t t h s u b r o u t i n ed i d n ' t h a v e t b t o l d w h e r e t h a s t e r i s k w a t b p l a c e d , n o d i w s p e c i f y w h i c h c o l o u r . T h e s u b r o u t i n e p l a c e d t h a s t e r i s k i n t h ec u r r e n t c u r s o r p o s i t i o n w i t h t h c u r r e n t . . c o l o u r .

U n f o r t u n a t e l y , t h o u g h , o u t p u t t i n g i n o q u i t e a s t r a i g h t f o r w a r d a t h i s p r o g r a m s u g g e s t s ! T d e m o n s t r a t e t h i s , t r r e p l a c i n g t h L D A I M 4 w i t h a L D A I M 1 Y o c a d t h i s w i t h P O K E 8 2 9 , 1 . N o w ,w h e n y o u r u t h p r o g r a m , a l t h o u g h t h e x p e c t e d w h i t e ' A a p p e a r si t h m i d d l e o t h s c r e e n , n o t h i n g a p p e a r s i n t h e t o t o l e f th a n d c o r n e r o t h s c r e e n . T h e p r o b l e m i t h a t t h e c o m p u t e r u s e s t h A S C I I c h a r a c t e r s e w h e n u s i n g s u b r o u t i n e 6 5 4 9 0 , r a t h e r t h a n t h s c r e e n s e t . T h A S C I I c o d e f o a s t e r i s k h a p p e n s t b t h s a m e a t h s c r e e n c o d e , s o t h e p r o g r a m w o r k e d p r o p e r l y i t h f i r s t r u n .H o w e v e r , t h A S C I I c o d e f o r * A i 6 5 n o a i t i s i s c r e e nc o d e . T h A S C I I c o d e s e i l i s t e d i T a b l e ( A p p e n d i x 2 ) p a g eA 2 - 2 5 .

T h t w m a j o r a d v a n t a g e s o t h i s s u b r o u t i n e a r t h a t , f i r s t i l o c a t e s t h c u r s o r a u t o m a t i c a l l y a n w i l l i n c r e m e n t t h i sa u t o m a t i c a l l y e a c h t i m e t h s u b r o u t i n e i c a l l e d s e c o n d i w i l la u t o m a t i c a l l y s t o r e t h c u r r e n t c o l o u r i t h a p p r o p r i a t e p o s i t i o ni t h c o l o u r R A M . I y o u r a n t h p r o g r a m f r o m t h a s s e m b l e r t h e nt h c u r r e n t c u r s o r p o s i t i o n w o u l d h a v e b e e n 1 0 2 4 , b e c a u s e t h a s s e m b l e r d o e s ' c l e a r s c r e e n 1 a n s e t s t h c u r r e n t c o l o u r t l i g h t b l u e b e f o r e r u n n i n g y o u r p r o g r a m .

I w w i s h t o w c a s e t t h e c u r r e n t c u r s o r p o s i t i o n a n t h c u r r e n t c o l o u r f r o m o u r m a c h i n e c o d e p r o g r a m . S e t t i n g t h c u r r e n tc o l o u r i q u i t e e a s y . T h c u r r e n t c o l o u r i s t o r e d i 6 4 6 ( $ 0 2 8 6 )0 b l a c k , w h i t e , r e a u s u a l . S o l o a d i n g t h a p p r o p r i a t e

Page 117: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 117/243

c u r s o r u s i n g t h a n r e g i s t e r s . I f e n t e r e d w i t h t h c a r r y f l a s e t , t h e n P L O T w i l l r e t u r n t h c u r r e n t p o s i t i o n o t h c u r s o r i t h a n r e g i s t e r s , c o n t a i n i n g t h n u m b e r o t h r o ( 0 t o 2 4 a n d Y c o n t a i n i n g t h c o l u m n ( 0 t o 7 9 ) . I e n t e r e d w i t h c a r r y c l e a r t h e n t h v a l u e s t h a t w h a v e s t o r e d i t h a n Y r e g i s t e r s w i l b u s e d t p o s i t i o n t h c u r s o r .L e u u s t h P L O T s u b r o u t i n e t p l a c e y e l l o w a s t e r i s k a t t hb e g i n n i n g o f t h e t e n t h l i n e o t h s c r e e n .

S e t u p f o r m o v e c u r s o r ( n o t r e a d i t ) .L o a d ( g i v e s t e n t h l i n e ) *L o a d ( g i v e s f i r s t c o l u m n ) .C a l l P L O T t p o s i t i o n c u r s o r ,L o a d f o y e l l o w -M a k e t h i s t h c u r r e n t c o l o u rL o a d a s t e r i s k -O u t p u t i

I f w e h a w i s h e d t p u t t h e a s t e r i s k a t t h e e i g h t e e n t h p o s i t i o n o t h t e n t h l i n e t h e n w w o u l d h a v e l o a d e d w i t h 1 i n t h e l i n b e g i n n i n g a t 8 3 1 . T h u s p r o g r a m 7 . 2 c a n b m o d i f i e d b p r o g r a 7 . 2 a .

P R O G R A M 7 . 2 a

8 3 L D Y I M 1 E N

. . t o y i e l d p r o g r a m 7 * 3 :

P R O G R A M 7 .

P R O G R A M 7 . 2 .C L L D X I ML D Y I MJ S L D A I MS T L D A I MJ S R T

$ F F F O

6 4 4 $ F F D 2

C L L D X I ML D Y I MJ S L D A I MS T L D A I MJ S R T E N

1 $ F F F O

6 4 4 $ F F D 2

W h e n r u n , t h i s p r o g r a m m o v e s t h c u r s o r d o w n 1 a n d a c r o s s 1 8 a n

Page 118: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 118/243

P R O G R A M 7 . 3 a0 8 4 3 L D Y I M* L O O P J S $ F F D 2

D E B N L O O PR T E N

t y i e l d :

P R O G R A M 7 . 3 bC L L D X I ML D Y I M 1 J S $ F F F OL D A I MS T 6 4 L D A I M 4 L D Y I M

* L O O P J S R $ F F D 2D E B N L O O PR T E N

W h e n r u n , p r o g r a m 7 . 3 b w i l l p r i n t f o u r y e l l o w a s t e r i s k s i n l i n e 1 i c o l u m n s 1 8 1 9 2 a n 2 1 N o t i c e t h a t w e d i d n ' t h a v e t r e l o a dt h a c c u m u l a t o r w i t h 4 e a c h t i m e r o u n d t h e l o o p , s t h C H R O U Ts u b r o u t i n e d i d n o t a l t e r t h v a l u e o t h a c c u m u l a t o r . I i a l s oc l e a r t h a t t h r e g i s t e r i n o a l t e r e d , o t h e r w i s e t h c o u n t i n g o t h f o u r a s t e r i s k s w o u l d n o h a v e w o r k e d . I f a c t , C H R O U T d o e s n ' ta l t e r e i t h e r A o Y T h i s i o n o t h g o o d f e a t u r e s o t h i sp a r t i c u l a r r o u t i n e . N o t a l l t h b u i l t - i n s u b r o u t i n e s a r s k i n d ,s i t i s i m p o r t a n t t b e a r i m i n d t h p o s s i b i l i t y o f o n e o m o r eo t h r e g i s t e r s b e i n g a l t e r e d b y a n y s u b r o u t i n e t h a t w c h o o s e t u s f r o m t h k e r n e l o B A S I C R O M s .

M a n y B A S I C p r o g r a m s u s t h G E T c o m m a n d , w h i c h a c c e p t s s i n g l eb y t e i n p u t i n t o t h k e y b o a r d b u f f e r . G E T u s e s o n e o f t h K e r n e ls u b r o u t i n e s t c a r r y o u t h i s o p e r a t i o n c a l l e d G E T I N w h i c h i l o c a t e d a 6 5 5 0 8 ( $ F F E 4 ) . W h e n c a l l e d , G E T I N r e t r i e v e s c h a r a c t e rf r o m t h k e y b o a r d q u e u e a n r e t u r n s i a s a n A S C I I v a l u e i t h a c c u m u l a t o r . I t h q u e u e i e m p t y , G E T I N d o e s n o w a i t , b u tr e t u r n s v a l u e o z e r o . P r o g r a m 7 . s h o w s G E T I N i o p e r a t i o n .

P R O G R A M 7 .

Page 119: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 119/243

W h e n r u n , t h i s p r o g r a m s e t s u p t h l o o p

* L O O P J S $ F F E 4B E L O O P

w h i c h w a i t s f o r a n i n p u t . O n c e t h i n p u t o c c u r s t h p r o g r a m r u n st h r o u g h t h B E c o m m a n d a n e x e c u t e s t h r e m a i n d e r o t h p r o g r a m ,i . e . d i s p l a y i n g t h c h a r a c t e r o b t a i n e d b y G E T I N . N o t i c e t h a t w c h o o s e t u s t h C H R O U T s u b r o u t i n e t p l a c e t h r e s u l t o n t h es c r e e n r a t h e r t h a n p l a c e i t h e r e d i r e c t l y . B o t h G E T I N a n C H R O U Tw o r k w i t h A S C I I c o d e , r a t h e r t h a n t h c o m p u t e r s c r e e n c o d e . D i r e c tp l a c e m e n t o n t h e s c r e e n w o u l d c a u s e a n i n p u t ' Z ' t o a p p e a r a d i a m o n d o t h s c r e e n . I n t h e c a s e o n u m b e r s , t h c o m p u t e r s c r e e na n A S C I I c o d e s c o i n c i d e , s n u m e r i c i n p u t w i l l r e s u l t i n t h es a m e c h a r a c t e r b e i n g d i s p l a y e d o n t h e s c r e e n e i t h e r w a y .

C H R I N a 6 5 4 8 7 ( $ F F C F ) i a a l t e r n a t i v e i n p u t K e r n e l s u b r o u t i n e t G E T I N . W h e n i n p u t t i n g f r o m t h k e y b o a r d , i t a c t i o n i s i m i l a r t t h B A S I C I N P U T s t a t e m e n t i . e . t h f i r s t t i m e t h a t G E T I N i c a l l e d ,t h c u r s o r w i l l b s w i t c h e d o a n w i l l r e m a i n o u n t i l R E T U R N( C H R $ ( 1 3 ) ) i t y p e d . T h e c h a r a c t e r s w h i c h a r t y p e d i a r s t o r e di t h B A S I C b u f f e r w h i c h s t a r t s a 5 1 2 ( $ 0 2 0 0 ) , a n e d i t i n g w h i c hi d o n e d u r i n g t h t y p i n g s u c h a d e l e t e s a n i n s e r t s w i l l b a p p l i e d . H o w e v e r , w e d n o n e e d t o r g a n i s e t h r e t r i e v a l o t h i sc h a r a c t e r f r o m t h i s b u f f e r , a t h c h a r a c t e r s w i l l b r e t u r n e d i s e q u e n c e f o r e a c h c a l l o C H R I N . T h e r e i s n o n e e d t o r g a n i s e t h d i s p l a y o t h c h a r a c t e r s a t h i s a l s o i o r g a n i s e d b C H R I N . T h i sp r o d u c e s t h s h o r t e s t p r o g r a m y e i 7 . 5 !

P R O G R A M 7 .

J S $ F F C FR T

T h G E T I N s u b r o u t i n e c a b u s e d t d e s i g n y o u r o w I N P U T r o u t i n e .F o r i n s t a n c e , y o c o u l d u s G E T I N t e n t e r o n c h a r a c t e r a t i m e ,c h e c k i n g , s a y , f o c e r t a i n n u m b e r o c h a r a c t e r s o r f o r t h i n p u t t i n g o c e r t a i n t e r m i n a t i n g c h a r a c t e r w h i c h n e e d n o n e c e s s a r i l y b R E T U R N . Y o u c o u l d a l s o s e t h r o u t i n e t c h e c ke a c h c h a r a c t e r i t h a c t u a l I N P U T i t s e l f a n g i v e w a r n i n g i i i a i n v a l i d c h a r a c t e r .

P r o g r a m 7 . 6 s h o w s a a r r a n g e m e n t w i t h c h e c k b u i l t i t l o o k f o c o m m a ( A S C I I 4 4 t b i n p u t .

Page 120: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 120/243

P R O G R A M 7 .

L D X I M 4 S t o r e t e r m i n a t o r i S T X 9 0 0 9 0

♦ L O O P J S $ F F E 4 U s G E T I N s u b r o u t i n eB E L O O P W a i t f o i n p u t

J S $ F F D 2 O u t p u t A o n t o s c r e e n

C M P 9 0 0 L o o k f o r c o m m a , i f n o t p r e s e n tB N E L O O P b r a n c h b a c kL D A I M 1 O u t p u t < r e t u r n > t J S $ F F D 2 s c r e e n t b t i d y

R T T h i s p r o g r a m s i m u l a t e s a I N P U T r o u t i n e t h a t i t e r m i n a t e d b c o m m a i n s t e a d o R E T U R N . T o u s e t e r m i n a t o r o t h e r t h a n c o m m a ,c h a n g e t h o p e r a n d o t h f i r s t i n s t r u c t i o n s t h a t 9 0 w i l l b l o a d e d w i t h t h c o r r e c t v a l u e . T r t h i s f o r :

E x e r c i s e 7 .

M o d i f y p r o g r a m 7 « t a c c e p t a i n p u t t h a t i t e r m i n a t e d b ys p a c e , u s P O K E c o m m a n d t m a k e t h c h a n g e .

A n s w e r o p a g e 9 - 1 0 .

A w a m e n t i o n e d e a r l i e r f o n e o f t h m a j o r p r o b l e m s e n c o u n t e r e dw h e n u s i n g b u i l t - i n s u b r o u t i n e s i t h a t t h e y t o o u s e t h 6 5 1 0 .T h a t m e a n s t h a t t h e y p u t h i n g s i n t o A a n Y a n d t h e y a l s om o d i f y t h S t h Z N a n d o t h e r f l a g s . H e n c e w h e n r e t u r n i n gf r o m a n J S i t ' s n o r e a s o n a b l e t a s s u m e t h a t e v e r y t h i n g i j u s ta i w a s l e f t b e f o r e t h e J S R , u n l e s s , o f c o u r s e w k n o w ( a w i t ht h C H R O U T r o u t i n e ) w h a t i l e f t u n d i s t u r b e d .A s a n e x a m p l e o t h i s , l o o k a t t h e f o l l o w i n g p r o g r a m t h a t i d e s i g n e d t i n p u t f o u r - c h a r a c t e r s t r i n g f r o m t h k e y b o a r d .F i r s t i s e t s l o o p c o u n t e r i a 4 t h e n i u s e s t h G E T I Na n C H R O U T s u b r o u t i n e s . O n r e t u r n f r o m t h e s e i d e c r e m e n t s a n c h e c k s f o r f l a g s e a l v e r y s t r a i g h t f o r w a r d !

P R O G R A M 7 . 8

L D X I M S e u l o o p c o u n t e r♦ L O O P J S $ F F E 4 G E T I N s u b r o u t i n e

Page 121: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 121/243

H o w e v e r , w h e n r u n , t h i s p r o g r a m R T S ' s a f t e r o n l y o n c h a r a c t e r h a b e e n i n p u t ! T h i s s u g g e s t s t h a t o n e o f t h e s u b r o u t i n e s i u s i n g t h

r e g i s t e r . T v e r i f y t h i s t h p r o g r a m c a b m a d e t p r i n t o u t h r e g i s t e r a v a r i o u s s t a g e s j u s t t c h e c k . T h i s i d o n e i p r o g r a m 7 . 9 , w h e r e t h c o n t e n t s o t h e X r e g i s t e r a r e x a m i n e di m m e d i a t e l y a f t e r r e t u r n i n g f r o m t h s u b r o u t i n e .P R O G R A M 7 . 9

L D X I M♦ L O O P J S $ F F E 4

B E L O O PS T 1 0 2 4L D Y I MS T Y 5 5 2 9 6J S $ F F D 2S T X 1 0 2 6L D Y I MS T 5 5 2 9 8D E B N L O O PR T

W h e n r u n , t h i s p r o g r a m p r i n t s t h e o n e c h a r a c t e r i n p u t a b e f o r e b u a l s o d i s p l a y s t w A ' a 1 0 2 4 a n d 1 0 2 6 . S i n c e t h f i r s t i o u t p u t i m m e d i a t e l y a f t e r t h e x i t f r o m t h G E T I N r o u t i n e ( J S R$ P P E 4 ) t h e n i t i s c l e a r t h a t G E T I N d o e s m o d i f y t h r e g i s t e r . A w h a v e a l r e a d y d i s c o v e r e d , C H R O U T i i n n o c e n t i t h i s c a s e . S i n c et h r e g i s t e r w a c h a n g e d t b G E T I N t h e n t h D E X / B N E a t h e n o t h p r o g r a m c a u s e d t h p r o g r a m t l e a v e t h e l o o p o n t h ef i r s t p a s s .

T o v e r c o m e t h i s p r o b l e m , t h r e g i s t e r v a l u e m u s t b s t o r e ds o m e w h e r e p r i o r t e n t e r i n g t h s u b r o u t i n e a n t h e n r e t r i e v e d p r i o rt b e i n g d e c r e m e n t e d . P r o g r a m 7 . 1 0 s h o w s t h i s p r o c e s s , w h e r e i t e m p o r a r i l y s t o r e d i 9 0 d u r i n g t h s u b r o u t i n e .

P R O G R A M 7 © L O O P C O N S T

♦ S T O R E♦ G E T

1 9 0 L D X I MS T J S B E J S L D D E

L O O P C O N S T$ F P E 4G E $ F F D 2L O O P C O N S T

A s s e m b l e d V e r s i o n

L D X I MS T X 9 0 0J S R 6 5 5 0 8B E 2 5 J S R 6 5 4 9 0L D X 9 0 0D E

Page 122: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 122/243

A t e c h n i q u e , t h e u s e o f m e m o r y s t o r a g e i t h i s w a d o e s w o r k ,b u i d o e s c a l l f o r s o m e c a r e i s t o r i n g t h d a t a s a f e l y a n r e t r i e v i n g i w h e n n e e d e d . F o r t u n a t e l y t h 6 5 1 0 h a s d e v i c e f o d o i n g t h i s o p e r a t i o n a u t o m a t i c a l l y . I t i s

T H S T A C K

T h s t a c k ( s ) i s b l o c k o m e m o r y ; o t h C o m m o d o r e 6 l o c a t e df r o m 5 1 ( $ 0 1 F F ) d o w n t 2 5 ( $ 0 1 0 0 ) a n d c a p a b l e o h o l d i n g 2 5 b y t e s .I t i s u s e d f o r t h r a p i d t r a n s f e r o f d a t a a n i f i l l e d d o w n w a r d sf r o m 5 1 i t n e x t v a c a n t l o c a t i o n b e i n g r e c o r d e d b y S T A C KP O I N T E R ( o S a i t i s k n o w n ; t h i s w a s s h o w n a t h l a s t v a l u e i t h l i n e w h i c h w a d i s p l a y e d b M o n i t o r i r e s p o n s e t t h e Rc o m m a n d ) . W h e n a n y t h i n g i t b r e t r i e v e d f r o m t h S T A C K o n l yt h l a s t i t e m p u i i a c c e s s i b l e . T h e u s u a l a n a l o g y i w i t hs t a c k o f p l a t e s , o n l y t h e t o p o n b e i n g a c c e s s i b l e a t h i s w a s t h l a s t o n p u t h e r e . H o w e v e r , t h 6 5 1 0 s t a c k i f i l l e d D O W N W A R D S ,i . e . f r o m 5 1 t o w a r d s 2 5 6 , s p l a t e s a r p u a t h b o t t o m a n r e t r i e v e d f r o m t h e r e , a n t i p o d e a n f a s h i o n ! T h i s m o d e o f i l l i n g a n e m p t y i n g t h s t a c k i k n o w n a l a s t - i n , f i r s t - o u t , i . e . t h s t a c ki s l a s t - i n , f i r s t - o u t ( L I F O ) s t o r e .O n f u n c t i o n o t h s t a c k i s t o r e c o r d a d d r e s s e s d u r i n g s u b r o u t i n ej u m p s , ; w h i c h i d o e s a u t o m a t i c a l l y . W h e n t h 6 5 1 0 s e e s a i n s t r u c t i o n s u c h a J S 5 0 0 0 0 i m u s t f i r s t o a l r e c o r d w h e r e t h n e x t i n s t r u c t i o n i s t h a t i c a f i n d i t n e w l o c a t i o n a f t e r t h s u b r o u t i n e h a b e e n e x e c u t e d a n t h e n p l a c e t h " 5 0 0 0 0 " i n t o t h ep r o g r a m c o u n t e r ( P C ) . T h p r o c e s s i e x a m i n e d b e l o w w i t h t h p r o g r a m s e g m e n t 7 . 1 1 ( f r o m 7 . 1 0 ) *P R O G R A M 7 . 1 1

S T E P 1 S T E P 2 S T E P 3

8 2 ( $ O 3 3 C )8 3 ( $ O 3 3 F )8 3 4 ( $ 0 3 4 2 >

S T X 9 0 0J S $ F F E 4B E 2 5

( $ 0 3 8 4 )( 6 5 5 0 8 )

I N S T R U C T I O N S T 9 0

S T E P 1 i C a l c u l a t e a d d r e s s o f n e x t i n s t r u c t i o n , i . e . 8 3 o

S T E P 2

i i P u t n e x t a d d r e s s i n t o p r o g r a m c o u n t e r .i i i ) E x e c u t e i n s t r u c t i o n S T 9 0 0 .

i v R e t r i e v e a d d r e s s f o n e x t i n s t r u c t i o n f r o m P C i . e . $ O 3 3 F . :

v F e t c h n e x t i n s t r u c t i o n , i . e . a $ O 3 3 F .T h i s i J S $ F F E 4 .

Page 123: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 123/243

$ 0 3 4 2$ 0 3 -$ 4 2 -

$ 0 3 ( M S B$ 4 2 ( L S BS T A C K

5 0 5 1 5 1

v i i )v i i i )

i x x

x i x i i )

x i i i )

R e c o r d t h n e x t v a c a n t l o c a t i o n i t h s t a c p o i n t e r , i . e . 5 0 9 ( S P = 5 O 9 ) .L o a d $ F F E 4 i n t o p r o g r a m c o u n t e r .J u m p t s u b r o u t i n e a $ F F E 4 .E x e c u t e s u b r o u t i n e u n t i l R T i e n c o u n t e r e d .L o o k a s t a c k p o i n t e r t f i n d l a s t d a t a . S P = 5 O 9 t h e r e f o r e D a t a s t o r e d a 5 1 0 a n d 5 1 1 .E x t r a c t d a t a f r o m 5 1 a n 5 1 ( i . e . $ 0 3 4 2 ) a n l o a d t h i s i n t o P C r e s e t S P . t o 5 1 1 .J u m p t $ 0 3 4 2 .

S T E P 3 x i v ) F e t c h n e x t i n s t r u c t i o n a n c a r r y o w i t h p r o g r a m .I t h i s e x a m p l e , t h f i r s t s u b r o u t i n e c o u l d h a v e m e t n e s t es u b r o u t i n e s , a n d e a c h t i m e J S w a s e x e c u t e d t h r e t u r n a d d r e s w o u l d h a v e b e e n p i l e d o t o p . T h e n , a s t h e p r o g r a m r e t u r n e s u c c e s s i v e l y t h r o u g h t h e s e s u b r o u t i n e s , t h r e t u r n a d d r e s s e s w o u l h a v e b e e n s t r i p p e d o f t s t e e r t h 6 5 1 0 b a c k t t h o r i g i n a l p o i n o d e p a r t u r e ► F i g . 7 . i l l u s t r a t e s t h i s f o s u b r o u t i n e s n e s t e dd e e p s t a r t i n g f r o m p r o g r a m w i t h J S $ 0 3 8 4 i n s t r u c t i o n i $ O 3 3 D

M A I N

S T A R T

$ O 3 3 C$ O 3 3 D

$ 0 $ 3 S T A C K

R O G .

5 1 5 1

S U

$ 0 3 8 4$ 0 3 8 5$ 0 3 8 6$ 0 3 8 7

$ 0 $ 8 $ 0 $ 3 S T A C K

J J 5 0 5 0 5 1 5 1

S U

$ 0 3 A 2$ O 3 A 3$ 0 3 A 4$ O 3 A 5$ O 3 A 6

$ 0 $ A $ 0 $ 8 $ 0 $ 3 S T A C K

= { 5 0 5 0 5 0 5 0 5 1 5 1

S U

$ 0 3 E 8$ O 3 E 9$ 0 3 E A$ 0 3 E B

$ 0 $ A $ 0 $ 8 $ 0 $ 3 S T A C K

S P = 5 O 9 S P = 5 O 7

$ 0 V > 1 — " *

5 1

S P = 5 O 5

$ 0 $ 8 $ 0

S P = 5 O 4

5 0 5 0 5 0 5 0 5 1 5 1

5 0 5 0 5 1

Page 124: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 124/243

I t h p r e v i o u s p r o g r a m , i n s t r u c t i o n s a $ O 3 E B , $ O 3 A 6 a n $ 0 3 8 8w o u l d a l l b R T S . T h u s w h e n t h 6 5 1 0 f i n d s a n R T S a $ 0 3 E B , i t a k e s t h e t o p a d d r e s s o f t h e s t a c k , w h i c h i $ 0 3 A 4 . R e a d i n gd o w n w a r d s a S U 3 t h s t a c k g i v e s

i a d d r e s s f o r e t u r n t s u b r o u t i n e 2 i i a d d r e s s f o r e t u r n t s u b r o u t i n e 1 i i i ) a d d r e s s f o r e t u r n t t h m a i n p r o g r a m .

F o r t u n a t e l y , t h o p e r a t i o n o f t h e s t a c k i n r e c o r d i n g a d d r e s s e s w h e ne x e c u t i n g s u b r o u t i n e s i a u t o m a t i c a n s t h p r o g r a m m e r c a a l l o wt h 6 5 1 0 t o d o t h j o b . H o w e v e r , a w a s s e e n w h e n u s i n g b u i l t - i ns u b r o u t i n e s , t h s t a c k d o e s n o a u t o m a t i c a l l y s t o r e r e g i s t e rc o n t e n t s b u m u s t b p r o g r a m m e d t d s o O n l y t w i n s t r u c t i o n se x i s t f o r s t o r i n g r e g i s t e r s , n e i t h e r o f t h e s e o p e r a t i n g o n t h ea n Y r e g i s t e r s . T h e s e m u s t b h a n d l e d v i t h a c c u m u l a t o r w h i c hi s t o r e d u s i n g : -

P H Z U S H c o n t e n t s o A c c u m u l a t o r o n t o s t a c k .T h c o n t e n t s m a y t h e n b r e t r i e v e d b y m e a n s o f : -

P L P u L l * ° o s t a c k i n t o A c c u m u l a t o r .I b o t h c a s e s , t h e s t a c k p o i n t e r i u p d a t e d a p p r o p r i a t e l y s t h a ti c o n t i n u e s t p o i n t t t h n e x t e m p t y l o c a t i o n i n t h e s t a c k .

U s i n g t h e s e i n s t r u c t i o n s , p r o g r a m 7 . 8 c a n b r e w r i t t e n t t r a n s f e rt h e X r e g i s t e r i n t o t h s t a c k a n r e t r i e v e t h i s w h e n n e e d e d . T h es t a c k p o i n t e r w i l l t a k e c a r e o t h o r d e r o t h e d a t a , p r o v i d i n gt h a t t h L I F O s t r u c t u r e o t h s t a c k i b o r n e i m i n d a n d a t a i e n t e r e d a n r e t r i e v e d i n t h e r i g h t o r d e r .

W r i t i n g t h e s e i n y i e l d s :

P R O G R A M 7 . 1 2

L D X I MT X P H J S B E J S P L T A D E B N

$ F F E 42 5 $ F F D 2

2 4

S e u l o o p c o n s t a n t .T r a n s f e r X i n t o s t a c k .

R e c o v e r X f r o m s t a c k ,d e c r e m e n t a n c h e c k f o e n o l o o p .

Page 125: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 125/243

A w e l l a a f f e c t i n g t h a c c u m u l a t o r , s u b r o u t i n e i m o s t l i k e l yt r e s e t o n o m o r e f l a g s i t h S R O n r e t u r n i n g t t h m a i np r o g r a m , t h r e s e t f l a g s a r t h e n c e r t a i n t u p s e t t h c o u r s e o t h i s . T o v e r c o m e t h i s p r o b l e m , t h e 6 5 1 0 h a b u i l t - i n p r o v i s i o nf o s a v i n g t h S ( i . e . t h c o n d i t i o n o f a l l t h f l a g s o n t h s t a c k ) . T h i s i b r o u g h t a b o u t b t h i n s t r u c t i o n : -

P H P u s H P r o c e s s o r s t a t u s r e g i s t e r o s t a c k .

R e t r i e v a l o f t h e d a t a i a c h i e v e d b y : -

P L P u X l s t a c k t P r o c e s s o r s t a t u s r e g i s t e r .I p r o g r a m 7 . 1 2 t h e S w a n o s a v e d o n t h e s t a c k a s p r i o r t t e s t i n g t h f l a g w i t h a B N E , t h D E i n s t r u c t i o n r e s e t t h i s .H o w e v e r , u n d e r o t h e r c i r c u m s t a n c e s i m a h a v e b e e n n e c e s s a r y t p r e s e r v e t h s t a t e o f t h e S R s t h i s s h o u l d b w r i t t e n i n t o 7 - 1 2 a a e x e r c i s e .

E x e r c i s e 7 .

R e w r i t e p r o g r a m 7 . 1 2 s a t s a v e t h c o n d i t i o n o f S i t h s t a c kp r i o r t t h s u b r o u t i n e s a n r e t r i e v e t h i s a f t e r t h e s e .

p o s s i b l e a n s w e r o p a g e 9 * 1 1

W h e n u s i n g t h s t a c k , t h m a i n p r e c a u t i o n t t a k e i t c h e c k t h o r d e r o e n t r y a n d r e t r i e v a l s e v e r a l t i m e s a l w a y s L I F O . F o i n s t a n c e , p o s s i b l e r o u t i n e f o s a v i n g t h a c c u m u l a t o r , X a n d Yr e g i s t e r s a n d t h e S i g i v e n i F i g . 7 . 3 .

S a v e A S a v e S RS a v e YS a v e

R e t r i e v eR e t r i e v e Y R e t r i e v e S R e t r i e v e

F i g . 7 .

Page 126: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 126/243

Page 127: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 127/243

C H A P T E RI n t e r r u p t i n g t h e 6 5 1 0 —

S i g n e d a n F l o a t i n g - P o i n t V a r i a b l e sW h e n d o i n g j o t h a t h a t o b e d o n e , n o o n e l i k e s i n t e r r u p t i o n su n t i l t h J o b ' s f i n i s h e d . T h 6 5 1 0 i j u s t l i k e t h a t t o o ! D u r i n g

p i e c e o p r o g r a m i h a i t A c c u m u l a t o r a n a n d Y r e g i s t e r su n d e r c o n t r o l a n d a l l t h f l a g s s e t a p p r o p r i a t e l y - T h u s , w h e n a i n t e r r u p t . , c o m e s a l t h e s e r e g i s t e r s h a v e t b s t o r e d , u s u a l l y i t h e s t a c k , a n d a f t e r t h i n t e r r u p t t h e y m u s t a l b e . r e t r i e v e d . A i n t e r r u p t i s i f a c t , o n l y s u b r o u t i n e t h a t c o m e s w h e n I T i sr e a d y a n n o t t h e 6 5 1 0 ! T h i s m e a n s t h a t t h i n t e r r u p t i g e n e r a t e df r o m o u t s i d e t h 6 5 1 0 ' s c o s y s y s t e m , e i t h e r f r o m a e x t e r n a l d e v i c eo r t h e k e y b o a r d .

T h h a n d l i n g o a i n t e r r u p t h a t b p r e p a r e d f o i n t h e p r o g r a ma n a c e r t a i n t i m e s t h p r o g r a m m a y w e l l n o b a b l e t a l l o w a i n t e r r u p t i o n . I f , , f o r i n s t a n c e , a n o t h e r d e v i c e i s e n d i n g s t r e a mo d a t a i n t o m e m o r y , t h e n H A N D - S H A K I N G p r o c e d u r e i c a r r i e d o u b e t w e e n t h t w m a c h i n e s . Q u i t e s i m p l y , t h i s i a e x c h a n g e o m e s s a g e s a l o n g t h e l i n e : " a r e a d y t s e n d d a t a , a r y o u r e a d y t r e c e i v e i t ? " " Y e s . " " H e r e ' s t h d a t a . . e n o d a t a . " " T h a n k s ! " .I f s u c h a e x c h a n g e i i n t e r r u p t e d , t h e n t h d a t a i l i k e l y t b e c o m e g a r b l e d , a n h e n c e w o r t h l e s s . D u r i n g s u c h p e r i o d s w h e n n i n t e r r u p t i a l l o w a b l e , t h p r o g r a m c a b l o c k m o s t i n t e r r u p t s n o a l t a l l o w p a r t i c u l a r p r o c e s s t b c o m p l e t e d . T h ei n s t r u c t i o n t h a t a l l o w s t h i s b l o c k i n g i s : -

S E S E i n t e r r u p t D i s a b l e F l a g ( p r e v e n t i n t e r r u p t s ) .I r o n i c a l l y , t h f i r s t a c t i o n t h a t u s u a l l y n e e d s t b t a k e n i a i n t e r r u p t i s t o s e t t h e ( i n t e r r u p t d i s a b l e ) f l a g b y u s e o S E I .W e n e e d t p r e v e n t a n m o r e i n t e r r u p t i o n s , a l e a s t f o w h i l e , s t h a t w c a c h e c k u t s e e i f t h e i n t e r r u p t w h i c h h a o c c u r r e d i o u i n t e r r u p t ( t h e r e m a b o t h e r p o t e n t i a l i n t e r r u p t s l u r k i n ga r o u n d ) . W h e n w h a v e m a d e s u r e t h a t t h i n t e r r u p t w h i c h h a s t a k e np l a c e i t h e o n e w a r i n t e r e s t e d i n t h e n t h i n t e r r u p t d i s a b l ef l a g ( o s i m p l y f l a g ) m a b r e s e t t ' 0 o c l e a r e d b y t h i n s t r u c t i o n : -

C L C L e a r L ^ t e r r u p t D i s a b l e F l a g ( a l l o w i n t e r r u p t s ) .I y o t h i n k a b o u t t h a b o v e , y o w i l l r e a l i s e t h a t w m a y a l l o wi n t e r r u p t s t b i n t e r r u p t e d . T h a t i p e r f e c t l y t r u e . I i r a t h e r l i k e s o m e o n e s t a r t i n g t p e e l t h e p o t a t o e s w h e n t h k e t t l ei n t e r r u p t s b c o m i n g t t h e b o i l , s o t h e p o t a t o c l e a n i n g t a s k i

Page 128: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 128/243

m a k i n g t h e p o t o t e a , a w h i c h t i m e w c a g b a c k t p e e l i n g t h p o t a t o e s . T h t r i c k i s t o r e m e m b e r h o f a y o u h a v e g o w i t h e a c ht a s k w h e n i t i s r e s t a r t e d . U n l i k e h u m a n b e i n g s , t h 6 5 1 0 i v e r yg o o d a r e m e m b e r i n g w h e r e i h a g o t : t o a n d h a s l i t t l e d i f f i c u l t y .

N o t a l l i n t e r r u p t s c a b b l o c k e d b s e t t i n g t h ( i n t e r r u p td i s a b l e ) f l a g , a s s o m e a r e c r u c i a l a n d m u s t g e t t h r o u g h a a l c o s t s . M a n y s u c h c i r c u m s t a n c e s m a y a r i s e d u r i n g t h c o n t r o l o p l a n t o r w h e n p o w e r f a i l u r e c a l l s f o i m m e d i a t e a c t i o n . T e n a b l e t h 6 5 1 0 t d i s t i n g u i s h b e t w e e n t h e t w o c o n d i t i o n s , i h a t w d i f f e r e n t i n p u t p i n s , o n e f o r e a c h t y p e o f i n t e r r u p t . O n o t h e s e i t h N M o N o M a s k a b l e I n p u t p i n w h i c h c a n n o t b b l o c k e d ,a n t h o t h e r t h I R o I n t e r r u p t R e Q u e s t p i - w h i c h i m a s k e d o f b t h f l a g .

W h e n t h 6 5 1 0 r e c e i v e s a i n t e r r u p t s i g n a l i a l w a y s c o m p l e t e s i t c u r r e n t i n s t r u c t i o n b e f o r e d o i n g a n y t h i n g e l s e . I t h c a s e o f a nI B i n t e r r u p t i w o u l d t h e n c h e c k t h f l a g a n d i n o t c l e a r ,c o n t i n u e u n t i l t h p r o g r a m c l e a r e d t h i s . N e x t , b e f o r e g o i n g i n t ot h i n t e r r u p t p r o c e d u r e , t h c o n t e n t s o P a r s a v e d o t h s t a c k( t e l l i n g t h 6 5 1 0 w h e r e t r e t u r n t w h e n i h a f i n i s h e dp r o c e s s i n g t h i n t e r r u p t ) , a n d t h e n t h e S R ( s t a t u s w o r d ) i s a v e d .T h i s s a v i n g o f d a t a i r e a l l y o n l y h a l f - m e a s u r e a s a l m o s tc e r t a i n l y , A a n w o u l d b c h a n g e d d u r i n g t h i n t e r r u p tp r o c e d u r e .

N e x t t h f l a g i s e t 1 t p r e v e n t f u r t h e r i n t e r r u p t s a n t h e n t h a p p r o p r i a t e a d d r e s s f o t h a p p r o p r i a t e i n t e r r u p t r o u t i n ei l o a d e d i n t o t h P C T h e s e a d d r e s s e s a r e f o u n d a 6 5 5 3 0 a n 6 5 5 3 1 ( $ F P F A a n d $ P P P B ) f o r a n N M I i n t e r r u p t a n d 6 5 5 3 4 a n d 6 5 5 3 5( $ P P F E a n d $ F P P F ) f o r a I R i n t e r r u p t . T h e s e i n t e r r u p t r o u t i n e sa r t e r m i n a t e d w i t h a n i n s t r u c t i o n : -

I R T I R e T u r n f r o m I n t e r r u p t .~ O m e e t i n g t h i s , t h e 6 5 1 0 d o e s t h r e e t h i n g s :( a r e s t o r e s t h e S R s t h a t t h s t a t u s f l a g s a r e t h s a m e a w h e n

t h i n t e r r u t o c c u r r e d ,( b r e s e t s t h f l a g a n a u t o m a t i c C L I , a n ( c l o o k s i n t h e s t a c k f o t h r e t u r n a d d r e s s a n s t o r e s i i t h

S l i k e a n a u t o m a t i c R T S .

U n f o r t u n a t e l y , t h 6 5 1 0 o n l y d o e s h a l f t h e j o b a t a n i n t e r r u p t a n d ,a d i s c u s s e d i c h a p t e r ( p a g e 7 - 1 0 ) , A a n s h o u l d b s a v e do t h e s t a c k . I t h c a s e o f b o t h X a n d Y t h i s m u s t b t r a n s f e r r e di n t o b e f o r e b e i n g p u s h e d o n t o t h s t a c k ( b P H A ) , a n w h e np u l l e d o f f t h s t a c k ( b a P L A ) i w i l l n e e d t o b e r e s t o r e d t o t h e

Page 129: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 129/243

T h e 6 5 1 0 p o s s e s s e s o n o t h e r i n t e r r u p t i n s t r u c t i o n : -B R K B R e a K ~ ~ " ~ ' "

W h e n t h B R i n s t r u c t i o n i e n c o u n t e r e d , t h 6 5 1 0 f i r s t r e s e t s t h P b i n d e x i n g t h i s b y o n p l a c e ( s t h a t t h e P C p o i n t s t t h b y t e f o l l o w i n g t h B R K i n s t r u c t i o n ) , a n s t o r e s t h i s a d d r e s s o t h s t a c k t h e n i s e t s t h B r e a k F l a g ( f l a g ) w h i c h i b i o t h e S Ra n s t o r e s i o t h s t a c k a l s o . F o l l o w i n g t h i s , t h e 6 5 1 0 t h e nd o e s n o r m a l I R i n t e r r u p t u s i n g t h I R V E C T O R a $ F F F E ( L S B ) a n $ F F F F ( M S B ) . T h e I R Q r o u t i n e w i l l c h e c k w h e t h e r t h i s i n t e r r u p t w a c a u s e d b t r u e I R Q o r B R i n s t r u c t i o n . N o r m a l l y , o t h C 6 4 i t h I R r o u t i n e d i s c o v e r s t h a t i w a B R e n t r y , t h e n t h r o u t i n e w i l l j u m p i n t o B A S I C , t h s c r e e n i c l e a r e d a n R E A D Y w i l lb d i s p l a y e d . A y o w i l l h a v e d i s c o v e r e d i c h a p t e r 6 h o w e v e r ,n o t h i n g s m u n d a n e a n u s e l e s s w i l l o c c u r w h e n y o u a r t h o w n e r o

D r W a t s o n a s s e m b l e r . B R i n s t r u c t i o n o n t h e C o m m o d o r e 6 w i l l c a u s e a e n t r y t M o n i t o r , g r e a t s t u f f f o d e b u g g i n g !T o t e s t t h i s , r u t h f o l l o w i n g : -

P R O G R A M 8 .

L D A I M 9 S T A 1 0 2 4L D A I MS T 5 5 2 9 6B R

W h e n r u n , t h i s w i l l p r i n t w h i t e d i a m o n d i 1 0 2 4 a n d t h e n g i n t ot h i n t e r r u p t r o u t i n e , w h i c h o t h W a t s o n i s e d C o m m o d o r e 6 e n t e r s t h M o n i t o r .

B u s i n g t h a s s e m b l e r t d i s a s s e m b l e t h e c o d e a $ F F F E a n d $ F F F F ,y o u w i l l b a b l e t f i n d t h i s a d d r e s s a n f r o m t h e r e t f o l l o wt h r o u g h t h e r e s t o f t h i s r o u t i n e . B y f o l l o w i n g t h i s c o d e t h r o u g hy o w i l l d i s c o v e r h o w t h a s s e m b l e r t r a p s t h B R c o m m a n d a n e n t e r s M o n i t o r i n s t e a d o t h n o r m a l b o r i n g o l d r e s e t , c l e a r s c r e e na n R E A D Y r o u t i n e . T h v i t a l e l e m e n t i s t h e J M P I A 7 9 0 i n s t r u c t i o nw h i c h y o u w i l l f i n d i 6 5 3 6 8 ( $ F F 5 8 ) . B e f o r e W a t s o n i s a t i o n , 7 9 a n 7 9 n o r m a l l y c o n t a i n t h a d d r e s s o t h r o u t i n e s w h i c h t a k e u b a c k t t h e ^ B A S I C R E A D Y s t a t e . N o t h e y c o n t a i n t h a d d r e s s o f t h eM o n i t o r B r e a k e n t r y p o i n t , t h i s a d d r e s s h a v i n g b e e n p l a n t e d t h e r eb y t h e a s s e m b l e r .

W h e n t h 6 5 1 0 c h i p i u s e d a s a n e l e m e n t i n s y s t e m , i i t h e nt h a t B R c o m e s i n t o i t o w a s t h e i n t e r r u p t v e c t o r s , a t h e y a r

Page 130: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 130/243

d o i n g s o m e t h i n g d i f f e r e n t , l i k e r u n n i n g M o n i t o r . T h i s u s e o f R A v e c t o r s i f e a t u r e o t h c o m p u t e r w h i c h e n a b l e s u s t o t r a p i n t om a n y o t h r o u t i n e s u s e d b y B A S I C o t h K e r n e l o p e r a t i n g s y s t e m .

S i g n e d N u m b e r sI a l t h m a t h e m a t i c a l e x e r c i s e s d o n e s f a r , t h n u m b e r s u s e dh a v e b e e n t r e a t e d a s i m p l e p o s i t i v e n u m b e r s . T h u s , a n a r i t h m e t i c a l p r o c e s s e s h a v e d e a l t w i t h t h e s e n u m b e r s a s t r i n g s o e i g h t b i t s . H o w e v e r , i n e g a t i v e i n t e g e r s a r t b u s e d i a r i t h m e t i c , o n o t h b i t s m u s t b u s e d t i n d i c a t e t h a t g i v e nb y t e r e p r e s e n t s n e g a t i v e n u m b e r . B i 7 t h l e f t - m o s t b i t , i u s e d t d t h i s , b e i n g s e t z e r o i f t h e n u m b e r i p o s i t i v e a n

o n i i i n e g a t i v e . B u s i n g o n e b i t i t h i s w a y , t h m a g n i t u d e o t h n u m b e r s t o r e d i t h r e m a i n i n g s e v e n b i t s i r e s t r i c t e d t + 1 2 7 t - 1 2 8 . C o n v e n t i o n a l l y z e r o i u s e d o b i t i n d i c a t e t h p r e s e n c e o f p o s i t i v e n u m b e r a n f o m i n u sn u m b e r . O n e o f t h p r o b l e m s t h a t a r i s e s f r o m t h i s u s a g e i t h a t ,i n t h e o r y , t w o f o r m s c a n e x i s t f o r t h e n u m b e r z e r o , i . e . + z e r o a n - z e r o :

+ 0 0 0 0 0 0 0 0

- 1 0 0 0 0 0 0 0

I o r d e r t o v e r c o m e t h p r o b l e m , t h n e g a t i v e n u m b e r i n o r m a l l yr e p r e s e n t e d i w h a t a p p e a r s t b w i e r d f o r m T W O ' S C O M P L E M E N T !W i e r d i m a b e b u i w o r k s ! ! I o r d e r t w o r k o u t w o ' sc o m p l e m e n t r e p r e s e n t a t i o n , t a k e t h n u m b e r 3 8 - j q w h i c h i b i n a r y i 0 0 1 0 0 1 1 0 . T . c o n v e r t i t i t t w o ' s c o m p l e m e n t n e g a t i v e f o r m ,f i r s t o f a l l s w i t c h , e a c h b i t o f t h p o s i t i v e n u m b e r f r o m t o v i c e v e r s a , i . e . t o i t C O M P L E M E N T :

0 0 1 0 0 1 1 0 1 0 1 1 0 0 1

N e x t i a d d e d t t h i s s w i t c h e d f o r m o r O N E ' S C O M P L E M E N T , i . e .

1 1 0 1 1 0 0 1

1 1 0 1 1 0 1 0

T h i s y i e l d s t h n e g a t i v e t w o ' s c o m p l e m e n t r e p r e s e n t a t i o n , i . e .

- 3 8 1 0 1 1 0 1 1 0 1 0 2T u n d e r s t a n d t h s i g n i f i c a n c e o t h i s r e p r e s e n t a t i o n , t h r e e s u m su s i n g i a r i l l u s t r a t e d b e l o w .

Page 131: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 131/243

i i 4 3 i o 3 8 1 04 3 i o 0 0 1 0 1 0 1 1 2

- 3 8 1 0 1 1 0 1 1 0 1 0 2i . e . - 3 1 1 0 1 1 0 1 0

+ 4 0 0 1 0 1 0 1 10 0 0 0 0 1 0 1 5 _ 1 0

i i i ) 2 4 ! 0 3 8 1 02 4 1 0 0 0 0 1 1 0 0 0 2

i . e . 2 - 0 0 0 1 1 0 0 0- 3 + 1 1 0 1 1 0 1 0

1 1 1 1 0 0 1 0

A t h i s a n s w e r h a i b i 7 i t i s n e g a t i v e t w o ' s c o m p l e m e n tr e p r e s e n t a t i o n . T c o n v e r t t h i s , f i r s t f i n d t h o n e ' s c o m p l e m e n t :

1 1 1 1 0 0 1 0 0 0 0 0 1 1 0 1

N e x t a d d o n

i . e . 0 0 0 0 1 1 0 1

0 0 0 0 1 1 1 0 - 1 4 mO v e r f l o w sI s i g n e d n u m b e r a r i t h m e t i c t h s e v e n " m a g n i t u d e " b i t s ( i . e . t 6 c a o n l y s t o r e n u m b e r u t + 1 2 7 , s a n y - a t t e m p t t s t o r en u m b e r g r e a t e r t h a n t h i s w i l l r e s u l t i ' c a r r y ' i n t o b i 7 o a i i k n o w n i t h i s c a s e , a O V E R F L O W . C o n s i d e r t h s u 1 0 0 1 Q + 3 0 .

i . e . 1 0 0 1 0 0 1 1 0 0 1 0 0 20 0 0 1 1 1 1 O

1 0 0 0 0 0 1 0 2

T h u s 1 0 0 0 0 0 1 0 i n e g a t i v e n u m b e r , a s i g n i f i e d b t h i b i 7 a n i t h e r e f o r e i t w o ' s c o m p l e m e n t f o r m . T c o n v e r t t h i s ,f i r s t f i n d t h o n e ' s c o m p l e m e n t

0 1 1 0 0 1 0 0 2 — * 1 0 0 1 1 0 1 1 2a n t h e n a d d

Page 132: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 132/243

T h 6 5 1 0 h a n d l e s t h i s s i t u a t i o n b m o n i t o r i n g t h a c c u m u l a t o r a n d ,w h e n a o v e r f l o w o c c u r s , s e t t i n g t h o v e r f l o w o V - f l a g . T h i s f l a gc a n b e t e s t e d b y t h i n s t r u c t i o n s : -

B V B r a n c h o o V e r f l o w C l e a r .

B V B r a n c h o o V e r f l o w S e t .

B V t e s t s t h o v e r f l o w f l a g a n i i t i s c l e a r , o n o s e ( V = 0 ) ,t h e n b r a n c h i e x e c u t e d .

B V S t e s t s t h o v e r f l o w f l a g a n i i t i s s e ( V = 1 ) , t h e n b r a n c hi e x e c u t e d .

W h e n c a r r y i n g o u m u l t i p l e p r e c i s i o n a r i t h m e t i c p r o c e s s e s w i t hs i g n e d i n t e g e r s , b i m u s t b t r e a t e d a a i n t e r n a l c a r r y a n w h e n a o v e r f l o w o c c u r s t h i s m u s t b t r a n s f e r r e d t t h m o s ts i g n i f i c a n t b y t e .

P r o g r a m 8 . 2 i l l u s t r a t e s t h u s o f B V C t t e s t f o a o v e r f l o w a t h a c c u m u l a t o r i i n d e x e d w i t h o n e s .

P R O G R A M 8 . C L L D A I MA D C I MB V S T L D X I MS T R T E N

2 5 1 0 2 45 5 2 9 6

W h e n r u n , t h c o n t e n t o t h a c c u m u l a t o r i i n c r e a s e d p r o g r e s s i v e l yu n t i l t h e . r i g h t - m o s t s e v e n b i t s a r f i l l e d w i t h o n e s . A t h n e x ti n c r e m e n t , t h s e v e n o n e s r e s e t t z e r o e s a n d a c a r r y i g e n e r a t e dt h a t p o p s i n t o b i t s e v e n . T h i s s e t s t h c a r r y b i a n s t o p s t h b r a n c h i n g , a l l o w i n g t h p r o g r a m t r u t h r o u g h t t h R T S . T h S T 7 9 0 0 t h e n p r i n t s t h v a l u e a o v e r f l o w a a w h i t e r e v e r s e ( i . e .t 2 8 1 0 ) .A w i t h o t h e r f l a g s , p r o v i s i o n e x i s t s f o t h c o n t r o l o t h o v e r f l o w f l a g a n i c a n b e c l e a r e d b y t h i n s t r u c t i o n : -

C L C L e a r t h o V e r f l o w f l a g .

Page 133: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 133/243

N u m e r i c a l S c r e e n O u t p u tI a l t h n u m e r i c a l e x a m p l e s t d a t e , s c r e e n o u t p u t h a b e e n i c o m p u t e r d i s p l a y c o d e . W h i l e i t i s p o s s i b l e t i n t e r p r e t t h i s u s i n g

t a b l e , i i o b v i o u s l y n e c e s s a r y i p r o g r a m t d i s p l a y n u m b e r sa n u m b e r s t t h b a s e 1 0 T h m a j o r c o m p l i c a t i o n i t h i sp r o c e d u r e l i e s i n t h e f a c t t h a t c o m p u t e r d i s p l a y c o d e i e f f e c t i v e l y

b a s e 2 5 r e p r e s e n t a t i o n a n d c a n t h u s d i s p l a y O ^ q t 2 5 5 1 0 u s i n go n l y o n c h a r a c t e r , w h e r e b a s e 1 d i s p l a y s w o u l d r e q u i r e u t t h r e e c h a r a c t e r s t r e p r e s e n t t h s a m e v a l u e .

P r o g r a m 8 . 3 t a c k l e s t h i s c o n v e r s i o n t a s k a n f i r s t s e t s o u t f i n d i f t h e n u m b e r i g r e a t e r t h a n 2 0 0 i . e . f i r s t d i g i t i ' 2 ' -o r i f i i l e s s t h a n 2 0 0 a n d g r e a t e r t h a n 1 0 i . e . f i r s t d i g i t1 ' I t h e n c h e c k s i n s i m i l a r w a y f o t h t e n s a n u n i t s a n

u s e s t h s t a c k t s t o r e t h r e m a i n d e r ( i . e . s o f a u n p r o c e s s e dp o r t i o n ) o t h n u m b e r w h i l e a d d i n g t h c o n v e r s i o n c o n s t a n t ( 4 8 ) t ot h a c c u m u l a t o r t c h a n g e t h b i n a r y v a l u e t t h d i s p l a y v a l u ee q u i v a l e n t . I n t h e e x a m p l e g i v e n , t h n u m b e r t b d i s p l a y e d 1 5

i l o a d e d i n t o t h a c c u m u l a t o r a t h s t a r t o f t h p r o g r a m .

Page 134: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 134/243

P R O G R A M 8 . 3L D X I ML D A I MC M P I MB C S B C I M

P H L D A I MS T S T P L J M

♦ O N E H U N D C L C M P I MB C S B C I M

P H L D A I MS T S T P L

♦ T E N S C L L D Y I MC M P I M

1 5 2 0 O N E H U N D2 0

5 1 0 2 45 5 2 9 6

T E N S

1 0 T E N S1 0

4 1 0 2 45 5 2 9 6

B C Z E R O T E N S♦ L O O P I N

S B C I M

C M P I MB C

♦ Z E R O T E N S P H T Y A D C I M

S T S T P L A D C I M

S T S T R T

1

L O O P

4

1 0 2 55 5 2 9 7

4

1 0 2 65 5 2 9 8

L o a d c o l o u r w h i t e i n t o XP u i n u m b e r t b 0 / C o m p a r e w i t h 2 0 B r a n c h i n u m b e r l e s s t h a n 2 0 0R e m o v e l e f t - m o s t d i g i t( c a r r y a l r e a d y s e t )S t o r e r e m a i n d e r o s t a c kL o a d A w i t h % 2 f o x 1 0 0P r i n t o s c r e e nS t o r e w h i t e i c o l o u r R A MR e t r i e v e f r o m s t a c k . - .J u m p t t e n s r o u t i n eC l e a r c a r r y .C o m p a r e w i t h 1 0 0 .B r a n c h i f l e s s t h a n 1 0 R e m o v e l e f t - m o s t d i g i t( c a r r y a l r e a d y s e t ) ,S t o r e r e m a i n d e r o s t a c k .L o a d A w i t h f o 1 0 0 .P r i n t A o s c r e e nS t o r e w h i t e i c o l o u r R A MR e t r i e v e A f r o m s t a c kC l e a r c a r r yS e t Y t Z e r oC o m p a r e w i t hB r a n c h i f A l e s s t h a nI n c r e m e n tS u b t r a c t 1 f r o m( c a r r y a l r e a d y s e t )C o m p a r e w i t hB r a n c h i g r e a t e r t h a nS t o r e A o s t a c kT r a n s f e r t o A d c o n v e r s i o n c o n s t a n t t ( c a r r y a l r e a d y c l e a r )P r i n t A o s c r e e nS t o r e w h i t e i c o l o u r R A MR e t r i e v e f r o m s t a c kA d c o n v e r s i o n c o n s t a n t t ( c a r r y s t i l l c l e a r )I r i n t o s c r e e nS t i o r e w h i t e i c o l o u r R A

Page 135: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 135/243

W h e n a s s e m b l e d t h p r o g r a m l o o k s a s i n p r o g r a m 8 . 3 a .P R O G R A M 8 . 3 a

L o a d w h i t e i n t o X

C h e c k i n u m b e r l e s s t h a n 2 0 0 ,i s b r a n c h

S u b t r a c t 2 0 0 f r o m n u m b e r a n p r i n t o u d i g i t ' 2 f o r 2 0 0

C h e c k i n u m b e r l e s s t h a n 1 0 0 ,i s b r a n c h *

S u b t r a c t 1 0 f r o m n u m b e r a n p r i n t o u d i g i t ' 1 f o 1 0

C h e c k i r e m a i n d e r o n u m b e r l e s st h a n o e q u a l t 9 i s o b r a n c h

C o u n t n u m b e r o 1 0 ' s l e f t i r e m a i n d e r a n p r i n t o u t 1 0 ' sd i g i t ;

P r i n t o u u n i t ' s d i g i t

L D X I M

L D A I MC M P I MB C

S B C I MP H L D A I MS T S T P L J M C L C M P I MB C

S B C I MP H L D A I MS T S T P L

C L L D Y I MC M P I MB C

I N S B C I MC M P I MB C P H T Y A D C I MS T S T P L A D C I MS T S T

1 5 2 0 1 2 0

5 1 0 2 45 5 2 9 6

8 6

1 0 1

1 0

4 1 0 2 45 5 2 9 6

1

2 4

4 1 0 2 55 5 2 9 7

4 1 0 2 65 5 2 9 8

Page 136: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 136/243

F l o a t i n g - P o i n t N u m b e r sW i t h t h i n t e g e r s u s e d s o f a r , t h s c o p e o f t h e a r i t h m e t i c a lp r o c e s s e s c a r r i e d o u t h a s b e e n s o m e w h a t r e s t r i c t e d . W h e n w o r k i n gi B A S I C , h o w e v e r , t h b i n a r y f l o a t i n g - p o i n t c o n s t a n t s h a v e 1 d i g i t p r e c i s i o n a n d a r e d i s p l a y e d w i t h d i g i t s . T h e i r e x p o n e n t sh a v e r a n g e o f - 3 t + 3 7 . E a c h v a l u e i s t o r e d i s i c o n s e c u t i v e b y t e s o m e m o r y a n d , t e a s e t h m a n i p u l a t i o n o t h e s e ,t w ' a c c u m u l a t o r s ' a r e p r o v i d e d i m e m o r y l o c a t i o n s 6 - j t 6 6 - j g( 9 7 ^ 0 t 1 0 2 i q ) a n d 6 9 - | 6 t ^ E 1 6 ( 1 ° 5 i Q * 1 1 o 1 o ) # T h e s e a r k n o w na s t h e F l o a t i n g - P o i n t A c c u m u l a t o r ( F A C ; a n A l t e r n a t i v e F l o a t i n g —P o i n t A c c u m u l a t o r ( A F A C ) .T s t o r e n u m b e r w h i c h c o u l d c o n t a i n u t 1 d i g i t s w h e nd i s p l a y e d i b a s e 1 f o r m , t h e f l o a t i n g p o i n t a c c u m u l a t o r s u s o n l ys i x b y t e s . H o i t h i s t r i c k m a n a g e d ?

W h e n r u n n i n g B A S I C p r o g r a m s , y o w i l l h a v e s e e n t h a t v e r y l a r g e o v e r y s m a l l n u m b e r s a r e x p r e s s e d i e x p o n e n t i a l f o r m o s c i e n t i f i cn o t a t i o n . T h u s , 4 0 7 9 . 0 1 3 c a b e x p r e s s e d a 0 . 4 0 7 9 0 1 3 E + 4 o 3 . 4 0 7 9 0 1 3 x 1 0 4 , w h i l e 0 . 0 0 0 0 4 1 7 c a b e x p r e s s e d a 0 . 4 1 7 E - 4 o ) . 4 1 7 1 0 ~ 4 . T h i s r e p r e s e n t a t i o n c o n t a i n s t w p a r t s : t h f i r s t> a r t ( t h e 0 . 4 0 7 9 0 1 3 i n t h e f i r s t e x a m p l e ) i c a l l e d t h M A N T I S S A ,i n t h s e c o n d p a r t ( t h e + o t h 1 0 ^ ) i c a l l e d t h e x p o n e n t .? h e s e t w p a r t s a r s t o r e d i b i n a r y i n t h e f l o a t i n g p o i n ti c c u m u l a t o r s i t h f o l l o w i n g m a n n e r :

( a B I N A R Y M A N T I S S A i s t o r e d i t h m i d d l e f o u r b y t e s o F A a n A F A C . T h s i g n o t h m a n t i s s a i s t o r e d i t h s i x t h b y t ew h e r e ' 1 o n b i t s i g n i f i e s n e g a t i v e m a n t i s s a a n ' 0 s i g n i f i e s p o s i t i v e m a n t i s s a .

( b B I N A R Y E X P O N E N T , i s t o r e d i t h f i r s t b y t e o f F A C a n A F A C .A t h i s h a t s t o r e b o t h p o s i t i v e a n n e g a t i v e e x p o n e n t s ,t h e x p o n e n t n e e d s t b c o n v e r t e d t p o s i t i v e f o r m a n t h i s i d o n e b a d d i n g 1 2 8 . T h u s , a e x p o n e n t o 1 w o u l d b s t o r e d a 1 2 8 + 2 0 , i . e . 4 8 w h i l e a e x p o n e n t o - 2 w o u l dy i e l d 1 2 8 - 2 0 , i . e . 1 0 8 .

F A A F A C

9 , 0

9 1 0

9 1 0

1 0 1 0

1 0 1 0

1 0 1 1

E x p o n e n t - S i g nB i t -

W h e n l o a d i n g f l o a t i n g - p o i n t n u m b e r , B A S I C n o r m a l i s e s i t b i n a r yr e p r e s e n t a t i o n a n t h u s i t s l e f t - m o s t ( m o s t s i g n i f i c a n t ) d i g i t i a l w a y s 1 T a k e , f o r i n s t a n c e , t h n u m b e r + 1 4 0 0 1 0 ( 0 5 7 8 1 g ) .

Page 137: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 137/243

0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 0 2 .

O * - B i n a r y p o i n t o R A D I X

P u t t i n g t h e x p o n e n t i g i v e s r e p r e s e n t a t i o n :0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 0 2 . * 2

a n w h e n N O R M A L I S E D , i . e . t h b i n a r y p o i n t m o v e d j u s t p a s t t h l e f t - m o s t s i g n i f i c a n t d i g i t :

. 1 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 * 2 1 1i . e . t h b i n a r y p o i n t h a b e e n m o v e d 1 p l a c e s t t h l e f t a n d s t h n u m b e r n e e d s t b m u l t i p l i e d b 2 1 t r e t u r n t i t o r i g i n a lv a l u e .F i n a l l y t h i s i p a d d e d u w i t h O ' i n i t s l e a s t - s i g n i f i c a n t b y t e st f o r m f o u r b y t e m a n t i s s a .

i . e . B y t e B y t e B y t e B y t e

1 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

T h e x p o n e n t o 1 1 1 0 i n o w c o n v e r t e d t t h c o r r e c t f o r m a t b a d d i n g 1 2 8 1 0 t i t s r a w f o r m , i . e . i b e c o m e s 1 3 9 - m o 1 0 0 0 1 0 1 1 2a n d t h i s b y t e i a d d e d t t h f i r s t l o c a t i o n i t h a c c u m u l a t o r ,i . e . t h r e p r e s e n t a t i o n b e c o m e s :

B y t e B y t e B y t e B y t e B y t e1 0 0 0 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0T h s i g n o t h m a n t i s s a i e x p r e s s e d i b y t e b t h c o n d i t i o n o i t m o s t - s i g n i f i c a n t b i t , t h i s b e i n g s e t ' 1 f o n e g a t i v en u m b e r a n ' 0 f o p o s i t i v e o n e . T h u s , t r e p r e s e n t + 1 4 0 0 1 0 ,b i o f b y t e o F A C o r A F A C i s e t a t ' 0 * . T h e o t h e r b i t s o t h i s b y t e m a b s e t a t s o m e o t h e r n u m b e r . T h u s t h e f i n a lf l o a t i n g - p o i n t a c c u m u l a t o r r e p r e s e n t a t i o n o + 1 4 0 0 1 0 i s

B y t e B y t e B y t e B y t e B y t e B y t e1 0 0 0 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ? ? ? ? ? ? ?

T u s t h i s a c c u m u l a t o r d i r e c t l y f r o m m a c h i n e - c o d e i c l e a r l y n o n et e a s y , a t h d a t a h a t b l o a d e d i n t o t h m e m o r y l o c a t i o n s i t h c o r r e c t f o r m a t . H o w e v e r , s o m e b u i l t - i n s u b r o u t i n e s a r

Page 138: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 138/243

T a c c o m p l i s h t h i s , t h e l e a s t - s i g n i f i c a n t b y t e i l o a d e d i n t o t h r e g i s t e r a n t h M S i n t o t h a c c u m u l a t o r . T h e n J S $ B 3 9 1 w i l lc o n v e r t t h i s i n t e g e r i n t o f l o a t i n g - p o i n t a n d l o a d i i n t o F A C .T h i s s u b r o u t i n e i d e m o n s t r a t e d b e l o w i p r o g r a m 8 . 4 , w h e r e t h L S a n M S o a i n t e g e r a r c o n v e r t e d i n t o f l o a t i n g - p o i n t n u m b e r .

P R O G R A M 8 .

L D Y L S BL D M S J S $ B 3 9 1R T

T h e s e t w f l o a t i n g - p o i n t a c c u m u l a t o r s F A a n A F A C a r e u s e d f o t h m a n i p u l a t i o n o f l o a t i n g - p o i n t d a t a a n m e a n s i p r o v i d e d f o t r a n s f e r r i n g f l o a t i n g - p o i n t d a t a i n t o t h e m .

T a k i n g A F A C , f o r i n s t a n c e , t h s u b r o u t i n e f o l o a d i n g f l o a t i n gp o i n t d a t a f r o m m e m o r y r e s i d e s a $ B A 8 C ( 4 7 7 5 6 ) a n i m o v e s t h c o n t e n t s p o i n t e d t b t h A c c u m u l a t o r ( L S B ) a n t h e Y r e g i s t e r( M S B ) i n t o A F A C . T h u s , i t h d a t a i s t o r e d a $ 0 3 5 0 o n w a r d s ,p r o g r a m 8 . 5 c a r r i e s o u t h i s t r a n s f e r :

P R O G R A M 8 .

L D A I M 8 0L D Y I MJ S $ B A 8 CR T

A s i m i l a r s u b r o u t i n e r e - l o c a t e s d a t a i n t o F A C , t h i s b e i n g l o c a t e da $ B B A 2 ( 4 8 0 3 4 ) .U n f o r t u n a t e l y , a w i t h t h o t h e r r o u t i n e s u s i n g t h f l o a t i n g - p o i n ta c c u m u l a t o r s , m e a s u r e o f a i t h h a b e e n c a l l e d f o r a i n c a s eh a t h c o n t e n t o e i t h e r F A C o r A F A C b e e n v i s i b l e . T o r e c t i f yt h i s h y b r i d B A S I C / m a c h i n e - c o d e c o m m a n d i u t i l i s e d :

T h U S C o m m a n dT h i s c o m m a n d a l l o w s t h t r a n s f e r o d a t a b e t w e e n F A C a n dm a c h i n e - c o d e p r o g r a m . F o i n s t a n c e , t h e l i n e B = U S R ( Q ) i n B A S I Cp r o g r a m c a u s e s t h s y s t e m t p l a c e t h v a l u e o i n t o t h f l o a t i n g - p o i n t a c c u m u l a t o r . I t h e n j u m p s t o m a c h i n e c o d er o u t i n e w h o s e a d d r e s s i f i n d s a t $ 0 3 1 1 ( 7 8 5 ) L S a n $ 0 3 1 2 ( 7 8 6 )M S B . T h p r e s u m p t i o n i t h a t y o h a v e p l a c e d m a c h i n e c o d er o u t i n e i n t o m e m o r y s t a r t i n g a t h a t a d d r e s s a n d s e t t h v a l u e s o $ 0 3 1 1 a n $ 0 3 1 2 t p o i n t t t h r o u t i n e . W h e n y o u r r o u t i n e m a k e s

Page 139: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 139/243

i t h s a m e w a a a n o t h e r f u n c t i o n , f o i n s t a n c e P R I N T U S R ( P + 2 )w i l l p r i n t o u t t h e r e s u l t o f t h m a c h i n e c o d e r o u t i n e w h i c h w i l lh a v e s t a r t e d w i t h F A c o n t a i n i n g t h v a l u e s t o r e d i i n c r e m e n t e db 2

T o t e s t t h i s , a n u m b e r c a b p l a c e d i n t o t h e f l o a t i n g - p o i n ta c c u m u l a t o r b y m e a n s o t h U S f u n c t i o n a n t h e n p r i n t e d o u t .T h i s i d o n e b m e a n s o t h t w p r o g r a m s p r o g r a m 8 . 6 a a n p r o g r a m 8 . 6 b .

P R O G R A M 8 , 6 a

2 0 0 P R I N T " < c l e a r > 1 12 0 1 0 P O K E 7 8 5 , 6 0 6 0 = $ 3 C l A d d r e s s = $ O 3 3 C2 0 2 P O K E 7 8 6 , 3 3 = $ O 32 0 3 B = 1 4 0 02 0 4 0 Q = U S R ( B )2 0 5 P R I N T " Q = " ; Q

P R O G R A M 8 . 6 b

8 2 R T

W h e n t h i s p r o g r a m i s r u n , a a d d r e s s $ O 3 3 C ( 8 2 8 ) i l o a d e d i n t ol o c a t i o n s 7 8 a n 7 8 b l i n e s 2 0 1 a n 2 0 0 2 0 .

W h e n l i n e 2 0 4 i r u n , t h a r g u m e n t B ( 1 4 0 0 ) i l o a d e d i n t o t h f l o a t i n g - p o i n t a c c u m u l a t o r . C o n t r o l i t h e n h a n d e d o v e r t o t h em a c h i n e c o d e p r o g r a m a $ O 3 3 C ( 8 2 8 ) . A t h i s p o i n t , t h e m a c h i n ec o d e r o u t i n e d o e s n ' t a c t u a l l y m o d i f y t h v a l u e o F A C , i s i m p l yo b e y s t h R T w h i c h w i l l r e t u r n c o n t r o l t B A S I C . B A S I C w i l l p l a c et h c o n t e n t s o f F A C i n t o Q L i n e 2 0 5 p r i n t s o u t t h e v a l u es t o r e d i Q w h i c h h a n o b e e n m o d i f i e d b t h m a c h i n e c o d er o u t i n e .

T h i s r o u t i n e o f f e r s w a o l o a d i n g a n y n u m b e r , w h i c h i v a l i d i B A S I C , i n t o t h f l o a t i n g - p o i n t a c c u m u l a t o r . I a l s o o f f e r s w a a l b e i t r a t h e r r o u n d - a b o u t o f e x a m i n i n g ' t h e c o n t e n t s o F A C . T h i si n o a s t r a i g h t - f o r w a r d a m i g h t b i m a g i n e d , a m o s t B A S I Cc o m m a n d s u s e t h e F A C w h e n o p e r a t i n g ; t h u s e v e n P E E K c o m m a n dc h a n g e s i t c o n t e n t s . H o w e v e r , i f t h c o n t e n t s a r e x a m i n e d i m a c h i n e - c o d e , i m m e d i a t e l y a f t e r b e i n g s e t h e y c a n b s e e n b e f o r eB A S I C g e t s i t h a n d s o t h e m a g a i n . T o d o t h i s , p r o g r a m 8 . 6 bs h o u l d b a m e n d e d t e x a m i n e m e m o r y l o c a t i o n s $ 6 t $ 6 ( 9 7 t o1 0 2 ) a n t h e n t p r i n t t h e s e o n t h s c r e e n . T h i s i d o n e i p r o g r a m 8 . 7 .

Page 140: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 140/243

P R O G R A M 8 . L D X I ML D Y I ML D A XS T A XT Y S T A XD E B N R T

9 1 4 2 4

5 5 6 9 6

2 4

P R I N T

S e t u p l o o p c o u n t e rL o a d w i t h f o w h i t eL o a d o n b y t e o F A P r i n t b y t e o s c r e e nT r a n s f e r t L o a d w h i t e i c o l o u r R A D e c r e m e n t l o o p c o u n t e rB r a n c h i m o r e t d

C O N T E N T S O F F A C O S C R E E N

A t h i s p r o g r a m p r i n t s t h c o n t e n t s i c o m p u t e r d i s p l a y c o d e , p r o g r a m8 . 6 a s h o u l d a l s o b m o d i f i e d t d e c o d e t h i s a s b e l o w .

P R O G R A M 8 . 8

2 0 0 0 P R I N T " < h o m e > "2 0 1 P 0 K E 7 8 5 , 6 02 0 2 0 P 0 K E 7 8 6 , 32 0 3 0 B = 1 4 0 02 0 4 0 A = U S R ( B )2 0 5 P R I N T " A = " ; A2 0 6 F 0 R X = 0 T 0 52 0 7 P R I N T P E E K ( 1 4 2 5 + X ) ; " " 2 0 8 0 N E X T

T h i s p r o g r a m s i m p l y P E E K s t h l o c a t i o n s t h a t d i s p l a y t h c o n t e n t sa n p r i n t s t h a n s w e r . O n t h i n g t n o t e w h e n y o a r t y p i n g t h i sp r o g r a m i i t h a t t h e l < h o m e > 1 i l i n e 2 0 0 0 i n d i c a t e s t h a t y o t y p e t h u n s h i f t e d C L R / H O M E k e y ( e a s i e r f o y o t i n t e r p r e t t h a nr e v e r s e d h e a r t i n t h e l i s t i n g ) .W h e n r u n , t h i s w i l l d i s p l a y :

1 4 0 0

a n d t h e c o n t e n t s :

1 3 9 1 7 5 0 0 4

W h e n c o m p a r e d w i t h t h c a l c u l a t e d c o n t e n t s o p a g e 8 - 1 1 i w i l l b s e e n t h a t b y t e s 1 - a r i d e n t i c a l , w h i l e b y t e c o n t a i n s s o m e t h i n gd i f f e r e n t . I f a c t i t i s o n l y t h m o s t - s i g n i f i c a n t b i t t h a t i o c o n s e q u e n c e a n d a s t h i s i s e t o , a ' 0 ' , p o s i t i v e m a n t i s s a i

Page 141: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 141/243

O r e - r u n n i n g , b y t e o t h F A w i l l n o w r e a d 1 2 8 + 4 7 , i . e . i t M S h a b e e n r e s e t t T T h o s e e a r l i e r s u b r o u t i n e s t h a t r e q u i r e d f a i t h c a n n o b t e s t e du s i n g p r o g r a m s 8 . 8 a n 8 . 9 - T a k e p r o g r a m 8 . 4 . T h i s , i w a s a i d ,c o n v e r t e d a i n t e g e r i n t o f l o a t i n g - p o i n t a n l o a d e d i i n t o F A C .P r o g r a m 8 . b e l o w t e s t s t h i s b p r i n t i n g o u t h c o n t e n t s o f F A C ;i u s e s 1 4 0 0 ( $ 0 5 7 8 ) a s t h i n t e g e r , i . e . L S B = $ 7 8 = 1 2 0 a n M S B » $ O 5 - 5 .T h e y a r l o a d e d i n t o ( L D Y I M 1 2 0 ) a n d A ( L D A I M 5 )

P R O G R A M 8 .

L D Y I M 1 2 L D A I MJ S $ B 3 9 1L D X I ML D Y I ML D A X 9 S T A X 1 4 2 4T Y S T A X 5 5 6 9 6D E B N 2 4 R T

C O N V E R T I N T E G E R T F L O A T I N G - P O I N TP r o g r a m 8 . 8 s h o u l d t h e n b m o d i f i e d s a s n o t t p u 1 4 0 0 i n t o F A C ,i . e . l i n e 2 0 3 s h o u l d r e a d

2 0 3 B = ( o a n n u m b e r o t h e r t h a n 1 4 0 0 )T h e s u i t e o p r o g r a m s c a n t h e n b e x e c u t e d u s i n g R U 2 0 0 0 0 a n w i l l d i s p l a y t h v a l u e o a 1 4 0 0 .

D o u b t e r s w i l l a l s o , m o s t p r o b a b l y , w i s h t s e p r o g r a m 8 . r u n ,i . e . t l o a d f l o a t i n g - p o i n t d a t a f r o m m e m o r y . T h i s i d o n e i p r o g r a m s 8 . 1 0 a n 8 . 1 1 w h e r e t h n u m b e r + 2 0 0 0 i u s e d . H o w e v e r ,s l i g h t c o m p l i c a t i o n a r i s e s h e r e , a f l o a t i n g - p o i n t n u m b e r s a r s t o r e d i s l i g h t l y d i f f e r e n t f o r m a t i m e m o r y f r o m t h o s e i n F A Ca n A F A C . T a k i n g + 2 0 0 0 , f o r i n s t a n c e , t h i s i s t o r e d i F A a n A F A C a 1 3 2 5 0 0 0 0 0 o r

Page 142: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 142/243

P A

A F A Cl o c a t i o n

P A

A P A Cl o c a t i o n

9

1 0

1 3 1 0 0 0 1 0 1 1

1 0

1 0

0 0 0 0 0 0 0 0

9

1 0

2 5

1 1 1 1 1 0 1 0

1 0

1 1

0 0 0 0 0 0 0 0

9

1 0

0 0 0 0 0 0 0 0

1 0

1 0

0 0 0 0 0 0 0 0

H o w e v e r , w h e n s t o r i n g l a r g e q u a n t i t i e s o d a t a , m o r e e c o n o m i c a lf o r m a t i u s e d w h i c h r e q u i r e s f i v e b y t e s o n l y . T h i s i k n o w n a M P L P T ( M e m o r y P l o a T i n g P o i n T ) f o r m a t ; t h s i b y t e f o r m a t u s e d i F A C a n d A F A C i k n o w n a F L P T . B y t e o f F L P T i v e r y w a s t e f u l a o n l y b i o b y t e ( m e m o r y l o c a t i o n 1 0 o 1 1 0 ) i r e a l l y n e e d e d .O b v i o u s l y , t b a b l e t d i s p e n s e w i t h b y t e w e h a v e t f i n d w a o s t o r i n g b i a i g i v e s t h s i g n o f t h m a n t i s s a , s o n s p a r eb i m u s t b f o u n d e l s e w h e r e . S u c h s p a r e l o c a t i o n e x i s t s i b y t e

o n i t s l e f t - m o s t b i t , a d u r i n g n o r m a l i s a t i o n o t h m a n t i s s a t h d e c i m a l p o i n t i m o v e d u n t i l t h i s b i t i f i l l e d b y T A i s h o u l d a l w a y s b e s e t t t h e n t h B A S I C i n t e r p r e t e r d o e s n ' tr e a l l y n e e d t r e a d i t d i s c o v e r t h a t i i ' 1 a n d t h e r e f o r e i t l o c a t i o n c a n b e u s e d f o a n o t h e r p u r p o s e . T h e B A S I C i n t e r p r e t e rc a b e s e t u t a s s u m e t h a t t h i s i a n a u t o m a t i c a l l y m a k ea a l l o w a n c e m a d e f o r t h i s b y a d d i n g t t h e l e f t o f t h m a n t i s s a w h e n u s i n g t h i s . T h i s r e d u n d a n t b i t , t h e r e f o r e , c a b a n i u s e d t s t o r e t h s i g n b i t , i . e . ' 0 f o p o s i t i v e a n ' 1 f o n e g a t i v e , a n d b y t e c a b d i s p e n s e d w i t h , g i v i n g u s t h e

M P L P T f o r m a t . T h e r e p r e s e n t a t i o n o + 2 0 0 0 i t h a l t e r n a t i v ef l o a t i n g - p o i n t f o r m a t s t h e n b e c o m e s :

a i F L P T f o r m a t ( a u s e d i n F A C a n d A F A C ) :

1 0 0 0 1 0 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

b i M F L P T f o r m a t ( a u s e d f o s t o r i n g v a r i a b l e s ) :1

Page 143: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 143/243

T o u s e t h " l o a d F A C f r o m m e m o r y " s u b r o u t i n e t h v a r i a b l e d a t a h a t o b e l o a d e d i n t o m e m o r y , a n d t h i s i m o s t e a s i l y d o n e u s i n gd i r e c t P O K E p r o g r a m p r o g r a m 8 . 1 0 b e l o w :

P R O G R A M 8 . 1 0

P O K E 8 5 3 , 1 3 9P O K E 8 5 4 , 1 2 2P O K E 8 5 5 , 0P O K E 8 5 6 , 0P O K E 8 5 7 , 0

T h m a c h i n e c o d e p r o g r a m t u s e t h i s s u b r o u t i n e s e t s p o i n t e r t t h d a t a i 8 5 3 i n ( O 3 8 5 i f i ) o n w a r d s a n d t h e n t r a n s f e r s i w i t h J S $ B B A 2 .

P r o g r a m 8 . 1 1 s h o u l d b e x e c u t e d b m e a n s o t h B A S I C p r o g r a m 8 . 8 .P R O G R A M 8 . 1 1

L D A I ML D Y I MJ S L D X I ML D Y I ML D A XS T A XT Y S T A XD E B N

8 $ B B A 2

9 1 4 2 45 5 6 9 6

2 4 R T

T R A N S F E R D A T A F R O M M E M O R Y T O F A C

W h e n r u n , p r o g r a m 8 . 1 1 w i l l l o a d t h e f l o a t i n g - p o i n t r e p r e s e n t a t i o no + 2 0 0 0 i n t o F A C a n d p r i n t t h i s o n t o t h s c r e e n . B A S I C p r o g r a 8 . 8 w i l l t h e n d e c o d e t h g r a p h i c d a t a a n d p r i n t t h i s o u t i nn u m b e r s .

N a t u r a l l y A F A C h a s s i m i l a r f a c i l i t y f o t r a n s f e r r i n g d a t a , t h i sb e i n g l o c a t e d a t $ B A 8 C ( 4 7 7 5 6 ) .O n c e t h d a t a i l o a d e d i n t o F A C o r A F A C f a c i l i t y e x i s t s f o c o p y i n g f r o m o n e t t h o t h e r , t h a t f o r c o p y i n g A F A C i n t o F A Cr e s i d i n g a $ B B F 6 ( 4 8 1 1 8 ) .

s i m i l a r s u b r o u t i n e c o p i e s t h o t h e r w a y , i . e . f r o m A F A C t o F A C ,

Page 144: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 144/243

E X E R C I S E 8 . W r i t e s u i t e o p r o g r a m s t l o a d 2 0 0 0 i n t oA F A C f r o m m e m o r y a n d t h e n p r i n t o u t h c o n t e n t s o t h i s f r o m 1 4 2 4 o n w a r d s . T h e nt r a n s f e r A F A C i n t o F A a n d a g a i n p r i n t o u t h c o n t e n t s , t h i s t i m e f r o m 1 6 2 4 o n w a r d s .

O n p o s s i b l e a n s w e r g i v e o p a g e 9 - 1 1

F l o a t i n g - P o i n t S u b r o u t i n e sH a v i n g g o n u m b e r s i n t o F A C a n d A F A C , u s e c a n b m a d e o t h b u i l t - i n C O M M O D O R E s u b r o u t i n e s t h a t o f f e r t h o p p o r t u n i t y t h a n d l et h e s e s i x - b y t e m o n s t e r s w i t h r e a s o n a b l e e a s e . A w o r d o w a r n i n gh e r e , t h o u g h . W h e n u s i n g t h e s e s u b r o u t i n e s o n m u s t f i r s t k n o ww h e r e t h e y a r o b v i o u s , e h ? O b v i o u s p e r h a p s ! H o w e v e r , t d a t e ,m a n y o t h e s e h a v e a l r e a d y h a d f o u r a d d r e s s e s o l R O M , 2 . B A S I Ca n 4 . B A S I C o t h P E T , B A S I C V 2 o n t h e V I C 2 a n t h e y a r e n o r e l o c a t e d f o B A S I C V o n t h e C o m m o d o r e 6 4 T h a d d r e s s e s q u o t e di n t h e b o o k a r e f o r t h C - 6 4 a n a r e c o r r e c t f o t h m o d e l s o C o m m o d o r e 6 u s e d i t h i s b o o k . H o w e v e r , i c o u l d h a p p e n t h a tC O M M O D O R E m i g h t d e c i d e t i s s u e n e v e r s i o n o B A S I C f o r t h e C 6 s o m e t i m e i t h f u t u r e , a n d t h e s e a d d r e s s e s m i g h t t h e r e f o r ec h a n g e a g a i n .

c o m p r e h e n s i v e l i s t o t h e s e a d d r e s s e s i p r o v i d e d i A p p e n d i x3 . I a d d i t i o n t t h a d d r e s s e s f o t h C - 6 4 , t h e q u i v a l e n ta d d r e s s e s a r p r o v i d e d f o t h V I 2 0 P E B A S I C 2 . 0 , a n d B A S I C4 . 0 . T h e q u i v a l e n t a d d r e s s e s w i l l b f o u n d m o s t u s e f u l w h e ns e t t i n g o u t c o n v e r t f o r t h e C o m m o d o r e 6 m a c h i n e c o d e p r o g r a mw r i t t e n f o o n o t h o t h e r m a c h i n e s .

f u r t h e r c a v e a t m u s t b g i v e n a l o n g w i t h t h a d v i c e t t r y t h e s e .O n m u s t k n o w w h e r e t h e s e s u b r o u t i n e s p i c k u t h e i r d a t a f r o m a n w h e r e t h e y d e p o s i t t h e r e s u l t , i f t h e y a r t b u s e d s a f e l y . M a n yo f t h e m s t a r t w i t h s h o r t i n i t i a l i s a t i o n s e c t i o n t h a t p r e p a r e s t h d a t a a n d d e p o s i t s i i t h r i g h t p l a c e f o a c t i o n . T h s u b r o u t i n et h a t m o v e s d a t a f r o m m e m o r y i n t o A F A C , f o r i n s t a n c e , s t a r t s b t r a n s f e r r i n g i t d a t a a d d r e s s i n t o $ 1 ( 3 1 ) a n d $ 2 ( 3 5 ) f r o m t h a c c u m u l a t o r a n r e g i s t e r , h e n c e w h e n s t a r t e d a $ B A 8 C i e x p e c t st f i n d t h e d a t a a d d r e s s i n a n Y H o w e v e r , a $ B A 9 0 ( 4 7 7 6 0 ) t h s u b r o u t i n e p r o p e r b e g i n s a n d t h e n r e t r i e v e s i t d a t a a d d r e s s f r o m$ 2 ( 3 4 ) a n $ 2 ( 3 5 ) . T h u s i c a b e n t e r e d e a r l y w i t h t h d a t aa d d r e s s i n A a n d Y o r f e w b y t e s l a t e r w i t h i t a d d r e s s i $ 2 a n $ 2 3 . A s a n i n t e r e s t i n g e x e r c i s e i m a b o f u s e t d i s a s s e m b l e t h i s s u b r o u t i n e a n d t r y a n d f o l l o w t h r o u g h t h v a r i o u ss t a g e s . T h i s c a n b d o n e b y s e l e c t i n g ' L f o r L i s t a t t h e M E N U a n t h e n l i s t i n g f r o m 4 7 7 5 6 .

Page 145: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 145/243

E X E R C I S E 8 .

W r i t e p r o g r a m s u i t e t i n p u t t h n u m b e r s 1 . 0 4 7 a n d 4 0 3 8 . 2 2 i n t om a c h i n e c o d e p r o g r a m . M u l t i p l y t h e s e t o g e t h e r a n t h e n t a k e t h s q u a r e - r o o t o f t h e s u m . P r i n t o u t h i s a n s w e r o n t h e s c r e e n f r o B A S I C .

S e c o n d t h o u g h t s ! ! . . .T h i s i n o a e a s y a i s e e m s ; t h e o r i g i n a l p l a n l o o k e d l i k t h i s :

2 0 0 0 P R I N T " < C L R >2 0 1 0 P O K E 7 8 5 , 6 02 0 2 P O K E 7 8 6 , 32 0 3 I N P U T2 0 4

P u t s i n t o F A

* 8 2 8 J S R $ B C O C ( F A C A F A C )R T

2 0 5 0 P O K E 7 8 5 , 7 22 0 6 0 P O K E 7 8 6 , 32 0 7 I N P U T2 0 8 C = U S R ( D ) - ^

P u t s i n t o F A C

8 3 2 J S R $ B A 2 B ( M U L T )

8 3 5 J S R $ B F 7 1

^ - 8 3 8 R T 2 0 9 0 P R I N T " C = " ; C

T h a t w a s t h e p l a n ; h o w e v e r , i w o n ' t w o r k ! I t f a i l s b e c a u s e t h p r o g r a m s u i t e a s s u m e s t h a t t h F A C ' s c o n t e n t s s t a y p u w h i l e t h e a r e , i f a c t , c o n s t a n t l y c h a n g i n g a B A S I C r u n s p r o g r a m .F o l l o w i n g l i n e 2 0 0 4 0 F A c o n t a i n s B a n d a f t e r t h J S ( F A C - A F A C b o t h F A C a n d A F A C c o n t a i n B H o w e v e r , i e x e c u t i n g l i n e s 2 0 0 5 0 t 2 0 0 8 0 t h o p e r a t i n g s y s t e m u t i l i s e s F A a n A F A C , a n d t h u s c h a n g e t h c o n t e n t s o t h e s e . T h u s w h e n J S ( M U L T ) i c a l l e d , t h c o n t e n t s o F A C a n d A F A C t h a t a r m u l t i p l i e d t o g e t h e r a r e n o t t h o s e x p e c t e d .

Page 146: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 146/243

P R O G R A M 8 . 1 5

L D A I MS T A Z

L D A I MS T A Z

J S R T

1 3 $ 4

$ 4

$ B B C 7

L o a d L S o a d d r e s s( $ 8 4 ) i n t o $ 4 L o a d M S o a d d r e s s( $ 0 3 ) i n t o $ 4

C O P Y A F A C I N T O M E M O R Y

1 3

T h a c t i o n o f t h i s c a n b c h e c k e d b r u n n i n g t h d i r e c t p r o g r a m :F O X = T P R I N T P E E K ( 9 0 0 + X ) ; : N E X T

T o u s e t h i s s u b r o u t i n e i E x e r c i s e 8 . 2 , i t i s n e c e s s a r y t r e l o a dt h d a t a i n t o A F A C . T h i s i d o n e b y t h s u b r o u t i n e a $ B A 8 C( 4 7 7 5 6 ) .

I o r d e r t o p e r a t e , t h s u b r o u t i n e n e e d s t b t o l d w h e r e t f i n dt h d a t a a n t h i s i d o n e b y l o a d i n g t h a d d r e s s o t h f i r s t b y t eo f d a t a i n t o t h a c c u m u l a t o r ( L S B ) a n t h r e g i s t e r ( M S B ) . T h u s

" r e l o a d A F A C " p r o g r a m t r e c o v e r d a t a f r o m 9 0 0 o n w a r d s w o u l d l o o kl i k e :

P R O G R A M 8 . 1 6

L D A I M 1 3 L o a d L S o a d d r e s sL D Y I M L o a d M S o a d d r e s sJ S $ B A 8 C L o a d A F A C f r o m m e m o r yR T

P e r h a p s n o i s u i t a b l e t i m e t t r E x e r c i s e 8 . 2 . O n e p o s s i b l ea n s w e r i g i v e n o p a g e 9 - 1 2 .

O t h e r A v a i l a b l e S u b r o u t i n e sA p p e n d i x l i s t s a l t o g e t h e r 1 s u b r o u t i n e s t h a t h a v e b e e n f o u n dm o s t u s e f u l w h e n d e a l i n g w i t h f l o a t i n g - p o i n t n u m b e r s f r o m m a c h i n ec o d e . T h o s e n o y e t d i s c u s s e d a r c o v e r e d b e l o w :

A d d i t i o nU s i n g t h s u b r o u t i n e a $ B 8 6 A ( 4 7 2 1 0 ) , t h f l o a t i n g - p o i n t n u m b e r si F A a n A F A C a r a d d e d t o g e t h e r a n t h s u m l o a d e d i n t o F A C .T h i s i d e m o n s t r a t e d i p r o g r a m s 8 . 1 7 a n d 8 . 1 8 .

Page 147: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 147/243

P R O G R A M 8

P R O G R A M 8

8 2 8 L D A I MS T A ZL D A I MS T A ZJ S R T

8 4 L D A I ML D Y I MJ S

8 4 J S R T

1

2 2 2 2 2 2 2 2 2 2 2

1

1 3 7

7

0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 0

$ B B C 7

1 3

$ B A 8 C

$ B 8 6 A

P R I N T 1 < C L R > "P O K E 1 , 6 0P O K E 2 , I N P U TA = U S R ( B )R U 2 0 6 P O K E 1 , 7 2P O K E 2 , I N P U TC = U S R ( D )P R I N T " C = " ; C

S t o r e F A i m e m o r y

R e t r i e v e d a t a f r o mm e m o r y , s t o r e i A F A C

A d d i t i o n s u b r o u t i n e

A D T W F L O A T I N G - P O I N T N U M B E R S

W h e n t h p r o g r a m a 2 0 0 0 0 i r u n , i r e q u e s t s t w i n p u t s a n d t h e p r i n t s o u t h s u o t h e s e .

S u b t r a c t i o nP r o g r a m s 8 . 1 7 a n d 8 . 1 8 c a b u s e d t d e m o n s t r a t e t h i s b y i n s e r t i n t h s u b r o u t i n e $ B 8 5 3 ( 4 7 1 8 7 ) a t 8 4 a n 8 4 9 , i . e .P R O G R A M 8 . 1 9 ( P a r t )

8 4 7 J S R $ D 8 5 3T h i s c a n b r u n b y R U N 2 0 0 0 0 a n w i l l p r i n t o u t t h e a n s w e r . I s u b t r a c t s t h s e c o n d i n p u t f r o m t h e f i r s t .

Page 148: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 148/243

P R O G R A M 8 . 2 0 ( P a r t )8 4 7 J S R $ D B 1 2

a n R U 2 0 0 0 0 . T h p r o g r a m d i v i d e s t h f i r s t i n p u t b y t h s e c o n d .

E x p o n e n t i a t i o nT h e x p o n e n t i a t i o n r o u t i n e i a $ B F 7 B ( 4 9 0 1 9 ) s r e p l a c e 8 4 a n 8 4 9 o n c e a g a i n .

P R O G R A M 8 . 2 1 ( P a r t )

8 4 7 J S R $ D F 7 Ba n R U 2 0 0 0 0 . T h p r o g r a m r a i s e s t h f i r s t i n p u t t e d n u m b e r t t h p o w e r o t h s e c o n d i n p u t .

O t h e r r o u t i n e s n e e d o n l y o n i n p u t a n o p e r a t e o t h i s a l o n e ; t h e s ea r e :

L o T h s u b r o u t i n e i a $ B 9 E A ( 4 7 5 9 4 ) a n c o m p u t e s t h n a t u r a ll o g a r i t h m o l o g e ( l o g t t h b a s e e ) P r o g r a m 8 . 2 2 d e m o n s t r a t e st h i s s u b r o u t i n e i n u s e .

P R O G R A M 8 . 2 2

8 2 J S $ B 9 E A8 3 R T

I i c a l l e d b p r o g r a m 8 . 2 3 -

P R O G R A M 8 . 2 32 0 0 P R I N T " < C L R > "2 0 1 P O K E 7 8 5 , 6 02 0 2 P O K E 7 8 6 , 32 0 3 I N P U T2 0 4 0 A = U S R ( B )2 0 5 0 P R I N T " A = " ; A

R U N 2 0 0 0 0 a c t i v a t e s b o t h r o u t i n e s a n p r i n t s o u l o g e o f t h i n p u t v a l u e .

Page 149: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 149/243

T h t h r e e f u n c t i o n s S I $ E 2 6 B ( 5 7 9 6 3 )C O $ E 2 6 4 ( 5 7 9 5 6 )

a n T A N $ E 2 B 4 ( 5 8 0 3 6 )c a b i n c o r p o r a t e d i n t o p r o g r a m , s u i t e s u c h a 8 . 2 2 / 2 3 . I n e a c hc a s e , t h e i n p u t v a l u e i e n t e r e d i r a d i a n s a n t h c o m p u t e df u n c t i o n i r e t u r n e d i n F A C .

A p p e n d i x c o n t a i n s v e r y c o m p r e h e n s i v e l i s t o f t h m a n ys u b r o u t i n e s w h i c h e x i s t w i t h i n t h 6 4 R O M , a n s u s p e c t t h a ty o f i n d t h l i s t s o m e w h a t b e w i l d e r i n g a f i r s t s i g h t . A y o d e v e l o p m o r e e x p e r i e n c e , h o w e v e r , f e e l s u r e t h a t y o u w i l l f i n dt h l i s t m o r e a n m o r e u s e f u l , s d o n ' t b p u t o f f .H o w e v e r , w h e n u s i n g t h e s e b u i l t - i n s u b r o u t i n e s , t h m a j o r p r o b l e ml i e s i k n o w i n g e x a c t l y h o w t i n t e g r a t e t h e s e i n t o a n y p a r t i c u l a rp r o g r a m a s t h e s u b r o u t i n e s ' s t a r t p o i n t s a r w e l l d o c u m e n t e d .T h e s e p r o b l e m s b r o a d l y r e v o l v e a r o u n d w h e r e t h s u b r o u t i n e g e t s i t d a t a f r o m a n w h e r e i d e p o s i t s i t r e s u l t s . O n t h l a t t e r p o i n t ,a w i l l h a v e b e e n s e e n , t h F A i s c o m m o n p l a c e i w h i c h t p l a c er e s u l t s .

A t t h l i n k i n g i o d a t a , t h f i r s t f e w l i n e s o f s u b r o u t i n es h o u l d g i v e t h e c l u e . F i r s t , t h e s e l i n e s s h o u l d b d i s a s s e m b l e du s i n g t h a s s e m b l e r L I S T f u n c t i o n . N e x t t h e s e s h o u l d b e x a m i n e df o t h f i r s t u s e o f A a n a o n c e t h e s e a r e r e - l o a d e d i t h s u b r o u t i n e , a n y d a t a o r i g i n a l l y i t h e m w i l l h a v e b e e n d e s t r o y e d .I a d d r e s s e s a r i n v o l v e d c e r t a i n z e r o - p a g e l o c a t i o n s a r p o p u l a rs u c h a $ 2 2 / $ 2 3 ( 3 4 / 3 5 ) a w e l l a $ 4 9 / $ 4 A ( 7 3 / 7 4 ) . A p p e n d i xa l s o c o n t a i n s l i s t o f t h e l o c a t i o n s i t h z e r o - p a g e a n f o l l o w i n g t h r e e p a g e s t o g e t h e r w i t h b r i e f d e s c r i p t i o n o t h e i ru s e s .

S i m i l a r l y , t h d e p o s i t i o n o p r o c e s s e d d a t a w i l l b r e v e a l e d b t h l a s t f e i n s t r u c t i o n s a n d i f F A a n d A F A C a r e i n v o l v e d , t h e i rm e m o r y l o c a t i o n s s h o u l d f e a t u r e . T h e e n o t h s u b r o u t i n e i u s u a l l y a n R T S i n s t r u c t i o n b u i m i g h t b J M i n s t r u c t i o n t a n o t h e r o t h R O s u b r o u t i n e s , t h a c t u a l r e t u r n b e i n g t h r e s u l to t h e ' c a l l e d ' r o u t i n e s R T S .

F i n a l l y , i l o g i c ( a n d a l l e l s e ! ) f a i l s t h e r e ' s a l w a y s t h ' s h o o to b u s t 1 t e c h n i q u e l e f t , i . e . j u s t t r y i n g i t I f t h i s i a t t e m p t e d , k e e p g o o d r e c o r d s a c o n s t a n t r e l o a d i n g a f t e r c r a s h e sc a b t i m e - c o n s u m i n g . O f c o u r s e , a f t e r c r a s h t h m a c h i n e - c o d ep r o g r a m i n o t l o s t p r o v i d e d c o n t r o l c a b r e g a i n e d b u s o t h

Page 150: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 150/243

R e m e m b e r t h a t t h e s u b r o u t i n e s i R O a r o n l y m a c h i n e - c o d e l i k et h a t t h a t y o u h a v e p r o d u c e d a n o n c e d i s a s s e m b l e d s h o u l d b q u i t ec o m p r e h e n d a b l e , i a t i m e s s o m e w h a t i n v o l v e d . D o n ' t b a f r a i d ,t h e r e f o r e , t u s t h i n g s n o d e t a i l e d h e r e ! T h ' e x p e r t s ' g a i n e dt h e i r e x a l t e d s t a t u s b y b e i n g i n q u i s i t i v e a n t r y i n g i t o u t !

Page 151: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 151/243

C H A P T E RS o l u t i o n s t E x e r c i s e s

C h a p t e rE x e r c i s e 1 . ( P a g e 1 - 1 0 )S i n c e t h a c c u m u l a t o r c o n t a i n s i s e e m s r e a s o n a b l e t u s t h i s t p r i n t w h i t e ' A ' .

S T A R T A D D R E S S ? 8 2 L D A I MS T S T R T E N

1 0 2 45 5 2 9 6

E x e r c i s e 1 . ( P a g e 1 - 1 0 )A s s u m i n g t h a t y o u r n a m e i F R E D a n y o u w i s h t p r i n t t h i s i b l a c k , t h e n y o u r s o l u t i o n m i g h t l o o k l i k e t h f o l l o w i n g :

S T A R T A D D R E S S ? 8 2 L D A I MS T L D A I MS T L D A I MS T L D A I MS T L D A I MS T L D A I MS T L D A I MS T L D A I MS T R T E N

1 0 2 4

5 5 2 9 61 1 0 2 5

5 5 2 9 71 0 2 6

5 5 2 9 8

1 0 2 7

5 5 2 9 9

T h i s i p e r f e c t l y g o o d s o l u t i o n a n d d o e s w h a t i s e t s o u t t o d o .H o w e v e r , y o u m a t h i n k t h a t t h e r e i s c o p e f o i m p r o v e m e n t , a n t h e r e i s T h L D A I M o c c u r s f o u r t i m e s i n t h e s o l u t i o n a b o v e , a n o n l y o n c e i t h f o l l o w i n g :

Page 152: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 152/243

E x e r c i s e 1 . ( a l t e r n a t i v e s o l u t i o n ) .S T A R TL D A I MS T L D A I MS T L D A I MS T L D A I MS T L D A I MS T S T S T S T R T

A D D R E S S ? 8 2

1 0 2 41 1 0 2 5

1 0 2 6

1 0 2 7

5 5 2 9 65 5 2 9 75 5 2 9 85 5 2 9 9

E N

I d o e s n ' t m a t t e r t h a t w e h a v e s e p a r a t e d t h s e t t i n g o f t h e c o l o u rf r o m t h i n s e r t i o n o t h c h a r a c t e r . M a c h i n e c o d e o p e r a t e s s f a s t , t h a t y o h a v e n ' t t h s l i g h t e s t c h a n c e o s e e i n g t h s m a l li n t e r v a l b e t w e e n t h t w o .

E x e r c i s e 1 . 3 ( P a g e 1 - 1 0 )S T A R T A D D R E S S ? 8 2 L D A I M 2 S T 1 0 2 4S T A 1 0 6 3S T A 1 9 8 4S T 2 0 2 3L D A I MS T A 5 5 2 9 6S T A 5 5 3 3 5S T 5 6 2 5 6S T 5 6 2 9 5R T E N

Page 153: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 153/243

E x e r c i s e 1 . ( P a g e 1 - 1 3 )S T A R T A D D R E S S ? 8 2 L D A I M 2 L D X I MS T 9 0 T A L D 9 0 S T A 1 0 2 4S T 2 0 2 3S T 5 5 2 9 6S T A 5 6 2 9 5R T E N

T h c h a r a c t e r s h a t b p r i n t e d i w h i t e i f t h e s o l u t i o n w a s n ' t t h a v e t h i r d I m m e d i a t e M o d e i n s t r u c t i o n .

E x e r c i s e 1 . ( P a g e 1 - 1 3 )S T A R T A D D R E S S ? 8 2 L D A I M 9 L D X I M 4 L D Y I MS T 9 0 T A T Y L D 9 0 S T 1 0 2 4S T 1 0 6 3S T X 1 9 8 4S T Y 2 0 2 3S T A 5 5 2 9 6S T A 5 5 3 3 5S T A 5 6 2 5 6S T A 5 6 2 9 5R T E N

O n c e a g a i n , i o r d e r t c o m p l y w i t h t h c o n d i t i o n t h a t o n l y t h r e eL D ? I M i n s t r u c t i o n s w e r e t b e u s e d , t h i s s o l u t i o n u s e s t h ( f E f )t p r o v i d e t h e c o l o u r , w h i c h t u r n s o u t b g r e e n .

Page 154: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 154/243

C h a p t e rE x e r c i s e 2 . (

i S T A R TL D A I MJ S S T L D A I MS T R T E N

i i S T A R TS T A D R T E N

> a g e 2 - 5 )A D D ? 8 2

9 0 1 0 2 45 5 2 9 6

A D D ? 9 0 9 ^ 9 5

E x e r c i s e 2 . 2 ( P a g e 2 - 7 )S T A R TL D Y I MD E B E J M S T S T R T E N

A D D ? 8 2 81 0

8 3 1 0 2 45 5 2 9 6

E x e r c i s e 2 . ( P a g e 2 - 1 0S T A R TL D A I MS T L D Y I MI N C P B E J M S T L D A I MS T R T E N

A D D ? 8 2 8 8 9

8 9

8 3 1 0 3 4

5 5 3 9 6

Page 155: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 155/243

E x e r c i s e 2 . 4 ( P a g e 2 - 1 2 )

S T A R T A D D ? 8 2 8L D X I ML D Y I M 8 S T Y 8 9 0I N C P 8 9 B N E 2 5 0S T 1 0 2 4L D A I MS T 5 5 2 9 6R T E N

T h i s p r o g r a m p r i n t s t h r e s u l t i w h i t ey o c o u l d u s e a n y o t h e r c o l o u r .

E x e r c i s e 2 . ( P a g e 2 - 1 5 )S T A R T A D D R E S S ? 8 2 L D X I M 9 D E C P X 9 0 0B P 2 5 S T 1 0 2 4S T X 5 5 2 9 6R T E N

P r i n t s i b l a c k a c o n t a i n s 0

Page 156: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 156/243

C h a p t e rE x e r c i s e 3 * ( P a g e 3 - 1 )T h L D X I M i n s t r u c t i o n a 8 2 m u s t b c h a n g e d t L D Y I M . F r o m t h T a b l e ( A p p e n d i x 1 t h o b j e c t c o d e f o r t h i s i 1 6 0 , s o

P O K E 8 2 8 , 1 6 0d o e s t h e t r i c k .

T h S T A X a 8 3 2 h a s t o b e c h a n g e d t a S T A Y ( o b j e c t c o d e 1 5 3 )i . e . P O K E 8 3 2 , 1 5 3

T h S T A X a 8 3 7 h a s t b c h a n g e d t S T A Y ( o b j e c t c o d e 1 5 3 )i . e . P O K E 8 3 7 , 1 5 3

L a s t l y , t h D E a 8 4 n e e d s t b r e p l a c e d b D E ( o b j e c t c o d e1 3 6 )i . e . P O K E 8 4 0 , 1 3 6

T h u s t h f i n a l p r o g r a m r e a d s : -

L D Y I ML D A I MS T A YL D A I MS T A YD E B N R T E N

1 0 9 1 0 2 3

5 5 2 9 52 4

E x e r c i s e 3 . ( P a g e 3 - 2 )L D X I MS T L D X I ML D A I M

1 0 9 0

4 r * * ^

L o a dS t o r eL o a dL o a d

w i t h ' 1 0 0 'i n 9 0 0

w i t h f 0 w i t h 4 ( a s t e r i s k )

L D A I M L o a d w i t h ( w h i t e )S T A X 5 5 2 9 5 S t o r e i ( 5 5 2 9 5 X I N I n c r e m e n tC P X 9 0 0 C o m p a r e w i t h 9 0

Page 157: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 157/243

C h a p t e rE x e r c i s e 4 . ( P a g e 4 - 6 )

1 8 O 7 1 6 4 0 9 6 x 2 5 6 1 4 0 9 6 2 0 4 8 0

2 A F A 1 6 4 0 9 6 1 2 5 1 1 1 1 1 0 0 2 1 0

i . e . 6 1 5 1 1 O 1 1 0 0 2 1 0 1 7 1 5 3 1 0P r o g r a m t a d

S T A R T A D D ? 8 2 8C L C L L D A I M $ 0 A D C I M $ F S T 1 0 2 6L D X I M U s i n g f o t h c o l o u r s a v e sS T 5 5 2 9 8 a i n s t r u c t i o n l a t e r o L D A I M $ 1 8A D C I M : $ 2 AS T 1 0 2 4S T X 5 5 2 9 6R T E N

S c r e e n d i s p l a y g i v e s a n s w e r o a n A i . e . 6 7 1 0 a n d 0 1 1 0 .C o n v e r t i n g t h i s t h e g i v e s :

6 7 1 0 1 $ 4 0 1 1 0 1 $ 0 T h u s 6 7 1 0 a n 0 1 1 0 $ 4 3 0 1

" 4 x 4 0 9 6 + 3 + 2 5 6 + 0 x 1 6 + 1

C r o s s - c h e c k i n g t h i s b h e x a d d i t i o n :

Page 158: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 158/243

E x e r c i s e 4 . 2 ( P a g e 4 - 7 )S T A R TC L S E L D A I MS B C I MS T L D X I MS T L D A I MS B C I MS T S T R T E N

A n

A D D R E S S ? 8 2

3 8 1 0 3 6

5 5 3 0 8

1 0 3 45 5 3 0 6

a n d D0 a n d 2 0 0

E x e r c i s e 4 . ( P a g e 4 - 8 )C L

C L

L D A I M $ 2 A D C I M $ 9 S T A 9 0 0

L D A I M $ 0 A D C I M $ 0 S T 9 0

S E

L D A 9 0 0S B C I M $ F 4S T 1 0 4 1L D X I MS T 5 5 3 1 3

L D 9 0 S B C I M $ 0

C l e a r d e c i m a l t m a k e s u r eb i n a r y m o d eC l e a r c a r r y f l a g

i

L o a d L S A d L S t o S u o L S B ' s

A d d L S B ' st o g e t h e r

L o a d M S 1 A d M S B ' sA d M S t o g e t h e r i n c l .S u o M S B ' s i n c l . c a r r y

( i t h e r e )S e c a r r y i p r e p a r a t i o nf o s u b t r a c t i o n <

S T 1 0 4 0

L o a d L S B ( o f 5 0 0 )S u b t r a c t L S B 1 + f r o m L S B 3 .D i s p l a y s u m o L S B ' si w h i t e

L o a d M S ( o 5 0 0 )S u b t r a c t M S B 1 + f r o m M S 3 D i s p l a y s u M S B ' s

S u b t r a c tL S B ' sa n d i s p l a y

S u b t r a c tM S B ' sa n d i s p l a y .

Page 159: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 159/243

W h e n r u t h i s s h o u l d d i s p l a y a n o a n 2 0 0 .E x e r c i s e 4 . 4 ( P a g e 4 - 1 2 )

( o u t p u t )C

E x e r c i s e 4 . 5 ( P a g e 4 - 1 3 )1 0 0 1 0 1 0 1 20 0 1 1 0 1 0 0 p1 1 4 A N 5

1 6 + 42 1

E x e r c i s e 4 . 6 ( P a g e 4 - 1 7 )

1 0 8 7 9 5 9 2

s

1 2

T h u s :

i 1 0 A N

8

6 3 1

0 1 0 0 0 1 0 0 26

L D A I M 1 0 A N D I M 8 S T 1 0 2 4L D X I MS T X 5 5 2 9 6R T E N

6 1

Page 160: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 160/243

7 O 2

i i

O 1 O 1 1 O 1 16 4 + 0 1 6

L D A I M 7 O R A I M 2 S T 1 0 2 4L D X I MS . T X 5 5 2 9 6R T E N

W h e n r u g i v e s w h i t e + ( 9 1

L i i )

0 1 0 1 1 0 1 00 + 6 4 + 0 + 1 6 + 8 + 0 + 2 + 0L D A I M 9 E O R I M 5 S T 1 0 2 4L D X I MS T 5 5 2 9 6R T E N

W h e n r u g i v e s w h i t e d i a m o n d ( 9 0 ^ 0 ) *i v

9 1 1 0

9 E O

5

1 0 A N

8

E O 9

Page 161: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 161/243

L D A I M 1 0 A N D I M 8 E O R I M 9 S T A 1 0 2 4L D X I MS T X 5 5 2 9 6R T E N

W h e n r u g i v e s w h i t e ( 2 6 1 0 ) .

E x e r c i s e 4 . ( P a g e 4 - 1 8 )8 6 1 Q i B C 1 0 0 0 0 1 1 0

w h i c h i b i n a r y 3 4 - j q

T h u s p r o g r a m i s

L D A I M 1 5 A N D I M 1 S T 1 0 2 5L D X I MS T X 5 5 2 9 7L D Y I ML D A I M 1 3 L S R AD E B N E 2 5 2S T 1 0 2 4S T X 5 5 2 9 6R T E N

W h e n r u t h i s g i v e s a o u t p u t o w h i t e a n ( i . e . 8 6 ) .

Page 162: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 162/243

E x e r c i s e 4 . ( P a g e 4 - 2 2 )W r i t t e n t m u l t i p l y t o g e t h e r t h n u m b e r s a n 4

L D X I MS T 9 0 L D X I MS T 9 0 L D Y I M

b e l o w h e r e a p r o g r a m 4 . 1 2

L D A I MC L L S 9 0 B C C L A D 9 0 A S 9 0 D E B N 2 4 S T 1 0 2 4L D X I MS T X 5 5 2 9 6R T E N

W h e n r u n t h i s s h o u l d p r i n t w h i t e ( 1 2 ) i n 1 0 2 4 .C h a p t e r

E x e r c i s e 5 . ( P a g e 5 - 3 ). 2 2 .L D X I M 8 L D X I M 8

L D Y I M L D Y I MJ M P L 0 0 P 3 J M P 8 6

* L 0 0 P 1 L D A I M 8 L D A I M 8 S T A X 1 1 8 3 S T A X 1 1 8 3T Y A T Y AS T A X 5 5 4 5 5 S T A X 5 5 4 5 5D E X D E XB N L 0 0 P 1 B N 2 4 J M E N J M 8 8

♦ L 0 0 P 2 L D A I M 9 L D A I M 9 S T A X 1 0 2 3 S T A X 1 0 2 3T Y T Y

Page 163: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 163/243

* L 0 0 P 3 L D A I MS T A XT Y S T A XD E B N L D X I MD E J M

» E N D R T E N

4 1 2 2 3

5 5 4 9 5

L O O P S1 2

L 0 0 P 2

L D A I MS T A XT Y S T A XD E B N L D X I MD E J M

R T

4 1 2 2 3

5 5 4 9 5

2 4 1 2

8 5

C h a p t e rE x e r c i s e 7 . ( P a g e 7 - 6 )

P O K E 8 2 9 , 3 2 ( i p r o g r a m l o a d e d f r o m 8 2 8 )T h p r o g r a m s h o u l d t h e n r e a d : -

L D X I M 3 S T J S B E J S C M B N

9 0 6 5 5 0 82 5 6 5 4 9 09 0 2 4

L D A I M 1 J S R T E N

E x e r c i s e 7 .

6 5 4 9 0

( P a g eL D X I MT X P H P H J S B E J S P L P L T A D E B N R T

$ F F E 42 5 $ P F D 2

2 3

Page 164: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 164/243

C h a p t e rE x e r c i s e 8 . ( P a g e 8 - 1 8 )T h d a t a , + 2 0 0 0 i t b s t o r e d f r o m 9 0 0 1 0 ( 0 3 8 4 i g ) o n w a r d s ,d a t a i s

1 3 9 1 2 2 0

I c a b e n t e r e d u s i n g :

P R O G R A M 8 . 1 2

T h i s

P O K EP O K EP O K EP O K EP O K E

9 0 0 , 1 3 99 0 1 , 1 2 29 0 2 , 09 0 3 , 09 0 4 , 0

T h m a c h i n e c o d e r o u t i

P R O G R A M 8 . 1 3

L D A I M 1 3 L D Y I MJ S $ D A 8 CL D X I ML D A XS T A X

9 1 4 2 3

L D A I MS T A XD E B N J S

5 5 6 9 52 4 $ D B F C

L D X I ML D A XS T A X

9 1 6 2 3

L D A I MS T A XD E B N R T E N

5 5 8 9 52 4

i s

S e t u p a d d r e s s f o r s u b r o u t i n eO 5 i 6 = O 3 i o 8 4 1 6 * 1 3 2 1 0P u d a t a i n t o A F A C f r o m m e m o r y -

P r i n t F A C o n t o s c r e e n

T r a n s f e r d a t a A F A C - * F A C

Page 165: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 165/243

T R A N S F E R A F A C t F A

T h e B A S I C p r o g r a m i s

P R O G R A M 8 . 1 42 0 0 0 P R I N T " < c l e a r > "2 0 1 P O K E 7 8 5 , 6 02 0 2 P O K E 7 8 6 , 32 0 3 B = ( f o r e x a m p l e )2 0 4 0 A = U S R ( B )2 0 5 P R I N T " A = " ; A2 0 6 0 R U N 2 0 0 7 02 0 7 F O X = T 2 0 8 P R I N T P E E K ( 1 4 2 4 + X ) ; " " 2 0 9 N E X T2 0 1 0 0 P R I N T2 1 1 F O R X = 0 T 2 0 1 2 0 P R I N T P E E K ( 1 6 2 4 + X ) ; " " 2 1 3 N E X T

W h e n r u t h i s s h o u l d p r i n t o u t :

A 2 0 0 01 2 9 1 2 8 0 0 1 3 2 5 0 1 2 2

R E A D Y

E x e r c i s e 8 . 2 ( P a g e 8 - 1 9 )2 2 2 2 2 2 2 2 2 2 2

0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 0

P R I N T " < c l e a r > "P O K E 7 8 5 , 6 0P O K E 7 8 6 , 3I N P U TA = U S R ( B )R U N 2 0 0 6 0P O K E 7 8 5 , 7 0P O K E 7 8 6 , 3I N P U TC = U S R ( D )P R I N T H C = " ; C

Page 166: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 166/243

E x e r c i s ei

i i

i i i )

i v

L D A I MS T A ZL D A I MS T A ZJ S R T L D A I ML D Y I MJ S J S J S R T E N

A 1 . 1

1 3 7 7 $ A B C 71 3

$ A A 8 C$ A 8 2 B$ A F 7 1

( P a g e A 1 - 2 )0 0 0 0 0 0 1 1 2

0 0 0 0 0 1 0 0 2

1 0 0 0 0 0 0 0 2

1 0 0 0 0 0 1 1 ?

A p p e n d i x0 + 0 + 0 + 0 + 0 + 0 + 2 + 1

3 1 0 + 0 + 0 + 0 + 0 + 4 + 0 + 0

4 1 1 2 8 + 0 + 0 + 0 + 0 + 0 + 0 + 0

1 2 8 1 01 2 8 + 0 + 0 + 0 + 0 + 0 + 2 + 1

v 1 0 1 1 0 1 1 1 2 1 2 8 + 0 + 3 2 + 1 6 + 0 + 4 + 2 + 1

v i 0 1 1 1 0 0 1 1 2 0 + 6 4 + 3 2 + 1 6 + 0 + 0 + 2 + 1

E x e r c i s e A 1 . 2 ( P a g e A 1 - 5 )7 O O O 9 i 6 0 x 4 0 9 + 0 x 2 5 6 + 0 x 1 6 + 9 x 1

0 + 0 + 0 + 99 1

i i 0 0 1 3 - , 6 0 x 4 0 9 6 + 0 x 2 5 6 + 1 x 1 6 + 3 x 10 + 0 + 1 6 + 31 9 1 0

i i i ) O O A 5 ! 6 0 + 0 + 1 0 x 1 6 + 5 x 11 6 0 + 5

i v O A A E 1 6 0 + 1 0 x 2 5 6 + 1 0 x 1 6 + 1 4 x 1

Page 167: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 167/243

v i O H A 1 6 0 + 2 5 6 + 1 6 + 1 02 8 2 1 0

v i i ) O O E A 1 6 0 + 0 + 1 4 x 1 6 + 1 02 2 4 + 1 0

v i i i ) F O A 3 ! 6 1 5 x 4 0 9 6 + 0 + 1 0 x 1 6 + 36 1 4 4 0 + 1 6 0 + 36 1 6 O 3 1 O

E x e r c i s e A 1 . 3 ( P a g e A 1 - 7 )T 4 1 0 1 0 0 2 ( B C D )i i 1 0 1 0 1 x 1 0 + 0i i i ) 7 7 7 x 1 0 + 7

0 1 1 1 0 1 1 1 2 ( B C D )i v 9 7 ! 0 9 x 1 0 + 7

1 0 0 1 0 1 1 1 2 ( B C D )v 5 3 i O 5 x 1 0 + 3

0 1 0 1 0 0 1 1 2 ( B C D )v i 1 0 2 1 0 1 x 1 0 0 + 0 x 1 0 + 2 x 1

0 0 0 1 0 0 0 0 0 0 1 0 2 ( B C D )v i i ) 9 5 3 ! 0 9 x 1 0 0 + 5 x 1 0 + 3 x 1

1 0 0 1 0 1 0 1 0 0 1 1 2 ( B C D )v i i i ) 2 5 7 9 ! 0 2 x 1 0 0 0 + 5 x 1 0 0 + 7 x 1 0 + 9 x 1

0 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 2 ( B C D )E x e r c i s e A 1 . 4 ( P a g e A 1 - 8 )T 0 0 0 0 0 0 0 1 2 ( B C D ) 0 x 1 0 + 1 x 1

1 i i i 0 0 0 0 1 0 0 1 2 ( B C D ) 0 x 1 0 + 9 x 1

9 1 i i i ) 0 0 0 1 0 1 0 1 2 ( B C D ) 1 x 1 0 + 5 x 1

i v 0 0 1 0 0 0 0 0 2 2 x 1 0 + 0 x 12 0 1 0

Page 168: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 168/243

r ± 1 0 1 0 0 0 1 1 2 ( B C D )* * T h i s i n o v a l i d B C D n u m b e r a t h f i r s t n y b b l e , 1 0 1 0 2 1 0 i q » i - e - i g r e a t e rt h a n a l l o w e d i B C D .

v i i ) 1 0 0 1 0 1 1 1 2 ( B C D ) 0 x 1 0 + 7 x 19

v i i i ) 1 0 0 0 1 0 0 0 2 ( B C D ) 8 x 1 0 + 8 x 18 8 1 0

Page 169: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 169/243

A P P E N D I XB i n a r y , B i n a r y - C o d e d D e c i m a l ,

H e x a d e c i m a l N o t a t i o nC o u n t i n g s y s t e m s i g e n e r a l u s t h r o u g h o u t t h w o r l d u s t h d e c i m a l s y s t e m a n t h i s h a s b e e n d e v e l o p e d t c o u n t u p t a n b e y o n d 1 a n a l s o b e l o w t h v a l u e 1 I t h i s s t a n d a r d t h d i g i t st t h l e f t o f n u m b e r a r e o f g r e a t e r v a l u e t h a n t h o s e t t h r i g h t . F o i n s t a n c e , i t h n u m b e r 6 6 t h f i r s t h a v a l u e 1 t i m e s t h s e c o n d , i . e .

T h i s i e x t e n d e d i l a r g e r n u m b e r s w h e r e d i g i t s t t h l e f t a r s u c c e s s i v e l y g r e a t e r b m u l t i p l e o t e n , i . e .

1 0 0 % ^ x 1

1 0 0 0

6 6 6 6s y s t e m w h e r e t h p o s i t i o n o p l a c e o d i g i t i n u m b e r a f f e c t s

i t v a l u e i k n o w n a s P L A C E - V A L U E n u m b e r i n g s y s t e m . I t h d e c i m a l s y s t e m , t h v a l u e s o d i g i t s i n c r e a s e i m u l t i p l e s o 1 a n t h i s i k n o w n a t h e . B A S E f o t h a t s y s t e m . O t h e r s y s t e m s u s d i f f e r e n t b a s e s b u f o l l o w t h s a m e p a t t e r n a t h d e c i m a l s y s t e m ,i . e . t h p l a c e t t h l e f t i g r e a t e r b b e i n g m u l t i p l i e d b t h b a s e .

T h e c o m p u t e r , b e i n g b a s i c a l l y e l e c t r o n i c i o p e r a t i o n , w o r k s b e t t e ri i t i s t o l d t o n l y r e c o g n i s e t w o s t a t e s , o o r o f f o r f 0 a n T a n d t h u s u s e s t h B i n a r y s y s t e m b a s e 2 T h u s , a n n u m b e r i b i n a r y c o n s i s t s s i m p l y o f O ' s a n 1 ' s , o r e l e c t r o n i c a l l y , z e r ov o l t s ( o f f ) a n s o m e v o l t s ( o n ) . T c o u n t p a s t o n e , t h b i n a r ys y s t e m m u s t r e s o r t t p l a c e - v a l u e n o t a t i o n a n d , a w i t h o t h e rc a s e s , t h e m u l t i p l y i n g f a c t o r i t h e b a s e , i . e . 2 T h u s , t h en u m b e r 1 0 i b a s e o b i n a r y r e p r e s e n t s :

4 - 0 x *

i . e . 4 + 0 + 1 = 5 . C l e a r l y t h p l e t h o r a o f , b a s e s p r e s e n t s p r o b l e mw h e n r e p r e s e n t i n g n u m b e r s a s i n b a s e 1 0 1 0 r e p r e s e n t s o n

Page 170: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 170/243

1 0 1 i o O n h u n d r e d a n d o n e i n b a s e t e n .

1 0 1 2 F i v e i b a s e t w o .T h p r e s e n t - d a y g e n e r a t i o n o t h v a r i o u s p e r s o n a l c o m p u t e r s u s e i g h t b i r e g i s t e r s o r m e m o r i e s a n c a t h u s r e p r e s e n t n u m b e r s u t 1 1 1 1 1 1 1 1 2 , i . e . i b a s e 1 0

1 2

1 2

+ 6

6

+ 3

3

+ 1

1

+ + + +

D i g i tE q u i v a l e n t i b a s e 1

P i g . A 1 . 1

B w a o f e x a m p l e , l e t ' s t a k e o n e m o r e c o n v e r s i o n s a y ,1 0 1 0 1 0 1 1 1 2 .

x 1 2 8

3 1 0 x T h u s 1 0 1 0 0 1 1 1 1 x 1 2 8 + 0 x 6 4 + 1 x 3 2 + 0 x 1 6 + 0 x 8 + 1 x 4 + 1 x 2 + 1 x 1

= 1 2 8 + 3 2 + 4 + 2 + 11 6 7 1 0

J u s t t c h e c k y o u r u n d e r s t a n d i n g , h a v e g a t h f o l l o w i n g :E X E R C I S E A 1 . 1

C a l c u l a t e t h v a l u e o f t h f o l l o w i n g i b a s e 1 0 : -i 0 0 0 0 0 0 1 1 2

i i 0 0 0 0 0 1 0 0 2i i i ) 1 0 0 0 0 0 0 0 2

i v 1 0 0 0 0 0 1 1 2v 1 0 t 1 0 1 1 1 2

v i 0 1 1 1 0 0 1 1 2 A n s w e r s o p a g e 9 - 1 6 .I y o u r e m a i n u n c l e a r o t h i s , o s i m p l y w a n t t s e i d e m o n s t r a t e d , l o a d a n d r u n t h B i n a r y / H e x t u t o r p r o g r a m w h i c h i i n c l u d e d o n t h e a s s e m b l e r t a p e . A t h m e n u s e l e c t ' H f o " D e c i m a l , B i n a r y a n H e x a d e c i m a l H . T h e n , w h e n a s k e d " A w h a tn u m b e r d y o u w i s h t s t a r t ? 1 1 , p r e s s " 1 < r e t u r n > . T h e s c r e e n w i l lt h e n d i s p l a y t h r e e r o w s o b o x e s , o w h i c h t h e t o p t w o a r e

Page 171: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 171/243

F r o m t h i s p o i n t , t h e p r o g r a m w i l l s i m p l y c o u n t , e v e r y t i m e t h a t y o p r e s s t h s p a c e b a r , b o t h t h d e c i m a l a n t h b i n a r y b o x e s w i l li n d e x o n e . T r p r e s s i n g t h s p a c e b a o n c e a n d t h b o x e s s h o u l dc o n t a i n 2 1 0 a n d a I O 2 . I y o u c a r r y o i n d e x i n g t h e n y o w i l ls e h o b i n a r y c o u n t s . W h e n y o u g e t t h s t a g e w h e r e t h d e c i m a ls h o w s 1 5 - | o t n b i n a r y s h o u l d r e a d 1 1 . 1 1 2 • N o w i n d e x o n f u r t h e r a n t h b i n a r y w i l l c h a n g e t 1 0 0 0 0 2 . O n w a o l o o k i n g a t h i s i t l a o u t t h e a d d i t i o n : -

1 1 1 1B ' ' ;

O a d d i n g t h ( A ) t o t h ( B t h i s g i v e s f 2 i . e . 0 c a r r y 1 T h i s c a r r y t h e n p r o d u c e s a n o t h e r ' 0 p l u s a n o t h e r c a r r y , a n s o n I y o c o n t i n u e d t p r e s s t h s p a c e b a r l o n g e n o u g h , t h e ne v e n t u a l l y t h b i n a r y r e g i s t e r w o u l d b e c o m e f u l l . H o w e v e r , t h i sw o u l d t a k e a a w f u l l o o f p r e s s i n g , s w e w i l l t a k e s h o r t c u t t h i s s t a t e o a f f a i r s . I n s t e a d o p u s h i n g t h s p a c e b a r , p r e s s t h R E T U R N k e y i n s t e a d . T h i s w i l l r e t u r n y o t t h m e n u w h e r e y o u c a s e l e c t t h ' H o p t i o n a g a i n . T h i s t i m e , w h e n a s k e d " a w h a t n u m b e rd y o w a n t t s t a r t ? " t y p e f a i r l y h i g h v a l u e w h i c h i l e s s t h a n2 5 5 , s a 2 4 0 . O f f y o u g a g a i n u n t i l t h b i n a r y r e g i s t e r i f u l li . e . 1 1 1 1 1 1 1 1 1 1 2 . T h e a d d i t i o n o f u r t h e r o n e , n o w , w i l l c l o c ka l t h b i n a r y r e g i s t e r b a c k t z e r o s a n 2 5 w i l l b e l o s t .H o w e v e r , w i t h t h e 6 5 1 0 , a l i n o l o s t a t h 6 5 1 0 h a c a r r yf l a g t h a t s t o r e s t h f a c t t h a t c a r r y h a s o c c u r r e d . C l o c k i n g p a s t2 5 5 i o w i t h t h B i n a r y / H e x t u t o r w i l l s h o w t h i s h a p p e n i n g . T h i s i

h a n d y f e a t u r e o t h 6 5 1 0 b u t i m u s t n o b r e l i e d o n a s m o r et h a n t e m p o r a r y s t o r e o f t h e c a r r y . T h c a r r y f l a g i j u s t a e a s i l y r e s e t a i i s s e t t o 1

I o r d e r t m a k e s u r e t h a t y o r e a l l y u n d e r s t a n d t h b i n a r yn o t a t i o n , y o u m a w i s h t t r s o m e o t h e x e r c i s e s w h i c h a r p r o v i d e d b y t h B I N / H E X e x e r c i s e s . S e l e c t * E a t h m a i n m e n u .T h i s w i l l p r o v i d e y o u w i t h m e n u o e x e r c i s e s a n y o c a s e l e c tf 2 t t r t h e x e r c i s e s c o n v e r t i n g d e c i m a l n u m b e r s i n t o b i n a r y o ' 5 t t r c o n v e r t i n g t h e m b a c k a g a i n . W h e n y o u a r r u n n i n g t h e x e r c i s e s , b y t h e w a y , t y p i n g s p a c e ( i n s t e a d o f d i g i t ) w i l ld e l e t e t h e l a s t e n t r y t h a t y o m a d e , t h u s p r o v i d i n g y o u w i t hc o r r e c t i o n f a c i l i t y . W h e n y o a r s a t i s f i e d t h a t y o u h a v e d o n ee n o u g h , p r e s s i n g t h < r e t u r n > k e w i l l t a k e y o b a c k t t h m a i nm e n u .

W h i l e t h O ' s a n d * a r c o n v e n i e n t f o t h c o m p u t e r , t h e y a r m u c h l e s s s f o t h m e r e h u m a n s c o m p r o m i s e i s o u g h t . D e c i m a ln o t a t i o n i o l i t t l e u s a s a p a r t f r o m 1 a n d 1 ^ q t h e r e i n

Page 172: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 172/243

t e d i g i t s ( t 9 a r n e e d e d t r e p r e s e n t t h t e n s t e p s u t 1 0 I n t h e b a s e s y s t e m , t w d i g i t s a r n e e d e d s b a s e 2 5 w o u l d n e e d2 5 6 d i g i t s !

c o m p r o m i s e s y s t e m a d o p t e d s p l i t s t h e i g h t b i t s u p i n t o t w p a r t sa n r e p r e s e n t s t h e s e s e p a r a t e l y . T h u s , t h e l a r g e s t n u m b e r t o b er e p r e s e n t e d i 1 1 1 1 2 o 1 5 - i q a n t h i s r e q u i r e s , a l o n g w i t h t h 0 s i x t e e n d i f f e r e n t s y m b o l s . T h e o n e s a d o p t e d f o r t h i s j o b a r e :

2 7 9 1 1 1 2 1 3 1

2 3 6 7 8 9 S y m b o l

D e c i m a l n u m b e r

F i g . A 1 . 2

U s i n g t h i s n o t a t i o n , a n y e i g h t b i n u m b e r c a n b e r e p r e s e n t e d b t w s y m b o l s , o n f o t h m o s t s i g n i f i c a n t f o u r b i t s a n d o n e f o t h l e a s t s i g n i f i c a n t f o u r b i t s . T o a v o i d t h r a t h e r l o n g d e s c r i p t i o no t h e s e t w h a l v e s o a b y t e , t h e y a r e g i v e n t h t e r m N Y B B L E S .T h u s b y t e c o n s i s t s o t w n y b b l e s , m o s t s i g n i f i c a n t n y b b l e( M S N ) a n l e a s t s i g n i f i c a n t n y b b l e ( L S N ) s e F i g . A 1 . 3 .

1 1 I 1 - 2 5 5 1 O ,L S

F i g . A 1 . 3T h e s y s t e m d e s c r i b e d , w h i c h u s e s s i x t e e n s y m b o l s i s o c o u r s e ,g i v e n t h n a m e H E X A D E C I M A L u s u a l l y a b b r e v i a t e d t H E X . I t m a j o ra d v a n t a g e , a f a a c o m p u t e r s a r e c o n c e r n e d , i t h a t i i c o m p a t i b l e w i t h b i n a r y . A n e i g h t b i t b i n a r y n u m b e r c a b r e p r e s e n t e d b t w h e x a d e c i m a l c h a r a c t e r s .Y o a r n o i n p o s i t i o n t l o o k a t h B i n a r y / H e x t u t o r p r o g r a ma g a i n . T h e t h i r d r o o f b o x e s , w h i c h w e i g n o r e d l a s t t i m e r o u n d ,c o n t a i n s t h H e n u m b e r s . W h i l e t h c o u n t i n g i g o i n g o i t h b i n a r y b o x e s , s i i i n t h e H e b o x e s a l s o . T h c o m p a r a b i l i t yb e t w e e n b i n a r y a n H E s h o w s w h e r e v e r m a j o r c a r r y o c c u r s t a k ef o i n s t a n c e 1 1 1 1 2 , 1 5 | q o F 1 6 : o n i n d e x p a s t t h i s c l o c k s t h b i n a r y o n e s t z e r o e s a n d a d d s a o n e t o t h l e f t , i . e . t 1 0 0 0 0 2 o l O T h e s e m a j o r p o i n t s o f c o r r e s p o n d e n c e o c c u r a w h a t f o l l o w s

Page 173: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 173/243

0 0 0 1 0 0 0 0 2

0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 20 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 2

1 1 1 1 6 1 O

1 O O 1 6 2 5 6 1 0

1 0 0 0 - 1 5 = 4 O 9 6 1 OU t 9 t h h e c h a r a c t e r s c o i n c i d e w i t h t h d e c i m a l o n e s a n b e t w e e n 1 a n 1 t h s i n g l e l e t t e r s c o r r e s p o n d t t h d e c i m a ln u m b e r s . A f t e r 1 5 H e t d e c i m a l c o n v e r s i o n b e c o m e s l i t t l e m o r et r i c k y , a s t h e u s o t w n u m b e r s t o g e t h e r , e . g . F F 1 g = 2 5 5 , o n c ea g a i n c a l l s f o r p l a c e - v a l u e n o t a t i o n . T h i s t i m e , a s t h e b a s e i 1 t h r a t i o b e t w e e n a n y p l a c e a n i t n e i g h b o u r i 1 6

T h e v a l u e s , i b a s e 1 o t h p l a c e s i h e x a d e c i m a l a r e :

P l a c e N u m b e r

F i g . A 1 . 4

U s i n g " F i g . A 1 . 4 t h w a t h a t E 9 2 F 1 6 m a k e s u 5 9 6 9 5 . 1 0 i e x p l a i n e db e l o w i f i g u r e A 1 . 5 .

9

E ( 1 4 ) x 4 0 9 6 9 x 2 5 6 2 x 1 6F i g . A 1 . 5

F ( 1 5 ) x 1 5 9 6 9 5

N o t h a t h e i t o t a l l y m a s t e r e d ! t r t h f o l l o w i n g ; t h f i r s t t w a r e x p l a i n e d f u l l y o p a g e 9 . 1 6 .

E X E R C I S E A 1 . 2

C a l c u l a t e t h v a l u e i d e c i m a l o t h f o l l o w i n g : -

i O O O 9 1 6i i O O 1 3 1 6

i i i ) O O A 5 1 6i v O A A E 1 6

A n s w e r s o p a g e 9 - 1 3 *

v 0 0 0 E 1 6v i O 1 1 A 1 6

v i i )v i i i )

Page 174: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 174/243

B i n a r y C o d e d D e c i m a lA s w e l l a d e c i m a l , b i n a r y a n h e x a d e c i m a l n o t a t i o n s , o n o t h e rs y s t e m i u s e d i c o m p u t i n g b i n a r y - c o d e d d e c i m a l . A s i t s n a m es u g g e s t s i t i s h y b r i d f o r m w i t h e l e m e n t s f r o m b i n a r y a n d d e c i m a l .I i c o m m o n l y u s e d w h e r e a o u t p u t i r e q u i r e d i d i g i t a l f o r m a t ,e . g . d i g i t a l c l o c k , o r w h e n g r e a t p r e c i s i o n i r e q u i r e d a n n b i t s c a n b d r o p p e d .

I B C t h n o r m a l d e c i m a l b a s e i r e t a i n e d , i . e . o n e p l a c e i f a c t o r o 1 t i m e s i t n e i g h b o u r b u e a c h i n d i v i d u a l d i g i t i r e p r e s e n t e d i b i n a r y . T h u s t h n u m b e r 8 7 - j o w o u l d b r e p r e s e n t e d :

b a s e 1

1 0 0 0 0 1 1 1

i . e . B C 1 0 0 0 0 1 1 1 ( o i e i g h t b i t s1 0 0 0 0 1 1 1 )

F i g . A 1 . 6

A s t h e l a r g e s t d i g i t r e q u i r e d i d e c i m a l n o t a t i o n i 9 o n l y f o u rb i t s o f b i n a r y a r n e e d e d t r e p r e s e n t t h i s , i . e . 9 < i o = 1 O O 1 2 » * ^ u sB C d i g i t c a b r e p r e s e n t e d b y n y b b l e a n d t w o d i g i t s b y b y t e .F i g u r e A 1 . 6 s h o w s t h i s , w h e r e 8 7 1 0 i r e p r e s e n t e d i B C a 1 0 0 0 0 1 1 1 2 . T h i s c a n g i v e , r i s e t a m b i g u i t y i t h a t 1 0 0 0 0 1 1 1 2 i b i n a r y i 1 3 5 < i o - T o v e r c o m e t h i s , B C r e p r e s e n t a t i o n s w i l l b g i v e n t h n o t a t i o n 1 0 0 0 0 1 1 1 2 ( B C D ) .U s i n g f o u r b i t s o f b i n a r y , i i p o s s i b l e t c o u n t u t 1 5 f o ( i - e .1 1 1 1 2 = 1 5 i o ) * > u t i B C t n l a r g e s t d i g i t u s e d i 9 s i n e v i t a b l yB C i l e s s e c o n o m i c a l i n i t s u s e o f s p a c e . I t s l a r g e s t d i g i t , 9 i 1 0 0 1 2 a n w h e n o n i a d d e d t t h i s i c l o c k s o v e r t 0 0 0 0 2 a n c a r r i e s t h t t h n e x t n y b b l e , i . e .

( b a s e B C D )8 1 9 1 1 0 1 0

0 0 0 00 0 0 00 0 0 10 0 0 1

1 0 0 01 0 0 10 0 0 00 0 0 1

F i g . A 1 . 7I w o u l d p r o b a b l y b h e l p f u l a t h i s p o i n t i y o l o a d a n d r u n t h B i n a r y / H e x t u t o r p r o g r a m a g a i n . T h i s t i m e , s e l e c t ' B a t h m a i nm e n u , a n w h e n a s k e d " A w h a t n u m b e r . . . " e n t e r < r e t u r n > .

Page 175: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 175/243

T h e d i s p l a y w i l l t h e n s h o w t h r e e r o w s o b o x e s a g a i n b u t t h i s t i m et h e y w i l l c o n t a i n d e c i m a l , b i n a r y a n B C D . I y o p r e s s t h s p a c eb a a b e f o r e , t i n d e x f r o m ' 1 ' , y o w i l l n o t i c e t h a t u t b i n a r y a n B C a r i d e n t i c a l . . H o w e v e r , a y o i n d e x f r o m 9 - j q1 0 - j o k e e p a e y o n t h e B C b o a n y o w i l l s e e t h e c a r r i e d o v e rt t h m o s t s i g n i f i c a n t n y b b l e . F r o m 1 0 1 0 u p w a r d s B C b e c o m e st r u e h y b r i d r e p r e s e n t i n g t h d e c i m a l n u m b e r i n b i n a r y f o r m .

A t h n u m b e r i n c r e a s e s , t h u n e c o n o m i c a l n a t u r e o f B C D w i l l b e c o m ea p p a r e n t a 9 9 ^ 0 c h a n g e s t 1 0 0 - j q * U b e f o r e , t y p i n g < r e t u r n >i n s t e a d o t h s p a c e b a r w i l l g e y o u b a c k t t h m a i n m e n u , w h i c hw i l l a l l o w y o t r e s t a r t a v a l u e n e a r e r t 9 9 ^ 0 - ) W h e n 9 9 i oi n d e x e s t 1 0 0 1 0 y o w i l l s e t h B C g e n e r a t e c a r r y f r o m i t m o s t s i g n i f i c a n t n y b b l e t t h c a r r y f l a g .

A m e n t i o n e d a b o v e , t h i s c a r r y i o n l y s h o r t t e r m e x p e d i e n t a n m u s t b p i c k e d u p a t t h e e a r l i e s t p o s s i b l e m o m e n t i i i n o t b e l o s t . T h c a r r y i g e n e r a t e d o n t h e B C D b o x e s a 9 9 w h i l e t h b i n a r y b o x e s w i l l s t o r e u p t 2 5 5 i q - b c i t h e r e f o r e f a i r l yu n e c o n o m i c a l i m e m o r y u s a g e , b u i h a i t u s e s i p a r t i c u l a rs i t u a t i o n s . I n t h e p a s t , m i c r o c o m p u t e r s h a v e a l w a y s b e e n d o g g e d b t h e i r l a c k o f m e m o r y a n c o n s e q u e n t l y B C h a b e e n l i t t l e u s e d .H o w e v e r , t h n e w g e n e r a t i o n o m i c r o c o m p u t e r s h a v e m u c h l a r g e rm e m o r i e s a n i t i s q u i t e l i k e l y t h a t B C w i l l b u s e d m u c h m o r ef r e q u e n t l y t h a n i w a i n t h e p a s t . P e r h a p s i i s i g n o f t h t i m e s , t h a t , a l t h o u g h a l C O M M O D O R E c o m p u t e r s h a v e h a d B C Dc a p a b i l i t y , y o u r c o m p u t e r i t h f i r s t t m a k e u s e o f B C D , a l b e i t i

v e r y s m a l l w a y . T h e 2 4 h o u r t i m e . o f d a y c l o c k s w h i c h a r e b u i l ti n t o t h e 6 4 ' s t w o i n p u t / o u t p u t c h i p s ( 6 5 2 6 * s ) d m a k e u s e o f B C D .A y o k n o w a l a b o u t B C D n o w ! t r t h f o l l o w i n g : -

E X E R C I S E A 1 . 3C o n v e r t t h f o l l o w i n g d e c i m a l n u m b e r s i n t oB C D :

i v 5 i i ) 1 0 v i ) 1 0

i i i ) 7 v i i ) 9 5 3i v 9 7 v i i i ) 2 5 7 9

A n s w e r s o p a g e 9 - 1 7 .

Page 176: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 176/243

E X E R C I S E A 1 . 4 .

C o n v e r t t h f o l l o w i n g B C n u m b e r s i n t o d e c i m a l : -

i i i i i i )i v

v v i

v i i )v i i i )

O O O O0 0 0 00 0 0 10 0 1 00 1 0 01 0 1 01 0 0 11 0 0 0

0 0 0 11 0 0 10 1 0 10 0 0 01 0 0 10 0 1 10 1 1 11 0 0 0

A n s w e r s o p a g e 9 - 1 7 .

I n t h e e x p l a n a t i o n s g i v e n o t h v a l u e o f p l a c e s i p l a c e - v a l u en o t a t i o n s i m p l i f i c a t i o n w a a d o p t e d i o r d e r t m a k e t h e s ee x p l a n a t i o n s c l e a r e r f o o u l e s s m a t h e m a t i c a l l y - i n c l i n e d b r e t h r e n .H o w e v e r , i y o w i s h t s e s l i g h t l y m o r e m a t h e m a t i c a le x p l a n a t i o n , p l e a s e r e a d o n O t h e r w i s e E N D O F A P P E N D I X 1

W i t h b i n a r y n u m b e r s i w a s a i d t h a t t h e p l a c e s i n c r e a s e t h e i rv a l u e i m u l t i p l e s o 2 b u t h l e a s t s i g n i f i c a n t b i t o f t h b i n a r y n u m b e r w a e q u i v a l e n t t t h s a m e s y m b o l b a s e 1 ( o f o t h a t m a t t e r b a s e 3 o w h a t e v e r ) . I a c t u a l f a c t t h m u l t i p l y i n gf a c t o r i t h b a s e r a i s e d t t h p o w e r o i t p l a c e s t a r t i n g w i t hz e r o a t t h e l e f t . I b i n a r y :

P l a c eP r e v i o u s l y s t a t e dm u l t i p l i c a t i o n f a c t o r

M a t h e m a t i c a l l y m o r ep r e c i s e f a c t o r .

T h u s t h l e a s t s i g n i f i c a n t b i i m u l t i p l i e d b 2 o 1 ( i y o a r e n o t s u r e o f t h i s t r y t h d i r e c t p r o g r a m P R I N T 0 . T h n e x tb i i m u l t i p l i e d b 2 1 a n s o o n .T h i s r u l e h o l d s f o A N b a s e ; l e t ' s a p p l y i f o h e x , i . e . b a s e 1 6

L e a s t s i g n i f i c a n t b i f a c t o r 1 6 2 n m o s t s i g n i f i c a n t b i f a c t o r 1 6 1 3 r m o s t s i g n i f i c a n t b i f a c t o r 1 6 2 2 2 5 M o s t s i g n i f i c a n t b i f a c t o r 1 6 4 0 9 6

1 2

2 6 2

3

2 1 2 2 2 2 2

Page 177: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 177/243

A P P E N D I XT a b l e s

T A B L ET h 6 5 1 0 I n s t r u c t i o n S e t w i t h M n e m o n i c s u s e d i t h i s B o o kA b b r e v i a t i o n s u s e d i t h i s t a b l e :

A c c u m u l a t o rr e g i s t e rr e g i s t e r

S e ( t 1 C l e a r ( t 0 C o n d i t i o n a c c o r d i n g t d a t a .

A D C A D d s p e c i f i e d c o n t e n t s t a c c u m u l a t o r w i t h C a r r y : s t o r ea n s w e r i a c c u m u l a t o r ; C o n d i t i o n n e g a t i v e , o v e r f l o w ,z e r o a n d c a r r y f l a g s a c c o r d i n g t r e s u l t .

M N E M .

A D A D C I MA D C I XA D C I YA D C XA D C YA D C ZA D C Z X

O P - C O D E

D E

1 0 1 0

9 1 1 1 2 1 2 1 0 1 1

H E

6 6 6 7 7 7 6 7

N O B Y T E SO P E R .

N O C Y C L E S

5 4 4

A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eI n d i r e c t w i t hI n d i r e c t w i t hI n d e x e d w i t hI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

P l u s c y c l e i p a g e b o u n d a r y c r o s s e d .T a d w i t h o u t c a r r y , c l e a r c a r r y f l a g ( C L C ) b e f o r e A D C .A D o p e r a t e s i d e c i m a l o b i n a r y m o d e a c c o r d i n g t s e t t i n g .

D - f l a g

.

Page 178: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 178/243

A N D A N D s p e c i f i e d c o n t e n t s w i t h a c c u m u l a t o r : s t o r e a n s w e r i a c c u m u l a t o r : c o n d i t i o n n e g a t i v e a n d z e r o f l a g s a c c o r d i n gt r e s u l t .

M N E M .

A N A N D I MA N D I XA N D I YA N D XA N D YA N D ZA N D Z X

O P - C O D E

D E

4 4 3 4 6 5 5 5

H E

2 2 2 5 5 5 2 5

N O B Y T E SO P E R .

N O C Y C L E S

5 4 4

A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eI n d i r e c t w i t hI n d i r e c t w i t hI n d e x e d w i t hI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

♦ P l u s c y c l e i p a g e b o u n d a r y c r o s s e d .

T R U T H T A B L E

^

A S A r i t h m e t i c S h i f t ^ e f t o s p e c i f i e d c o n t e n t s : b i p u i n t o c a r r y , ' O i n t o b i t z e r o ; c o n d i t i o n n e g a t i v e a n z e r o f l a g s a c c o r d i n g t r e s u l t .

M N E M .

A S A S L AA S L XA S L ZA S L Z X

O P - C O D E

D E

1 1 5

2

H E

0 O 1 0 1

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eA c c u m u l a t o rI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

Page 179: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 179/243

B C J 3 r a n c h o £ a r r y £ l e a r : T e s t c a r r y f l a g , i c l e a r ( C = O )b r a n c h r e l a t i v e .

M N E M .

B C

O P - C O D E

D E

1 4

H E

9

N O B Y T E SO P E R .

N O C Y C L E S

2

A D D R E S S I N G M O D E

R e l a t i v e

P l u s o n c y c l e i b r a n c h i m p l e m e n t e d t s a m e p a g e .P l u s t w c y c l e s i b r a n c h i m p l e m e n t e d t d i f f e r e n t p a g e .F L A G S : N o e f f e c t .

B C j J r a n c h o £ a r r y S e t : t e s t c a r r y f l a g , i s e ( C = 1 ) ,b r a n c h r e l a t i v e .

M N E M .

B C

O P - C O D E

D E

1 7

H E

B

N O B Y T E SO P E R .

N O C Y C L E S

2

A D D R E S S I N G M O D E

R e l a t i v e

♦ P l u s o n e c y c l e i f b r a n c h i m p l e m e n t e d t s a m e p a g e .P l u s t w c y c l e s i b r a n c h i m p l e m e n t e d t d i f f e r e n t p a g e .F L A G S : n e f f e c t .

B E B r a n c h o n r e s u l t E g u a l t o z e r o : t e s t Z - f l a g , i s e t ,T Z S 1 ) , b r a n c h r e l a t i v e .

M N E M .

B E

O P - C O D E

D E

2 4

H E

F

N O B Y T E SO P E R .

N O C Y C L E S

2

A D D R E S S I N G M O D E

R e l a t i v e

♦ P l u s o n e c y c l e i f b r a n c h i m p l e m e n t e d t s a m e p a g e .P l u s t w c y c l e s i b r a n c h i m p l e m e n t e d t d i f f e r e n t p a g e .F L A G S : n e f f e c t .

Page 180: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 180/243

B I T A N D s p e c i f i e d B I T s w i t h a c c u m u l a t o r : A r e m a i n su n a l t e r e d ; s e ( = 1 ) i f b i t s m a t c h , t r a n s f e r b i t sa n o s p e c i f i e d d a t a t V a n d N f l a g s r e s p e c t i v e l y ;c o n d i t i o n z e r o f l a g a c c o r d i n g t d a t a .

M N E M .

B I B I T Z

O P - C O D E

D E

4 3

H E

2 2

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eZ e r o - p a g e

B i B i

B M I B r a n c h o r e s u l t M i n u s : t e s t f l a g i f s e tb r a n c h r e l a t i v e .

M N E M .

B M

O P - C O D E

D E

4

H E

3

N O B Y T E SO P E R .

N O C Y C L E S

2

A D D R E S S I N G M O D E

R e l a t i v e

* P l u s o n c y c l e i b r a n c h i m p l e m e n t e d t s a m e p a g e .P l u s t w c y c l e s i b r a n c h i m p l e m e n t e d t d i f f e r e n t p a g e .F L A G S : n o e f f e c t .

B N E B r a n c h o n r e s u l t N o t I S q u a l t z e r o : t e s t f l a g i n o s e t ( Z = 0 ) , b r a n c h r e l a t i v e .

M N E M .

B N

O P - C O D E

D E

2 0

H E

D

N O B Y T E SO P E R .

N O C Y C L E S

2

A D D R E S S I N G M O D E

R e l a t i v e

Page 181: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 181/243

B P I J r a n c h o r e s u l t P L u s : t e s t f l a g , i f n o s e ( N = 0 ) ,b r a n c h r e l a t i v e .

M N E M .

B P

O P - C O D E

D E

1

H E

1

N O B Y T E SO P E R .

N O C Y C L E S

2

A D D R E S S I N G M O D E

R e l a t i v e

P l u s o n e c y c l e i f b r a n c h i m p l e m e n t e d t s a m e p a g e .P l u s t w c y c l e s i b r a n c h i m p l e m e n t e d t d i f f e r e n t p a g e .F L A G S : n e f f e c t .

B R B R e a j C i n t o i n t e r r u p t : i n i t i a t e i n t e r r u p t s e q u e n c e ; S a v eP C + 2 o n s t a c k ; s e t B f l a g ( B = 1 ) ; s a v e P S W o n s t a c k ;l o a d i n t e r r u p t v e c t o r s ( F F F E a n d F F F F ) i n t o P C S e f l a g ( 1 = 1 ) .

M N E M .

B R

O P - C O D E

D E H E

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

B V j ^ r a n c h o o v e r f l o w _ C l e a r : t e s t o v e r f l o w f l a g , i f n o s e t ( V = 0 ) , b r a n c h r e l a t i v e .

M N E M .

B V

O P - C O D E

D E

8

H E

5

N O B Y T E SO P E R .

N O C Y C L E S

2

A D D R E S S I N G M O D E

R e l a t i v e

P l u s o n c y c l e i b r a n c h i m p l e m e n t e d t s a m e p a g e .P l u s t w c y c l e s i b r a n c h i m p l e m e n t e d t d i f f e r e n t p a g e .

Page 182: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 182/243

B V S B r a n c h o o V e r f l o w S e t : t e s t o v e r f l o w f l a g , i f s e t" ( V ^ i ) , b r a n c h r e l a t i v e .

M N E M .

B V

O P - C O D E

D E

1 1

H E

7

N O B Y T E SO P E R .

N O C Y C L E S

2

A D D R E S S I N G M O D E

R e l a t i v e

* P l u s o n c y c l e i b r a n c h i m p l e m e n t e d t s a m e p a g e .P l u s t w c y c l e s i b r a n c h i m p l e m e n t e d t d i f f e r e n t p a g e .F L A G S : n o e f f e c t .

C L C L e a r £ a r r y f l a g : l o a d ' O i n t o c a r r y f l a g ( C O ) .

M N E M .

C L

O P - C O D E

D E

2

H E

1

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

C L C J L e a r D e c i m a l f l a g : l o a d f O i n t o d e c i m a l f l a g .

M N E M .

C L

O P - C O D E

D E

2 1

H E

D

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O

I m p l i e d

Page 183: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 183/243

C L C ^ e a r i n t e r r u p t d i s a b l e f l a g : l o a d f 0 i n t o i n t e r r u p tf l a g ( 1 = 0 ) .

M N E M .

C L

O P - C O D E

D E

8

H E

5

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

C L C L e a r o v e r f l o w f l a g : l o a d ' 0 i n t o o v e r f l o w f l a g ( V = 0 ) .

M N E M .

C L

O P - C O D E

D E

1 8

H E

B

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

Page 184: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 184/243

C M P C o M P a r e s p e c i f i e d d a t a w i t h a c c u m u l a t o r : s u b t r a c t d a t aT r o i n a c c u m u l a t o r , d n o s t o r e r e s u l t ; s e i e q u a l ,o t h e r w i s e r e s e t ; c o n d i t i o n b b i a n b r e s u l t .

M N E M .

C M C M P I MC M P I XC M P I YC M P XC M P YC M P ZC M P Z X

O P - C O D E

D E

2 0 2 0 1 9 2 0 2 2 2 1 1 9 2 1

H E

C C C D D D C D

N O B Y T E SO P E R .

N O C Y C L E S

5 4 4

A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eI n d i r e c t w i t hI n d i r e c t w i t hI n d e x e d w i t hI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

P l u s o n c y c l e i p a g e b o u n d a r y c r o s s e d .

C P C J o m P a r e s p e c i f i e d d a t a w i t h X r e g i s t e r : s u b t r a c t d a t af r o m r e g i s t e r , d o n o t s t o r e r e s u l t ; s e t i e q u a l ,o t h e r w i s e r e s e t ; c o n d i t i o n b b i a n b r e s u l t .

M N E M .

C P C P X I MC P X Z

O P - C O D E

D E

2 3 2 2 2 2

H E

E E E

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eZ e r o - p a g e

Page 185: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 185/243

C P Y C o m P a r e s p e c i f i e d d a t a w i t h Y - r e ^ i s t e r : s u b t r a c t d a t af r o m Y - r e g i s t e r , d n o s t o r e r e s u l t ; s e t i f e q u a l ,o t h e r w i s e r e s e t ; c o n d i t i o n N b y b i t a n b y r e s u l t .

M N E M .

C P C P Y I MC P Y X

O P - C O D ED E

2 0 1 9 1 9

H E

C C C

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eZ e r o - p a g e

D E D E C r e m e n t s p e c i f i e d m e m o r y c o n t e n t s b o n e , s t o r er e s u l t i s p e c i f i e d m e m o r y l o c a t i o n ; c o n d i t i o n n e g a t i v ea n z e r o f l a g s a c c o r d i n g t r e s u l t .

M N E M .

D E D E C XD E C ZD E C Z X

O P - C O D E

D E

2 0 2 2 1 9 2 1

H E

C D C D

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e , i n d e x e d w i t h

Page 186: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 186/243

D E X D E c r e m e n t c o n t e n t s o X - r e g i s t e r : s t o r e r e s u l t i X - r e g i s t e r ; c o n d i t i o n n e g a t i v e a n d z e r o f l a g s a c c o r d i n gt r e s u l t .

M N E M .

D E

O P - C O D ED E

2 0

H E

C

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

D E D E c r e m e n t c o n t e n t s o Y - r e 6 i s t e r t s t o r e r e s u l t i Y - r e g i s t e r ; c o n d i t i o n n e g a t i v e a n d z e r o f l a g s a c c o r d i n gt r e s u l t .

M N E M .

D E

O P - C O D E

D E

1 3

H E

8

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

Page 187: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 187/243

E O P e r f o r m E x c l u s i v e O b e t w e e n a c c u m u l a t o r a n s p e c i f i e dc o n t e n t s : s t o r e r e s u l t i a c c u m u l a t o r . C o n d i t i o nn e g a t i v e a n z e r o f l a g s a c c o r d i n g t r e s u l t .

M N E M .

E O E O R I ME O R I XE O R I YE O R XE O R YE O R ZE O R Z X

O P - C O D ED E

7 7 6 8 9 8 6 8

H E

4 4 4 5 5 5 4 5

N O B Y T E SO P E R .

N O C Y C L E S

5 4 4

A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eI n d i r e c t w i t hI n d i r e c t w i t hI n d e x e d w i t hI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

P l u s o n c y c l e i p a g e b o u n d a r y c r o s s e d .

T R U T H T A B L E

I N I N C r e m e n t s p e c i f i e d c o n t e n t s b y o n e : s t o r e r e s u l t i s p e c i f i e d l o c a t i o n ; c o n d i t i o n n e g a t i v e a n d z e r o p a g ef l a g s a c c o r d i n g t r e s u l t .

M N E M .

I N I N C XI N C ZI N C Z X

O P - C O D E

D E

2 3 2 5 2 3 2 4

H E

E F E F

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

Page 188: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 188/243

I N _ I N c r e m e n t X r e g i s t e r b y o n e : s t o r e r e s u l t i X - r e g i s t e r ; c o n d i t i o n n e g a t i v e a n d z e r o f l a g s a c c o r d i n gt r e s u l t .

M N E M .

I N

O P - C O D ED E

2 3

H E

E

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

I N J E N c r e m e n t Y r e g i s t e r b o n e : s t o r e r e s u l t i Y - r e g i s t e r ; c o n d i t i o n n e g a t i v e a n d z e r o f l a g s a c c o r d i n gt r e s u l t .

M N E M .

I N

O P - C O D E

D E

2 0

H E

C

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

J M J u X P _ t s p e c i f i e d a d d r e s s ( l o a d s p e c i f i e d a d d r e s s i n t op r o g r a m c o u n t e r ) .

M N E M .

J M J M P I A

O P - C O D E

D E

7 1 0

H E

4 6

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eI n d i r e c t

Page 189: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 189/243

J S £ u m p t S u b R o u t i n e a s p e c i f i e d a d d r e s s : s t o r e p r o g r a mc o u n t e r c o n t e n t s + 2 o n s t a c k ; l o a d s p e c i f i e d c o n t e n t si n t o p r o g r a m c o u n t e r .

M N E M .

J S

O P - C O D ED E

3

H E

2

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t e

F L A G S : n o e f f e c t .

L D . L c - a D A c c u m u l a t o r w i t h s p e c i f i e d c o n t e n t s :n e g a t i v e a n z e r o f l a g s a c c o r d i n g t d a t a .

c o n d i t i o n

M N E M .

L D L D A I ML D A I XL D A I YL D A XL D A YL D A ZL D A Z X

O P - C O D E

D E

1 7 1 6 1 6 1 7 1 8 1 8 1 6 1 8

H E

A A A B B B A B

N O B Y T E SO P E R .

N O C Y C L E S

5 4 4

A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eI n d i r e c t w i t hI n d i r e c t w i t hI n d e x e d w i t hI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

P l u s o n c y c l e i p a g e b o u n d a r y c r o s s e d .

Page 190: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 190/243

L D I i o a J D X - r e g i s t e r w i t h s p e c i f i e d c o n t e n t s : c o n d i t i o nn e g a t i v e a n d z e r o f l a g s a c c o r d i n g t d a t a .

M N E M .

L D L D X I ML D X YL D X ZL D X Z Y

O P - C O D E

D E

1 7 1 6 1 9 1 6 1 8

H E

A A B A B

N O B Y T E SO P E R .

N O C Y C L E S

4

A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

♦ P l u s o n c y c l e i f p a g e b o u n d a r y c r o s s e d .

L D ^ o a d Y - r e g i s t e r w i t h s p e c i f i e d c o n t e n t s : c o n d i t i o nn e g a t i v e a n d z e r o f l a g s a c c o r d i n g t d a t a .

M N E M .

L D L D Y I ML D Y XL D Y ZL D Y Z X

O P - C O D ED E

1 7 1 6 1 8 1 6 1 8

H E

A A B A B

N O B Y T E SO P E R .

N O C Y C L E S

4

A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

Page 191: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 191/243

L S P e r f o r m L o g i c a l S h i f t R i g h t o s p e c i f i e d c o n t e n t s : l o a db i i n t o c a r r y b i a n ' 0 i n t o b i t 7 c o n d i t i o nn e g a t i v e a n d z e r o f l a g s a c c o r d i n g t d a t a .

M N E M .

L S L S R AL S R XL S R ZL S R Z X

O P - C O D E

D E

7 7 9 7 8

H E

4 4 5 4 5

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eA c c u m u l a t o rI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

N O N o O P e r a t i o n ; w a i t t w c y c l e s t h e n c o n t i n u e .

M N E M .

N O

O P - C O D E

D E

2 3

H E

E

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

F L A G S : n e f f e c t .

O R P e r f o r m l o g i c a l O R b e t w e e n A c c u m u l a t o r a n c o n t e n t s : s t o r e r e s u l t i a c c u m u l a t o r ;n e g a t i v e a n z e r o f l a g s a c c o r d i n g t r e s u l t .

s p e c i f i e dc o n d i t i o n

M N E M .

O R O R A I MO R A I XO R A I YO R A XO R A YO R A ZO R A Z X

O P - C O D E

D E

1

1 2 2 2

H E

O 0 0 1 1 1 0 5 1

N O B Y T E SO P E R .

N O C Y C L E S

5 4 4

A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eI n d i r e c t w i t hI n d i r e c t w i t hI n d e x e d w i t hI n d e x e d w i t h YZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

Page 192: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 192/243

P H £ u s H A c c u m u l a t o r o n t o s t a c k : u p d a t e s t a c k p o i n t e r ;r e m a i n s u n a l t e r e d .

M N E M .

P H

O P - C O D E

D E

7

H E

4

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

F L A G S : n e f f e c t .

P H P . U S H . P r o c e s s o r s t a t u s w o r d o n t o s t a c k : u p d a t e s t a c kp o i n t e r ; P S r e m a i n s u n a l t e r e d .

M N E M .

P H

O P - C O D E

D E H E

0

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

F L A G S : n o e f f e c t .

P L P u L l . A c c u m u l a t o r f r o m s t a c k : u p d a t e p o i n t e r ; c o n d i t i o nn e g a t i v e a n d z e r o f l a g s a c c o r d i n g t d a t a .

M N E M .

P L

O P - C O D E

D E

1 0

H E

6

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

Page 193: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 193/243

P L j ^ I i 1 . P r o c e s s o r s t a t u s w o r d f r o m s t a c k : u p d a t e s t a c kp o i n t e r ; c o n d i t i o n A L L f l a g s a c c o r d i n g t P S r e t r i e v e d .

M N E M .

P L

O P - C O D E

D E

4

H E

2

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

R O L R O t a t e J L e f t o n e p l a c e , s p e c i f i e d c o n t e n t s : l o a d c a r r y -b i i n t o b i t 0 o s p e c i f i e d d a t a a n d b i o f t h e s ei n t o c a r r y f l a g ; c o n d i t i o n n e g a t i v e a n d z e r o f l a g sa c c o r d i n g t r e s u l t .

M N E M .

R O R O L AR O L XR O L ZR O L Z X

O P - C O D E

D E 4 4 6 3 5

H E 2 2 3 2 3

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eA c c u m u l a t o rI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

R O i 9 - b i t r o t a t i o n .

B b i

Page 194: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 194/243

R O R O t a t e R i g h t o n p l a c e , s p e c i f i e d c o n t e n t s :l o a d c a r r y b i t i n t o b i o s p e c i f i e d d a t a a n b i t 0o t h e s e i n t o c a r r y f l a g ; c o n d i t i o n n e g a t i v e a n d z e r of l a g s a c c o r d i n g t r e s u l t .

M N E M .

R O R O R AR O R XR O R ZR O R Z X

O P - C O D ED E

1 1 1 0 1 2 1 0 1 1

H E

6 6 7 6 7

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eA c c u m u l a t o rI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

R O i 9 - b i t r o t a t i o n .

b i

R T R e T u r n f r o m i n t e r r u p t : r e t r i e v e P S W a n d P C f r o m s t a c k ,u p d a t e s t a c k p o i n t e r .

M N E M .

R T

O P - C O D E

D E

6

H E

4

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

R T I t e T u r n f r o m S u b r o u t i n e : r e t r i e v e P f r o m s t a c k a n di n c r e m e n t b o n e , u p d a t e s t a c k . p o i n t e r .

M N E M .O P - C O D E

D E H E

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

Page 195: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 195/243

S B S t a B t r a c t w i t h C a r r y s p e c i f i e d c o n t e n t s f r o ma c c u m u l a t o r : s t o r e a n s w e r i a c c u m u l a t o r ; c o n d i t i o nn e g a t i v e , o v e r f l o w , z e r o a n c a r r y f l a g s a c c o r d i n g t r e s u l t .

M N E M .

S B S B C I MS B C I XS B C I YS B C XS B C YS B C ZS B C Z X

O P - C O D E

D E H E

2 3 E 2 3 3 E 92 2 E 2 4 F 2 5 F 2 4 9 F 92 2 E 2 4 5 F 5

N O B Y T E SO P E R .

N O C Y C L E S

5 4 4

A D D R E S S I N G M O D E

A b s o l u t eI m m e d i a t eI n d i r e c t w i t hI n d i r e c t w i t hI n d e x e d w i t hI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

* P l u s o n c y c l e i p a g e b o u n d a r y c r o s s e d .T s u b t r a c t w i t h o u t c a r r y , s e c a r r y f l a g ( S E C ) b e f o r e S B C .S B C o p e r a t e s i d e c i m a l o b i n a r y m o d e a c c o r d i n g t D - f l a gs e t t i n g .

S E S E £ a r r y f l a g : C =

M N E M .

S E

O P - C O D E

D E H E

5 3

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

Page 196: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 196/243

S E S E d e c i m a l f l a g : D »

M N E M .

S E

O P - C O D E

D E

2 4

H E

F

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

S E S E I n t e r r u p t d i s a b l e f l a g : 1 =

M N E M .

S E

O P - C O D E

D E

1 2

H E

7

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

S T A S T o r e A c c u m u l a t o r c o n t e n t s a s p e c i f i e d a d d r e s s :r e m a i n s u n a l t e r e d .

M N E M .

S T S T A I XS T A I YS T A XS T A YS T A ZS T A Z X

O P - C O D E

D E

1 4 1 2 1 4 1 5 1 5 1 3 1 4

H E

8 8 9 9 9 8 9

N O B Y T E SO P E R .

N O C Y C L E S

6

A D D R E S S I N G M O D E

A b s o l u t eI n d i r e c t w i t hI n d i r e c t w i t hI n d e x e d w i t hI n d e x e d w i t hZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

Page 197: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 197/243

S T X S T o r e c o n t e n t s o X - r e g i s t e r a s p e c i f i e d a d d r e s s :r e m a i n s u n a l t e r e d .

M N E M .

S T S T X ZS T X Z Y

O P - C O D E

D E

1 4 1 3 1 5

H E

8 8 9

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

F L A G S : n e f f e c t .

S T Y S T o r e c o n t e n t s o Y - r e g i s t e r a s p e c i f i e d a d d r e s s :r e m a i n s u n a l t e r e d .

M N E M .

S T S T Y ZS T Y Z X

O P - C O D E

D E

1 4 1 3 1 4

H E

8 8 9

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

A b s o l u t eZ e r o - p a g eZ e r o - p a g e i n d e x e d w i t h

F L A G S : n e f f e c t .

T A T r a n s f e r c o n t e n t s o A c c u m u l a t o r t X - r e g i s t e r :r e m a i n s u n a l t e r e d ; c o n d i t i o n n e g a t i v e a n d z e r o f l a g sa c c o r d i n g t d a t a .

M N E M .

T A

O P - C O D E

D E

1 7

H E

A

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

Page 198: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 198/243

T A Y T r a n s f e r c o n t e n t s o A c c u m u l a t o r t Y - r e g i s t e r :r e m a i n s u n a l t e r e d ; c o n d i t i o n n e g a t i v e a n z e r o f l a g sa c c o r d i n g t d a t a .

M N E M .

T A

O P - C O D E

D E

1 6

H E

A

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

T S T r a n s f e r c o n t e n t s o S t a c k - p o i n t e r t X - r e g i s t e r : S r e m a i n s u n a l t e r e d ; c o n d i t i o n n e g a t i v e a n z e r o f l a g sa c c o r d i n g t d a t a .

M N E M .

T S

O P - C O D E

D E

1 8

H E

B

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

T X T r a n s f e r c o n t e n t s o X - r e g i s t e r i n t o A £ c u m u l a t o r :r e m a i n s u n a l t e r e d ; c o n d i t i o n n e g a t i v e a n z e r o f l a g sa c c o r d i n g t d a t a .

M N E M .

T X

O P - C O D E

D E

1 3

H E

8

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

Page 199: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 199/243

T X T r a n s f e r c o n t e n t s o X - r e g i s t e r i n t o S t a c k : r e m a i n su n a l t e r e d .

M N E M .

T X

O P - C O D E

D E H E

1 5 9

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

F L A G S : n o e f f e c t .

T Y T r a n s f e r c o n t e n t s o f J T - r e g i s t e r i n t o A c c u m u l a t o r :" r e m a i n s u n a l t e r e d ; c o n d i t i o n n e g a t i v e a n z e r o f l a g sa c c o r d i n g t d a t a .

M N E M .

T Y

O P - C O D E

D E H E

1 5 9

N O B Y T E SO P E R .

N O C Y C L E S A D D R E S S I N G M O D E

I m p l i e d

Page 200: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 200/243

Page 201: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 201/243

T A B L E 3H e x a d e c i m a l t D e c i m a l C o n v e r s i o n C h a r t

H E

1 3 4 6 8 9

1 1 1 2 1 4 1 6 1 7 1 9 2 0 2 2 2 4

1 3 4 6 8 9

1 1 1 2 1 4 1 6 1 7 1 9 2 0 2 2 2 4

1 3 5 6 8 9

1 1 1 3 1 4 1 6 1 7 1 9 2 1 2 2 2 4

1 3 5 6 8 9

1 1 1 3 1 4 1 6 1 7 1 9 2 1 2 2 2 4

2 3 5 6 8

1 0 1 1 1 3 1 4 1 6 1 8 1 9 2 1 2 2 2 4

2 3 5 6 8

1 0 1 1 1 3 1 4 1 6 1 8 1 9 2 1 2 2 2 4

2 3 5 7 8

1 0 1 1 1 3 1 5 1 6 1 8 1 9 2 1 2 3 2 4

2 3 5 7 8

1 0 1 1 1 3 1 5 1 6 1 8 1 9 2 1 2 3 2 4

2 4 5 7 8

1 0 1 2 1 3 1 5 1 6 1 8 2 0 2 1 2 3 2 4

2 4 5 7 8

1 0 1 2 1 3 1 5 1 6 1 8 2 0 2 1 2 3 2 4

1 2 4 5 7 9

1 0 1 2 1 3 1 5 1 7 1 8 2 0 2 1 2 3 2 5

1 2 4 5 7 9

1 0 1 2 1 3 1 5 1 7 1 8 2 0 2 1 2 3 2 5

1 2 4 6 7 9

1 0 1 2 1 4 1 5 1 7 1 8 2 0 2 2 2 3 2 5

1 2 4 6 7 9

1 0 1 2 1 4 1 5 1 7 1 8 2 0 2 2 2 3 2 5

1 3 4 6 7 9

1 1 1 2 1 4 1 5 1 7 1 9 2 0 2 2 2 3 2 5

1 3 4 6 7 9

1 1 1 2 1 4 1 5 1 7 1 9 2 0 2 2 2 3 2 5

T A B L E 4A S C I I C h a r a c t e r S e

H E L S

M S B I

0 0 0 00 0 0 10 0 1 00 0 1 10 1 0 00 1 0 10 1 1 00 1 1 11 0 0 01 0 0 11 0 1 01 0 1 11 1 0 01 1 0 11 1 1 01 1 1 1

0 0

N U S O S T E T E O E N A C B E B H L V F C S S

0 0

D L D C D C D C D C N A S Y E T C A E S U E S F G R U

0 1

S P A C E

0 1 1 0 1 0 1 1 1 1

« .

D E

Page 202: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 202/243

T A B L E S6 5 1 0 F l a g G u i d eT h i s g u i d e l i s t s a l l 6 5 1 0 f l a g s w i t h t h f a m i l i e s o i n s t r u c t i o n st h a t s e t t h e m a n d t h e b r a n c h i n s t r u c t i o n s t h a t t e s t t h e i r c o n d i t i o n .

N E G A T I V E F L A G

I n s t r u c t i o n

A D A N A S B I C M C P C P D E

D E D E E O I N I N I N L D L D

t C o n d i t i o n

L D L S O R P L P L R O R O R T

S B T A T A T X T Y

I n s t r u c t i o n t T e s t

B M B P

B I i n s t r u c t i o n l o a d s N - f l a g w i t h b i o s p e c i f i e d m e m o r y l o c a t i o n O V E R F L O W F L A G

I n s t r u c t i o n t C o n d i t i o n I n s t r u c t i o n t T e s t

A D C C L V R T I B V CB I P L S B C B V S

B I l o a d s w i t h b i o t h s p e c i f i e d m e m o r y l o c a t i o n .

B R E A K F L A G

I n s t r u c t i o n t C o n d i t i o n I n s t r u c t i o n t T e s tB R P L R T

D E C I M A L F L A G

I n s t r u c t i o n t C o n d i t i o n I n s t r u c t i o n t T e s tC L P L P R T I S E D

I N T E R R U P T F L A G

I n s t r u c t i o n t C o n d i t i o n I n s t r u c t i o n t T e s t

Page 203: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 203/243

Z E R O F L A G

I n s t r u c t i o n

A D A N A S B I C M C P C P D E

C

D E D E E O I N I N I N L D L D

t C o n d i t i o n

L D L S O R P L P L R O R O R T

3 A R R Y F L A G

I n s t r u c t i o n

S B T A T A T X T Y

t C o n d i t i o n

I n s t r u c t i o n t T e s t

B E B N

I n s t r u c t i o n t T e s t

A D C P X R O L S E C B C CA S C P R O B C C L L S R T C M P P L P S B

Page 204: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 204/243

Page 205: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 205/243

A P P E N D I XM a p s a n V e c t o r s

T h s e r i o u s m a c h i n e c o d e p r o g r a m m e r o t h C - 6 4 i w r i t i n gp r o g r a m s w h i c h , o f n e c e s s i t y , m u s t c o - e x i s t p e a c e f u l l y w i t h t h o t h e r s o f t w a r e w h i c h h a b e e n p r o v i d e d i F O M b y C o m m o d o r e .

F i r s t m a n y o t h m o s t p o w e r f u l 6 5 1 0 i n s t r u c t i o n s r e q u i r e u s e o z e r o p a g e l o c a t i o n s . T h B A S I C a n K e r n e l r o u t i n e s m a k e e x t e n s i v eu s e o f t h e z e r o p a g e , h o w e v e r , a n k n o w l e d g e o f t h e l o c a t i o n s t h a tt h e y u s e , i n e c e s s a r y t a v o i d c o n f l i c t s .

S e c o n d c o n s i d e r a b l e b e n e f i t s m a y b o b t a i n e d b m a k i n g u s o t h c o m p r e h e n s i v e r o u t i n e s w h i c h a r i n c l u d e d i t h R O M .C o n s i d e r a b l e e x p e r t i s e w a s u s e d i t h w r i t i n g o t h e s e r o u t i n e sa n i t i s u n l i k e l y t h a t w e o r d i n a r y f o l k a r g o i n g t i n v e n tb e t t e r ' w h e e l 1 , n o t j u s t y e a n y w a y .

L a s t , t h e r e w i l l b m a n y o c c a s i o n s w h e r e t h c o m p u t e r p r o g r a m m e rw i l l w i s h t c o n v e r t o t h C o m m o d o r e 6 p r o g r a m s w h i c h w e r eo r i g i n a l l y w r i t t e n f o o t h e r m i c r o c o m p u t e r s i t h C o m m o d o r e r a n g e .U n f o r t u n a t e l y , t h u s m a d e o R A a n t h d i s p o s i t i o n o t h v a r i o u s r o u t i n e s i R O M h a s c h a n g e d a s t h e d i f f e r e n t m a c h i n e s h a v eb e e n d e v e l o p e d , a n d s o m e k n o w l e d g e o f t h e s e c h a n g e s i r e q u i r e d .M a n y o t h p r o g r a m s w r i t t e n f o t h P E c o m p u t e r r a n g e ,p a r t i c u l a r l y t h e 4 0 c o l u m n s c r e e n m a c h i n e s , a r p o t e n t i a l t a r g e t sf o c o n v e r s i o n . T h e V I C 2 s h a r e s m a n y c h a r a c t e r i s t i c s o t h C o m m o d o r e 6 a n d , p r o v i d e d t h d i f f e r e n t s c r e e n w i d t h c a b a l l o w e df o r , t h e r e i a m p l e s c o p e f o r c o n v e r s i o n h e r e a l s o .

T h i n t r e p i d t r a v e l l e r w i t h o u t m a i l i k e l y t g e t l o s t . T h em e m o r y m a p s w h i c h f o l l o w s h o u l d g s o m e w a t a v o i d i n g t h d r e a d e d j o u r n e y u p t h a t w e l l k n o w n c r e e k . I a d d i t i o n t p r o v i d i n g

c o m p r e h e n s i v e g u i d e t t h C 6 R O M , h o w e v e r , t h e y a l s o p r o v i d ee s s e n t i a l c r o s s r e f e r e n c e s t o t h e r C o m m o d o r e m i c r o s : t h V I 2 0 a n dP E T r a n g e u s i n g e i t h e r B A S I C o B A S I C 4 I t h t a b l e s t h e s er e f e r e n c e s a r i d e n t i f i e d a s V I C , B 2 o B r e s p e c t i v e l y . A e n t r yo i n t h e t a b l e m e a n s t h a t n o m e a n i n g f u l c o u n t e r p a r t e x i s t s .

Y J h e r e v e r p o s s i b l e , h a v e u s e d t h l a b e l s w h i c h a r u s e d b C o m m o d o r e f o t h C - 6 4 . I c a s e s w h e r e i h a s n o t b e e n p o s s i b l et d i s c o v e r t h ' g e n u i n e ' l a b e l s , h a v e a d o p t e d t h l a b e l s c r e a t e db M i k e T o d d o f t h I n d e p e n d e n t C o m m o d o r e P r o d u c t s U s e r s G r o u p( I C P U G ) . W h e r e a l l e l s e h a f a i l e d , h o w e v e r , h a v e i n v e n t e d m o w n l a b e l s .

Page 206: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 206/243

M e m o r y M a p sL a b e l 6 4

H e V I

D e m i H e xB 2 / B 4H e x D e s c r i p t i o n

D 6 5 1 0 0 0 0 0R 6 5 1 0 0 0 0 1

0 0 0 2A D R A Y 1 0 0 0 3 - 0 0 0 4A D R A Y 2 0 0 0 5 - 0 0 0 6

C H A R A C 0 0 0 7

E N D C H R 0 0 0 8I N T E G R 0 0 0 7 - 0 0 0 8T R M P O S 0 0 0 9V E R C K 0 0 0 AC O U N T 0 0 0 BD I M F L G 0 0 0 CV A L T Y P O O O DI N T F L G O O O E

G A R B F L O O O F

S U B F L G 0 0 1 0

I N P F L G 0 0 1 1

T A N S G NC H A N N LL I N N U MT E M P P TL A S T P TT E M P S TI N D E XI N D E X 1I N D E X 2R E S H O

T X T T A BV A R T A BA R Y T A BS T R E N DF R E T O P

0 0 1 20 0 1 30 0 1 4 - 0 0 1 50 0 1 60 0 1 7 - 0 0 1 80 0 1 9 - 0 0 2 10 0 2 2 - 0 0 2 50 0 2 2 - 0 0 2 30 0 2 4 - 0 0 2 50 0 2 6 - 0 0 2 A

0 0 2 B - 0 0 2 C0 0 2 D - 0 0 2 E0 0 2 F - 0 0 3 00 0 3 1 - 0 0 3 20 0 3 3 - 0 0 3 4

— 6 5 1 0 O n - c h i p D a t a D i r e c t i o n R e g i s t e r .— 6 5 1 0 O n - c h i p 8 - b i t I n p u t / O u t p u t r e g i s t e r .

U n u s e d .0 0 0 3 J u m p v e c t o r : C o n v e r t F A t i n t e g e r i ( A / Y ) .0 0 0 5 - J u m p v e c t o r : C o n v e r t i n t e g e r i ( A / Y ) t o

f l o a t i n g p o i n t i ( F A C ) .0 0 0 7 0 0 0 3 S e a r c h c h a r a c t e r t e m p o r a r y i n t e g e r d u r i n g

I N T .0 0 0 8 0 0 0 4 F l a g : S c a n f o Q u o t e a e n d o f s t r i n g .0 0 0 7 0 0 0 3 T e m p o r a r y i n t e g e r d u r i n g O R / A N D .0 0 0 9 S c r e e n c o l u m n f r o m l a s t T A B .

1 0 0 0 A 0 0 9 D F l a g : l o a d , v e r i f y .1 0 0 0 B 0 0 0 5 I n p u t b u f f e r p o i n t e r / N u m b e r o s u b s c r i p t s .1 0 0 0 C 0 0 0 6 F l a g : D e f a u l t A r r a y d i m e n s i o n .1 O O O D 0 0 0 7 D a t a t y p e f l a g : $ 0 n u m e r i c , $ F s t r i n g .1 O O O E 0 0 0 8 D a t a t y p e f l a g : $ 0 0 f l o a t i n g p o i n t , $ 8 0

i n t e g e r .1 O O O F 0 0 0 9 F l a g : D A T A s c a n L i s t q u o t e G a r b a g e

c o l l e c t i o n •1 0 0 1 0 0 0 0 A F l a g : S u b s c r i p t r e f e r e n c e U s e r f u n c t i o n

c a l l .1 0 0 1 1 0 0 0 B I n p u t f l a g : $ 0 I N P U T , $ 4 G E T , $ 9 8

R E A D .1 0 0 1 2 0 0 0 C F l a g : T A N s i g n C o m p a r i t i v e r e s u l t .1 0 0 1 3 O O O E F i l e n u m b e r o c u r r e n t i n p u t d e v i c e .2 0 0 1 4 0 0 1 1 T e m p o r a r y : I n t e g e r v a l u e .2 0 0 1 6 0 0 1 3 P o i n t e r : T e m p o r a r y s t r i n g s t a c k .2 0 0 1 7 0 0 1 4 L a s t t e m p o r a r y s t r i n g a d d r e s s .2 0 0 1 9 0 0 1 6 S t a c k f o t e m p o r a r y s t r i n g s .3 0 0 2 2 0 0 1 F U t i l i t y p o i n t e r a r e a .3 0 0 2 2 0 0 1 F F i r s t u t i l i t y p o i n t e r .3 0 0 2 4 0 0 2 1 S e c o n d u t i l i t y p o i n t e r .3 0 0 2 6 0 0 2 3 F l o a t i n g p o i n t p r o d u c t o m u l t i p l y a n

d i v i d e .4 0 0 2 B 0 0 2 8 P o i n t e r : S t a r t o B A S I C t e x t a r e a .4 5 0 0 2 D 0 0 2 A P o i n t e r : S t a r t o B A S I C v a r i a b l e s .4 0 0 2 D 0 0 2 C P o i n t e r : S t a r t o B A S I C a r r a y s .4 0 0 3 1 0 0 2 E P o i n t e r : E n d o B A S I C a r r a y s + 1 5 0 0 3 3 0 0 3 0 P o i n t e r : B o t t o m o s t r i n g s p a c e .

Page 207: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 207/243

L a b e lH e

- 6 V I D e m i H e

B 2 / B 4H e D e s c r i p t i o n

V A R N A M 0 0 4 5 - 0 0 4 6

V A R P N T 0 0 4 7 - 0 0 4 8

F O R P N T 0 0 4 9 - 0 0 4 AV A R T X T 0 0 4 B - 0 0 4 C

O P M A S K 0 0 4 DT E M P F 3 0 0 4 E - 0 0 5 2F 0 U R 6 0 0 5 3

J M P E R 0 0 5 4 - 0 0 5 6

T E M P F 1 0 0 5 7 - 0 0 5 BT E M P F 2 0 0 5 C - 0 0 6 0F A 0 0 6 1 - 0 0 6 6F A C E X P 0 0 6 1F A C H O 0 0 6 2 - 0 0 6 5F A C S G N 0 0 6 6S G N F L G 0 0 6 7B I T S 0 0 6 8

A F A C 0 0 6 9 - 0 0 6 EA R G E X P 0 0 6 9A R G H O 0 0 6 A - 0 0 6 DA R G S G N 0 0 6 EA R I S G N 0 0 6 FF A C O V 0 0 7 0F B U F P T 0 0 7 1 - 0 0 7 2

C H R G E TC H R G O TT X T P T RR N D XS T A T U SS T K E YS V X TV E R C K KC 3 P 0B S O U R

0 0 7 3 - 0 0 8 A0 0 7 90 0 7 A - 0 0 7 B0 0 8 B - 0 0 8 F0 0 9 00 0 9 10 0 9 20 0 9 30 0 9 40 0 9 5

6 0 0 4 5 0 0 4 2 N a m e o v a r i a b l e b e i n g s o u g h t i v a r i a b lt a b l e .

7 0 0 4 7 0 0 4 4 P o i n t e r : t o v a l u e o f ( V A R N A M ) i i n t e g e rd e s c r i p t o r i s t r i n g .

7 0 0 4 9 0 0 4 6 P o i n t e r : I n d e x v a r i a b l e f o F O R / N E X T l o o7 5 0 0 4 B 0 0 4 8 T e m p o r a r y s t o r a g e f o T X T P T R d u r i n g R E A D

I N P U T a n d G E T .7 0 0 4 D 0 0 4 A M a s k u s e d d u r i n F R M E V L .7 0 0 4 E 0 0 4 B T e m p o r a r y s t o r a g e f o F L P T v a l u e .8 3 0 0 5 3 0 0 5 0 L e n g t h o s t r i n g v a r i a b l e d u r i n g g a r b a g e

c o l l e c t i o n .8 4 0 0 5 4 0 0 5 1 J u m p v e c t o r u s e d i f u n c t i o n e v a l u a t i o n

( $ 4 C ) f o l l o w e d b a d d r e s s .8 0 0 5 7 0 0 5 4 T e m p o r a r y s t o r a g e f o F L P T v a l u e .9 0 0 5 C 0 0 5 9 T e m p o r a r y s t o r a g e f o F L P T v a l u e .9 0 0 6 1 0 0 5 E M a i n f l o a t i n g p o i n t a c c u m u l a t o r .9 0 0 6 1 0 0 5 E F A e x p o n e n t .9 0 0 6 2 0 0 5 F F A m a n t i s s a .

1 0 0 0 6 6 0 0 6 3 F A C s i g n .1 0 0 0 6 7 0 0 6 4 P o i n t e r : s e r i e s e v a l u a t i o n c o n s t a n t .1 0 4 0 0 6 8 0 0 6 5 B i o v e r f l o w a r e a d u r i n g n o r m a l i s a t i o n

r o u t i n e .1 0 5 0 0 6 9 0 0 6 6 A u x i l i a r y f l o a t i n g p o i n t a c c u m u l a t o r .1 0 5 0 0 6 9 0 0 6 6 A F A C e x p o n e n t .1 0 0 0 6 A 0 0 6 7 A F A C m a n t i s s a .1 1 0 0 6 E 0 0 6 A A F A C s i g n .1 1 0 0 6 F 0 0 6 C S i g n o r e s u l t o a r i t h m e t i c e v a l u a t i o n .1 1 0 0 7 0 0 0 6 D F A C l o w - o r d e r r o u n d i n g .1 1 0 0 7 1 0 0 6 E P o i n t e r : u s e d d u r i n g C R U N C H A S C I I

c o n v e r s i o n .1 1 0 0 7 3 0 0 7 0 S u b r o u t i n e : G e n e x t b y t e o B A S I C t e x t .1 2 0 0 7 9 0 0 7 6 E n t r y t o g e t s a m e b y t e a g a i n .1 2 0 0 7 A 0 0 7 9 P o i n t e r : c u r r e n t b y t e o B A S I C t e x t .1 3 9 0 0 8 B 0 0 8 8 F l o a t i n g R N f u n c t i o n s e e d v a l u e .1 4 0 0 9 0 0 0 9 6 K e r n a l 1 / s t a t u s w o r d S T 1 4 0 0 9 1 0 0 9 B F l a g : $ 7 S T O P k e y ( 6 4 ) .1 4 0 0 9 2 0 0 9 C T i m i n g c o n s t a n t f o t a p e .1 4 0 0 9 3 0 0 9 D F l a g : l o a d , v e r i f y .1 4 0 0 9 4 0 0 A 0 F l a g : S e r i a l b u o u t p u t c h a r a c t e r b u f f 1 4 0 0 9 5 0 0 A 5 B u f f e r e d c h a r a c t e r f o s e r i a l b u s .

Page 208: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 208/243

L a b e l 6 V I H e x D e m i H e x

B 2 / B 4H e x D e s c r i p t i o n

P R T Y 0 0 9 BD P S W 0 0 9 CM S G F L G 0 0 9 D

F N M I D X 0 0 9 E

P T R 1 0 0 9 EP T R 2 0 0 9 FT i r C 0 0 A 0 - 0 0 A 2T S F C N T 0 0 A 3

T B T C N T 0 0 A 4

C N T D N 0 0 A 5B U F P N T 0 0 A 6

I N B I T 0 0 A 7

B I T C 1 0 0 A 8

R I N O N E 0 0 A 9

R I D A T A O O A A

R I P R T Y O O A B

S A E A C M > 0T A P E !B I T T S

O O A C - O O A DO O A E - O O A FO O B O - O O B 1O O B 2 - O O B 30 0 B 4

N X T B I T 0 0 B 5

R O D A T A 0 0 B 6

F N L E NL S F

0 0 8 70 0 B 80 0 B 9O O B A

1 5 0 0 9 B 0 0 B 1 P a r i t y o b y t e o u t p u t t t a p e .1 5 6 0 0 9 C 0 0 B 2 F l a g : b y t e r e c e i v e d f r o m t a p e .1 5 0 0 9 D F l a g : $ 0 0 p r o g r a m m o d e : s u p p r e s s e r r o r

m e s s a g e s , $ 4 k e r n a l e r r o r m e s s a g e s o n l y ,$ 8 d i r e c t m o d e : f u l l e r r o r m e s s a g e s .

1 5 0 0 9 E 0 0 B 4 I n d e x t c a s s e t t e f i l e n a m e H e a d e r I D f o rt a p e w r i t e .

1 5 8 0 0 9 E O O C O T a p e e r r o r l o g p a s s 1 1 5 0 0 9 F 0 0 C 1 T a p e e r r o r l o p a s s 2 1 6 0 0 A 0 0 0 8 D R e a l - t i m e j i f f y c l o c k .1 6 0 0 A 3 0 0 B 7 B i c o u n t e r t a p e r e a d o w r i t e S e r i a l b u s

E O f l a g .1 6 0 0 A 4 0 0 B 9 P u l s e c o u n t e r t a p e r e a d o w r i t e S e r i a l b

s h i f t c o u n t e r .1 6 0 0 A 5 O O B A T a p e s y n c h r o n i s i n g c o u n t d o w n .1 6 6 0 0 A 6 O O B B P o i n t e r : T a p e I / O b u f f e r .

O O B C " s e c o n d b u f f e r P E T .1 6 0 0 A 7 R S 2 3 2 t e m p o r a r y f o r e c e i v e d b i ( 6 4 / V I C )

T a p e t e m p o r a r y ( a l l ) .1 6 0 0 A 8 O O B E R S 2 3 2 i n p u t b i t c o u n t ( 6 4 / V I C ) / T a p e

t e m p o r a r y ( a l l ) .1 6 Q 0 A 9 O O B F R S 2 3 2 f l a g : S t a r t b i c h e c k ( 6 4 / V I C ) T a p e

t e m p o r a r y ( a l l ) .1 7 O O A A 0 0 C 2 R S 2 3 2 i n p u t b y t e b u f f e r ( 6 4 / V I C ) T a p e

t e m p o r a r y ( a l l ) .1 7 O O A B 0 0 C 3 R S 2 3 2 I n p u t p a r i t y ( 6 4 / V I C ) T a p e t e m p o r a r

( a l l ) .1 7 O O A C 0 0 C 7 P o i n t e r : T a p e b u f f e r S c r e e n s c r o l l i n g .1 7 O O A E 0 0 C 9 T a p e e n d a d d r e s s e s / E n d o f p r o g r a m .1 7 O O B O O O C B T a p e t i m i n g c o n s t a n t s .1 7 0 0 B 2 0 0 D 6 P o i n t e r : S t a r t a d d r e s s o f t a p e b u f f e r .1 8 0 0 B 4 O O C E R S 2 3 2 w r i t e b i c o u n t ( 6 4 / V I C ) / T a p e r e a d

t i m i n g f l a g ( a l l ) .1 8 0 0 B 5 O O C F R S 2 3 2 n e x t b i t s e n d ( 6 4 / V I C ) / t a p e r e a d

e n o t a p e ( a l l ) .1 8 0 0 B 6 0 0 D 0 R S 2 3 2 o u t p u t b y t e b u f f e r ( 6 4 / V I C ) t a p e r e

e r r o r f l a g ( a l l ) .1 8 0 0 B 7 0 0 0 1 N u m b e r o c h a r a c t e r s i f i l e n a m e .1 8 0 0 B 8 0 0 D 2 C u r r e n t f i l e l o g i c a l a d d r e s s ( n u m b e r ) .1 8 5 0 0 B 9 0 0 0 3 C u r r e n t f i l e s e c o n d a r y a d d r e s s .1 8 O O B A 0 0 0 4 C u r r e n t f i l e f i r s t a d d r e s s .

Page 209: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 209/243

L a b e l

L S T X

N D

R V N D

L X S P

S F D XK E Y V A L

B L N S W

B L N C TG D B L NB L N O NC R S W

P N P N T R

Q T S WL N M X

T B L XS C H A R

I N S R TL O T B 1U S E RK E Y T A BR I B U FR O B U FF R E K 2 PB A S Z P TA S C W R K

B A S T A C K

6 H e

0 0 C 5

0 0 C 6

0 0 C 70 X

0 0 C 9 - 0 0 C A

O O C B

o o c c

O O C DO O C EO O C FO O D O

0 0 D 1 - 0 0 D 20 0 D 3

0 0 D 40 0 D 5

0 0 D 60 0 D 7

0 0 D 80 0 D 9 - 0 0 F 20 0 F 3 - 0 0 F 4O 0 F 5 - O O F 60 0 F 7 - 0 0 F 8O O 9 - O O FO O F B - O O F EO O F FQ 0 F F - 1 0 A

0 1 0 0 - 0 1 3 E0 1 0 0 - 0 1 F F

D e m i

1 9

1 9

1 9 2 0

2 0

2 0

2 0

2 0 2 0 2 0 2 0

2 0 2 1

2 1 2 1

2 1 2 1

2 1 2 1 2 4 2 4 2 4 2 4 2 5 2 5 2 5

2 5 2 5

V I H e

0 0 C 5

0 0 C 6

0 0 C 70 0 C 8

0 0 C 9

_

O O C B

O O C C

O O C DO O C EO O C FO O D O

0 0 D 10 0 D 3

0 0 D 40 0 D 5

0 0 D 60 0 D 7

0 0 D 80 0 D 9O O O O F 5O O 0 0 F 9O O F BO O F FO O F F

0 1 0 00 1 0 0

B 2 / BH e

0 0 9 7

0 0 9 E

0 0 9 F0 0 A 1

0 0 A 3

_

0 0 A 6

0 0 A 7

0 0 A 80 0 A 90 0 A AO O A C

0 0 C 40 0 C 6

O O C D0 0 D 5

0 0 D 80 0 D 9

O O D CO O E O

O O F F

0 1 0 00 1 0 0

D e s c r i p t i o n

M a t r i x v a l u e o f l a s t k e y p r e s s e d ; n k e $ 4 ( 6 4 / V I C ) , $ F ( P E T ) .N u m b e r o c h a r a c t e r s i k e y b o a r d b u f f e rq u e u e .F l a g : R e v e r s e - o $ 0 1 , o f $ 0 0 .P o i n t e r : E n o f l i n e f o i n p u t ( u s e d t s u p p r e s s t r a i l i n g s p a c e s ) .C u r s o r X - ( l i n e - c o l u m n ) p o s i t i o n a s t a i n p u t .F l a g : P r i n t s h i f t e d c h a r a c t e r s .M a t r i x v a l u e o f k e y p r e s s e d d u r i n g l a s tk e y b o a r d s c a n .F l a g : C u r s o r b l i n k $ 0 e n a b l e d , $ 0 d i s a b l e d .T i m e r : C o u n t d o w n f o c u r s o r b l i n k t o g g l C h a r a c t e r u n d e r c u r s o r w h i l e c u r s o r i n v e F l a g : C u r s o r s t a t u s $ 0 o f f , $ 0 o F l a g : I n p u t f r o m s c r e e n $ 0 3 , o k e y b o a $ 0 0 .P o i n t e r : C u r r e n t s c r e e n l i n e a d d r e s s .C u r s o r c o l u m n o c u r r e n t l i n e , i n c l u d i n gw r a p - r o u n d l i n e ( s ) , i f a n y .F l a g : E d i t o r i q u o t e m o d e $ 0 n o t .C u r r e n t l o g i c a l l i n e l e n g t h : 3 o r 7 9( 6 4 / P E T ) ; 2 1 , 4 3 , 6 5 o 8 7 ( V I C ) .

C u r r e n t s c r e e n l i n e n u m b e r o f c u r s o r .S c r e e n v a l u e o f c u r r e n t i n p u t c h a r a c t e r / lc h a r a c t e r o u t p u t .C o u n t o n u m b e r o i n s e r t s o u t s t a n d i n g .S c r e e n l i n e l i n k t a b l e E d i t o r t e m p o r a r iP o i n t e r : C u r r e n t c o l o u r R A l o c a t i o n .V e c t o r : C u r r e n t k e y b o a r d d e c o d i n g t a b l e .R S 2 3 2 i n p u t b u f f e r p o i n t e r .R S 2 3 2 o u t p u t b u f f e r p o i n t e r .F r e e z e r o p a g e s p a c e f o u s e r p r o g r a m s .B A S I C t e m p o r a r y d a t a a r e a .A s s e m b l y a r e a f o f l o a t i n g p o i n t t o A S C I Ic o n v e r s i o n .T a p e i n p u t e r r o r l o g .6 5 1 0 h a r d w a r e s t a c k a r e a .

Page 210: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 210/243

L a b e l 6 4 V I CH e x D e m i H e

B 2 / B 4H e D e s c r i p t i o n

K E Y D 0 2 7 7 - 0 2 8 0M E M S T R 0 2 8 1 - 0 2 8 2

M E M S I Z 0 2 8 3 - 0 2 8 4T I M O U T 0 2 8 5

C O L O R 0 2 8 6G D C O L 0 2 8 7H I B A S E 0 2 8 8X M A X 0 2 8 9R P T F L G 0 2 8 A

K O U N T 0 2 8 BD E L A Y 0 2 8 CS H F L A G 0 2 8 D

L S T S H F 0 2 8 EK E Y L O G 0 2 8 F - 0 2 9 0

M O D E 0 2 9 1

A U T O D N 0 2 9 2M 5 1 C T R 0 2 9 3M 5 1 C D R 0 2 9 4M 5 1 A J B 0 2 9 5 - 0 2 9 6

R S S T A T 0 2 9 7B I T N U M 0 2 9 8B A U D O F 0 2 9 9 - 0 2 9 AR I D B E 0 2 9 BR I D B S 0 2 9 C

R O D B S 0 2 9 D

R O D B E 0 2 9 EI R Q T M P 0 2 9 F - 0 2 A O

6 3 0 2 7 7 0 2 6 F K e y b o a r d b u f f e r q u e u e ( F I F O ) .6 4 0 2 8 1 P o i n t e r : b o t t o m o m e m o r y f o o p e r a t i n g

s y s t e m .6 4 3 0 2 8 3 P o i n t e r : t o p o f m e m o r y f o o p e r a t i n g s y s t e m6 4 0 2 8 5 S e r i a l I E E E b u s t i m e o u t d e f e a t f l a g ( n o t u s

b y V I C ) .6 4 0 2 8 6 C u r r e n t c h a r a c t e r c o l o u r c o d e .6 4 0 2 8 7 B a c k g r o u n d c o l o u r u n d e r c u r s o r .6 4 0 2 8 8 H i g h b y t e o s c r e e n m e m o r y a d d r e s s .6 4 0 2 8 9 M a x i m u m n u m b e r o f b y t e s i k e y b o a r d b u f f e r .6 5 0 2 8 A F l a g : R e p e a t k e y s $ 0 c u r s o r s , I N S T / D E L

s p a c e r e p e a t , $ 4 n k e y s r e p e a t , $ 8 0 a k e y s r e p e a t .

6 5 0 2 8 B R e p e a t k e s p e e d c o u n t e r .6 5 0 2 8 C R e p e a t k e f i r s t r e p e a t d e l a y c o u n t e r .6 5 0 2 8 D 0 0 9 8 F l a g : S h i f t k e $ 0 n o n e , $ 0 S H I F T ,

$ 0 C B M , $ 0 C T R L ( n o t e s h i f t s a r ea d d i t i v e i . e . $ 0 S H I F T , C B M & C T R L ) .

6 5 0 2 8 E L a s t s h i f t k e y u s e d f o d e b o u n c i n g .6 5 0 2 8 F V e c t o r : R o u t i n e t d e t e r m i n e k e y b o a r d t a b l e

t u s e b a s e d o s h i f t k e p a t t e r n , $ E B 4 8 f o 6 4 $ E B D C f o V I C .

6 5 0 2 9 1 F l a g : C a s e c h a n g e $ 0 d i s a b l e d , $ 8 0e n a b l e d .

6 5 0 2 9 2 F l a g : A u t o s c r o l l d o w n $ 0 d i s a b l e d .6 5 9 0 2 9 3 R S 2 3 2 P s e u d o 6 5 5 1 c o n t r o l r e g i s t e r i m a g e .6 6 0 0 2 9 4 R S 2 3 2 P s e u d o 6 5 5 1 c o m m a n d r e g i s t e r i m a g e .6 6 1 0 2 9 5 R S 2 3 2 n o n - s t a n d a r d b i t s / s e c o n d , n o t u s e d o

V I C .6 6 3 0 2 9 7 R S 2 3 2 P s e u d o 6 5 5 1 s t a t u s r e g i s t e r i m a g e .6 6 4 0 2 9 8 R S 2 3 2 n u m b e r o b i t s l e f t t o s e n d .6 6 5 0 2 9 9 R S 2 3 2 b a u d r a t e f u l l b i t t i m e m i c r o s e c o n d 6 6 0 2 9 B R S 2 3 2 i n d e x t o e n o f i n p u t b u f f e r .6 6 8 0 2 9 C R S 2 3 2 p o i n t e r : H i g h b y t e o a d d r e s s o f i n p u

b u f f e r .6 6 0 2 9 D R S 2 3 2 p o i n t e r : H i g h b y t e o a d d r e s s o f o u t p

b u f f e r .6 7 0 2 9 E R S 2 3 2 i n d e x t e n d o f o u t p u t b u f f e r .6 7 1 0 2 9 F T e m p o r a r y s t o r e f o I R Q v e c t o r d u r i n g t a p e

o p e r a t i o n s .

Page 211: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 211/243

Page 212: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 212/243

L a b e l 6 V I H e D e m i H e

B 2 / B 4H e D e s c r i p t i o n

I C L A L L 0 3 2 C - 0 3 2 D 8 1 0 3 2 C V e c t o r : I n d i r e c t e n t r y t k e r n a l C L A L Lr o u t i n e . N o r m a l c o n t e n t s $ F 3 2 F ( 6 4 ) .

U S R C M D 0 3 2 E - 0 3 2 F 8 1 0 3 2 E 0 3 F A U s e r d e f i n e d v e c t o r . N o r m a l c o n t e n t s $ F E 6 6( 6 4 ) i . a C B I N V .

I L O A D 0 3 3 0 - 0 3 3 1 8 1 0 3 3 0 V e c t o r : I n d i r e c t e n t r y t k e r n a l L O A Dr o u t i n e . N o r m a l c o n t e n t s $ F 4 A 5 ( 6 4 ) .

I S A V E 0 3 3 2 - 0 3 3 3 8 1 8 0 3 3 2 V e c t o r : I n d i r e c t e n t r y t k e r n a l S A V Er o u t i n e . N o r m a l c o n t e n t s $ F 5 E D ( 6 4 ) .

0 3 3 4 - 0 3 3 B 8 2 0 0 3 3 4 U n u s e d .T B U F F R 0 3 3 C - 0 3 F B 8 2 8 0 3 3 C 0 2 7 A T a p e 1 / b u f f e r .

0 3 F C - 0 3 F F 1 0 2 0 0 3 F C U n u s e d .V I C S C N 0 4 0 0 - 0 7 F 7 1 0 2 4 1 E 0 0 8 0 0 0 D e f a u l t s c r e e n v i d e o m a t r i x .S P N T R S 0 7 F 8 - 0 7 F F 2 0 4 0 — D e f a u l t S p r i t e d a t a p o i n t e r s .

0 8 0 0 - 9 F F F 2 0 4 8 0 4 0 0 0 4 0 08 0 0 0 - 9 F F F 3 2 7 6 8 A 0 0 0A O O O - B F F F 4 0 9 6 0 C 0 0 0 B 0 0 0C O O O - C F F F 4 9 1 5 2 — D O O O - D F F F 5 3 2 4 8 9 0 0 0 E 8 0 0

D 0 0 0 - D 0 2 E 5 3 2 4 8 9 0 0 0 E 8 8 0

D 4 0 0 - D 4 1 C 5 4 2 7 2 9 0 0 0

D 5 0 0 - D 7 F F 5 4 5 2 8D 8 0 0 - D B F F 5 5 2 9 6 9 4 0 0D C O O - D C O F 5 6 3 2 0 9 1 1 0 E 8 1 0

D D O Q - D D O F 5 6 5 7 6 9 1 2 0 E 8 2 0

E 8 4 0E O O O - F F F F 5 7 3 4 4 E 0 0 0 B 0 0 0E 0 0 0 - E 4 F F 5 7 3 4 4 E 0 0 0E 5 0 0 - F F F F 5 8 6 2 4 E 5 0 0 E 0 0 0

N o r m a l B A S I C p r o g r a m s p a c e .O p t i o n a l c a r t r i d g e R O M s p a c e .B A S I C R O M ( p a r t ) o 8 R A M .4 R A M .I n p u t / o u t p u t d e v i c e s a n d c o l o u r R A o 4 R A M .6 5 6 6 V i d e o I n t e r f a c e C h i p V I C - I I ( 6 4 ) ,6 5 6 1 V I C ( V I C ) , 6 8 4 5 C R T ( 8 0 - c o l P E T s ) .6 5 8 1 S o u n d I n t e r f a c e D e v i c e S I ( 6 4 ) , 6 5 6 1V I ( V I C ) .S I D i m a g e s .C o l o u r R A M ( n y b b l e s ) .6 5 2 6 C o m p l e x I n t e r f a c e A d a p t o r C I ( 6 4 ) ,6 5 2 2 V I ( V I C ) , 6 5 2 1 P I ( P E T ) .6 5 2 6 C o m p l e x I n t e r f a c e A d a p t o r C I A ( 6 4 ) ,6 5 2 2 V I ( V I C ) , 6 5 2 1 P I ( P E T ) .6 5 2 2 V I A ( P E T ) .B A S I C ( p a r t ) / K e r n a l R O o r 8 k R A M .B A S I C R O M o R A M .K e r n a l R O o R A M .

Page 213: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 213/243

B A S I C R O

L a b e l 6 V I B 2 B 4 D e s c r i p t i o n

B C O L DB W A R M

S T M D S PF U N D S PO P T A B

R E S L S TM S C L S TO P L I S TF U N L S TE R R T A BE R R P T RO K

F N D F O R

B L T U

B L T U C

G E T S T K

R E A S O N

O M E R RE R R O R

E R R F I NR E A D YM A I N

M A I N I

I N S L I N

A O O OA 0 0 2A 0 0 4A O O CA 0 5 2

0 8

A 0 9 EA 1 2 9A 1 4 0A 1 4 DA 1 9 EA 3 2 8A 3 6 4

A 3 8 A

A 3 B 8

A 3 B F

A 3 F B

A 4 0 8

A 4 3 5A 4 3 7 *

A 4 6 9A 4 7 4A 4 8 0 *

A 4 9 C

A 4 A 2

C O O OC 0 0 2C 0 0 4C O O CC 0 5 2C 0 8 0

C 0 9 EC 1 2 9C 1 4 0C 1 4 DC 1 9 EC 3 2 8C 3 6 4

C 3 8 A

C 3 B 8

C 3 B F

C 3 F B

C 4 0 8

C 4 3 5C 4 3 7 *

C 4 6 9C 4 7 4C 4 8 0 *

C 4 9 C

C 4 A 2

C O O OC 0 4 6C 0 7 4

C 0 9 2C U C 1 3 4C 1 4 1C 1 9 2

C 2 A A

C 2 D 8

C 2 D F

C 3 1 B

C 3 2 8

C 3 5 5C 3 5 7

C 3 7 AC 3 8 9C 3 9 2

C 3 A B

C 3 B 1

B O O OB 0 6 6B 0 9 4

B 0 B 2B 1 3 DB 1 6 1B 1 6 EB 2 0 D

B 3 2 2

B 3 5 0

B 3 5 7

B 3 9 3

B 3 A 0

B 3 C DB 3 C F

B 3 F 0B 3 F FB 4 0 6

B 4 1 F

B 4 7 0

F I N I C 5 2 A C 4 3 9 B 4 A D

B A S I C c o l d s t a r t v e c t o r .B A S I C w a r m s t a r t v e c t o r .A u t h o r s h i p n o t e : " C B M B A S I C "B A S I C C o m m a n d v e c t o r t a b l e .B A S I C F u n c t i o n v e c t o r t a b l e .B A S I C O p e r a t o r s v e c t o r p r i o r i t y t a b l e - b y t a d d r e s s & o n e p r i o r i t y .C o m m a n d k e y w o r d t a b l e .M i s c e l l a n e o u s k e y w o r d t a b l e .O p e r a t o r k e y w o r d t a b l e .F u n c t i o n k e y w o r d t a b l e .E r r o r m e s s a g e s .E r r o r m e s s a g e p o i n t e r s .N o n - e r r o r m e s s a g e s " O K " , " E R R O R " , " I N " , " R E A D Y" B R E A K " .

F i n d F O R e n t r y o s t a c k o s k i p t h e m t f i n d G O e n t r y w h e n c a l l e d b R E T U R N .M o v e b l o c k o m e m o r y u c h e c k s u f f i c i e n t m e m o t h e n . . M o v e b l o c k ( L O W T R ) t ( H I G H T R ) - l u t n e b l o c e n d i n g a ( H I G H D S ) - l .C h e c k s t a c k f o s p a c e t a c c o m m o d a t e ( A ) x 2 e n t r e r r o r " O U T O * € M O R Y " i n o t .C h e c k a d d r e s s ( A / Y ) i s l o w e r t h a n b o t t o m o s t r s p a c e i n o . . P r i n t " O U T O M E M O R Y " e r r o r m e s s a g e .[ * 0 3 0 0 t + 3 ] P r i n t e r r o r m e s s a g e i n d i c a t e d b t h e n . . P r i n t " E R R O R " o " B R E A K " ( i e n t e r e d f r o m S T P E N B A S I C r e s t a r t p r i n t " R E A D Y " t h e n . . [ * 0 3 0 2 t + 3 I n p u t l i n e i d e n t i f y B A S I C l i n ec o m m a n d •I B A S I C l i n e t h e n g e l i n e n u m b e r a n d c o n v e r tk e y w o r d s i n l i n e t o t o k e n s .I n s e r t t e x t f r o m B A S I C b u f f e r i n t o p r o g r a m l i n u m b e r i ( L I N I S B J M ) o e n t r y l i n e m u s t h a v ek e y w o r d s c h a n g e d t t o k e n s a n d l e n g t h o f l i n e i

i ( B B U F F ) 0 t h e n l i n e w i l l b d e l e t e dr o u t i n e e x i t s t o M A I N .A f t e r i n s e r t i n g n e w l i n e i n t o B A S I C t e x t d o R

Page 214: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 214/243

L a b e l 6 V I B B D e s c r i p t i o n

C R U N C H A 5 7 9 » C 5 7 9 * C 4 9 5 B 4 F B

F N D L I N A 6 1 3 C 6 1 3 C 5 2 C B 5 A 3

F N D L N C A 6 1 7 C 6 1 7 C 5 3 0 B 5 A 7

S C R A T HS C R T C H

R U N C

C L E A RC L E A R C

L D C L RS T X P T

L I S TQ P L O P

F O

N E W S T T

C K E O L

G O N EG O N ER E S T O R

S T O P

E N F I N I DS T P E N D

C O N T

R U

A 6 4 2A 6 4 4

A 6 5 9

A 6 3 EA 6 6 0

A 6 7 7A 6 8 E

A 6 9 CA 7 1 7 *

A 7 4 2

A 7 A E

A 7 C 4

A 7 E 1 *A 7 E DA 8 1 D

A 8 2 C

A 8 2 FA 8 3 4A 8 4 1

A 8 5 7

A 8 7 1

C 6 4 2C 6 4 4

C 6 5 9

C 6 5 EC 6 6 0

C 6 7 7C 6 8 E

C 6 9 CC 7 1 7 *

C 4 7 2

C 7 A E

C 7 C 4

C 7 E 1 *C 7 E DC 8 1 D

C 8 2 C

C 8 2 FC 8 3 4C 8 4 1

C 8 5 7

C 8 7 1

C 5 5 BC 5 5 D

C 5 7 2

C 5 7 7C 5 7 9

C 5 9 0C 5 A 7

C 5 B 5C 6 3 A

C 6 5 8

C 6 C 4

C 6 D A

C 6 F 7C 7 0 0C 7 3 0

C 7 3 F

C 7 4 1C 7 4 4C 7 5 1

C 7 6 B

C 7 8 5

B 3 D 2B 5 D 4

B 5 E 9

B 5 E EB 5 F C

B 6 O BB 6 2 2

B 6 3 0B 6 B 5

B 6 D E

B 7 4 A

B 7 F 5

B 7 7 CB 7 8 5B 7 B 7

B 7 C 6

B 7 C 8B 7 C BB 7 D 8

B 7 E E

B 8 0 8

[ ♦ 0 3 0 4 t - 1 - 3 ] C h a n g e k e y w o r d s t t o k e n s l i n e i B B U F F s e t ( T X T P T R ) t o B B U F F ( Y t l i n e l e n g t ha n d ( T X T P T R ) t B B U F F - 1 o e x i t .S e a r c h B A S I C t e x t f r o m s t a r t f o l i n e n u m b e r i ( L I N N U M ) . . o r . . .S e a r c h B A S I C t e x t f r o m ( A / X ) f o r l i n e n u m b e r i ( L I N N U M ) i f o u n d : s e C a n d ( L I N P T R ) p o i n t s t s t a r t o f l i n e e l s e c l e a r C N E W e n t e r s h e r e c h e c k s y n t a x t h e n . . R e s e t f i r s t b y t e o f t e x t t 0 0 s e ( V A R T A B ) t o( T X T T A B ) + 2 t h e n . . R e s e t e x e c u t i o n t o s t a r t o f p r o g r a m ( S T X P T R ) a n t h e n C L E A R C .C L R e n t e r s h e r e c h e c k s y n t a x t h e n . . S e ( F R E T O P ) t ( M E M S I Z ) a b o r t I / O s e t ( A R Y T A B )t o ( V A R T A B ) t h e n . . D R E S T O R r e s e t ( T E M P P T ) r e s e t s t a c k .S e t ( T X T P T R ) t ( T X T T A B ) - l t r e s e t e x e c u t i o n t s t a r t o f p r o g r a m .E n t r y p o i n t f o L I S T c o m m a n d .[ ♦ 0 3 0 6 t + 3 H a n d l e L I S T c h a r a c t e r - i n o n - t o k e no t o k e n i q u o t e s p r i n t i t e l s e e x p a n d t o k e n a n p r i n tE n t r y p o i n t f o F O s a v e s ( T X T P T R ) , ( C U R L I N ) a n df i n a l v a l u e o n s t a c k t h e n . . C h e c k f o S T O P k e t h e n h a n d l e n e x t B A S I C s t a t e m e n tf r o m t e x t .C h e c k e n o f l i n e i s a l s o e n o f t e x t - e l s e g e n e x t l i n e p a r a m e t e r s .[ ♦ 0 3 0 8 t + 3 E x e c u t e s t a t e m e n t w i t h i n l i n e .I n t e r p r e t B A S I C c o m m a n d a n d e x e c u t e i t E n t r y p o i n t f o R E S T O R E c o m m a n d r e s e t ( D A T P T R ) t s t a r t o B A S I C .E n t r y p o i n t f o S T O P c o m m a n d c l e a r c a r r y ( f o r' B R E A K 1 m e s s a g e t h e n j u m p i n t o E N D r o u t i n e .

E n t r y p o i n t f o E N s e c a r r y t h e n . . . .I n o d i r e c t - s a v e ( T X T P T R ) i ( O L D T X T ) t h e n . . .S a v e ( C U R L I N ) i ( O L D L I N ) a n d e x i t t R E A D Y ( i fc a r r y s e E N D ) o E R R F I N ( i c a r r y c l e a r S T O P ) .E n t r y p o i n t f o C O N T r e s t o r e ( T X T P T R ) a n d ( C U R L I N )u n l e s s ( O L D T X T ) i z e r o ( C A N ' T C O N T I N U E ) .E n t r y p o i n t f o r R U N - d C L R t h e n G O T O .

Page 215: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 215/243

L a b e l 6 4 V I C B 2 B 4 D e s c r i p t i o n

R E T U R N A 8 D 2 C 8 D 2 C 7 O A B 8 5 DR T A 8 D 4 C 8 D 4 C 7 D C B 8 5 F

R T N O G S A 8 E 0 C 8 E 0 C 7 E 8 B 8 6 BN O S T M T A 8 E 3 C 8 E 3 C 7 E A B 8 6 ED A T A A 9 F 8 C 8 F 8 C 8 0 0 B 8 8 3

D A T A N A 9 0 6 C 9 0 6 C 8 0 E B 8 9 1

R E M N A 9 0 9 C 9 0 9 C 8 0 E B 8 9 1

S E R C H X A 9 0 B C 9 0 B C 8 1 1 B 8 9 4

I A 9 2 B C 9 2 B C 8 3 0 B 8 B 3

R E A 9 3 B C 9 3 B C 8 4 3 B 8 C 6

D O C O N D A 9 4 0 C 9 4 0 C 8 4 8 B 8 C B

O N G O T O A 9 4 B C 9 4 B C 8 5 3 B 8 D 6

L I N G E T A 9 6 B C 9 6 B C 8 7 3 B 8 F 6

L E A 9 A 5 C 9 A 5 C 8 A D B 9 3 0

P T F L P TP U T I N T

P U T T I M

G E T S P T

P R I N T N

C H

S T R D O N

P R I N T

A 9 C 4A 9 D 6

A 9 E 3

A A 2 C

A A 8 0

A A 8 6

A A 9 A

A A A O

C 9 C 4C 9 D 6

C 9 E 3

C A 2 C

C A 8 0

C A 8 6

C A 9 A

C A A O

C 8 C CC 8 D E

C 8 E B

C 9 3 7

C 9 8 B

C 9 9 1

C 9 A 5

C 9 A B

B 9 4 FB 9 6 1

B 9 7 2

B 9 B A

B A 8 8

B A S E

B A A 2

B A A 8

E n t r y p o i n t f o R E T U R N c h e c k s y n t a x t h e n . . C l e a r s t a c k u p t o f i r s t G O S U B e n t r y t h e n s e ( T X T P T R ) a n d ( C U R L I N ) f r o m s t a c k .

D i s p l a y " R E T U R N W I T H O U T G O S U B " t h e n e x i t t o M A I N .D i s p l a y " U N D E F I N E D S T A T E M E N T M t h e n e x i t t o M A I N .E n t r y p o i n t f o D A T A s c a n t e x t f o e n d o fs t a t e m e n t u p d a t e ( T X T P T R ) t i g n o r e .S e t s c a n f o c o l o n s t a t e m e n t d e l i m i t e r t h e n d S E R C H X . . S e s c a n f o r z e r o b y t e ( e n d o f s t a t e m e n t ) t h e n d S E R C H X .S e a r c h t e x t f o ( X o z e r o b y t e e x i t w i t h ( Y t o n u m b e r o f b y t e s t d e l i m i t e r .E n t r y p o i n t f o I e v a l u a t e e x p r e s s i o n , p e r f o r R E i z e r o ( F A L S E ) .E n t r y f o R E s c a n f o z e r o b y t e a n u p d a t e( T X T P T R ) .I I c o n d i t i o n n o n z e r o ( T R U E ) t h e n d c o m m a n dG O T O a a p p r o p r i a t e .E n t r y p o i n t f o r O N - g e n u m b e r f r o m t e x t a n d s c f o l i n e n u m b e r - d G O T O o G O S U B .R e a d i n t e g e r f r o m t e x t i n t o ( L I N N U M ) - e r r o r i v a l u e n o i r a n g e 0 - 6 3 9 9 9 .E n t r y p o i n t f o r L E T f i n d t a r g e t v a r i a b l e i v a r i a b l e s p a c e a n s e ( F O R P N T ) t p o i n t a i e v a l u a t e e x p r e s s i o n t h e n P U T I N T , P T F L P T , P U T T I MG E T S P T a a p p r o p r i a t e .P u ( F A C ) i n t o v a r i a b l e p o i n t e d t b y ( F O R P N T ) .P u t i n t e g e r i ( F A C + 3 ) i n t o v a r i a b l e p o i n t e d t ( F O R P N T ) .S e T I f r o m s t r i n g s e ( I N D E X 1 ) t p o i n t t s t r i n g a n ( A t o s i x ( s t r i n g l e n g t h ) .P u s t r i n g d e s c r i p t o r p o i n t e d t o b y ( F A C + 3 ) i n t os t r i n g v a r i a b l e p o i n t e d t o b y ( F O R P N T ) .E n t r y p o i n t f o P R I N T £ d C M D t h e n r e s t o r e d e f I / ( u n l i a t e n I E E E i d e v i c e n u m b e r 3 ) E n t r y p o i n t f o C M - s e t C W o u t p u t d e v i c e f r o mt a b l e t h e n c a l l P R I N T .P r i n t r o u t i n e p r i n t s t r i n g a n d c h e c k f o e n o p r i n t s t a t e m e n t .E n t r y p o i n t f o r P R I N T - i d e n t i f y P R I N T p a r a m e t e r ( S P C , T A B e t c ) - e v a l u a t e e x p r e s s i o n .

Page 216: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 216/243

L a b e l 6 V I B 2 B 4 D e s c r i p t i o n

S T R O U T

S T R P R T

O U T S T RO U T S P CP R T S P CO U T S K PO U T Q S TO U T D OT R M N O KG E

I N P U T N

I N P U T

B U F F U L

Q I N L I NR E A D

I N P C O N

I N P C O 1

R D G E TR D I N PD A T L O P

N E X T

D O N E X T

F R M N U M

C H K N U M

A B I E

A B 2 1

A B 2 4A B 3 BA B 3 FA B 4 2A B 4 5A B 4 7A B 4 DA B 7 B

A B A 5

A B B F

A B E A

A B F 9A 0

A C O D

A O

A 3 A C 4 3A C B 8

A C F CA D O CA I

A 6

A D 8 A

A D 8 D

C B 1 E

C B 2 1

C B 2 4C B 3 BC B 3 FC B 4 2C B 4 5C B 4 7C B 4 DC B 7 B

C B A 5

C B B F

C B A E

C B F 9C C 0 6

C C O D

C C O F

C C 3 5C C 4 3C C B 8

C C F CC D O CC I

C D 6 1

C D 8 A

C D 8 D

C A 1 C

C A 1 F

C A 2 2C A 3 9C A 3 DC A 4 0C A 4 3C A 4 5C A 4 FC A 7 D

C A A 7

C A C 1

C A E D

C A F AC B 0 7

C B O E

C B 1 0

C B 3 6C B 4 4C B B 9

C B F CC C O DC C 2 0

C C 6 2

C C 8 B

C C 8 E

B B 1 D

B B 2 0

B B 2 3B B 3 AB B 3 EB B 4 1B B 4 4B B 4 6B B 4 CB B 7 A

B B A 4

B B B E

B B E 8

B B F 5B C 0 2

B C 0 9

B C O B

B C 3 1B C 3 FB C B 4

B C F 7B D 0 7B D 1 9

B D 5 B

B D 8 4

B D 8 7

P r i n t s t r i n g p o i n t e d t b ( A / Y ) u n t i l z e r o b y t eq u o t e ( " f o u n d , ( s t r i n g 2 5 b y t e s ) .P r i n t s t r i n g p o i n t e d t b ( F A C + 3 ) u n t i l z e r o b y o q u o t e ( " f o u n d ( s t r i n g 2 5 6 b y t e s ) .P r i n t s t r i n g p o i n t e d t b ( I N D E X 1 ) o l e n g t h ( A P r i n t s p a c e ( c u r s o r r i g h t i f t o s c r e e n ) .P r i n t s p a c e a l w a y s .P r i n t c u r s o r r i g h t a l w a y s .P r i n t q u e s t i o n m a r k .P r i n t ( A ) .H a n d l e e r r o r m e s s a g e s f o r G E T , I N P U T a n d R E A D .E n t r y p o i n t f o G E c h e c k n o t d i r e c t ( i l l e g a l ) ,i d e n t i f y G E T £ , g e o n c h a r a c t e r .E n t r y p o i n t f o I N P U T £ s e i n p u t d e v i c e , d o i n t h e n u n l i s t e n I E E E i d e v i c e > 3 E n t r y p o i n t f o I N P U T o u t p u t p r o m p t m e s s a g e i a n y , d i n p u t .R e a d i n p u t - i f ( B B U F F ) z e r o ( n i n p u t s t r i n g )V I C / 6 4 s k i p , B A S I C 2 / a b o r t .P r i n t a n d i n p u t d a t a i n t o B B U F F b u f f e r .E n t r y p o i n t f o R E A D s e R E A D f l a g ( 9 8 ) i I N P s e ( X / Y ) D A T P T R .E n t r y p o i n t t R E A D f o I N P U T - s e t I N P U T f l a g ( i I N P F L G , . s e t ( X / Y ) B U F .E n t r y p o i n t t R E A D f o r G E T s e t G E T f l a g ( 4 0 )I N P F L G , s e ( X / Y ) B U F .P a r t o R E A D r o u t i n e w h i c h G E T s b y t e .P a r t o R E A D r o u t i n e w h i c h I N P U T s , u s e s R D G E T .P a r t o R E A D r o u t i n e w h i c h R E A D s D A T A v a l u e s , u s R D G E T .A S C I I s t r i n g " 7 E X T R A I G N O R E D < C R > " .A S C I I s t r i n g " 7 R E D 0 F R O M S T A R T < C R > " .E n t r y p o i n t f o N E X T g e N E X T ' s v a r i a b l e a n c o n f i r m t h a t c o r r e s p o n d i n g F O i s o n s t a c k ,c a l c u l a t e n e x t l o o p v a r i a b l e v a l u e .I l o o p c o u n t e r v a l i d , s e t s ( C U R L I N ) a n d ( T X T P T R )f r o m s t a c k a n r e e n t e r s F O R l o o p .E v a l u a t e n u m e r i c e x p r e s s i o n f r o m B A S I C t e x t , e n t F R M E V L ( s e e b e l o w ) t h e n e n t e r s C H K N U M .T e s t s V A L T Y P f o n u m e r i c r e s u l t f r o m F R M E V L ( s e eb e l o w ) , e x i t s t R E A D Y w i t h " T Y P E M I S M A T C H E R R O R "s t r i n g f o u n d .

Page 217: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 217/243

L a b e l 6 V I B B 4 D e s c r i p t i o n

F R * € V L A D 9 E C O 9 E C C 9 F B D 9 8

E V A L

Q D O TP A R C H K

C H K C L S

C H K O P N

C H K C O M

S Y N C H R

S Y N E R R

D O M I NR S V V A R

A E 8 3

A E A 8

A E A DA E F 1

A E F 7

A E F A

A E F D

A E F F

A F 0 8

A F O Df i F l b

C E 8 3

C E A 8

C E A DC E F 1

C E F 7

C E F A

C E F D

C E F F

C F 0 8

C F O DC F 1 4

C D 8 4

C O A 3

C D A BC D E C

C D F 2

C D

C D F 8

C D F A

C E O 3

C E 0 8

B 8

B E A O

B E B E E 9

B E E F

B E F 2

B E F 5

B E F 7

B F O O

B 5 0 5_ _

I S V A R A F 2 8 C F 2 8 C E O F B F 0 4

T I S A S C

I S F U N

A F 4 8

A F A 7

C F 4 8

C F A 7

C E 2 E

C E 8 9

B F A D

C 0 3 7

I n p u t a n e v a l u a t e a n e x p r e s s i o n i B A S I C t e x t .S e t s V A L T Y P ( 0 0 i n u m e r i c , F F i f s t r i n g ) a n I N ( 0 i f l o a t i n g p o i n t , 8 0 i f i n t e g e r ) .I e x p r e s s i o n i n u m e r i c f l o a t i n g p o i n t , r e s u l tr e t u r n e d i F A C . I e x p r e s s i o n i n u m e r i c i n t e g r e s u l t i r e t u r n e d i ( F A C + 3 ) i H I / L O f o r m a t .e x p r e s s i o n i s t r i n g , t h e n p o i n t e r t t h s t r i d e s c r i p t o r i r e t u r n e d i n ( F A C + 3 ) , t h i s i u s u a l c o p y o V A R P N T . I n a d d i t i o n , i e x p r e s s i o n i s i m p l e v a r i a b l e , t h e n V A R N A M w i l l b e s e t t o p o i n t h e f i r s t b y t e o f t h e n a m e . F i n a l l y , i a e r r o f o u n d i t h e x p r e s s i o n t h e n e x i t s t R E A D Y w i t h" S Y N T A X E R R O R " .E v a l u a t e s i n g l e t e r m i e x p r e s s i o n . I d e n t i f yf u n c t i o n s , p i , T I , T I e t c .F l o a t i n g p o i n t v a l u e o p i t L P f o r m a t3 . 1 4 1 5 9 6 5 .E v a l u a t e n o n - v a r i a b l e t e r m i e x p r e s s i o n .E v a l u a t e e x p r e s s i o n w i t h i n p a r e n t h e s i s i e x p r e s s i o n .C h e c k t h a t c h a r a c t e r p o i n t e d t b ( T X T P T R ) i s r i g h t p a r e n t h e s i s . I n o " S Y N T A X E R R O R " .C h e c k t h a t c h a r a c t e r p o i n t e d t b ( T X T P T R ) i l e f t p a r e n t h e s i s . I n o " S Y N T A X E R R O R " .C h e c k t h a t c h a r a c t e r p o i n t e d t b ( T X T P T R ) i c o m m a . I f n o t " S Y N T A X E R R O R " .C h e c k t h a t c h a r a c t e r p o i n t e d t b ( T X T P T R ) i t s a m e a c o n t a i n e d i t h e a c c u m u l a t o r . I n o " S Y N T A X E R R O R " .P r i n t e r r o r m e s s a g e " S Y N T A X E R R O R " a n r e t u r n t B A S I C R E A D Y .C r e a t e m o n a d i c m i n u s o r N O T f o u s i e v a l u a t i o S e t c a r r y i v a r i a b l e p o i n t e d t b ( F A C + 3 ) i ' r e s e r v e d ' i . e . S T T I T I $ .

F i n d v a r i a b l e n a m e d i B A S I C t e x t . S e t ( V A R N A M )p o i n t a n a m e i t a b l e s i f o u n d . P l a c e n u m e r i cv a l u e s i F A C , s t r i n g p o i n t e r i ( F A C + 3 ) .C o n v e r t T t A S C I I s t r i n g a n d s e ( F A C + 3 ) t p ot s t r i n g .E v a l u a t e f u n c t i o n . R e t u r n n u m e r i c v a l u e i F A C ,s t r i n g v a l u e a p o i n t e r i ( F A C + 3 ) .

Page 218: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 218/243

L a b e l 6 V I C B 2 B 4 D e s c r i p t i o n

D R O P A F E 6 C F E 6 C E C 8 C 0 8 6 P e r f o r m O R S e O f l a g a n u s A N D O P t e v a l u a t e .A N O O P A F E 9 C F E 9 C E C B C 0 8 9 P e r f o r m A N D . S e t A N D f l a g t h e n c o n v e r t f l o a t i n g

p o i n t v a l u e s t o f i x e d p o i n t , d A N ( o O i O f l a g s e t ) t h e n c o n v e r t b a c k t f l o a t i n g p o i n t .

D O R E L B 0 1 6 D 0 1 6 C E F 8 C 0 B 6 P e r f o r m r e l a t i o n s o = I n u m e r i c e x p r e s s i o nu s e s N U M R E L , s t r i n g e x p r e s s i o n u s e s S T R R E L .

N U K K E L B 0 1 B D O 1 B C E F D C 0 B 8 P e r f o r m n u m e r i c c o m p a r i s o n .S T R R E L B 0 2 E D O 2 E C F 1 O C O C E P e r f o r m s t r i n g c o m p a r i s o n .D I M B 0 8 1 D 0 8 1 C F 6 3 C 1 2 1 P e r f o r m D I M .P T R G E T B 0 8 B D O 8 B C F 6 D C 1 2 B I d e n t i f y v a r i a b l e n a m e d i B A S I C t e x t a n d p l a c e

n a m e , n o p o i n t e r t o n a m e , i ( V A R N A M ) .O R D V A R B 0 E 7 D 0 E 7 C F C 9 C 1 8 7 F i n d v a r i a b l e w h o s e n a m e i i ( V A R N A M ) a n d s e t

( V A R P N T ) t p o i n t a t i t . I n e c e s s a r y , u s N O T F N St c r e a t e n e w v a r i a b l e .

I S L E T C B 1 1 3 D 1 1 3 C F F 7 C 1 B 6 S e c a r r y i c h a r a c t e r i a c c u m u l a t o r i l e t t e r .N O T F N S B 1 1 D D U D 0 0 1 C 1 C O C r e a t e n e v a r i a b l e w i t h n a m e a i ( V A R N A M ) , u n l e

P T R G E T c a l l e d b y I S V A R .N O T E V L B 1 2 8 D 1 2 8 D O O C C 1 C B C r e a t e n e w v a r i a b l e w i t h n a m e a s i ( V A R N A M ) a n d s

( V A R P N T ) t p o i n t a i t F M A P T R B 1 9 4 D 1 9 4 D 0 7 8 C 2 C 8 S e ( A R Y P N T ) t s t a r t o a r r a y a n d p l a c e n u m b e r o

a r r a y d i m e n s i o n s i C O U N T .B 1 A 5 D 1 A 5 D 0 8 9 C 2 D 9 F l o a t i n g p o i n t v a l u e o f 3 2 7 6 8 ( 1 0 0 0 0 1 6 ) F L P T

f o r m a t •F A C I N X B 1 A A D 1 A A C o n v e r t ( F A C ) t o i n t e g e r i n ( A / Y ) .I N T I D X B 1 B 2 D 1 B 2 D 0 8 D C 2 D O E v a l u a t e e x p r e s s i o n i B A S I C t e x t a p o s i t i v e

i n t e g e r ( 0 t o 3 2 7 6 7 ) a n d p l a c e r e s u l t i ( F A C + 1 ) .A Y I N T B 1 B F D 1 B F D 0 9 A C 2 E A C h e c k t h a t F L P T v a l u e i ( F A C ) i w i t h i n i n t e g e r

r a n g e ( - 3 2 7 6 8 t o 3 2 7 6 7 ) t h e n c o n v e r t t f o u r b y t ei n t e g e r i F A C + 1 t h r o u g h F A C + 4 .

I S A R Y B 1 D 1 D 1 D 1 D O A C C 2 F C G e a r r a y p a r a m e t e r s f r o m B A S I C t e x t a n d p u s ho n t a c k .

F N D A R Y B 2 1 8 D 2 1 8 D 0 F 3 C 3 4 3 F i n d a r r a y n a m e i ( V A R N A M ) , p a r a m e t e r s r e a d b I S A R Y .

B D S B S C B 2 4 5 D 2 4 5 D 0 2 0 C 3 7 0 D i s p l a y " B A D S U B S C R I P T 1 1 t h e n e x i t t M A I N .I L L Q N T B 2 4 8 D 2 4 8 D 0 2 3 C 3 7 3 D i s p l a y " I L L E G A L Q U A N T I T Y " t h e n e x i t t M A I N .N O T F D D B 2 6 1 D 2 6 1 D 1 3 C C 3 8 C C r e a t e a r r a y f r o m p a r a m e t e r s o s t a c k .I N L P N 2 B 3 0 E D 3 0 E D 1 E A C 4 3 9 S e ( V A R P N T ) t o p o i n t a e l e m e n t w i t h i n a r r a y .U M U L T B 3 4 C D 3 4 C D 2 2 8 C 4 7 7 C o m p u t e n u m b e r o f b y t e s i s u b s c r i p t ( Y o a r r a y

s t a r t i n g a ( V A R P N T ) .F R B 3 7 D D 3 7 D D 2 5 9 C 4 A 8 E n t r y p o i n t f o F R f u n c t i o n - d g a r b a g e c o l l e c t i o

a n s e f u n c t i o n v a l u e t ( F R E T O P ) ( S T R E N D ) .G I V A Y F B 3 9 1 D 3 9 1 D 2 6 D C 4 B C C o n v e r t i n t e g e r i n ( A / Y ) t o F L P T i ( F A C ) w i t h i n

Page 219: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 219/243

L a b e l 6 V I B B D e s c r i p t i o n

D E G E T F N M

B 3 B 3B 3 E 1

D 3 B 3D 3 E 1

D 2 8 DD 2 B B

C 4 0 CC 5 O A

F N D O E R B 3 F 4 D 3 F 4 D 2 C E C 5 1 D

S E T F N V B 4 2 3 D 4 2 3 D 2 F D C 5 4 C

S T R D B 4 6 5 D 4 6 5 D 3 3 F C 5 8 E

S T R I N I B 4 7 5 D 4 7 5 D 3 4 F C 5 9 E

S T R U T B 4 8 7 D 4 8 7 D 3 6 1 C 5 B 0

P U T N W 1

G E T S P A

G A R B A 2

D V A R S

G R B P A S

C A

M O V I N S

F R E S T RF R E F A C

F R E T M SC H R DL E F T DR I G H T DM I D DP R E A M

L E

B 4 O 5

B 4 F 4

B 5 2 6

B 5 B D

B 6 0 6

B 6 3 D

B 6 7 A

B 6 A 3B 6 A 6

B 6 D BB 6 E CB 7 0 0B 7 2 CB 7 3 7B 7 6 1

B 7 7 C

D 4 O 5

D 4 F 4

D 5 2 6

D 5 B D

D 6 0 6

D 6 3 D

D 6 7 A

D 6 A 3D 6 A 6

D 6 D 6O 6 E CD 7 0 0D 7 2 CD 7 3 7D 7 6 1

D 7 7 C

D 3 A F

D 3 C E

D 4 0 0

D 4 9 7

D 4 0 E

D 5 1 7

D 5 5 4

D 5 7 DD 5 8 O

D 5 B 5D 5 C 6D 5 D AD 6 0 0D 6 1 1D 6 3 B

D 6 3 6

C 6 1 D

C 6 6 A

C 7 4 F

C 7 8 C

C 7 B 5

C 8 1 1C 8 2 2C 8 3 6C 8 6 2C 8 6 DC 8 9 7

C 8 B 2

E n t r y p o i n t f o D E - c r e a t e F f u n c t i o n .C h e c k s y n t a x o F N a n d l o c a t e F d e s c r i p t o r a n ( D E F P N T ) t p o i n t a i t E n t r y p o i n t f o F f u n c t i o n - g e t F d e s c r i p t o r

S e t ( T X T P T R ) t o s t a r t a F i t e x t , e v a l u a t ee x p r e s s i o n , r e s e t ( T X T P T R ) .E n t r y p o i n t f o S T R $ f u n c t i o n , e v a l u a t e e x p r e s sa n c o n v e r t t A S C I I s t r i n g .C r e a t e s p a c e f o s t r i n g w h o s e d e s c r i p t o r i i ( F A C + 3 ) a n l e n g t h i n ( A ) , e x i t w i t h n e w d e s c r i i ( D S C T F P ) a n d p o i n t e r t o o l d d e s c r i p t o r i ( D S C P N T ) .S c a n s t r i n g s t a r t i n g a ( A / Y ) a n c r e a t e d e s c r ie x i t w i t h ( F A C + 3 ) p o i n t i n g t d e s c r i p t o r . S t r ie x p e c t e d t e n w i t h n u l l b y t e o " S e d e s c r i p t o r o d e s c r i p t o r s t a c k a n u p d a t ep o i n t e r .S e ( F R E T O P ) a n d ( F R E S P C ) f o n e s t r i n g w h o s el e n g t h i i ( A ) .D g a r b a g e c o l l e c t i o n c l o s e u s p a c e i s t r i ns p a c e u s e d b d i s c a r d e d s t r i n g s .S e a r c h v a r i a b l e a n a r r a y t a b l e s f o n e x t s t r i nd e s c r i p t o r t o b e s a v e d b g a r b a g e c o l l e c t i o n .M o v e s t r i n g u t o v e r w r i t e u n w a n t e d s t r i n g s i g a r b a g e c o l l e c t i o n .C o n c a t e n a t e t w o s t r i n g s i e x p r e s s i o n t h e n c o n tt e v a l u a t e e x p r e s s i o n .T r a n s f e r s t r i n g w h o s e d e s c r i p t o r i p o i n t e d t ( S T R N G 1 ) .C o n f i r m s t r i n g m o d e t h e n . . P e r f o r m s t r i n g h o u s e k e e p i n g , e n t e r w i t h p o i n t e rs t r i n g d e s c r i p t o r i ( F A C + 3 ) a n d e x i t w i t h l e n ( A a n ( I N D E X 1 ) p o i n t i n g t o s t a r t o f s t r i n g .U p d a t e s t r i n g d e s c r i p t o r s t a c k p o i n t e r .E n t r y p o i n t f o C H R $ f u n c t i o n .E n t r y p o i n t f o L E F T S f u n c t i o n .E n t r y p o i n t f o R I G H T ! f u n c t i o n .E n t r y p o i n t f o M D f u n c t i o n .P u l l f r o m s t a c k s t r i n g d e s c r i p t o r p o i n t e r , s t o ( D S C P N T ) , p u l l s t r i n g p a r a m e t e r t ( A ) .

E n t r y p o i n t f o L E f u n c t i o n .

Page 220: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 220/243

L a b e l 6 V I B B 4 D e s c r i p t i o n

V A

S T R V A L

G E T N U M

G E T A D R

P E E KP O K EW A I TF A D D HF S U B

F S U B TF A D D 5F A D D

F A D D TO V E R RM U L S H F

L O

F M U L T

F M U L T T

M L T P L YC O N U P KM U L D I V

B 7 A D

B 7 B 5B 7 E B

B 7 F 7

B 8 0 DB 8 2 4B 8 2 DB 8 4 9B 8 5 0

B 8 5 3B 8 6 2B 8 6 7

B 8 6 AB 9 7 EB 9 8 3B 9 B CB 9 C 1

B 9 E A

B A 2 8

B A 3 O

B A 5 9B A 8 CB A B 7

D 7 A D

D 7 B 5D 7 E B

D 7 F 7

D 8 0 D0 8 2 4D 8 2 DD 8 4 9D 8 5 0

D 8 5 3D 8 6 2D 8 6 7

D 8 6 AD 9 7 ED 9 8 3D 9 B CD 9 C 1

D 9 E A

D A 2 8

D A 3 0

D 5 D A 8 CD A B 7

D 6 8 7

D 6 8 F

D 6 C 6

D 6 D 2

D 6 E BD 7 0 7D 7 1 0D 7 2 CD 7 3 3

D 7 3 6D 7 6 ED 7 7 3

D 7 7 6D 8 8 AD 8 8 FD 8 C 8D 8 C D

D 8 F 6

D 9 3 4

D 9 3 4

D 9 6 5D 9 9 8D 9 C 3

C 8 E 3

C 8 F 5C 9 2 1

C 9 2 D

C 9 4 3C 9 5 AC 9 6 3C 9 7 FC 9 8 6

C 9 8 9C 9 9 8C 9 9 D

C 9 A 0C A B 4C A B 9C A F 2C A F 7

C B 2 0

C B 5 E

C B 5 E

C B 8 FC B C 2C B E D

M L D V E X B A D 4 D A D 4 D 9 E 0 C C O A

M U L 1 0T E N CD I V 1 0F D I V F

F D I V

B A E 2B A F 9B A F EB B 0 7

B B O F

D A E 2D A F 9D A F ED B 0 7

D B O F

D 9 E ED A 0 5D A O AD A

D A 1 B

C C 1 8C C 2 FC C 3 4C C 3 D

C C 4 5

E n t r y p o i n t f o r V A L f u n c t i o n . C o n f i r m a r g u m e n t i s t r i n g t h e n . . C o n v e r t s t r i n g s t a r t i n g a ( I N D E X 1 ) o l e n g t h ( A t F L P T v a l u e i ( F A C ) .R e a d p a r a m e t e r s f r o m B A S I C t e x t f o P O K E o W A I T ,s t o r e 1 s i n t e g e r i ( L I N N U M ) , 2 n d i n t e g e r i ( X ) .C o n v e r t F L P T v a l u e i n ( F A C ) t o a u n s i g n e d i n t e g e r( t 6 5 5 3 5 ) a n d p l a c e r e s u l t i ( L I N N U M ) a n d ( Y / A ) .E n t r y p o i n t f o P E E K .E n t r y p o i n t f o P O K E .E n t r y p o i n t f o W A I T .A d d 0 . 5 t o ( F A C ) .F l o a t i n g p o i n t s u b t r a c t i o n : ( F A C ) M F L P T a ( A / Y )( F A C ) .E n t r y p o i n t f o s u b t r a c t i o n : ( F A C ) ( A R G ) ( F A C ) .P a r t o a d d i t i o n n o r m a l i s a t i o n r o u t i n e .F l o a t i n g p o i n t a d d i t i o n : ( F A C ) M F L P T a ( A / Y )( F A C ) .E n t r y p o i n t f o a d d i t i o n : ( F A C ) ( A R G ) ( F A C ) .O u t p u t " O V E R F L O W E R R O R " m e s s a g e .M u l t i p l y b y b y t e .C o n s t a n t 1 . i M F L P T f o r m a t .V a r i o u s c o n s t a n t s u s e d f o s e r i e s e v a l u a t i o n o f u n c t i o n s .P e r f o r m L O f u n c t i o n c h e c k a r g u m e n t i p o s i t i v e ,t h e n s e r i e s e v a l u a t i o n o f L O GM u l t i p l y ( F A C ) b M P L P T p o i n t e d t b ( A / Y ) , a n s w e ri F A C .P e r f o r m f l o a t i n g p o i n t m u l t i p l y r o u t i n e . M u l t i p l y( F A C ) b ( A F A C ) , a n s w e r i F A C .

M u l t i p l y ( F A C ) b a b y t e , r e s u l t t R E S H O .L o a d A 7 A C w i t h f - f L P T v a l u e p o i n t e d t b ( A / Y ) .M u l t i p l i c a t i o n s u b r o u t i n e t t e s t ( F A C ) a n ( A F A C )f o u n d e r f l o w / o v e r f l o w .H a n d l e o v e r f l o w ( " 7 0 V E R F L 0 W E R R O R " ) o u n d e r f l o we r r o r ( z e r o F A C ) .M u l t i p l y ( F A C ) b 1 0 a n s w e r i F A C .C o n s t a n t 1 i f f L P T f o r m a t .D i v i d e ( F A C ) b 1 0 a n s w e r i F A C .D i v i d e ( A F A C ) b F f L P T v a l u e p o i n t e d a b ( A / Y )( s i g n i n X ) , a n s w e r t o F A C .D i v i d e ( A F A C ) b y F C L P T p o i n t e d t o b ( A / Y ) , a n s w e r

Page 221: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 221/243

Page 222: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 222/243

Page 223: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 223/243

L a b e l 6 V I B B 4 D e s c r i p t i o n

O C P A R A

C O S I T A P I T W O P IF R

A T

B A S S F T

E 2 1 9

E 2 6 4E 2 6 BE 2 B 4E 2 E 0E 2 E 5E 2 E AE 2 E FE 3 0 EE 3 3 EE 3 7 B

E 2 1 6

E 2 6 1E 2 6 8E 2 B 1E 2 D DE 2 E 2E 2 E 7E 2 E CE 3 O BE 3 3 BE 4 6 7

F 4 C E

D F D 8D F D FE 0 2 8E 0 5 4E 0 5 9E O 5 EE 0 6 3E 0 8 CE O B C

F 5 O D

D 2 8 2D 2 8 9D 2 D 2D 2 F ED 3 O 3D 3 0 8D 3 0 DD 3 2 CD 3 5 C

I N I T VI N I T

I N I T N V

I N I T A T

R N O S E O

I N I T C Z

E 4 5 3E 3 9 4

E 3 9 7

E 3 A 2E 3 B A

E 3 B F

E 4 5 BE 3 7 8

E 3 7 B

E 3 8 7E 3 9 F

E 3 A 4

E 1 1 6

E 0 F 9

E l l l

D 3 B 6

D 3 9 9D 3 B 1

W O R D SF R E M E SB V T R SI N I T V

E 4 6 0E 4 7 3E 4 4 7E 4 5 3

E 4 2 9E 4 3 6E 4 4 FE 4 5 B

E 1 B 7E 1 C 4

D 4 4 BD 4 3 8

F e t c h p a r a m e t e r s f r o m B A S I C t e x t f o O P E N a n r o u t i n e s s e d e f a u l t s .E v a l u a t e C O S f u n c t i o n a d p i / 2 t ( F A C ) t h eE v a l u a t e S I f u n c t i o n .E v a l u a t e T A f u n c t i o n b c o m p u t i n g S I N / C O S .M F L P T c o n s t a n t p i / 2 .M F L P T c o n s t a n t 2 » p i .M F L P T c o n s t a n t 0 . 2 5 .* F L P T c o n s t a n t s f o S I f u n c t i o n e v a l u a t i o n .E v a l u a t e A T N f u n c t i o n .^ L P T c o n s t a n t s f o A T f u n c t i o n e v a l u a t i o n .B A S I C w a r m r e s t a r t r o u t i n e c a l l e d b B R E A K i i n s t r u c t i o n e n c o u n t e r e d o S T O P / R E S T O R E p r e s sC l o s e s c h a n n e l s , r e s t o r e s d e f a u l t I / O , r e s e t sa n e x i t s t h r o u g h I E R R O R w i t h ( X $ 8 0 .C o p i e s B V T R S t R A b l o c k 0 I n i t i a l i s e B A S I C o r e s e t ( c o l d s t a r t ) i c a l l I N I T V t s e B A S I C v e c t o r s i $ 0 3 0 0 $ t h e n . .C a l l I N I T C Z t s e u B A S I C v a r i a b l e i b l o c kR A M , ( P E T s d d e s t r u c t i v e R A t e s t a b o v e $ 0 4 6 4 / V I C d o n ' t ) , c a l l I N T M S , t h e n e x i t t B A S I C H R G E T r o u t i n e m a s t e r c o p y c o p i e d d o w n t p a g e b I N I T C Z .h f L P T c o n s t a n t 0 . 8 1 1 6 3 5 1 5 7 u s e d a s i n i t i a l s r a n d o m n u m b e r g e n e r a t i o n .I n i t i a l i s e B A S I C R A M s e U S R P O K , A D R A Y 1 , A c o p y I N I T A T a n R N D S E D t C H R G E T a n d R N D X ,T X T T A B a n F R E T O P t ( L O R A M ) , H I R A M ) , s e f b y t e i B A S I C t e x t a r e a t o z e r o .T e x t " B Y T E S F R E E " .T e x t " ? ? ? C O M M O D O R E B A S I C ? ? ? " e t c .R O M c o p i e s o B A S I C v e c t o r s .C o p i e s B V T R S t R A b l o c k 0

Page 224: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 224/243

K e r n a l R O M C B 6 O p e r a t i n g S y s t e m R o u t i n e s

L a b e l 6 V I C B 2 B 4 D e s c r i p t i o n

I O B A S K E 5 0 0 E 5 0 0

S C R O R G

P L O T K

E 5 O 5

E 5 0 A

E 5 O 5

E 5 0 A

C I N T

N X T DS T U P TV P A NP A N I CI N I T V

K B G E T

K B I N P

K S I N P

S C I N P

Q T S W C

S C P U TS C N P N T

C K D E C LC K I N LC K C O L RC O L T A BS C R O LC L R L ND S P PK E

E 5 1 8

E 5 6 6E 5 6 CE 5 9 AE 5 A 0E 5 A 8

E 5 B 4

E 5 C A

E 6 3 2

E 6 3 A

E 6 8 4

E 6 9 1E 7 1 6

E 8 A 1E 8 B 3E 8 C BE 8 D AE 8 E AE 9 F FE A 1 3E A 3 1 *

E 5 1 8

E 5 8 1E 5 8 7E 5 B 5E 5 B BE 5 C 3

E 5 C F

E 5 E 5

E 6 4 F

E 6 5 7

E 6 B 8

E 6 C 5E 7 4 2

E 8 E 8E 8 F AE 9 1 2E 9 2 1E 9 7 5E A 8 DE A A 1E A B F *

E 1 D E

E 2 5 7E 2 5 D

E 2 8 5

E 2 9 A

E 2 F 4

E 2 F F

E 3 3 F

E 3 4 CE 3 D 8

E 5 3 F

E 6 1 B *

E 6 0 F

E 0 5 FE 0 6 F

E O A 7

E O B C

E 1 1 6

E 1 2 1

E 1 6 A

E 1 7 7E 2 0 2

E 3 C 8E 3 9 6

E 4 4 2 *

R e t u r n s i ( X / Y ) t h a d d r e s s o 6 5 2 6 C o m p l e xI n t e r f a c e A d a p t o r ( C I A ) c h i p u s e d b y I R Q r o u t i n e s( a n d h e n c e k e y b o a r d r o u t i n e s ) . T h i s i s t h e I O B A S E

K e r n a l r o u t i n e .R e t u r n s s c r e e n o r g a n i s a t i o n c o l u m n s ( X ) , r o w s ( Y ) .E n t r y t h r o u g h S C R E E N K e r n a l v e c t o r .S e t s / r e t u r n s c u r s o r p o s i t i o n : s c r e e n r o t h r o u g h( X ) , c o l u m n t h r o u g h ( Y ) . S e t s c u r s o r i c a r r yc l e a r , r e t u r n s p o s i t i o n i c a r r y s e t o n e n t r y .E n t r y t h r o u g h P L O T K e r n a l v e c t o r .I n i t i a l i s e i n p u t / o u t p u t . T h i s r o u t i n e i c a l l e d b t h I O B A S E K e r n a l v e c t o r .H o m e s c r e e n , r e s e t s c r e e n l i n e l i n k t a b l e .M o v e c u r s o r t ( T B L X ) , ( P N T R ) .R e s e t d e f a u l t I / O , i n c l u d i n g V I C - I I c h i p r e g i s t e r s .R e s e t d e f a u l t I / O c h a n n e l s t h e n . . R e s t o r e d e f a u l t v a l u e s o f 6 5 6 7 ( V I C - I I ) c h i pr e g i s t e r s .G e c h a r a c t e r s f r o m k e y b o a r d b u f f e r . G E T I N c o m e sh e r e i D F L T N 0 I n p u t c h a r a c t e r ( a d i s t i n c t f r o m G E T ) . K S I N P c o m e sh e r e i C R S W = O .I n p u t c h a r a c t e r f r o m k e y b o a r d o s c r e e n . C H R I Nc o m e s h e r e i D F L T N 0 I n p u t c h a r a c t e r f r o m s c r e e n . K S I N P c o m e s h e r e i C R S W 3 T o g g l e q u o t e f l a g ( Q T S W ) . D u r i n g i n p u t s t o p st o k e n i s a t i o n o k e y w o r d s w i t h i n q u o t e s .P r i n t s ( A t s c r e e n . U s e d b S C N P N T .P r i n t s c h a r a c t e r t s c r e e n i n t e r p r e t s c u r s o rc o n t r o l s , c o l o u r c h a n g e s , c a s e c h a n g e s e t c .C h e c k f o d e c r e m e n t o f l i n e c o u n t e r .C h e c k f o i n c r e m e n t o f l i n e c o u n t e r .C h e c k c o l o u r .T a b l e o c o l o u r c o d e s .S c r e e n s c r o l l i n g r o u t i n e s .C l e a r t h e s c r e e n l i n e p o i n t e d t b ( x i e t 2 4 D i s p l a y ( A o s c r e e n .T h e m a i n I R i n t e r r u p t h a n d l i n g r o u t i n e . [ * C I N Vv e c t o r p o i n t s h e r e ] .

Page 225: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 225/243

L a b e l 6 V I H e x D e m i H e

B 2 / B 4H e D e s c r i p t i o n

T A L K K E . D 0 9 E E 1 4 F 0 B 6 F Q D 2

L I S T N K E D O C E E 1 7 F O B A F 0 D 5

S E C N D K E D B 9 E E C O

T K S A K E D C 7 E E C E

C I O U T K E D D D E E E 4 F 1 6 F F 1 9 E

U N T L K K E D E F E E F 6 F 1 7 F F 1 A E

U N L S N K

A C P T R K

R S T R A B

R S T B G NR S R C V R

K M S G T X

K M E S S G

N G E T I N

E D F E

E E 1 3

E E B B

E F 0 6E F 5 9

F O B D

F 1 2 B

F 1 3 E

E F 0 4

E F 1 9

E F A 3

E F E EF 0 3 6

F 1 7 4

F 1 E 2

F 1 F 5

F 1 8 3

F 1 8 C

F O O O

F 1 5 6

F 1 D 1

F 1 B 9

F 1 C O

F O O O

F 1 8 5

F 2 0 5

N B A S I N F 1 5 7 F 2 0 E F 1 E 1 F 2 1 5

N B S O U T F 1 C A F 2 7 A F 2 3 2 F 2 6 6

N C H K I N F 2 0 E F 2 C 7 F 7 B C F 7 F E

O R ( A t c o n v e r t d e v i c e n u m b e r t T A L K a d f o t h I E E E b u a n t r a n s m i t s t h i s a c o m m T h i s i t h e K e r n a l r o u t i n e p o i n t e d t o b y T A L K O R ( A t c o n v e r t d e v i c e n u m b e r t o L I S T E Nf o t h I E E E b u s a n d t r a n s m i t s t h i s a c o m m T h i s i t h K e r n a l r o u t i n e p o i n t e d t b L I S T C o n v e r t s ( A a n t r a n s m i t s i t a s a L I S T E N s e c a d d r e s s o t h I E E E b u s . T h i s i t h K e r n a la c c e s s e d b S E C O N D .C o n v e r t s ( A a n t r a n s m i t s i a T A L K s e c o n a d d r e s s o t h I E E E b u s . T h i s i t h k e r n e la c t i o n e d b T K S A .T r a n s m i t s b y t e o t t h I E E E b u s . T h c h a i b u f f e r e d s t h a t t h e ' h a n d s h a k i n g 1 c a n b ec a r r i e d o u t . T h i s i t h e C I O U T K e r n a l r o u t i n T r a n s m i t s a U N T A L K c o m m a n d o n t o t h I E E E b u T h i s i t h K e r n a l r o u t i n e w h i c h i a d d r e s s e dU N T A L K v e c t o r .T r a n s m i t s a U N L I S T E N c o m m a n d o t h I E E E b u s U N L S N K e r n a l v e c t o r c o m e s h e r e .

b y t e i s ' h a n d s h a k e n ' o f f t h e I E E E b u a n i A T h i s i t h A C P T R K e r n a l r o u t i n e .C o n t i n u a t i o n o f t h e m a i n N M i n t e r r u p t r o u t i n f o R S 2 3 2 d e v i c e s .O u t p u t s b y t e t t h R S 2 3 2 c h a n n e l ( 2 ) .P a r t o f t h N M i n t e r r u p t r o u t i n e w h i c h b u i l d i n d i v i d u a l b i t c o m i n g f r o m t h e R S 2 3 2 c h a n n e l b y t e .T e x t o t h K e r n a l e r r o r a n c o n t r o l m e s s a g e ss t o r e d h e r e .P r i n t K e r n a l m e s s a g e t o t h e s c r e e n i o u t p u te n a b l e d i . e . ( M S G F L G ) h a b i s e t .G e c h a r a c t e r f r o m c h a n n e l a n r e t u r n i A c h a r a c t e r h a s b e e n s e n t , r e t u r n 0 T h i s i t K e r n a l G E T I N r o u t i n e .I n p u t c h a r a c t e r f r o m b u f f e r i n t o A T h i s i C H R I N K e r n a l r o u t i n e .

O u t p u t t h b y t e i t t h o u t p u t c h a n n e l .t h C H R O ' U T K e r n a l r o u t i n e .A l l o c a t e s t h f i l e s p e c i f i e d b ( X ) a s t h e i n

Page 226: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 226/243

L a b e l 6 V I B B 4 D e s c r i p t i o n

N C L O S E F 2 9 1 F 3 4 A F 2 A C F 2 E 0

N C L A L L F 3 2 F F 3 E F F 2 6 E F 2 A 2

N C L R C H F 3 3 3 F 3 F 3 F 2 7 2 F 2 A 6

N O P E N F 3 4 A F 4 0 A F 5 2 4 F 5 6 3

L O A D S P F 4 9 E F 5 4 2 F 3 C C F 4 0 B

L I K I N GO U T F NL O O I N G

F 5 A FF 5 C 1F 5 D 2

F 6 4 7F 6 5 9F 6 6 A

S A V E S P F 5 D D F 6 7 5 F 6 A 4 F 6 E 3

S A V I N GU D T I M K

F 6 8 FF 6 9 B

F 7 2 8F 7 3 4 F 7 2 9 F 7 6 8

R D T I M K F 6 D O F 7 6 0

( A s p e c i f i e s t h e f i l e t b c l o s e d . T h d e t a i l sa r r e m o v e d f r o m t h e d e v i c e t a b l e s ( L A T , F A T a n dS A T ) . O u t p u t f i l e s a r e ' t i d i e d u p 1 . T h i s i t h C L O S E K e r n a l r o u t i n e .T h i s r o u t i n e a b o r t s a l c u r r e n t I / O . T h n u m b e r o o p e n f i l e s ( L N T N D ) i s s e t t o z e r o a n a n y I E E E f i l e sa r U N T A L K e d o U N L I S T E N e d . T h r o u t i n e d o e s n o c l o s e ' o u t p u t ' f i l e s t i d i l y , s m a o n l y b s a f e l yu s e d w i t h i n p u t f i l e s ( u s e C L O S E f o o u t p u t f i l e s ) .T h i s i t h C L A L L K e r n a l r o u t i n e .D e a l l o c a t e s t h i n p u t / o u t p u t c h a n n e l s a n r e s t o r e st h d e f a u l t d e v i c e s ( D F L T N = 0 D F L T O 3 ) T h i s i t h C L R C H N K e r n a l r o u t i n e .O p e n s t h e f i l e w h o s e s p e c i f i c a t i o n i s t o r e d i F N L E N , L A , F A , S a n F N A D R b i n s e r t i n g t h d e t a i l si t h e L A T , F A T a n d S A T t a b l e s a n c a r r y i n g o u ta p p r o p r i a t e p r o c e d u r e s f o f i l e s o t a p e o d i s k .T h i s i s t h e K e r n a l O P E N r o u t i n e .L o a d R A M ( i f A c o n t a i n s 0 o v e r i f y ( i c o n t a i n s1 t h e f i l e s p e c i f i e d b F N A D R , F N L E N f r o m d e v i c es p e c i f i e d b F A 6 4 / V I C w i l l , i f S A c o n t a i n s 0 l o a df i l e i n t o R A M s t a r t i n g a ( X / Y ) . O t h e r w i s e , t h ea d d r e s s s t o r e d w i t h t h e f i l e i u s e d . T h i s i t h L O A D K e r n a l r o u t i n e .S u b r o u t i n e t d i s p l a y " S E A R C H I N G F O R " t h e n . . . .D i s p l a y f i l e n a m e s p e c i f i e d b F N A D R , F N L E N .S u b r o u t i n e t d i s p l a y " L O A D I N G " o " V E R I F Y I N G "( d e p e n d i n g o A t h e n d o e s O U T F N .S a v e t h e R A s p e c i f i e d b y A , X a n d a f i l es p e c i f i e d b y F N A D R , F N L E N , o d e v i c e F A . I f S c o n t a i n s a f i l e s a v e d t o t a p e w i l l h a v e a n e n d o ft a p e m a r k e r w r i t t e n a f t e r p r o g r a m . 6 4 / V I C , i S c o n t a i n s o 3 p r o g r a m s a v e d t t a p e w i l la u t o m a t i c a l l y r e l o a d t m e m o r y f r o m w h e n c e i w a s a v e d .S u b r o u t i n e t d i s p l a y " S A V I N G " f i l e n a m e .P a r t o f t h I R i n t e r r u p t s e r v i c i n g r o u t i n e w h i c hu p d a t e s t h e r e a l t i m e j i f f y c l o c k . I n a d d i t i o n i s t o r e s t h c u r r e n t k e y b o a r d m a t r i x v a l u e i S T K E Y ,w h i c h e n a b l e s S T O P t f u n c t i o n . T h i s i t h U D T I MK e r n a l r o u t i n e .S u b r o u t i n e t r e a d j i f f y c l o c k i n t o ( A / X / Y ) . T h i s i

Page 227: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 227/243

L a b e l 6 V I B 2 B 4 D e s c r i p t i o n

K E R R O R F f l E B F 7 7 E F 3 1 5 F 3 4 9

F A F 7 2 C F 7 A FT C N T L F 8 0 O F 8 8 A

R E A D F 9 2 C F 9 8 AR D I 5 F A 7 0 F A B DW R I T E F B A 6 F B E AS T A R T F C E 2 F D 2 2

F 5 A 6 F 5 E 5F 8 0 6 F 8 4 B

F 8 5 5 F 8 9 AF 9 3 1 F 9 7 6F B 9 3 F B D 8F C D 1 F D 1 6

A O I N T F D 0 2 F D 3 F

R E S T R K F D 1 5 F D 5 2

V E C T R K F D 1 A F D 5 7

R A M T S K F D 5 0 F D 8 D

I O I N T K F D A 3 F D F 9

S E T N M K F D f 9 F E 4 9

S T L F S K F E O O F E 5 0

R E A D S S F E 0 7 F E 5 7

E r r o r s d e t e c t e d b y t h K e r n e l r o u t i n e s e n t e r tr o u t i n e t i s s u e t h a p p r o p r i a t e e r r o r m e s s a g e F i n d a n d r e a d t h e h e a d e r b l o c k o t a p e .T h t a p e c o n t r o l r o u t i n e s r e s i d e h e r e . T h e yu n d e r t a k e f u n c t i o n s s u c h a s w i t c h i n g c a s s e t t em o t o r s o n a n d o f f , t i m i n g e t c .T h e t a p e r e a d i n g r o u t i n e s .B y t e h a n d l i n g r o u t i n e f o t a p e r e a d i n g .T a p e w r i t i n g r o u t i n e s .C o l d s t a r t r o u t i n e n o r m a l l y a c c e s s e d w h e n t h i i n i t i a l l y s w i t c h e d o n I i t h e r o u t i n e w i p o i n t e d t o b y t h e h a r w a r e v e c t o r a $ F F F C .M e m o r y i i n i t i a l i s e d , a n a l i n p u t / o u t p u t d e a r s e u p T h f i r s t p a r t o t h 6 4 / V I C r o u tc h e c k s i c a r t r i d g e i l o a d e d i b l o c k ( 6 4( V I C ) a n i s o j u m p s t t h c a r t r i d g e f o i n i t i a l i s a t i o n .M E M C H K F E 9 1 — T h r o u t i n e o V I c h e c k s o w h a t m e m o r y e n h a n c e m e n t s a r e f i ta n d s e t s u t h e s c r e e n a n s t a r t o B A S I C t e x ta c c o r d i n g l y .S u b r o u t i n e c h e c k s f o c a r t r i d g e i n b l o c k ( 6 4 b l o c k ( V I C ) . R e t u r n s w i t h f l a g s e t i f c a r t r i f o u n d .R e s t o r e s K e r n a l i n d i r e c t v e c t o r s t s t a n d a r d . i t h K e r n a l R E S T O R r o u t i n e .L o a d s K e r n a l i n d i r e c t o r v e c t o r s w i t h u s e r ' s v a T h i s i t h K e r n a l V E C T O R r o u t i n e .S u b r o u t i n e s e t s z e r o p a g e u p a n d d o e sn o n - d e s t r u c t u r e t e s t o f w h o l e o R A M u n t i l c h e f a i l s . A d d r e s s o c h e c k f a i l u r e s t o r e d i M E M S u s i n g M E M T O P . T h i s i s t h e K e r n a l R A M T A S r o u t i n I n i t i a l i s e 6 5 2 6 C I A ' s ( 6 4 ) , 6 5 2 2 V I A ' s ( V I C ) o P I A ' s a n d 6 5 2 2 V I ( P E T ) t o s t a n d a r d v a l u e s . T t h e K e r n a l I O I N I T r o u t i n e .S t o r e s X , i F N A D R a n i F N L E N . T h i s i t h K e r n a l S E T N A N E r o u t i n e .S t o r e s A , a n i n ' L A , F A S A T h i s i t h K S E T L F S r o u t i n e .R e a d s s t a t u s f l a g i n t o A I F c o n t a i n s 2 r e R S S T A T ( R S 2 3 2 s t a t u s ) e l s e r e a d s S T A T U S . T h i s

Page 228: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 228/243

L a b e l 6 V I B B 4 D e s c r i p t i o n

t C M B T K F E 2 5 F E 7 3

N M F E 4 3 F E A 9

B A U D T B F E C 2 F F 5 CT 2 N M I F E D 6

P U L S F F 4 8 F F 7 2 E 6 1 B E 4 4 2

P C I N T F F 5 B

R L S N U M F F 8 0

K V C T R S F F 8 1 F F 8 AN M I V E C F F F A F F F AR S T V E C F F F C F F F CI R Q V E C F F F E F F F E

F F C O F F C OF F F A F F F AF F F C F F F CF F F E F F F E

R e a d s / s e t s ( X / Y ) f r o m / i n t o * £ M S T R d e p e n d i n g o n t h ec a r r y f l a g . T h i s i t h K e r n a l M E M B O T r o u t i n e .O n t h e 6 4 / V I C N M i n t e r r u p t s a r m a i n l y u s e d t h a n d l e R S 2 3 2 d e v i c e s . P E T ' 3 m a d e n o u s e o f N M I a a l l .B a u d r a t e t a b l e s f o t h 6 4 / V I C .S u b r o u t i n e t h a n d l e R S 2 3 2 b i i n p u t .

T h i s r o u t i n e i e n t e r e d w h e n a i n t e r r u p t o c c u r s .R e g i s t e r s a r s a v e d a n d t h e s o u r c e o f t h e i n t e r r u p ti d e t e r m i n d , I R o B R i n s t r u c t i o n . A p p r o p r i a t ea c t i o n s a r e t h e n t a k e n .P a t c h t C I N I T r o u t i n e w h i c h d e t e r m i n e s w h e t h e r P A o N T S C s t a n d a r d i b e i n g u s e d b c h e c k i n g n u m b e r o r a s t e r l i n e s .R e l e a s e / v e r s i o n n u m b e r s o K e r n a l o p e r a t i n g s y s t e m .I y o u r v a l u e d i f f e r s f r o m t h e n s o m e o t h a d d r e s s e s l i s t e d f o t h e 6 4 a b o v e m a b s l i g h t l yd i f f e r e n t .K e r n a l j u m p v e c t o r t a b l e .N M i n t e r r u p t v e c t o r a d d r e s s .I n i t i a l i s a t i o n v e c t o r a d d r e s s .I R i n t e r r u p t v e c t o r a d d r e s s .

Page 229: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 229/243

T h K e r n a l V e c t o r s E a s y A c c e s s t O p e r a t i n g S y s t e m R o u t i n e sT h p r u d e n t p r o g r a m m e r m a k e s a m u c h u s a p o s s i b l e o t h R O r o u t i n e s p r o v i d e d w i t h h i s / h e r c o m p u t e r ; a f t e r a l i o n w a n t s t r e - i n v e n t w h e e l s t h e n t h e r e a r e p l e n t y o h o b b i e s w h i c h s e e m t b d e s i g n e d t c a t e r f o t h i s . H o w e v e r , t h e r e i p r o b l e m w i t h t h i sa p p r o a c h . W h a t h a p p e n s t y o u r c l e v e r p r o g r a m s w h e n y o c h a n g ey o u r c o m p u t e r f o t h s u p e r n e C o m m o d o r e c o m p u t e r w i t h t h r e e t i m e st h p o w e r a q u a r t e r o f t h e p r i c e n e x t y e a r A l l t h o s e J S R ' s t t h R O M r o u t i n e s a r n o g o i n g t t h w r o n g a d d r e s s , a n m a s s i v er e - w r i t e i r e q u i r e d .

T h p u r p o s e o f t h e K e r n a l i s t o a v o i d t h i s p r o b l e m a n t m a k e i e a s i e r t w r i t e p r o g r a m s w h i c h w i l l t r a n s f e r . T h e K e r n a l i s i m p l y

' j u m p t a b l e 1 i t h R O w h i c h p r o v i d e s t h l i n k t t h r o u t i n e .T h a d d r e s s o t h j u m p s t a y s i n t h e s a m e p l a c e , a l t h o u g h t h r o u t i n e t w h i c h i j u m p s m a b m o v e d t d i f f e r e n t p l a c e s i t h n e w m a c h i n e s . A l t h p r o g r a m m e r h a t d i J S t t h a d d r e s sw h i c h g i v e s a c c e s s t t h r o u t i n e t h a t h e / s h e r e q u i r e s , t h i sa d d r e s s a l w a y s r e m a i n i n g t h e s a m e .

T h C o m m o d o r e 6 h a K e r n a l j u m p t a b l e p r o v i d i n g a c c e s s t 3 r o u t i n e s i t h o p e r a t i n g s y s t e m R O M . T h e s e r o u t i n e s h a v e b e e nc h o s e n a s t h e o n e s m o s t l i k e l y t b o f u s e t t h p r o g r a m m e r . I i p a r t o t h u n d e r s t a n d i n g t h a t y o u h a v e w i t h C o m m o d o r e , t h a tt h e s e K e r n a l e n t r i e s w i l l b p r e s e r v e d o a n y u p g r a d e s o t h o p e r a t i n g s y s t e m o c o m p u t e r . T h e C o m m o d o r e 6 K e r n a l i n c l u d e st h V I 2 K e r n a l , s a n m a c h i n e c o d e p r o g r a m w h i c h w a w r i t t e nf o r t h e V I 2 0 , w h i c h d e p e n d e d o u s o t h V I 2 K e r n a l , w i l l r u o t h C o m m o d o r e 6 4

U s i n g t h K e r n a l r o u t i n e s i q u i t e s t r a i g h t f o r w a r d . F i r s t l y , i y o n e e d t p r o v i d e d a t a t t h r o u t i n e , t h e n y o m u s t p r o v i d e t h i si t h f o r m t h a t t h r o u t i n e e x p e c t s . T h i s u s u a l l y m e a n s l o a d i n gt h v a l u e s i n t o o n o m o r e o t h r e g i s t e r s . T h e n y o s i m p l y J S t t h K e r n a l a d d r e s s . I f t h e r o u t i n e r e t u r n s a n y v a l u e s , t h e nt h e s e t o o a r e n o r m a l l y r e t u r n e d i o n e o r m o r e o t h r e g i s t e r s .

T h t h i r t y n i n e C o m m o d o r e 6 K e r n a l r o u t i n e s a r d e s c r i b e d b e l o w :

A C P T R : R e a d b y t e f r o m a n I E E E S e r i a l D e v i c e .C a l l a d d r e s s : $ F F A 5 ( 6 5 4 4 5 )C o m m u n i c a t i n g r e g i s t e r s : A R e g i s t e r s a f f e c t e d : A , X .P r e p a r a t o r y r o u t i n e s : T A L K , T K S A .A s s o c i a t e d r o u t i n e s : T A L K , T K S A , U N T L K .E r r o r r e t u r n s : S e e R E A D S T .

Page 230: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 230/243

C H K I N O p e n c h a n n e l f o r i n p u t .C a l l a d d r e s s : $ F F C 6 ( 6 5 4 7 8 )C o m m u n i c a t i n g r e g i s t e r s : X R e g i s t e r s a f f e c t e d : A , X .P r e p a r a t o r y r o u t i n e s : O P E N .A s s o c i a t e d r o u t i n e s : S E T L F S , S E T N A M , O P E N , C H R I N , C L R C H N ,C L O S E , C L A L L .E r r o r r e t u r n s : 3 , o ( s e e R E A D S T ) .

l o g i c a l f i l e p r e v i o u s l y o p e n e d b O P E N i d e f i n e d a a n i n p u tc h a n n e l i o r d e r t h a t i m a b r e a d . L o a d w i t h l o g i c a l n u m b e ro f f i l e .C H K O U T O p e n c h a n n e l f o r o u t p u t .

C a l l a d d r e s s : $ F F C 9 ( 6 5 4 8 1 )C o m m u n i c a t i n g r e g i s t e r s : X R e g i s t e r s a f f e c t e d : A X P r e p a r a t o r y r o u t i n e s : O P E N .A s s o c i a t e d r o u t i n e s : S E T L F S , S E T N A M , O P E N , C H R O U T , C L R C H N ,C L O S E C L A L L .E r r o r r e t u r n s : 0 3 , 5 , ( s e e R E A D S T ) .

l o g i c a l f i l e p r e v i o u s l y o p e n e d b y O P E N i d e f i n e d a a o u t p u tc h a n n e l i o r d e r t h a t i m a b w r i t t e n t o L o a d w i t h l o g i c a ln u m b e r o f i l e .

C H R I N I n p u t b y t e f r o m t h i n p u t c h a n n e l .C a l l a d d r e s s : $ F F C F ( 6 5 4 8 7 ) .C o m m u n i c a t i n g r e g i s t e r s : A R e g i s t e r s a f f e c t e d : A X P r e p a r a t o r y r o u t i n e s : O P E N , C H K I N .A s s o c i a t e d r o u t i n e s : S E T L F S , S E T N A M , O P E N , C H K I N , C L R C H N ,C L O S E , C L A L L .E r r o r r e t u r n s : ( s e e R E A D S T ) .

R e a d s b y t e o d a t a f r o m t h c h a n n e l o p e n e d f o i n p u t . B y t er e t u r n e d i A

C H R O U T O u t p u t b y t e t t h o u t p u t c h a n n e l .C a l l a d d r e s s : $ F F D 2 ( 6 5 4 9 0 ) .C o m m u n i c a t i n g r e g i s t e r s : A R e g i s t e r s a f f e c t e d : A P r e p a r a t o r y r o u t i n e s : O P E N , C H K O U T .A s s o c i a t e d r o u t i n e s : S E T L F S , S E T N A M , O P E N , C H K O U T , C L R C H N ,C L O S E , C L A L L .E r r o r r e t u r n s : 0 ( s e e R E A D S T ) .

O u t p u t t h b y t e i t t h c h a n n e l o p e n e d f o r o u t p u t .

Page 231: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 231/243

C I N T I n i t i a l i s e s c r e e n e d i t o r a n 6 5 6 7 V I C - I I c h i p .C a l l a d d r e s s : $ F F 8 1 ( 6 5 4 0 9 ) .C o m m u n i c a t i n g r e g i s t e r s : n o n e .R e g i s t e r s a f f e c t e d : A X Y P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e

T h i s r o u t i n e m a b u s e d t r e s t o r e t h V I C - I I c h i p t i t n o r m a ls t a t u s , f o r i n s t a n c e , f o l l o w i n g h i - r e s o l u t i o n g r a p h i c s .

C I O U T O u t p u t b y t e t d e v i c e o n t h e I E E E s e r i a l b u s .C a l l a d d r e s s : $ F P A 8 ( 6 5 4 4 8 ) .C o m m u n i c a t i n g r e g i s t e r s : A R e g i s t e r s a f f e c t e d : n o n e .P r e p a r a t o r y r o u t i n e s : L I S T E N , S E C O N D .A s s o c i a t e d r o u t i n e s : L I S T E N , S E C O N D , U N L S N .E r r o r r e t u r n s : ( s e e R E A D S T ) .

O u t p u t s t h b y t e i n t t h s e r i a l b u s . B e g i n n e r s w i l l f i n d t h a tu s i n g S E T L F S , S E T N A M , O P E N a n C H R O U T p r o v i d e s a e a s i e r a p p r o a c ht w r i t i n g d a t a t t h s e r i a l b u s .

C L A L L C l o s e a l f i l e s c u r r e n t l y o p e n .C a l l a d d r e s s : $ F F E 7 ( 6 5 5 1 1 ) .C o m m u n i c a t i n g r e g i s t e r s : n o n e .R e g i s t e r s a f f e c t e d : A X P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : S E T L F S , S E T N A M , O P E N , C H K I N , C H R I N ,C H K O U T , C H R O U T , C L R C H N , C L O S E .E r r o r r e t u r n s : n o n e .

T h i s r o u t i n e m a b c a l l e d t c l o s e a l c u r r e n t l y o p e n f i l e s .C L O S E C l o s e t h s p e c i f e d l o g i c a l f i l e .

C a l l a d d r e s s : $ F F C 3 ( 6 5 4 7 5 ) .C o m m u n i c a t i n g r e g i s t e r s : A R e g i s t e r s a f f e c t e d : A , X , Y .P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : S E T L F S , S E T N A M , O P E N , C H K I N , C H R I N ,C H K O U T , C H R O U T , C L R C H N , C L A L L .E r r o r r e t u r n s : 0 2 4 ( s e e R E A D S T ) .

C l o s e s t h e f i l e w h o s e l o g i c a l n u m b e r i s u p p l i e d i A

Page 232: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 232/243

C L R C H N C l o s e ( c l e a r ) a l I n p u t / o u t p u t c h a n n e l s .C a l l a d d r e s s : $ F F C C ( 6 5 4 8 4 ) .C o m m u n i c a t i n g r e g i s t e r s : n o n eR e g i s t e r s a f f e c t e d : A X P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : S E T L F S , S E T N A M , O P E N , C H K I N , C H R I N ,C H K O U T , C H R O U T , C L O S E , C L A L L .E r r o r r e t u r n s : n o n e .

C l o s e s t h c u r r e n t i n p u t / o u t p u t c h a n n e l s a n r e s e t s d e f a u l tc h a n n e l s ( k e y b o a r d a n s c r e e n ) .G E T I N G e c h a r a c t e r f r o m t h k e y b o a r d o R S 2 3 2 d e v i c e .

C a l l a d d r e s s : $ F F E 4 ( 6 5 5 0 8 ) .C o m m u n i c a t i n g r e g i s t e r s : A R e g i s t e r s a f f e c t e d : A X , Y .P r e p a r a t o r y r o u t i n e s : O P E N , C H K I N .E r r o r r e t u r n s : ( s e e R E A D S T ) .

R e a d s b y t e i n t o f r o m t h i n p u t c h a n n e l . S h o u l d o n l y b u s e dw i t h t h k e y b o a r d o r a n R S 2 3 2 d e v i c e . I n c h a r a c t e r i a v a i l a b l e , t h e n z e r o i r e t u r n e d i A C H R I N i t h p r e f e r r e dr o u t i n e f o r a l d e v i c e s .

I O B A S E R e t u r n s t h a d d r e s s o f t h e 6 5 2 6 C I A .C a l l a d d r e s s : $ F F F 3 ( 6 5 5 2 3 ) .C o m m u n i c a t i n g r e g i s t e r s : X Y R e g i s t e r s a f f e c t e d : X Y P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

R e t u r n s i n ( X / Y ) t h e a d d r e s s o t h 6 5 2 6 C o m p l e x I n t e r f a c e A d a p t o r( C I A ) u s e d f o I R ( a n d h e n c e k e y b o a r d p r o c e s s i n g ) . I p r o g r a m sw h i c h h a n d l e t h C I r e g i s t e r s u s e a n o f f s e t f r o m t h a d d r e s so b t a i n e d v i a t h i s r o u t i n e , t h e n i n t e r c h a n g a b i l i t y o p r o g r a m s w i l lb i m p r o v e d . F o r t h C O M M O D O R E 6 t h a d d r e s s r e t u r n e d i $ D C O O , V I C 2 0 r e t u r n s $ 9 1 0 0 .

I O I N I T I n i t i a l i s e a l i n p u t / o u t p u t d e v i c e s a n r o u t i n e s .C a l l a d d r e s s : $ F F 8 4 ( 6 5 4 1 2 ) .C o m m u n i c a t i n g r e g i s t e r s : n o n e .R e g i s t e r s a f f e c t e d : A X Y P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

M a b u s e d t r e s t o r e a l i n p u t / o u t p u t d e v i c e s t t h e i r n o r m a lc o n d i t i o n . D e v i c e s i n c l u d e 6 5 6 7 V I C - I I , t h e t w o 6 5 2 6 C I A s a w e l la t h 6 5 8 1 S I D .

Page 233: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 233/243

L I S T E N C o m m a n d t h s p e c i f i e d d e v i c e o t h s e r i a l b u t l i s t e n .C a l l a d d r e s s : $ F F B 1 ( 6 5 4 5 7 ) .C o m m u n i c a t i n g r e g i s t e r s : A R e g i s t e r s a f f e c t e d : A P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : S E C O N D , C I O U T , U N L S N .E r r o r r e t u r n s : ( s e e R E A D S T ) .

T h i s r o u t i n e w i l l c o m m a n d t h d e v i c e s p e c i f i e d b ( A ) , o t h I E E s e r i a l b u t l i s t e n . C I O U T m a t h e n b u s e d t t r a n s m i t b y t e s t t h d e v i c e .

L O A D L o a d ( o v e r i f y ) R A M f r o m d e v i c e .C a l l a d d r e s s : $ F F D 5 ( 6 5 4 9 3 ) .C o m m u n i c a t i n g r e g i s t e r s : A , X ,R e g i s t e r s a f f e c t e d : A , X , Y .P r e p a r a t o r y r o u t i n e s : S E T L F S , S E T N A M .E r r o r r e t u r n s : 0 , 4 , 5 , 8 , ( s e e R E A D S T ) .

L o a d s ( o v e r i f i e s ) R A f r o m t h d e v i c e d e f i n e d b y t h S E T L F S a n S E T N A M r o u t i n e s . ( A s p e c i f i e s t h a c t i o n : l o a d , v e r i f y . I t h s e c o n d a r y a d d r e s s s p e c i f i e d i t h c o r r e s p o n d i n g S E T L F S r o u t i n e w a 0 t h e n ( X / Y ) s p e c i f y t h l o a d a d d r e s s . A s e c o n d a r y a d d r e s s o o c a u s e s t h a d d r e s s s t o r e d i n t h e h e a d e r o t h f i l e t b u s e d a l o a a d d r e s s .

M E M B O T R e t u r n o s e t t h e a d d r e s s o t h b o t t o m o a v a i l a b l e m e m o r y .C a l l a d d r e s s : $ F F 9 C ( 6 5 4 3 6 ) .C o m m u n i c a t i n g r e g i s t e r s : X Y R e g i s t e r s a f f e c t e d : X Y P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

I c a r r y i s e o e n t r y , r o u t i n e r e t u r n s t h a d d r e s s o t h b o t t o m o m e m o r y i ( X / Y ) . I c a r r y i c l e a r , t h ( X / Y ) i u s e d t s e t h b o t t o m o f m e m o r y .

M E M T O P R e t u r n o s e t t o p o f m e m o r y .C a l l a d d r e s s : $ F F 9 9 ( 6 5 4 3 3 ) .C o m m u n i c a t i n g r e g i s t e r s : X , Y .R e g i s t e r s a f f e c t e d : X , Y .P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

S i m i l a r i a c t i o n t M E M B O T , b u t u s e d t r e t u r n o s e t t h e t o p o m e m o r y .

O P E N O p e n l o g i c a l f i l e .

Page 234: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 234/243

U s e d t o p e n t h f i l e w h i c h h a p r e v i o u s l y b e e n s p e c i f i e d b t h S E T L F Sa n S E T N A M r o u t i n e s .

P L O T R e t u r n o s e c u r r e n t c u r s o r p o s i t i o n .C a l l a d d r e s s : $ F F P O ( 6 5 5 2 0 ) .C o m m u n i c a t i n g r e g i s t e r s : A X , Y .R e g i s t e r s a f f e c t e d : A , X , Y P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

T h i s r o u t i n e m a y b u s e d t o b t a i n t h c u r r e n t p o s i t i o n ( r o w , c o l u m n )o t h c u r s o r , o t m o v e t h c u r s o r t s p e c i f i e d p o s i t i o n . E n t r yw i t h c a r r y s e o b t a i n s t h c u r r e n t r o ( X ) , a n c o l u m n ( Y ) . E n t r y w i t hc a r r y c l e a r m o v e t h c u r s o r t t h r o a r i d c o l u m n s p e c i f i e d b a n Y

R A M T A S I n i t i a l i s e m e m o r y , i n c l u d i n g n o n - d e s t r u c t i v e t e s t o f R A a b o v e$ 0 3 F F .C a l l a d d r e s s : $ F F 8 7 ( 6 5 4 1 5 ) .C o m m u n i c a t i n g r e g i s t e r s : A , X , Y .R e g i s t e r s a f f e c t e d : A X Y P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

N o r m a l l y u s e d d u r i n g t h i n i t i a l i s a t i o n o t h c o m p u t e r a f t e r s w i t c h i n go n C l e a r s a n r e s e t s $ 0 0 0 0 t $ 0 1 0 1 , a n d $ 0 2 0 0 t $ 0 3 F F , c a r r i e s o u

n o n - d e s t r u c t i v e t e s t o f R A a b o v e $ 0 4 0 0 , r e s e t s t h s c r e e n b a s e t $ 0 4 0 0 .

R D T I M R e a d s y s t e m c l o c k .C a l l a d d r e s s : $ F F D E ( 6 5 5 0 2 ) .C o m m u n i c a t i n g r e g i s t e r s : A , X , Y R e g i s t e r s a f f e c t e d : A X Y P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

R e t u r n s t h s y s t e m ( j i f f y ) c l o c k i n t o ( m s b ) , X ( l s b ) . T h e o n l ya d v a n t a g e o f t h i s r o u t i n e i t h a t t h u s e r d o e s n ' t n e e d t k n o w w h e r et h c l o c k i m a i n t a i n e d . I i l o c a t e d i n $ A 2 , $ A 1 , $ A o n t h e c o m p u t e rb u i c o u l d b e l s e w h e r e o a n o t h e r m a c h i n e .

Page 235: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 235/243

Page 236: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 236/243

S C N K E Y S c a n s t h k e y b o a r d a n r e t u r n s t h A S C I I v a l u e o f a n y k e c u r r e n t l y b e i n g p r e s s e d .C a l l a d d r e s s : $ F F 9 F ( 6 5 4 3 9 ) .C o m m u n i c a t i n g r e g i s t e r s : n o n e .R e g i s t e r s a f f e c t e d : A X , Y .P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : S T O P , U D T I M .E r r o r r e t u r n s : n o n e .

I t h n o r m a l c o u r s e o f e v e n t s , t h k e y b o a r d i s c a n n e d o n c e e v e r yf i f t i e t h o s e c o n d a n a n k e y s b e i n g p r e s s e d a r t r a n s f e r r e d t t h k e y b o a r d b u f f e r . T h e S C N K E Y r o u t i n e i t h r o u t i n e w h i c hu n d e r t a k e s t h i s t a s k . T h s c a n n i n g i p a r t o f t h e n o r m a l I R i n t e r r u p t s e r v i c i n g p r o c e d u r e . I m a s o m e t i m e s o c c u r t h a t t h n o r m a l I R Q i n t e r r u p t i d i s a l l o w e d , e i t h e r b S E I , o r p e r h a p s b yt r a p p i n g t h i f t Q a n n o a l l o w i n g n o r m a l k e y b o a r d s c a n n i n g . I t h i s c a s e , t h e u s e r m a c a l l t h S C N K E Y r o u t i n e . T h i s s h o u l da l w a y s b d o n e p r i o r t a n y a t t e m p t t r e a d t h e k e y b o a r d u s i n gG E T I N o C H R I N , w h e n n o r m a l i n t e r r u p t i s u s p e n d e d .

S C R E E N R e t u r n s t h f o r m a t o f t h e s c r e e n .C a l l a d d r e s s : $ F F E D ( 6 5 5 1 7 ) .C o m m u n i c a t i n g r e g i s t e r s : X Y R e g i s t e r s a f f e c t e d : X Y P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

R e t u r n s t h f o r m a t o f t h s c r e e n : c o l u m n s i n a n d r o w s i n Y . I ti t h e r e f o r e p o s s i b l e f o m a c h i n e c o d e p r o g r a m t d i s c o v e r t h w i d t h o t h s c r e e n a n a d j u s t i t s e l f a c c o r d i n g l y .S E C O N D S e n d s e c o n d a r y a d d r e s s f o L I S T E N t o d e v i c e o n t h e s e r i a l

b u s .C a l l a d d r e s s : $ F F 9 3 ( 6 5 4 2 7 ) .C o m m u n i c a t i n g r e g i s t e r s : A R e g i s t e r s a f f e c t e d : A P r e p a r a t o r y r o u t i n e s : L I S T E N .A s s o c i a t e d r o u t i n e s : A C P T R .A s s o c i a t e d r o u t i n e s : L I S T E N , A C P T R , U N L S N .E r r o r r e t u r n s : ( s e e R E A D S T ) .

T h i s r o u t i n e i u s e d t t r a n s m i t s e c o n d a r y a d d r e s s o n t h e s e r i a lb u f o l l o w i n g L I S T E N r o u t i n e , i o r d e r t h a t t h e d e v i c e w h i c h i t l i s t e n m a b i d e n t i f i e d .

Page 237: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 237/243

S E T L F S S e t u p l o g i c a l f i l e .C a l l a d d r e s s : $ F F B A ( 6 5 4 6 6 ) .C o m m u n i c a t i n g r e g i s t e r s : A X , Y .R e g i s t e r s a f f e c t e d : n o n e .P r e p a r a t o r y r o u t i n e s : a l t h o u g h S E T N A M a n S E T L F S m a b c a l l e d i a n o r d e r , b o t h m u s t b c a l l e d .A s s o c i a t e d r o u t i n e s : S E T N A M , O P E N , S A V E , L O A D .E r r o r r e t u r n s : n o n e .

T h i s r o u t i n e i a e s s e n t i a l p r e l i m i n a r y f o r m a n y o f t h i n p u t / o u t p u t r o u t i n e s . T h e l o g i c a l f i l e n u m b e r , f i r s t a d d r e s s ( o d e v i c e n u m b e r a s i t i s b e t t e r k n o w n ) a n s e c o n d a r y a d d r e s s ( i f a n y )a r t r a n s m i t t e d t t h r o u t i n e t h r o u g h A a r e s p e c t i v e l y .T h u s t o p e n t h p r i n t e r w i t h l o g i c a l f i l e n u m b e r o 2 a n t r a n s m i t s e c o n d a r y a d d r e s s o f f o r l o w e r c a s e , t h e f o l l o w i n gc o d e w o u l d b w r i t t e n :

L D A I M 2 L D X I M L D Y I M J S $ F F B A .S E T M S G C o n t r o l s t h f o r m a t o t h s y s t e m m e s s a g e s o u t p u t b y t h

o p e r a t i n g s y s t e m .C a l l a d d r e s s : $ F F 9 O ( 6 5 4 2 4 ) .C o m m u n i c a t i n g r e g i s t e r s : AR e g i s t e r s a f f e c t e d : A P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

T h i s l i t t l e u s e d r o u t i n e c o n t r o l s t h p r i n t i n g o e r r o r a n d c o n t r o lm e s s a g e s . B i t s a n o t h v a l u e s u p p l i e d i n d e t e r m i n e t h p r i n t i n g . I b i i s e t h e n e r r o r m e s s a g e s a r e p r i n t e d , b i ts e p e r m i t s c o n t r o l m e s s a g e s t b p r i n t e d .

S E T N A M S e u f i l e n a m e .C a l l a d d r e s s : $ F F B D ( 6 5 4 6 9 ) .C o m m u n i c a t i n g r e g i s t e r s : A , X , Y .R e g i s t e r s a f f e c t e d : n o n e .P r e p a r a t o r y r o u t i n e s : a l t h o u g h S E T N A M a n S E T L F S m a b c a l l e d i a n o r d e r , b o t h a r r e q u i r e d .A s s o c i a t e d r o u t i n e s : S E T L F S , O P E N , L O A D , S A V E .E r r o r r e t u r n s : n o n e .

T h i s r o u t i n e i u s e d p r i o r t o p e n i n g , s a v i n g o r l o a d i n g . T h el e n g t h o f t h f i l e n a m e i b y t e s i l o a d e d i n t o A a n t h a d d r e s sw h e r e t h e f i l e n a m e s t r i n g i s t o r e d i s u p p l i e d i ( X / Y ) . I n f i l e n a m e i d e s i r e d t h e n t h r o u t i n e i c a l l e d w i t h z e r o l o a d e di n t o A

S E T T I M S e t h s y s t e m ( j i f f y ) c l o c k .C a l l a d d r e s s : $ F F D B ( 6 5 4 9 9 ) .

Page 238: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 238/243

S E T T M O S e I E E E b u t i m e o u t f l a g .C a l l a d d r e s s : $ F F A 2 ( 6 5 4 4 2 ) .C o m m u n i c a t i n g r e g i s t e r s :R e g i s t e r s a f f e c t e d : n o n e .P r e p a r a t o r y r o u t i n e s : n o n e .E r r o r r e t u r n s : n o n e .

T h i s r o u t i n e m a y b u s e d t e n a b l e o d i s a b l e t i m e o u t t r a p p i n g o t h I E E E b u s . C o m m o d o r e m i c r o s c o n t r o l l i n g p a r a l l e l I E E E b u w i l ln o r m a l l y a b a n d o n t r a n s a c t i o n i f n o r e s p o n s e i r e c e i v e d w i t h i n 6 m i l l i s e c o n d s . T h i s t i m e o u t m a b d e f e a t e d b c a l l i n g S E T T M O w i t h b i

o ( A s e t , a n e n a b l e d i b i i c l e a r . T h e C o m m o d o r e , 6 s e r i a lb u i n o a f f e c t e d b y t h t i m e o u t f l a g , h o w e v e r , t h i s r o u t i n e m a b r e q u i r e d i i t i s a n t i c i p a t e d t h a t t h p r o g r a m m a y b u s e d t h a n d l et h P E r a n g e o f I E E E d e v i c e s ( u s i n g a a p p r o p r i a t e h a r d w a r ei n t e r f a c e ) .S T O P C h e c k i S T O P k e y h a b e e n p r e s s e d .

C a l l a d d r e s s : $ F F E 1 ( 6 5 5 0 5 ) .C o m m u n i c a t i n g r e g i s t e r s :R e g i s t e r s a f f e c t e d : A P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : U D T I M , S C N K E Y .E r r o r r e t u r n s : n o n e .

I t h S T O P w a p r e s s e d d u r i n g t h e l a s t k e y b o a r d s c a n , t h i s r o u t i n er e t u r n s w i t h t h f l a g s e t . I t h e v e n t t h a t t h e n o r m a l k e y b o a r di n t e r r u p t h a s b e e n d i s a b l e d , i t i s n e c e s s a r y t i s s u e c a l l t U D T I Mp r i o r t S T O P .

T A L K C o m m a n d d e v i c e o t h I E E E s e r i a l b u t T A L K .C a l l a d d r e s s : $ F F B 4 ( 6 5 4 6 0 )C o m m u n i c a t i n g r e g i s t e r s : AR e g i s t e r s a f f e c t e d :P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : T K S A , A C P T R , U N T L KE r r o r r e t u r n s : ( s e e R E A D S T ) .

T h a c c u m u l a t o r s h o u l d c o n t a i n t h n u m b e r o t h d e v i c e o t h s e r i a lb u w h i c h i c o m m a n d e d t T A L K .

T K S A S e n d s e c o n d a r y a d d r e s s t t h d e v i c e w h i c h i c o m m a n d e d t T A L K .C a l l a d d r e s s : $ F F 9 6 ( 6 5 4 3 0 ) .C o m m u n i c a t i n g r e g i s t e r s :R e g i s t e r s a f f e c t e d :P r e p a r a t o r y r o u t i n e s : T A L KA s s o c i a t e d r o u t i n e s : T A L K , A C P T R , U N T L KE r r o r r e t u r n s : ( s e e R E A D S T ) .

Page 239: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 239/243

U D T I M U p d a t e t h s y s t e m c l o c k .C a l l a d d r e s s : $ F F E A ( 6 5 5 1 4 ) .C o m m u n i c a t i n g r e g i s t e r s : n o n e .R e g i s t e r s a f f e c t e d : A , X .P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : S C N K E Y , S T O P .E r r o r r e t u r n s : n o n e .

T h n a m e o f t h i s r o u t i n e i s o m e w h a t d e c e p t i v e . I a d d i t i o nu p d a t i n g t h s y s t e m c l o c k i a l s o s c a n s t h k e y b o a r d a n d s t o r e sc u r r e n t k e y m a t r i x i S T K E Y . A s c o n s e q u e n c e i i p o s s i b l e f o S T O P K e r n a l r o u t i n e t d e t e r m i n e w h e t h e r t h S T O P k e y h a bd e p r e s s e d , e v e n w h e n t h n o r m a l I R i n t e r r u p t r o u t i n e h a bd i s a b l e d . P r o v i d e d t h u s e r i s s u e s c a l l t U D T I M ( o S C N K E Y ) p rt a n c a l l t t h S T O P r o u t i n e , t h e n S T O P k e y d e t e c t i o n w i l l tp l a c e .

U N L S N S e n d a n U N L I S T E N c o m m a n d t a l d e v i c e s o n t h e I E E E s e r i a l bC a l l a d d r e s s : $ P F A E ( 6 5 4 5 4 ) .C o m m u n i c a t i n g r e g i s t e r s : n o n e .R e g i s t e r s a f f e c t e d : AP r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : L I S T E N , S E C O N D , C I O U T .E r r o r r e t u r n s : ( s e e R E A D S T ) .

T h i s r o u t i n e c o m m a n d s a l d e v i c e s c u r r e n t l y L I S T E N i n g o n t h e Is e r i a l b u s t s t o p L I S T E N i n g .

U N T L K S e n d a n U N T L K c o m m a n d t a l d e v i c e s o n t h e I E E E s e r i a l b u s .C a l l a d d r e s s : $ F F A B ( 6 5 4 5 1 )C o m m u n i c a t i n g r e g i s t e r s : n o n e .R e g i s t e r s a f f e c t e d : A P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : T A L K , T K S A , A C P T R .E r r o r r e t u r n s : ( s e e R E A D S T ) .

A l d e v i c e s c u r r e n t l y T A L K i n g o t h I E E E s e r i a l b u w i l l s t o p s e n dd a t a f o l l o w i n g t h i s s u e o f t h i s c o m m a n d .

V E C T O R R e a d / s e t t h s y s t e m v e c t o r j u m p a d d r e s s e s s t o r e d i R A M .C a l l a d d r e s s : $ F F 8 D ( 6 5 4 2 1 ) .C o m m u n i c a t i n g r e g i s t e r s : X , Y .R e g i s t e r s a f f e c t e d : A , X , Y P r e p a r a t o r y r o u t i n e s : n o n e .A s s o c i a t e d r o u t i n e s : R E S T O R .E r r o r r e t u r n s : n o n e .

C a l l i n g t h i s r o u t i n e w i t h c a r r y s e w i l l c a u s e t h c u r r e n t c o n t e n t s

Page 240: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 240/243

Page 241: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 241/243

I N D E X£ E x p l a i n e d

F i r s t P r o g r a mA b s o l u t e a d d r e s s i n g 3 - A c c u m u l a t o r 1 - 2 E , 1 - A D 1 - 6 E , 1 - 7 PA D C I X 3 - 1 3A D C I Y 3 - A D C Z 3 - 1 0A d d r e s s i n g 3 - 8 f fA d d i t i o n 4 - f f 8 - 2 0A L 1 - A N 4 - 1 2 E , 4 - 1 4 PA N D Z 3 - 1 0A r c h i t e c t u r e o 6 5 1 0 1 - A S 4 - 1 8 E , 4 - 1 9 PA S L A 4 - 1 9 PA S L Z 3 - 1 0A s s e m b l e r , e n t e r i n g p r o g r a m 1 - B C 4 - 1 E , 4 - 1 PB C D a r i t h m e t i c 4 - 1 0B C n o t a t i o n A 1 - 7B C 4 - 2 E , 4 - 2 PB E 2 - 7 E , 2 - 7 P

f l a g 2 - 1 3 EB i n a r y - c o d e d d e c i m a l A 1 - 6B i n a r y - c o d e d d e c i m a l

a r i t h m e t i c 4 - 1 0B i n a r y n o t a t i o n A 1 - 1 f fB i n a r y m u l t i p l i c a t i o n 4 - 1 9B I 4 - 2 3 EB i m a n i p u l a t i o n 4 - 1 2 f fB I T Z 3 - 1 0B M I 2 - 1 4 E , 2 - 1 4 PB N 2 - 7 E , 2 - 8 PB P 2 - 1 4 E , 2 - 1 4 PB r a n c h e s 2 - 6 f fB R 8 - 3 EB V C 8 - 6 E , 8 - 6 PB V 8 - 6 E

f l a g 2 - 1 3 EC L 4 - C L 4 - 1 O E , 4 - 1 1 PC L 8 - 1 EC L 8 - 1 6C M 2 - O E 2 - 1 1 PC M P I X 3 - 1 3

C P X I M 3 - 1 3 PC P X Z 3 - 1 0C P Y 2 - 1 0C P Y I M 3 - 1 1 E , 3 - 1 1 PC P Y Z 3 - 1 0C r a s h e s 1 - 1 0D a t a B u 1 - D e b u g g i n g 6 - D e l a y s 3 - D E C Z 3 - 1 0D E 2 - 6 E , 2 - 6 PD E 2 - 7 E , 2 - 1 2 PD i s a s s e m b l y 5 - D i v i s i o n 4 - 9 , 8 - 2 1

f l a g 2 - 1 3 ED o u b l e p r e c i s i o n 4 - 1 f fE i g h t b i

m u l t i p l i c a t i o n 4 - 2 0E O 4 - 1 6 E , 4 - 1 6 PE O R I M 4 - 1 6 PE O R I X 3 - 1 3E O R I Y 3 - 1 3E O R Z 3 - 1 0E x p o n e n t i a t i o n 8 - 2 2F l a g s 2 - 6 f fF l o a t i n g p o i n t a c c u m u l a t o r 8 - 1 0F l o a t i n g p o i n t n u m b e r s 8 - 1 0F l o a t i n g p o i n t s u b r o u t i n e s 8 - 1 8H e x a d e c i m a l i n p u t s 4 - H e x a d e c i m a l n o t a t i o n A 1 - 4

f l a g 2 - 1 3 EI m m e d i a t e a d d r e s s i n g 3 - 1 0I m p l i e d a d d r e s s i n g 3 - I N 4 - 8 EI N C Z 3 - 1 0I n d e x e d a d d r e s s i n g 3 - 1 1I n d e x R e g i s t e r 2 - I n d i r e c t A b s o l u t e A d d r e s s i n g 3 - 1 I n d i r e c t A d d r e s s i n g 3 - 1 2I n t e r r u p t s 8 - I N 2 - 8 EI N 2 - 8 EJ M 2 - 1 E , 2 - 1 PJ M P I A 3 - H E , 3 - 1 4 P

Page 242: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 242/243

L D A Z 3 - 1 0L D 1 - 9 E , 1 - 9 PL D X Z 3 - 1 0L D 1 - 1 0 EL D Y I M 1 - 1 1 EL D Y Z 3 - 1 0L o a d p r o g r a m f r o m t a p e 6 - 9L i s t f r o m M o n i t o r 6 - L o 8 - 2 2L o g i c a l o p e r a t o r s 4 - 1 2 f fL S R 4 - 1 7 E , 4 - 1 8 PL S R A 4 - 1 8 PL S R Z 3 - 1 0M a c h i n e c o d e 1 - M a c r o s 5 - 1 1M e m o r y L a b e l s 5 - M o n i t o r 6 - M u l t i p l i c a t i o n 6 - 7 E

f l a g 2 - 1 3 EN y b b l e A 1 - 4N O 3 - 7 , 4 - 4 E , 4 - 4 PN u m e r i c a l s c r e e n o u t p u t 8 - O n e ' s c o m p l e m e n t 8 - 4O p e r a n d 2 - O R 4 - 1 5 E , 4 - 1 5 PO R A I M 4 - 1 5 PO R A I X 3 - 1 3O R A I Y 3 - 1 4O R A Z 3 - 1 0O v e r f l o w s 8 - P H 7 - 1 1P H P 7 - 1 0P L 7 - 1 0P L 7 - 1 1P r o c e s s o r s t a t u s r e g i s t e r 2 - 1 3 EP O K E , e n t r y o p r o g r a m s 6 - P r o g r a m C o u n t e r 2 - P r o t e c t i n g m a c h i n e c o d e

i m e m o r y 6 - P s e u d o c o d e 1 - 1 3 , 2 - P S 2 - 1 6R e l a t i v e a d d r e s s i n g 3 - 1 2R e g i s t e r d i s p l a y 6 - R O L 4 - 2 3 ER O L A 4 - 2 3 E , 4 - 2 5 PR O L Z 3 - 1 0

R O R 4 - 2 3 ER O R A 4 - 2 3 ER O R Z 3 - 1 0R T 8 - 2 ER T 1 - 3 E , 1 - 3 PS a v e p r o g r a m 6 - 8S B 4 - 6 ES B C I M 4 - 7 PS B C I X 3 - 1 3S B C I Y 3 - 1 4S B C Z 3 - 1 0S E 3 - 9 , 4 - 7 E , 4 - 7 PS E 3 - 9 , 4 - 1 0 E , 4 - 1 1 PS E 3 - 9 , 8 - S i g n e d n u m b e r s 8 - 4 f fS T 1 - 2 E , 1 - 3 PS t a c k 7 - 8S T A I X 3 - 1 3S T A I Y 3 - 1 4S t a t u s r e g i s t e r 2 - 6 ES t a t u s w o r d 2 - 6 ES T A Y 3 - 1 E , 3 - 6 ES t a t u s f l a g s 2 - 1 3S T A X 3 - 1 E , 3 - 1 PS T A Y 3 - 1 E , 3 - 1 PS T A Z 3 - 1 0S T 1 - 9 E , 1 - 9 PS T X Z 3 - 1 0S T Y 1 - 1 1 ES T Y Z 3 - 1 0S u b r o u t i n e ; f l o a t i n g p o i n t 8 - 1 8S u b t r a c t i o n 4 - 6 , 8 - 2 1T A 1 - 1 2 E , 1 - 1 3 PT A Y 1 - 1 3 ET i m i n g 3 - T r a n s f e r i n s t r u c t i o n s 1 - T r u t h t a b l e 4 - 1 2 f fT w o ' s c o m p l e m e n t 8 - T X A 1 - 1 3 ET Y A 1 - 1 3 EU S c o m m a n d 8 - 1 2

f l a g 2 - 1 3 EX - r e g i s t e r 1 - 8 , 3 - 1 1Y - r e g i s t e r 1 - 8 , 3 - 1 2Z e r o p a g e a d d r e s s i n g 3 - 1 0

f l a g 2 - 6 E , 2 - 1 3

Page 243: Commodore 64 Assembly Language Programming

8/6/2019 Commodore 64 Assembly Language Programming

http://slidepdf.com/reader/full/commodore-64-assembly-language-programming 243/243