Saving Z80 stack pointer location

Discussion in 'Programmer's Corner' started by Robin Mitchell, Jul 9, 2014.

  1. Robin Mitchell

    Thread Starter Well-Known Member

    Oct 25, 2009
    732
    199
    Hi everyone,

    Im in a bit of a pickle!
    Just about every register in the Z80 can be saved with the exception of the stack pointer, that is, it can only be written to :/

    Does anyone know of a way to save the position that the stack pointer is currently pointing to? For example, a instruction that I would have loved in the Z80 would be LD HL, SP where HL gets loaded with the value of the SP register.

    Many thanks guys,
    Robin
     
  2. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,729
    How about:


    Code ( (Unknown Language)):
    1.     LD  (SPBUFF),SP
    2.     LD  HL,(SPBUFF)
    Where SPBUFF is the address of a 2 byte RAM location?
     
    Robin Mitchell and Papabravo like this.
  3. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    That'll do it.
     
  4. Robin Mitchell

    Thread Starter Well-Known Member

    Oct 25, 2009
    732
    199
    Well, im really happy now :)

    Thanks sooo much!
     
  5. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,729
    You're welcome.

    I had to dig up some code from 1980 (I was 13 at the time) to remember how to do that. I wrote a tape base "operating system" for the TRS-80, and it included a machine language monitor/debugger. I used that sequence of instructions to capture and display the stack pointer value after a break point.
     
  6. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    Wow, I'm impressed that you still have code from 1980 all my Z80 stuff is long gone.
     
  7. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,729
    I lost a lot of stuff, including my original customized TRS-80 Model I. Very depressing. I've got a lot of yellowed printouts, though.
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
Loading...