home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / MISC.ZIP / RESTART.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-01-28  |  4.6 KB  |  144 lines

  1. {
  2. Demonstrates the capability to restart a program from any place
  3. in the program.
  4.  
  5. Call the procedure StoreRestartPoint from the main block of the
  6. program after executing whatever initialization code is desired.
  7. Subsequent restarts will return to the point immediately after
  8. the call to StoreRestartPoint.
  9.  
  10. Whenever you would like to restart the program, call the
  11. procedure RestartProgram. This will return you to the point
  12. saved by StoreRestartPoint, and will restore the stack space
  13. to what it was originally. Note that RestartProgram can be
  14. called at any nesting level (that's the good part!).
  15.  
  16. This capability supports multiple code segment programs
  17. generated by BIGTURBO, and allows restarts from any code segment.
  18. In this case, StoreRestartPoint and RestartProgram should be exported
  19. to all modules. Alternatively, they can be implemented in all modules
  20. by incorporating their source code in the program's .GLO file.
  21.  
  22. It would be straightforward to extend this concept to multiple
  23. restart points, and to allow restart points to reside in procedures
  24. rather than just within the main block. If this is done, it would
  25. effectively extend Turbo Pascal to support interblock GOTOs. Anyone
  26. needing this feature is invited to GO FOR IT. I would appreciate
  27. hearing about it if you make it work.
  28.  
  29. written 1/25/86, Kim Kokkonen, TurboPower Software.
  30. telephone 408-378-3672. Compuserve 72457,2131.
  31. }
  32.  
  33. PROGRAM demo;
  34.   {-demonstrate the ability to jump to a restart point in the program}
  35.  
  36.   {**************include the following in your program********************}
  37.  
  38. VAR
  39.   {store the registers to restore on a reset jump - MUST BE GLOBAL VARIABLES}
  40.   restart_address : ^Integer;
  41.   restart_sp : Integer;
  42.  
  43.   PROCEDURE StoreRestartPoint;
  44.     {-store those registers needed for a return to the main reset point}
  45.     {CALL ONLY FROM THE MAIN BLOCK OF YOUR PROGRAM}
  46.   BEGIN
  47.     INLINE(
  48.       $BE/restart_address/    {MOV    SI,offset(restart_address)}
  49.       $8C/$C8/                {MOV    AX,CS}
  50.       $89/$44/$02/            {MOV    [SI+02],AX}
  51.       $8B/$46/$02/            {MOV    AX,[BP+02]}
  52.       $89/$04/                {MOV    [SI],AX}
  53.       $8B/$C4/                {MOV    AX,SP}
  54.       $05/$06/$00/            {ADD    AX,0006}
  55.       $A3/restart_sp          {MOV    restart_sp,AX}
  56.       );
  57.   END;                        {StoreRestartPoint}
  58.  
  59.   PROCEDURE RestartProgram;
  60.     {-restore the stack(s) and jump to the stored Restart point}
  61.   BEGIN
  62.     {BIGTURBO stack pointer - activate if BIGTURBO program}
  63.     {LocalStackPtr:=MaxStackSize;}
  64.     INLINE(
  65.       $A1/restart_sp/         {MOV    AX,restart_sp}
  66.       $8B/$E0/                {MOV    SP,AX}
  67.       $8B/$EC/                {MOV    BP,SP}
  68.       $FF/$2E/restart_address {JMP    FAR restart_address}
  69.       );
  70.   END;                        {RestartProgram}
  71.  
  72.   {************************demonstration follows*****************************}
  73.  
  74. VAR
  75.   i : Integer;
  76.   ch : Char;
  77.  
  78.   PROCEDURE Dummy;
  79.  
  80.     PROCEDURE NestedDummy;
  81.     BEGIN
  82.       WriteLn('in nested dummy');
  83.       Write('press a key to restart... ');
  84.       Read(Kbd, ch);
  85.       WriteLn;
  86.       WriteLn('restarting program from nested dummy');
  87.       RestartProgram;
  88.     END;                      {NestedDummy}
  89.  
  90.   BEGIN
  91.     WriteLn('in dummy');
  92.     WriteLn('choose from menu: ');
  93.     WriteLn('  1.run nested dummy procedure');
  94.     WriteLn('  2.exit dummy normally');
  95.     WriteLn('  3.restart');
  96.     Write('Enter choice: ');
  97.     REPEAT
  98.       Read(Kbd, ch);
  99.     UNTIL (ch IN ['1', '2', '3']);
  100.     WriteLn(ch);
  101.     CASE ch OF
  102.       '1' : NestedDummy;
  103.       '3' : RestartProgram;
  104.     END;
  105.     WriteLn('normal dummy exit');
  106.   END;                        {Dummy}
  107.  
  108. BEGIN
  109.   WriteLn('Entered main block');
  110.   {put any desired initialization code here}
  111.  
  112.   WriteLn('storing restart point');
  113.   StoreRestartPoint;
  114.   WriteLn('after restart point');
  115.   WriteLn('choose from menu: ');
  116.   WriteLn('  1.run dummy procedure');
  117.   WriteLn('  2.write some integers');
  118.   WriteLn('  3.restart');
  119.   WriteLn('  4.terminate');
  120.   Write('Enter choice: ');
  121.   REPEAT
  122.     Read(Kbd, ch);
  123.   UNTIL (ch IN ['1', '2', '3', '4']);
  124.   WriteLn(ch);
  125.   CASE ch OF
  126.     '1' : Dummy;
  127.     '2' : BEGIN
  128.             FOR i := 1 TO 10 DO WriteLn(i);
  129.             Write('press a key to restart... ');
  130.             Read(Kbd, ch);
  131.             WriteLn;
  132.             WriteLn('restarting program');
  133.             RestartProgram;
  134.           END;
  135.     '3' : BEGIN
  136.             WriteLn('restarting program');
  137.             RestartProgram;
  138.           END;
  139.     '4' : ;
  140.   END;
  141.   WriteLn('normal program exit');
  142.  
  143. END.
  144.