Divide by 10
GAF
Posts: 25
In the September issue if Nuts & Volts there was an article on floating point math on a 8-bit·chip.· Using what the author outlined I came up with a simple routine to divide a byte by 10 and determine the remainder.· This routine only involves simple shift and add instructions. In the following code X is the byte that will be divided by 10, tmp1 and tmp2 are just temporary variables. The "Result" contains the answer to the division.
tmp1 = X>>2
tmp1 = tmp1 + X
tmp2 = tmp1>>1
tmp2 = tmp2 + X
Result= tmp2>>4
tmp1 = Result<<2
tmp1 = tmp1 + Result
tmp2 = tmp1 << 1
Remainder = X - tmp2
I have used this routine in a couple of my SX programs and it seems to work fine.
tmp1 = X>>2
tmp1 = tmp1 + X
tmp2 = tmp1>>1
tmp2 = tmp2 + X
Result= tmp2>>4
tmp1 = Result<<2
tmp1 = tmp1 + Result
tmp2 = tmp1 << 1
Remainder = X - tmp2
I have used this routine in a couple of my SX programs and it seems to work fine.
Comments
Here is what I suggest for a fast divide by 10 with remainder:
This is basicly and "unrolled" version of the normal division code.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The first rule to being successful is "Learn from your mistakes",
The second rule is "Be willing to make mistakes"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
Post Edited (Bean (Hitt Consulting)) : 9/9/2007 1:05:31 PM GMT