ASM division
tom90
Posts: 55
Hey All,
Some of you helped me some time ago with some simple ASM code for division.· It works just fine when I am dividing by a small number (25), but now I am trying to divide by a larger number (1135) and this code doesn't seem to be working correctly.· The main problem is that I still dont completely understand how this code is working.· Can anybody give any insight into how this code works, and possibly how to get it working with 1135 as the divisor (x).·
Thanks
Tom
Some of you helped me some time ago with some simple ASM code for division.· It works just fine when I am dividing by a small number (25), but now I am trying to divide by a larger number (1135) and this code doesn't seem to be working correctly.· The main problem is that I still dont completely understand how this code is working.· Can anybody give any insight into how this code works, and possibly how to get it working with 1135 as the divisor (x).·
mov y,"x" 'divide by "x" 'get divisor into y[noparse][[/noparse]30..15] *****I want x = 1135******* shl y,#15 mov divcounter,#16 'ready for 16 quotient bit :divloop cmpsub getsdivided,y wc 'if y =< x then subtract it, quotient bit into c rcl getsdivided,#1 'rotate c into quotient, shift dividend djnz divcounter,#:divloop 'loop until done
Thanks
Tom
Comments
You'll need to allocate a long to x elsewhere in the cog and preset it to 1135
if( getsDivided <= (divisor << 15) )
· getsDivided -= (divisor << 15)
· result += (1 << 15)
if( getsDivided <= (divisor << 14) )
· getsDivided -= (divisor << 14)
· result += (1 << 14)
...and so on, down to 1.· It starts by shifting the divisor up by only 15 bits, which means that you couldn't divide 16 million by 2, because the 2 wouldn't get shifted up enough to start the division properly.
Another, possibly better way to do it would be to start it by figuring out where the first 'on' bit of the divisor is, like this:
int initialShift = 31
while( (divisor OR initialShift) <> initialShift )
· initialShift--
Then use the resulting initialShift instead of '15', and change the loop counter to initialShift+1.· Does that make sense?
Jason
I just realized that I had my two values switched around when I was trying to calculate it out by hand.
Thanks Again
Tom