SlideShare a Scribd company logo
1 of 27
Subject Code    :151001
Name Of Subject :Microcontroller & Interfacing
Name of Unit     :Arithmetic& logical instructions
Topic            :Arithmetic & logical operations
Name of Faculty  : Mr. Haresh Suthar
                   Miss. Madhuri Thakkar
Name of Students : (i) Savalia Avani(100870111020)
                   (ii) Patel Jay   (100870111021)
Arithmetic Instruction :
  There are 24 arithmetic opcodes which are grouped
  into the following types:   ADD and ADDC
                                 SUBB
                                 MUL
                                 DIV
                                 INC
                                 DEC
                                 DA

Sub: MC          Topic: Arithmetic & Logical operations
Arithmetic Flags
          Flag: It is a 1-bit register that indicates the
          status of the result from an operation

          Flags are either at a flag-state of value 0 or 1

          Arithmetic flags indicate the status of the
          results from mathematical operations ( +, −,
          *, / )



Sub: MC               Topic: Arithmetic & Logical operations
Arithmetic Flags (Conditional Flags)
     There are 4 arithmetic flags in the 8051
             Carry (C)
             Auxiliary Carry (AC)
             Overflow (OV)
             Parity (P)

     All the above flags are stored in the Program
     Status Word (PSW)

          CY     AC      --      RS1      RS0       0V         --   P
          PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0
Sub: MC               Topic: Arithmetic & Logical operations
Arithmetic Flags (Conditional Flags)
  
   CY           PSW.7     Carry flag
   AC          PSW.6     Auxiliary carry flag
   --          PSW.5     Available to the user for general purpose
   RS1         PSW.4     Register Bank selector bit 1
   RS0         PSW.3     Register Bank selector bit 0
   0V          PSW.2     Overflow flag
   --          PSW.1     User definable flag
   P           PSW.0     Parity flag

          The C flag is keeping track in unsigned operations
          The OV flag is keeping track in signed operations

Sub: MC                Topic: Arithmetic & Logical operations
Instructions that Affecting Flags                       (1/2)


      Instruction Mnemonic                   Flags Affected
      ADD                               C           AC        OV
      ADDC                              C           AC        OV
      SUBB                              C           AC        OV
      MUL                               C=0                   OV
      DIV                               C=0                   OV
      DA A                              C
      SETB C                            C=1
      MOV C, bit                        C
Sub: MC            Topic: Arithmetic & Logical operations
Instructions that Affecting Flags                       (2/2)


    Instruction Mnemonic                  Flags Affected
    ORL C, bit                       C
    ANL C, bit                       C
    RLC                              C
    RRC                              C
    CLR C                            C=0
    CPL C                            C = /C
    CJNE                             C

Sub: MC          Topic: Arithmetic & Logical operations
The ADD and ADDC Instructions

      ADD         A, source ; A = A + source
      ADDC        A, source ; A = A + source + C

      A register must be involved in additions
      The C flag is set to 1 if there is a carry out of bit 7
      The AC flag is set to 1 if there is a carry out of bit 3
      ADD is used for ordinary addition
      ADDC is used to add a carry after the LSB addition in a
      multi-byte process

Sub: MC               Topic: Arithmetic & Logical operations
Example -1
  Show how the flag register is affected by the following instructions.
       MOV A, #0F5h             ; A = F5h
       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 zero
                 AC = 1 since there is a carry from D3 to D4

Sub: MC               Topic: Arithmetic & Logical operations
Example -2
    Assume that RAM locations 40h – 42h have the following
    values. Write a program to find the sum of the values in these
    locations. At the end of the program, register A should contain
    the low byte and R7 contain the high byte.
    RAM locations: 40h = (7Dh), 41h = (EBh), 42h = (C5h)
    Solution:
                MOV   A, 40h       ; set A = RAM location 40h
                MOV   R7, #0       ; set R7 = 0
                ADD   A, 41h       ; add A with RAM location 41h
                JNC   NEXT         ; if CY = 0 don’t accumulate carry
                INC   R7           ; keep track of carry
    NEXT:       ADD   A, 42h       ; add A with RAM location 42h
                JNC   NEXT1        ; if CY = 0 don’t accumulate carry
                INC   R7           ; keep track of carry
    NEXT1:
                END
