Shop OBEX P1 Docs P2 Docs Learn Events
SXB 2.0 16bit add/sub and overflow/underflow — Parallax Forums

SXB 2.0 16bit add/sub and overflow/underflow

Peter VerkaikPeter Verkaik Posts: 3,956
edited 2009-04-04 01:34 in General Discussion
@Bean,

Adding 2 words generates

· 5488· 01B8· 0219······· ADD dacValue_MSB,adcValue_MSB· ;dacValue = dacValue + adcValue
······· 01B9· 01F7
· 5489· 01BA· 0218······· ADD dacValue_LSB,adcValue_LSB
······· 01BB· 01F6
· 5490· 01BC· 0603······· ADDB dacValue_MSB,C··········
······· 01BD· 02B7

If adding the MSB parts generates overflow (C set) but adding the LSB parts does not,
then the overflow is not detected.

If the code was changed to

··mov·w,adcValue_LSB
··add·dacValue_LSB,w
··mov·w,adcValue_MSB
··snc
··movsz·w,++adcValue_MSB·;leaves C intact
··add·dacValue_MSB,w

then we can detect 16bit overflow by testing carry.
This takes the same amount of code.
(code was found on sxlist.com)

regards peter

Post Edited (Peter Verkaik) : 4/2/2009 7:51:05 PM GMT

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-04-02 19:46
    For subtracting this is generated

    · 5488· 01B8· 0218······· SUB dacValue_LSB,adcValue_LSB· ;dacValue = dacValue - adcValue
    ······· 01B9· 00B6
    · 5489· 01BA· 0703······· SUBB dacValue_MSB,/C·········
    ······· 01BB· 00F7
    · 5490· 01BC· 0219······· SUB dacValue_MSB,adcValue_MSB

    But again underflow may be missed.

    Alternative code from sxlist.com
    ··mov W, adcValue_LSB
    · sub dacValue_LSB,w
    · mov w,adcValue_MSB
    · sc
    · movsz w,++adcValue_MSB
    · sub dacValue_MSB,w ;· dest=dest-source, with valid carry (although the Z flag is not valid).

    regards peter
  • pjvpjv Posts: 1,903
    edited 2009-04-03 00:13
    Hi Peter;

    I will have to check again, but I use the same code, and I believe that Bean has the sequence correct, that is to say adding the 'lsb, then dealing with any carry out, and then adding the 'msb.

    Cheers,

    Peter (pjv)
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-04-03 01:02
    Hi pjv,

    I think I made an error regarding the subtraction. The sx clears the C for underflow
    and sets C when there is no underflow. So the subtract sequence appears right.

    But the addition fails

    dacValue = $FF00
    adcValue = $0100
    dacValue + adcValue = $0000 with C set

    Using the sxb sequence
    add the MSB yields 0 with C set
    add the LSB yields 0 with C clear
    so the ADDB dacValue_MSB,C leaves C clear

    regards peter

    PS. Have you seen my latest reply on the IFDEF issue.
    I thought you were looking for a macro to autocreate a jumptable.
    http://forums.parallax.com/showthread.php?p=795495
  • pjvpjv Posts: 1,903
    edited 2009-04-03 15:56
    Hi Peter;

    On confirming this, you are absolutely correct in the case of adding two words together. Subtracting two words is done correctly. Adding or subtracting a byte to a word are also both done correctly. In the case of adding two words, Bean simply has the order backwards, the '_lsb needs to be added and "carry corrected"·before adding the '_msb.

    Here is the compiled results on adding/subtracting two words as well as a word with a byte:
     
       342  0000  0215        ADD WordOne_MSB,WordTwo_MSB    ;WordOne = WordOne + WordTwo
            0001  01F5
       343  0002  0214        ADD WordOne_LSB,WordTwo_LSB   
            0003  01F4
       344  0004  0603        ADDB WordOne_MSB,C            
            0005  02B5
       345                  
       346  0006  0214        SUB WordOne_LSB,WordTwo_LSB    ;WordOne = WordOne - WordTwo
            0007  00B4
       347  0008  0703        SUBB WordOne_MSB,/C           
            0009  00F5
       348  000A  0215        SUB WordOne_MSB,WordTwo_MSB   
            000B  00B5
       349                  
       350  000C  021C        ADD WordOne_LSB,ByteOne        ;WordOne = WordOne + ByteOne
            000D  01F4
       351  000E  0603        ADDB WordOne_MSB,C            
            000F  02B5
       352                  
       353  0010  021C        SUB WordOne_LSB,ByteOne        ;WordOne = WordOne - ByteOne
            0011  00B4
       354  0012  0703        SUBB WordOne_MSB,/C           
            0013  00F5
    


    Regarding the IFDEF, I have not had the time recently to mess with that, but I hope to look into it soon.

    Thanks for your interest.

    Cheers,

    Peter (pjv)







    Post Edited (pjv) : 4/3/2009 4:01:27 PM GMT
  • NetHogNetHog Posts: 104
    edited 2009-04-04 01:34
    One frustrating thing about add/sub with addb/subb is that you lose the final carry/borrow and cannot check for overflow, regardless of which way you go about it.

    Last thing I compiled with SXB/2.0, the compiler·turned on CARRYX, which allows overflow checking.

    Without CARRYX, you have to take a very convoluted approach.


    Post Edited (NetHog) : 4/4/2009 1:39:38 AM GMT
Sign In or Register to comment.