14

I'm wondering how to write a program in Z80 assembler that discovers the value stored in the SP register.

From what I've seen, the only instructions that touch the SP register are:

  • LD SP, HL/IX/IY
  • EX (SP), HL/IX/IY
  • PUSH / POP

However, none of them seem to load the address stored in the SP register anywhere. Is there a way to do it?

1
  • 4
    Incidentally, the Z-80 also has LD SP,nn, LD SP,(mem) and LD (mem),SP. Jan 8 at 4:54
26

The standard way would be adding it to HL. After clearing HL that is.

    LD  HL,0
    ADD HL,SP

Voila.

This is not only already available with the 8080,

    DAD  SP   ; Same opcode (39h), same workings

thus preferable, but as well very handy when setting up a pointer to parameters on stack as, of course, any other constant than 0 can be used and added.

Now, the Z80 did in addition bring a way to store SP with the ED group:

    LD  (loc),SP

With 4 bytes (ED 73 ll hh) it's as long as the LD/ADD sequence and only one T-cycle faster (20 vs. 21) while restricting the store to a fixed address, which may not be always handy - accessing SP is quite often done during task switch or alike where hard coded addresses are a bad idea.

1
  • 3
    Great, I didn't realized that I could add SP. Thanks! Jan 7 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.