7

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   
5
  • More info needed: for example, target architecture, assembler version. Commented Apr 9, 2011 at 18:18
  • @suddnely_me target architecture? Commented Apr 9, 2011 at 18:21
  • Meant assembler version OFC >_< Commented Apr 9, 2011 at 18:22
  • @suddnely_me o right didn't see the assembler version bit x86 Commented Apr 9, 2011 at 18:24
  • Assembly question must go with architecture tag. No one can make sure that you're on x86, ARM, MIPS or some other architectures Commented Dec 17, 2013 at 0:46

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.