Inline ASM : the "DIV" instruction crash my prog

Just starting out? Need help? Post your questions and find answers here.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Inline ASM : the "DIV" instruction crash my prog

Post 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).
- Registered PB user -

Using PB 4.00
Wayne Diamond
User
User
Posts: 38
Joined: Tue Dec 30, 2003 1:37 pm
Location: Australia

Post 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
Last edited by Wayne Diamond on Thu Jan 01, 2004 5:03 pm, edited 1 time in total.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post 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 ;) )
- Registered PB user -

Using PB 4.00
Fred
Administrator
Administrator
Posts: 18393
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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)
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post 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
- Registered PB user -

Using PB 4.00
Post Reply