I'm new to assemblers, so here is a simple question:
My custom subroutines change the X, Y, and A registers. They manipulate these to produce the desired results. Is it a good idea to push these values to the stack when the routine starts and restore them before RTS?
I mean, this way I can write routines which can be called from anywhere without messing up the "state" or affecting other routines. But is it OK to use the stack this way? Or is there a better way to do this?
Absolutely; BASIC does it all the time, as do many routines in the kernal.
But, there is no right answer to this, it comes down to at least speed, portability, and style.
If you use the stack a lot, there are some speed considerations. Your typical
phatxaphatyaphaat the start, and then the reverse (platayplataxpla) eats up 3 bytes of your stack, and adds in some cycle time due to the 2 x 5 operationsYou could use zero page, but that takes away some portability between different machines; VIC-20, C64, C128, the free zero page addresses may not be the same across platforms. And your routine can't be called "more than once" without exiting first (e.g. no recursion) because if it is called while it is active, it will overwrite zero page with new values. But, you don't need to use zero page...
...because you can just create your own memory locations as part of your code:
the downside to this is that your routine can only be called "once", otherwise subsequent calls will overwrite your storage areas (e.g. no recursion allowed!!, same problem as before!)
You could write your own mini-stack
This has the added benefit that you now have 3 virtual stacks (one for each of
.A,.X,.Y), but at a cost (not exactly a quick routine). And because we are usingSEIandCLI, you may need to re-think this if doing it from an interrupt handler. But this also keeps the "true" stack clean and more than triples your available space.