46
1 Chapter 6 Arithmetic Instructions and Programs

1 Chapter 6 Arithmetic Instructions and Programs

  • View
    246

  • Download
    5

Embed Size (px)

Citation preview

Page 1: 1 Chapter 6 Arithmetic Instructions and Programs

1

Chapter 6 Arithmetic Instructions and Programs

Page 2: 1 Chapter 6 Arithmetic Instructions and Programs

2

Sections

6.1 Unsigned Addition and Subtraction

6.2 Unsigned Multiplication and Division

6.3 Signed Number Concepts and Arithmetic Operations

Page 3: 1 Chapter 6 Arithmetic Instructions and Programs

3

Objective

• 介紹關於加減乘除的指令。• 8051 內部運算都是以 byte 為單位,所以當資料量會有機會超過 one byte 時, programmer 自己要小心。

• 由於 8051 內部運算其實是很簡單的,你可想成都是 unsigned number 的運算。所以當我們想要做 signed number 或 BCD 的運算時,就必需靠 programmer 自己觀察 PSW 的變化與增加許多的檢查。

Page 4: 1 Chapter 6 Arithmetic Instructions and Programs

4

Section 6.1Unsigned Addition and Subtraction

Page 5: 1 Chapter 6 Arithmetic Instructions and Programs

5

Unsigned Numbers

• Unsigned numbers are defined as data in which all the bits are used to represent data, and no bits are set aside for the positive or negative sign.

• For a 8-bit data, the operand can be between 00 and FFH.

Page 6: 1 Chapter 6 Arithmetic Instructions and Programs

6

Addition of Unsigned Numbers

• Add the source operand to register A and put the result in A.

ADD A, source A + source A

MOV A,#25H ;load 25H into A

ADD A,#34H ;add 34H to A,now A=59H – The destination operation is always in A.

– The instruction could change CY,AC,OV and P.

Page 7: 1 Chapter 6 Arithmetic Instructions and Programs

7

Show how the flag register is affected by the following instructions.

MOV A,#0F5H ;A=F5 hex

ADD A,#0BH ;A=F5+0B=00

Solution:

F5H 1111 0101

+0BH +0000 1011

100H 1 0000 0000

After the addition, register A = 00H and the flags are as follows:

CY = 1 since there is a carry out from D7.

P = 1 because the number of 1s is 0 (an even number), P is set to 1.

AC = 1 since there is a carry from D3 to D4.

Example 6-1

Page 8: 1 Chapter 6 Arithmetic Instructions and Programs

8

Addition of Individual Bytes

• To calculate the sum of any number of operands, the carry flag should be checked after the addition of each operation. A 1111 0101

+ R0 0000 1011

1 0000 0000

CY=1

Add 1 to R7

0000 0001 0000 0000

R7 A

Result :

Page 9: 1 Chapter 6 Arithmetic Instructions and Programs

9

Example 6-2Assume that RAM locations 40-44 have the following values.Write a program to find the sum of the values. At the end of theprogram, register A should contain the low byte and R7 the highbyte. All values are in hex.

40=(7D) 41=(EB) 42=(C5) 43=(5B) 44=(30)Solution: MOV R0,#40H ;load pointer MOV R2,#5 ;load counter CLR A ;A=0 MOV R7,A ;clear R7AGAIN:ADD A,@R0 ;add (R0) JNC NEXT ;jump to carry INC R7 ;keep track of carriesNEXT: INC R0 ;increment pointer DJNZ R2,AGAIN ;repeat until R2 is zero

Page 10: 1 Chapter 6 Arithmetic Instructions and Programs

10

ADDC

• ADD with carry• To add two 16-bit data operands

ADDC A, source ; A=A+source+CY

MOV A,#E7H 3C E7 1559110

ADD A,#8DH + 3B 8D 1524510

MOV R6,A 78 74 3083610

MOV A,#3CH CY=1

ADDC A,#3BH ; A=A+operand+CY

MOV R7,A ; R7=78H R6=74H

