Chapter 6 Arithmetic Instructions and Programs

27
2002/4/8 2002/4/8 Microcomputers and Microproce Microcomputers and Microproce ssors ssors 1 Suranaree University Suranaree University Of Technology Of Technology มมม มมม 2002 Anant Oonsivilai 2002 Anant Oonsivilai Chapter 6 Chapter 6 Arithmetic Arithmetic Instructions and Instructions and Programs Programs

description

Chapter 6 Arithmetic Instructions and Programs. Outlines. Range of numbers in 8051 unsigned data Addition & subtraction instructions for unsigned data BCD system of data representation Packed and unpacked BCD data Addition & subtraction on BCD data Range of numbers in 8051 signed data - PowerPoint PPT Presentation

Transcript of Chapter 6 Arithmetic Instructions and Programs

Page 1: 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

Page 2: Chapter 6 Arithmetic Instructions and 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

Page 3: Chapter 6 Arithmetic Instructions and Programs

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

Page 4: Chapter 6 Arithmetic Instructions and Programs

442002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

Page 5: Chapter 6 Arithmetic Instructions and Programs

552002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

Addition of Individual BytesAddition of Individual Bytes

Page 6: Chapter 6 Arithmetic Instructions and Programs

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

+

Page 7: Chapter 6 Arithmetic Instructions and Programs

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

Page 8: Chapter 6 Arithmetic Instructions and Programs

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

Page 9: Chapter 6 Arithmetic Instructions and Programs

992002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

ExampleExample

Page 10: Chapter 6 Arithmetic Instructions and Programs

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

Page 11: Chapter 6 Arithmetic Instructions and Programs

11112002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

Example (Positive Result)Example (Positive Result)

Page 12: Chapter 6 Arithmetic Instructions and Programs

12122002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

Example (Negative Result)Example (Negative Result)

Page 13: Chapter 6 Arithmetic Instructions and Programs

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

Page 14: Chapter 6 Arithmetic Instructions and Programs

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

Page 15: Chapter 6 Arithmetic Instructions and Programs

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

Page 16: Chapter 6 Arithmetic Instructions and Programs

16162002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

Example ( 1 of 2 )Example ( 1 of 2 )

Page 17: Chapter 6 Arithmetic Instructions and Programs

17172002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

Example ( 2 of 2 )Example ( 2 of 2 )

Page 18: Chapter 6 Arithmetic Instructions and Programs

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

Page 19: Chapter 6 Arithmetic Instructions and Programs

19192002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

ExampleExample

Page 20: Chapter 6 Arithmetic Instructions and Programs

20202002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

ExampleExample

Page 21: Chapter 6 Arithmetic Instructions and Programs

21212002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

ExampleExample

Page 22: Chapter 6 Arithmetic Instructions and Programs

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

Page 23: Chapter 6 Arithmetic Instructions and Programs

23232002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

Overflow in Signed Number Overflow in Signed Number OperationsOperations

Page 24: Chapter 6 Arithmetic Instructions and Programs

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

Page 25: Chapter 6 Arithmetic Instructions and Programs

25252002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

ExampleExample

Page 26: Chapter 6 Arithmetic Instructions and Programs

26262002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

ExampleExample

Page 27: Chapter 6 Arithmetic Instructions and Programs

27272002/4/82002/4/8 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai

ExampleExample