From 3aef055739a635118a14468a80c97e56ff5c5adc Mon Sep 17 00:00:00 2001 From: John Date: Fri, 26 Jul 2024 06:10:59 -0500 Subject: [PATCH] sample-code/ascii: Use `as` casting to print the entire printable ASCII range --- sample-code/ascii.cl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sample-code/ascii.cl diff --git a/sample-code/ascii.cl b/sample-code/ascii.cl new file mode 100644 index 0000000..0104f8b --- /dev/null +++ b/sample-code/ascii.cl @@ -0,0 +1,28 @@ +//! Prints out the characters in the ASCII printable range +//! in the format of a hex-dump + +// TODO: Convenient stuff like this in the stdlib +const HEX_LUT: [char; 16] = [ + '0', '1', '2', '3', // + '4', '5', '6', '7', // + '8', '9', 'a', 'b', // + 'c', 'd', 'e', 'f', // +]; + +fn ascii() { + for row in 0..6 { + for col in 0..16 { + if col == 8 { + print(' ') + } + let i = 0x20 + row * 16 + col + print(HEX_LUT[(i >> 4) & 0xf], HEX_LUT[i & 0xf], ' ') + } + print(" |") + for col in 0..16 { + let i = 0x20 + row * 16 + col + print(i as char) + } + println("|") + } +}