Keil -- внимание, грабли.

Hемедленно нажми на RESET, All!

signed bit fields

Does anybody knows, Keil C compiler V7.xx supports bit fields with signed types or not? The code always generated same as for unsigned types. This is compiler bug or feature? My code compiled with Keil C fails on MCU, but works excellent on PC when compiled with gcc.

Example:

struct xxx { signed y: 7; };

struct xxx *s; volatile int i;

i = s->y; /* This operator does not extends y to i with sign, instead all higher bits masked with zeros */ ; i = s->y; MOV DPTR,#s?25158 MOVX A,@DPTR MOV R3,A INC DPTR MOVX A,@DPTR MOV R2,A INC DPTR MOVX A,@DPTR MOV R1,A LCALL ?C?ILDPTR MOV R5,A MOV R4,B ANL A,#07FH MOV R7,A MOV DPTR,#i?25159 CLR A MOVX @DPTR,A INC DPTR MOV A,R7 MOVX @DPTR,A

i=-2, s->y=i; /* This operator again only masking higher bits of i without carying of sign bit */ ; i=-2; MOV DPTR,#i?25159 MOV A,#0FFH MOVX @DPTR,A INC DPTR DEC A MOVX @DPTR,A ; s->y=i; MOV DPTR,#i?25159+01H MOVX A,@DPTR ANL A,#07FH MOV R7,A MOV A,R5 ANL A,#080H MOV R5,A MOV A,R4 MOV R6,A MOV A,R5 ORL A,R7 MOV R7,A MOV A,R6 MOV B,R7 LCALL ?C?ISTPTR

Author Drew Davis

Posted

1/27/05 12:52:20

Toolset C51 RE: signed bit fields Per the ANSI standard, bit fields are always of type "int". Every compiler I've ever used gives you the (extended) ability to declare bitfields in chunks of different widths (char, short, long, etc), but that is a non-standard capability. Also per standard, and despite the fact that bitfields are "ints", the signed-ness of a bitfield and whether or not to do sign extension is up to the implementation. There is no requirement by the language that you can store a signed quantity in a bitfield.

[...skipped...]

Author Keil Reinhard

Posted

1/28/05 1:13:46

Toolset C51 RE: signed bit fields As already indicated in this forum, the behaviour on bitfields is implementation specific.

In Keil C51, bitfields are always 'unsigned'. >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

We recomment to avoid bitfields and use instead 8051 bits and bdata variables.

Reply to
Kirill Frolov
Loading thread data ...

Hello Kirill.

29 Jan 05 11:51, you wrote to All:

... KF> As already indicated in this forum, the behaviour on bitfields is KF> implementation specific. >> In Keil C51, bitfields are always 'unsigned'. >>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

KF> We recomment to avoid bitfields and use instead 8051 bits and bdata KF> variables.

а слабо всякие такие приколы куда-нить сразу в wiki складывать?

Vladimir PS Hа openhardware.ru кстати wiki есть...

Reply to
Vladimir V. Teplouhov

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.