News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

div question

Started by [PT]Devilishly, January 13, 2005, 08:39:20 PM

Previous topic - Next topic

[PT]Devilishly

Hi,

Why doesn't this work?
    mov eax, 6
    mov ecx, 2
    div ecx

Basically I want to do 6/2

Best regards,
[PT]Devilishly

raymond

The division by a 32-bit number (ECX in your case even if only the "2" bit is used) is performed on a 64-bit number, the most significant 32 bits being in EDX and the least significant being in EAX. The result must then fit into the 32-bit EAX register.

If the result is too large to fit into EAX, an exception is detected and would crash your program if it does not have code for exception handling.

The result will always be too large if the content of EDX is greater than or equal to your divisor. When you want to divide only the content of the EAX register, you MUST zero the EDX register before the division. If you would perform a signed division (using idiv), you should then extend the sign of EAX into the EDX register with the cdq instruction.

Raymond

When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

[PT]Devilishly

Thanks raymond   :U

Best regards,
[PT]Devilishly