From f0499c77612b74a627727336579eb6b980c35529 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Thu, 25 Jul 2019 23:13:23 -0700 Subject: [PATCH] Added deepdup subroutine to VVS stdlib. --- stdlib/README.md | 5 +++-- stdlib/stack.pvvs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/stdlib/README.md b/stdlib/README.md index e7ebedd..099a70e 100644 --- a/stdlib/README.md +++ b/stdlib/README.md @@ -37,6 +37,7 @@ header comment for each function to learn the call and return stack. 1001 ----- print number from stack (stdio.pvvs) 1010 ----- stackrotate (stack.pvvs) 1011 ----- stackrotatereverse (stack.pvvs) + 1100 ----- deepdup (stack.pvvs) 010xxx - math functions 10000 ----- random (math.pvvs) 10001 ----- absolute value (math.pvvs) @@ -63,8 +64,8 @@ header comment for each function to learn the call and return stack. 111000 ----- dump heap (debug.pvvs) 111001 ----- dump stack (debug.pvvs) 1xxxxxx - reserved for less common entry points - 1000000 ----- slurp registers (heap.pvvs) - 1000001 ----- spew registers (heap.pvvs) + 1000000 ----- + 1000001 ----- 1000010 ----- print sign of number (stdio.pvvs) 1000011 ----- print magnitude of number (stdio.pvvs) 1000100 ----- print string from stack (stdio.pvvs) diff --git a/stdlib/stack.pvvs b/stdlib/stack.pvvs index 0ef72bf..417b3bb 100644 --- a/stdlib/stack.pvvs +++ b/stdlib/stack.pvvs @@ -210,5 +210,40 @@ TSSS | ADD TTT | LOAD NTN | RTS +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ Name: +@ deepdup +@ Description: +@ Duplicates an item deep on the stack, placing the duplicate on TOS. +@ By default, maximum depth is 11. +@ True maximum depth is (max depth of stackrotate & stackrotatereverse)-3. +@ Call Stack: +@ stack word n +@ ... +@ stack word 1 +@ dupdepth <-- TOS +@ Return Stack: +@ stack word n +@ ... +@ stack word 1 +@ copy of stack word 3 <-- TOS +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +NSSVTTSSN | Mark: 1100 (deepdup) + +@ Copy 'dupdepth' and use it to bring forth the desired stack word. +SNS | DUP +SSSTN | PUSH 1 +TSSS | ADD +NSTTSTTN | JSR > 1011 (stackrotatereverse) + +@ Copy the desired stack word and return it back into the stack. +SNS | DUP +SSSTTN | PUSH 3 +NSTTSTTN | JSR > 1011 (stackrotatereverse) +SSSTN | PUSH 1 +TSSS | ADD +NSTTSTSN | JSR > 1010 (stackrotate) +NTN | RTS + #endif -- 2.20.1