Menu

#2153 z80 crt0 wastes a byte above stack

closed-fixed
None
Z80
2
2013-12-18
2013-04-11
No

The crt0.s file for the z80 port (version #8471 used here) contains this section:

init:
;; Stack at the top of memory.
ld sp,#0xffff

According to Zilog's documentation, the data from a push command (or call, etc...) is stored at SP-1 (high byte) and SP-2 (low byte). This means that a push will store at 0xfffe and 0xfffd, wasting the byte at address 0xffff.

Whilst this is safe and causes no problems, and based on the assumption that the stack pointer wraps around at 0 and 0xffff, it may be worth looking into whether the stack should be started at 0x0000, meaning the first push/call will store at addresses 0xfffe and 0xffff.

Discussion

  • Philipp Klaus Krause

    • Priority: 8 --> 2
     
  • Philipp Klaus Krause

    Wasting a single byte is not top priority.

    Philipp

    P.S.: I wonder if saving that byte is even the safe thing to do: People might use custom crt0.o that goes to main() using jp instead of call, and main() might not use a frame pointer, but have a singel byte of lcoal vars on stack. In that case, a read from that variable might be generated as a pop followed by push, which would then read one byte above the top, wrapping to address 0. If there is memory-mapped I/O at address zero, this would cause problems.

     
  • Brian Ruthven

    Brian Ruthven - 2013-07-16

    Wasting a single byte is not top priority.

    Understood. I may have got the priorities the wrong way round when I logged it ;-)

    P.S.: I wonder if saving that byte is even the safe thing to do: People might use custom crt0.o ..

    If they were using a custom crt, then any change to the initial sp value in the supplied crt0 would not affect them.

    Even with the current starting value of 0xffff, a pop to retrieve a single-byte arg would read 1 byte from address 0xffff and one byte from 0x0, so any concerns about mem-mapped I/O at address 0 exist now anyway.

    At the end of the day, I don't particularly care as I use my own crt anyway, but I thought others could benefit from the extra byte :-)

     
  • Philipp Klaus Krause

    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
     
  • Philipp Klaus Krause

    Fixed in revision #8911 (also for z180, r2k, r3ka and gbz80 ports).

    Philipp

     

Log in to post a comment.