division - i386 Assembly floating exception on div instruction -


i have been playing assembly recently, , when trying use div instruction, coming across floating exception. have searched online past day or 2 , nothing working. saw this question, , because of answer, put in 3 xorq instructions 0 out registers (specifically rdx because high byte of dividend), still didn't help. when compiling c program modulus operation (and creates few variables compiler doesn't optimize away modulus) using gcc -s myprog.c, gcc creates assembly file has cltd (convert signed long signed long double) instruction right before idivl. tried in program well, change anything. have tried using different size values in rax ans rbx, still doesn't work.

question: doing wrong, , weird quirks there using div instruction? this documentation have been using learning assembly.

in code below, function print prints contents of rax. don't care gets printed, printed can see instruction crashes program.

.cstring     _format: .asciz "%d\n"  .text .globl _main _main:     # program setup     pushq   %rbp     movq    %rsp, %rbp      # program - 16 byte aligned     # 0 out registers     xorq    %rax, %rax     xorq    %rdx, %rdx     xorq    %rbx, %rbx      # set values use in division     movq    $5, %rax     movq    $16, %rbx  #   cltd    # 'gcc -s' command puts in cltd before idivl      call    print     idivl   %ebx, %eax     ########### crash here     call    print      # program cleanup     popq    %rbp     ret  # prints value in %rax (could junk, don't care. long printed) print:     subq    $8, %rsp    # 16 byte alignment     lea     _format(%rip), %rdi     movq    %rax, %rsi     call    _printf     addq    $8, %rsp     ret 


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -