FIX: Bad Code Generated for 'loop' When Using Optimizations

Last reviewed: September 18, 1997
Article ID: Q113677
1.00 WINDOWS kbtool kbfixlist kbbuglist

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE), included with:

        - Microsoft Visual C++ for Windows, version 1.0
    

SYMPTOMS

When you use the loop instruction in inline assembly and are enabling optimizations, the C/C++ compiler will generate incorrect code. When the code is run it will produce random run-time errors or possibly hang the machine. This problem can be reproduced by compiling and running the sample code shown below.

RESOLUTION

To work around the problem, do one of the following:

  • Do not use optimization switches when compiling the module that contains the loop instruction.
  • Use the #pragma optimize() directive around the function containing the loop instruction, as indicated in the sample code.
  • Remove the loop instruction and replace it with equivalent instructions, as indicated in the sample code.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed above. This problem was corrected in Visual C++ version 1.5.

Sample Code

/* Compile options needed: any /O options (excluding /Od and /Oo-)
*/

#include <stdio.h>
#define COUNT 2

/*    Uncomment this #pragma to turn off optimizations and correct the
      problem:

#pragma optimize( "", off )
*/

void main()
{
    __asm mov cx, COUNT

    mylabel:
    __asm {
        ;
        ; various assembly instructions
        ;

        loop mylabel

        ; remove the loop instruction above and uncomment
        ; the lines below to correct the problem

        ;dec cx
        ;jnz mylabel
    }
    printf( "Test was successful if no run-time error messages\n" );
    printf( "were displayed!\n" );
}

/*    Uncomment this #pragma to turn optimizations back on:

#pragma optimize( "", on )
*/


Additional reference words: M6111 R6001 8.00 1.00
KBCategory: kbtool kbfixlist kbbuglist
KBSubcategory: CLIss
Keywords : CLIss kb16bitonly kbbuglist kbfixlist kbtool
Version : 1.00
Platform : WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.