6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Apr 27, 2024 8:34 am

All times are UTC




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: 6502 "AND" Instruction
PostPosted: Sun Feb 26, 2012 7:00 pm 
Offline

Joined: Sun Feb 26, 2012 6:48 pm
Posts: 5
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 26, 2012 7:06 pm 
Offline

Joined: Tue Jul 05, 2005 7:08 pm
Posts: 990
Location: near Heidelberg, Germany
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)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 26, 2012 7:23 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
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:
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 26, 2012 8:13 pm 
Offline

Joined: Sun Feb 26, 2012 6:48 pm
Posts: 5
#$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?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 26, 2012 8:17 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
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] and [/code], 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?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 26, 2012 10:03 pm 
Offline

Joined: Sun Feb 26, 2012 6:48 pm
Posts: 5
Thank you. I can't express how relieved I am to have it figured out now. :D


Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 26, 2012 10:54 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8144
Location: Midwestern USA
It's useful when coding Boolean operations to write the operand in binary format. For example:
Code:
AND #%00010101

is usually easier to understand than:
Code:
AND #$15

or worse yet:
Code:
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!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Feb 27, 2012 5:49 am 
Offline

Joined: Sun Nov 08, 2009 1:56 am
Posts: 387
Location: Minnesota
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Feb 27, 2012 6:16 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
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?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Feb 28, 2012 3:17 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8144
Location: Midwestern USA
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!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Feb 28, 2012 3:42 am 
Offline

Joined: Sun Nov 08, 2009 1:56 am
Posts: 387
Location: Minnesota
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:


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Feb 28, 2012 4:29 pm 
Offline

Joined: Fri Jun 27, 2003 8:12 am
Posts: 618
Location: Meadowbrook
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"


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 29, 2012 10:15 am 
Offline

Joined: Sun Apr 10, 2011 8:29 am
Posts: 597
Location: Norway/Japan
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: