Calling section addresses from C

Comments

9 comments

  • Avatar
    Paul Curtis

    I think you need three underscores.

    0
    Comment actions Permalink
  • Avatar
    John Wood

    Hello Paul,

    Even with three underscores each end I still get:

    undeclared identifier '__CODE_start__'

    Although oddly, the error line only has two underscores, even though there are three in the source file?

    Thanks, John

     

    0
    Comment actions Permalink
  • Avatar
    John Wood

    Hello Paul,

    I should add that I'm also trying to use the address in my header file to setup my data memory size:

    #define START_DATA 0x1200     // must start & end on 512 byte boundary
    #define END_DATA &___CODE_start___  //0xac00       // make sure this matches memory map file!
    #define MSP430_MEMSIZE (END_DATA - START_DATA)/512
    Perhaps it doesn't work in this way?

    John

    0
    Comment actions Permalink
  • Avatar
    Brian Myers

    Hello John -

    Unrelated to your specific question, but - it is always a good idea to enclose any complex #define with (), like this :

    #define MSP430_MEMSIZE ((END_DATA - START_DATA)/512)


    Been bitten by this once or twice. You are probably safe with this one, since your subtraction is already enclosed, and multiply/divide is left-to-right; but it's good practice anyway and does no harm.

     

    0
    Comment actions Permalink
  • Avatar
    Paul Curtis

    These are linker-generated symbols that the C compiler knows nothing about.

    Hence, you need to have something at the C level to hitch it to:

    extern unsigned char __CODE_end[];

    0
    Comment actions Permalink
  • Avatar
    Paul Curtis
    #include <__cross_studio_io.h>

    extern unsigned char __begin_CODE[];
    extern unsigned char __end_CODE[];

    void
    main(void)
    {
      debug_printf("Code size = %d bytes\n", __end_CODE-__begin_CODE);
      debug_exit(0);
    }
    0
    Comment actions Permalink
  • Avatar
    John Wood

    Thanks Paul,

    I got there after a bit of trial and error. My code now looks like:

    extern  unsigned char __begin_CODE[];      // two underscores, and __begin_CODE, __end_CODE, not CODE_start
    // Address limits of data memory
    #define START_DATA 0x1200     // must start & end on 512 byte boundary
    #define END_DATA (unsigned int)__begin_CODE  //0xac00       // make sure this matches memory map file! 
    #define MSP430_MEMSIZE ((END_DATA - START_DATA)/512)

    However, every time I use MSP430_MEMSIZE, it does the full calculation, which adds quite a bit to my code size, as compared to putting in a number for the start of code. Is there any way of persuading the compiler and linker to just do the calculation once at build time, because the sections don't change thereafter?

    Thanks, John

    0
    Comment actions Permalink
  • Avatar
    Paul Curtis

    There is no possibility to compute this at compile time or link time.  It must be done at runtime.

    0
    Comment actions Permalink
  • Avatar
    Jordi Castells Moreno

    that is true from C code, not from assembler code, yoy can just add

     

            public _CODE_Size
    _CODE_Size          equ ___end_CODE - ___begin_CODE 

            public _CODE_Size

    _CODE_Size          equ ___end_CODE - ___begin_CODE 

    in assembler there are allways one _ character more than in C

    into an assembler code (or just modify crt0.asm if you do not use any)

    then in your C code add

    extern unsigned int CODE_size (no underscore here), and use &CODE_size as the size of your code not the value (that has no sense due it is a label not a real variable)

     

     

    0
    Comment actions Permalink

Please sign in to leave a comment.