Q33368 Incorrect Code with /Oal and Unsigned Chars in Loops
C Compiler
5.10   | 5.10
MS-DOS | OS/2

Summary:
   When relaxed alias checking is used with loop optimization, errors
may occur on arithmetic operations on unsigned chars. Turning off either
loop optimization or relaxing alias checking and declaring the
variables in question to unsigned short alleviates the problem.
   Microsoft has confirmed this to be a problem in Version 5.10. We
are researching this problem and will post new information as it
becomes available.

More Information:
   The following code should print the remainder multiplied by ten of
each number entered. However, ProcessVar is erroneously always zero.
   The following is a sample program:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]);     /* Function Prototypes */

int main (argc, argv)
int    argc;
char    *argv[];
{
       unsigned char TestVar;
       unsigned char ProcessVar;
       int           i;

   if(argc < 2)
       printf("usage: %s <integer 1> <integer 2>...<integer n>\n",argv[0]);
   else {
       for (i = 1; i < argc; i++) {
           TestVar = (unsigned char)atoi(argv[i]);
           ProcessVar = (TestVar % 10) * 10;
           printf("ProcessVar: %d\n",ProcessVar);
           }
       }
return (0);
}

   The incorrect assembly for the arithmetic operation is as follows:

;|***                   ProcessVar = (TestVar % 10) * 10;
; Line 19
*** 000045      2a e4                   sub     ah,ah
*** 000047      8b c2                   mov     ax,dx
*** 000049      d0 e2                   shl     dl,1
*** 00004b      d0 e2                   shl     dl,1
*** 00004d      02 d0                   add     dl,al
*** 00004f      d0 e2                   shl     dl,1
*** 000051      88 56 fe                mov     BYTE PTR [bp-2],dl

Keywords:  buglist5.10
Updated  88/08/10 03:33