high byte low byte

Page 11: 1 Chapter 6 Arithmetic Instructions and Programs

11

Example 6-3

Write a program to add two 16-bit numbers. The numbers are

3CE7H and 3B8DH. Place that sum in R7 and R6; R6 should have

the lower byte.

Solution:

CLR C ;make CY=0

MOV A,#0E7H ;load the low byte now A=E7H

ADD A,#8DH ;add the low byte,A=74H and CY=1

MOV R6,A ;save the low byte in R6

MOV A,#3CH ;load the high byte

ADDC A,#3BH ;add with the carry

MOV R7,A ;save the high byte of the sum

Page 12: 1 Chapter 6 Arithmetic Instructions and Programs

12

BCD Number System

• Binary Coded Decimal : use binary to represent 0-9 only.

• See Table 6.1 BCD Code• Two terms for BCD

number:– Unpacked BCD

– Packed BCD

Digit BCD

0123456789

0000000100100011010001010110011110001001

Page 13: 1 Chapter 6 Arithmetic Instructions and Programs

13

Unpacked / Packed BCD Numbers

• In Unpacked BCD, a byte is used to contain a BCD number. – 0000 0101 is unpacked BCD for 5

– 0000 1001 is unpacked BCD for 9

• In Packed BCD, a byte has two BCD numbers. – 0101 1001 is packed BCD for 59.

– It is twice as efficient in storing data.

• When we get two BCD numbers, we want to add them directly in form of packed BCD.

• However, ADD and ADDC are just for binary!

Page 14: 1 Chapter 6 Arithmetic Instructions and Programs

14

Addition of Packed BCD Numbers

• Use ADD and ADDC to add two packed BCD numbers:

• To calculate 1710+1810=3510.

MOV A,#17H

ADD A,#18H

• The programmer must add 6 to the low digital.

• Then we get the correct packed BCD sum (35H).

1 7

+ 1 8

2 F

2 F

+ 0 6

3 5

Page 15: 1 Chapter 6 Arithmetic Instructions and Programs

15

DA

• Decimal adjust for addition• DA instruction will add 6 to the lower nibble or higher

nibble if needed.

DA AMOV A,#47H 4 7

MOV B,#55H + 5 5

ADD A,B 9 C

DA A A 2

;check CY here CY=1 0 2

1. Lower nibble

2. Upper nibble

Page 16: 1 Chapter 6 Arithmetic Instructions and Programs

16

Example 6-4Assume that 5 packed BCD data items are stored in RAM locations

starting at 40H, as shown below. Write a program to find the sum of all the numbers. The result must be in BCD.

40=(71) 41=(11) 42=(65) 43=(59) 44=(37)

Solution: MOV R0,#40H ;load pointer

MOV R2,#5 ;load counter

CLR A ;A=0

MOV R7,A ;clear R7

AGAIN: ADD A,@R0 ;addition

DA A ;adjust for BCD

JNC NEXT ;jump no carry

INC R7 ;keep track of carries

NEXT: INC R0 ;increment pointer

DUNZ R2,AGAIN ;

Page 17: 1 Chapter 6 Arithmetic Instructions and Programs

17

SUBB

• Subtraction with borrow

SUBB A, source ; A = A – source – CY

CLR C

MOV A,#3FH 3 F

MOV R3,#23H - 2 3

SUBB A,R3 1 C

• After the subtraction,– CY=0: positive result

– CY=1: negative result

Page 18: 1 Chapter 6 Arithmetic Instructions and Programs

18

Example 6-7Analyze the following program:

CLR C

MOV A,#62H 27 62 1008210

SUBB A,#96H - 12 96 373410

MOV R7,A 14 CC 634810

MOV A,#27H

SUBB A,#12H

MOV R6,A

Solution:

After the SUBB, A = 62H-96H = CCH and CY=1indicating there is a borrow.

Since CY = 1, when SUBB is executed the second time A = 27H-12H-1 =14H. Therefore, we have 2762H-1296H =14CCH.

