# how to detect overflow with SUMC?

ManAtWork
Posts:

**916**
The SUMC instruction is very useful if I want to add or subtract a value from a register based on the C flag. But how can I detect an over or underflow? I don't quite understand the stament "C = correct sign of (D +/- S)" in the docs.

What I like to do:

* if C is 0 then add S to D, set C if an overflow occurs (boundary $FFFFFFFF to $00000000 is crossed)

* if C is 1 then subtract S from D, set C if an underflow occurs (boundary $00000000 to $FFFFFFFF is crossed)

Is this possible with the SUMC instruction? Or do I have to split the operation into something like this?

What I like to do:

* if C is 0 then add S to D, set C if an overflow occurs (boundary $FFFFFFFF to $00000000 is crossed)

* if C is 1 then subtract S from D, set C if an underflow occurs (boundary $00000000 to $FFFFFFFF is crossed)

Is this possible with the SUMC instruction? Or do I have to split the operation into something like this?

```
MODZ ... ' put C into Z
if_z SUB D,S wc
if_nz ADD D,S wc
```

BTW, where are those constants defined that specify the actions for the MODC/Z instructions? Can't find them in the docs, again.
## Comments

10,230EDIT: Here's the original conversation - http://forums.parallax.com/discussion/167295/c-after-cmps-cmpsx-adds-addsx-subs-subsx/p1

The list is in instructions.txt in PNut zip, and now also added to end of main hardware google doc.

1,129This should be what you're looking for (untested):

10,230916I've just tested it. The idea is good but the results are a bit different:

* if C is 0 then add S to D, set C if a

signedoverflow occurs (boundary$7FFFFFFF to $80000000is crossed)* if C is 1 then subtract S from D, set C if a

signedunderflow occurs (boundary$80000000 to $7FFFFFFFis crossed)However, that is not a problem for me. I can just add an offset to D. The exact number where the C bit is set doesn't really matter. It's only important that it happens once per "revolution" and at the same point when turning backwards.

10,2301,6001,1291,60013,180Yes, TJV is what they're looking for.

916Thanks for the solution

1,60010,230