File: kforth3.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 (859 lines) | stat: -rw-r--r-- 27,659 bytes parent folder | download
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
<html>
<head>
<title>kForth Dictionary</title>
</head>
<body bgcolor=white>
<h2><img src="kforth.gif"> <a name="Dictionary"></a> Dictionary</h2><br>
<ol>
<li><a href="#Dictionary Maintenance">Dictionary Maintenance</a>
<li><a href="#Defining Words">Defining Words</a>
<li><a href="#Control Structures">Control Structures</a>
<li><a href="#Stack and Memory Operations">Stack and Memory Operations</a>
<li><a href="#Logic Operations">Logic and Bit Manipulation Operations</a>
<li><a href="#Arithmetic and Relational Operations">Arithmetic and
Relational Operations</a>
<li><a href="#Floating Point Functions">Floating Point Functions</a>
<li><a href="#Number Conversion">Number Conversion</a>
<li><a href="#Input and Output">Input and Output</a>
<li><a href="#File Access">File Access</a>
<li><a href="#Operating System Interface">Operating System Interface</a>
<li><a href="#Miscellaneous">Miscellaneous</a>
</ol><br>
<p>
<a href="kforth2c.html#Stack Diagrams">Stack diagram</a> notation:
<br><br>
<table border>
<tr align=center><td width="25%"></td><td><b>Data Type</b></td><td><b>Stack Cells</b></td>
<tr align=center><td>a</td><td>address</td><td>1</td>
<tr align=center><td>n</td><td>signed single integer</td><td>1</td>
<tr align=center><td>u</td><td>unsigned single integer</td><td>1</td>
<tr align=center><td>d</td><td>signed double length integer</td><td>2</td>
<tr align=center><td>b</td><td>boolean flag: true or false (-1 or 0)</td><td>1</td>
<tr align=center><td>f</td><td>double precision floating point value</td><td>2</td>
<tr align=center><td>^str</td><td>counted string address</td><td>1</td>
</table>
<br><hr>

<h3><a name="Dictionary Maintenance"></a>Dictionary Maintenance</h4>
The word <code>FORGET</code> may be used to remove words from the
dictionary. Typing<br><br>
<center><code>FORGET</code> <var>name</var></center><br><br>
will remove <var>name</var> and all words defined after <var>name</var>
from the dictionary.<br>
<br>
The word <code>COLD</code> deletes all non-intrinsic definitions and 
strings, resets all stacks, and restarts the Forth environment in
interpreter mode.<br>
<br>
The words <code>'</code> (TICK), and <code>[']</code> may be used to 
search the dictionary for a specified word. These words behave 
according to the ANS Forth standard, and return an execution address
on the stack. The word <code>EXECUTE</code> may be used
to execute a word given the execution address on the stack. The
word <code>FIND</code> is also implemented.<br> 

<hr>

<h3><a name="Defining Words"></a>Defining Words</h4>
In addition to ordinary "colon definitions" of the form,
<br><br>
<center>
<code>: NAME ... ;</code><br>
</center><br>
the following defining words are also provided:<br><br>
<center>
<table>
<tr><td><code>CREATE</code> <var>name</var></td>
<tr><td><code>VARIABLE</code> <var>name</var></td>
<tr><td><var>n</var> <code>CONSTANT</code> <var>name</var></td>
<tr><td><code>FVARIABLE</code> <var>name</var></td>
<tr><td><var>f</var> <code>FCONSTANT</code> <var>name</var></td>
</table>
</center><br>
<code>CREATE</code> can be used inside a word definition to make 
your own defining words. The word <code>DOES></code>,
as part of a <code>CREATE ... DOES></code> expression, 
allows you to specify the run time behavior of words created
by the defining word.<br>
<br><hr>

<h3>
<a name="Control Structures"></a>Control Structures</h4>

<p>
The following control structures are provided in kForth:
<br><br>
<center>
<table>
<tr><td><code>DO ... LOOP</code></td>
<tr><td><code>DO ... +LOOP</code></td>
<tr><td><code>IF ... THEN</code></td>
<tr><td><code>IF ... ELSE ... THEN</code></td>
<tr><td><code>BEGIN ... AGAIN</code></td>
<tr><td><code>BEGIN ... UNTIL</code></td>
<tr><td><code>BEGIN ... WHILE ... REPEAT</code></td>
<tr><td><code>CASE ... OF ... ENDOF ... ENDCASE</code></td>
</table>
</center><br>

