home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / p / uniforth.lbr / Z80.DZC / Z80.DOC
Encoding:
Text File  |  1993-10-25  |  81.3 KB  |  2,340 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                     Chapter 1
  8.  
  9.                               THE UNIFORTH SAMPLER
  10.  
  11.  
  12.              The  UNIFORTH  Sampler is a subset of the full  Professional 
  13.         UNIFORTH.   It  provides  a  full  assembler,  video  editor  and 
  14.         software  floating-point arithmetic.   It supports  the  FORTH-83 
  15.         Standard  with few exceptions.   You will find the Sampler to  be 
  16.         the equivalent of several commercial systems selling for $200  or 
  17.         more.
  18.  
  19.              We decided to release the Sampler into the public domain for 
  20.         several reasons.   First, distributing a version of UNIFORTH that 
  21.         is  useful,  inexpensive  and  reproducible is  a  cost-effective 
  22.         method  of advertising.   Second,  it gives you a chance to try a 
  23.         good FORTH system at no cost.   If you don't like it, you haven't 
  24.         spent a dime.   If you do like it,  we ask (but don't demand!)  a 
  25.         $25  donation,  most  of  which  will be  reinvested  in  product 
  26.         development  to  improve both the Sampler and  the  Professional.  
  27.         Lastly,  the Sampler includes several novel features that we feel 
  28.         are  useful,   but  want  to  bring  before  the  community   for 
  29.         discussion.
  30.  
  31.              You  are  encouraged to copy and distribute the  Sampler  to 
  32.         your friends and local bulletin boards.   You may use the Sampler 
  33.         as the underlying language for a commercial software product.  In 
  34.         return,  we hope that you will consider us when you need a  full-
  35.         featured FORTH system or other options,  and that you will spread 
  36.         the word about UNIFORTH.
  37.  
  38.              We  can  supply  you  with the latest disk  version  of  the 
  39.         Sampler  in your 5 1/4" or 8" (CP/M) format for $35,  or you  can 
  40.         obtain a copy from a friend or local bulletin board.  Consult the 
  41.         order form at the end for more details.
  42.  
  43.              This  manual is a much-abbreviated edition of  the  UNIFORTH 
  44.         Manual Set.  (The complete set has over 500 pages, and comes in a 
  45.         custom  slipcase/binder.   Copies of the full set are $60.)  This 
  46.         manual  is  divided  into four  sections:   an  introduction  and 
  47.         advertisement  for the Professional;  a brief tutorial on  FORTH; 
  48.         some  of  the  system-specific  features  of  UNIFORTH;   and   a 
  49.         vocabulary list.
  50.  
  51.              The  UNIFORTH  Sampler  is NOT a supported  product  and  is 
  52.         distributed without warranties of Merchantability and fitness for 
  53.         a  particular  purpose.    If  you  need  support,  purchase  the 
  54.         Professional UNIFORTH.
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.          THE UNIFORTH SAMPLER                                PAGE 1-2
  69.  
  70.  
  71.         1.1  Getting Started
  72.  
  73.              The Sampler is distributed on one disk.  You can use the DOS 
  74.         utilities  COPY and DISKCOPY for backup and distribution  copies.  
  75.         You will find on the disk the following files:
  76.  
  77.              UNIFORTH.COM   The executable task image.
  78.  
  79.              FORTH.FTH      The default file that is opened when UNIFORTH 
  80.                             is executed.  It contains the SYSGEN utility, 
  81.                             the segment utility, a game and other stuff.
  82.  
  83.              VEDITOR.FTH    The configuration file for the video editor.
  84.  
  85.              UNIFORTH.DOC   This manual.
  86.  
  87.              README.1ST     If   present,   additional  information  that 
  88.                             should be read before using the system.
  89.  
  90.         All  of  the features of UNIFORTH are preconfigured in  the  task 
  91.         image.   You  need not "load" anything to have a working  system.  
  92.         UNIFORTH.COM  requires a minimum of 40Kb user space,  which means 
  93.         that  you probably will need a 52Kb machine with CP/M  2.2.   The 
  94.         Sampler will ONLY work with a Z80 CPU and CP/M 2.2, and will bomb 
  95.         if  you try it with an 8080 or CP/M 1.4.   We've also found  that 
  96.         the Sampler will not run on an Osborne Executive because of their 
  97.         poor  handling  of the real-time clock,  or on a banked  CP/M-3.0 
  98.         system.
  99.  
  100.              Now start the execution of UNIFORTH by entering
  101.  
  102.                   UNIFORTH
  103.  
  104.         at which time you should get the Sampler startup screen.   If the 
  105.         screen  is  blank or your computer hangs  up,  then  your  system 
  106.         probably  has insufficient memory.   If you get a message to  the 
  107.         effect of a missing file and an inquiry about creating it, answer 
  108.         "N" and leave FORTH at the earliest opportunity.   Then make sure 
  109.         FORTH.FTH is on the same disk as UNIFORTH.COM.
  110.  
  111.              After  you get the "ok" prompt,  you're  into  FORTH.   Then 
  112.         follow  the  tutorial or an introductory text,  and learn  FORTH.  
  113.  
  114.              There are three ways to exit UNIFORTH:  enter two successive 
  115.         control-C  keystrokes (which aborts);  enter the word END  (which 
  116.         has the same effect as the CTRL-C);  or enter BYE (which  flushes 
  117.         and closes files before exiting).
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.                                    Chapter 2
  142.  
  143.                                    USING FORTH
  144.  
  145.  
  146.              The  first  concept  to  understand in using  FORTH  is  the 
  147.         entering  and  execution  of words  through  the  terminal.   The 
  148.         general rules are:
  149.  
  150.  
  151.              1.   FORTH does not interpret a line of user input until the 
  152.                   user  terminates the line by entering a carriage return 
  153.                   (<cr>).
  154.  
  155.              2.   You  may  delete the previous character by  entering  a 
  156.                   backspace (ASCII 08) or delete (ASCII 127).   An  ASCII 
  157.                   "bell" character will be output if you try to backspace 
  158.                   past the beginning of the line.
  159.  
  160.              3.   You may delete an entire line by entering a CTRL-U (the 
  161.                   CTRL and U keys depressed simultaneously).
  162.  
  163.              4.   The user may interrupt printout at any time by entering 
  164.                   CTRL-S.    The  printout  is  resumed  when  CRTL-Q  is 
  165.                   pressed.  (Some systems permit any key for resumption.)
  166.  
  167.              5.   You  can  abort  a task that is  actively  printing  by 
  168.                   pressing the ESCAPE key.
  169.  
  170.         After pressing the return key to terminate a line of input, FORTH 
  171.         will  go  through the line and execute every "word" in the  input 
  172.         line.  The definition of a FORTH word is very simple:
  173.  
  174.                               <<<WORD DEFINITION>>>
  175.  
  176.                        A FORTH word is a sequence of up to
  177.                        64  characters,  with  at least one
  178.                        blank (space) on  each  side.   The
  179.                        sequence  of characters may contain
  180.                        any  character  in  the  ASCII  set
  181.                        except      control     characters.
  182.                        UNIFORTH  retains  the   first   31
  183.                        characters  and  the  length of the
  184.                        word;  any word with the  first  31
  185.                        characters and length identical are
  186.                        not   unique.    All   words   with
  187.                        identical  first  31 characters and
  188.                        lengths greater than or equal to 31
  189.                        characters     are    treated    as
  190.                        identical.
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.         USING FORTH                                           Page 2-2
  201.  
  202.  
  203.  
  204.  
  205.         For example, entering the line
  206.  
  207.                   OK 43 24 + LIST <cr>
  208.  
  209.         will cause FORTH to execute the four "words"
  210.  
  211.                   43         (push the number 43 on the stack)
  212.                   24         (push the number 24 on the stack)
  213.                   +          (add them together to get 67)
  214.                   LIST       (and then list block 67)
  215.  
  216.         Notice the spaces between the words on the input line.  You don't 
  217.         need to type spaces before the first word or after the last  one. 
  218.         The  words are executed in the order in which they were  entered. 
  219.         The actual execution of each word will be discussed later.
  220.  
  221.              If  all goes well and FORTH successfully executes each  word 
  222.         in the input line,  then FORTH will respond with its prompt (OK). 
  223.         If  an error occurs,  the name of the word it was executing  when 
  224.         the error occurred will be repeated, followed by a question mark.
  225.  
  226.  
  227.         2.1  The Stack
  228.  
  229.  
  230.              To  see  how  FORTH does such things as  manipulating  data, 
  231.         controlling  devices,  printing and accepting user's  input,  and 
  232.         managing  files,  you will have to understand the mechanism  that 
  233.         FORTH uses to keep track of the work in progress:  the STACK.
  234.  
  235.              A stack (sometimes called last-in-first-out or LIFO  memory) 
  236.         is  a  device used largely in  compiler  writing,  microprocessor 
  237.         design,  and HP calculators.  A stack can best be compared to the 
  238.         tray holders commonly used in cafeterias:  the type with a spring 
  239.         at  the bottom to push up trays as they are used.   Only the tray 
  240.         most recently added is accessable;  it must be removed before the
  241.         trays below it are available.  
  242.  
  243.              To see how this relates to FORTH, we need to experiment from 
  244.         a terminal.   If you type a carriage return,  FORTH will  respond 
  245.         with
  246.  
  247.                   OK  <cr>
  248.                   OK
  249.  
  250.         to  indicate that it has completed your previous request (in this 
  251.         case,  you requested nothing) and is ready to do  more.   Suppose 
  252.         you type a number:
  253.  
  254.                   OK  5 <cr>
  255.                   OK
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.         USING FORTH                                           Page 2-3
  267.  
  268.  
  269.         FORTH  has apparently done nothing;   however,  it has taken your 
  270.         number  (5),  interpreted it as the numeric value 5 (rather  than 
  271.         some other word), and "pushed" it onto the stack (added a tray to 
  272.         the holder).
  273.  
  274.              Suppose you type a series of numbers,  separated by at least 
  275.         one space so FORTH can tell them apart.   Basic FORTH only under-
  276.         stands 16-bit integers,  i.e.   numbers between -32768 and  32767 
  277.         with  no  decimal  points or other punctuation except  a  leading 
  278.         minus sign for negative numbers:
  279.  
  280.                   OK 4 1 2 7 -23 6 <cr>
  281.  
  282.         (We'll  tell  you  about other number types later.)  FORTH  still 
  283.         doesn't  seem to have done anything,  but the stack now  contains 
  284.         the original number 5,  plus 6 new numbers,  with the "top" value 
  285.         being the number 6.
  286.  
  287.              Obviously something is missing:   a method of removing  data 
  288.         from the stack and doing something useful with it.   The simplest 
  289.         thing  that  can be done with the stack is to see what's  on  top 
  290.         (easiest to get at).   FORTH has a word,  "." (dot),  which takes 
  291.         the  number  on the top of the stack,  prints its  value  on  the 
  292.         terminal,  and  throws the number away.   (We will enclose  FORTH 
  293.         words in quotes whenever confusion is possible).   For an example 
  294.         of dot:
  295.  
  296.                   OK 8 .  <cr>
  297.                   8
  298.                   OK
  299.  
  300.         will take the number 8,  push it on the stack,  then print it and 
  301.         prompt for more input.  Another example,
  302.  
  303.                   OK 8 7 .  CR .  <cr>
  304.                   7
  305.                   8
  306.                   OK 
  307.  
  308.         will type the two top stack values on separate lines.   CR is our 
  309.         second learned FORTH word:  it just outputs a carriage return and 
  310.         line feed to the terminal.
  311.  
  312.  
  313.         2.2  Stack Operators
  314.  
  315.  
  316.              So  far we can only get to the topmost stack value.   As  on 
  317.         the HP calculators,  there are words that allow us to  manipulate 
  318.         the stack to get at other values.   The table below lists some of 
  319.         the  most important operators.   For this and subsequent  tables, 
  320.         the topmost stack value is indicated by A,  the next stack  value 
  321.         by B,  and so on.   The table gives the word, its function, and a 
  322.         representation of the stack before and after the use of the word.
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.         USING FORTH                                           Page 2-4
  333.  
  334.  
  335.  
  336.              WORD          FUNCTION         BEFORE    AFTER
  337.              DUP        copies top of stack    B A     B A A
  338.              SWAP       reverses top two       B A     A B
  339.                         numbers on the stack
  340.              DROP       destroys top of stack  B A     B
  341.              OVER       copies 2nd number and  B A     B A B
  342.                          pushes it on top
  343.              ROT        moves 3rd num to top   C B A   B A C
  344.              -ROT       moves top to 3rd num   C B A   A C B
  345.  
  346.  
  347.              The  usefulness  of these stack manipulation  words  becomes 
  348.         apparent  when  you  consider  the  following  problem:   suppose 
  349.         someone  else  has  put  a number on the stack and  you  wish  to 
  350.         calculate its square without knowing what it is.  You could type
  351.  
  352.              OK  DUP * . <cr>
  353.              25
  354.              OK 
  355.  
  356.         By inference,  the number on the stack was 5.  Of course, we used 
  357.         a  word  not defined yet to multiply the two  values.   So  let's 
  358.         digress and discuss arithmetic functions.
  359.  
  360.  
  361.         2.3  Arithmetic Functions
  362.  
  363.  
  364.              All  arithmetic  functions act on the stack  with  a  method 
  365.         called  postfix or Reverse Polish Notation (RPN),  commonly  used 
  366.         with the HP calculators.  Rather than give extensive examples, we 
  367.         will  assume  that you have used an RPN calculator at some  time, 
  368.         and just give a table of the elementary functions.
  369.  
  370.              WORD       FUNCTION                BEFORE   AFTER
  371.              ABS       absolute value of top       B A    B |A|
  372.                        of stack
  373.              NEGATE    2's complement top          B A    B -A
  374.                        of stack
  375.              +         add top stack values        B A    B+A
  376.              -         sub top stack values        B A    B-A
  377.              *         mul top stack values        B A    B*A 
  378.              /         div top stack values        B A    B/A  
  379.              MOD       remainder of division     C B A    C R
  380.                        of B/A
  381.              MAX       top two numbers dropped,  C B A    C MAX(A,B)
  382.                        larger one put back
  383.              MIN       top two numbers dropped,  C B A    C MIN(A,B)
  384.                        smaller one put back
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.         USING FORTH                                           Page 2-5
  399.  
  400.  
  401.         An  example of the use of some of these functions is to  evaluate 
  402.         the expression
  403.  
  404.                                ABS [(5+4)/2 - 20]
  405.  
  406.         You would enter it with FORTH by
  407.  
  408.                   OK  5 4 + 2 / 20 - ABS . <cr>
  409.                   16
  410.                   OK
  411.  
  412.         Note  that  the division truncates because we are  using  integer 
  413.         arithmetic.
  414.  
  415.  
  416.         2.4  Logical Operators
  417.  
  418.  
  419.              The  logical  operators act upon all 16 of the  bits  in  an 
  420.         integer  and leave results which involve all those  bits.   There 
  421.         are  four basic operators,  three of which require two numbers on 
  422.         the stack and one which acts only on the top value:
  423.  
  424.                   NOT    one's complement of the top value.
  425.                   AND    bitwise "AND" of the top two values.
  426.                   OR     bitwise "OR" of the top two values.
  427.                   XOR    bitwise exclusive OR of the top two values.
  428.  
  429.  
  430.              The use of these definitions is most easily seen by using  a 
  431.         base which is a power of two.  The most common of these are octal 
  432.         and  hexadecimal.   This way there is an easily decoded relation-
  433.         ship  between  the value of a number and the state of  its  indi-
  434.         vidual bits.  Type
  435.  
  436.                   OCTAL <cr>
  437.  
  438.         FORTH is now ready to handle base 8.   Typing HEX would give base 
  439.         16,  and DECIMAL gives base 10.   Using base 8, an example of the 
  440.         logical operators is
  441.  
  442.                   OK 453 321 AND .  <cr>
  443.                   1
  444.                   OK
  445.  
  446.         where  the only bit in common between the octal numbers  453  and 
  447.         321 is the least significant bit.
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.         USING FORTH                                           Page 2-6
  465.  
  466.  
  467.         2.5  Relational Operators
  468.  
  469.  
  470.              Relational  operators  compare  stack  values  and  leave  a 
  471.         logical  truth flag;   ie.,  a number that is either zero or  one 
  472.         depending  on  the tested condition.   All  relational  operators 
  473.         destroy  the stack values they test and just leave the truth flag 
  474.         on  the  stack.   There  are  many  of  these  operators  because 
  475.         condition  testing is one of the most common functions  performed 
  476.         in FORTH.  The table below lists the major relational operators.
  477.  
  478.              WORD                   FUNCTION
  479.                0=       If the top stack value equals zero, leaves a 1
  480.                         (true);  otherwise, leaves a 0 (false).  Also
  481.                         called NOT.
  482.  
  483.                0<       True if stack value is less than 0, false
  484.                         otherwise.
  485.  
  486.                0>       True if stack value is greater than 0, false
  487.                         otherwise.
  488.  
  489.                <        True if the next lower stack value (B) is less
  490.                         than the top stack value (A), false otherwise.
  491.  
  492.                =        True if B is equal to A, false otherwise.
  493.  
  494.                >        True if B is greater than A, false otherwise.
  495.  
  496.  
  497.              An example of the use of some of these relational  operators 
  498.         is to compare
  499.  
  500.                   OK 8 0= 9 10 < = .  <cr>
  501.                   0
  502.                   OK 
  503.  
  504.         The  number 8 is compared to see if it is equal to zero.   It  is 
  505.         not, so this operation leaves a 0 on the stack in place of the 8. 
  506.         Then 9 is tested to see if it is less than 10.   This is true, so 
  507.         a 1 is pushed on the stack.  Then the results of the previous two 
  508.         operations  (0 and 1) are tested to see if they are  equal.   The 
  509.         result is 0, which is then printed.  The stack is now empty.
  510.  
  511.  
  512.         2.6  Forth Memory Allocation
  513.  
  514.  
  515.              Before we go any further in examining FORTH, a little of the 
  516.         nuts  and  bolts  must be presented.   FORTH is  a  core-resident 
  517.         program that occupies approximately 24000 bytes of  memory.   Not 
  518.         all of the allocated memory is filled under normal use;  instead, 
  519.         it  is  partitioned  into subsections that can  expand  with  the 
  520.         increasing load.  The partitions are:
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.         USING FORTH                                           Page 2-7
  531.  
  532.  
  533.  
  534.         I.   THE DICTIONARY.  Nearly all of FORTH resides in the diction-
  535.              ary.   The  dictionary  starts at about address 300  and  is 
  536.              composed of three contiguous sections:
  537.  
  538.              a.   Kernel.   Core locations 300 through 8000 (bytes)  hold 
  539.                   the  FORTH  kernel.   This  is  the  assembly  language 
  540.                   program  that  contains  the 190 most  primitive  FORTH 
  541.                   definitions; somewhat ignorant but easily teachable. 
  542.  
  543.              b.   Basic FORTH.   This section is the SYSTEM  definitions, 
  544.                   some  200  elementary words that later definitions  can 
  545.                   draw on.
  546.  
  547.              c.   Application  FORTH.   In this area are the  definitions 
  548.                   for any program or utility that you load after starting 
  549.                   your session.  This area grows upwards to higher memory 
  550.                   locations.
  551.  
  552.         II.  THE  DATA  STACK.   The data stack contains  such  items  as 
  553.              arguments passed between routines, numbers typed in from the 
  554.              terminal,  system word temporary storage,  etc.,  and is the  
  555.              stack we previously talked about.  This stack is placed near 
  556.              the  top of the FORTH program,  and "grows" downward towards 
  557.              lower memory locations.
  558.  
  559.         III. THE  RETURN STACK.   Less used by the  beginning  programmer 
  560.              (for  good  reason!),  the return stack's primary use is  to 
  561.              contain  addresses  of  the words to  be  executed  when  we 
  562.              "return" from the word being currently executed.  Other uses 
  563.              include  storage for loop parameters,  and numbers that  are 
  564.              "in  the  way" on the data stack.   The return stack  starts 
  565.              near the top of the memory allocation and grows downward  in 
  566.              memory.  It is much smaller than the data stack (only 80 16-
  567.              bit  locations),  so  use  it  sparingly  and  with  careful 
  568.              attention to the protocol.
  569.  
  570.         IV.  THE BLOCK BUFFERS.  UNIFORTH is a disk-based system and uses 
  571.              two  or  more  buffers to bring data to and from  the  disk.  
  572.              Each buffer is nominally 1024 bytes long.
  573.  
  574.         The  addresses  listed above are for a Z80  CP/M  system.   Other 
  575.         systems will have slightly different addresses.
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.         USING FORTH                                           Page 2-8
  597.  
  598.  
  599.              A diagram of how memory is allocated in core is shown below.
  600.  
  601.              high limit     |---------------------------|
  602.                             |    block buffer 2         |
  603.                             |---------------------------|
  604.                             |    block buffer 1         |
  605.                             |---------------------------|
  606.                             | v                         |
  607.                             | v       return stack      |
  608.                             | v                         |
  609.                             |---------------------------|
  610.                             | v                         |
  611.                             | v        data stack       |
  612.                             | v                         |
  613.                         ^   |   .   .   .   .   .   .   |
  614.                         ^   |                           |
  615.                         ^   |                           |
  616.               memory    ^   |   .   .   .   .   .   .   |
  617.              addresses  ^   | ^                         |
  618.                         ^   | ^ user application        |
  619.                         ^   | ^  dictionary             |
  620.                             | ^                         |
  621.                             |---------------------------|
  622.                             |                           |
  623.                             | "standard" FORTH          |
  624.                             |     dictionary            |
  625.                             |                           |
  626.                             |---------------------------|
  627.                             |                           |
  628.                             |    FORTH kernel           |
  629.                             |        dictionary         |
  630.                             |                           |
  631.              low limit      |---------------------------|
  632.  
  633.  
  634.  
  635.         2.7  Pointer Values
  636.  
  637.  
  638.              FORTH  has  four  "pointers,"  most  of  which  are  usually 
  639.         transparent  to the user.   The two stack pointers  have  already 
  640.         been mentioned.   The data stack pointer can be referenced by the 
  641.         word  SP@,  which  indicates the address of the top of the  stack 
  642.         before  SP@ was used.   The return stack pointer's current  value 
  643.         can be obtained by the word RP@.
  644.  
  645.              The dictionary pointer (DP) indicates the first free  memory 
  646.         address  for  the storage of new definitions.   Its value can  be 
  647.         obtained by DP @ and can be changed by ALLOT,  which uses the top 
  648.         stack  value  as the number of bytes by which  to  increment  the 
  649.         pointer.   Further  explanation of the dictionary pointer can  be 
  650.         found in the User's Guide.
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.         USING FORTH                                           Page 2-9
  663.  
  664.  
  665.              The   interpreter  pointer  (IP)  is  an  internal  register 
  666.         containing  the address of the next FORTH "word" to be  executed. 
  667.         It  cannot  be accessed by the programmer  except  with  assembly 
  668.         language  routines.   Some further information concerning the  IP 
  669.         can be found in the User's Guide.
  670.  
  671.  
  672.         2.8  Definitions
  673.  
  674.  
  675.              Now let's get back to our problem of squaring numbers.  What 
  676.         we discussed before leads into the next logical point:   it would 
  677.         be   convenient  to  take  a  series  of  stack  operations   and 
  678.         automatically call in the whole series when needed.  Consider the 
  679.         case of squaring a number, i.e., calculating A*A.
  680.  
  681.              You can define a word to perform this function for you. Pick 
  682.         a name for your word,  for example,  SQUARE.  You can define this 
  683.         word by
  684.  
  685.                   OK : SQUARE DUP * ;  <cr>
  686.  
  687.  
  688.              The  first  thing  on the line is a word which is  just  the 
  689.         single  character  ":" (colon).   Following it (separated  by  at 
  690.         least one space) is the word name you are defining.   Next is the 
  691.         series of words you wish to have executed (DUP *).   Finally, the 
  692.         closing word is a semicolon,  which indicates that the definition 
  693.         of SQUARE is complete.   The word,  once defined, can now be used 
  694.         like any of the well-known words, i.e.
  695.  
  696.                   OK 5 SQUARE . <cr>
  697.                   25
  698.                   OK 3 4 + SQUARE 40 - . <cr>
  699.                   9
  700.                   OK
  701.  
  702.  
  703.              Before  proceeding,  you  should stop and consider  what  is 
  704.         happening  in  more detail.   The process of putting a series  of 
  705.         words  together  in  a so-called  "colon  definition"  is  called 
  706.         "compilation".   When  FORTH  is  processing the words  inside  a 
  707.         definition,  it  does almost all the work needed to perform  your 
  708.         function.   When the word is used, FORTH can very rapidly perform 
  709.         the function you have designed.
  710.  
  711.              The  colon  starts  the compilation  process  by  using  the 
  712.         following  character  string  as  the "name"  of  what  is  being 
  713.         compiled.   The name is stored at the beginning of the definition 
  714.         in  the  "header section" as a count byte followed by  the  ASCII 
  715.         characters  of  the name.   The address of an  assembly  language 
  716.         routine  is next (the "code section") and then the information to 
  717.         be processed (the "parameter section"),  such as the addresses of 
  718.         the FORTH words to be included in your definition.   Finally, the 
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.         USING FORTH                                           Page 2-10
  729.  
  730.  
  731.         semicolon turns off the compilation process and gets you back  to 
  732.         the "interactive execution" mode you started with.  If you forget 
  733.         the semicolon, any words you type after it will be thrown in with 
  734.         the colon definition and not executed immediately.
  735.  
  736.              Remember  that the only restriction on names is that control 
  737.         characters  are  not allowed.   There is nothing sacred  about  a 
  738.         "number" such as "7".  If you want to be different you can define
  739.  
  740.                   :  7 5 ;  
  741.  
  742.         and obtain such bizarre results as
  743.  
  744.                   OK 7 3 + .  <cr>
  745.                   8
  746.                   OK
  747.  
  748.  
  749.              The next step in defining words is to realize that there  is 
  750.         no   distinction   between  "FORTH  words"  and   "your   words". 
  751.         Previously, you defined the word
  752.  
  753.                   :  SQUARE DUP * ;  
  754.  
  755.         Using  this word you can define a routine to get the fifth  power 
  756.         of numbers like this:
  757.  
  758.                   :  **5 DUP SQUARE SQUARE * ;  
  759.  
  760.  
  761.              Now suppose you choose to redefine SQUARE.  You could choose 
  762.         to do this by typing
  763.  
  764.                   OK :  SQUARE DROP 5 .  ;  <cr> SQUARE Redefined 
  765.                   OK 7 SQUARE <cr>
  766.                   5
  767.                   OK
  768.  
  769.         Note the "Redefined" informative message that you get.   However, 
  770.         the  new  definition  of SQUARE will not affect its  use  in  the 
  771.         previously defined word, **5.
  772.  
  773.                   OK 2 **5 .  <cr>
  774.                   32
  775.                   OK
  776.  
  777.         But  if  **5 is now redefined,  the new definition of  SQUARE  is 
  778.         used.
  779.  
  780.                   OK :  **5 DUP SQUARE SQUARE * ;  <cr> **5 Redefined 
  781.                   OK 2 **5 .  <cr>
  782.                   5 5 2
  783.                   OK
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.         USING FORTH                                           Page 2-11
  795.  
  796.  
  797.  
  798.              The behavior is governed by these two rules:
  799.  
  800.              1.   When  you  define  a word,  all words used  within  the 
  801.                   definition must have been previously defined.
  802.  
  803.              2.   Later  changing a word you use within definitions  will 
  804.                   not  affect  words you have  already  defined  -- their 
  805.                   function  will  remain unchanged until you enter a  new 
  806.                   definition for the word.
  807.  
  808.  
  809.              Notice  that only the most recent dictionary  definition  of 
  810.         words used within a colon definition will be used.   For example, 
  811.         in   subsequent  use,   the  correct  definition  for  SQUARE  is 
  812.         inaccessible  because another definition for SQUARE  was  created 
  813.         later.   There  is  a  way of "forgetting" the later  version  of 
  814.         SQUARE.  The FORTH word FORGET, used like
  815.  
  816.                   OK FORGET SQUARE <cr>
  817.                   OK
  818.  
  819.         will erase from the dictionary the most recent definition of
  820.         SQUARE and all definitions made after it.
  821.  
  822.              We  have mentioned colon definitions.   You can also  define 
  823.         machine  language words with the word CODE,  and terminate  their 
  824.         definition with the word END-CODE,  in a similar manner as  colon 
  825.         and semicolon.  We will mention machine language routines in more 
  826.         detail in a subsequent chapter.
  827.  
  828.              One  other item before we leave the subject of  definitions. 
  829.         Besides using RPN representation, FORTH is decidedly not FORTRAN-
  830.         like   in  its  definitions.    Though  these  act  exactly  like 
  831.         subroutines, they differ in that most arguments are passed on the 
  832.         data   stack,   and  that  the  entire  definition   is   written 
  833.         horizontally.   While  you can write definitions with each called 
  834.         word  on a separate line,  it is not commonly done.   This can be 
  835.         confusing to the beginning programmer,  but think of it as if you 
  836.         were entering the line interactively.
  837.  
  838.  
  839.         2.9  Constants and Variables
  840.  
  841.  
  842.         FORTH  has several words for reading from and writing to  memory. 
  843.         To  read  from memory,  an address must be specified.   Then  "@" 
  844.         (pronounced "fetch") takes that address,  reads the 16-bit  value 
  845.         starting at that address,  and places it on the stack.   The word 
  846.         C@ accomplishes a similar task, except that it retrieves only the 
  847.         byte stored at the address, places it in the low byte of a 16-bit 
  848.         location or cell which is then placed on the stack.
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.         USING FORTH                                           Page 2-12
  861.  
  862.  
  863.                               <<<CELL DEFINITION>>>
  864.  
  865.                        A cell is a 16-bit memory location.
  866.                        The low order byte is stored at the
  867.                        first byte address;  the high order
  868.                        byte   is   stored   at   the  next
  869.                        consecutive byte address.
  870.  
  871.  
  872.         The  procedure  of  reading (or fetching as it is  more  commonly 
  873.         called)  and  then printing is used  often.   Therefore,  a  word 
  874.         exists that combines both of these operations--the question  mark 
  875.         (?).  It fetches an integer from a given address and prints it. 
  876.  
  877.              Two words exist to write into a specific memory location. As 
  878.         for  reading,  the  exclamation point ("!",  pronounced  "store") 
  879.         stores a 16-bit value and C!   stores the low-order byte of a 16-
  880.         bit  value from the top of the stack.   In both cases,  the  data 
  881.         value and the address are removed from the stack.  For example, 
  882.  
  883.                                   16432 18500 !
  884.  
  885.         stores the values 16432 at address 18500.
  886.  
  887.              Also in the context of definitions, you can define constants 
  888.         and variables to be used in your programs.  Numeric constants can 
  889.         be defined with the word CONSTANT.  For example,
  890.  
  891.                   31415 CONSTANT PI-TIMES-10000
  892.  
  893.         defines  the FORTH word PI-TIMES-10000.   Whenever you type  this 
  894.         word, the constant 31415 will be pushed on the stack.
  895.  
  896.              Often  you find that it is awkward to have all your data  on 
  897.         the  stack at once.   You can store data in single  named  memory 
  898.         locations with the FORTH word VARIABLE.  For example,
  899.  
  900.                   VARIABLE Q
  901.  
  902.         defines the word Q.   When you type Q, the address of the storage 
  903.         location corresponding to Q is pushed onto the stack.  You cannot 
  904.         preset  this storage location during the definition as you  could 
  905.         with  CONSTANT.   You can use Q in conjunction with @ and  !   to 
  906.         change  the  contents  during program  execution  (for  instance, 
  907.         updating a counter).   There is a special word called tick,  "'", 
  908.         which  allows  the  changing of the value of  a  CONSTANT.   Tick 
  909.         places the address of a word's parameter field (where the  number 
  910.         is  stored for CONSTANT) on the stack.   You can then change  the 
  911.         value using "store".  For example,
  912.  
  913.                   31416 ' PI-TIMES-10000 !
  914.  
  915.         changes PI-TIMES-10000 from 31415 to 31416.
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.         USING FORTH                                           Page 2-13
  927.  
  928.  
  929.  
  930.         2.10  Typing
  931.  
  932.  
  933.              One of the problems with FORTH as far as users are concerned 
  934.         is that there is no typing of variables.   That is, once you have 
  935.         defined  variable XXX,  the system doesn't know later whether XXX 
  936.         is an integer,  floating point number, or what.  The advantage of 
  937.         this  is  that  less  storage  space  and  compiler  overhead  is 
  938.         necessary,  and  as long as you are dealing with  characters  and 
  939.         integers, the inconvenience is small.
  940.  
  941.              The  disadvantage  comes into play when FORTH  becomes  more 
  942.         complicated,  and  you  start implementing types such  as  double 
  943.         precision integer,  string variables,  etc.  Because of this lack 
  944.         of typing, you cannot perform mixed-mode arithmetic automatically 
  945.         using a precedence scheme such as in FORTRAN.
  946.  
  947.              Therefore, when using FORTH, you must keep the type of every 
  948.         variable  and  stack  member in mind,  and  perform  the  correct 
  949.         operation on the numbers.  For example:
  950.  
  951.                 +      Add the two top 16-bit stack values, assuming they
  952.                        are single precision integers, and leave a 16-bit
  953.                        result.
  954.  
  955.                 D+     Add the four top 16-bit stack values, assuming
  956.                        they are two double precision (32-bit) integers,
  957.                        and leave a 32-bit result.
  958.  
  959.                 F+     Add the four top 16-bit stack values, assuming
  960.                        they are two single precision (32-bit) floating
  961.                        point values, and leave a 32-bit floating point
  962.                        result.
  963.  
  964.                 SS*D   Multiply the two top 16-bit stack values assuming
  965.                        they are single precision integers, and leave a
  966.                        32-bit double precision integer result.
  967.  
  968.         In  other  words,  some  of the work has been taken  out  of  the 
  969.         compiler  or language and placed on the  programmer's  shoulders. 
  970.         There  is  no  inherent  reason why  a  typed-FORTH  couldn't  be 
  971.         created;  there just has been no emphasis in this direction.
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.         USING FORTH                                           Page 2-14
  993.  
  994.  
  995.         2.11  Control Structures
  996.  
  997.  
  998.              Control   structures  allow  you  to  modify  program  flow, 
  999.         primarily by making decisions and causing various definitions  to 
  1000.         execute within a colon definition.  
  1001.  
  1002.  
  1003.         2.11.1  Looping
  1004.  
  1005.  
  1006.              Maintaining an iteration counter and causing a branch out of 
  1007.         the  loop  when a preset value is reached is a  very  fundamental 
  1008.         software feature.  FORTH provides the DO and LOOP words for this.
  1009.  
  1010.              When making a loop,  you usually know the starting value and 
  1011.         the number of repetitions desired.   When you sum these,  you get 
  1012.         the total.   DO expects to find two parameters on the stack,  the 
  1013.         total and the starting value:
  1014.  
  1015.              [total] [start] DO [things to be done] LOOP
  1016.  
  1017.  
  1018.              As an example, consider a definition to print out 5 numbers.  
  1019.         If we start with 1, then the total is 6, and our definition is:
  1020.  
  1021.                   :  PRINT_FIVE 6 1 DO .  LOOP ;  <cr>
  1022.  
  1023.         This definition expects to find 5 numbers on the stack.
  1024.  
  1025.                   OK 34 5 2 6 -3 PRINT_FIVE <cr>
  1026.                   -3 6 2 5 34
  1027.                   OK
  1028.  
  1029.              Other  languages use the starting value and the ending value 
  1030.         as the loop limits,  and switch the order of the  limits.   These 
  1031.         variances are  matters of preference.
  1032.  
  1033.              Often  it  is useful to have a word to access the  iteration 
  1034.         counter, and FORTH provides several of them as indicated below.
  1035.  
  1036.              WORD                    FUNCTION
  1037.              I           Push the value of the iteration counter of
  1038.                           the innermost loop onto the stack.
  1039.  
  1040.              J           Push the value of the next outermost loop
  1041.                           counter onto the stack (i.e.,2 nested 
  1042.                           loops).
  1043.  
  1044.              I-          Subtract the value of the innermost counter
  1045.                          from the value on the stack.
  1046.  
  1047.              I+          Add the value of the innermost counter to
  1048.                           the value on the stack.
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.         USING FORTH                                           Page 2-15
  1059.  
  1060.  
  1061.  
  1062.  
  1063.              Consider a loop to print the numbers from 1 to 10:
  1064.  
  1065.                   OK :  10_#'S 11 1 DO I .  LOOP ; <cr>
  1066.  
  1067.         When executed, 10_#'S will give
  1068.  
  1069.                   OK 10_#'S <cr>
  1070.                   1 2 3 4 5 6 7 8 9 10
  1071.                   OK
  1072.  
  1073.  
  1074.              Since  DO  expects to find the iteration parameters  on  the 
  1075.         stack at execution time,  you can wait until then to define them. 
  1076.         A general, ascending order, number printing routine might be:
  1077.  
  1078.                   OK :  GEN_ASC_ORD DO I .  LOOP ; <cr>
  1079.  
  1080.         When executed, this word expects to find the DO parameters on the 
  1081.         stack:
  1082.  
  1083.                   OK 11 1 GEN_ASC_ORD <cr>
  1084.                   1 2 3 4 5 6 7 8 9 10
  1085.                   OK
  1086.  
  1087.              Since  one  normally thinks of these parameters  as  [start] 
  1088.         [finish] rather than the order that DO prefers, and further since 
  1089.         [finish] is often preferred over [finish+1], try this:
  1090.  
  1091.                   :  LIMITS 1+ SWAP ;  
  1092.  
  1093.         The  1+ adds 1 to [finish] and SWAP puts [start] and [finish]  in 
  1094.         the correct order for a DO.   In other words,  you have created a 
  1095.         "preprocessor"  for  DO-LOOPS.   This is rarely  done  with  most 
  1096.         languages,  but  if  you  expect  to use this  a  few  times  the 
  1097.         convenience pays off in FORTH.
  1098.  
  1099.                   OK :  GEN_#_PRINT LIMITS DO I .  LOOP ; <cr>
  1100.  
  1101.         In use, this gives:
  1102.  
  1103.                   OK 1 5 GEN_#_PRINT <cr>
  1104.                   1 2 3 4 5 
  1105.                   OK
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.         USING FORTH                                           Page 2-16
  1125.  
  1126.  
  1127.         2.11.2  Conditional branching
  1128.  
  1129.  
  1130.              Conditionals traditionally allow some value to be tested and 
  1131.         a  branch to occur based upon the test.   FORTH  allows  one- and 
  1132.         two-sided conditional branches.
  1133.  
  1134.              The  IF word and the THEN word are the simplest  cases.   IF 
  1135.         will  cause  the  definitions  between it  and  THEN  to  execute 
  1136.         provided  the top stack value (TOS) is logical true  (or  really, 
  1137.         not  equal  to zero).   If the TOS is zero,  FORTH will  skip  to 
  1138.         execute the words following the THEN word.  For example,
  1139.  
  1140.                   OK :  USE_IF IF 1 .  THEN 2 .  ;  <cr>
  1141.  
  1142.         When executed, this definition will give
  1143.  
  1144.                   OK 1 USE_IF <cr>
  1145.                   1 2
  1146.                   OK 0 USE_IF <cr>
  1147.                   2
  1148.                   OK
  1149.  
  1150.         IF always removes the logical quantity it tests from the stack.
  1151.  
  1152.              Another word,  ELSE, allows one of two branches to be taken. 
  1153.         As an example:
  1154.  
  1155.                   OK :  USE_ELSE IF 1 .  ELSE 0 .  THEN ;  <cr>
  1156.  
  1157.              If 0 is on the stack,  the ELSE-clause will be executed;  if 
  1158.         a  number other than 0 is on the stack,  the TRUE-clause will  be 
  1159.         executed, as in
  1160.  
  1161.                   OK 0 USE_ELSE <cr>
  1162.                   0
  1163.                   OK 4 USE_ELSE <cr>
  1164.                   1
  1165.                   OK
  1166.  
  1167.         The ELSE portion executes if TOS is false, or 0.  In either case, 
  1168.         the  definition after THEN executes.   In the preceding  example, 
  1169.         the compiler operator ";" will execute.   It will signal both the 
  1170.         end of the definition at compile time, and perform house-cleaning 
  1171.         at the end of the definition at execute time.
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.         USING FORTH                                           Page 2-17
  1191.  
  1192.  
  1193.         2.11.3  Block structure branching
  1194.  
  1195.  
  1196.              FORTH also provides a block structure which is essentially a 
  1197.         loop without an iteration counter.  It is of the form:
  1198.  
  1199.                   BEGIN [good stuff] [flag] UNTIL
  1200.  
  1201.              BEGIN  is  a  place holder and marks the  beginning  of  the 
  1202.         block.   UNTIL tests for a true or false TOS, and if false trans-
  1203.         fers  back to the word following BEGIN.   If true,  the word fol-
  1204.         lowing UNTIL will execute.   UNTIL will remove its logical  quan-
  1205.         tity from the stack.
  1206.  
  1207.  
  1208.         2.12  And so Forth....
  1209.  
  1210.  
  1211.              You  now have seen most of the basics of FORTH,  along  with 
  1212.         some  examples.   The best way to learn more at this stage is  to 
  1213.         sit at a terminal and practice!
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260.  
  1261.                                     Chapter 3
  1262.  
  1263.                            UNIQUE FEATURES OF UNIFORTH
  1264.  
  1265.  
  1266.                 As  mentioned  earlier,  the Sampler is a subset  of  our 
  1267.         commercial FORTH.  Even so, it has many additions to the FORTH-83 
  1268.         standard.  This chapter will mention a few of those features.  We 
  1269.         recommend  the  purchase  of  the UNIFORTH Manual  Set  for  more 
  1270.         information.
  1271.  
  1272.  
  1273.         3.1  Files
  1274.  
  1275.  
  1276.              UNIFORTH uses DOS 2 tree-structured files.   In other words, 
  1277.         you can access files in any directory on any disk from inside  of 
  1278.         UNIFORTH.  The limitation is that the file pathname cannot exceed 
  1279.         63 characters.
  1280.  
  1281.              When  first  invoking  UNIFORTH,  you  have  the  option  of 
  1282.         including  a  file  name on the same line.   If no  name  follows 
  1283.         UNIFORTH,  then file FORTH.FTH will be  opened.   Otherwise,  the 
  1284.         passed file will be opened.  For example,
  1285.  
  1286.                        UNIFORTH TEST.BIN
  1287.  
  1288.         will  open file TEST.BIN for access after the coldstart procedure 
  1289.         is completed.
  1290.  
  1291.              All UNIFORTH source code filenames have an extension of .FTH 
  1292.         for unique identification.
  1293.  
  1294.              The following words give the basic file access functions:
  1295.  
  1296.              CHANA,CHANB
  1297.                   There  are  two default "channels" for file  access  in 
  1298.                   UNIFORTH,  selected by the words CHANA and CHANB.  When 
  1299.                   first  invoking UNIFORTH,  CHANA is assumed.   After  a 
  1300.                   channel has been selected, all file read/write commands 
  1301.                   are  directed  to that channel.   If  you  now  specify 
  1302.                   CHANB,  all  commands are directed to the file open  on 
  1303.                   that channel.
  1304.  
  1305.              MAKE-CHAN <name>
  1306.                   If  you  want more files to be open  concurrently,  you 
  1307.                   might  want to add channels.   This word will create  a 
  1308.                   new word like CHANA that,  when executed, switches file 
  1309.                   commands  to a new channel.   Each channel occupies  64 
  1310.                   bytes  of  dictionary  space in addition  to  the  word 
  1311.                   header.
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.         UNIQUE FEATURES OF UNIFORTH                        Page 3-2
  1323.  
  1324.  
  1325.              OPEN <fname>
  1326.                   Open the file.   If the file doesn't exist, you will be 
  1327.                   prompted for its creation.   If you decline to create a 
  1328.                   new file, the previous file will be reopened.
  1329.  
  1330.              CLOSE
  1331.                   Close  the file on the current channel.   No action  is 
  1332.                   taken if a file is not currently open.
  1333.  
  1334.              MAKE <fname>
  1335.                   You can explicitly create a file with this word.   MAKE 
  1336.                   and OPEN only create the DOS directory entry;  the file 
  1337.                   has  no length until you explicitly write data  to  it.  
  1338.                   Also,  until you close the file,  DOS does not know the 
  1339.                   file's length.
  1340.  
  1341.              DELETE <fname>
  1342.                   Deletes the file.
  1343.  
  1344.              FILESTATUS
  1345.                   Displays information about the currently open file.
  1346.  
  1347.              Once opened, a file can be accessed in one of two ways:
  1348.  
  1349.              (1)  As  a block-oriented (FORTH) file.   You read/write  to 
  1350.                   the file using 1024-byte random-access blocks of  data.  
  1351.                   Common words are BLOCK, BUFFER, UPDATE, LOAD, etc.
  1352.  
  1353.              (2)  As a sequential, text-oriented file.  You read/write to 
  1354.                   the file in bytes or lines,  where a line is terminated 
  1355.                   with  a  CR/LF pair of ASCII characters.   This is  the 
  1356.                   kind of file created by ED or WORDSTAR.
  1357.  
  1358.              Block-oriented  files are the most straightforward.   All of 
  1359.         the  FORTH-83  read/write/load words work on  block  files.   The 
  1360.         limitations are that you will get an error message if you try  to 
  1361.         access data beyond the end of the file,  and that you cannot load 
  1362.         block  0  of any file.   You can also open and read binary  files 
  1363.         such as UNIFORTH.COM with BLOCK, and dump their contents.
  1364.  
  1365.              A blank file can be created,  or a current file extended  in 
  1366.         length,  using  the word EXTEND.   It has the number of blocks to 
  1367.         extend as its argument.   Each added block is blank-filled with a 
  1368.         ";S" at the beginning.
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.         UNIQUE FEATURES OF UNIFORTH                        Page 3-3
  1389.  
  1390.  
  1391.              You can move blocks from one file to another with a brace of 
  1392.         words:
  1393.  
  1394.              n1 n2 COPYATOB
  1395.                   Copy  block n1 of the file currently open on channel  A 
  1396.                   into block n2 of channel B.
  1397.  
  1398.  
  1399.              n1 n2 #blks MCOPY
  1400.                   Copy #blks starting at block n1 in channel A to channel 
  1401.                   B, starting at block n2.
  1402.  
  1403.              Text files are read byte-by-byte.   You should not use BLOCK 
  1404.         on a text file, as there is no guarantee that the file will be an 
  1405.         exact multiple of 1024 bytes in length (meaning you won't be able 
  1406.         to access the last part of the file).  Instead, there is a set of 
  1407.         words to perform the major read/write operations for you.
  1408.  
  1409.              RDBYTE char
  1410.                   Read one byte from the current file.  If the end of the 
  1411.                   file  is  reached,  leave a true flag in place  of  the 
  1412.                   character.
  1413.  
  1414.              adr cnt RDLINE nchar
  1415.                   Read a "line" from the file into a buffer.   The number 
  1416.                   of  characters actually read (nchar) will be less  than 
  1417.                   cnt if a CR/LF is detected.
  1418.  
  1419.              char WRBYTE
  1420.                   Write the character to the current file.
  1421.  
  1422.              adr cnt WRLINE
  1423.                   Write  cnt characters to the current  file.   Append  a 
  1424.                   CR/LF pair to terminate the line.
  1425.  
  1426.              WREOF
  1427.                   Write an end-of-file mark on the current file.
  1428.  
  1429.              blk byte !FILEPOS 
  1430.                   A   method  of  random-access  on  a  sequential  file.  
  1431.                   Position  the next read/write to byte pointed to by the 
  1432.                   block # and byte offset.
  1433.  
  1434.              @FILEPOS blk byte
  1435.                   Returns  the current file pointer.
  1436.  
  1437.              You  can  also  load source code  from  a  text  file.   For 
  1438.         example,  you might like to use FRED as a basic text editor,  and 
  1439.         feel  restricted by the 12x64 format of a FORTH block.   You  can 
  1440.         create a text file and load the entire file:
  1441.  
  1442.              TLOAD
  1443.                   Load  the  currently  open text file  starting  at  the 
  1444.                   current file pointer.  Each line of text is interpreted 
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.         UNIQUE FEATURES OF UNIFORTH                        Page 3-4
  1455.  
  1456.  
  1457.                   just  as if you had entered it from the  console.   You 
  1458.                   must terminate the file with a ;S.
  1459.  
  1460.         Loading  from  a  text  file requires a  minimum  of  three  disk 
  1461.         buffers, so check #BUFF before you attempt a load.
  1462.  
  1463.  
  1464.         3.2  Vectored I/O
  1465.  
  1466.  
  1467.              UNIFORTH  permits up to three input devices and five  output 
  1468.         devices to be used for console I/O.  These devices are controlled 
  1469.         by  the low-order byte of variable IOBYTE.   Each bit is assigned 
  1470.         to  a  particular  input  or  output  device.    By  setting  the 
  1471.         appropriate bit,  you can send output to 5 devices simultaneously 
  1472.         or select an alternate keyboard.
  1473.  
  1474.              A  32-byte table is located at JMPTAB.   The bits in  IOBYTE 
  1475.         select from these 8 addresses to execute the proper routine.  The 
  1476.         bit definitions are:
  1477.  
  1478.                   bit#      JMPTAB+...      routine
  1479.                   0         0              console input
  1480.                   1         3              auxillary input
  1481.                   2         9              user specified input routine
  1482.                   3         12             user specified output routine
  1483.                   4         15             auxillary output
  1484.                   5         18             disk output
  1485.                   6         21             printer output
  1486.                   7         24             console output
  1487.  
  1488.                             27             console status
  1489.                             30             auxillary status
  1490.                             36             user specified status
  1491.  
  1492.         For  example,  to select console input and  printer  output,  you 
  1493.         would  set  IOBYTE to 82H.   You can insert your own routines  in 
  1494.         this table, with restrictions.  Check the Professional Manual Set 
  1495.         for more information.
  1496.  
  1497.              We've defined a set of words to account for the most  common 
  1498.         combinations:
  1499.  
  1500.                   TERMINAL       select PC input and output
  1501.                   PRINTER        select printer output only
  1502.                   +PRINTER       add printer output to current selection
  1503.                   +DISK          add disk output to current selection
  1504.  
  1505.         The  disk output is very restricted.   You must have a text  file 
  1506.         open  on  channel B.   All terminal output is then stored on  the 
  1507.         text file (a log file if you will).
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.         UNIQUE FEATURES OF UNIFORTH                        Page 3-5
  1521.  
  1522.  
  1523.         3.3  Numeric input
  1524.  
  1525.  
  1526.              The  UNIFORTH  Sampler  supports  three  basic  data  types:  
  1527.         single  precision (16-bit) integers,  double  precision  (32-bit) 
  1528.         integers, and single precision (32-bit) floating point.
  1529.  
  1530.              When  entering  a number from the keyboard,  you  must  tell 
  1531.         FORTH  what  type  of  number it is  to  convert.   Since  16-bit 
  1532.         integers are the default,  you need only enter the digits with an 
  1533.         optional  +  or - sign at the beginning.   For  32-bit  integers, 
  1534.         include a comma anywhere within the number.   For floating point, 
  1535.         include a period anywhere within the number, with an optional "E" 
  1536.         style exponent.  Examples:
  1537.  
  1538.                   string         converted as:
  1539.                   235            16-bit integer 
  1540.                   23,5           32-bit integer
  1541.                   235.0          floating point
  1542.                   2.35E2         floating point
  1543.  
  1544.         All values convert to 235.   Variable PRECIS tells you what  kind 
  1545.         of  number  was  last  converted  (-1=none,  0=16-bit,  1=32-bit, 
  1546.         2=floating point),  and variable DPL locates the decimal point or 
  1547.         comma within the number.
  1548.  
  1549.  
  1550.         3.4  Video editor
  1551.  
  1552.  
  1553.              We've included our "slow" video editor as part of the  basic 
  1554.         system.  It comes preconfigured for an ADM3/5, and uses only BIOS 
  1555.         calls to perform its duties.  We suggest that you try this editor 
  1556.         as-is before attempting any modifications.   At worst, the screen 
  1557.         will  be  filled with garbage characters,  and you can  exit  the 
  1558.         editor  at  any time with ctrl-X without any modifications  being 
  1559.         written to disk (or ESC if changes are to be written).
  1560.  
  1561.              To enter the editor,  just use "n EDIT" where n is the block 
  1562.         number to be edited.   If you were attempting to load a block and 
  1563.         ran  into  an error,  and received the  block/line  number  error 
  1564.         message,  the word WHERE (no arguments) will enact the editor and 
  1565.         display  the  erroneous  code.
  1566.  
  1567.              Once  inside  the  editor,  the commands  are  pretty  self-
  1568.         explanatory.  All commands are mnemonic related and are displayed 
  1569.         at the top of the screen.   The line stack is a temporary holding 
  1570.         area  that  can  be  used to transfer lines  from  one  block  to 
  1571.         another.   Jump  block will position the cursor at the  displayed 
  1572.         block number; overtype it with the block you wish to move to.
  1573.  
  1574.  
  1575.  
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.         UNIQUE FEATURES OF UNIFORTH                        Page 3-6
  1587.  
  1588.  
  1589.              If   you  wish  to  customize  the  editor,   look  at  file 
  1590.         VEDITOR.FTH.   It  contains configuration blocks for three  other 
  1591.         popular terminals:   the TV-912,  the H-19 and the  VT-100.   The 
  1592.         UNIFORTH  Professional  version includes about 10  more  terminal 
  1593.         types.  However, this basic selection will cover 95% of all clone 
  1594.         cases.   Find  the  terminal type that most closely matches  your 
  1595.         system  and  load  the  appropriate  block.   This  modifies  the 
  1596.         vectored editor words that are terminal specific.
  1597.  
  1598.              In  addition,  all control codes are defined  as  constants.  
  1599.         You  may  wish  to use another key (such as  a  WordStar  layout) 
  1600.         instead  of  what we've selected.   Just modify  the  appropriate 
  1601.         parameter   and   menu  item  from  VEDITOR.FTH  and   load   the 
  1602.         configuration block.
  1603.  
  1604.              When you've made all of the necessary changes, resysgen your 
  1605.         system to save the modifications.
  1606.  
  1607.  
  1608.         3.5  SYSGEN
  1609.  
  1610.  
  1611.              UNIFORTH permits the storage of a precompiled system,  to be 
  1612.         later executed in a similar manner as UNIFORTH.COM.   You may add 
  1613.         anything  to the system that you want;  make patches to the basic 
  1614.         system;  delete  earlier utilities like the  high-level  floating 
  1615.         point  functions;  and then save the new task image onto  another 
  1616.         file.
  1617.  
  1618.              The SYSGEN utility is in file FORTH.FTH.   You will find two 
  1619.         words most useful:
  1620.  
  1621.              REALLOCATE
  1622.                   This  word  prompts you for two pieces of  information:  
  1623.                   the  number of disk buffers and the  memory  size.   We 
  1624.                   recommend  3  buffers.   If you have more than 64Kb  of 
  1625.                   user program space on your system (that is,  more  than 
  1626.                   about 96Kb of total memory),  you may be tempted to use 
  1627.                   a  number  larger than 64 when asked for  memory  size.  
  1628.                   The  UNIFORTH  Sampler is limited to 64K,  and so  will 
  1629.                   truncate  any  answer  you give  to  fit  within  those 
  1630.                   constraints.
  1631.  
  1632.              SYSGEN
  1633.                   The main word.   After you have used reallocate, sysgen 
  1634.                   will  save  the compiled image onto a task  file.   You 
  1635.                   specify the file name.
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.         UNIQUE FEATURES OF UNIFORTH                        Page 3-7
  1653.  
  1654.  
  1655.              SCRAMBLE
  1656.                   If you wish to distribute a commercial application  and 
  1657.                   don't  want  the user to have any access to  FORTH,  we 
  1658.                   recommend  the  use of the  word  SCRAMBLE.   It  first 
  1659.                   destroys  the word links,  then executes  SYSGEN.   The 
  1660.                   resultant  compiled task image will not be able to find 
  1661.                   enough words in the dictionary to make effective use of 
  1662.                   FORTH,  yet your later application words will still  be 
  1663.                   available.
  1664.  
  1665.  
  1666.         3.6  Assembler
  1667.  
  1668.  
  1669.              Included on the basic library disk is an assembler that uses 
  1670.         the Z-80 mnemonics.  This assembler is essential in defining some 
  1671.         of  the  basic  words of the system,  and uses some 3K  bytes  of 
  1672.         storage.
  1673.  
  1674.              You have full access to this assembler for your own use and, 
  1675.         in fact, are highly encouraged to use it.  FORTH machine language 
  1676.         definitions are extremely easy to create and debug, compared with 
  1677.         other  languages.   It  is ideal for learning how to  program  in 
  1678.         machine language, or to test out various algorithms before incor-
  1679.         porating them in other programs.
  1680.  
  1681.              Machine language FORTH words are created by using one of the 
  1682.         defining words CODE or SUBROUTINE.  In each case, the interpreta-
  1683.         tion state is changed from compilation (i.e.,storing the  addres-
  1684.         ses  of routines into a definition) to execution (executing  each 
  1685.         word as it is encountered).  The words executed are Z-80 opcodes, 
  1686.         which  use stack parameters to create Z-80 instructions which are 
  1687.         then stored into the definition.
  1688.  
  1689.  
  1690.         3.6.1  Creation of Code Words
  1691.  
  1692.  
  1693.              To create a machine-language word callable from other  FORTH 
  1694.         words,  use CODE,  just like you would the defining word "colon". 
  1695.         For example,
  1696.  
  1697.         CODE DUP  HL POP,  HL PUSH,  HL PUSH,  NEXT,  END-CODE
  1698.  
  1699.         which  creates  a  code word called DUP that pops the  top  stack 
  1700.         value  into the register pair HL,  then pushes it back on  twice. 
  1701.         The last statement in a CODE definition must be  END-CODE,  which 
  1702.         cleans up the stack and returns the user to the FORTH vocabulary.
  1703.  
  1704.              The word NEXT, is one of the three required ways of termina-
  1705.         ting the actual word definition if the new word is to be callable 
  1706.         from other FORTH words.  The three ways are:
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.         UNIQUE FEATURES OF UNIFORTH                        Page 3-8
  1719.  
  1720.  
  1721.                   NEXT,     Jump  to the interpreter to pick up the  next 
  1722.                             word to execute.
  1723.  
  1724.                   HPUSH,    Push  the  contents of HL onto the stack  and 
  1725.                             then jump to the interpreter (go to NEXT).
  1726.  
  1727.                   DPUSH,    Push the contents of DE onto the stack,  then 
  1728.                             push HL, and finally go to NEXT.
  1729.  
  1730.  
  1731.              A  defining  word called SUBROUTINE is available  to  create 
  1732.         machine-language  words  to  be used only  within  other  machine 
  1733.         language words (using CALLs or JPs).  For example,
  1734.  
  1735.         SUBROUTINE 4*  HL HL ADD,  HL HL ADD,  RET,  END-CODE
  1736.  
  1737.         creates  the  word 4* that will multiply HL by 4.   To  use  this 
  1738.         word:
  1739.  
  1740.         CODE 64*  HL POP,  4* CALL,  4* CALL,  4* CALL,
  1741.             HPUSH,  END-CODE
  1742.  
  1743.         will multiply the top stack value by 64.   Remember:   you cannot 
  1744.         directly execute 4* as defined in this way.  For example,
  1745.  
  1746.                   : 64*  4* 4* 4* ;
  1747.  
  1748.         will fail, as 4* does not have a code-field address nor a jump to 
  1749.         NEXT.
  1750.  
  1751.              The  data stack is accessed by the "PUSH," and "POP,"  FORTH 
  1752.         instructions.   All return stack operations are not easily acces-
  1753.         sible from inside CODE words.   Also,  because push, pop and call 
  1754.         use the data stack,  you must be careful on how to pass arguments 
  1755.         to  subroutines as the arguments to be passed will be covered  up 
  1756.         by a "call" type statement unless you first pop them into  regis-
  1757.         ters in the calling routine.
  1758.  
  1759.              The Z-80 registers are assigned by UNIFORTH as follows:
  1760.  
  1761.                    A   general purpose register
  1762.                   BC   general purpose register pair
  1763.                   DE   general purpose register pair
  1764.                   HL   general purpose register pair
  1765.                   IX   general purpose register pair
  1766.                   IY   return stack pointer
  1767.                    A'  general purpose register
  1768.                   BC'  general purpose register pair
  1769.                   DE'  general purpose register pair, sometimes
  1770.                           output from NEXT
  1771.                   HL'  Interpreter pointer
  1772.                   SP   data stack pointer
  1773.                    I   interrupt vector  (set by monitor)
  1774.                    R   refresh register  (set by monitor)
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.         UNIQUE FEATURES OF UNIFORTH                        Page 3-9
  1785.  
  1786.  
  1787.  
  1788.         Where  primes indicate the alternate register set.   Any register 
  1789.         or  register pair labelled as general purpose can be used at  any 
  1790.         time  by  any word.   The register pair HL' can be  used  by  the 
  1791.         programmer if its contents are saved and restored.  The interrupt 
  1792.         vector (I) and refresh (R) registers are usually reserved for the 
  1793.         monitor  and definitely should not be used.   DE' is only used by 
  1794.         ;CODE  and can generally be used by the programmer as  a  general 
  1795.         purpose register.  BC' may be used in future versions for storing 
  1796.         the  user  area  pointer,   so  use  it  with  caution  in   your 
  1797.         applications.
  1798.  
  1799.  
  1800.         3.6.2  Assembler Mnemonics
  1801.  
  1802.  
  1803.              The  mnemonics  used for UNIFORTH are basically the same  as 
  1804.         used by Zilog, except:
  1805.  
  1806.              1.  All instructions are reverse-polish.  For example,
  1807.  
  1808.                            Zilog:  LD A,C
  1809.  
  1810.                            FORTH:  C A LD,
  1811.  
  1812.  
  1813.              2.   All instructions have a trailing comma.  This indicates 
  1814.                   that something is being stored in the dictionary by the 
  1815.                   instruction,  and also prevents multiple definitions as 
  1816.                   would be the case for XOR.
  1817.  
  1818.              3.   Indirect  memory addressing is shown by adding a  right 
  1819.                   parenthesis after the address:
  1820.  
  1821.                            Zilog:  LD A,(4000)
  1822.  
  1823.                            FORTH:  4000 ) A LD,
  1824.  
  1825.              4.  Index register with displacement is similar:
  1826.  
  1827.                            Zilog:  LD A,(IX+32)
  1828.  
  1829.                            FORTH:  32 (IX) A LD,
  1830.  
  1831.  
  1832.  
  1833.              5.   The assembler is definitely not user-proof.   The  only 
  1834.                   error  checking  is for removal of all items placed  on 
  1835.                   the stack during the definition (i.e.,  loops and bran-
  1836.                   ches  properly terminated).   You can really  screw  up 
  1837.                   using codes not allowed by Zilog.  For example,
  1838.  
  1839.                                         31 PE JR,
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.         UNIQUE FEATURES OF UNIFORTH                        Page 3-10
  1851.  
  1852.  
  1853.                   is  accepted  by  the assembler,  but stores a  48  hex 
  1854.                   followed by the 1F hex displacement into memory,  which 
  1855.                   is interpreted by the Z-80 as:
  1856.  
  1857.                            LD    C,B
  1858.                            RRA
  1859.  
  1860.                   which is obviously not what you wanted!   You,  as  the 
  1861.                   programmer, are responsible for knowing proper usage of 
  1862.                   each instruction.
  1863.  
  1864.  
  1865.                                     NOTATION
  1866.  
  1867.         reg       any of the registers A,B,C,D,E,H,L,I,R,IXH,IXL,
  1868.                       IYH,IYL,(HL),(C) as appropriate
  1869.         rp        any of the register pairs AF,AF',BC,BC',DE,DE',
  1870.                       HL,HL',IX,IY,SP
  1871.         (xy)      index register displacement (IX),(IY)
  1872.         cc        condition codes Z,NZ,CY,NC,PO,PE,P,M
  1873.         b         a 3-bit integer
  1874.         data      an 8-bit integer
  1875.         data16    a 16-bit integer
  1876.         addr      a 16-bit address
  1877.         disp      a signed 8-bit displacement
  1878.             note:  anywhere HL is used, you can use IX or IY
  1879.  
  1880.  
  1881.         opcode details
  1882.  
  1883.              Zilog                     FORTH
  1884.              ADC reg                 reg ADC,
  1885.              ADC data               data ADC,
  1886.              ADC HL,rp             rp HL ADC,
  1887.              ADC (xy+disp)     disp (xy) ADC,
  1888.              BIT b,reg             reg b BIT,
  1889.              CALL addr             addr CALL,
  1890.              CALL cc,addr       addr cc CALL,
  1891.              EX AF,AF'             AF' AF EX,
  1892.              EX (SP),HL           HL (SP) EX,
  1893.              JP (HL)                 (HL) JP,
  1894.              LD rp,(addr)       addr ) rp LD,
  1895.              LD (addr),rp       rp addr ) LD,
  1896.              OUT (C),reg         reg (C) OUT,
  1897.              OUT (port),A       A port ) OUT,
  1898.  
  1899.         3.6.3  The Use of ;CODE
  1900.  
  1901.  
  1902.              The  word ;CODE is the machine-level counterpart  to  DOES>. 
  1903.         Instead  of high level words following DOES>,  assembler instruc-
  1904.         tions follow ;CODE.  Again, you must terminate with a "NEXT," and 
  1905.         the word END-CODE.
  1906.  
  1907.  
  1908.  
  1909.  
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.         UNIQUE FEATURES OF UNIFORTH                        Page 3-11
  1917.  
  1918.  
  1919.              One less than the parameter field address is in register DE' 
  1920.         upon the entry of the instructions after ;CODE.  The instructions 
  1921.         following ;CODE should first switch to the alternate register set 
  1922.         with "EXX," and then increment DE' ("DE' INC,") to have DE' point 
  1923.         to the parameter field.  Finally, before you exit the definition, 
  1924.         use another "EXX," to return to the primary register  set.   More 
  1925.         information about ;CODE can be found in the Programmer's Manual.
  1926.  
  1927.  
  1928.         3.6.4  Conditional Branching
  1929.  
  1930.  
  1931.              Several  branching structures have been included as in  high 
  1932.         level FORTH.   You can use IFs and BEGIN-UNTIL loops, in addition 
  1933.         to hard branching with JP and JR opcodes.
  1934.  
  1935.           cc IF,       Start a forward branch.   cc is the Z-80 condition 
  1936.                        code to be satisfied.   (Actually, IF, codes a 'JP 
  1937.                        nn', where nn is the opposite of cc).
  1938.  
  1939.           cc RIF,      Same  as IF,  except that a relative jump (JR)  is 
  1940.                        used.   You can only use a limited set of cc,  and 
  1941.                        must terminate with RTHEN, when using the relative 
  1942.                        form.
  1943.  
  1944.           ELSE,        The branch for the case when the condition code is 
  1945.                        not satisfied.
  1946.  
  1947.           RELSE,       Same as ELSE, except uses the relative jump.  Must 
  1948.                        terminate with RTHEN to work.
  1949.  
  1950.           THEN,        Termination of an IF, statement.
  1951.  
  1952.           RTHEN,       Termination of an RIF, statement.
  1953.  
  1954.           BEGIN,       Start of a block structure.
  1955.  
  1956.           cc UNTIL,    Branches back to BEGIN, until cc is satisfied.
  1957.  
  1958.           cc RUNTIL,   Same as UNTIL except uses the relative jump.
  1959.  
  1960.           cc WHILE,    Executes   statements   following  while   cc   is 
  1961.                        satisfied.
  1962.  
  1963.           REPEAT,      End of a BEGIN, WHILE, sequence.
  1964.  
  1965.           RAGAIN,      Similar to REPEAT, except uses the relative jump.
  1966.  
  1967.           DO,          Mark  the  beginning of a DO-LOOP.   Identical  to 
  1968.                        BEGIN,
  1969.  
  1970.           LOOP,        Performs a DJNZ, operation.  Must be preceded by a 
  1971.                        DO, statement.
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.         UNIQUE FEATURES OF UNIFORTH                        Page 3-12
  1983.  
  1984.  
  1985.         In each case of a conditional testing instruction,  you must have 
  1986.         set  or tested the condition codes before the instruction  execu-
  1987.         tion.   For example, for 'NZ IF,' you could have executed 'B CP,' 
  1988.         before the IF, to set the condition codes.
  1989.  
  1990.         3.6.5  Examples
  1991.  
  1992.  
  1993.              Some of the best examples of CODE words can be found in your 
  1994.         library  disk.   Listed  below  are three simple  examples  as  a 
  1995.         further guide.
  1996.  
  1997.              For more help in programming the Z-80,  we highly  recommend 
  1998.         the  purchase  of Z-80 Assembly Language  Programming,  by  Lance 
  1999.         Leventhal,  Osborne  Press,  or Programming the Z-80,  by  Rodney 
  2000.         Zaks, Sybex Press.
  2001.  
  2002.  
  2003.         Example  1.   Incrementing  the top stack value by the amount  in 
  2004.         variable INCREASE.
  2005.  
  2006.              VARIABLE INCREASE         ( amt to increase by)
  2007.              2 INCREASE !              ( initial value)
  2008.  
  2009.              CODE BUMP                 ( define BUMP)
  2010.                  DE POP,               ( pop stack value into DE)
  2011.                  INCREASE ) HL LD,     ( get increment into HL)
  2012.                  DE HL ADD,            ( add together)
  2013.                  HPUSH,                ( put total back on stack)
  2014.                  END-CODE
  2015.                
  2016.         Example  2.   Adding  or subtracting 1 from the top  stack  value 
  2017.         depending on whether it is positive or negative respectively.
  2018.  
  2019.              CODE BIGGER               ( define BIGGER)
  2020.                   HL POP,              ( pop stack value into HL)
  2021.                   A XOR,               ( clear A)
  2022.                   H CP,                ( is value negative)
  2023.                   CY IF,               ( if CY, yes)
  2024.                   HL DEC,              (   so then subtract)
  2025.                   ELSE,                ( if not CY, then...)
  2026.                   HL INC,              (   increment)
  2027.                   THEN,                ( now end)
  2028.                   HPUSH,               ( by pushing HL onto stack)
  2029.                   END-CODE
  2030.  
  2031.         Example 3.   Using a DO-LOOP construct with DO and LOOP.  Shift a 
  2032.         byte left n times.
  2033.  
  2034.              CODE LSHIFT               ( define LSHIFT)
  2035.                   DE POP,              ( get count)
  2036.                   HL POP,              ( byte to shift is now in L)
  2037.                   E B LD,              ( move count to B)
  2038.                   DO,                  ( start of loop)
  2039.  
  2040.  
  2041.  
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.         UNIQUE FEATURES OF UNIFORTH                        Page 3-13
  2049.  
  2050.  
  2051.                   L RL,                ( rotate left one bit)
  2052.                   LOOP,                ( continue until B=0)
  2053.                   HPUSH,               ( put rotated value back)
  2054.                   END-CODE             (   on stack and end)
  2055.  
  2056.  
  2057.         3.7  Deviations from FORTH-83
  2058.  
  2059.  
  2060.              We've sold FORTH-79 systems for years, but took 18 months to 
  2061.         decide to revise UNIFORTH to match the FORTH-83 Standard.   There 
  2062.         has been a lot of controversy about F83, with good reason.
  2063.  
  2064.              The primary problem is that FIG-FORTH and F79 programs  will 
  2065.         not  run  under F83.   The Standards committee changed the  basic 
  2066.         function of several words,  but kept their names the same.   This 
  2067.         means that the wealth of published FORTH software is not  useable 
  2068.         without recoding, so beware!
  2069.  
  2070.              UNIFORTH closely follows F83 with some exceptions.   Primary 
  2071.         amongst  these is the F83 requirement of floored division.   What 
  2072.         this  means  is  that integer truncation is  to  the  next  lower 
  2073.         number.   Dividing  -14  by 10 yields -2,  since  the  fractional 
  2074.         result   is   -1.4.    This  type  of  truncation   makes   sense 
  2075.         mathmatically  but  doesn't match your natural thinking  nor  the 
  2076.         currently available hardware.   All 8086,  68000,  PDP-11, Z8000, 
  2077.         etc.  processors  use  arithmetic where the result  is  truncated 
  2078.         towards zero.   Our example would then yield -1 as the result.
  2079.  
  2080.              Another  problem  is in handling the sign of the  remainder.  
  2081.         If you divide -14 by 10, is the remainder -4 or +4?  F83 requires 
  2082.         that  the  result by +4 (taking the sign of  the  divisor).   All 
  2083.         processors give remainders that use the sign of the dividend,  or 
  2084.         -4 in our example.  Again, UNIFORTH conforms to the hardware, not 
  2085.         the Standard.
  2086.  
  2087.              We've  left  many  words "immediate" (such  as  ."  and  all 
  2088.         vocabulary names).  F83 uses separate words for execution and for 
  2089.         compile.    F83's  UM*  is  UNIFORTH's  USS*D,   where  our  form 
  2090.         explicitly  tells  what  kind of mixed mode arithmetic  is  being 
  2091.         used.
  2092.  
  2093.              We're  not happy about the -1 truth flag.   While  it  makes 
  2094.         sense logically,  it makes error checking more difficult.  Beware 
  2095.         of F79 and FIG-FORTH uses of the truth flag!
  2096.  
  2097.              The  Programmer's Guide gives information on how to  convert 
  2098.         FIG-FORTH and F79 systems to UNIFORTH.
  2099.  
  2100.              There  is  much  more  about the Sampler  that  hasn't  been 
  2101.         discussed here.   Explore the system!  You may find features that 
  2102.         are of use.   The Manual Set describes the UNIFORTH  Professional 
  2103.         in  gory detail,  so you might take a look at it if you need more 
  2104.         help.
  2105.  
  2106.  
  2107.  
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.  
  2117.  
  2118.  
  2119.                                     Chapter 4
  2120.  
  2121.                               WHAT YOU ARE MISSING
  2122.  
  2123.  
  2124.              Now that you have used UNIFORTH,  you might be interested in 
  2125.         how the Professional differs from the public domain Sampler.   We 
  2126.         recommend  writing  for our free,  complete catalog,  but  listed 
  2127.         below are some of the major differences in more technical detail.
  2128.  
  2129.         Multitasking
  2130.              The Professional is a fast multitasking system.   Tasks  can 
  2131.         be assigned priorities,  and messages can be passed between them.  
  2132.         Each  task  takes less than 1K bytes of memory,  so many  can  be 
  2133.         resident  at once.   We often build systems that include the user 
  2134.         task,  a  printer spooler,  a temperature background  acquisition 
  2135.         task,  and  a  telecommunications task,  where all  four  are  in 
  2136.         operation at once at no noticible degradation in response.
  2137.  
  2138.         Speed
  2139.              We've carefully optimized the Professional and use state-of-
  2140.         the-art  threading techniques to make the fastest possible  FORTH 
  2141.         system.   The  floating  point routines use a separate stack  for 
  2142.         fastest calculations.   You will find a 50% or larger improvement 
  2143.         in speed.
  2144.  
  2145.         Strings
  2146.              A full string package is included.   All the functions  that 
  2147.         you would find in BASIC,  such as MID$ and INSTR, are included so 
  2148.         that you can manipulate strings as easily as integers.
  2149.  
  2150.         Custom utilities
  2151.              The  Professional  UNIFORTH includes files of  functions  to 
  2152.         access the keyboard, provide full musical note generation, and to 
  2153.         draw lines, dots and filled polygons on color graphics board.  In 
  2154.         addition, every operating system request is implemented.
  2155.  
  2156.         Source code
  2157.              You get the source code to everything except the 5Kb kernel.  
  2158.         In all, about 400Kb of source code is distributed.
  2159.  
  2160.         Debugger
  2161.              The  Professional  comes with a single-step  debugger/tracer 
  2162.         that can be used to track down errors in your code.  The data and 
  2163.         return  stacks  are displayed as each word  is  executed.   Break 
  2164.         points may be set.
  2165.  
  2166.         Decompiler
  2167.              A  complete decompiler will display any high-level  UNIFORTH 
  2168.         word on your screen.   This utility is highly useful to check the 
  2169.         syntax of a word or to retrieve an interactive definition.
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.         WHAT YOU ARE MISSING                               PAGE 4-2
  2181.  
  2182.  
  2183.         More systems
  2184.              The   Professional   UNIFORTH  is  available  for  a   dozen 
  2185.         processors and even more operating systems.  We will customize it 
  2186.         for  a particular computer,  create stand-alone systems  and  add 
  2187.         multi-user support as desired.
  2188.  
  2189.         Documentation
  2190.              The   UNIFORTH  Professional  comes  with  500+   pages   of 
  2191.         documentation in a custom slipcase/binder.   All aspects of FORTH 
  2192.         are covered,  from a tutorial much like the one in this manual to 
  2193.         detailed   discussions   of   threading   techniques,    database 
  2194.         management,  forms generation,  coding conventions and much more.  
  2195.         Numerous examples are included to speed the learning process.
  2196.  
  2197.         Support
  2198.              You get 90 days of free hotline support.  Additional support 
  2199.         is also available.   We offer on-site classes.   The Professional 
  2200.         is offered with a 30-day money-back guarantee.
  2201.  
  2202.         Cross-compilers
  2203.              The  UNIFORTH cross-compiler requires the Professional,  and 
  2204.         supports many microprocessor targets.
  2205.  
  2206.              For  complete  information,  write  for  the  free  UNIFORTH 
  2207.         catalog.   If  you  just  wish  to order CP/M  2.2  version  that 
  2208.         corresponds to this Sampler,  then use the catalog numbers listed 
  2209.         below.
  2210.  
  2211.         Cat #          Computer                      Price
  2212.         C22       Generic Z80 CP/M 2.2               $195.00
  2213.         C22K      KayPro Z80 CP/M 2.2                $195.00
  2214.  
  2215.         Other items of interest are listed below.
  2216.  
  2217.         Cat #          Sampler version               Price
  2218.         S21Z      Z80 CP/M 2.2                       $35
  2219.         S21P      IBM PC DOS 2.x                     $35
  2220.         S21A      Apple // (ProDos, 80col)      coming soon
  2221.         S21C      Commodore 64                  coming soon
  2222.         S21M      Macintosh                     coming soon
  2223.  
  2224.         Cat #          Item                          Price
  2225.         BK01      UNIFORTH Manual Set                $60
  2226.         BK11      Starting FORTH/Brodie              $17.95
  2227.         BK12      Thinking FORTH/Brodie              $15.95
  2228.  
  2229.  
  2230.         Ordering information:
  2231.  
  2232.              Specify your computer type and your disk format.   All items 
  2233.         are sent post-paid by UPS or First Class within the U.S.  Checks, 
  2234.         CODs,  money  orders  and purchase orders  in  U.S.  dollars  are 
  2235.         accepted.  Maryland residents add 5% sales tax.  Prices are valid 
  2236.         as of April 15,  1985 and may be different if ordering much later 
  2237.  
  2238.  
  2239.  
  2240.  
  2241.  
  2242.  
  2243.  
  2244.  
  2245.  
  2246.         WHAT YOU ARE MISSING                               PAGE 4-3
  2247.  
  2248.  
  2249.         than that date.  Make checks payable to:
  2250.  
  2251.                             Unified Software Systems
  2252.                                   P.O. Box 2644
  2253.                             New Carrollton, MD 20784
  2254.                                  (301) 552-9590
  2255.  
  2256.  
  2257.         Conclusions
  2258.  
  2259.              We  hope  that you have enjoyed using the UNIFORTH  Sampler.  
  2260.         It is a complete FORTH system that will provide you many years of 
  2261.         usefulness.   Please, let us know your impressions.  Did you find 
  2262.         FORTH worthwhile?   Are there bugs in this system?  What features 
  2263.         would  you  like to see?   Its through user input  and  community 
  2264.         discussion that FORTH will grow or die.  Cast your vote!
  2265.  
  2266.  
  2267.  
  2268.  
  2269.  
  2270.  
  2271.  
  2272.  
  2273.  
  2274.  
  2275.  
  2276.  
  2277.  
  2278.  
  2279.  
  2280.  
  2281.  
  2282.  
  2283.  
  2284.  
  2285.  
  2286.  
  2287.  
  2288.  
  2289.  
  2290.  
  2291.  
  2292.  
  2293.  
  2294.  
  2295.  
  2296.  
  2297.  
  2298.  
  2299.  
  2300.  
  2301.  
  2302.  
  2303.  
  2304.  
  2305.  
  2306.  
  2307.  
  2308.  
  2309.  
  2310.  
  2311.  
  2312. l grow or die.  Cast your vote!
  2313.  
  2314.  
  2315.  
  2316.  
  2317.  
  2318.  
  2319.  
  2320.  
  2321.  
  2322.  
  2323.  
  2324.  
  2325.  
  2326.  
  2327.  
  2328.  
  2329.  
  2330.  
  2331.  
  2332.  
  2333.  
  2334.  
  2335.  
  2336.  
  2337.  
  2338.  
  2339.  
  2340.  
  2341.