low byte

high byte

Page 19: 1 Chapter 6 Arithmetic Instructions and Programs

19

Subtraction of Unsigned Numbers (1/2)

• The 8051 use adder circuitry to perform the subtraction command.

• In subtraction, the 8051 use the 2’s complement method.A-B = A+(100H-B)-100 = A+(2’s complement of B)-

100 = A + (2’s complement of B) + (toggle CY)

3F 0011 1111

- 23 + 1101 1101 2’s complemet of 23H

1C 1 0001 1100 borrow 100H and carry

positive toggle CY=0 A

Page 20: 1 Chapter 6 Arithmetic Instructions and Programs

20

Subtraction of Unsigned Numbers (2/2)

• The steps of the hardware of the CPU in executing the SUBB instruction:

1. A minus the CY value.

2. Take the 2’s complement of the subtrahend.

3. Add it to the minuend (A).

4. Invert the carry.

• After the subtraction,– CY=0: positive result– CY=1: negative result

• 3FH–23H=1CH

1. A=A-CY=3FH

2. 23H DDH (2’s complement)

3. 3FH+DDH=11CH, where CY=1,A=1CH.

4. Toggle CY=0

Page 21: 1 Chapter 6 Arithmetic Instructions and Programs

21

Example 6-5Show the steps involved in the following.

CLR C

MOV A,#3FH

MOV R3,#23H

SUBB A,R3

Solution:

A = 3F 0011 1111 0011 1111 (A=A-CY)

R3= 23 0010 0011 + 1101 1101 (2’s complement)

1C 1 0001 1100

CY=0 (Toggle CY)

The flags would be set as follows: CY = 0, AC = 0

The programmer must look at the carry flag to determine if the result is positive or negative.

Page 22: 1 Chapter 6 Arithmetic Instructions and Programs

22

Example 6-6Analyze the following program: CLR C MOV A,#4C SUBB A,#6EH JNC NEXT ;jump not carry (CY=0) CPL A ;get 2’s complement by INC A ; yourselfNEXT:MOV R1,A ;save A in R1

Solution:

4C 0100 1100 - 6E + 1001 0010 2’s complemet of 6EH -22 1101 1110(A)Borrow 100H and no carry negative toggle CY CY=1, the result is negative and get the 2’s complement of A =0010 0010=22.

Page 23: 1 Chapter 6 Arithmetic Instructions and Programs

23

Section 6.2Unsigned Multiplication and Division

Page 24: 1 Chapter 6 Arithmetic Instructions and Programs

24

MUL AB

• The 8051 supports byte by byte multiplication only.

• Multiple A * B, result: B=high byte, A=low byte.

MUL AB

MOV A,#25H 25

MOV B,#65H + 65

MUL AB 0E 99 B=0EH: high byte; A=99H: low byte

Page 25: 1 Chapter 6 Arithmetic Instructions and Programs

25

Table 6-1: Unsigned Multiplication Summary (MUL AB)

Multiplication Operand 1 Operand 2 Result

byte × byte A B A = low byte B = high byte

Page 26: 1 Chapter 6 Arithmetic Instructions and Programs

26

DIV AB

• The 8051 supports byte by byte division only.• Divide A by B, result: B=remainder, A=quotient.

DIV AB

MOV A,#95H

MOV B,#10H

DIV AB ;A=09H (quotient)

;B=05H (remainder)– IF the numerator=B=0, then OV=1 indicates an error and CY=0.

Page 27: 1 Chapter 6 Arithmetic Instructions and Programs

27

Table 6-2: Unsigned Division Summary (DIV AB)

Division Numerator Denominator Quotient Remainder

byte/byte A B A B

Page 28: 1 Chapter 6 Arithmetic Instructions and Programs

28

An Application for DIV

• There are times when an analog-to-digital converter is connected to a port.

• The ADC represents some quantity such as temperature or pressure.

