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?
DEC and flags.
- Ast A. Moore
- Rick Dangerous
- Posts: 2641
- Joined: Mon Nov 13, 2017 3:16 pm
Re: DEC and flags.
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.
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.
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.
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?
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?
- Ast A. Moore
- Rick Dangerous
- Posts: 2641
- Joined: Mon Nov 13, 2017 3:16 pm
Re: DEC and flags.
I believe the Zilog manual does explain it:
And the part about the INC/DEC instructions is pretty straightforward: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.
INC r
. . .
Condition Bits Affected
. . .
P/V is set if r was 7Fh 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.”DEC m
. . .
Condition Bits Affected
. . .
P/V is set if m was 80h before operation; otherwise, it is reset.
. . .
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.
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
- Bugaboo
- Posts: 3125
- 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
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
Standby alert
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Looking forward to summer later in the year.
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Looking forward to summer later in the year.
- Ast A. Moore
- Rick Dangerous
- Posts: 2641
- Joined: Mon Nov 13, 2017 3:16 pm
Re: DEC and flags.
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.
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.
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.
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.
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.
- Metalbrain
- Microbot
- Posts: 107
- Joined: Thu Feb 15, 2018 2:14 pm
Re: DEC and flags.
Yes, the half carry is explicitly for DAA to work. Though obviously other instructions will modify it.