It is currently Mon Oct 22, 2018 11:35 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Mar 22, 2012 8:09 am 
Offline
User avatar

Joined: Tue Feb 13, 2007 9:02 pm
Posts: 147
Location: Richmond, VA
A quick elementary assembly question I'm puzzling over:

Code:
LDY #$04
CPY #$04
BPL label


When the compare results are equal, does the N-flag remain 0 and thus the branch is taken?

Now, a similar question with a two's complement twist:

Code:
LDY #$84
CPY #$04
BPL label


Am I correct that the result of the CPY ($80) is -128 in two's complement and the branch will be skipped?

Finally, a slightly related question on absolute indexed addressing:

Code:
LDY #$80
LDA DATA, Y


Is Y treated as a positive or negative index? In other words, does indexing ignore two's complement?


Top
 Profile  
 
PostPosted: Thu Mar 22, 2012 9:43 am 
Offline

Joined: Sun Apr 13, 2008 11:12 am
Posts: 7679
Location: Seattle
noattack wrote:
A quick elementary assembly question I'm puzzling over:
Code:
LDY #$04
CPY #$04
BPL label
When the compare results are equal, does the N-flag remain 0 and thus the branch is taken?
4 - 4 = 0; bit seven remains clear so N is 0, BPL takes the branch
Quote:
Now, a similar question with a two's complement twist:
Code:
LDY #$84
CPY #$04
BPL label
Am I correct that the result of the CPY ($80) is -128 in two's complement and the branch will be skipped?
The CPU doesn't really do signed math for you.

0x84 = 132; 132-4 = 128; bit seven is set so N is set so the BPL doesn't follow.
Quote:
Finally, a slightly related question on absolute indexed addressing:
Code:
LDY #$80
LDA DATA, Y
Is Y treated as a positive or negative index? In other words, does indexing ignore two's complement?
Indices are unsigned. It's not so much that it just "ignores" two's complement here (although it does), it's that on the 6502 it barely supports signed math at all. (relative addresses for branches and the N bit are it)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 22, 2012 9:50 am 
Offline
User avatar

Joined: Tue Feb 13, 2007 9:02 pm
Posts: 147
Location: Richmond, VA
Thanks for the answers. I guess I was over-thinking the CPU's 'perception' of negative values. It has no sense of positive or negative - it just does or does not set the flag.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 22, 2012 11:09 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20685
Location: NE Indiana, USA (NTSC)
The only places I can think of where the 6502 treats a value with its high bit set as a negative number are A. branch offsets, and B. setting the V flag in an ADC or SBC. Otherwise, the sign flag (bit 7 of P) just refers to bit 7 of the previous result.


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

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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:  
Powered by phpBB® Forum Software © phpBB Group