@ (c) 2020 Aaron Taylor <ataylor at subgeniuskitty dot com>
@ See LICENSE.txt file for copyright and license details.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ printstackstring (1000100)
@ Prints a null-terminated string from the stack.
@ null-terminator (ASCII '\0')
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSSTSSN | Mark: 1000100 (print string from stack)
NTSSTSSSTSSSSSSSSSTN | BRZ > 01000100 00000001
NSNTSSSTSSN | JMP > 1000100
NSSVSTSSSTSSSSSSSSSTN | Mark: 01000100 00000001
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ printheapstring (1000101)
@ Prints a null-terminated string from the heap.
@ pointer to first character <-- TOS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSSTSTN | Mark: 1000101 (print string from heap)
NTSSTSSSTSTSSSSSSSTN | BRZ > 01000101 00000001
NSNTSSSTSTN | JMP > 1000101
NSSVSTSSSTSTSSSSSSSTN | Mark: 01000101 00000001
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ printstacknumber (1001)
@ Prints 'number' from the stack in sign-magnitude format.
@ Leading zeros are suppressed.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSTN | Mark: 1001 (print number from stack)
NSTTSSSSTSN | JSR > 1000010 (printstacknumbersign)
NSTTSSSSTTN | JSR > 1000011 (printstacknumbermagnitude)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ printstacknumbersign (1000010)
@ Prints the sign of 'number' from the stack.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
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
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ printstacknumbermagnitude (1000011)
@ Prints the magnitude of 'number' from the stack.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSSSTTN | Mark: 1000011 (print magnitude of number from stack)
@ Catch -(2^63) as a special case since its absolute value will overflow
@ a twos-complement 64-bit word.
SSTTSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSN | -(2^63)
NTSSTSSSSTTSSSSSSTSN | BRZ > 01000011 00000010
@ No special case applies. Prepare for computation by converting the number
@ to absolute value and preparing a string on the stack.
NSTTSSSTN | JSR > 10001 (absolute value)
@ Pick off one digit on each pass through this loop.
NSSVSTSSSSTTSSSSSSSSN | Mark: 01000011 00000000
@ Mod-off a digit, convert to ASCII, store on stack as part of the string.
SSSTTSSSSN | PUSH ASCII '0'
@ Divide down to next digit and keep looping if number != 0 yet.
NTSSTSSSSTTSSSSSSSTN | BRZ > 01000011 00000001
NSNSTSSSSTTSSSSSSSSN | JMP > 01000011 00000000
@ Print the string we have built on the stack.
NSSVSTSSSSTTSSSSSSSTN | Mark: 01000011 00000001
NSTTSSSTSSN | JSR > 1000100 (print string from stack)
@ Replace the number on the stack with its decimal ASCII representation.
NSSVSTSSSSTTSSSSSSTSN | BRZ > 01000011 00000010
NSNSTSSSSTTSSSSSSSTN | JMP > 01000011 00000001
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ If printing a static string (i.e. no substitutions), pass
@ 'number of substitutions' as 0, immediately followed by 'string word 1'.
@ If printing a string from the heap instead of stack, pass an empty string
@ on the stack followed by a pointer to the first word of the
@ null-terminated string on the heap.
@ <remainder of call stack is unchanged>
@ Maximum substitutions determined by upper heap limit in stackrotate and
@ stackrotatereverse subroutines.
@ number of substitutions <-- TOS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSSN | Mark: 1000 (printf)
@ If the stack contains an empty string (i.e. just an ASCII '\0'), the next
@ word is a pointer we must use to load the string from the heap.
@ This will leave the stack looking exactly like the example call stack above.
@ Do the test this way so we can keep the code inline.
NSTTTSSN | JSR > 1100 (deepdup)
NTTSSSSTSSSSSSSSSSTN | BMI > 00001000 00000001
NSTTSTTN | JSR > 1011 (stackrotatereverse)
NSTTSTTN | JSR > 1011 (stackrotatereverse)
NSTSSSSTSSSTSSSTTSSN | JSR > 00001000 10001100 (printf_deepslurp)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ The rest of printf parses a string according to the following information.
@ ASCII '\ ': putchar '\ '
@ ASCII 'n': putchar '\n'
@ ASCII 't': putchar '\t'
@ ASCII 'c': (print character)
@ ASCII 's': (print string)
@ ASCII 'd': (print decimal digit)
@ ASCII 'u': (print abs(integer), w/o sign)
@ ASCII 'i': (print integer w/sign)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ The next block tests the char against all possible level 1 branches (see above).
@ If there is a match, execution jumps to the appropriate level 2 branch label.
@ If no match is found, print the character and move on.
NSSVSSSSTSSSSSSSSSSTN | Mark: 00001000 00000001
@ Move the next character of the string to TOS.
NSTSSSSTSSSTSSSTSTTN | JSR > 00001000 10001011 (next char to TOS)
@ TOS is an ASCII '\ '. Jump to process the possible level 2 branches.
SSSTSTTTSSN | PUSH ASCII slash
NTSSSSSTSSSSSSSSSTSN | BRZ > 00001000 00000010
@ TOS is an ASCII '%'. Jump to process the possible level 2 branches.
SSSTSSTSTN | PUSH ASCII '%'
NTSSSSSTSSSSSSSSSTTN | BRZ > 00001000 00000011
@ TOS is an ASCII "\0". Jump to clean-up-and-exit.
NTSSSSSTSSSSSSSSSSSN | BRZ > 00001000 00000000
@ TOS is a normal character. Print it and loop again.
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ Level 2 - ASCII '\ ' - Escapes
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ The level 1 match was an ASCII '\ '.
@ Now look for level 2 matches that trigger a character substitution (n -> newline, etc).
@ If no matches are found, print the character directly (e.g. "\%" -> '%')
@ When finished, loop back to testing level 1 branches.
NSSVSSSSTSSSSSSSSSTSN | Mark: 00001000 00000010
@ Move the next character of the string to TOS.
NSTSSSSTSSSTSSSTSTTN | JSR > 00001000 10001011 (next char to TOS)
SSSTTSTTTSN | PUSH ASCII 'n'
NTSSSSSTSSSSSSSSTSSN | BRZ > 00001000 00000100
SSSTTTSTSSN | PUSH ASCII 't'
NTSSSSSTSSSSSSSSTSTN | BRZ > 00001000 00000101
@ No substitution necessary. Print literally.
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
@ These are utility labels to output the appropriate non-printable ASCII character.
@ After output, they loop back to testing level 1 branches.
@ Print a newline and loop for the next character.
NSSVSSSSTSSSSSSSSTSSN | Mark: 00001000 00000100
SSSTSTSN | PUSH ASCII '\n'
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
@ Print a horizontal tab and loop for the next character.
NSSVSSSSTSSSSSSSSTSTN | Mark: 00001000 00000101
SSSTSSTN | PUSH ASCII '\t'
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ Level 2 - ASCII '%' - Substitutions
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ The level 1 match was an ASCII '%'.
@ Now look for level 2 matches that trigger a substitution.
@ When finished, loop back to testing level 1 branches.
NSSVSSSSTSSSSSSSSSTTN | Mark: 00001000 00000011
@ Move the next character of the string to TOS.
NSTSSSSTSSSTSSSTSTTN | JSR > 00001000 10001011 (next char to TOS)
@ Check for ASCII 'c' - Print character
SSSTTSSSTTN | PUSH ASCII 'c'
NTSSSSSTSSSSSSSSTTSN | BRZ > 00001000 00000110
@ Check for ASCII 's' - Print string
SSSTTTSSTTN | PUSH ASCII 's'
NTSSSSSTSSSSSSSSTTTN | BRZ > 00001000 00000111
@ Check for ASCII 'd' - Print decimal digit
SSSTTSSTSSN | PUSH ASCII 'd'
NTSSSSSTSSSSSSSTSSSN | BRZ > 00001000 00001000
@ Check for ASCII 'u' - Print unsigned number
SSSTTTSTSTN | PUSH ASCII 'u'
NTSSSSSTSSSSSSSTSSTN | BRZ > 00001000 00001001
@ Check for ASCII 'i' - Print signed number
SSSTTSTSSTN | PUSH ASCII 'i'
NTSSSSSTSSSSSSSTSTSN | BRZ > 00001000 00001010
@ Unrecognized substitution specifier.
@ For now, silently consume it and continue.
@ Do not increment the substitution counter.
@ TODO: Is this really what I want to do here?
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
@ These are utility labels to call the appropriate type of output subroutine.
@ After output, they decrement the substition counter and loop back to testing
NSSVSSSSTSSSSSSSSTTSN | Mark: 00001000 00000110
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
NSSVSSSSTSSSSSSSSTTTN | Mark: 00001000 00000111
NSTTSSSTSTN | JSR > 1000101 (print string from heap)
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
NSSVSSSSTSSSSSSSTSSSN | Mark: 00001000 00001000
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
@ Print an unsigned integer
NSSVSSSSTSSSSSSSTSSTN | Mark: 00001000 00001001
NSTTSSSSTTN | JSR > 1000011 (print magnitude of number from stack)
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
NSSVSSSSTSSSSSSSTSTSN | Mark: 00001000 00001010
NSTTSSTN | JSR > 1001 (print number from stack)
NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ These are misc labels associated with the printf function.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ Found an ASCII "\0" when processing the format string. Clean up and exit.
NSSVSSSSTSSSSSSSSSSSN | Mark: 00001000 00000000
@ Move the next string character to TOS.
@ Stack should look like the printf call stack, with num-of-subs at TOS.
NSSVSSSSTSSSTSSSTSTTN | Mark: 00001000 10001011 (next char to TOS)
NSTTSTTN | JSR > 1011 (stackrotatereverse)
@ Slurps a string from the heap to the stack, storing it behind the substitutions.
@ number of substitutions <-- TOS
@ number of substitutions <-- TOS
@ TODO: This, along with a deepspew, should probably be stdlib routines.
NSSVSSSSTSSSTSSSTTSSN | Mark: 00001000 10001100 (printf_deepslurp)
@ Advance a duplicate copy of the pointer until it points to the null-terminator.
NSSVSSSSTSSSTSSSTTSTN | Mark: 00001000 10001101
NTSSSSSTSSSTSSSTTTSN | BRZ > 00001000 10001110
NSNSSSSTSSSTSSSTTSTN | JMP > 00001000 10001101
@ Load a character to the stack on each pass through this loop.
NSSVSSSSTSSSTSSSTTTSN | Mark: 00001000 10001110
NSTTTSSN | JSR > 1100 (deepdup)
NSTTSTSN | JSR > 1010 (stackrotate)
NSTTTSSN | JSR > 1100 (deepdup)
NTSSSSSTSSSTSSSTTTTN | BRZ > 00001000 10001111
@ Decrement pointer to end of string, loop again.
NSNSSSSTSSSTSSSTTTSN | JMP > 00001000 10001110
NSSVSSSSTSSSTSSSTTTTN | Mark: 00001000 10001111