Shop OBEX P1 Docs P2 Docs Learn Events
Compare, Compare, Compare those bits! — Parallax Forums

Compare, Compare, Compare those bits!

Ted SchaferTed Schafer Posts: 2
edited 2005-06-03 20:37 in General Discussion
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! rolleyes.gif

Ted Schafer

Comments

  • KenMKenM Posts: 657
    edited 2005-06-03 14:34
    Ted,

    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
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-03 14:39
    With SX/B in my toolbox, I'm not much of an assembly programmer, but I think you can do it like this:

    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
  • KenMKenM Posts: 657
    edited 2005-06-03 14:43
    Jons suggestion also works in assembly, less work, but only one bit at a time. So his suggestion is better if you need to test only one bit.

    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
  • BeanBean Posts: 8,129
    edited 2005-06-03 14:45
    Even easier:
    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 WilliamsJon Williams Posts: 6,491
    edited 2005-06-03 14:47
    I told you I wasn't much of an assembly guy! SX/B rocks!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-06-03 14:53
    Just to add one item, Bean's JB/JNB instructions are compound (meaning when they are assembled into code more than one instruction is produced). JB/JNB are translated into Jon's SB/SNB-JMP code, the compound instructions provided by SASM are to make the code easier to read.
  • Ted SchaferTed Schafer Posts: 2
    edited 2005-06-03 14:57
    Thanks Guys.
    I got a lot of playing to do now that I understand this better!

    Thanks

    Ted Schafer
  • PLJackPLJack Posts: 398
    edited 2005-06-03 17:14
    While we are on the subject. [noparse]:)[/noparse]

    Why does "MOV LEDS, #0 " fire off in the debuger.

    IrCurrentState and IrLastState are both zero.



    ;=======================================================================
    ;TITLE:         RTCC Test
    ;
    ;PURPOSE:       Test 20us RTTC
    ;=======================================================================
    
    
    ;-------------------------- DEVICE DIRECTIVES --------------------------
    
    DEVICE        SX28,OSC4MHZ,TURBO
    
    IFDEF    __SASM  
            DEVICE        STACKX,OPTIONX
            IRC_CAL        IRC_FAST
            
    ELSE        
            DEVICE        STACKX_OPTIONX
    ENDIF
    
    RESET        Initialize
    
    
    ORG    $8
    
    LEDS            EQU        rb
    PinIr                     EQU        ra.0
    
    
    IrState            DS    1
    IrCurrentState        DS        IrState.0
    IrLastState           DS    IrState.1
    
    
    watch                 IrCurrentState,    1,    UDEC
    watch                 IrLastState,    1,    UDEC
    
    
    
    
    
    ;---------------------------- DEBUG SETTINGS ---------------------------
    
            FREQ    4_000_000
        
    ;-------------------------- INTERRUPT ROUTINE --------------------------
    
            ORG     $00
    
    Interrupt
    
        mov w, #-80
        retiw
    
    ;------------------------ INITIALIZATION ROUTINE -----------------------
    
    Initialize
        MOV    !OPTION,#%10011111                    ;    Enable RTCC rollover , no prescaler
        mov    !rb,             #0                ;    Make RB outputs
        mov    rb,             #0                    
        CLRB    IrCurrentState
        CLRB    IrLastState
    
    
    ;---------------------------- MAIN PROGRAM -----------------------------
    
    Main        
    
        MOVB    IrCurrentState,    PinIr
        CJE    IrCurrentState,    IrLastState,    :Again    ;Jump if Equal?
        MOV     LEDS, #0                          ;Why does this code run
                                    
    :Again 
        MOVB    IrLastState,    IrCurrentState        
        jmp    Main
    
    
    
    
    
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jack
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2005-06-03 17:43
    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
  • BeanBean Posts: 8,129
    edited 2005-06-03 17:54
    Jack,
    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."
    ·
  • PLJackPLJack Posts: 398
    edited 2005-06-03 18:13
    OK, it's official. I'm a ASM noobie. [noparse]:)[/noparse]
    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
  • PLJackPLJack Posts: 398
    edited 2005-06-03 20:37
    Just reread your post Bean.

    I get now, thank you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jack
Sign In or Register to comment.