Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,364 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,266 people online right now. Registration is fast and FREE... Join Now!




Why do I get Floating point exception when I divide?

 
Reply to this topicStart new topic

Why do I get Floating point exception when I divide?, using idiv x86 nasm

no2pencil
post 27 Aug, 2008 - 12:12 AM
Post #1


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


CODE

_div: ; use division
  push ebp       ; bring in the arguments.
  mov ebp, esp
  mov eax, [ebp+8]; Argument 1
  mov edx, [ebp+12]; Argument 2
  xor edx, edx; clear out register edx
  mov edx, ebx; Place ebx *argument 2* into ebx
  idiv ebx; Divide EDX:EAX by EBX
  pop eax; Return the quotient
  ret


No matter what arguments I give, I get the dreaded floating point exception error. I can't find a proper explanation w/Google as to why! Any ideas?
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 27 Aug, 2008 - 12:41 AM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


I'm not sure if this is correct, but this is what I did to fix it :

CODE

_div:
  push ebp
  mov ebp, esp
  mov eax, [ebp+8] ; low
  mov edx, 0           ; high
  idiv dword [ebp+12] ; divide edx:eax by ebp+12 (2nd argument)
  mov [ebp+12],eax ; Store the remainder back into ebp
  pop ebp                 ; return the quotient
  ret
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 27 Aug, 2008 - 12:50 AM
Post #3


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


You copied your argument 2 in edx and then cleared it in next statement using XOR. I think you wanted to copy your second argument in ebx instead of edx, right?

CODE

mov edx, [ebp+12]; Argument 2    You copied it to edx and not ebx
  xor edx, edx; clear out register edx
  mov edx, ebx; Place ebx *argument 2* into ebx    but you never had argument 2 in ebx!!!


May be it was just a typo. Correct it and see whether it works.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 27 Aug, 2008 - 12:57 AM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(AmitTheInfinity @ 27 Aug, 2008 - 04:50 AM) *

Correct it and see whether it works.

Yeah tongue.gif

Now how can I return the remainder? Shouldn't it be in register edx?
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 05:02AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month