1 / 109

Chapter 6 Arithmetic, Logic Instructions, and Programs

Chapter 6 Arithmetic, Logic Instructions, and Programs. Objective (1/2). 首先介紹關於加減乘除的指令 。 8051 內部運算都是以 byte 為單位,所以當資料量會有機會超過 one byte 時, programmer 自己要小心。 由於 8051 內部運算其實是很簡單的,跟本就是單純的 binary 的運算。所以當我們想要做 signed number 或 BCD 的運算時,就必需靠 programmer 自己觀察 PSW 的變化與增加許多的檢查。

kalkin
Download Presentation

Chapter 6 Arithmetic, Logic Instructions, and Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Arithmetic, Logic Instructions, and Programs

  2. Objective (1/2) • 首先介紹關於加減乘除的指令。 • 8051 內部運算都是以 byte 為單位,所以當資料量會有機會超過 one byte 時,programmer 自己要小心。 • 由於 8051 內部運算其實是很簡單的,跟本就是單純的binary 的運算。所以當我們想要做 signed number 或 BCD 的運算時,就必需靠 programmer 自己觀察 PSW 的變化與增加許多的檢查。 • 介紹關於邏輯運算的指令。如ANL、ORL、XRL、CPL等指令。另外有執行 byte 旋轉的指令。如RR、RL、SWAP,簡單可是很有用。

  3. Objective (2/2) • CY的operations指令是用於 bit manipulation,我們關心的只是 byte 中的某幾個 bits 而已。 • 最後有一個範例是利用這些指令做 BCD 與 ASCII 之間的轉換。 • 我們將只是很簡單的介紹這些指令,如果你想得到更多關於這些指令的用法與範例,如更多的 addressing mode 的用法,請看 Appendix A.1。

  4. Sections 6.1 Arithmetic instructions 6.2 Signed number concepts and arithmetic operations 6.3 Logic and compare instructions 6.4 Rotate instructions and data serialization 6.5 BCD, ASCII, and other application programs

  5. Section 6.1Arithmetic Instructions

  6. 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.

  7. Unsigned Addition and Subtraction

  8. 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 operand is always in A. • The instruction could change CY,AC,OV and P.

  9. 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 1111 0101 +0BH+0000 1011 100H 0000 0000 After the addition, register A (destination) contains 00 and the flags are as follows: CY = 1 since there is a carry out from D7. P = 0 because the number of 1s is 0 (an even number), P is set to 0. AC = 1 since there is a carry from D3 to D4.

  10. 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 0111 1101 + R0 1110 1011 1 0110 1000 CY=1 Add 1 to R7 Result : 0000 0001 0110 1000 R7 A

  11. 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 A,@R0 ;add (R0) JNC NEXT ;jump to carry INC R7 ;keep track of carries NEXT:INC R0 ;increment pointer DJNZ R2,AGAIN ;repeat until R2 is zero

  12. ADDC • ADD with carry ADDC A, source • To add two 16-bit data operands, Add the source operand to register A and put the result in A. MOV A,#E7H 3C E7 ADD A,#8DH+ 3B 8D MOV R6,A 78 74 MOV A,#3CH CY=1 ADDC A,#3BH; A=A+operand+CY MOV R7,A ; R7=78H R6=74H

  13. 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

  14. SUBB • Subtraction with borrow SUBB A, source ; A = A – source – CY CLR C MOV A,#3FH3 F MOV R3,#23H- 2 3 SUBB A,R3 1 C • If CY=0, it is A=A-source.

  15. 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)-100H = A+(2’s complement of B)-100H = A + (2’s complement of B) + (toggle CY) • There are two cases for the SUBB: • 1 byte subtraction (CLR C is used to clear borrow CY) • Used for multi-byte numbers and will take care of the borrow (CY) of the lower operand. (Example 6-7)

  16. 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. • 3FH–23H=1CH & CY=0 • A=A-CY=3FH • 23H  DDH (2’s complement) • 3FH+DDH=11CH, where CY=1,A=1CH. • Toggle & get CY=0 • After the subtraction, • CY=0: A holds the positive result. • CY=1: a negative result and A is its 2’s complement format.

  17. Example 6-5 Show the steps involved in the following. CLR C ;make CY=0 MOV A,#3FH ;load 3FH into A (A=3FH) MOV R3,#23H ;load 23H into R3 (R3=23H) SUBB A,R3 ;A = 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 (after 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.

  18. Example 6-6 Analyze the following program: CLR C MOV A,#4CH ;load A =4CH SUBB A,#6EH ;subtract 6E from A 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 0100 1100 0100 1100 - 6E 0110 1110 + 1001 0010 -22 0 1101 1110(A) CY=1, the result is negative. We borrow 100H and get positive result DEH. Get the 2’s complement of A =0010 0010=22.

  19. Example 6-7 Analyze the following program: CLR C MOV A,#62H 27 62 0110 0010 62H SUBB A,#96H - 12 96+ 0110 1010 MOV R7,A 14 CC 0 1100 1100 MOV A,#27H SUBB A,#12H 0010 0110 MOV R6,A + 1110 1110 Solution: 1 0001 0100 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. 96H’s 2’s CY=1 after toggle 27H-CY 12H’s 2’s CY=0 after toggle

  20. Unsigned Multiplication and Division

  21. 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: upper byte; A=99H: lower byte

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

  23. 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.

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

  25. 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.

  26. Example 6-8 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 ;B=0A hex (10 dec) DIV AB ;divide by 10 MOV R7,B ; MOV B,#10 ;P1 has max value 255 DIV AB ;3 bytes to save 3 decimals MOV R6,B ;Twice DIV are used MOV R5,A ;

  27. Example 6-9 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) FD / 0A= 19 3 (low digit R7=3) 19 / 0A= 2 5 (middle digit R6=5) (high digit R5=2) Therefore, we have FDH = 253. In order to display this data it must be converted to ASCII, which is described in the next chapter.

  28. Binary Coded Decimal

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

  30. 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 unpacked BCD for 59. • It is twice as efficient in storing data.

  31. Addition of BCD Numbers • ADD and ADDC are just for hexadecimal! • To calculate 17+18=35. MOV A,#17H ADD A,#28H • The programmer must add 6 to the low digital such that a carry occurs. 1 7 + 1 8 2 F 2 F + 0 6 3 5 If lower nibble>9 or AC=1, add 6 to lower nibble. If higher nibble>9 or CY=1, add 6 to higher nibble.

  32. DA • Decimal adjust for addition • DA instruction will add 6 to the lower nibble or higher nibble if needed. DA A MOV A,#47H 4 7 MOV B,#55H+ 5 5 ADD A,B 9 C DA A A 2 CY=1 0 2 1. Lower nibble 2. Upper nibble

  33. Section 6.2Signed Number Concepts and Arithmetic Operations

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

  35. magnitude sign Figure 6-2. 8-Bit Signed Operand For 2’s complement representation 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.

  36. 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 0111 1111 7FH 2‘s complement

  37. Example 6-10 Show how the 8051 would represent -5. Solution: Observe the following steps: • 0000 0101 5 in 8-bit binary • 1111 1010 invert each bit • 1111 1011 add 1 (which becomes FB in hex) Therefore -5 = FBH, the signed number representation in 2’s complement for -5. We can get FBH=100H-05H=FBH, too.

  38. Example 6-11 Show how the 8051 would represent -34H. Solution: Observe the following steps. • 0011 0100 128 in 8-bit binary • 1100 1011 invert each bit • 1100 1100 add 1 (which becomes CC in hex) Therefore -34 = CCH, the signed number representation in 2’s complement for -34H. We can get FBH=100H-34H=CCH, too.

  39. Example 6-12 Show how the 8051 would represent –128. Solution: Observe the following steps. • 1000 0000 128 in 8-bit binary 80H • 0111 1111 invert each bit • 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.

  40. 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. • 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. • The range –128 to 127 in decimal.

  41. Example 6-13 Examine the following code and analyze the result. MOV A,#+96 ;A=0110 0000(A=60H) MOV R1,#+70 ;R1=0100 0110(R1=46H) ADD A,R1 ;A=1010 0110 ;A=A6H=-90, INVALID!! Solution: 96 0110 0000 + 700100 0110 + 166 1010 0110 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

  42. 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.

  43. Example 6-14 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 + -21111 1110 - 130 0111 1110 (CY=1,AC=0,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.

  44. Example 6-15 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 + -51111 1011 - 7 1111 1001 (CY=1,AC=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).

  45. XRL XRL destination-byte,source-byte MOV A,#35H ;0011 0101 XRL A,#0FH ;0000 1111 => A=0011 1010 • No effect on any of the flags. • XRL is often used (1) to clear a register, (2) to see if two registers have the same value or (3) to toggle bits of an operands. unchanged toggled XOR 2 bits X and Y

  46. ANL ANL destination-byte,source-byte MOV A,#35H ;0011 0101 ANL A,#0FH ;0000 1111 => A=0000 0101 • No effect on any of the flags. • ANL is often used to mask (set to 0) certain bits of an operands. AND 2 bits X and Y

  47. Example 6-17 Show the results of the following. MOV A,#35H ;A = 35H ANL A,#0FH ;A = A AND 0FH (now A = 05) Solution: 35H 0 0 1 1 0 1 0 1 0FH 0 0 0 0 1 1 1 1 05H 0 0 0 0 0 1 0 1 35H AND 0FH = 05H

  48. ORL ORL destination-byte,source-byte MOV A,#35H ;0011 0101 ORL A,#0FH ;0000 1111 => A=0011 1111 • No effect on any of the flags. • ORL is often used to set certain bits of an operands to 1. OR 2 bits X and Y

  49. Example 6-19 Show the results of the following. MOV A,#54H XRL A,#78H Solution: 54H 0 1 0 1 0 1 0 0 78H 0 1 1 1 1 0 0 0 2CH 0 0 1 0 1 1 0 0 54H XOR 78H = 2CH

  50. Example 6-20 The XRL instruction can be used to clear the contents of a register by XORing it with itself. Show how “XRL A,A” clears A, assuming that A = 45H. Solution: 45H 01000101 45H01000101 00 00000000 XOR a number with itself = 0

More Related