## DEC and flags.

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Joefish
Manic Miner
Posts: 533
Joined: Tue Nov 14, 2017 10:26 am

### DEC and flags.

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?
0 x

Ast A. Moore
Dynamite Dan
Posts: 1128
Joined: Mon Nov 13, 2017 3:16 pm

### Re: DEC and flags.

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.
2 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

Joefish
Manic Miner
Posts: 533
Joined: Tue Nov 14, 2017 10:26 am

### Re: DEC and flags.

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?
0 x

Ast A. Moore
Dynamite Dan
Posts: 1128
Joined: Mon Nov 13, 2017 3:16 pm

### Re: DEC and flags.

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.”
1 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

1024MAK
Manic Miner
Posts: 453
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

### Re: DEC and flags.

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
0 x

Ast A. Moore
Dynamite Dan
Posts: 1128
Joined: Mon Nov 13, 2017 3:16 pm

### Re: DEC and flags.

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.
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

Joefish
Manic Miner
Posts: 533
Joined: Tue Nov 14, 2017 10:26 am

### Re: DEC and flags.

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.
0 x

Metalbrain
Dizzy
Posts: 73
Joined: Thu Feb 15, 2018 2:14 pm

### Re: DEC and flags.

Joefish wrote:
Mon Jan 14, 2019 12:06 pm
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.
I'd say DAA is the only one?
0 x

AndyC
Manic Miner
Posts: 248
Joined: Mon Nov 13, 2017 5:12 am

### Re: DEC and flags.

Yes, the half carry is explicitly for DAA to work. Though obviously other instructions will modify it.
0 x