Technical Support

A51: GLOBAL REGISTER VARIABLES IN ASSEMBLY

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.

Last Reviewed: Monday, May 10, 2004


Did this article provide the answer you needed?
 
Yes
No
Not Sure