FIX: Incorrect Code Generated in Function That Uses _asm

ID: Q114070

1.00 WINDOWS kbtool kbfixlist kbbuglist

The information in this article applies to:

SYMPTOMS

Executing a program built from the sample code shown below after compiling with the optimizing compiler causes the program to hang, crash, or generate a General Protection Fault.

CAUSE

The code generated for the return statements in the Test() function contains incorrect JMP instructions.

RESOLUTION

To work around the problem, use the fast compiler instead of the optimizing compiler. Use of the fast compiler can be forced by compiling with the /f switch.

STATUS

Microsoft has confirmed this to be a problem with C/C++ for MS-DOS version 8.0. This problem was corrected with C/C++ for MS-DOS version 8.0c, include with Visual C++ for Windows, version 1.5.

MORE INFORMATION

The incorrect code can be seen by either generating a code listing file with the /Fc compiler option or by running the executable built from the sample code in the CodeView debugger. The code generated for the two return statements in the Test() function specify the wrong displacement for the JMP instructions.

Sample Code

/* Compile options needed: /f-
* * This sample code must also be compiled as a .CPP file.
*/ 

double Test(const double x, double& dt);

double dub;

void main(void)
{
    Test(0.0, dub);
}

double Test(const double x, double& dt) {

    if(x == 0.0)
        return dt = 1.0;

    double y;

    _asm
    {
        fld x
        fld st(0)
        frndint
        fxch
        fsub st(0),st(1)
        f2xm1
        fld1
        faddp st(1),st(0)
        fscale
        fst st(1)
        fld1
        fdiv st(0),st(1)
        fadd st(2),st(0)
        fsubp st(1),st(0)
        fdivrp st(1),st(0)
        fst y
        fmul st(0),st(0)
        fld1
        fsubrp st(1),st(0)
        les bx,DWORD PTR dt
        fstp QWORD PTR es:[bx]
        fwait
    }

    return y;
}

Additional reference words: 1.00 8.00 inline assembly KBCategory: kbtool kbfixlist kbbuglist KBSubcategory: CodeGen

Keywords          : kb16bitonly kbCodeGen kbbuglist kbfixlist
Version           : 1.00
Platform          : WINDOWS
Solution Type     : kbfix

Last Reviewed: September 22, 1997