MCS6500
Microcomputer Family
Programming Manual
APPENDIX H
REVIEW OF BINARY
AND
BINARY CODED DECIMAL
ARITHMETIC
The number 1789 is assumed by most people to mean one thousand, seven
hundred eighty-nine, or 1 x 103 + 7 x 102 + 8 x 101 + 9 x 100. However,
until the number base is defined, it might mean
1 x 163 + 7 x 162 + 8 x 161 + 9 x 160
which is hexadecimal and the form used in the microprocessor.
In order to distinguish between numbers on different bases, mathematicians
usually write 178910 or just 1789 for base 10, or decimal, and
178916 for base 16 for hexadecimal. Because very few computers or I/O devices
allow subscripting, all hexadecimal numbers are preceded by a $
notation. Then 1789 means base 10 and $1789 means base 16. Why hexadecimal?
This is a convenient way of representing 2 digits in 8 bits.
The MCS650X is a byte-oriented microprocessor which means most operations
have 8-bit operations.
There are 2 ways to look at 8 bits. The first is as 8 individual
bits in which 00001000 means that bit 3 (bit 7 to representation) is on
and all other bits are off or as an 8-bit binary number in which case the
value is
0 x 27 + 0 x 26 + 0 x 25 + 0 x 24 + l x 23 + 0 x 22 + 0 x 21 + 0 x 20 = 8
or $08.
For logic analysis purposes, each bit is unique, but for arithmetic
purposes, the 8 bits are treated as a binary number.
Binary Arithmetic Rules:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 with a carry
Carry occurs when the resulting number is too long for the base. In
decimal, 8 + 4 = 2 + 10.
In hexadecimal, $8 + $4 = $C (see hexadecimal details), so that 8 + 4 has
a carry in base 10 but not in base 16.
Using these rules to add 8 + 2 in binary gives the following:
00001000 8 1 x 23
00000010 +2 1 x 21
00001010 10 1 x 23 + 1 x 21
Therefore, any number from 0 - 255 may be represented in 8 bits, and
binary addition performed using the basic binary add equation,
Rj =(Aj ∨ Bj ∨ Cj-1), where, as defined previously, ∨ is notation for
Exclusive-Or.
In most applications, it is also necessary to subtract. Subtract
operations either require a different hardware implementation or a new way
of representing numbers.
A combination of this is to implement a simple inverter in each bit.
This would make
00001100 12
11110011 -12
However, when subtracting 12 from 12, the result should also be 0.
00001100 +12
11110011 -12
11111111 0
However, if a carry is added to the complemented number:
1 Carry
00001100 12
11110011 -12
00000000 = 0
If, instead of representing -12 as the complement of 12, it is represented
as the complement plus carry, the following is obtained:
11110011 = 12
1 = Carry
11110011 -12
00001100 = +12
00000000 = 0
This representation is called two's complement and represents the way that
negative numbers are kept in the microcomputer. Below are examples of
negative numbers represented in two's complement form.
-0 = 00000000
-1 = 11111111
-2 = 11111110
-3 = 11111101
-4 = 11111100
-5 = 11111011
-6 = 11111010
-7 = 11111001
-S = 11111000
-9 = 11110111
Hexadecimal is the representation of numbers to the base 16. The following
table shows the advantages of Hex:
Hexadecimal Binary Decimal
0 0000 00
1 0001 01
2 0010 02
3 0011 03
4 0100 04
5 0101 05
6 0110 06
7 0111 07
8 1000 08
9 1001 09
A 1010 10
B 1011 11
C 1100 12
D 1101 13
E 1110 14
F 1111 15
Because 16 is a multiple of 2, hexadecimal is a convenient shorthand
for representation of 4 binary digits or bits. The rules on arithmetic
also hold.
Binary Hex
0100 1111 4F
+ 0110 0010 + 62
1011 0001 Bl
To take advantage of this shorthand, all addresses in this manual are
shown in hexadecimal notation. It should be noted that the reader should
learn to operate in Hex as soon as possible. Continual translation back
to decimal is both time consuming and error prone. Working in Hex and
binary will quickly force learning of hexadecimal manipulation and the familiarity
with working with this convenient representation.
Although many microcomputer applications can successfully be accomplished
with binary operations, some applications are best performed in
decimal. Although the use of 1 decimal character per byte would be a
legitimate way to solve this problem, this is an inefficient use of the capability
of the 8-bit byte.
The microprocessor allows the use of packed BCD representation. This
representation is, in 4-bit form:
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
In BCD, the number 79 is represented:
Binary BCD Hex
01111001 = 79 = 79
The microprocessor automatically takes this into account and corrects
for the fact that
Decimal BCD Hex
79 = 01111001 79 = 01111001
+12 = 00010010 12 = 00010010
91 = 10010001 88 = 10001011
The only difference between Hex and BCD representation is that the
microprocessor automatically adjusts for the fact that BCD does not allow
for Hex values A - F during add and subtract operations.
The offset which follows a branch instruction is in signed two's
complement form which means that
$+50 = +80 = 01010000
and $-50 = -80 = 10110000
Proof = 00000000
The sign for this operation is in bit 7 where an equals positive and
a 1 equals negative.
This bit is correct for the two's complement representation but also
flags the microprocessor whether to carry or borrow from the address high
byte.
The following 4 examples represent the combinations of offsets which
might occur (all notations are in hexadecimal):
Example H.4.1: Forward reference, no page crossing
0105 BNE
0106 +55
0107 Next OP CODE
To calculate next instruction if the branch is taken
Offset +55 01010101
Address Low
for next
OP CODE 07 00000111
5C 01011100
with no carry, giving 015C as the result.
Example H.4.2: Backward reference, no page crossing
015A BNE
015B -55
015C Next OP CODE
To calculate if branch is taken,
Offset -55 = AB = 10101011
+ Address Low for
Next OP CODE +55 = 5C = 01011100
07 07 00000111
The carry is expected because of the negative offset and is ignored,
thus giving 0107 as the result.
Example H.4.3 : Backward reference if page boundary crossed
0105 BNE
0106 -55
0107 Next OP CODE
To calculate if branch is taken, first calculate a low byte
Offset -55 = AB = 10101011
Address Low for
Next OP CODE 07 = 07 = 00000111
B2 = B2 = 10110010
There is no carry from a negative offset; therefore, a carry must be
made:
-1 = -1 = FF = 11111111
+ Address High = 01 = 01 = 00000001
00 00 00000000
This gives 00 B2 as a result.
Example H.4.4: Forward reference across page boundary
00B0 BNE
00B1 +55
00B2 Next OP CODE
To calculate next instruction if branch is taken.
Offset 55 = 01010101
Address Low
for Next
OP CODE B2 = 10110010
07 00000111
with carry on positive number.
+1 1 = 00000001
Address High 00 = 00000000
1 = 00000001
which gives 0107.