Programming The 6800 1977
Programming The 6800 1977
PROGRAMMING THE
6800 MICROPROCESSOR
- Bob Southern -
Algonquin College
Ottawa Ont. Canada
A self-instructional workbook
for assembly language and machine code programming
01 the 6800 family of microprocessors and peripherals
2- Accumulator Operations
3- Symbolic Addressing
4- Index Register
9- Subroutines
10 - Stack Operations
11 - Interrupt
AppendICes
A ._. . Hex Codes G _._. . C)laraeter Set
B .... ASCII Codes H .._ . Common losll\lctJons
el, C2 Instruction Set ~ ".." Glossary
D . .. Machme Code J 1, J2 Assembler Error Codes
E1. E2 . .. ACIA K OM Instruction
F1. F2 . .. PIA
- Many people helped make this workbook possible. I would like to thank Peter
Booler, Brian Bradley, Michel Brule and Bill Foster of Algonquin College, and
Don Lindsay of Dynalogic Limited for their advice and comments. I also would
like to thank Lynne Hall who formatted and typed this book. Lastly, I would like
to thank Richard Leir, John Oldfield and John Quarterman for their time in
testing the final version of this book.
- The program on the front cover was written by Don Lindsay of Dynalogic
Limited, Ottawa.
Bob Southern
Disclaimer
- The information contained in this workbook has been carefully checked and is
believed to be correct. However the author and publisher cannot assume
responsibility for errors or omissions or liability for any damages or
consequential damages arising from the use of this workbook.
9 8 7 6
Printed in Canada
PROGRAMMING THE 6800 MICROPROCESSOR
FOR WHOM
This workbook was designed primarily for use by students
at the community college level, although it has been successfully
used by at least one capable high school student. Previous
programming experience is not necessary •. Early high school
mathematics is adequate, although mathematical competence beyond
this level is a good predictor of success.
THIS WORKBOOK IS
AVAILABLE FROM
_ _------1/
Answers The student should compare his/her answer with the one
given in the workbook and, when satisfied with any
\ .
d1fferences, move on to the next paragraph.
I
BINARY AND HEX NUMBERS
PRE-TEST
Contd •••
PRE-TEST
Contd.
(a) Solutions 75 - 41 = 34 (decimal)
2 W 1001011
--.,.-~
2 W 101001
"--w-' "-v-'
2 L1Z
+ 1 4 B 2 ~+ 1 2 9
2 ill + 1 2 L1Q
+ 0
2 U+ 0 2 U+ 0
2 U+ 1 2 U+ 1
2 U+ 0 2 L! + 0
2 L! + 0 o + 1
o+ 1
Calculate -29 then add 75. all in hex.
FF
-~
D6
+-1
D7
+4B
22 hex
o
~::: 16 = 2} "
16 1 = )2
)4 decimal
----------------------------~-------------------------
------
75
= 01001011 (as an 8 bit number)
41 = 00101001
one's complement of 41 = 11010110
+ 1 ---~
~~
~'572 xx1001 = 7 x 1 =
10 = 2 x 10 = 20
7
x 10 2 = 5 x 100 = 500
527
Computers use the binary or two-state number system, that
is each "binary digit" or IJbit" has only two states,' 0 or 1.
Adding 1 to 1 results in 0 with a carry.
The first 3 numbers in the binary number system are 0, 1
and 10. This is seen by adding 0 1
+1 then +1
=1 =10 = 2 (decimal)
In binary add 2 + 1. Your answer should be written above
this line. Then check your answer.
_ _ _--J/
10
+ 1
=11 = 3 (decimal) 11 (binary) : J (decimal)
--
1-2
Now calculate the binary values for 4, 5 and 6, starting
from the binary equivalent of 3.
11 =J 100 = 4
-----/
101 = 5
U U U
100 =4 101 =5 110 = 6
~ 1
1
+
+
1 =0
c~rry
+ carry
= 0 + carry.
~: x 21 = 0
x 22 =..2t...
5
Determine the binary value for 8 and 9. ---
1-3
_ _ _--J/
8 = 1000 To verify 110 =6 110 = 6
9 = 1001 ~ +10 = 2
111 =7 1000 = 8
+ 1
OR + 1
1000 = 8 1001 = 9
~
1001 =9
The second solution is more direct and also demonstrates binary
addition with a carry.
1-4
In the binary number 101, the ri"ght bit carries the
least weight and is therefore called the Least Significant
-
Bit or LSB. The left bit carries the most weight (2 2 in this
case) and is the Most Significant Bit or MSB.
In binary, calculate 6 + 4. Verify by converting
your answer to decimal.
6 = 110
---~/
+4 = 100
10 1010
~l- ~ ~~
= 0
: : = 2
2
a x 2 =0
1 x 23 = 8
10 10 Yes! It works.
Calculate 8 + 7 in binary. Verify your answer by
....
1-5
1000 = 8 1111
/
+0111 = 7 20 = 1
lS::
x
1111 = 15 1
x 2 = 2
x 22 = 4
1 x 23 =...L
15
In summary the binary equivalents for a to 15 are:
0000 = a 0100 = 4 1000 = 8 1100 = 12
0001 = 1 0101 = 5 1001 = 9 1101 = lJ
0010 = 2 0110 = 6 1010 = 10 1110 = 14
0011 = 3 0111 = 7 1011 = 11 1111 = 15
1-6
Each bit of a binary number is assigned a bit number
which is the same as its binary exponent as shown below.
--
1011
"t:~~ :~
~(:bit #2
' - bit #J
What is another name for bit #J in this binary number lOll?
.--bit #@,
110110 5)~same.
~1 x 2~
1-7
. -
Let's look at a method to convert from decimal to binary.
This method involves successive division of the decimal number
by 2, noting the remainder at each stage. Conversion of 19 10 to
binary is illustrated.
2 l.!2 .---- remainder
r
2 l2+ 1
2 IJ!.+ 1 10011. This is obtained by reading
2 13. + 0 the remainders, bottom to top.
2 l!.+ 0
o+1
To verifyc 10011
0
LS~:
2 = 1
1
2 2=
1 x 2 4 = 16
19 10
Now calculate the binary equivalent of 69 and verify your answer.
2 111 7 1110101
20
~:
2 ~+ 1 x = 1
2 (29 + 0 x 22 = 4
2 (14 + 1 x 24 = 16
25
\......,
2 11. + 0 ~ 1 x = 32
2 f1.+ 1 1 x 26 = 64
2 L!.+ 1 117 10
o+ 1 117 10 = 1110101 2
If you are satisfied with your progress proceed to the
next frame. If not, try another number of your own choice now.
-----------------
~ Note that here 1 1 plus
/ +
111 =7 a carry =1 plus a carry.
1101 = 1)10
~'-
------1..~ 1
:~ ~~:
x 23 = ~
=
=
1
4
13 10 13 10 = 1101 2
1-10
Calculate 5 + 7 in binary and convert your answer to
decimal to verify it.
5 = 101
_ _ _----J/
Z = 111
1210 = 110°2
~~
x 22 = 4 )
) ...... 12 10 = 110°2
x 23 = 8 )
1-11
Values less than 1 can be expressed in binary as in the
example below
1010 binary point
The 1 on the right side of the binary point carries the weighting
of 2- 1 (or 0.5 10 ); since the binary exponent continues to decrease
by 1 for each move to the right. The decimal value is then
x 22 = 4
~1
x 2
x 2°
1
=
=
°1
-1
x 2 = ~
101.1
5·5
Express 110.11 in decimal.
-------'/
---_/
1-13
By breaking up longer binary numbers into groups of 4
bits each we can express them in their hex equivalents e.g.,
the 8 bit binary number
10011010 can be grouped as
1001
"'-y--'
1010
.~
,
_ _ _-1/
-bit #6
C3 ~ ~ ---iJ' 11000011
C 3 76543210 4-bit #
Hex codes are very popular with 8 bit microprocessors,
such as the 6800, with 2 hex characters equalling 8 bits or
1 byte. If for some reason only 7 bits are used in a binary
number, a leading zero may be added to fill out the 8 bits, e.g.,
1011101 = ~~!l~
5 D
1-14
Express each of the following binary numbers in hexi
11000101 1111000 111011
_ _ _--.J/
11000101 ~1~10~
~-v--'----.....'
~
C 5 7 8
With a base of 16 the hex number 78 equals,
78 ,-;:- 16 used here for ~ numbers.
l'- : 8 x 16° =
7 x 16
1
= 112
8
120
10
The hex number 78 can be expressed as 78 16 to avoid confusion
with the decimal number 78 10 , a different value.
1-15
Express each of the following hex numbers in binary
and in decimal.
D4
39
6A
D4 = ~91~ D4
\ ~D4 x= 16
D 4 0
= 4 '1. 212 10
'-. 13 13 x 16 1 = 208)
39=~~
3 9
48
9} 57 10
6A = W10!Q,
6 A 10 10 x 16° = 10} 106
.... 6 x 16 1 = 96 10
1-16
Addition in hex can be challenging, although the problem
does not exist for computers since they work in binary. Hex is
for our convenience in expressing binary numbers.
One solution is to convert to binary, add the numbers
and convert the answer back to hex, possible but not the fastest
way. If we had 8 toes on each foot we could count on our toes
to add. Did you ever consider why our number system has a base
of ten?
The solution proposed is the use of the number line.
until you become more familiar with hex addition.
For example. 9 + 3 = C
start here~"';",--count J to the right to get "C II
o 1 2 J 4 5 6 7 8 9 ABC D E Ff F1 2 J 4 5 6 7 8 9 ABC D EF
to o. Contd .•.
1-16
Contd-
Using this principle show that A + 9 = 13 16-
start
--_--.J/
1 2 3 4 5 6 7 8 9 To verifyl
8 9 ABC DEFt 0 1 2 J 4 A = 1010
9 = 1001
' - carry produced 1 0011
~
in going from 1 3 = 13 16
F to o.
1-17
Now add C + 9 and verify your answer by adding the
decimal equivalents.
start·
_ _ _---..J/
1 2 3 4 5 6 7 8 9~~------5 plus carry = 15 16
ABC D E F 0 1 2 345 6 7
C = 12 10
9 = -2..-
21 10
~---- agrees
1-18
Now add 7 + D and verify your answer by adding in
decimal.
7 8 9 ABC D E FlO 1 2 3 4 5 6 7 8
~4 plus carry = 14 16
To verify 14 16
,,-~4 x 16° = 4 7 =7
1 x 16 1 = 16 D = 13 10
2010 2010
....... agrees ~
It would have been easier to add 7 to D rather than D to 7.
The answer still is 14 16 •
1-19
Add the hex numbers C and D. Verify your answer.
start
c
c =
D=
4-- agrees ~
1-20
To add 2 column hex numbers each column is added
separately, as in decimal. If the right column produces a
carry it is added to the left column
e.g.. 2F
ill
42
(: : F ++ J +== 2 plus carry
- ~ 2 1 carry = 4
Add the hex numbers JE + 27.
JE
_ _ _---.J/
+27
65
l: :E + 7 = 5 plus carry
J + 2 + carry
.... = 6
1-21
Add the hex numbers 4D and 25.
72 16 4D
--_----.1/
~
72 16
~. :: D + 5 = 2 plus carry
4 + 2 + carry = 7
To verify we'll convert all data to decimal
4D 16 = 4 x 16 1 + 1J x 16° = 64 + 1) = 77 10 77
25 16 = 2 x 16 1 + 5 x 16 0 = )2 + 5 = J7 10 !-12
1 0
72 16 = 7 x 16 + 2 x 16 = 112 + 2 = 114 10 ~1f~------~"~ 114 10
agrees
1-22
Subtraction involves moving to the left on the number
line, e.g., D - 5 = 8 as seen below
54 ) 2 1 start
~
o 1 2 ) 4 5 6 7 8 9 ABC D
For the moment we will avoid "borrow" operations.
Calculate B - 7.
4 7 6 5 4 )
---~/ start
o 1 2 ) 4 5 678 9 A B
1-2)
t
00 01 02 - - - - - - FD FE FF
However, if 1 is added to FF the result, still using 2 hex
characters (8 bits), is FF
+01
1 00
carry---1{
or 00, the carry being lost as an overflow, outside the 8 bit
limit. The question now asked is "What number, when 1 is
added to it, becomes O?" The answer is -1. By definition
therefore FF = -1. We now reconstruct our number line
-1 0 +1 +2
I I I
,
---- FD FE FF 00 01 02 - -------
What is the value of FD based on this number line?
_ _ _-J/
-) Since FD + ) = 00 (carry is outside the 8 bit limit)
This new number line is called a signed number line since it
permits both positive and negative values.
1-24
Continuing with the signed number line if the leading
bit (MSB) of the 8 bit number = 1, that is 8 or more for the
first hex character, the number by definition is negative. The
extent of this signed number line is shown below in decimal,
hex and binary.
-..............
~ ~
-127 ~ ~--.......
..- ......--.. __ -... +127
10 10
-128 10 ~ -) -::2 -:'1 - - - + 1 - +2 - +) +12~10 ~
~• II I I l II 1- I
80 81 FD FE FF 00 01 02 OJ 7E 7F
\..10000000 2 01111111
2
J
The extent of this signed number line is then -128 10
to +12710, Based on this number line which of the following
hex values are negative,
7A 94 F2 00 8E CA
-----'/
All except 7A and 00 are negative. having a leading hex
character 8 or larger. If converted to binary all except 7A and
00 would have 1 as a leading bit.
If a larger range is needed for the signed number line
16 bits (2 bytes) could be used, again providing negative
values if the leading bit equals 1. This is sometimes referred
to as a double precision value.
1-25
To determine the negative value for the hex number 31
is more difficult. A procedure shown below is based on the 2's
complement arithmetic used in binary subtraction.
The procedure then iSI
- Start with the largest possible hex value
(ignoring the sign) -----I...... FF
- then subtract the number .::1! F-1~
using the number line approach CE ABCDEF
~,
- then add 1 F -3 = c
OF now equals -3116 CF
To prove it the sum of OF and 31 should be zero in 2 character
hex format. Prove it.
CF
_ _ _---J/
ill
100
~ =
F + 1 = 0 + carry
C + 3 + carry = 0 + carry
carry, which is ignored as an overflow
CF = -3 116
1-26
Determine the hex value for -5D and prove that it is
correct by adding +5D to it.
/
FF DC B A 9 8 ? 6 5 4 3 2 1 start
-5D 2 J 4 5 678 9 ABC D E F
A2 ~start
.:!:....! In the top row a more direct subtraction
AJ = -5D is seen in that F and D are separated by
2, hence F -D = 2.
To check AJ
+.2Q
carry~
1 00
1-27
Now calculate -6C and verify it.
-6c = 94 FF
------/ To checkl
-6C ~start 4
U
93
94
9ABCDEF
~start
carry)
E 6c
100
1-28
The "two hex character" value of -) is FD. If 4
characters are used to express -3. prove that -3 = FFFD.
FFFD
----'/
.:..-..J Similarly a 6 character representation would be
1 0000 FFFFFD.
~ carry.
To determine the value of _.) using 4 hex characters, the
procedure is FFFF FFFFFF
=---1 3
FFFC Using 6 hex characters FFFFFC
~ -) equals + 1
= FFFD FFFFFD
Almost all our work will employ 2 hex characters only.
For 6 hex characters () bytes) the signed number line would
extend from 800000 16 (most negative) to 7FFFFF16 (most positive) ..
1-29
We now have the capability to subtract in hex since
72 -JD is actually 72 +(-JD). Once -3D has been calculated the
hex addition will produce the answer. Try it.
_ _ _ _-----J/'
FF largest hex value To checkl
-JQ If 72 -)D = 35 then 35 + 3D = 72
C2 35
+ 1 plus 1 +3D
CJ = -3D 72
+1£ now add the 72
1 J5 answer
~overflow ignored
FF
-----/
E; is already a negative number FF
-DC -E3
2; Ej = -lD 16 = -29 10 1C
.!...1 .!...1
24 = -DC 1D
+E3 DC is already a negative number too FF
07 -DC
DC = -24 16
Therefore -DC = 24 16 = 36 10 2;
.!...1
24
To verify. EJ - DC = 07
OR -29 - (-j6) = 7
This shows that subtraction is valid with positive negative or
mixed numbers. Errors will occur if the result goes beyond the
range of -128 10 to 12710, the limit of an 8 bit signed number.
1-31
Now calculate 57 -2C and verify your answer in decimal.
FF To check 57 16 = 5 x 16 1 + 7 x 16 0 = 80 + 7 = 87 10
/
-2C 2C =2 x 16 1 + 12 x 16 0 = 32 + 12 = 44 10
D3
Total 43 10
U
D4 2B =2 x 16 1 + 11 x 16 0 = 32 + 11 = 43 10
ill
1 2B
1-32
As a variation, let's reverse the data in the last
question. Calculate 2C -57.
D5 or -2B FF
- _ _--J/
.:.i'Z
A8
U
A9
+2C
D5
But D5 is a negative number. To find its positive equivalent.
FF
-D5
2A
.!.J.
-2B
Therefore D5 = -2B, the same answer but the opposite sign,
compared to the previous question, since the data was reversed.
1-))
To complete this section let's review it all within
several questions. Given two decimal numbers, 47 and 7),
calculate the sum by converting to hex, adding, then converting
back to decimal. Verify by decimal addition.
2 I 47 101111
--_-----.J/ 2 17)
1001001
2 I 2) +1
= 00101111 2 l.1£ +1
= '---v--
01001001
2 l!! +1
=
~~
2 F 2 W +0
= 4
.-....--'
9
2 U +1 2 L...2 +0
2U +1 2 L2t +1
2 L! +0 2 U +0
o +1 2 L...! +0
o +1
2F
+49 start 1 2 J 4 56 7 8 9
~-v--.""~I"~V~
78 F 0 1 2 J 4 5 678
l~ 8 0
:7 x 16 1 = 8
x 16 = ill
120 10
1-34
Now perform the following decimal subtraction 83 -52
by converting to hex, subtracting, then converting to decimal.
Verify in decimal.
_ _ _-----J/
2W 2 L2£
2 I 41 +1
1010011 110100
2 ; 26 +0
2 I 20 +1 = 53 16 2 W +0
= )4 16
\.
2 L1Q +0 2U +1
2U +0 2U +0 FF )
2 U +1 2 L.! +1 ~/
2 L.!. +0 o +1 CB
a +1 .:!:..-!
CC = -)4 16
ill
1 1F
1
1F =1 x 16 + 15 x 16° = 3110 At last! It agrees.
1-35
Binary subtraction is not essential if you can subtract
in hex. However it is included to complete the arithmetic
operations in both formats. From a previous hex example,
D -5 = 8
D = 13 = 1101 1101
5 = 5 = 0101 -0101
1000
As in hex subtraction start with the number to be subtracted,
0101 in this example. Complement it , that is each 0 becomes 1
and each 1 becomes o. Then add 1. This will produce the neg-
ative value of the original number (-5 = 1011 below).
0101
becomes 1010
plus 1 .:!:..-!
= 1011 =-5
Now add the +1101 ill
minuend 1101 1 1000 = 8
{overflow or carry is ignored.
This subtraction is limited to 4 bits as shown above.
Now calculate 12 10 - 7 10 in binary.
12 10 = 1100 -7 = 1000
--------'/
1100 = 12
710 = 0111 + 1 ~+1001 = =2.
1001.-J 1 0101 = 5
10110100 = 180 10
complemented = 01001011
plus 1 + 1
01001100 = -180 10
+11010111 + 21 510
1 00100011
( overflow 35 10
If your data is in hex form already it is more direct
to subtract in hex. If the data is in decimal and conversion
has to be made to binary first, it is your choice whether you
subtract in binary or hex. If the answer is needed in hex, then
hex is preferred.
1-37
Here is the last question for this chapter. Calculate
in binary.
10110100
-11010111
-------'/
CLR B, which in machine code is 5F. This instruction
can be written CLRB, omitting the space. Similarly CLR A can be
written CLRA. Machine codes for all assembly language instructions
are provided in Appendix C, at the end of this workbook. Instruc-
tions involving accumulators are on the first page of Appendix C.
--
2-2
If a hex value such as 2C is to be loaded into accumulator
A the instruction is
LDA A #$2C (LDA A = LoaD Accumulator A) •
The # symbol denotes that data follows immediately within the
instruction. The $ symbol denotes that the data is in hex format.
After this instruction is executed, the contents of ACC·A is
---' '-- C .7
Contd. • •
2-2
Contd .
•
Such an instruction requires 2 bytes of machine code.
LDA A, when followed by the # symbol is known as an immediate
mode instruction; its machine code, 86, is found under the
"IMMED" column, opposite LDAA in Appendix C. The second byte
of the instruction contains the data to be loaded, 2C. Hence
86 2C = LDA A #$2C. Write the assembly language instruction
and machine code to load ACC B with the hex value 7D.
LDA B #$7D C6 7D
---~/
Appendix G summarizes the use of special symbols such as # and $.
2-)
Write the instruction to load ACC A with the hex value
4D. Also write the machine code.
LDA A #$4D 86 4D
--_--....../
t 86, the machine code for the "operator" part of
the instruction is also known as an operation code, commonly
called the "op code".
The operand value, 4D, is also the code for the letter 00,
based on the ASCII (American Standard Code for Information
Interchange) code, listed in Appendix B at the back of this
workbook.
For practice use this table now to confirm that the
ASCII code for Z is 5A, under column 5 opposite row A.
LDA A #$38 86 38
~from Appendix B -ASCII codes.
The ASCII codes for the numbers 0 to 9 are easy to remember,
being 30 + N where N = 0 to 9.
LDA B #'7 c6 37
' - opposi te LDAB under Il'fiMED in Appendix C
2-6
.--.
Now write two instructions, the first to load ACC A with
the hex value OF, the second to load ACe B with the ASCII code
for the letter F (using a literal). For each instruction provide
the machine code on the left side of the assembly language
instructions.
_ _ _----.J/
86 0F LCtA A #$0F
C6 46 LCtA B #"'F
The first instruction loads a hex value, OF, into Ace A.
The second loads an ASCII code for the letter F into Aec B. If
the difference is not clear, please reread the question and answer.
If the above two instructions were executed in the order
listed ACe A would take on a value, OF, and ACC B a value of 46.
This example although trivial shows the beginning of a program,
a series of instructions executed by the computer which modifies
the contents of an accumulator or a memory location (discussed
later).
.....
2-7
Write the assembly language instructions to load ACC A
with the ASCII code for A and load ACC B with the hex value OA.
For each provide the machine code.
Again note the distinction between a hex value and an ASCII code.
The above machi~e code and instructions are part of an
assembler listing, the printout produced by the assembler when
converting assembly language instructions to machine code.
The addition of 2 hex values, JF and 27,
........
2-8
in ACC A can be
performed by
4F elf;,: A
E:B 3F ADD A #$3F'" (Adds JF + 0 = )F in ACe A)
88 27 ADD A #$27 - (JF + 27 = 66 16 in ACC A)
\....Y-' .~
machine assembly
code language
instructions
Rewrite the. above, using 2 rather than J instructions, again
providing the machine code.
~
2-9
The memory of a computer, where data is stored, can be
envisaged as a series of mail boxes, each with a 4 character hex
address, e.g. l4D5, and the capability to store one byte of data.
'rhe instruction
LDA A $12B7 (no # this time)
loads ACC A with the 8 bit contents of address 12B7, without
destroying the contents of 12B7. Such an instruction is known as
an EXTENDED mode instruction, requiring one byte for the operator
(LDA A) and 2 bytes for the operand ($12B7). Hence LDA A $12B7
becomes B6 12B7. The B6 is found under the EXTND heading, opposite
the LDAA instruction in Appendix C. The total number of bytes
required {J} is found two columns to the right of B6, under
the # column.
Contd •.•
2-9
Contd.
Write the assembly language instructions and machine code
to load accumulator B with the contents of address 06E4.
2-10
Write the assembly language instructions to add the
contents of memory addresses lCOO, lCOl and lC02, the answer
residing in ACC B. ...Provide the machine code •
F6 1.cee
FB 1.C01.
LDA B
ADD B
$1.C~10
$1.CI2I1.
-------'/
FB 1.C"32 ADD B $1.Ce2
2-11
The accumulators are used for many purposes within a
program. Data, after being processed in an accumulator, usually
is stored in a memory location, e:',g.,
STA A $064c
which stores the contents of ACC A in address 0640 but does not
destroy the contents of ACC A. This instruction, referencing a
4 character hex address, also is "extended" mode. Write the
machine code for the above instruction.
87 12164(:
_ _ _--J/
·l.. address
·'
STA A (extended mode)
2-12
Write the assembly language instructions and machine code
to add the hex contents of addresses 14no and 14D1. then store
the sum in address 14D2. without using ACC A.
If 14no contains JE (14DO/JE) and 14Dl contains B5 (14Dl!BS) ,
what will the hex value in address 14D2 be when this program is
executed?
_ _ _--J/
F6 1.400 LOA B $14De (ACe B/JE)
FB :1.40:1. ADD B $1.4D1. )E + B5 = F)
F7 1402 STA B $1.4D:2 14D2/FJ (ACC B still contains F3)
JE
+B5
start 1 2
FJ
BeD E FlO
start-~4
2-1J
To place a particular value in a particular memory address
it is first necessary to set it into Aee A or B. With this in mind
write the assembly language instructions and machine code to put
the hex value JB in address 12EJ.
_ _ _--J/
86 3B LDA A #$38 }_ assuming use of ACC A.
87 1.2E3 STA A $1.2E3
Such a procedure is known as initializatio~, providing a particular
memory address with an initial value, for use during a program.
2-14
Write the assembly language instructions and machine code
to initialize address 0439 with the ASCII code for the letter G,
with the computer providing the ASCII code.
86 47 LDA A # ..013
---~/
87 (1439 STA A $0439
Again it is not necessary to memorize the machine code for the
instructions. However, the 86 and B7 values will soon become
quite familiar.
2-15
The instruction SUB A $1524 subtracts from accumulator A
the contents of address 1524. Write the assembly language
instructions and machine code tOI
(a) ADD the contents of addresses 13C4 and 13C8
(b) then SUBTRACT from this the contents of address 13CA
(c) then STORE the result in address 1)CC.
#$3C
_ _ _-----J/
86 3C LDA A
40 NEG A
87 1!:195A STA A $t195A
2-17
Memory addresses referenced in an instruction normally
require 2 bytes (4 hex characters) to describe them, e.g.,
LOA A $12A6, requiring an EXTENDed mode instruction. Memory
addresses below 100 16 require only 1 byte to describe them, as
is seen in a DIRECT mode instruction, e.g.,
LDA A $4A
which loads ACC A from address 004A. The machine codes for
DIRECT mode instructions are in Appendix C. For the above
instruction the machine code is
96 4A
'-v-' "-v-'
address 004A
L LDA A (DIREC'! mode)
Contd ..•
2-17
Contd .
•
STA E: $66
---_/
Aside from requiring fewer memory locations to stor~ the
instruction a DIRECT mode instruction requires fewer machine
cycles to execute as seen in Appendix C. Large programs often
use addresses below 100 as a "scratch pad" storage area, e.g.,
for storage of counter values, or temporary storage of a byte
of data. Use of this area of memory saves memory bytes and
reduces execution time.
2-18
The instruction TAB transfers the contents of ACC A to
ACC B. Similarly TBA provides the reverse transfer. Using as
few instructions as possible, swap the contents of the two
accumulators. Memory addresses below 100 16 are available (use
DIRECT mode only). Write the assembly language instructions
and machine code.
97 50 STA A $50
---_/
(or your choice of address)
1.7 TBA
[)6 5~3 LDA E: $5~ (or your choice of address)
Q) 50~
A/ B
4Q)
Counter-clockwise execution of the above flow diagram would
utilize TAB (op code 16).
2-19
Accumulator A can be incremented (1 is added to it) via
the instruction
INC A (INCrement accumulator A)
for which the op code is 4C.
Similarly DEC A (DECrement accumulator A) will decrease
its contents by 1. Its op code is 4A. Accumulator B also can
be incremented or decremented.
Calculate the contents of each accumulator after the
following instructions are executed.
CLR A
CLR B
INC B
ADD A #$2C
ADD A #$16
TAB
NEG A
INC A
Ace A Ace E:
/
ClF.: A fl
ACC A/BF ClF.: E: 0 f,1
ACC B/42 INC E: f:t i
ADD A #$2C 2C :1
ADD A #$16 42 1
FF TAB 4'-'
.::. 42
NEG A BE 4'-:'
0::..
-42 INC A ElF 4'::-
..-
BD
.±-!
BE
Therefore -42 = BE
2-20
Sometimes it is necessary to clear (force to 0) or set
(force to 1) specific bits of an accumulator, without disturbing
the other bits of the accumulator. This is accomplished via the
AND and ORA operating on the accumulator. The AND instruction
clears specific bits while the ORA instruction sets specific
bits. The instruction
AND A #i5A (machine code 84 SA)
performs the "logical AND" operation (not addition) bit by bit
with ACC A and the data SA being inputs and ACC A holding the
result.
In the "logical AND" operation each bit of the result
will be 1, if and only if both the corresponding inputs are 1.
Looking first at bit #7, below, one of the two inputs has a zero.
Therefore bit #7 of the result is zero. Complete the bottom line
showing the contents of ACC A after the AND A #$5A instruction is
executed ~
bit #7 J r11 1 1 0 1 1 0 0 - ACC A (before)
0 1 0 1 1 0 1 0 -5A
0 ,- ACC A (after)
---~/
1 1 1 0 1 1 0 0
0 1 0 1 1 0 1 0
0 1 0 0 1 0 0 0
~
"'---
\. \. \,,, 1.. at
~_.;z-..-_ _--a._-.a_~
result
leas is 0 since
t one of the
inputs is o.
2-21
If address 14A2 contains 70, what will ACC A contain
after execution of
LDA A $ 14A2
AND A #$BF
JC
7 6 543
----/
2 1 o ...-bit II
7C = 0 1 1 1 1 1 0 0 L{Contents of 14A2
BF = 1 0 1 1 1 1 1 1 to ACC A
0 0 1 1 1 1 0 0 = 3C
Bit #6 is guaranteed to be zero regardless of the contents
of address 14A2 since the "mask word", BF contains a zero in bit #6.
The result can be shown as
XOXXXXXX
where X denotes the original data in ACC A before the AND
operation. If the purpose of this operation was to clear bit #6
of the data in address 14A2, the modified data would then be stored
back in address 14A2 by another instruction, STA A $14A2.
2-22
Write the assembly language instructions and machine code
to clear bit #3 of the contents of address 1256.
~I---X-=---~~~-T--~
~ contents
same as before
2-23
Similarly all bits, except a specific bit, of a
particular. address can be cleared by the appropriate "mask word".
Write the assembly language instructions and machine code to clear
all bits, except bit #6, of address 065E.
86 f165E
84 40
LOA A
AND A
$065E
#$40
---_/ iii bit #6
87 t165E 5TA A $~~165E (40 = 01000000)
Since only bit #6 of the mask word = 1, then only bit #6
of the original contents of 065E will be retained. All other bits
of the result will be zero. This technique will be used extensively
later in this workbook.
The above AND instruction could be rewritten in terms of the
binary value of the mask word e.g.,
AND A #%01000000
The % symbol indicates that a binary value will follow. This form
is often useful to both the programmer and the user in quickly
determining which bits are cleared.
----
2-24
An ASCII code, produced by an external device, such as
a keyboard requires only 7 bits to describe it. The 8th bit
(bit #7) may be 1 or 0 depending on the particular data source.
Assume that an ASCII code is now in ACC A. Write the assembly
language and machine code instruction to clear bit #7 of the
ASCII data. Use the binary version of the mask word in your
answer.
84 7F AND A
_ _ _----J/
#~0~11~~1~
2-25
We have seen how to clear specific bits. Let's look at
a method to set specific bits. For this purpose the "logical
OR" operation is used (sometimes called INCLUSIVE OR). Given 2
bits as inputs the logical OR output will be 1 if either the
first input OR the second input OR both inputs are 1. Stated in
logical form ~z ~C + D ~
output 'one tlOgical other
input OR input
symbol
(not addition)
The instruction ORA A #$08 will perform the logical OR operation
with ACC A contents and the mask word, 08, as inputs. The result
will reside in ACC A. If 144A contains $CA, what will be the
result after execution of
B6 144A IDA A $144A
8A 5C ORA A #$5C
Contd •..
2-25
Contd.
2-26
Write the assembly language instruction and machine
code to set bits #2 and #7 of the data in address o6A4, without
changing the other bits of this data. Use binary format for
the mask word.
$€16A4
----_/
86 06A4 LDA A
E:A E:4 OF.:A A #~'-;::t ~~1 ~j~~UZ1:t (10
87 €16A4 STA A $06A4
86 1.6D6
E:A 08
LDA A $1.6D6
#:..~00001.000
----'/ set bit #3
OF.~A
A
84 DF AND A #~·~:11.0:11.:111 clear bi t #5
87 16D6 5TA A $:16D6
Ix [ X 10 I X 11 I X I X I xl
X = unchanged bit
2-28
Once more now! Set bits #7, 6 and 2 of address lA42
and clear bits #1 and 4. Assume that each bit controls the
lights for one room in an 8 room house. Provide both assembly
language and machine code instructions.
86 1.A42
:3A C4
LDA A
OF.: A A
$:lA42
#~'~110~~n~1100
------'/ (Set 7, 6 and 2)
f=:~+ ED AND A # ~.~ 1 :11€1:11 ~~11. (Clear 4 and 1)
6''7
.'1 :1A42 5TA A $:1A42
LDA A
STA A
#$3C
COUNT~:
------.;/
3-2
......
Symbolic addresses generated by the programmer can be
up to 6 characters long, the first character being a letter and
all subsequent characters being a letter or a number. It is
good practice to choose a symbolic address which describes the
function, COUNTR perhaps being a counter to keep track of the
number of events that take place when the program is executed.
The only illegal symbolic addresses are A, B and X, the first two
being previously assigned to accumulators. Single letters for
symbolic addresses are almost meaningless and should be avoided.
Write the assembly language instructions to set bit #5
of STATUS, without changing any other bits.
LDA A STATUS
ORA A #~0010e000
STA A STATUS
Only after the above instructions are assembled into machine code
will we know the absolute address for STATUS.
When the computer assembles an assembly language -
)-)
_ _ _----J/
ORG $1200
:1.200 86 124E LOA A STATUS
1.2~13 84 EF AND A #~~1.1.1.011.11.
1.205 87 1.24E STA A STATUS
J
I
I
0243 0001 COUNTR RM8 1
040f1 OF.:Ci
_ _ _-----.J/
$t:.::140~Z1
t14~~1t1 86 A4 LDA A #$A4
13402 E:7 ~3462 STA A [)ATA5
NAt'1 CiENPF.:O
ind"ented one OPT 0 .. 5
space to start OF~Ci $12~n3
in the Operator LDA A STATUS
Field. AND A #;'~:1:1.:t€11.1:t1
STA A STATUS
for Label Field r
start first column : It is legal to
, ORG $:t24E .-:..-.-
have more than
'~STATUS FeE: $FF one ORG directive
END
within a program.
t
1
st
column
The comment field, mentioned on the previous page,
permits entry of comments to improve the readability of a
---
3-6
program. e.g.,
L.DA A #$2£1 INITIALIZE NUVALU
STA A NU'·.. ALU WITH :2£1 (DECIMAL
.... Commenf Field
Such comments are ignored by the assembler but printed on the
resultant listing. One space is all that is needed to separate
such a comment from the operand field.
A good program should begin with a brief description of
its purpose and perhaps some of its internal details. Whole
lines of comments are legal if the * symbol appears in column 1
of each comment line. These too are ignored by the assembler
but printed on the listing. Both examples are seen below.
:+:
:+: PROGRAM TO OUTPUT TEN CHARACTERS
:+: TO THE LINE PRINTER.
:+: VERSION 38 77/11/12 RWS
:+:
LDA A #$£1A INITIALIZE COUNTER
STA A COUNTR WITH 0A (1£1 DECIMAL)
NAM HEXCHK
OPT 0. 5
ORG $04013
•I
I
I
Program
Instructions
END
NAt1 Cl~:ALl
_ _-----...1'/
OPT 0., S
OF.:G
:+:
:+:ClRALl. . . CLEARS 80TH ACCUMULATORS.
:+:
1214121121 4F ClR A A TRIVIAL PROGRAM
121401 5F ClR 8
END
To save space in this workbook the directives will not normally
be shown in the listing, but will be assumed.
Note that END only tells the assembler that this is the end
of the program. It does not halt the program, when it is
later executed.
)-9
~
I I
016B 0160 016D ~memory address
Machine codes for Index Register instructions are on the second
page of Appendix C.
Write the instruction sequence to initialize 2 bytes of
memory, 14C4 and 14C5, with the hex value 0640. Include the
corresponding machine code.
----/
CE 2C4e LDX #$2C4121
FF 1.C8£1 ST>:: $1.C8121
~
A symbolic rather than an absolute address may be used
to store the value, e.g.,
(a) Why does the above example use RMB 2 rather than RMB 11
(b) Initialize a symbolic address POINTR with the hex value
1C60. Omit machine code this time.
_ _ _---J/
(a) 2 bytes are necessary to store the 2 byte value 1506.
(b)
LD>:: #$1.C6£1 (an lOOMED instruction)
,
ST>~ POI NT F.: (an EXfND instruction)
I
POINTR F.:t18 2
lB3J
-----------"/
Since MESSAG corresponds to address lB34, then
MESSAG-l corresponds to address lBJJ.
132130 CE 1833 LDX #MESSAG-1 ~ lMMED MODE (USES #)
121203 FF 186A STX POINTR
4-6 ~
19FF
_ _ _---i/0)80
lAOO -1 = 19FF, one The 2 byte contents of
address below lAOO, TOPBLK and TOPBLK+l is
now stored in MEMPNT 0)80, now stored in
and MEMPNT+ 1. MEMPNT and MEMPNT+l.
4-7
~
The instruction
CLR :3,X
is interpreted as "Calculate a new address which is the sum of
the Index Register contents and the offset, :3 in this example,
then clear that memory address." The above instruction could
be written as
CLR $3,X
although the $ is redundant for values of 7 or less.
If the Index Register contains l)E4, what address has
its contents cleared by CLR :3,X?
_ _ _---.-.1,/
l)E7 X/ 1)E4
+ 3
IJE7 = address operated upon by CLR ),X
4-9
..--
Machine code for Index mode instructions are found
under the INDEX column in Appendix C. Note that
LDA A 3,X (op code A6)
requires 2 bytes as seen by the 2 under the # column, 2 columns
to the right of A6. What does the second byte denote? Take
a guess. Attempt to encode the above instruction in machine
code.
--------/
The second byte contains the offset value, 03 in this case,
e.g., ~~~
LDA A offset
(Index Mo de)
4-10
The 2 byte contents of the Index Register can be
incremented (1 is added to it) via the instruction
INX INcrement indeX register (08)
Similarly, DEX DEcrement indeX register (09)
will decrement it.
Write the assembly language instructions to increment
the contents of MEMPNT which now contains the hex value 19FF.
What will its new contents (2 bytes) be after the above
incrementing?
_ _ _---J/
LD>!. t'1EMPNT
INX
STX t'1EMPNT
t
I
I
MEMPNT RMB 2 (If not already present in
the rest of' ·the program.)
This J line sequence will be used many times in this workbook to
increment a 2 byte value in memory. Note that the Index Register
(X) still contains the incremented value, 1A00 in the above
example, after STX MEMPNT is executed.
LD>c:
STX
#MESSACi-1.
POIr'~T~:
-----/
I
I
I
POINTR Rt1B 2
4-12
Store the message "ENTER DATA" in memory headed by the
label MESS04, and terminated by a null. Initialize MESPNT with
the address one below the start of this message.
LDX #MESS(14-:1.
-----/
ST>!' t'1ESPNT
I
I
I
t1ESF'NT F.:t1B 2
t'lESS04 FCC /ENTEF.: DATA/
FCE: ~~1
4-13
What will be the contents of ACe A after execution of
the instructions shown below?
LDX #MESS04-1 INITIALIZE POINTER WITH
STX POINTR ADDRESS MESS04-1
LD::'!, PO I NTF.:
I~~i::
ST::< PO I NTF.:
>!, GET CHAR VIA :;.~
LDA A
~
i
POI NT F.: Rt1B .-..::.
MESS(14 FCC .·...ENTER DATA/
FCE: ~3
4-14
If address 12A6 contains C4 (12A6 / c4) the instruction
LDA A $12A6
loads ACC A with C4, the contents of address 12A6.
If address 14AS and the next address contain l2A6
(14A5 / 12 and l4A6 / A6) then
LD::·:: $:14A5 X.·...:12A6
LDA A ::.~ A,.··C4
also places e4 in ACe A, this time via an "indirect" manner,
with X containing the address of the data, 12A6, after execution
of LDX $14A5. Hence this is commonly known as an "indirect" or
"deferred" memory reference.
This process can be extended further. Given the
following initial conditionsl
lCSO / l4A5
14A5 / 12A6
l2A6 / C4
the instructions
LDX $lC50
LDX X
LDA A X
----....,,/
in address 13C4. The best way to interpret this instruction is
"store the data in Accumulator A via X". that is X points to the
destination •
4-16
~
---_/
B5 Accumulator B is loaded via X, that is from the address
now in X. This time X points to the source of the data.
is executed?
----/
The contents of Accumulator A is compared with the contents
of address 267E.
BRANCHING - ASSEMBLY LANGUAGE -5-1
---~/
No branching will take place. The next instruction executed
will be the one following BLS BADHEX.
-5-2
If the first test was passed (no branch since the ASCII
value was 30 or greater), the next test is to check for values
greater than 39, the ASCII code for 9. If the value is 39 or
lower, the program should branch to NUMOK. otherwise it should
continue. Write the instructions to ·do this noting the avail-
ability of the instructions I
BLS - Branch if Lower than or Same
BHI - Branch if HIgher than
BRA - BRAnch unconditionally.
Ct'1F' A #$39
---_/
BLS NUt10K o TO 9. VALID HEX
or
NUt'10K SUB A #···0
BF.:A GOODH::<
We now have a
HEXCHK Ct1P A #$2F
E:LS E:ADHE:X: t'lUST BE E:ELOl,~ 3:(1
Cr'1P A #$3S1
E:LS NUr'10k: NUST BE 3:~~1-3:9
GOO[)H:X:
I
I
I
NUNOK SUE: A #$30
BRA GOODH:X:
BADHE>::
Now scree n for valu es A to F. Vali d char acte rs in this
grou p shou ld be conv erted from thei r ASCII code to thei
---
5-4
r true
hex valu e, e.g. , OA when A is struc k. For valid char acte
rs
cont inue to GOODHX, the next line, afte r this conv ersio
n. For
inva lid char acte rs branc h to BADHEX.
_ - - - - .. J /
cr'lP A #$40
BLS BA[)HE >~ t1UST BE 3:A-4~3
Ct1F' A #$46
BHI BADHE>:: MUST BE GREATER THAN 46
SUE: A #$37 41-46 NOt,J 0A-~3F
END OF ROUT I NE.
The ASCII code for A is 41, for whic h the hex value is
OA. The ~i:ference is 37, whic h when subt racte d from 41
give s
us OA. S~m~larly when F is struc k, 46 - 37 = OF. Calc
ulati ons
are shown below .
FF 41 When A is struc k 41 ASCII for "A"
-OA .+F6 +C9 (-37)
U
F5
F6
j
= -OA
1 37 1 OA
~ hex code for A
HEXCHK) iss
The fina l versi on of this routi ne (let 's call it -
5-5
---_/
When 0 is struc k on the keyb oard the ASCII code 30 woul
d resu lt.
The firs t 2 line s would then cause a bran ch to BADHEX (norm
ally
reser ved for inva lid char acte rs), since BLS BADHEX reco
gniz es
that the code prod uced is the same as 30. Such an erro
r where
a bran ch instr uctio n is inco rrect for one valu e, is very
common.
Hence a progr amm er shou ld manu ally check for boun dary valu
es,
0, 9, A and F in the abov e progr am.
The labe l GOODHX could prov ide an instr uctio n JMP NEXT,
jump ing to the next progr am segm ent. The BADHEX secti on
could
be temp orari ly term inate d by the instr uctio n BADHEX BRA
BADHEX,
an instr uctio n whic h loop s back to itse lf, prev entin g exec
ution
of "lef t over " code in that memory addr ess.
Modify this HEXCHK program to include the necessary --
.5-6
NAr'1 HE::-::2C
-------'/
OPT 0 .. 5
OF.:G $1E4€1
HE;<:CHI< Cr'1P A #$;~:F
i
I
BADHE>::
END
Note that all 4 directives appear in the operator field. The
first label of the program does not have to agree with that
used with NAM. The latter usually designates which version is
listed, e.g., version 2C in this example. Updating the version
number when changes are made is a very effective way of denoting
which listing is the latest, an absolute essential as programs
evolve.
~
.5-7
To understand better how the branch instructions
operate one must be aware of the Condition Code Register (CCR)
in which each of the 6 assigned bits may be set or cleared
according to each instruction executed.
5 4 :3 2 1
'I""----::'-~---+----r-__+-__r-
°....-Condition
__- _ _ r
bit #
Code
Register
For example bit #0 is the CARRY or C bit which will be set if an
8 bit addition produces an overflow, the C bit behaving as the
9th bit. The C bit can be set under other conditions, seen later.
Bit #1, the oVerflow or V bit, is set if a 2's complement
(signed number) arithmetic operation produces an answer exceeding
the range of -128 10 (8016) to +127 10 (7F16)' the available range
using an 8 bit signed number.
Contd •••
5-7
Contd.
b
_ _ _-.--J/
Yes branching will occur since C2-3E = 2 (not equal to zero),
clearing the Z bit and causing a branch via BNE MATCH.
~
Will the following instructions cause a branch to HIT
if KEDATA contains 291
LDA A I<E[:.ATA
AND A #$D6
E:NE HIT
NO KEDATA
---_/
= 00101001
D6 = 11010110
LOGICAL AND = 00000000
Since the result is zero the BNE instruction (Branch if not
equal to zero) will not cause a branch to HIT. The Z bit will
be set.
.5-9
.-
The instructions I
LDA A CONTF.:O
E: I T A #$4~~1
E:NE HIE:IT
perform the logical AND on CONTRO and 40, without modifying
ACC A. The CCR bits are affected and branching to HIBIT will
occur if bit #6 of CONTRO = 1 (not equal to zero).
XXXXXXXX CONTRO
01000000 40
t
Bit #6 is only bit of CONTRO tested.
Since the BIT instruction does not destroy the original
contents of ACC A, several bits can be individually tested,
permitting multiple branches.
Write the instructions to branch to RECEIV if bit #0 of
SERCSR is set or to TRANS if bit #1 of SERCSR is set; otherwise
continue.
,
J
5-10
Write the instructions to test bits #2 and 3 of SPEED,
branching to LSPEED if bit #2 is set, to HSPEED if bit #3 is
set or to STOPIT if both bits are cleared. Assume that both
bits will not be set at the same time.
LDA
BIT
A
A
SPEED
#t::€1000:1:1~3(1
--------'/
CHECK FOR 00
BEQ STOPIT
BIT A #~"~€10~3(1(11€1€1 CHECK FOF.: BIT #2=:1
BNE LSPEED
BIT A #~";:€100€1:1~300 CHECK FOF~ BIT #3:=:1
BNE HSPEED
I
I
I
Note that all bits of ACC A, "viewed" via the mask word, must
be zero to set the Z bit of the CCR. Hence both bits #2 and #3
of SPEED must be zero to branch to STOPIT via the above test.
The above instructions could be part of a speed control routine
for a machine, the individual bits of SPEED being controlled by
the machine's push buttons, connected to the computer.
5-11
....-
Further branching operations will.be seen in a program
to clear a group of memory locations. In the program below,
what is the initial contents of MEMADD? What address will be
first to be cleared?
~3200 CE 23FF LOX #$240£1-:1.
0203 FF (12613 STX t'1Et'1A[:. 0
132136 FE ~326e t'10F.:CLF.: L.D:X: t'1Et1ADO
132139 08 INX
e;;:~eA FF 02613 STX MEt'1AOD
132(10 6F 00 elF.: :=-!.
5-12
What address will be cleared when CLR X is executed the
second time? Explain, starting at MORCLR (second time through
here). When does this clearing operation cease?
Address 2401
-----/
When MORCLR LDX MEMADD is executed the second time X contains
2400. After INX, X contains 2401 which is stored via STX ME~~DD.
CLR X then clears address 2401.
This clearing operation will continue until the above
program is partially overwritten (cleared) by its own operation.
We need a method to break out of this loop after a specific
address is cleared. If the suspense is killing you, check the
next page!
5-13
The CPX (ComPare indeX register) instruction compares
. t er con t ent s t 0 some 2 byte reference value, e.g.,
the Index Reg1s
CPX #$24C7
or CPX HIVALU
Only 2 branch instructions are valid after CPX, BEQ or BNE.
Modify the previous program to exit from the loop after
address 240F is cleared.
---~/
02(U~1 CE 23FF LD::< *$240~1-1 THIS F'F.~OGf;.:At'1 CLEAF~S
€1203 FF ~1260 ST:X: t'lEt'lAD(:t AND LOOPS BACK
(12~~16 FE €1260 t'10RCLF.: LDX t1Et'lA[)(:l UNTIL t1Et10F~'T' A(:l(:tF~ESS
€1209 ~j8 IN>:: 240F Ie-' CLEAF.:ED
020A FF ~3260 ST::< t1Et1ADD AFTEF~ I.oJHICH E~,::IT
1!:12~3D 6F e0 CLF.: X TAKES PLACE
0;;::0F E:C 240F CF'>~ #$240F
0:.:!::1.2 26 F2 E:NE t10RCLR
:+:
026~3 OF.:G $~1260
~3260 ~~U~102 t1Et1ADD F.:t1B 2
END
While it is true that the Index Register could remain the pointer
throughout this program, without using MEMADD, we are looking
ahead to programs where the Index Register is used for several
purposes inside one loop, requiring retrieval and storage of
each memory address pointer each time it is used.
How many memory locations will be cleared by the
previous program?
---
5-14
10 16 or 16 10
---_/
After CLR X is executed JL Hof addresses cleared
1st time 2400 1
2nd time 2401 2
Jrd time 2402 3
2462
2463
5-16
....-
What would be the effeci~ if the label MORCLR appeared
opposite the first instruction, e.g.,
MORCLR LDX #$2400-1
rather than in its present location? Refer back several frames
for the program.
---_/
The program would be re-initialized after each loop, hence it
would clear address 2400 each time in a continuous loop. This
is a fundamental error which ev~ybody makes at least once,
including you and me. The only question is when. More important
though is to be aware of this potential problem. The solution
can be summarized by
LOOPBACK IS ALWAYS BELOW INITIALIZATION
Initialization in the previous program sets up MEMADD with 2;FF,
its initial value. The program loops back to MORCLR, below the
initialization in the original program.
5-17
Good programming requires good planning. While many
planning methods are advocated today, one of the simplest and
most effective is the flow chart, shown below.
CLEAR
MEMORY
IU)I>R£SS
HAMEl> v,~ X
---------/
IN'T"'L.12G
- COUNTE'I\
- ADI>P.'ESS
CLEAR MSM
AOl>RESS
DECReME'NT
COU~Te~
useful to you.
--_-----..1/
NAt'1 t1Et·1CLF.:
OPT 0 .. S
1218121121 OF.:G
COUNT could have been incremented from 0, exit taking place when
count equals 64. Down counting is preferred since it is easier
to detect zero than a specific value (CMP A #$64). Both,
however. are valid.
5-20
In the previous program the task was to clear an
address. In the next program the task is to count the number
of addresses, 0900 to 09FF inclusive, which contain zero. This
time the task itself will contain a decision, to count or not
to count. First flow chart, then write the program.
C\.1:AR COUNTeR.
".'T f\DDRESS 0200 7F 0262 ZCOUNT CLR
---~/BLANt< EMPTY COUNTER
POIMTSR 0203 CE 0::::FF LD:~ #$1219£1£1-:1
1212"~6 FF ~126121 ST::-:: t'1Et'1PNT INIT ADDRESS POINTER
0209 FE €126£1 t'10~:CHK LD;:':: t1Et'lPNT
020C £1:::: IN:>::
X =UPbATE.D £12£1(:1 FF 026121 ST::-:: t'lEt1PNT GET NEXT ADDRESS
ADDRESS "32:1£1 A6 "3121 LDA A X GET ITS CONTENTS
£12:12 26 ..3 3 BNE SKIPIT NOT ZEF~O
1212:14 7C 121262 INC BLANK GOT ONE
02:17 E:C e9FF SI<IPIT '::F·::.:: #$"~19FF LAST ADDFi:ESS?
02:1.A 26 ED BNE t10RCHK NO. BACK AGAIN
I
•
13260 OFi:G $0260
f126"~1
1210132 t'1Et'1F'NT ~:t'1E: 2
0262 00€1:1. BLANK Rt'18 :1.
END
Before
---~/ After
Before
---_/ After
INC BLANK+i LDX BLANk:
BNE SKIF'IT IN>~
INC BLANK STX BLANK
SKIPIT SKIPIT
\NIT
_ _ _-----01'/
GETCH~: EG~U $~F00
"1)1)RESS
PO'"T&~
STOASC... STORES ASCII CODES FROM KEYBOARD
IN SUCCESIVE MEM ADDR STARTING AT ~200.
! TERMINATES PROGRAM.
CALLS GETCHR. USES A AND X.
GET CHAR
STOASC L[:'~: #$1.2f10-1.
5T:)~ ADDRES INIT POINTER
GETt'10F.: JSF.: GETCHR GET ASCII CODE
Ct'1F' A # .., !
BEG! ALLDUN MUST BE !
LD::"~ A[:'D~:ES
IN::·::
ST::< ADDRES UP[:'ATE ADDF.:ESS
.....
STA A .... AND STORE ASCII CODE
x: UPoA,eo BRA GETt10~: AN [:a BACK AGA I N.
PO'HTER ALLDLIN
STORE CH"~
ADDF.:ES Rt1B 2
Here the test takes place before
the task, to avoid storing the !
character.
5-24
Branching instructions recogn~z~ng signed (~) values area
BGE - Branch if Greater or Equal
BGT - Branch if Greater Than
BLE - Branch if Less than or Equal
BLT - Branch if Less Than
BPL - Branch if PLus
BMI - Branch if MInus
Flow chart a program to count the number of
occurrences of values between! 26 16 inclusive, within the
memory range 0800 - OBFF inclusive. Manually check your program
for proper branching for values of !26 and ±27.
---_/
SET up L'M'TS
I N I" ADOk POINT
CLEAR COUNTER
)(: UPDATED
ADDRESS
GET CONTE"'TS
5-25
From your flow chart on the previous page, write the
program.
_ _ _..----.J/
MEMCHK. . . COUNTS OCCURRENCES OF +26 TO -26 HEX
IN MEM ADDR 0800-0BFF INCLUSIVE
£12';:'0
02710 '3(11.::11 LOLIt.., F~t"'B 1
10271 121001. HILIt1 F.:t·lB :1
0272 a;:n3f12
0274 ~:::n3f12
t1Et'lPNT Rt1B
HIT F.:~18
.-.2
~
5-26
Previously we saw how to store a message in memory. It
is time to print such a message. For now, assume that the
instruction JSR PRINT, a subroutine call, prints the contents of
ACC A as one ASCII character on a printer. Assume that the
label MESSAG heads a stored message, in ASCII format, terminated
by a null. Flow chart and write a program to print this message,
using the JSR PRINT instruction. If you are stuck, look at the
first two instructions of the solution
IN'T POINTER
------J/
MESSPR... PRINTS MESSAGE THAT IS STORED IN MEMORY.
CALLS PRINT SUBROUTINE FOR EACH CHARACTER PRINTED.
TO MESSAGE
USES A AND X PLUS PRINT SUBROUTINE.
Lei?::
t1ESSPF.~ #t1ESSAG-:1.
ST:X: POI~TR INIT MEM POINTER
MORPRT LD>:: POINTF.:
x= UPDATED INX
PO\NTER ST>:: POINTF~ GET ADDRESS OF CHAR
GeT CHAR. LDA A '.J
...., GET ASC I I CHAF.: INA.
BEG! ALLDUN
.JSF.: PF.:I NT PF.: I NT IT
BF.:A t'10F.~PF.:T BACI< FOF~ MOF~E
ALLDUN
I
I
POINTF.~ ~:t1B 2
t1ESSAG FCC . . . FILENAME?...1
FeB o
PR'NT CHAR Note the test before printing
to avoid trying to type a null
which cannot be printed.
5-27
Data stored on a diskette, a magnetic mass storage
device, is usually written in blocks of 80 16 characters at a
time from a buffer. which is a specific block of memory. In
such an operation the X register must be used both for
retrieving data from the "source" memory address and for storing
it in the "destination" address. For this 2 pointers must be
initialized. For each byte moved. each pointer must then be
updated for use by X. With this in mind, flow chart and write
a program to move the memory block 0600 - o6FF to 0800 - 08FF.
I
SOUF~CE F.:t·lE: 2
DEST Rt'lE: 2
END
Earlier we saw how to increment a 2 byte counter
-5-28
without using the X Register. Similarly a 2 byte counter can
be decremented without using the X Register. A special
condition, shown below, exists when the least significant byte
is zero, before decrementing, since both bytes will have to be
decremented this time. Least Significant Byte
.~
Before Decrementing 00111011 00000000
After Decrementing ~ ,111~111.1
Count Count +1
Write the instructions to decrement the two byte counter COUNT,
recognizing the special condition above. The instruction TST
(TeST or "compare to zero") is useful here.
--_----.J/
TST COUNT+1. CHECK LEAST SIG BYTE FOR ZERO
BNE DECLOW IF NOT e IGNORE MOST SIG BYTE
r.. EC COUNT IF LEAST SIG BYTE e DEC MOST
DECLOW DEC COUNT+1 ALWAYS DEC LEAST SIG BYTE
ENDHE:x:
BAC'HE::·(
/
ENDVAL F~t'1B :t
tolEt'1ADD Rt1B 2
103623 ~~TS
/
0618 86 17 INIT LDA A #$17
:+:
e61.A B7 0624 STA A ENDVAL
:+:
1o:::161.D CE €16D7 LDX #$06()7
:+:
e620 FF 121625 ST::-:: t'lEt1ADD
:+:
€i623 39 ~:TS
:+:
0624 ":300:1 EN[)VAL Rt18 1
:+:
1216~~5 01211212 t1Et1A[:t[:' Rt18 2
6-3
...-
In general it is easy to work with the machine code for
the 6800 microcomputer. Only one area, that of encoding branch
instructions, requires extra care. In the instruction sequence.
the code for BNE is 26. The next byte, 06, is a forward
reference to STORTN, 6 bytes beyond the byte following 06.
Better read that again! When the microprocessor has fetched
06 from memory and is processing it, to determine the address
to which to branch, the program counter (PC) contains the
address of the next byte, 1874. It is 6 bytes (hence the 06)
from 1874, the PC contents, to 187A, the address of STORTN.
2 4 5.-~ 6
1873 1874 1876 1878 1879 187A
06
t
while proCeSSing" PC. points
t
address of
this byte :J here STORTN
If STORTN is at address 187E instead of 187A, while the
BNE instruction remains at the same address, what value is in
address 1873, the forward reference to STORTN for the BNE
instruction?
OA
--_---..J/
187E - 1874 = OA ~ branch offset
target address~ addtess following branch offset
-
6-4
Backward branching is somewhat more challenging. e.g ••
/
G>~e
02013 7H :1 60 NOTl.r'ET (:.EC lOOPNO
02€13 @~
2,· F: * BEQ NOT'r'ET
02~35
4(i):J * ClF.: A
*
NEXCHR JSR GETCHR Manually assemble the program
(opposite) using both the first
-6-6
LDX MEt1A[)D
and last methods to determine
IN>::
each branch offset. Machine
STX t1Et1ADD code for JSR GETCHR is BD tFOO
LDA A X and for JSR OUTERM is BD lFOJ.
Start at address 0740.
CMF' A #$00
BEQ ENDLIN
.JSR OUTERt1
BRA NEXCH(;:
ENDLIN RTS
MEMADO Rt'1B 2
/
a.3f1 FF FE 12:1740 BD :1Fa.~n~1 NEXCHF.: .JSR GETCHF.:
*=
FD Fe FE: 12:1743: FE 0756 L[:a>:: t1Et'1ADD
:+:
FA 0746 1.38 IN>~
:+:
F9 F8 F7 (1747 FF 121756 ST>:: t1Et1AD[:a
:+:
'.}
F6 F5 (174A A6 0(1 LDA A .'.,
:+:
F4 F3 1-~174C :=::1 a.3D C~1P A #$&;:1[:1
:+:
F2 F:1 074E 27 1-35 BEG~ ENDLIN
F0 EF EE (1750 B ~
f :1~..z,~.
@* JSR OUTEF.:t'1
:+:
Q) ®
ED EC (1753 20 EB
:+:
EE: (1755 RTS
02~~1'j
3:9
E:1 ~~1A
F.:TS
/
~+.
213(,27 134) ..
11:1 BEG! .JUr'lPNU Changed lines
:+:
02~Z14 BD :1A64 .JSF.: STOF.:E
are circled.
F.:TS
.Jt1P NUCHAF.:
--
6-8
A problem often encountered in writing machine code
programs is the need to insert a few instructions in the middle
of a program. This results in new addresses for all labels
below the insert (on the listing) requiring re-encoding of the
program.
To prevent or minimize such problems it is desirable to
leave memory address gaps between subroutines or program segments,
typically 1/4 the length of the code written. Where instructions
follow one another continuously for more than ten lines, insert
several Nap (No OPeration) instructions (OP CODE 01) which do
absolutely nothing except to occupy memory locations. These are
easily removed when extra addresses are required for later
changes. The only cost is the extra memory used and slower
execution.
When re-assembly is undesirable or impossible a PATCH is
recommended. This involves a jump to some external address,
where the extra instructi"Ons are placed, followed by a "jump back"
to the address just below the first "jump out". The cost is
usually 6 bytes (2 jumps) plus the inserted code. In the program
below a CLR COUNT instruction is needed just after STX ~mMADD.
Modify the program below to patch in the extra instruction
assuming that COUNT is address OOFF and that addresses 0680 - o68F
are available. Write both .the assembly language instructions and
the machine code for the patch.
1361313 CE i34E LOX #$i34E
0603 FF 136213 STX MEMAOO
13606 FE 0620 LOX MEMAOO
06139 08 INX
0620 t'lEt1ADD EG~U $0620
/
0600
0603
0606
0609
0680
0680
CE i34E
7E 0680
FE 062€1
08
FF 136213 PATCH
r
.
LDX
, ~Tt1P
LDX
IN>::
ORG
STX
l$i~4E
PATCH ,
MEt1ADO
$06813
t'lEt1ADD
13683 7F e0FF CLR COUNT
13686 7E 0606 .JMP $0606
The problem below presents a condition where memory -6-9
---~/
Hint. Use branch rather than jump instructions.
Calculations
(1) 0470 - 0405
FFFF
-0405 €14~]f1 BD l.F~::10 JSF.: TEF.:t'l I N
04~(:t ~;~~~~ 68 BF.:A
FBFA PATCH
~14~15 ::::t BACK Ct'lF' A #'·L
+__1 £14-;::'121 OF.~G $047~3
~14?t1 5F PATCH
FBFB CLR 8
0471 84 AND A #$5F
+0470 121473: E:F.~A BACK
006B
(2) 0405 - 0475
FFFF
-~
FB8A
+ __1
FBBB
+0405 Since only 5 locations are available
FF90 . 90 branch instructions (2 bytes per branch)
would just fit. Such situations are quite
common when modifying old programs,
particularly if source listings are
unavailable.
The previous example shows how the program counter
.......
6-10
_ _ _----.J/ lF4D
lF49 :::1 (14 Cr'1P A #$04 +~ lF4F
1F48 27 1-:;'4
lF4D 8D (19
8EG'~
8SF.~ $1F58
$1F3D
-...,,"f"---lF51 _lF58
$:1F5:1 ~~~- +~
'ol~~-LiJ STAll ,-Si\ "', "'1.. .. ~ "'t tl5 q.~ ~7 St'oP ~,\-(iP
a,r P.uT '~,1
Contd.
7-1
Contd.
Let's look at the Data Buffer first, assuming an address
of 7FF5 for the ACIA Data Buffer "SERBUF". This single buffer
services 2 internal buffers, receiving data from the "read only"
RECEIVE BUFFER. and transmitting data to the "write only"
TRANSMIT BUFFER. The same address is used for both buffers
(see below). Hence the instruction LDA A SERBUF automatically
gets its data from the RECEIVE BUFFER. while STA A SERBUF
automatically passes its data to the TRANSMIT BUFFER.
RECe\ve ~\JFFe~
( i).EA 0 ONL.'()
STA A $7FF5
----'/
All that for one instruction!
Symbolic addresses are preferable when working with the ACIA.
lrhe statement
SERBUF EQU $7FF5
directs the assembler to substitute 7FF5 for the symbolic
address SERBUF. To improve readability of programs it is
usual practice to place all "EQU" assembler directives at
the beginning of a program.
Address 7FF4 is known as the Control and Status Register,
described in detail later in this chapter. Arbitrarily it is
called SERCSR (SERial Control and Status Register).
7-2
Write an instruction to read serial data from the ACIA
into Ace B. Assume previous symbolic definition of the Data
Buffer.
LDA B SERBUF
----'/
Note that if STA A SERBUF
LDA A SERBUF
is executed, the data in ACC A will normally change since data
is stored in the TRANSMIT buffer but loaded from the RECEIVE
buffer, even though both carry the same symbolic address SERBUF.
-----
If serial data is being received by the ACIA, some
7-)
LDA A SE~:CSR
----.-,,/
AND A #$121:1
7-4
.....-.
Now add instructions to cause continuous testing of
bit #0 until data is ready, whereupon the data is to be
transferred to ACC A.
---~/
INLOOP LDA A SEF.:CSF.:
AND A #$0:1
BEG! INLOOP
L.DA A SE~:E:UF
lJ)51) .,
-DATA READY.
Reading of the data from the RECEIVE buffer, SERBUF, clears the
READY bit, sometimes referred to as a READY FLAG or DONE FLAG.
A timing diagram of these events is shown here.
few if repeated in a looping
RECEIVER
READY
BIT
bit#
1 ---
microseconds
fJ
~~
n
type program
,...,-__--.A.------..
~
o { 1/ -_........ ---
Goes to 1'\ ~utomatically Data'\ \Data is read
when dat~} cleared when READY into ACC A
is READY data is read, (LDA A SERBUF)
in Data e . g. ,
Buffer. LDA A SERBUF.
Although the rate of transmitting and receiving data bits is fixed
ther~ may be long time gaps between successive characters. Hence
the term "asynchronous" in the ACIA, meaning no specified number
of characters per second.
7-5
. -
Data to be transmitted in serial form by the ACIA should
not be transferred to the ACIA's TRANSMIT data buffer until this
buffer is empty and therefore ready to accept a new byte. Bit #1
of the Status Register is the transmitter's READY bit. When in
the 1 state, it denotes this READY condition.
Write a short program to put the byte now in ACC A into
the TRANSMIT buffer when the transmi·~ter is READY. Warning.
Don't destroy data now in ACC A while testing for the READY
condition.
--------/
$132013
:+:
:+:
:+:
7FF4 SEF.:CSF.: EG!U $7FF4
7FF5 SEF.:BUF EfJ.U $7FF5
:+:
020f~ 86 7FF4 INLOOP LCJA A SEF.:CSF~
13203 84 €Ii. AND A #$131. R:ECE I VEF.: F.:EAD'-r'?
€12€15 27 F9 BEQ INLOOP
1212137 86 7FF5 LDA A SEF~E:UF GET CHAR IN A
020A F6 7FF4 OLOOP LDA B SEF.:CSF.:
020() C4 02 AND 8 #$f:l2 T:xi F.:EADY?
02€1F 27 F9 BEG! OLOOP
12121:1 87 7FF5 STA A SEF.:BUF OUT TO T·"J.
....
END
I N'T'A~'%E
----'/
ADDRess L[):)'~ #$1.000-1.
pOIN1'eR
5TX MEMADD SET UP ADDF.:ESS POINTEF.:
t10F.:TES LDA A SERCSF~
AND A #$~31. F~:X: DATA REAr..y?
8EQ MOF.:TE5
LI)A A 5ERE:UF GET CHAF~
NO LD~·!.
OUTER t'1Et1ADD
~OOP IN::-::
5TX t'1Et1A [:1 D AND NEI.oJ ADDF~ESS
STA A >~ AND STORE CHAF.:
BRA t10F.:TES BACt< FOR r·10F.:E
SToRe IT
RME: 2
IN END
HE t.-r AODRESS
Here we see an inner loop testing the READY bit and an outer loop
storing data. 'fhis is known as a "nested" loop format.
.ll
Modify your program such that receipt of 5A will cause
storage of this byte, then exit from the loop. Show changes only.
Before
---_./
After
BRA MORTES cr1P A 1$5A IS IT Z?
BNE t"10RTES
_ _ _----J/
GET ORIGINAL STATUS
01.1313 B6 738E LDA A ACIAC~~
131.03: 84 FE AND A CLEAR BIT 0
#~:11.:1:1:11.:10
0:1'.::15 SA 02 ORA A #~~000000:10 SET BIT :1
131.1217 87 738E STA A ACIACR UPDATE OF~IGINAL
01.0A 87 7FF4 STA A SERCSF~
19200/300 = 64
Therefore bit #1 = 1 ) See
bit #0 = 0 )- Appendix
in the Control Register} E.
If both bits are 1 RESET takes place. This is necessary when
power is first turned on, before changing speed, parity, etc.
7-11
Bits 2, 3 and 4 (see Appendix E) determine the number
of data bits and stop bits of the data format. It also
determines the parity options for the data. Parity control
determines whether each transmitted data byte carries an even,
odd or unspecified number of ones, bit #7 of the data being
modified to produce odd or even parity.
The number of data bits and stop bits, plus parity
options must be agreed upon for both ends of the data link.
Although programmable, they are not usually changed once a
data link is set up.
Without disturbing unspecified Cpntrol Register bits,
set the ACIA for 1200 bps operation using a 19200 bps oscillator.
The data formed is to be 7 data bits plus l' odd parity bi t plus
1 stop bit. Again use ACIACR as the original for the Control
Register.
7 data -:16
odd
1 stop
For your first few programs, which are not part of a larger
program, simply place the desired value in the Control
Register e.g. LDA A #%00001101
STA A SERCSR
7-12
Serial data processed by the ACIA essentially follows
the RS-232-C Specifications of the Electronic Industries
Association (EIA). Voltage levels, source and load resistances,
connector type and pin assignments for data and control signals
are contained within this specification. Some of these control
signals are produced by the ACIA for the serial device. Others
are produced by the serial device for the ACIA.
One control signal is RTS (Request To Send)t which is
produced by the ACIA when requesting permission of the serial
device, a printer perhaps, to send data to it. This signal
is active when low hence is called RTS, the bar over RTS
indicating inversion, that is when RTS = 1, RTS = 0. RTS
is determined by Control Register bits #6 and 5.
The usual response by a serial device (printer) upon
°
receiving RTS = is to activate a control line to the ACIA
called CTS (Clear To Send), also active when low.
This exchange of control signals, usually preceding data
transmission, is often called "hand shaking" and can be used to
permit data transfer only when a device is turned on and
operational. The RTS line can alternately be used as a control
line without feedback (CTS is ignored), perhaps controlling a
function in an external device.
Control Register bits #7! 6 and 5 remain to be discussed.
Bit #7 controls receiver "Interrupt" operations (Chapter 11) and
is assumed to be 0 for now. Similarly bit #5 is assumed to be 0
since it controls transmitter "Interrupt" and "Break" operations.
°
With bit #5 = 0, bit #6 controls the RTS line; RTS = when
bit #6 = 0, and 1 when bit #6 = 1. See Appendix E for details.
--------.",/
7 6 5 4 J 2 1 0 ~bi t #
10 -..,-
0 0 0
~
1 0 1 0
"~
I Control
Register
LDA B SERCSR and
AND B #~~000010 10 expose"
_
RTS = 0
I
7 data.
r I II
7FF4
7FF5
EQU
SE~:CSF.: $7FF4
---_/
SERBUF EQU $7FF5
86 7FF4 * LDA A SE~:CSF~
:=:4 70 ANI) A #t~~111100~30 CHEC.< FOF~ 3 T',..PES OF ERROR
26 59 BNE EF.:~:OF.:
7 6 5 4 :3 2 1 0 ... bi t #
1X 0 0 0 X X X
t I I
xl
P 0 F
- PIA - ---8-1
~ CONT~OL
iFFO
I
7Ffl ~_ _~
)
R1:G\S'TER
7FF0 *
F'IABFA EI)U
-------/
$7FFe
7FF:1 PIACF~A EG~U $7FF1
/
7FF0 *PIA8FA EG'~U $7FF0
7FF1 PIACRA EQU $7FF1
7FF2 PIABFB EI~U $7FF2
7FF3 PIACRE: EG'~U $7FF3
:+:
01(10 86 7FF1 AHALF LDA A PIACF~A
0103 84 FE: --,
AND A #~~1.1111011 CLEAR BIT .:!. TO ACCESS DDR
01.05 87 7FF1 STA A PIACRA
01.0S 7F 7FF0 CLR PIABFA SET A HALF FOR INPUT
01.£18 SA 04 OF::A A #~'-;:00£10£110£1 BIT 2 = 1 FOR DATA
e1£1D 87 7FF1 STA A PIACRA
0110 F6 7FF3 BHALF LDA A PIACRE:
01.13 84 FE: AND A # %111.11 ~31. 1 CLEAR BIT 2 TO ACCESS DDR
0115 B7 7FF3 STA A PIACRB
01.:1.8 86 FF LDA A # ~.~ 1.:1.1.:1.1.1.1.:1.
01.:1.A
87 7FF2 STA A PIABFB SET 8 DCIF~ FOR OUTPUT
01.1.086 7FF3 LDA A PIACF~E: GET CR AGAIN
01213 8A 134 ORA A #;-;:(113000100 BIT 2 = 1 FOR DATA
01.22 B7 7FF3 STA A PIACRB
Assuming that the B half of the PIA is already
initialized for output (see previous frame), set bit #5 and
clear bit #J of Data Buffer B, without disturbing other Data
Buffer bits. From now on assume PIA Register definition
(PIABFA EQU $7FFO etc.), unless otherwise requested.
----_/
LDA A PIABFB
ORA A #:Y.001.00000 SET BIT 5
AND A #~1111e111 CLEAR BIT 3
STA A PIABFB
The PIA could be controlling a machine tool, with the changes
in bits #J and #5 representing control signals for the next
machine process.
previous frame?
---~/
Bit #2 of PIACRB = 1 permitting communication with the Data
Buffer rather than the Data Direction Register.
..§.;.l
The PIA could be used with a 6800 microcomputer in an
automobile sensor and alarm system. Assume INDATA as Data
Buffer A, at address 7FFO. Also assume the following bit
assignments for INDATA.
Contd •. ·
8-5
Contd.
The input Buffer, INDATA, has the following bit
assignments.
Bit # Function Status if 0 Status if 1
o Seat Belt Monitor disconnected fastened
1 Door Monitor closed opened
2 Oil Pressure Monitor low normal
J Ignition Monitor ignition off ignition on
4 Gear Shift Monitor park/neutral all others
5 Engine Monitor not running running
6 Day/Night Monitor night day
7 Headlight Monitor lights off lights on
The output Buffer, OUTDAT, has the following bit
assignments.
Bit # Function Status if 0 Status if 1
o Buzzer off on
1 Bell off on
2 Panel Alarm Light off on
3 Starter Control starting starting
disabled enabled
Flow chart and write the in~tructions to ring the bell
if the ignition is off and the headlights are on. (I wish
that I had that on my car.) Assume previous initialization of
the PIA for input on Buffer A and output on Buffer B.
_ _ _--J/
€1:100 B6 7FF0 CARCHK * LDA A INDATA
~1:1.03 85 0'-' c' BIT A #%01300:11210121 IGN?
0:105 .-,.-
oe:.t.:. 49 BNE NOBELL
€1:1.07 85 8~:::1 BIT A #:"'~:1.0€100000 LIGHT?
~1:109 27 45 SEQ NOBELL
0:1.0B 86 7FF2 LDA A OUTDAT
0:1.€1E E:A €12 OF.:A A #::-~0~3000e1.€1 BELL?
1211.:1.0 87 7FF2 STA A OUTDAT RING BELL
*
ExaT R'WG BELL 01.50 *
NOBELL
-8-6
This time permit the car to be started if and only ife
(a) seat belt is fastened and
(b) gear shift is in Park or Neutral and
(c) door is closed.
otherwise turn on the buzzer.
First flow chart your solution.
_ _ _-.1/
Your order of checking
the functions may correctly
be different. The order
shown here leads to
slightly easier testing
O"fHe~S ( l '
as seen in answer in the
next frame.
OPE" (I)
Now write the program, preferably using the flow chart -8-7
shown in the previous frame.
0:1.22
01.25
86 7FF0
85 01.
TESCA~:LDA
BIT
A
A
---------/
IN[)ATA
#%000000€11. BELT ON?
01.27 27 1214 8EQ BUZZ
01.29 85 :1.2 BIT A #%12100:1.00:1.et GEAR SHIFT AND DOOR?
0:1.28 27 etA BEQ OKTOGO
0:1.20 86 7FF2 BUZZ LDA A OUTDAT
0:1.313 8A (11. ORA A #~~000000(11
0132 87 7FF2 STA A OUTDAT BUZZ
0135 20 08 BRA [)ONE
0:1.37 B6 7FF2 OKTOGO LDA A OUTDAT
013A 8A 138 ORA A #"~0(1e01.000 OK TO START
0:1.3(: 87 7FF2 STA A OUT[)AT
0:1.3F 20 E:1. DONE
7 6 .5 4 J 2 1 0 bit #
o ijBelt on
I~ IX X X 0 X X
L-Door
1 closed
Park or Neutral
OVT 3 ;J. 0
xXXXXX1 X
t ~ Assume 0
Set (no interrupt)
t lo
for "no interrupt"
(0 = CA2 ACTIVE in going LOW
(1 = CA2 ACTIVE in gOillf..~ HIGH
o for input operation of CA2
CA2 READY bit (read only) 1 = REAux
Bit #5 for input. =0 Bits #4 and J behave the same as bits #1
and 0 for CAl.
---~/
xx° ° 1 0 x 0 ctf-Control Reg. A
tt \..CAl no Interrupt
CAl ACTIVE LOW
o to set direction
{ then 1
CA2 no Interrupt
$'?FF ~.:~
CA2 ACTIVE HIGH
?FF~3 PIABFA EOU
?FFl P I ACF.~A EG!U $?F F:.I.. CA2 Input
?FF:;:~ F'IAE:FE: EG!U $7'FF;;::
7FF3: P I ACF.~E: EOU $?FF3:
~11.~:::1~:3 E:6 7FF:l LDA A F'IACF.:A
t1:1.ff::' ::::4 D~:::1 AND A # :'.~:1 :1. ~:1:1 ~] ~:::10 ~:::1
~:3:tf15B7 7FF:l STA A PIACRA SET FOR DDR
(11.(18 '?F 7FF~3 eLf;,: F'IABFA INPUTS FOR A HALF
0:1t1E: ::::A :1.4 OPA A #%00010100 DATA 8UF NQW
~~:1.~~[) E:~:O ·?FF1. :::;TA A PIACRA
8-10
There are J possible modes for CA2, acting as an output
(bit #5 = 1). The first is seen when bit #4 = 1. CA2 will now
act as an output line whose state will be determined by bit #3,
(0 produces LOW, 1 produces HIGH).
7 6 4 J 2 1 0
1
_C_A_l__ n:::'
L-Data Ready p
CAt
~
~
DAtA
k'<r"] 1
A
~I."""J'I
....., 8 '-,..,&S
OF DATA
SOURCE
\.O~_
CA2 follows
CAl state.
J '---nata Read.
Therefore CA2 " -.
is LOW.
When CA2 goes LOW the external device will know that new data
may be put on the data lines.
Flow chart and write the instructions to read the data
from the external source via the PIA (A half) when CAl goes HIGH,
automatically indicating via CA2 that the data has been read.
Store the data starting at 0800. terminating data storage
after FF has been read and stored.
_ _ _-1/
CB 1 (in) nf..;.~.~':-------
\ data received by printer, therefore new data
can be put on data lines by PIA
CB2
(out)"
I I1-----------
CB2' ~new data now available in
follows PIA, therefore CB2 goes LOW
OBl state
Again the hand shaking operation permits optimum data
flow. Although the printer would not normally store more than
132 characters for one complete line of text, the data rate within
this line could be as high as 50 000 characters/second, limited
by the computer's clock and the number of instructions per loop.
8-13
One last mode, the STROBE mode is available when
bit #5 = 1, bit #4 = 0, and bit #3 = 1. It is similar to the
previous HANDSHAKE mode in that CA2 goes low when data is read
(LDA A PIABFA) into the A Data Buffer. It differs in that CA2
automatically returns to the 1 state several microseconds (one
instruction) later. Similarly, in the B half of the PIA, CB2
goes low when a write operation (STA A PIABFB) takes place and
returns to the 1 state automatically, several microseconds
later. This mode of operation releases CAl and CBl for other
tasks, but assumes that data is always ready for the "A" half
and that the external device is always ready to receive data
from the "B half. A summary of control line operations is
II
shown below.
CAl (CB 1)
(input only)
7 6 543 2 1 0
CAl (CB1)
READY BIT
(read only)
I
"'O=DDR access ~
via Data Buff
! l L
(0 =
(
°CAlfor (CB1)
going LOW
no interrupt
ACTIVE in
l=Data Buffer (1 = CAl (CD1) ACTIVE in
access (going HIGH
CA2 (CB2) I
bit #5
7 6 5
,..-__...._---".,A..---
o=
4 3°
input
2 1
I 1 = output
_ ,
bit #4
=1.&...._ _-_0..........
~I/] /Id/h). 10)~1 '?///} ,...,.. - "
..
~ >~'(, I a;;1;/t-r i~PI/l PROGRAMMED mode I
~ t CA2 (CB2) bit #3
1~~2)
READY
j 0 1
f I 0 for no
interrupt
fO_l_IO_W_S_b_i_t_II_3
_
=_o_l= 1
+
r
BIT ~.,
,
r
CA2 (CB2) ACTIVEJ HANDSHAKE mode STROBE mode
~~~nfci~~~ 'ACTIVE CA2 (CB2) goes HIGH CA2 goes LOvl
momentarily after
• I following CAl (CB1).
go~ng HIGH CA2 goes LOW after READ from A Buffer.
READ from A BUFFER. CB2 goes LO~l1
CB2 goes LOW after momentarily after
WRITE to B BUFFER. ~RlrE to B Buffer.
--_---..J/ XXllo'M'10
~ '-v-J
CA2 CAl
output" input
= bit #3 active
hi.gh
:+: PIA PROG FOR FOUR KEY KEYBOARD.
* CA2 IS OUTPUT TO SWITCHES. CAi IS
:+: INPUT TO PIA. DATA GOES TO LOW 4 BITS.
:+:
~:::11.~::'1f1 86 7FFi KE1TIPIA LOA A PIACRA
~:::1:l03: :=~·4 F;;'-:: AND A ~* ~,~ :t 1 :1 :1 ~:1 ~~11 ~:::1
~~1:t~~15 E:~:t ~;:OFF1. STA A PIACRA ACCESS DDR
0:t~?1S ?F 7FFf1 CLF.~ PIA8FA DATA INPUT MODE
~:::11J1E: SA 3:6 OF.~A A #~'~~?1~3:1.1~';):1 i~:::1
(1:1(1[) E'?'I 7FF:t STA A PIACRA DATA MODE NOW
8-15
Now flow chart and write the instructions to branch to
KEYA, KEYB, KEYC, or KEYD, corresponding to a depression of
keys A, B, C or D.
-----"/
TR'T'AGN LDA A PIACF.:A
BPL Tf;,:'T'ACiN CA1. r'~OT UP 'r'ET
LDA A PIABFA UP r-.low
AND A #$~)F LOl.oJER 4 BITS ONL'T'
BIT A #$f1::L KElT' A HIT?
SEQ KE'r'A 'T'ES.
BIT A #$02 KElT' B HIT'f'
BEGJ. KE'T'S
BIT A #$04 I<E'r' C HIT?
BEI;J, I<E'r'(:
BIT A #$(1E: KE'T' D HIT?
BEG! I<E'T'[)
t'10F.:DE>:: DE:X:
BNE t'10RDE>~
4 CYCLES
4 C'r'CLES
------"'/
:+:
:+: 8 CYCLES TOTAL
8-17
To get 10 msec., then the # of loops required =
10 / ~6
10 3 10
= 125010 loops
NAt1
-----/
PF.:OG68
OPT 0.. S
OF.:G $~j1.€nj
:+:
:+: P8-1.7
:+:
CE 04E2 TIMER LDX #1.25~] 4-- No $ silZll for decimal #.
09 MORDEX DEX 4 C'T'CLES
26 FD BNE MORDEX 4 CYCLES
:+:
:+: 1::1 C'T'CLES TOTAL
A:~
~;:e~
Each of electromagnets At B and C are directly under control
of a PIA Data Buffer bit, as shown in the diagram below.
A magnet is ON when the appropriate bit is in the 1 state,
and OFF when the bit is o. Energizing magnet C causes the
North pole of the central magnet to rotate to the South pole
at c.
[
C B
[I
A
PIA Data Buffer B
_ _ _...---J/
0~00 86 ~1 MAGA LDA A #:$:~~1~
0:1€12 87 7FF0 STA A F'IABFB
Bit #0 (electromagnet A) is ON.
8-19
How would you suggest having the N pole of the central
magnet point to a half way between A and B? Write the
instructions.
------/
€1:10E: 86 ~:1:$ t'lAGA8 LDA A #$03
0:10A 87 7FF0 STA A PIA8FB
---_/
Data Buffer
01.1313 86 (11. MAGA LDA A #$131.
01.132 B-:O
,( 7FF2 STA A PIABFB
01.135 80 €11.32 JSF.: C'ELAY
131.08 86 0-"
..=:. t1AGA8 LOA A #$03
..31.eA E'"?
'I 7FF2 STA A PIAE:FE:
(11.0D 8D 13:1.32 J5R DELA'T'
(1:11.(1 86 02 MAGE: LOA A #$02
(11.1.2 B7 7FF2 STA A PIABFB
13:1:1.5 BC' 0132 .J5~: DELA'T'
€I1.18 86 (16 t1AGBC LOA A #$06
13:1.1A 87 7FF2 STH A PIABFB
0:1.:1.D BD (11.32 JS~: DELA'T'
..::t120 86 04 t1AGC LDA A #$134
"~tl22 E'?
'I ?FF2 5TFt A PIABFB
~~1125 BD (11.32 .J5~: DELA..,'
0:1.28 E:6 &.35 t'1AGCA L[:eA A #$135
'.
€1:12A E''? 7FF2
e:12D BD 0:1.32
STA A
.JSR
PIABFE:
DELAY
13:130 2€1 CE BRA MAGA
How would you modify the angular velocity for this
--8-21
---_/
The constant used for the delay could be entered via a keyboard
e.g., using the keys 1 - 9, each producing a different constant
and therefore a different angular velocity. The smaller
constant would then be down-counted sooner, producing a shorter
delay, hence a higher speed.
Modern stepping motors usually have many (dozens) of
coils around 'the circumference I alternating between A, B and C
groups, each group being driven by one specific line, hence
PIA bit. An output of the sequence 001, then 010, then 100
would represent one cycle, usually a few degrees. Reversing
the order would reverse rotational direction.
SUBROUTINES
.......
9-1
overall tasks being defined first, and from these tasks the sub-
tasks defined. Each task can then be assigned to a subroutine
which in turn can call lower level subroutines to carry out the
sub-tasks. Subroutine calls can be many levels deep, if
necessary, those at the lowest level being responsible for the
simplest tasks, like checking a READY bit in an ACIA or a
control line in a PIA. The overall result is a hierarchical or
pyramidical structure, the top levels being general or "global",
the lowest levels looking after detail.
Contd •••
9-1
Contd.
A typical subroutine. properly documented, is
shown here,
* GETCHR... SUBROUTINE WHICH RETURNS WITH
* ASCII CHAR IN ACC A. X AND B NOT CHANGED.
7FF4 *
SEF.:CSF.: EG!L1 $7FF4
7FF5 SEF.:E:UF EG!U $7FF5
:+:
(11(17 86 7FF4 J3ETCHf;.: LDA A SEF.:CSR
~310A :=:4 01 AN[) A #$1211 [:tATA F.:EA[:tIT'?
(110C 27 F9 BEG! CiETCHF.: NOT 'TIET.
~Z11(1E 86 7FF5 LDA A SERBUF 'TIES. GET DATA
(1:1.11. 39 F.:TS AND E::·::1T.
--------/
* ECHO... SUBROUTINE TO ACCEPT ASCI CODE FROM ACIA
* RECEIVER AND ECHO IT ON THE ACIA TRANSMITTER.
* CALLS GETCHR AND PRINT SUBS..
~31.~3~Z1
SD 01.07
*ECHO .JSF.: GETCHF~ GETS INPUT
€1:103 SD 1-3:1:12 JSF.: PF.:INT Ar·~D OUTPUTS IT.
e1.e6 39 RTS AND RETURNS
At this point the details of GETCHR and PRINT are not necessary
except that they both use ACe A.
~
Assuming co.mrnunication to the printing device via the
ACIA, convert the instructions shown below to a well documented
subroutine called PRINT.
PF.:INT LDA B SERCSF.:
AND B #$02 READY TO PF.: I NT""!'
E:Ef~ PRINT NOT 'TIET.
STA A SEF.:E:UF PRINT CHAF.:.
___-------J/
* PRINT... SUBROUTINE TO PRINT ASCII CONTENTS
* OF ACC A ON ACIA OUTPUT DEVICE. USES A AND B.
0112 F6 7FF4
*
PF~INT LDA B SEF.:CSF.:
0:115 C4 02 AND E: . #$1212 F.:EAD'TI TO PF.: I NT?
0:1:17 27 F9 BEt] PF.: I NT NOT 'T'ET.
0:1:19 87 7FF5 STA A SERBUF PF.: I NT CHAR.
0:1:1C 39 F~TS AND F~ETUF~N.
ECHO
GETCHR~ PRINT "
implying that ECHO calls both GETCHR and PRINT. For lack of a
better name let's call this a "subroutine tree".
Imagine a system where the computer is to receive -- 9-)
-----/
The C bit is set when data is ready, and cleared when data is
not ready.
~
In Appendix C find 2 instructions, each of which branch
conditionally, depending on the state of the C bit. Use one of
them in the main program below, upon return from the subroutine
INCHKl to determine whether or not to store data, MEMADl being
the pointer. If that is not too difficult repeat for ACIA #2,
where MEMAD2 is the pointer within INCHK2, which similarly checks
if ACIA #2 is ready.
_ _--c/
:+: HEXADO. . . STORES 2 BYTES IN MEf1 AT LABEL ADDRES
=+: CALLS INBYTE TWICE. USES ACC A.
=+:
£1101'21 BC' 01.13 HEXAr..D JSR INB'r'TE GET 8 BITS IN ACC A.
1'21103 87 1-31.130 STA A ADORES AND STORE THEt-1.
01£16 SD e113 JSR INBYTE 8 t10RE BITS
131£19 87 e10E STA A AD[:'~~ES+1. INTO NE~"~T ADDRESS.
010C 39 RTS
e:1.eD 0£102 Ar)DRES Rt1B 2
=+:
13113 INB'T'TE EQU $0:1.13
:+:
---~/
HEXADD... STORES 2 BYTES IN MEM AT LABEL ADORES
:+: CALLS INBYTE TWICE, CHECKING FOR ERROR WITHIN BYTE
:+: SUB VIA SET C BIT. ACe A USED.
:+:
:+:
0::1-£1£1 BD ~]:1:13 HE:~-::ADD ..TSR INB'T'TE GET 8 E:ITS IN Ace A.
~3:1~~13 25 FE: BCS HEXADD RESTART IF ERROF.:.
~1:1~~15 87 '.:11.:1:1 STR A ADDRES ELSE STOF.:E THEt1.
£11.£18 BD '.:1:11.3 .JSR INB'T'TE 8 t10F.:E BITS
€1:1(1B c:..._1 F3
.-.~
BCS HE>::ADD RESTART IF ERROR
f1:10D 87 €1:1.:1.2 STA A ADDF.~ES+:1 ELSE STORE IN NEXT ADDRESS.
~J:t:1€1 39 RTS
!::11.:1:1 1-~1(1f12 ADDF.~ES F.~t1B 2
---..----.-1/
No. If 5 is the first of two keys struck, the 0101 result
must be moved to the left half of ACC A, to make room for
the next 4 bits, which go in the right half when the second
key is struck.
~
Write the first half of the INBYTE subroutine to place
the first 4 bits in the left half of ACC B. Useful instructions
might be ASL A and TAB. Why is ACC B needed? The HEXCHR
subroutine is still available and returns with the C bit set if
an invalid hex key was struck. Such a condition should cause
an immediate return from INBYTE to HEXADD, with the C bit still
set.
Ace B is used to store the first 4 bits when HEXCHR, which uses
ACC A, is called to get the second 4 bits. RTS passes the C bit,
undisturbed, to the calling subroutine HEXADD.
Now finish the INBYTE subroutine including documentation. ---
9-9
*= HE:X:CHR.
---~/
RECEIVES ASCII eODE IN Ace A VIA GETCHR
*= CONVEF.:TS TO 4 BIT HEX EQUIVALENT IF VALID
AND CLEARS C BIT. ELSE RETURNS WITH C SET.
**=
121125 BD 0143 HE>~CHF~ .J5F.: GETCH~~ (ECHO WOULD BE BETTER STILL)
13128 81 2F #$2F
eMF' A
012A .-,-.. 14
oe:.~ BLS BADHEX BEL.OW 3121, NOT HEX
012C 81 39 cr'1P A #$39
0:12E 23 fi(: BL.S NUt10K 121 TO 9. VALID HEX
121:13121 E::1 4121 er'1P A #$40
€1:t32 23 ec BLS BADHE::-:: 3A TO 4121. ILL.EGAL
0134 :::1 46 Ct1F' A #$46
01.36 22 138 BHI BADHEX ABOVE 46. ILLEGAL
£1138 8~1 37 SUB A #$37 A TO F IN 4 BIT FORMAT
0:t3A 0C GOODH:X: CLC TELL THEM IT/5 GOOD
013B ::~9 HE>!'F.~Tr·~ F.~TS
et13C 80 3=121 NUt10K SUB A #$3€1 121 TO 9 IN 4 BIT FORMAT.
~313E 2~3 FA BRA GOO[:IH>::
0::1.4~3 €1D E:ADHE:)~ SEC BAD NEWS. WRONG KEY.
~1:14:1 ;;::€i F:::: BF.:A HE::<RTN
9-11
The GETCHR subroutine is essentially the same as before
except for 2 changes.
(a) Bit #7, the parity bit must be cleared for all data.
(b) Lower case alphabetic characters a to z, must be forced to
upper case by clearing bit #5. Write the GETCHR subroutine.
Both of the above are required to make the data independent of the
type of terminal (some produce parity bit set, others cleared)
and to eliminate having to hold the SHIFT key down when entering
alphabetic characters.
------"/
* GETCHR... SUBROUTINE TO GET ASCII CODE FROM ACIA RX.
*_BIT #7 (PARITY BIT) CLEARED. UPPER CASE IS FORCED.
7FF4 *SERCSREQU $7FF4
7FF5 SERBUF EQU $7FF5
0:143 B6 *
7FF4 GETCHR LDA A SERCSR
0:146 84 01. AND A #$01. DATA READY?
01.48 27 F9 BEQ GETCHR NOT YET.
014A 86 7FF5 LDA A SERBUF YES. GET DATA
0140 84 7F AND A #$7F CLEAR PARITY BIT.
014F 81. 60 CMP A #$60
01.51 23 06 BLS GETRTN BELOW "SMALL All
01.53 81. 7A Cf1P A #$7A
01.55 22 02 BHI GETRTN ABOVE "SMALL ZU
0:157 84 DF AND A #$DF UPPER CASE ALPHA CHAR
01.59 39 GETRTN RTS AND EXIT.
9-12
Describe the sequence of events when a non-hex key is
struck. Sketch the "subroutine tree" in your answer.
HEXADD
---~/
•
INBYTE
~
HEXCHR
/
• "-.
ECHO
GETCHR PRINT
---------//
* PAGE... SUBROUTINE TO PRINT ONE PAGE (16 LINES)
* OF DATA FROM MEMORY. ENTER WITH X POINTING TO
STORE
PO'NTER-I * FIRST CHAR TO BE PRINTED. CALLS LINE SUB.
COUNT: "10 *PAGE DE:>:: ONE BELOW FIRST CHAR ADDR
STX MEt1PNT INIT POINTER.
LDA A #1.6
STA A LINCNT SET UP COUNTER.
NULINE JSR LINE PF.:INT LINE
DEC LINCNT LAST LINE?
BNE NULINE NO. PRINT ANOTHER
RTS LAST ONE.
\ \ LINCNT Rt18 1
'---........- - - MEt'1PNT Rt1B 2
Note the double vertical bar here
"-- indicating a subroutine.
_____---J/
* LINE... SUBROUTINE TO PRINT 64 (DECIMAL) CHAR
:+: FROM 32 MEMORY ADDRESSES. CALLS WORD. USES A.
SET UP :+:
,""OR.O c.o~N'TER START NEW LINE
LINE .JSR C~:LF
LDA A #$~Z1:=:
STA A l·JF~DNUt·1 SET UP COUNTER
.JSF~ L·JORD
DEC L·JF.~DNUt'1 LAST laJORD?
BNE NULaJORD NO. BACK AGAIN.
RTS LAST ONE.
l·JF.:DNUt·1 Rt1B :1.
*
9-15
~henext subroutine proceeding downward is WORD, which
prints the contents of 4 memory locations, then skips one space.
The subroutine OBYTE, to print the contents of ACe A as 2 ASCII
.
characters is available. SPA~E, a~other subroutine will pr~n" .
(or skip over) one space. Flow chart and write the NORD
subroutine.
---------J/
~ WORD... SUBROUTINE TO PRINT CONTENTS OF 4 MEM
* ADRESSES AS 8 HEX CHAR. CALLS OBYTE AND SPACE.
:+: USES ACe A.
:+:
l.oJO F.: D LDA A #$~:::14
STA R B'T'TeNT INIT COUNTER
.JSP OB'T'TE PPINT 1 BYTE AS 2 CHAF.:.
DEC B'T'Te~·~T
E:NE NUB'T'TE NOT LAST B'T'TE
JS~: SPACE 'T'ES. LAST B'T'TE. ONE SPACE.
F.:TS DONE
B'T'TCNT F.:t18 :1.
RETURN
9-16
The OBYTE subroutine is next. It gets one byte from
memory via the pointer MEMPNT and calls HEXPRT twice to print
it as 2 ASCII characters. HEXPRT is entered with 4 bits
right-justified in ACC A. Flow chart and write the OBYTE
subroutine.
* DBYTE... SUBROUTINE
* ADDRESS AS 2 ASCII
--------/ TO PRINT CONTENTS OF ONE MEM
CHAR. CALLS HEXPRT. USES A JX.
* ENTER WITH ADDRESS IN MEMPNT.
*OBYTE LD~':: MEMPNT
IN>-~
STX MEMPNT GET ADDRESS
LDA A >:: GET BYTE
STA A TEMP SA.. . E COP'T'.
ASF.: A F.:IGHT
ASF.: A .JUSTIF'T'
ASF.: A LEFT
ASR A HALF
ANC' A #$(1F ZAP LEFT HALF
.JSF.: HE>::PF.:T PF.:INT IT
Le'A A TEt'1P GET CLEAN COP'T'
AND A #$0F ZAP LEFT HALF
.JSF.: HE~·::PF.:T PF.:INT IT
RTS DONE
TEMP RMB 1
Re"'URN
Note the use of TEW~ rather than ACC B. It is not good practice
to tie up an accumulator, when calling a subroutine which may
need the accumulator.
9-11
HEXPRT is entered with 4 bits right-justified in Ace A.
It prints the corresponding ASCII character. Flow chart and
write this subroutine noting that PRINT is available to print
the ASCII contents of ACC A.
*
_----J/
HEXPRT... SUB TO PRINT ASCII CHAR. CALLS PRINT SUB.
~oo 30 TO * ENTER WITH 4 BITS RIGHT JUSTIFIED IN ACC A.
ACt " CON'Tetm
HE>::PF.~T ADD A #$3~3 CONVEF.~T TO ASC I I
Cf1P A #$]:9 NUt1BEF.~?
BLS OUTPUT
ADD A #$07 LETTEF.~. A[:ID 7 t'10RE.
OUTPUT JSR PRINT OUT IT GOES.
RTS
PR\NT CHRIl.
*
----..1/
PRINT... SUBROUTINE TO PRINT CHAR IF DEVICE
* IS ON LINE VIA CTS=~ CCTS NOT=0). USES ACC A AND B
* ENTER WITH ASSCII CODE IN Ace A.
SERCSR EQU $7FF4
SERBUF EQU $7FF5
*PRINT LDA B SERCSR
BIT 8 #$08 CTS NOT=0?
BHE PRINT NO. TRY AGAIN.
BIT 8 #$02 READY?
PIUNT CHAR BEQ PRINT
STA A SERBUF PRINT IT
F.~TS
RETURN
Loopback for the second test is to the top to ensure that CTS
has not gone to 1, while waiting for the printer to become READY.
9-19
SPACE and CRLF now remain. A problem exists in using
the ACIA with the printer in that the ACIA will transmit the
last character in its TRANSMIT Buffer even though the printer
requests a halt to more data by clearing CTS (Clear To Send).
eTS is normally cleared during a Carriage Return or Line Feed
operation or when the printer is not ready to print data. The
above problem results in the loss of the last transmitted
character. The solution is to send a 2 nulls (ae) to the ACIA
after both the CR and LF characters. ;rhe nulls are then
"sacrificed" to preserve the next legal character printed.
With this in mind, write the CRLF and SPACE subroutines. Flow
charts are not necessary for these.
*
--_---J/
SPACE... SUBROUTINE TO OUTPUT ONE SPACE CHAR.
:+: CALLS PRINT SUB. USES ACC A.
*
SPACE LDA A #$20 ASCII FOR SPACE
.JSF.: I::'R I NT
~:TS
PAGE
---------/
~
LINE
/ "CRLF
WORD
/ "SPACE
OBYTE
In only a few words, the
overview of PAGE is depicted
HE~p0 here.
----~/
* DELAY... SUBROUTINE TO PRODUCE DELAY
tHaT cou,,"ER * OF N MILLISECONDS~ WHERE N= BINARY
* CONTENTS OF ACC A ON ENTRY. CALLS MILSEC.
10:1130 B7 ~1:10C *DELA'T' STA A COUNT STO~~ES N
------/
No! The subroutine would execute properly the first time it is
called, MILCNT being decremented from 64 to o. The second time
(and all subsequent times) that it is called MILCNT would start
at FF, after first being decremented from 0 by DEC MILCNT. This
subroutine MILSEC would then go through 25610 loops to reach zero,
instead of 100 10 loops, producing an incorrect delay. Self-
initialization is required within the subroutine to reset MILCNT
to 64 every time the subroutine is called. Lack of self-
initialization is a common catastrophic error when coverting a
program, which runs correctly once, into a subroutine which is
called many times within a larger program.
This concept should be ext~nded to all programs, as well
as subroutines enabling faulty programs to be restarted during
de-bugging without the necessity of being reassembled or reloaded.
Enough said for now about subroutines!
10-1
STACK OPERATIONS
Previously we have seen data storage in which the Index
Register was used as a pointer. Another 16 bit register. the
Stack Pointer (Sp) is also used to store and retrieve data.
employing a user-defined block of memory, called the stack, for
the storage operations. 'rhe Stack Pointer may be initialized
to point to the address lC40 ·J'ia
LDS #$lC40 (LoaD the Stack pointer)
Another instruction
PSH A (PuSH accumulator A)
performs a "push" operation, that is it stores the contents of
ACC A in the address now contained in the Stack Pointer. The
Stack Pointer is automatically decremented after the storage
operation.
"PuSH" is an appropriate description, similar to the
"pushing" of individual serviettes into a metal holder, each
new serviette now being on the top of the stack.
Initialize the Stack Pointer to lAFF, then store the
contents of Ace A and Ace B on the stack in that order.
13C5 ~SP
. lJC 6 ...SP ~CC A SP
13C 7 04 04
lJC 8 ....SP 13C 8 29 13C8 29 lJe
before just after after after after
JSR PRINT JSR PRIN'r PSH A PUL A RTS
-
10-4
Examination of data stored on the stack is achieved vial
TSX - Transfer Stack pointer to indeX register.
which transfers the Stack Pointer to the Index Register, then
increments the. Index Register. In this way the Index Register
points at the last byte stored on the stack. This permits
direct access to the data, storea l)n the stack, wi thout
disturbing the Stack Pointer. Wri tl~ the instructions to print
the value of the last byte, stored on the stack. The sub-
routine OBY'l'E is available.
-------..;l'/
0203 30 TSX
0204 A6 0€1 LDA A X
02€16 E:D €1142 .JSR OBYTE
10-5
Assume' that 4 bytes have been stored on the stack. It
is now desired to increment the first of these 4 bytes without
disturbing the stack pointer or other data on the stack. Write
the necessary instructions.
_ _ _----.II
~11 ~3 ~:::1 J: ~:::1
~31"31 6C ~~f3: INC 'rsx =
SP + 1 ~ X
7JF5 -+-SP
7JF6 4 · . x
7JF7 1 • ••••••••• 1, X
7JF8 2 • ••••••••• 2. X
7JF9 Iii · J, X
More stack operations will be seen in the next chapter, Interrupt,
where the stack is used extensively.
11-1 (a~
INTERRUPT
The simplest type of "interrupt" operation is that
produced when you start the 6800 microcomputer by pushing the
RESET button. This starts execution of a permanently stored
program or "service routine". as interrupt initiated programs are
called, this one servicing the RESEr button. When this button
is pushed the RESET line to the MPU is grounded. This causes
the computer to look in addresses FFFE and FFFF (called "vector"
addresses) for the address of the RESET service routine. The
RESET service routine is then started. typically clearing all
READY bits, initializing the stack pointer and setting up
input/output devices such as the PIA or ACIA for the required
mode of operation.
The RESET line also can be converted to force a restart
of this service routine automatically when power is first
applied, eliminating the RESET button. This is particularly
useful when the microcomputer controls an electronic subsystem
or an appliance (e.g., microwave oven).
Another form of interrupt provides the solution to the
problem of determining when a peripheral device has data or
requires data, without the continuous check of READY bits in an
ACIA or PIA. Under interrupt operation, such devices are ignored
by the computer until the device demands service, whereupon the
computer suspends its present operation. known as a "background"
program and executes the service routine or IIforeground program
ll
I EXTE~NAL
DEVICE
I c:,,, I 51! TS
I .7
f!A _
-1 &'T (bit.oal) XRQ
I fR~
LDX IACIARX
---"/
STX $FFF8 INIT VECTOR FOR IRQ
When an interrupt occurs, the contents of the accumulators
and registers will be pushed on the stack. Then the address of
the next instruction to be executed will be obtained from FFF8
and FFF9, the IRQ vector address. In other words the next
instruction to be executed will be the first instruction of the
the IRQ service routine.
11-2
....-
Continuing with the background initialization, set
the ACIA Receiver Interrupt bit, to permit interrupt to occur.
Then initialize MEMADD with the address one below address lAOO,
to permit storage of data from the ACIA Receiver. Assume, as
before, that ACIACR is the "original" for the "write only"
Control Register of the ACIA.
-----.;/
LDA A ACIACF.:
O~:A A #~10e00000 ENABLE RX INT
STA A ACIACR
STA A SERCSR
LD::.-:: #$1A0~1-1
STX MEMADD SET UP STORAGE POINTER.
CLI
-----..,,/
ENABLE INTERRUPT
HF.: E:F.:A HF.: BACKCiF.:OUNO LOOP
--_----J/
* INTERRUPT SERVICE ROUTINE FOR ACIA RX.
* STORES ONE CHAR IN MEM VIA MEMADD POINTER.
0:1:1C FE 0:11A *ACIARX LDX t'1Et1ADD
01.iF 08 INX
0120 FF 01:1.A ST>:: t"lEt'lADI) GET NEXT ADDRESS
0:123: 86 7FF5 L[:IA A SEF~BUF GET DATA
0:126 A7 00 STA A>:: AND STORE VIA MEMADD
0128 38 F.~T I AND RETURN TO BACKGROUND.
Each time that the ACIA's Receiver is READY with another byte of
data, bit #0 of its Status Register will go to 1, indicating the
READY condition. Since bit #7 of the ACIA Control Register is
also set, permitting ACIA Receiver Interrupt, the setting of the
READY bit automatically activates the IRQ line to the iJIPU,
causing execution of the service routine whose starting address
is in FFF8 and FFF9. After the RTI instruction of this service
routine the background task, if there is one, will be resumed.
A long story isn't it?
11-i
Printing a message via the ACIA under interrupt is
similar to data reception in the previous frame. Here the
ACIA Control Register bits #6 and 5 must be initialized to
provide "RTS = low, Transmitting Interrupt Enabled". (See
Appendix E).
Write the background initialization to permit printing
of the message INVALID HEX via the ACIA under interrupt.
Include the message in the background initialization.
------------------ /
~1:.1.. &.3~3 CE &.Z1:12A LD>~ #MESPRT GET INT ROUTINE ADDRESS
&':::':1&.Z1]: FF FFF8 ST;:-:: IRQVEC INIT MESSAGE POINTER
":::':1&.36 E"-.t,::• 73::=:E LDA A ACIACF.:
.31.(19 84 SF AND A #%1.0:1:1111:1. CLEAR BIT 6
":::'1 .:::,8 :=:A 2(1 oF.:A A #%130100000 SET BIT 5 TX INT ENABLED
(11.1-3D E'-.,::O 73E:E STA A ACIACR UPDATE ORIGINAL
(':1.1&,:::, ., 7FF4
Eo"::' STA A SERCSR SET UP ACIA
01.13 CE &.3:1.:1.8 LD::·:: #8ADHE::-::-:1.
1-)1:16 FF &.312S ST::-:: MEMADD SET UP POINTER
1-:::':1:1.9 "3E eLI
":;:'1:tA 213 FE HP 8 F.: A HR SPIN FOREVER
:+:
\.311C 49 BADHE::·:: FCC ••••1 I N\,'AL I D HE::·::.····
a.Z1:1.27 1.: :,&.:;:, FeE: 1-3
&.3:12S &.3(1&.32 t'lEt'lAO[) F.:t·18 2
11-6
Within the service routine how will you ensure that the
ACIA Transmitter will stop sending characters to the printer,
after the last character of the message is printed? /
----
Contd •••
11-6
Peptd.
(1:1.44 20 F'-'
c.. E:F.:A PF.~TF.~T I
:fi
--=-----~/
0200 86
7FF4 IRQSER LDA A SE(;'~CSR
12121213 85
1211 8IT A #$1211 R>~ READ.,.'?
12121215 26
49 BNE KE'r'SER
12121217 85
1212 BIT A #$1212 TX READY?
121209 26 65 8NE PRTSER
0208 38 INTRTN RTI RETURN POINT FOR ALL
Both servifYe
routines would
branch back
to here.
Although all IRQ controlled devices are theoretically equal for
interrupt service it is normal to poll the READY bit of the
fastest device first, if one is significantly faster than the
other to avoid losing data from the faster device while servicing
a slower device. Hence the first device polled effectively has a
slightly higher priority, this advantage increasing as more
devices requiring IRQ service are added to the system.
11-9
PIA Control Lines acting as inputs can produce IRQ
operation if enabled for interrupt via the PIA's Control
Register. When bit #0 of Control Register A (or B) is set,
interrupt is then possible via CAl (CB1). Similarly CA2 (CB2)
is enabled via bit #3. CA2 (CB2) as an output line does not
produce an interrupt since interrupts originate with the external
device such as a keyboardttelling the computer that data is ready
to be moved or that some control action is needed.
Write the background initialization to permit CAl of the
PIA to interrupt when going high (1) and CA2 as an input to
interrupt when going low (O). The A half of the PIA should be
set to receive 8 bit parallel data.
0350
0352
HICJATA EQU
LODATA EQU
$0350
$0352
-------.i/
0100 86 *
7FF1 PIASER LDA A PIACRA
0103 2B 05 BMI CA1.INT CA1 INT F.:EGJ.UEST VIA BIT 7
0105 85 40 BIT A #~. ~010e0000
0107 26 0B SHE CA2INT CA2 INT F.:EQUEST VIA BIT 6
0109 38 PIARTN RTI
0i0A B6 7FF0 CAiINT LOA A PIABFA
010D 84 OF AND A #$0F ZAP HI BITS
0i0F 87 0352 STA A LODATA
0112 20 F5 BRA PIARTN
01i4 86 7FF0 CA2INT LOA A PIABFA
0117 84 F0 AND A #$F0 ZAP LO BITS
0119 87 0350 STA A HIDATA
0iiC 20 EB BRA PIARTN
If several PIA's are connected as IRQ devices. but capable of
interrupt via CAl only. the skip chain becomesl
LDA A PIACR5
BMI PIA5
LDA A PIACR6
BMI PIA6
etc.
11-11
Another major use of IRQ operation is in controll1ng
the timing of specific computer operations. For example a
digital voltmeter m~y be required to make a measurement in a
lab experiment or in a process-control operation at the rate
of 10 measurements per second. Aside from the inaccuracy of
using timing loops for control of these measurements, the
computer is not available for other tasks.
The solution is in the use of a "Real Time Clock",
a device which produces interrupts at specific times or rates.
The service routine for the real time clock would then determine
which devices. get service at what times. In the example above,
the real time clock could be driven by the 60Hz line signal
producing 60 interrupts/sec. Write the background initialization
and service routine for this clock which causes the digital volt-
meter to make 10 measurements per second via the subroutine
DVMSER.
0:1.£10 86 06 LDA A
_ _ _-.--J/
#$(16
0:1£12 87 0:11C STA A COUNT
1211.1215 CE 0:1.0E LOX #CLKSER
0:108 FF FFF8 STX $FFFE:
01.08 0E CLI
01.0C 2121 FE HERE BRA HERE SPIN IN BACK
_ _ _--.-J/
t-----1
. . SP DURING NMI UPON RETURN TO IRQ
SERVICE ROU'rINE
IRQ Status
7 bytes
}
1---1
------/
Data must be saved by MPU hardware rather than via software
(program) which itself would use some of these registers and
therefore modify their contents.
11-14
Write the background initialization and the SWI serv~ce
routine to print the contents of CCR, ACe B, and ACC A simply as
6 ASCII characters, one after the other, when SWI is encountered
within the program. Assume an available subroutine, OBYTE,
which prints 2 ASCII characters, based on the 8 bit contents of
ACe A.
:+:
_ _ _-----J1,/
:+: SOFTWARE INTERRUPT SERVICE TO PRINT CCRJ
:+: ACC A AND B ON CONSOLE TERMINAL. CALLS OBYTE SUB.
:+:
FFFA SWIVEC EQU $FFFA
:+:
:+: BACKGROUND INITIALIZATION FOR SWI.
0200 CE 0240 LD>:: #Sl'~ I SEF.:
~321-33: FF FFFA
~3206 2"3 FE HF.: E:F.~A HF.~
:+:
:+: S~·JI SER'·lICE ROUTINE
:+:
024..:::' OF.:G $0240
~3240
-..::.....:::,- , PUL A GET CCR FF.~Ot'1 STACK
0241. BD ~11.42 .JSF.: OB'T'TE PF~INT CCF.:
0244 3:2 PUL A
0245 BD 01.42 .J 5F.: OB'r'TE PF.:INT B
024:3 32 PUL. A
..3 249 B[) ~11.42 .JSF.: OB'T'TE PRINT A
024C 20 FE HEF.:E BF.:A HEF.:E
:+:
01.42 OBYTE EQU
END
11-15
Now write the first part of a different SWI service
routine SOFINT, which prints a more readable output of the
stored data, e.g.,
CCR= XX (where XX = stored CCR value)
Assume the following a~ailable subroutines I
OBYTE - prints contents of ACC A as 2 ASCII
character.
OUTMES - prints ASCII message terminated by
null. X = pointer.
CRLF - Carriage Return and Line Feed.
:+:
:+:
---------/
PRINTOUT OF REGISTERS AFTER SOFTWARE INTERRUPT
:+:
0200 CE 0250 LDX ISOFINT
02103 FF FFFA STX $FFFA INIT SWI VECTOR.
:+: NOW JUMP TO TARGET PROGRAM
:+:
0250 OF.:G $02510
0250 8D 0:179 SOFINT JSR CRLF
0253 CE 028:1 LDX ICCRt1ES
10256 BD 1.F0C JSF.~ OUTMES PRINT CCR=
0259 3:2 PUL A
025A 8D 0142 ..JSF.: OBYTE PRINT CCR CONTENTS
:+:
:+:
0281 43 CCF.:t1ES FCC: . . . eCR= I
0286 1Zn.3 FCE: ~3
No.
-_-------.#/
Data to be printed will not be destroyed •
. .SP
. .SP RH RH
CCR CCR ~SP RL RL ~SP
B B B B
A A A A
IXH IXH IXH IXH
IXL IXL IXL IXL
PCH PCR peH PCH
PCL peL peL peL
Within SWI After Within OBYTE sub. After return
service first RH and RL are from OBYTE
routine PUL A. return address subroutine.
before bytes. H = high, RL and RH will
I?rintout L = low. CCR data be overwritten
begins. on the stack is in future use
overwritten but of the stack.
only after it is
in ACC A for
printing.
11-17
Cont inuin g with the same serv ice rout ine. assum e that
eCR. ACC B and ACe A have been pulle d and prin ted on one
line.
How woul d you prin t the Index Regi ster cont ents. stil l
cont inuin g
on the same line? Inclu de the mess age in your answ er.
0271.
0274
CE
BD
~1291
1.F~3C
LD::-::
.JSR OUTt1ES
- - - - - .. 1 /
# I ~·::t1ESS
PRINT ::.(=
121277 32 PlIL A
-BD
13278 0:1.42 JSF:: OB'r'TE PF.:INT HI B'T'TE OF ::.::
027E: .....-.
.::or:!. PUL A
027C BD 13:1.42 JSF~ OB'r'TE PF:~INT LO B'r'TE OF ::-::
:+;
:+;
0278 <.-.
_.~ PUL A
~327C SO 13142 .JSF~ OB'r'TE PRINT LO B'r'TE OF X
1!:127F 2~3 FE HEF.:E BF.:A HEF.:E
:+:
13281 4~
.::. CCRt'1ES FCC ICCR= ,,/
0286 00 FCB 0
0287 20 At1ESS FCC I A= I
028B 0121 FCB €I
028C 2£1 Bt1ESS FCC ,~J E'-
,- (.J
END
CCR= 2F B= D3 A= F2 X= lC5S
APPENDIX A
0000 = 0 1000 = 8
0001 = 1 1001 = 9
0010 = 2 1010 = A
0011 = J 1011 = B
0100 = 4 1100 = C
0101 = 5 1101 = D
0110 = 6 1110 = E
0111 = 7 1111 = F
----~~-~-~-~~---------------~~~-
----~-----~---~-~~------~-~-
APPENDIX B
ASCII Code s
BIrrS 4 thru 6 0 1 2 3 4 5 6 7
0 NUL DLE SP 0 @ p
P
1 SOH DCI ! 1 A Q a q
2 STX DC2 " 2 B R b r
3 ETX DC3 # 3 C S c s
4 EOT DC4 $ 4 D T d t
5. Et\Q N.AK % 5 E U e u
BITS 0 thru 3 6 ACK SYN & 6 F V f v
7 BEL ETB 7 G W g w
8 BS CAN ( 8 H X h x
9 HT E~1 ) 9 I Y Y
A LF SUB * J Z j z
B VI ESC + K [ k {
C FF FS < L I I I
D CR GS = M ] m }
E SO RS > N ( n =
F 51 US I ? 0 0 DEL
Pull Data
PSHA
PSHB
PULA
36
31
32
4
4
4
1
1
1
A-MSp, SP-l-SP
B -MSp,SP-l-SP
SP+ I-SP, MSp-A
•
•
•
0
11
~
• • • • •
• ·..
• • •
3
0'
9C
-
4
:F
2
0'
AC
- # 0' - ~
'!'f" 0' - ~
~ BOOLEAN/ARITHMETIC OPERATION H I
3 2 1
N Z V C
I
INHEA
I SP+l-X • • • • •
5 4 3 2 1
- 0
.OPERATIONS MNEfe'ONIC 0' - ::: 0' - ;; 0' - ~ 0' - :t BRANCH TEST H I N Z V C
Branch AlwaVs
Branch If Carry Clear
Branch If Carry Set
BRA
BCC
20
24
4
4
2
2
None
CzO
•
• - •• •• •• ••
•
Sranch If ~ Zero
BCS 25 4 2 C~l
• •• • • •
Branch If ;;. Zero
BEQ 21 4 2 Z2 1 • •• • • •
Branch If > Zero
BGE 2C 4 2 N'9V=O • •• • • •
Branch I f Higher
BGT 2E 4 2 Z+(N-itV)aO • •• • • •
Branch'f < Zero
BHI 22 4 2 C+Z :I 0 • •• • • •
BLE 2F 4 2 Z +fNav) = I • •• • • •
Branch If loWtf' Or Same
Branch If < Zero
BLS 23 4 2 C+Z= 1 • •• • • •
Sranch If Minus·'
alT
SMI
20
28
4
4
2
2
N'='V=l
N:zl
•
•
•
•
•
•
•
•
•
• -
•
Branch If Not Equal Zero SNE 26 4 2 Z-o • •• • • •
Branch If Overflow Clear BVC 28 .4 2 V=o • •• • • •
Branch If Overflow Set BVS 29 4 2 V= I • •• • • •
Branch If Plus
Branch To Subroutine
BPL
BSR
2A 4
80 8
2
2
N=O •
•
•
•
•
•
•
•
•
• -
•
Jump JMP SE 4 2 1E 3 3 } See Sptcial Operations • •• • • •
Jump To Subroutine
No Operation
JSR AD 8 2 BO 9 3
j
• • • • • •
Return From ,,,terrupt
NOP
RTI
01 2 Advances Prog. Cntr. Only • • • • • •
38 10 1 --@--
Retur~ From Subroutine
Software Interrupt
Wait for Interrupt
RTS
SWI
WAI
39
3F
3E
5
12
9
1
1
1
} See special Operation.
-I-fl-I-r
• S ••••
• @ ••••
CONDITIONS CODe REGISTER- INHER 5 4 3 2 1 0
CONDITION CODE REGIST£R NOTES:
,.OPE~ATIO~S MNEMONIC 0' - =
BGOL'EAN
OPERATION H I N Z V C
(Bit set if test's true .nd cleared otherwise)
Clear Carry ClC OC 2 1 O-C • • • • • R CD (Bit V) Test: Result.= 100000001
Clear Interrupt Mask CLI OE 2 1 0-1 • R • • • • @ (Bit C) Test: Result: 000000001
Clear Overflow ClV OA 2 I o-v • • • • R • @ (Bit C) Test: Decimal value of mOlt significant BCD Character gr~.ter than nine?
Set Carry
Set Interrupt Mask
SEC
SEI
00
OF
2
2
1
I
1-C
I-I -• • • • -
• • • •
S
S INot cleared if previously set.)
@ (Sit V) Test: Operand = 10000000 prior to execution?
Set Overffow
Acm~tr A-CCR
SEV
TAP
OB
06
2
2
1
1
I-V
A-CCR --• - •S ® IBitV) Test: Opennd: 01111111 prior to execution?
- - - @ - - ® (Bit V) Test: Set equal to rewlt of N $ Caft" shitt has occurred.
0~ (Bit N) Test: Sign bit of ",ost significant (MS) byte of result :II 1?
CCR -Acmltr A TPA 01 2 1 CeR -A -1·1-1·1-1· @8 Bit
() V Test: 's com plement overflow from sub traction f lS bytes?
® (Bat N) Test: Resun less than zero? (Bit 15 - 1)
LEGEND: 00 Byte =Zero; @ (AU) load Condition Code Register from Stack. (Se. Special Oplfations)
OP Operation Code (Hexadecimal); H Half-carry from bit 3; o
(Bit I) Set when int!mJpt occun. If previously Sit, a Non-Maskabl, Interrupt is
required to exit the wait state:
Number of MPU Cycles; I Interrupt mask
11 Number of Program Bytes; N Negative lsign bit) o (ALL) Set accordif'g to the contents of Accumulator A.
+ Ari.thmetic Plus; Z Zero (byte)
Arithmetic Minus; V Overflow. 2's complement
800lean AND; C Carry from bit 1
MSp Contents of memory loution R Reset Always
pointed to be Stack Pointer; Set Always -
S
+ Boolean Inclusive OR; ~ Test and 'itt if true. cleared otherwise
$ Boolean Exclusive OR; • Not Affected
fA tomp'ement of M; CCR Condition Cod. Register
Transfer Into; lS Least Significant
a Bit ~ Zero; MS Most Significant
Buffer Address
D~ta
Bus Transmit Receive
Line Data Data Control Status
Numb~r Register Regist.. Register Register
(Write Only) (Read Only') (Write Only) (Read Only)
0 Data Bit O· Qata Bit 0 Counter Divide Receive Data Register
Select 1 (CRO) Fuil (RORF)
; , Data Bit 1 Dat8 Bit 1 Counter 0 ivide Transmi~ .Data Register
Select 2 (CR 1) Empty (TORE)
2, Data Bit 2 Data Bit 2 Word Select 1 Data Carr ier Detect
(-CA2) (~CD)
Enable tor Receiver In terrupt Counter ratio and Master reset select used
in both transmitters and receiver sections
b7 c: \ : Enable~ Int errupt Output in
Receiving Mode b1 bO Function (Tx, Rx)
0 0 +1
b7 = O~ Disables Interrupt Output In
Receiving M ode 0 1 +16
1 0 +64
1 1 MASTER RESET
I
l b7 b6 b5 b4 b3 b2 bl bO
I
Word Length, Parity, and Stop Bit Select
Transmitter Control Bits: Controls the Interrupt Output· and RTS b4 b3 b2 Word Length + Parity + Stop Bits
Output, and providei for Transmission of a Break 0 0 0 7 Even 2
b6 b5 Fu nction 0 0 1 7 Odd 2
~;~~arri;:~:~:::
b2 = 1:
carrip.r is presc~
Indicates the: I()$'; of carrier.
1. The low-to·high transition of the OeD in-
t. - --l
put C~\Jscs b2= 1 and g'::nera\es an interrupt
(b7=1), (IRQ=O)
2. Reading the Status Registe, ~r.d Rx Data
Register or master res~ttlng the AC~A
causes b2=O and b 7~O .
...-._---
Receiver Data Register Full
bO == 0: Indicates that the Receiver Data
Register is ernpty.
,----
! Interrupt Request
bO = 1: Indicates that data has been trans-
ferred to the Receiver Data Register
and status bits states are set (PE,
OVRN, FE).
1 The interrupt request bit is the complement of 1. The Read Data Command on the high-to-
I the i AQ Ol.'tput. Any in1errl.pt that is set and low E transition or a master reset causes
, enabled '.vill bE:. available in th~ status register bO = O.
{ in addition to the norrnal I RQ output. 2. A "high" 011 the OCD input causes bO-=O
L_--- and the receiver to be reset.
I
I
I b7
IRQ
I
b6
PE I
b5
OVRN
I
b4
FE
1
b3
CTS I I OeD
b2
I
bl
TxDAE
I bO
RxDRF
I
T 1 T
Framing Error
b4 = 1: Indicates the absence of the first stop
bit resulting from character synchro·
nization error, faulty transmission, or
I
'-- a Break condition. Transmitter Data Register Empty
1. The internal Rx data transfer signal causes
b1 = 1: 'ndicates that tha transmitter data
b4=1 due to the above conditions and causes
Register is empty.
b4=O on the next Rx data transfer signal if
b1 :: 0: Indicates t~at the transmitter data
conditions ha'ie been rectified. Register is full.
1. The internal Tx transfer signal forces b1==1.
2. The Write Data Command on the high·to-
Overrun Error low E transition causes b1 =0.
b5 = 1: Indicates that a character or a num- 3. A Uhigh" on the CTS input causes b1=O.
ber of characters V\'ere received but
not read from the R x data register
prier to subsequent characters being
~ received.
1. The Read Data Command on the high-to-
10V\' E transition causes b5=1 and bO=1 if an
overrun condition exists. The next Read
Data Command on the high-to-Iow E transi-
tion causes b5=O and bO=O.
l Parity Error
b6 = 1: Indicates that a parity error exists.
The parity error bit is inhibited if no
parity is selected.
1. The parity error status is updated during
Clear to Send
The CTS bit reflects the CTS input status for
use by the MPU for interfacing to a modem.
NOTE: The CTS input does not reset the
transmitter.
the Interna! receiver data trans1er signal.
--------
CONTROL REGISTER
CAl (CB'l)
(input o~ly)
7 6 5 4 3 2 1 0
~~ft9?1
I
CA1(CB 1)
,READY BIT
~
O=DDR access
~ I l 0 for no interrupt
(0 = CAl (CB1) ACTIVE in
(read only) via Data Buff L( goin@; LOW'
l=Data Buffer (1 = CAl (CB1) ACTIVE in
access (going, HIGH .
CA2 (CB2) I
bit #5
INPurr mOde.,....__·O_=.......i_n....
Plllllllu_t_..-.",A.. 1 = outPu~
7 6 5 4 J 2 1 0 bit #4
=1,....J.. =0
~~llt~)~~JrgA PROGRAMMED mO-d-e----....... 1
1~~2) J
READY
BIT
t ~ t
0 1 0 for no
t
+
interrupt
f
CA2 (CB2)
f.ollows bi t /13 =0 l=
bit #)
_
1
mode
HANDSHAKE STROBE mode
CA2 (CB2) ACTIVE CA2 (CB2) goes HIGH CA2 goes LOW'
goin~ LOW ( ) momentarily after
CA2 (CB2) ACTIVE following CAl CBl ·
READ from A Buffer.
going HIGH CA2 goes LOW after
READ from A Buffer. CB2 goes LOW
CB2 goes LOW after 'momentarily after
WRITE to B Buffer. WRITE to B Buffer.
APIENDIX F2
PIA -
Peripheral Interface Adapter
01 :.: 0 : I AQA(9, 1 se~ by high-to-tow transi'tion on
CAt (CB1).
bl·c, 1 : 1RCA(S" se! by low-to·high tr:tns;tion on
CAl (CB1). r
'------- 1____ I ~A1 (Cal) Inter~JPt REtQuest Enable/Disable
. bO = 0: DisdbJa:i I RCAtB) r~1PU Interrupt by CAl (Cal)
lI
active transition.'
bO = 1 : Enable I AQA{B} MPU Interrupt by CA 1 (CB 1)
IRQA(B) 1 Interrupt Flag (bit b7) a~:tive tran~i'dor,.
cleared by "',PU Read of Output Regtsti!r A(B). May also be transit:~n of bO if CA 1 (CB1) active transition occurred
cleared by hardware Reset. , V ' . ' h i l e interrupt was rjisabled.
'=1_ ---=t-
b7
IRQA{B)l
'---.
, b6
IRCA(B)2
b5 _~J
CA2(CB2)
b3 p I DDR
t--_b__l----.i_b_6--f
CA1(CS1)
Flag f_'a_g_._~ C_'~~~~..-L~._c_c_e_s_s C_o_n_t_r_o_1__
- .Io._ _
__~_=r__.. --Jt=
I RQA(B)2 Interrupt Flag (bit b6)
CA2 (C82) Estobl!shed as Input (bS = O}- Goes high on active
.--.---__,J~_~
Detcuoines VJhether Data Direction Register Or au tput
transition of CA2 (CB2); Automa~icaJl" clcared by MPU Read Re~~i is Addressed
of Output Register A(B). May also be cieared by hardware
Reset. b2 ::II 0 : Data Direction R~gister seJe~teCS.
o
b3
T
CA2
(Note t [hDt
u
CA2 :;.nd Co
output fonct;ons are not identic-31)
_
.. 2
_ .,
b5
o
b4
T TL-f>
b3
CB~
high-to-Iow E tr"nsition.
IL -... disabled.
l S'?t/Res~t
next io~·,.to-high E transition.
CA2 (C32)
The characters used in the source language for the Motorola assembler form
a
sub-set of ASCII (American Standard Code for Information Interchange,
1968).
The ASCII Code is shown-in App B •. The follo\ving characters are recogn
ized
by the assembler:
I. The alphabet A through Z
2. The integers 0 through 9
3. Four arithmetic operators:
+ - * ,.
4. Characters used as special prefixes:
# (pounds sign) specifies the immediate mode of addressing
$ (dollar sign) specifies a hexadecimal number
@ (commercial at) specifies an octal number
% (percent) specifies a binary number
(apostrophe) specifies an ASCII liter-dl character
5. Characters used as special suffices:
B (letter B) specifies a binary number
H (letter H) specifies a hexadecimal number
o (letter 0) specifies an octal number
Q (letter Q) specifies a octal number
6. Four separating characters:
SPACE
Horizontal TAB
CR (carriage return)
, (comma)
The use of horizontal TAB is always optional, and can be replaced by
SPACE.
Courtos y Motoro la Se~icon ductor Produc ts, Inc.
APPENDIX H
Commonly Used Instr uctio ns
Reprinted from the September 1917 edition of Electronics Today International magazine. Toronto. Ontario_
APPENDIX Jl
Asse mble r Erro r Codes
ref,r to contents)
l,b.ls
H , N Z V C
Add AODA 8B 2 2 9B 3 2 AB 5 2 8B 4 3 A+ M-A t • t t t t
Add Acmltrs
AOOB
ABA
C8 2 2 DB J 2 EB 5 2 FB 4 3
18 2 1
8 .. M-B
A+B-A
t
t
-- t t t t
t t t t
--
Add with Carry AOCA 89 2 2 99 3 2 A9 5 2 B9 4 3 A+M+C-A t t t t t
AOCB C9 2 2 09 J 2 E9 5 2 F9 4 3 B"'M+C-B t 1 t t 1
And ANDA
ANOB
84
C4
2
2
2
2
94
04
3
3
2
2
A4
E4
5
5
2
2
B4
F4
4
4
J
J
A. M-A
B· M-B
-- -• • 1
t
t
R
R
--
Bit Test BITA
BITB
85
C5
2
2
2
2
95
OS
3
3
2
2
AS
E5
5
5
2
2
B5
F5
4
4
3
3
A.M
B.M
-- •- t
1
t
1 R
R
-•
•- -
Clear CLR 6F 1 2 1F 6 3 00 -M • R S R R
CLRA 4F 2 1 00 -A R S R R
ClRB SF 2 1 00 -B • • R S R R
Compare CMPA
CMPB
81
C1
2
2
2
2
91
01
3
3
2
2
Al
E1
5
5
2
2
B1
F1
4
4
3
3
A-M
B-M
-- -• 1
t
t t 1
t t t
Compare Acmltrs
Complement, 1's
CBA
COM 63 1 2 13 6 3
11 2 1 A-B
M-M
-•
-• ·•
t
t
t t 1
t R S
COMA 43 2 1 A-A 1 t R S
COMB 53 2 1 B.... 8 • • 1 t R S
Complement. 2's NEG 60 7 2 10 6 3 00 - M - M • • t 1 0 0
(Negate) NEGA 40 2 1 00 - A-A • • t 1 0 0
Decimal Adjust, A
NEGB
DAA
50
19
2
2
1
1
00 - B - B
Converts Binary Add. of BCD Characters
- •
• •
t
t
t
t
0 0
1 0
IOto BCD Fa. mat
Decrement DEC 6A 1 2 7A 6 3 M -l-M • • 1 t
o0-•
DECA
OEca
4A
5A
2
2
1
1
A - l-A
B-1-8
- •
• •
t
t
t
t
o ••
Exclusive OR EORA 88 2 2 98 3 2 A8 5 2 88 4 3 A-vM-+A • • 1 t R
Increment
EORB
INC
C8 2 2 08 3 2 E8
6C
5
1
2
2
F8
7C
4
6
3
3
B~M-+B
M + 1-M ·- •- t
® •
1
t
t
R
-
INCA
INCB
4C
5C
2
2
1
1
A+l-+A
8 .. , .... B
• -
t t ® •
• • t t ® •
load Acmltr LDAA
LOAB
86
C6
2
2
2
2
96
06
J
3
2
2
A6
E6
5
5
2
2
B6
F6
4
4
3
3
oM .... A
M -B
•
•
•
•
1
t
t
t
R
R
-•
Or. Inclusive ORAA 8A 2 2 9A 3 2 AA 5 2 BA 4 3 A+M .... A • • t t R •
DRAB CA 2 2 OA 3 2 EA 5 2 FA 4 3 B+M -B • • t t R •
Push Data PSHA
PSHB
36
37
4
4
1
1
A -+ MSp. SP-1 .... SP
8 - MSp, SP-l -SP
- - -
• •
• • • • •
•
•
Pull Data PULA 32 4 1 SP .. 1 .... SP. MSp - A
-• • • • •
-- •• ®® -
PULB 33 4 1 SP .. l-SP. MSp .... B • • • • •
Rotate Left ROL 69 7 2 79 6 3 t t t
r~·- ~-~~;;~~L~-:J
ROLA
ROLB
49
59
2
2
1
1 :1 C b7 ~
be
• •
t
t
t t
t @ t
Rotate Right ROA
RORA
66 1 2 16 6 3
46 2 1 :: C O -:'- rnJllD:}=:J
B C b] be
-• •• t
t
t ® t
t ® t
ROR8 56 2 1
-+
• • t t ® t
Shift Left. Arithmetic ASL 68 1 2 78 6 3 ~ • • t t ® t
:]
-+
--
lSRA 44 2 1 o -+ ITD..ITlIJ -+ 0
c • R t @ t
b7 be
LSRB 54 2 1 • R t @ t
Store Acmltr. STAA 91 4 2 A1 6 2 81 5 3 A -M • • t t R •
Subtract
STA8
SUBA 80 2 2
07
90
4
3
2
2
E7
AD
6
5
2
2
F7
80
5
4
3
3
8-M
A-M-A ·- -• t
f
t R
t t
•
t
Subract Acmltrs.
SUBB
SBA
CO 2 2 00 3 2 EO 5 2 fO 4 3
10 2 1
B - M-8
A-B-A ·• ·• t
t
t t
t t
t
t
Subtr. with Carry SBCA 82 2 2 92 3 2 A2 5 2 B2 4 3 A-M-C-A • • t t t t
Transfer Acmltrs
SBCB
TAB
C2 2 2 02 3 2 E2 5 2 F2 4 3
16 2 1
B-M-C-+B
A -+B -• •• t
t
t t
t R •
t
TBA 11 2 1 B-A • • t t R •
Test. Zero or Minus TST 60 7 2 70 6 3 M - 00 • • t t R R
:. TSTA 40 2 1 A-DO • • t t R R
TST8 50 2 1 B - 00 • • t t R R
Instruction Set (spare copy)
·· ·· ·
Decrem~nt Stad Pntr DES 34 4 1 SP - 1 -SP • • • •
Increment Index Reg INX 08 4 1 X + l-X • • t •
Increment Stack Pntr INS 31 4 1 SP +- 1 - SP • • • • •
load Index Reg
Load Stac k Pntr
lOX
lOS
CE
BE
3
3
3
3
DE
9E
4
4
2
2
EE
AE
6
6
2
2
FE
BE
5
5
3
3
M - XH. (M +- 1) - XL
M -SPH. (M +- 1) -SP l
• ·® t R •
• • @ t R •
Store [ndel Reg STX OF 5 2 EF 7 2 FF 6 3 XH -M,Xl-(M+-1) • • ® : R •
-- · · ··
Store Stack Pntr STS 9F 5 2 AF 7 2 BF 6 3 SP H -M, SP L -(M ~ 1) • • ® : R
Indx Reg - Stack Pnlr TXS 3S 4 1 X-I .... SP • • • • •
Stact. Pntr -Indx Rt9 TSX 30 4 1 SP +- 1 - X • •
JUMP AND BRANCH RElA TlVE INDEX EXTNO INHER 5 4 3 2 1 0
Branch Always
Branch If Carry Clear
BRA
BCC
20
24
4
4
2
2
None
c=o -• •• •• •• ·• ··
Bra:lch If Carry Set BCS
8EQ
25
21
"
4 2
2
C =1
Z =1
• • • •
• • • • ·· ••
· · ·•
Branch If :: Zero
Branch If ~ Zero BGE 2C 4 2 Ni1V::O • • •
> BGT 2E 4 2 Z + (N.; V):: 0 • • • • •
- ·· ··
Branch If Zero
Branch If HIgher - "I\Sl!1\ del BHI 22 4 2 C + Z :: a • • •
Blanch If ~ Zero BlE 2F 4 2 Z +,N .;V) = 1 • • • •
Branch If Lower Or Same
Branch If < Zero
-u""9"e' BlS
BLT
23
20
4
4
2
2
C + Z:: 1
N'E-V:: 1 • • • ·
• • • •
· ·· •
•
•
Branch If MInus 8MI 2B 4 2 N:: 1 • • • •
· · · · · ••
Branch If Not Equal Zero BNE 26 4 2 Z=O • • • • •
Branch If Overflow Clear BVC 28 4 2 v=o • •
BVS 29 4 2 V:: 1 • • • •
· · ·· ·•
B, anch II Overflow Set
Branch If Plus BPL 2A 4 2 N=O • •
BSR 80 8 2 • •
·.
· ·-I-. ··· ··•
Branch T0 Subroulln~
Clear Overflow
Set Carry
CLV
SEe
OA
00
2
2
1
1
O-V
l-e
• ·
• • R • @
• • • • • S
(Bit CI Test: Oeclmal value of most sigl\lficant BCD Character greater than nrne'
(Not cleared .f prevIously set.)
Set Interrupt Mask SEI OF 2 1 1-1 • S • • • • o (Bit V) Test: Operand:: 10000000 ;lrior to oecutlon?
Set Overflow SEV OB 2 I 1-V • • • • S • ® (BIt VI Test: Operand = 011 till t prior to execution?
Acmlt( A - CCR TAP 06 2 1 A -CCR --@-- ® (BIt V) Test: Set equal to result of N.; Cafter shIft has occurred.
LEGEND: 00 Byte:: Zero; @ (All) Load Condition Code Register from Stack. (See SpeCial Opp.!a(lons)
Operation Code IHexadecimal); H Half·carry from bit 3; ([9 (Bit I) Set when Interrupt occurs. If p,eviously ser, a Non·Maskabie Inlerruct I~
OP reqUIred ro eXIt (he walt slate.
NumbEr of MPU Cycles; I Interrupt mask
(ALL) Set according 10 the conlenls of Accumulato, A.
Number of Program Bytes; N NegatIve (sign bitl
Anthmetlc Plus; Z Zero (byte)
Arithmetic Minus; V Overflow, 2·s complement
Boolean AND; C Carry from bit 7
MSp Contents of memory location R Reset Always
pointed to be Stack Pointer; S Set Always
+ Boolean InclUSIve OR; t Test and set if true. cleared otherwise
oB Boolean Exclusive 0 R: • Not Affected
M Complement of M; eCR ConditIon Code Register
Transfer Into; lS least SignIficant
Bit = Zero; MS Most SigO/ficant
The DAA instruction converts the normal hex sum, OE, to 14, the
expected decimal sum in BCD. This is accomplished internally by
adding 6 in this example (OE + 06 = 14). Details of the internal
operation of the DAA instruction are not essential to its use,
but are given at the bottom of this page. What is important is
that this instruction operates on ACC A, only after execution of
the ADD, ADC or ABA instructions.
Assuming that symbolic addresses OLDATA and NUDATA each
contain one BCD digit, write the instructions to produce the BCD
sum in ACC A.
----/
L[)A A OLDATA
ADD A NUDATA
DAA
DAA Details: When two 2 digit BCD numbers are added a "carry",
produced by the addition of the "least significant column, sets
lJ
_ _-----J/
:+:
:+: ADDITION OF 4 CHAf;.: BCD DATA. SUt'1 IN TOTAL.
:+:
l1:1l10 7F t1i50 eLF.: TOTAL
E:6 ~:::11.5]:
~31.~:::13 L[)A A OLDATA+1.
t11.£16 BE: (11.55 ADD A NU[)ATA+1.
_.
~31.(19 i'=t DAA BCD SUr1 OF 2 LO DIGITS
01.~3A Eo?" 0:151- STR A TOTAL+1.
01.li[) E:6 ~:::11.52 LDA A OLDATA
~31.1.0 89 ~31.54 ADC A NU[)ATA
ttl1.3 1.9 DAA BCD SUr1 OF .-.
~ HI DIGITS
011.4 B7 l31.5£1 STA A TOTAL
I
•
I