Crossware

Table of Contents        Previous topic       Next topic       

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

Global variables can be directly accessed by name from within the assembler code.  The compiler adds an underscore to all global variable names.  Therefore the programmer should add an underscore to enable access from assembler.

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

char nGlobal;

int get_byte()
{
#asm
    move.l #_nGlobal,a0
    move.b (a0),d0
#endasm
}

Using the _asm syntax, the compiler will add the underscore for you:

char nGlobal;

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

compiles to:

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

As you can see, {nGlobal} has been replaced by _nGlobal.