On boot, MOS will set up a stack. As MOS runs in ADL mode, this means the underlying SPL register is set up to point to that stack, which is presented as the SP register to ADL-mode code.
No stack is set up for Z80 mode code, i.e. the underlying SPS register is not set up. Thus programs run in Z80 mode should consider the SP register as undefined on startup.
To protect the MOS stack, on running a new ADL-mode program MOS could create a new stack by allocating a chunk of memory on the heap, and set SP to point to that before running the program, saving the current MOS stack pointer. Once execution has been returned from the program, the original MOS stack can be restored.
As part of this idea, it may be worth considering whether making an API call from code running in ADL-mode should just use the SP as-is, using the program's stack, or whether it should swap it out for the saved MOS stack pointer.
On boot, MOS will set up a stack. As MOS runs in ADL mode, this means the underlying
SPLregister is set up to point to that stack, which is presented as theSPregister to ADL-mode code.No stack is set up for Z80 mode code, i.e. the underlying
SPSregister is not set up. Thus programs run in Z80 mode should consider theSPregister as undefined on startup.To protect the MOS stack, on running a new ADL-mode program MOS could create a new stack by allocating a chunk of memory on the heap, and set
SPto point to that before running the program, saving the current MOS stack pointer. Once execution has been returned from the program, the original MOS stack can be restored.As part of this idea, it may be worth considering whether making an API call from code running in ADL-mode should just use the SP as-is, using the program's stack, or whether it should swap it out for the saved MOS stack pointer.