The QBNews Page 19 Volume 1, Number 4 September 3, 1990 Using Stay-Res Plus to make TSR's by Larry Stone Have you ever had the need to create a program that is RAM resident? Needless to say, it is impossible to do it directly in BASIC because of the controlling influence of the BASIC engine. This doesn't mean that it can't be done, it means that it can't be done directly. There are two commercial products available that can allow your QB programs to become RAM resident. One product, PDQ is offered by Crescent Software and the other, Stay-Res Plus by MicroHelp, Inc. I own both products and highly recommend each. I use PDQ for small, memory resident utilities - those programs whose DOS kernels are no larger than 15K bytes. But, most of what I code turns out to be full-fledged application programs. Not little utilities performing a single minded task, rather, my programs tend to be large applications that have multiple menus and perform multiple I/O functions. Stay-Res Plus excels in this arena. Stay-Res Plus, coupled with a little additional effort by you, can turn your application into a TSR (Terminate and Stay Resident). A Stay-Res Plus assisted application can pop-up with the press of up to 23 pre-defined hot keys, on a specific date and time, on the "ring detect" from a communications port, or even from a POKE by your other programs. Your Stay-Res program can even SHELL other applications if popped-up over DOS. And, most important, when your program goes to sleep, it only locks out a 10K - 11K byte DOS kernel, sending the rest of the memory "image" to either EMS, or to temporary disk files (note Stay-Res will allow the entire application to be DOS RAM resident). To write a Stay-Res application demands of you to conform to some basic (no pun intended) rules. Your program needs an initialization section where all of your arrays and TYPEs are DIMed, and CONST's are defined, and where all of the necessary logic required is performed before entering the main program section. Here is where your program displays it's message that it is going to sleep. Your program also needs a memory-resident section which the main section will transfer control to, every time it pops down. Because your program will be TSR, there are a few do's and don'ts you must adhere to. Never, ever ERASE or REDIM an array. This is due to how BASIC will handle these commands. Issuing these commands will allow BASIC to move memory around. Once this happens, Stay-Res cannot protect the memory pool and your program will probably crash. If you need to clear a numeric array, place it into a FOR...NEXT loop and set each element of the array to zero. String arrays would be set to null Dynamic arrays also need special attention due to the manner that BASIC allocates their memory. QB4, BASCOM6, or PDS will store dynamic arrays downward from the top of the heap (high memory) instead of just above the stack (bottom of the heap), leaving a gap in between. In order for Stay-Res to protect this memory, you must instruct QB to put these arrays just above the stack, making all of the program's memory contiguous. You do this by using BASIC's SETMEM instruction followed The QBNews Page 20 Volume 1, Number 4 September 3, 1990 by a call to SrAutoSetBlock. A note for all of those Stay-Res users who may be reading this. Stay-Res v3.3 has an undocumented command, SrAutoSetBlock. This command can be used in lieu of SrSetBlock and is much more efficient. Simply DIM and define every variable possible at the top of your code (pretend your programming in Pascal), then issue the command HeapMem& = FRE(-1). Subtract a fudge factor from HeapMem& to account for anything missed or for what variable length strings may require: HeapMem& = HeapMem& - 2048 then have BASIC set the memory contiguous: MemNeeded& = SETMEM(-HeapMem&) and follow up with the call CALL SrAutoSetBlock(Paragraphs%, Ecode%). Then you should check if it will work with: IF MemNeeded& <= HeapMem& OR Ecode% <> 0 THEN PRINT "Not Enough Memory - Bye Bye". One important question to consider is which BASIC commands cannot be used with Stay-Res. Well, under no circumstance can you ever use BEEP or PRINT CHR$(7), or BASIC's RUN or SHELL statements once your program has become memory resident the first time (use the alternative SHELL's supplied with Stay-Res). You can use SOUND or PLAY so long as you allow sufficient time for the speaker to shut off before going to sleep. However, when Stay-Res pops up with an Ecode% equal to 1 then DOS is, at this time, busy and you cannot use LOCK, UNLOCK, DATE$ TIME$, PEN, LOF(), EOF(), PLAY, LPRINT, RANDOMIZE TIMER, and all other file related commands except LOC(), and all directory commands. So, what does this mean? The manual trys explain it with an example to instruct DOS to copy a *huge* file then pop-up your Stay Res program in the middle of the COPY. I tried that and, on my system, DOS simply suspended the COPY. Stay-Res did not pop-up with Ecode = 1 and, when my program popped down, DOS continued as if it were never interrupted. I even tried this procedure with PKzip and it too simply suspended it's operation until I popped down. I suspect that on my computer, it would have to be networked with local I/O activity before an Ecode of 1 would be returned by Stay-Res. Never-the-less, if DOS is busy, set a flag and if the user needs any of the commands listed, print a message to try later when DOS isn't so busy. Compiling and linking Stay-Res must be done from the DOS command line. Compiled programs must use the /O switch and the linker must be instructed to place the Stay-Res object first and a supporting object last. The link command should look like: LINK STAYQB4+prog+otherMods+MHMISCP,prog,nul,externalLibs; The term, prog stands for the name of your program. Notice that prog is repeated mid-way in the command and separated by commas. This will instruct the linker to name the completed EXE with your program name. To facilitate your coding TSR's, MicroHelp supplies a template program with clear, concise, pre-defined areas and GOSUB instructions required. If you can paint by numbers then you can use this template. Creating a TSR is not a snap but, neither is it very difficult if you adhere to the rules. To illustrate the power of Stay-Res, accompanying this article is The QBNews Page 21 Volume 1, Number 4 September 3, 1990 a full function, scientific and statistical calculator with built-in mouse support. The calculator has 72 buttons supporting 92 functions such as, trigometric computations (including inverse and hyperbolic), log e, log 10, and log n, angular conversions, binary, octal, complex and hexidecimal number support, a 500 element array for statistical computations, permutations, combinations, four types or means, nth roots, standard deviations, a 512 line user configurable look up table and, not-to-mention, factorials and random number table whose sequence far exceeds BASIC's and offers three types of random number - defined range randoms, randoms with an exponential curve defined by your mean, and randoms with a normal curve defined by your mean and your standard deviation. The EXE size is 129,440 bytes and requires some 204,000 bytes to load. The program incorporates ERROR trapping in several subprograms and functions. However, when it pops down, it reduces it- self to a 10 Kbyte kernel!!! I and my beta testers found the calculator to be 100% compatible with QB, QBX, Lotus 123 v2.1, MS WORD, Word Perfect, Qedit, PC-Write, dClip, CLIPPER applications, dBase, LIST, et. al. In fact, the only program that it won't pop up over is Code View or Lotus v1.1 because these are not "well-behaved" programs (Microsoft broke their own compatibility requirements when they constructed Code View). If a Stay-Res program is not compatible with another program, I assure you the fault is that the other program is not well-behaved. ********************************************************************** Larry Stone is President of LSRGroup and is involved in writing software for marine and aquatic research. He can be reached at LSRGroup, P.O. Box 5715, Charleston, OR 97420, or in care of this newsletter. ********************************************************************** [Editor's Note] When I asked Larry to do a pop up calculator, I expected a small 4 function little program. Well, Larry isn't known for a lack of effort and has come up with a truely professional program. Because of the time and effort that has gone into the program, I feel that it is only right for him to offer it as shareware. But, as readers of the QBNews, you also get a special bonus when you register. Just mention that you read the QBNews and Larry will send you full source code when you register. The file CALC.ZIP contains an abridge version of the documentation. To get the full documentation, you can download it from these fine boards: The Crescent Software Support BBS 203-426-5958 2400 Baud The Empire Builder 503-888-4121 2400 Baud