escape from nested subroutines
Posted: Mon May 22, 2017 7:32 am
Hi all,
I need a way to escape from any level of nested subroutines and get back to the first caller level (somewhat like an exception handler), ensuring that its last RTS will be handled correctly.
Neither the main loop nor the subroutines make explicit use of the stack (i.e., it is used only by the jsr/rts mechanism).
Do you think it is enough to have the stack pointer saved and restored, as in following example?
I need a way to escape from any level of nested subroutines and get back to the first caller level (somewhat like an exception handler), ensuring that its last RTS will be handled correctly.
Neither the main loop nor the subroutines make explicit use of the stack (i.e., it is used only by the jsr/rts mechanism).
Do you think it is enough to have the stack pointer saved and restored, as in following example?
Code: Select all
;LEVEL 0 - MAIN
tsx
stx CPU_STACK_PTR
main_loop:
...
jsr sub1
...
jsr sub2
...
<test_end_loop here>
bne main_loop
...
rts
sub1: ...
jsr sub1_1
...
<some test here>
bne +
jmp exception
+ ...
jsr sub1_2
...
rts
...
sub1_2: ...
<some test here>
bne +
jmp exception
+ ...
rts
;EXCEPTION HANDLER
exception:
...
<do some useful stuff>
...
;ESCAPE TO LEVEL 0 - MAIN
ldx CPU_STACK_PTR
txs
jmp main_loop