[OK] Maximize speed optimization problem

Started by skirby, May 04, 2007, 10:25:47 AM

Previous topic - Next topic

skirby

Hello Pelle,

I don't know if it can be considered as a bug but here is what I have found:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
  int i;
  char s[10];
  strcpy(s, "TEST");

  for (i = 0; i < 2; i++) {
    char c1, c2, r1, r2;

    c1 = s[0]; c2 = s[1];
    r1 = 0; r2 = 0;

    _asm {
      PUSHAD
      MOV AL, BYTE PTR [c1]
      MOV CL, BYTE PTR [c2]
      MOV BYTE PTR [r1], AL
      MOV BYTE PTR [r2], CL
      POPAD
    }
    printf("r1 : %c   ---   r2 : %c\n", r1, r2);
  }


  system("PAUSE");
  return 0;
}


In this small example, the content of r1 and r2 variables is incorrect if optimizations compiler option is Maximize speed.

Build warning :

Building main.obj.
D:\Test\main.c(11): warning #2115: Local 'c1' is initialized but never used.
D:\Test\main.c(11): warning #2115: Local 'c2' is initialized but never used.
Building Test.exe.
Done.

There is no problem if I add c1 and c2 variables in the printf function or if I remove compiler optimization.

Is it normal?

For information, I have done the same test with Visual C++ 2005 and there is no problem.

frankie

The problem is already known, it is due to the fact that the compiler doesn't check variables use in assembler code.
There should be a precedent answer of mine on this issue, anyway to workaround define the variables c1 and c2 with the qualifier 'volatile' as:
  for (i = 0; i < 2; i++) {
    volatile char c1, c2;
    char r1, r2;

Maybe in next versions Pelle will check for variables use in asm blocks.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

skirby

Thanks a lot for the information :)

Quote from: frankie on May 04, 2007, 10:59:07 AM
Maybe in next versions Pelle will check for variables use in asm blocks.

I hope too.