Hello =]
It's been a while since i wanted to find a way to exit an asm program, the "clean" way.
By "clean", i mean by restoring all the previous home entries properly, and eventually the graph screen (in case the program was executed in horiz mode), without any unwanted effects.
In other words, you get what you would have after executing a basic program, no matter what you've been displaying.
The challenge was to make it without using any custom ram locations, as well as stack entries.
Also, the code is compatible with all 8X+ models (CSE excluded), and all OS (mathcrap included).
Of course, i guess that is mainly useful for standalone programs (nostub).
It is optimized in favor of size.
Here is the magic formula :
HEADER
.db t2bytetok,tasmcmp
bit 5,(iy+$44)
jr nz,backup_over
ld a,(currow)
ld (textshadcur),a
backup_over
Your code starts here...
TAIL
...Your code ends here.
bit 5,(iy+$44)
jr nz,restore_mp
ld a,(flags+sgrflags)
rra
jr nc,restore_home
bcall(_grbufcpy)
restore_home
bcall(_rstrshadow)
ret
restore_mp
ld a,3
out (5),a
ld b,64
ld hl,$DA7E
bcall(_restoredisp)
xor a
out (5),a
ret
NOTES
If you intend to use _clrscrn, _clrscrnfull, or any large character displaying rom call, just add "res apptextsave,(iy+appflags)" after the header, as well as "set apptextsave,(iy+appflags)" before the tail.
Basically, if textshadow or cmdshadow are altered, the home screen won't be what it was before the program execution.
Same goes for plotsscreen for the graph screen.