ADC Instruction and BCD

For discussing the 65xx hardware itself or electronics projects.
Post Reply
andrewem
Posts: 17
Joined: 21 May 2004
Location: Burlington, ON

ADC Instruction and BCD

Post by andrewem »

Hi,

I read that in when in decimal mode the Zero Flag is invalid. What does this mean? In other words, does it mean that the Zero Flag is not updated?

Thanks,

Andrew
User avatar
dclxvi
Posts: 362
Joined: 11 Mar 2004

Re: ADC Instruction and BCD

Post by dclxvi »

andrewem wrote:
I read that in when in decimal mode the Zero Flag is invalid. What does this mean? In other words, does it mean that the Zero Flag is not updated?
No, the Z flag will always be affected by a ADC or SBC instruction. Invalid means that the Z flag does not always correctly reflect the result in the accumulator. For example, after:

Code: Select all

SED
CLC
LDA #$99
ADC #$01
the accumulator is $00, but the Z flag is clear.

On the other hand, after:

Code: Select all

SED
SEC
LDA #$01
SBC #$01
the accumulator is $00, but the Z flag is set. So the Z flag is sometimes correct, sometimes not.

The Z flag is only invalid on the 6502. On the 65C02 and the 65816, the Z flag correctly reflect the accumulator contents.

On the 6502, if you want to predict the value of the Z flag after an ADC or SBC, just add or subtract the numbers (accounting for the carry) as though they were binary number rather than BCD numbers. In other words, after:

Code: Select all

SED
ADC NUMBER
the Z flag will be the same as after:

Code: Select all

CLD
ADC NUMBER
assuming, of course, that upon entry, the accumulator and carry were the same. The same is true of SBC.
andrewem
Posts: 17
Joined: 21 May 2004
Location: Burlington, ON

Post by andrewem »

Thanks for the clear response :D.

Andrew
Post Reply