<p>
All control stuctures may be nested --- for <code>DO</code> loops
the number of levels of nesting is only limited by return stack space.
The following execution control words are also defined:<br><br>
<center>
<table>
<tr><td><code>RECURSE</code></td>
<tr><td><code>LEAVE</code></td>
<tr><td><code>EXIT</code></td>
<tr><td><code>QUIT</code></td>
<tr><td><code>ABORT</code></td>
</table>
</center><br>
<code>RECURSE</code> causes the currently executing word to be 
executed.<br><br>

<code>LEAVE</code> removes the current loop parameters
from the return stack, by calling <code>UNLOOP</code>, and causes 
an immediate jump out of the current loop. Execution 
resumes at the instruction following the loop.<br><br>

<code>EXIT</code> causes an immediate return from the word 
currently being executed. Note that <code>EXIT</code> from
within a loop requires that the loop parameters be discarded
from the return stack explicitly with <code>UNLOOP</code>.<br><br> 

<code>QUIT</code> empties the return stack, terminates execution
of the current word and returns kForth to the interpreter mode.<br><br>

<code>ABORT</code> empties the data stack and executes <code>QUIT</code>.
<br><hr>

<h3>
<a name="Stack and Memory Operations"></a>Stack and Memory Operations</h4>
<p>
Allowed data stack operations are listed in the following table:
<br>
<br>
<center><table>
<tr>
  <td width="25%"><code>DUP</code></td><td>n -- n n</td><td>duplicate</td>
<tr>
  <td><code>?DUP</code></td><td>n -- n n | 0</td>
  <td>dup if not zero</td>
<tr>
  <td><code>SWAP</code></td><td>n1 n2 -- n2 n1</td><td>swap</td>
<tr>
  <td><code>OVER</code></td><td>n1 n2 -- n1 n2 n1</td><td>over</td>
<tr>
  <td><code>ROT</code></td><td>n1 n2 n3 -- n2 n3 n1</td>
  <td>rotate cw</td>
<tr>
  <td><code>-ROT</code></td><td>n1 n2 n3 -- n3 n1 n2</td>
  <td>rotate ccw</td>
<tr>
  <td><code>DROP</code></td><td>n1 --</td><td>drop</td>
<tr>
  <td><code>NIP</code></td><td>n1 n2 -- n2</td><td>nip</td>
<tr>
  <td><code>TUCK</code></td><td>n1 n2 -- n2 n1 n2</td><td>tuck</td>
<tr>
  <td><code>PICK</code></td><td>... n -- ... m</td>
  <td>copy nth item deep</td> 
<tr>
  <td><code>ROLL</code></td><td>... n -- ... m</td>
  <td>rotate nth item deep to top of stack</td>
<tr>
  <td><code>DEPTH</code></td><td> ... -- ... n</td><td>stack depth</td>
<tr>
  <td><code>2DUP</code></td><td>n1 n2 -- n1 n2 n1 n2</td>
<tr>
  <td><code>2SWAP</code></td>
  <td>n1 n2 n3 n4 -- n3 n4 n1 n2</td>
<tr>
  <td><code>2OVER</code></td>
  <td>n1 n2 n3 n4 -- n1 n2 n3 n4 n1 n2</td>
<tr>
  <td><code>2ROT</code></td>
  <td>n1 n2 n3 n4 n5 n6 -- n3 n4 n5 n6 n1n2</td>
<tr>
  <td><code>2DROP</code></td>
  <td>n1 n2 --</td>
<tr>
  <td><code>FDUP</code></td><td>f -- f f</td>
  <td>same as <code>2DUP</code></td>
<tr>
  <td><code>FSWAP</code></td>
  <td>f1 f2 -- f2 f1</td>
  <td>same as <code>2SWAP</code></td>
<tr>
  <td><code>FOVER</code></td>
  <td>f1 f2 -- f1 f2 f1</td>
  <td>same as <code>2OVER</code></td>
<tr>
  <td><code>FROT</code></td>
  <td>f1 f2 f3 -- f2 f3 f1</td>
  <td>same as <code>2ROT</code></td>
<tr>
  <td><code>FDROP</code></td>
  <td>f1 --</td>
  <td>same as <code>2DROP</code></td>
</table></center>

