Keil Logo

C51: COMPILER OPTIMIZES OUT NECESSARY XDATA READS


Information in this article applies to:

  • C51 Version 5.50a
  • C51 Version 6.00 Beta
  • C51 Version 6.00

SYMPTOMS

We are interfacing to an XDATA device that requires several reads from the same address. For example:

void func (void)
{
unsigned char xdata xdata_junk;
unsigned char xdata *p = &xdata_junk;
unsigned char t1, t2;

t1 = *p;
t2 = *p;
}

In this example, the compiler should read from xdata_junk for each of the assignments to t1 and t2. But, it doesn't.

0000 7E00    R     MOV     R6,#HIGH xdata_junk
0002 7F00    R     MOV     R7,#LOW xdata_junk
;---- Variable 'p' assigned to Register 'R6/R7' ----

0004 8F82          MOV     DPL,R7
0006 8E83          MOV     DPH,R6
0008 E0            MOVX    A,@DPTR

0009 F500    R     MOV     t1,A

000B F500    R     MOV     t2,A
000D 22            RET

CAUSE

This is normal operation for most good C compilers. The optimizer figures out that it can optimize out the second read of xdata_junk.

RESOLUTION

If you require the second read, then change the declaration for that variable and the pointer to it to volatile data types. For example:

void func (void)
{
volatile unsigned char xdata xdata_junk;
volatile unsigned char xdata *p = &xdata_junk;
unsigned char t1, t2;

t1 = *p;
t2 = *p;
}

This will generate the following assembly code:

0000 7E00    R     MOV     R6,#HIGH xdata_junk
0002 7F00    R     MOV     R7,#LOW xdata_junk
;---- Variable 'p' assigned to Register 'R6/R7' ----

0004 8F82          MOV     DPL,R7
0006 8E83          MOV     DPH,R6

0008 E0            MOVX    A,@DPTR
0009 F500    R     MOV     t1,A

000B E0            MOVX    A,@DPTR
000C F500    R     MOV     t2,A
000E 22            RET

In this example, a MOVX is generated for each read of the xdata variable.

Last Reviewed: Sunday, March 12, 2000


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.