[PIC] How to ANDed bitwise in assembler ?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[PIC] How to ANDed bitwise in assembler ?

mircea2012
Hy.
For example if I have a variable count = 30.
In binary sistem 30 = 11110.
I must do something like that :
if bit 4 is logic one
   AND
bit 3 is logic one
   AND
bit 2 is logic one
   AND
bit 1 is logic one
DO SOMETHING.
Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to ANDed bitwise in assembler ?

Mitch-98
I don't write assembly, but can't you just do the asm equiv of

if (count == 30) {
//Do something
}

On Feb 10, 2013, at 13:39, mircea2012 <[hidden email]> wrote:

> Hy.
> For example if I have a variable count = 30.
> In binary sistem 30 = 11110.
> I must do something like that :
> if bit 4 is logic one
>   AND
> bit 3 is logic one
>   AND
> bit 2 is logic one
>   AND
> bit 1 is logic one
> DO SOMETHING.
> Thank you.
>
>
>
> --
> View this message in context: http://microcontrollers.2385.n7.nabble.com/PIC-How-to-ANDed-bitwise-in-assembler-tp180767.html
> Sent from the MicroControllers - PIC mailing list archive at Nabble.com.
> --
> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist

--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to ANDed bitwise in assembler ?

Isaac Marino Bavaresco
In reply to this post by mircea2012
Does your count go past 30?
If you just need to detect when the count is 30, then it is better to
XOR your counter with 30 and check if the result is zero:

(assuming you are using a regular PIC device)
    movlw    30
    xorwf      counter,w
    btfsc        STATUS,Z
    goto        do_something

The test you suggested will do the something for counter values of 30
and 31 (0x1e and 0x1f), because you are ignoring bit zero.
If your counter goes past 30, your test will succeed again for values of
62 and 63 (0x3e and 0x3f), and also for 0x5e, 0x5f, 0x7e, 0x7f, 0x9e,
0x9f, 0xbe, 0xbf, 0xde, 0xdf, 0xfe, 0xff.


Isaac


Em 10/02/2013 17:34, mircea2012 escreveu:

> Hy.
> For example if I have a variable count = 30.
> In binary sistem 30 = 11110.
> I must do something like that :
> if bit 4 is logic one
>    AND
> bit 3 is logic one
>    AND
> bit 2 is logic one
>    AND
> bit 1 is logic one
> DO SOMETHING.
> Thank you.
>
>
>
> --
> View this message in context: http://microcontrollers.2385.n7.nabble.com/PIC-How-to-ANDed-bitwise-in-assembler-tp180767.html
> Sent from the MicroControllers - PIC mailing list archive at Nabble.com.

--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
ivp
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to ANDed bitwise in assembler ?

ivp
In reply to this post by mircea2012
Hi,

you can XOR

         movlw   b'00011110'
         xorwf   counter,w
         skpnz
         goto    do_something

or

         movlw   .30
         xorwf   counter,w
         bz      do_something

or

         movf    counter,w
         xorlw   .30
         bz      do_something

Only when all bits of the test and target are the same positions
and polarity will the result be zero. This includes the bits you
aren't interested in

00011110
00011110 XOR
----------
00000000

It's a very useful function, and is reversible, for example in a
character search

         xorlw   "A"   ;test W for "A"
         bz      is_a  ;yes, branch
         xorlw   "A"   ;no, restore W

01000010 'B', W contents
01000001 XOR with 'A'
----------
00000011 result, W not zero
01000001 XOR with 'A'
----------
01000010 result, 'B' restored to W

Can also be compounded

         xorlw   "1"        ;test W for '1'
         bz      job1       ;yes, branch
         xorlw   "1" ^ "2"  ;no, restore W, test for '2'
         bz      job2       ;yes, branch

Joe
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to ANDed bitwise in assembler ?

Brian Gregory-2
In reply to this post by mircea2012
In-Reply-To: <[hidden email]>

Mircea2012,

> Hy.
> For example if I have a variable count = 30.
> In binary sistem 30 = 11110.
> I must do something like that :
> if bit 4 is logic one
>    AND
> bit 3 is logic one
>    AND
> bit 2 is logic one
>    AND
> bit 1 is logic one
> DO SOMETHING.
> Thank you.

Typically one would use AND to force the bits you don't care about to zero and then just test whether it's equal to
the value you want.

Thus unknown value in 8 bit register X.
And 'AND' is an infix bitwise and operator.

IF (X AND 0b00011110) == 0b000111110
THEN ....
ENDIF

The two values are the same because all the bits you care about you want to be 1.

I know that's not assembler but you didn't say what assembler you were using.

Brian Gregory.
[hidden email]

--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to ANDed bitwise in assembler ?

Brian Gregory-2
In reply to this post by mircea2012
In-Reply-To: <[hidden email]>

Mircea2012,

> Hy.
> For example if I have a variable count = 30.
> In binary sistem 30 = 11110.
> I must do something like that :
> if bit 4 is logic one
>    AND
> bit 3 is logic one
>    AND
> bit 2 is logic one
>    AND
> bit 1 is logic one
> DO SOMETHING.
> Thank you.
>

Usually one would use bitwise and to force all the bits you don't care about to zero and test the result equal to the required
value.

Lets say 'AND' is an bitwise and operator and '==' tests if two values are equal.

if (count AND 0b00011110) == 0b00011110)
then
   ...
endif

I know that's not assembler but you didn't say what assembler you were using.

Brian Gregory.
[hidden email]

--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] How to ANDed bitwise in assembler ?

Oli Glaser
In reply to this post by mircea2012
On Sun, Feb 10, 2013 at 8:34 PM, mircea2012 <[hidden email]>wrote:

> Hy.
> For example if I have a variable count = 30.
> In binary sistem 30 = 11110.
> I must do something like that :
> if bit 4 is logic one
>    AND
> bit 3 is logic one
>    AND
> bit 2 is logic one
>    AND
> bit 1 is logic one
> DO SOMETHING.
> Thank you.
>
>
Hi,
I noticed you asked this on Stack Exchange also, glad to see you got some
good answers here (as well as one from Olin on SE now I see)
Just a small tip - it's worth mentioning either the microcontroller when
dealing with Assmebler, since (as you probably know already) different
microcontollers have different syntax and range of commands for their
assembly languages. This will help you to get a code example rather than
the pseudo code Olin gave you. See these links for some info:
http://en.wikipedia.org/wiki/Assembly_language
http://en.wikipedia.org/wiki/Reduced_instruction_set_computing
http://en.wikipedia.org/wiki/Complex_instruction_set_computing
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist