FIX: M6111 Stack Underflow Error, Incorrect Code Optimization
PSS ID Number: Q105079
Article last modified on 01-19-1994

7.00

MS-DOS


----------------------------------------------------------------------
The information in this article applies to:

 - Microsoft C/C++ compiler for MS-DOS, version 7.0
----------------------------------------------------------------------

SYMPTOMS
========

Programs compiled with /Oxa /Gr combination of optimizations while
using the Microsoft C/C++ 7.0 compiler may recieve the following
error:

   run-time error M6111: MATH - floating-point error: stack underflow

CAUSE
=====

Because the /Gr option is being used, the function is returning the
variable "d" (in the sample below) via the coprocessor stack. However,
the /Oxa /Gr combination causes an

   fstp ST(0)

to be generated. This pops "d" from the stack, but on returning from
the function "fd" to the printf call, the compiler generates the
following code:

   fstp  QWORD PTR SS:[bx]  <----- copy ST on to the user stack

Because the stack is already empty, an M6111 stack underflow error is
generated.

RESOLUTION
==========

Following is a list of workarounds for this problem:

 - Use the pragma optimize to disable the /Oa optimization around the
   functions in which you are having the problem. The syntax would be:

      #pragma optimize("a",off)
      #pragma optimize("a",on)

   These lines would go immediately before and after (but outside of)
   the functions where the problem occurs.

 - Remove the optimization for the whole module by turning off the /Oa
   optimization on the cl command line.

 - Remove the /Gr options from the command line.

 - Add the /Op option. This option is used to improve floating point
   consistency.

STATUS
======

Microsoft has confirmed this to be a problem in C/C++ version 7.0.
This problem was corrected in C/C++ version 8.0.

Sample Code
-----------

/* Compile options needed: /Oxa /Gr
*/

#include <stdio.h>

double ad[6];
double d=1;

double fd()
{
int i;
if(d==0)
return 0;

for (i=0;i<6;i++)
    ad[i]+=d;
return d;
}
int main()
{
    printf("%lf\n",fd());
    return 0;
}

Additional reference words: 7.00
KBCategory: Tls
KBSubCategory:

=============================================================================

Copyright Microsoft Corporation 1994.
