Added memsrch function to VVS stdlib.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Mon, 15 Jul 2019 23:05:54 +0000 (16:05 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Mon, 15 Jul 2019 23:05:54 +0000 (16:05 -0700)
stdlib/README.md
stdlib/heap.pvvs

index 8babbde..925c82c 100644 (file)
@@ -38,6 +38,7 @@ header comment for each function to learn the call and return stack.
           11001 ----- memcpy                        (heap.pvvs)
           11010 ----- memrand                       (heap.pvvs)
           11011 ----- memcmp                        (heap.pvvs)
           11001 ----- memcpy                        (heap.pvvs)
           11010 ----- memrand                       (heap.pvvs)
           11011 ----- memcmp                        (heap.pvvs)
+          11100 ----- memsrch                       (heap.pvvs)
          100xxx - unassigned
          101xxx - unassigned
          110xxx - conversion functions
          100xxx - unassigned
          101xxx - unassigned
          110xxx - conversion functions
@@ -55,3 +56,6 @@ private label space associated with it, formed as follows:
     00001000 xxxxxxxx - for use by 1000
     00001001 xxxxxxxx - for use by 1001
     ...etc
     00001000 xxxxxxxx - for use by 1000
     00001001 xxxxxxxx - for use by 1001
     ...etc
+
+Also by convention, functions which return a pointer will use the value `0` to
+represent a `NULL` pointer.
index aef5527..7a3ddc3 100644 (file)
@@ -217,6 +217,71 @@ SNN                     | DROP
 SSSSN                   | PUSH 0
 NTN                     | RTS
 
 SSSSN                   | PUSH 0
 NTN                     | RTS
 
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Description:
+@   This function searches the heap from address to address+count.
+@   If pattern is found, the return value is a pointer to the matching word.
+@   If not found, the return value is a null pointer.
+@ Call Stack:
+@   pattern
+@   count
+@   address <-- TOS
+@ Return Stack:
+@   pointer <-- TOS
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+NSSVTTTSSN              | Mark: 11100 (memsrch)
+
+@ Prepare the stack for computation.
+@ Stack:
+@   offset  <-- TOS
+@ Addr | Contents
+@    0 | address
+@    1 | count
+@    2 | pattern
+
+SSSSN                   | PUSH 0 (ptr)
+SNT                     | SWAP
+TTS                     | STORE
+SSSTN                   | PUSH 1 (ptr)
+SNT                     | SWAP
+TTS                     | STORE
+SSSTSN                  | PUSH 2 (ptr)
+SNT                     | SWAP
+TTS                     | STORE
+
+@ Compare one word on each pass through this loop.
+NSSVSSSTTTSSSSSSSSSSN   | Mark: 00011100 00000000
+SNS                     | DUP
+SSSSN                   | PUSH 0 (ptr)
+TTT                     | LOAD
+TSSS                    | ADD
+TTT                     | LOAD
+SSSTSN                  | PUSH 2 (ptr)
+TTT                     | LOAD
+TSST                    | SUBTRACT
+NTSSSSTTTSSSSSSSSSTN    | BRZ > 00011100 00000001
+SNS                     | DUP
+SSSTN                   | PUSH 1 (ptr)
+TTT                     | LOAD
+TSST                    | SUBTRACT
+NTSSSSTTTSSSSSSSSTSN    | BRZ > 00011100 00000010
+SSSTN                   | PUSH 1
+TSSS                    | ADD
+NSNSSSTTTSSSSSSSSSSN    | JMP > 00011100 00000000
+
+@ Found a match. Clean up and return.
+NSSVSSSTTTSSSSSSSSSTN   | Mark: 00011100 00000001
+SSSSN                   | PUSH 0 (ptr)
+TTT                     | LOAD
+TSSS                    | ADD
+NTN                     | RTS
+
+@ No match found. Clean up and return.
+NSSVSSSTTTSSSSSSSSTSN   | Mark: 00011100 00000010
+SNN                     | DROP
+SSSSN                   | PUSH 0
+NTN                     | RTS
+
 #include <math.pvvs>
 
 #endif
 #include <math.pvvs>
 
 #endif