Programming with Ophis | ||
---|---|---|
<<< Previous | Structured Programming | Next >>> |
The 6502 has an onboard stack in page 1. You can modify the stack pointer by storing values in X register and using txs; an "empty" stack is value $FF. Going into a procedure pushes the address of the next instruction onto the stack, and RTS pops that value off and jumps there. (Well, not precisely. JSR actually pushes a value that's one instruction short, and RTS loads the value, increases it by one, and THEN jumps there. But that's only an issue if you're using RTS to implement jump tables.) On an interrupt, the next instruction's address is pushed on the stack, then the process flags, and it jumps to the handler. The return from interrupt restores the flags and the PC, just as if nothing had happened.
The stack only has 256 possible entries; since addresses take two bytes to store, that means that if you call something that calls something that calls something that (etc., etc., 129 times), your computation will fail. This can happen faster if you save registers or memory values on the stack (see below).
<<< Previous | Home | Next >>> |
Structured Programming | Up | Procedures and register saving |