Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#1866 PIC16: Global variables not initialized to 0 by default

open-later
nobody
PIC16
5
2013-07-16
2011-10-28
Diego Herranz
No

SDCC : mcs51/gbz80/z80/ds390/pic16/pic14/TININative/ds400/hc08 3.0.0 #6037

uint8_t variable0 = 0;
uint8_t variable1;

Declaring these 2 global variables, both should be initialized to 0 according to ANSI, but only variable0 is.

Bug sample attached showing that variable1 is not initialized to 0 because LED1 blinks (check simple code attached).
When running this code on a simulator it works correctly but I think that it is because RAM values are not random on start. However it's not working on real-life PIC.

Category "none" because I don't know where the bug is.

If you need anything else please let me know.
Thank you very much

P.S: hex file is different if variables are declared this way (and I think that hex file shouldn't change):

uint8_t variable0;
uint8_t variable1;

Discussion

<< < 1 2 (Page 2 of 2)
  • Bodo Wenzel
    Bodo Wenzel
    2011-11-01

    What is the problem with clearing just the .bss section in crt0iz?

    They are quite rare but in the last 20 years I have at least written two applications which need a preserved memory outsise .data and .bss. Well, they were no PIC applications. ;-)

     
  • Raphael Neider
    Raphael Neider
    2011-11-01

    The problem is that we do not know the location(s) of the .bss sections at runtime. Possibly, one can patch gputils to provide this information along with the initializer list: It currently provides us with (src, dst, size) records, instruction sdcc to copy SIZE bytes from SRC (supposed to be in __code space) to DST (in __data space). As a hack, one could use records with SRC == 0 to denote a "set SIZE bytes at DST to 0" .bss section ...
    Alternatively, one could introduce a larger "header" to the list, augmenting it from its current contents (number of records to follow) to include a second counter for .bss records right after the regular ones.
    This should probably be discussed on the gputist list.

     
  • Borut Ražem
    Borut Ražem
    2011-11-01

    For the beginning I can change the gputils linker to generate section begin and section end symbols fore each section, for example:
    _?<section_name>?begin
    _?<section_name>?end

    I doubt that this will be of much help since sdcc generates a separate section for each C variable:

    char c[10];
    int a;
    long b;

    generates:

    ;--------------------------------------------------------
    ; global definitions
    ;--------------------------------------------------------
    UD_t_0 udata
    _c res 10

    UD_t_1 udata
    _a res 2

    UD_t_2 udata
    _b res 4

    Borut

     
  • Diego Herranz
    Diego Herranz
    2013-04-13

    Now that 3.3.0 is coming, should we use crt0iz by default for a better ANSI compliance?

     
    • Category: --> PIC16
     
<< < 1 2 (Page 2 of 2)