8051-CH6

Shared by: xiaopangnv
Categories
Tags
-
Stats
views:
19
posted:
10/11/2012
language:
English
pages:
46
Document Sample
scope of work template
							             Chapter 6
Arithmetic Instructions and Programs




                                       1
                   Sections

6.1 Unsigned Addition and Subtraction
6.2 Unsigned Multiplication and Division
6.3 Signed Number Concepts and Arithmetic
  Operations




                                            2
             Objective

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




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




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

                                                                        7
         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
        Result :   0000 0001     0000 0000
                         R7         A                   8
                        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                    9
                  ADDC

• ADD with carry
• To add two 16-bit data operands
  ADDC A, source ; A=A+source+CY
                      high byte low byte
  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            10
                       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
                                                               11
             BCD Number System

• Binary Coded Decimal :       Digit   BCD
  use binary to represent 0-     0     0000
  9 only.                        1     0001
• See Table 6.1 BCD Code         2     0010
                                 3     0011
• Two terms for BCD              4     0100
  number:                        5     0101
   – Unpacked BCD                6     0110
   – Packed BCD                  7     0111
                                 8     1000
                                 9     1001   12
     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!     13
     Addition of Packed BCD Numbers

• Use ADD and ADDC to add          1   7
  two packed BCD numbers:        + 1   8
• To calculate 1710+1810=3510.     2   F
   MOV   A,#17H
   ADD   A,#18H                    2   F
                                 + 0   6
• The programmer must add 6
                                   3   5
  to the low digital.
• Then we get the correct
  packed BCD sum (35H).
                                           14
                        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
                                         1. Lower nibble
   DA    A                         A   2
                                          2. Upper nibble
   ;check CY here            CY=1 0    2
                                                            15
                        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    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     ;                                     16
                        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               17
                       Example 6-7
Analyze the following program:
      CLR C
low   MOV A,#62H                   27 62     1008210
byte SUBB A,#96H                 - 12 96      373410
      MOV R7,A                     14 CC      634810
      MOV A,#27H
high
byte 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.           18
  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
                                                        19
                  A
    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.        • 3FH–23H=1CH
    2. Take the 2’s complement      1. A=A-CY=3FH
       of the subtrahend.           2. 23H  DDH
    3. Add it to the minuend (A).      (2’s complement)
    4. Invert the carry.            3. 3FH+DDH=11CH,
•   After the subtraction,             where CY=1,A=1CH.
                                    4. Toggle CY=0
    – CY=0: positive result
                                                           20
    – CY=1: negative result
                        Example 6-5
Show 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.                                21
                   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        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.                    22
Section 6.2
Unsigned Multiplication and Division




                                       23
                    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
                                                       24
       Table 6-1: Unsigned Multiplication
             Summary (MUL AB)

Multiplication Operand 1 Operand 2 Result
                                   A = low byte
byte × byte   A         B
                                   B = high byte




                                                   25
                    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.                   26
     Table 6-2: Unsigned Division Summary
                   (DIV AB)


Division    Numerator Denominator Quotient   Remainder
byte/byte   A         B           A          B




                                                         27
            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.
                                                     28
                     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                      R5          R6       R7
      MOV R6,B
      MOV R5,A                                                        29
                     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                  30
Section 6.3
Concepts and Arithmetic Operations




                                     31
                Signed Numbers

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




                                                     32
        Figure 6-2. 8-Bit Signed Operand


  D7      D6     D5     D4    D3     D2     D1     D0


 sign                   magnitude



The most significant         The rest is used for the
bit (MSB) is used for        magnitude which is represented
the sign.                    in its 2’s complement.
                                                              33
  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       2‘s
  +1       0000 0001   01H       complement
  +2       0000 0010   02H
               :                              34
  +127     1111 1111   7FH
                      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.
                                                              35
                      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.
                                                               36
                       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.
                                                                37
                 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.
                                                              38
                       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
                                                              39
              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.



                                                            40
                        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.                                  41
                        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).
                                                                     42
                        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).
                                                                      43
           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
                                                   44
            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


                                                  45
                     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




                                                              46

						
Related docs
Other docs by xiaopangnv
Yearlings in Legacy - McQuay Stables
Views: 259  |  Downloads: 0
Weekly Updates - Edublogs
Views: 172  |  Downloads: 0
What Counts as 5 a Day - Webs
Views: 153  |  Downloads: 0
What causes it
Views: 164  |  Downloads: 0
UNIFORM - Guthrie Street Primary School
Views: 153  |  Downloads: 0
Time Field Visitor vs. Home
Views: 237  |  Downloads: 0