This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

A bug in C51 code generation

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.
}

Parents
  • The incorrectly generated code is:

    ;---- Variable 'x' assigned to Register 'R5' ----
    ;---- Variable 'y' assigned to Register 'R4' ----
        MOV     R4,#02H
        CLR     C
        MOV     A,#03FH
        SUBB    A,#02H
        MOV     R7,A
        MOV     A,R7
        MOV     R6,A
        MOV     A,R4
        CPL     A         ; Incorrect
        INC     A         ; Incorrect
        CLR     C         ; Incorrect
        SUBB    A,#03FH   ; Incorrect
        MOV     R3,A
        MOV     A,R6
        MOV     z,A
        MOV     A,R3
        MOV     z+01H,A
    

    z should be 0x3d3d. However, the result of the incorrectly generated code is 0x3dbf

Reply
  • The incorrectly generated code is:

    ;---- Variable 'x' assigned to Register 'R5' ----
    ;---- Variable 'y' assigned to Register 'R4' ----
        MOV     R4,#02H
        CLR     C
        MOV     A,#03FH
        SUBB    A,#02H
        MOV     R7,A
        MOV     A,R7
        MOV     R6,A
        MOV     A,R4
        CPL     A         ; Incorrect
        INC     A         ; Incorrect
        CLR     C         ; Incorrect
        SUBB    A,#03FH   ; Incorrect
        MOV     R3,A
        MOV     A,R6
        MOV     z,A
        MOV     A,R3
        MOV     z+01H,A
    

    z should be 0x3d3d. However, the result of the incorrectly generated code is 0x3dbf

Children