Author Topic: clean asm program exiting  (Read 2461 times)

0 Members and 1 Guest are viewing this topic.

Offline the_mad_joob

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 346
  • Rating: +47/-0
    • View Profile
clean asm program exiting
« on: May 20, 2014, 06:49:14 am »
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

Code: [Select]
.db t2bytetok,tasmcmp
    bit 5,(iy+$44)
    jr nz,backup_over
    ld a,(currow)
    ld (textshadcur),a
backup_over
Your code starts here...

TAIL

Code: [Select]
...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.
« Last Edit: May 20, 2014, 12:42:33 pm by the_mad_joob »