I am trying to "AND" 2 Variables "If Variable_1 = 0 AND Variable_2 = 1 then" "DO WHAT IS NEEDED." "End If"

This below makes no sense used as a "AND" Logic Function. Will the above STATEMENT work using ANDWF or ANDLW? Do I need to ANDWF each Variable separately, or is it possible to use something else?

ANDWF f , d Logic AND between the content of W register with the content of a file register 'f'. If d is 0 the result is stored in the W register, if d is 1 the result is stored back to the file register 'f'. Operation: (W).AND. (f) ® destination Description: AND the W register with register 'f'. If 'd' is 0 the result is stored in the W register. If 'd' is 1 the result is stored back in register 'f'.

I have read the data-sheet PIC18f2550 and the PIC18 Family Reference Manual and Looked All over the internet and Stackexchange. I simply have not found an understandable answer. If the "ANDWF & ANDLW" are truly "AND" Functions



The IF, AND, THEN and END IF in your code are all statements affecting program flow, not mathematical operators.

The PIC18F2550 has an instruction CPFSEQ (compare w to f, skip next instruction if equal) which can be used to implement the AND function in your example, like this:-

  MOVLW  0
  CPFSEQ Variable_1  ; IF Variable_1 = 0  
  GOTO   _endif      
  MOVLW  1
  CPFSEQ Variable_2  ; AND variable_2 = 1 
  GOTO   _endif      
  .                  ; THEN do what is needed       

Note: this code just shows the principle, and may not be the most efficient (eg. using TSTFSZ Variable_1 and BRA _endif would be faster and more compact).

  • \$\begingroup\$ After Working with the CPFSEQ I found out my question was actually easier than the question I needed to ask! The real problem turned out to be an AND OR problem. \$\endgroup\$ – user66377 Apr 16 '15 at 22:06

They're bit-wise AND instructions whereas if I'm reading your question correctly you're expecting a logical AND.

In these bit-wise AND instructions, each of the 8 bits of one operand are individually AND-ed with the appropriate bits of the other operand and the result is stored wherever specified.

So for example if your W register contains 0x23 and you execute a ANDLW 0x12 you will end up with 0x02 in W.

0x23   == 0b00100011  
0x12   == 0b00010010  
AND-ed == 0b00000010  
  • \$\begingroup\$ So basically it is not a Logical AND.... \$\endgroup\$ – user66377 Apr 16 '15 at 19:18
  • \$\begingroup\$ Each individual bit gets a Logical AND, so if you feed those instructions appropriate operands you can make them look and act like logical ANDs... \$\endgroup\$ – brhans Apr 16 '15 at 19:20

After Working with the CPFSEQ I found out my question was actually easier than the question I needed to ask!

The real problem turned out to be a combination AND OR problem.

If var_1 = 0 AND var_2 = 0 then GOTO Loop..... If var_1 > 0 OR var_2 > 1 then GOTO Continue.

May not be the most efficient but it does work.


MOVLW b'00000000'

 MOVWF    RxCount              ;Start Count at Zero


MOVLW b'00000000' .....Set RxRegister to Zero Normally changes in

Program Routine.

MOVWF RxRegister

nop ....;Added because the next MOVWF had a

number > 0, didn't work right .

MOVLW b'00000000'

CPFSGT RxCount ....; if RxCount = Zero goto "If_Or_And" OR else GOTO Continue

GOTO If_Or_And


INCF       RxCount

CALL      Other_Routines      ;Get RxRegister Data

        GOTO     Loop 


TSTFSZ  RxRegister            ;if RxRegister > Zero then GOTO Continue: 

GOTO    Continue              ;if RxRegister = Zero then GOTO Loop: 

GOTO    Loop    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.