ARM Technical Support Knowledge Articles

COMPILER OPTIMIZES OUT NECESSARY XDATA READS

Applies to: C51 C Compiler

Answer


Information in this article applies to:


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.

Article last edited on: 2000-03-12 00:00:00

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential