Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Representation Binary Numbers Binary Addition

Similar presentations


Presentation on theme: "Data Representation Binary Numbers Binary Addition"— Presentation transcript:

1 Data Representation Binary Numbers Binary Addition
Translating between binary and decimal Binary Addition Integer Storage Sizes Hexadecimal Integers Translating between decimal and hexadecimal Hexadecimal subtraction Signed Integers Binary subtraction Character Storage Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

2 Binary Numbers Digits are 1 and 0 MSB – most significant bit
1 = true 0 = false MSB – most significant bit LSB – least significant bit Bit numbering: Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

3 Binary Numbers Every binary number is a sum of powers of 2
Each digit (bit) is either 1 or 0 Each bit represents a power of 2: Every binary number is a sum of powers of 2 Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

4 Translating Binary to Decimal
Weighted positional notation shows how to calculate the decimal value of each binary bit: dec = (Dn-1  2n-1) + (Dn-2  2n-2) (D1  21) + (D0  20) D = binary digit binary = decimal 9: (1  23) + (1  20) = 9 Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

5 Translating Unsigned Decimal to Binary
Repeatedly divide the decimal integer by 2. Each remainder is a binary digit in the translated value: (see Decimal-to-binary.pdf for further explanation!) 37 = Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, (Modified – John Carelli)

6 Binary Addition Starting with the LSB, add each pair of digits, include the carry if present. Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

7 Integer Storage Sizes Standard sizes:
What is the largest unsigned integer that may be stored in 20 bits? Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

8 Hexadecimal Integers Binary values are represented in hexadecimal.
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

9 Translating Binary to Hexadecimal
Each hexadecimal digit corresponds to 4 binary bits. Example: Translate the binary integer to hexadecimal: Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

10 Converting Hexadecimal to Decimal
Multiply each digit by its corresponding power of 16: dec = (D3  163) + (D2  162) + (D1  161) + (D0  160) Hex 1234 equals (1  163) + (2  162) + (3  161) + (4  160), or decimal 4,660. Hex 3BA4 equals (3  163) + (11 * 162) + (10  161) + (4  160), or decimal 15,268. Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

11 Powers of 16 Used when calculating hexadecimal values up to 8 digits long: Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

12 Converting Decimal to Hexadecimal
decimal 422 = 1A6 hexadecimal Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

13 Hexadecimal Addition 36 28 28 6A 42 45 58 4B 78 6D 80 B5
Divide the sum of two digits by the number base (16). The quotient becomes the carry value, and the remainder is the sum digit. 1 1 A B 78 6D 80 B5 21 / 16 = 1, rem 5 Important skill: Programmers frequently add and subtract the addresses of variables and instructions. Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

14 Hexadecimal Subtraction
When a borrow is required from the digit to the left, add 16 (decimal) to the current digit's value: = 21 -1 C6 75 A2 47 24 2E Practice: The address of var1 is The address of the next variable after var1 is A. How many bytes are used by var1? Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

15 What about negative numbers?
Signed Binary Most intuitive approach Use the MSB to represent the sign MSB -> most significant bit 0 is positive, 1 is negative “complement” notation One’s-complement Two’s-complement First look at Signed Binary

16 Signed Integers Binary: The highest bit indicates the sign. 1 = negative, 0 = positive F 6 A Hexadecimal: If the highest digit of an integer is > 7 (i.e. highest bit is 1), the value is negative. Above examples: F6 is negative, 0A is positive Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, (modified John Carelli)

17 Signed Binary Addition and Subtraction get complicated 2+3 2-3 2+ (-3)
Need to identify the sign of each operand to decide whether to add or subtract 2+3 2-3 2+ (-3) (-2) + 3 (-2) – (-3)

18 Two’s Complement Notation
Negative representation of number is selected such that, when it is added to the positive number, the result is zero. To construct the two’s complement : First, create a one’s complement representation by inverting each bit Then add one to the resulting one’s complement value

19 Forming the Two's Complement
Negative numbers are stored in two's complement notation Represents the additive Inverse Note that = Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

20 Why two’s complement? Subtraction happens automatically when positive and negative numbers are added It eliminates the need for a subtraction circuit Example: 3 – 6 is the same as 3 + (-6) 3: + (-6): (-3): 1101 3: 0011 6: 0110 -3: 1101 -6: 1010

21 Binary Subtraction When subtracting A – B, convert B to its two's complement, then add… Add A to (–B) Practice: Subtract 0101 from 1001. Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

22 Two’s Complement Examples
3 Bits Unsigned Two’s Complement 011 3  010 2  001 1  000 0  111 7  −1  110 6  −2  101 5  −3  100 4  −4 8 Bits Unsigned Two’s Complement 127  126  2  1  0000 0000 0  255  −1  254  −2  130  −126  Source: Wikipedia

23 Ranges of Signed Integers
The highest bit is reserved for the sign. This limits the range: Practice: What is the largest positive value that may be stored in 20 bits? Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

24 What about fractions? No new operations, just like integer arithmetic
Line up the decimal points Decimal example: 10-1 10-2 10-3

25 More on fractions Fixed-point arithmetic + 11111110.110 (-1.25)
Use a “binary point” instead of a “decimal” point Be sure to line them up! Addition/subtraction, two’s complement work as with integers (40.625) (-1.25) (39.375) 2-1 = 0.5 2-2 = 0.25 2-3 = 0.125

26 F x 2E Floating Point Use “scientific notation” with a base of 2:
F is the fraction E is the exponent Stored in binary “word” with a standardized format Define by IEEE (Standard ) Single precision (32 bits) 1 sign bit 8 exponent bits 23 fraction bits

27 IEEE Floating-Point Binary Reals
Types Single Precision 32 bits: 1 bit for the sign, 8 bits for the exponent, and 23 bits for the fractional part of the significand. Approximately 7 decimal digits of precision Range of approximately 10+/-38 Double Precision 64 bits: 1 bit for the sign, 11 bits for the exponent, and 52 bits for the fractional part of the significand. Approximately 15 decimal digits of precision Range of approximately 10+/-308 Double Extended Precision 80 bits: 1 bit for the sign, 16 bits for the exponent, and 63 bits for the fractional part of the significand. Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015. modified John Carelli

28 Character Storage Character sets Null-terminated String
Standard ASCII (0 – 127) Extended ASCII (0 – 255) ANSI (0 – 255) Unicode (0 – 65,535) Null-terminated String Array of characters followed by a null byte Using the ASCII table back inside cover of book Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.

29 Ascii Table

30 Numeric Data Representation
pure binary can be calculated directly ASCII binary string of digits: " " ASCII decimal string of digits: "65" ASCII hexadecimal string of digits: "9C" next: Boolean Operations Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.


Download ppt "Data Representation Binary Numbers Binary Addition"

Similar presentations


Ads by Google