-
Notifications
You must be signed in to change notification settings - Fork 30
/
kernel-strings-and-numbers.mu4
63 lines (44 loc) · 1.86 KB
/
kernel-strings-and-numbers.mu4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| This file is part of muforth: https://muforth.dev/
|
| Copyright 2002-2024 David Frech. (Read the LICENSE for details.)
loading MSP430 Forth kernel (string and number addons)
__meta
hex
( String display.)
: count ( a - a+1 u) dup 1+ swap c@ ; ( a species of c@+)
: aligned 1+ -2 and ;
: (") ( - a u) r> count 2dup + aligned >r ;
meta: cstring, ( a u) ( compile a counted string - with prefix length byte)
dup c, ( count) string, 0 align, ;
( For interactive use. Copies the string into target ram.)
meta: " ( - a u) ( leaves a target address!)
h preserve ram
char " parse here over 2push string, 0 align, 2pop ;
compiler: "
\t (") target-compile, char " parse cstring, ;
compiler: char
\f char target-literal ;
( Pictured numeric output.)
( XXX Hex only at the moment!)
: /digit ( u - uquot umod) dup u2/ u2/ u2/ u2/ swap 0f and ;
( #13 words)
( XXX try writing this as a code word?)
: >digit ( n - ch) ( convert 1 binary digit to char; hex to lowercase)
9 over u< [ char a char 9 1+ - ] literal and + char 0 + ;
-- : abs ( n - |n|) dup 0< if negate then ;
| : spaces ( n) 0 max ?if for space next then ;
| pad is where we convert numbers to ASCII. pad returns the address of the
| _end_ of the buffer, since conversion occurs right-to-left.
| XXX for right now, set aside 4 bytes - we're doing unsigned hex _only_ at
| the moment. And because pad can grow down from hld, we don't need to
| define both!
( Leave room for pad, _below_ hld.)
ram 4 allot flash
variable hld
: hold -1 hld +! hld @ c! ;
: <# hld hld ! ;
: #> ( u - a #) drop hld @ hld over - ;
| : sign ( n -) 0< if char - hold then ;
: # ( u - u') /digit >digit hold ;
: (.uw) ( w - a u) ( convert unsigned word in hex) <# # # # # #> ;
: (.ub) ( b - a u) ( convert unsigned byte in hex) <# # # #> ;