The called routines at present finish with a GoTo back to Rtnlabel but I would prefer they just did a Return. Is it possible to manipulate the stack so that when the routine finishes with a Return the prgram returns to RtnLabel?
Something like this should work:
Code:
main:
'etc.
Asm-
;setup return address
Push
Movlw ReturnLabel & 255
Movwf TOSL
Movlw (ReturnLabel >> 8) & 255
Movwf TOSH
Movlw (ReturnLabel >> 16) & 255 ;writing upper byte not necessary with < 64K program memory
Movwf TOSU
Endasm-
;branch to subroutine
GoTo subroutine
ReturnLabel:
'etc.
GoTo main
subroutine:
'etc.
Return
It's a bit messy, I don't know if this is the best way to do this, but we must manipulate the top of stack. The push instruction is there basically to increment the stack pointer, the actual value placed on the stack by push is overwritten by the correct return address before jumping to the subroutine. The return instruction in the subroutine will pop our modified value off the TOS and branch back to the return label. The stack pointer at this point is decremented back to where it was before and ready for the next call.