6502 "AND" Instruction

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
Joshua L. Tolles
Posts: 5
Joined: 26 Feb 2012

6502 "AND" Instruction

Post 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
fachat
Posts: 1124
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Post 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)
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Post 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.
Joshua L. Tolles
Posts: 5
Joined: 26 Feb 2012

Post 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?
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Post 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.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Joshua L. Tolles
Posts: 5
Joined: 26 Feb 2012

Post by Joshua L. Tolles »

Thank you. I can't express how relieved I am to have it figured out now. :D
User avatar
BigDumbDinosaur
Posts: 9425
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

And some more about AND...

Post 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.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
teamtempest
Posts: 443
Joined: 08 Nov 2009
Location: Minnesota
Contact:

Post 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.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Post 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.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
User avatar
BigDumbDinosaur
Posts: 9425
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Post 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:
x86?  We ain't got no x86.  We don't NEED no stinking x86!
teamtempest
Posts: 443
Joined: 08 Nov 2009
Location: Minnesota
Contact:

Post 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:
Nightmaretony
In Memoriam
Posts: 618
Joined: 27 Jun 2003
Location: Meadowbrook
Contact:

Post 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....
"My biggest dream in life? Building black plywood Habitrails"
Tor
Posts: 597
Joined: 10 Apr 2011
Location: Norway/Japan

Post 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
Post Reply