FORTH Pictured Numeric Output

By Jim Brooks  www.jimbrooks.org/forth/

The FORTH programming language's set of words for pictured numeric output (PNO) is a syntax for numeric-to-ASCII conversion (number formatting). FORTH PNO is analoguous to the C language's ability to print numeric variables by printf("%d"). Double-length values, not single-length, are used in PNO. The FORTH word <# marks the beginning of PNO and #> marks the end. PNO is coded by writing combinations of the words # #S SIGN HOLD between <# #> . A double-length value is expected to be on the S-stack before the <# . The word # converts the least-significant digit of the binary number to an ASCII digit. The digit is stored in a special buffer dedicated to PNO named PAD. The word #S converts all of the remaining digits. The word HOLD is invoked by # which first decrements the variable HLD and stores the least-significant digit into PAD as an ASCII character. Afterwards, the word # divides the double-length number by the current number base, which effectively discards the least-significant digit. By using #S or # repetitively, the numerical string accumulates by repetitively shifting out the least-significant digits until the double-length number becomes 0, while appending the shifted-out digit into the numerical string. The first byte of the PNO result is stored at PAD-1. Where the final byte is stored depends on how many PNO words were written, but it will be stored at an address lower than the first byte.

Diagram of PAD if a PNO operation produces three bytes:

 
                      +------------------------+ <-- higher address
            PAD       |   N/A                  |
                      +------------------------+
            PAD-1     |   #3 resulting byte    |
                      +------------------------+
            PAD-2     |   #2 resulting byte    |
                      +------------------------+
            PAD-3     |   #1 resulting byte    |
                      +------------------------+ <-- lower address

Example:

 
: .DOLLARS ( ud -- )
    <# # # 46 HOLD #S 36 HOLD #>
    TYPE
    ;

Entering  59998. .DOLLARS  will display $599.98


index   home