how to detect overflow with SUMC?
ManAtWork
Posts: 2,176
in Propeller 2
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 wcBTW, where are those constants defined that specify the actions for the MODC/Z instructions? Can't find them in the docs, again.
Comments
EDIT: 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.
This should be what you're looking for (untested):
I'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 signed overflow occurs (boundary $7FFFFFFF to $80000000 is crossed)
* if C is 1 then subtract S from D, set C if a signed underflow occurs (boundary $80000000 to $7FFFFFFF is 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.
Yes, TJV is what they're looking for.
Thanks for the solution