Added not, rshift and lshift functions to VVS stdlib.
[vvhitespace] / stdlib / logic.pvvs
CommitLineData
0c56152e
AT
1#ifndef VVS_STDLIB_LOGIC
2#define VVS_STDLIB_LOGIC
3
4@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5@ These subroutines assume they are running on the official VVS interpreter
6@ which internally uses a twos-complement representation.
7@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
8
9@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
10@ Name:
11@ not (101000)
12@ Description:
13@ Performs a bitwise NOT on the TOS word.
14@ Call Stack:
15@ X
16@ Return Stack:
17@ NOT(X)
18@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
19NSSVTSTSSSN | Mark: 101000 (not)
20@ In twos-complement, NOT(X) = (-X)-1
21SSTTN | PUSH -1
22TSSN | MULTIPLY
23SSTTN | PUSH -1
24TSST | SUBTRACT
25NTN | RTS
26
27@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
28@ Name:
29@ rshift (101100)
30@ Description:
31@ Shifts 'X' right by 'shiftcount' bits with sign extension.
32@ Call Stack:
33@ X
34@ shiftcount
35@ Return Stack:
36@ X >> shiftcount
37@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
38NSSVTSTTSSN | Mark: 101100 (rshift)
39
40@ Test for loop completion first since it's allowable to shift by zero.
41NSSVSSTSTTSSSSSSSSSTN | Mark: 00101100 00000001
42SNS | DUP
43NTSSSTSTTSSSSSSSSSSN | BRZ > 00101100 00000000
44@ Shift by one bit on each pass.
45SNT | SWAP
46SSSTSN | PUSH 2
47TSTS | DIVIDE
48@ Decrement the counter.
49SNT | SWAP
50SSSTN | PUSH 1
51TSST | SUBTRACT
52@ Loop again.
53NSNSSTSTTSSSSSSSSSTN | JMP > 00101100 00000001
54
55@ Clean up and return.
56NSSVSSTSTTSSSSSSSSSSN | Mark: 00101100 00000000
57SNN | DROP
58NTN | RTS
59
60@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
61@ Name:
62@ lshift (101101)
63@ Description:
64@ Shifts 'X' left by 'shiftcount' bits with zero filling.
65@ Call Stack:
66@ X
67@ shiftcount
68@ Return Stack:
69@ X << shiftcount
70@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
71NSSVTSTTSTN | Mark: 101101 (lshift)
72
73@ Test for loop completion first since it's allowable to shift by zero.
74NSSVSSTSTTSTSSSSSSSTN | Mark: 00101101 00000001
75SNS | DUP
76NTSSSTSTTSTSSSSSSSSN | BRZ > 00101101 00000000
77@ Shift by one bit on each pass.
78SNT | SWAP
79SSSTSN | PUSH 2
80TSSN | MULTIPLY
81@ Decrement the counter.
82SNT | SWAP
83SSSTN | PUSH 1
84TSST | SUBTRACT
85@ Loop again.
86NSNSSTSTTSTSSSSSSSTN | JMP > 00101101 00000001
87
88@ Clean up and return.
89NSSVSSTSTTSTSSSSSSSSN | Mark: 00101101 00000000
90SNN | DROP
91NTN | RTS
92
93#endif