How can I perform AND two times with 2 separate registers?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
infidelity
Posts: 486
Joined: Fri Mar 01, 2013 4:46 am

How can I perform AND two times with 2 separate registers?

Post by infidelity »

In my routine, I am able to load a register, use AND, and the value is changed. But after that, ill do the same with another register, and the value does not change....

Example

Code: Select all

LDA $70	;46 is in $70
AND #$F0	;i use this for checking bits, I want to make the (6) in the value (46) to 0, so now the value in A is 40
STA $70		;40 into $70

Now here is where I get no change...

LDA $84	;95 is in $84
AND #$FD	;i use this cause I want the (5) in the value (95) to become (D) making 95 into 9D, but nothing happens, and 95 still remains...
Shiru
Posts: 1161
Joined: Sat Jan 23, 2010 11:41 pm

Re: How can I perform AND two times with 2 separate register

Post by Shiru »

Your code gives correct result, as 0x95 'and' 0xfd is 0x95.

If you want to replace low nibble, you should 'and' 0xf0 then 'or' new value (like 0x05).
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How can I perform AND two times with 2 separate register

Post by tokumaru »

AND is used to clear bits, and OR (ORA in the 6502) is used to set bits. To set part of an arbitrary value (i.e. you don't know the value beforehand) you must first clear the bits you want to change with AND and then set them to the values you want with OR. The exceptions are when you want the modified bits to be all 0 (an AND alone can do it) or all 1 (an OR alone will do). If you want to flip bits (0 becomes 1 and 1 becomes 0) you can use a XOR (EOR on the 6502).
infidelity
Posts: 486
Joined: Fri Mar 01, 2013 4:46 am

Re: How can I perform AND two times with 2 separate register

Post by infidelity »

@Shiru, your example worked thank you!

One more question How do you compare a bit? Id like to see if a hex value matches #8, and if it does, to branch to whatever i want.
Shiru
Posts: 1161
Joined: Sat Jan 23, 2010 11:41 pm

Re: How can I perform AND two times with 2 separate register

Post by Shiru »

If you want compare a number, well,

Code: Select all

 ;value to test in A
 cmp #$08
 beq address ; go somewhere if A=$08
However, if you want to test a bit, say, it is bit 3, which is $08 bitmask, then just use AND:

Code: Select all

 ;value to test a bit in A
 and #$08
 bne address ;go somewhere if bit 3 is set, i.e. and result is non zero
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How can I perform AND two times with 2 separate register

Post by tokumaru »

infidelity wrote:Id like to see if a hex value matches #8, and if it does, to branch to whatever i want.
When you say an "hex value" do you mean a nibble? Like the "8" in $38 or $8D? If so, you need to clear the bits you don't care about and then compare normally:

Code: Select all

and #$0f ;keep only the lower nibble
cmp #$08 ;compare it to 8
beq LowerNibbleIs8 ;branch if equal
infidelity
Posts: 486
Joined: Fri Mar 01, 2013 4:46 am

Re: How can I perform AND two times with 2 separate register

Post by infidelity »

I need to preserve the original left bits, while being able to properly compare then alter the right bits. I hope that makes sense.
3gengames
Formerly 65024U
Posts: 2281
Joined: Sat Mar 27, 2010 12:57 pm

Re: How can I perform AND two times with 2 separate register

Post by 3gengames »

Code: Select all

LDA #$FF ;11111111 in binary.
AND #$F0 ;11110000 in binary. 11110000 is the result in this case.
LSR A
LSR A
LSR A
LSR A ;A is not the same as 0000**** where **** is the same as the top 4 bits of the result of AND #$F0 since we just moved it right 4 bits.
CMP CompareValue ;Do your compare.
;Branch
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How can I perform AND two times with 2 separate register

Post by tokumaru »

infidelity wrote:I need to preserve the original left bits, while being able to properly compare then alter the right bits. I hope that makes sense.
For that you'd typically have the value stored in RAM before clearing the irrelevant half, so that later you can load it back from RAM to compare the other half.
infidelity
Posts: 486
Joined: Fri Mar 01, 2013 4:46 am

Re: How can I perform AND two times with 2 separate register

Post by infidelity »

Thank you everyone for your help. :-)
Post Reply