Since this is a little Off Topic of How DIV works(Started by Sliver) thread....

----------------------------------------------------------------------

Also, there's one thing i've noticed.

``````
mov eax, 15
mov ecx, 4
div ecx

Output: 3.3
Correct Output: 3.75

Using 3.3 Isolate the remainder which is 3
3 x 10 = 30

30 - 4 = 26 -> 1  -----+
26 - 4 = 22 -> 2       |
22 - 4 = 18 -> 3       |
18 - 4 = 14 -> 4       +--> first division(n)
14 - 4 = 10 -> 5       |
10 - 4 = 6 -> 6        |
6 - 4 = 2 -> 7    -----+

Save 7, Last Value is 2
2 x 10 = 20

20 - 4 = 16 -> 1  -----+
16 - 4 = 12 -> 2       |
12 - 4 = 8 -> 3        +--> Second division(n)
8 - 4 = 4 -> 4         |
4 - 4 = 0 -> 5    -----+

isn't 7x10 + 5x1 == 75?(Our Remainder)
``````

In conclusion, Determining how many times we started dividing the remainder, which is 2 times, determines how we should multiply the MSD of the remainder, which is 7 up to the LSD of the remainder, which is 5. In contrast. If we have a third digit, we then have to multiply 7 by 100(7x100), then 5 by 10(7x10) and the third digit as (ThirdDigitx1).

In this equation 7*x, where x is the value of 10 to the power of n-1, Where n is in decreasing state starting from the number of times we divided the whole entire remainder(in this case 2).

E.G.

``````
N == 2;

7*x --> (x = 10 ^ n - 1)
--> (x = 10 ^ 2 - 1)
--> (x = 10 ^ 1)
--> (x = 10)

7*10 = 70

decrease N by 1;

N == 1

5*x --> (x = 10 ^ n - 1)
--> (x = 10 ^ 1 - 1)
--> (x = 10 ^ 0)
--> (x = 1)

5*1 = 5
``````

Adding both 70+5 == 75(Our remainder)

I explained this just in case you want another concept of dividing floating point values without having to convert to ascii, as what I have done.

For me, in ASM you have to do the reverse...
...do some div(sub)
push eax(7) increment counter(ecx)
...do some div(sub)
push eax(5) increment counter(ecx)
save ecx to memory...
ecx == 0

pop eax(5) increment counter(ecx)
5*x
- using the x formula. The value of n is ecx
- save to some register/mem.
- check if we have achieve the maximum(the value of ecx we save in memory)

pop eax(7) increment counter(ecx)
7*x
- using the x formula. The value of n is ecx
- Add the previous register/mem with this value(the one we save earlier)
- check if we have achieve the maximum(the value of ecx we save in memory)
...

Hope that helps!!!

:)
Posted on 2002-01-21 01:44:59 by stryker
The best way to do a software DIV is to use the bit search instructions to locate the largest amount of divisors that can be subtracted from the dividend. I don't have any source for that here but I'll maybe post some later.
Posted on 2002-01-25 08:52:03 by gliptic
There is some code in the other thread: ( go there )
Posted on 2002-01-25 10:02:15 by bitRAKE
Oh, I see that the code there uses the bsr instruction too. That means that I'm not the first one to think about that. Anyway, I'll post my Divide32, Multiply32 and Power32 procs tomorrow. You can maybe learn something from them.
Posted on 2002-01-28 03:38:26 by gliptic
umberg, did you clear edx before the division (eg. xor edx, edx)?
bcoz when you use a 32bit register (ecx in your example) as divisor it will divide edx:eax and not only eax..

bye
B-Boy
Posted on 2002-02-05 15:44:18 by B-Boy
You have to clear edx before starting to divide again...Thanks for pointing that out.

Actually this kind of algo doesn't work very well. I tested it a lot and found a flaw that's why I never posted an update on this one and never posted the code. It's better to use the fdiv or fidiv when doing division.

Flaw:

505 / 100 = 5.05 (Actual Answer)
505 / 100 = 5.5 (Using a simple DIV)
505 / 100 = 5.5 (Using DIV with the X formula plus the concept I presented)

653 / 7 = 93.285714285714285714285714285714 (Actual answer)
653 / 7 = 93.2 (Using a simple DIV)
653 / 7 = 93.28571 (Using DIV with the X formula plus the concept I presented.... edx can't contain 285714285714285714285714285714 so in my actual source code which I didn't post i limited it to 5 decimal places, but it works, it gives you the correct answer)

It doesn't work on certain numbers and not all the time. It's a long explanation, I'm sure you can pick up this one.

The concept without the x formula will work perfectly when you convert it to ascii.
Posted on 2002-02-05 17:01:19 by stryker