flat assembler
Message board for the users of flat assembler.

Index > Windows > DIV Command

Author
Thread Post new topic Reply to topic
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 Smile

Thanks!
Post 20 Oct 2007, 03:47
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


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.
Post 20 Oct 2007, 04:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.