1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Logic statement in assembly language

Discussion in '8051/8951' started by Parth86, Oct 4, 2013.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    I want to write assembly code for 8051

    switch A is connected to P0.1
    switch B is connected to P0.2
    LED is connected to P1.0

    IF both switches high then the LED is turn on otherwise turn off

    switch A high then turn off LED
    switch B high then turn of LED
    both A B high then turn on LED
    both A B low then turn off led
    I want to write assembly program using AND logic statement
    How to write statement for AND logic for switches
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    The command ANL works with a byte so

    ANL B, #FF ; and's B with 0xFF...

    You need to test individual bits...

    ie..
    LOOP:
    JNB P0.1, OFF ; switch 1
    JNB P0.2, OFF ; switch2
    SETB P1.0 ; LED on
    SJMP LOOP​
    OFF:
    CLR P1.0 : LED off
    SJMP LOOP​
     
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    I am confused it means I don't need to write statement in program I can use only loop for AND logic
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK

    I suppose you can really.. As both switches are on a single port..

    so...
    ANL P0 , #6 ; And Port 0 with 6 ( P0.1 and P0.2 )..

    But I don't think that's what you want it to do!!

    In the little example, I check switch 1.. If its on then I check if switch 2 is on.. Only then do you switch on the LED
     
  6. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    p
    please clear one thing all logical operation can be done in accumulator so where and how to use acc in above program
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    ANL Is used by the Acc only... Sorry about that... A quick look at the datasheet says it can... But it can't.

    This works... with bit test..
    Code (asm):

        org     0000h        ; Reset vector.
        sjmp    start        ; Jump over vector space.
        org    030h  

    start:                ; This will be assembled at 0x30.
        clr    p2.0        ; Turn LED off
        mov    P0,#6        ; Inputs...

    while:
        jb    p0.1, turnon    ; Test switch 1.
        jb    p0.2, turnon    ; Test switch 1.
        clr    p2.0        ; Turn LED on.
        sjmp    while        ; Or go back to LABEL (while)
    turnon:

        setb    p2.0        ; turn LED OFF.
        sjmp    while        ; end while

    end
     
    This is with a ANL command
    Code (asm):

        org     0000h        ; Reset vector.
        sjmp    start        ; Jump over vector space.
        org    030h  

    start:                ; This will be assembled at 0x30.
        clr    p2.0        ; Turn LED off
        mov    P0,#6        ; Inputs...

    while:
        mov    A,P0        ; Copy switches to Acc
        ANL    A,#0x6        ; Test which are on / off
        jnz    turnoff        ; both on.. if not zero
        clr    P2.0        ; one or more are off
        sjmp    while
    turnoff:
        setb    p2.0        ; turn LED ON.
        sjmp    while        ; end while

    end
     
     
  8. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    A quick look at the datasheet says it can... But it can't.
    what are you trying to say
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    Oh!! Ignore that... I was just having a whinge at the datasheet... As you know they are not always clear about some commands.

    Capture.png

    If you look at the table it doesn't specifically state the accumulator..
     
  10. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    thank you very much Ian
     

Share This Page