<p>
Return stack operations are:
<br><br>
<center><table>
<tr>
  <td width="25%"><code>>R</code></td>
  <td width="25%">n --</td>
  <td>push onto return stack</td>
<tr>
  <td><code>R></code></td>
  <td>-- n</td>
  <td>pop from return stack</td>
<tr>
  <td><code>R@</code></td>
  <td>-- n</code></td>
  <td>copy from top of return stack</td>
<tr>
  <td><code>I</code></td><td>-- n</td><td>current loop index</td>
<tr>
  <td><code>J</code></td><td>-- n</td><td>next outer loop index</td>
<tr>
  <td><code>UNLOOP</code></td><td> -- </td>
  <td>discard loop parameters from return stack</td>
</table></center>


<p>
The following memory access words are implemented:
<br><br>
<center><table>
<tr>
  <td width="25%"><code>@</code></td><td>a -- n</td><td>fetch single</td>
<tr>
  <td><code>!</code></td><td>n a --</td>
  <td>store single n to address a</td>
<tr>
  <td><code>A@</code></td><td>a1 -- a2</td>
  <td>fetch address from address a</td>
<tr>
  <td><code>C@</code></td><td>a -- n</td>
  <td>fetch byte</td>
<tr>
  <td><code>C!</code></td>
  <td>n a --</td><td>store byte</td>
<tr>
  <td><code>W@</code></td><td>a -- n</td>
  <td>fetch signed word</td>
<tr>
  <td><code>W!</code></td><td>n a --</td><td>store signed word</td>
<tr>
  <td><code>SF@</code></td><td>a -- f</td>
  <td>fetch single precision float</td>
<tr>
  <td><code>SF!</code></td><td>f a --</td>
  <td>store f as single precision float</td>
<tr>
  <td><code>DF@</code></td><td>a -- f</td>
  <td>fetch double precision float</td>
<tr>
  <td><code>DF!</code></td><td>f a --</td>
  <td>store double precision float</td>
<tr>
  <td><code>F@</code></td><td>a -- f</td>
  <td>same as <code>DF@</code></td>
<tr>
  <td><code>F!</code></td><td>f a --</td>
  <td>same as <code>DF!</code></td>
<tr>
  <td><code>SP@</code></td><td>-- a</td>
  <td>fetch data stack pointer</td>
<tr>
  <td><code>RP@</code></td><td>-- a</td>
  <td>fetch return stack pointer</td>
<tr>
  <td><code>?</code></td><td>a -- </td>
  <td>fetch and print single; equivalent to <code>@ .</code></td>
<tr>
  <td><code>ALLOT</code></td><td>n --</td>
  <td>allocates n bytes in the dictionary</td>
<tr>
  <td><code>?ALLOT</code></td><td>n -- a</td>
  <td>allocates n bytes in the dictionary and returns</td>
<tr>
  <td></td><td></td><td>starting address of the allocated region</td>
<tr>
  <td><code>C"</code></td><td> -- ^str</td>
  <td>compile a counted string into the string table;</td>
<tr><td></td><td></td>
  <td>the string is taken from the input stream and</td>
<tr><td></td><td></td>
  <td>must be terminated by "</td>
<tr>
  <td><code>S"</code></td><td> -- a n</td>
  <td>compile a string and return address and count</td>
<tr><td><code>COUNT</code></td><td>^str -- a n</td>
  <td>convert counted string address to character</td>
<tr><td></td><td></td>
  <td>buffer address a and character count n</td>
<tr>
  <td><code>CMOVE</code></td><td>a1 a2 n --</td>
  <td>move n bytes from source a1 to dest a2</td>
<tr>
  <td><code>CMOVE></code></td><td>a1 a2 n --</td>
  <td>move n bytes from a1 to a2 in descending order</td>
<tr>
  <td><code>FILL</code></td><td>a n1 n2 --</td>
  <td>fill n1 bytes with byte value n2 starting at a</td>
<tr>
  <td><code>ERASE</code></td><td>a n -- </td>
  <td>fill n bytes with zero starting at a</td>
</table></center>


<p>
The non-ANSI standard word <code>A@</code> is needed because
kForth performs type checking for operands involved in
memory access. It is essentially identical to <code>@</code>
except the type field is set to be an <i>address</i> for the
retrieved value. Addresses may be stored in ordinary variables 
using <code>!</code>; however they should be retrieved with 
<code>A@</code>.<br><br>

