Added example that prints a multi-digit number. Added library functions in support.
[vvhitespace] / stdlib / stdio.pvvs
CommitLineData
8bed3ccd
AT
1#ifndef VVS_STDLIB_STDIO
2#define VVS_STDLIB_STDIO
3
4@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5@ Description:
6@ This function prints a null-terminated string from the stack.
7@ Call Stack:
8@ null-terminator (ASCII '\0')
9@ char n
10@ ...
11@ char 2
12@ char 1 <-- TOS
13@ Return Stack:
14@ <empty>
15@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
32c440bf
AT
16NSSVTSSSN | Mark: 1000 (print string from stack)
17SNS | DUP
18NTSSSSSTSSSSSSSSSSTN | BRZ > 00001000 00000001
19TNSS | Print character
20NSNTSSSN | JMP > 1000
21NSSVSSSSTSSSSSSSSSSTN | Mark: 00001000 00000001
22SNN | DROP
23NTN | RTS
24
8bed3ccd
AT
25@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
26@ Description:
27@ This function prints a null-terminated string from the heap.
28@ Call Stack:
29@ pointer to first character <-- TOS
30@ Return Stack:
31@ <empty>
32@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
32c440bf
AT
33NSSVTSSTN | Mark: 1001 (print string from heap)
34SNS | DUP
35TTT | LOAD
36SNS | DUP
37NTSSSSSTSSTSSSSSSSTN | BRZ > 00001001 00000001
38TNSS | Print character
39SSSTN | Push +1
40TSSS | ADD
41NSNTSSTN | JMP > 1001
42NSSVSSSSTSSTSSSSSSSTN | Mark: 00001001 00000001
43SNN | DROP
44SNN | DROP
45NTN | RTS
8bed3ccd 46
3625ff3a
AT
47@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
48@ Description:
49@ This functions prints a number from the stack in sign-magnitude format.
50@ Leading zeros are suppressed.
51@ Call Stack:
52@ number to print <-- TOS
53@ Return Stack:
54@ <empty>
55@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
56NSSVTSTSN | Mark: 1010 (print number from stack)
57SNS | DUP
58NSTTSSSSSSN | JSR > 1000000
59NSTTSSSSSTN | JSR > 1000001
60NTN | RTS
61
62@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
63@ Description:
64@ This functions prints the sign of a number from the stack.
65@ Call Stack:
66@ number to print <-- TOS
67@ Return Stack:
68@ <empty>
69@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
70NSSVTSSSSSSN | Mark: 1000000 (print sign of number from stack)
71NTTSTSSSSSSSSSSSSSTN | BMI > 010000000 00000001
72SSSTSTSTTN | PUSH ASCII '+'
73NSNSTSSSSSSSSSSSSTSN | JMP > 010000000 00000010
74NSSVSTSSSSSSSSSSSSSTN | Mark: 010000000 00000001
75SSSTSTTSTN | PUSH ASCII '-'
76NSSVSTSSSSSSSSSSSSTSN | Mark: 010000000 00000010
77TNSS | PUTC
78NTN | RTS
79
80@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
81@ Description:
82@ This functions prints the magnitude of a number from the stack.
83@ Call Stack:
84@ number to print <-- TOS
85@ Return Stack:
86@ <empty>
87@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
88NSSVTSSSSSTN | Mark: 1000001 (print magnitude of number from stack)
89NSTTSSSTN | JSR > 10001 (absolute value)
90
91SSSSN | PUSH ASCII '\0'
92SNT | SWAP
93
94@ Pick off one digit on each pass through this loop.
95NSSVSTSSSSSTSSSSSSSSN | Mark: 01000001 00000000
96SNS | DUP
97
98@ Mod-off a digit, convert to ASCII, store on stack as part of the string.
99SSSTSTSN | PUSH +10
100TSTT | MODULO
101SSSTTSSSSN | PUSH ASCII '0'
102TSSS | ADD
103SNT | SWAP
104
105@ Divide down to next digit and keep looping if number != 0 yet.
106SSSTSTSN | PUSH +10
107TSTS | DIVIDE
108SNS | DUP
109NTSSTSSSSSTSSSSSSSTN | BRZ > 01000001 00000001
110NSNSTSSSSSTSSSSSSSSN | JMP > 01000001 00000000
111
112@ Print the string we have built on the stack.
113NSSVSTSSSSSTSSSSSSSTN | Mark: 01000001 00000001
114SNN | DROP
115NSTTSSSN | JSR > 1000 (print string from stack)
116NTN | RTS
117
118#include <math.pvvs>
119
8bed3ccd 120#endif