I HATE DIV!!!

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Peter_Vigren

I HATE DIV!!!

Post by Peter_Vigren »

Scenario:
I tries to divide 1536 with 1024. The result should be that Ax will contain 1 and Dx 5. However, Dx contains 512 when I tested it. Then I divided 1792 with 1024 and got 1 in Ax and 768 in Dx...
It seems to me that the CPU (by some insane reason) multiply the remainder with 1024 because 0.5 multiplied with 1024 is 512 and 0.75 multiplied with 1024 is 768... Is this correct??

Now I wonder, HOW ON EARTH DO I DO TO DIVIDE 1536 AND GET 1 IN AX AND 5 IN DX???

Best regards,
Peter Vigren
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re: I HATE DIV!!!

Post by df »

Scenario:
I tries to divide 1536 with 1024. The result should be that Ax will contain 1 and Dx 5. However, Dx contains 512 when I tested it. Then I divided 1792 with 1024 and got 1 in Ax and 768 in Dx...
those numbers are fine!

mov eax,1536
mov ebx,1024
xor edx,edx
idiv ebx

eax = 1
edx = 512

which is correct.

edx wont be 5.. (1536-1024 does not have a remainder of 5) (1536/1024 = 1.5, but that is not how the computer does it, 512 being half of 1024.. hence the  .5)
-- Stu --
BLoggins02

Re: I HATE DIV!!!

Post by BLoggins02 »

Gotta love DIV and MUL  ;D

Once you understand 'em, they're your best friends, but until then, be prepared to lose some hair!  Hehe
Peter_Vigren

Re: I HATE DIV!!!

Post by Peter_Vigren »



those numbers are fine!

mov eax,1536
mov ebx,1024
xor edx,edx
idiv ebx

eax = 1
edx = 512

which is correct.

edx wont be 5.. (1536-1024 does not have a remainder of 5) (1536/1024 = 1.5, but that is not how the computer does it, 512 being half of 1024.. hence the ?.5)
Well then... How do I do to "translate" 512 (or 768) to 5 (or 75)?????? I need a program which rounds up bytes into Kb/Mb/Gb/Tb in the format a.bb or just a (if it is an even number)... Any tips??????

Best regards,
Peter Vigren
VLAVI

Re: I HATE DIV!!!

Post by VLAVI »

...one idea:

you want : 1536/1025 = 1.5

1) to obtain the first number:

1536 div 1025 -> COCIENT 1 ( THE FIRST NUMBER!)
? ? ? ? ? ? ? ? ? ? ? ? REST = 512

2) next number (moving one place right in decimals <=> * 0.1)

REST / 1024 = COCIENT 5 (NEXT NUMBER!)
? ? ? ? ? ? ? ? ? ? ?REST = 0 -> STOP
? ? ? ? ? ? ? ? ? ? ?REST != 0 -> DO 2) until the number of decimals wanted)

In this example you have RESULT = 1 + (5*0.1) = 1.5

The divisor (1024) could be other...

Maybe this idea will help you...
...try other examples to adjust it...
VLAVI

Re: I HATE DIV!!!

Post by VLAVI »

SORRY! in step 2) I've forgotten something:

you have to multiply by 10 the previous rest, so:

(10 * REST) div 1024 -> cocient = 5
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? rest = 0
Post Reply