<p>
The behavior of <code>ALLOT</code> does not conform exactly
to the ANSI standard. <code>ALLOT</code> dynamically allocates
the requested amount of memory and sets the parameter field
address (PFA) of the last created word to the address of the
alloted region. Thus, <code>ALLOT</code> should always be
preceeded by <code>CREATE</code>. In kForth, an attempt to
<code>ALLOT</code> without first creating a named dictionary
entry, using <code>CREATE</code>, will result in a virtual
machine error. Thus kForth limits the use of <code>ALLOT</code>,
but code written for kForth will be portable to ANSI Forths.<br>
<p> 
The non-ANSI standard word <code>?ALLOT</code> is provided
because kForth contains no <code>HERE</code> address. 
<code>?ALLOT</code> should be preceeded by <code>CREATE</code>
as described above. All memory is dynamically allocated, and freed 
upon exiting kForth.<br>

<hr>

<h3>
<a name="Logic Operations"></a>
Logic and Bit Manipulation Operations</h4>
<br>
<center>
<table>
<tr>
  <td width="25%"><code>AND</code></td><td width="25%">n1 n2 -- n3</td>
  <td>bitwise AND of n1 and n2</td>
<tr>
  <td><code>OR</code></td><td>n1 n2 -- n3</td>
  <td>bitwise OR of n1 and n2</td>
<tr>
  <td><code>XOR</code></td><td>n2 n2 -- n3</td>
  <td>bitwise exclusive OR of n1 and n2</td>
<tr>
  <td><code>NOT</code></td><td>n1 -- n2</td>
  <td>one's complement of n1</td>
<tr>
  <td><code>INVERT</code></td><td>n1 -- n2</td>
  <td>same as NOT</td>
<tr>
  <td><code>LSHIFT</code></td><td>n1 n2 -- n3</td>
  <td>n3 is n1 shifted left by n2 bits</td>
<tr>
  <td><code>RSHIFT</code></td><td>n1 n2 -- n3</td>
  <td>n3 is n1 shifted right by n2 bits</td>
</table>
</center>
<br><hr>

<h3>
<a name="Arithmetic and Relational Operations"></a>
Arithmetic and Relational Operations</h4>

<p>
Single Integer Operations<br>
<center>
<table>
<tr>
  <td width="25%">1+</td><td width="25%">n1 -- n2</td>
  <td>increment (n2 = n1 + 1)</td>
<tr>
  <td>1-</td><td>n1 -- n2</td><td>decrement (n2 = n1 - 1)</td>
<tr>
  <td>2+</td><td>n1 -- n2</td><td>n2 = n1 + 2</td>
<tr>
  <td>2-</td><td>n1 -- n2</td><td>n2 = n1 - 2</td>
<tr>
  <td>2*</td><td>n1 -- n2</td><td>arithmetic left shift (n2 = n1*2)</td>
<tr>
  <td>2/</td><td>n1 -- n2</td><td>arithmetic right shift (n2 = n1/2)</td>
<tr>
  <td><code>CELLS</code></td><td>n1 -- n2</td>
  <td>n2 is n1 times size in bytes of a cell (4)</td>
<tr>
  <td><code>CELL+</code></td><td>n1 -- n2</td>
  <td>n2 is n1 plus the size in bytes of a cell</td>
<tr>
  <td><code>DFLOATS</code></td><td>n1 -- n2</td>
  <td>n2 is n1 times size of a floating point number</td>
<tr>
  <td><code>DFLOAT+</code></td><td>n1 -- n2</td>
  <td>n2 is n1 plus the size of a floating point number</td>
<tr>
  <td><code>SFLOATS</code></td><td>n1 -- n2</td>
  <td>same as <code>CELLS</code></td>
<tr>
  <td><code>SFLOAT+</code></td><td>n1 -- n2</td>
  <td>same as <code>CELL+</code></td>
<tr>
  <td><code>CHAR+</code></td><td>n1 -- n2</td>
  <td>same as <code>1+</code></td>
<tr>
  <td>+</td><td>n1 n2 -- n3</td><td>add</td>
<tr>
  <td>-</td><td>n1 n2 -- n3</td><td>subtract (n3 = n1 - n2)</td>
<tr>
  <td>*</td><td>n1 n2 -- n3</td><td>multiply</td>
<tr>
  <td>/</td><td>n1 n2 -- n3</td><td>divide ( n3 = n1/n2)</td>
