010 signed numbers kho tài liệu training

22 54 0
010 signed numbers kho tài liệu training

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Signed numbers Assembly language programming By xorpd xorpd.net Objectives  You will recall the traditional ways to represent negative numbers  You will learn how to invoke “subtraction by addition” in base 10  You will learn about the “two’s complement” method to negate a binary number Motivation  We want to be able to represent negative numbers inside the computer  We want the usual arithmetic to work with the new representation for negative numbers Traditional negative decimals  In base 10, we traditionally use the ‘-’ symbol to represent a negative number  Examples:  −34510  −710  We can invoke arithmetic operations between positive numbers and negative numbers:  −510 + 710 = − = 210  −1110 − −3210 = 32 − 11 = 2110  It seems like everything works right Drawbacks of the traditional “-”  Before adding any two numbers, we have to check their signs, and act accordingly:  𝑎 + −𝑏 = 𝑎 − 𝑏 −𝑎 + 𝑏 = 𝑏 − 𝑎  −𝑎 + −𝑏 = − 𝑎 + 𝑏  𝑎+𝑏 =𝑎+𝑏   The usual subtraction is hard to invoke (We have to handle borrows from far away bits)  We want to only have only the addition operator  To calculate 𝑎 − 𝑏 We calculate instead 𝑎 + (−𝑏)  We have to keep the “−” sign before any signed number That means keeping one extra symbol before every number Subtraction by addition  We want to solve the following: 93210 − 15110 =?  We add and remove 100010 :  93210 − 15110 = 93210 + 100010 − 15110 − 100010  100010 − 15110 = 84910 (“Mechanical” operation)  We replace the subtraction with the result:  93210 − 15110 = 93210 + 84910 − 100010 = 178110 − 100010  178110 − 100010 = 78110 (“Mechanical” operation)  We conclude that 93210 − 15110 = 78110  We only used addition to solve it  All the subtraction operations were mechanical Subtraction by addition (Cont.)  We saw that:  93210 − 15110 = 78110  93210 + 84910 = 178110 Subtraction by addition (Cont.)  We saw that:  93210 − 15110 = 78110  93210 + 84910 = 178110 Subtraction by addition (Cont.)  We saw that:  93210 − 15110 = 78110  93210 + 84910 = 178110  Subtraction by Addition!  What is the relation between 15110 and 84910 ?  100010 − 15110 = 84910  Mechanically: Every digit 𝑑 is replaced by (9 − 𝑑), and finally we add to the result  84910 is virtually the negation of 15110 , in the world of decimal digits  We call this method of turning 15110 into 84910 : The ten’s complement The ten’s complement  Summary of the method:  We want to calculate 63710 − 29110 =?  We confine ourselves to the world of decimal digits  We find the ten’s complement of 29110 by finding − 𝑑 for every digit, and finally adding 1:  29110 → 70810 + 110 = 70910  We add: 63710 + 70910 = 134610 , and consider only the lowest digits  We get that 63710 − 29110 = 34610 Take a break  And come back when you are ready for the Binary version of ten’s complement The two’s complement  We apply the same method to binary numbers  We want to calculate 101102 − 1112 =?  We confine ourselves to the world of bits  We find two’s complement of 1112 by calculating (1 − 𝑏) for every bit 𝑏 (“Flip” every bit), and finally adding 12  1112 = 001112 → 110002 + 12 = 110012  We add 101102 + 110012 = 1011112 and consider only the lowest digits  We get that 101102 − 1112 = 011112 = 11112 Observations  Using the two’s complement imposes a limit on the amount of bits used  We have to fix the amount of bits used before finding the two’s complement of a number  Example: 1012 → 0112 in the world of bits, however 1012 → 1110112 in the world of bits  Adding a number with his complement gives  Example: 1012 + 1110112 = 10000002 Signed binary numbers  Let us look at all the binary numbers with bits  Also called Byte  We call numbers that begin with the bit “1” negative  To change the sign of a number we use the two’s complement method  Example: The number 101100012 is a negative number 101100012 → 010011102 + 12 = 010011112 = 7910 Hence 101100012 represents −7910  We call this representation Signed Binary Numbers of size 8, as opposed to the simple representation that is called Unsigned Binary Numbers of size  We can add signed numbers “in the usual way”, and the sign of the numbers is taken into account automatically! Examples of signed addition  Example: 4610 − 1710 = 2910  In binary:  4610 = 001011102  1710 = 000100012  000100012 → 111011102 + 12 = 111011112 = −1710  4610 − 1710 = 4610 + −1710 = 001011102 + 111011112 = 1000111012  We consider only the lowest bits  000111012 = 2910  The “+” operator works well with two’s complement Examples of signed addition (Cont.)  Example:−1510 − 10110 = −11610  In binary:  1510 = 000011112  −1510 = 111100002 + 12 = 111100012  10110 = 011001012  −10110 = 100110102 + 12 = 100110112  −1510 − 10110 = −1510 + −10110 = 111100012 + 100110112 = 1100011002  We consider only the lower bits  100011002 begins with 1, it is a negative number  100011002 → 011100112 + 12 = 011101002 = 11610 Exceptions  The two’s complement takes positive numbers into negative numbers and vice versa  The highest bit is usually flipped after invoking two’s complement  Two Exceptions:  complements himself   02 → 111111112 + 12 = 100000002 Begins with 0, therefore it is formally positive  “The most negative number” 100000002 complements himself:   100000002 → 011111112 + 12 = 100000002 Also called the “weird number” Graphical view Graphical view Numbers that complement themselves Some Philosophy of representation  You now know about at least two interpretations for every binary number you see  How could you decide which interpretation is the right one?  The bits don’t know what they represent, and they don’t care  100011002 could mean −11610 in two’s complement representation, or 14010 in simple representation  The meaning of a number is obtained from your thoughts about it  And the actions you perform on it, accordingly Some philosophy (Cont.)  Example:  010010112 + 111001112 = 1001100102  This could mean that:  7510 + 23110 = 30610  Because 7510 = 010010112 ; 23110 = 111001112 ; 1001100102 = 30610 in the unsigned binary interpretation  This could also mean that:  7510 − 2510 = 5010  Because 7510 = 010010112 ; 111001112 = −2510 ; 001100102 = 5010 in the signed two’s complement interpretation  Amazingly, both are correct Exercises  Basic ten’s and two’s complement calculations  Use a pen and a paper to solve Use a calculator to check your results  Some more interesting exercises ... 10 0010 − 15110 − 10 0010  10 0010 − 15110 = 84910 (“Mechanical” operation)  We replace the subtraction with the result:  93210 − 15110 = 93210 + 84910 − 10 0010 = 178110 − 10 0010  178110 − 10 0010. .. interpretation  This could also mean that:  7510 − 2510 = 5010  Because 7510 = 0100 10112 ; 111001112 = −2510 ; 0011 00102 = 5010 in the signed two’s complement interpretation  Amazingly, both... is called Unsigned Binary Numbers of size  We can add signed numbers “in the usual way”, and the sign of the numbers is taken into account automatically! Examples of signed addition  Example:

Ngày đăng: 17/11/2019, 08:27

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

  • Đang cập nhật ...

Tài liệu liên quan