home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / msdos-programmer-faq / part2 < prev    next >
Encoding:
Internet Message Format  |  2004-05-09  |  36.1 KB

  1. Path: senator-bedfellow.mit.edu!dreaderd!not-for-mail
  2. Message-ID: <msdos-programmer-faq/part2_1084010824@rtfm.mit.edu>
  3. Supersedes: <msdos-programmer-faq/part2_1081511793@rtfm.mit.edu>
  4. Expires: 11 Jun 2004 10:07:04 GMT
  5. References: <msdos-programmer-faq/part1_1084010824@rtfm.mit.edu>
  6. X-Last-Updated: 2003/08/14
  7. From: jeffrey@carlyle.org (Jeffrey Carlyle)
  8. Sender: jeffrey@carlyle.org (Jeffrey Carlyle)
  9. Reply-To: jeffrey@carlyle.org (Jeffrey Carlyle)
  10. Approved: news-answers-request@MIT.edu
  11. Organization: JeffC.org
  12. Subject: comp.os.msdos.programmer FAQ part 2/5
  13. Newsgroups: comp.os.msdos.programmer,alt.msdos.programmer,comp.answers,alt.answers,news.answers
  14. Followup-To: poster
  15. Summary: Frequently asked questions from DOS programmers with tested answers.
  16. Originator: faqserv@penguin-lust.MIT.EDU
  17. Date: 08 May 2004 10:08:02 GMT
  18. Lines: 831
  19. NNTP-Posting-Host: penguin-lust.mit.edu
  20. X-Trace: 1084010882 senator-bedfellow.mit.edu 580 18.181.0.29
  21. Xref: senator-bedfellow.mit.edu comp.os.msdos.programmer:127443 alt.msdos.programmer:54821 comp.answers:57099 alt.answers:72786 news.answers:270990
  22.  
  23. Archive-name: msdos-programmer-faq/part2
  24. Comp-os-msdos-programmer-archive-name: dos-faq-pt2.txt
  25. Posting-frequency: 28 days
  26. Last-modified: 14 Aug 2003
  27.  
  28. comp.os.msdos.programmer FAQ Version 2003.08.14
  29.  
  30. This is the Frequently Asked Questions list for the newsgroup
  31. comp.os.msdos.programmer.
  32.  
  33. COPYRIGHT
  34.  
  35. Copyright 2003 by Jeffrey Carlyle. All rights reserved. This article is
  36. not in the public domain, but it may be redistributed so long as this
  37. notice, the acknowledgments, and the information on obtaining the latest
  38. copy of this list are retained and no fee is charged. The code fragments
  39. may be used freely; credit to the FAQ would be polite. This FAQ is not to
  40. be included in any static: archive (e.g. CD-ROM or book); however, a
  41. pointer to the FAQ may be included. See <Q:01.14> [Where can I get the
  42. latest copy of this FAQ list?] for a link to the latest version of the
  43. FAQ.)
  44.  
  45. This is part 2 of 5 parts.
  46.  
  47. TABLE OF CONTENTS
  48.  
  49. PART 1: 
  50. Section 1.  General FAQ and Newsgroup Information
  51.   <Q:01.01> - Is MS-DOS Dead?
  52.   <Q:01.02> - What is this article for?
  53.   <Q:01.03> - Who has contributed to this article?
  54.   <Q:01.04> - How can I search this article for a particular topic?
  55.   <Q:01.05> - Are the answers guaranteed to be correct and complete?
  56.   <Q:01.06> - What is comp.os.msdos.programmer about?
  57.   <Q:01.07> - Is comp.os.msdos.programmer just for C programmers?
  58.   <Q:01.08> - What is comp.sys.ibm.pc.programmer?
  59.   <Q:01.09> - Is comp.os.msdos.programmer available as a mailing list?
  60.   <Q:01.10> - What's this netiquette?
  61.   <Q:01.11> - How can I learn more about Usenet?
  62.   <Q:01.12> - What other technical newsgroups should I know about?
  63.   <Q:01.13> - Where are FAQ lists archived?
  64.   <Q:01.14> - Where can I get the latest copy of this FAQ list?
  65.  
  66. Section 2.  General Reference
  67.   <Q:02.01> - Are there any good on-line references for PC hardware
  68.               components?
  69.   <Q:02.02> - Are there any good on-line references for PC interrupts?
  70.   <Q:02.03> - What and where is Ralf Brown's interrupt list?
  71.   <Q:02.04> - Where can I find lex, yacc, and language grammars?
  72.   <Q:02.05> - What's the best book to learn programming?
  73.   <Q:02.06> - Why won't my code work?
  74.   <Q:02.07> - Are there any good sources of example code?
  75.   <Q:02.08> - What and where is SNIPPETS?
  76.   <Q:02.09> - Is the source code MS-DOS available?
  77.   <Q:02.10> - What are my alternatives for MS-DOS compatible OSes?
  78.   <Q:02.11> - What and where is FreeDOS?
  79.   <Q:02.12> - Where can I find out about batch files?
  80.  
  81. PART 2: (this part)
  82. Section 3.  Compile and link
  83.   <Q:03.01> - What the heck is DGROUP > 64K?
  84.   <Q:03.02> - How do I fix 'automatic data segment exceeds 64K' or 'stack
  85.               plus data exceed 64K'?
  86.   <Q:03.03> - Will Borland C code and Microsoft C code link together?
  87.   <Q:03.04> - Why did my program bomb at run time with 'floating point
  88.               formats not linked' or 'floating point not loaded'?
  89.   <Q:03.05> - How can I change the stack size in Borland's C compilers?
  90.   <Q:03.06> - What's the format of an .OBJ file?
  91.   <Q:03.07> - What's the format of an .EXE header?
  92.   <Q:03.08> - What's the difference between .COM and .EXE formats?
  93.   <Q:03.09> - How do I create a .COM file?
  94.   <Q:03.10> - Where is EXE2BIN located?
  95.   <Q:03.11> - What does this message mean: 'A20 already enabled so test
  96.               is meaning less?'
  97.  
  98. Section 4.  Keyboard
  99.   <Q:04.01> - How can I read a character without echoing it to the
  100.               screen, and without waiting for the user to press the Enter
  101.               key?
  102.   <Q:04.02> - How can I find out whether a character has been typed,
  103.               without waiting for one?
  104.   <Q:04.03> - How can I disable Ctrl-C/Ctrl-Break and/or Ctrl-Alt-Del?
  105.   <Q:04.04> - How can I disable the print screen function?
  106.   <Q:04.05> - How can my program turn NumLock (CapsLock, ScrollLock) on
  107.               or off?
  108.   <Q:04.06> - How can I speed up the keyboard's auto-repeat?
  109.   <Q:04.07> - What is the SysRq key for?
  110.   <Q:04.08> - How can my program tell what kind of keyboard is on the
  111.               system?
  112.   <Q:04.09> - How can I tell if input, output, or stderr has been
  113.               redirected?
  114.   <Q:04.10> - How can I increase the size of the keyboard buffer?
  115.   <Q:04.11> - How can I stuff characters into the keyboard buffer?
  116.  
  117. PART 3: 
  118. Section 5.  Disks and files
  119.   <Q:05.01> - What drive was the PC booted from?
  120.   <Q:05.02> - How can I boot from drive B:?
  121.   <Q:05.03> - Which real and virtual disk drives are valid?
  122.   <Q:05.04> - How can I make my single floppy drive both a: and b:?
  123.   <Q:05.05> - How can I disable access to a drive?
  124.   <Q:05.06> - How can a batch file test existence of a directory?
  125.   <Q:05.07> - Why won't my C program open a file with a path?
  126.   <Q:05.08> - How can I redirect printer output to a file?
  127.   <Q:05.09> - How can I redirect the output of a batch file?
  128.   <Q:05.10> - How can I redirect stderr?
  129.   <Q:05.11> - How can my program open more files than DOS's limit of 20?
  130.   <Q:05.12> - How can I read, create, change, or delete the volume label?
  131.   <Q:05.13> - How can I get the disk serial number?
  132.   <Q:05.14> - What's the format of .OBJ, .EXE., .COM files?
  133.   <Q:05.15> - How can I flush the software disk cache?
  134.   <Q:05.16> - How can I see if a drive is a RAM drive?
  135.   <Q:05.17> - How can I determine a hard drive's manufacturer?
  136.   <Q:05.18> - Where can I find information about the ATA/ATAPI
  137.               specification?
  138.   <Q:05.19> - How can I copy files to or from filenames containing date
  139.               information?
  140.  
  141. Section 6.  Serial ports (COM ports)
  142.   <Q:06.01> - How do I set my machine up to use COM3 and COM4?
  143.   <Q:06.02> - How do I find the I/O address of a COM port?
  144.   <Q:06.03> - But aren't the COM ports always at I/O addresses 3F8, 2F8,
  145.               3E8, and 2E8?
  146.   <Q:06.04> - How do I configure a COM port and use it to transmit data?
  147.  
  148. PART 4: 
  149. Section 7.  Other hardware questions and problems
  150.   <Q:07.01> - Which 80x86 CPU is running my program?
  151.   <Q:07.02> - How can a C program send control codes to my printer?
  152.   <Q:07.03> - How can I redirect printer output?
  153.   <Q:07.04> - Which video adapter is installed?
  154.   <Q:07.05> - How do I switch to 43- or 50-line mode?
  155.   <Q:07.06> - How can I find the Microsoft mouse position and button
  156.               status?
  157.   <Q:07.07> - How can I access a specific address in the PC's memory?
  158.   <Q:07.08> - How can I read or write my PC's CMOS memory?
  159.   <Q:07.09> - How can I access memory beyond 640K?
  160.   <Q:07.10> - How can I use the protected mode?
  161.   <Q:07.11> - How can I tell if my program is running on a PS/2-style
  162.               machine.
  163.   <Q:07.12> - Is there a 80x87 math unit installed?
  164.   <Q:07.13> - How can I power off the computer from a batch file?
  165.  
  166. Section 8.  Other software questions and problems
  167.   <Q:08.01> - How can a program reboot my PC?
  168.   <Q:08.02> - How can I time events with finer resolution than the system
  169.               clock's 55 ms (about 18 ticks a second)?
  170.   <Q:08.03> - How can I find the error level of the previous program?
  171.   <Q:08.04> - How can a program set DOS environment variables?
  172.   <Q:08.05> - How can I change the switch character to - from /?
  173.   <Q:08.06> - How can I write a TSR (terminate-stay-resident utility)?
  174.   <Q:08.07> - Why does my interrupt function behave strangely?
  175.   <Q:08.08> - How can I write a device driver?
  176.   <Q:08.09> - What can I use to manage versions of software?
  177.   <Q:08.10> - What's this 'null pointer assignment' after my C program
  178.               executes?
  179.   <Q:08.11> - How can a batch file tell whether it's being run in a DOS
  180.               box under Windows?
  181.   <Q:08.12> - How can my program tell if it's running under Windows?
  182.   <Q:08.13> - How can a program tell whether ANSI.SYS is installed?
  183.   <Q:08.14> - How do I copyright software that I write?
  184.   <Q:08.15> - How can I place date and time information into environment
  185.               variables?
  186.  
  187. PART 5: 
  188. Section 9.  Downloading
  189.   <Q:09.01> - What are SimTel and Garbo?
  190.   <Q:09.02> - Can I get archives on CD-ROM?
  191.   <Q:09.03> - Where do I find program <mumble>?
  192.  
  193. Section 10.  Vendors and products
  194.   <Q:10.01> - How can I contact Borland?
  195.   <Q:10.02> - How can I contact Microsoft?
  196.   <Q:10.03> - What is the current version of DJGPP?
  197.   <Q:10.04> - What and where is DJGPP?
  198.   <Q:10.05> - Are there any good shareware/freeware compilers?
  199.   <Q:10.06> - Where is QBASIC?
  200.   <Q:10.07> - What is a vendor's web site address?
  201.  
  202. ------------------------------
  203.  
  204. Subject: Section 3. Compile and link
  205. Date: 5 Feb 2002 22:03:03 -0400
  206.  
  207.  The Compile and Link section contains information issues involving
  208.  compiling and linking of MS-DOS code.
  209.  
  210. ------------------------------
  211.  
  212. Subject: <Q:03.01> - What the heck is DGROUP > 64K?
  213. Date: 8 Feb 2002 19:38:12 -0400
  214.  
  215.  This question explains the problem; the next question gives some
  216.  remedies.
  217.  
  218.  DGROUP is a link-time group of data segments, and the compiler typically
  219.  generates code that expects DS to be pointing to DGROUP. (Exception:
  220.  Borland's huge model has no DGROUP.)
  221.  
  222.  Here's what goes into DGROUP:
  223.  
  224.  * Tiny models (all pointers near): DGROUP holds the entire program.
  225.  
  226.  * Small and medium models (data pointers near): DGROUP holds all globals
  227.    and static variables including string literal data, plus the stack and
  228.    the heap.
  229.  
  230.  * Large, compact, and huge models in Microsoft (data pointers far):
  231.    DGROUP holds only initialized globals and static variables including
  232.    string literal data, plus the stack and the near heap.
  233.  
  234.  * Large and compact models in Borland (data pointers far): DGROUP holds
  235.    initialized and uninitialized globals and static variables including
  236.    string literal data, but not the stack or heap.
  237.  
  238.  * Huge model in Borland (data pointers far): there is no DGROUP, so the
  239.    64K limit doesn't apply.
  240.  
  241.  In all of the above, which is to say all six models in Microsoft C and
  242.  all but huge in Borland C, DGROUP is limited to 64K including string
  243.  literal data (which are treated as static data). This limitation is due
  244.  to the Intel CPU's segmented architecture.
  245.  
  246.  For more information, see topics like "memory models" and "memory
  247.  management" in the index of your compiler manual. Also for an extended
  248.  general discussion of memory usage in Borland C programs, of which much
  249.  applies to any C compiler in DOS see TI738.asc, downloadable as part of:
  250.  
  251.  <ftp://garbo.uwasa.fi/pc/c-lang/bchelp10.zip>
  252.  
  253. ------------------------------
  254.  
  255. Subject: <Q:03.02> - How do I fix 'automatic data segment exceeds 64K' or
  256.         'stack plus data exceed 64K'?
  257. Date: 5 Feb 2002 22:03:03 -0400
  258.  
  259.  These messages are a variation of "DGROUP > 64K". For causes, please see
  260.  the preceding question.
  261.  
  262.  If you get this error in tiny model, your program is simply too big and
  263.  you must use a different memory model. If you get this link error in
  264.  models small, compact, medium, large, or Microsoft's huge, there are
  265.  some things you can do. (This error can't occur in Borland's huge
  266.  model.)
  267.  
  268.  If you have one or two big global arrays, simply declare them far. The
  269.  compiler takes this to mean that any references to them will use 32-bit
  270.  pointers, so they'll be in separate segments and no longer part of
  271.  DGROUP.
  272.  
  273.  Or you can use the /Gt[number] option with Microsoft or - Ff[=size] with
  274.  Borland C++ 2.0 and up. This will automatically put variables above a
  275.  certain size into their own segments outside of DGROUP.
  276.  
  277.  Yet another option is to change global arrays to far pointers. Then at
  278.  the beginning of your program, allocate them from the far heap
  279.  (_fmalloc() in Microsoft, farmalloc() in Borland).
  280.  
  281.  Finally, you can change to huge model (with Borland compilers, not
  282.  Microsoft). Borland's H model still uses far pointers by default, but
  283.  "sets aside the [64K] limit" and has no DGROUP group, according to the
  284.  BC++ 2.0 Programmer's Guide. Microsoft's H model does use huge data
  285.  pointers by default but retains DGROUP and its 64K limit, so switching
  286.  to the huge model doesn't buy you anything if you have DGROUP problems.
  287.  
  288. ------------------------------
  289.  
  290. Subject: <Q:03.03> - Will Borland C code and Microsoft C code link
  291.         together?
  292. Date: 5 Feb 2002 22:03:03 -0400
  293.  
  294.  Typically someone who owns compiler A and is trying to write code to
  295.  link with a third-party library that was compiled under compiler B asks
  296.  this question.
  297.  
  298.  The answer to the question is, Not in general. Here are some of the
  299.  reasons:
  300.  
  301.  * "Helper" functions (undocumented functions for stack checking,
  302.    floating-point arithmetic, and operations on longs) differ between the
  303.    two compilers.
  304.  
  305.  * Extended dictionaries are not compatible between the 2 formats.
  306.    However, the basic structure of both MS and Borland OBJ formats is
  307.    based on the OMF format so specifying that the linker ignore the
  308.    extended dictionary records (/NOE for LINK, -e for TLINK) will disable
  309.    this little hassle.
  310.  
  311.  * The compilers may embed instructions in the object code that tell the
  312.    linker to look for their own run-time libraries. You can use the
  313.    linker option that says to ignore such instructions: /n in TLINK, /NOD
  314.    in the Microsoft linker (the one that comes with the C compiler, not
  315.    the one that used to come with DOS). But getting around this problem
  316.    will very likely just reveal other problems, like different helper
  317.    functions, that have no easy solution.
  318.  
  319.  Those problems will generate link-time errors. Others may not show up
  320.  until run time:
  321.  
  322.  * Borland's compact, large, and huge models don't assume DS=SS, but
  323.    Microsoft's do. The -Fs option on the Borland compiler, or one of the
  324.    /A options on Microsoft, should take care of this problem-once you
  325.    know that's what's going on.
  326.  
  327.  * Check conventions for ordering and packing structure members, and for
  328.    alignment of various types on byte, word, paragraph, or other
  329.    boundaries. Again, you can generally adjust your code to match if you
  330.    know what conventions were used in compiling the "foreign" libraries.
  331.  
  332.  * Check the obvious and make sure that your code was compiled under the
  333.    same memory model as the code you're trying to link with. (That's
  334.    necessary, but no guarantee. Microsoft and Borland don't use exactly
  335.    the same conventions for segments and groups, particularly in the
  336.    larger memory models.)
  337.  
  338.  That said, there are some circumstances where you can link hybrids. Your
  339.  best chance of success comes if you compile in large model with the
  340.  compiler switch that says to reload DS on entry to each function, avoid
  341.  longs and floating point, use only 16-bit pointers, suppress stack
  342.  checking, and specify all libraries used in the link.
  343.  
  344. ------------------------------
  345.  
  346. Subject: <Q:03.04> - Why did my program bomb at run time with 'floating
  347.         point formats not linked' or 'floating point not loaded'?
  348. Date: 5 Feb 2002 22:03:03 -0400
  349.  
  350.  These messages look similar but have very different causes.
  351.  
  352.  "Floating point not loaded" is Microsoft C's run-time message when the
  353.  code requires a numeric coprocessor but your computer doesn't have one
  354.  installed. If the program is yours, relink it using the xLIBCE or xLIBCA
  355.  library (where x is the memory model).
  356.  
  357.  "Floating point formats not linked" is a Borland run-time error (Borland
  358.  C or C++, Turbo C or C++). Borland's compilers try to be smart and not
  359.  link in the floating- point (f-p) library unless you need it. Alas, they
  360.  all get the decision wrong. One common case is where you don't call any
  361.  f-p functions, but you have %f or other f-p formats in scanf() or
  362.  printf() calls. The cure is to call an f-p function, or at least force
  363.  one to be present in the link.
  364.  
  365.  To do that, define this function somewhere in a source file but don't
  366.  call it:
  367.  
  368.    static void forcefloat(float *p)
  369.    {
  370.      float f = *p;
  371.      forcefloat(&f);
  372.    }
  373.  
  374.  It doesn't have to be in the module with the main program, as long as
  375.  it's in a module that will be included in the link.
  376.  
  377.  If you have Borland C++ 3.0, the README file documents a slightly less
  378.  ugly work-around. Insert these statements in your program:
  379.  
  380.    extern unsigned _floatconvert;
  381.    #pragma extref _floatconvert
  382.  
  383. ------------------------------
  384.  
  385. Subject: <Q:03.05> - How can I change the stack size in Borland's C
  386.         compilers?
  387. Date: 5 Feb 2002 22:03:03 -0400
  388.  
  389.  In Turbo C, Turbo C++, and Borland C++, you may not find "stack size" in
  390.  the index but the global variable _stklen should be there. The manual
  391.  will instruct you to put a statement like
  392.  
  393.    extern unsigned _stklen = 54321U;
  394.  
  395.  in your code, outside of any function. You must assign the value right
  396.  in the extern statement; it won't work to assign a value at run time.
  397.  The linker may give you a duplicate symbol warning, which you can
  398.  ignore.
  399.  
  400.  If you are using the Borland PowerPack for DOS _stklen does not change
  401.  the stack size. To change the stack size you must use STACKSIZE in your
  402.  .DEF file. HEAPSIZE can be used to change the size of your program's
  403.  heap.
  404.  
  405. ------------------------------
  406.  
  407. Subject: <Q:03.06> - What's the format of an .OBJ file?
  408. Date: 27 Jun 2003 07:17:15 -0400
  409.  
  410.  Reader Bob Smith, reports that there is a free tool at
  411.  <ftp://ftp.sudleyplace.com/sudleyplace/dispobj.zip> which displays the
  412.  contents of an .OBJ file, as well as two more free tools at
  413.  <ftp://ftp.sudleyplace.com/sudleyplace/displib.zip> which display and
  414.  search the contents of .LIB files.
  415.  
  416.  Information about the base .OBJ format can be found in Intel's document
  417.  number #121748-001, {8086 Relocatable Object Module Formats} (not
  418.  verified).
  419.  
  420.  Both Microsoft and Borland have extended the .OBJ format, as has IBM for
  421.  OS/2; and according to the MS-DOS encyclopedia, Microsoft doesn't
  422.  actually use all the listed formats.
  423.  
  424.  Microsoft-specific .OBJ formats:
  425.  
  426.  * A 45-page article can be found in the {MS-DOS Encyclopedia}, ISBN
  427.    1-55615-049-0, now out of print.
  428.  
  429.  * "Microsoft Object Module Format (OMF)" Specification, 22 Nov 1991, was
  430.    published by the Microsoft Languages Group (not verified).
  431.  
  432.  Borland-specific .OBJ formats:
  433.  
  434.  * Open Architecture Handbook. The Borland Developer's Technical Guide,
  435.    1991, no ISBN. Chapter 2, "Object file contents", (pages 27-50) covers
  436.    the comment records sent to the object file by Borland C++ version 3.0
  437.    and other Borland compilers. The comment records mostly contain
  438.    information for the Borland debugger (not verified).
  439.  
  440.  A "tutorial on the .OBJ format" comes with the VAL experimental linker,
  441.  downloadable as <ftp://garbo.uwasa.fi/pc/assembler/linker.zoo>.
  442.  
  443. ------------------------------
  444.  
  445. Subject: <Q:03.07> - What's the format of an .EXE header?
  446. Date: 5 Feb 2002 22:03:03 -0400
  447.  
  448.  See PC Magazine 30 June 1992 (XI: 12) pages 349-350 for the old and new
  449.  formats. For a more detailed layout, look under INT 21 AH=4B in Ralf
  450.  Brown's interrupt list <Q:02.03> [What and where is Ralf Brown's
  451.  interrupt list?] That list includes extensions for Borland's TLINK and
  452.  Borland debugger info.
  453.  
  454.  Among the books that detail formats of executable files are {DOS
  455.  Programmer's Reference: 2d Edition} by Terry Dettman and Jim Kyle, ISBN
  456.  0-88022-458-4; and {Microsoft MS-DOS Programmer's Reference}, ISBN
  457.  1-55615-329-5.
  458.  
  459. ------------------------------
  460.  
  461. Subject: <Q:03.08> - What's the difference between .COM and .EXE formats?
  462. Date: 7 Feb 2002 14:47:51 -0400
  463.  
  464.  To oversimplify: a .COM file is a direct image of how the program will
  465.  look in main memory, and a .EXE file will undergo some further
  466.  relocation when it is run (and so it begins with a relocation header). A
  467.  .COM file is limited to 64K for all segments combined, but a .EXE file
  468.  can have as many segments as your linker will handle and be as large as
  469.  RAM can take.
  470.  
  471.  The actual file extension doesn't matter. DOS knows that a file being
  472.  loaded is in .EXE format if its first two bytes are MZ or ZM; otherwise
  473.  it is assumed to be in .COM format. For instance, DR-DOS 6.0's
  474.  COMMAND.COM is in .EXE format as is COMMAND.COM in recent versions of
  475.  MS-DOS.
  476.  
  477.  Reader Paul Schylter posted this description of .COM files vs. .EXE
  478.  files to the newsgroup in message <a3rpp8$a9h$1@merope.saaf.se>:
  479.  
  480.  "Actually they must be less than 0xFF00 bytes long, since the PSP, which
  481.  isn't included in the COM file but is within those 64K, is 256 bytes
  482.  long.
  483.  
  484.  "Then CAN use many segments, but they don't have to. In particular, any
  485.  .COM file can be converted to an .EXE file by adding an appropriate
  486.  header to it.
  487.  
  488.  "There are some other differences between a .COM file and a single
  489.  segment .EXE file (both of which must be smaller than 64K):
  490.  
  491.  "The entry point of the .COM file is _always_ 0x100, while the entry
  492.  point of the .EXE file can be at any address.
  493.  
  494.  "The stack size of the .COM file is the remainder of those 64K which
  495.  isn't used by the code image, while the stack size if the single segment
  496.  .EXE file can be set at any size as long as it fits within those 64K.
  497.  Thus the stack can be smallere in the .EXE file.
  498.  
  499.  "When a COM file is loaded, the entire TPA (= "free memory") of MS-DOS
  500.  is allocated for that COM file -- including those parts of the TPA which
  501.  are outside the 64k of the COM file. If you don't want this (e.g.
  502.  because your COM file is a TSR and you want to load other programs
  503.  later), you must explicitly free those parts of the TPA you want freed.
  504.  In the header of an .EXE file you can specify how large part of the TPA
  505.  that .EXE file should receive."
  506.  
  507. ------------------------------
  508.  
  509. Subject: <Q:03.09> - How do I create a .COM file?
  510. Date: 5 Feb 2002 22:03:03 -0400
  511.  
  512.  There are two steps to creating a .COM file. First, your program must
  513.  not have a stack. In C, you must compile your program with the TINY
  514.  memory model. Second, use EXE2BIN or a similar program to convert an EXE
  515.  file to a COM file. To find EXE2BIN see subject: <Q:03.10> [Where is
  516.  EXE2BIN located?]
  517.  
  518. ------------------------------
  519.  
  520. Subject: <Q:03.10> - Where is EXE2BIN located?
  521. Date: 5 Feb 2002 22:03:03 -0400
  522.  
  523.  EXE2BIN was formerly shipped with MS-DOS. If you are still using DOS 5.0
  524.  or earlier you can find EXE2BIN in your DOS directory. Users of DOS 6.x
  525.  need to get the MS-DOS Supplemental Disks. These disks are available via
  526.  FTP at ftp.microsoft.com.
  527.  
  528.  <ftp://ftp.microsoft.com/peropsys/msdos/public/supplmnt>
  529.  
  530. ------------------------------
  531.  
  532. Subject: <Q:03.11> - What does this message mean: 'A20 already enabled so
  533.         test is meaning less?'
  534. Date: 5 Feb 2002 22:03:03 -0400
  535.  
  536.  The DPMIINST program included with older versions of Borland C++ and
  537.  Turbo C++ compilers generates this message. Before running DPMIINST you
  538.  must clean boot your computer.
  539.  
  540. ------------------------------
  541.  
  542. Subject: Section 4. Keyboard
  543. Date: 5 Feb 2002 22:03:03 -0400
  544.  
  545.  The keyboards sections coontains information about how to access the
  546.  keyboard.
  547.  
  548. ------------------------------
  549.  
  550. Subject: <Q:04.01> - How can I read a character without echoing it to the
  551.         screen, and without waiting for the user to press the Enter key?
  552. Date: 5 Feb 2002 22:03:03 -0400
  553.  
  554.  The C compilers from Microsoft and Borland offer getch() (or getche() to
  555.  echo the character); Turbo Pascal has ReadKey.
  556.  
  557.  In other programming languages, execute INT 21 AH=8; AL is returned with
  558.  the character from standard input (possibly redirected). If you don't
  559.  want to allow redirection, or you want to capture Ctrl-C and other
  560.  special keys, use INT 16 AH=10; this will return the scan code in AH and
  561.  ASCII code (if possible) in AL, but AL=E0 with AH nonzero indicates that
  562.  one of the gray "extended" keys was pressed. (If your BIOS doesn't
  563.  support the extended keyboard, use INT 16 AH=0 not 10.)
  564.  
  565. ------------------------------
  566.  
  567. Subject: <Q:04.02> - How can I find out whether a character has been
  568.         typed, without waiting for one?
  569. Date: 5 Feb 2002 22:03:03 -0400
  570.  
  571.  In Turbo Pascal, use KeyPressed. Both Microsoft C and Turbo C offer the
  572.  kbhit() function. All of these tell you whether a key has been pressed.
  573.  If no key has been pressed, they return that information to your
  574.  program. If a keystroke is waiting, they tell your program that but
  575.  leave the key in the input buffer.
  576.  
  577.  You can use the BIOS call, INT 16 AH=01 or 11, to check whether an
  578.  actual keystroke is waiting; or the DOS call, INT 21 AH=0B, to check for
  579.  a keystroke from stdin (subject to redirection). See Ralf Brown's
  580.  interrupt list <Q:02.03> [What and where is Ralf Brown's interrupt
  581.  list?].
  582.  
  583. ------------------------------
  584.  
  585. Subject: <Q:04.03> - How can I disable Ctrl-C/Ctrl-Break and/or
  586.         Ctrl-Alt-Del?
  587. Date: 5 Feb 2002 22:03:03 -0400
  588.  
  589.  Several utilities are downloadable from:
  590.  <ftp://ftp.simtel.net/pub/simtelnet/msdos/keyboard/>
  591.  
  592.  In that directory, cadel.zip contains a TSR (with source code) to
  593.  disable those keys. Also, keykill.arc contains two utilities:
  594.  keykill.com lets you disable up to three keys of your choice, and
  595.  deboot.com changes the boot key to leftShift-Alt-Del. C programmers who
  596.  simply want to make sure that the user can't Ctrl-Break out of their
  597.  program can use the ANSI-standard signal() function; the Borland
  598.  compilers also offer ctrlbrk() for handling Ctrl-Break. However, if your
  599.  program uses normal DOS input such as getch(), ^C will appear on the
  600.  screen when the user presses Ctrl-C or Ctrl-Break. You can avoid the ^C
  601.  echo for Ctrl-C by using _bios_keybrd() in MSC or bioskey() in BC++;
  602.  however, Ctrl-Break will still terminate the program.
  603.  
  604.  An alternative approach involves programming input at a lower level. You
  605.  can use INT 21 AH=7, which allows redirection but doesn't echo the ^C
  606.  (or any other character, for that matter); or use INT 16 AH=0 or 10; or
  607.  hook INT 9 to discard Ctrl-C and Ctrl-Break before the regular BIOS
  608.  keyboard handler sees them; etc., etc.
  609.  
  610.  You should be aware that Ctrl-C and Ctrl-Break are processed quite
  611.  differently internally. Ctrl-Break, like all keystrokes, is processed by
  612.  the BIOS code at INT 9 as soon as the user presses the keys, even if
  613.  earlier keys are still in the keyboard buffer: by default the handler at
  614.  INT 1B is called. Ctrl-C is not special to the BIOS, nor is it special
  615.  to DOS functions 6 and 7; it is special to DOS functions 1 and 8 when at
  616.  the head of the keyboard buffer. You will need to make sure BREAK is OFF
  617.  to prevent DOS polling the keyboard for Ctrl-C during non-keyboard
  618.  operations.
  619.  
  620.  Some good general references are {Advanced MS-DOS} by Ray Duncan, ISBN
  621.  1-55615-157-8; {8088 Assembler Language Programming: The IBM PC}, ISBN
  622.  0-672-22024-5, by Willen & Krantz; and {COMPUTE!'s Mapping the IBM PC},
  623.  ISBN 0-942386- 92-2.
  624.  
  625. ------------------------------
  626.  
  627. Subject: <Q:04.04> - How can I disable the print screen function?
  628. Date: 5 Feb 2002 22:03:03 -0400
  629.  
  630.  There are really two print screen functions: 1) print current screen
  631.  snapshot, triggered by PrintScreen or Shift- PrtSc or Shift-gray*, and
  632.  2) turn on continuous screen echo, started and stopped by Ctrl-P or
  633.  Ctrl-PrtSc.
  634.  
  635.  1) Screen snapshot to printer:
  636.  
  637.  The BIOS uses INT 5 for this. Fortunately, you don't need to mess with
  638.  that interrupt handler. The standard handler, in BIOS versions dated
  639.  December 1982 or later, uses a byte at 0040:0100 (= 0000:0500) to
  640.  determine whether a print screen is currently in progress. If it is,
  641.  pressing PrintScreen again is ignored. So to disable the screen
  642.  snapshot, all you have to do is write a 1 to that byte. When the user
  643.  presses PrintScreen, the BIOS will think that a print screen is already
  644.  in progress and will ignore the user's keypress. You can re-enable
  645.  PrintScreen by zeroing the same byte.
  646.  
  647.  Here's some simple code:
  648.  
  649.    void prtsc_allow(int allow) /* 0=disable, nonzero=enable */
  650.    {
  651.      unsigned char far* flag = (unsigned char far*)0x00400100UL;
  652.      *flag = (unsigned char)!allow;
  653.    }
  654.  
  655.  2) Continuous echo of screen to printer:
  656.  
  657.  If ANSI.SYS is loaded, you can easily disable the continuous echo of
  658.  screen to printer (Ctrl-P or Ctrl- PrtSc). Just redefine the keys by
  659.  "printing" strings like these to the screen (BASIC print, C printf(),
  660.  Pascal Write statements, or ECHO command in batch files), where <27>
  661.  stands for the Escape character, ASCII 27:
  662.  
  663.    <27>[0;114;"Ctrl-PrtSc disabled"p
  664.    <27>[16;"^P"p
  665.  
  666.  If you haven't installed ANSI.SYS, I can't offer an easy way to disable
  667.  the echo-screen-to-printer function.
  668.  
  669.  Actually, you might not need to disable Ctrl-P and Ctrl- PrtSc. If your
  670.  only concern is not locking up your machine, when you see the "Abort,
  671.  Retry, Ignore, Fail" prompt just press Ctrl-P again and then press I. As
  672.  an alternative, install one of the many print spoolers that intercept
  673.  printer-status queries and always return "Printer ready".
  674.  
  675. ------------------------------
  676.  
  677. Subject: <Q:04.05> - How can my program turn NumLock (CapsLock,
  678.         ScrollLock) on or off?
  679. Date: 12 Sep 2002 17:46:15 -0400
  680.  
  681.  First, if you just don't want NumLock turned on when you reboot, check
  682.  your system's setups. (Press a special key like Del at boot time, or run
  683.  the setup program supplied with your system.) Many systems may have an
  684.  option in setup to turn NumLock off at boot time.
  685.  
  686.  You need to twiddle bit 5, 6, or 4 of location 0040:0017. The code
  687.  example below demonstrates changing NumLock status: lck() turns on a
  688.  lock state, and unlck() turns it off.
  689.  
  690.    /* The  status  lights on some keyboards may not reflect  the
  691.     * change.   If  yours  is one, call INT 16 AH=2,  "get  shift
  692.     * status", and that may update them.  It will certainly do no
  693.     * harm.)
  694.     */
  695.  
  696.     #define NUM_LOCK  (1 << 5)
  697.     #define CAPS_LOCK (1 << 6)
  698.     #define SCRL_LOCK (1 << 4)
  699.   
  700.     void lck(int shiftype)
  701.     {
  702.       char far* kbdstatus = (char far*)0x00400017UL;
  703.       *kbdstatus |= (char)shiftype;
  704.     }
  705.     void unlck(int shiftype)
  706.     {
  707.       char far* kbdstatus = (char far*)0x00400017UL;
  708.       *kbdstatus &= ~(char)shiftype;
  709.     }
  710.  
  711. ------------------------------
  712.  
  713. Subject: <Q:04.06> - How can I speed up the keyboard's auto-repeat?
  714. Date: 8 Feb 2002 19:42:39 -0400
  715.  
  716.  The keyboard speed has two components: delay (before a key that you hold
  717.  down starts repeating) and typematic rate (the speed once the key starts
  718.  repeating). Most BIOS versions since 1986 let software change the delay
  719.  and typematic rate by calling INT 16 AH=3, "set typematic rate and
  720.  delay"; see Ralf Brown's interrupt list <Q:02.03> [What and where is
  721.  Ralf Brown's interrupt list?]. If you have DOS 4.0 or later, you can use
  722.  the MODE CON command that you'll find in your DOS manual.
  723.  
  724.  On 83-key keyboards (mostly XTs), the delay and typematic rate can't
  725.  easily be changed. According to PC Magazine 15 Jan 1991 (x: 1) page 409,
  726.  to adjust the typematic rate you need "a memory-resident program which
  727.  simply '[watches]' the keyboard to see if you're holding down a key .
  728.  and after a certain time [starts] stuffing extra copies of the held-down
  729.  key into the buffer." No source code is given in that issue; but the
  730.  QUICKEYS utility that PC Magazine published in 1986 does this sort of
  731.  watching (not verified); source and object code are downloadable from
  732.  <http://www.simtel.net/pub/pd/48667.html>
  733.  
  734. ------------------------------
  735.  
  736. Subject: <Q:04.07> - What is the SysRq key for?
  737. Date: 5 Feb 2002 22:03:03 -0400
  738.  
  739.  There is no standard use for the key. The BIOS keyboard routines in INT
  740.  16 simply ignore it; therefore so do the DOS input routines in INT 21 as
  741.  well as the keyboard routines in libraries supplied with high-level
  742.  languages.
  743.  
  744.  When you press or release a key, the keyboard triggers hardware line
  745.  IRQ1, and the CPU calls INT 9. INT 9 reads the scan code from the
  746.  keyboard and the shift states from the BIOS data area.
  747.  
  748.  What happens next depends on whether your PC's BIOS supports an enhanced
  749.  keyboard (101 or 102 keys). If so, INT 9 calls INT 15 AH=4F to translate
  750.  the scan code. If the translated scan code is 54 hex (for the SysRq key)
  751.  then INT 9 calls INT 15 AH=85 and doesn't put the keystroke into the
  752.  keyboard buffer. The default handler of that function does nothing and
  753.  simply returns. (If your PC has an older BIOS that doesn't support the
  754.  extended keyboards, INT 15 AH=4F is not called. Early ATs have 84-key
  755.  keyboards, so their BIOS calls INT 15 AH=85 but not 4F.)
  756.  
  757.  Thus your program is free to use SysRq for its own purposes, but at the
  758.  cost of some programming. You could hook INT 9, but it's probably easier
  759.  to hook INT 15 AH=85, which is called when SysRq is pressed or released.
  760.  
  761. ------------------------------
  762.  
  763. Subject: <Q:04.08> - How can my program tell what kind of keyboard is on
  764.         the system?
  765. Date: 5 Feb 2002 22:03:03 -0400
  766.  
  767.  Ralf Brown's Interrupt List <Q:02.03> [What and where is Ralf Brown's
  768.  interrupt list?] includes MEMORY.LST, a detailed breakdown by Robin
  769.  Walker of the contents of the BIOS system block that starts at
  770.  0040:0000. Bit 4 of byte 0040:0096 is "1=enhanced keyboard installed".
  771.  Here is a C code example to test the keyboard type:
  772.  
  773.    char far *kbd_stat_byte3 = (char far *)0x00400096UL;
  774.    if (0x10 & *kbd_stat_byte3)
  775.    {
  776.      /* 101- or 102- keyboard is installed */
  777.    }
  778.    else
  779.    {
  780.      /* Not installed */
  781.    }
  782.  
  783.  PC Magazine 15 Jan 1991 (x: 1) suggests on page 412 that "for some
  784.  clones [the above test] is not foolproof". If you use this method in
  785.  your program you should provide the user some way to override this test,
  786.  or at least some way to tell your program to assume a non-enhanced
  787.  keyboard. The article suggests a different approach to determining the
  788.  type of keyboard.
  789.  
  790. ------------------------------
  791.  
  792. Subject: <Q:04.09> - How can I tell if input, output, or stderr has been
  793.         redirected?
  794. Date: 8 Feb 2002 19:49:17 -0400
  795.  
  796.  Normally, input and output are associated with the console (i.e., with
  797.  the keyboard and the screen, respectively). If either is not, you know
  798.  that it has been redirected. Some source code to check this is available
  799.  at the usual archive sites.
  800.  
  801.  Timo Salmi has created a collection of Turbo Pascal units, one of which
  802.  can be used to detect such redirection. These can be downloaded from
  803.  <http://garbo.uwasa.fi/pc/ts.html>. The files you are looking for have
  804.  names of the format tspaVV??.zip where the VV is the current version and
  805.  ?? is 70, 60, 55, 50, or 40 for Turbo Pascal 7.0, 6.0, 5.5, 5.0, or 4.0
  806.  respectively.) Source code is not included. Also see the downloadable
  807.  Frequently Asked Questions files by Timo Salmi:
  808.  <ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip>
  809.  
  810.  If you program in C, use isatty() if your implementation has it.
  811.  
  812.  Good references for the principles are PC Magazine 16 Apr 1991 (x: 7)
  813.  page 374; Ray Duncan's {Advanced MS-DOS}, ISBN 1-55615-157-8, or Ralf
  814.  Brown's interrupt list (<Q:02.03> [What and where is Ralf Brown's
  815.  interrupt list?]) for INT 21 AX=4400; and Terry Dettman and Jim Kyle's
  816.  {DOS Programmer's Reference: 2d edition}, ISBN 0-88022-458-4, pages
  817.  602-603.
  818.  
  819. ------------------------------
  820.  
  821. Subject: <Q:04.10> - How can I increase the size of the keyboard buffer?
  822. Date: 8 Feb 2002 19:50:41 -0400
  823.  
  824.  Microsoft has its own keyboard extender available on the MS-DOS
  825.  supplemental disks for MS-DOS 6.22 which can be found at:
  826.  <ftp://ftp.microsoft.com/peropsys/msdos/public/supplmnt/sup622.exe>
  827.  
  828.  Stan Brown, the former list maintainer, tested only one of the many
  829.  available device drivers that do this, namely BUF160, which extends the
  830.  keyboard buffer to 160 characters. It performed flawlessly for two years
  831.  with MS-DOS 5 and Windows 3.1. It's downloadable as:
  832.  <http://www.simtel.net/pub/pd/47186.html>
  833.  <ftp://garbo.uwasa.fi/pc/keyboard/buf160_6.zip>
  834.  
  835. ------------------------------
  836.  
  837. Subject: <Q:04.11> - How can I stuff characters into the keyboard buffer?
  838. Date: 5 Feb 2002 22:03:03 -0400
  839.  
  840.  If your computer has an enhanced keyboard (see <Q:04.08> [How can my
  841.  program tell what kind of keyboard is on the system?]), put the scan
  842.  code in CH and the ASCII character in CL, then execute INT 16 AH=5. The
  843.  return in AL is 0 for success or 1 for buffer full.
  844.  
  845. ------------------------------
  846.  
  847. Subject: Conclusion
  848.  
  849. This is the end of part 2 of 5 parts.
  850.  
  851. This text is copyright 2003 by Jeffrey Carlyle. All rights reserved.
  852. Please see the top of this article for additional copyright information.
  853.  
  854.