home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / os / msdos / programm / 10653 < prev    next >
Encoding:
Text File  |  1992-11-15  |  41.3 KB  |  893 lines

  1. Xref: sparky comp.os.msdos.programmer:10653 news.answers:4002
  2. Newsgroups: comp.os.msdos.programmer,news.answers
  3. Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!ncoast!brown
  4. From: brown@NCoast.ORG (Stan Brown)
  5. Subject: comp.os.msdos.programmer FAQ part 3 of 4
  6. Expires: Wed, 30 Dec 1992 17:50:32 GMT
  7. Organization: Oak Road Systems, Cleveland Ohio USA
  8. Date: Sun, 15 Nov 1992 17:50:32 GMT
  9. Approved: news-answers-request@MIT.Edu
  10. Message-ID: <msdos-faq.921115.3@NCoast.ORG>
  11. Followup-To: comp.os.msdos.programmer
  12. References: <msdos-faq.921115.1@NCoast.ORG>
  13. Supersedes: <msdos-faq.921020.3@NCoast.ORG>
  14. Lines: 877
  15.  
  16. Archive-name: msdos-programmer-faq/part3
  17. Last-modified: 15 November 1922
  18.  
  19.  
  20. (continued from part 2)         (no warranty on the code or information)
  21.  
  22. If the posting date is more than six weeks in the past, see instructions
  23. in part 4 of this list for how to get an updated copy.
  24.  
  25.             Copyright (C) 1992  Stan Brown, Oak Road Systems
  26.  
  27.  
  28. section 4.  Disks and files
  29. ===========================
  30.  
  31. Q401. What drive was the PC booted from?
  32.  
  33.     Under DOS 4.0 or later, load 3305 hex into AX; do an INT 21.  DL is
  34.     returned with an integer indicating the boot drive (1=A:, etc.).
  35.  
  36. Q402. How can I boot from drive b:?
  37.  
  38.     Download PD1:<MSDOS.DSKUTL>BOOT_B.ZIP (shareware) from Simtel.  The
  39.     included documentation says it works by writing a new boot sector on
  40.     a disk in your a: drive that redirects the boot to your b: drive.
  41.  
  42. Q403. Which real and virtual disk drives are valid?
  43.  
  44.     Use INT 21 function 29 (parse filename).  Point DS:SI at a null-
  45.     terminated ASCII string that contains the drive letter and a colon,
  46.     point ES:DI at a 37-byte dummy FCB buffer, set AX to 2900h, and do
  47.     an INT 21.  On return, AL is FF if the drive is invalid, something
  48.     else if the drive is valid.  RAM disks and SUBSTed drives are
  49.     considered valid.
  50.  
  51.     Unfortunately, the b: drive is considered valid even on a single-
  52.     diskette system.  You can check that special case by interrogating
  53.     the BIOS equipment byte at 0040:0010.  Bits 7-6 contain the one less
  54.     than the number of diskette drives, so if those bits are zero you
  55.     know that b: is an invalid drive even though function 29 says it's
  56.     valid.
  57.  
  58.     Following is some code originally posted by Doug Dougherty, with my
  59.     fix for the b: special case, tested only in Borland C++ 2.0 (in
  60.     the small model):
  61.  
  62.         #include <dos.h>
  63.         void drvlist(void)  {
  64.             char *s = "A:", fcb_buff[37];
  65.             int valid;
  66.             for (   ;  *s<='Z';  (*s)++) {
  67.                 _SI = (unsigned) s;
  68.                 _DI = (unsigned) fcb_buff;
  69.                 _ES = _DS;
  70.                 _AX = 0x2900;
  71.                 geninterrupt(0x21);
  72.                 valid = _AL != 0xFF;
  73.                 if (*s == 'B'  &&  valid) {
  74.                     char far *equipbyte = (char far *)0x00400010UL;
  75.                     valid = (*equipbyte & (3 << 6)) != 0;
  76.                 }
  77.                 printf("Drive '%s' is %sa valid drive.\n",
  78.                         s, valid ? "" : "not ");
  79.             }
  80.         }
  81.  
  82. Q404. How can I make my single floppy drive both a: and b:?
  83.  
  84.     Under any DOS since DOS 2.0, you can put the command
  85.  
  86.         assign b=a
  87.  
  88.     into your AUTOEXEC.BAT file.  Then, when you type "DIR B:" you'll no
  89.     longer get the annoying prompt to insert diskette B (and the even
  90.     more annoying prompt to insert A the next time you type "DIR A:").
  91.  
  92.     You may be wondering why anybody would want to do this.  Suppose you
  93.     use two different machines, maybe one at home and one at work.  One
  94.     of them has only a 3.5" diskette drive; the other machine has two
  95.     drives, and b: is the 3.5" one.  You're bound to type "dir b:" on
  96.     the first one, and get the nuisance message
  97.  
  98.         Insert diskette for drive B: and press any key when ready.
  99.  
  100.     But if you assign drive b: to point to a:, you avoid this problem.
  101.  
  102.     Caution:  there are a few commands, such as DISKCOPY, that will not
  103.     work right on ASSIGNed or SUBSTed drives.  See the DOS manual for
  104.     the full list.  Before typing one of those commands, be sure to turn
  105.     off the mapping by typing "assign" without arguments.
  106.  
  107.     The DOS 5.0 manual says that ASSIGN is obsolete, and recommends the
  108.     equivalent form of SUBST: "subst b: a:\".  Unfortunately, if this
  109.     command is executed when a: doesn't hold a diskette, the command
  110.     fails.  ASSIGN doesn't have this problem, so I must advise you to
  111.     disregard that particular bit of advice in the DOS manual.
  112.  
  113. Q405. Why won't my C program open a file with a path?
  114.  
  115.     You've probably got something like the following code:
  116.  
  117.         char *filename = "c:\foo\bar\mumble.dat";
  118.         . . .  fopen(filename, "r");
  119.  
  120.     The problem is that \f is a form feed, \b is a backspace, and \m is
  121.     m.  Whenever you want a backslash in a string constant in C, you
  122.     must use two backslashes:
  123.  
  124.         char *filename = "c:\\foo\\bar\\mumble.dat";
  125.  
  126.     This is a feature of every C compiler, because Dennis Ritchie
  127.     designed C this way.  It's a problem only on MS-DOS systems, because
  128.     only DOS (and Atari ST/TT running TOS, I'm told) uses the backslash
  129.     in directory paths.  But even in DOS this backslash convention
  130.     applies _only_ to string constants in your source code.  For file
  131.     and keyboard input at run time, \ is just a normal character, so
  132.     users of your program would type in file specs at run time the same
  133.     way as in DOS commands, with single backslashes.
  134.  
  135.     Another possibility is to code all paths in source programs with /
  136.     rather than \ characters:
  137.  
  138.         char *filename = "c:/foo/bar/mumble.dat";
  139.  
  140.     Ralf Brown writes that "All versions of the DOS kernel accept either
  141.     forward or backslashes as directory separators.  I tend to use this
  142.     form more frequently than backslashes since it is easier to type and
  143.     read."  This applies to DOS function calls (and therefore to calls
  144.     to the file library of every programming language), but not to DOS
  145.     commands.
  146.  
  147. Q406. How can I redirect printer output to a file?
  148.  
  149.     My personal favorite utility for this purpose is PRN2FILE from {PC
  150.     Magazine}, available from Simtel as PD1:<MSDOS.PRINTER>PRN2FILE.ARC,
  151.     or from garbo as prn2file.zip in /pc/printer.  ({PC Magazine} has
  152.     given copies away as part of its utilities disks, so you may already
  153.     have a copy.)
  154.  
  155.     Check the PD1:<MSDOS.PRINTER> directory at Simtel, or /pc/printer
  156.     at garbo, for lots of other printer-redirection utilities.
  157.  
  158. Q407. What's the format of an .EXE header?
  159.  
  160.     See pages 349-350 of {PC Magazine}'s June 30, 1992 issue (xi:12) for
  161.     the old and new formats.  For a more detailed layout, look under INT
  162.     21 function 4B in Ralf Brown's interrupt list.
  163.  
  164. Q408. What's the format of an .OBJ file?
  165.  
  166.     I have seen a reference to Intel's document number #121748-001,
  167.     {8086 Relocatable Object Module Formats}, which covers the Intel
  168.     .OBJ format.  However, both Microsoft and Borland formats differ
  169.     from the base Intel format.  If you have specific references, either
  170.     to fpt-able documents or to published works (author, title, order
  171.     number or ISBN), please email them to brown@ncoast.org for inclusion
  172.     in the next edition of this list.
  173.  
  174. Q409. How can my program open more files than DOS's limit of 20?
  175.  
  176.     (This is a summary of an article Ralf Brown posted on 8 August 1992.)
  177.  
  178.     There are separate limits on files and file handles.  For example,
  179.     DOS opens three files but five file handles:  CON (stdin, stdout,
  180.     and stderr), AUX (stdaux), and PRN (stdprn).
  181.  
  182.     The limit in FILES= in CONFIG.SYS is a system-wide limit on files
  183.     opened by all programs (including the three that DOS opens and any
  184.     opened by TSRs); each process has a limit of 20 handles (including
  185.     the five that DOS opens).  Example:  CONFIG.SYS has FILES=40.  Then
  186.     program #1 will be able to open 15 file handles.  Assuming that the
  187.     program actually does open 15 handles pointing to 15 different
  188.     files, other programs could still open a total of 22 files (40-3-15
  189.     = 22), though no one program could open more than 15 file handles.
  190.  
  191.     If you're running DOS 3.3 or later, you can increase the per-process
  192.     limit of 20 file handles by a call to INT 21 function 67, Set Handle
  193.     Count.  Your program is still limited by the system-wide limit on
  194.     open files, so you may also need to increase the FILES= value in
  195.     your CONFIG.SYS file (and reboot).  The run-time library that you're
  196.     using may have a fixed-size table of file handles, so you may also
  197.     need to get source code for the module that contains the table,
  198.     increase the table size, and recompile it.
  199.  
  200. Q410. How can I read, create, change, or delete the volume label?
  201.  
  202.     You can't do it using the more modern DOS functions (3C, 41, 43),
  203.     but the older FCB-oriented directory functions will do the job.
  204.     Specifically, you need to allocate a 64-byte buffer and a 41-byte
  205.     extended FCB (file control block).  Call INT 21 AH=1A to find out
  206.     whether there is a volume label.  If there is, AL will return 0 and
  207.     you can change the label using DOS function 17 or delete it using
  208.     DOS function 13.  If there is no volume label, function 1A will
  209.     return FF and you can create a label by using function 16.
  210.  
  211.     The following MSC 7.0 code worked for me, where the parameter is 0
  212.     for the current disk, 1 for a:, 2 for b:, etc.  It doesn't matter
  213.     what your current directory is; these functions always search the
  214.     root directory for volume labels.  This code should work for DOS
  215.     2.0+, though I tested it only under DOS 5.0.  (I don't know what
  216.     happens with networked drives.)
  217.  
  218.     Important points to notice are that ? wildcards are allowed but *
  219.     are not; the volume label must be space filled not null-terminated.
  220.  
  221.     void vollabel(unsigned char drivenum) {
  222.         static unsigned char extfcb[41], dta[64], status, *newlabel;
  223.         int chars_got = 0;
  224.         #define DOS(buff,func) __asm { __asm mov dx,offset buff \
  225.             __asm mov ax,seg buff  __asm push ds  __asm mov ds,ax \
  226.             __asm mov ah,func  __asm int 21h  __asm pop ds \
  227.             __asm mov status,al }
  228.         #define getlabel(buff,prompt) newlabel = buff;  \
  229.             memset(newlabel,' ',11);  printf(prompt);   \
  230.             scanf("%11[^\n]%n", newlabel, &chars_got);  \
  231.             if (chars_got < 11) newlabel[chars_got] = ' ';
  232.  
  233.         // Set up the 64-byte transfer area used by function 1A.
  234.         DOS(dta, 1Ah)
  235.         // Set up an extended FCB and search for the volume label.
  236.         memset(extfcb, 0, sizeof extfcb);
  237.         extfcb[0] = 0xFF;             // denotes extended FCB
  238.         extfcb[6] = 8;                // volume-label attribute bit
  239.         extfcb[7] = drivenum;         // 1=A, 2=B, etc.; 0=current drive
  240.         memset(&extfcb[8], '?', 11);  // wildcard *.*
  241.         DOS(extfcb,11h)
  242.         if (status == 0) {            // DTA contains volume label's FCB
  243.             printf("volume label is %11.11s\n", &dta[8]);
  244.             getlabel(&dta[0x18], "new label (\"delete\" to delete): ");
  245.             if (chars_got == 0)
  246.                 printf("label not changed\n");
  247.             else if (strncmp(newlabel,"delete     ",11) == 0) {
  248.                 DOS(dta,13h)
  249.                 printf(status ? "label failed\n" : "label deleted\n");
  250.             }
  251.             else {                    // user wants to change label
  252.                 DOS(dta,17h)
  253.                 printf(status ? "label failed\n" : "label changed\n");
  254.             }
  255.         }
  256.         else {                        // no volume label was found
  257.             printf("disk has no volume label.\n");
  258.             getlabel(&extfcb[8], "new label (<Enter> for none): ");
  259.             if (chars_got > 0) {
  260.                 DOS(extfcb,16h)
  261.                 printf(status ? "label failed\n" : "label created\n");
  262.             }
  263.         }
  264.     }   // end function vollabel
  265.  
  266. Q411. How can I get the disk serial number?
  267.  
  268.     Use INT 21.  AX=6900 gets the serial number; AX=6901 sets it.  See
  269.     Ralf Brown's interrupt list, or page 496 of the July 1992 {PC
  270.     Magazine}, for details.
  271.  
  272.  
  273. section 5. Serial ports (COM ports)
  274. ===================================
  275.  
  276. Q501. How do I set my machine up to use COM3 and COM4?
  277.  
  278.     Unless your machine is fairly old, it's probably already set up.
  279.     After installing the board that contains the extra COM port(s),
  280.     check the I/O addresses in word 0040:0004 or 0040:0006.  (In DEBUG,
  281.     type "D 40:4 L4" and remember that every word is displayed low
  282.     byte first, so if you see "03 56" the word is 5603.)  If those
  283.     addresses are nonzero, your PC is ready to use the ports and you
  284.     don't need the rest of this answer.
  285.  
  286.     If the I/O address words in the 0040 segment are zero after you've
  287.     installed the I/O board, you need some code to store these values
  288.     into the BIOS data segment:
  289.  
  290.         0040:0004  word  I/O address of COM3
  291.         0040:0006  word  I/O address of COM4
  292.         0040:0011  byte (bits 3-1): number of serial ports installed
  293.  
  294.     The documentation with your I/O board should tell you the port
  295.     addresses.  When you know the proper port addresses, you can add
  296.     code to your program to store them and the number of serial ports
  297.     into the BIOS data area before you open communications.  Or you can
  298.     use DEBUG to create a little program to include in your AUTOEXEC.BAT
  299.     file, using this script:
  300.  
  301.             n SET_ADDR.COM      <--- or a different name ending in .COM
  302.             a 100
  303.             mov  AX,0040
  304.             mov  DS,AX
  305.             mov  wo [0004],aaaa <--- replace aaaa with COM3 address or 0
  306.             mov  wo [0006],ffff <--- replace ffff with COM4 address or 0
  307.             and  by [0011],f1
  308.             or   by [0011],8    <--- use number of serial ports times 2
  309.             mov  AH,0
  310.             int  21
  311.                                 <--- this line must be blank
  312.             rCX
  313.             1f
  314.             rBX
  315.             0
  316.             w
  317.             q
  318.  
  319. Q502. How do I find the I/O address of a COM port?
  320.  
  321.     Look in the four words beginning at 0040:0000 for COM1 through COM4.
  322.     (The DEBUG command "D 40:0 L8" will do this.  Remember that words
  323.     are stored and displayed low byte first, so a word value of 03F8
  324.     will be displayed as F8 03.)  If the value is zero, that COM port is
  325.     not installed (or you've got an old BIOS; see the preceding Q).  If
  326.     the value is nonzero, it is the I/O address of the transmit/receive
  327.     register for the COM port.  Each COM port occupies eight consecutive
  328.     I/O addresses (though only seven are used by many chips).
  329.  
  330.     Here's some C code to find the I/O address:
  331.  
  332.         unsigned ptSel(unsigned comport) {
  333.             unsigned io_addr;
  334.             if (comport >= 1  &&  comport <= 4) {
  335.                 unsigned far *com_addr = (unsigned far *)0x00400000UL;
  336.                 io_addr = com_addr[comport-1];
  337.             }
  338.             else
  339.                 io_addr = 0;
  340.             return io_addr;
  341.         }
  342.  
  343. Q503. But aren't the COM ports always at I/O addresses 3F8, 2F8, 3E8,
  344.       and 2E8?
  345.  
  346.     The first two are usually right (though not always); the last two
  347.     are different on many machines.
  348.  
  349. Q504. How do I configure a COM port and use it to transmit data?
  350.  
  351.     After hearing several recommendations, I looked at Joe Campbell's {C
  352.     Programmer's Guide to Serial Communications}, ISBN 0-672-22584-0,
  353.     and agree that it is excellent.  He gives complete details on how
  354.     serial ports work, along with complete programs for doing polled or
  355.     interrupt-driver I/O.  The book is quite thick, and none of it looks
  356.     like filler.
  357.  
  358.     If Campbell's book is overkill for you, you'll find a good short
  359.     description of serial I/O in {DOS 5: A Developer's Guide}, ISBN
  360.     1-55851-177-6, by Al Williams.
  361.  
  362.     You may also want to look at an extended example in Borland's
  363.     TechFax TI445, part of PD1:<MSDOS.TURBO-C> at Simtel.  Though
  364.     written by Borland, much of it is applicable to other forms of C,
  365.     and it should give you ideas for other programming languages.
  366.  
  367. section 6. Other hardware questions and problems
  368. ================================================
  369.  
  370. Q601. Which 80x86 CPU is running my program?
  371.  
  372.     According to an article posted by Michael Davidson, Intel's approved
  373.     code for distinguishing among 8086, 80286, 80386, and 80486 and for
  374.     detecting the presence of an 80287 or 80387 is published in the
  375.     Intel's 486SX processor manual (order number 240950-001).  You can
  376.     download David Kirschbaum's improved version of this from Simtel as
  377.     PD1:<MSDOS.SYSUTL>CPUID593.ZIP.
  378.  
  379.     According to an article posted by its author, WCPU041.ZIP knows the
  380.     differences between DX and SX varieties of 386 and 486 chips, and
  381.     can also detect a math coprocessor.  It's in PD1:<MSDOS.SYSUTL> at
  382.     Simtel.
  383.  
  384. Q602. How can a C program send control codes to my printer?
  385.  
  386.     If you just fprintf(stdprn, ...), C will translate some of your
  387.     control codes.  The way around this is to reopen the printer in
  388.     binary mode:
  389.  
  390.         prn = fopen("PRN", "wb");
  391.  
  392.     You must use a different file handle because stdprn isn't an lvalue.
  393.     By the way, PRN or LPT1 must not be followed by a colon in DOS 5.0.
  394.  
  395.     There's one special case, Ctrl-Z (ASCII 26), the DOS end-of-file
  396.     character.  If you try to send an ASCII 26 to your printer, DOS
  397.     simply ignores it.  To get around this, you need to reset the
  398.     printer from "cooked" to "raw" mode.  Microsoft C users must use int
  399.     21 function 44, "get/set device information".  Turbo C and Borland
  400.     C++ users can use ioctl to accomplish the same thing:
  401.  
  402.         ioctl(fileno(prn), 1, ioctl(fileno(prn),0) & 0xFF | 0x20, 0);
  403.  
  404.     An alternative approach is simply to write the printer output into a
  405.     disk file, then copy the file to the printer with the /B switch.
  406.  
  407.     A third approach is to bypass DOS functions entirely and use the
  408.     BIOS printer functions at INT 17.  If you also fprintf(stdprn,...)
  409.     in the same program, you'll need to use fflush( ) to synchronize
  410.     fprintf( )'s buffered output with the BIOS's unbuffered.
  411.  
  412.     By the way, if you've opened the printer in binary mode from a C
  413.     program, remember that outgoing \n won't be translated to carriage
  414.     return/line feed.  Depending on your printer, you may need to send
  415.     explicit \n\r sequences.
  416.  
  417. Q603. How can I redirect printer output to a file?
  418.  
  419.     Please see section 4, "Disks and files", for the answer.
  420.  
  421. Q604. Which video adapter is installed?
  422.  
  423.     The technique below should work if your BIOS is not too old.  It
  424.     uses three functions from INT 10, the BIOS video interrupt.  (If
  425.     you're using a Borland language, you may not have to do this the
  426.     hard way.  Look for a function called DetectGraph or something
  427.     similar.)
  428.  
  429.     Set AH=12h, AL=0, BL=32h; INT 10h.  If AL is 12h, you have a VGA.
  430.     If not, set AH=12h, BL=10h; INT 10h.  If BL is 0,1,2,3, you have an
  431.     EGA with 64,128,192,256K memory.  If not, set AH=0Fh; INT 10h.  If
  432.     AL is 7, you have an MDA (original monochrome adapter) or Hercules;
  433.     if not, you have a CGA.
  434.  
  435.     I've tested this for my VGA and got the right answer; but I can't
  436.     test it for the other equipment types.  Please let me know by email
  437.     at brown@ncoast.org if your results vary.
  438.  
  439. Q605. How do I switch to 43- or 50-line mode?
  440.  
  441.     Download PD1:<MSDOS.SCREEN>VIDMODE.ZIP from Simtel or one of the
  442.     mirror sites.  It contains .COM utilities and .ASM source code.
  443.  
  444. Q606. How can I find the Microsoft mouse position and button status?
  445.  
  446.     Use INT 33 function 3, described in Ralf Brown's interrupt list.
  447.  
  448.     The Windows manual says that the Logitech mouse is compatible with
  449.     the Microsoft one, so I assume the interrupt will work the same.
  450.  
  451.     Also, see the directory PD1:<MSDOS.MOUSE> at Simtel.
  452.  
  453. Q607. How can I access a specific address in the PC's memory?
  454.  
  455.     First check the library that came with your compiler.  Many vendors
  456.     have some variant of peek and poke functions; in Turbo Pascal use
  457.     the pseudo-arrays Mem, MemW, and MemL.  As an alternative, you can
  458.     construct a far pointer:  use Ptr in Turbo Pascal, MK_FP in the
  459.     Turbo C family, and FP_OFF and FP_SEG in Microsoft C.
  460.  
  461.     Caution:  Turbo C and Turbo C++ also have FP_OFF and FP_SEG macros,
  462.     but they can't be used to construct a pointer.  In Borland C++ those
  463.     macros work the same as in Microsoft C, but MK_FP is easier to use.
  464.  
  465.     By the way, it's not useful to talk about "portable" ways to do
  466.     this.  Any operation that is tied to a specific memory address is
  467.     not likely to work on another kind of machine.
  468.  
  469. Q608. How can I read or write my PC's CMOS memory?
  470.  
  471.     There are a great many public-domain utilities that do this.  These
  472.     were available for download from Simtel as of 31 March 1992:
  473.  
  474.     PD1:<MSDOS.AT>
  475.     CMOS14.ZIP     5965  920817  Saves/restores CMOS to/from file
  476.     CMOSER11.ZIP  28323  910721  386/286 enhanced CMOS setup program
  477.     CMOSRAM.ZIP   76096  920214  Save AT/386/486 CMOS data to file and restore
  478.     ROM2.ARC      20497  900131  Save AT and 386 CMOS data to file and restore
  479.     SETUP21.ARC   24888  880613  Setup program which modifies CMOS RAM
  480.     VIEWCMOS.ARC  15374  900225  Display contents of AT CMOS RAM, w/C source
  481.  
  482.     At garbo, /pc/ts/tsutle17.zip contains a CMOS program to check and
  483.     display CMOS memory, but not to write to it.
  484.  
  485.     I have heard good reports of CMOS299.ZIP, available in the pc.dir
  486.     directory of cantva.canterbury.ac.nz [132.181.30.3].
  487.  
  488.     Of the above, my only experience is with CMOSRAM, which seems to
  489.     work fine.  It contains an excellent (and witty) .DOC file that
  490.     explains the hardware involved and gives specific recommendations
  491.     for preventing disaster or recovering from it.  It's $5 shareware.
  492.  
  493.     Robert Jourdain's {Programmer's Problem Solver for the IBM PC, XT,
  494.     and AT} has code for accessing the CMOS RAM, according to an article
  495.     posted in this newsgroup.
  496.  
  497. Q609. How can I access memory beyond 640K?
  498.  
  499.     I'm outside my expertise on this one, but in November 1992 Jamshid
  500.     Afshar (jamshid@emx.utexas.edu) kindly supplied the following:
  501.  
  502.     ...........................(begin quote)............................
  503.     1. Use XMS memory (don't bother with EMS).  There are some libraries
  504.     available at Simtel to access XMS.  The disadvantage is that you
  505.     don't allocate the memory as you would with malloc() (or `new' in
  506.     C++).  I believe it also requires that you lock this memory when in
  507.     use.  This means your code is not easily ported to other (and
  508.     future) operating systems and that your code is more convoluted than
  509.     it would be under a "real" os.  The advantage is that the library
  510.     works with compilers since Turbo C 2.0 (I think) and that your
  511.     program will run on even 286s.
  512.  
  513.     2. Program under MS Windows.  MS Windows functions as a DOS extender
  514.     (see #3).  Borland/Turbo C++ 3.0 includes EasyWin [and Microsoft
  515.     C/C++ 7.0 has QuickWin --ed.] which is a library that automatically
  516.     lets you compile your current code using C/C++ standard input or
  517.     <conio.h>'s gotoxy() into a MS Windows program so your code can
  518.     immediately allocate many MBs of memory (Windows enhanced mode even
  519.     does virtual memory).  The disadvantage of MS Windows is that any
  520.     one object (e.g., a single malloc( )) is still restricted to 64K
  521.     (unless you want to mess with huge pointers in Windows).
  522.  
  523.     3. Use a DOS extender.  This is definitely the best solution from
  524.     the programmer's standpoint.  You just allocate as much memory as
  525.     you need using malloc( ) or `new' and you don't have to worry about
  526.     any 64K limits.  It doesn't require source code changes and unlike
  527.     option #1 your code is portable and not obsolete in a few months.
  528.     Your options for this solution are:
  529.  
  530.     - Buy PharLap's DOS extender (286 or 386 version) that works with
  531.       BC++ 3.0+ (just requires a relink).  Note, the BC++ 3.1 upgrade
  532.       came with PharLap "lite".
  533.  
  534.     - Get the GNU (free,copylefted) gcc 2.1 compiler/extender that was
  535.       ported to MS-DOS and runs on 386 machines (supports C and C++).
  536.       FTP to barnacle.erc.clarkson.edu and get pub/msdos/djgpp/readme.
  537.  
  538.     - Wait for Borland's DOS extender package (in BC++ 4.0) that is
  539.       supposed to be released at the end of this year.  I believe MS is
  540.       also doing the same.  Zortech 3.0 comes with a DOS extender, but I
  541.       wouldn't recommend the product.
  542.  
  543.     4. This option doesn't really count since it's not a solution in
  544.     DOS, but you could switch to a full 32-bit operating system like
  545.     OS/2 2.0 or UNIX (or Win32/NT when it comes out).  Borland is
  546.     putting its OS/2 compiler into beta now.  OS/2 is doing well (and it
  547.     runs DOS or Windows programs).  BC++ for OS/2 should also be a
  548.     success and there should be a good upgrade path from current Borland
  549.     compilers.
  550.     ............................(end quote).............................
  551.  
  552.  
  553. section 7. Other software questions and problems
  554. ================================================
  555.  
  556. Q701. How can a program reboot my PC?
  557.  
  558.     You can generate a "cold" boot or a "warm" boot.  A cold boot is
  559.     the same as turning the power off and on; a warm boot is the same as
  560.     Ctrl-Alt-Del and skips the power-on self test.
  561.  
  562.     For a warm boot, store the hex value 1234 in the word at 0040:0072.
  563.     For a cold boot, store 0 in that word.  Then, if you want to live
  564.     dangerously, jump to address FFFF:0000.  Here's C code to do it:
  565.  
  566.         /* WARNING:  data loss possible */
  567.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  568.             void (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  569.             unsigned far* type = (unsigned far*)0x00400072UL;
  570.             *type = (want_warm ? 0x1234 : 0);
  571.             (*boot)( );
  572.         }
  573.  
  574.     What's wrong with that method?  It will boot right away, without
  575.     closing files, flushing disk caches, etc.  If you boot without
  576.     flushing a write-behind disk cache (if one is running), you could
  577.     lose data or even trash your hard drive.
  578.  
  579.     There are two methods of signaling the cache to flush its buffers:
  580.     (1) simulate a keyboard Ctrl-Alt-Del in the keystroke translation
  581.     function of the BIOS (INT 15 function 4F), and (2) issue a disk
  582.     reset (DOS function 0D).  Most disk-cache programs hook one or both
  583.     of those interrupts, so if you use both methods you'll probably be
  584.     safe.
  585.  
  586.     When user code simulates a Ctrl-Alt-Del, one or more of the programs
  587.     that have hooked INT 15 function 4F can ask that the key be ignored by
  588.     clearing the carry flag.  For example, HyperDisk does this when it
  589.     has started but not finished a cache flush.  So if the carry flag
  590.     comes back cleared, the boot code has to wait a couple of cluck
  591.     ticks and then try again.  (None of this matters on older machines
  592.     whose BIOS can't support 101- or 102-key keyboards; see "What is the
  593.     SysRq key for?" in section 3, "Keyboard".)
  594.  
  595.     Here's C code that tries to signal the disk cache (if any) to flush:
  596.  
  597.         #include <dos.h>
  598.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  599.             union REGS reg;
  600.             void    (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  601.             unsigned far* boottype    =     (unsigned far*)0x00400072UL;
  602.             char     far* shiftstate  =         (char far*)0x00400017UL;
  603.             unsigned      ticks;
  604.             int           time_to_waste;
  605.             /* Simulate reception of Ctrl-Alt-Del: */
  606.             for (;;) {
  607.                 *shiftstate |= 0x0C;    /* turn on Ctrl & Alt */
  608.                 reg.x.ax = 0x4F53;      /* 0x53 = Del's scan code */
  609.                 reg.x.cflag = 1;        /* sentinel for ignoring key */
  610.                 int86(0x15, ®, ®);
  611.                 /* If carry flag is still set, we've finished. */
  612.                 if (reg.x.cflag)
  613.                     break;
  614.                 /* Else waste some time before trying again: */
  615.                 reg.h.ah = 0;
  616.                 int86(0x1A, ®, ®);/* system time into CX:DX */
  617.                 ticks = reg.x.dx;
  618.                 for (time_to_waste = 3;  time_to_waste > 0;  ) {
  619.                     reg.h.ah = 0;
  620.                     int86(0x1A, ®, ®);
  621.                     if (ticks != reg.x.dx)
  622.                         ticks = reg.x.dx , --time_to_waste;
  623.                 }
  624.             }
  625.             /* Issue a DOS disk reset request: */
  626.             reg.h.ah = 0x0D;
  627.             int86(0x21, ®, ®);
  628.             /* Set boot type and boot: */
  629.             *boottype = (want_warm ? 0x1234 : 0);
  630.             (*boot)( );
  631.         }
  632.  
  633. Q702. How can I time events with finer resolution than the system
  634.       clock's 55 ms (about 18 ticks a second)?
  635.  
  636.     The following files, among others, can be downloaded from Simtel:
  637.  
  638.     PD1:<MSDOS.AT>
  639.     ATIM.ARC       5946  881126  Precision program timing for AT
  640.  
  641.     PD1:<MSDOS.C>
  642.     MILLISEC.ZIP  37734  911205  MSC/asm src for millisecond res timing
  643.     MSCHRT3.ZIP   53708  910605  High-res timer toolbox for MSC 5.1
  644.     MSEC_12.ZIP    8484  920320  High-def millisec timer v1.2 (C,ASM)
  645.     ZTIMER11.ZIP  77625  920428  Microsecond timer for C, C++, ASM
  646.  
  647.     PD1:<MSDOS.TURBO-C>
  648.     TCHRT3.ZIP    53436  910606  High-res timer toolbox for Turbo C 2.0
  649.     TCTIMER.ARC   20087  891030  High-res timing of events for Turbo C
  650.  
  651.     PD1:<MSDOS.TURBOPAS>
  652.     BONUS507.ARC 150435  900205  [Turbo Pascal source: high-res timing]
  653.  
  654.     Pascal users can download source code in /pc/turbopas/bonus507.zip
  655.     at garbo.
  656.  
  657. Q703. How can I find the error level of the previous program?
  658.  
  659.     First, which previous program are you talking about?  If your
  660.     current program ran another one, when the child program ends its
  661.     error level is available to the program that spawned it.  Most
  662.     high-level languages provide a way to do this; for instance, in
  663.     Turbo Pascal it's Lo(DosExitCode) and the high byte gives the way in
  664.     which the child terminated.  In Microsoft C, the exit code of a
  665.     synchronous child process is the return value of the spawn-type
  666.     function that creates the process.
  667.  
  668.     If your language doesn't have a function to return the error code
  669.     of a child process, you can use INT 21 function 4D (get return
  670.     code).  By the way, this will tell you the child's exit code and the
  671.     manner of its ending (normal, Ctrl-C, critical error, or TSR).
  672.  
  673.     It's much trickier if the current program wants to get the error
  674.     level of the program that ran and finished before this one started.
  675.     G.A.Theall has published source and compiled code to do this; you
  676.     can download it from Simtel as PD1:<MSDOS.BATUTL>ERRLVL12.ZIP.  (The
  677.     code uses undocumented features in DOS 3.3 through 5.0.  Theall says
  678.     in the .DOC file that the values returned under 4DOS or other
  679.     replacements won't be right.)
  680.  
  681. Q704. How can a program set DOS environment variables?
  682.  
  683.     Program functions that read or write "the environment" typically
  684.     access only the program's copy of the environment.  What this Q
  685.     really wants to do is to modify the active environment, the one that
  686.     is affected by SET commands in batch files or at the DOS prompt.
  687.     You need to do some programming to find the active environment, and
  688.     that programming varies for different versions of DOS.
  689.  
  690.     A fairly well-written article in {PC Magazine} volume 8 number 20
  691.     (1989 Nov 28), pages 309-314, explains how to find the active
  692.     environment, and includes Pascal source code.  The article hints at
  693.     how to change the environment, and suggests creating paths longer
  694.     than 128 characters as one application.
  695.  
  696.     In searching Simtel for source code, I found many possibilities.  I
  697.     liked PD1:<MSDOS.SYSUTL>RBSETNV1.ZIP of the ones I looked at (not
  698.     all of them).  It includes some utilities to manipulate the environ-
  699.     ment, with source code in C.
  700.  
  701.     You can also use a call to INT 2E, Pass Command to Interpreter for
  702.     Execution; see Ralf Brown's interrupt list for details and cautions.
  703.  
  704. Q705. How can I change the switch character to - from /?
  705.  
  706.     Under DOS 5.0, you can't -- not completely, anyway.  INT 21 function
  707.     3700, get switch character, always returns a '/' (hex 2F) -- and the
  708.     DOS commands don't even call that function, but hard code '/' as the
  709.     switch character.
  710.  
  711.     Some history:  DOS used to let you change the switch character by
  712.     using SWITCHAR= in CONFIG.SYS or by calling DOS function 3701.  DOS
  713.     commands and other programs called DOS function 3700 to find out the
  714.     switch character.  If you changed the switch character to '-' (the
  715.     usual choice), you could then type "dir c:/c700 -p" rather than "dir
  716.     c:\c700 /p".  Under DOS 4.0, the DOS commands ignored the switch
  717.     character but functions 3700 and 3701 still worked and could be used
  718.     by other programs.  Under DOS 5.0, even those functions no longer
  719.     work, though all DOS functions still accept '/' or '\' in file
  720.     specs.
  721.  
  722.     You can reactivate the functions to get and set switchar by using
  723.     programs like SLASH.ZIP or the sample TSR called SWITCHAR in
  724.     AMISL091.ZIP (see "How can I write a TSR?", below.)  DOS commands
  725.     will still use the slash, but non-DOS programs that call DOS func-
  726.     tion 3700 will use your desired switch character.  (DOS replacements
  727.     like 4DOS may honor the switch character for internal commands.)
  728.  
  729.     Some readers may wonder why this is even an issue.  Making '-' the
  730.     switch character frees up the front slash to separate names in the
  731.     path part of a file spec.  This is easier for the ten-fingered to
  732.     type, and it's one less difference to remember for commuters between
  733.     DOS and Unix.  The switch character is the only issue, since all the
  734.     INT 21 functions accept '/' or '\' to separate directory names.
  735.  
  736. Q706. Why does my interrupt function behave strangely?
  737.  
  738.     Interrupt service routines can be tricky, because you have to do
  739.     some things differently from "normal" programs.  If you make a
  740.     mistake, debugging is a pain because the symptoms may not point at
  741.     what's wrong.  Your machine may lock up or behave erratically, or
  742.     just about anything else can happen.  Here are some things to look
  743.     for.  (See the next Q for general help before you have a problem.)
  744.  
  745.     First, did you fail to set up the registers at the start of your
  746.     routine?  When your routine begins executing, you can count on
  747.     having CS point to your code segment and SS:SP point to some valid
  748.     stack (of unknown length), and that's it.  In particular, an
  749.     interrupt service routine must set DS to DGROUP before accessing any
  750.     data in its data segments.  (If you're writing in a high-level
  751.     language, the compiler may generate this code for you automatically;
  752.     check your compiler manual.  For instance, in Borland and Microsoft
  753.     C, give your function the "interrupt" attribute.)
  754.  
  755.     Did you remember to turn off stack checking when compiling your
  756.     interrupt server and any functions it calls?  The stack during the
  757.     interrupt is not where the stack-checking code expects it to be.
  758.     (Caution:  Some third-party libraries have stack checking compiled
  759.     in, so you can't call them from your interrupt service routine.)
  760.  
  761.     Next, are you calling any DOS functions (INT 21, 25, or 26) in your
  762.     routine?  DOS is not re-entrant.  This means that if your interrupt
  763.     happens to be triggered while the CPU is executing a DOS function,
  764.     calling another DOS function will wreak havoc.  (Some DOS functions
  765.     are fully re-entrant, as noted in Ralf Brown's interrupt list.
  766.     Also, your program can test, in a way too complicated to present
  767.     here, when it's safe to call non-re-entrant DOS functions.  See INT
  768.     28 and functions 34, 5D06, 5D0B of INT 21; and consult {Undocumented
  769.     DOS} by Andrew Schulman.  Your program must read both the "InDOS
  770.     flag" and the "critical error flag".)
  771.  
  772.     Is a function in your language library causing trouble?  Does it
  773.     depend on some initializations done at program startup that is no
  774.     longer available when the interrupt executes?  Does it call DOS (see
  775.     preceding paragraph)?  For example, in both Borland and Microsoft C
  776.     the memory-allocation functions (malloc, etc..) and standard I/O
  777.     functions (scanf, printf) call DOS functions and also depend on
  778.     setups that they can't get at from inside an interrupt.  Many other
  779.     library functions have the same problem, so you can't use them
  780.     inside an interrupt function without special precautions.
  781.  
  782.     Is your routine simply taking too long?  This can be a problem if
  783.     you're hooking on to the timer interrupt, INT 1C or INT 8.  Since
  784.     that interrupt expects to be called 18.2 times a second, your
  785.     routine -- plus any others hooked to the same interrupts -- must
  786.     execute in less than 55 ms.  If they use even a substantial fraction
  787.     of that time, you'll see significant slowdowns of your foreground
  788.     program.  For a good writeup, download INTSHARE (from ni.funet.fi
  789.     in pub/msdos/simtel20/info or from Simtel in PD1:<MSDOS.INFO>).
  790.  
  791.     Did you forget to restore all registers at the end of your routine?
  792.  
  793.     Did you chain improperly to the original interrupt?  You need to
  794.     restore the stack to the way it was upon entry to your routine, then
  795.     do a far jump (not call) to the original interrupt service routine.
  796.     (The process is a little different in high-level languages.)
  797.  
  798. Q707. How can I write a TSR (terminate-stay-resident) utility?
  799.  
  800.     Several books can help you with this.
  801.  
  802.     - Ray Duncan's {Advanced MS-DOS}, ISBN 1-55615-157-8, gives a brief
  803.       checklist intended for experienced programmers.  The ISBN is for
  804.       the second edition, through DOS 4; but check to see whether the
  805.       DOS 5 version is available yet.
  806.  
  807.     - {DOS 5:  A Developer's Guide} by Al Williams, ISBN 1-55851-177-6,
  808.       goes into a little more detail, 90 pages worth!
  809.  
  810.     - Pascal programmers might look at {The Ultimate DOS Programmer's
  811.       Manual} by John Mueller and Wallace Wang, ISBN 0-8306-3534-3, for
  812.       an extended example in mixed Pascal and assembler.
  813.  
  814.     - For a pure assembler treatment, check Steven Holzner's {Advanced
  815.       Assembly Language}, ISBN 0-13-663014-6.  He has a book with the
  816.       same title out from Brady Press, but it's about half as long as
  817.       this one.
  818.  
  819.     - For C programmers, there's a chapter in Herbert Schildt's {The Art
  820.       of C:  Elegant Programming Solutions}.  I haven't seen the book,
  821.       but a posted article recommended it.
  822.  
  823.     At Simtel, check PD1:<MSDOS.ASMUTL>AMISL091.ZIP, which contains Ralf
  824.     Brown's assembly-language implementation of the Alternate Multiplex
  825.     Interrupt Specification, with utilities in C.  The spec itself is
  826.     PD1:<MSDOS.INFO>ALTMPX35.ZIP.  Both are also available at CS.CMU.EDU
  827.     [128.2.222.173] in /afs/cs/user/ralf/pub (change directory with a
  828.     single command and use lower-case filenames).
  829.  
  830.     You might want to download PD1:<MSDOS.ASMUTL>TEMPLATE.ZIP from
  831.     Simtel.  It's Douglas Boling's MASM template for a TSR.
  832.  
  833.     Finally, there are commercial products, of which TesSeRact (for
  834.     C-language TSRs) is one of the best known.
  835.  
  836. Q708. How can I write a device driver?
  837.  
  838.     Many books answer this in detail.  Among them are {Advanced MS-DOS}
  839.     and {DOS 5: A Developer's Guide}, cited in the preceding Q.
  840.     Michael Tischer's {PC System Programming}, ISBN 1-55755-036-0, has
  841.     an extensive treatment, as does Dettman and Kyle's {DOS Programmer's
  842.     Reference: 2d Edition}, ISBN 0-88022-458-4.  For a really in-depth
  843.     treatment, look for a specialized book like Robert Lai's {Writing
  844.     MS-DOS Device Drivers}, ISBN 0-201-13185-4.
  845.  
  846. Q709. What can I use to manage versions of software?
  847.  
  848.     In PD1:<MSDOS.PGMUTL> at Simtel you'll find RCS55.ZIP.  I haven't
  849.     used it myself, but I understand this is a port of the Unix RCS
  850.     utility, but is limited to one-character extensions on filenames (no
  851.     .CPP).  A correspondent wrote that he has an RCS port to DOS that is
  852.     not limited to one-character extensions, but he doesn't remember
  853.     where he got it.  Anyone have a verified archive site?
  854.  
  855. Q710. What's this "null pointer assignment" after my C program executes?
  856.  
  857.     Somewhere in your program, you assigned a value _through_ a pointer
  858.     without first assigning a value _to_ the pointer.  (This might have
  859.     been something like a strcpy or memcpy with a pointer as its first
  860.     argument, not necessarily an actual assignment statement.)  Your
  861.     program may look like it ran correctly, but if you get this message
  862.     you can be certain that there's a bug somewhere.
  863.  
  864.     Microsoft and Borland C, as part of their exit code (after a return
  865.     from your main function), check whether the location 0000 in your
  866.     data segment contains a different value from what you started with;
  867.     if so, they infer that you must have used an uninitialized pointer.
  868.  
  869.     To track down the problem, you can put exit( ) statements at various
  870.     spots in the program and narrow down where the uninitialized pointer
  871.     is being used by seeing which added exit( ) makes the null-pointer
  872.     message disappear.  Or, in the debugger, set a watch at location
  873.     0000 in your data segment, assuming you're in small or medium model.
  874.     (If data pointers are 32 bits, as in the compact and large models, a
  875.     null pointer will overwrite the interrupt vectors at 0000:0000 and
  876.     probably lock up your machine.)
  877.  
  878.     Under MSC/C++ 7.0, you can declare the undocumented library function
  879.  
  880.         extern _cdecl _nullcheck(void);
  881.  
  882.     and then sprinkle calls to _nullcheck( ) through your program at
  883.     regular intervals.
  884.  
  885.     Borland's TechFax document #TI726 discusses the null pointer
  886.     assignment from a Borland point of view.  Download file BCHELP10.ZIP
  887.     from PD1:<MSDOS.TURBO-C> at Simtel.
  888.  
  889. (continued in part 4)
  890. -- 
  891. Stan Brown, Oak Road Systems                      brown@Ncoast.ORG
  892. Cleveland, Ohio, USA
  893.