Sub: MC               Topic: Arithmetic & Logical operations
Example -3
   Write a program segment to add two 16-bit numbers. The numbers are
   3CE7h and 3B8Dh. Place the sum in R7 and R6; R6 should store the lower
   byte.



    CLR  C             ; make C=0
    MOV  A, #0E7h      ; load the low byte now A=E7h
    ADD  A, #8Dh       ; add the low byte now A=74h and C=1
    MOV  R6, A; save the low byte of the sum in R6
    MOV  A, #3Ch       ; load the high byte
    ADDC A, #3Bh       ; add with the carry
                       ; 3B + 3C + 1 = 78 (all in hex)
    MOV  R7, A; save the high byte of the sum
Sub: MC                  Topic: Arithmetic & Logical operations
ADDC …..              ADD …..
The DA Instruction                           DA   A                DA   A

  DA A
   The action is to “decimal adjust” the register A
   Used after the addition of two BCD numbers


      Example 4 :
          MOV   A, #47h          ; A=47h first BCD operand
          MOV   B, #25h          ; B=25h second BCD operand
          ADD   A, B             ; hex (binary) addition (A=6Ch)
          DA    A                ; adjust for BCD addition (A=72h)




Sub: MC                   Topic: Arithmetic & Logical operations
Example 4 of DA Instruction

           Hex                        BCD
            47                     0100 0111
          + 25                   + 0010 0101
            6C                     0110 1100
          + 6                   +       0110
            72                     0111 0010

           Offset decimal 6 !

Sub: MC           Topic: Arithmetic & Logical operations
SUBB    A, #data
 The SUBB Instruction                       SUBB    A, direct
                                            SUBB    A, @Ri , where i =0 or 1
     SUBB       A, source                   SUBB    A, Rn, where n =0,1,,7


     No borrow: A = A – source
     With borrow:
     A = A – source – carry (i.e. borrow)
      Note that the 8051 uses the 2’s complement method to do
       subtraction
      After execution:
      The C flag is set to 1 if a borrow is needed into bit 7
      The AC flag is set to 1 if a borrow is needed into bit 3


Sub: MC             Topic: Arithmetic & Logical operations
The MUL Instruction
     MUL        AB

     Uses registers A and B as both source and destination
     registers
     Numbers in A and B are multiplied, then put the
     lower-order byte of the product in A and the high-
     order byte in B
     The OV flag is set to 1 if the product > FFh
     Note that the C flag is 0 at all times

Sub: MC              Topic: Arithmetic & Logical operations
The DIV Instruction
     DIV AB

     Similarly, it uses registers A and B as both source and
     destination registers
     The number in A is divided by B. The quotient is put
     in A and the remainder (if any) is put in B
     The OV flag is set to 1 if B has the number 00h
     (divide-by-zero error)
     Note that the C flag is 0 at all times

Sub: MC             Topic: Arithmetic & Logical operations
The INC and DEC Instructions
   To increment (INC) or decrement (DEC) the internal
   memory location specified by the operand

   No change with all the arithmetic flags in this
   operation

   e.g. INC 7Fh      ; content in 7Fh increased by 1
          DEC R1     ; content in R1 decreased by 1
                      INC   A
                      INC   direct
                      INC   @Ri where i=0,or 1
                      INC   Rn where n=0,,7

Sub: MC            Topic: Arithmetic & Logical operations
Logic Operation in 8051

          Logical operations

          Rotate and swap operations

          Comparison operations




Sub: MC           Topic: Arithmetic & Logical operations
Logical Instructions
    The source operand can be any of the 4 addressing modes (i.e.
    immediate/register/ direct/indirect)

    ANL can be used to clear (0) certain bits

    ORL can be used to set (1) certain bits
     Examples

      Instruction      ANL A,R0             ORL A,R0           XRL A,R0

      A before:        10010111              10010111          10010111
      R0 before:       11110010              11110010          11110010
      A afterwards:    10010010              11110111          01100101