• The 8-bit ADC provides data in hex.• This hex data must be converted to decimal for

display. We divide it by 10 repeatedly until the quotient is less than 10.

• See Example 6-8.

Page 29: 1 Chapter 6 Arithmetic Instructions and Programs

29

Example 6-8 (1/2)

(a) Write a program to get hex data in the range of 00 – FFH from port 1 and convert it to decimal. Save the digits in R7, R6 and R5, where the least significant digit is in R7.

Solution of (a):

MOV A,#0FFH MOV P1,A ;make P1 an input port MOV A,P1 ;read data from P1 MOV B,#10 ;divide by 10 DIV AB ;P1 has max value 255 MOV R7,B ;3 bytes to save 3 decimals MOV B,#10 DIV AB MOV R6,B MOV R5,A

R5 R6 R7

Page 30: 1 Chapter 6 Arithmetic Instructions and Programs

30

Example 6-8 (2/2)(b) Analyze the program, assuming that P1 has a value of FDH for

data.

Solution of (b):

In the case of an 8-bit binary = FDH = 253 in decimal.

Q(A) R(B)

(1) FD 0A= 19 3 (2) 19 0A= 2 5

Therefore, we have FDH = 253. In order to display this data it must

be converted to ASCII, which is described in the next chapter.

2 5 3

R5 R6 R7

Page 31: 1 Chapter 6 Arithmetic Instructions and Programs

31

Section 6.3Concepts and Arithmetic Operations

Page 32: 1 Chapter 6 Arithmetic Instructions and Programs

32

Signed Numbers

• In everyday life, numbers are used that could be positive or negative.

• Usually, 2’s complement is used for signed numbers.

Page 33: 1 Chapter 6 Arithmetic Instructions and Programs

33

Figure 6-2. 8-Bit Signed Operand

D7 D6 D5 D4 D3 D2 D1 D0

sign magnitude

The most significant bit (MSB) is used for the sign.

The rest is used for the magnitude which is represented in its 2’s complement.

Page 34: 1 Chapter 6 Arithmetic Instructions and Programs

34

The Range of Sign Number for a Single Byte

Decimal Binary Hexadecimal-128 1000 0000 80H = 100H-80H

-127 1000 0001 81H = 100H-7FH

:

-2 1111 1110 FEH = 100H –2H

-1 1111 1111 FFH = 100H –1H

0 0000 0000 00H

+1 0000 0001 01H

+2 0000 0010 02H

:

+127 1111 1111 7FH

2‘s complement

Page 35: 1 Chapter 6 Arithmetic Instructions and Programs

35

Example 6-9

Show how the 8051 would represent -5.

Solution:

Observe the following steps:

1. 0000 0101 5 in 8-bit binary

2. 1111 1010 invert each bit

3. 1111 1011 add 1

Therefore -5 = FBH, the signed number representation in 2’s

complement for -5.

We can get FBH=100H-05H=FBH, too.

Page 36: 1 Chapter 6 Arithmetic Instructions and Programs

36

Example 6-10

Show how the 8051 would represent -34H.

Solution:

Observe the following steps.

1. 0011 0100 128 in 8-bit binary

2. 0111 1111 invert each bit

3. 1000 0000 add 1 (which becomes 80 in hex)

Therefore -34 = CCH, the signed number representation in 2’s

complement for -34H.

We can get FBH=100H-34H=CCH, too.

Page 37: 1 Chapter 6 Arithmetic Instructions and Programs

37

Example 6-11

Show how the 8051 would represent –128.

Solution:

Observe the following steps.

1. 1000 0000 128 in 8-bit binary 80H

2. 0111 1111 invert each bit

3. 1000 0000 add 1 (which becomes 80 in hex)

Therefore -128 = 80H, the signed number representation in 2’s

complement for -128.

We can get FBH=100H-80H=FBH, too.

Page 38: 1 Chapter 6 Arithmetic Instructions and Programs

38

Overflow Problem

• The CPU understands only 0s and 1s and ignores the human convention of positive and negative numbers.

