home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / l6p060.seq < prev    next >
Text File  |  1990-04-22  |  4KB  |  112 lines

  1. \ Lesson 6 Part 6  ( F-PC 3.5 Tutorial by Jack Brown )
  2.  
  3. : CPACK  ( addr-from addr-to n -- )
  4.          SWAP 2DUP  C!    ( addr-from n addr-to ) \ Store string count.
  5.          1+  SWAP  CMOVE  ;  \ skip over count and move string.
  6.  
  7. \ Converting a digit string with an optional -ve sign into a double number.
  8. \ The definition below does not handle embedded decimal points.
  9. \ Convert a ASCII digit string to a double number.
  10. :  VAL  ( addr count -- dn  flag )
  11.         PAD SWAP CPACK     \ Copy and pack string at PAD buffer.
  12.         BL PAD COUNT + C!  \ Add a blank at the end of string.
  13.         0 0                \ Double number accumulator.
  14.         PAD                \ Start address-1
  15.         CONVERT            \ Convert the number.
  16.         DUP C@ ASCII - =   \ Stopped by -ve sign?
  17.         IF  CONVERT        \ If so continue conversion.
  18.             >R DNEGATE R>  \ Apply the -ve sign to result.
  19.         THEN C@  BL =  ;   \ Successful conversion if we end
  20.                            \ with a blank.
  21. \ Copied from Lesson 6 Part 3
  22. CREATE BUFFER1  80 C, 80 ALLOT       VARIABLE LEN
  23.  
  24. \ Accept a string up to 80 characters long from the console.
  25. : READLINE  ( -- )
  26.            BUFFER1 COUNT BL FILL    \ Clear BUFFER1 to blanks.
  27.         CR BUFFER1 COUNT  EXPECT    \ Input up to 80 characters
  28.          \ SPAN @ BUFFER1 C!        \ Alternate to below method.
  29.            SPAN @ LEN ! ;           \ Save actual character count
  30.  
  31. : D#IN   ( -- dn )
  32.          BEGIN  READLINE  BUFFER1 LEN @ VAL NOT
  33.          WHILE  CR ." REDO FROM START"  2DROP
  34.          REPEAT  ;
  35. COMMENT:
  36. Problem 6.7
  37. a) Modify VAL so it will skip over any embedded decimal points.
  38. b) Modify VAL so that the Forth VARIABLE  DPL is set to -1 or true if no
  39.    decimal point is entered.  Set DPL to the number of digits following
  40.    the decimal point if a decimal point is entered.
  41.  
  42. Although most Forth Programmers like to build their own string to number
  43. conversion routines you might like to use F-PC's word NUMBER instead.
  44.  
  45. NUMBER ( addr -- dn )
  46. Convert the packed string delimited by a blank at addr into a double number
  47. dn.  Viewing NUMBER and its relatives might help you with the modifications
  48. to definition of VAL requested in problem 6.7
  49. COMMENT;
  50. \ The Hex ASCII Dump programming project
  51.  
  52. \ Leave true flag if  a <= x <= b .
  53. : [IN]  ( x a b -- f )
  54.         1+ -ROT 1- OVER < -ROT > AND ;
  55.  
  56. \ Display n as printable ascii or a space. BL is a Forth CONSTANT
  57. \ whose value is the 32 the decimal value for an ASCII space.
  58. : .ASCII ( n  -- )
  59.         127 AND DUP BL 126 [IN] NOT
  60.         IF DROP BL THEN EMIT ;
  61.  
  62. \ Double space if i is equal to 8 . Used to format output.
  63. : ?SPACE ( i  -- )
  64.          8 = IF SPACE SPACE THEN ;
  65.  
  66. \ Print byte right justified in field w wide.  The number formatting
  67. \ operators will be explained shortly.
  68. : .RBYTE ( n w  -- )
  69.          >R 0 <# # # #>
  70.          R> OVER -
  71.          SPACES TYPE ;
  72.  
  73. \ Based on address addr ,  display heading for VERIFY
  74. : HEAD  ( addr -- )
  75.       CR 5 SPACES
  76.       16 0 DO I OVER + 255 AND
  77.               I ?SPACE 3 .RBYTE
  78.            LOOP 2 SPACES
  79.       16 0 DO I OVER + 15 AND 1 .R
  80.            LOOP DROP ;
  81.  
  82. \ Verify 16 bytes from address.
  83. : 1LINE     ( addr -- )
  84.    DUP CR 0 4 D.R SPACE  DUP           \ Display address.
  85.    16 0 DO   I ?SPACE COUNT  3 .RBYTE  \ Display bytes in hex.
  86.         LOOP DROP 2 SPACES
  87.    16 0 DO   COUNT  .ASCII             \ Display bytes as ASCII.
  88.         LOOP DROP SPACE   ;
  89.  
  90. : VERIFY ( addr -- ) \ Only 32 bytes from addr with header.
  91.      BASE @ SWAP HEX DUP HEAD
  92.      DUP 1LINE
  93.      DUP 16 + 1LINE
  94.      HEAD  CR BASE ! ;
  95. COMMENT:
  96. Problem 6.8
  97. Use  HEAD  and 1LINE to write a word called HADUMP ( for HexAsciiDUMP )
  98. whose stack inputs are ( addr n -- ) that will save the current system
  99. BASE , and then do a Hex ASCII DUMP of n bytes of memory starting at
  100. addr .  Your HADUMP routine should pause after every 8 lines ( if n is
  101. large enough ) and wait for a key press before continuing with a new
  102. header.
  103.  
  104. Problem 6.9
  105. Modify HADUMP to save the last memory location displayed in a VARIABLE .
  106. Now write the word  DMORE ( -- ) which dumps an additional 8 lines from
  107. the location saved by HADUMP .  Make sure that DMORE updates the last
  108. memory location displayed so you can do successive DMOREs .
  109. COMMENT;
  110. ( Please Move to Lesson 6 Part 7 )
  111.  
  112.