flat assembler
Message board for the users of flat assembler.

 Index > Main > how DIV works? Goto page 1, 2  Next
Author
zhak

Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 19 Feb 2010, 17:51
Hello guys,

i'm trying to implement a hex to dec function which is able to convert 64-bit integers. i use DIV algorithm, but! it seems that with DIV instruction it's possible to divide integers that are below or equal to 0x9ffffffff. any greater values cause divide error exception. is there any elegant way to fix this issue? (note: code is executed on a 32-bit CPU in 16-bit Real Mode)

thanks in advance for any ideas
19 Feb 2010, 17:51
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20212
Location: In your JS exploiting you and your system
revolution 19 Feb 2010, 17:57
Do the division twice:
Code:
```;edi=high 32 bits
;esi=low 32 bits
;ecx=divisor
mov edx,0
mov eax,edi
div ecx
mov edi,eax ;update high order quotient
mov eax,esi
div ecx
mov esi,eax ;update low order quotient
;edx=remainder    ```
19 Feb 2010, 17:57
r22

Joined: 27 Dec 2004
Posts: 805
r22 20 Feb 2010, 05:48
Just for fun Binary Coded Decimal
Code:
```        fild    qword[int_somebignumber] ; 56bit between 0x0 and 0xFFFFFFFFFFFFFF
fbstp   [lbl_bcd] ; 10bytes
```

ALMOST useful ...
20 Feb 2010, 05:48
DOS386

Joined: 08 Dec 2006
Posts: 1900
DOS386 22 Feb 2010, 08:35
> trying to implement a hex to dec function

More likely a 64-bit dec out function, no HEX at all

> Do the division twice:

FASM IDE's contain a COOL CALCULATOR using this code

PS @re: I'll answer the other one later
22 Feb 2010, 08:35
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 22 Feb 2010, 08:52
revolution wrote:
Do the division twice:
Code:
```;edi=high 32 bits
;esi=low 32 bits
;ecx=divisor
mov edx,0
mov eax,edi
div ecx
mov edi,eax ;update high order quotient
mov eax,esi
div ecx
mov esi,eax ;update low order quotient
;edx=remainder    ```

Rather than "mov eax,esi" you should do
Code:
```mov eax,edx
there.
22 Feb 2010, 08:52
edemko

Joined: 18 Jul 2009
Posts: 549
edemko 22 Feb 2010, 09:06
How can divide a dqword(128bit) by a dword?
22 Feb 2010, 09:06
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 22 Feb 2010, 09:15
serfasm wrote:
How can divide a dqword(128bit) by a dword?
Just extrapolate the aformentioned method.
22 Feb 2010, 09:15
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20212
Location: In your JS exploiting you and your system
revolution 22 Feb 2010, 09:20
Tomasz Grysztar wrote:
Rather than "mov eax,esi" you should do
Code:
```mov eax,edx
there.
Eh? I don't think you can simply add the high order remainder to the low order dividend
22 Feb 2010, 09:20
edemko

Joined: 18 Jul 2009
Posts: 549
edemko 22 Feb 2010, 10:07
true as a remainder(edx) could be 1 and the low 32 bits(esi) could be -1
22 Feb 2010, 10:07
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 22 Feb 2010, 10:10
revolution wrote:
Tomasz Grysztar wrote:
Rather than "mov eax,esi" you should do
Code:
```mov eax,edx
there.
Eh? I don't think you can simply add the high order remainder to the low order dividend

Oh, sorry, I meant it should stay in EDX. Stupid mistake.
22 Feb 2010, 10:10
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20212
Location: In your JS exploiting you and your system
revolution 22 Feb 2010, 10:13
serfasm wrote:
true as a remainder(edx) could be 1 and the low 32 bits(esi) could be -1
Not just that, they are of a different scale. One is 2^32 times the significance of the other.

I think the original code I posted above is correct.
22 Feb 2010, 10:13
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 22 Feb 2010, 10:14
revolution wrote:
I think the original code I posted above is correct.
That's right. I don't know what I was thinking, sorry.
22 Feb 2010, 10:14
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20212
Location: In your JS exploiting you and your system
revolution 22 Feb 2010, 10:19
serfasm wrote:
How can divide a dqword(128bit) by a dword?
Okay, just in case someone can't see the general solution:
Code:
```;edi=highest part of dqword
;ebp=2nd highest
;ebx=3rd highest
;esi=lowest part of dqword
;ecx=divisor

mov     edx,0   ;previous remainder is zero
mov     eax,edi
div     ecx
mov     edi,eax ;highest
mov     eax,ebp
div     ecx
mov     ebp,eax ;2nd highest
mov     eax,ebx
div     ecx
mov     ebx,eax ;3rd highest
mov     eax,esi
div     ecx
mov     esi,eax ;lowest

;edx=remainder    ```
22 Feb 2010, 10:19
edemko

Joined: 18 Jul 2009
Posts: 549
edemko 22 Feb 2010, 10:20
adding big remainder(edx) of a high ordered dividend(edi) to eax=esi
we, even expanding result to 64 bits, can get integer overflow on the next DIV iteration
22 Feb 2010, 10:20
edemko

Joined: 18 Jul 2009
Posts: 549
edemko 22 Feb 2010, 10:21
oops, my post is old, i'll continue from yours, revolution
22 Feb 2010, 10:21
edemko

Joined: 18 Jul 2009
Posts: 549
edemko 22 Feb 2010, 10:28
It looks easy enough. But returning to decimal system to understand better
i must say about borrowing from the higher ranks but you do not.

Argue with you tomorrow: must go home, bye.

Last edited by edemko on 22 Feb 2010, 10:33; edited 1 time in total
22 Feb 2010, 10:28
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20212
Location: In your JS exploiting you and your system
revolution 22 Feb 2010, 10:32
You run the code in a loop. At at iteration edx is the LSD which you should store somewhere (push edx), and later recall in reverse order (pop reg) to print the digits.
22 Feb 2010, 10:32
zhak

Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 22 Feb 2010, 10:33
i didn't try dqword conversion, yet, but the idea that revolution suggested in his first post works correctly. i just modified the algo to do "double division" only when needed (if div overflow error will occur)
22 Feb 2010, 10:33
edemko

Joined: 18 Jul 2009
Posts: 549
edemko 22 Feb 2010, 10:34
What is an LSD.
Sorry, i've found the borrow mechanism of you out. Still, can an overflow happen?

Last edited by edemko on 22 Feb 2010, 10:38; edited 1 time in total
22 Feb 2010, 10:34
zhak

Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 22 Feb 2010, 10:35
least significant digit, i suppose

Last edited by zhak on 22 Feb 2010, 10:37; edited 1 time in total
22 Feb 2010, 10:35
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page 1, 2  Next

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum