Chapter 6 Arithmetic Instructions and Programs
description
Transcript of Chapter 6 Arithmetic Instructions and Programs
2002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors 11
Suranaree UniversitySuranaree University
Of TechnologyOf Technologyมทสมทส
2002 Anant Oonsivilai2002 Anant Oonsivilai
Chapter 6Chapter 6Arithmetic Instructions Arithmetic Instructions
and Programsand Programs
222002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
OutlinesOutlines
Range of numbers in 8051 unsigned dataRange of numbers in 8051 unsigned dataAddition & subtraction instructions for unsigned Addition & subtraction instructions for unsigned datadataBCD system of data representationBCD system of data representationPacked and unpacked BCD dataPacked and unpacked BCD dataAddition & subtraction on BCD dataAddition & subtraction on BCD dataRange of numbers in 8051 signed dataRange of numbers in 8051 signed dataSigned data arithmetic instructionsSigned data arithmetic instructionsCarry & overflow problems & correctionsCarry & overflow problems & corrections
332002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Addition of Unsigned NumbersAddition of Unsigned Numbers
ADDADD A, sourceA, source ; A = A + ; A = A + sourcesource
442002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
552002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Addition of Individual BytesAddition of Individual Bytes
662002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
ADDC & Addition of 16-bit NumbersADDC & Addition of 16-bit Numbers
13C E73B 8D78 74
+
772002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
BCD Number SystemBCD Number System
Unpacked BCD: 1 byteUnpacked BCD: 1 byte
Packed BCD: 4 bitsPacked BCD: 4 bits
882002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Adding BCD Numbers & DA InstructionAdding BCD Numbers & DA Instruction
MOVMOV A,#17HA,#17H
ADDADD A,#28HA,#28H
MOVMOV A,#47HA,#47H ;A=47H first BCD operand;A=47H first BCD operandMOVMOV B,#25HB,#25H ;B=25 second BCD operand;B=25 second BCD operand
ADDADD A,BA,B ;hex (binary) addition (A=6CH);hex (binary) addition (A=6CH)
DADA AA ;adjust for BCD addition(A=72H);adjust for BCD addition(A=72H)
HEXHEX BCDBCD
2929 0010 10010010 1001
++ 1818 +0001 1000 +0001 1000
4141 0100 00010100 0001 AC=1AC=1
++ 6 6 + 0110 + 0110
4747 0100 01110100 0111
992002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
ExampleExample
10102002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Subtraction of Unsigned NumbersSubtraction of Unsigned Numbers
SUBBSUBB A, sourceA, source ; A = A – source – CY; A = A – source – CY
SUBB when CY = 0SUBB when CY = 0 Take 2’s complement of subtracted (source)Take 2’s complement of subtracted (source) Add it to minuendAdd it to minuend Invert carryInvert carry
11112002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Example (Positive Result)Example (Positive Result)
12122002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Example (Negative Result)Example (Negative Result)
13132002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
SUBB When CY = 1SUBB When CY = 1For multibyte numbersFor multibyte numbers
14142002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Multiplication of Unsigned NumbersMultiplication of Unsigned Numbers
MULMUL ABAB ; A ; A B, place 16-bit result in B and A B, place 16-bit result in B and A
MOVMOV A,#25HA,#25H ;load 25H to reg. A;load 25H to reg. A
MOVMOV B,#65HB,#65H ;load 65H in reg. B;load 65H in reg. B
MULMUL ABAB ;25H * 65H = E99 where;25H * 65H = E99 where
;B = 0EH and A = 99H;B = 0EH and A = 99H
Table 6-1:Unsigned Multiplication Summary (MUL AB)Table 6-1:Unsigned Multiplication Summary (MUL AB)
MultiplicationMultiplication Operand 1Operand 1 Operand 2Operand 2 ResultResult
byte byte bytebyte AA BB A=low byte,A=low byte,
B=high byteB=high byte
15152002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Division of Unsigned NumbersDivision of Unsigned Numbers
DIVDIV ABAB ; divide A by B; divide A by B
MOVMOV A,#95HA,#95H ;load 95 into A;load 95 into A
MOVMOV B,#10HB,#10H ;load 10 into B;load 10 into B
DIVDIV ABAB ;now A = 09 (quotient) and ;now A = 09 (quotient) and
;B = 05 (remainder);B = 05 (remainder)
Table 6-2:Unsigned Division Summary (DIV AB)Table 6-2:Unsigned Division Summary (DIV AB)
DivisionDivision NumeratorNumerator DenominatorDenominator QuotientQuotient RemainderRemainder
byte / bytebyte / byte AA BB AA BB
16162002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Example ( 1 of 2 )Example ( 1 of 2 )
17172002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Example ( 2 of 2 )Example ( 2 of 2 )
18182002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Signed 8-bit OperandsSigned 8-bit Operands
Covert to 2’s complementCovert to 2’s complement Write magnitude of number in 8-bit binary (no Write magnitude of number in 8-bit binary (no
sign)sign) Invert each bitInvert each bit Add 1 to itAdd 1 to it
19192002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
ExampleExample
20202002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
ExampleExample
21212002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
ExampleExample
22222002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Byte-sized Signed Numbers Byte-sized Signed Numbers RangesRanges
DecimalDecimal BinaryBinary HexHex
-128-128 1000 00001000 0000 8080
-127-127 1000 00011000 0001 8181
-126-126 1000 00101000 0010 8282
…….. …………………… ....
-2-2 1111 11101111 1110 FEFE
-1-1 1111 11111111 1111 FFFF
00 0000 00000000 0000 0000
+1+1 0000 00010000 0001 0101
+2+2 0000 00100000 0010 0202
…… …………………… ......
+127+127 0111 11110111 1111 7F7F
23232002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Overflow in Signed Number Overflow in Signed Number OperationsOperations
24242002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
When Is the OV Flag Set?When Is the OV Flag Set?
Either: there is a carry from D6 to D7 but Either: there is a carry from D6 to D7 but no carry out of D7 (CY = 0)no carry out of D7 (CY = 0)
Or: there is a carry from D7 out (CY = 1) Or: there is a carry from D7 out (CY = 1) but no carry from D6 to D7but no carry from D6 to D7
25252002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
ExampleExample
26262002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
ExampleExample
27272002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
ExampleExample