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

ID: Q113677

1.00 WINDOWS kbtool kbfixlist kbbuglist

The information in this article applies to:

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:

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          : kb16bitonly kbCompiler kbbuglist kbfixlist
Version           : 1.00
Platform          : WINDOWS
Solution Type     : kbfix

Last Reviewed: September 22, 1997