<tr>
  <td>+!</td><td>n a --</td>
  <td>add n to value at address a</td>
<tr>
  <td><code>MOD</code></td><td>n1 n2 -- n3</td>
  <td>modulus</td>
<tr>
  <td><code>/MOD</code></td><td>n1 n2 -- n3 n4</td>
  <td>n3 = remainder and n4 = quotient for n1/n2</td>
<tr>
  <td><code>*/</code></td><td>n1 n2 n3 -- n4</td>
  <td>n4 = n1*n2/n3; intermediate value is 64 bit</td>
<tr>
  <td><code>*/MOD</code></td><td>n1 n2 n3 -- n4 n5</td>
  <td>n4 and n5 are remainder and quotient for n1*n2/n3</td>
<tr>
  <td><code>M*</code></td><td>n1 n2 -- d</td>
  <td>multiply two singles and return signed double</td>
<tr>
  <td><code>UM*</code></td><td>u1 u2 -- ud</td>
  <td>multiply unsigned singles and return unsigned double</td>
<tr>
  <td><code>FM/MOD</code></td><td>d n1 -- n2 n3</td>
  <td>divide double by single to give floored quotient n3</td>
<tr><td></td><td></td><td>and remainder n2</td>
<tr>
  <td><code>SM/REM</code></td><td>d n1 -- n2 n3</td>
  <td>divide double by single to give symmetric quotient n3</td>
<tr> <td></td><td></td><td>and remainder n2</td>
<tr>
  <td><code>ABS</code></td><td>n1 -- n2</td>
  <td>absolute value</td>
<tr>
  <td><code>NEGATE</code></td><td>n1 -- n2</td>
  <td>n2 = -n1</td>
<tr>
  <td><code>MIN</code></td><td>n1 n2 -- n1 | n2</td>
  <td>minimum of n1 and n2</td>
<tr>
  <td><code>MAX</code></td><td>n1 n2 -- n1 | n2</td>
  <td>maximum of n1 and n2</td>
<tr>
  <td>=</td><td>n1 n2 -- b</td><td>test n1 equal to n2</td>
<tr>
  <td>&lt;></td><td>n1 n2 -- b</td>
  <td>test n1 not equal to n2</td>
<tr>
  <td>&lt;</td><td>n1 n2 -- b</td>
  <td>test n1 less than n2</td>
<tr>
  <td>></td><td>n1 n2 -- b</td>
  <td>test n1 greater than n2</td>
<tr>
  <td>&lt;=</td><td>n1 n2 -- b</td>
  <td>test n1 less than or equal to n2</td>
<tr>
  <td>>=</td><td>n1 n2 -- b</td>
  <td>test n1 greater than or equal to n2</td>
<tr>
  <td><code>U</code>&lt;</td><td>u1 u2 -- b</td>
  <td>test unsigned u1 less than u2</td>
<tr>
  <td><code>U></code><td>u1 u2 -- b</td>
  <td>test unsigned u1 greater than u2</td>
<tr>
  <td>0&lt;</td><td>n -- b</td><td> test n less than zero</td>
<tr>
  <td>0></td><td>n -- b</td><td>test n greater than zero</td>
<tr>
  <td>0=</td><td>n -- b</td><td>test n equal to zero</td>
<tr>
  <td>0&lt;></td><td>n -- b</td><td>test n not equal to zero</td>
</table>
</center>
<br>
kForth provides pre-defined constants <code>TRUE</code> (-1) and
<code>FALSE</code> (0).
<br>
<p>
Floating Point Operations<br>
<center>
<table>
<tr>
  <td width="25%"><code>F+</code></td><td>f1 f2 -- f3</td><td>fadd</td>
<tr>
  <td><code>F-</code></td><td>f1 f2 -- f3</td><td>fsubtract (f3 = f1 - f2)</td>
<tr>
  <td><code>F*</code></td><td>f1 f2 -- f3</td><td>fmultiply</td>
<tr>
  <td><code>F/</code></td><td>f1 f2 -- f3</td><td>fdivide ( f3 = f1/f2)</td>
<tr>
  <td><code>FABS</code></td><td>f1 -- f2</td><td>absolute value</td>
<tr>
  <td><code>FNEGATE</code></td><td>f1 -- f2</td><td>f2 = -f1</td>
<tr>
  <td><code>FROUND</code></td><td>f1 -- f2</td><td>round to nearest whole number</td>
