#424 Typecast is ignored

closed-fixed
Bernhard Held
5
2013-05-25
2002-12-25
Tzvetan Mikov
No

The code bellow should truncate the result to 8-bit, but
it doesn't. The compiler seems to ignore the cast and
returns 16-bit result.

unsigned func ( unsigned char a, unsigned char b )
{
return (unsigned char)(a + b);
}

The generated code for MCS51 is:

_func:
mov r2,dpl
mov a,_func_PARM_2
add a,r2
mov r2,a
clr a
addc a,#0x00
mov r3,a
mov dpl,r2
mov dph,r3
ret

Ignoring possible optimizations, it should be something
like:

_func:
mov r2,dpl
mov a,_func_PARM_2
add a,r2
mov r2,a
clr a
mov r3,a
mov dpl,r2
mov dph,r3
ret

Discussion

  • Bernhard Held
    Bernhard Held
    2002-12-25

    • milestone: --> fixed
    • assigned_to: nobody --> bernhardheld
    • status: open --> closed-fixed
     
  • Bernhard Held
    Bernhard Held
    2002-12-25

    Logged In: YES
    user_id=203539

    Not reproducable in 2.3.2:

    _func:
    ar2 = 0x02
    ar3 = 0x03
    ar4 = 0x04
    ar5 = 0x05
    ar6 = 0x06
    ar7 = 0x07
    ar0 = 0x00
    ar1 = 0x01
    ; genReceive
    mov r2,dpl
    ; u.c:3: return (unsigned char)(a + b);
    ; genPlus
    mov a,_func_PARM_2
    add a,ar2
    mov r2,a
    ; genCast
    mov r3,#0x00
    ; genRet
    mov dpl,r2
    mov dph,r3
    00101$:
    ret

     
  • Tzvetan Mikov
    Tzvetan Mikov
    2002-12-25

    Logged In: YES
    user_id=675109

    I just downloaded 2.3.2 and it behaves incorrectly, although
    in a different respect. Implicit integral promotion seems to be
    broken. The same code without the typecast should
    calculate 16-bit result, but it truncates to 8-bits.