Sub: MC               Topic: Arithmetic & Logical operations
The CLR and CPL Instructions

      CLR          A
      All bits in register A are cleared
      CPLA
      All bits in register A are complemented (inverted)
      Note that CLR and CPL instructions operate on
      register A only




Sub: MC             Topic: Arithmetic & Logical operations
RL A
  The Rotate Instructions                                    RR A

    Contents in register A is rotated one bit position to the
    left or to the right (operated in A only)

    The bit shifted out is used as the new bit shifted in

    May include the C flag in the operation

    Useful in inspecting the bits in a byte one by one

    Also useful for multiplication and division in powers of
    2

Sub: MC             Topic: Arithmetic & Logical operations
The Rotate Instructions
    RL A
    Rotates A one bit position to the left
    RLC        A
    Rotates A and the carry flag one bit position to the left
    RR A
    Rotates A one bit position to the right
    RRC        A
    Rotates A and the carry flag one bit position to the right
    Note that for RLC and RRC, you have to know the C flag
    first

Sub: MC            Topic: Arithmetic & Logical operations
The Rotate Instructions
                       7   6     5     4   3   2    1   0
                                                                Before: 10011100
                                                                 After: 00111001
                                       RL A


              C        7   6     5     4   3   2    1   0       Before: 10011100 CY = 0
                                                                 After: 00111000 CY = 1

          Carry Flag                   RLC A


                       7   6     5     4   3   2   1    0       Before: 10011100
                                                                 After: 01001110

                                     RR A


             7    6    5   4     3     2   1   0        C       Before: 10011100 CY = 1
                                                                 After: 11001110 CY = 0

                               RRC A               Carry Flag
Sub: MC                         Topic: Arithmetic & Logical operations
The SWAP Instruction
     Swapping the lower-nibble (lower 4 bits) and the
     higher-nibble (upper 4 bits) of register A.

                   7     6    5      4    3     2    1     0

                       High Nibble            Low Nibble


                                   SWAP A


          Register A = 5Eh (original value) after SWAP Register A =
          E5h
Sub: MC                   Topic: Arithmetic & Logical operations
Comparison Operation

   CJNE destination, source, relative address

     Compare the source and destination operands first

     Jump to the relative address (subroutine) if they are
     not equal


            Carry flag = 1, if destination-byte is less than the source-byte,
            Otherwise, the carry flag is cleared.


Sub: MC                Topic: Arithmetic & Logical operations
Example -5
   Write a program segment to monitor P1 continuously
   for the value of 63h. It should get out of the
   monitoring only if P1 = 63h.

    Solution :
           MOV P1, #0FFh                ; make P1 an input port
    HERE: MOV A, P1                     ; get P1
           CJNE A, #63h, HERE           ; keep monitoring
    unless                              ; P1=63h


Sub: MC           Topic: Arithmetic & Logical operations
Arithmetic & Logical Operations in 8051 Microcontroller

More Related Content

What's hot

8051 Microcontroller ppt
8051 Microcontroller ppt8051 Microcontroller ppt
8051 Microcontroller pptRahul Kumar
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerGaurav Verma
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil KawareProf. Swapnil V. Kaware
 
8085 Architecture & Memory Interfacing1
8085 Architecture & Memory Interfacing18085 Architecture & Memory Interfacing1
8085 Architecture & Memory Interfacing1techbed
 
DAC Interfacing with 8051.pdf
DAC Interfacing with 8051.pdfDAC Interfacing with 8051.pdf
DAC Interfacing with 8051.pdfSrikrishna Thota
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086Mahalakshmiv11
 
Branching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessorBranching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessorRabin BK
 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085ShivamSood22
 
Accessing I/O Devices
Accessing I/O DevicesAccessing I/O Devices
Accessing I/O DevicesSlideshare
 