<tr>
  <td><code>FLOOR</code></td><td>f1 -- f2</td><td>round down to whole number</td>
<tr>
  <td><code>FMIN</code></td><td>f1 f2 -- f1 | f2</td>
  <td>minimum of f1 and f2</td>
<tr>
  <td><code>FMAX</code></td><td>f1 f2 -- f1 | f2</td>
  <td>maximum of f1 and f2</td>
<tr>
 <td><code>F0=</code></td><td>f -- b</td><td>test f equal to zero</td>
<tr>
 <td><code>F0&lt;</code></td><td>f -- b</td><td>test f less than zero</td>
<tr>
  <td><code>F=</code></td><td>f1 f2 -- b</td><td>test f1 equal to f2</td>
<tr>
  <td><code>F&lt;></code></td><td>f1 f2 -- b</td>
  <td>test f1 not equal to f2</td>
<tr>
  <td><code>F&lt;</code></td><td>f1 f2 -- b</td>
  <td>test f1 less than f2</td>
<tr>
  <td><code>F></code></td><td>f1 f2 -- b</td>
  <td>test f1 greater than f2</td>
<tr>
  <td><code>F&lt;=</code></td><td>f1 f2 -- b</td>
  <td>test f1 less than or equal to f2</td>
<tr>
  <td><code>F>=</code></td><td>f1 f2 -- b</td>
  <td>test f1 greater than or equal to f2</td>
</table>
</center>

<hr>

<h3>
<a name="Floating Point Functions"></a>
Floating Point Functions</h4>
<br>
<center>
<table>
<tr>
  <td width="25%"><code>F**</code></td><td width="25%">f1 f2 -- f3</td>
  <td>f3 = f1 raised to power of f2</td>
<tr>
  <td><code>FSQRT</code></td><td>f1 -- f2</td>
  <td>square root</td>
<tr>
  <td><code>FLOG</code></td><td>f1 -- f2</td>
  <td>f2 = log base 10 of f1</td>
<tr>
  <td><code>FEXP</code></td><td>f1 -- f2</td>
  <td>f2 = exp(f1)</td>
<tr>
  <td><code>FLN</code></td><td>f1 -- f2</td>
  <td>f2 = log base e of f1</td>
<tr>
  <td><code>DEG>RAD</code></td><td>f1 -- f2</td>
  <td>degrees to radians</td>
<tr>
  <td><code>RAD>DEG</code></td><td>f1 -- f2</td>
  <td>radians to degrees</td>
<tr>
  <td><code>FSIN</code></td><td>f1 -- f2</td><td>f2 = sin(f1)</td>
<tr>
  <td><code>FCOS</code></td><td>f1 -- f2</td><td>f2 = cos(f1)</td>
<tr>
  <td><code>FTAN</code></td><td>f1 -- f2</td><td>f2 = tan(f1)</td>
<tr>
  <td><code>FASIN</code></td><td>f1 -- f2</td>
  <td>arc sine</td>
<tr>
  <td><code>FACOS</code></td><td>f1 -- f2</td>
  <td>arc cosine</td>
<tr>
  <td><code>FATAN</code></td><td>f1 -- f2</td>
  <td>arc tangent</td>
<tr>
  <td><code>FATAN2</code></td><td>f1 f2 -- f3</td>
  <td>f3 is arc tangent of f1/f2</td>
</table>
</center>
<br><hr>

<h3>
<a name="Number Conversion"></a>
Number Conversion</h4>
<br>
<center>
<table>
<tr>
  <td width= "25%"><code>S>D</code></td><td width="25%">n -- d</td>
  <td>convert single integer to double length integer</td>
<tr>
  <td><code>S>F</code></td><td>n -- f</td>
  <td>convert single integer to floating point number</td>
<tr>
  <td><code>D>F</code></td><td>d -- f</td>
  <td>convert double length integer to fp number</td>
<tr>
  <td><code>F>S</code></td><td>f -- n</td>
  <td>convert floating point to integer by <i>rounding;</i></td>
  <tr><td></td><td></td><td>use <code>F>D</code> if 
  <i>truncation</i> is desired</td>
<tr>
  <td><code>F>D</code></td><td>f -- d</d>
  <td>convert fp number to double integer by truncation</td>
<tr>
  <td><code>NUMBER?</code></td><td>^str -- d b</td>
  <td>convert counted string to signed double number</td>
  <tr><td></td><td></td><td>b is TRUE if successfull</td>
