Signed Byte Addition: preventing overflow/underflow??
![Zoot](https://forums.parallax.com/uploads/userpics/559/nW4UO23UEIWZ4.png)
I'm looking for a *real* tidy way to add a signed byte (-127 to 127 ) to another byte (the result) and prevent the result from going below zero (if the signed byte is negative) and prevent the result from going over 255 if signed byte is positive.
Would something like this work? or not? Is there something better?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
Would something like this work? or not? Is there something better?
' param2 is byte (and result) ' param1 is signed byte do_it: ADD __PARAM2, __PARAM1 ' add it and check for overflow, underflow JB __PARAM1.7, @negative positive: MOV W, #255 SNC MOV __PARAM2, W JMP @:done negative: SC CLR __PARAM2 :done
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
Comments
Will you clarify whether or not the second byte is considered to be signed before the addition? If you consider it to be unsigned can it be greater than 127 before the addition?
- Sparks
First "param" is unsigned byte. Second "param" is signed byte, as above. Result is unsigned.
FYI -- the code above seems to works (I'm already using it), but that's on an initial PID motor control system that I haven't pushed to it's limits yet.
Just figured there might be a more elegant way.
Since I'm tired today and not sure I'm explaining it decently, here's pseudo code to explain:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
The code I wrote awhile ago is the same as your original code (scary ain't it).
Except that I used a "STC" instead of "JMP @[noparse]:D[/noparse]one".
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson
"It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter
www.iElectronicDesigns.com
·
The STC is clever. I'm trying to think more like that now that I'm doing lots more assembly.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php