       ͻ
        Lesson 6 Part 060  F-PC 3.5 Tutorial by Jack Brown 
       ͼ

           Ŀ
             Converting a digit string with an       
             optional -ve sign into a double number. 
           

The definition below does not handle embedded decimal points.
\ Convert a ASCII digit string to a double number.
:  VAL  ( addr count -- dn  flag )
        PAD SWAP CPACK     \ Copy and pack string at PAD buffer.
        BL PAD COUNT + C!  \ Add a blank at the end of string.
        0 0                \ Double number accumulator.
        PAD                \ Start address-1
        CONVERT            \ Convert the number.
        DUP C@ ASCII - =   \ Stopped by -ve sign?
        IF  CONVERT        \ If so continue conversion.
            >R DNEGATE R>  \ Apply the -ve sign to result.
        THEN C@  BL =  ;   \ Successful conversion if we end
                           \ with a blank.
: D#IN   ( -- dn )
         BEGIN  READLINE  BUFFER1 LEN @ VAL NOT
         WHILE  CR ." REDO FROM START"  2DROP
         REPEAT  ;

ķ
 Problem 6.7 
Ľ
a) Modify VAL so it will skip over any embedded decimal points.
b) Modify VAL so that the Forth VARIABLE  DPL is set to -1 or true if no
   decimal point is entered.  Set DPL to the number of digits following
   the decimal point if a decimal point is entered.

Although most Forth Programmers like to build their own string to number
conversion routines you might like to use F-PC's word NUMBER instead.

NUMBER ( addr -- dn )
Convert the packed string delimited by a blank at addr into a double number
dn.  Viewing NUMBER and its relatives might help you with the modifications
to definition of VAL requested in problem 6.7

          Ŀ
            The Hex ASCII Dump Programming Project 
          

\ Leave true flag if  a <= x <= b .
: [IN]  ( x a b -- f )
        1+ -ROT 1- OVER < -ROT > AND ;

\ Display n as printable ascii or a space. BL is a Forth CONSTANT
\ whose value is the 32 the decimal value for an ASCII space.
: .ASCII ( n  -- )
        127 AND DUP BL 126 [IN] NOT
        IF DROP BL THEN EMIT ;

\ Double space if i is equal to 8 . Used to format output.
: ?SPACE ( i  -- )
         8 = IF SPACE SPACE THEN ;

\ Print byte right justified in field w wide.  The number formatting
\ operators will be explained shortly.
: .RBYTE ( n w  -- )
         >R 0 <# # # #>
         R> OVER -
         SPACES TYPE ;

\ Based on address addr ,  display heading for VERIFY
: HEAD  ( addr -- )
      CR 5 SPACES
      16 0 DO I OVER + 255 AND
              I ?SPACE 3 .RBYTE
           LOOP 2 SPACES
      16 0 DO I OVER + 15 AND 1 .R
           LOOP DROP ;

\ Verify 16 bytes from address.
: 1LINE     ( addr -- )
   DUP CR 0 4 D.R SPACE  DUP           \ Display address.
   16 0 DO   I ?SPACE COUNT  3 .RBYTE  \ Display bytes in hex.
        LOOP DROP 2 SPACES
   16 0 DO   COUNT  .ASCII             \ Display bytes as ASCII.
        LOOP DROP SPACE   ;

: VERIFY ( addr -- ) \ Only 32 bytes from addr with header.
     BASE @ SWAP HEX DUP HEAD
     DUP 1LINE
     DUP 16 + 1LINE
     HEAD  CR BASE ! ;

ķ
 Problem 6.8 
Ľ
Use  HEAD  and 1LINE to write a word called HADUMP ( for HexAsciiDUMP )
whose stack inputs are ( addr n -- ) that will save the current system
BASE , and then do a Hex ASCII DUMP of n bytes of memory starting at
addr .  Your HADUMP routine should pause after every 8 lines ( if n is
large enough ) and wait for a key press before continuing with a new
header.

ķ
 Problem 6.9  
Ľ
Modify HADUMP to save the last memory location displayed in a VARIABLE .
Now write the word  DMORE ( -- ) which dumps an additional 8 lines from
the location saved by HADUMP .  Make sure that DMORE updates the last
memory location displayed so you can do successive DMOREs .

Ŀ
  Please Move to Lesson 6 Part 070  

