Programming with Ophis | ||
---|---|---|
<<< Previous | Functionals | Next >>> |
Ordinarily, subroutines are called with JSR and finished with RTS. The JSR instruction takes its own address, adds 2 to it, and pushes this 16-bit value on the stack, high byte first, then low byte (so that the low byte will be popped off first).
But wait, you may object. All JSR instructions are three bytes long. This "return address" is in the middle of the instruction. And you would be quite right; the RTS instruction pops off the 16-bit address, adds one to it, and then sets the program counter to that value.
So it is possible to set up a "JSR indirect" kind of operation by adding two to the indirect jump's address and then pushing that value onto the stack before making the jump; however, you wouldn't want to do this. It takes six bytes and trashes your accumulator, and you can get the same functionality with half the space and with no register corruption by simply defining the indirect jump to be a one-instruction routine and JSR-ing to it directly. As an added bonus, that way if you have multiple indirect jumps through the same pointer, you don't need to duplicate the jump instruction.
Does this mean that abusing JSR and RTS is a dead-end, though? Not at all...
<<< Previous | Home | Next >>> |
Functionals | Up | Dispatch-on-type and Data-Directed Assembler |