150 likes | 235 Views
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
E N D
EECC201 Freshman Seminar Intro to Micro controllerDr. 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 Take 1’s complement 0100 1 0 1 Add 1 to result 1 + 1 1100 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 • Numbers with same sign • Positive • c • 1111 • 1111 15 • 1011 11 • 11010 26 Carry 1 bit 1 XOR 1 = No overflow + CY flag set to 1 This means that more than 4 bit is needed to represent sum.
ADDITION • Signed number addition • Numbers with same sign • Negative • 1111 • 1111 -1 • 1011 -5 • 11010 -6 1 XOR 1 = No overflow + CY flag set to 1 Ignore carry since there is no overflow
ADDITION • Signed number addition • Numbers with different sign • 1000 -8 • 0111 +7 • 01111 -1 + CY flag set to 0
SUBTRACTION (COMPARE CMP) • Unsigned number subtraction • Numbers with different sign • 0011 +3 • 1011 -5 Do 2’s Complement with second number and add with first number - 0 XOR 1 = overflow 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 • Numbers with same sign • 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 • Numbers with same sign • 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 • Numbers with different sign • 1000 -8 • 0111 +7 Do 2’s Complement with second number and add with first number - 1 XOR 0 = overflow 10 1000 1001 10001 -15 + Overflow. This means that 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. RORC 01101 13
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 Rotate right with carry by 1 bit. RORC 0111 7.5 7
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. RORC 01101 -3
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 Rotate Right with carry by 1 bit. RORC 11111 -0.5 -1