home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l3p040
< prev
next >
Wrap
Text File
|
1990-07-15
|
5KB
|
117 lines
╔════════════════════════════════════════════════════╗
║ Lesson 3 Part 040 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
In part 3 of lesson 4 we introduced the words DECIMAL OCTAL and HEX for
switching the radix of Forth's number system to 10, 8 and 16
respectively. These words are included as part of the F-PC system.
However F-PC does not include a word to set the radix to 2 so that we
can study binary or base 2 numbers. We can easily make a word called
BINARY that will switch the radix to 2.
: BINARY ( -- ) 2 BASE ! ;
In this definition BASE is a Forth variable that contains the system
radix. We are not going to discuss variables at this time as we prefer
to give you plenty of time to become familiar with Forth's parameter
stack before introducing CONSTANTS and VARIABLES. But we will say this
much.... " ! " means store and the action of BINARY is to store the
number 2 in the system variable BASE. If you are curious VIEW DECIMAL
to see how it is defined.
Some examples:
DECIMAL 15 BINARY . <enter> 1111 ok
DECIMAL 255 BINARY . <enter> 11111111 ok
DECIMAL 255 HEX . <enter> FF ok
The only problem in using these words to see what decimal numbers look
like in binary , hex and octal is that the procedure is clumsy. We must
first make sure that we are in DECIMAL, enter the number, switch to the
base we are interested in, display the number and then switch back to
DECIMAL again... Let's make some words to make the switch faster.
: .H ( n -- ) HEX . DECIMAL ; \ Display top number in HEX
: U.H ( n -- ) HEX U. DECIMAL ; \ Display top as unsigned HEX
: .O ( n -- ) OCTAL . DECIMAL ; \ Display top number in OCTAL
: U.O ( n -- ) OCTAL U. DECIMAL ; \ Display top as unsigned OCTAL
: .B ( n -- ) BINARY . DECIMAL ; \ Display top number in BINARY
: U.B ( n -- ) BINARY U. DECIMAL ; \ Display top as unsigned BINARY
╓──────────────╖
║ Problem 3.7 ║
╙──────────────╜
Use the above definitions to display numbers in different bases and
upload your results. Why does each of the above definitions end with
DECIMAL ? Should there also be a word called .D that displays the top
number in DECIMAL?
╓──────────────╖
║ Problem 3.8 ║
╙──────────────╜
Modify the table program you wrote in Problem 3.6 replacing the OCTAL
output with BINARY output. Note, You may have to modify the column
widths to get decent looking output.
┌────────────────────────┐
│ True and False Flags. │
└────────────────────────┘
Forth has a family of words that allow us to determine if certain
conditions are true or false. To indicate that something is true Forth
will leave a True Flag on the parameter stack. To indicate that
something is false Forth will leave a False Flag on the parameter stack.
In Forth a True Flag is just the number -1 decimal, FFFF hex or
1111111111111111 binary. A false flag is just the number 0 decimal, 0
hex or 0 binary. Shown below are are the unsigned versions of True.
-1 U.B <enter> 1111111111111111 ok
-1 U.H <enter> FFFF ok -1 U. <enter> 65535 ok
Forth also has a word called TRUE that is used to make programs more
readable. The word TRUE could be simply defined as : TRUE -1 ;
TRUE U.B <enter> 1111111111111111 ok TRUE U.H <enter> FFFF ok
TRUE U. <enter> 65535 ok TRUE . <enter> -1 ok
Forth also has a word called FALSE that could be defined : FALSE 0 ;
FALSE U. <enter> 0 ok FALSE U.H <enter> 0 ok FALSE U.B <enter> 0 ok
TRUE and FALSE are used instead of -1 and 0 to make programs more
readable. Note that FALSE or 0 looks the same in all number bases.
┌─────────────────────────────┐
│ Logicals and Conditionals │
└─────────────────────────────┘
Listed below are Forth's Logical and conditional tests. In stack
pictures we often use " flag " , " f1 " , " f2 " etc for flags that
could be either true of false. " tf " and " ff " are used for true flag
and false flag respectively.
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.
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.
NOT ( f1 -- not-f1 ) Reverse the flag f1.
╓─────────────╖
║ Problem 3.9 ║
╙─────────────╜
Construct two sets of input data for each of the above words.
One set that leaves a true flag and on set that leaves a false flag.
┌────────────────────────────────────┐
│ Please move to Lesson 3 Part 050 │
└────────────────────────────────────┘