Page 405 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 405
382 Appendix 1 Number Representations and Binary Arithmetic
A1.2 Binary Arithmetic
One can add the binary representations of unsigned numbers exactly like the addition of
decimal representations, except that a carry is generated when 2 is obtained as a sum of
particular bits. For example,
1010 1100 1110
ifllll +0111 ±0111
10001 10011 10101
Notice that when two 4-bit representations are added and a carry is produced from adding
the last or most significant bits, five bits are needed to represent the sum.
Similarly, borrows are generated when 1 is subtracted from 0 for a particular bit. For
example,
1111 1011 1000
-0101 -0100 -1001
1010 0111 (1)1111
In the last of the examples above, we had to borrow out of the most significant bit,
effectively loaning the first number 24 to complete the subtraction. We have put a "(1)"
before the 4-bit result to indicate this borrow. Of course, when a borrow occurs out of
the most significant bit, the number being subtracted is larger than the number that we
are subtracting it from.
When handling numbers in microprocessors, one usually has instructions that add
and subtract m-bit numbers, yielding an m-bit result and a carry bit. Labeled C, the carry
bit is put equal to the carry out of the most significant bit after an add instruction, while
for subtraction, it is put equal to the borrow out of the most significant bit. The bit C
thus indicates unsigned overflow; that is, it equals 1 when the addition of two positive
m-bit numbers produces a result that cannot be expressed with m-bits, while with
subtraction, it equals 1 when a positive number is subtracted from a smaller positive
number so that the negative result cannot be expressed with equation (1).
We can picture the m-bit result of addition and subtraction of these nonnegative
numbers (also called unsigned numbers) using Figure Al.l, where m is taken to be four
bits. For example, to find the representation of M + N, one moves N positions clockwise
from the representation of M while, for M - N, one moves N positions counterclockwise.
Mathematically speaking, we are doing our addition and subtraction modulo-16 when
we truncate the result to four bits. In particular, we get all the usual answers as long as
unsigned overflow does not occur, but with overflow, 9 + 8 is 1, 8-9 is 15, and so on.
We also want some way of representing negative numbers. If we restrict ourselves
m
to m binary digits c m, CQ, then we can clearly represent 2 different integers. For
example, with (1) we can represent all of the nonnegative integers in the range 0 to
m
2 -l. Of course, only nonnegative integers are represented with (1), so that another
representation is needed to assign negative integers to some of the m-bit sequences. With
the usual decimal notation, plus and minus signs are used to distinguish between
positive and negative numbers. Restricting ourselves to binary representations, the
natural counterpart of this decimal convention is to use the first bit as a sign bit. For
example, put c m equal to 1 if N is negative, and put c m _i, equal to 0 if N is positive or

