add hl, xx does affect the carry flag - just not the zero flag. The carry is fine, however, because if the sbc caused a carry, then add will also cause one. eg:Timendus wrote:So, the subtraction sbc hl,bc sets all the flags right and add hl,bc restores de without screwing up the flags?
1 - 3 = -2 with carry.
-2 + 3 = 1 with carry.
As well as affecting the carry, sbc will also subtract an extra 1 if the carry flag is set when the instruction is executed (that's where it gets its name from - subtract with carry). Example:That sounds pretty good. In fact, I can probably just throw one value away after the comparison, and just doIs that or a really necessary, by the way? Doesn't sbc take care of the carry flag, or does it only set and not reset it?Code: Select all
ex de,hl or a sbc hl,bc ex de,hl
Code: Select all
ld hl, 6
ld de, 5
scf ; set carry flag
sbc hl, de ; hl will contain 0 (6 - 5 - 1)
ld hl, 6
ld de, 5
or a ; zero carry
sbc hl, de ; hl will contain 1 (6 - 5 - 0)