diff --git a/forth/dictionary.txt b/forth/dictionary.txt index 2759cc0..2d8f454 100644 --- a/forth/dictionary.txt +++ b/forth/dictionary.txt @@ -193,10 +193,11 @@ core. number parsing. By default, (parse). (print) a -- Print string at addr a. . n -- Print n in its decimal form -.X n -- Print n in its hexadecimal form. In hex, numbers +.x n -- Print n's LSB in hex form. Always 2 characters. +.X n -- Print n in hex form. Always 4 characters. Numbers + are never considered negative. "-1 .X" --> ffff ." xxx" -- *I* Compiles string literal xxx followed by a call to (print) - are never considered negative. "-1 .X -> ffff" C< -- c Read one char from buffered input. EMIT c -- Spit char c to output stream IN> -- a Address of variable containing current pos in input diff --git a/forth/fmt.fs b/forth/fmt.fs index 98963bb..bf994e3 100644 --- a/forth/fmt.fs +++ b/forth/fmt.fs @@ -1,7 +1,6 @@ ( requires core, parse ) -( TODO FORGET this word ) -: PUSHDGTS +: _ 999 SWAP ( stop indicator ) DUP 0 = IF '0' EXIT THEN ( 0 is a special case ) BEGIN @@ -16,7 +15,7 @@ ( that "0 1 -" thing is because we don't parse negative number correctly yet. ) DUP 0 < IF '-' EMIT 0 1 - * THEN - PUSHDGTS + _ BEGIN DUP '9' > IF DROP EXIT THEN ( stop indicator, we're done ) EMIT @@ -25,24 +24,21 @@ : ? @ . ; -: PUSHDGTS - 999 SWAP ( stop indicator ) - DUP 0 = IF '0' EXIT THEN ( 0 is a special case ) - BEGIN - DUP 0 = IF DROP EXIT THEN - 16 /MOD ( r q ) - SWAP ( r q ) +: _ DUP 9 > IF 10 - 'a' + - ELSE '0' + THEN ( q d ) - SWAP ( d q ) - AGAIN + ELSE '0' + THEN ; -: .X ( n -- ) - ( For hex display, there are no negatives ) - PUSHDGTS - BEGIN - DUP 'f' > IF DROP EXIT THEN ( stop indicator, we're done ) - EMIT - AGAIN +( For hex display, there are no negatives ) + +: .x + 256 MOD ( ensure < 0x100 ) + 16 /MOD ( l h ) + _ EMIT ( l ) + _ EMIT +; + +: .X + 256 /MOD ( l h ) + .x .x ;