flat assembler
Message board for the users of flat assembler.

 Index > Windows > DIV Command
Author
Sandurr

Joined: 20 Oct 2007
Posts: 2
Sandurr 20 Oct 2007, 03:47
Hello there!

I have been writing in asm for a pretty long while now, but I never really came to use the DIV yet, I write in OllyDbg

I understand that 'div edx' basicly does this (at least thats what I get from most pages):
eax = eax/edx

So if I write this:
mov eax,4
mov edx,2
div edx

I get 'integer overflow' error, why?

Hopefully someone can help me out quickly

Thanks!
20 Oct 2007, 03:47
LocoDelAssembly

Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 20 Oct 2007, 04:19
Because it actually performs:
EAX = EDX:EAX div divisor (1) {[(EDX * 4294967296 + EAX) / divisor]}
EDX = EDX:EAX mod divisor (2) {(EDX * 4294967296 + EAX) mod divisor}
(with div and mod behaving like in Pascal language)

Since (2) is impossible to give a value greater or equal to the divisor that part never overflows. However, we can't say the same about (1), by just having EDX=1 and EAX=0, dividing by 1 is enough to overflow because EDX:EAX before division is 4294967296, and after division is the same value again, but the maximun value supported in EAX (a 32-bit reg), is 4294967295 (one minus than the result).

To sumarize, you will get overflows if the divisor of EDX:EAX cannot reduce the number to a value less than 4294967296. You'll typically want to clear EDX before dividing using XOR EDX, EDX; MOV EDX, 0; or any other thing that you found more suitable for your particular context.
20 Oct 2007, 04:19
 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

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