-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_text_routines.spinh
92 lines (78 loc) · 2.22 KB
/
std_text_routines.spinh
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
PUB str(s = string("")) | c
REPEAT WHILE ((c := byte[s++]) <> 0)
tx(c)
''
'' print an number with a given base
'' we do this by finding the remainder repeatedly
'' this gives us the digits in reverse order
'' so we store them in a buffer; the worst case
'' buffer size needed is 32 (for base 2)
''
''
'' signflag indicates how to handle the sign of the
'' number:
'' 0 == treat number as unsigned
'' 1 == print nothing before positive numbers
'' anything else: print before positive numbers
'' for signed negative numbers we always print a "-"
''
'' we will print at least prec digits
''
VAR
byte buf[32]
PUB Num(val, base, signflag, digitsNeeded) | i, digit, r1, q1
'' if signflag is nonzero, it indicates we should treat
'' val as signed; if it is > 1, it is a character we should
'' print for positive numbers (typically "+")
if (signflag)
if (val < 0)
signflag := "-"
val := -val
'' make sure we will not overflow our buffer
if (digitsNeeded > 32)
digitsNeeded := 32
'' accumulate the digits
i := 0
repeat
if (val < 0)
' synthesize unsigned division from signed
' basically shift val right by 2 to make it positive
' then adjust the result afterwards by the bit we
' shifted out
r1 := val&1 ' capture low bit
q1 := val>>1 ' divide val by 2
digit := r1 + 2*(q1 // base)
val := 2*(q1 / base)
if (digit => base)
val++
digit -= base
else
digit := val // base
val := val / base
if (digit => 0 and digit =< 9)
digit += "0"
else
digit := (digit - 10) + "A"
buf[i++] := digit
--digitsNeeded
while (val <> 0 or digitsNeeded > 0) and (i < 32)
if (signflag > 1)
tx(signflag)
'' now print the digits in reverse order
repeat while (i > 0)
tx(buf[--i])
'' print a signed decimal number
PUB dec(val)
num(val, 10, 1, 0)
'' print an unsigned decimal number with the specified
'' number of digits; 0 means just use as many as we need
PUB decuns(val, digits)
num(val, 10, 0, digits)
'' print a hex number with the specified number
'' of digits; 0 means just use as many as we need
PUB hex(val, digits) | mask
num(val, 16, 0, digits)
'' print a newline
PUB nl()
tx(13)
tx(10)