Page 1 of 1

6502 "AND" Instruction

Posted: Sun Feb 26, 2012 7:00 pm
by Joshua L. Tolles
I am desperately trying to figure out what the AND instruction means in 6502. I have found half a dozen or more resources online, but they all describe AND as being AND.

CMP means Compare Memory and Accumulator

That is a good definition, I know what CMP means now.

AND means AND

What kind of definition is that? I just don't get it.

I have been beating my head against the wall for days, and am really stressed out about it.

Here is a string of instructions that I could understand if the AND was a CMP or something, but what is the AND doing there?

$89B2:A5 B6 LDA $00B6 = #$10
$89B4:29 04 AND #$04
$89B6:D0 51 BNE $8A09
$89B8:A5 B5 LDA $00B5 = #$10
$89BA:29 03 AND #$03
$89BC:D0 15 BNE $89D3
$89BE:A5 B6 LDA $00B6 = #$10
$89C0:29 03 AND #$03
$89C2:F0 45 BEQ $8A09

Posted: Sun Feb 26, 2012 7:06 pm
by fachat
AND in 6502 is a bitwise AND of the operand with the accumulator, and putting the result back in the Accumulator.

André

Edit: I think what confuses here is that all arithmetic and logic operations in the 6502 set the processor flags. So and AND of #$10 and #$04 is zero, so the zero flag is set, and the BNE branch is not taken (BNE = Branch Not Equal)

Posted: Sun Feb 26, 2012 7:23 pm
by BigEd
You can think of AND as masking out one or more bits of interest (consider the operands as binary rather than hex, and for each bit position put a 1 in the result if both the operands have a 1 in that position):

Code: Select all

LDA #$10  ;; 0001 0000
AND #$04  ;; 0000 0100
;; result    0000 0000

LDA #$3f  ;; 0011 1111    
AND #$04  ;; 0000 0100
;; result    0000 0100 
Now you see that comparing the result to zero using BNE or BEQ is the same as checking the value of the original bit in the third position from the right.

Posted: Sun Feb 26, 2012 8:13 pm
by Joshua L. Tolles
#$01 ;; 0000 0001
#$03 ;; 0000 0011
;;result 0000 0001

#$04 ;; 0000 0100
#$03 ;; 0000 0011
;; result 0000 0000




so in this scenario, the branch is taken:

$89B8:A5 B5 LDA $00B5 = #$01
$89BA:29 03 AND #$03
$89BC:D0 15 BNE $89D3



but here it is not:

$89B8:A5 B5 LDA $00B5 = #$04
$89BA:29 03 AND #$03
$89BC:D0 15 BNE $89D3

....because the AND of $01 and $03 is not zero, but an AND of $04 and $03 is.




Furthermore:

in this scenario, the branch is not taken

$89BE:A5 B6 LDA $00B6 = #$01
$89C0:29 03 AND #$03
$89C2:F0 45 BEQ $8A09




in this scenario, the branch is taken

$89BE:A5 B6 LDA $00B6 = #$04
$89C0:29 03 AND #$03
$89C2:F0 45 BEQ $8A09

.

Am I on the right track?

Posted: Sun Feb 26, 2012 8:17 pm
by GARTHWILSON
Yep.

And to get the forum software to give you monospacing and quit throwing out the spaces added for vertical alignment, put the code fragment between

Code: Select all

 and 
, and make sure the "Disable BBCode in this post" box unchecked below the window where you write your post.

Posted: Sun Feb 26, 2012 10:03 pm
by Joshua L. Tolles
Thank you. I can't express how relieved I am to have it figured out now. :D

And some more about AND...

Posted: Sun Feb 26, 2012 10:54 pm
by BigDumbDinosaur
It's useful when coding Boolean operations to write the operand in binary format. For example:

Code: Select all

AND #%00010101
is usually easier to understand than:

Code: Select all

AND #$15
or worse yet:

Code: Select all

AND #21
The first example clearly shows which bits are acting as the mask value for AND. The others require the person reading the code to mentally translate.

Posted: Mon Feb 27, 2012 5:49 am
by teamtempest
Quote:
It's useful when coding Boolean operations to write the operand in binary format.
Indeed it is. To my eye the original code fragment posted looked more like a disassembly than a source, though. Which caused me to wonder, are there any disassemblers which render the operands of immediate mode bitwise instructions (AND, EOR, ORA) in binary rather than hex?

No stopping there! A slightly smarter disassembler might take note of any immediate mode LDA instruction and hold it for a moment in case the following instruction is any of AND, BIT, EOR or ORA. If so, render the LDA operand as binary, otherwise hex.

Those doesn't cover every variation possible, of course, but maybe enough to be useful.

Posted: Mon Feb 27, 2012 6:16 am
by GARTHWILSON
Joshua, on a related note, make sure you check out the sometimes-overlooked BIT instruction too. You can for example test several things in a row against a pattern without affecting the accumulator every time. If you only need to test bit 6 and/or 7, you can ignore the accumulator altogether and just do BIT <operand> then follow it immediately with a branch on the N or V flag, without affecting A, X, or Y, and without using AND.

The 65C02 (ie, CMOS, not NMOS) has the BBS (branch on bit set in a ZP location) and BBR (branch on bit reset in a ZP location) instructions also, so a single instruction reads the contents of the address, tests the desired bit (any bit), and, on the desired condition, branches the desired distance and direction, all in one instruction, again without affecting the registers. The Rockwell 65C02 had this, and all the WDC ones made in the last dozen+ years do too. GTE (CMD), Synertek, and others did not have this.

Posted: Tue Feb 28, 2012 3:17 am
by BigDumbDinosaur
teamtempest wrote:
Quote:
It's useful when coding Boolean operations to write the operand in binary format.
Indeed it is. To my eye the original code fragment posted looked more like a disassembly than a source, though. Which caused me to wonder, are there any disassemblers which render the operands of immediate mode bitwise instructions (AND, EOR, ORA) in binary rather than hex?
I contemplated doing so with the disassembler in my POC's BIOS ROM. However, by the time I stuffed the SCSI driver in there I was almost out of room.
Quote:
No stopping there! A slightly smarter disassembler might take note of any immediate mode LDA instruction and hold it for a moment in case the following instruction is any of AND, BIT, EOR or ORA. If so, render the LDA operand as binary, otherwise hex.

Those doesn't cover every variation possible, of course, but maybe enough to be useful.
Don't forget BIT immediate! The poor guy will feel left out. :lol:

Posted: Tue Feb 28, 2012 3:42 am
by teamtempest
Quote:
Don't forget BIT immediate! The poor guy will feel left out.
Aw, I didn't forget so much as contemplated for a split second the differences between the 6502 and the 65C02 and decided that's even further off topic :wink:

Posted: Tue Feb 28, 2012 4:29 pm
by Nightmaretony
back to the AND, it is merely that a result is true if the inputs are true. So like this:

C = A AND B

A B C
0 0 0
0 1 0
1 0 0
1 1 1

notice how C is 1 only when A AND B are 1....

Posted: Wed Feb 29, 2012 10:15 am
by Tor
Boolean logic. Good old truth tables! :-)
That was the first part of the course in digital electronics at school.
Here's a reasonably simple page explaining it (and/or/xor/not/ etc.)
http://computer.howstuffworks.com/boolean1.htm

-Tor