Memory Segmentation of 8086
Memory Segmentation of 8086Memory Segmentation of 8086
Memory Segmentation of 8086Nikhil Kumar
 
Combinational circuits
Combinational circuits Combinational circuits
Combinational circuits DrSonali Vyas
 
Microcontroller-8051.ppt
Microcontroller-8051.pptMicrocontroller-8051.ppt
Microcontroller-8051.pptDr.YNM
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unitchidabdu
 
floating point multiplier
floating point multiplierfloating point multiplier
floating point multiplierBipin Likhar
 

What's hot (20)

8051 Microcontroller ppt
8051 Microcontroller ppt8051 Microcontroller ppt
8051 Microcontroller ppt
 
8051 memory
8051 memory8051 memory
8051 memory
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontroller
 
Interfacing LCD with 8051 Microcontroller
Interfacing LCD with 8051 MicrocontrollerInterfacing LCD with 8051 Microcontroller
Interfacing LCD with 8051 Microcontroller
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware
 
Interrupts in 8051
Interrupts in 8051Interrupts in 8051
Interrupts in 8051
 
8085 Architecture & Memory Interfacing1
8085 Architecture & Memory Interfacing18085 Architecture & Memory Interfacing1
8085 Architecture & Memory Interfacing1
 
DAC Interfacing with 8051.pdf
DAC Interfacing with 8051.pdfDAC Interfacing with 8051.pdf
DAC Interfacing with 8051.pdf
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
 
program status word
program status wordprogram status word
program status word
 
Branching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessorBranching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessor
 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085
 
Accessing I/O Devices
Accessing I/O DevicesAccessing I/O Devices
Accessing I/O Devices
 
Memory Segmentation of 8086
Memory Segmentation of 8086Memory Segmentation of 8086
Memory Segmentation of 8086
 
Combinational circuits
Combinational circuits Combinational circuits
Combinational circuits
 
Microcontroller-8051.ppt
Microcontroller-8051.pptMicrocontroller-8051.ppt
Microcontroller-8051.ppt
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unit
 
8085 instruction set
8085 instruction set8085 instruction set
8085 instruction set
 
floating point multiplier
floating point multiplierfloating point multiplier
floating point multiplier
 
Adc and dac
Adc and dacAdc and dac
Adc and dac
 

Similar to Arithmetic & Logical Operations in 8051 Microcontroller

INTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONSINTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONSSwapnil Mishra
 
Chapter 3 instruction set-of-8085
Chapter 3 instruction set-of-8085Chapter 3 instruction set-of-8085
Chapter 3 instruction set-of-8085Shubham Singh
 
12 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa1412 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa14John Todora
 
Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.pptsteffydean
 
microp-8085 74 instructions for mct-A :P
microp-8085 74 instructions for mct-A :Pmicrop-8085 74 instructions for mct-A :P
microp-8085 74 instructions for mct-A :PJathin Kanumuri
 
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction setSaumitra Rukmangad
 
microp-8085 74 instructions for mct-A :P-2
microp-8085 74 instructions for mct-A :P-2microp-8085 74 instructions for mct-A :P-2
microp-8085 74 instructions for mct-A :P-2Jathin Kanumuri
 
Instruction set class
Instruction set   classInstruction set   class
Instruction set classshiji v r
 
Instruction set of 8085
Instruction set  of 8085Instruction set  of 8085
Instruction set of 8085shiji v r
 
MICROPROCESSOR INSTRUCTION SET OF 8085
MICROPROCESSOR INSTRUCTION SET OF 8085MICROPROCESSOR INSTRUCTION SET OF 8085
MICROPROCESSOR INSTRUCTION SET OF 8085Sumadeep Juvvalapalem
 
8085 instruction set.pptx
8085 instruction set.pptx8085 instruction set.pptx
8085 instruction set.pptxAchintKaur27
 
Microprocessor Part 3
Microprocessor    Part  3Microprocessor    Part  3
Microprocessor Part 3Sajan Agrawal
 
