home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / TSR.ZIP / TSR.TXT
Encoding:
Text File  |  1989-06-09  |  20.7 KB  |  364 lines

  1.  
  2.                                                      July 1987
  3.  
  4.                            The History and Technology
  5.                    of TSR Terminate and Stay Resident software
  6.  
  7.                                        by
  8.                                   Steve Gibson
  9.                         InfoWorld TechTalk Columnist and
  10.                        President of Gibson Research Corp.
  11.  
  12.  
  13.                                  HISTORY BEGINS
  14.         ----------------------------------------------------------------
  15.         Once upon a time a Big Blue company named IBM decided that it 
  16.         wanted to make personal computers too.  For reasons known only 
  17.         to the Gods of Boca Raton Florida, IBM decided to use the 8088 
  18.         microcomputer from Intel rather than Motorola's vastly "cleaner" 
  19.         68000 (as is used in Apple's macintosh).  Viewed in retrospect, 
  20.         this has turned out to be a ridiculously expensive, i.e. WRONG, 
  21.         decision.  But anyway... IBM journeyed to the West to get its 
  22.         software for this new machine.  
  23.  
  24.         IBM first visited the company who was then the acknowledged king 
  25.         of operating systems, Digital Research, Inc.  DRI had developed 
  26.         CP/M, the defacto standard operating system for the very popular 
  27.         8080 and Z80 microcomputer systems.  IBM explained that it 
  28.         needed an operating system for the new 8088 microprocessor in 
  29.         their new, blue, personal computer and asked if DRI would please 
  30.         provide them with one.  DRI said, quite truthfully, that they 
  31.         didn't have one.  Big blue must have then asked if they would 
  32.         like to make one, to which DRI must have again said: "No 
  33.         thanks." 
  34.  
  35.         IBM shrugged and headed North. . . . . 
  36.  
  37.         Though IBM may have only planned to ask Microsoft for an 8088 
  38.         BASIC language interpreter for their new machine, they found 
  39.         themselves in Washington without an operating system.  Microsoft 
  40.         said they would be pleased to provide IBM with both an 8088 
  41.         operating system and the BASIC language interpreter.  IBM said 
  42.         "Thank you" and said they needed it very soon please. 
  43.  
  44.         But Microsoft didn't have an 8088 operating system either, and 
  45.         being so very much smaller then than now, didn't have the 
  46.         resources to build one from scratch by IBM's deadline.  So they 
  47.         found an even tinier company called Seattle Computer who had 
  48.         built a little unpretentious operating system for the 8088 and 
  49.         8086 micros called 86-DOS.  It was also known by some as QDOS.  
  50.         QDOS was said to stand for Quick and Dirty Operating System. 
  51.  
  52.         So Microsoft, then able to meet IBM's 8088 machine timetable, 
  53.         delivered both an operating system (albeit one of unassuming 
  54.         parentage and questionable virtue) and a BASIC interpreter. 
  55.  
  56.         Everything seemed fine until we all began really using the new 
  57.         operating system.  We wanted additional and rather reasonable 
  58.         things from MS-DOS and PC-DOS.  Things which had not been 
  59.         designed into 86-DOS, like printing in the background while the 
  60.         user was doing other things.  Or supporting RS-232 serial 
  61.         printers, which required routing the printer data out through a 
  62.         serial port instead of the normal parallel printer port. 
  63.  
  64.         To meet these reasonable demands Microsoft made a series of 
  65.         small changes and additions to the guts of MS-DOS which allowed 
  66.         extra "add-on" code to be "hung" onto the outside of MS-DOS and 
  67.         also provided for limited communication between DOS and the add-
  68.         on code.  Microsoft's idea was that these MS-DOS limitations 
  69.         would be "fixed" by writing some new DOS commands (in the above 
  70.         examples PRINT and MODE) which would make use of special 
  71.         undocumented aspects of their latest version operating system. 
  72.  
  73.         The problem was that MS/PC-DOS, still really old 86-DOS (QDOS) 
  74.         in disguise, was never built to support multitasking.  This 
  75.         means that it was never meant to serve more than a single master 
  76.         at a time.  A command like PRINT, which can continue to operate 
  77.         even AFTER the user has begun doing something else, could very 
  78.         easily confuse it, since now there could be service requests 
  79.         coming into MS-DOS from two quite different places at once. 
  80.  
  81.         Since this began making the entire operating system rather 
  82.         fragile and weird, Microsoft's grand plan was to retain all 
  83.         these special hooks and communications paths as a company 
  84.         secret.  They figured that as long as they were the only ones to 
  85.         use these new MS-DOS secrets things wouldn't get out of hand. 
  86.  
  87.         But Microsoft must have gravely underestimated the cleverness 
  88.         and determination of the development community.  When the MODE 
  89.         command proudly stated: "Resident portion installed" developer's 
  90.         eyebrows shot up!  RESIDENT portion?  Hmmmmmm.  And if the PRINT 
  91.         command could access the disk for file printing when we were 
  92.         messing around with a spreadsheet, why couldn't we build a 
  93.         little resident notepad or two, too? 
  94.  
  95.         But EVEN after these new resident goodies began appearing in 
  96.         increasing numbers, Microsoft refused to "disclose" anything 
  97.         whatsoever.  They said they "couldn't" document these things or 
  98.         they'd be forced to make all FUTURE versions of the operating 
  99.         system support them in upwardly compatible fashion.  Since these 
  100.         were awkward "kludge" solutions to the real need for a true 
  101.         multitasking operating system, Microsoft's delicate condition 
  102.         can be readily understood. 
  103.  
  104.         However, developers sensing massive market opportunities were 
  105.         not to be stopped.  Debugging software which allowed programmers 
  106.         to peer inside the still working machine sold like wildfire.  
  107.         Using these debuggers (like PERISCOPE, my personal favorite), 
  108.         developers probed into the very heart of Microsoft's own PRINT 
  109.         and MODE commands, unscrambling and deciphering the undocumented 
  110.         secrets of their operation.  Using the same secrets Microsoft's 
  111.         own developers had used, they learned how to give their OWN 
  112.         programs similar capabilities. 
  113.  
  114.         In an old episode of Star Trek, Mr. Spock said, after capturing 
  115.         the Romulan's cloaking device, "...military secrets are the most 
  116.         fleeting of all, Commander."  This applies equally well to 
  117.         technological secrets in general, and as we've seen, to Resident 
  118.         Software in particular! 
  119.  
  120.         It's unfortunate that Microsoft did not have time back then in 
  121.         the beginning to build their own DOS from scratch.  They might 
  122.         have done it right.  But we're stuck with what we have today, 
  123.         and making the most of it is certainly our best strategy. 
  124.  
  125.         It remains unfortunate, though certainly understandable, that 
  126.         Microsoft isn't willing to disclose the details of MS-DOS's 
  127.         resident software hooks and techniques.  This policy keeps such 
  128.         knowledge quite secret, since even the third-party developers 
  129.         are reluctant to disclose their own hard-won "insider's" 
  130.         knowledge and proprietary techniques for forcing reliable 
  131.         multitasking behavior from good ol' single tasking DOS.  It's 
  132.         quite interesting to speculate that perhaps today even Microsoft 
  133.         does not KNOW how to write MS-DOS multitasking applications as 
  134.         well and robustly as several of the larger commercial resident 
  135.         software publishers!  The tricks required to make DOS multitask 
  136.         are real nasty stomach-turners. 
  137.  
  138.         So... what are these tricks and techniques?  How DO resident 
  139.         programs "pop up over" applications at the simple press of a 
  140.         "hot key"? 
  141.  
  142.  
  143.  
  144.                        THE TECHNOLOGY OF RESIDENT SOFTWARE
  145.         ---------------------------------------------------------------
  146.  
  147.         Since we've now know how we got to wherever it is we are, let's 
  148.         answer the those questions posed above.... 
  149.  
  150.         Two things are required for the functioning of TSR technology:  
  151.         First, the TSR program needs to somehow "hang around" well after 
  152.         any other program has started to run.  Secondly, it has to 
  153.         "know" what's going on in the system to be able to jump in and 
  154.         help out when called upon for action.  It also needs some way of 
  155.         re-assuming control of the machine when the time comes for it to 
  156.         take action. 
  157.  
  158.         The requirement of continuing RAM residency is satisfied by a 
  159.         service provided by the operating system from which TSR's get 
  160.         their name: Terminate and Stay Resident (TSR). 
  161.  
  162.         The main operating RAM memory of an IBM compatible PC is a 
  163.         contiguous block beginning at address zero (called the bottom of 
  164.         RAM memory) and extending "upward" for a maximum of 640K bytes 
  165.         (called the top of memory).  Note that 640K is actually 655,360 
  166.         bytes since "Ks" in computerdom are really 1024 bytes, not 1000.  
  167.         Since the very bottom of this large contiguous block of RAM is 
  168.         reserved for system housekeeping purposes, DOS is "booted" by 
  169.         copying it from disk into RAM just "above" this low-RAM region. 
  170.  
  171.         Once DOS has been loaded, all the space "above" it, to the top 
  172.         of RAM, is available for application program loading and use.  
  173.         We'll call this location just above DOS the LOAD POINT for 
  174.         application software.  So, when a program is started, it is 
  175.         copied from disk into RAM starting at this LOAD POINT, 
  176.         continuing upward until the entire program has been loaded.  The 
  177.         memory available, and allocated, to the program for its own data 
  178.         storage and manipulation extends from the end of itself to the 
  179.         physical top of the RAM memory. 
  180.  
  181.         Most standard programs word processors, spreadsheets, or 
  182.         databases have "control" of the computer until the user tells 
  183.         the program to terminate.  After performing whatever cleanup and 
  184.         file saving might be required, the program simply informs DOS 
  185.         that it is all finished by saying the equivalent of "I'm all 
  186.         finished now, terminate me please". 
  187.  
  188.         Responding to this "Terminate" request from a program, DOS 
  189.         simply "forgets" all about the program and whatever data it 
  190.         might have had in RAM.  It displays the command line prompt and 
  191.         awaits another command.  The next program to run is loaded at 
  192.         the same LOAD POINT as before, thus overwriting whatever 
  193.         terminated program was just running, and implicitly making the 
  194.         same amount of memory available to this next program. 
  195.  
  196.         Now consider the special case of the RESIDENT PROGRAM... 
  197.  
  198.         It begins innocently, just like any other application program, 
  199.         when its name is typed at the DOS prompt.  DOS copies it from 
  200.         disk into the user's RAM memory starting at the same LOAD POINT 
  201.         as any prior application software, implicitly leaving the rest 
  202.         of RAM memory for the program's own use until done, then hands 
  203.         control of the computer system over to this program. 
  204.  
  205.         Now things start getting interesting! 
  206.  
  207.         The resident program will generally say hello to the user 
  208.         announcing its intention to hang around for the duration.  Then 
  209.         it does a few "non-standard things" to the system.  These are 
  210.         the very things MICROSOFT wanted to keep all to itself.  These 
  211.         special "things" keep the program from being ex-communicated 
  212.         from system activities after it terminates.  Then, rather than 
  213.         simply saying "terminate me" to DOS, it says: "I'm xxx bytes 
  214.         long.  Now terminate me but DON'T OVERWRITE ME."  Which is to 
  215.         say: Terminate me, but let me Stay Resident. 
  216.  
  217.         Upon receiving this request, DOS takes control from the program, 
  218.         moves the software LOAD POINT UPWARD by those xxx bytes, and 
  219.         only THEN returns the DOS prompt to the user.  Since the LOAD 
  220.         POINT has been moved up by "xxx" bytes, all subsequent programs 
  221.         will be loaded AFTER the programs which ASKED to remain 
  222.         resident, thus leaving it undisturbed in the system's memory! 
  223.  
  224.         Additionally, since it's the resident program which tells DOS 
  225.         how big it is, it is able to declare itself to be ANY LENGTH IT 
  226.         CHOOSES!  This is how programs like SideKick, which are only 39K 
  227.         bytes long while on the disk, can have a 50K notepad file and 
  228.         take up 89K bytes while in RAM!  It effectively LIES to DOS 
  229.         about how big it is, so DOS obligingly leaves it space for 
  230.         itself and for however much working memory it wants!  This is 
  231.         the way resident programs are able to eat up so much of our main 
  232.         RAM memory!  When this process is repeated many time (as is 
  233.         frequently the case) a resident program "stack" is created in 
  234.         main RAM memory. 
  235.  
  236.         So, we've now seen how programs are able to remain in memory 
  237.         after being "officially" terminated.  The final question is:  
  238.         How are these "terminated" programs able to remain "aware" of 
  239.         the activities around them?... and how are they able to regain 
  240.         control of the computer whenever they see fit to do so?  In 
  241.         other words, how can a notepad or other utility "pop up" ??  To 
  242.         answer these last questions let's step back for a moment, 
  243.         reviewing a bit about how computer's are controlled: 
  244.  
  245.         A computer's actions are directed by its reading of instructions 
  246.         which are stored in its main memory.  These instructions may be 
  247.         temporary, as in the case of programs loaded from disk into RAM, 
  248.         or permanent, as in the case of instructions permanently wired 
  249.         into ROM chips.  In either case, each instruction has a unique 
  250.         address which is just the number of the byte it occupies in the 
  251.         machine's memory. 
  252.  
  253.         We cause the computer to perform a given task by having it read 
  254.         a series of instructions which detail the task.  In a sense we 
  255.         CALL upon those instructions whenever we need to exert their 
  256.         intended function.  Such a "CALLED" instruction sequence 
  257.         generally has a clever instruction called RETURN which is able 
  258.         to send the computer back to wherever it was called from! 
  259.  
  260.         This notion of FUNCTIONS which are being performed by 
  261.         instruction sequences called upon in memory, and which will 
  262.         return to us when finished, is very powerful.  IBM compatible 
  263.         computers come equipped with a BIOS ROM which contains a host of 
  264.         very useful general purpose functions which return control to 
  265.         their caller as soon as they have performed their designated 
  266.         duty.  This is EXACTLY what that often mentioned BIOS ROM is all 
  267.         about!  B.I.O.S. stands for "Basic Input Output System", and 
  268.         consists of a collection of standard functions for reading and 
  269.         writing to the system keyboard, screen, mass storage, and many 
  270.         other standard devices. 
  271.  
  272.         Naturally, we must know the exact address of these "functional" 
  273.         services in order to CALL them into service.  By convention, the 
  274.         starting address of every built-in function in the IBM PC is 
  275.         located in a predefined table of addresses located at the very 
  276.         start of the computer's RAM memory.  Every function is numbered 
  277.         simply by the location of its address in this table.  This means 
  278.         that in order to call for any built-in IBM PC function we only 
  279.         need to know its standard function number - from this we're able 
  280.         to calculate the location in the table where that function's 
  281.         starting address can be found. 
  282.  
  283.         There's even a clever computer instruction which lets you 
  284.         directly call upon a function whose ADDRESS is stored at a 
  285.         specific location in this table.  So in effect you can simply 
  286.         ask the computer to call "standard function number 7".  In 
  287.         response to this function request the computer will look into 
  288.         the 7th slot in the table for the address of function number 7, 
  289.         and go there directly, returning to you when the function has 
  290.         been performed.  In computer lingo, a place which contains the 
  291.         address of something else is a POINTER or a VECTOR. 
  292.  
  293.         Even the electrical hardware of the computer is able to call 
  294.         upon these standard functions.  In fact many of the standard 
  295.         functions in the ROM BIOS are designed to operate hand-in-hand 
  296.         with the PC's hardware.  For example, every time a keyboard key 
  297.         is either pressed or released the standard function number 9, 
  298.         whose address is naturally in location 9 of the Address Table, 
  299.         is called upon by the hardware.  This function is called EVEN IF 
  300.         THE SOFTWARE WAS BUSY DOING SOMETHING ELSE AT THE TIME! 
  301.  
  302.         This ability of the hardware to INTERRUPT whatever the software 
  303.         might be doing at the time is called a "hardware interrupt".  
  304.         Essentially the keyboard hardware says to the processor: 
  305.  
  306.         "Hey there CPU, hold on a second, remember where you are and 
  307.         what you are doing, now go perform the function whose address is 
  308.         in slot number 9, and when that function returns to you, go back 
  309.         to whatever is was you were doing when I bugged you just like 
  310.         nothing ever happened." 
  311.  
  312.         This is EXACTLY how we are able to type ahead of our computers.  
  313.         Even if our spreadsheet is lost in thought recalculating our tax 
  314.         refund, we're able to type ahead of it!  Each depression and 
  315.         release of a key steals a fraction of a moment, a slice of time, 
  316.         away from the spreadsheet's recalc.  The function invoked by the 
  317.         keyboard's hardware interrupt places the key into a type-ahead 
  318.         buffer ... which the spreadsheet will read from as soon as its 
  319.         good and ready ... then returns control to the recalc as if 
  320.         nothing at all had happened. 
  321.  
  322.         Now imagine what would happen if some program (like our resident 
  323.         one) were to point the POINTER stored in the 9th table location 
  324.         to somewhere else, such as to ITSELF, prior to issuing the 
  325.         Terminate and Stay Resident (TSR) request to DOS! 
  326.  
  327.         THEN the hardware interrupt occurring at every keyboard action 
  328.         would turn control over to THIS TSR'd RESIDENT PROGRAM instead 
  329.         of to the normal built-in keyboard handling function!  If the 
  330.         resident program wasn't "interested" in that particular keyboard 
  331.         action or keystroke, it could simply pass control along to the 
  332.         place where the POINTER HAD BEEN POINTING BEFORE just as if it 
  333.         hadn't been evesdropping at all!  However, if the key action 
  334.         happened to be THIS resident program's assigned "HOT KEY", it 
  335.         could retain control of the system as long as it wished, before 
  336.         passing it along as if nothing at all had happened! 
  337.  
  338.         That's all there is to it! 
  339.  
  340.         As we've seen, the clever application of these various tricks 
  341.         and techniques can significantly expand the power and 
  342.         flexibility of MS-DOS and PC-DOS based personal computers. 
  343.  
  344.         There are those who feel that resident software is a makeshift 
  345.         temporary solution which is not long for this world.  They say 
  346.         that TSR programs will be eliminated by the presence of full 
  347.         multitasking operating systems.  This author could not disagree 
  348.         more.  I believe TSR programs will be here for the duration as 
  349.         necessary and viable add-ons to otherwise incomplete solutions. 
  350.  
  351.         So there you have it.  The history and technology of TSR 
  352.         Resident Software in a nutshell.  I hope this gives you a better 
  353.         feel for what's going on under the hood of your machine! 
  354.  
  355.         Thanks for listening ... and REMEMBER, if you would like to 
  356.         receive notification of other free information, software, and 
  357.         our future TSR and utility products, simply pickup the phone or 
  358.         drop us a note or a card.  We'd be pleased to keep you up to 
  359.         date. 
  360.  
  361.         Steve Gibson. 
  362.                                <<< END OF FILE >>> 
  363.  
  364.