• The overflow flag (OV) is designed to indicate an overflow of the operations for the signed numbers.– The accept range –128 to 127 in decimal.

• When is an overflow?– If the result of an operation on signed numbers is too large

for the 8-bit register, an overflow has occurred and the programmer must be notified.

Page 39: 1 Chapter 6 Arithmetic Instructions and Programs

39

Example 6-12

Examine the following code and analyze the result.

MOV A,#+96 ;A=60H

MOV R1,#+70 ;R1=46H

ADD A,R1 ;A=A6H=-90, INVALID!!

Solution:

96 0110 0000

+ 70 0100 0110

+ 166 1010 0110 (CY=0, OV=1)

The signed value in the register A=A6H=-90 is wrong. Programmer must check it by themselves.

Note: There is a carry from D6 to D7 but no carry out of D7

Page 40: 1 Chapter 6 Arithmetic Instructions and Programs

40

When is an Overflow?

• In 8-bit signed number operations, OV is set to 1 if either of the following two conditions occurs:

1. There is a carry from D6 to D7 but no carry out of D7.

2. There is a carry from D7 out but no carry from D6 to D7.

• In both above cases, the overflow flag is set to 1.

Page 41: 1 Chapter 6 Arithmetic Instructions and Programs

41

Example 6-13

Observe the following, noting the role of the OV flag.

MOV A,#-128 ;A=1000 0000(A=80H)

MOV R4,#-2 ;R1=1111 1110(R4=FEH)

ADD A,R4 ;A=0111 1110(A=7EH=126)

Solution:

-128 1000 0000

+ -2 1111 1110

- 130 0111 1110 (CY=1,OV=1)

There is a carry from D7 out but no carry from D6 to D7.

According to the CPU, there is an overflow (OV = 1).

The sign number is wrong.

Page 42: 1 Chapter 6 Arithmetic Instructions and Programs

42

Example 6-14

Observe the following, noting the OV flag.

MOV A,#-2 ;A=1111 1110 (A=FEH)

MOV R1,#-5 ;R1=1111 1011(R1=FBH)

ADD A,R1 ;A=1111 1001 (A=F9H=-7,correct)

Solution:

-2 1111 1110

+ -5 1111 1011

- 7 1111 1001 (CY=1,OV=0)

There are carries from D7 out and from D6 to D7.

According to the CPU, the result is -7, which is correct (OV = 0).

Page 43: 1 Chapter 6 Arithmetic Instructions and Programs

43

Example 6-15

Examine the following, noting the role of OV.

MOV A,#+7 ;A=0000 0111 (A=07H)

MOV R1,#+18 ;R1=0001 0010(R1=12H)

ADD A,R1 ;A=0001 1001 (A=19H=+25,correct)

Solution:

7 0000 0111

+ 18 0001 0010

25 0001 1001 (CY=0,OV=0)

No carry occurs.

According to the CPU, the result is +25, which is correct (OV = 0).

Page 44: 1 Chapter 6 Arithmetic Instructions and Programs

44

You are able to ( 1/2 )

• Define the range of numbers possible in 8051 unsigned data

• Code addition and subtraction instructions for unsigned data

• Explain the BCD ( binary coded decimal ) system of data representation

• Contrast and compare packed and unpacked BCD data

• Perform addition and subtraction on BCD data

Page 45: 1 Chapter 6 Arithmetic Instructions and Programs

45

You are able to ( 2/2 )

• Code 8051 unsigned data multiplication and division instructions

• Define the range of numbers possible in 8051 signed data

• Code 8051 signed data arithmetic instructions• Explain carry and overflow problems and their

corrections

Page 46: 1 Chapter 6 Arithmetic Instructions and Programs

46

Homework

• Chapter 6 Problems : 1,3,4,11,13,15• Note:

– In the problem, please find the CY, AC and OV flags for each the following. You can do it by the simulation tools.

– Please write and compile the program of Problems 3,4,11,13,15