6502 "AND" Instruction
-
Joshua L. Tolles
- Posts: 5
- Joined: 26 Feb 2012
6502 "AND" Instruction
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
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
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)
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)
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):
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.
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
-
Joshua L. Tolles
- Posts: 5
- Joined: 26 Feb 2012
#$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?
#$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?
- GARTHWILSON
- Forum Moderator
- Posts: 8775
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
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, and make sure the "Disable BBCode in this post" box unchecked below the window where you write your post.
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 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?
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
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
And some more about AND...
It's useful when coding Boolean operations to write the operand in binary format. For example:
is usually easier to understand than:
or worse yet:
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.
Code: Select all
AND #%00010101Code: Select all
AND #$15Code: Select all
AND #21x86? We ain't got no x86. We don't NEED no stinking x86!
-
teamtempest
- Posts: 443
- Joined: 08 Nov 2009
- Location: Minnesota
- Contact:
Quote:
It's useful when coding Boolean operations to write the operand in binary format.
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.
- GARTHWILSON
- Forum Moderator
- Posts: 8775
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
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.
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?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
- BigDumbDinosaur
- Posts: 9428
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
teamtempest wrote:
Quote:
It's useful when coding Boolean operations to write the operand in binary format.
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.
Those doesn't cover every variation possible, of course, but maybe enough to be useful.
x86? We ain't got no x86. We don't NEED no stinking x86!
-
teamtempest
- Posts: 443
- Joined: 08 Nov 2009
- Location: Minnesota
- Contact:
-
Nightmaretony
- In Memoriam
- Posts: 618
- Joined: 27 Jun 2003
- Location: Meadowbrook
- Contact:
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
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