Integer overflow problem

I keep getting the 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 
+7
source share
2 answers

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

 mov eax, 174 mov edx, 181 xor eax, edx shr eax, 1 

If you really insist on using a div for any reason, you want to use a different register. Note that x86 expects the division result to be placed in the same register, so before division you need edx zero:

 mov eax, 174 mov edx, 181 xor eax, edx xor edx, edx mov ebx, 2 div ebx 
+6
source

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

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

You can fix this using a different register than edx to separate it by specifying edx zero:

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

All Articles