home *** CD-ROM | disk | FTP | other *** search
/ Danny Amor's Online Library / Danny Amor's Online Library - Volume 1.iso / html / faqs / faq / msdos / programmer-faq.part3 < prev    next >
Encoding:
Text File  |  1995-07-25  |  55.4 KB  |  1,227 lines

  1. Subject: comp.os.msdos.programmer FAQ part 3 of 4
  2. Newsgroups: comp.os.msdos.programmer,comp.answers,news.answers
  3. From: brown@NCoast.ORG (Stan Brown)
  4. Date: Sat, 25 Sep 1993 14:10:01 GMT
  5.  
  6. Archive-name: msdos-programmer-faq/part3
  7. Last-modified: 24 Sep 1993
  8.  
  9.  
  10. (continued from part 2)         (no warranty on the code or information)
  11.  
  12. If the posting date is more than six weeks in the past, see instructions
  13. in part 4 of this list for how to get an updated copy.
  14.  
  15. Copyright (C) 1993  Stan Brown, Oak Road Systems.  All rights reserved.
  16.  
  17.  
  18. section 4.  Disks and files
  19. ===========================
  20.  
  21. Subject:  401. What drive was the PC booted from?
  22.  
  23.     Under DOS 4.0 or later, load 3305 hex into AX; do an INT 21.  DL is
  24.     returned with an integer indicating the boot drive (1=A:, etc.).
  25.  
  26. Subject:  402. How can I boot from drive b:?
  27.  
  28.     (rev: 9 Aug 1993)  Downloadable shareware:
  29.         pd1:<msdos.dskutl>boot_b.zip from Simtel
  30.         /pc/bootutil/boot_b.zip from Garbo.
  31.     The included documentation says it works by writing a new boot
  32.     sector on a disk in your a: drive that redirects the boot to your
  33.     b: drive.  (A similar utility is bboot.zip in the same directory at
  34.     Garbo only.)
  35.  
  36.     If that doesn't work, you can always interchange your a: and b:
  37.     drives by switching ribbon cables and changing the setup in your
  38.     BIOS.  From an article posted 27 Jan 1993 on another newsgroup:
  39.  
  40.     Take the "ribbon" connector, as you call it, and switch them.  To
  41.     double check, start at the end of the cable that connects to the
  42.     motherboard or floppy controller.  Follow the cable until you get to
  43.     the first connector.  Connect this to the drive you want to be b:.
  44.     After this, there should be a few lines on the cable that get
  45.     flipped left to right.  (On most cables, they just cut the lines and
  46.     physically reverse them.  It should be quite obvious from looking at
  47.     the cable.)  Anyway, the connector after the pins get flipped
  48.     right to left is the connector for your a: drive.
  49.  
  50. Subject:  403. Which real and virtual disk drives are valid?
  51.  
  52.     (rev: 15 Aug 1993)  Use INT 21 function 29 (parse filename).  Point
  53.     DS:SI at a null-terminated ASCII string that contains the drive
  54.     letter and a colon, point ES:DI at a 37-byte dummy FCB buffer, set
  55.     AX to 2900h, and do an INT 21.  On return, AL is FF if the drive is
  56.     invalid, something else if the drive is valid.  RAM disks and
  57.     SUBSTed drives are considered valid.
  58.  
  59.     You can detect whether the drive is ASSIGNed by using INT 2F
  60.     AX=0601.  To check whether the drive is SUBSTed, use INT 21 AX=4409;
  61.     or use INT 21 function 52 to test for both JOIN and SUBST.  See Ralf
  62.     Brown's interrupt list.
  63.  
  64.     Unfortunately, the b: drive is considered valid even on a single-
  65.     diskette system.  You can check that special case by interrogating
  66.     the BIOS equipment byte at 0040:0010.  Bits 7-6 contain the one less
  67.     than the number of diskette drives, so if those bits are zero you
  68.     know that b: is an invalid drive even though function 29 says it's
  69.     valid.
  70.  
  71.     Following is some code originally posted by Doug Dougherty to test
  72.     valid drives (without regard to SUBST and JOIN), with SB's fix for
  73.     the b: special case, tested in Borland C++ 2.0 (in the small model):
  74.  
  75.         #include <dos.h>
  76.         void drvlist(void)  {
  77.             char *s = "A:", fcb_buff[37];
  78.             int valid;
  79.             for (   ;  *s<='Z';  (*s)++) {
  80.                 _SI = (unsigned) s;
  81.                 _DI = (unsigned) fcb_buff;
  82.                 _ES = _DS;
  83.                 _AX = 0x2900;
  84.                 geninterrupt(0x21);
  85.                 valid = _AL != 0xFF;
  86.                 if (*s == 'B'  &&  valid) {
  87.                     char far *equipbyte = (char far *)0x00400010UL;
  88.                     valid = (*equipbyte & (3 << 6)) != 0;
  89.                 }
  90.                 printf("Drive '%s' is %sa valid drive.\n",
  91.                         s, valid ? "" : "not ");
  92.             }
  93.         }
  94.  
  95.     SB translated this to MSC 7.0 and tested it in small model:
  96.  
  97.         #include <dos.h>
  98.         #include <stdio.h>
  99.         void drvlist(void)  {
  100.             char *s = "A:", fcb_buff[37], *buff=fcb_buff;
  101.             int valid;
  102.             for (   ;  *s<='Z';  (*s)++) {
  103.                 __asm mov si,s         __asm mov di,buff
  104.                 __asm mov ax,ds        __asm mov es,ax
  105.                 __asm mov ax,0x2900    __asm int 21h
  106.                 __asm xor ah,ah        __asm mov valid,ax
  107.                 valid = (valid != 0xFF);
  108.                 if (*s == 'B'  &&  valid) {
  109.                     char far *equipbyte = (char far *)0x00400010UL;
  110.                     valid = (*equipbyte & (3 << 6)) != 0;
  111.                 }
  112.                 printf("Drive '%s' is %sa valid drive.\n",
  113.                         s, valid ? "" : "not ");
  114.             }
  115.         }
  116.  
  117. Subject:  404. How can I make my single floppy drive both a: and b:?
  118.  
  119.     Under any DOS since DOS 2.0, you can put the command
  120.  
  121.         assign b=a
  122.  
  123.     into your AUTOEXEC.BAT file.  Then, when you type "DIR B:" you'll no
  124.     longer get the annoying prompt to insert diskette B (and the even
  125.     more annoying prompt to insert A the next time you type "DIR A:").
  126.  
  127.     You may be wondering why anybody would want to do this.  Suppose you
  128.     use two different machines, maybe one at home and one at work.  One
  129.     of them has only a 3.5" diskette drive; the other machine has two
  130.     drives, and b: is the 3.5" one.  You're bound to type "dir b:" on
  131.     the first one, and get the nuisance message
  132.  
  133.         Insert diskette for drive B: and press any key when ready.
  134.  
  135.     But if you assign drive b: to point to a:, you avoid this problem.
  136.  
  137.     Caution:  there are a few commands, such as DISKCOPY, that will not
  138.     work right on ASSIGNed or SUBSTed drives.  See the DOS manual for
  139.     the full list.  Before typing one of those commands, be sure to turn
  140.     off the mapping by typing "assign" without arguments.
  141.  
  142.     The DOS 5.0 manual says that ASSIGN is obsolete, and recommends the
  143.     equivalent form of SUBST: "subst b: a:\".  Unfortunately, if this
  144.     command is executed when a: doesn't hold a diskette, the command
  145.     fails.  ASSIGN doesn't have this problem, so under DOS 5.0 you
  146.     should disregard that particular bit of advice in the manual.
  147.  
  148. Subject:  405. How can I disable access to a drive?
  149.  
  150.     (new: 15 Aug 1993)  Reader Eric DeVolder writes that he has made
  151.     available a program to do this.  It's downloadable from Simtel as
  152.         pd1:<msdos.dskutl>rmdriv20.zip from Simtel
  153.         /pc/sysutil/rmdriv20.zip at Garbo.
  154.     (existence verified; files not tested by SB)
  155.  
  156. Subject:  406. How can a batch file test existence of a directory?
  157.  
  158.     (new: 28 Aug 1993)  The standard way, which in fact is documented in
  159.     the DOS manual, is
  160.  
  161.         if exist d:\path\nul goto found
  162.  
  163.     Unfortunately, this is not entirely reliable.  SB found it failed in
  164.     Pathworks (a/k/a PCSA, DEC's network that connects PCs and VAXes),
  165.     or on a MARS box that uses an OEM version of MS-DOS 5.0.  Readers
  166.     have reported that it failed on Novell networks or on DR-DOS.
  167.  
  168.     There appears to be no foolproof way to use pure batch commands to
  169.     test for existence of a directory.  The real solution is to write a
  170.     program, which returns a value that your batch program can then test
  171.     with an if errorlevel.  Reader Duncan Murdoch kindly posted the
  172.     following Turbo Pascal version:
  173.  
  174.         program existdir;
  175.         { Confirms the existence of a directory given on the command line.
  176.           Returns errorlevel 2 on error, 1 if not found, 0 if found. }
  177.  
  178.         uses
  179.           dos;
  180.  
  181.         var
  182.           s : searchrec;
  183.  
  184.         begin
  185.           if paramcount <> 1 then
  186.           begin
  187.             writeln('Syntax:  EXISTDIR directory');
  188.             halt(2);
  189.           end
  190.           else
  191.           begin
  192.             findfirst(paramstr(1),Directory,S);
  193.             while (Doserror = 0) and ((Directory and S.Attr) = 0) do
  194.               findnext(S);
  195.             if Doserror <> 0 then
  196.             begin
  197.               Writeln('Directory not found.');
  198.               halt(1);
  199.             end
  200.             else
  201.             begin
  202.               Writeln('Directory found.');
  203.               halt(0);
  204.             end;
  205.           end;
  206.         end.
  207.  
  208.     Timo Salmi also has a Turbo Pascal version in his Turbo Pascal FAQ,
  209.     which is downloadable as
  210.         /pc/ts/tsfaqp15.zip at Garbo
  211.         pd1:<msdos.info>tsfaqp15.zip at Simtel.
  212.  
  213. Subject:  407. Why won't my C program open a file with a path?
  214.  
  215.     You've probably got something like the following code:
  216.  
  217.         char *filename = "c:\foo\bar\mumble.dat";
  218.         . . .  fopen(filename, "r");
  219.  
  220.     The problem is that \f is a form feed, \b is a backspace, and \m is
  221.     m.  Whenever you want a backslash in a string constant in C, you
  222.     must use two backslashes:
  223.  
  224.         char *filename = "c:\\foo\\bar\\mumble.dat";
  225.  
  226.     This is a feature of every C compiler, because Dennis Ritchie
  227.     designed C this way.  It's a problem only on MS-DOS systems, because
  228.     only DOS (and Atari ST/TT running TOS) uses the backslash in
  229.     directory paths.  But even in DOS this backslash convention applies
  230.     _only_ to string constants in your source code.  For file and
  231.     keyboard input at run time, \ is just a normal character, so users
  232.     of your program would type in file specs at run time the same way as
  233.     in DOS commands, with single backslashes.
  234.  
  235.     Another possibility is to code all paths in source programs with /
  236.     rather than \ characters:
  237.  
  238.         char *filename = "c:/foo/bar/mumble.dat";
  239.  
  240.     Ralf Brown writes that "All versions of the DOS kernel accept either
  241.     forward or backslashes as directory separators.  I tend to use this
  242.     form more frequently than backslashes since it is easier to type and
  243.     read."  This applies to DOS function calls (and therefore to calls
  244.     to the file library of every programming language), but not to DOS
  245.     commands.
  246.  
  247. Subject:  408. How can I redirect printer output to a file?
  248.  
  249.     (rev: 16 Aug 1993)  Recommended: PRN2FILE from {PC Magazine},
  250.     downloadable as:
  251.         pd1:<msdos.printer>prn2file.zip at Simtel
  252.         /pc/printer/prn2file.zip at Garbo.
  253.     {PC Magazine} has given copies away as part of its utilities disks,
  254.     so you may already have a copy.
  255.  
  256.     The directories mentioned above have lots of other utilities to
  257.     redirect printer output.
  258.  
  259. Subject:  409. How can I redirect the output of a batch file?
  260.  
  261.     (new: 12 June 1993) Assuming the batch file is called batch.bat, to
  262.     send its output (stdout) to another file, just invoke COMMAND.COM as
  263.     a secondary command processor:
  264.  
  265.         command /c batch parameters_if_any >outfile
  266.  
  267.     Timo Salmi's notes on this and other batch tricks are downloadable:
  268.         pd1:<msdos.batutl>tsbat43.zip at Simtel
  269.         /pc/ts/tsbat43.zip at Garbo.
  270.  
  271. Subject:  410. How can I redirect stderr?
  272.  
  273.     (new: 15 Aug 1993)  Use freopen(..., stderr) and then execute the
  274.     desired command via system( ).  There are downloadable versions of
  275.     programs to do this.  Recommended by SB:
  276.         pd1:<msdos.sysutl>rdstderr.zip from Simtel.
  277.     Source code (in Turbo Pascal 4.0) and executable are included.
  278.  
  279.     A C example is downloadable as
  280.         pd1:<msdos.c>redirect.c from Simtel.
  281.     SB compiled it with MSC 7.0, and it works fine with one exception:
  282.     Contrary to the included comments, redirected output starts writing
  283.     at the beginning of the output file rather than appending.  That is
  284.     easily solved by adding "fseek(stderr, 0L, SEEK_END);" after the
  285.     freopen( ) call for stderr.
  286.  
  287. Subject:  411. How can my program open more files than DOS's limit of 20?
  288.  
  289.     (rev: 12 Sep 1993) This is a summary of an article Ralf Brown posted
  290.     on 8 August 1992, with some additions from a Microsoft tech note.)
  291.  
  292.     DOS imposes some limits.  Once you overcome those, which is pretty
  293.     easy, you may have to take additional measures to overcome the
  294.     limitations built into your compiler's run-time library.
  295.  
  296.     1) Limitations imposed by DOS
  297.  
  298.     There are separate limits on files and file handles.  For example,
  299.     DOS opens three files but five file handles:  CON (stdin, stdout,
  300.     and stderr), AUX (stdaux), and PRN (stdprn).
  301.  
  302.     The limit in FILES= in CONFIG.SYS is a system-wide limit on files
  303.     opened by all programs (including the three that DOS opens and any
  304.     opened by TSRs); each process has a limit of 20 handles (including
  305.     the five that DOS opens).  Example:  CONFIG.SYS has FILES=40.  Then
  306.     program #1 will be able to open 15 file handles.  Assuming that the
  307.     program actually does open 15 handles pointing to 15 different
  308.     files, other programs could still open a total of 22 files (40-3-15
  309.     = 22), though no one program could open more than 15 file handles.
  310.  
  311.     If you're running DOS 3.3 or later, you can increase the per-process
  312.     limit of 20 file handles by a call to INT 21 function 67, Set Handle
  313.     Count.  Your program is still limited by the system-wide limit on
  314.     open files, so you may also need to increase the FILES= value in
  315.     your CONFIG.SYS file (and reboot).  The run-time library that you're
  316.     using may have a fixed-size table of file handles, so you may also
  317.     need to get source code for the module that contains the table,
  318.     increase the table size, and recompile it.
  319.  
  320.     2) Limitations in Microsoft C run-time library
  321.  
  322.     In Microsoft C the run-time library limits you to 20 file handles.
  323.     To change this, you must be aware of two limits:
  324.  
  325.     - file handles used with _open( ), _read( ), etc.: Edit _NFILE_ in
  326.       CRT0DAT.ASM.
  327.  
  328.     - stream files used with fopen( ), fread( ), etc.: Edit _NFILE_ in
  329.       _FILE.C for DOS or FILE.ASM for Windows/QuickWin.  This must not
  330.       exceed the value of _NFILE_ in CRT0DAT.ASM.
  331.  
  332.     (QuickWin uses the constant _WFILE_ in CRT0DAT.ASM and WFILE.ASM for
  333.     the maximum number of child text windows.)
  334.  
  335.     After changing the limits, recompile using CSTARTUP.BAT.  Microsoft
  336.     recommends that you first read README.TXT in the same directory.
  337.  
  338.     3) Limitations in Borland C++ run-time library
  339.  
  340.     (Reader Chin Huang provided this information on 12 Sep 1993.)
  341.  
  342.     To increase the open file limit for a program you compile with
  343.     Borland C++ 3.1, edit the file _NFILE.H in the include directory and
  344.     change the _NFILE_ value.  Compile and link the modules FILES.C and
  345.     FILES2.C from the lib directory into your program.
  346.  
  347. Subject:  412. How can I read, create, change, or delete the volume
  348.                label?
  349.  
  350.     In DOS 5.0 (and possibly in 4.0 as well), there are actually two
  351.     volume labels: one, the traditional one, is an entry in the root
  352.     directory of the disk; and the other is in the boot record along
  353.     with the serial number (see next Q).  The DIR and VOL commands
  354.     report the traditional label; the LABEL command reports the
  355.     traditional one but changes both of them.
  356.  
  357.     In DOS 4.0 and later, use INT 21 function 69 to access the boot
  358.     record's serial number and volume label together; see the next Q.
  359.  
  360.     Assume that by "volume label" you mean the traditional one, the one
  361.     that DIR and VOL display.  Though it's a directory entry in the root
  362.     directory, you can't change it using the newer DOS file-access
  363.     functions (3C, 41, 43); instead, use the old FCB-oriented directory
  364.     functions.  Specifically, you need to allocate a 64-byte buffer and
  365.     a 41- byte extended FCB (file control block).  Call INT 21 AH=1A to
  366.     find out whether there is a volume label.  If there is, AL returns 0
  367.     and you can change the label using DOS function 17 or delete it
  368.     using DOS function 13.  If there's no volume label, function 1A will
  369.     return FF and you can create a label via function 16.  Important
  370.     points to notice are that ? wildcards are allowed but * are not; the
  371.     volume label must be space filled not null terminated.
  372.  
  373.     The following MSC 7.0 code worked for SB in DOS 5.0; the functions
  374.     it uses have been around since DOS 2.0.  The function parameter is 0
  375.     for the current disk, 1 for a:, 2 for b:, etc.  It doesn't matter
  376.     what your current directory is; these functions always search the
  377.     root directory for volume labels.  (I didn't try to change the
  378.     volume label of any networked drives.)
  379.  
  380.     // Requires DOS.H, STDIO.H, STRING.H
  381.     void vollabel(unsigned char drivenum) {
  382.         static unsigned char extfcb[41], dta[64], status, *newlabel;
  383.         int chars_got = 0;
  384.         #define DOS(buff,func) __asm { __asm mov dx,offset buff \
  385.             __asm mov ax,seg buff  __asm push ds  __asm mov ds,ax \
  386.             __asm mov ah,func  __asm int 21h  __asm pop ds \
  387.             __asm mov status,al }
  388.         #define getlabel(buff,prompt) newlabel = buff;  \
  389.             memset(newlabel,' ',11);  printf(prompt);   \
  390.             scanf("%11[^\n]%n", newlabel, &chars_got);  \
  391.             if (chars_got < 11) newlabel[chars_got] = ' ';
  392.  
  393.         // Set up the 64-byte transfer area used by function 1A.
  394.         DOS(dta, 1Ah)
  395.         // Set up an extended FCB and search for the volume label.
  396.         memset(extfcb, 0, sizeof extfcb);
  397.         extfcb[0] = 0xFF;             // denotes extended FCB
  398.         extfcb[6] = 8;                // volume-label attribute bit
  399.         extfcb[7] = drivenum;         // 1=A, 2=B, etc.; 0=current drive
  400.         memset(&extfcb[8], '?', 11);  // wildcard *.*
  401.         DOS(extfcb,11h)
  402.         if (status == 0) {            // DTA contains volume label's FCB
  403.             printf("volume label is %11.11s\n", &dta[8]);
  404.             getlabel(&dta[0x18], "new label (\"delete\" to delete): ");
  405.             if (chars_got == 0)
  406.                 printf("label not changed\n");
  407.             else if (strncmp(newlabel,"delete     ",11) == 0) {
  408.                 DOS(dta,13h)
  409.                 printf(status ? "label failed\n" : "label deleted\n");
  410.             }
  411.             else {                    // user wants to change label
  412.                 DOS(dta,17h)
  413.                 printf(status ? "label failed\n" : "label changed\n");
  414.             }
  415.         }
  416.         else {                        // no volume label was found
  417.             printf("disk has no volume label.\n");
  418.             getlabel(&extfcb[8], "new label (<Enter> for none): ");
  419.             if (chars_got > 0) {
  420.                 DOS(extfcb,16h)
  421.                 printf(status ? "label failed\n" : "label created\n");
  422.             }
  423.         }
  424.     }   // end function vollabel
  425.  
  426. Subject:  413. How can I get the disk serial number?
  427.  
  428.     Use INT 21.  AX=6900 gets the serial number; AX=6901 sets it.  See
  429.     Ralf Brown's interrupt list, or page 496 of {PC Magazine} July 1992,
  430.     for details.
  431.  
  432.     This function also gets and sets the volume label, but it's the
  433.     volume label in the boot record, not the volume label that a DIR
  434.     command displays.  See the preceding Q.
  435.  
  436. Subject:  414. What's the format of .OBJ, .EXE., .COM files?
  437.  
  438.     Please see section 2, "Compile and link".
  439.  
  440. Subject:  415. How can I flush the software disk cache?
  441.  
  442.     Please see "How can a program reboot my PC?" in section 7, "Other
  443.     software questions and problems".
  444.  
  445. section 5. Serial ports (COM ports)
  446. ===================================
  447.  
  448. Subject:  501. How do I set my machine up to use COM3 and COM4?
  449.  
  450.     Unless your machine is fairly old, it's probably already set up.
  451.     After installing the board that contains the extra COM port(s),
  452.     check the I/O addresses in word 0040:0004 or 0040:0006.  (In DEBUG,
  453.     type "D 40:4 L4" and remember that every word is displayed low
  454.     byte first, so if you see "03 56" the word is 5603.)  If those
  455.     addresses are nonzero, your PC is ready to use the ports and you
  456.     don't need the rest of this answer.
  457.  
  458.     If the I/O address words in the 0040 segment are zero after you've
  459.     installed the I/O board, you need some code to store these values
  460.     into the BIOS data segment:
  461.  
  462.         0040:0004  word  I/O address of COM3
  463.         0040:0006  word  I/O address of COM4
  464.         0040:0011  byte (bits 3-1): number of serial ports installed
  465.  
  466.     The documentation with your I/O board should tell you the port
  467.     addresses.  When you know the proper port addresses, you can add
  468.     code to your program to store them and the number of serial ports
  469.     into the BIOS data area before you open communications.  Or you can
  470.     use DEBUG to create a little program to include in your AUTOEXEC.BAT
  471.     file, using this script:
  472.  
  473.             n SET_ADDR.COM      <--- or a different name ending in .COM
  474.             a 100
  475.             mov  AX,0040
  476.             mov  DS,AX
  477.             mov  wo [0004],aaaa <--- replace aaaa with COM3 address or 0
  478.             mov  wo [0006],ffff <--- replace ffff with COM4 address or 0
  479.             and  by [0011],f1
  480.             or   by [0011],8    <--- use number of serial ports times 2
  481.             mov  AH,0
  482.             int  21
  483.                                 <--- this line must be blank
  484.             rCX
  485.             1f
  486.             rBX
  487.             0
  488.             w
  489.             q
  490.  
  491. Subject:  502. How do I find the I/O address of a COM port?
  492.  
  493.     (rev: 15 Aug 1993)  Look in the four words beginning at 0040:0000
  494.     for COM1 through COM4.  (The DEBUG command "D 40:0 L8" will do this.
  495.     Remember that words are stored and displayed low byte first, so a
  496.     word value of 03F8 will be displayed as F8 03.)  If the value is
  497.     zero, that COM port is not installed (or you've got an old BIOS; see
  498.     the preceding Q).  If the value is nonzero, it is the I/O address of
  499.     the transmit/receive register for the COM port.  Each COM port
  500.     occupies eight consecutive I/O addresses (though only the first
  501.     seven are used by many chips).
  502.  
  503.     Here's some C code to find the I/O address:
  504.  
  505.         unsigned ptSel(unsigned comport) {
  506.             unsigned io_addr;
  507.             if (comport >= 1  &&  comport <= 4) {
  508.                 unsigned far *com_addr = (unsigned far *)0x00400000UL;
  509.                 io_addr = com_addr[comport-1];
  510.             }
  511.             else
  512.                 io_addr = 0;
  513.             return io_addr;
  514.         }
  515.  
  516.     You might also want to explore Port Finder, downloadable as
  517.  
  518.         pd1:<msdos.sysutl>pf271.zip at Simtel
  519.         /pub/msdos/utilities/sysutl/pf271.zip at nic.funet.fi
  520.  
  521.     I (SB) haven't tried it myself, but a posted article reviewed it
  522.     very favorably and said it also lets you swap ports around.
  523.  
  524. Subject:  503. But aren't the COM ports always at I/O addresses 3F8,
  525.                2F8, 3E8, and 2E8?
  526.  
  527.     The first two are usually right (though not always); the last two
  528.     are different on many machines.
  529.  
  530. Subject:  504. How do I configure a COM port and use it to transmit data?
  531.  
  532.     (rev: 17 Sep 1993)  Do you want actual code, or do you want books
  533.     that explain what's going on?
  534.  
  535.     1) Source code
  536.  
  537.     First, check your compiler's run-time library.  Many compilers offer
  538.     functions similar to Microsoft C's _bios_serialcom() or Borland's
  539.     bioscom(), which may meet your needs.
  540.  
  541.     Second, check for downloadable resources at Simtel and Garbo.  At
  542.     Simtel, pd1:<msdos.c>pcl4c34.zip (March 1993) is described as
  543.     "Asynchronous communications library for C"; Garbo has a whole
  544.     /pc/comm directory.  Also, an extended example is in Borland's
  545.     TechFax TI445, downloadable as part of
  546.         pd1:<msdos.turbo-c>bchelp10.zip at Simtel
  547.         /pc/turbopas/bchelp10.zip at Garbo.
  548.     Though written by Borland, much of it is applicable to other forms
  549.     of C, and it should give you ideas for other programming languages.
  550.  
  551.     2) Reference books
  552.  
  553.     Highly recommended: Joe Campbell's {C Programmer's Guide to Serial
  554.     Communications}, ISBN 0-672-22584-0.  He gives complete details on
  555.     how serial ports work, along with complete programs for doing polled
  556.     or interrupt-driver I/O.  The book is quite thick, and none of it
  557.     looks like filler.
  558.  
  559.     If Campbell's book is overkill for you, you'll find a good short
  560.     description of serial I/O in {DOS 5: A Developer's Guide}, ISBN
  561.     1-55851-177-6, by Al Williams.
  562.  
  563.     Finally, a reader has recommended {Serial Communications Programming
  564.     in C/C++} by Mark Goodwin (ISBN 1558281983), with source code in the
  565.     book and on disk.  Topics include the basics, various methods of
  566.     serial communications on the PC (with consideration of high-speed
  567.     modems), ANSI screen interface, file transfer protocols (Xmodem and
  568.     Ymodem), etc.  There is code in C, and that code is extended into a
  569.     C++ class for those who use C++.  There are also subroutines in
  570.     Assembly.
  571.  
  572.     3) Downloadable information files
  573.  
  574.     A "Serial Port FAQ" is occasionally posted to this newsgroup.  You
  575.     can get a copy by ftp from pfsparc02.phil15.uni-sb.de.  Look for
  576.     file names *Serial* in directory /pub/E-Technik/afd .  (The archive
  577.     administrator warns that the ftp address may change, sometime in
  578.     the future, to etcip1.ee.uni-sb.de .)  North American users should
  579.     access rtfm.mit.edu, directory /pub/usenet/comp.os.msdos.programmer,
  580.     file names T_S_P*_3.
  581.  
  582. section 6. Other hardware questions and problems
  583. ================================================
  584.  
  585. Subject:  601. Which 80x86 CPU is running my program?
  586.  
  587.     (rev: 16 Aug 1993)  According to an article posted by Michael
  588.     Davidson, Intel's approved code for distinguishing among 8086,
  589.     80286, 80386, and 80486 and for detecting the presence of an 80287
  590.     or 80387 is published in Intel's 486SX processor manual (order
  591.     number 240950-001).  David Kirschbaum's improved version of this is
  592.     downloadable as
  593.         pd1:<msdos.sysutl>cpuid593.zip from Simtel
  594.         /pc/sysinfo/cpuid593.zip from Garbo.
  595.  
  596.     According to an article posted by its author, WCPU knows the
  597.     differences between DX and SX varieties of 386 and 486 chips, and
  598.     can detect a math coprocessor and a Pentium.  It's downloadable as
  599.         pd1:<msdos.sysinfo>wcpu050.zip at Simtel
  600.         /pc/sysinfo/wcpu050.zip at Garbo.
  601.  
  602. Subject:  602. How can a C program send control codes to my printer?
  603.  
  604.     If you just fprintf(stdprn, ...), C will translate some of your
  605.     control codes.  The way around this is to reopen the printer in
  606.     binary mode:
  607.  
  608.         prn = fopen("PRN", "wb");
  609.  
  610.     You must use a different file handle because stdprn isn't an lvalue.
  611.     By the way, PRN or LPT1 must not be followed by a colon in DOS 5.0.
  612.  
  613.     There's one special case, Ctrl-Z (ASCII 26), the DOS end-of-file
  614.     character.  If you try to send an ASCII 26 to your printer, DOS
  615.     simply ignores it.  To get around this, you need to reset the
  616.     printer from "cooked" to "raw" mode.  Microsoft C users must use int
  617.     21 function 44, "get/set device information".  Turbo C and Borland
  618.     C++ users can use ioctl to accomplish the same thing:
  619.  
  620.         ioctl(fileno(prn), 1, ioctl(fileno(prn),0) & 0xFF | 0x20, 0);
  621.  
  622.     An alternative approach is simply to write the printer output into a
  623.     disk file, then copy the file to the printer with the /B switch.
  624.  
  625.     A third approach is to bypass DOS functions entirely and use the
  626.     BIOS printer functions at INT 17.  If you also fprintf(stdprn,...)
  627.     in the same program, you'll need to use fflush( ) to synchronize
  628.     fprintf( )'s buffered output with the BIOS's unbuffered.
  629.  
  630.     By the way, if you've opened the printer in binary mode from a C
  631.     program, remember that outgoing \n won't be translated to carriage
  632.     return/line feed.  Depending on your printer, you may need to send
  633.     explicit \n\r sequences.
  634.  
  635. Subject:  603. How can I redirect printer output to a file?
  636.  
  637.     Please see section 4, "Disks and files", for the answer.
  638.  
  639. Subject:  604. Which video adapter is installed?
  640.  
  641.     The technique below should work if your BIOS is not too old.  It
  642.     uses three functions from INT 10, the BIOS video interrupt.  (If
  643.     you're using a Borland language, you may not have to do this the
  644.     hard way.  Look for a function called DetectGraph or something
  645.     similar.)
  646.  
  647.     Set AH=12h, AL=0, BL=32h; INT 10h.  If AL is 12h, you have a VGA.
  648.     If not, set AH=12h, BL=10h; INT 10h.  If BL is 0,1,2,3, you have an
  649.     EGA with 64,128,192,256K memory.  If not, set AH=0Fh; INT 10h.  If
  650.     AL is 7, you have an MDA (original monochrome adapter) or Hercules;
  651.     if not, you have a CGA.
  652.  
  653.     This worked when tested with a VGA, but SB had no other adapter
  654.     types to test it with.
  655.  
  656. Subject:  605. How do I switch to 43- or 50-line mode?
  657.  
  658.     pd1:<msdos.screen>vidmode.zip, downloadable from Simtel, contains
  659.     .COM utilities and .ASM source code.
  660.  
  661. Subject:  606. How can I find the Microsoft mouse position and button
  662.                status?
  663.  
  664.     Use INT 33 function 3, described in Ralf Brown's interrupt list.
  665.  
  666.     The Windows manual says that the Logitech mouse is compatible with
  667.     the Microsoft one, so the interrupt will probably work the same.
  668.  
  669.     Also, many files are downloadable from pd1:<msdos.mouse> at Simtel.
  670.  
  671. Subject:  607. How can I access a specific address in the PC's memory?
  672.  
  673.     First check the library that came with your compiler.  Many vendors
  674.     have some variant of peek and poke functions; in Turbo Pascal use
  675.     the pseudo-arrays Mem, MemW, and MemL.  As an alternative, you can
  676.     construct a far pointer:  use Ptr in Turbo Pascal, MK_FP in the
  677.     Turbo C family, and FP_OFF and FP_SEG in Microsoft C.
  678.  
  679.     Caution:  Turbo C and Turbo C++ also have FP_OFF and FP_SEG macros,
  680.     but they can't be used to construct a pointer.  In Borland C++ those
  681.     macros work the same as in Microsoft C, but MK_FP is easier to use.
  682.  
  683.     By the way, it's not useful to talk about "portable" ways to do
  684.     this.  Any operation that is tied to a specific memory address is
  685.     not likely to work on another kind of machine.
  686.  
  687. Subject:  608. How can I read or write my PC's CMOS memory?
  688.  
  689.     (rev: 24 Sep 1993) There are a great many public-domain utilities
  690.     that do this.  These are downloadable from Simtel:
  691.  
  692.     pd1:<msdos.at>
  693.     cmos14.zip     5965  920817  Saves/restores CMOS to/from file
  694.     cmoser11.zip  28323  910721  386/286 enhanced CMOS setup program
  695.     cmosram.zip   76096  920214  Save AT/386/486 CMOS data to file and restore
  696.     rom2.zip      15692  900131  Save AT and 386 CMOS data to file and restore
  697.     setup21.zip   18172  880613  Setup program which modifies CMOS RAM
  698.     viewcmos.zip  11068  900225  Display contents of AT CMOS RAM, w/C source
  699.  
  700.     A program to check and display CMOS memory (but not write to it) is
  701.     downloadable as part of
  702.         /pc/ts/tsutle22.zip at Garbo
  703.         pd1:<msdos.sysutl>tsutle22.zip at Simtel.
  704.  
  705.     Good reports of CMOS299.ZIP, available in the pc.dir directory of
  706.     cantva.canterbury.ac.nz [132.181.30.3], have been posted.
  707.  
  708.     Of the above, SB's only experience is with CMOSRAM, which seems to
  709.     work fine.  It contains an excellent (and witty) .DOC file that
  710.     explains the hardware involved and gives specific recommendations
  711.     for preventing disaster or recovering from it.  It's $5 shareware.
  712.  
  713.     Robert Jourdain's {Programmer's Problem Solver for the IBM PC, XT,
  714.     and AT} has code for accessing the CMOS RAM, according to an article
  715.     posted in this newsgroup.
  716.  
  717. Subject:  609. How can I access memory beyond 640K?
  718.  
  719.     (rev: 14 Sep 1993) This is a legitimate FAQ, in that it is frequently
  720.     asked.  But there is no single agreed-upon answer.  Please see the
  721.     separate article called "How to access memory above 640K" in
  722.     comp.os.msdos.programmer and in faqp*.zip at Simtel and Garbo.
  723.  
  724.     The 29 June 1993 issue (xii:12) of {PC Magazine} carries an article,
  725.     "How DOS Programs Can Use Over 1MB of RAM" on pages 302-304.
  726.  
  727. Subject:  610. Where can I find a list of 80x86 opcodes?
  728.  
  729.     (new: 2 May 1993)  It's part of a rather long file, the 8 Dec 1992
  730.     edition of the Info-IBMPC Digest (V92 #185), downloadable as
  731.     pd2:<archives.ibmpc>9212.1-txt at Simtel.  (Note: pd2, not
  732.     pd1.)  Opcodes for the 8086 through 80386 are listed.
  733.  
  734. section 7. Other software questions and problems
  735. ================================================
  736.  
  737. Subject:  701. How can a program reboot my PC?
  738.  
  739.     (rev: 11 Sep 1993) You can generate a "cold" boot or a "warm" boot.
  740.     A cold boot is the same as turning the power off and on; a warm boot
  741.     is the same as Ctrl-Alt-Del and skips the power-on self test.
  742.  
  743.     For a warm boot, store the hex value 1234 in the word at 0040:0072.
  744.     For a cold boot, store 0 in that word.  Then, if you want to live
  745.     dangerously, jump to address FFFF:0000.  Here's C code to do it:
  746.  
  747.         /* WARNING:  data loss possible */
  748.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  749.             void (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  750.             unsigned far* type = (unsigned far*)0x00400072UL;
  751.             *type = (want_warm ? 0x1234 : 0);
  752.             (*boot)( );
  753.         }
  754.  
  755.     What's wrong with that method?  It will boot right away, without
  756.     closing files, flushing disk caches, etc.  If you boot without
  757.     flushing a write-behind disk cache (if one is running), you could
  758.     lose data or even trash your hard drive.
  759.  
  760.     There are two methods of signaling the cache to flush its buffers:
  761.     (1) simulate a keyboard Ctrl-Alt-Del in the keystroke translation
  762.     function of the BIOS (INT 15 function 4F; but see notes below), and
  763.     (2) issue a disk reset (DOS function 0D).  Most disk-cache programs
  764.     hook one or both of those interrupts, so if you use both methods
  765.     you'll probably be safe.
  766.  
  767.     When user code simulates a Ctrl-Alt-Del, one or more of the programs
  768.     that have hooked INT 15 function 4F can ask that the key be ignored by
  769.     clearing the carry flag.  For example, HyperDisk does this when it
  770.     has started but not finished a cache flush.  So if the carry flag
  771.     comes back cleared, the boot code has to wait a couple of clock
  772.     ticks and then try again.  (None of this matters on older machines
  773.     whose BIOS can't support 101- or 102-key keyboards; see "What is the
  774.     SysRq key for?" in section 3, "Keyboard".)
  775.  
  776.     C code that tries to signal the disk cache (if any) to flush is
  777.     given below.  Turbo Pascal code by Timo Salmi that does more or less
  778.     the same job may be found at question 49 (as of this writing) in the
  779.     Turbo Pascal FAQ in comp.lang.pascal, and is downloadable in
  780.     FAQPAS2.TXT in
  781.         /pc/ts/tsfaqp15.zip at Garbo
  782.         pd1:<msdos.info>tsfaqp15.zip at Simtel.
  783.  
  784.     Here's C code that reboots after trying to signal the disk cache:
  785.         #include <dos.h>
  786.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  787.             union REGS reg;
  788.             void    (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  789.             unsigned far* boottype    =     (unsigned far*)0x00400072UL;
  790.             char     far* shiftstate  =         (char far*)0x00400017UL;
  791.             unsigned      ticks;
  792.             int           time_to_waste;
  793.             /* Simulate reception of Ctrl-Alt-Del: */
  794.             for (;;) {
  795.                 *shiftstate |= 0x0C;    /* turn on Ctrl & Alt */
  796.                 reg.h.ah = 0x4F;        /* see notes below */
  797.                 reg.h.al = 0x53;        /* 0x53 = Del's scan code */
  798.                 reg.x.cflag = 1;        /* sentinel for ignoring key */
  799.                 int86(0x15, ®, ®);
  800.                 /* If carry flag is still set, we've finished. */
  801.                 if (reg.x.cflag)
  802.                     break;
  803.                 /* Else waste some time before trying again: */
  804.                 reg.h.ah = 0;
  805.                 int86(0x1A, ®, ®);/* system time into CX:DX */
  806.                 ticks = reg.x.dx;
  807.                 for (time_to_waste = 3;  time_to_waste > 0;  ) {
  808.                     reg.h.ah = 0;
  809.                     int86(0x1A, ®, ®);
  810.                     if (ticks != reg.x.dx)
  811.                         ticks = reg.x.dx , --time_to_waste;
  812.                 }
  813.             }
  814.             /* Issue a DOS disk reset request: */
  815.             reg.h.ah = 0x0D;
  816.             int86(0x21, ®, ®);
  817.             /* Set boot type and boot: */
  818.             *boottype = (want_warm ? 0x1234 : 0);
  819.             (*boot)( );
  820.         }
  821.  
  822.     Reader Timo Salmi reported (26 July 1993) that the INT 15 AH=4F call
  823.     may not work on older PCs (below AT, XT2, XT286), according to Ralf
  824.     Brown's interrupt list.
  825.  
  826.     Reader Roger Fulton reported (1 July 1993) that INT 15 AH=4F call
  827.     above hangs even a modern PC "ONLY when ANSI.SYS [is] loaded high
  828.     using EMM386.EXE.  (Other things loaded high with EMM386.EXE were
  829.     OK; ANSI.SYS loaded high with QEMM386.SYS was OK; ANSI.SYS loaded
  830.     low with EMM386.EXE installed was OK.)"  His solution was to use
  831.     only the disk reset, INT 21 function 0D, which does flush SMARTDRV,
  832.     then wait five seconds in hopes that any other disk-caching
  833.     software would have time to flush its queue.
  834.  
  835.     If you have a more bulletproof solution, please send it to the
  836.     editor.
  837.  
  838.     Reader Per Bergland reported (10 Sep 1993) that the jump to
  839.     FFFF:0000 will not work in Windows or other protected-mode programs.
  840.     (For example, when the above reboot code ran in a DOS session under
  841.     Windows, a box with "waiting for system shutdown" appeared.  The PC
  842.     hung and had to be reset by cycling power.)  His solution, which does
  843.     a cold boot not a warm boot, is to pulse pin 0 of the 8042 keyboard
  844.     controller, which is connected to the CPU's "reset" line.  He has
  845.     tested the following code on various Compaqs, and expects it will
  846.     work for any AT-class machine; he cautions that you must first flush
  847.     the disk cache as indicated above.
  848.  
  849.             cli
  850.         @@WaitOutReady:   { Busy-wait until 8042 is ready for new command}
  851.             in al,64h         { read 8042 status byte}
  852.             test al,00000010b { Bit 1 of status indicates input buffer full }
  853.             jnz @@WaitOutReady
  854.             mov al,0FEh       { Pulse "reset" = 8042 pin 0 }
  855.             out 64h,al
  856.             { The PC will reboot now }
  857.  
  858. Subject:  702. How can I time events with finer resolution than the
  859.                system clock's 55 ms (about 18 ticks a second)?
  860.  
  861.     (rev: 28 Aug 1993) The following files, among others, are
  862.     downloadable from Simtel:
  863.  
  864.     pd1:<msdos.at>
  865.     atim.zip       4783  881126  Precision program timing for AT
  866.  
  867.     pd1:<msdos.c>
  868.     millisec.zip  37734  911205  MSC/asm src for millisecond res timing
  869.     mschrt3.zip   53708  910605  High-res timer toolbox for MSC 5.1
  870.     msec_12.zip    8484  920320  High-def millisec timer v1.2 (C,ASM)
  871.     ztimer11.zip  77625  920428  Microsecond timer for C, C++, ASM
  872.         (also at Garbo as /pc/c/ztimer11.zip)
  873.  
  874.     pd1:<msdos.turbo-c>
  875.     tchrt3.zip    53436  910606  High-res timer toolbox for Turbo C 2.0
  876.     tctimer.arc   20087  891030  High-res timing of events for Turbo C
  877.         (same as /pc/c/tctimer.zoo at Garbo; both are version 1.0)
  878.  
  879.     For Turbo Pascal users, source and object code are downloadable in
  880.         pd1:<msdos.turbopas>bonus507.zip at Simtel
  881.         /pc/turbopas/bonus507.zip at Garbo.
  882.     Also see "Q: How is millisecond timing done?" in FAQPAS.TXT,
  883.     downloadable as
  884.         /pc/ts/tsfaqp15.zip at Garbo
  885.         pd1:<msdos.info>tsfaqp15.zip at Simtel.
  886.  
  887. Subject:  703. How can I find the error level of the previous program?
  888.  
  889.     (rev: 16 Aug 1993)  First, which previous program are you talking
  890.     about?  If your current program ran another one, when the child
  891.     program ends its error level is available to the program that
  892.     spawned it.  Most high-level languages provide a way to do this; for
  893.     instance, in Turbo Pascal it's Lo(DosExitCode) and the high byte
  894.     gives the way in which the child terminated.  In Microsoft C, the
  895.     exit code of a synchronous child process is the return value of the
  896.     spawn-type function that creates the process.
  897.  
  898.     If your language doesn't have a function to return the error code
  899.     of a child process, you can use INT 21 function 4D (get return
  900.     code).  By the way, this will tell you the child's exit code and the
  901.     manner of its ending (normal, Ctrl-C, critical error, or TSR).
  902.  
  903.     It's much trickier if the current program wants to get the error
  904.     level of the program that ran and finished before this one started.
  905.     G.A.Theall has published source and compiled code to do this; the
  906.     code is downloadable as
  907.         pd1:<msdos.batutl>errlvl13.zip at Simtel
  908.         /pc/batchutil/errlvl12.zip (an older version) at Garbo.
  909.     (The code uses undocumented features in DOS 3.3 through 5.0.  Theall
  910.     says in the .DOC file that the values returned under 4DOS or other
  911.     replacements won't be right.)
  912.  
  913. Subject:  704. How can a program set DOS environment variables?
  914.  
  915.     (rev: 13 June 1993)  Program functions that read or write "the
  916.     environment" typically access only the program's copy of it.  What
  917.     this Q really wants to do is to modify the active environment, the
  918.     one that is affected by SET commands in batch files or at the DOS
  919.     prompt.  You need to do some programming to find the active
  920.     environment, and that depends on the version of DOS.
  921.  
  922.     A fairly well-written article in {PC Magazine} 28 Nov 1989
  923.     (viii:20), pages 309-314, explains how to find the active
  924.     environment, and includes Pascal source code.  The article hints at
  925.     how to change the environment, and suggests creating paths longer
  926.     than 128 characters as one application.
  927.  
  928.     Now as for downloadable source code, there are many possibilities.
  929.     SB looked at some of these, and liked
  930.         pd1:<msdos.envutil>rbsetnv1.zip at Simtel
  931.         /pc/envutil/rbsetnv1.zip at Garbo
  932.     the best.  It includes some utilities to manipulate the environment,
  933.     with source code in C.  A newer program is
  934.         pd1:<msdos.batutl>strings2.zip at Simtel
  935.         part of /pc/pcmag/vol11n22.zip at Garbo,
  936.     which is the code from {PC Magazine} 22 Dec 1992 (xi:22).
  937.  
  938.     You can also use a call to INT 2E, Pass Command to Interpreter for
  939.     Execution; see Ralf Brown's interrupt list for details and cautions.
  940.  
  941. Subject:  705. How can I change the switch character to - from /?
  942.  
  943.     Under DOS 5.0, you can't -- not completely, anyway.  INT 21 function
  944.     3700, get switch character, always returns a '/' (hex 2F) -- and the
  945.     DOS commands don't even call that function, but hard code '/' as the
  946.     switch character.
  947.  
  948.     Some history:  DOS used to let you change the switch character by
  949.     using SWITCHAR= in CONFIG.SYS or by calling DOS function 3701.  DOS
  950.     commands and other programs called DOS function 3700 to find out the
  951.     switch character.  If you changed the switch character to '-' (the
  952.     usual choice), you could then type "dir c:/c700 -p" rather than "dir
  953.     c:\c700 /p".  Under DOS 4.0, the DOS commands ignored the switch
  954.     character but functions 3700 and 3701 still worked and could be used
  955.     by other programs.  Under DOS 5.0, even those functions no longer
  956.     work, though all DOS functions still accept '/' or '\' in file
  957.     specs.
  958.  
  959.     You can reactivate the functions to get and set switchar by using
  960.     programs like SLASH.ZIP or the sample TSR called SWITCHAR in
  961.     amisl091.zip (see "How can I write a TSR?", below.)  DOS commands
  962.     will still use the slash, but non-DOS programs that call DOS func-
  963.     tion 3700 will use your desired switch character.  (DOS replacements
  964.     like 4DOS may honor the switch character for internal commands.)
  965.  
  966.     Some readers may wonder why this is even an issue.  Making '-' the
  967.     switch character frees up the front slash to separate names in the
  968.     path part of a file spec.  This is easier for the ten-fingered to
  969.     type, and it's one less difference to remember for commuters between
  970.     DOS and Unix.  The switch character is the only issue, since all the
  971.     INT 21 functions accept '/' or '\' to separate directory names.
  972.  
  973. Subject:  706. Why does my interrupt function behave strangely?
  974.  
  975.     (rev: 24 Sep 1993)  Interrupt service routines can be tricky,
  976.     because you have to do some things differently from "normal"
  977.     programs.  If you make a mistake, debugging is a pain because the
  978.     symptoms may not point at what's wrong.  Your machine may lock up or
  979.     behave erratically, or just about anything else can happen.  Here
  980.     are some things to look for.  (See the next Q for general help
  981.     before you have a problem.)
  982.  
  983.     First, did you fail to set up the registers at the start of your
  984.     routine?  When your routine begins executing, you can count on
  985.     having CS point to your code segment and SS:SP point to some valid
  986.     stack (of unknown length), and that's it.  In particular, an
  987.     interrupt service routine must set DS to DGROUP before accessing any
  988.     data in its data segments.  (If you're writing in a high-level
  989.     language, the compiler may generate this code for you automatically;
  990.     check your compiler manual.  For instance, in Borland and Microsoft
  991.     C, give your function the "interrupt" attribute.)
  992.  
  993.     Did you remember to turn off stack checking when compiling your
  994.     interrupt server and any functions it calls?  The stack during the
  995.     interrupt is not where the stack-checking code expects it to be.
  996.     (Caution:  Some third-party libraries have stack checking compiled
  997.     in, so you can't call them from your interrupt service routine.)
  998.  
  999.     Next, are you calling any DOS functions (INT 21, 25, or 26) in your
  1000.     routine?  DOS is not re-entrant.  This means that if your interrupt
  1001.     happens to be triggered while the CPU is executing a DOS function,
  1002.     calling another DOS function will wreak havoc.  (Some DOS functions
  1003.     are fully re-entrant, as noted in Ralf Brown's interrupt list.
  1004.     Also, your program can test, in a way too complicated to present
  1005.     here, when it's safe to call non-re-entrant DOS functions.  See INT
  1006.     28 and functions 34, 5D06, 5D0B of INT 21; and consult {Undocumented
  1007.     DOS} by Andrew Schulman.  Your program must read both the "InDOS
  1008.     flag" and the "critical error flag".)
  1009.  
  1010.     Is a function in your language library causing trouble?  Does it
  1011.     depend on some initializations done at program startup that is no
  1012.     longer available when the interrupt executes?  Does it call DOS (see
  1013.     preceding paragraph)?  For example, in both Borland and Microsoft C
  1014.     the memory-allocation functions (malloc, etc..) and standard I/O
  1015.     functions (scanf, printf) call DOS functions and also depend on
  1016.     setups that they can't get at from inside an interrupt.  Many other
  1017.     library functions have the same problem, so you can't use them
  1018.     inside an interrupt function without special precautions.
  1019.  
  1020.     Is your routine simply taking too long?  This can be a problem if
  1021.     you're hooking on to the timer interrupt, INT 1C or INT 8.  Since
  1022.     that interrupt expects to be called 18.2 times a second, your
  1023.     routine -- plus any others hooked to the same interrupts -- must
  1024.     execute in less than 55 ms.  If they use even a substantial fraction
  1025.     of that time, you'll see significant slowdowns of your foreground
  1026.     program.  A good discussion is downloadable as
  1027.         pub/msdos/SIMTEL20-mirror/info/intshare.zip at ni.funet.fi
  1028.         pd1:<msdos.info>intshare.zip at Simtel.
  1029.  
  1030.     Did you forget to restore all registers at the end of your routine?
  1031.  
  1032.     Did you chain improperly to the original interrupt?  You need to
  1033.     restore the stack to the way it was upon entry to your routine, then
  1034.     do a far jump (not call) to the original interrupt service routine.
  1035.     (The process is a little different in high-level languages.)
  1036.  
  1037. Subject:  707. How can I write a TSR (terminate-stay-resident utility)?
  1038.  
  1039.     (rev: 20 June 1993)  There are books, and there's code to download.
  1040.  
  1041.     First, the books:
  1042.  
  1043.     - Ray Duncan's {Advanced MS-DOS}, ISBN 1-55615-157-8, gives a brief
  1044.       checklist intended for experienced programmers.  The ISBN is for
  1045.       the second edition, through DOS 4; but check to see whether the
  1046.       DOS 5 version is available yet.
  1047.  
  1048.     - {DOS 5:  A Developer's Guide} by Al Williams, ISBN 1-55851-177-6,
  1049.       goes into a little more detail, 90 pages worth!
  1050.  
  1051.     - Pascal programmers might look at {The Ultimate DOS Programmer's
  1052.       Manual} by John Mueller and Wallace Wang, ISBN 0-8306-3534-3, for
  1053.       an extended example in mixed Pascal and assembler.
  1054.  
  1055.     - For a pure assembler treatment, check Steven Holzner's {Advanced
  1056.       Assembly Language}, ISBN 0-13-663014-6.  He has a book with the
  1057.       same title out from Brady Press, but it's about half as long as
  1058.       this one.
  1059.  
  1060.     Next, the code.  Some of it is companion code to published articles,
  1061.     which are also listed below:
  1062.  
  1063.     - The Alternate Multiplex Interrupt Specification, downloadable as
  1064.         pd1:<msdos.info>altmpx35.zip at Simtel
  1065.         /pc/programming/altmpx35.zip at Garbo
  1066.         /afs/cs/user/ralf/pub/altmpx35.zip at cs.cmu.edu
  1067.  
  1068.     - Ralf Brown's assembly-language implementation of the spec, with
  1069.       utilities in C, downloadable as
  1070.         pd1:<msdos.asmutl>amisl091.zip at Simtel
  1071.         /pc/c/amisl091.zip at Garbo
  1072.         /afs/cs/user/ralf/pub/amisl091.zip at cs.cmu.edu
  1073.  
  1074.     - Douglas Boling's MASM template for a TSR is downloadable as
  1075.         pd1:<msdos.asmutl>template.zip at Simtel.
  1076.  
  1077.     - A posted article mentions Boling's "Strategies and Techniques for
  1078.       Writing State-of-the-Art TSRs that Exploit MS-DOS 5", Microsoft
  1079.       Systems Journal, Jan-Feb 1992, Volume 7, Number 1, pages 41-59,
  1080.       with examples downloadable in
  1081.           pd1:<msdos.msjournal>msjv7-1.zip at Simtel
  1082.  
  1083.     - code for Al Stevens's "Writing Terminate-and-Stay-Resident
  1084.       Programs", Computer Language, February 1988, pages 37-48 and March
  1085.       1988, pages 67-76 is downloadable as
  1086.         pd1:<msdos.c>tsrc.zip at Simtel
  1087.  
  1088.     - software examples to accompany Kaare Christian's "Using Microsoft
  1089.       C Version 5.1 to Write Terminate-and-Stay-Resident Programs",
  1090.       Microsoft Systems Journal, September 1988, Volume 3, Number 5,
  1091.       pages 47-57 are downloadable as
  1092.           pd1:<msdos.msjournal>msjv3-5.arc at Simtel
  1093.  
  1094.     Finally, there are commercial products, of which TesSeRact (for
  1095.     C-language TSRs) is one of the best known.
  1096.  
  1097. Subject:  708. How can I write a device driver?
  1098.  
  1099.     Many books answer this in detail.  Among them are {Advanced MS-DOS}
  1100.     and {DOS 5: A Developer's Guide}, cited in the preceding Q.
  1101.     Michael Tischer's {PC System Programming}, ISBN 1-55755-036-0, has
  1102.     an extensive treatment, as does Dettman and Kyle's {DOS Programmer's
  1103.     Reference: 2d Edition}, ISBN 0-88022-458-4.  For a really in-depth
  1104.     treatment, look for a specialized book like Robert Lai's {Writing
  1105.     MS-DOS Device Drivers}, ISBN 0-201-13185-4.
  1106.  
  1107. Subject:  709. What can I use to manage versions of software?
  1108.  
  1109.     (rev: 21 Aug 1993) A port of the Unix RCS utility is downloadable as
  1110.         pd1:<msdos.gnuish>rcs55ax.zip (EXE and docs) from Simtel
  1111.         pd1:<msdos.gnuish>rcs55as.zip (source) from Simtel
  1112.         /pc/unix/alrcs5ex.zip (EXE and docs ?) from Garbo.
  1113.     This is no longer limited to one-character extensions on filenames
  1114.     (.CPP and .BAS are now OK).
  1115.  
  1116.     An RCS56 is available at a number of archive sites, but it appears
  1117.     to be unauthorized.  In response to a query, Keith Petersen, Simtel
  1118.     administrator, said that RCS56 was removed from Simtel at the
  1119.     author's request because it did not contain source code and thus was
  1120.     in violation of the GNU copyleft.
  1121.  
  1122.     As for commercial software, SB posted a question asking for readers'
  1123.     experiences in July 1993 and seven readers responded.  PVCS from
  1124.     Intersolv (formerly Polymake) got five positive reviews, though
  1125.     several readers commented that it's expensive; RCS from MKS got one
  1126.     positive and one negative review; Burton TLIB got one negative
  1127.     review; DRTS from ILSI got one positive review.
  1128.  
  1129. Subject:  710. What's this "null pointer assignment" after my C program
  1130.                executes?
  1131.  
  1132.     (rev: 17 Sep 1993)  Somewhere in your program, you assigned a value
  1133.     _through_ a pointer without first assigning a value _to_ the
  1134.     pointer.  (This might have been something like a strcpy or memcpy
  1135.     with a pointer as its first argument, not necessarily an actual
  1136.     assignment statement.) Your program may look like it ran correctly,
  1137.     but if you get this message you can be certain that there's a bug
  1138.     somewhere.
  1139.  
  1140.     Microsoft and Borland C, as part of their exit code (after a return
  1141.     from your main function), check whether the location 0000 in your
  1142.     data segment contains a different value from what you started with;
  1143.     if so, they infer that you must have used an uninitialized pointer.
  1144.     This implies that the message will appear at the end of execution of
  1145.     your program regardless of where the error actually occurred.
  1146.  
  1147.     To track down the problem, you can put exit( ) statements at various
  1148.     spots in the program and narrow down where the uninitialized pointer
  1149.     is being used by seeing which added exit( ) makes the null-pointer
  1150.     message disappear.  Or, in the debugger, set a watch at location
  1151.     0000 in your data segment, assuming you're in small or medium model.
  1152.     (If data pointers are 32 bits, as in the compact and large models, a
  1153.     null pointer will overwrite the interrupt vectors at 0000:0000 and
  1154.     probably lock up your machine.)
  1155.  
  1156.     Under MSC/C++ 7.0, you can declare the undocumented library function
  1157.  
  1158.         extern _cdecl _nullcheck(void);
  1159.  
  1160.     and then sprinkle calls to _nullcheck( ) through your program at
  1161.     regular intervals.
  1162.  
  1163.     Borland's TechFax document #TI726 discusses the null pointer
  1164.     assignment from a Borland point of view.  It's one of many documents
  1165.     downloadable as part of
  1166.         pd1:<msdos.turbo-c>bchelp10.zip at Simtel
  1167.         /pc/turbopas/bchelp10.zip at Garbo.
  1168.  
  1169. Subject:  711. How can my program tell if it's running under Windows?
  1170.  
  1171.     (rev: 18 Apr 1993)  Set AX=4680 and execute INT 2F.  If AX contains
  1172.     0, you're in Windows real mode or standard mode (or under the DOS
  1173.     5.0 shell).  Otherwise, set AX=1600 and INT 2F.  If AL does not
  1174.     contain 0 or 80, you're in Windows 386 enhanced mode.  See {PC
  1175.     Magazine} 24 Nov 1992 (xi:20), pages 492-493.
  1176.  
  1177.     When Windows 3.0 or 3.1 is running, the DOS environment will contain
  1178.     a definition of the string windir, in lower case.
  1179.  
  1180.     For more information, see {PC Magazine} 26 May 1992 (xi:10) pages
  1181.     345-346.  A program, WINMODE, is available as part of
  1182.         pd1:<msdos.pcmag>vol11n10.zip at Simtel
  1183.         /pc/pcmag/vol11n10.zip at Garbo.
  1184.  
  1185. Subject:  712. How do I copyright software that I write?
  1186.  
  1187.     (rev: 9 Sep 1993) The following is adapted (and greatly condensed)
  1188.     from chapter 4 of the Chicago Manual of Style (13th edition, ISBN
  1189.     0-226-10390-0).  Disclaimer:  This is not written by a lawyer, and
  1190.     is not legal advice.  Also, there are very likely to be differences
  1191.     in copyright law among nations.  No matter where you live, if
  1192.     significant money may be involved, get legal advice.
  1193.  
  1194.     That said, in the U.S. (at least), when you write something, you own
  1195.     the copyright.  (The most significant exception to programmers is
  1196.     "works made for hire", i.e., something you write because your
  1197.     employer or client pays you to.  A contract, agreed in advance, can
  1198.     vest the copyright in the programmer even if an employee.)  You
  1199.     don't have to register the work with the Copyright Office unless
  1200.     (until) the copyright is infringed and you intend to bring suit;
  1201.     however, it is easier to recover damages in court if you did
  1202.     register the work within three months of publication.
  1203.  
  1204.     From paragraph 4.16 of the Chicago Manual:  "... the [copyright]
  1205.     notice consists of three parts: (1) the symbol [C-in-a-circle]
  1206.     (preferred because it also suits the requirements of the Universal
  1207.     Copyright Convention), the word 'Copyright', or the abbreviation
  1208.     'Copr.', (2) a date--the year of first publication, and (3) the name
  1209.     of the copyright owner.  Most publishers also add the phrase 'All
  1210.     rights reserved' because it affords some protection in Central and
  1211.     South American countries ...."  Surprise:  "(C)" is legally not the
  1212.     same as the C-in-a-circle, so those of us who are ASCII-bound must
  1213.     use the word or the abbreviation.
  1214.  
  1215.     You can download a much more comprehensive treatment from the
  1216.     Internet.  Terry Carroll posts a six-part Copyright FAQ to
  1217.     misc.legal, news.answers and other groups.
  1218.  
  1219.  
  1220. (continued in part 4)
  1221. -- 
  1222. Stan Brown, Oak Road Systems                    brown@Ncoast.ORG
  1223.  
  1224. Can't find FAQ lists?  ftp to 'rtfm.mit.edu' and look in /pub/usenet
  1225. (or email me >>> with valid reply-to address <<< for instructions).
  1226.  
  1227.