I am having a DIV problem and I am a total newbie, as U can see from my post count :grin:

I have the following code fragment:

lea ecx, [rect.right]
xor edx, edx
mov eax, [ecx]
mov bl, 10
div bl
.IF edx > 9
add edx, 7
add edx, 48
push edx

...and so on in a loop outputting strings from numbers.

Directly after the DIV, EAX contains 00000209h and EDX contains 00000000h. I don't get it! I expected EDX to contain 2 and EAX to contain 9 . It looks like EAX contains the remainder as well as the quotient. BTW I'm debugging it with W32DAsm 8.93 and this is a dual Athlon MP box running MASM 8.0.

Any help greatly appreciated. :alright:
Posted on 2003-05-02 08:50:22 by spanner
When you divide by an 8 bit register (as you did with 'div bl') you divide the word in AX. The remainder is placed in AH and the quotient in AL.

If you want the remainder in EDX change your code to mov ebx, 10 - div ebx.
Posted on 2003-05-02 09:42:15 by iblis
Ah I see...the reason I didn't already use ebx is because I have another value in bh. I'll have to juggle my registers.

Posted on 2003-05-02 09:50:44 by spanner
A quick work-around would be to insert "movzx edx, ah" after the div.
Posted on 2003-05-02 10:02:49 by iblis
:eek: You use EBX ?

[b]Lea ecx,[rect.right][/b]
mov eax,[ecx]

I think it was...

Mov ecx,rect.right

Right? because rect.right contain the address and contain the value. BTW: Is it FASM ?
Posted on 2003-05-02 10:12:35 by realvampire
If you adopt the suggestion from iblis,
A quick work-around would be to insert "movzx edx, ah" after the div.
also insert "mov ah,0" after the above movzx so that your next division of AX by BL will give you the correct answer.

Secondly, because you are dividing by 10, it's impossible that the remainder will ever be greater than 9. You can therefore remove the ".IF edx>9" ... .ENDIF instructions. They are useless in this context.

Posted on 2003-05-02 11:04:17 by Raymond
Instead of using div bl, you could use aam. It will place the quotient in ah, and the remainder in al, but you save a register.
Posted on 2003-05-02 16:30:35 by Sephiroth3