home *** CD-ROM | disk | FTP | other *** search
- {$K-} { probably better to run with stack checking set off, at this point }
-
- {---------- LONGJMP/ SETJMP PACKAGE ----------------------------------}
- { Author: Sammy Mitchell }
- { Feb 18, 1986 }
- { I have tested this program, and it seems to work. However, use it }
- { at your own risk! }
-
- { I am not responsible, and my company is not responsible, but my keyboard }
- { well... }
-
- { If you have any changes or additions, then please upload them so that }
- { all of us can share them! Thanks }
-
- { If you have any questions or problems, then I can be reached at:
- all area code 404
- PC-Exchange 977-6686
- TJ's 394-1756 or
- C & Turbo Exchange 441-9702 }
-
- { NOTES: SetJmp MUST be called BEFORE LongJmp. LongJmp MUST NOT be called }
- { LongJmp MUST be called BEFORE the function that called SetJmp }
- { returns. }
-
- {---------- do NOT touch these variables - they are necessary for the
- setjmp/longjmp routines ----------------------------------}
- var
- dest, { longjmp address }
- savesp, { to restore environment }
- savebp : integer; { ditto }
- {---------- end of setjmp/longjmp variables --------------------------}
-
- procedure longJmp;
- { Restore the environment, then jump to the location set by a previously }
- { called SetJmp command }
- begin
- Inline(
- $FA { cli ;lets not get interrupted }
- /$8B/$26/savesp { mov sp,savesp;restore stack pointer }
- /$8B/$2E/savebp { mov bp,savebp;and base pointer }
- /$FB { sti ;alrite to get interrupted now }
- /$A1/dest { mov ax,dest ;where we want to jump to }
- /$FF/$E0 ); { jmp ax ;and go there }
- end;
-
- procedure SetJmp;
- { Save the current Evironment and set the return address for LongJmp Calls }
- begin
- Inline(
- $8B/$E5 { mov sp,bp ;restore stack pointer }
- /$5D { pop bp ;and base page pointer }
- /$58 { pop ax ;get return address }
- /$89/$26/savesp { mov savesp,sp;save stack pointer for longjmp }
- /$89/$2E/savebp { mov savebp,bp;save base pointer for longjmp }
- /$A3/dest { mov dest,ax ;save return address for longjmp }
- /$FF/$E0 ); { jmp ax ;now just return }
- end;
- {----------------- END LONGJMP/ SETJMP PACKAGE ----------------------------}
-
-
- { demo program to illustrate LongJmp/SetJmp usage }
- var
- HereBefore : boolean;
-
- procedure test3;
- begin
- writeln('In test3 - calling LongJmp');
- LongJmp;
- end;
-
- procedure test2;
- begin
- writeln('In test2');
- test3;
- end;
-
- procedure test1;
- begin
- writeln('In test1');
- test2;
- end;
-
- procedure main;
- var
- a : integer;
- s : string[80];
- begin
- a := 999;
- s := 'Is this string the same now as it was before? If its not, then watch out!';
- writeln('In Main start - a is ',a);
- writeln(s);
- SetJmp; { set longjmp return point to nxt instruction }
- HereBefore := NOT HereBefore;
- writeln('In main a is ',a);
- writeln(s);
- if NOT HereBefore then
- test1;
- end;
-
- begin
- writeln('Program starting');
- HereBefore := true;
- main;
- end.