* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download EECC201 Freshman Seminar Intro to Micro controller Dr. Ken …
Mathematics of radio engineering wikipedia , lookup
Infinitesimal wikipedia , lookup
Georg Cantor's first set theory article wikipedia , lookup
Law of large numbers wikipedia , lookup
Elementary arithmetic wikipedia , lookup
Location arithmetic wikipedia , lookup
Surreal number wikipedia , lookup
Positional notation wikipedia , lookup
Large numbers wikipedia , lookup
Real number wikipedia , lookup
EECC201 Freshman Seminar Intro to Micro controller Dr. Ken Hsu WEEK 7 ADDITION, SUBTRACTION AND COMPARE OF BINARY NUMBERS Important: Irritant sounds may be heard during browsing. Better turn the volume off. Binary numbers Unsigned decimal Signed Decimal Hexadecimal 0000 0 0 0 0001 1 1 1 0010 2 2 2 0011 3 3 3 0100 4 4 4 0101 5 5 5 0110 6 6 6 0111 7 7 7 1000 8 -8 8 1001 9 -7 9 1010 10 -6 A 1011 11 -5 B 1100 12 -4 C 1101 13 -3 D 1110 14 -2 E 1111 15 -1 F 2’S COMPLEMENT 0100 +10111 1100 Take 1’s complement Add 1 to result Get 2’s complement To distinguish between two numbers with the same sign and two numbers with different signs in assembly language programming AND Num1,#80H AND Num2,#80H XOR Num1,Num2 If result of XOR = 0 then Z flag is set to 1 and sign of both numbers is the same If result of XOR = 1 then Z flag is set to 0 and sign of the numbers are different. NOTE: 80H is called a MASK. ADDITION Unsigned number addition (1) Numbers with same sign (a) Positive c Carry 1 bit 1 XOR 1 = No overflow 1111 1111 15 1011 11 11010 26 + CY flag set to 1 This means that more than 4 bit is needed to represent sum. ADDITION Signed number addition (1) Numbers with same sign (a) Negative 1 XOR 1 = No overflow 1111 1111 -1 1011 -5 11010 -6 + CY flag set to 1 Ignore carry since there is no overflow ADDITION Signed number addition (1) Numbers with different sign 1000 + 0111 -8 01111 -1 +7 CY flag set to 0 SUBTRACTION (COMPARE CMP) Unsigned number subtraction (1) Numbers with different sign - 0 XOR 1 = overflow 0011 +3 1011 -5 Do 2’s Complement with second number and add with first number 01 0011 + 0101 01000 8 Carry is 0. Make sign bit positive and thus equal to 8 and not -8. SUBTRACTION (COMPARE CMP) Unsigned number subtraction (1) Numbers with same sign (a) Positive - 1111 15 1011 11 Do 2’s Complement with second number and add with first number 1 XOR 1 = No overflow 1111 1111 + 0101 10100 4 Ignore carry since there is no overflow SUBTRACTION (COMPARE CMP) Unsigned number subtraction (1) Numbers with same sign (a) Negative - 1111 -1 1011 -5 Do 2’s Complement with second number and add with first number 1 XOR 1 = No overflow 1111 1111 + 0101 10100 4 Ignore CY since there is no overflow SUBTRACTION (COMPARE CMP) Unsigned number subtraction (1) Numbers with different sign - 1 XOR 0 = overflow 1000 -8 0111 +7 Do 2’s Complement with second number and add with first number 10 1000 + 1001 10001 Overflow. This means that -15 more than 4 bit is needed to represent number. AVERAGE OF 2 NUMBERS NOTE: If the average of a positive number gives a fraction, round fraction down. (1) Average of a positive number with a whole result 1111 1111 15 + 1011 11 11010 26 Rotate right with carry by 1 bit. 01101 13 RORC AVERAGE OF 2 NUMBERS NOTE: If the average of a positive number gives a fraction, round fraction down. (1) Average of a positive number with a fractional result. + 1000 8 0111 7 01111 15 0111 7.5 7 Rotate right with carry by 1 bit. RORC AVERAGE OF 2 NUMBERS NOTE: If the average of a negative number gives a fraction, round fraction down. (1) Average of a negative number with a whole result. 1111 1111 -1 + 1011 -5 11010 -6 Rotate right with carry by 1 bit. 01101 -3 RORC AVERAGE OF 2 NUMBERS NOTE: If the average of a negative number gives a fraction, round fraction down. (1) Average of a negative number with a fractional result. 1000 -8 + 0111 +7 01111 -1 NOT1 CY 11111 11111 -0.5 Rotate Right with carry by 1 bit. RORC -1