0
\$\begingroup\$

I am trying to design a board for a Z80 based computer, and am having trouble designing the memory map. I have found that the Program Counter defaults to address #0000, but I cannot find where the Stack Pointer defaults to. Is it also #0000? Am I just supposed to set it to whatever I want on startup?

Thank you.

Z80 Datasheet

\$\endgroup\$
  • 1
    \$\begingroup\$ Yes, you set it yourself before you do any operations that require the stack, such as calling subroutines. \$\endgroup\$ – Dave Tweed Mar 9 '20 at 19:01
3
\$\begingroup\$

You would normally set the stack pointer at the start of your program.

I think the usual initial value for the stack pointer would be the top of available RAM (but its a long time since I used an 8085/Z80 style microprocessor).

\$\endgroup\$
0
\$\begingroup\$

Doesn't the stack pointer, among other CPU registers like PC,..reside in CPU?

But if you are asking to which address the stack pointer is pointing at, then is a different story. Don't know about Z80, but usually it points to the top off the available memory e.g. -1 (FFFF) and it decrements, it makes sense since you are loading static variables from bottom to the top , then the CPU can't create a stack and then clear it in the middle of allocated variables or program (von Neuman). So the only way to make a stack on top of the stack, on top of the stack.... is to store variables from top down, until the stack grows so it "touches" the allocated data on bottom - this is so called stack overflow. The stacks are built up at every function call and then nested function calls, it is purged in the reverse order.

EDIT: Swapped bottom and top.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ I would call 0xFFFF the top of a 64K memory - it is the highest address. \$\endgroup\$ – Peter Bennett Mar 9 '20 at 23:14
  • \$\begingroup\$ Yes you can. I am not an big expert in computer science, so there may be also some misuse of synonyms in my answer. I shall correct them. \$\endgroup\$ – Marko Buršič Mar 10 '20 at 7:08

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.