home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l6p070 < prev    next >
Text File  |  1990-07-15  |  5KB  |  108 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 6 Part 070  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.               ┌───────────────────────────────┐
  6.               │  Number Formatting Operators. │
  7.               └───────────────────────────────┘
  8.  
  9. We are going to redefine Forth's number formatting primitives using our
  10. own output buffer.  Most Forth systems prepare numbers for output by
  11. placing the ASCII digits in a buffer that grows grows towards low memory
  12. from a location called PAD which is normally defined to be a hundred or
  13. so bytes beyond the end of the dictionary.  You can SEE or VIEW PAD to
  14. see exactly where it is located.
  15.  
  16. Note:   The code which follows requires the the colon definitions of
  17. Lesson 6 Part 060 to be loaded first.
  18.  
  19. \ Double Number Conversion Primitives
  20.  
  21.   CREATE PADBUF  40  ALLOT   \ Buffer to hold output string.
  22.  
  23. : PAD  ( -- addr )         \ Return address for output string.
  24.       PADBUF  16  +  ;
  25.  
  26. \ The variable HLD points to the current position in the output buffer.
  27. \ HLD is initialized to PAD by  <# below and is decremented by 1 just
  28. \ before another ASCII digit is added to the output.
  29.  
  30. VARIABLE  HLD            \ Current output address in below PAD .
  31. VARIABLE  S&PAD          \ Used to control inspection dump.
  32. S&PAD ON                 \ Set to true to see operation of  # and HOLD
  33.  
  34. \ Display Stack and Pad if S&PAD is true.
  35. : .S&PAD   ( -- )
  36.          S&PAD @ IF CR .S  PADBUF 1LINE THEN ;
  37.  
  38. \ Add character n to string being formed.
  39. : HOLD   ( n -- )
  40.         -1 HLD +!   HLD @  C! .S&PAD ;
  41.  
  42. \ Start numeric conversion.
  43. : <#     ( -- )   \ Initialize HLD for new output.
  44.         PADBUF  32 ERASE
  45.         PAD  HLD  !   .S&PAD ;
  46.  
  47. \ Terminate numeric conversion.
  48. : #>     ( dn -- addr len )
  49.         2DROP         \ Drop double number.
  50.         HLD @         \ Address of string.
  51.         PAD OVER -    \ Compute length of string.
  52.         .S&PAD  ;
  53.  
  54. \ If n is negative insert a -ve sign in the output string.
  55. : SIGN  ( n -- )
  56.         0< IF   ASCII -  HOLD  THEN  ;
  57.  
  58. \ Convert a single digit using the current number BASE.
  59. : #  ( dn -- dn' )
  60.         BASE @   MU/MOD     \ Divide dn by current base.
  61.         ROT  9   OVER  <    \ Digit greater than 9 ?
  62.         IF   7   +   THEN   \ Add offset of letter A for hex etc
  63.         ASCII 0 + HOLD    ; \ Add offset to digit zero and save.
  64.  
  65. \ MU/MOD  is a mixed mode division operator.  It divides a
  66. \ double number dn by a single divisor n leaving a single
  67. \ remainder r and a double quotient dq.
  68.  
  69. \ MU/MOD   ( dn n -- r dq )   \  dn = dq*n + r
  70.  
  71. : #S  ( dn -- dn')   \ Convert a number until finished.
  72.         BEGIN  #  2DUP  OR  0=  UNTIL  ;
  73.  
  74.          ┌─────────────────────────────┐
  75.          │  Interactive Demonstration  │
  76.          └─────────────────────────────┘
  77.  
  78. HEX 12345. <enter> ok  \ Hex mode and place number on the stack.
  79. <# <enter>             \ Set up for numeric conversion.
  80.  [2]   2345       1    \ Note Stack and Buffer DUMP below.
  81. 7575  00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00          ok
  82. # <enter>              \ Convert one digit  see  below
  83.  [2]   1234       0    \                             \/      \/
  84. 7575  00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 35       5  ok
  85. # <enter>              \ Convert another digit
  86.  [2]    123       0    \                          \/        \/
  87. 7575  00 00 00 00 00 00 00 00   00 00 00 00 00 00 34 35      45  ok
  88. #S <enter>             \ Convert all remaining digits
  89.  [2]     12       0    \                       \/          \/
  90. 7575  00 00 00 00 00 00 00 00   00 00 00 00 00 33 34 35     345
  91.  [2]      1       0                         \/            \/
  92. 7575  00 00 00 00 00 00 00 00   00 00 00 00 32 33 34 35    2345
  93.  [2]      0       0                      \/              \/
  94. 7575  00 00 00 00 00 00 00 00   00 00 00 31 32 33 34 35   12345  ok
  95. #> <enter>             \ terminate conversion
  96.  [2]   7580       5    \ Note string address and count left on stack
  97. 7575  00 00 00 00 00 00 00 00   00 00 00 31 32 33 34 35             12345  ok
  98. TYPE <enter> 12345 ok   \ and that's the way she works.
  99.  
  100. You should appreciate that Forth continually uses the area above and
  101. below the actual PAD and we can only see what is happening here because
  102. we have redefined the operators.  In the next screen we redefine the
  103. standard output operators.
  104.  
  105. ┌────────────────────────────────────┐
  106. │  Please Move to Lesson 6 Part 080  │
  107. └────────────────────────────────────┘
  108.