Unit ii microcontrollers final
Unit ii microcontrollers finalUnit ii microcontrollers final
Unit ii microcontrollers finalSARITHA REDDY
 

Similar to Arithmetic & Logical Operations in 8051 Microcontroller (20)

6 arithmetic logic inst and prog
6 arithmetic logic inst and prog6 arithmetic logic inst and prog
6 arithmetic logic inst and prog
 
Class4
Class4Class4
Class4
 
INTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONSINTEL 8085 DATA FORMAT AND INSTRUCTIONS
INTEL 8085 DATA FORMAT AND INSTRUCTIONS
 
Chapter 3 instruction set-of-8085
Chapter 3 instruction set-of-8085Chapter 3 instruction set-of-8085
Chapter 3 instruction set-of-8085
 
Hemanth143
Hemanth143 Hemanth143
Hemanth143
 
12 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa1412 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa14
 
Addressing modes
Addressing modesAddressing modes
Addressing modes
 
Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.ppt
 
microp-8085 74 instructions for mct-A :P
microp-8085 74 instructions for mct-A :Pmicrop-8085 74 instructions for mct-A :P
microp-8085 74 instructions for mct-A :P
 
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
 
microp-8085 74 instructions for mct-A :P-2
microp-8085 74 instructions for mct-A :P-2microp-8085 74 instructions for mct-A :P-2
microp-8085 74 instructions for mct-A :P-2
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Instruction set class
Instruction set   classInstruction set   class
Instruction set class
 
Instruction set of 8085
Instruction set  of 8085Instruction set  of 8085
Instruction set of 8085
 
MICROPROCESSOR INSTRUCTION SET OF 8085
MICROPROCESSOR INSTRUCTION SET OF 8085MICROPROCESSOR INSTRUCTION SET OF 8085
MICROPROCESSOR INSTRUCTION SET OF 8085
 
Microprocessor 11el01
Microprocessor 11el01Microprocessor 11el01
Microprocessor 11el01
 
8085 instruction set.pptx
8085 instruction set.pptx8085 instruction set.pptx
8085 instruction set.pptx
 
Microprocessor Part 3
Microprocessor    Part  3Microprocessor    Part  3
Microprocessor Part 3
 
Unit ii microcontrollers final
Unit ii microcontrollers finalUnit ii microcontrollers final
Unit ii microcontrollers final
 
Uc 2(vii)
Uc 2(vii)Uc 2(vii)
Uc 2(vii)
 

More from Jay Patel

Design step for making slot antenna in HFSS
Design step for making slot antenna in HFSSDesign step for making slot antenna in HFSS
Design step for making slot antenna in HFSSJay Patel
 
Introduction to web20
Introduction to web20Introduction to web20
Introduction to web20Jay Patel
 
Mean square error
Mean square errorMean square error
Mean square errorJay Patel
 
Internet server components
Internet server componentsInternet server components
Internet server componentsJay Patel
 
Fractal Antenna
Fractal AntennaFractal Antenna
Fractal AntennaJay Patel
 
TLS in manet
TLS in manetTLS in manet
TLS in manetJay Patel
 
Continuous Random variable
Continuous Random variableContinuous Random variable
Continuous Random variableJay Patel
 
Global positioning system
Global positioning systemGlobal positioning system
Global positioning systemJay Patel
 
Net Nutrality
Net NutralityNet Nutrality
Net NutralityJay Patel
 
Energy density in electrostatic field
Energy density in electrostatic field Energy density in electrostatic field
Energy density in electrostatic field Jay Patel
 
different marketing concept
different marketing conceptdifferent marketing concept
different marketing conceptJay Patel
 
slew rate in opamp
slew rate in opampslew rate in opamp
slew rate in opampJay Patel
 
synathesized function generator
synathesized function generatorsynathesized function generator
synathesized function generatorJay Patel
 
Tracking in receivers
Tracking in receiversTracking in receivers
Tracking in receiversJay Patel
 
