Is it possible to compare bits?
Hello,
I've been working on a little program where I am putting various bit data into the same variable (to save space), and then trying to compare them.
Apparently the sx-key assembler did not like the following piece of code, or others like it:
cje encbits.0, encbits.1, encAcountAddTwo
what I was hoping to accomplish with the above line was to compare bit 0 and bit 1 from variable 'encbits', then jump to the label 'encAcountAddTwo' if they were the same.
Any suggestions as to the options that I have?![confused.gif](http://forums.parallax.com/images/smilies/confused.gif)
Thanks,
Mark
I've been working on a little program where I am putting various bit data into the same variable (to save space), and then trying to compare them.
Apparently the sx-key assembler did not like the following piece of code, or others like it:
cje encbits.0, encbits.1, encAcountAddTwo
what I was hoping to accomplish with the above line was to compare bit 0 and bit 1 from variable 'encbits', then jump to the label 'encAcountAddTwo' if they were the same.
Any suggestions as to the options that I have?
![confused.gif](http://forums.parallax.com/images/smilies/confused.gif)
Thanks,
Mark
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
mov w,>>encbits· 'mov b1 into b0 of w
xor w,encbits····· 'compare with b0 of encbits
and w,#1·········· 'extract bit, zero if equal
snz
jmp encAcountAddTwo
regards peter
Whew, it looks like it's going to be a bit tricky, as I am using cje, cjne, and cja quite a lot! This is my first attempt at writing a program in assembly, so i'm sure there is a more efficient way I could do things. I suppose I could try doing some trickery with masks, but I wonder if that's truly the best way to go. Also, currently all the bits I'm comparing with one another are not necessarily next to each other, perhaps I could rearrange them.
Yikes! Just reviewing my code, I noticed a large omission, so perhaps I just need to rewrite the entire program with these concepts in mind [noparse]:([/noparse]
I suppose I could just write it in C or basic (neither of which I'm proficient in though), but then I won't get the strange pleasure obtained from the challenge of assembly.
· It's not pretty, but it works.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There is a fine line between arrogance and confidence. Make sure you don't cross it...
·
I ended up creating a new variable 'TestBits' which I will reuse throughout the program. I will just move the desired bits into it, then just compare the entire byte value which I have full control of. By my logic, if encbits 0 and 1 are both zero, then when I test them against 0, if the statement is true, it will jump to the appropriate label. Same thing if they're both 1, then I just compare the 'testbits' variable with 3. Is this really inefficient?
A_1_CngStateSub ;Sub for calculating Enc_A_count var if Enc A ch.1 changed state
clr TestBits
movb testbits.0, encbits.0
movb testbits.1, encbits.1
cje testbits, #0, :encAcountAdd
cje testbits, #3, :encAcountAdd
:encAcountSub
dec Enc_A_count
jmp :End_A1_cngState
:encAcountAdd
inc Enc_A_count
:End_A1_cngState
ret
This works because if both bits are 0 or 1 tempBits.0 will be 0.
[noparse][[/noparse]Edit] The jump out of encAcountSub is jumping over one instruction (INC) so SKIP can be used.
Post Edited (JonnyMac) : 2/25/2009 4:34:08 AM GMT
Thanks for the help, everyone!
I'm considering interfacing a device such as a "transmissive photomicrosensor" by omron. Looking at datasheets for various models, it's confusing me, so I want someone to give me some insight as to whether I'm on the right track, or not. If not, what am I missing?
http://www.components.omron.com/components/web/pdflib.nsf/0/0ADB48765B0912AD85257201007DD5D8/$file/24_sx1035_0607.pdf
There's a tiny usage schematic on the bottom right of page two. It shows a resistor attached to the emitter leg, which I presume is to limit the current through the device. But trying to interpret the info in the table labeled "Electrical and optical characteristics" is leaving me at a total loss. I don't understand what the value and condition column.
I'm looking to apply +5v at the collector. Am I correct at interpreting max current at 20ma? So using ohms law, my load resistor should be somewhere above 250ohms, right? Am I the only one who thinks that datasheet totally sucks?
· The load resistor simply converts the varying current into a varying voltage that you can detect.
· The Rise/Fall time table shows a load resistor of 100 ohms. And the Responce time chart shows that the load can be up to 10K.
· You will probably have to experiment with different values to get the voltage levels you want out (this will depend you the amount of current you put through the LED).
· The datasheet looks pretty standard to me. They never give you want you "really" want to know.
P.S. Yes, you should have started a new thread...No biggie...
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There is a fine line between arrogance and confidence. Make sure you don't cross it...
·
· mov· w, >>encBits
··xor · w, encBits
· and· w,#1
· add· enc_a_count,w
· add· enc_a_count,w
· dec· enc_a_count
David
NICE... Elegant solution.. Only 6 instructions.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There is a fine line between arrogance and confidence. Make sure you don't cross it...
·
Wow! It took me a second to try to figure out why you were adding w twice, then performing a subtraction. Finally, zoot's version makes sense to me too! I don't know how some of your brains figure this stuff out! This certainly helped expand my mind on sx assembly, and logic.
Thanks,
Mark S
Actually, I missed a trick too ... now 5 words
·mov w,<<encbits
·xor w,encbits
·and w,#2
·add enc_a_count,w
·dec enc_a_count
Same idea as before but accumulate the result in bit 1; allows the double add to be done in one instruction ...
David
Ok.. does this sound right to you?:
IL (14ma) is the current that will be sunk through RL (and whatever the next circuit sinks), and IF (20ma) is the total amount of current the detector circuit uses.
Mark