Keil Logo

C51: Using ACC, B, R0-R7 Register in C-Source Code

Information in this knowledgebase article applies to:

  • C51 all Versions


When I use the 8051 processor registers ACC, B or R0-R7 in my C source code, the C51 compiler generates wrong code because it overwrites the values I have written into these registers. Here is an example with ACC:

unsigned char MyArray [10];

20    unsigned char test (void) {
22      ACC = MyArray [10];
23      ACC -= 0x30;
24      return(ACC);
25    }

Is compiled to this code:

             ; FUNCTION test (BEGIN)
                                           ; SOURCE LINE # 20
                                           ; SOURCE LINE # 22
0000 E500        R     MOV     A,MyArray+0AH
                                           ; SOURCE LINE # 23
0002 74D0              MOV     A,#0D0H  <<<<<< ACC is overwritten
0004 25E0              ADD     A,ACC
                                           ; SOURCE LINE # 24
0006 FF                MOV     R7,A
                                           ; SOURCE LINE # 25
0007         ?C0001:
0007 22                RET
             ; FUNCTION test (END)

Is this a compiler bug?


No, this is not a compiler issue. The compiler handles ACC and B like any other SFR and R0 - R7 as a data memory location. C51 does allow access to these registers because they are all memory mapped but it is not aware about their special function. Only in rare cases, you can read or wrote these registers with great care (see link below for parity calculation). C51 automatically assigns registers to C variables according to internal algorithms and therefore might overwrite your assignments.

In most cases the C51 compiler has a very good optimization when standard C variables are used and it is not worth to use direct register access. If you need direct control over these registers, consider to use assembly language or C inline assembly.


  • Refer to ASM in the Cx51 User's Guide.


Last Reviewed: Monday, July 3, 2017

Did this article provide the answer you needed?
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.