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 numbersUnsigned decimalSigned DecimalHexadecimal A B C D E F
2’S COMPLEMENT 0100 Take 1’s complement 1011Add 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 ANDNum1,#80H ANDNum2,#80H XORNum1,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 CY flag set to 1 This means that more than 4 bit is needed to represent sum. Carry 1 bit + 1 XOR 1 = No overflow
ADDITION Signed number addition (1)Numbers with same sign (a)Negative XOR 1 = No overflow CY flag set to 1 Ignore carry since there is no overflow
ADDITION Signed number addition (1)Numbers with different sign CY flag set to 0
SUBTRACTION (COMPARE CMP) Unsigned number subtraction (1)Numbers with different sign Do 2’s Complement with second number and add with first number Carry is 0. Make sign bit positive and thus equal to 8 and not XOR 1 = overflow
SUBTRACTION (COMPARE CMP) Unsigned number subtraction (1)Numbers with same sign (a)Positive Do 2’s Complement with second number and add with first number Ignore carry since there is no overflow 1 XOR 1 = No overflow
SUBTRACTION (COMPARE CMP) Unsigned number subtraction (1)Numbers with same sign (a)Negative Do 2’s Complement with second number and add with first number Ignore CY since there is no overflow 1 XOR 1 = No overflow
SUBTRACTION (COMPARE CMP) Unsigned number subtraction (1)Numbers with different sign Do 2’s Complement with second number and add with first number Overflow. This means that more than 4 bit is needed to represent number. 1 XOR 0 = overflow
AVERAGE OF 2 NUMBERS NOTE: If the average of a positive number gives a fraction, round fraction down Rotate right with carry by 1 bit. RORC (1) Average of a positive number with a whole result
Rotate right with carry by 1 bit. RORC (1) Average of a positive number with a fractional result. AVERAGE OF 2 NUMBERS NOTE: If the average of a positive number gives a fraction, round fraction down. 7
Rotate right with carry by 1 bit. RORC (1) Average of a negative number with a whole result. AVERAGE OF 2 NUMBERS NOTE: If the average of a negative number gives a fraction, round fraction down. 1111
Rotate Right with carry by 1 bit. RORC (1) Average of a negative number with a fractional result. AVERAGE OF 2 NUMBERS NOTE: If the average of a negative number gives a fraction, round fraction down NOT1 CY