title: Assembly program to find all divisors of an integer
body: <p>I am trying to write a program that reads in an integer and finds all divisors of that integer (up to but not including the integer). After finding a divisor it is pushed on to the stack and at the end the divisors should be printed in reverse order.</p>
  
  <p>This is the code I currently have: </p>
  
  <pre><code>; global and external declarations
  
  global _main
  extern  read_int, print_int, print_string
  extern  read_char, print_char, print_nl
  
  segment .data
  prompt01 db 10, "Enter an integer: ",0
  out01 db "Divisors: ",0
  
  segment .bss
  a resd 1
  
  segment .text
  
  _main:
  
  enter   0,0 ; setup routine
  pusha
  
  ; prompt for number
  mov eax, prompt01   ; prompt for a
  call print_string
  
  call read_int       ; input a
  mov [a], eax        ; move from eax to a
  xor ecx, ecx        ; zero out ecx register
  
  
  ; do the division
  mov ebx, 1      ; divisor in ebx
  L1: mov eax, [a]        ; dividend in eax
  cdq         ; sign extend eax to edx:eax pair
  div ebx         ; divide - quotient in eax, remainder in edx
  ; if edx is 0, push on stack
  cmp edx, 0
  je L2
  jmp L3
   ; if remainder was zero, push on to stack
  L2: push edx
  inc ecx         ; increment counter loop
  L3: inc ebx         ; increment divisor
  cmp ebx, eax        ; check if ebx = eax
  je out
  jmp L1
  
  ; print out everything in the stack
  out:    mov eax, out01
  call print_string
  
  jcxz done
  L4: pop edx
  call print_int
  call print_nl
  loop L4
  
  done:   popa        ; return routine
  mov eax, 0
  leave
  ret
  </code></pre>
  
  <p>The expected output is something similar to this:</p>
  
  <pre><code>Enter an integer: 210
  Divisors:
  105
  70
  42
  35
  30
  21
  15
  14
  10
  7
  6
  5
  3
  2
  1
  </code></pre>
  
  <p>But I actually get this:</p>
  
  <pre><code>Enter an integer: 210
  Divisors: 4210712
  4210712
  4210712
  4210712
  4210712
  4210712
  4210712
  4210712
  </code></pre>
  
  <p>Since the Intel 80x86 has a division operation, I'm using DIV to divide my numbers and using the CDQ command to sign extend eax to the edx:eax pair.</p>
  
  <p>I think the issue is occurring during the division since something is being pushed onto the stack (even though it isn't the correct thing). Any ideas for where this could be going wrong?</p>