en:pfw:print-hex
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
en:pfw:print-hex [2023-09-04 18:17] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | en:pfw:print-hex [2023-09-04 18:17] (current) – ↷ Seite von pfw:print-hex nach en:pfw:print-hex verschoben uho | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | {{pfw: | ||
+ | ====== Print Hex ====== | ||
+ | |||
+ | In this programming pearl Albert Nijhof shows how to print hexadecimal numbers with a given number of digits even if your Forth system does not provide pictured numeric output by means of '' | ||
+ | |||
+ | < | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 10 : .NHX ( x n -- ) \ Print last n digits of x in hex | ||
+ | 11 1 max 16 min >r \ x r: n | ||
+ | 12 r@ 1 ?do dup 4 rshift loop \ collect on data stack | ||
+ | 13 r> 0 do .1hx loop space ; | ||
+ | 14 \ ----- end of code ----- | ||
+ | 15 | ||
+ | 16 (* | ||
+ | 17 Examples | ||
+ | 18 decimal | ||
+ | 19 19150 2 .nhx \ CE | ||
+ | 20 19150 3 .nhx \ ACE | ||
+ | 21 19150 4 .nhx \ 4ACE | ||
+ | 22 19150 8 .nhx \ 00004ACE | ||
+ | 23 | ||
+ | 24 \ .NHX version without DO-LOOP | ||
+ | 25 : .NHX ( x n -- ) \ Print last n digits of x in hex | ||
+ | 26 swap >r 1 max 16 min dup \ n n r: x | ||
+ | 27 begin 1- dup while r@ 4 rshift >r \ collect on return stack | ||
+ | 28 repeat drop | ||
+ | 29 begin r> .1hx 1- dup 0= until drop space ; | ||
+ | 30 *) | ||
+ | 31 \ <><> | ||
+ | </ | ||
+ | |||
+ | ===== Idea ===== | ||
+ | |||
+ | In order to print numbers you have to extract the digits one by one and print each digit. As typical written number representation starts with the most signficant digit but extracting is easier starting with the least significant digit, the order of digits needs to be reversed. This could be done by storing the digits either on the data stack or on the return stack and make use of their last in first out property. | ||
+ | |||
+ | ===== Display a single digit: .1HX ===== | ||
+ | |||
+ | The word '' | ||
+ | |||
+ | Line 6 extracts the least significant nibble of '' | ||
+ | |||
+ | To display that nibble as a character the word uses arithmetic with comparison results: | ||
+ | |||
+ | The phrase '' | ||
+ | If the nibble is not greater than '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | If we look at the ASCII character sequence we see: | ||
+ | |||
+ | < | ||
+ | 48 49 50 51 52 53 54 55 56 57 | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | There is a gap between the character ' | ||
+ | |||
+ | Line 8 adds the character value of '' | ||
+ | |||
+ | This character is eventually displayed by the '' | ||
+ | |||
+ | ===== Display n digits: .NHX ===== | ||
+ | |||
+ | In order to display complete numbers the word '' | ||
+ | |||
+ | Line 10 does some clipping so that '' | ||
+ | |||
+ | To display complete numbers '' | ||
+ | |||
+ | After that the nibbles on the stack are processed in reversed order. They are displayed via '' | ||
+ | '' | ||
+ | |||
+ | ===== No DO LOOP ===== | ||
+ | |||
+ | If your system does not provide '' | ||
+ | |||
+ | Line 26 does parameter clipping of '' | ||
+ | |||
+ | To avoid stack juggling the first loop (line 28) collects nibbles on the return stack. (That wouldn' | ||
+ | |||
+ | The second loop (line 29) retrieves the nibbles from the return stack and prints them with '' | ||
+ | |||
+ | ===== Example output ===== | ||
+ | |||
+ | Lines 18-22 show hot to use '' | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | uho 2022-01-20 | ||