title: idiv overflow exception asm
body: <p>I'm very new to assembly, before only C C++<br>
  I was trying to create a simple application that print all the prime number from 2 to given input<br>
  When I run, it crashes, and that's normal. Looking at OllyDbg, the guilty piece is this:</p>
  
  <pre><code>move eax, ebx
  idiv ecx ; !!! here
  </code></pre>
  
  <p>It is strange because ecx is not 0 <br> Values are EAX = 0000 0004, ECX = 0000 0002, and it says me that's an Integer_Overflow (exception C000 0095)<br>
  How it's possible to have an <strong>overflow</strong> during a division? Both operands are 32bit<br>
  <br>Floowing the code and a screenshot of ollydbg</p>
  
  <pre><code>%include "asm_io.inc"
  
  segment .data
  input_msg   db  "Insert a number: ", 0
  output_msg  db  "This is prime: ", 0
  
  segment .bss
  input       resd    1
  
  segment .text
  global _asm_main
  
  ; input =&gt; input number
  ; ebx =&gt; current number to execute [2 .. input]
  ; ecx =&gt; counter from 2 to current [2 .. current]
  
  _asm_main:
      mov eax, input_msg
      call print_string
  
      call read_int
      inc eax
      mov [input], eax
      call print_nl
  
      mov ebx, 2h
  
      _start_main_loop:
          mov eax, [input] ; if current &gt; input
          cmp eax, ebx
          jz _end_main_loop       
  
          mov ecx, 2h
  
          _iteration:
              cmp ebx, ecx
              je _print_number
  
              mov eax, ebx
              idiv ecx ; unsigned division?
  
              cmp edx, 0 ; if rem != 0 jmp
              jne _end_iteration
              inc ecx ; else inc ecx and re-divide
              jmp _iteration
  
          _print_number:
              mov eax, output_msg
              call print_string
              mov eax, ebx
              call print_int
              call print_nl
  
          _end_iteration:
              inc ebx
  
              jmp _start_main_loop
  
      _end_main_loop:
  
      popa
      mov     eax, 0
      leave
      ret
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/pZt7P.png" alt="enter image description here"></p>