Shop OBEX P1 Docs P2 Docs Learn Events
How to toggle a single bit using Assembly Language — Parallax Forums

How to toggle a single bit using Assembly Language

Bashar SadigBashar Sadig Posts: 10
edited 2007-09-22 18:28 in General Discussion
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

Comments

  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-09-22 14:26
    The only thing you need is the XOR. If, for example, you want to toggle flags.3 you would do:

    XOR flags, #%00001000
  • Bashar SadigBashar Sadig Posts: 10
    edited 2007-09-22 15:26
    yes flags is my "temp" variable. I was looking more for an instruction that works on the single bit:

    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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-09-22 15:48
    It looks the LED bits are aligned with the temp bits,
    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
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-09-22 17:02
    We all have access to the same SX documentation, and as far as I can tell, there is no XORB instruction. Since RA (where you LEDs are connect, right?) is part of RAM you can operate on it directly:

    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.
  • Bashar SadigBashar Sadig Posts: 10
    edited 2007-09-22 17:55
    Great input. Thank you Jonny. I was hung on using symbolic names "LED_PIN" for ease of porting or changing my pins and exchange code across programs. This is why I was focused on setting of the single bit. I was hoping to see an instruction that handles xoring of a single bit for this and other purposes.

    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
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-09-22 18:21
    You can still use symbolic names equating to bit positions. For example, if LED_PIN is bit 1, then

    LED_PIN EQU 1
            ...
            XOR temp,#1<<LED_PIN  ;Equivalent to #%00000010
    
    
    


    will toggle that bit in temp.

    -Phil
  • Bashar SadigBashar Sadig Posts: 10
    edited 2007-09-22 18:28
    Is super. Thank you Phil, I stressed out a little and was just worried about having to abandon use of symbols.

    Great...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Bashar Sadig

    Senior Solutions Architect - Enterprise Applications
Sign In or Register to comment.