1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
<html>
<head>
<title>Using kForth I</title>
</head>
<body>
<h2><img src="kforth.gif"> Using kForth</h2>
<br>
<a name="The Basics"></a><h3>2.1 Basics</h3>
<p>
Type<br><br>
<code>kforth</code><br><br>
to start the program. Upon startup, kForth will inform
you that it is ready to accept input by displaying<br><br>
<tt>Ready!</tt><br><br>
You may type commands, a sequence of <i>words</i>, and
press Enter. kForth will respond with the prompt <br><br>
<tt>ok</tt><br><br>
after it finishes executing each line of input.
To illustrate, try typing the following<br><br>
<code>2 5 + .</code>
<br><br>
and press Enter. kForth will respond with<br><br>
<tt>7 ok</tt><br><br>
You may now enter another sequence of words.
One particularly useful word to know is<br><br>
<code>bye</code><br><br>
kForth will respond by saying<br><br>
<tt>Goodbye</tt><br><br>
and exiting. Finally, note that kForth is not case
sensitive --- you may enter words in lower case
<i>or</i> upper case.<br>
<br><hr>
<a name="More Words"></a><h3>2.2 More Words</h3>
<br>
<p>
The word<br><br>
<code>words</code><br><br>
will display a list of currently defined words in the
<a href="kforth3.html">dictionary</a>. You may define your own words by
typing them at the kForth prompt. For example, a word that
counts from one to ten and displays each number counted may be
defined by entering<br><br>
<code>: count_to_ten 10 0 do i 1+ . loop ;</code>
<br><br>
The symbols ":" and ";" are very important --- they indicate to
the kForth compiler the beginning and ending of the definition of
the word, called <code>count_to_ten</code> in this example. kForth will
display the prompt <tt>ok</tt> after the new word has been
compiled into the dictionary.<br>
<p>
You can verify that our newly defined word has been added to
the dictionary by using <code>words</code>. Now,
execute the word by typing<br><br>
<code>count_to_ten</code><br><br>
and pressing Enter. kForth will display the output<br><br>
<tt>1 2 3 4 5 6 7 8 9 10 ok</tt><br><br>
<p>
If you are entering a definition that requires
several lines of typing, the <tt>ok</tt> prompt will not be
displayed until the end of the definition has been entered,
<i>i.e.</i> until the compiler encounters a semicolon.<br>
<p>
Although you can write Forth programs this way, it is much easier
to create the definitions in a separate source file and then load
them into kForth by issuing the command<br><br>
<code>include</code> <var>filename</var><br><br>
For example, the definition of <code>count_to_ten</code> could
have been entered into a plain text file called <tt>prog1.4th</tt>.
Once kForth has been started, you can simply issue the
command<br><br>
<code>include prog1</code><br><br>
kForth will read the input from the specified file as though
it was being entered from the keyboard. You may have noticed
that the full filename was not entered in the <code>include</code>
command. If no extension is specified, the file is
assumed to have an extension of <tt>.4th</tt>.<br>
<p>
You may also load a source file upon startup of kForth by typing<br><br>
<code>kforth </code><var>filename</var><br>
<br><hr>
<a name="Using the Stack"></a><h3>2.3 Using the Stack</h3>
Forth provides a memory region called the <i>stack</i>
in which data may be placed and operated upon by defined words.
You may enter numbers onto the stack simply by typing them
and pressing Enter. You can use the word <code>.S</code> to
list the contents of the stack.
For example, type the following and press Enter.
<br><br>
<code>2 5</code>
<br><br>
You have placed two numbers onto the stack. Now, type
<br><br>
<code>.S</code>
<br><br>
and press Enter. kForth will respond by listing the items
on the stack:<br><br>
<center><tt>5<br>2</tt></center><br><br>
Notice that 5 is on the top of the stack --- items are placed
into the stack in a first-in, last-out order. kForth provides
most of the stack operators (words) that are a part of the
Forth language. Examples include the arithmetic operators<br><br>
<code>+ - * /</code><br><br>
These operate on the top two items on the stack and replace them
with the result. Other words change the order of items on the
stack or copy or remove items from the stack:<br><br>
<code>SWAP ROT DUP OVER TUCK DROP NIP</code><br><br>
Each stack <i>cell</i> holds a single integer number. You may
also place real numbers, also known as <i>floating point</i> numbers,
onto the stack. These must be entered in a special way known as
<i>exponential notation</i>. For example, to enter
the real number 3.14 onto the stack, type<br><br>
<code>3.14e0</code><br><br>
and press Enter. The zero following the <tt>'e'</tt> indicates
the power of ten that is multiplied to the number (10 raised to
the zero power is equal to 1). If the exponent is zero, as in
this example, the entry can be shortened to simply<br><br>
<code>3.14e</code><br><br>
Exponential format allows you to enter very small and very large numbers
easily. To enter the fractional number representing one-billionth,
0.000000001, you may type<br><br>
<code>1e-9</code><br><br> and press Enter.
<br><br>
When you enter a floating point number onto the stack and list
the stack using <code>.S</code>, you will see two integer numbers
printed instead of one real number. A floating point number occupies
two stack cells instead of one, and <code>.S</code> lists the contents
of each cell as though it were a single integer. You may print the
floating point number occupying the top two cells of the stack with
the word<br><br>
<code>F.</code><br><br>
Use the words<br><br>
<code>F+ F- F* F/</code><br><br>
to perform arithmetic on floating point numbers which have been
entered onto the stack. Example:<br><br>
<code>3.14e 6.28e f+ f.</code><br><br>
will print the result <tt>9.42</tt>. Words to manipulate floating
point numbers on the stack include<br><br>
<code>FSWAP FROT FDUP FOVER FDROP</code>
<br><br><hr>
<a href="kforth2.html"><img src="left.gif"></a>
<a href="kforth2b.html"><img src="right.gif"></a><br><br>
©1998--2000 Creative Consulting for Research and Education
</body>
</html>
|