#364 Optimization error: external RAM...


Dear Sir,

I send to You 3 small 'C' sources compiled with SDCC
version 'mcs51 2.3.1-pj3 (Jan 20 2002) (MINGW)'.

In each of those sources, I try to make this: 'nb =
In source 1, I wrote: nb = _CMD_BUF[0];
In source 2, I wrote: nb = (xdata unsigned char)
In source 3, I wrote: nb = (xdata) _CMD_BUF[0];

Only source 3 is encoded correctly from C to assembler.

_CMD_BUF is a buffer of 256 bytes starting in external
RAM from address 0x0000 and has been declared as
xdata at 0x0000 unsigned char _CMD_BUF[256];

nb is a local variable.

I use the small memory model.

Source 1: (See attachment METHOD1.ZIP)
nb = _CMD_BUF[0]; // (from line 14 in ng7.c1)

The encoding is: (see ng7.ls1)
mov _dummy_process_parm_2,__CMD_BUF.

My conclusion is: due to optimizations, the compiler
forgot that _CMD_BUF[0] was a byte in external RAM.
The compiler encoded it as a byte from internal RAM.

Source 2: (See attachment METHOD2.ZIP)
nb = (xdata unsigned char) _CMD_BUF[0]; // (from line
14 in ng7.c2)

Same as source 1 but with a casting; same results than
from source 1.

Source 3: (See attachment METHOD3.ZIP)
nb = (xdata) _CMD_BUF[0]; // (from line 14 in ng7.c3)

By adding (xdata) before _CMD_BUF[0], the encoding is
now correct: _CMD_BUF[0] is seen by the compiler as
coming from external RAM.

My question is: Is it an optimization problem? Do You
have a solution? Is it correct to use this work around?
(xdata before _CMD_BUF[0]).

Best Regards,


  • Paul Stoffregen

    Paul Stoffregen - 2002-07-26

    Logged In: YES


    No files ended up attached to this bug report.

    I tried to reproduce this, and all three cases were compiled
    correctly with a MOVX instruction. Here's what I tried:

    xdata at 0x0000 unsigned char _CMD_BUF[256];

    char test1(void)
    unsigned char nb;
    nb = _CMD_BUF[0];
    return nb;

    char test2(void)
    unsigned char nb;
    nb = (xdata unsigned char)_CMD_BUF[0];
    return nb;

    char test3(void)
    unsigned char nb;
    nb = (xdata)_CMD_BUF[0];
    return nb;

  • Johan Knol

    Johan Knol - 2003-01-07
    • priority: 5 --> 2
    • milestone: --> unreproducable
  • Johan Knol

    Johan Knol - 2003-01-24
    • status: open --> closed

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks