From 0c56152e6ad67ef7e311f1e394250279864edd1a Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Thu, 25 Jul 2019 23:11:21 -0700 Subject: [PATCH] Added not, rshift and lshift functions to VVS stdlib. --- stdlib/README.md | 8 +++- stdlib/logic.pvvs | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 stdlib/logic.pvvs diff --git a/stdlib/README.md b/stdlib/README.md index 7508410..e7ebedd 100644 --- a/stdlib/README.md +++ b/stdlib/README.md @@ -51,7 +51,13 @@ header comment for each function to learn the call and return stack. 11111 ----- spew (heap.pvvs) 100xxx - string functions 100000 ----- strlen (string.pvvs) - 101xxx - unassigned + 101xxx - logic functions + 101000 ----- not (logic.pvvs) + 101001 ----- and (logic.pvvs) + 101010 ----- or (logic.pvvs) + 101011 ----- xor (logic.pvvs) + 101100 ----- rshift (logic.pvvs) + 101101 ----- lshift (logic.pvvs) 110xxx - conversion functions 111xxx - debug functions 111000 ----- dump heap (debug.pvvs) diff --git a/stdlib/logic.pvvs b/stdlib/logic.pvvs new file mode 100644 index 0000000..eccc68b --- /dev/null +++ b/stdlib/logic.pvvs @@ -0,0 +1,93 @@ +#ifndef VVS_STDLIB_LOGIC +#define VVS_STDLIB_LOGIC + +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ These subroutines assume they are running on the official VVS interpreter +@ which internally uses a twos-complement representation. +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ Name: +@ not (101000) +@ Description: +@ Performs a bitwise NOT on the TOS word. +@ Call Stack: +@ X +@ Return Stack: +@ NOT(X) +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +NSSVTSTSSSN | Mark: 101000 (not) +@ In twos-complement, NOT(X) = (-X)-1 +SSTTN | PUSH -1 +TSSN | MULTIPLY +SSTTN | PUSH -1 +TSST | SUBTRACT +NTN | RTS + +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ Name: +@ rshift (101100) +@ Description: +@ Shifts 'X' right by 'shiftcount' bits with sign extension. +@ Call Stack: +@ X +@ shiftcount +@ Return Stack: +@ X >> shiftcount +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +NSSVTSTTSSN | Mark: 101100 (rshift) + +@ Test for loop completion first since it's allowable to shift by zero. +NSSVSSTSTTSSSSSSSSSTN | Mark: 00101100 00000001 +SNS | DUP +NTSSSTSTTSSSSSSSSSSN | BRZ > 00101100 00000000 +@ Shift by one bit on each pass. +SNT | SWAP +SSSTSN | PUSH 2 +TSTS | DIVIDE +@ Decrement the counter. +SNT | SWAP +SSSTN | PUSH 1 +TSST | SUBTRACT +@ Loop again. +NSNSSTSTTSSSSSSSSSTN | JMP > 00101100 00000001 + +@ Clean up and return. +NSSVSSTSTTSSSSSSSSSSN | Mark: 00101100 00000000 +SNN | DROP +NTN | RTS + +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ Name: +@ lshift (101101) +@ Description: +@ Shifts 'X' left by 'shiftcount' bits with zero filling. +@ Call Stack: +@ X +@ shiftcount +@ Return Stack: +@ X << shiftcount +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +NSSVTSTTSTN | Mark: 101101 (lshift) + +@ Test for loop completion first since it's allowable to shift by zero. +NSSVSSTSTTSTSSSSSSSTN | Mark: 00101101 00000001 +SNS | DUP +NTSSSTSTTSTSSSSSSSSN | BRZ > 00101101 00000000 +@ Shift by one bit on each pass. +SNT | SWAP +SSSTSN | PUSH 2 +TSSN | MULTIPLY +@ Decrement the counter. +SNT | SWAP +SSSTN | PUSH 1 +TSST | SUBTRACT +@ Loop again. +NSNSSTSTTSTSSSSSSSTN | JMP > 00101101 00000001 + +@ Clean up and return. +NSSVSSTSTTSTSSSSSSSSN | Mark: 00101101 00000000 +SNN | DROP +NTN | RTS + +#endif -- 2.20.1