title: How to use FDIV properly? Assembly
body: <p>I wanted to do a simple hybrid code using 16-bit assembly, masm. 
  The goal was for program to calculate average, but I get very wrong output every single time and I don't know why. After fadd operation the result should be in st(0). So fdiv should devide st(0)/size and the result should be stored in st(0), right? So when my input is 1+1+1+1+1, the output should be 1 — not 6.39,516e+33. Did I get something wrong? The sum was always correct so it's problem with fdiv part. </p>
  
  <p>Code in c++</p>
  
  <pre><code>#include &lt;iostream.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  extern "C" float f1(float *arr, int);
  
  int main()
  {
      int size=5;
      float *array;
          cout &lt;&lt; "Give your numbers:" &lt;&lt; endl;
      for(int i=0; i&lt;size; i++)
      cin&gt;&gt; *(array+i);
  
      cout&lt;&lt;"\nAverage = "&lt;&lt; f1(array, size) ;
  
      return 0;
  }
  </code></pre>
  
  <p>Assembly</p>
  
  <pre><code>.model SMALL, C
  
  .data
  
  .code
  
  PUBLIC f1
  f1 PROC NEAR
  
      push    BP
      mov     BP, SP      
  
  
  
      mov ax, [bp+6]
      mov cx, ax
      mov ax, [bp+4]
      mov bp, ax
      fld dword ptr [bp]
      dec cl
  
  
      Loop1:
      add bp, 4
      fld dword ptr [bp]
      fadd
      loop Loop1
  
      mov bp, sp
      fdiv dword ptr [bp +6]
  
  
  
      pop BP
      ret
  
  f1 ENDP
  
  .stack
  db 100(?)
  
  END
  </code></pre>