Menu

Acessing global symbols from main.c

Bert Lange
2026-01-18
2026-01-18
  • Bert Lange

    Bert Lange - 2026-01-18

    Hi folks!

    I wish to access global symbols from my main:

    void main( void)
    {
        extern uint16_t l__GSINIT;
        printf( "gsinit: %04X\n", l__GSINIT);
    }
    

    But at linker stage I got an error:

    sdldz80  -n  -mwx  -m1 -u  -b _CODE=0x200 -l stuff.lib -l printf.lib  -i build/stuff.ihx  build/crt0.rel build/stuff.rel
    
    ?ASlink-Warning-Undefined Global '_l__GSINIT' referenced by module 'main'
    

    The compiler(?) will add an underscore, because in the map file I can find the symbol:

    ASxxxx Linker V03.00/V05.40 + sdld,  page 1.
    Hexadecimal  [32-Bits]
    
    Area                                    Addr        Size        Decimal Bytes (Attributes)
    --------------------------------        ----        ----        ------- ----- ------------
    .  .ABS.                            00000000    00000000 =           0. bytes (ABS,CON)
    
          Value  Global                              Global Defined In Module
          -----  --------------------------------   ------------------------
         00000000  .__.ABS.                           
         00000000  l__BSEG                         
         00000000  l__BSS                          
     ...
         00000022  l__GSINIT                       
     ...  
    

    How can I make this work?

    regards,
    Bert

     

    Last edit: Bert Lange 2026-01-18
    • Benedikt Freisen

      The compiler prepending an underscore is the expected behavior.
      That happens to prevent name clashes.
      I believe that you can use inline assembly to copy the value of a global variable originating from assembly code to a variable originating from C code, provided that you use the name of the latter with a prepended underscore within the inline assembly code block.

       
  • Bert Lange

    Bert Lange - 2026-01-18

    Ok, thanks Benedikt. This will do:

    uint16_t val; // need to be global (or use static and find the internal name)
    
    void main( void)
    {
        __asm
            ld hl, #(l__GSINIT)
            ld (_val), hl
        __endasm;
        printf( "l_gsinit: %04X\n", val);
    }
    

    regards,
    Bert

     

Log in to post a comment.