@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ Returns a kinda-random number.
@ Before using for the first time, seed heap[0] with a value.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSSSN | Mark: 10000 (random)
@ Generate the next seed value
SSSTSSSSSTTTSSSTTSSTSSTTTSSTTSTTSTN | PUSH 1103515245
SSSTTSSSSSSTTTSSTN | PUSH 12345
@ Store the next seed value but keep a copy on the stack.
@ Calculate the random number and return.
SSSTSSSSSSSSSSSSSSSSN | PUSH 65536
SSSTSSSSSSSSSSSSSSSN | PUSH 32768
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ Returns the absolute value of its argument
@ abs(signed number) <-- TOS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSSTN | Mark: 10001 (absolute value)
@ Catch -(2^63) as a special case since its absolute value will overflow
@ a twos-complement 64-bit word. Return zero as though the absolute value
@ overflowed to the bottom of the non-negative integers rather than
@ overflowing back to the most negative integer.
SSTTSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSN | -(2^63)
NTSSSSTSSSTSSSSSSTSN | BRZ > 00010001 00000010
@ Handle all the other numbers.
NTTSSSTSSSTSSSSSSSSN | BMI > 00010001 00000000
NSNSSSTSSSTSSSSSSSTN | JMP > 00010001 00000001
NSSVSSSTSSSTSSSSSSSSN | Mark: 00010001 00000000
NSSVSSSTSSSTSSSSSSSTN | Mark: 00010001 00000001
@ Special case: Push 0 and return.
NSSVSSSTSSSTSSSSSSTSN | Mark: 00010001 00000010
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ Returns greatest common divisor of X and Y.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSTSN | Mark: 10010 (gcd)
@ Since 1 > -1, transform problem into gcd(abs(X),abs(Y)).
NSTTSSSTN | JSR > 10001 (abs)
NSTTSSSTN | JSR > 10001 (abs)
@ Verify neither operand is zero.
NTSSSSTSSTSSSSSSSSSN | BRZ > 00010010 00000000 (gcd:zero input)
NTSSSSTSSTSSSSSSSSSN | BRZ > 00010010 00000000 (gcd:zero input)
@ Verify X != Y and sort X,Y so the smaller is TOS.
NSTTTSSN | JSR > 1100 (deepdup)
NTTSSSTSSTSSSSSSSSTN | BMI > 00010010 00000001 (gcd:swap inputs)
NSNSSSTSSTSSSSSSSTSN | JMP > 00010010 00000010 (gcd:main loop)
NSSVSSSTSSTSSSSSSSSTN | MARK: 00010010 00000001 (gcd:swap inputs)
NSSVSSSTSSTSSSSSSSTSN | MARK: 00010010 00000010 (gcd:main loop)
NSTTSTSN | JSR > 1010 (stackrotate)
NTSSSSTSSTSSSSSSSTTN | BRZ > 00010010 00000011 (gcd:loop termination)
NSNSSSTSSTSSSSSSSTSN | JMP > 00010010 00000010 (gcd:main loop)
NSSVSSSTSSTSSSSSSSTTN | MARK: 00010010 00000011 (gcd:loop termination)
@ At least one operand was zero.
@ Since we define gcd(a,0) = a, return the other operand.
NSSVSSSTSSTSSSSSSSSSN | MARK: 00010010 00000000 (gcd:zero input)