Menu

[z80] initialization of globals

Help
sverx
2015-04-06
2015-04-16
  • sverx

    sverx - 2015-04-06

    I see that globals explicitly initialized to some value get initialized thru a copy in crt0... but I don't get how/when other globals are initialized.
    I mean, doesn't C require that every global is initialized to 0 when not explicitly set to a value?

     

    Last edit: sverx 2015-04-06
  • Philipp Klaus Krause

    Some platforms do the zeroing in the BIOS / loader, so it is left out of the default crt0.s (I guess for historic reasons, maybe the original developers of the z80 port were using such a platform). On platforms where this doesn ot happen, you need to zero the memory in your custom crt0.s (e.g. libcv, http://colecovision.eu/ColecoVision/development/libcv.shtml for the ColecoVision).

    Philipp

     
  • sverx

    sverx - 2015-04-07

    So you mean that actually if I declare a global like this:

    unsigned int myGlobal=0;
    

    it will be initialized but if I declare it like this:

    unsigned int myGlobal;
    

    it won't get initialized so actually it could end up with a bogus initial value if I don't provide a system to clear RAM in crt0?

     
    • Philipp Klaus Krause

      Yes. It doesn't make much sense, but that's how it currently is for historical reasons.

      Philipp

       
      • sverx

        sverx - 2015-04-08

        OK. So, since you're not doing it in the provided z80 crt0 I guess you should write somewhere in the docs that globals won't be initialized if you don't do it explicitly.

        Thanks for your help :)

         
  • Maarten Brock

    Maarten Brock - 2015-04-16

    Instead of documenting this I think we should change it first. The default should be to zero the memory for uninitialized globals/statics. And if you don't need that, you can remove it in your custom crt0.

     
    • sverx

      sverx - 2015-04-16

      I agree, but how can you do that? I mean I have this code added to my crt0:

      xor a                ; clear RAM (to value 0x00)
      ld hl,#0xc000        ; by setting value 0
      ld (hl),a            ; to $c000 and
      ld de,#0xc001        ; copying (LDIR) it to next byte
      ld bc,#0x1ff0        ; for 8 KB minus 16 bytes
      ldir                 ; do that
      

      which of course works, but assumes RAM is at 0xC000 and 8 KB big, but that's of course platform dependent...

       
  • Philipp Klaus Krause

    The linker provides symbols for location and length of of segments.

    Philipp

    P.S.: I'd write their names here, but Sourceforge markup keeps messing them up.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.