#2239 incorrect address generated for global with offset

open
nobody
None
other
7
2014-02-24
2013-12-29
ClinicallyObeast
No
unsigned char data[100];
unsigned char* datapos;

#define ENT_DAT(_p) (*((unsigned int*)((unsigned char*)(_p) + 3)))

void setFoo()
{
    // this line goes wrong. the address generated should be
    // datapos -5 + 3 but you get datapos + 0x00FE (not 0xFFFE)
    ENT_DAT(datapos - 5) = 0xffff;
}

int main()
{
    datapos = data + 5;
    setFoo();
    return 0;
}

code i get:

_setFoo:
;bug1.c:9: ENT_DAT(datapos - 5) = 0xffff;
    ld  a,(#_datapos + 0)
    add a, #0xFE
    ld  d,a
    ld  a,(#_datapos + 1)
    adc a, #0x00                     <<<<< is wrong, should be 0xff ?
    ld  e,a
    ld  l, d
    ld  h, e
    ld  (hl),#0xFF
    inc hl
    ld  (hl),#0xFF
    ret
_setFoo_end::

sdcc -v
SDCC : mcs51/gbz80/z80/z180/r2k/r3ka/ds390/pic16/pic14/TININative/ds400/hc08/s08
3.3.0 #8604 (May 11 2013) (MINGW32)

more info contact,

hughserious@gmail.com

thanks,
-- hugh.

Discussion

<< < 1 2 (Page 2 of 2)
  • Increasing priority, since bad code is generated silently.

    Philipp

     
    • summary: incorrect address generated for global with offset Z80 --> incorrect address generated for global with offset
     
  • Maarten Brock
    Maarten Brock
    2014-02-24

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,7 +1,8 @@
    +~~~~
     unsigned char data[100];
     unsigned char* datapos;
    
    -\#define ENT_DAT(_p) (*((unsigned int*)((unsigned char*)(_p) + 3)))
    +#define ENT_DAT(_p) (*((unsigned int*)((unsigned char*)(_p) + 3)))
    
     void setFoo()
     {
    @@ -17,9 +18,10 @@
         setFoo();
         return 0;
     }
    -
    +~~~~
     code i get:
    
    +~~~~
     _setFoo:
     ;bug1.c:9: ENT_DAT(datapos - 5) = 0xffff;
        ld  a,(#_datapos + 0)
    @@ -35,7 +37,7 @@
        ld  (hl),#0xFF
        ret
     _setFoo_end::
    -
    +~~~~
     sdcc -v
     SDCC : mcs51/gbz80/z80/z180/r2k/r3ka/ds390/pic16/pic14/TININative/ds400/hc08/s08
      3.3.0 #8604 (May 11 2013) (MINGW32)
    
     
<< < 1 2 (Page 2 of 2)