Continue to Site

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.

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

Logic statement in assembly language

Status
Not open for further replies.

Parth86

Member
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
 
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​
 
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​
I am confused it means I don't need to write statement in program I can use only loop for AND logic
 
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
 
p
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
please clear one thing all logical operation can be done in accumulator so where and how to use acc in above program
 
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:
    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:
    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
 
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..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top