Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Arithmetic Instructions and Programs

Similar presentations


Presentation on theme: "Chapter 6 Arithmetic Instructions and Programs"— Presentation transcript:

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

3 Objective 介紹關於加減乘除的指令。
8051 內部運算都是以 byte 為單位,所以當資料量會有機會超過 one byte 時,programmer 自己要小心。 由於 8051 內部運算其實是很簡單的,你可想成都是 unsigned number 的運算。所以當我們想要做 signed number 或 BCD 的運算時,就必需靠 programmer 自己觀察 PSW 的變化與增加許多的檢查。

4 Section 6.1 Unsigned Addition and Subtraction

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.

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.

7 Example 6-1 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 +0BH 100H 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.

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 + R CY=1 Add 1 to R7 Result : R A

9 Example 6-2 Assume that RAM locations 40-44 have the following values.
Write a program to find the sum of the values. At the end of the program, register A should contain the low byte and R7 the high byte. 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 R7 AGAIN:ADD ;add (R0) JNC NEXT ;jump to carry INC R ;keep track of carries NEXT: INC R ;increment pointer DJNZ R2,AGAIN ;repeat until R2 is zero In the first iteration of the loop, 7DH is added to A with CY=0 and R7=00, and the counter R2=04. In the second iteration of the loop, EBH is added to A with CY=1. Since a carry occurred, R7 is incremented. No the counter R2=03H, R7=1, A=68H. C5H is added to A, which makes CY=1, A=2DH, R7=02H, R2=02H. 5BH is added to A, which makes CY=0, A=88H, R7=02H, R2=01H. 30H is added to A, which makes CY=1, A=B8H, R7=02H. DJNZ R2, AGAIN, R2=00H and not jump to AGAIN

10 ADDC ADD with carry To add two 16-bit data operands
ADDC A, source ; A=A+source+CY MOV A,#E7H C E ADD A,#8DH B 8D MOV R6,A MOV A,#3CH CY=1 ADDC A,#3BH ; A=A+operand+CY MOV R7,A ; R7=78H R6=74H high byte low byte To add two 16-bit data operands, we need to be concerned with the propagation of a carry from the lower byte to the higher byte.

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

12 BCD Number System Digit BCD 1 2 3 4 5 6 7 8 9 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 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

13 Unpacked / Packed BCD Numbers
In Unpacked BCD, a byte is used to contain a BCD number. is unpacked BCD for 5 is unpacked BCD for 9 In Packed BCD, a byte has two BCD numbers. 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!

14 Addition of Packed BCD Numbers
Use ADD and ADDC to add two packed BCD numbers: To calculate =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 + 0 6 3 5

15 DA Decimal adjust for addition
DA instruction will add 6 to the lower nibble or higher nibble if needed. DA A MOV A,#47H MOV B,#55H ADD A,B C DA A A 2 ;check CY here CY=1 0 2 1. Lower nibble 2. Upper nibble

16 Example 6-4 Assume 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 ;addition DA A ;adjust for BCD JNC NEXT ;jump no carry INC R ;keep track of carries NEXT: INC R ;increment pointer DUNZ R2,AGAIN ;

17 SUBB Subtraction with borrow SUBB A, source ; A = A – source – CY
CLR C MOV A,#3FH F MOV R3,#23H SUBB A,R C After the subtraction, CY=0: positive result CY=1: negative result

18 Example 6-7 Analyze the following program: CLR C
MOV A,#62H SUBB A,#96H MOV R7,A CC 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

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 ’s complemet of 23H 1C borrow 100H and carry  positive  toggle CY=0 A

20 Subtraction of Unsigned Numbers (2/2)
The steps of the hardware of the CPU in executing the SUBB instruction: A minus the CY value. Take the 2’s complement of the subtrahend. Add it to the minuend (A). Invert the carry. After the subtraction, CY=0: positive result CY=1: negative result 3FH–23H=1CH A=A-CY=3FH 23H  DDH (2’s complement) 3FH+DDH=11CH, where CY=1,A=1CH. Toggle CY=0

21 Example 6-5 Show the steps involved in the following. CLR C MOV A,#3FH
MOV R3,#23H SUBB A,R3 Solution: A = 3F (A=A-CY) R3= (2’s complement) 1C 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.

22 Example 6-6 Analyze 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 ; yourself NEXT:MOV R1,A ;save A in R1 Solution: 4C - 6E ’s complemet of 6EH (A) Borrow 100H and no carry  negative  toggle CY  CY=1, the result is negative and get the 2’s complement of A = =22.

23 Section 6.2 Unsigned Multiplication and Division

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 MOV B,#65H MUL AB E 99 Multiplication of operands larger than 8-bit takes some manipulation. Programmer need to multiple byte by byte and sum them together. B=0EH: high byte; A=99H: low byte

25 Table 6-1: Unsigned Multiplication Summary (MUL AB)
Operand 1 Operand 2 Result byte × byte A B A = low byte B = high byte

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. numerator:分子(除數) denominator:分母(被除數) quotient:商 remainder:餘數

27 Table 6-2: Unsigned Division Summary (DIV AB)
Numerator Denominator Quotient Remainder byte/byte A B

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.

29 Example 6-8 (1/2) 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

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= (2) 19  0A= 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 R R R7

31 Section 6.3 Concepts and Arithmetic Operations

32 Signed Numbers In everyday life, numbers are used that could be positive or negative. Usually, 2’s complement is used for signed numbers.

33 Figure 6-2. 8-Bit Signed Operand
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.

34 The Range of Sign Number for a Single Byte
Decimal Binary Hexadecimal H = 100H-80H H = 100H-7FH : FEH = 100H –2H FFH = 100H –1H H H H FH 2‘s complement

35 Example 6-9 Show how the 8051 would represent -5. Solution:
Observe the following steps: in 8-bit binary invert each bit add 1 Therefore -5 = FBH, the signed number representation in 2’s complement for -5. We can get FBH=100H-05H=FBH, too.

36 Example 6-10 Show how the 8051 would represent -34H. Solution:
Observe the following steps. in 8-bit binary invert each bit 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.

37 Example 6-11 Show how the 8051 would represent –128. Solution:
Observe the following steps. in 8-bit binary 80H invert each bit 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.

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.

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: (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 Simlab_8051 does not know #+96 and #-128.

40 When is an Overflow? In 8-bit signed number operations, OV is set to 1 if either of the following two conditions occurs: There is a carry from D6 to D7 but no carry out of D7. 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.

41 Example 6-13 Observe the following, noting the role of the OV flag.
MOV A,#-128 ;A= (A=80H) MOV R4,#-2 ;R1= (R4=FEH) ADD A,R4 ;A= (A=7EH=126) Solution: (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.

42 Example 6-14 Observe the following, noting the OV flag.
MOV A,#-2 ;A= (A=FEH) MOV R1,#-5 ;R1= (R1=FBH) ADD A,R1 ;A= (A=F9H=-7,correct) Solution: (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).

43 Example 6-15 Examine the following, noting the role of OV.
MOV A,#+7 ;A= (A=07H) MOV R1,#+18 ;R1= (R1=12H) ADD A,R1 ;A= (A=19H=+25,correct) Solution: (CY=0,OV=0) No carry occurs. According to the CPU, the result is +25, which is correct (OV = 0).

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

45 Code 8051 unsigned data multiplication and division instructions
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

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


Download ppt "Chapter 6 Arithmetic Instructions and Programs"

Similar presentations


Ads by Google