Knowledge of Binary Numbers Prerequisite to Writing a program

10 454 0
Knowledge of Binary Numbers Prerequisite to Writing a program

Đang tải... (xem toàn văn)

Thông tin tài liệu

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 + − + + h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 15 The result is correct if only the low-order 8 digits are considered. 01100000 00100000 10000000 1 − 96 + 32 + 128- − This calculation, on the other hand, gives an incorrect result, because the correct result of +144 cannot be represented by an 8-bit signed binary number. Such a state of a result exceeding a predetermined length is called an "overflow." Next, examples of subtraction of signed binary numbers are given below. 11111111 01010010 01010001 − 1- 82+ 81+ − No overflow has occurred in this calculation. 00100000 00100000 10000000 − 96+ 32+ 128- − Subtracting a positive value from a negative value should always deliver a negative value, but an overflow is seen in this case because the result is positive. Summing up, the following four conditions Positive value + Positive value = Negative value Negative value + Negative value = Positive value Positive value - Negative value = Negative value Negative value - Positive value = Positive value are called "overflows." It is important to note the length of any signed binary number, because simply prefixing a signed binary number with 0 could result in an inverted sign as in the following example: 8-bit 1000 0010 is -126 9-bit 0 1000 0010 is +130 Twos complement notation Signed binary numbers are also called a "twos complement notation." If summing up two values produces a carry, with the result of 0, then a twos complement relation exists between the two values as in the following example: 00000000 01001111 10110001 1 + These two values have the same absolute value but differ only in their sign. Ones complements, too, exist. h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 16 11111111 01001110 10110001 + If summing up two values results in a complete sequence of 1s, a ones complement relation is said to exist between them. These two values have their 0s and 1s inverted. Changing the sign of a signed binary number can be easily done by creating its ones complement and then adding 1 to it. This is the same as calculating a complement. As an example, consider converting +12 to -12. +12 can be expressed in an 8-bit format as: 00001100 First, create the ones complement of 00001100: 11110011 Then, add 1 to it to get: 11110100, or -12 2.4 BCD Code Short for "Binary Coded Decimal," BCD code means a decimal number expressed in binary. The BCD code represents each decimal digit with a string of four binary digits. For example, decimal 156 is expressed as 0001 0101 0110 in BCD code. The addition of BCD coded characters must deliver a decimal result like 48 + 24 = 72 as in the example: 01110010 00100100 01001000 + 72 24 48 + It involves addition different from the ordinary addition of binary numbers. To this end, special instructions are needed to perform BCD calculations. 2.5 ASCII Code The ASCII (American Standard Code for Information Interchange) code is used to represent alphanumeric characters and symbols. The ASCII code represents each character with 8 bits. Table 2.3 is an ASCII code table. For example, A is represented by 01000001 and a by 01100001. When you type 123 from the keyboard and then press the return key, the following four characters are entered: Character code of 1 00110001 Character code of 2 00110010 Character code of 3 00110011 Character code of the return 00001101 h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 17 To handle these characters as unsigned binary 123, they must be converted to the binary number: 01111011 On the other hand, to display signed binary number 10000000 on the display as -128, it must be converted to the four characters: Character of the minus sign 00101101 Character code of 1 00110001 Character code of 2 00110010 Character code of 8 0011100 Note that the ASCII code is characters, and 1 through 9 are "digits" and not "numeric values." [Audiovisual guidance] 2.6 Numeric Representations While the computer internally operates on the basis of binary numbers, binary numbers take more digits than decimal numbers to represent the same value. For example, decimal 100 can be expressed in three digits, but its unsigned binary equivalent would end up in a seven-digit sequence of 1100100. Hexadecimal numbers save on extra lengths and make values easier to read. Memory addresses are expressed in hexadecimal in most situations. Binary notation also comes an effective way of representation as long as economizing on lengths is concerned, but it involves a laborious binary conversion process. Hexadecimal numbers are easy to convert to and from h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 18 binary numbers, because each hexadecimal digit simply represents a string of four binary digits. Table 2.4 gives the correspondence among binary, hexadecimal, and decimal numbers. Table 2.4 Correspondence among binary, hexadecimal and decimal numbers Binary Hexadecimal Decimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 In programs, H 'and B' designate hexadecimal notation and binary notation, respectively. For example, binary 12 is written as H'12, and binary 01011110 is expressed as B'01011110. 12 or 10, when simply stated, means a decimal number. Example: H'12 H' identifies a hexadecimal number. It is 18 in decimal and 00010010 in binary. B'01011100 B' identifies a binary number. It is 5C in hexadecimal and 92 in decimal. 110 Decimal number. It is 6E in hexadecimal and 01101110 in binary. h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 19 1. Fill out the blanks below. Binary notation (8 bits) Decimal notation (unsigned) Decimal notation (signed) Hexadecimal notation 01001110 78 78 H'4E 01100100 100 100 H'64 11111101 253 -3 H'FD 10000000 128 -128 H'80 Binary notation 01001110 Decimal notation (signed) 01001110 = 64 + 8 + 4 = 2 = 78 Decimal notation (signed) = positive + 78, because the most significant bit is 0 Hexadecimal notation 0100 = 4 and 1110 = E, hence H'4E Decimal notation (unsigned) 100 Binary notation 100 = 64 + 32 + 4 = 01100100 Decimal notation (signed) = positive + 100, because the most significant bit is 0 Hexadecimal notation 0110 = 6 and 0100 = 4, hence H'64 Decimal notation (signed) -3 Binary notation -3 = -128 + 64 + 32 + 16+ 8 + 4 + 1 = 11111101 Decimal notation (unsigned) 128 + 64 + 32 + 16+ 8 + 4 + 1 = 253 Hexadecimal notation 1111 = F and 1101 = D, hence H'FD Hexadecimal notation H'80 Binary notation 10000000 = -128 Decimal notation (unsigned) 10000000 = 128 Decimal notation (signed) = negative -128, because the most significant bit is 1 2. Perform the operations given below and calculate the results to a length of 8 bits. Answer also whether a carry or borrow has been produced from the operations and whether an overflow has occurred from the results when they are viewed as signed binary numbers. Carry ( Yes ) Overflow ( No ) The result is 9 digits, with a carry. +72 + (-49) = +23, or positive + negative = positive with no overflow Borrow ( No ) Overflow ( Yes ) No borrow and no carry. -116 - (+43) = +65, or negative - positive = positive, with an overflow . numbers. This chapter covers the minimum knowledge of binary numbers prerequisite to writing a program in an assembler language. The concept of binary numbers. 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

Ngày đăng: 29/09/2013, 11:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan