Most modern computer systems do not represent numeric
values using the decimal
system.
Instead, they typically use a binary or two’s complement numbering system.
To
understand
the limitations of computer arithmetic, you must understand how
computers
represent
numbers.
A Review of the Decimal
System
You’ve been using the decimal (base 10) numbering system
for so long that you probably
take it for
granted. When you see a number like “123”, you don’t think about
the
value 123;
rather, you generate a mental image of how many items this value
represents.
In reality, however, the number 123
represents:
1*102 + 2 *
101 + 3*100
or
100+20+3
Each digit appearing to the left of the decimal point
represents a value between zero
and nine
times an increasing power of ten. Digits appearing to the right of the
decimal
point
represent a value between zero and nine times an increasing negative power of
ten.
For example, the value 123.456
means:
1*102 +
2*101 + 3*100 + 4*10-1 + 5*10-2 +
6*10-3
or
100 + 20 + 3 + 0.4 + 0.05
+ 0.006
The Binary Numbering
System
Most modern computer systems (including the IBM PC)
operate using binary logic.
The computer represents values using two voltage levels
(usually 0v and +5v). With two
such levels
we can represent exactly two different values. These could be any two
different
values, but
by convention we use the values zero and one. These two values,
coincidentally,
correspond
to the two digits used by the binary numbering system. Since there
is
a
correspondence between the logic levels used by the 80x86 and the two digits
used in
the binary
numbering system, it should come as no surprise that the IBM PC employs
the
binary
numbering system.
The binary numbering system works just like the decimal
numbering system, with
two
exceptions: binary only allows the digits 0 and 1 (rather than 0-9), and binary
uses
powers of
two rather than powers of ten. Therefore, it is very easy to convert a
binary
number to
decimal. For each “1” in the binary string, add in 2n where “n” is
the
zero-based
position of the binary digit. For example, the binary value
110010102
represents:
1*27 +
1*26 + 0*25 + 0*24 + 1*23 +
0*22 + 1*21 + 0*20
= 128 + 64 + 8 +
2
=
20210
To convert decimal to binary is slightly more difficult.
You must find those powers of
two which,
when added together, produce the decimal result. The easiest method is
to
work from
the a large power of two down to 20
. Consider the decimal value 1359:
• 210 = 1024, 211=2048. So 1024 is
the largest power of two less than 1359.
Subtract 1024 from 1359 and begin the binary value on
the left with a “1”
digit.
Binary = ”1”, Decimal result is 1359 - 1024 =
335.
• The next lower power of two (29= 512) is
greater than the result from
above, so
add a “0” to the end of the binary string. Binary = “10”,
Decimal result is still 335.
• The next lower power of two is 256 (28).
Subtract this from 335 and add a
“1” digit to the end of the binary
number. Binary = “101”,
Decimal result is 79.
• 128 (27) is greater than 79, so tack a “0”
to the end of the binary string.
Binary = “1010”, Decimal result remains
79.
• The next lower power of two (26 = 64) is
less than79, so subtract 64 and
append a “1”
to the end of the binary string. Binary = “10101”,
Decimal result is 15.
• 15 is less than the next power of two (25 =
32) so simply add a “0” to the
end of the
binary string. Binary = “101010”, Decimal result is still
15.
• 16 (24) is greater than the remainder so
far, so append a “0” to the end of
the binary
string. Binary = “1010100”, Decimal result is 15.
• 23 (eight) is less than 15, so stick
another “1” digit on the end of the binary
string.
Binary = “10101001”, Decimal result is 7.
• 22 is less than seven, so subtract four
from seven and append another one
to the
binary string. Binary = “101010011”, decimal result is 3.
• 21 is less than three, so append a one to
the end of the binary string and
subtract two
from the decimal value. Binary = “1010100111”, Decimal
result is
now 1.
• Finally, the decimal result is one, which is
20, so add a final “1” to the end
of the
binary string. The final binary result is “10101001111”
Data
Representation:
Binary numbers, although they have little importance in
high level languages, appear
everywhere
in assembly language programs.
In the purest sense, every binary number contains an
infinite number of digits (or bits
which is
short for binary digits). For example, we can represent the number five
by:
101
00000101
0000000000101
000000000000101
Any number of leading zero bits may precede the binary
number without changing its
value. We
will adopt the convention ignoring any leading zeros. For example,
1012
represents
the number five. Since the 80x86 works with groups of eight bits, we’ll find
it much
easier to zero extend all binary numbers to some multiple of four or eight bits.
Therefore, following this convention, we’d represent the
number five as 01012
or
000001012
.
Most people separate every three digits with a comma to
make
larger
numbers easier to read. For example, 1,023,435,208 is much easier to read and
comprehend
than 1023435208. We’ll adopt a similar convention in this text for binary
numbers. We
will separate each group of four binary bits with a space. For example,
the binary
value 1010111110110010 will be written 1010 1111 1011
0010.
We often pack several values together into the same
binary number.
1) The rightmost bit in a binary number is bit position
zero.
2) Each bit to the left is given the next successive bit
number.
An eight-bit binary value uses bits zero through
seven:
X7
X6 X5 X4 X3 X2
X1 X0
A 16-bit binary value uses bit positions zero through
fifteen:
X15
X14 X13 X12 X11 X10
X9 X8 X7 X6 X5
X4 X3 X2 X1
X0
Bit zero is usually referred to as the low order
(L.O.) bit.
The left-most bit is typically called the high order
(H.O.) bit.
The Hexadecimal Numbering
System
A big problem with the binary system is verbosity. To
represent the value 20210
requires
eight binary digits. The decimal version requires only three decimal digits
and,
thus,
represents numbers much more compactly than does the binary numbering
system.
This fact was not lost on the engineers who designed
binary computer systems. When
dealing with
large values, binary numbers quickly become too unwieldy.
Unfortunately,
the computer
thinks in binary, so most of the time it is convenient to use the binary
numbering
system.
Although we can convert between decimal and binary, the conversion
is
not a
trivial task. The hexadecimal (base 16) numbering system solves these
problems.
Hexadecimal numbers offer the two features we’re looking
for: they’re very compact, and
it’s simple
to convert them to binary and vice versa. Because of this, most binary
computer
systems
today use the hexadecimal numbering system2. Since the radix (base) of a
hexadecimal
number is
16, each hexadecimal digit to the left of the hexadecimal point
represents
some value
times a successive power of 16. For example, the number 123416 is
equal to:
1 * 163 + 2 *
162 + 3 * 161 + 4 * 160
or
4096 + 512 + 48 + 4 =
466010.
Each hexadecimal digit can represent one of sixteen
values between 0 and 1510. Since
there are
only ten decimal digits, we need to invent six additional digits to represent
the
values in
the range 1010 through 1510. Rather than create new
symbols for these digits,
we’ll use
the letters A through F. The following are all examples of valid
hexadecimal
numbers:
123416 DEAD16 BEEF16 0AFB16 FEED16 DEAF16
Since we’ll often need to enter hexadecimal numbers into
the computer system, we’ll
need a
different mechanism for representing hexadecimal numbers. After all, on
most
computer
systems you cannot enter a subscript to denote the radix of the associated
value.
We’ll adopt the following
conventions:
• All numeric values (regardless of their radix) begin
with a decimal digit.
• All hexadecimal values end with the letter “h”, e.g.,
123A4h3.
• All binary values end with the letter
“b”.
• Decimal numbers may have a “t” or “d”
suffix.
Examples of valid hexadecimal
numbers:
1234h 0DEADh 0BEEFh
0AFBh 0FEEDh 0DEAFh
As you can see, hexadecimal numbers are compact and easy
to read. In addition, you
can easily
convert between hexadecimal and binary.
Consider the following table:
Table 1: Binary/Hex Conversion
Binary
Hexadecimal
0000
0
0001
1
0010
2
0011
3
0100
4
0101
5
0110
6
0111
7
1000
8
1001
9
1010
A
1011
B
1100
C
1101
D
1110
E
1111
F
This table provides all the information you’ll ever need
to convert any hexadecimal number
into a
binary number or vice versa.
To convert a hexadecimal number into a binary number,
simply substitute the corresponding
four bits
for each hexadecimal digit in the number. For example, to convert 0ABCDh into a
binary
value, simply convert each hexadecimal digit according to the table
above:
Data Representation:
0ABCDh into a binary value, simply convert each
hexadecimal digit according to the table above:
0
A B C
D
Hexadecimal
0000 1010 1011 1100 1101
Binary
To convert a binary number into hexadecimal format is
almost as easy. The first step is
to pad the
binary number with zeros to make sure that there is a multiple of four bits
in
the number.
For example, given the binary number 1011001010, the first step would be
to
add two bits
to the left of the number so that it contains 12 bits. The converted
binary
value is
001011001010. The next step is to separate the binary value into groups of
four
bits, e.g.,
0010 1100 1010. Finally, look up these binary values in the table above and
substitute
the
appropriate hexadecimal digits, e.g., 2CA. Contrast this with the difficulty of
conversion
between
decimal and binary or decimal and hexadecimal! Since converting between
hexadecimal
and binary
is an operation you will need to perform over and over again, you should take a
few
minutes and
memorize the table above. Even if you have a calculator that will do the
conversion
for you,
you’ll find manual conversion to be a lot faster and more convenient when
converting
between
binary and hex.