Changed all #include directives for the stdlib to <> instead of "".
[vvhitespace] / stdlib / stack.pvvs
CommitLineData
23d17247
AT
1#ifndef VVS_STDLIB_STACK
2#define VVS_STDLIB_STACK
3
4@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5@ Name:
6@ stackrotate
7@ Description:
8@ Maximum rotation depth is five. Stomps on heap[9]-heap[14].
9@ Assumes rotation depth greater than two, otherwise use SWAP.
10@ Call Stack:
11@ stack word n
12@ ...
13@ stack word 1
14@ rotation depth (rd) <-- TOS
15@ Return Stack (n>rd=3):
16@ stack word n
17@ ...
18@ stack word 1
19@ stack word 3
20@ stack word 2 <-- TOS
21@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
22NSSVTSTSN | Mark: 1010 (stackrotate)
23
24@ For the convenience of other functions, modulo the
25@ rotation depth by the available registers.
26SSSTSTN | PUSH 5
27TSTT | MODULO
28
29@ Use heap[14] for generating register addresses.
30SSSTTTSN | PUSH 14 (ptr)
31SSSSN | PUSH 0
32TTS | STORE
33
34@ Dump one word from stack to heap each pass through the loop.
35NSSVSSSSTSTSSSSSSSSSN | Mark: 00001010 00000000
36SNT | SWAP
37SSSTSSTN | PUSH 9 (starting register)
38SSSTTTSN | PUSH 14 (ptr)
39TTT | LOAD
40TSSS | ADD
41SNT | SWAP
42TTS | STORE
43
44@ See if the loop is complete.
45SNS | DUP
46SSSTTTSN | PUSH 14 (ptr)
47TTT | LOAD
48SSSTN | PUSH 1
49TSSS | ADD
50TSST | SUBTRACT
51NTSSSSSTSTSSSSSSSSTN | BRZ > 00001010 00000001
52
53@ Increment the loop counter
54SSSTTTSN | PUSH 14 (ptr)
55TTT | LOAD
56SSSTN | PUSH 1
57TSSS | ADD
58SSSTTTSN | PUSH 14 (ptr)
59SNT | SWAP
60TTS | STORE
61NSNSSSSTSTSSSSSSSSSN | JMP > 00001010 00000000
62
63@ The correct number of words have been stored to registers.
64@ Time to read them back in rotated order.
65
66@ First, prepare the counter in heap[14] again.
67@ This consumes 'rd' from the stack.
68NSSVSSSSTSTSSSSSSSSTN | Mark: 00001010 00000001
69SSSTN | PUSH 1
70TSST | SUBTRACT
71SSSTTTSN | PUSH 14 (ptr)
72SNT | SWAP
73TTS | STORE
74
75@ Second, read in the old TOS manually.
76SSSTSSTN | PUSH 9 (starting register)
77TTT | LOAD
78
79@ Read one word per pass through this loop.
80NSSVSSSSTSTSSSSSSSTSN | Mark: 00001010 00000010
81SSSTSSTN | PUSH 9 (starting register)
82SSSTTTSN | PUSH 14 (ptr)
83TTT | LOAD
84TSSS | ADD
85TTT | LOAD
86
87@ See if the loop is complete.
88SSSTTTSN | PUSH 14 (ptr)
89TTT | LOAD
90SSSTN | PUSH 1
91TSST | SUBTRACT
92NTSSSSSTSTSSSSSSSTTN | BRZ > 00001010 00000011
93
94@ Decrement the loop counter and loop again.
95SSSTTTSN | PUSH 14 (ptr)
96TTT | LOAD
97SSSTN | PUSH 1
98TSST | SUBTRACT
99SSSTTTSN | PUSH 14 (ptr)
100SNT | SWAP
101TTS | STORE
102NSNSSSSTSTSSSSSSSTSN | JMP > 00001010 00000010
103
104@ Return
105NSSVSSSSTSTSSSSSSSTTN | Mark: 00001010 00000011
106NTN | RTS
107
108#endif