CPY, BPL, and absolute indexed addressing questions

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
CPY, BPL, and absolute indexed addressing questions
by on (#91696)
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?
Re: CPY, BPL, and absolute indexed addressing questions
by on (#91700)
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)

by on (#91701)
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.

by on (#91702)
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.