Shop OBEX P1 Docs P2 Docs Learn Events
Signed Byte Addition: preventing overflow/underflow?? — Parallax Forums

Signed Byte Addition: preventing overflow/underflow??

ZootZoot Posts: 2,227
edited 2008-08-19 13:49 in General Discussion
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?

' 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

  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2008-08-19 00:52
    Zoot,

    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
  • ZootZoot Posts: 2,227
    edited 2008-08-19 02:27
    Zoot said...
    signed byte (-127 to 127 )

    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:

    x = some byte value from 0 to 255
    y = some byte value from -127 to 127
    z = result
    
    z = x + y
    if ( z < 0 ) {
      z = 0
    } elseif ( z > 255 ) {
      z = 255
    }
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • BeanBean Posts: 8,129
    edited 2008-08-19 11:47
    Zoot,
    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

    ·
  • ZootZoot Posts: 2,227
    edited 2008-08-19 13:49
    Yah, well, I didn't think I was reinventing the wheel.... smile.gif

    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
Sign In or Register to comment.