SXB 2.0 16bit add/sub and overflow/underflow
Peter Verkaik
Posts: 3,956
@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
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
· 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
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)
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
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:
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
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