Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: Hot_Dog on May 06, 2011, 03:36:52 pm

Title: Why does inc/dec not affect carry flag?
Post by: Hot_Dog 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?
Title: Re: Why does inc/dec not affect carry flag?
Post by: Runer112 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.
Title: Re: Why does inc/dec not affect carry flag?
Post by: Hot_Dog on May 06, 2011, 04:07:45 pm
So the reason, then, is to preserve the carry flag?
Title: Re: Why does inc/dec not affect carry flag?
Post by: Runer112 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.
Title: Re: Why does inc/dec not affect carry flag?
Post by: ralphdspam on May 06, 2011, 08:03:06 pm
I wonder why DEC HL does not affect any flags, though.
Title: Re: Why does inc/dec not affect carry flag?
Post by: Runer112 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.
Title: Re: Why does inc/dec not affect carry flag?
Post by: ralphdspam 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.  :\