Compare, Compare, Compare those bits!
Ted Schafer
Posts: 2
Hello Members!
I would like to ask for your help in understanding a SX instruction.
I am new to the SX instruction set and the SX28 chip
I have had no problem usings·the CJE·instruction when comparing bytes of data.
I do not however understand comparing bits.
Example: mov w,·rb.0· Now the working register has not only my bit 0 but also bits 1-7.
Moving on, lets say that the other bits are changing due to outside events
Now comes the CJE instruction.
Example: cje w, #%XXXXXXX1, Jump_Somewhere
Question: Am i only comparing bit 0 or all 8 bits.
If this is comparing all 8 bits or the byte, how would you just compare a single bit in that byte with the other bits· changing.
Like cje rb.0, #%1, Jump_Somewhere
Thanks for any help in understanding!
Ted Schafer
I would like to ask for your help in understanding a SX instruction.
I am new to the SX instruction set and the SX28 chip
I have had no problem usings·the CJE·instruction when comparing bytes of data.
I do not however understand comparing bits.
Example: mov w,·rb.0· Now the working register has not only my bit 0 but also bits 1-7.
Moving on, lets say that the other bits are changing due to outside events
Now comes the CJE instruction.
Example: cje w, #%XXXXXXX1, Jump_Somewhere
Question: Am i only comparing bit 0 or all 8 bits.
If this is comparing all 8 bits or the byte, how would you just compare a single bit in that byte with the other bits· changing.
Like cje rb.0, #%1, Jump_Somewhere
Thanks for any help in understanding!
Ted Schafer
Comments
I believe what you need to do is called bit masking.
Set up a temporary storage called rb0m (rb.0 memory)
rb0m ds 1 ;1 byte for temp storage
mov w,rb.0 ;w now holds rb.0
mov rb0m,w ;rb0m now has status of rb.0
and rb0m,#%00000001 ;this line performs bitwise AND on rb0m and %00000001
cje rb0m,#%00000001 ;compare bit 1 of rb.0 with 1, jump if equal
I used binary to hopefully illustrate that you can easily mask any bit, or combination of bits.
Post Edited (KenM) : 6/3/2005 2:37:35 PM GMT
Check_Bit
··· SNB RB.0
··· JMP Somewhere
Somewhere_Else
If the bit is set (what you're looking for), the SNB instruction will fail and the code will drop to the jump, otherwise the jump gets skipped.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 6/3/2005 2:43:40 PM GMT
To do more than one bit at a time, bit masking as previously described.
snb rb.0
Post Edited (KenM) : 6/3/2005 2:48:43 PM GMT
JB RB.0,Somewhere ; Jump if bit is set
or
JNB RB.0,Somewhere ; Jump if bit is NOT set
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
"It's not getting what you want, it's wanting what you've got."
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
I got a lot of playing to do now that I understand this better!
Thanks
Ted Schafer
Why does "MOV LEDS, #0 " fire off in the debuger.
IrCurrentState and IrLastState are both zero.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jack
you can't use CLRB instructions to clear byte variables like IrCurrentState and IrLastState - use CLR instructions instead. CLRB is used to clear a single bit in a register. Usually, CLRB should be used with a bit number, like in
CLRB IrCurrentState.0
to clear the lowest bit, for example. As SASM does not complain about the missing bit number, I think it assumes a default bit but I don't remember which one.
You should also use the MOVB instruction together with a bit number. Instead of
MOVB IrCurrentState, PinIr
you should use something like
MOVB IrCurrentState.0, PinIr
also
MOVB IrLastState, IrCurrentState
does not specify which bits shall be moved - use something like
MOVB IrLastState.0, IrCurrentState.0
or, when you want to copy all bits from IrCurrentState to IrLastState use
MOV IrLastState, IrCurrentState
instead.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greetings from Germany,
G
CJE is not really for a bit variable (like IrCurrentState). Plus in your post you don't have the required label on the CJE line.
This code will work to compare bits in position 0 and 1:
MOV W,>>IrState
XOR W,IrState
AND W,#1
JZ BitsAreEqual
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
"It's not getting what you want, it's wanting what you've got."
·
Although in some twisted way I am enjoying not knowing what
I am doing when it comes to programming.
Been a long time since I learned a new programming language
First to Gunther:
You say "CLRB should be used with a bit number, like in CLRB IrCurrentState.0".
I thought that was what I am doing.
From the code:
IrState DS 1
IrCurrentState DS IrState.0
IrLastState DS IrState.1
You say I should use something like IrState.0.
By declaring:
IrCurrentState DS IrState.0
Does not IrCurrentState equate to IrState.0.
Oh, man I see it now, it should be:
IrCurrentState EQU IrState.0
Yes/no?
Bean, thanks for the snippet.
Your code is working with bytes, if the above is correct then it
does not apply, right?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jack
I get now, thank you.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jack