Author Topic: Emergency Exiting out of an application by sexually abusing the stack  (Read 5598 times)

0 Members and 1 Guest are viewing this topic.

Ashbad

  • Guest
I have a question.  I'm the horrible flavor of coder who doesn't fare well with manipulating the stack perfectly, so in case I run into difficulties with some code, I tried writing two small routines that would get the location of the stack (with an extra pop beforehand) and the value at the top of the stack before I even touch it myself, so when I do a final exit from the app, the stack looks as though it was never touched to the OS.  Here is my code for this:

Code: [Select]
;start stack check initializer
;keeps track of app start location
;followed up later in case of emergency exits

StartStackCheckInit
pop hl
ld (OP6),hl  ;load emergency resources into OP6
ld hl,0
adc hl,sp
ld (OP6+2),hl
ld hl,(OP6)
push hl
ret
StartStackCheckInit_end


;emergency exit call
;used only when an exit is REALLY REALLY REALLY needed

ExitUsingEmergencyResources
ld sp,(OP6+2)
ld hl,(OP6)
push hl
END_APP_RUN  ;just a small stand-in for that bcall(_JForceNoCMD...) thing I always forget the name frequently
ExitUsingEmergencyResources_end


Looks like it'll work (from what I saw in Wabbit, it seemed to work fine there too -- however, I can't test on hardware ATM) but is it safe to sexually abuse the stack this much?  Or is it even safe in general to attempt such a thing?

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Is there a reason why you can't save SP somewhere at the very beginning of the app, then just restore that if you need to exit?
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline BuckeyeDude

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 283
  • Rating: +42/-0
    • View Profile
Bcall(_JForceNoCmd) already does this for you. It fixes the hardware stack, the floating point stack, and whatever else, so even if you're stack is leaking horribly you should still be able to call it and exit safely to the OS

Ashbad

  • Guest
Bcall(_JForceNoCmd) already does this for you. It fixes the hardware stack, the floating point stack, and whatever else, so even if you're stack is leaking horribly you should still be able to call it and exit safely to the OS

Oh, really?  Thanks, that's a lot easier :)

Offline BuckeyeDude

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 283
  • Rating: +42/-0
    • View Profile
Yeah thats why its ok to bcall it, even though it was technically meant to be a bjump.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Sorry if this is off-topic, but I lol'd at the topic title, which immediately made me figure out who started it, and it turned out I guessed right :P :

Quote
DJ_O   also I just saw this in the new posts list: "Emergency Exiting out of an application by sexually abusing the stack" in the ASM sub forum
[23:21:34]   DJ_O   let me guess: The topic was started by Ashbad <.<
[23:21:42]   shmibs   yup
[23:21:48]   DJ_O   YES i was right

We didn't cheat by checking before :P
« Last Edit: July 01, 2011, 11:23:53 pm by DJ_O »

Ashbad

  • Guest
You know me too well *.*

Offline mrmprog

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 559
  • Rating: +35/-1
    • View Profile
wow DJ...

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Don't worry I still like him ;D