How to toggle a single bit using Assembly Language
Is there an assembly language(SX)·instruction that can toggle a single bit
Example: (If Bit=1 Then Bit=0; If Bit=0 Then Bit=1)
I am currently using this code (Which I believe is not optimum)
·clr·buffer······································ ;1 Cycle
·movb temp.0, LED_PIN···················· ;1 Cycle
·xor temp,#%00000001···················· ;1 Cycle
·movb LED_PIN, temp.0···················· ;1 Cycle
Is there an instruction that can perform a similar operation in a single instruction? Or is there a more optimum way of doing it?
Cheers,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bashar Sadig
Senior Solutions Architect - Enterprise Applications
Example: (If Bit=1 Then Bit=0; If Bit=0 Then Bit=1)
I am currently using this code (Which I believe is not optimum)
·clr·buffer······································ ;1 Cycle
·movb temp.0, LED_PIN···················· ;1 Cycle
·xor temp,#%00000001···················· ;1 Cycle
·movb LED_PIN, temp.0···················· ;1 Cycle
Is there an instruction that can perform a similar operation in a single instruction? Or is there a more optimum way of doing it?
Cheers,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bashar Sadig
Senior Solutions Architect - Enterprise Applications
Comments
XOR flags, #%00001000
xorb mybit (I am wishing)
The reason I use the buffer temp is becaues, I will update all leds at the end of the cycling. This saves memory.
If I follow the same scheme I presented before
:_Event_One_Label
clr temp ;1 Cycle or mov temp, ra (for example)
movb temp.0, LED_PIN ;1 Cycle
xor temp,#%00000001 ;1 Cycle
movb LED_PIN, temp.0 ;1 Cycle
:_Event_Two_Label
movb temp.1, LED_PIN_2 ;1 Cycle
xor temp,#%00000010 ;1 Cycle
movb LED_PIN_2, temp.2 ;1 Cycle
:_When_Ready_To_Update_Port
mov ra, temp
For each LED I want to toggle (On Event) will require the same sequence of instructions updating the temp var
Note my code does work, it just costs too many cycles - especially if I need to work on 8 LED Indicators for example
Regards,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bashar Sadig
Senior Solutions Architect - Enterprise Applications
so this could help
Copy aligned bits from one register to another
Here is a fast way to save specific bits from one register into another. mov W, Source xor W, Destination and W, #%xxxxxxxx ;Replace "x" with "1" to Copy the Bit xor Destination, W
Edit: movb takes 4 cycles
To make the bit toggle
mov W, Source xor W, Destination and W, #%xxxxxxxx ;Replace "x" with "1" to Copy the Bit xor W, #%xxxxxxxx ;Replace "x" with "1" to Toggle the Bit xor Destination, W
regards peter
Post Edited (Peter Verkaik) : 9/22/2007 3:56:41 PM GMT
Event_one:
XOR RA, #%0001 ; toggle LED 1
Event_two:
XOR RA, #%0010 ; toggle LED 2
I'm not the sharpest tool in the shed, so maybe I don't understand what you want to do -- but... it really seems like you're making things harder than they have to be.
Your answer will be my interim solution and if Parallax has a hidden instruction for "XORB" or equivalent as I wish, it will come in handy.
Here is how I will use your approach:
mov temp, a
Event_One:
xor temp,#%00000010
Event_Two:
xor temp,#%00000001
When_Done:
mov ra, temp
I will try this today. My biggest challenge is if I branch out and still want to update a port. Anyway, no need to over-kill
Thanks a bunch
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bashar Sadig
Senior Solutions Architect - Enterprise Applications
will toggle that bit in temp.
-Phil
Great...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bashar Sadig
Senior Solutions Architect - Enterprise Applications