home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / doc / chapter7.txt < prev    next >
Text File  |  1989-10-29  |  19KB  |  494 lines

  1. CHAPTER 7.   DOS INTERFACE
  2.  
  3.  
  4.  
  5.  
  6. Forth is a language with an operating system packed in.  It provides a
  7. complete programming environment so that a user can write, test and debug
  8. substantial programs completely inside of this environment.  When Chuck
  9. Moore started developing Forth, the operating systems were primitive, and
  10. often got in the way, preventing the user from exploiting the full
  11. capability of the hardware underneath the operating system.  His
  12. solution, adopted by most earlier Forth implementations, was to eliminate
  13. the operating system completely and incorporate all essential functions
  14. of the operating system into Forth.  BLOCK was invented to access mass
  15. storage directly, and it allows Forth to use different magnetic storage
  16. media very efficiently and quite transparently to the user.
  17.  
  18. However, using blocks imposes many restrictions on the style of
  19. programming in Forth.  The most apparent restriction is on the editor,
  20. which handles text in fixed 1024 byte chunks.  It also makes Forth
  21. incompatible with other programming environments.
  22.  
  23. In the good old days of minicomputers and mainframes, compatibility was
  24. among the least of concerns because there were so many of them.  None of
  25. them were compatible in hardware or in software.  In the confusion, Forth
  26. was able to offer an universal solution, and was ported easily to every
  27. machine in sight.  It is very sad that these bygone days are no more.  In
  28. the field of microcomputers and personal computers, Big Blue and
  29. Not-So-Big Red have established certain common practices, if not
  30. standards. Whether we like them or not, DOS and MAC prevail as the
  31. environments we are condemned to live in for the next few years.  After
  32. that, we will all be sentenced for life under UNIX if we can ever escape
  33. from C.
  34.  
  35. It is thus very important that Forth should take advantage of these
  36. less-than-optimized operating systems in areas where convenience and
  37. compatibility are useful and cost effective.  Forth can be then further
  38. refined to arrive at the optimized solutions which demand the best of
  39. performance and code compacting.  F-PC tries to exploit the DOS
  40. environment to its practical limit in adopting the sequential file
  41. structure for mass storage and providing a smooth and convenient path to
  42. all the utility in DOS and OS2.  All DOS and BIOS calls are readily
  43. accessible from F-PC.  You can even spawn a DOS shell within F-PC to use
  44. DOS utilities.
  45.  
  46.  
  47. 7.1.   SYSTEM INTERRUPTS AND BIOS CALLS 
  48.  
  49.  
  50. BIOS (Basic Input Output System) is a set of subroutines stored in the
  51. Read-Only Memory (ROM) in the PC.  These subroutines provide the most
  52. elementary services for the operating system to access the resources in
  53. PC, such as the keyboard, the display, the disk drives, and the printer.
  54.  
  55. To invoke any of these service subroutines, a software interrupt
  56. instruction INT must be executed at the machine level.  This interrupt
  57. instruction transfers the control to the proper service routine via an
  58. interrupt vector table in the memory area 0:0 to 0:3FFH.  Parameters are
  59. sent to the service routine in one or more registers in the CPU.
  60. Results, if any, are returned in the registers or as flags.  For
  61. available BIOS services and their functional specifications, one must
  62. consult the IBM Hardware Reference Manual for the specific model or any
  63. reasonably good book on MS-DOS. See some references in the Preface to
  64. this Manual.
  65.  
  66. F-PC itself does not provide high level tools to do software interrupts.
  67. However, it does use BIOS service to handle keyboard input, screen and
  68. printer output.  Since most of these words were coded in assembly, they
  69. invoke INT instructions directly.  For example, to receive a character
  70. from the keyboard through BIOS:
  71.  
  72.         CODE BIOSKEY    ( -- char )
  73.                 BEGIN
  74.                         MOV AH, # 0
  75.                         INT 16
  76.                         CMP AX, # 0
  77.                 0<> UNTIL
  78.                 MOV BIOSKEYVAL AX
  79.                 1PUSH
  80.                 END-CODE
  81.  
  82. Other important software interrupts are: 
  83.  
  84.         INT 5           printer
  85.         INT 6           video display
  86.         INT 19          diskette drive
  87.         INT 33          DOS services
  88.  
  89. If you intend to use any of the software interrupts, you can find lots of
  90. good examples in the source files of F-PC.  Use them as models and modify
  91. them to suit your own need.
  92.  
  93.  
  94. 7.2.    DOS SERVICE CALLS
  95.  
  96.  
  97. INT 33 invokes the DOS services which cover a wide range of very
  98. interesting functions which are not in the BIOS ROM, but loaded into the
  99. RAM memory when DOS is booted.  F-PC uses the DOS services extensively
  100. and a set of words is defined to facilitate their uses.
  101.  
  102. BDOS                    ( data function -- n )
  103.  
  104. This service emulates the CP/M BDOS call protocol, which requires a
  105. function number and some data.  The number returned on the stack is zero
  106. always, so that it is compatible with the BDOS function implemented in
  107. F83.
  108.  
  109. When a service call requires more input and/or output parameters, F-PC
  110. gives you the following choices:
  111.  
  112. BDOS2                   ( CX DX AX -- CX DX AX )
  113.  
  114. Pop the top three items on the data stack and then do an INT 33.  The
  115. contents of CX, DX, and AX, at the completion of the DOS service is
  116. returned on the stack.  Most status information is returned in these
  117. registers by DOS.  You have to consult a DOS manual for detailed
  118. specifications on registers.
  119.  
  120. OS2                     ( CX DX AX -- CX DX AX )
  121.  
  122. OS2 is identical to BDOS2.  Its sole purpose is to please people in the
  123. three piece suits.  The last item from AX register is masked to an 8 bit
  124. number.
  125.  
  126. XFDOS                   ( DX CX BX AX ES DS -- CX BX AX Carry )
  127.  
  128. XFDOS is needed to allow DOS services to access memory above the 64K byte
  129. Code/Data Segment.
  130.  
  131. DOS 33 Interrupt Services provides more than 30 different types of
  132. services.  It is not possible to cover them here even to a very small
  133. extent.  If you are interested, please consult the MS-DOS systems manual
  134. or books.  In F-PC, the file handles, time/date, directory path, some
  135. keyboard and screen functions are all done through this interrupt
  136. service.
  137.  
  138.   7.3. THE DOS SHELL
  139.  
  140.  
  141. The interface from F-PC to DOS occurs at several levels.  In this section
  142. we will discuss the interface to the DOS commands as opposed to the DOS
  143. system calls.  DOS is a language, with its own syntax rules and
  144. functions.  By providing a bridge to DOS, F-PC encompasses two powerful
  145. languages at a very small cost.
  146.  
  147. It is convenient to be able to issue DOS commands from within F-PC.  This
  148. avoids having to leave F-PC to do the normal housekeeping works that are
  149. regular occurrences in a DOS based computer. In line with this, F-PC
  150. implements several commands:
  151.  
  152. $SYS                            ( counted-string -- f1 )
  153.  
  154. Pass the counted string to COMMAND.COM as a command line.  A shell is
  155. spawned in the process with the "/c" parameter included so that
  156. COMMAND.COM terminates on completion of the command line.  If a NULL
  157. string is passed then the DOS shell will be spawned for you to enter one
  158. or several command lines.  You can then return to Forth by typing EXIT
  159. <return>.
  160.  
  161. SYS  <commands> (  -- )
  162.  
  163. Accept a command line following SYS and executes the commands as a DOS
  164. command line.
  165.  
  166. `  <commands>   ( --  )
  167.  
  168. Pronounced 'back tick'.  A pseudonym for SYS. 
  169.  
  170. These words allow performing almost any DOS command line operation you
  171. would want to do. To make things even more convenient, several FORTH
  172. words have been defined which use the $SYS for specific DOS functions.
  173. They are as follows:
  174.  
  175.                 DIR  DEL  CHDIR  CD  COPY  REN  RENAME
  176.  
  177.                 A:   B:   C:   D:
  178.  
  179. These commands may be used exactly as they are used in DOS. If you press
  180. Control-C, or Control-Break during the execution of any of the above
  181. words, the operation will abort and control will be returned back to
  182. Forth.
  183.  
  184. SYS or ` can be given without a DOS command.  In this case you return the
  185. COMMAND.COM shell and you will see the familiar C> or equivalent DOS
  186. prompt.  You can execute any DOS commands or other COM and EXE files.
  187. After you are through with DOS, EXIT will return you back to F-PC.  You
  188. can switch very conveniently back and forth among Forth, DOS and any
  189. other application.
  190.  
  191.  
  192. 7.4.    BATCH COMMANDS
  193.  
  194.  
  195. F-PC allows commands to be passed to Forth on the DOS command line in the
  196. following manner:
  197.  
  198.         C> F-PC  <filename>  <forth words>  <return>
  199.  
  200. This illustrates how you can start F-PC with a specified file name, and
  201. perform commands on the file.  An example of how this might be used is as
  202. follows:
  203.  
  204.         C>  F-PC  BANNER  LOAD  <return>
  205.  
  206. Here we are starting F-PC with the file BANNER.SEQ, and then performing
  207. LOAD to compile the file.  Another way to use command line parameters is:
  208.  
  209.         C>  F-PC  -  <forth words>  <return>
  210.  
  211. Here we are starting F-PC, followed by a dash "-" to tell F-PC we are not
  212. opening a file, then telling F-PC to perform the Forth words following
  213. the "-".  Here is an example:
  214.  
  215.         C>  F-PC  -  REPAIR  DIR  <return>
  216.  
  217. This example starts up F-PC without a file and tells F-PC we want to
  218. repair the word DIR.  It will locate the word DIR and start up the editor
  219. with the cursor located on the first line of the DIR definition.
  220.  
  221. We can build batch files this way.  As you can see, many types of
  222. commands could be given to F- PC on the command line.  Here are the
  223. contents of the FMETA.BAT:
  224.  
  225.         F-PC  -  FLOAD  META86
  226.  
  227. This single line batch file meta-compiles the F-PC kernel file and
  228. creates the KERNEL.COM file. A similar batch file EXTEND.BAT is provided
  229. to extend the the kernel into a full featured F-PC system.  There are a
  230. number of batch files in the F-PC system and they are good examples of
  231. how to call F-PC to perform various tasks in the DOS environment and then
  232. return automatically to DOS.  It is a valuable tool for building
  233. customized applications.
  234.  
  235. The only caution on batch files is that you must not place commands on
  236. the command line which cause the command line to be interpreted again.
  237. The words HELLO and COLD are such words. They should not be used since
  238. infinite recursion will crash the system.
  239.  
  240.  
  241. 7.5.   DOS MEMORY MAP OF F-PC
  242.  
  243.   F-PC was built to handle a class of programming problems that requires
  244. very large memory and cannot be handled well on any normal 64K
  245. implementation of Forth because of space constraints. Careful analysis
  246. showed that in a large Forth system most memory space is consumed by
  247. colon definitions.  This is quite natural because Forth is a virtual
  248. machine.  The virtual machine is built upon a small kernel which has to
  249. be implemented in code definitions.  However, once the basic mechanism is
  250. in place, utilities and applications are constructed in high level using
  251. colon definitions.  Therefore, F-PC struggled to give the user as much
  252. space as he will need to store colon definitions, while reserving only
  253. 64K bytes for code, constants, variables, and arrays, and 64K bytes for
  254. heads which may or may not be needed in an application.  F-PC also
  255. includes words with which you can ask the operating system for more
  256. memory when needed and release it back to DOS when you have finished
  257. using it.
  258.  
  259. F-PC allows the List (colon space) Segment to be much larger than 64K
  260. bytes.  In fact the only real limitation in system size comes from the
  261. fact that there are only 64k available for heads. Calculations indicate
  262. there is room for about 2300 definitions on top of the current system of
  263. about 3000 words.  This calculation is based on an average name length of
  264. 5 characters, with an average head length of 12 bytes.  With 5300 total
  265. definitions, less than 30k of code segment would be used for code fields
  266. of colon definitions, leaving the remaining 30+K of Code Segment for
  267. variables and arrays.  Not enough for an infinitely large application
  268. obviously, but probably large enough for most  applications.
  269.  
  270. F-PC places a segment (ES) and and offset (IP) on the return stack for
  271. each nest operation.  Only the relative segment offset is compiled into
  272. the code field, and the conversion to absolute segment is performed by
  273. NEST.  This makes the code fully relocatable as is required by the DOS
  274. environment.  Some performance could be gained by using absolute segment
  275. addressing, and performing a conversion at save and load time to and from
  276. relative and absolute.  The performance gain would come from having a
  277. simpler NEST, which would not have to perform the relative to absolute
  278. conversion at run-time.
  279.  
  280. CS, SS, and DS always point to the Code Segment, so that all assembly
  281. language operations work with data in the Code Segment unless a special
  282. operator like @L or !L is used to reach an external memory area.  This
  283. means there is no penalty to be paid for most Forth operations and very
  284. high compatibility to F83 is maintained.
  285.  
  286. The header of every word contains a view field for locating the source
  287. code, a link field to maintain the vocabulary thread, a name field, and a
  288. code pointer field.  The code pointer field contains a code field address
  289. which points to the executable code of this word in the Code Segment.
  290. The machine code in the Code Segment is executed when this word is
  291. invoked.  For a colon definition, the code starts with a JMP NEST, which
  292. saves the current ES and IP registers, adds the next two bytes to XSEG
  293. and moves them into ES and clears IP to zero.  When the NEST routine is
  294. executed to completion, NEXT obtains the next instruction in the List
  295. Segment pointed to by ES register and starts processing the address list
  296. in this colon definition.
  297.  
  298. In a code definition, the code pointer field in the header points to its
  299. machine code in the Code Segment.  For other types of words, like
  300. CONSTANTs, VARIABLEs, and other executable structures, the first
  301. instruction in the code field is always a CALL to the appropriate inner
  302. interpreter which carries out the task assigned to the specific type of
  303. word.
  304.  
  305. This manual is not the proper place to discuss the inner mechanism of
  306. F-PC.  If the above discussion seems to be too abstract, please don't
  307. worry.  F-PC behaves just like any other Forth system.  You can be very
  308. productive without any knowledge about how or why the Forth virtual
  309. machine works.  However, it is very comforting to know where things are
  310. stored, just in case you need to find them.
  311.  
  312.  
  313.  
  314. The memory segments in F-PC
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.                                                            
  362.  
  363. Data structures in a colon definition
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.                                                
  405.  
  406.  
  407. 7.6.    LONG MEMORY WORD SET
  408.  
  409.  
  410. A complete set of words is defined in F-PC to let the user accessing the
  411. DOS memory very conveniently.  The standard Forth memory accessing words
  412. are retained for addressing the Code/Data Segment in the memory.  Generic
  413. extended memory words are appended (or prepended) with the letter L, and
  414. they require address specifications in segment:offset pairs. Words
  415. addressing the List Segment have X prefix and words addressing the Head
  416. Segment have Y prefix.
  417.  
  418. Generic Long Memory Words are those which require an explicit segment
  419. address with a 16 bit address offset.  They are the basic word set from
  420. which other segment specific memory words are derived.  This word set
  421. includes the following words:
  422.  
  423.  @L      ( seg addr -- n )       Fetch 16 bit integer.
  424.  !L      ( n seg addr -- )       Store 16 bit integer.
  425.  C@L     ( seg addr -- b )       Fetch a byte.
  426.  C!L     ( b seg addr -- )       Store a byte.
  427.  CMOVEL  ( seg addr seg' addr' n -- ) Move n bytes from seg:addr to seg':addr'.
  428.  CMOVEL> ( seg addr seg' addr' n -- ) Move n bytes in reversed order.
  429.  LFILL   ( seg addr n b -- )     Fill n bytes from seg:addr with b.
  430.  LDUMP   ( seg addr n -- )       Dump n bytes from seg:addr.
  431.  Seg is stored into DUMPSEG.
  432.  
  433. Following are words addressing the List Segment: 
  434.  
  435.  X,      ( n -- )         Compile integer to top of the list dictionary.
  436.  XC,     ( b -- )         Compile byte to the list dictionary.
  437.  XDUMP   ( rel-seg n -- ) Dump n bytes in the list segment starting at XSEG.
  438.  XDP     ( -- addr )      Offset to top of list dictionary.
  439.  XDPSEG  ( -- seg )       Segment to top of list dictionary.
  440.  XHERE   ( -- seg addr )  Seg:addr to top of list dictionary.
  441.  XSEG    ( -- seg )       Segment base of List Space.
  442.  
  443. The corresponding words addressing into the Head Segment are: 
  444.  
  445.  Y@      ( addr -- n )    Fetch integer from head segment.
  446.  Y!      ( n addr -- )    Store integer to head segment.
  447.  YC@     ( addr -- b )    Fetch byte.
  448.  YC!     ( b addr -- )    Store byte.
  449.  Y,      ( n -- )         Compile integer to head dictionary.
  450.  YCSET   ( b addr -- )    Set bits in a byte.
  451.  YDUMP   ( addr n -- )    Dump n bytes from addr in the head segment.
  452.  YDP     ( -- addr )      Pointer to top of head dictionary.
  453.  YHERE   ( -- addr )      Address of top of head dictionary.
  454.  YSEG    ( -- seg )       Segment base of Head Space.
  455.  
  456.  
  457. 7.7.    DOS MEMORY ALLOCATION
  458.  
  459.  
  460. The following words allow you to request, adjust and discard memory
  461. blocks from DOS for your program to use.
  462.  
  463. ALLOC   ( size -- seg2 seg flag )
  464.  
  465. Request size segments of free memory from DOS.  Flag=0 if ok, 8 if not
  466. enough memory.  Seg is the segment base of the allocated memory.  seg2 is
  467. maximum number of segments available that will not cause an error 8,
  468. insufficient memory.
  469.  
  470. DEALLOC ( seg -- flag )
  471.  
  472. Release a block of memory to DOS.  Seg must be the same as the segment
  473. base returned by ALLOC as the base of the allocated memory.  Flag=0 if
  474. ok, 9 if seg is not valid.
  475.  
  476. SETBLOCK        ( seg size --- flag )
  477.  
  478. Re-adjust the memory block specified by seg to a new size in segments.
  479. SEG is the absolute segment address that was returned by ALLOC. 'Size' is
  480. the size of the array in 16 byte units. Typically you would take the BYTE
  481. size needed and use the word SEGMENTS to convert that to the number of
  482. segments needed.
  483.  
  484. These words are useful if you need an extra large space to store large
  485. arrays of data.  F-PC in its default configuration uses about 400K bytes
  486. of memory.  The DOS still has about 200K bytes held in reserve, if you
  487. have not loaded any TSR software programs.  This free memory can be
  488. allocated for your use.  With the extended memory words, you can address
  489. any memory whether DOS allows it or not.  However, if you are building a
  490. system which will coexist with other software, it is prudent to be polite
  491. to Ms. DOS and observe her protocol when it is also convenient to do so.
  492. You make her happy.  She will make you happy.
  493.  
  494.