user700176
user700176

Reputation: 73

Integer Overflow problem

I keep getting a integer overflow problem and i have no idea how to solve it can anyone help? edx conatins 181 and eax contains 174

       xor eax,edx       
       mov edx,2
       div edx   

Upvotes: 7

Views: 14899

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490028

Assuming you're talking about x86, div edx doesn't really make sense -- a 32-bit div divides edx:eax by the specified target register. Fortunately, to divide by 2, you don't really need to use div at all.

mov eax, 174
mov edx, 181

xor eax, edx
shr eax, 1

If you do insist on using a div for some reason, you want to use a different register. Note that the x86 expects the result of the division to fit in one register, so you'll need to zero edx before the division:

mov eax, 174
mov edx, 181

xor eax, edx
xor edx, edx
mov ebx, 2
div ebx

Upvotes: 6

ughoavgfhw
ughoavgfhw

Reputation: 39905

When dividing using a 32 bit register, the dividend is edx:eax. Since eax is originally 174, and edx is originally 181, this is what happens:

  1. eax and edx are xor-ed, with the result being stored in eax. eax is now 27
  2. 2 is stored in edx
  3. edx:eax is divided by edx. This means 0x20000001B is divided by 0x2. The result of this operation is 0x10000000D. The CPU attempts to store this value in eax, with the remainder, 1, in edx, but it does not fit because the 1 is in the 33rd bit. Therefore, you get an overflow.

You can fix this by using a different register than edx to divide, making sure to zero edx:

xor eax,edx
mov ecx,2
xor edx,edx ; Zero edx
div ecx
; eax contains 0xD, edx contains 1

Upvotes: 5

Related Questions