Hello, I'm writing a compiler in C++ which generates x86 assembly code. I'm using masm32 to test generated code. I don't really know assembly, I have to look up a lot of things while writing code. My problem is that when I generate the code for a division, like

4/2
mov edx, 4
push edx
mov edx, 2
pop eax
div edx
mov edx, eax

and assemble it with masm32, after assembling and linking, when I run it, it says text.exe has stopped working. When I've tried the same like this

mov eax, 4
mov ecx, 2
div ecx

it does not work either. What am I doing wrong?

Also, how

MOVE X(PC),D0

is translated to x86 assembly (using edx instead of d0)?

I'm also don't know many but I face this problem and I fix it
you MUST make edx ==0 so write this while divide
push ecx
mov ecx,edx
xor edx,edx
div ecx
pop ecx

DON'T use div edx and KEEP edx==0 while divide and please don't ask me why because I don't know :)

hope that helps

The DIV instruction implies that the EDX:EAX combined register is the number you are dividing. The user above stated that EDX must be 0. It doesn't, but you shouldn't be using div edx. My suggestion, is that if you are dividing a 32 bit number, place it in eax, make edx 0, and then use edi or some other register for the div instruction operand.

Here's an example (forgive me, but I only code in AT&T syntax. The operands and destinations will be mixed up for machine instructions):

#divides 100 by 10
movl $10, %edi
movl $100, %eax
xorl %edx, %edx
divl %edi

Ignore the l at the end of each instruction, as it simply stands for "long." This places 10 into edi, 100 into eax, 0 into edx, and then finally divides. It will divide 100 by 10, leaving the quotient in eax and the remainder in edx.

I hope this helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.