home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 201_01 / stayres.doc < prev    next >
Text File  |  1986-03-03  |  4KB  |  67 lines

  1.  
  2.  
  3.                TERMINATE AND STAY RESIDENT
  4.                --------- --- ---- --------
  5.                   Brian Irvine
  6.  
  7.  
  8.    STAYRES.C was written for the Cware C Development System ( DeSmet C ).
  9. It uses library functions and other features ( such as in-line assembly
  10. language ) which might not be available with other C compilers.  However, the
  11. basic stuff is there and it should be a trivial task to convert it to be
  12. compilable under other C compilers.
  13.  
  14.    When a resident program takes over from DOS, if the program uses DOS I/O, it
  15. will most likely leave the local DOS stack and register values changed from
  16. what they were when the program gained control.  When control is returned to
  17. DOS, then it gets very confused and usually ends up doing something completely
  18. unpredictable, or just going off into j-space.    As a result, most programs
  19. which terminate and stay resident take care of their own disk and screen I/O,
  20. and are written in assembly language.  This does not have to be the case,
  21. however.  Programs can be written in high-level languages as long as a little
  22. stack management is performed before and after program use.
  23.  
  24.    The programming in STAYRES.C proposes one solution to the problem.  It saves
  25. the top of the DOS stack and all of the processor registers on the local
  26. program stack before the program is entered.  The program can then perform
  27. normal operations like disk access and formatted screen output.  On exit, all
  28. of the information saved on the local stack is transferred back to the DOS
  29. stack and to the processor registers, creating virtually the same machine state
  30. as was present before the program took control.
  31.  
  32. STAYRES works by intercepting the software interrupt 16H, which is called by
  33. DOS to get a character from the keyboard.  Now when DOS issues a request for
  34. the next character from the queue, we can examine the character before passing
  35. it on to DOS.  If it is the character or keypress combination which we have
  36. designated as that required to activate the TSR program, then we make a further
  37. check to see if the program is already active.    If not, then we go through the
  38. process of saving the machine state, then call the entry point of the TSR
  39. program.
  40.  
  41.    The TSR code is contained in a module which requires the modification of
  42. only one line of code to suit the name of the function which will be designated
  43. as the main entry point to your program.  The module can then be compiled and
  44. linked in with any other object modules to produce a complete program.
  45.  
  46.    The code contained in STAYRES.C is inspired by a set of Turbo Pascal
  47. routines written by Lane H. Ferris.  I have taken the central idea of Lane's
  48. code and used it in this program.  The Turbo routines are available on the
  49. Borland SIG on Compuserve.
  50.  
  51.    I realize that I don't know all there is about this process, so if you have
  52. any further information you would like to impart, or if you have any comments
  53. or suggestions for modifications or improvements, please leave a message for me
  54. on Compuserve.    Use Easyplex or leave a message addressed to me on one of the
  55. IBM PC Sig's.
  56.  
  57.    I am releasing this code to the Public Domain so that others can use the
  58. information for non-profit purposes.  If you do use the code in any of your
  59. applications, I ask only that you give credit for the source.  Feel free to
  60. copy and distribute these files as you wish, but please ensure that the credits
  61. are included.
  62.  
  63.                     Brian Irvine
  64.                     3379 St Marys Place
  65.                     Santa Clara, CA 95051
  66.                     Compuserve User ID [71016,544]
  67.