Tải bản đầy đủ (.pdf) (10 trang)

Knowledge of Binary Numbers Prerequisite to Writing a program

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (164.89 KB, 10 trang )


h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 10
Chapter 2
Knowledge of Binary Numbers Prerequisite to Writing
a Program



There are close links between the computer and binary numbers. This
chapter covers the minimum knowledge of binary numbers prerequisite to
writing a program in an assembler language. The concept of binary numbers is
not restricted to the H8/300H but broadly pertains to computers in general.
Without a correct understanding of the topics covered here, you would not be
able to write correct programs. The key concepts of "Signed binary numbers,"
"Carry," and "Overflow," among other things, would be needed instantly.
Even when you have finished with this chapter, refer back to it from
time to time as needed.





The reason why binary numbers are used in the computer is that the
computer is built of digital circuitry. Digital circuitry concerns only two states -
whether a voltage of interest is higher or lower than a given voltage - and not
any intermediate voltage. A higher-voltage state is designated by H, a lower-
voltage state by L. As the computer is a calculator, the two states of H and L
can be more conveniently expressed in numeric terms as 1 and 0 in binary. All
binary numbers that the computer handles correspond to H and L in digital
circuitry.
The unit bit, or binary digit, is used to count binary numbers. For


example, a reference to 8 bits means 8 digits in binary. A sequence of 8 bits is
called a "byte."

2.1 Kinds of Data Handled by Microprocessors

Before starting to consider how data is represented in binary numbers,
let's see what kinds of data are available. Here, data is broadly grouped into
numeric data and character data as shown in Figure 2.1.


Figure 2.1 Kinds of data

h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 11
Numeric data is classified into unsigned binary numbers, signed binary
numbers that distinguish between positive and negative, and BCD code used to
express decimal numbers.
Character data is used to print or display characters, or enter characters
from the keyboard. The ASCII code is mainly used in microprocessors.

2.2 Numeric Representation in Unsigned Binary Number

Unsigned binary numbers do not allow for the positive or negative
signs. To promote understanding, unsigned binary number may be
conceptually converted to decimal numbers to express values. Consider an
eight-digit binary number as an example.
Since each digit of a binary number has a weight of 2, the least
significant digit is 2
0
, or 1, the next digit is 2
1

, or 2, and still the next digit is 2
2
,
or 4. Thus, the weight doubles on each carry. In converting an 8-bit binary
number to a decimal number, assuming


we get
Decimal representation =
a
7
*2
7
+ a
6
*2
6
+ a
5
*2
5
+ a
4
*2
4
+ a
3
*2
3
+ a

2
*2
2
+ a
1
*2
1
+ a
0
*2
0


10110010 in binary, for example, is converted to a decimal equivalent as
1*2
7
+ 0*2
6
+ 1*2
5
+ 1*2
4
+ 0*2
3
+ 0*2
2
+ 1*2
1
+ 0*2
0


= 128 + 32 + 16 + 2
= 178
All-one data "11111111" is the largest number that can be expressed in
an 8-bit format. It corresponds to 255 in decimal. Eight-bit unsigned binary
numbers can represent decimal numbers from 0 to 255.
Generally, N-bit unsigned binary numbers can represent the range of 0
to 2
N
-1

Table 2.1 lists the lengths of unsigned binary numbers and the ranges
they can represent.

Table 2.1 Ranges of unsigned binary numbers
Binary number length Range that can be represented
4 digits (4 bits)
0 to 15
8 digits (8 bits)
0 to 255
16 digits (16 bits)
0 to 65,535
32 digits (32 bits)
0 to 4,294,967,295




h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 12
Examples of addition of unsigned binary numbers are given below. To

their right are their decimal equivalents.

10000010
00111000
01001010
+

130
56
74
+

and,

00000011
01111010
10001001
1
+

3
122
173
+


This calculation, on the other hand, yields an incorrect result. The
resultant 9-bit value of 255 may appear correct, but the fact is that "calculating
in 8-bit terms within the computer delivers a result in 8-bit terms only." You
can stretch or contract the length of a value as long as you work on its

calculation on paper or in your brains, but not in the computer. You need to
remember the length of arithmetic circuitry in the computer at all times.
The addition of an extra digit to the length of a result is called a "carry."


Figure 2.2 Carry in a binary number

Next, examples of subtraction of unsigned binary numbers are given
below. To their right are their decimal equivalents.
00011110
10010111
10110101


30
151
181


and,

h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 13

11111111
01010000
01001111


255
80

79



This calculation yields an incorrect result due to its failure to subtract a
given value from a smaller value, where a 1 was leased from the ninth bit of
the lower value. This is called a "borrow."

When a carry or borrow occurs in the course of a calculation in the
computer, they are stored in status (in the H8/300H, the condition code). When
writing a program, include a condition test instruction to define what specific
action should be taken if a carry or borrow is encountered in the execution of a
calculation instruction.
Generally, no distinction is made between a carry and a borrow in the
computer, but they are collectively called a "carry."

2.3 Numeric Representation in Signed Binary Number

Signed binary numbers distinguish between positive and negative, but
they cannot be prefixed with a sign, as in +10110001 or -11001110. This is
because the computer operates on the basis of digital circuitry in which only
two voltage states H and L exist, as explained earlier. As H and L are
designated by 1 and 0, the positive and negative states must be designated by a
combination of 1 and 0, as well.
To promote understanding, the most significant bit of data is assumed
to designate a sign. Eight-bit data is expressed as
-(a
7
*2
7

)+ a
6
*2
6
+ a
5
*2
5
+ a
4
*2
4
+ a
3
*2
3
+ a
2
*2
2
+ a
1
*2
1
+ a
0
*2
0



Only the most significant bit is treated as being negative. Accordingly,

00000000 represents +0
00000001 represents +1
00000010 represents +2
--------
01111111 represents +127

Thus, signed binary values having their most significant bit being 0 are treated
positive.

10000000 represents -128
10000001 represents -127
10000010 represents -126
--------
11111111 represents -1


h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 14
Thus, signed binary values having their most significant bit being 1 are treated
negative.
Generally, N-bit signed binary numbers can represent values in the following
range:
-2
N-1
to +2
N-1
-1

Table 2.2 lists the lengths of signed binary numbers and the ranges they

can represent.

Table 2.2 Ranges of signed binary numbers
Binary number length Range that can be represented
4 digits (4 bits) -8 to -1, +0 to +7
8 digits (8 bits) -128 to -1, +0 to 127
16 digits (16 bits) -32,768 to -1, +0 to 32,767

The most significant bit of a signed binary number is called a "sign bit"
because it denotes a sign.


Figure 2.3 8-bit binary numbers

Examples of addition of signed binary numbers are given below.

00000000
11111111
00000001
1
+

0
1
1
+

+
+






×