Keil Logo

A251: Accessing Global Variables Defined in C Code


Information in this article applies to:

  • C251 Version 2.14

QUESTION

I have the following global variable defined in my C code. How can I access it from assembly code?

char data send_it _at_ 0x07;

ANSWER

Use the following line in your assembly code. It is the equivalent of a C 'extern' declaration for the global variable:

EXTRN         DATA : BYTE (send_it)

You can easily determine what the equivalent assembler code is for your C code by getting the compiler to generate the assembler for you. For example, to find out the assembler required for the extern declaration you could write the following C source file:

#pragma src
extern char data send_it _at_ 0x07;

void main(void)
{
  send_it = 7;
}

The assignment is included to prevent the optimizer from removing the variable access.

Compiling this file generates an .src file which contains the equivalent assembler code. Looking at the variable declarations in this file shows how to declare the 'extern' in C and access the global variable.

MORE INFORMATION

  • Refer to EXTERN in the Ax51 User's Guide.
  • Refer to SRC in the Cx51 User's Guide.

Last Reviewed: Wednesday, September 21, 2016


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.