#197 Bad code generated for Parity flag

closed
None
5
2007-02-09
2007-02-05
Jan Rejlek
No

This function always returns even parity:

#include <8051.h>

bit getParity(char c)
{
ACC = c;
return P;
}

return !P; or return !(!P); works well.

Compiled with SDCC Version 2.6.3 #4543 (Dec 31 2006) invoked by command sdcc -c test.c.

The Parity flag is overwritten by clearing accumulator before it is used.

; genReceive
mov _ACC,dpl
; test.c:6: return P;
; genRet
clr a <--- the P flag is overwritten here
mov c,_P
rlc a
add a,#0xff
; Peephole 500 removed redundant label 00101$
ret

Best Regards
Jan

Discussion

  • Bernhard Held

    Bernhard Held - 2007-02-05

    Logged In: YES
    user_id=203539
    Originator: NO

    This isn't really a bug. Nobody guarantees you correct behaviour, if you directly access ACC, P or any other register or bit from C statements.

    You might like this solution:

    bit getParity (char c) _naked
    {
    c; /* hush compiler */
    {
    _asm
    mov a,dpl
    mov cy,p
    ret
    _endasm;
    }
    }

    Without doubt the emitted code is doing unessary conversions. Without these converions sdcc will show the expected behaviour. I'll move it to the feature requests and have a look later.

     
  • Bernhard Held

    Bernhard Held - 2007-02-05
    • assigned_to: nobody --> bernhardheld
     
  • Bernhard Held

    Bernhard Held - 2007-02-09
    • status: open --> closed
     
  • Bernhard Held

    Bernhard Held - 2007-02-09

    Logged In: YES
    user_id=203539
    Originator: NO

    Fixed in src/mcs51/gen.c (genRet), rev. 4626

    My example is still 1 byte smaller, if "mov cy,p" is replaced by "mov c,p" ;-)

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks