Crossware

Table of Contents        Previous topic       Next topic       

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 directives it is necessary to know the name that the C compiler will generate.  Since this name can vary depending on 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 variable located in xdata or code space, you need load its address into the data pointer dptr.  For example if the following is compiled in the large memory model, nStatic will be in xdata space:

static char nStatic;

int get_byte()
{
    _asm(    mov dptr,#{ nStatic });
    _asm(    movx a,@dptr);
}

compiles to:

_get_byte
    mov dptr,#.@2
    movx a,@dptr
    RET

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

To access a variable located in idata space, you need load its address into R0 or R1. For example if the following is compiled in the small memory model, nStatic will be in idata space:

static char nStatic;

int get_byte()
{
    _asm(    mov R0,#{ nStatic });
    _asm(    mov a,@R0);
}

A variable located in data space can be accessed directly.  For example if the following is compiled in the tiny or mini memory model, nStatic will be in data space:

static char nStatic;

int get_byte()
{
    _asm(    mov a,{ nStatic });
}