The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: [PT]Devilishly on January 13, 2005, 08:39:20 PM

Title: div question
Post by: [PT]Devilishly on January 13, 2005, 08:39:20 PM
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
Title: Re: div question
Post by: raymond on January 13, 2005, 09:08:19 PM
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

Title: Re: div question
Post by: [PT]Devilishly on January 13, 2005, 10:15:04 PM
Thanks raymond   :U

Best regards,
[PT]Devilishly