Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating Point Exception when dividing in x86 nasm

I'm busy with learning Assembly and was looking at dividing, however I ran into a pickle with the following statement:

mov edx,0x00000001
mov eax,0x00000000
mov ecx,0x00000002
idiv ecx

GDB:

   0x08048071 <+17>:    mov    edx,0x1
   0x08048076 <+22>:    mov    eax,0x0
   0x0804807b <+27>:    mov    ecx,0x2
=> 0x08048080 <+32>:    idiv   ecx

I wanted to divide 0x100000000 by 0x00000002, so since the span for division is EDX:EAX I moved 0x1 into EDX and 0x0 into EAX. I then move 0x2 into ECX and divide, this unfortunately gives me a floating point exception, I' m not sure what I did wrong.

When using div (unsigned) it works normal, so I wonder what the difference is in interpretation between div and idiv for this particular statement which causes the exception.

like image 817
Lucas Kauffman Avatar asked Mar 24 '13 18:03

Lucas Kauffman


1 Answers

The quotient (0x80000000) doesn't fit into a 32-bit signed integer (max value: 0x7fffffff). Hence you get an exception. It does fit into a 32-bit unsigned integer (max value 0xffffffff), so no exception is triggered by an unsigned divide.

like image 113
Stephen Canon Avatar answered Nov 11 '22 11:11

Stephen Canon