KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q66923: _fastcall Code Generation Error with Shifted Operands

Article: Q66923
Product(s): See article
Version(s): 6.00 6.00a  | 6.00 6.00a
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | buglist6.00 buglist6.00a fastcall | mspl13_c
Last Modified: 18-NOV-1990

When using _fastcall, shifted immediate operands may be passed
incorrectly. In the example below, the two parameters to be passed to
the sub1() function are stored in ax and dx (per _fastcall
convention). Because of a problem in the compiler, the correct values
are not used in this case. As a workaround, either use the /Os
optimization or assign the value to a temporary variable and pass that
variable instead.

Sample Code
-----------

#include <stdio.h>

int _fastcall sub1(int i, int j);

long a=0x12345678;

void main(void)
{
   int llama;

   printf("%x\n",(int)(a>>15));
   llama = sub1( (int)(a>>15), (int)(a>>15) );
   printf("%x\n",llama);
}

int _fastcall sub1(int i, int j)
{
   printf("%x \t %x\n",i,j);
   return(i);
}

In all optimizations except /Os and /Od, the compiler calculates the
right-shift value correctly but passes the high-order two bytes of the
result instead of the low-order two bytes. When compiled with /Od, the
generated code correctly passes one variable but not the other.
Finally, with the /Os optimization, a helper function performs the
shift calculation and the correct values are passed in both cases.

Microsoft has confirmed this to be a problem in C versions 6.00 and
6.00a. We are researching this problem and will post new information
here as it becomes available.

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.

Copyright Microsoft Corporation 1986-2002.