Table of Contents Previous topic Next topic
C COMPILER->In Line Assembler Code->Accessing Global Variables
Global variables can be directly accessed by name from within the assembler code. The compile adds an underscore to all variable names. Therefore the programmer should add an underscore to enable access for assembler.
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, nGlobal will be in xdata space:
char nGlobal;
int get_byte()
{
#asm
mov dptr,#_nGlobal
movx a,@dptr
#endasm
}
Using the _asm syntax, the compiler will add the underscore for you:
char nGlobal;
int get_byte()
{
_asm( mov dptr,#{nGlobal});
_asm( movx a,@dptr);
}
compiles to:
_get_byte
mov dptr,#_nGlobal
movx a,@dptr
RET
As you can see, {nGlobal} has been replaced by _nGlobal.
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, nGlobal will be in idata space:
char nGlobal;
int get_byte()
{
_asm( mov R0,#{nGlobal});
_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, nGlobal will be in data space:
char nGlobal;
int get_byte()
{
_asm( mov a,{nGlobal});
}