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

  1. \ Lesson 6 Part 7 ( F-PC 3.5 Tutorial by Jack Brown )
  2. COMMENT:
  3. Number Formatting Operators.
  4.  
  5. We are going to redefine Forth's number formatting primitives
  6. using our own output buffer.  Most Forth systems prepare numbers
  7. for output by placing the ASCII digits in a buffer that grows
  8. grows towards low memory from a location called PAD which is
  9. normally defined to be a hundred or so bytes beyond the end of
  10. the dictionary.  You can SEE or VIEW PAD to see exactly where it
  11. is located.
  12.  
  13. Note:   The code which follows requires the the colon definitions
  14. of Lesson 6 Part 6 to be loaded first.
  15. COMMENT;
  16. \ Double Number Conversion Primitives
  17.  
  18.   CREATE PADBUF  40  ALLOT   \ Buffer to hold output string.
  19.  
  20. : PAD  ( -- addr )         \ Return address for output string.
  21.       PADBUF  16  +  ;
  22.  
  23. \ The variable HLD points to the current position in the output buffer.
  24. \ HLD is initialized to PAD by  <# below and is decremented by 1 just
  25. \ before another ASCII digit is added to the output.
  26.  
  27. VARIABLE  HLD            \ Current output address in below PAD .
  28. VARIABLE  S&PAD          \ Used to control inspection dump.
  29. S&PAD ON                 \ Set to true to see operation of  # and HOLD
  30.  
  31. \ Display Stack and Pad if S&PAD is true.
  32. : .S&PAD   ( -- )
  33.          S&PAD @ IF CR .S  PADBUF 1LINE THEN ;
  34.  
  35. \ Add character n to string being formed.
  36. : HOLD   ( n -- )
  37.         -1 HLD +!   HLD @  C! .S&PAD ;
  38.  
  39. \ Start numeric conversion.
  40. : <#     ( -- )   \ Initialize HLD for new output.
  41.         PADBUF  32 ERASE
  42.         PAD  HLD  !   .S&PAD ;
  43.  
  44. \ Terminate numeric conversion.
  45. : #>     ( dn -- addr len )
  46.         2DROP         \ Drop double number.
  47.         HLD @         \ Address of string.
  48.         PAD OVER -    \ Compute length of string.
  49.         .S&PAD  ;
  50.  
  51. \ If n is negative insert a -ve sign in the output string.
  52. : SIGN  ( n -- )
  53.         0< IF   ASCII -  HOLD  THEN  ;
  54.  
  55. \ Convert a single digit using the current number BASE.
  56. : #  ( dn -- dn' )
  57.         BASE @   MU/MOD     \ Divide dn by current base.
  58.         ROT  9   OVER  <    \ Digit greater than 9 ?
  59.         IF   7   +   THEN   \ Add offset of letter A for hex etc
  60.         ASCII 0 + HOLD    ; \ Add offset to digit zero and save.
  61.  
  62. \ MU/MOD  is a mixed mode division operator.  It divides a
  63. \ double number dn by a single divisor n leaving a single
  64. \ remainder r and a double quotient dq.
  65.  
  66. \ MU/MOD   ( dn n -- r dq )   \  dn = dq*n + r
  67.  
  68. : #S  ( dn -- dn')   \ Convert a number until finished.
  69.         BEGIN  #  2DUP  OR  0=  UNTIL  ;
  70.  
  71.  
  72. ( Please Move to Lesson 6 Part 8 )
  73.  
  74.  
  75.