Jump to content
IGNORED

Checking a bit - cannot get this to work


Recommended Posts

I have all of these flag variables and I want to consolidate them into one byte called Flags.

 

Flags is set to 0, but it doesn't branch here.

 

    lda #%00000010                ; setting up accumulator to check position 1 bit
    bit Flags                    ; comparing Flags - position 1 for Add Hole Flag set
    bne .SetPoleFlag

 

What am I doing wrong?

 

Thanks!

 

 

 

 

 

 

Link to comment
Share on other sites

Ah... The logic is backwards.... switch the branch instruction to a BEQ if you want to branch when the 'position 1 bit' is not set.

 

----

I'll try to explain:

 

It's best to think of the BIT op as an AND operation (that's mainly what it is)... not a CMP.

 

If the 'Flags' variable is zero, the BIT op will set the zero flag because (Flags AND %00000010) will be zero.  In this case, BNE won't branch because the zero flag is set.

 

BNE is Branch Not Equal... but can also be thought of as BNZ - Branch if Not Zero (zero flag is not set).

BEQ is Branch Equal...       but can also be thought of as BZ   - Branch if Zero (zero flag is set).

 

BNE and BEQ are named as they are because they're typically used with the CMP op, in which their names make more sense.

 

 

This is one of the fun little confusing bits of programming in Assembly language that sometimes trips me up also.

 

Hopefully this all makes sense and I haven't made things more confusing.

Link to comment
Share on other sites

39 minutes ago, splendidnut said:

Ah... The logic is backwards.... switch the branch instruction to a BEQ if you want to branch when the 'position 1 bit' is not set.

Can I ask you one more question?

 

What should I be using to set that bit on and off.

 

I want to control it with the 2nd joystick - joystick right / turn off - joystick left / turn on. I have this code below, but cannot get it to work either.

 

Thanks!

 

CheckP1Left:
        lda #%00000100           ; player 1 joystick right - J
        bit SWCHA
        bne CheckP1Right

        lda Flags                ; loading Flags variable into the accumulator
        ora #%00000010            ; Flags - Position 1 - setting Add Hole Flag to False
        sta Flags

CheckP1Right:
        lda #%00001000           ; player 1 joystick left - G
        bit SWCHA
        bne CheckP1Up

        lda Flags                ; loading Flags variable into the accumulator
        and #%00000010            ; Flags - Position 1 - setting Add Hole Flag to True
        sta Flags


CheckP1Up:

 

Link to comment
Share on other sites

To set a bit:

lda Flags                ; loading Flags variable into the accumulator
ora #%00000010            ; Flags - Position 1 - setting Add Hole Flag to True
sta Flags

Basically OR with the bit you want to set

 

----

 

To clear a bit:

lda Flags                ; loading Flags variable into the accumulator
and #%11111101            ; Flags - Position 1 - setting Add Hole Flag to False
sta Flags

In this case, you need to Mask off the bit by performing an AND op with the inverse.  This basically keeps the state of all the other bits, while turning off the bit you want.

Link to comment
Share on other sites

And if you want to flip / invert a bit:

 

lda Flags                ; loading Flags variable into the accumulator
eor #%00000010           ; Flags - Position 1 - flip the state of the Add Hole Flag
sta Flags

 

If the Add Hole Flag is on, this will turn it off.

If the Add Hole Flag is off, this will turn it on.

Link to comment
Share on other sites

11 minutes ago, splendidnut said:

And if you want to flip / invert a bit:

 


lda Flags                ; loading Flags variable into the accumulator
eor #%00000010           ; Flags - Position 1 - flip the state of the Add Hole Flag
sta Flags

 

If the Add Hole Flag is on, this will turn it off.

If the Add Hole Flag is off, this will turn it on.

I was using eor for flipping the pause flag and that was the only part working fine.

 

Thanks for all of your help!!!!!

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...