Author Topic: Why does inc/dec not affect carry flag?  (Read 5676 times)

0 Members and 1 Guest are viewing this topic.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Why does inc/dec not affect carry flag?
« on: May 06, 2011, 03:36:52 pm »
If Zilog had wanted to, they could have made inc reg8 and dec reg8 affect the carry flag on teh Z80 processor.  So I'm assuming there's a good reason they chose not to do so.  Does anyone know the reason?

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Why does inc/dec not affect carry flag?
« Reply #1 on: May 06, 2011, 03:56:16 pm »
They don't affect the carry flag because the zero flag does pretty much the same thing, allowing data in the carry flag to be preserved. For inc reg8, the zero flag is affected in the exact way the carry flag would be affected, both of which would be set if the register was 0xFF before the operation. For dec reg8 it's not exactly the same, but it's very close. The zero flag would be set if the register was 0x01 whereas the carry flag would be set if the register was 0x00, but this can be easily corrected for by keeping the register's value at 1 above the value you actually want it to represent.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Why does inc/dec not affect carry flag?
« Reply #2 on: May 06, 2011, 04:07:45 pm »
So the reason, then, is to preserve the carry flag?

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Why does inc/dec not affect carry flag?
« Reply #3 on: May 06, 2011, 04:12:08 pm »
Yes. There's no reason to affect the carry flag if you can look to another flag for (nearly) identical results. I've seen a few routines that rely on this lack of carry flag affecting to function optimally.

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Why does inc/dec not affect carry flag?
« Reply #4 on: May 06, 2011, 08:03:06 pm »
I wonder why DEC HL does not affect any flags, though.
ld a, 0
ld a, a

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Why does inc/dec not affect carry flag?
« Reply #5 on: May 06, 2011, 08:05:43 pm »
The inc reg16 and dec reg16 instructions don't affect flags because they are very often used to simply advance a pointer in data as part of a routine that needs the flags for other information. Also, it's simple enough to make your own version that affects flags by using 8-bit inc or dec instead.

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Why does inc/dec not affect carry flag?
« Reply #6 on: May 06, 2011, 08:06:14 pm »
DEC HL does not affect any flags, not even the zero flag.  I can see why you don't want to affect the carry flag, but DEC reg16 doesn't change flags at all. 

EDIT: Ninja'd
EDIT2:
Also, it's simple enough to make your own version that affects flags by using 8-bit inc or dec instead.
Yeah, I guess.  It would be nice to do that in one instruction, though.  :\
« Last Edit: May 06, 2011, 08:09:20 pm by ralphdspam »
ld a, 0
ld a, a