Keil Logo

A51: Global Register Variables In Assembly


Information in this article applies to:

  • C51

QUESTION

I have an 8051 program that uses code written in C and assembly. I want my assembly code to have exclusive access to several registers for global variables. These are not used in my C code, however, the code from the C compiler uses all registers (R0 to R7). Is there a way to get around this problem and how do I keep my C code from overwriting global variables stored in registers by my assembler code?

ANSWER

The easiest way to do this is to use a different register bank in your assembly code.

For example, by default, the C compiler uses register bank 0 for functions you create. If you select register bank 1 for your assembly routines (remember RB0 and RB1 in the PSW?) then your global register variables in your assembly modules will never get overwritten by your C code.

Make sure that your C code doesn't ever use register bank 1. Also, make sure that your assembly code specifies the RB(1) directive, so the linker "knows" that the 8-bytes from 0x08 to 0x0F are used. That way, it won't try to locate variables there.

MORE INFORMATION


Last Reviewed: Thursday, February 25, 2021


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.