File: kforth2b.html

package info (click to toggle)
kforth 20010227-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 508 kB
  • ctags: 652
  • sloc: asm: 2,026; cpp: 1,795; ansic: 575; makefile: 64
file content (112 lines) | stat: -rw-r--r-- 4,479 bytes parent folder | download | duplicates (3)
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
<html>
<head>
<title>Using kForth II</title>
</head>
<body>
<h2><img src="kforth.gif"> Using kForth</h2>
<br>
<a name="Variables and Constants"></a><h3>2.4 Variables and Constants</h3>
An integer variable may be declared as follows:<br><br>
<code>variable</code> <i>name</i>
<br><br>
Values may be stored and retrieved from the variable
using the "store" (<code>!</code>) and "fetch"
(<code>@</code>) operators. For example, if we
want to define a variable called <code>counter</code>
and initialize its value to 20, we enter the following:<br><br>
<code>variable counter<br>
20 counter !</code>
<br><br>
When you define a variable, memory is reserved at some address to hold
an integer value, and the name of the variable becomes part of the
dictionary. Typing the name <code>counter</code> at the
Forth prompt and pressing enter will cause the memory address
of <code>counter</code> to be placed onto the stack. Try the
following:<br><br>
<code>counter<br>.s</code><br><br>
You will see a memory address on top of the stack.<br><br>
To examine the value stored in the variable <code>counter</code>,
we place the address of counter on the stack, then use the fetch 
operator to retrieve the value from that address onto the stack: 
<br><br>
<code>counter @</code>
<br><br>
The number 20 will be on top of the stack. Of course to see the
value, we must print it using the word "dot" (<code>.</code>),
so entering<br><br>
<code>counter @ .</code><br><br>
will print the value 20. Forth also has a built-in word, 
<code>?</code>, that performs the sequence '<code>@ . </code>'.<br><br>
Now, let's say we want to increment the value of <code>counter</code>
by ten. First we fetch the value stored in <code>counter</code>
onto the stack, then add ten, and finally store the new value into
the variable. This is accomplished by the sequence:<br><br>
<code>counter @ 10 + counter !</code>
<br><br>
Actually, Forth provides another shorter way of doing the same thing:<br><br>
<code>10 counter +!</code>
<br><br><hr>   
Floating point variables are defined in a similar way:<br><br>
<code>fvariable</code> <i>name</i>
<br><br>
The correspoding operators for storing and retrieving floating
point numbers into the variable are <code>f!</code> and 
<code>f@</code>. Let's define a floating point variable called
<code>velocity</code> and initialize it to zero.<br><br>
<code>
fvariable velocity<br>
0e velocity f!</code>
<br><br>
Note that a floating point value of zero is entered as <code>0e</code>
and we used the operator <code>f!</code> to store the value into
<code>velocity</code>. If we now want to increment the value of
<code>velocity</code> by 9.8, we can enter<br><br>
<code>velocity f@ 9.8e f+ velocity f!</code><br><br>
(kForth does not have a word called <code>f+!</code>, but
you may define such a word!). To print a floating point value on the 
stack, use the word <code>f.</code>
as explained previously. For example,<br><br>
<code>velocity f@ f.</code>
<br><br>
will print the value 9.8.<br>
<hr>  
<br><br>
Integer constants are defined as follows<br><br>
<i>value</i> <code>constant</code> <i>name</i>
<br><br>
To define a constant called <code>megabyte</code>, for example,
enter<br><br>
<code>1048576 constant megabyte</code><br><br>
I often can't remember how many bytes there are in
a megabyte, so I would have written instead<br><br>
<code>1024 1024 * constant megabyte</code><br><br>
Now, type the name of the constant and print the top
item on the stack<br><br>
<code>megabyte .</code><br><br>
and you will see printed the value 1048576. Typing the name
of the constant retrieves <i>its value</i> (not an address)
onto the stack.<br><br>
<hr><br>
Floating point constants are defined in a similar fashion<br><br>
<i>fvalue</i> <code>fconstant</code> <i>name</i>
<br><br>
To define a constant containing the acceleration due to gravity,
9.8 meters per second squared, type<br><br>
<code>9.8e fconstant g</code><br><br>
The name of the constant is <code>g</code>. Typing<br><br>
<code>g f.</code><br><br>
will print 9.8. Now, let's add the value of <code>g</code>
to the value of <code>velocity</code> and print the result
to illustrate the use of floating point variables and constants<br><br>
<code>velocity f@ g f+ f.</code><br><br>
<hr>
<a href="kforth2a.html"><img src="left.gif"></a>
&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="kforth2c.html"><img src="right.gif"></a><br><br>
&copy;1998--2000 Creative Consulting for Research and Education  
</body>
</html>