1

I`m trying to build a compiler from the Scheme language to X86_64bit, while debugging with the gdb in order to see where my program crashes this what is happening: (I displayed the relevant registers, you can see that rax=1, r15=1 and the command that crashes my program is div r15)

0x000000000040088f in L_add_fraction ()
1: $r11 = 1
2: $r12 = 3
3: $rax = 1
4: $r15 = 1
(gdb) disass
Dump of assembler code for function L_add_fraction:
0x000000000040087b <+45>:   push   %r11
0x000000000040087d <+47>:   push   %r12
0x000000000040087f <+49>:   callq  0x400588 <gcd>
---Type <return> to continue, or q <return> to quit---
0x0000000000400884 <+54>:   pop    %r12
0x0000000000400886 <+56>:   pop    %r11
0x0000000000400888 <+58>:   mov    %rax,%r15
0x000000000040088b <+61>:   mov    %r11,%rax
0x000000000040088e <+64>:   push   %rdx
=> 0x000000000040088f <+65>:    div    %r15
0x0000000000400892 <+68>:   pop    %rdx
0x0000000000400893 <+69>:   mov    %rax,%r11
0x0000000000400896 <+72>:   mov    %r15,%rax
0x0000000000400899 <+75>:   mov    %r12,%rax
0x000000000040089c <+78>:   push   %rdx
0x000000000040089d <+79>:   div    %r15
0x00000000004008a0 <+82>:   pop    %rdx
0x00000000004008a1 <+83>:   mov    %rax,%r12
0x00000000004008a4 <+86>:   cmp    $0x1,%r11
0x00000000004008a8 <+90>:   je     0x4008d0 <L_result_is_integer_plus>
0x00000000004008aa <+92>:   push   %r11
0x00000000004008ac <+94>:   push   %r12
0x00000000004008ae <+96>:   mov    $0x8,%edi
0x00000000004008b3 <+101>:  callq  0x400570 <my_malloc>
0x00000000004008b8 <+106>:  pop    %r12
0x00000000004008ba <+108>:  pop    %r11
0x00000000004008bc <+110>:  mov    %r11,(%rax)
---Type <return> to continue, or q <return> to quit---
0x00000000004008bf <+113>:  shlq   $0x22,(%rax)
0x00000000004008c3 <+117>:  shl    $0x4,%r12
0x00000000004008c7 <+121>:  or     %r12,(%rax)
0x00000000004008ca <+124>:  orq    $0x4,(%rax)
0x00000000004008ce <+128>:  jmp    0x4008e9 <L_plus_bin_end>
End of assembler dump.
(gdb) ni

Program received signal SIGFPE, Arithmetic exception.
0x000000000040088f in L_add_fraction ()
1: $r11 = 1
2: $r12 = 3
3: $rax = 1
4: $r15 = 1

I searched for the problem but all i can see is that SIGFPE is given when dividing by zero (clearly not the case here)

  • 1
    [DIV](http://www.felixcloutier.com/x86/DIV.xml) takes RDX:RAX (128 bit value) and divides it by the operand (64-bits). You need to zero _RDX_ to avoid division overflow (it gets reported as SIGFPE as well).Division overflow will occur if the result of the division can't be represented in 64-bits. – Michael Petch Feb 08 '18 at 08:17
  • 1
    I will guess that _RDX_ is some left over value that is non zero. You don't show _RDX_ but I'm guessing it is above zero. – Michael Petch Feb 08 '18 at 08:25
  • 1
    x86 raises `#DE` (and the kernel delivers SIGFPE) on division overflow as well as div by zero. https://stackoverflow.com/questions/37262572/on-which-platforms-does-integer-divide-by-zero-trigger-a-floating-point-exceptio for more, and especially https://stackoverflow.com/questions/46378104/why-does-integer-division-by-1-negative-one-result-in-fpe re: div overflow from INT_MIN / -1 causing SIGFPE. – Peter Cordes Feb 08 '18 at 08:33
  • Great you helped me a lot guys, I Really appreciate it. you can close this question – Yair Landmann Feb 08 '18 at 08:41

0 Answers0