Page 1 of 1

Inline ASM : the "DIV" instruction crash my prog

Posted: Thu Jan 01, 2004 4:41 pm
by newbie
Procedure ASM_div(l_a32, l_b32)
MOV eax, l_a32
DIV l_b32
MOV l_a32, eax

ProcedureReturn l_a32

EndProcedure

a1 = 10
a2 = 2

res = ASM_div(a1, a2) ; a1 / a2

debug str(res)
Why my prog crash with an error about capacity overflow or something like that ?

I thought i have followed the intel paper but something is wrong.

(PS : the result will always be positive).

Posted: Thu Jan 01, 2004 4:50 pm
by Wayne Diamond
Here's a modified version:

Code: Select all

Procedure ASM_div(l_a32, l_b32) 
 mov eax, l_a32   ;number to divide
 mov ebx, l_b32   ;by this number
 xor edx, edx     ;edx must be zero before div
 div ebx          ;after div, remainder is in edx
 ;mov eax, edx    ;uncomment this to make it MOD instead of DIV :)
ProcedureReturn  ;result is in eax
EndProcedure

Posted: Thu Jan 01, 2004 5:02 pm
by newbie
Thanks a lot !

i done simple things like +/-/* and compare (i 'm training me to ASM) but i has trouble with div.
I didn't put the register to 0, may that was what causing my crash.

Now it rocks :D

(nice comments too ;) )

Posted: Thu Jan 01, 2004 8:05 pm
by Fred
Here is the same for signed numbers:

Code: Select all

Procedure ASM_div(l_a32, l_b32) 
MOV eax, l_a32   ;number to divide 
MOV ebx, l_b32   ;by this number 
CDQ              ;convert eax to 64 bits in eax:edx
IDIV ebx         ;after div, remainder is in edx 
;mov eax, edx    ;uncomment this to make it MOD instead of DIV :) 
ProcedureReturn  ;result is in eax 
EndProcedure

Debug ASM_div(-40, 5)

Posted: Thu Jan 01, 2004 8:08 pm
by newbie
Thanks you too Fred, i will add it to my lib and study this instructions with the pdf of intel ASM instructions that i have downloaded, fortunaly all the code is commented, easier for me to learn :P