home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / funk108a.zip / DOS32V30.ZIP / DOS32.DOC < prev    next >
Text File  |  1995-06-14  |  48KB  |  1,276 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.                        DOS32        Version 3.01
  11.  
  12.  
  13.  
  14.                        Released on 14 June 1995
  15.  
  16.  
  17.  
  18.                     A 32 bit DOS extender package 
  19.  
  20.  
  21.  
  22.        Copyright (C) 1994  Adam Seychell,  All Rights Reserved.
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.      This document should explain everything about the DOS extender
  43. DOS32 V3.01. This program may only be of use to people who have some
  44. knowledge of assembly language programming. However, I have tried to
  45. make this document easy for a beginner to understand. Anyone who can
  46. no longer tolerate programming with pathetic 64Kb segments or is sick
  47. of the 640Kb barrier and wants to program using the full power of
  48. their computer then use DOS32 for protected mode programming. 
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.                                AGREEMENT
  70.  
  71.      This program is free software only and may not be sold other than
  72. the cost of the disk or physical handling. The use of this program is
  73. entirly at the users risk. In no event will the author be liable for 
  74. any  damages, including any lost profits, lost savings  or other
  75. incidental or consequential damage arising out of the use or inability
  76. to use the program ,or for any claim by any other party. This program
  77. is distributed WITHOUT ANY WARRANTY.
  78.      You can redistribute it only in the same form as you have
  79. received it and all files must be left unmodified. The exception to
  80. this rule is if you choose to develop an application that requires the
  81. file DOS32.EXE then you may freely distribute it with the application.
  82. Only with my permission can you take separate files from this package
  83. and include them in your productions.
  84.      If you are releasing the source code to your program then it may
  85. require DOS32 for compiling. Under these circumstances you may include
  86. an unmodified copy of the entire DOS32 package within your program. 
  87.      Use of the DOS32 dos-extender in any commercial software requires
  88. written permission from the author. 
  89.      Of course I have no real control on what people do with it so I
  90. am just relying on your honesty. So please, help support free
  91. software.
  92.  
  93.                               *  *  *   
  94.  
  95.      The DOS32 project has been developed entirely in my spare time
  96. and I've been slowly working on it for over two years now. It's only
  97. fair for me to ask that ALL programs that use the DOS32 dos-extender
  98. must include full credit for it's use.  If you really like this 
  99. program then I would also enjoy receiving a postcard from you.
  100.  
  101.  
  102.      Anyone may buy a copy of the complete source code to DOS32.EXE
  103. for a fee of $40 Australian. The source code is about 180KB in size
  104. and may be compiled with either MASM (Microsoft Macro Assembler) or
  105. TASM (Turbo Assembler), it is also very well commented and so it
  106. should be easy to follow and learn from. 
  107.  
  108.      Address all correspondence to the author at the following
  109. address;
  110.  
  111.  
  112.                              Adam Seychell
  113.                             16 Avion Place
  114.                         Westmeadows, VIC.  3049
  115.                                AUSTRALIA
  116.  
  117. internet:    s921880@minyos.xx.rmit.edu.au
  118.  
  119.  
  120. Please ensure you enclose your return address!
  121.    If you wish to purchase the DOS32 source code then write out your
  122. cheque in the name of Adam Seychell.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                        WHAT IS A DOS EXTENDER ?
  135.  
  136.      The 80386 has been around since 1987 and to this day DOS is still
  137. running in real mode. Since nobody has bothered to design a protected
  138. mode DOS, programs called 'DOS extenders' were written. A DOS extender
  139. is basically a program that will switch the CPU into protected mode so
  140. that it can then execute a protected mode application under DOS. If
  141. you don't know the difference between real mode and protected mode
  142. then have a go at reading the file PMODE.DOC or better still get a
  143. proper book before reading to much further. I think that it is much
  144. easier to program in protected mode than in real mode ( Intel 8086
  145. architecture ) because you can use only one big segment for the entire
  146. application.
  147.     You could say a DOS extender is a separate program that handles
  148. all the setting up needed for protected mode under DOS. You let the
  149. DOS extender worry about all of the protected mode initialising. So
  150. you write programs in plain and pure protected mode. 
  151.      Since DOS runs in real mode ( or V86 mode ) a protected mode
  152. program can no longer execute DOS calls directly. The DOS extender
  153. should have services that allow protected mode programs to call real
  154. mode code and will either enter V86 mode or return to real mode when
  155. doing so. Usually a DOS extender has many more services than just real
  156. mode calls. For example, setting interrupt vectors, memory allocation,
  157. disk I/O routines and possibly a debugger.
  158.  
  159.  
  160.  
  161.  
  162.  
  163.                           DOS32  Introduction
  164.  
  165.         DOS32 V3.0 is a public domain 32bit DOS-extender package for
  166. the development of true 32bit DOS executable. The actual extender is a
  167. real mode DOS executable called DOS32.EXE. This tiny 8K file is the
  168. heart of the hole package. DOS32.EXE will initialise protected mode
  169. then load and execute a custom 32bit executable. To make one of these
  170. 32bit executables you must use the linker, DLINK, which is also
  171. distributed in this package. 
  172.      My main aims when developing DOS32 was to make a DOS-extender
  173. that is very easy to use, has powerful features and small in size. I
  174. have also tried to make the extender as close as possible to
  175. programming under conventional real mode DOS, and yet have full 32bit
  176. power. This should make it easier for people to start programming in
  177. protected mode. The debugger that is distributed with DOS32 should
  178. also help you become more familiar with the workings of protected
  179. mode. I have released my work so people can stop programming in a 8086
  180. architecture, a CPU produced back in 1978. 
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                             What is DLINK?
  200.  
  201.      The program DLINK is a linker supporting the Intel standard
  202. Object Module Format (OMF). DLINK is distributed with the DOS32
  203. package and is required to produce the customary 32bit executable file
  204. that's loaded by DOS32. The main difference between DLINK and a
  205. conventional DOS linker such as Borland Turbo Linker (TLINK.EXE) or
  206. Microsoft Link (LINK.EXE), is that DLINK produces a special executable
  207. format made specifically for flat memory model programs. When DLINK is
  208. first ran it will begin processing the object modules specified on the
  209. command line. The information inside the modules is used to create a
  210. 32bit executable file of your application, which may simply be
  211. executed under DOS. Exact details of the loading process are explained
  212. later in this document.
  213.  
  214.  
  215.  
  216.  
  217.  
  218.                  Here are some of DOS32 main features
  219.  
  220.  
  221. *    Complies with the following specifications;
  222.        Dos Protected Mode Interface ( DPMI v0.9 )
  223.        Virtual Control Program Interface ( VCPI v1.0 )
  224.        Extended Memory Specification ( XMS v2.0 and v3.0 )
  225.        Int 15h Top-Down extended memory management
  226. *    Unlimited executable size.
  227. *    Paged memory enabling a true flat memory model.
  228. *    Functions for allocating/freeing memory and DMA buffers.
  229. *    Contains a complete set of 32bit DOS file I/O services
  230.        ( open/close/create/read/write/chdir/rename, ect).
  231. *    Services for interfacing with Real Mode programs.
  232. *    Capable of terminating and staying resident.
  233. *    Uses only 42K _total_ memory and requires just 8K of disk space.
  234. *    Distributed with a debugger , C library and linker.
  235.  
  236.                             Minor features
  237.  
  238. *    Supports up to 4Gb of RAM
  239. *    Executable can be compressed
  240. *    Can run with zero extended memory
  241. *    Handles all errors during initialising
  242. *    Load time selector fixups in executable.
  243. *    Not a single 4KB block is wasted on the machine
  244. *    DOS32.EXE can be integrated in the main .EXE file
  245. *    Hooks XMS driver to stop disabling of the A20 gate.
  246. *    Detects VCPI even with Microsoft's EMM386.EXE  NOEMS
  247. *    No overhead on hardware interrupts (expect under DPMI)
  248. *    Runtime termination for CPU exceptions 0,1,3,6,13 & 14
  249. *    Uses VCPI if available even when a DPMI server is present
  250. *    Hooks INT 23h for correct Ctrl+C/Ctrl+Break termination
  251. *    Fast starting and exiting. Less than 0.3 sec total on a 386SX16
  252. *    Guards from returning to protected mode from a real mode switch
  253.      when under a XMS or raw systems and the 386 is in V86 mode.
  254. *    Stops DOS32 trying to be loaded into upper memory because the
  255.      system resets when doing so.
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.             The DOS32  Applications Programming Interface 
  265.  
  266.      The DOS32 extender supplies many useful functions for the
  267. application to use. The complete list of each service and programming
  268. details are described in the file API.DOC. I've put all the API
  269. documentation in a separate file so it may be printed as a handy
  270. reference. ( 65 lines per page).
  271.  
  272.  
  273.  
  274.  
  275.                            DOS32  OPERATION
  276.  
  277.  
  278.      This is the main section of this document and will explain the
  279. workings of DOS32.
  280.  
  281.      This is a flat memory model DOS extender. Flat memory means that
  282. the entire application is operating in a single block of linear
  283. memory. You may think of it as the application gets loaded into memory
  284. in one hit and is located somewhere in the CPU address space. This
  285. location is called the program base address since it represents the
  286. first byte of the program. The CPU is set up for data and code type
  287. segments with their base addresses equal to the program base address.
  288. However, as these two segments are on top of each other in memory they
  289. can effectively be treated as one segment. This allows the
  290. applications data variables and code to be mixed together. The memory
  291. map below may help you understand what's happening.
  292.  
  293.  
  294. Figure (1). The CPU memory map demonstrating a protected mode
  295. application loaded into memory.
  296.  
  297.  
  298.  
  299.       ┌─────────────────────┐ FFFFFFFF
  300.       │                     │
  301.       │                     │
  302.        ░░░░░░░░░░░░░░░░░░░░░
  303.       │                     │
  304.       │                     │
  305.       │                     │
  306.       ├─────────────────────┤
  307.       │ 32bit application is│
  308.       │   loaded in here    │
  309.       ├─────────────────────┤ <-program base address 
  310.       │                     │         (can be anywhere above 1Mb )
  311.       │                     │
  312.       │                     │
  313.       ├─────────────────────┤ 100000h
  314.       │      ROM area       │
  315.       ├─────────────────────┤ C0000h
  316.       │  video memory map   │
  317.       ├─────────────────────┤ A0000h
  318.       │                     │
  319.       │   640K base RAM     │
  320.       │                     │
  321.       └─────────────────────┘ 0
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.      The program's data and code segment limits are 4GB in size. This
  330. means the largest OFFSET address (relative to the program base
  331. address) can range from 0 to FFFFFFFF. An offset of FFFFFFFF will not
  332. address memory above 4Gb but rather rap around so it will address
  333. memory somewhere below the program base address. Therefore it's
  334. possible to access the entire CPU address space with only a single
  335. 32bit offset. It also means there is no need for your programs to ever
  336. again use multiple segment registers. These are the reasons why I
  337. wrote DOS32.
  338.      DOS32 sets up three segments for the application. As explained
  339. above, two of these are the applications code and data segments. The
  340. third segment is another data segment with a fixed base address of
  341. zero and may be used to access physical addresses such as video
  342. memory, BIOS data area, ect. Most of the time the application will
  343. never need to use this secondary data segment because the entire CPU
  344. address space can already be accessed from the programs data segment
  345. using 32bit offsets. The selector value of this zero based data
  346. segment can be obtained from function AX=EE00h INT 31h. The program
  347. base address value can be obtained from function AX=EE02h INT31h (see
  348. API.DOC for details).
  349.      If you are unfamiliar with protected mode, descriptors and
  350. segments then have a go at reading the files PMODE.DOC and PMODE2.DOC.
  351. These files will explain some basics of protected mode operation.
  352.     So basically there are three different segments available to the
  353. application, and all have limits of FFFFFFFF.
  354.  
  355. * data segment
  356. * code segment
  357. * zero base data segment
  358.  
  359.      Please note that the first 1 MB of linear address space is mapped
  360. to the lower 1Mb of physical address space. This means that accessing
  361. locations 0..100000h will actually access the first megabyte of
  362. physical memory. This allows the application to access physical memory
  363. anywhere in the lower 1 MB. The offset address relative to the program
  364. segment can be calculated to point at any location in the first 1Mb of
  365. physical memory.
  366.  
  367. offset = Physical address in first MByte - Base address of segment.
  368.  
  369.      Alternatively you can also use the zero based segment to access
  370. memory in the first megabyte. In this case if the memory is below 1MB
  371. then the  Offset = linear = physical. You will probably only need
  372. physical memory locations to access the video memory (i.e 0A0000h -
  373. 0BFFFFh).  If your program dos not need the zero base segment then you
  374. can ignore this selector value and keep all of the segment registers 
  375. ( DS,ES,FS,GS,SS ) loaded with the program data selector. This is what
  376. makes protected mode so powerful, you can access all of your
  377. data,code, video memory or whatever just by using the 32bit offset. 
  378. If you are still not clear on what I am saying here then try having a
  379. look the programming examples distributed with this package.
  380.      When the application is first executed at the program entry point
  381. the CPU registers will hold the following values. All data segment
  382. registers, DS,ES,FS,GS and SS, will contain the programs DATA
  383. selector. CS will contain the programs CODE selector. EIP will point
  384. to the program entry address. ESP will point to the last dword of the
  385. defined stack (as explained below). EAX,EBX,ECX,EDX,EDI,ESI,EBP and
  386. FLAGS registers are all undefined and may contain any value.
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.                 Executable format and loading process.
  395.  
  396.      The information here will explain how DOS32 and DLINK work
  397. together and exactly what happens when the executable is first ran
  398. from DOS to when the application's first instruction is executed.
  399. Please note that it is not essential for you to know these details but
  400. will be described here for those of you who are interested.  
  401.  
  402.  
  403.  
  404.  
  405.    File format of a DOS32 executable (.EXE file produced from DLINK)
  406.  
  407. Offset    Size           Description
  408. ─────────────────────────────────────────────────────────────────────
  409. 0         n    A stub loader or DOS32.EXE of n bytes 
  410. n+0       4    Signature "Adam"
  411. n+4       2    DLINK version 
  412.                 bit 15   = 0 executable image is not compressed
  413.                          = 1 executable image is compressed.       
  414.                 bits 0..14 hold the DLINK version in packed BCD format
  415. n+6       2    Minimum DOS32 version required (packed BCD)
  416. n+8       4    Number of bytes remaining after the stub file.
  417.                The entire .EXE file size (after linking) will be equal 
  418.                to this value plus the stub file size.
  419. n+0Ch     4    Start of 32bit executable image relative to the 'Adam' 
  420.                signature.
  421. n+10h     4    Length of executable image ( see below ).
  422. n+14h     4    Initial memory required for application. This value
  423.                must be larger than the size of the 32bit executable
  424.                image. 
  425. n+18h     4    Initial EIP
  426. n+1Ch     4    Initial ESP
  427. n+20h     4    Number of entries in selector fixup table. Each entry
  428.                in the table contains a 32bit offset relative to the
  429.                start of the executable image of a 16bit WORD that must
  430.                be set to the programs data selector at load time. The
  431.                table immediately follows the executable image.
  432.  
  433. The 32bit executable image (application binary data) can start
  434. anywhere after this point (offset n+24h in the .EXE file).
  435.  
  436. --- more entries can be added in future ----
  437.  
  438.  
  439.  
  440.  
  441.      At the front of the .EXE is the stub loader or DOS32.EXE file and
  442. must be a normal DOS executable ( i.e start with a 'MZ' signature ).
  443. When the .EXE is executed, DOS will only load and execute the 'MZ'
  444. executable at the front of the file and ignore the rest of the file.
  445. The stub loader is a small program that will simply execute DOS32.EXE.
  446. I have distributed a sample stub loader, STUB.ASM. This actual program
  447. is stored internally in DLINK and is automatically used as the stub if
  448. the -S option is not used. See later in this document for more
  449. information about the -S switch.
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.      When the program DOS32.EXE is executed it will first initialise
  460. for protected mode and try an load in the 32bit application. It does
  461. this by first checking at the end of the it's self for the 'Adam'
  462. signature. If found it will then assume that it is a DOS32 32bit
  463. executable and read the rest of the header and begin loading the
  464. application it into memory. If no 'Adam' signature was found it will
  465. then look at parent program which had executed DOS32.EXE. If the
  466. parent program has the 'Adam' signature then it will begin loading the
  467. executing the DOS32 executable. If the parent does not contain the
  468. signature then the following error will be displayed.
  469.  
  470.  
  471. DOS32 Version 3.00  32bit DOS-extender and loader
  472. Copyright (c) Adam Seychell, 1994. All rights reserved.
  473.  
  474. File name: C:\DOS32.EXE
  475.  
  476. Error:  Bad DOS32 executable
  477.  
  478.  
  479.  
  480.      With this set up it allows DOS32.EXE to be used in place of the
  481. stub loader. If a stub loader is used, and the user runs the .EXE, the
  482. stub file will be executed which in turn will execute DOS32.EXE. DOS32
  483. will then begin it's work, by first initialising for protected mode
  484. operation and allocate a memory block for the application.  The
  485. executable image is then loaded into the memory block followed by
  486. applying the selector fixups. Finally the stack pointer (SS:ESP) and
  487. instruction pointer (CS:EIP) are set to finally start the application
  488. up and running.
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.                              PROGRAMMING      
  525.  
  526.  
  527.      The section explains, in general, on how to write protected mode
  528. applications. It will explain about the initial stack, entry point,
  529. selectors, segments definitions and examples of basic ASM file
  530. structure. 
  531.  
  532.  
  533.  
  534.                           Segment Definitions
  535.  
  536.      There may be some confusion with segments defined in the ASM file
  537. to the segments set up by the CPU. If you cannot grasp all this
  538. segment stuff then don't worry. It's only useful to know if you want
  539. to understand how the programs are stored in the executable. 
  540.      In real mode if you declared a segment that is not grouped to the
  541. main segment then a segment register must be loaded with the segment
  542. value if any data/code is to access it. For example, if the
  543. application had more than 64KB of code then multiple code segments
  544. would need to be defined and far calls would be required to call
  545. across the segments. The CPU segment registers have to be altered each
  546. time a the CPU accesses a new segment.
  547.      In a flat memory things work completely different. Segments
  548. defined in the ASM file (or in modules from a C compiler) have nothing
  549. to do with segments defined in the CPU descriptor tables. As described
  550. earlier, flat memory is when only one segment in memory exists for the
  551. entire application. Segments defined in the ASM file are like
  552. secondary segments or sections within the actual program segment in
  553. memory. The object modules ( OBJ files) contain information of all the
  554. segments that have been defined in the source code. The linker will
  555. place all the code/data from a particular segment name and group it
  556. into one section in the program segment in memory. For example, all
  557. data/code that has been defined in the segments named 'blabla' will be
  558. grouped to together when stored in the executable.
  559.      The Object Module Format ( format of OBJ files ) are capable of
  560. containing all sorts of information about a segment definition. Some
  561. of these include  public or private,  Alignment in memory and Group
  562. definitions. However for flat memory most of them can be ignored.
  563.  
  564.  
  565.  
  566.  
  567. Rules on how DLINK handles segments.
  568.  
  569. *    DLINK can support up to 100 segment definitions.
  570. *    Each segment is automatically made public. The public field in
  571.      the  segment definition is ignored.
  572. *    The segment alignment may be set on the DLINK command line. The
  573.      alignment field in the segment definition is ignored. for example
  574. *    The occurrence of each segment definition in the executable will
  575.      be same order as they are defined in the object modules. 
  576. *    Each segment will automatically be grouped together. The
  577.      assembler GROUP directive is ignored. 
  578. *    The code/data defined in segments of the same name will be next
  579.      to each other in the executable.
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.      It is possible to define data that is not initialised. In this
  590. case the actual segment size greater than the total size of code and
  591. data that is defined in it. In assembler, non-initialised data can be
  592. defined with the ? operator. The following two examples will define a
  593. 1Kb array of data that is not initialised to any value.
  594.  
  595. My_Array    DB  1024 DUP (?)            ; Assembly  language
  596.  
  597. char  My_Array[1024];                   /*  C language   */
  598.  
  599.  
  600.      The non-initialised data does not matter what information it
  601. contains when the application is first loaded. Therefore if the end of
  602. the executable contains non-initialised data then there is no need for
  603. it to be loaded into memory and therefore stored in the .EXE file.
  604.      DLINK takes advantage this and will not include any non-
  605. initialised data in the executable file which has been declared at the
  606. end of the program segment. This is how the initial stack area is set
  607. up. A segment is defined with an array and the stack pointer will
  608. point to the last dword of the segment.
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.                    Defining the program entry point
  617.  
  618.      The entry point of the program is defined exactly the same way as
  619. writing a conventional assembler program. The start address will equal
  620. to the label at the assembler END directive. For example, the
  621. following code shows how to identify a program's starting instruction
  622. with the label 'start'.
  623.  
  624.    .CODE                                  ; define code segment using 
  625.                                           ; quick segment definitions.
  626. start:
  627.         ..   
  628.         ..
  629.         ..
  630.  
  631. END start
  632.  
  633.  
  634.      Please note that the program entry point will only be set to the
  635. first module to contain a starting address. Starting addresses defined
  636. in following modules will be ignored and the linker will report with a
  637. warning message. The program eventually terminates with INT 21h
  638. AH=4Ch, just as in good old DOS.
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.                       Defining the initial stack
  655.  
  656.      When the program is first loaded the initial stack area is
  657. defined by the last segment with it's class name equal to 'STACK'. The
  658. initial value of ESP will point to the last dword of this segment.
  659. Please also note that DOS32 should have a stack size of 128 bytes to
  660. play with. Add to this what ever stack your program is going to use.
  661.  
  662. Example: To define a 128Kb stack
  663.  
  664.   My_Stack  SEGMENT 'STACK'             
  665.  
  666.   DB 20000h DUP (?)
  667.  
  668.   My_Stack  ENDS
  669.  
  670.  
  671. If you are using the assembler quick segment definitions ( e.g  .model
  672. .code .data .stack ) then the stack may be defined with the assembler
  673. directive .STACK [size]. 
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.                    Reading the program data selector
  681.  
  682.      In a real mode program the segment value could be obtained by
  683. referencing a segment name. E.G   MOV AX,_TEXT    will load AX with
  684. the real mode segment value of the _TEXT segment. In protected mode
  685. only selectors are used so loading AX with a real mode segment value
  686. is useless not to mention impossible if it's above 1MB. Whenever a
  687. segment name is referenced  (like in the above instruction ) the value
  688. of the programs data selector is read. It does not matter what segment
  689. name or group definition is referenced, the value read will *always*
  690. be the programs data selector. This feature is handy for interrupt
  691. handlers as the program data selector may be required. For example,
  692. the following code will set both DS and FS segment registers to the
  693. programs data selector.
  694.  
  695. First_Seg SEGMENT
  696.  
  697. First_Seg ENDS
  698.  
  699. The_Seg SEGMENT
  700.      mov  ax,The_Seg
  701.      mov  ds,ax
  702.      mov  ax,First_Seg
  703.      mov  fs,ax
  704. The_Seg ENDS
  705.  
  706.  
  707.  
  708. If you are using the assembler quick segment definitions ( e.g  .model
  709. .code .data ) then you may reference the data selector using '_DATA'.
  710. This is because the .Model directive automatically defines _DATA as
  711. the name of the data segment.
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.                General Assembly Language file structure
  720.  
  721.  
  722.      For most applications the programmer will only ever need to use
  723. the assembler quick definitions directives for setting up segments.
  724. Both TASM and MASM 6.x support the new FLAT memory model types and so
  725. have no problems compiling flat memory 32bit code. Flat memory model
  726. programming is easy since there is no limitation on what goes where.
  727. For example you could write all the code in the .DATA segment and
  728. place all data variables in the .CODE segment.
  729. Below is an example of a basic ASM file;
  730.  
  731.  
  732. .386
  733. .MODEL FLAT
  734. .STACK 
  735.  
  736. .DATA
  737.           .. 
  738.           .. code/data
  739.           ..
  740. .CODE
  741.  
  742.          ...
  743.          ... more code/data
  744.          ...
  745.  
  746. Start:
  747.          ...
  748.          ...
  749.          ... code and data can go anywhere 
  750.          ...
  751.  
  752. END Start
  753.  
  754.  
  755. The above example will create a starting module since it contains a
  756. symbol after the END directive. The .STACK should only need to be
  757. defined in one module only. If it's defined more than once then the
  758. linker will added all the stacks together to form one large stack area
  759. (assuming the assembler greats the same names for each stack segment
  760. definition). If other modules are to be linked with the above example
  761. then they should not define a stack and/or a starting point.
  762.  
  763.  
  764. For example;
  765.  
  766. .386
  767. .MODEL FLAT
  768. .CODE
  769.  
  770.  ...
  771.  ... code/data 
  772.  ...
  773.  
  774. END
  775.  
  776. It couldn't get any simpler, really.
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.                         32bit Programming Notes
  785.      
  786.      If you are new to 32bit assembly then there are a few important
  787. things you need to know. First off, all memory refrences should always
  788. be using 32bit addrssing modes instead of 16bit addressing modes. For
  789. example, the instruction MOV AL,Varible[BX] is using 16bit addressing
  790. and consequently will have a 16bit displacment value. If this varible
  791. is located above 64Kb then the assembler cannot fix up the correct
  792. address. The above instruction should be  MOV AL,Varible[EBX].
  793.      Secondly, avoid pushing and poping 16bit registers on the stack
  794. becuase these may take the stack pointer out of dword alignment. ESP
  795. should always be kept aligned on a 4 byte boundary or else your
  796. program may run slower. This is because in 32bit mode, the stack
  797. transfers in double words and the CPU will take extra clocks cycles
  798. for every misaligned memory reference. 
  799.      The Segment Registers (i.e DS,ES,FS,GS,CS) are always stored as
  800. dwords on the stack in 32bit mode.
  801.  
  802.  
  803.  
  804.                           USING  THE  LINKER 
  805.           
  806.      Since the DOS32 package is mainly for assembly language
  807. programming I will assume you are familiar and have used a
  808. conventional linker before. Remember DLINK has been designed
  809. specifically for flat memory model programs so there is no need to
  810. worry about all the modules and segments combining to produce one
  811. large executable. If you wish to stop DLINK while it is processing
  812. then press the CTRL+BREAK keys, it then should return to the command
  813. prompt.
  814.      DLINK is controlled entirely by the command line so you will need
  815. to know what each command line switch does. The format of the command
  816. line is;
  817.  
  818.     DLINK  OBJ file list [, EXE file] [,MAP file] [, LIB file list]
  819.  
  820.      The OBJ file list is a list of the Object Modules that DLINK is
  821. required to link. Each file name is separated by spaces. If the
  822. executable file name is not specified then it will be equal to the
  823. name of the first OBJ file. The default file name extension of the
  824. Object Module file list and executable file are .OBJ and .EXE
  825. respectively. 
  826.      The MAP file is a small text file that the linker creates showing
  827. information of all the segments defined in the modules. I included
  828. this feature for compatibility with conventional linkers. If no MAP
  829. file is specified the file is not created.
  830.      The LIB file list are library files that the linker is required
  831. to process. The format of these files must be in the Microsoft Library
  832. file format (.LIB extension). DLINK will link ALL object modules
  833. contained in the library file. Even if the modules are not required by
  834. the program DLINK will still include them in the executable.
  835.      DLINK will also accept command line response files. The response
  836. file is a text file containing the input DLINK expects on the command
  837. line and has the exact same format as the Turbo linker (TLINK.EXE) or
  838. the Microsoft linker (LINK.EXE) response files. All response files
  839. specified must precede the name with an @ character. The response file
  840. can be used to overcome the 128 character limit of the command line or
  841. is sometimes used by compiler make utilities.
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.                             DLINK  Options
  850.      
  851.      The linker options can go anywhere on the command line and must
  852. start with - or / characters. If the environment variable
  853. "DLINK_OPTIONS" is available then DLINK will set options contained in
  854. this environment string. You can use this environment variable to hold
  855. frequently used options. Below is a description of each option.
  856.  
  857. Option                   Description
  858.  
  859. @xx  Where xx is the file name of a response file. Multiple response
  860.      files are allowed and can be placed anywhere on the command line.
  861.  
  862. /v   Verbose linking information. This will display all sorts of
  863.      information that the linker is currently processing.
  864.  
  865. /m   Create MAP file even if none is specified on the command line.
  866.  
  867. /L<path1>[;<path2>][;<path3>]
  868.      Specify library search paths. This switch tells the linker to
  869.      also search in the directory <path> when loading library files
  870.      specified on the command line.
  871.  
  872. /c   Case sensitive symbol matching. All external symbols that are to
  873.      be matched with public symbols in other object modules will be
  874.      compared case sensitive. When this switch is not used, matching
  875.      is case insensitive.
  876.  
  877. /Sx  Use alternate stub file x. The stub loader which is placed at the
  878.      front of the executable file will be the file x. If x is not
  879.      specified then DOS32.EXE will be used as the stub loader. If no
  880.      file extension is specified the extension '.EXE' will be used.
  881.      For example, dlink -Sstuber will use the file stuber.exe as the
  882.      stub loader. When this switch is not used, a default stub loader
  883.      is used.
  884.  
  885. /p   Ignore checking of the same symbol name defined more than one
  886.      object module. Setting this switch may speed up the linker when
  887.      processing more than a few hundred symbols but then has the risk
  888.      of linking in the same symbol name more than once. 
  889.  
  890. /A=N Set segment alignment to N. The starting address of the data
  891.      defined in each segment definition will be aligned on a N byte
  892.      boundary. Valid values of N are 1,2,4,8,16,32,64,....,2048,4096
  893.      If this switch is not used then the segment alignment defaults to
  894.      a four byte boundary (/A=4) which is all that is necessary for
  895.      the 386,486 and Pentium CPUs.
  896.  
  897. /C   Compress final .EXE. This option will tell the linker to compress
  898.      the 32bit executable image in the .EXE file. The stub loader,
  899.      header and fixup table are kept uncompressed. See reference 10
  900.      for the algorithm I used. On average the compression will 
  901.      produces files about 1.2 to 1.5 times that of the ZIP
  902.      compression. Decompression is done by the loader, DOS32.EXE, and
  903.      can expand about 1MB/sec on a 486 33Mhz. For tightest
  904.      compression, avoid creating executable with large arrays of the
  905.      same byte. If possible, define all arrays with non-initialised
  906.      data so they are not written directly into the .EXE file.
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.                              THE DEBUGGER
  915.  
  916.      This DOS extender comes with a complete debugger for the
  917. debugging your 32bit protected mode programs. The whole debugger is
  918. the file DEBUG.OBJ and must be to be linked in with the program that
  919. you wish to debug. The debugger is started by executing a near call to
  920. the external procedure named "Debug" or "Debug_Run". Calling "Debug"
  921. will immediately start the debugger directly after this call. Calling
  922. "Debug_Run" will only initialise the debugger and return to the
  923. program as normal.
  924. With this later call the debugger will only come into action when a
  925. CPU exception occurs (such as a Page Fault, General Protection,
  926. breakpoint) or if CTRL+BREAK key is pressed. In effect, this call is
  927. identical as calling 'Debug' and pressing the F9 key to continue
  928. executing. 
  929.     You have probably already ran the debugger example program,
  930. DEBUG.EXE You will find that it's similar to a conventional debugger
  931. except it's for 32bit protected mode. The debugger will not debug when
  932. in V86 mode or in real mode. Whenever your program calls real mode (or
  933. V86) then the debugger will be disabled until it returns to the
  934. program in protected mode. 
  935.      The main feature of the debugger is single-stepping. Single-
  936. stepping allows the user to execute one instruction at time and able
  937. to see the values of each register. There are two types of stepping
  938. available. Pressing the F7 key will step on every instruction except
  939. for INT xx. Pressing the F8 key will step on all instructions except
  940. for CALL, INT xx, LOOP and instructions with the REP prefix. For these
  941. instructions that are not stepped through, the debugger will begin
  942. normal execution of the instruction and return back to debugging mode
  943. on the following instruction. 
  944.      When stepping through, the debugger saves the value of each
  945. register. The debugger has back stepping feature, which allows the
  946. user to load every register to the same values as in the previous
  947. step. The back stepping keys are ALT+F4 and can only go back up to 40
  948. times.  
  949.      You can also insert breakpoints anywhere in the code simply by
  950. pressing F2. This will insert (or delete) an opcode 0CCh which is an
  951. single byte to cause an interrupt 3. You will see the instruction
  952. highlighted red. When the program is run ( F9 key ), it will stop at
  953. the breakpoint instruction and return to the debugger. You can also
  954. assemble your programs with an INT 3 instruction at any locations you
  955. wish to break. For example, if you have a procedure that hangs the
  956. system, you could assemble the program with an INT 3 somewhere inside
  957. the procedure. The program may then simply be executed ( F9) and the
  958. debugger will take charge soon as the INT 3 is executed.
  959.      Another feature of the debugger is toggling between the user
  960. screen and debuggers screen by ALT+F5. When switching to the users
  961. screens, the video mode, palette, cursor location, font memory, text
  962. mode screen memory, and all of the _standard_ VGA registers are
  963. restored. Non standard VGA registers are not restored so you may have
  964. some problems in SVGA modes.
  965.      The debugger also has it's own keyboard handler. This means the
  966. debugger is independent of the BIOS's keyboard handler. Programs can
  967. grab IRQ 1 for their own keyboard handlers and do not need to be
  968. redirect back to the BIOS. At any time the program is running you may
  969. return to the debugger by pressing CTRL+BREAK keys.
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.      When stepping through certain instructions a temporary switch to
  980. the users screen is made. The instructions that will cause a video
  981. switch include,  LOOP, INT xx ,CALL, REP prefix, and any access to the
  982. VGA ports. This makes sure that the VGA card is properly programmed 
  983. while debugging.
  984.  
  985.      This debugger directly uses the 386's debug registers so don't
  986. try running it under DPMI. Windows DPMI seems to handle single-
  987. stepping and the software breakpoints, but it's not very reliable and
  988. OS/2 hangs with blank screen, yes OS/2 can crash. I have tried using
  989. the DPMI services for setting the debug registers but have not had
  990. much luck. In my next version I might add support for DPMI but at the
  991. moment I cannot be bothered with it. As it is DEBUG.ASM is already
  992. over 140KB of assembler...( yuck )
  993.  
  994.  
  995.  
  996.                           Advanced Debugging
  997.  
  998.     As well as software breakpoints ( opcode 0CCh) and single
  999. stepping, the 386 has a set of special debug registers. There are four
  1000. breakpoint registers each of which hold a 32bit linear breakpoint
  1001. address ( registers DR0..DR3 ). Another debug register ( DR7 ) sets
  1002. the type of breakpoint for each of the four breakpoint addresses.
  1003. There is also a debug status register ( DR6 ) which contains
  1004. information for the exception 1 handler.  See the Intel documentation
  1005. for a complete description of the debug registers.
  1006.       The debuggers exception 1 handler will read DR6 to see what was
  1007. the caused of the exception.  If the exception was caused from one of
  1008. the four breakpoint addresses then a message will be displayed on the
  1009. screen telling you which breakpoint it was and if it was an
  1010. instruction or data breakpoint. At any time the user can hold down the
  1011. CONTROL key to display the values of the four linear breakpoint
  1012. addresses and the W, R, and LEN fields from DR7. 
  1013.      The debugger does not set any of the debug registers however the
  1014. program that you are debugging can.  Your program can load any of the
  1015. breakpoint addresses and control fields (e.g.  Ri,Ei,LENi & Gi ) and
  1016. the debugger will report the breakpoint so you can then analyse the
  1017. program.
  1018.  
  1019.  
  1020.  
  1021.                              DOS32  Notes
  1022.  
  1023.      DOS32 will run under a DPMI ( Dos Protected Mode Interface )
  1024. server if one is installed. DPMI is a bunch of services developed so
  1025. that DOS programs can run in protected mode in a multitasking
  1026. environment. Windows in enhanced mode and OS/2 supply a DPMI server
  1027. when executing a DOS program. All of the services in DOS32 will work
  1028. the same way wether it is using a DPMI server, VCPI server, XMS driver
  1029. or plain raw DOS. The VCPI ( Virtual Control Program Interface) is
  1030. what Expanded Memory Managers provide to allow dos extenders to switch
  1031. into and protected mode. Any Expanded Memory Manager, for example
  1032. EMM386.EXE, QEMM386.SYS, 386MAX, runs the CPU in V86 mode so that it
  1033. can use the 386 paging hardware to swap expanded memory pages for DOS.
  1034. If DOS32 is run when the CPU is already in V86 mode and there isn't a
  1035. DPMI or VCPI server available then it will exit with a error message
  1036. because there is no way for DOS32 to switch into protected mode. 
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.                  Exceptions ( with non DPMI systems )
  1045.  
  1046.      DOS32 will handle the exceptions interrupts 0 to 31 by displaying
  1047. a message on the screen of the type of exception, then it exits to
  1048. DOS. Your program can hook any of these interrupts but it is usually
  1049. not required.
  1050.      The allocate memory service will build page tables as the memory
  1051. is allocated. This means that if you try to access memory that was not
  1052. allocated you may end up with a page fault exception. When running
  1053. under DPMI, all exceptions will be handled by the DPMI server. 
  1054.  
  1055.  
  1056.  
  1057.  
  1058.                       Hardware Interrupt Handlers
  1059.  
  1060.      All hardware interrupts IRQ's 0..15 can be hooked in protected
  1061. mode by using the interrupt services. When you program starts DOS32
  1062. will initially handle all the hardware interrupt vectors (including
  1063. the NMI) so they  all will be redirected to the original real mode
  1064. interrupts. For example, when ever you push a key it will be calling
  1065. INT 9 in Real/V86 mode.
  1066.  
  1067.  
  1068.                                IMPORTANT
  1069.  
  1070.      Never assume what will be in the segment registers in a hardware
  1071. interrupt handler. You should always preserve the segment registers
  1072. used and then load them with the desired selector. The interrupt
  1073. handler may obtain the programs data selector by referencing any
  1074. segment definition name. This was explained before above in 'Reading
  1075. the program Data Selector'.
  1076.      The other important thing to remember is that when operating
  1077. under a DPMI server ALL modified interrupt vectors (INTs 0 .. 255)
  1078. should be returned to their original values before the application
  1079. terminates. When under Raw/XMS or VCPI this is not necessary to do but
  1080. should always be done for compatibility with DPMI.
  1081.  
  1082.  
  1083.     Oh I almost forgot, in protected mode you cannot write to the code
  1084. segment otherwise it will cause the famous General Protection
  1085. Exception. However reading data is allowed by using the program code
  1086. selector. So a MOV CS:[0],EAX is a no no.
  1087.  
  1088.  
  1089.  
  1090.                         bugs, bugs and more bugs
  1091.  
  1092.      A word about program bugs. Of course the more complicated a
  1093. program is the more likely it's going to have bugs. This is by far the
  1094. biggest assembler program I have ever written and so I am sure it will
  1095. have a bug or two. So I would appreciate it if you would contact me on
  1096. finding any bugs. A program like DOS32 or any DOS extender fiddles a
  1097. lot around with drivers and hardware, which means compatibility
  1098. problems across the thousands of different IBM compatible computers
  1099. available. I don't have access to very many different types of PCs so
  1100. it is hard for me to find these bugs. Of course it works flawlessly on
  1101. my computer :-)
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.                     The continuation of DOS32
  1110.  
  1111.      I wrote this DOS extender for myself and have customised it to my
  1112. preferences. I have found no reason to have descriptor allocation
  1113. services, however some people may desperately need extra descriptors
  1114. in their programs. The VESA BIOS Extension v2.0 interface supports
  1115. protected mode applications. 32bit BUS video cards are also becoming
  1116. more popular with linear memory mapping and so I included the physical
  1117. memory mapping service, INT31h AX=800h.
  1118.      If there are lots of people who are interested in this DOS
  1119. extender, it may end up becoming a big project. If there are people
  1120. who are willing to improve DOS32 or help with libraries then we may
  1121. even be able to get a team of people working on the project. I am
  1122. getting tired of the DOS32 project and I probably will not release
  1123. many more versions of DOS32. I would really like for other people who
  1124. are willing to improve DOS32 to take over and make it a bigger and
  1125. better program for the public domain. Of course, if I decide to do
  1126. this then I would have to release the entire DOS32 package source
  1127. code. It depends on how many people are interested in helping out with
  1128. DOS32. Remember DOS32 is freeware so it's here for everyone to use.
  1129.          Anyone who is interested then please contact me! 
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.                  Known bugs, problems and limitations
  1136.  
  1137. *       DOS32 will not run under Novell DOS 7.0 when using it's EMS
  1138. driver. An error is reported because DOS32 can only handle interrupt
  1139. vectors of 8 and 70h for PIC #1 and PIC #2 controllers respectively.
  1140. The VCPI server in Novell EMS driver remaps the PIC interrupt vectors
  1141. some other values. However DOS32 should work fine under the Novell DOS
  1142. Raw, XMS and DPMI configurations.
  1143.  
  1144. *    The Debugger will not correctly write to the screen while single
  1145. stepping through instructions that are accessing the video memory.
  1146.  
  1147. *       When under Raw/XMS systems and another application has been
  1148. executed ( e.g using INT21h AH=4Bh ) which switches the 386 into V86
  1149. mode and the DOS32 application has hooked a protected mode IRQ or a
  1150. real mode call back then this protected mode IRQ handler or Call Back
  1151. procedure will not be called but rather a IRET or RETF will be
  1152. executed. This avoids DOS32 trying to do a raw switch into protected
  1153. mode while the 386 is already in control of another protected mode
  1154. program and thus causing an exception to it.
  1155.  
  1156. *       When under raw/XMS/VCPI systems and the application has hooked
  1157. interrupt vectors 13 or 14 (IRQs 5 or 6 ) then general exceptions and
  1158. page faults will no longer be handled and may cause a "triple fault" 
  1159. ( CPU reset ) if one of these exceptions does occur.
  1160.  
  1161.  
  1162. *       If the application has terminated and stayed resident and the
  1163. system in under XMS and any IRQ other than IRQ 1 has been hooked then
  1164. Windows cannot be loaded. This should not be too much of a problem
  1165. since most people who use Windows are fools and run a EMS driver,
  1166. thinking they are increasing available memory!.
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.                          Limitations of DLINK
  1175.  
  1176.  
  1177.  Description                            Maximum limit
  1178. ─────────────────────────────────────────────────────────────────────
  1179. Number of externals                     1024 per module
  1180. Number of publics                       limited by memory available
  1181. Number of object modules                1024
  1182. Number of segments                      100
  1183. Size of response file                   6K
  1184. Size of executable                      4068 Mbytes
  1185. Memory required to link                 approx 135Kbytes
  1186.                                         + total size of OBJs and LIBs 
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194.  
  1195.                                THE  END
  1196.  
  1197.      After reading all this doc, I hope you have made at least some
  1198. sense out of it. I know my english is not the greatest but you cant
  1199. complain if your getting it for free.  I am also probably missing some
  1200. very important notes about DOS32. 
  1201.      So all in all I think ( I hope ) that this DOS extender works out
  1202. to be a very useful program for developing your software. If you are
  1203. new to 80x86 programming and what to jump straight into 32bit power
  1204. (flat memory) without getting involved with the 386's architecture
  1205. then this DOS extender is probably what your after. For people who
  1206. have only ever used real mode assembler I bet that they will be hooked
  1207. to protected mode programming once got the hang of it. The way I see
  1208. it, there is no reason why people should ever again write real mode
  1209. assembler programs when they can use DOS32.
  1210.  
  1211.  
  1212.  
  1213.  
  1214.                 Best of luck, and remember to support free software ! 
  1215.       
  1216.                  Adam Seychell.
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239. References used in the development of DOS32;
  1240.      
  1241.    1    Ralf Brown Interrupt listings, Release 41, 6/5/1994 
  1242.  
  1243.    2    DPMI Specification, Version 0.9,  26 July 1990
  1244.  
  1245.    3    VCPI Specification, Version 1.0,  12 June 1989
  1246.  
  1247.    4    eXtended Memory Specification (XMS), Version 3.0, Microsoft    
  1248.         Corporation, Lotus Development Corporation, Intel              
  1249.          Corporation,and AST Research, Inc. January 1991.
  1250.  
  1251.    5    Microprocessors Vol 1 & 2, Intel Corporation, 1993
  1252.  
  1253.    6    "Undocumented DOS"
  1254.  
  1255.    7    S.P.Morse, E.J.Isaacson and D.J.Albert, "The 80386/387
  1256.         Architecture", Jhon Wiley & Sons, Inc. 1987
  1257.  
  1258.    8    Microsoft Products Support Services Application Note (Text     
  1259.         file) SS0288: Relocatable Object Module Format. Revision 5/92.
  1260.  
  1261.    9    HelpPC,  quick reference utility,  David Jurgens, 1991
  1262.  
  1263.    10   LZSS.C source code of, Storer and Szymanski compression        
  1264.         algorithm and was written in C by Haruhiko Okumura who         
  1265.         distributed it as public domain in the archive LZ_C.ZIP.
  1266.  
  1267.      
  1268.  
  1269.  
  1270. MASM & LINK  are a trademark of Microsoft corporation.
  1271. TASM & TLINK  are a trademark of Borland International.
  1272.  
  1273.  
  1274.  
  1275.  
  1276.