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 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 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 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
|
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (Win95; I) [Netscape]">
<META NAME="Author" CONTENT="Phil Burk">
<META NAME="Description" CONTENT="Tutorial for pForth language.">
<META NAME="KeyWords" CONTENT="Forth, tutorial, pForth">
<TITLE>pForth Tutorial</TITLE>
</HEAD>
<BODY BACKGROUND="r2harch.gif">
<HR size=4>
<CENTER>
<H1>
Forth Tutorial</H1></CENTER>
<HR WIDTH="100%">
<P>by <A HREF="http://www.softsynth.com/philburk.html">Phil Burk</A>
<P>To <A HREF="pforth.html">pForth Home Page</A>
<H2>
Table of Contents</H2>
<UL>
<LI>
<A HREF="#Forth Syntax">Forth Syntax</A></LI>
<LI>
<A HREF="#The Stack">Stack Manipulation</A></LI>
<LI>
<A HREF="#Arithmetic">Arithmetic</A></LI>
<LI>
<A HREF="#Defining a New Word">Defining a New Word</A></LI>
<LI>
<A HREF="#More Arithmetic">More Arithmetic</A></LI>
<UL>
<LI>
<A HREF="#Arithmetic Overflow">Arithmetic Overflow</A></LI>
<LI>
<A HREF="#Convert Algebraic Expressions to Forth">Convert Algebraic Expressions
to Forth</A></LI>
</UL>
<LI>
<A HREF="#Character Input and Output">Character Input and Output</A></LI>
<LI>
<A HREF="#Compiling from Files">Compiling from Files</A></LI>
<LI>
<A HREF="#Variables">Variables</A></LI>
<LI>
<A HREF="#Constants">Constants</A></LI>
<LI>
<A HREF="#Logical Operators">Logical Operators</A></LI>
<LI>
<A HREF="#Conditionals - IF ELSE THEN CASE">Conditionals - IF ELSE THEN
CASE</A></LI>
<LI>
<A HREF="#Loops">Loops</A></LI>
<LI>
<A HREF="#Text Input and Output">Text Input and Output</A></LI>
<LI>
<A HREF="#Changing Numeric Base">Changing Numeric Base</A></LI>
<LI>
<A HREF="#Answers to Problems">Answers to Problems</A></LI>
</UL>
The intent of this tutorial is to provide a series of experiments that
will introduce you to the major concepts of Forth. It is only a starting
point. Feel free to deviate from the sequences I provide. A free form investigation
that is based on your curiosity is probably the best way to learn any language.
Forth is especially well adapted to this type of learning.
<P>This tutorial is written for the PForth implementation of the ANS Forth
standard. I have tried to restrict this tutorial to words that are part
of the ANS standard but some PForth specific words may have crept in.
<P>In the tutorials, I will print the things you need to type in upper
case, and indent them. You can enter them in upper or lower case. At the
end of each line, press the RETURN (or ENTER) key; this causes Forth to
interpret what you've entered.
<H2>
<A NAME="Forth Syntax"></A>Forth Syntax</H2>
Forth has one of the simplest syntaxes of any computer language. The syntax
can be stated as follows, "<B>Forth code is a bunch of words with spaces
between them.</B>" This is even simpler than English! Each <I>word</I>
is equivalent to a function or subroutine in a language like 'C'. They
are executed in the order they appear in the code. The following statement,
for example, could appear in a Forth program:
<UL>
<PRE> <TT>WAKE.UP EAT.BREAKFAST WORK EAT.DINNER PLAY SLEEP</TT></PRE>
</UL>
Notice that WAKE.UP has a dot between the WAKE and UP. The dot has no particular
meaning to the Forth compiler. I simply used a dot to connect the two words
together to make one word. Forth word names can have any combination of
letters, numbers, or punctuation. We will encounter words with names like:
<UL>
<PRE> ." #S SWAP ! @ ACCEPT . *</PRE>
</UL>
They are all called <I>words</I>. The word <B>$%%-GL7OP</B> is a legal
Forth name, although not a very good one. It is up to the programmer to
name words in a sensible manner.
<P>Now it is time to run your Forth and begin experimenting. Please consult
the manual for your Forth for instructions on how to run it.
<H2>
<A NAME="The Stack"></A>Stack Manipulation</H2>
The Forth language is based on the concept of a <I>stack</I>. Imagine a
stack of blocks with numbers on them. You can add or remove numbers from
the top of the stack. You can also rearrange the order of the numbers.
Forth uses several stacks. The <I>DataStack </I>is the one used for passing
data between Forth words so we will concentrate our attention there. The
<I>Return Stack</I> is another Forth stack that is primarily for internal
system use. In this tutorial, when we refer to the "stack," we will be
referring to the Data Stack.
<P>The stack is initially empty. To put some numbers on the stack, enter:
<UL>
<PRE><TT>23 7 9182</TT></PRE>
</UL>
Let's now print the number on top of the stack using the Forth word ' <B>.</B>
', which is pronounced " dot ". This is a hard word to write about in a
manual because it is a single period.
<P>Enter: <B>. </B>
<P>You should see the last number you entered, 9182 , printed. Forth has
a very handy word for showing you what's on the stack. It is <B>.S</B>
, which is pronounced "dot S". The name was constructed from "dot" for
print, and "S" for stack. (PForth will automatically print the stack after
every line if the TRACE-STACK variable is set to TRUE.) If you enter:
<UL>
<PRE><TT>.S</TT></PRE>
</UL>
you will see your numbers in a list. The number at the far right is the
one on top of the stack.
<P>You will notice that the 9182 is not on the stack. The word ' . ' removes
the number on top of the stack before printing it. In contrast, ' .S '
leaves the stack untouched.
<P>We have a way of documenting the effect of words on the stack with a
<I>stack diagram</I>. A stack diagram is contained in parentheses. In Forth,
the parentheses indicate a comment. In the examples that follow, you do
not need to type in the comments. When you are programming, of course,
we encourage the use of comments and stack diagrams to make your code more
readable. In this manual, we often indicate stack diagrams in <B>bold text</B>
like the one that follows. Do not type these in. The stack diagram for
a word like ' . ' would be:
<PRE><B><TT>. ( N -- , print number on top of stack )</TT></B></PRE>
The symbols to the left of -- describe the parameters that a word expects
to process. In this example, N stands for any integer number. To the right
of --, up to the comma, is a description of the stack parameters when the
word is finished, in this case there are none because 'dot' "eats" the
N that was passed in. (Note that the stack descriptions are not necessary,
but they are a great help when learning other peoples programs.)
<P>The text following the comma is an English description of the word.
You will note that after the -- , N is gone. You may be concerned about
the fact that there were other numbers on the stack, namely 23 and 7 .
The stack diagram, however, only describes the portion of the stack that
is affected by the word. For a more detailed description of the stack diagrams,
there is a special section on them in this manual right before the main
glossary section.
<P>Between examples, you will probably want to clear the stack. If you
enter <B>0SP</B>, pronounced "zero S P", then the stack will be cleared.
<P>Since the stack is central to Forth, it is important to be able to alter
the stack easily. Let's look at some more words that manipulate the stack.
Enter:
<UL>
<PRE><TT>0SP .S \ That's a 'zero' 0, not an 'oh' O.
777 DUP .S</TT></PRE>
</UL>
You will notice that there are two copies of 777 on the stack. The word
<B>DUP</B> duplicates the top item on the stack. This is useful when you
want to use the number on top of the stack and still have a copy. The stack
diagram for DUP would be:
<PRE><B><TT>DUP ( n -- n n , DUPlicate top of stack )</TT></B></PRE>
Another useful word, is <B>SWAP</B>. Enter:
<UL>
<PRE><TT>0SP
23 7 .S
SWAP .S
SWAP .S</TT></PRE>
</UL>
The stack diagram for SWAP would be:
<PRE><B><TT>SWAP ( a b -- b a , swap top two items on stack )</TT></B></PRE>
Now enter:
<UL>
<PRE><TT>OVER .S
OVER .S</TT></PRE>
</UL>
The word <B>OVER</B> causes a copy of the second item on the stack to leapfrog
over the first. It's stack diagram would be:
<P><B><TT>OVER ( a b -- a b a , copy second item on stack )</TT></B>
<P>Here is another commonly used Forth word:
<P><B><TT>DROP ( a -- , remove item from the stack )</TT></B>
<P>Can you guess what we will see if we enter:
<UL>
<PRE><TT>0SP 11 22 .S
DROP .S</TT></PRE>
</UL>
Another handy word for manipulating the stack is <B>ROT</B>. Enter:
<UL>
<PRE><TT>0SP
11 22 33 44 .S
ROT .S</TT></PRE>
</UL>
The stack diagram for ROT is, therefore:
<P><B><TT>ROT ( a b c -- b c a , ROTate third item to top ) </TT></B>
<P>You have now learned the more important stack manipulation words. You
will see these in almost every Forth program. I should caution you that
if you see too many stack manipulation words being used in your code then
you may want to reexamine and perhaps reorganize your code. You will often
find that you can avoid excessive stack manipulations by using <I>local
or global VARIABLES</I> which will be discussed later.
<P>If you want to grab any arbitrary item on the stack, use <B>PICK</B>
. Try entering:
<UL>
<PRE><TT>0SP
14 13 12 11 10
3 PICK . ( prints 13 )
0 PICK . ( prints 10 )
4 PICK .</TT></PRE>
</UL>
PICK makes a copy of the Nth item on the stack. The numbering starts with
zero, therefore:
<UL><TT>0 PICK is equivalent to DUP</TT>
<BR><TT>1 PICK is equivalent to OVER </TT></UL>
<B><TT>PICK ( ... v3 v2 v1 v0 N -- ... v3 v2 v1 v0 vN ) </TT></B>
<P>(Warning. The Forth-79 and FIG Forth standards differ from the ANS and
Forth '83 standard in that their PICK numbering starts with one, not zero.)
<P>I have included the stack diagrams for some other useful stack manipulation
words. Try experimenting with them by putting numbers on the stack and
calling them to get a feel for what they do. Again, the text in parentheses
is just a comment and need not be entered.
<P><B><TT>DROP ( n -- , remove top of stack ) </TT></B>
<P><B><TT>?DUP ( n -- n n | 0 , duplicate only if non-zero, '|' means OR
) </TT></B>
<P><B><TT>-ROT ( a b c -- c a b , rotate top to third position ) </TT></B>
<P><B><TT>2SWAP ( a b c d -- c d a b , swap pairs ) </TT></B>
<P><B><TT>2OVER ( a b c d -- a b c d a b , leapfrog pair ) </TT></B>
<P><B><TT>2DUP ( a b -- a b a b , duplicate pair ) </TT></B>
<P><B><TT>2DROP ( a b -- , remove pair ) </TT></B>
<P><B><TT>NIP ( a b -- b , remove second item from stack ) </TT></B>
<P><B><TT>TUCK ( a b -- b a b , copy top item to third position ) </TT></B>
<H3>
<A NAME="Problems - Stack"></A>Problems:</H3>
Start each problem by entering:
<UL>
<PRE><TT>0SP 11 22 33</TT></PRE>
</UL>
Then use the stack manipulation words you have learned to end up with the
following numbers on the stack:
<UL>
<PRE><TT>1) 11 33 22 22</TT></PRE>
<PRE><TT>2) 22 33</TT></PRE>
<PRE><TT>3) 22 33 11 11 22</TT></PRE>
<PRE><TT>4) 11 33 22 33 11</TT></PRE>
<PRE><TT>5) 33 11 22 11 22</TT></PRE>
</UL>
<A HREF="#Answers to Problems">Answers to the problems</A> can be found
at the end of this tutorial.
<H2>
<A NAME="Arithmetic"></A>Arithmetic</H2>
Great joy can be derived from simply moving numbers around on a stack.
Eventually, however, you'll want to do something useful with them. This
section describes how to perform arithmetic operations in Forth.
<P>The Forth arithmetic operators work on the numbers currently on top
of the stack. If you want to add the top two numbers together, use the
Forth word <B>+</B> , pronounced "plus". Enter:
<UL>
<PRE><TT>2 3 + .
2 3 + 10 + .</TT></PRE>
</UL>
This style of expressing arithmetic operations is called <I>Reverse Polish
Notation,</I> or<I> RPN</I>. It will already be familiar to those of you
with HP calculators. In the following examples, I have put the algebraic
equivalent representation in a comment.
<P>Some other arithmetic operators are <B>- * /</B> . Enter:
<UL>
<PRE><TT>30 5 - . ( 25=30-5 )
30 5 / . ( 6=30/5 )
30 5 * . ( 150=30*5 )
30 5 + 7 / . \ 5=(30+5)/7</TT></PRE>
</UL>
Some combinations of operations are very common and have been coded in
assembly language for speed. For example, <B>2*</B> is short for 2 * .
You should use these whenever possible to increase the speed of your program.
These include:
<UL>
<PRE><TT>1+ 1- 2+ 2- 2* 2/</TT></PRE>
</UL>
Try entering:
<UL>
<PRE><TT>10 1- .
7 2* 1+ . ( 15=7*2+1 )</TT></PRE>
</UL>
One thing that you should be aware of is that when you are doing division
with integers using / , the remainder is lost. Enter:
<UL>
<PRE><TT>15 5 / .
17 5 / .</TT></PRE>
</UL>
This is true in all languages on all computers. Later we will examine <B>/MOD</B>
and <B>MOD</B> which do give the remainder.
<H2>
<A NAME="Defining a New Word"></A>Defining a New Word</H2>
It's now time to write a <I>small program</I> in Forth. You can do this
by defining a new word that is a combination of words we have already learned.
Let's define and test a new word that takes the average of two numbers.
<DT>
We will make use of two new words, <B>:</B> ( "colon"), and <B>;</B> (
"semicolon") . These words start and end a typical <I>Forth definition</I>.
Enter:</DT>
<UL>
<PRE><TT>: AVERAGE ( a b -- avg ) + 2/ ;</TT></PRE>
</UL>
Congratulations. You have just written a Forth program. Let's look more
closely at what just happened. The colon told Forth to add a new word to
its list of words. This list is called the Forth dictionary. The name of
the new word will be whatever name follows the colon. Any Forth words entered
after the name will be compiled into the new word. This continues until
the semicolon is reached which finishes the definition.
<P>Let's test this word by entering:
<UL>
<PRE><TT>10 20 AVERAGE . ( should print 15 )</TT></PRE>
</UL>
Once a word has been defined, it can be used to define more words. Let's
write a word that tests our word.. Enter:
<UL>
<PRE><TT>: TEST ( --) 50 60 AVERAGE . ;
TEST</TT></PRE>
</UL>
Try combining some of the words you have learned into new Forth definitions
of your choice. If you promise not to be overwhelmed, you can get a list
of the words that are available for programming by entering:
<UL>
<PRE><TT>WORDS</TT></PRE>
</UL>
Don't worry, only a small fraction of these will be used directly in your
programs.
<H2>
<A NAME="More Arithmetic"></A>More Arithmetic</H2>
When you need to know the remainder of a divide operation. /MOD will return
the remainder as well as the quotient. the word MOD will only return the
remainder. Enter:
<UL>
<PRE><TT>0SP
53 10 /MOD .S
0SP
7 5 MOD .S</TT></PRE>
</UL>
Two other handy words are <B>MIN</B> and <B>MAX</B> . They accept two numbers
and return the MINimum or MAXimum value respectively. Try entering the
following:
<UL>
<PRE><TT>56 34 MAX .
56 34 MIN .
-17 0 MIN .</TT></PRE>
</UL>
Some other useful words are:
<P><B><TT>ABS ( n -- abs(n) , absolute value of n ) </TT></B>
<P><B><TT>NEGATE ( n -- -n , negate value, faster then -1 * ) </TT></B>
<P><B><TT>LSHIFT ( n c -- n<<c , left shift of n ) </TT></B>
<P><B><TT>RSHIFT ( n c -- n>>c , logical right shift of n ) </TT></B>
<P><B><TT>ARSHIFT ( n c -- n>>c ) , arithmetic right shift of n ) </TT></B>
<P>ARSHIFT or LSHIFT can be used if you have to multiply quickly by a power
of 2 . A right shift is like doing a divide by 2. This is often faster
than doing a regular multiply or divide. Try entering:
<UL>
<PRE><TT>: 256* 8 LSHIFT ;
3 256* .</TT></PRE>
</UL>
<H3>
<A NAME="Arithmetic Overflow"></A>Arithmetic Overflow</H3>
If you are having problems with your calculation overflowing the 32-bit
precision of the stack, then you can use <B>*/</B> . This produces an intermediate
result that is 64 bits long. Try the following three methods of doing the
same calculation. Only the one using */ will yield the correct answer,
5197799.
<UL>
<PRE><TT>34867312 99154 * 665134 / .
34867312 665134 / 99154 * .
34867312 99154 665134 */ .</TT></PRE>
</UL>
<H4>
<A NAME="Convert Algebraic Expressions to Forth"></A>Convert Algebraic
Expressions to Forth</H4>
How do we express complex algebraic expressions in Forth? For example:
20 + (3 * 4)
<P>To convert this to Forth you must order the operations in the order
of evaluation. In Forth, therefore, this would look like:
<UL>
<PRE><TT>3 4 * 20 +</TT></PRE>
</UL>
Evaluation proceeds from left to right in Forth so there is no ambiguity.
Compare the following algebraic expressions and their Forth equivalents:
(Do <B>not</B> enter these!)
<UL>
<PRE>(100+50)/2 ==> 100 50 + 2/
((2*7) + (13*5)) ==> 2 7 * 13 5 * +</PRE>
</UL>
If any of these expressions puzzle you, try entering them one word at a
time, while viewing the stack with .S .
<H3>
<A NAME="Problems - Square"></A>Problems:</H3>
Convert the following algebraic expressions to their equivalent Forth expressions.
(Do <B>not</B> enter these because they are not Forth code!)
<UL>
<PRE>(12 * ( 20 - 17 ))</PRE>
<PRE>(1 - ( 4 * (-18) / 6) )</PRE>
<PRE>( 6 * 13 ) - ( 4 * 2 * 7 )</PRE>
</UL>
Use the words you have learned to write these new words:
<UL>
<PRE><TT>SQUARE ( N -- N*N , calculate square )</TT></PRE>
<PRE><TT>DIFF.SQUARES ( A B -- A*A-B*B , difference of squares )</TT></PRE>
<PRE><TT>AVERAGE4 ( A B C D -- [A+B+C+D]/4 )</TT></PRE>
<PRE>HMS>SECONDS ( HOURS MINUTES SECONDS -- TOTAL-SECONDS , convert )</PRE>
</UL>
<A HREF="#Answers to Problems">Answers to the problems</A> can be found
at the end of this tutorial.
<H2>
<A NAME="Character Input and Output"></A>Character Input and Output</H2>
The numbers on top of the stack can represent anything. The top number
might be how many blue whales are left on Earth or your weight in kilograms.
It can also be an ASCII character. Try entering the following:
<UL>
<PRE><TT>72 EMIT 105 EMIT</TT></PRE>
</UL>
You should see the word "Hi" appear before the OK. The 72 is an ASCII 'H'
and 105 is an 'i'. EMIT takes the number on the stack and outputs it as
a character. If you want to find the ASCII value for any character, you
can use the word ASCII . Enter:
<UL>
<PRE><TT>CHAR W .
CHAR % DUP . EMIT
CHAR A DUP .
32 + EMIT</TT></PRE>
</UL>
There is an ASCII chart in the back of this manual for a complete character
list.
<P>Notice that the word CHAR is a bit unusual because its input comes not
from the stack, but from the following text. In a stack diagram, we represent
that by putting the input in angle brackets, <input>. Here is the stack
diagram for CHAR.
<P><B><TT>CHAR ( <char> -- char , get ASCII value of a character ) </TT></B>
<P>Using EMIT to output character strings would be very tedious. Luckily
there is a better way. Enter:
<UL>
<PRE><TT>: TOFU ." Yummy bean curd!" ;
TOFU</TT></PRE>
</UL>
The word <B>."</B> , pronounced "dot quote", will take everything up to
the next quotation mark and print it to the screen. Make sure you leave
a space after the first quotation mark. When you want to have text begin
on a new line, you can issue a carriage return using the word <B>CR</B>
. Enter:
<UL>
<PRE><TT>: SPROUTS ." Miniature vegetables." ;
: MENU
CR TOFU CR SPROUTS CR
;
MENU</TT></PRE>
</UL>
You can emit a blank space with <B>SPACE</B> . A number of spaces can be
output with SPACES . Enter:
<UL>
<PRE><TT>CR TOFU SPROUTS
CR TOFU SPACE SPROUTS
CR 10 SPACES TOFU CR 20 SPACES SPROUTS</TT></PRE>
</UL>
For character input, Forth uses the word <B>KEY</B> which corresponds to
the word EMIT for output. KEY waits for the user to press a key then leaves
its value on the stack. Try the following.
<UL>
<PRE><TT>: TESTKEY ( -- )
." Hit a key: " KEY CR
." That = " . CR
;
TESTKEY</TT></PRE>
</UL>
[Note: On some computers, the input if buffered so you will need to hit
the ENTER key after typing your character.]
<P><B><TT>EMIT ( char -- , output character ) </TT></B>
<P><B><TT>KEY ( -- char , input character ) </TT></B>
<P><B><TT>SPACE ( -- , output a space ) </TT></B>
<P><B><TT>SPACES ( n -- , output n spaces ) </TT></B>
<P><B><TT>CHAR ( <char> -- char , convert to ASCII ) </TT></B>
<P><B><TT>CR ( -- , start new line , carriage return ) </TT></B>
<P><B><TT>." ( -- , output " delimited text ) </TT></B>
<H2>
<BR>
<BR>
<A NAME="Compiling from Files"></A>Compiling from Files</H2>
PForth can read read from ordinary text files so you can use any editor
that you wish to write your programs.
<H3>
Sample Program</H3>
Enter into your file, the following code.
<UL>
<PRE><TT>\ Sample Forth Code
\ Author: <I>your name</I></TT></PRE>
<PRE><TT>: SQUARE ( n -- n*n , square number )
DUP *
;</TT></PRE>
<PRE><TT>: TEST.SQUARE ( -- )
CR ." 7 squared = "
7 SQUARE . CR
;</TT></PRE>
</UL>
Now save the file to disk.
<P>The text following the <B>\</B> character is treated as a comment. This
would be a REM statement in BASIC or a /*---*/ in 'C'. The text in parentheses
is also a comment.
<H3>
Using INCLUDE</H3>
"INCLUDE" in Forth means to compile from a file.
<P>You can compile this file using the INCLUDE command. If you saved your
file as WORK:SAMPLE, then compile it by entering:
<UL>
<PRE><TT>INCLUDE SAMPLE.FTH</TT></PRE>
</UL>
Forth will compile your file and tell you how many bytes it has added to
the dictionary. To test your word, enter:
<UL>
<PRE><TT>TEST.SQUARE</TT></PRE>
</UL>
Your two words, SQUARE and TEST.SQUARE are now in the Forth dictionary.
We can now do something that is very unusual in a programming language.
We can "uncompile" the code by telling Forth to <B>FORGET</B> it. Enter:
<UL>
<PRE><TT>FORGET SQUARE</TT></PRE>
</UL>
This removes SQUARE and everything that follows it, ie. TEST.SQUARE, from
the dictionary. If you now try to execute TEST.SQUARE it won't be found.
<P>Now let's make some changes to our file and reload it. Go back into
the editor and make the following changes: (1) Change TEST.SQUARE to use
15 instead of 7 then (2) Add this line right before the definition of SQUARE:
<UL>
<PRE><TT>ANEW TASK-SAMPLE.FTH</TT></PRE>
</UL>
Now Save your changes and go back to the Forth window.
<P>You're probably wondering what the line starting with <B>ANEW</B> was
for. ANEW is always used at the beginning of a file. It defines a special
marker word in the dictionary before the code. The word typically has "TASK-"
as a prefix followed by the name of the file. When you ReInclude a file,
ANEW will automatically FORGET the old code starting after the ANEW statement.
This allows you to Include a file over and over again without having to
manually FORGET the first word. If the code was not forgotten, the dictionary
would eventually fill up.
<P>If you have a big project that needs lots of files, you can have a file
that will load all the files you need. Sometimes you need some code to
be loaded that may already be loaded. The word <B>INCLUDE?</B> will only
load code if it isn't already in the dictionary. In this next example,
I assume the file is on the volume WORK: and called SAMPLE. If not, please
substitute the actual name. Enter:
<UL>
<PRE><TT>FORGET TASK-SAMPLE.FTH
INCLUDE? SQUARE WORK:SAMPLE
INCLUDE? SQUARE WORK:SAMPLE</TT></PRE>
</UL>
Only the first INCLUDE? will result in the file being loaded.
<H2>
<A NAME="Variables"></A>Variables</H2>
Forth does not rely as heavily on the use of variables as other compiled
languages. This is because values normally reside on the stack. There are
situations, of course, where variables are required. To create a variable,
use the word <B>VARIABLE</B> as follows:
<UL>
<PRE><TT>VARIABLE MY-VAR</TT></PRE>
</UL>
This created a variable named MY-VAR . A space in memory is now reserved
to hold its 32-bit value. The word VARIABLE is what's known as a "defining
word" since it creates new words in the dictionary. Now enter:
<UL>
<PRE><TT>MY-VAR .</TT></PRE>
</UL>
The number you see is the address, or location, of the memory that was
reserved for MY-VAR. To store data into memory you use the word <B>!</B>
, pronounced "store". It looks like an exclamation point, but to a Forth
programmer it is the way to write 32-bit data to memory. To read the value
contained in memory at a given address, use the Forth word <B>@</B> , pronounced
"fetch". Try entering the following:
<UL>
<PRE><TT>513 MY-VAR !
MY-VAR @ .</TT></PRE>
</UL>
This sets the variable MY-VAR to 513 , then reads the value back and prints
it. The stack diagrams for these words follows:
<P><B><TT>@ ( address -- value , FETCH value FROM address in memory ) </TT></B>
<P><B><TT>! ( value address -- , STORE value TO address in memory )</TT></B>
<P><B><TT>VARIABLE ( <name> -- , define a 4 byte memory storage location)</TT></B>
<P>A handy word for checking the value of a variable is <B>? </B>, pronounced
"question". Try entering:
<UL>
<PRE><TT>MY-VAR ?</TT></PRE>
</UL>
If ? wasn't defined, we could define it as:
<UL>
<PRE><TT>: ? ( address -- , look at variable )
@ .
;</TT></PRE>
</UL>
Imagine you are writing a game and you want to keep track of the highest
score. You could keep the highest score in a variable. When you reported
a new score, you could check it aginst the highest score. Try entering
this code in a file as described in the previous section:
<UL>
<PRE><TT>VARIABLE HIGH-SCORE</TT></PRE>
<PRE><TT>: REPORT.SCORE ( score -- , print out score )
DUP CR ." Your Score = " . CR
HIGH-SCORE @ MAX ( calculate new high )
DUP ." Highest Score = " . CR
HIGH-SCORE ! ( update variable )
;</TT></PRE>
</UL>
Save the file to disk, then compile this code using the INCLUDE word. Test
your word as follows:
<UL>
<PRE><TT>123 REPORT.SCORE
9845 REPORT.SCORE
534 REPORT.SCORE</TT></PRE>
</UL>
The Forth words @ and ! work on 32-bit quantities. Some Forths are "16-bit"
Forths. They fetch and store 16-bit quantities. Forth has some words that
will work on 8 and 16-bit values. C@ and C! work characters which are usually
for 8-bit bytes. The 'C' stands for "Character" since ASCII characters
are 8-bit numbers. Use W@ and W! for 16-bit "Words."
<P>Another useful word is <B>+!</B> , pronounced "plus store." It adds
a value to a 32-bit value in memory. Try:
<UL>
<PRE><TT>20 MY-VAR !
5 MY-VAR +!
MY-VAR @ .</TT></PRE>
</UL>
Forth also provides some other words that are similar to VARIABLE. Look
in the glossary for VALUE and ARRAY. Also look at the section on "<A HREF="pf_ref.htm#Local Variables { foo --}?">local
variables</A>" which are variables which only exist on the stack while
a Forth word is executing.
<P><I>A word of warning about fetching and storing to memory</I>: You have
now learned enough about Forth to be dangerous. The operation of a computer
is based on having the right numbers in the right place in memory. You
now know how to write new numbers to any place in memory. Since an address
is just a number, you could, but shouldn't, enter:
<UL>
<PRE><TT>73 253000 ! ( Do NOT do this. )</TT></PRE>
</UL>
The 253000 would be treated as an address and you would set that memory
location to 73. I have no idea what will happen after that, maybe nothing.
This would be like firing a rifle through the walls of your apartment building.
You don't know who or what you are going to hit. Since you share memory
with other programs including the operating system, you could easily cause
the computer to behave strangely, even crash. Don't let this bother you
too much, however. Crashing a computer, unlike crashing a car, does not
hurt the computer. You just have to reboot. The worst that could happen
is that if you crash while the computer is writing to a disk, you could
lose a file. That's why we make backups. This same potential problem exists
in any powerful language, not just Forth. This might be less likely in
BASIC, however, because BASIC protects you from a lot of things, including
the danger of writing powerful programs.
<P>Another way to get into trouble is to do what's called an "odd address
memory access." The 68000 processor arranges words and longwords, 16 and
32 bit numbers, on even addresses. If you do a <B>@</B> or <B>!</B> , or
<B>W@</B> or <B>W!</B> , to an odd address, the 68000 processor will take
exception to this and try to abort.
<P>Forth gives you some protection from this by trapping this exception
and returning you to the OK prompt. If you really need to access data on
an odd address, check out the words <B>ODD@</B> and <B>ODD!</B> in the
glossary. <B>C@</B> and <B>C!</B> work fine on both odd and even addresses.
<H2>
<A NAME="Constants"></A>Constants</H2>
If you have a number that is appearing often in your program, we recommend
that you define it as a "constant." Enter:
<UL>
<PRE><TT>128 CONSTANT MAX_CHARS
MAX_CHARS .</TT></PRE>
</UL>
We just defined a word called MAX_CHARS that returns the value on the stack
when it was defined. It cannot be changed unless you edit the program and
recompile. Using <B>CONSTANT</B> can improve the readability of your programs
and reduce some bugs. Imagine if you refer to the number 128 very often
in your program, say 8 times. Then you decide to change this number to
256. If you globally change 128 to 256 you might change something you didn't
intend to. If you change it by hand you might miss one, especially if your
program occupies more than one file. Using CONSTANT will make it easy to
change. The code that results is equally as fast and small as putting the
numbers in directly. I recommend defining a constant for almost any number.
<H2>
<A NAME="Logical Operators"></A>Logical Operators</H2>
These next two sections are concerned with decision making. This first
section deals with answering questions like "Is this value too large?"
or "Does the guess match the answer?". The answers to questions like these
are either TRUE or FALSE. Forth uses a 0 to represent <B>FALSE</B> and
a -1 to represent <B>TRUE</B>. TRUE and FALSE have been capitalized because
they have been defined as Forth constants. Try entering:
<UL>
<PRE><TT>23 71 = .
18 18 = .</TT></PRE>
</UL>
You will notice that the first line printed a 0, or FALSE, and the second
line a -1, or TRUE. The equal sign in Forth is used as a question, not
a statement. It asks whether the top two items on the stack are equal.
It does not set them equal. There are other questions that you can ask.
Enter:
<UL>
<PRE><TT>23 198 < .
23 198 > .
254 15 > .</TT></PRE>
</UL>
In California, the drinking age for alcohol is 21. You could write a simple
word now to help bartenders. Enter:
<UL>
<PRE><TT>: DRINK? ( age -- flag , can this person drink? )
20 >
;</TT></PRE>
<PRE><TT>20 DRINK? .
21 DRINK? .
43 DRINK? .</TT></PRE>
</UL>
The word FLAG in the stack diagram above refers to a logical value.
<P>Forth provides special words for comparing a number to 0. They are <B>0=</B>
<B>0></B> and <B>0<</B> . Using 0> is faster than calling 0 and > separately.
Enter:
<UL><TT>23 0> . ( print -1 )</TT>
<BR><TT>-23 0> . ( print 0 )</TT>
<BR><TT>23 0= . ( print 0 )</TT></UL>
For more complex decisions, you can use the <I>Boolean</I> operators <B>OR</B>
, <B>AND</B> , and <B>NOT</B> . OR returns a TRUE if either one or both
of the top two stack items are true.
<UL>
<PRE><TT>TRUE TRUE OR .
TRUE FALSE OR .
FALSE FALSE OR .</TT></PRE>
</UL>
AND only returns a TRUE if both of them are true.
<UL>
<PRE><TT>TRUE TRUE AND .
TRUE FALSE AND .</TT></PRE>
</UL>
NOT reverses the value of the flag on the stack. Enter:
<UL>
<PRE><TT>TRUE .
TRUE NOT .</TT></PRE>
</UL>
Logical operators can be combined.
<UL>
<PRE><TT>56 3 > 56 123 < AND .
23 45 = 23 23 = OR .</TT></PRE>
</UL>
Here are stack diagrams for some of these words. See the glossary for a
more complete list.
<P><B><TT>< ( a b -- flag , flag is true if A is less than B )</TT></B>
<P><B><TT>> ( a b -- flag , flag is true if A is greater than B )</TT></B>
<P><B><TT>= ( a b -- flag , flag is true if A is equal to B )</TT></B>
<P><B><TT>0= ( a -- flag , true if a equals zero )</TT></B>
<P><B><TT>OR ( a b -- a||b , perform logical OR of bits in A and B )</TT></B>
<P><B><TT>AND ( a b -- a&b , perform logical AND of bits in A and B
)</TT></B>
<P><B><TT>NOT ( flag -- opposite-flag , true if false, false if true )</TT></B>
<H3>
<A NAME="Problems - Logical"></A>Problems:</H3>
1) Write a word called LOWERCASE? that returns TRUE if the number on top
of the stack is an ASCII lowercase character. An ASCII 'a' is 97 . An ASCII
'z' is 122 . Test using the characters " A ` a q z { ".
<UL>
<PRE><TT>CHAR A LOWERCASE? . ( should print 0 )
CHAR a LOWERCASE? . ( should print -1 )</TT></PRE>
</UL>
<A HREF="#Answers to Problems">Answers to the problems</A> can be found
at the end of this tutorial.
<H2>
<A NAME="Conditionals - IF ELSE THEN CASE"></A>Conditionals - IF ELSE THEN
CASE</H2>
You will now use the TRUE and FALSE flags you learned to generate in the
last section. The "flow of control" words accept flags from the stack,
and then possibly "branch" depending on the value. Enter the following
code.
<UL>
<PRE><TT>: .L ( flag -- , print logical value )
IF ." True value on stack!"
ELSE ." False value on stack!"
THEN
;</TT></PRE>
<PRE><TT>0 .L
FALSE .L
TRUE .L
23 7 < .L</TT></PRE>
</UL>
You can see that when a TRUE was on the stack, the first part got executed.
If a FALSE was on the stack, then the first part was skipped, and the second
part was executed. One thing you will find interesting is that if you enter:
<UL>
<PRE><TT>23 .L</TT></PRE>
</UL>
the value on the stack will be treated as true. The flow of control words
consider any value that does not equal zero to be TRUE.
<P>The <B>ELSE</B> word is optional in the <B>IF...THEN</B> construct.
Try the following:
<UL>
<PRE><TT>: BIGBUCKS? ( ammount -- )
1000 >
IF ." That's TOO expensive!"
THEN
;</TT></PRE>
<PRE><TT>531 BIGBUCKS?
1021 BIGBUCKS?</TT></PRE>
</UL>
Many Forths also support a <B>CASE</B> statement similar to switch() in
'C'. Enter:
<UL>
<PRE><TT>: TESTCASE ( N -- , respond appropriately )
CASE
0 OF ." Just a zero!" ENDOF
1 OF ." All is ONE!" ENDOF
2 OF WORDS ENDOF
DUP . ." Invalid Input!"
ENDCASE CR
;</TT></PRE>
<PRE><TT>0 TESTCASE
1 TESTCASE
</TT>5 TESTCASE</PRE>
</UL>
See CASE in the glossary for more information.
<H3>
<A NAME="Problems - Conditionals"></A>Problems:</H3>
1) Write a word called DEDUCT that subtracts a value from a variable containing
your checking account balance. Assume the balance is in dollars. Print
the balance. Print a warning if the balance is negative.
<UL>
<PRE><TT>VARIABLE ACCOUNT</TT></PRE>
<PRE><TT>: DEDUCT ( n -- , subtract N from balance )
????????????????????????????????? ( you fill this in )
;</TT></PRE>
<PRE><TT>300 ACCOUNT ! ( initial funds )
40 DEDUCT ( prints 260 )
200 DEDUCT ( print 60 )
100 DEDUCT ( print -40 and give warning! )</TT></PRE>
</UL>
<A HREF="#Answers to Problems">Answers to the problems</A> can be found
at the end of this tutorial.
<H2>
<A NAME="Loops"></A>Loops</H2>
Another useful pair of words is <B>BEGIN...UNTIL</B> . These are used to
loop until a given condition is true. Try this:
<UL>
<PRE><TT>: COUNTDOWN ( N -- )
BEGIN
DUP . CR ( print number on top of stack )
1- DUP 0< ( loop until we go negative )
UNTIL
;</TT></PRE>
<PRE><TT>16 COUNTDOWN</TT></PRE>
</UL>
This word will count down from N to zero.
<P>If you know how many times you want a loop to execute, you can use the
<B>DO...LOOP</B> construct. Enter:
<UL>
<PRE><TT>: SPELL
." ba"
4 0 DO
." na"
LOOP
;</TT></PRE>
</UL>
This will print "ba" followed by four occurrences of "na". The ending value
is placed on the stack before the beginning value. Be careful that you
don't pass the values in reverse. Forth will go "the long way around" which
could take awhile. The reason for this order is to make it easier to pass
the loop count into a word on the stack. Consider the following word for
doing character graphics. Enter:
<UL>
<PRE><TT>: PLOT# ( n -- )
0 DO
[CHAR] - EMIT
LOOP CR
;</TT></PRE>
<PRE><TT>CR 9 PLOT# 37 PLOT#</TT></PRE>
</UL>
If you want to access the loop counter you can use the word I . Here is
a simple word that dumps numbers and their associated ASCII characters.
<UL>
<PRE><TT>: .ASCII ( end start -- , dump characters )
DO
CR I . I EMIT
LOOP CR
;</TT></PRE>
<PRE><TT>80 64 .ASCII</TT></PRE>
</UL>
If you want to leave a DO LOOP before it finishes, you can use the word
<B>LEAVE</B>. Enter:
<UL>
<PRE><TT>: TEST.LEAVE ( -- , show use of leave )
100 0
DO
I . CR \ print loop index
I 20 > \ is I over 20
IF
LEAVE
THEN
LOOP
;
TEST.LEAVE \ will print 0 to 20</TT></PRE>
</UL>
Please consult the manual to learn about the following words <B>+LOOP</B>
and <B>RETURN</B> . FIXME
<P>Another useful looping construct is the <B>BEGIN WHILE REPEAT</B> loop.
This allows you to make a test each time through the loop before you actually
do something. The word WHILE will continue looping if the flag on the stack
is True. Enter:
<UL>
<PRE><TT>: SUM.OF.N ( N -- SUM[N] , calculate sum of N integers )
0 \ starting value of SUM
BEGIN
OVER 0> \ Is N greater than zero?
WHILE
OVER + \ add N to sum
SWAP 1- SWAP \ decrement N
REPEAT
SWAP DROP \ get rid on N
;</TT></PRE>
<PRE><TT>4 SUM.OF.N \ prints 10 ( 1+2+3+4 )</TT></PRE>
</UL>
<H3>
<A NAME="Problems - Loops"></A>Problems:</H3>
1) Rewrite SUM.OF.N using a DO LOOP.
<P>2) Rewrite SUM.OF.N using BEGIN UNTIL.
<P>3) For bonus points, write SUM.OF.N without using any looping or conditional
construct!
<P><A HREF="#Answers to Problems">Answers to the problems</A> can be found
at the end of this tutorial.
<H2>
<A NAME="Text Input and Output"></A>Text Input and Output</H2>
You learned earlier how to do single character I/O. This section concentrates
on using strings of characters. You can embed a text string in your program
using S". Note that you must follow the S" by one space. The text string
is terminated by an ending " .Enter:
<UL>
<PRE>: TEST S" Hello world!" ;
TEST .S</PRE>
</UL>
Note that TEST leaves two numbers on the stack. The first number is the
address of the first character. The second number is the number of characters
in the string. You can print the characters of the string as follows.
<UL>
<PRE>TEST DROP \ get rid of number of characters
DUP C@ EMIT \ prints first character, 'H'
CHAR+ DUP C@ EMIT \ prints second character, 'e'
\ and so on</PRE>
</UL>
CHAR+ advances the address to the next character. You can print the entire
string using TYPE.
<UL>
<PRE>TEST TYPE
TEST 2/ TYPE \ print half of string</PRE>
</UL>
It would be nice if we could simply use a single address to describe a
string and not have to pass the number of characters around. 'C' does this
by putting a zero at the end of the string to show when it ends. Forth
has a different solution. A text string in Forth consists of a character
count in the first byte, followed immediately by the characters themselves.
This type of character string can be created using the Forth word C" ,
pronounced 'c quote'. Enter:
<UL>
<PRE><TT>: T2 C" Greetings Fred" ;
T2 .</TT></PRE>
</UL>
The number that was printed was the address of the start of the string.
It should be a byte that contains the number of characters. Now enter:
<UL>
<PRE><TT>T2 C@ .</TT></PRE>
</UL>
You should see a 14 printed. Remember that C@ fetches one character/byte
at the address on the stack. You can convert a counted Forth string to
an address and count using COUNT.
<UL>
<PRE><TT>T2 COUNT .S
TYPE</TT></PRE>
</UL>
The word <B>COUNT</B> extracts the number of characters and their starting
address. COUNT will only work with strings of less than 256 characters,
since 255 is the largest number that can be stored in the count byte. TYPE
will, however, work with longer strings since the length is on the stack.
Their stack diagrams follow:
<P><B><TT>CHAR+ ( address -- address' , add the size of one character )</TT></B>
<P><B><TT>COUNT ( $addr -- addr #bytes , extract string information ) </TT></B>
<P><B><TT>TYPE ( addr #bytes -- , output characters at addr )</TT></B>
<P>The $addr is the address of a count byte. The dollar sign is often used
to mark words that relate to strings.
<P>You can easily input a string using the word <B>ACCEPT</B>. (You may
want to put these upcoming examples in a file since they are very handy.)
The word <B>ACCEPT </B>receives characters from the keyboard and places
them at any specified address. <B>ACCEPT </B>takes input characters until
a maximum is reached or an end of line character is entered. <B>ACCEPT
</B>returns the number of characters entered. You can write a word for
entering text. Enter:
<UL>
<PRE><TT>: INPUT$ ( -- $addr )
PAD 1+ ( leave room for byte count )
127 ACCEPT ( recieve a maximum of 127 chars )
PAD C! ( set byte count )
PAD ( return address of string )
;</TT></PRE>
<PRE><TT>INPUT$ COUNT TYPE</TT></PRE>
</UL>
Enter a string which should then be echoed. You could use this in a program
that writes form letters.
<UL>
<PRE><TT>: FORM.LETTER ( -- )
." Enter customer's name." CR
INPUT$
CR ." Dear " DUP COUNT TYPE CR
." Your cup that says " COUNT TYPE
." is in the mail!" CR
;</TT></PRE>
</UL>
<B><TT>ACCEPT ( addr maxbytes -- numbytes , input text, save at address
) </TT></B>
<P>You can use your word INPUT$ to write a word that will read a number
from the keyboard. Enter:
<UL>
<PRE><TT>: INPUT# ( -- N true | false )
INPUT$ ( get string )
NUMBER? ( convert to a string if valid )
IF DROP TRUE ( get rid of high cell )
ELSE FALSE
THEN
;</TT></PRE>
</UL>
This word will return a single-precision number and a TRUE, or it will
just return FALSE. The word <B>NUMBER?</B> returns a double precision number
if the input string contains a valid number. Double precision numbers are
64-bit so we DROP the top 32 bits to get a single-precision 32 bit number.
<H2>
<A NAME="Changing Numeric Base"></A>Changing Numeric Base</H2>
Our numbering system is decimal, or "base 10." This means that a number
like 527 is equal to (5*100 + 2*10 + 7*1). The use of 10 for the numeric
base is a completely arbitrary decision. It no doubt has something to do
with the fact that most people have 10 fingers (including thumbs). The
Babylonians used base 60, which is where we got saddled with the concept
of 60 minutes in an hour. Computer hardware uses base 2, or "binary". A
computer number like 1101 is equal to (1*8 + 1*4 + 0*2 + 1*1). If you add
these up, you get 8+4+1=13 . A 10 in binary is (1*2 + 0*1), or 2. Likewise
10 in any base N is N .
<P>Forth makes it very easy to explore different numeric bases because
it can work in any base. Try entering the following:
<UL>
<PRE><TT>DECIMAL 6 BINARY .
1 1 + .
1101 DECIMAL .</TT></PRE>
</UL>
Another useful numeric base is <I>hexadecimal</I>. which is base 16. One
problem with bases over 10 is that our normal numbering system only has
digits 0 to 9. For hex numbers we use the letters A to F for the digits
10 to 15. Thus the hex number 3E7 is equal to (3*256 + 14*16 + 7*1). Try
entering:
<UL>
<PRE><TT>DECIMAL 12 HEX . \ print C
DECIMAL 12 256 * 7 16 * + 10 + .S
DUP BINARY .
HEX .</TT></PRE>
</UL>
A variable called <B>BASE</B> is used to keep track of the current numeric
base. The words HEX , <B>DECIMAL</B> , and <B>BINARY</B> work by changing
this variable. You can change the base to anything you want. Try:
<UL>
<PRE><TT>7 BASE !
6 1 + .
BASE @ . \ surprise!</TT></PRE>
</UL>
You are now in base 7 . When you fetched and printed the value of BASE,
it said 10 because 7, in base 7, is 10.
<P>PForth defines a word called .HEX that prints a number as hexadecimal
regardless of the current base.
<UL>
<PRE>DECIMAL 14 .HEX</PRE>
</UL>
You could define a word like .HEX for any base. What is needed is a way
to temporarily set the base while a number is printed, then restore it
when we are through. Try the following word:
<UL>
<PRE><TT>: .BIN ( N -- , print N in Binary )
BASE @ ( save current base )
2 BASE ! ( set to binary )
SWAP . ( print number )
BASE ! ( restore base )
;</TT></PRE>
<PRE><TT>DECIMAL
22 .BIN
22 .</TT></PRE>
</UL>
<H2>
<A NAME="Answers to Problems"></A>Answers to Problems</H2>
If your answer doesn't exactly match these but it works, don't fret. In
Forth, there are usually many ways to the same thing.
<H3>
<A HREF="#Problems - Stack">Stack Manipulations</A></H3>
<UL>
<PRE><TT>1) SWAP DUP
2) ROT DROP
3) ROT DUP 3 PICK
4) SWAP OVER 3 PICK
5) -ROT 2DUP</TT></PRE>
</UL>
<H3>
<A HREF="#Problems - Square">Arithmetic</A></H3>
<UL>
<PRE>(12 * (20 - 17)) ==> <TT>20 17 - 12 *
</TT>(1 - (4 * (-18) / 6)) ==> <TT>1 4 -18 * 6 / -
</TT>(6 * 13) - (4 * 2 * 7) ==> <TT>6 13 * 4 2 * 7 * -</TT></PRE>
<PRE><TT>: SQUARE ( N -- N*N )
DUP *
;</TT></PRE>
<PRE><TT>: DIFF.SQUARES ( A B -- A*A-B*B )
SWAP SQUARE
SWAP SQUARE -
;</TT></PRE>
<PRE><TT>: AVERAGE4 ( A B C D -- [A+B+C+D]/4 )
+ + + ( add'em up )
-2 ashift ( divide by four the fast way, or 4 / )
;</TT></PRE>
<DT>
<TT>: HMS>SECONDS ( HOURS MINUTES SECONDS -- TOTAL-SECONDS )</TT></DT>
<BR><TT> -ROT SWAP ( -- seconds minutes hours )</TT>
<BR><TT> 60 * + ( -- seconds total-minutes )</TT>
<BR><TT> 60 * + ( -- seconds )</TT>
<BR><TT>; </TT></UL>
<H3>
<A HREF="#Problems - Logical">Logical Operators</A></H3>
<UL>
<PRE><TT>: LOWERCASE? ( CHAR -- FLAG , true if lowercase )
DUP 123 <
SWAP 96 > AND
;</TT></PRE>
</UL>
<H3>
<A HREF="#Problems - Conditionals">Conditionals</A></H3>
<UL>
<PRE><TT>: DEDUCT ( n -- , subtract from account )
ACCOUNT @ ( -- n acc
SWAP - DUP ACCOUNT ! ( -- acc' , update variable )
." Balance = $" DUP . CR ( -- acc' )
0< ( are we broke? )
IF ." Warning!! Your account is overdrawn!" CR
THEN
;</TT></PRE>
</UL>
<H3>
<TT><A HREF="#Problems - Loops">Loops</A></TT></H3>
<UL>
<PRE><TT>: SUM.OF.N.1 ( N -- SUM[N] )
0 SWAP \ starting value of SUM
1+ 0 \ set indices for DO LOOP
?DO \ safer than DO if N=0
I +
LOOP
;</TT></PRE>
<PRE><TT>: SUM.OF.N.2 ( N -- SUM[N] )
0 \ starting value of SUM
BEGIN ( -- N' SUM )
OVER +
SWAP 1- SWAP
OVER 0<
UNTIL
SWAP DROP
;</TT></PRE>
<PRE><TT>: SUM.OF.N.3 ( NUM -- SUM[N] , Gauss' method )
DUP 1+ \ SUM(N) = N*(N+1)/2
* 2/
;</TT></PRE>
</UL>
Back to <A HREF="pforth.html">pForth Home Page</A>
</BODY>
</HTML>
|