title: intel-Based Assembly Language idiv
body: <p>I'm trying to get <code>idiv</code> working properly and I've read you put what you want to divide
  such as 25 and then in <code>ebx</code> you put what you want to divide by such as 5 then you do</p>
  
  <pre><code>idiv ebx
  </code></pre>
  
  <p>which then puts <code>EAX</code> = 5 and then <code>EDX</code> = 0.</p>
  
  <p>However it doesn't do that in my program the input I do is <strong><em>100000000</em></strong></p>
  
  <p><strong>output:</strong></p>
  
  <p><em>Kilobytes: 100000000</em></p>
  
  <p><em>Megabytes: 1869375819</em></p>
  
  <p>Was wondering what am I doing wrong here?</p>
  
  <pre><code>%include "asm_io.inc"
  ;
  ; initialized data is put in the .data segment
  ;
  segment .data
  prompt db "Please enter the number of bytes:", 0
  param db "1 = Calculate it in kilobytes", 0Ah, "2 = Calculate it in megabytes", 10, 0
  output db "Kilobytes: %d", 0Ah, "MegaBytes: %d", 10, 0
  ;
  ;
  segment .bss
  ;
  input resd 1
  input2 resd 1
  choice resd 1
  ;
  ; code is put in the .text segment
  ;
  segment .text
      global  asm_main
  extern printf
  asm_main:
      enter   0,0              
      pusha
  
      mov eax, prompt
      call print_string
      call read_int
      mov [input], eax
      mov [input2], eax
  
      sub esp, 10h
      push dword [output]
      push dword [input2]
      push dword [input]
  
  
      mov eax, param
      call print_string
      call read_int
  
      cmp eax, 1
      je kilobyte; Jump if eax is equal to 1
  
      cmp eax, 2
      je megabyte; Jump if eax equal to 2
  
  kilobyte:
      pop eax             ; Pop input into eax
      cdq             
      mov ebx, 1024       ; Put 1024 into ebx
      idiv ebx             ; EAX = 100000000/1024 = 97656
      mov [input], eax    ; Move 97656 into var=input
      push dword [input]  ; Put into stack
      jmp megabyte
  
  megabyte:
      pop eax             ; Pop input into eax
      cdq         
      mov ebx, 1048576    ; Put 1048576 into ebx
      idiv ebx             ; EAX = 100000000/1024 = 95
      mov [input2], eax   ; Move 95 into var=input    
      push dword [input]  ; Put into stack
      jmp printOut
  
  printOut:
      mov dword [esp], output
      call printf
  
      add esp, 4 * 3
  
  
      add esp, 10h
  
      popa
      mov     eax, 0            
      leave                     
      ret
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Well I put in <code>xor edx, edx</code> but I am still getting the same output as before. I've looked into my ebook and other sites and it says same thing so I am really unsure of what I am doing wrong. As well tried the <code>idiv</code> route with no luck.</p>