270 likes | 425 Views
Chapter Two Data Numbering and Character Encoding System in Microcomputer 计算机中的数制和编码 ( 3 ). 2.3 信 息 的 编 码. 2.3.1 二进制编码的十进制数 (BCD 编码 ) Binary Coded Decimal----sequence of nybbles representing digits 0-9 of the number.
E N D
Chapter TwoData Numbering and Character Encoding System in Microcomputer 计算机中的数制和编码(3) 2014年11月11日1
2.3 信 息 的 编 码 2.3.1 二进制编码的十进制数(BCD编码) Binary Coded Decimal----sequence of nybbles representing digits 0-9 of the number 2014年11月11日2
4位二进制数码有16种组合(00001111),原则上可任选其中的10个来分别代表十进制中09这10个数字。但为了便于记忆,最常用的是8421 BCD码,这种编码从00001111这16种组合中选择前10个即00001001来分别代表十进制数码09,8、4、2、1分别是这种编码从高位到低位每位的权值。BCD码有两种形式,即压缩型BCD码和非压缩型BCD码。 2014年11月11日4
1.压缩型BCD码 压缩型BCD码用一个字节表示两位十进制数。例如,10000110B表示十进制数86。 2.非压缩型BCD码 非压缩型BCD码用一个字节表示一位十进制数。高4位总是0000,低4位用00001001中的一种组合来表示09中的某一个十进制数。 2014年11月11日5
十进制数 压缩型BCD码 非压缩型BCD码 1 2 3 9 10 11 19 20 21 00000001 00000010 00000011 00001001 00010000 00010001 00011001 00100000 00100001 00000001 00000010 00000011 00001001 00000001 00000000 00000001 00000001 00000001 00001001 00000010 00000000 00000010 00000001 表2.2 8421 BCD 码部分编码表 2014年11月11日6
需要说明的是,虽然BCD码可以简化人机联系,但它比纯二进制编码效率低,对同一个给定的十进制数,用BCD码表示时需要的位数比用纯二进制码多,而且用BCD码进行运算所花的时间也要更多,计算过程更复杂,因为BCD码是将每个十进制数用一组4位二进制数来表示,若将这种BCD码送计算机进行运算,由于计算机总是将数当作二进制数来运算,所以结果可能出错,因此需要对计算结果进行必要的修正,才能使结果为正确的BCD码形式。详见本小节例2.17。需要说明的是,虽然BCD码可以简化人机联系,但它比纯二进制编码效率低,对同一个给定的十进制数,用BCD码表示时需要的位数比用纯二进制码多,而且用BCD码进行运算所花的时间也要更多,计算过程更复杂,因为BCD码是将每个十进制数用一组4位二进制数来表示,若将这种BCD码送计算机进行运算,由于计算机总是将数当作二进制数来运算,所以结果可能出错,因此需要对计算结果进行必要的修正,才能使结果为正确的BCD码形式。详见本小节例2.17。 2014年11月11日7
例2.15十进制数与BCD数相互转换。 ① 将十进制数69.81转换为压缩型BCD数: 69.81=(0110 1001.1000 0001)BCD ② 将BCD数1000 1001.0110 1001转换为十进制数: (1000 1001.0110 1001)BCD=89.69 2014年11月11日8
例2.16设有变量x等于10010110B,当该变量分别为无符号数、原码、补码、压缩型BCD码时,试分别计算变量x所代表的数值大小。例2.16设有变量x等于10010110B,当该变量分别为无符号数、原码、补码、压缩型BCD码时,试分别计算变量x所代表的数值大小。 解 无符号数: x=10010110B=127+026+025+124+023+122+121+020=150 原码: [x]原=10010110B x= -026+025+124+023+122+121+020= -22 补码: [x]补=10010110B [x]原=[[x]补]补=11101010B x= -126+125+024+123+022+121+020= -106 BCD码: [x]BCD=10010110B x=96 2014年11月11日9
0011 1000 38的BCD码 +) 0100 1001 49的BCD码 1000 0001 81的BCD码 例2.17 (BCD码运算时的修正问题)用BCD码求38+49。 解 计算结果1000 0001是81的BCD数,而正确结果应为87的BCD数1000 0111,因此结果是错误的。其原因是,十进制数相加应该是“逢十进一”,而计算机按二进制数运算,每4位为一组,低4位向高4位进位与十六进制数低位向高位进位的情况相当,是“逢十六进一”,所以当相加结果超过9时将比正确结果少6,因此结果出错。解决办法是对二进制加法运算结果采用“加6修正”,从而将二进制加法运算的结果修正为BCD码加法运算结果。 2014年11月11日10
BCD数相加时,对二进制加法运算结果修正的规则如下:BCD数相加时,对二进制加法运算结果修正的规则如下: ① 如果两个对应位BCD数相加的结果向高位无进位,且结果小于或等于9,则该位不需要修正;若得到的结果大于9而小于16,则该位需要加6修正。 ② 如果两个对应位BCD数相加的结果向高位有进位(结果大于或等于16),则该位需要进行加6修正。 因此,两个BCD数进行运算时,首先按二进制数进行运算,然后必须用相应的调整指令进行调整,从而得到正确的BCD码结果。有关BCD运算结果的调整指令将在第4章“80x86指令系统”中介绍。 2014年11月11日11
2.4 数的定点与浮点表示法 2.4.1 定点表示 所谓定点表示法,是指小数点在数中的位置是固定的。原理上讲,小数点的位置固定在哪一位都是可以的,但通常将数据表示成纯小数或纯整数形式,如图2.1所示。 图2.1 定点数的两种表示方法 (a) 纯小数形式;(b) 纯整数形式 2014年11月11日12
设用一个n+1位字来表示一个数x,其中一位表示符号位(0表示正,1表示负),其他n位为数值位。对于纯小数表示法,所能表示的数x (原码表示,下同)的范围为: -(1-2-n)≤x≤1-2-n (2.4.1) 它能表示的数的最大绝对值为1-2-n,最小绝对值为2-n。 对于纯整数表示法,所能表示的数x的范围为: -(2n-1)≤x≤2n-1 (2.4.2) 它能表示的数的最大绝对值为2n-1,最小绝对值为1。 2014年11月11日13
2.4.2 浮点表示 所谓浮点表示法,就是小数点在数中的位置是浮动的。 任意一个二进制数x总可以写成如下形式: (2.4.3) 其中,d称为尾数,是二进制纯小数,指明数的全部有效数字,前面的符号称为数符,表示数的符号,该位为0,表明该浮点数为正,该位为1,表明该浮点数为负;p称为阶码,它前面的符号称为阶符,阶码为正时,用0表示,阶码为负时,用1表示。 阶符 阶码(p) 数符 尾数(d) 1 位 m位 1位 n位 2014年11月11日14
可以看出,将尾数d的小数点向右(阶码p为正时)或向左(阶码p为负时)移动p位,即可得到该浮点数表示的数值x。阶码p指明小数点的位置,小数点随着阶码的大小和正负而浮动,因此把这种数称为浮点数。可以看出,将尾数d的小数点向右(阶码p为正时)或向左(阶码p为负时)移动p位,即可得到该浮点数表示的数值x。阶码p指明小数点的位置,小数点随着阶码的大小和正负而浮动,因此把这种数称为浮点数。 设阶码的位数为m位,尾数的位数为n位,则该浮点数表示的数值范围为: (2.4.4) 在字长相同的情况下,浮点数能表示的数值范围比定点数大得多,且精度高,但浮点运算规则复杂。 2014年11月11日15
Summary • Most modern computer systems use the binary numbering system to represent values. Since binary values are somewhat unwieldy, we’ll often use the hexadecimal representation in our assembly programs.A single hexadecimal digit consumes four binary digits (bits), and we call a group of four bits a nibble. • The 80x86 works best with groups of bits which are eight, 16, or 32 bits long. We call objects of these sizes bytes, words, and double words, respectively. • In order to talk about specific bits within a nibble, byte, word, double word, or other structure, we’ll number the bits starting at zero (for the least significant bit) on up to n-1(where n is the number of bits in the object) 2014年11月11日16
Summary • There are many operations we can perform on binary values including normal arithmetic (+, -, *, and /) and the logical operations (AND, OR, XOR, NOT, Shift Left, Shift Right, Rotate Left, and Rotate Right). Logical AND, OR, XOR, and NOT are typically defined for single bit operations. We can extend these to n bits by performing bitwise operations. The shifts and rotates are always defined for a fixed length string of bits. • There are two types of integer values which we can represent with binary strings on the 80x86: unsigned integers and signed integers. The 80x86 represents unsigned integers using the standard binary format. It represents signed integers using the two’s complement format. 2014年11月11日17
Summary • Character data is probably the most common data type encountered besides integer values. The IBM PC and compatibles use a variant of the ASCII character set – the extended IBM/ASCII character set. The first 128 of these characters are the standard ASCII characters, 128 are special characters created by IBM for international languages, mathematics, and line drawing. Since the use of the ASCII character set is so common in modern programs, familiarity with this character set is essential. 2014年11月11日18
Questions 1) Convert the following decimal values to binary: a) 128 b) 4096 c) 256 d) 65536 e) 254 f) 888 2) Convert the following binary values to decimal: a) 1001 1001 b) 1001 1101 c) 1100 0011 d) 0000 1001 e)1111 1110 2014年11月11日19
Questions 3) Convert the binary values in problem 2 to hexadecimal. 4) Convert the following hexadecimal values to binary: a) 0ABCD b) 1024 c) 0DEAD d) 0ADD e) 0BEEF Perform the following hex computations (leave the result in hex): 5) 1234 +9876 6) 0FFF - 0F34 7) 100 - 1 8) 0FFE - 1 9) What is the importance of a nibble? 10) How many hexadecimal digits in: a) a byte b) a word c) a double word 2014年11月11日20
Questions 11) How many bits in a: a) nibble b) byte c) word d) double word 12) Which bit (number) is the H.O. bit in a: a) nibble b) byte c) word d) double word 13) What character do we use as a suffix for hexadecimal numbers? Binary numbers? Decimal numbers? 14) Assuming a 16-bit two’s complement format, determine which of the values in question 4 are positive and which are negative. 15) Sign extend all of the values in question two to sixteen bits. Provide your answer in hex. 2014年11月11日21
Questions 16) Perform the bitwise AND operation on the following pairs of hexadecimal values. Present your answer in hex. (Hint: convert hex values to binary, do the operation, then convert back to hex). a) 0FF00, 0FF0 b) 0F00F, 1234 c) 4321, 1234 d) 2341, 3241 e) 0FFFF, 0EDCB 17) Perform the logical OR operation on the above pairs of numbers. 18) Perform the logical XOR operation on the above pairs of numbers. 19) Perform the logical NOT operation on all the values in question four. Assume all values are 16 bits. 2014年11月11日22
Questions 20) Perform the two’s complement operation on all the values in question four. Assume 16 bit values. 21) Sign extend the following hexadecimal values from eight to sixteen bits. Present your answer in hex. a) FF b) 82 c) 12 d) 56 e) 98 f) BF g) 0F h) 78 i) 7F j) F7 22) Sign contract the following values from sixteen bits to eight bits. If you cannot perform the operation, explain why. a) FF00 b) FF12 c) FFF0 d) 12 e) 80 f) FFFF g) FF88 h) FF7F i) 7F j) 2 23) Sign extend the 16-bit values in question 22 to 32 bits. 24) Assuming the values in question 22 are 16-bit values, perform the left shift operation on them. 25) Assuming the values in question 22 are 16-bit values, perform the right shift operation on them. 2014年11月11日23
Questions 26) Assuming the values in question 22 are 16-bit values, perform the rotate left operation on them. 27) Assuming the values in question 22 are 16-bit values, perform the rotate right operation on them. 28) Convert the following dates to the packed format described in this chapter (see “Bit Fields and Packed Data” on page 28). Present your values as a 16-bit hex number. a) 1/1/92 b) 2/4/56 c) 6/19/60 d) 6/16/86 e) 1/1/99 29) Describe how to use the shift and logical operations to extract the day field from the packed date record in question 28. That is, wind up with a 16-bit integer value in the range 0..31. 30) Suppose you have a value in the range 0..9. Explain how you could convert it to an ASCII character using the basic logical operations. 2014年11月11日24
本章作业 1. Convert the following decimal values to binary: a) 128 b) 4096 c) 256 d) 65536 2. Convert the following binary values to decimal: a) 1001 1001 b) 1001 1101 3. Convert the following hexadecimal values to binary: a) 0ABCD b) 1024 4. Perform the following hex computations (leave the result in hex): a) 1234 +9876 b) 0FFF - 0F34 c) 100 – 1 5. How many hexadecimal digits in: a) a byte b) a word c) a double word 2014年11月11日25
6. How many bits in a: a) nibble b) byte c) word d) double word 7. Perform the bitwise AND, OR, and XOR operation on the following pairs of hexadecimal values. Present your answer in hex. (Hint: convert hex values to binary, do the operation, then convert back to hex). a) 0FF00, 0FF0 b) 0F00F, 1234 8. Sign extend the following hexadecimal values from eight to sixteen bits. Present your answer in hex. a) FF b) 82 c) 12 d) 56 e) 98 9. Sign contract the following values from sixteen bits to eight bits. If you cannot perform the operation, explain why. a) FF00 b) FF12 c)F8F6 d) 12 10. Suppose you have a value in the range 0..9. Explain how you could convert it to an ASCII character using the basic logical operations. 2014年11月11日26
中文作业: P32 1.(1) (4) 6.(1) (6) 7. (1) (4) 9 11 12. (1) (4) 2014年11月11日27