NO

Author Topic: [OK] Maximize speed optimization problem  (Read 3101 times)

skirby

  • Guest
[OK] Maximize speed optimization problem
« on: May 04, 2007, 10:25:47 AM »
Hello Pelle,

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

Code: [Select]
#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.
« Last Edit: May 04, 2007, 01:26:57 PM by skirby »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Maximize speed optimization problem
« Reply #1 on: May 04, 2007, 10:59:07 AM »
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:
Code: [Select]
  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

  • Guest
Re: Maximize speed optimization problem
« Reply #2 on: May 04, 2007, 01:26:45 PM »
Thanks a lot for the information :)

Maybe in next versions Pelle will check for variables use in asm blocks.

I hope too.