Nociones de aritmética

Escrito el Lunes 06 de Abril del 2009 por Ealdor

[A] 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 0 representation) is on and all other bits are off or as an 8-bit binary number in which case the value is:

0x 27 + 0 x 26 + 0 x 25 + 0 x 24 + 1 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.

(1) Binary arithmetic rules

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 with 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 v Bj v Cj-1), where, as defined previously, V 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 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
11110100 = -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
-8 = 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      B1

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 i 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 am 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 0 equals positive and a 1 equals negative.

This bit is correct for the two9s complement representation but also flags the microprocessor whether to carry or borrow from the address high byte.