Crossware

Table of Contents        Previous topic       Next topic       

C/C++ COMPILER->In-Line Assembler Code->Accessing Static Variables

Static variables are allocated a unique name by the C compiler and so to access it from assembler code using the #asm/#endasm directive is is necessary to know the name that the C compiler will generate.  Since this name can vary depending upon the rest of your code, it is preferable to use the _asm syntax because the compiler will insert the appropriate name for you.

To access a static variable, you need to load its address into an address pointer:

static char nStatic;

int get_byte()
{
    _asm(    move.l #{nStatic},a0);
    _asm(    move.b (a0),d0);
}

compiles to:

_get_byte
    move.l #.@0,a0
    move.b (a0),d0
    RTS

As you can see, {nStatic} has been replaced by .@0 which is the label that the compiler is using to access this particular static variable.