Menu

#2334 Z80 HL overuse

closed-fixed
nobody
None
Z80
7
2015-09-11
2014-12-23
No

SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/pic16/pic14/TININative/ds400/h
c08/s08/stm8 3.4.0 #8981 (Apr 5 2014) (MINGW64)
published under GNU General Public License (GPL)

code:

   void foobar(unsigned char x, unsigned char y)
   {
       char* bar = (char*)0x3c00;
       bar += ((int)y<<6);
       bar += x;
       *bar = 0;
   }

   main()
   {
       unsigned char x = 0;
       unsigned char y = 15;
       foobar(x, y);
       return 0;
   }

command to run:

    sdcc -mz80 main.c

Now look at the z80 code generated by foobar. I get:

   ; -------------------------------
   ; Function foobar
   ; ---------------------------------
   _foobar_start::
   _foobar:
   ;main.c:4: bar += ((int)y<<6);
    ld  iy,#3
    add iy,sp
    ld  l,0 (iy)
    ld  h,#0x00
    add hl, hl
    add hl, hl
    add hl, hl
    add hl, hl
    add hl, hl
    add hl, hl
    ld  bc,#0x3C00
    add hl,bc
   ;main.c:5: bar += x;
    ld  a,l
    ld  hl,#2         HERE is the bug. H calculated in the previous
    add hl,sp         statement is overwritten. there is no way
    add a, (hl)       DE becomes HL + x
    ld  e,a
    ld  a,h           <- the wrong H !!
    adc a, #0x00
    ld  d,a
   ;main.c:6: *bar = 0;
    xor a, a
    ld  (de),a
    ret
   _foobar_end::

i am hughserious@gmail.com

thanks for any help,
-- hugh.

Discussion

  • Philipp Klaus Krause

    • Priority: 5 --> 7
     
  • Philipp Klaus Krause

    • status: open --> pending-works-for-me
     
  • Philipp Klaus Krause

    I can't reproduce this issue in sdcc 3.4.1 #9133.

    Plese check if sdcc from svn or a current snapshot work for you.

    Philipp

     
  • Ben Shi

    Ben Shi - 2015-09-11
    • status: pending-works-for-me --> closed-fixed
     
  • Ben Shi

    Ben Shi - 2015-09-11

    This bug did exist in release 3.4.0, but had been fixed before reversion 9133.

     

Log in to post a comment.

MongoDB Logo MongoDB