We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I think Keil C51 version 8 has a bug in the code generation. The following source shows the bug:
typedef unsigned char BYTE; typedef unsigned short WORD; #define FIXED_VAULE 64 #define MAKEWORD(h, l) (((WORD )(h) << 8) | (BYTE )(l)) void main(void) { BYTE x = 2; BYTE y = 2; WORD z; // The code generation for this line is incorrect z = MAKEWORD(FIXED_VAULE - x - 1, FIXED_VAULE - y - 1); // The Keil v8 asm listing shows the generated code for this // line is different to the previous line's. z = MAKEWORD(FIXED_VAULE - 1 - x, FIXED_VAULE - 1 - y); // However, if MAKEWORD is defined as: // #define MAKEWORD(h, l) (((WORD )(h) << 8) | (WORD )(l)) // the ASM code for 2 lines are the same. }
Hi,
I don't have access to the compiler at the moment to see what I get.
Could you say what the results of the code are? i.e., what is z after step#1 and step#2.
Is there a difference when the optimisation level is changed?
Maybe you should make z volatile - The compiler might see a chance to optimize out the first calculation to some extent.
Remember, the compiler and optimizer are free to shuffle around code as they see fit - So long as the result is the same.
If the results are correct but the underlying code produced by the compiler is different, then there is (probably) no problem.