Parson’s Turbine and condition for maximum efficiency of Parson’s reaction Tu...
Parson’s Turbine and condition for maximum efficiency of Parson’s reaction Tu...Parson’s Turbine and condition for maximum efficiency of Parson’s reaction Tu...
Parson’s Turbine and condition for maximum efficiency of Parson’s reaction Tu...Jay Patel
 

More from Jay Patel (17)

Css Basics
Css BasicsCss Basics
Css Basics
 
Java script
Java scriptJava script
Java script
 
Design step for making slot antenna in HFSS
Design step for making slot antenna in HFSSDesign step for making slot antenna in HFSS
Design step for making slot antenna in HFSS
 
Introduction to web20
Introduction to web20Introduction to web20
Introduction to web20
 
Mean square error
Mean square errorMean square error
Mean square error
 
Internet server components
Internet server componentsInternet server components
Internet server components
 
Fractal Antenna
Fractal AntennaFractal Antenna
Fractal Antenna
 
TLS in manet
TLS in manetTLS in manet
TLS in manet
 
Continuous Random variable
Continuous Random variableContinuous Random variable
Continuous Random variable
 
Global positioning system
Global positioning systemGlobal positioning system
Global positioning system
 
Net Nutrality
Net NutralityNet Nutrality
Net Nutrality
 
Energy density in electrostatic field
Energy density in electrostatic field Energy density in electrostatic field
Energy density in electrostatic field
 
different marketing concept
different marketing conceptdifferent marketing concept
different marketing concept
 
slew rate in opamp
slew rate in opampslew rate in opamp
slew rate in opamp
 
synathesized function generator
synathesized function generatorsynathesized function generator
synathesized function generator
 
Tracking in receivers
Tracking in receiversTracking in receivers
Tracking in receivers
 
Parson’s Turbine and condition for maximum efficiency of Parson’s reaction Tu...
Parson’s Turbine and condition for maximum efficiency of Parson’s reaction Tu...Parson’s Turbine and condition for maximum efficiency of Parson’s reaction Tu...
Parson’s Turbine and condition for maximum efficiency of Parson’s reaction Tu...
 

