flat assembler
Message board for the users of flat assembler.

Index > Main > how DIV works?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
zhak



Joined: 12 Apr 2005
Posts: 490
Location: Belarus
zhak
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
Post 19 Feb 2010, 17:51
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
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    
Post 19 Feb 2010, 17:57
View user's profile Send private message Visit poster's website Reply with quote
r22



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

ALMOST useful ...
Post 20 Feb 2010, 05:48
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
> trying to implement a hex to dec function

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

> Do the division twice:

FASM IDE's contain a COOL CALCULATOR using this code Smile

PS @re: I'll answer the other one later
Post 22 Feb 2010, 08:35
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
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
add eax,esi    
there.
Post 22 Feb 2010, 08:52
View user's profile Send private message Visit poster's website Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko
How can divide a dqword(128bit) by a dword?
Post 22 Feb 2010, 09:06
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
serfasm wrote:
How can divide a dqword(128bit) by a dword?
Just extrapolate the aformentioned method.
Post 22 Feb 2010, 09:15
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
Tomasz Grysztar wrote:
Rather than "mov eax,esi" you should do
Code:
mov eax,edx
add eax,esi    
there.
Eh? I don't think you can simply add the high order remainder to the low order dividend Question
Post 22 Feb 2010, 09:20
View user's profile Send private message Visit poster's website Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko
true as a remainder(edx) could be 1 and the low 32 bits(esi) could be -1
Post 22 Feb 2010, 10:07
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


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

Oh, sorry, I meant it should stay in EDX. Stupid mistake. Smile
Post 22 Feb 2010, 10:10
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
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.
Post 22 Feb 2010, 10:13
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
revolution wrote:
I think the original code I posted above is correct.
That's right. I don't know what I was thinking, sorry. Smile
Post 22 Feb 2010, 10:14
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
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    
Post 22 Feb 2010, 10:19
View user's profile Send private message Visit poster's website Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko
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
Post 22 Feb 2010, 10:20
View user's profile Send private message Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko
oops, my post is old, i'll continue from yours, revolution
Post 22 Feb 2010, 10:21
View user's profile Send private message Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko
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
Post 22 Feb 2010, 10:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
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.
Post 22 Feb 2010, 10:32
View user's profile Send private message Visit poster's website Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 490
Location: Belarus
zhak
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)
Post 22 Feb 2010, 10:33
View user's profile Send private message Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko
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
Post 22 Feb 2010, 10:34
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 490
Location: Belarus
zhak
least significant digit, i suppose


Last edited by zhak on 22 Feb 2010, 10:37; edited 1 time in total
Post 22 Feb 2010, 10:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.