</table>
</center>
<br><br>
Note that the kForth interpreter does not currently recognize 
double length integers, and the arithemtic words for double
numbers are not yet implemented. <code>NUMBER?</code> may be 
used to translate a counted string into a single length number 
by dropping the top half of the returned double number. Other useful
conversion words for converting strings to numbers and
vice-versa are given in 
<a href="strings.4th">
strings.4th</a>.<br><br> 
<hr>

<h3>
<a name="Input and Output"></a>
Input and Output</h4>
<br>
<center>
<table>
<tr>
  <td width="25%"><code>BASE</code></td><td width="25%"> -- a</td>
  <td>return the address containing current number base</td>
<tr>
  <td><code>DECIMAL</code></td><td> -- </td>
  <td>set the number base to ten</td>
<tr>
  <td><code>BINARY</code></td><td> -- </td>
  <td>set the number base to two</td>
<tr>
  <td><code>HEX</code></td><td> -- </td>
  <td>set the number base to sixteen</td>
<tr>
  <td><code>KEY</code></td><td> -- n</td>
  <td>wait for key press and return key code</td>
<tr>
  <td><code>ACCEPT</code></td><td>a n1 -- n2</td>
  <td>read up to n1 characters into buffer a</td>
<tr>
  <td></td><td></td><td>from keyboard. n2 is actual number input.</td>
<tr>
  <td><code>BL</code></td><td> -- 32</td>
  <td>return the ascii value for a blank space character</td>
<tr>
  <td><code>WORD</code></td><td>n -- ^str</td>
  <td>parse a word from the input stream, delimited by</td>
<tr>
  <td></td><td></td><td>character with ascii value n and return</td>
<tr>
  <td></td><td></td><td>the address of a counted string containing the word</td>
<tr>
  <td><code>CHAR</code></td><td> -- n</td>
  <td>parse the next word, delimited by a space and return</td>
<tr>
  <td></td><td></td><td>the ascii value of its first character</td>
<tr>
  <td><code>[CHAR]</code></td><td> -- n</td>
  <td>version of <code>CHAR</code> for use in compile state</td>
<tr>
  <td><code>.</code></td><td>n -- </td>
  <td>display top item on the stack in the current base</td>
<tr>
  <td><code>.R</code></td><td>n m -- </td>
  <td>display n in the current base in m-wide field</td>
<tr>
  <td><code>U.</code></td><td>u -- </td>
  <td>display unsigned single in current base</td>
<tr>
  <td><code>U.R</code></td><td>u m -- </td>
  <td>display u in the current base in m-wide field</td>
<tr>
  <td><code>F.</code></td><td>f -- </td>
  <td>display the floating point value on top of the stack</td>
<tr>
  <td><code>.S</code></td><td>n1 n2 ... -- n1 n2 ...</td>
  <td>non-destructive display of the stack</td>
<tr>
  <td><code>."</code></td><td> -- </td>
  <td>display text message; the message is read from</td>
<tr><td></td><td></td>
  <td>the input stream and must be terminated by "</td>
<tr>
  <td><code>CR</code></td><td> -- </td>
  <td>output carriage return</td>
<tr>
  <td><code>SPACES</code></td><td>n -- </td>
  <td>output n spaces</td>
<tr>
  <td><code>EMIT</code></td><td>n -- </td>
  <td>output character with ascii value n</td>
<tr>
  <td><code>TYPE</code></td><td>a n -- </td>
  <td>display n characters from buffer at a</td>
<tr>
 <td><code>>FILE</code></td><td> -- </td>
 <td>change output stream from the console to a file.</td>
<tr><td></td><td></td><td>The filename is the next word in the input stream</td>
<tr>
 <td><code>CONSOLE</code></td><td> -- </td>
 <td>reset output stream to the console</td>
</table>
</center>
<br><br>
<code>PAGE</code> and other terminal control words
for ANSI terminals are provided in the form of source 
code in 
<a href="ansi.4th">ansi.4th</a><br>
<hr>

<h3>
<a name="File Access"></a>
File Access</h4>
<br>
<center>
<table>
<tr>
  <td width="25%"><code>OPEN</code></td><td width="25%">^name n1 -- n2</td>
  <td>open file specified by counted string ^name</td>
