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