Added example that prints a multi-digit number. Added library functions in support.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Fri, 12 Jul 2019 23:24:10 +0000 (16:24 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Fri, 12 Jul 2019 23:24:10 +0000 (16:24 -0700)
examples/print-number-from-stack/Makefile [new file with mode: 0644]
examples/print-number-from-stack/printnum.pvvs [new file with mode: 0644]
stdlib/README.md
stdlib/math.pvvs [new file with mode: 0644]
stdlib/stdio.pvvs

diff --git a/examples/print-number-from-stack/Makefile b/examples/print-number-from-stack/Makefile
new file mode 100644 (file)
index 0000000..de5bfc0
--- /dev/null
@@ -0,0 +1,17 @@
+# (c) 2019 Aaron Taylor <ataylor at subgeniuskitty dot com>
+# All rights reserved.
+
+include ../config.mk
+
+all: printnum
+
+printnum:
+       $(CPP) $(CPP_FLAGS) -o temp.pvvs printnum.pvvs
+       $(VVS_COMPILER) -i temp.pvvs -o printnum.vvs
+       @rm -f temp.pvvs
+
+run: printnum
+       $(VVS_INTERPRETER) -i printnum.vvs
+
+clean:
+       @rm -f printnum.vvs temp.pvvs
diff --git a/examples/print-number-from-stack/printnum.pvvs b/examples/print-number-from-stack/printnum.pvvs
new file mode 100644 (file)
index 0000000..a05b42b
--- /dev/null
@@ -0,0 +1,9 @@
+@@ This program outputs "-42424242"
+
+SSTTSTSSSSTTTSTSTSTTTTSTTSSTSN         | ST: Push -42424242
+NSTTSTSN                       | FC: JSR>1010 (print number from stack; see stdlib)
+SSSTSTSN                       | ST: Push +10 (ASCII '\n')
+TNSS                           | IO: Putchar
+NNN                            | FC: Terminate program
+
+#include "stdio.pvvs"
index e56080f..3bff980 100644 (file)
@@ -25,9 +25,21 @@ following reservations:
 The following labels are entry points to stdlib functions. Read the
 header comment for each function to learn the call and return stack.
 
-    stdio.pvvs:
-           1000 - print string from stack
-           1001 - print string from heap
+         000xxx - reserved
+         001xxx - print functions
+           1000 ----- print string from stack       (stdio.pvvs)
+           1001 ----- print string from heap        (stdio.pvvs)
+           1010 ----- print number from stack       (stdio.pvvs)
+         010xxx - math functions
+          10001 ----- absolute value                (math.pvvs)
+         011xxx - unassigned
+         100xxx - unassigned
+         101xxx - unassigned
+         110xxx - unassigned
+         111xxx - conversion functions
+        1xxxxxx - reserved for less common entry points
+        1000000 ----- print sign of number          (stdio.pvvs)
+        1000001 ----- print magnitude of number     (stdio.pvvs)
 
 # Misc #
 
diff --git a/stdlib/math.pvvs b/stdlib/math.pvvs
new file mode 100644 (file)
index 0000000..b1a6c0e
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef VVS_STDLIB_MATH
+#define VVS_STDLIB_MATH
+
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Description:
+@   This function returns the absolute value of its argument
+@ Call Stack:
+@   signed number  <-- TOS
+@ Return Stack:
+@   abs(signed number)  <-- TOS
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+NSSVTSSSTN              | Mark: 10001 (absolute value)
+SNS                     | DUP
+NTTSSSTSSSTSSSSSSSSN    | BMI > 00010001 00000000
+NSNSSSTSSSTSSSSSSSTN    | JMP > 00010001 00000001
+NSSVSSSTSSSTSSSSSSSSN   | Mark: 00010001 00000000
+SSTTN                   | PUSH -1
+TSSN                    | MULTIPLY
+NSSVSSSTSSSTSSSSSSSTN   | Mark: 00010001 00000001
+NTN                     | RTS
+
+#endif
index 03c60c7..e0ff711 100644 (file)
@@ -44,4 +44,77 @@ SNN                     | DROP
 SNN                     | DROP
 NTN                     | RTS
 
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Description:
+@   This functions prints a number from the stack in sign-magnitude format.
+@   Leading zeros are suppressed.
+@ Call Stack:
+@   number to print  <-- TOS
+@ Return Stack:
+@   <empty>
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+NSSVTSTSN               | Mark: 1010 (print number from stack)
+SNS                     | DUP
+NSTTSSSSSSN             | JSR > 1000000
+NSTTSSSSSTN             | JSR > 1000001
+NTN                     | RTS
+
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Description:
+@   This functions prints the sign of a number from the stack.
+@ Call Stack:
+@   number to print  <-- TOS
+@ Return Stack:
+@   <empty>
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+NSSVTSSSSSSN            | Mark: 1000000 (print sign of number from stack)
+NTTSTSSSSSSSSSSSSSTN    | BMI > 010000000 00000001
+SSSTSTSTTN              | PUSH ASCII '+'
+NSNSTSSSSSSSSSSSSTSN    | JMP > 010000000 00000010
+NSSVSTSSSSSSSSSSSSSTN   | Mark: 010000000 00000001
+SSSTSTTSTN              | PUSH ASCII '-'
+NSSVSTSSSSSSSSSSSSTSN   | Mark: 010000000 00000010
+TNSS                    | PUTC
+NTN                     | RTS
+
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Description:
+@   This functions prints the magnitude of a number from the stack.
+@ Call Stack:
+@   number to print  <-- TOS
+@ Return Stack:
+@   <empty>
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+NSSVTSSSSSTN            | Mark: 1000001 (print magnitude of number from stack)
+NSTTSSSTN               | JSR > 10001 (absolute value)
+
+SSSSN                   | PUSH ASCII '\0'
+SNT                     | SWAP
+
+@ Pick off one digit on each pass through this loop.
+NSSVSTSSSSSTSSSSSSSSN   | Mark: 01000001 00000000
+SNS                     | DUP
+
+@ Mod-off a digit, convert to ASCII, store on stack as part of the string.
+SSSTSTSN                | PUSH +10
+TSTT                    | MODULO
+SSSTTSSSSN              | PUSH ASCII '0'
+TSSS                    | ADD
+SNT                     | SWAP
+
+@ Divide down to next digit and keep looping if number != 0 yet.
+SSSTSTSN                | PUSH +10
+TSTS                    | DIVIDE
+SNS                     | DUP
+NTSSTSSSSSTSSSSSSSTN    | BRZ > 01000001 00000001
+NSNSTSSSSSTSSSSSSSSN    | JMP > 01000001 00000000
+
+@ Print the string we have built on the stack.
+NSSVSTSSSSSTSSSSSSSTN   | Mark: 01000001 00000001
+SNN                     | DROP
+NSTTSSSN                | JSR > 1000 (print string from stack)
+NTN                     | RTS
+
+#include <math.pvvs>
+
 #endif