Page 1 of 1

DEC and flags.

Posted: Sat Jan 12, 2019 4:50 pm
by Joefish
If you have 0 in A and you SUBtract 1, you get the carry flag set as it goes to 255 or -1.
But if you DEC A you don't. According to the Z80 guide it should set Parity/Overflow flag so then you could detect it and branch with JP PE (i.e. Parity Even, or P/V flag=1).
But it doesn't seem to work right for me. What am I missing?

Re: DEC and flags.

Posted: Sat Jan 12, 2019 5:14 pm
by Ast A. Moore
Joefish wrote: Sat Jan 12, 2019 4:50 pm What am I missing?
The fact that the overflow is meant for testing 7-bit values, unlike the carry, which indicates an “overflow” of 8-bit values. Change your value to 128 and decrement it. The overflow flag will be set.

Re: DEC and flags.

Posted: Sat Jan 12, 2019 7:35 pm
by Joefish
Thanks.
So it gets set if you go one beyond the lowest negative value in signed arithmetic?
Why do none of the manuals bother to explain that?

Re: DEC and flags.

Posted: Sat Jan 12, 2019 8:21 pm
by Ast A. Moore
I believe the Zilog manual does explain it:
The Parity/Overflow (P/V) Flag is set to a specific state depending on the operation being performed. For arithmetic operations, this flag indicates an overflow condition when the result in the Accumulator is greater than the maximum possible number (+127) or is less than the minimum possible number (–128). This overflow condition is determined by examining the sign bits of the operands.
And the part about the INC/DEC instructions is pretty straightforward:
INC r

. . .

Condition Bits Affected
. . .
P/V is set if r was 7Fh before operation; otherwise, it is reset.
. . .
DEC m

. . .

Condition Bits Affected
. . .
P/V is set if m was 80h before operation; otherwise, it is reset.
. . .
I just kind of always thought of the P/V flag as “a carry for 7-bit numbers.” Mnemonically, it makes sense. You could also think of the carry as “an overflow for 8-bit numbers.” ;)

Re: DEC and flags.

Posted: Sat Jan 12, 2019 11:37 pm
by 1024MAK
I though that it was fairly standard that the carry flag is treated as an overflow bit for carry / borrow when an operation exceeds a value that can be represented as an 8 bit value.

I’m always double checking my Z80 code, as the Z80 includes both 8080 compatible instructions plus the extra operations that Zilog added. And it’s often slightly different to say 6502 code.

Mark

Re: DEC and flags.

Posted: Sun Jan 13, 2019 12:45 am
by Ast A. Moore
1024MAK wrote: Sat Jan 12, 2019 11:37 pm I though that it was fairly standard that the carry flag is treated as an overflow bit for carry / borrow when an operation exceeds a value that can be represented as an 8 bit value.
I think it depends on how one originally learned how specific ALUs and flags work. Carry is pretty straightforward, while signed numbers can be somewhat murky. It’s easy to become confused by two “overflow” flags, especially since one of them also keeps tabs on parity.

Re: DEC and flags.

Posted: Mon Jan 14, 2019 12:06 pm
by Joefish
Well, the reason I was looking is having done some 68000 again, there you tend to set the loop counter one lower, so for 20 loops you set it to 19 and on the last pass through the loop it holds 0 (which can actually be useful), then you detect the loop end when the decrement carries/overflows, rather than when it reaches 0.

Though I suppose I can use the SIGN flag as DEC appears to affect that (JP M if minus, JP P if zero/positive) if my counter is 127 or less..?

Curious that there's a half-carry flag (carry between the nybbles of a byte), but no instruction or condition that seems to act on it.

Re: DEC and flags.

Posted: Mon Jan 14, 2019 12:28 pm
by Metalbrain
Joefish wrote: Mon Jan 14, 2019 12:06 pmCurious that there's a half-carry flag (carry between the nybbles of a byte), but no instruction or condition that seems to act on it.
I'd say DAA is the only one?

Re: DEC and flags.

Posted: Mon Jan 14, 2019 5:44 pm
by AndyC
Yes, the half carry is explicitly for DAA to work. Though obviously other instructions will modify it.