home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / programming / misc_programming / dos32.doc < prev    next >
Text File  |  1993-10-04  |  11KB  |  310 lines

  1.                      DOS32  V0.1
  2.  
  3.       A 386 32 bit dos extender for Assembly Programmers.
  4.  
  5.  
  6.  
  7.      Written By Adam Seychell.    (5 / SEP / 1993)
  8.  
  9.      email  s921880@minyos.xx.rmit.oz.au
  10.  
  11.  
  12.  
  13.  
  14.  
  15.   Copying.
  16.  
  17.    Feel free to copy and distribute this DOS extender.
  18.     Use it in your programs any way you wish. I am             
  19.     distributing this to get people more familiar with         
  20.     protected mode programming.
  21.    However if you do make use of it in your program I would 
  22.   appreciate if you credit me for the dos extender. Also if 
  23.   you use it to write anything great could you send me a copy.
  24.   of it. I have spent a lot of hours writting this DOS extender.
  25.  
  26.  
  27.           *********     FEATURES     **********
  28.  
  29.      * Supports DPMI        ( windows and OS/2 compatibility !!! )
  30.  
  31.      * Supports XMS
  32.  
  33.      * Supports No memory mangers    ( most powerful )
  34.  
  35.      *  Data and code segment descriptors avalible for 
  36.                          1. Your ASM segment.
  37.                          2. Flat memory.
  38.                          3. Base memory.
  39.                          4. An allocated extended memory block
  40.                               ( size is defined by you).
  41.  
  42.      * Functions to set/get protected mode interrupt
  43.           descriptor vectors.
  44.  
  45.      * Calling real mode interrupts is nearly as easy as
  46.      calling in real mode itself. Switches to V86 mode when
  47.      called.
  48.  
  49.      * Supports the SYSTEM BIOS COPY EXTENDED MEMORY BLOCK
  50.      function INT 15h, AH = 87h for real mode. So a real
  51.      mode TSR that calls this BIOS function (eg. HIMEM.SYS )
  52.      will be emulated the dos extender. This allows TSRs like
  53.      disk caches to be running.
  54.  
  55.  
  56.          ********   KNOWN PROGRAM BUGS   ***************
  57.  
  58.   There is a problem with the BIOS int 15h ah = 87 emulation.
  59.    Only HIMEM.SYS calls it to transfer a XMS block while MSDOS
  60.    is loaded in the Upper Memeory Block. It seems to be called
  61.    with the location of the GDT ( for int 15 ah=87 ) in the ROM
  62.    area. This gives an invalid GDT.
  63.  When DOS is loaded into convention memory everything work fine because
  64.    HIMEM.SYS supplies a valid GDT to this call.
  65.  
  66.  
  67.  Introduction.
  68.  
  69.            DOS32.ASM is a 386 protected mode initialise
  70. program. The purpose of it is to make protected mode
  71. programming very easy. When I was writing DOS32.ASM my aim was
  72. to use full 32bit power of the 386 while making it as easy as
  73. possible to program.
  74.            The DOS32.ASM is meant to be assembled as an object
  75. file and then linked to you assembler program. You ASM file
  76. must follow the following rules. Again, I tried to make this
  77. simple as possible.
  78.  
  79.  
  80.      * Requires a small include file to setup macros,
  81.      structures ect.
  82.      * You must have the code segment named CODE32 and a data  
  83.      segment named DATA32.
  84.      * Never change the stack segment register, i.e. SS.
  85.      * Pass V86 mode segment registers on stack when calling   
  86.      V86 mode.     ( see below for details)
  87.      * The first 32 bytes of the stack are reserved. 
  88.           i.e locations  ss:[0] up to  ss:[1Fh]
  89.      * Must have a starting point called "START32:" in the
  90.           code segment.
  91.      * Requires you to load valid selectors in ES,GS,FS at
  92.      startup
  93.      * Must only terminate the program with INT 21h, AH = 4Ch.
  94.  
  95.  
  96.    ****  DETAILS ON WHAT DOS32 DOES.  *****
  97.  
  98.   DOS32.ASM basicly contains code to set up segment descriptors
  99. in the LDT or GDT, enter/exit protected mode, allocated/free a
  100. extended memory block and call real mode interrupts. These
  101. parts of the DOS32.ASM will never have to be called directly
  102. form your program.
  103.  
  104. Note. If operating under DPMI then all DPMI services will be
  105. available.
  106.  
  107.  When not operating under DPMI there are two services in the
  108. dos extender to set and get the protected mode interrupt
  109. vectors. These will work exactly the same as DPMI services. 
  110.  
  111.  SET interrupt
  112.      ax  = 204h
  113.      bl  = interrupt number
  114.      edx = offset
  115.      cx  = selector
  116.      int 31h
  117.   returns:      carry set on error
  118.  
  119.  GET interrupt 
  120.      ax = 205h
  121.      bl = interrupt number
  122.      int 31h
  123.   returns:  edx = offset
  124.             cx  = selector
  125.  
  126. Also the Programible Interrupt Controllers ( 8259) base vector
  127. values can be obtained from the DPMI Get Version function, int 31h
  128. ax = 400h. If not running under DPMI then only the PIC bases
  129. are returned.
  130. DH = master PIC  ( pic 2 )
  131. DL = slave PIC   ( pic 1 )
  132.  
  133.  
  134.  
  135.  
  136. AVAILABLE DESCRIPTORS .
  137.      There are nine available descriptors for you program
  138. that DOS32.ASM initially sets up. The selector values for each
  139. descriptor is stored in the data segment under the labels:
  140.  
  141.  
  142. PSP_sel   :Data descriptor for the Program Segment Prefix
  143.                     Base = the real mode PSP segment.
  144.                     Limit = 100h
  145.  
  146. Data_sel        :Data descriptor for the your data segment "DATA32"
  147.                           Base = the real mode data segment.
  148.                           Limit = 1MB
  149.  
  150. Code32_sel        :32 bit code descriptor for the your code segment "CODE32"
  151.                           Base = the real mode code segment.
  152.                           Limit = 1MB
  153.  
  154. TheStack_sel   :Data descriptor for the stack
  155.                           Base = the real mode stack segment.
  156.                           Limit = See DOS32.ASM
  157.  
  158. XMS_sel   :Data descriptor for the allocated extended memory
  159.                   Base = the base address of the block
  160.                   Limit = Size of allocated block ( see below)
  161.  
  162. BASE_sel        :Data descriptor for the free base memory
  163.                  Base = the unused base memory straight after
  164.                  MSDOS loads in you program.  (see below)
  165.                  Limit = 1Mb 
  166.  
  167.  
  168. VIDEO_sel          :Data descriptor for the video area.
  169.                       Base = A0000h
  170.                       Limit = 20000h
  171.  
  172. FLAT_DATA_sel      :Data descriptor for a flat memory model
  173.                       Base = 0
  174.                       Limit = FFFFFFFFh
  175.  
  176. FLAT_CODE32_sel    :32 bit code descriptor for a flat memory model
  177.                        Base = 0
  178.                        Limit = FFFFFFFFh
  179.  
  180.  
  181.  
  182.  The first instruction of you program will be executed is
  183.  at offset "start32:" in your code segment.
  184.  
  185.  Your program is initially started with DS pointed to your
  186. data segment ( DATA32 ) and ESP and SS setup for the stack.
  187. The stack size is given by the equate in DOS32.ASM "Normal_stack".
  188. I've set the value to 200h bytes, but can be changed if required.
  189.  
  190.  
  191. NOTE: Your program must load valid selectors into ES,FS,GS if
  192. it's going to use any of them. 
  193.  
  194. example
  195. ;----------------------------------------
  196. start32:       ; starting point
  197.      mov gs,[flat_sel]
  198.      mov es,[flat_sel]
  199.      mov fs,[xms_sel]
  200.  
  201.         ...
  202.         ...    You program
  203.         ...
  204.         ...
  205.  
  206.         mov ah,4ch  ; terminate
  207.         int 21h
  208. ;----------------------------------------
  209.  
  210.  One more feature with DOS32.ASM is that in the data segment
  211. there are four labels in the data segment with information
  212. about memory.
  213.  
  214.      1) "Base_Segment" will contain a real mode segment value
  215. of the start of free base memory.   i.e  The value stored in 
  216. this label * 16 = the base address of the BASE_sel descriptor.
  217.  
  218.      2) "PSP_segment". The real mode segment vlaue of the
  219. program segment prefix.
  220.  
  221.      3) "XMS_base". Contains the linear base address of the
  222. allocated extended memory block.
  223.  
  224.      4) "XMS_usage".  This location contains the size of the
  225. allocated extended memory block required by your program.
  226. DOS32.ASM will read the value of this  Label and attempt to
  227. allocate that amount of memory. If there isn't a enough free
  228. memory then DOS32.ASM will allocate as mush as it can and
  229. return what was actually allocated back into this Label.
  230.  
  231. Notes
  232.       o     The XMS_sel descriptor Limit is set to this value.
  233.       o     The Memory allocated will never be greater than
  234.             lockable memory.
  235.       o     If the XMS driver nor DPMI is available then the
  236.           entire extended memory will be allocated by the xms
  237.           descriptor. base = 0100000h , limit = available ram.
  238.       o     If      XMS_usage is zero then no memory will be   
  239.           allocated and the XMS_sel will point to a invalid    
  240.           descriptor.
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247. Calling a Real mode interrupt:
  248.  
  249.      This is one of the most useful features of DOS32.ASM. The
  250. way the interrupt is called is through the macro "DOSINT"
  251. followed by the interrupt number. This macro actually consists
  252. of two instructions, mov & int 20h ( see DOS32.INC).
  253.           When the real mode interrupt is called, all the
  254. 32bit general registers and the 16bit flags ( except for ESP )
  255.  
  256. are loaded with the same values they were in protected mode.
  257. Because in protected mode the segment registers cant be loaded
  258. with any value (except for valid selectors) a different method
  259. is used to pass the segment register values. 
  260.        The segment registers values MUST be passed from
  261. protected mode to the called real mode interrupt through the
  262. stack segment. This is located in the stack at the first 4
  263. dwords and has the following format.
  264.  
  265.         ss:[00h] holds the GS register
  266.         ss:[04h] holds the FS register
  267.         ss:[08h] holds the DS register
  268.         ss:[0Ch] holds the ES register
  269.  
  270.         This is the easiest and most versatile way I could
  271. think how to do it. Using the stack instead of say a data
  272. segment don't require DS,ES,GS or FS segment registers to be
  273. loaded with a specific selector. This is because SS will
  274. always contain the stack selector. I have included a data
  275. structure to make it easier to load these locations ( see the
  276. file DOS32.INC  ).
  277.  
  278.  
  279.          eg . to print a string using dos func 9 int 21h
  280.  ;---------------------------------------------------------
  281.     mov  SS:Real_DS ,seg test_message   ; pass DS register
  282.     mov  ah,9
  283.     mov  dx,offset test_message
  284.     dosint 21h
  285.  
  286.     mov ah,4ch                ; Terminate program
  287.     int 21h
  288.  
  289.     db test_mesg ' String message using dos func ah = 9 ',10,13,36
  290.  ;---------------------------------------------------------
  291.  
  292.  Just like calling real mode int, all the general registers,
  293. flags and segment registers(on the stack segment) will be
  294. returned with the values after the real mode interrupt call.
  295.  
  296.  
  297.        The only thing to left to say about the called real
  298. mode interrupt is that the stack is set up by DOS32.ASM and
  299. cant be altered throng your program. The real mode stack space
  300.  
  301. is defined by an equate in DOS32.ASM called "V86_stack_size". It
  302. is initially set to 200h and shouldn't need to be changed. I
  303. thought this should be a safe value.
  304.  
  305.  
  306.  
  307.  
  308.    ****     I don't ever want to here of people complaining
  309. about the 640K limit for there own assembly programs !
  310.