Added functions to stdlib:
[vvhitespace] / stdlib / stdio.pvvs
index d9184ad..49b8cfc 100644 (file)
@@ -1,15 +1,20 @@
-################################################################################
-# Description:
-#   This function prints a null-terminated string from the stack.
-# Call Stack:
-#   null-terminator (ASCII '\0')
-#   char n
-#   ...
-#   char 2
-#   char 1 <-- TOS
-# Return Stack:
-#   <empty>
-################################################################################
+#ifndef VVS_STDLIB_STDIO
+#define VVS_STDLIB_STDIO
+
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Name:
+@   printstackstring (1000)
+@ Description:
+@   Prints a null-terminated string from the stack.
+@ Call Stack:
+@   null-terminator (ASCII '\0')
+@   char n
+@   ...
+@   char 2
+@   char 1 <-- TOS
+@ Return Stack:
+@   <empty>
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 NSSVTSSSN               | Mark: 1000 (print string from stack)
 SNS                     | DUP
 NTSSSSSTSSSSSSSSSSTN    | BRZ > 00001000 00000001
 NSSVTSSSN               | Mark: 1000 (print string from stack)
 SNS                     | DUP
 NTSSSSSTSSSSSSSSSSTN    | BRZ > 00001000 00000001
@@ -19,14 +24,16 @@ NSSVSSSSTSSSSSSSSSSTN   | Mark: 00001000 00000001
 SNN                     | DROP
 NTN                     | RTS
 
 SNN                     | DROP
 NTN                     | RTS
 
-################################################################################
-# Description:
-#   This function prints a null-terminated string from the heap.
-# Call Stack:
-#   pointer to first character  <-- TOS
-# Return Stack:
-#   <empty>
-################################################################################
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Name:
+@   printheapstring (1001)
+@ Description:
+@   Prints a null-terminated string from the heap.
+@ Call Stack:
+@   pointer to first character  <-- TOS
+@ Return Stack:
+@   <empty>
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 NSSVTSSTN               | Mark: 1001 (print string from heap)
 SNS                     | DUP
 TTT                     | LOAD
 NSSVTSSTN               | Mark: 1001 (print string from heap)
 SNS                     | DUP
 TTT                     | LOAD
@@ -40,3 +47,83 @@ NSSVSSSSTSSTSSSSSSSTN   | Mark: 00001001 00000001
 SNN                     | DROP
 SNN                     | DROP
 NTN                     | RTS
 SNN                     | DROP
 SNN                     | DROP
 NTN                     | RTS
+
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Name:
+@   printstacknumber (1010)
+@ Description:
+@   Prints 'number' from the stack in sign-magnitude format.
+@   Leading zeros are suppressed.
+@ Call Stack:
+@   number  <-- TOS
+@ Return Stack:
+@   <empty>
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+NSSVTSTSN               | Mark: 1010 (print number from stack)
+SNS                     | DUP
+NSTTSSSSTSN             | JSR > 1000010
+NSTTSSSSTTN             | JSR > 1000011
+NTN                     | RTS
+
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Name:
+@   printstacknumbersign (1000010)
+@ Description:
+@   Prints the sign of 'number' from the stack.
+@ Call Stack:
+@   number  <-- TOS
+@ Return Stack:
+@   <empty>
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+NSSVTSSSSTSN            | Mark: 1000010 (print sign of number from stack)
+NTTSTSSSSTSSSSSSSSTN    | BMI > 01000010 00000001
+SSSTSTSTTN              | PUSH ASCII '+'
+NSNSTSSSSTSSSSSSSTSN    | JMP > 01000010 00000010
+NSSVSTSSSSTSSSSSSSSTN   | Mark: 01000010 00000001
+SSSTSTTSTN              | PUSH ASCII '-'
+NSSVSTSSSSTSSSSSSSTSN   | Mark: 01000010 00000010
+TNSS                    | PUTC
+NTN                     | RTS
+
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Name:
+@   printstacknumbermagnitude (1000011)
+@ Description:
+@   Prints the magnitude of 'number' from the stack.
+@ Call Stack:
+@   number  <-- TOS
+@ Return Stack:
+@   <empty>
+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+#include <math.pvvs>
+NSSVTSSSSTTN            | Mark: 1000011 (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.
+NSSVSTSSSSTTSSSSSSSSN   | Mark: 01000011 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
+NTSSTSSSSTTSSSSSSSTN    | BRZ > 01000011 00000001
+NSNSTSSSSTTSSSSSSSSN    | JMP > 01000011 00000000
+
+@ Print the string we have built on the stack.
+NSSVSTSSSSTTSSSSSSSTN   | Mark: 01000011 00000001
+SNN                     | DROP
+NSTTSSSN                | JSR > 1000 (print string from stack)
+NTN                     | RTS
+
+#endif