home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l4p010
< prev
next >
Wrap
Text File
|
1990-07-15
|
5KB
|
113 lines
╔════════════════════════════════════════════════════╗
║ Lesson 4 Part 010 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌──────────────────────────────────────┐
│ Review of Number Display Operators. │
└──────────────────────────────────────┘
Let's begin with a review of some of the words that we covered in the
previous lesson. One family of words we studied were the number output
operators.
\ Single signed 16bit numbers. -32768 - 32767
. ( n -- ) Display signed 16bit # followed by space.
.R ( n w -- ) Display # right justified in w wide field.
\ Single unsigned 16bit numbers. 0 - 65535
U. ( u -- ) Display unsigned 16bit # followed by space
U.R ( u w -- ) Display # right justified in w wide field.
\ Double signed 32bit numbers -2,147,483,648 - 2,147,483,647
D. ( d -- ) Display signed 32bit # followed by space.
D.R ( d w -- ) Display # right justified in w wide field.
\ Double unsigned 32bit numbers. 0 - 4,294,967,296
UD. ( ud -- ) Display unsigned 32bit # followed by space
UD.R ( ud w -- ) Display # right justified in w wide field.
┌───────────────────────────────────────┐
│ Review of Logicals and Conditionals │
└───────────────────────────────────────┘
One of the other important family of words covered in lesson 3 were the
logical and conditionals. The comparison operators took two stack
inputs and after performing the required test left a flag on the stack.
REVIEW OF CONDITIONALS
tf = -1 = 1111111111111111 binary or base 2
ff = 0 = 0000000000000000 binary or base 2
TRUE ( -- tf ) Leave true flag on top of data stack.
FALSE ( -- ff ) Leave false flag on top of data stack.
= ( n m -- flag ) Leave tf if n = m , otherwise ff.
<> ( n m -- flag ) Leave tf if n<> m , otherwise ff.
< ( n m -- flag ) Leave tf if n < m , otherwise ff.
> ( n m -- flag ) Leave tf if n > m , otherwise ff.
0= ( n -- flag ) Leave tf if n = 0 , otherwise ff.
0<> ( n -- flag ) Leave tf if n<> 0 , otherwise ff.
0< ( n -- flag ) Leave tf if n < 0 , otherwise ff.
0> ( n -- flag ) Leave tf if n > 0 , otherwise ff.
?DUP ( n -- n (n) ) Duplicate n if n is non zero.
The word ?DUP can come quite handy when setting up for an
IF ... THEN test whose true clause must use the flag or value
that is normally consumed by IF . Example:
: STARS ( n -- ) ?DUP IF 0 DO ASCII * EMIT LOOP THEN ;
This definition of STARS guarantees that nothing will happen in the
case where n is 0. See Problem 4.1 for simpler definition of STARS
Another way to code this problem would be to use ?DO as follows
: STARS ( n -- ) 0 ?DO ASCII * EMIT LOOP ;
Here the ?DO ... LOOP is equivalent to ?DUP IF DO ... LOOP THEN
WARNING you might like to include a panic exit from the DO ... LOOPs
in the these examples and problems. If you place the phrase
KEY? ?LEAVE inside a loop as shown below:
DO ... KEY? ?LEAVE .... LOOP
Now a press of any key will terminate the loop. Incorporate
this phrase if you wish to have this feature.
╓─────────────╖
║ Problem 4.1 ║
╙─────────────╜
a) What would the definition : STARS 0 DO ASCII * EMIT LOOP ;
output if 0 STARS were executed.
b) What would happen if -1 STARS were executed for each of the
three versions ( two above and that of (a)?
c) Modify STARS so that if n is positive n STARS outputs n stars in a
horizontal row and if n is negative n STARS outputs | n | stars in a
vertical column. Make sure that 0 STARS does nothing.
It should be noted that the logical operators AND OR XOR and NOT work at
the binary bit level!!
AND ( f1 f2 -- flag ) Leave tf only if f1 and f2 are true.
OR ( f1 f2 -- flag ) Leave tf if either f1 or f2 are true.
XOR ( f1 f2 -- flag ) Leave tf if f1=tf or f2=tf but not both.
NOT ( f1 -- not-f1 ) Reverse the flag f1.
1100 1100 1100
1010 1010 1010 1010
---- ---- ---- ----
AND 1000 OR 1110 XOR 0110 NOT 0101
╓──────────────╖
║ Problem 4.2 ║
╙──────────────╜
Write a word called LTEST that will take two numeric stack inputs
and display in tabular form both the stack inputs and the results
of ANDing ORing and XORing them all in BINARY!! You could design
your output to look something like the table above.
┌────────────────────────────────────┐
│ Please move to Lesson 4 Part 020 │
└────────────────────────────────────┘