Arithmetic & Logical Operations in 8051 Microcontroller

  • 1. Subject Code :151001 Name Of Subject :Microcontroller & Interfacing Name of Unit :Arithmetic& logical instructions Topic :Arithmetic & logical operations Name of Faculty : Mr. Haresh Suthar Miss. Madhuri Thakkar Name of Students : (i) Savalia Avani(100870111020) (ii) Patel Jay (100870111021)
  • 2. Arithmetic Instruction : There are 24 arithmetic opcodes which are grouped into the following types: ADD and ADDC SUBB MUL DIV INC DEC DA Sub: MC Topic: Arithmetic & Logical operations
  • 3. Arithmetic Flags Flag: It is a 1-bit register that indicates the status of the result from an operation Flags are either at a flag-state of value 0 or 1 Arithmetic flags indicate the status of the results from mathematical operations ( +, −, *, / ) Sub: MC Topic: Arithmetic & Logical operations
  • 4. Arithmetic Flags (Conditional Flags) There are 4 arithmetic flags in the 8051 Carry (C) Auxiliary Carry (AC) Overflow (OV) Parity (P) All the above flags are stored in the Program Status Word (PSW) CY AC -- RS1 RS0 0V -- P PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0 Sub: MC Topic: Arithmetic & Logical operations
  • 5. Arithmetic Flags (Conditional Flags)  CY PSW.7 Carry flag  AC PSW.6 Auxiliary carry flag  -- PSW.5 Available to the user for general purpose  RS1 PSW.4 Register Bank selector bit 1  RS0 PSW.3 Register Bank selector bit 0  0V PSW.2 Overflow flag  -- PSW.1 User definable flag  P PSW.0 Parity flag The C flag is keeping track in unsigned operations The OV flag is keeping track in signed operations Sub: MC Topic: Arithmetic & Logical operations
  • 6. Instructions that Affecting Flags (1/2) Instruction Mnemonic Flags Affected ADD C AC OV ADDC C AC OV SUBB C AC OV MUL C=0 OV DIV C=0 OV DA A C SETB C C=1 MOV C, bit C Sub: MC Topic: Arithmetic & Logical operations
  • 7. Instructions that Affecting Flags (2/2) Instruction Mnemonic Flags Affected ORL C, bit C ANL C, bit C RLC C RRC C CLR C C=0 CPL C C = /C CJNE C Sub: MC Topic: Arithmetic & Logical operations
  • 8. The ADD and ADDC Instructions ADD A, source ; A = A + source ADDC A, source ; A = A + source + C A register must be involved in additions The C flag is set to 1 if there is a carry out of bit 7 The AC flag is set to 1 if there is a carry out of bit 3 ADD is used for ordinary addition ADDC is used to add a carry after the LSB addition in a multi-byte process Sub: MC Topic: Arithmetic & Logical operations
  • 9. Example -1 Show how the flag register is affected by the following instructions. MOV A, #0F5h ; A = F5h 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 zero AC = 1 since there is a carry from D3 to D4 Sub: MC Topic: Arithmetic & Logical operations
  • 10. Example -2 Assume that RAM locations 40h – 42h have the following values. Write a program to find the sum of the values in these locations. At the end of the program, register A should contain the low byte and R7 contain the high byte. RAM locations: 40h = (7Dh), 41h = (EBh), 42h = (C5h) Solution: MOV A, 40h ; set A = RAM location 40h MOV R7, #0 ; set R7 = 0 ADD A, 41h ; add A with RAM location 41h JNC NEXT ; if CY = 0 don’t accumulate carry INC R7 ; keep track of carry NEXT: ADD A, 42h ; add A with RAM location 42h JNC NEXT1 ; if CY = 0 don’t accumulate carry INC R7 ; keep track of carry NEXT1: END Sub: MC Topic: Arithmetic & Logical operations
  • 11. Example -3 Write a program segment to add two 16-bit numbers. The numbers are 3CE7h and 3B8Dh. Place the sum in R7 and R6; R6 should store the lower byte. CLR C ; make C=0 MOV A, #0E7h ; load the low byte now A=E7h ADD A, #8Dh ; add the low byte now A=74h and C=1 MOV R6, A; save the low byte of the sum in R6 MOV A, #3Ch ; load the high byte ADDC A, #3Bh ; add with the carry ; 3B + 3C + 1 = 78 (all in hex) MOV R7, A; save the high byte of the sum Sub: MC Topic: Arithmetic & Logical operations
  • 12. ADDC ….. ADD ….. The DA Instruction DA A DA A DA A  The action is to “decimal adjust” the register A  Used after the addition of two BCD numbers Example 4 : MOV A, #47h ; A=47h first BCD operand MOV B, #25h ; B=25h second BCD operand ADD A, B ; hex (binary) addition (A=6Ch) DA A ; adjust for BCD addition (A=72h) Sub: MC Topic: Arithmetic & Logical operations
  • 13. Example 4 of DA Instruction Hex BCD 47 0100 0111 + 25 + 0010 0101 6C 0110 1100 + 6 + 0110 72 0111 0010 Offset decimal 6 ! Sub: MC Topic: Arithmetic & Logical operations
  • 14. SUBB A, #data The SUBB Instruction SUBB A, direct SUBB A, @Ri , where i =0 or 1 SUBB A, source SUBB A, Rn, where n =0,1,,7 No borrow: A = A – source With borrow: A = A – source – carry (i.e. borrow) Note that the 8051 uses the 2’s complement method to do subtraction After execution: The C flag is set to 1 if a borrow is needed into bit 7 The AC flag is set to 1 if a borrow is needed into bit 3 Sub: MC Topic: Arithmetic & Logical operations
  • 15. The MUL Instruction MUL AB Uses registers A and B as both source and destination registers Numbers in A and B are multiplied, then put the lower-order byte of the product in A and the high- order byte in B The OV flag is set to 1 if the product > FFh Note that the C flag is 0 at all times Sub: MC Topic: Arithmetic & Logical operations
  • 16. The DIV Instruction DIV AB Similarly, it uses registers A and B as both source and destination registers The number in A is divided by B. The quotient is put in A and the remainder (if any) is put in B The OV flag is set to 1 if B has the number 00h (divide-by-zero error) Note that the C flag is 0 at all times Sub: MC Topic: Arithmetic & Logical operations
  • 17. The INC and DEC Instructions To increment (INC) or decrement (DEC) the internal memory location specified by the operand No change with all the arithmetic flags in this operation e.g. INC 7Fh ; content in 7Fh increased by 1 DEC R1 ; content in R1 decreased by 1 INC A INC direct INC @Ri where i=0,or 1 INC Rn where n=0,,7 Sub: MC Topic: Arithmetic & Logical operations
  • 18. Logic Operation in 8051 Logical operations Rotate and swap operations Comparison operations Sub: MC Topic: Arithmetic & Logical operations
  • 19. Logical Instructions The source operand can be any of the 4 addressing modes (i.e. immediate/register/ direct/indirect) ANL can be used to clear (0) certain bits ORL can be used to set (1) certain bits Examples Instruction ANL A,R0 ORL A,R0 XRL A,R0 A before: 10010111 10010111 10010111 R0 before: 11110010 11110010 11110010 A afterwards: 10010010 11110111 01100101 Sub: MC Topic: Arithmetic & Logical operations
  • 20. The CLR and CPL Instructions CLR A All bits in register A are cleared CPLA All bits in register A are complemented (inverted) Note that CLR and CPL instructions operate on register A only Sub: MC Topic: Arithmetic & Logical operations
  • 21. RL A The Rotate Instructions RR A Contents in register A is rotated one bit position to the left or to the right (operated in A only) The bit shifted out is used as the new bit shifted in May include the C flag in the operation Useful in inspecting the bits in a byte one by one Also useful for multiplication and division in powers of 2 Sub: MC Topic: Arithmetic & Logical operations
  • 22. The Rotate Instructions RL A Rotates A one bit position to the left RLC A Rotates A and the carry flag one bit position to the left RR A Rotates A one bit position to the right RRC A Rotates A and the carry flag one bit position to the right Note that for RLC and RRC, you have to know the C flag first Sub: MC Topic: Arithmetic & Logical operations
  • 23. The Rotate Instructions 7 6 5 4 3 2 1 0 Before: 10011100 After: 00111001 RL A C 7 6 5 4 3 2 1 0 Before: 10011100 CY = 0 After: 00111000 CY = 1 Carry Flag RLC A 7 6 5 4 3 2 1 0 Before: 10011100 After: 01001110 RR A 7 6 5 4 3 2 1 0 C Before: 10011100 CY = 1 After: 11001110 CY = 0 RRC A Carry Flag Sub: MC Topic: Arithmetic & Logical operations
  • 24. The SWAP Instruction Swapping the lower-nibble (lower 4 bits) and the higher-nibble (upper 4 bits) of register A. 7 6 5 4 3 2 1 0 High Nibble Low Nibble SWAP A Register A = 5Eh (original value) after SWAP Register A = E5h Sub: MC Topic: Arithmetic & Logical operations
  • 25. Comparison Operation CJNE destination, source, relative address Compare the source and destination operands first Jump to the relative address (subroutine) if they are not equal Carry flag = 1, if destination-byte is less than the source-byte, Otherwise, the carry flag is cleared. Sub: MC Topic: Arithmetic & Logical operations
  • 26. Example -5 Write a program segment to monitor P1 continuously for the value of 63h. It should get out of the monitoring only if P1 = 63h. Solution : MOV P1, #0FFh ; make P1 an input port HERE: MOV A, P1 ; get P1 CJNE A, #63h, HERE ; keep monitoring unless ; P1=63h Sub: MC Topic: Arithmetic & Logical operations