<tr><td></td><td></td><td>in mode n1, which can be the following:</td>
<tr><td></td><td></td><td>0  read-only (R/O)</td>
<tr><td></td><td></td><td>1  write-only (W/O)</td>
<tr><td></td><td></td><td>2  read-write (R/W)</td>
<tr><td></td><td></td><td>n2 is the file descriptor, a non-negative</td>
<tr><td></td><td></td><td>integer if successful.</td>
<tr>
  <td><code>LSEEK</code></td><td>n1 n2 n3 -- n4</td>
  <td>change current position in opened file</td>
<tr><td></td><td></td><td>n1 is the file descriptor</td>
<tr><td></td><td></td><td>n2 is the offset, and</td>
<tr><td></td><td></td><td>n3 is the mode with the following meaning:</td>
<tr><td></td><td></td><td>0  offset is relative to start of file</td>
<tr><td></td><td></td><td>1  offset is relative to current position</td>
<tr><td></td><td></td><td>2  offset is relative to end of file</td>
<tr><td></td><td></td><td>n4 is the resulting offset from the</td>
<tr><td></td><td></td><td>beginning of the file, or -1 if error.</td>
<tr>
  <td><code>READ</code></td><td>n1 a n2 -- n3</td>
  <td>read n2 bytes into buffer address a, from</td>
<tr><td></td><td></td><td>file with descriptor n1.</td>
<tr><td></td><td></td><td>n3 is the number of bytes actually read.</td>
<tr>
  <td><code>WRITE</code></td><td>n1 a n2 -- n3</td>
  <td>write n2 bytes from buffer address a to</td>
<tr><td></td><td></td><td>file with descriptor n1.</td>
<tr><td></td><td></td><td>n3 is the number of bytes actually written.</td>
<tr>
  <td><code>CLOSE</code></td><td>n1 -- n2</td>
  <td>close file with descriptor n1 and return</td>
<tr><td></td><td></td><td>status n2 (0 if successful, -1 if error).</td>
</table>
</center>
<br><br>
The ANS standard file access words are provided as
Forth definitions in terms of these low-level words
in <a href="files.4th">
files.4th</a> (Linux) and 
<a href="filesw.4th">
filesw.4th</a> (Win95/98/NT).<br><hr>

<h3>
<a name="Operating System Interface"></a>
Operating System Interface</h4>
<br>
<center>
<table>
<tr>
  <td width="25%"><code>SYSTEM</code></td><td width="25%">^str -- n</td>
  <td>execute another process; ^str is the command line passed</td>
<tr>
  <td></td><td></td><td>to the operating system. n is the return code</td>
<tr>
  <td></td><td></td><td>and is OS dependent.</td>
<tr>
  <td><code>BYE</code></td><td> -- </td>
  <td>close the Forth environment and exit to the system.</td>
<tr>
  <td><code>CHDIR</code></td><td>^path -- n</td>
  <td>change the current directory to the one specified in</td>
<tr>
  <td></td><td></td><td>the counted string ^path; n is OS dependent return code</td>
<tr>
  <td><code>IOCTL</code></td><td>n1 n2 a -- n3</td>
  <td>send device control request n2 to file</td>
<tr><td></td><td></td><td>with descriptor n1 (Linux only). Additional parameters</td>
<tr><td></td><td></td><td>are passed through buffer at address a.</td>
<tr><td></td><td></td><td>n3 is the status (0 if successful, -1 if error).</td>
<tr>
  <td><code>TIME&DAY</code></td><td> -- sec min hr day mo yr</td>
  <td>return the local time</td>
<tr>
  <td><code>MS</code></td><td>u -- </td>
  <td>wait for at least u milliseconds</td>
<tr>
  <td><code>USLEEP</code></td><td>u -- </td>
  <td>wait for at least u microseconds</td>
<tr><td></td><td></td><td>(For Windows version, resolution is 1000)</td>
</table>
</center><br>
<hr>
<h3>
<a name="Miscellaneous"></a>Miscellaneous</h4>
<br>
<center>
<table>
<tr>
  <td width="25%"><code>CALL</code></td><td width="25%">a -- </td>
  <td>call machine language subroutine at address a</td>
</table>
</center>
<br><hr>
<a href="kforth2d.html"><img src="left.gif"></a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="kforth4.html"><img src="right.gif"></a><br><br>
Copyright &copy 1998--2001 
<a href="http://www.ccreweb.org">
Creative Consulting for Research and Education</a>
</body>
</html>