home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 3 Part 030 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- ┌──────────────────────────────┐
- │ Simple Number Formatting. │
- └──────────────────────────────┘
-
- Back in Lesson 2 Part 7 used the word " .R " to help format the output
- of our table programs into nicely spaced columns. There are a number of
- related words that can be used to display signed and unsigned single
- and double numbers. The .R suffix means right justified and when a
- number display operator has the .R suffix it is expected that the top of
- the stack is the width of the field that will contain the number.
-
- Here is a complete list of the " xx.R " number display operators paired
- with their simpler versions.
-
- \ 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.
-
- One of the most amazing things about Forth is its ability to work in any
- number base. In F-PC the words DECIMAL, OCTAL, and HEX are included for
- changing the radix of the number system to 10, 8, and 16 respectively.
- You should try switching to base 16 (HEX) and base 8 (OCTAL) and doing
- some simple arithmetic.
-
- Here is a simple table program that illustrates the use of then number
- formatting operators and different number bases.
-
- \ Illustration of Number bases and formatted display operators.
- : NTABLE ( dn -- )
- CR ." unsigned" ." signed" ." unsigned"
- ." signed" ." unsigned" ." signed"
- CR ." decimal" ." decimal" ." octal "
- ." octal " ." hex " ." hex "
- 20 0 DO
- CR
- DECIMAL 2DUP 13 UD.R
- 2DUP 13 D.R
- OCTAL 2DUP 13 UD.R
- 2DUP 13 D.R
- HEX 2DUP 13 UD.R
- 2DUP 13 D.R
- 1. D+
- LOOP
- 2DROP DECIMAL ;
- Here is a sample of the output:
- -10. NTABLE
- unsigned signed unsigned signed unsigned signed
- decimal decimal octal octal hex hex
- 4294967286 -10 37777777766 -12 FFFFFFF6 -A
- 4294967287 -9 37777777767 -11 FFFFFFF7 -9
- ( we have deleted the middle of the table !!! )
- 8 8 10 10 8 8
- 9 9 11 11 9 9
-
- ╓──────────────╖
- ║ Problem 3.6 ║
- ╙──────────────╜
- Make a new version NTABLE that outputs a similar table for single signed and
- unsigned integers.
-
- HIDE and REVEAL or dealing with Word Definitions containing errors.
-
- If a word definition does not compile correctly you will not see it
- in the dictionary even though part of the word was compiled. The reason
- for this is to keep you from executing a word containing an error.
- For example consider the following incomplete word definition:
- : TEST 10 0 DO I . ; Stack Changed
- ok
- WORDS <enter> ( TEST is not there!! )
- EMPTY ..... ok
- REVEAL <enter> ok
- WORDS <enter> ( Now you see it!! )
- TEST EMPTY ..... ok
- HIDE <enter> ok
- WORDS <enter> ( Now you don't !! )
- EMPTY ...... ok
-
- You should be aware that an incorrect or incomplete definition will
- leave junk compiled in the dictionary even though WORDS does not show
- anything. To remove an incorrectly compiled word TEST you can type:
-
- REVEAL FORGET TEST
-
- Use HELP and VIEW to find out more about HIDE and REVEAL
-
- Caution: Executing an incorrectly compiled word definition like
- TEST above, will most likely crash the computer and require a reset!
-
- ┌────────────────────────────────────┐
- │ Please move to Lesson 3 Part 040 │
- └────────────────────────────────────┘
-