Added print16.xyw and print8.xyw
h3rald h3rald@h3rald.com
Thu, 05 Feb 2026 16:13:57 +0100
4 files changed,
85 insertions(+),
1 deletions(-)
A
examples/factorial8.xyw
@@ -0,0 +1,33 @@
+.include "devices.xyw" + +; ;ain program: calculate and print factorial of 5 +$5 fact JSRw +print8 JSRw +$0A terminal.output STB +end JMPw + +; Factorial subroutine +; a8 -- | [xy] +.label fact + ; Save argument to both registers + POPxy + .label fact.loop + PSHy $0 EQU fact.end0 JCNw + PSHy $1 EQU fact.end1 JCNw + ; Decrement y and multiply + DECy PSHx PSHy MULx POPx + fact.loop JMPw + .label fact.end0 + ; Edge case: factorial of 0 is 1 + $1 + CLRxy + RTS + .label fact.end1 + ; Return result + PSHx + CLRxy + RTS + +.include "print8.xyw" + +.label end
A
examples/print16.xyw
@@ -0,0 +1,28 @@
+.include "devices.xyw" + +$321 print16 JSRw +$0A terminal.output STB +end JMPw + +; Printing subroutine +; a16 -- | [xw] +.label print16 + POPxw + ; If value is less than 10, print single digit + ; Push 16-bit literal as two bytes (high, low), then compare + PSHxw $00 $0A LTHw SWP POP print16.single JCNw + ; Divide by 10, stack becomes [remainder, quotient] with quotient on top + PSHxw $00 $0A DIVw + ; Call print16 recursively with quotient (on top, consumed by POPxw) + print16 JSRw + ; Remainder (16-bit) is still on stack, get low byte and print it + SWP POP $30 ADD terminal.output STB + CLRxw + RTS + .label print16.single + ; Print single digit (need low byte of XW since value is 0-9) + PSHxw SWP POP $30 ADD terminal.output STB + CLRxw + RTS + +.label end
A
examples/print8.xyw
@@ -0,0 +1,23 @@
+; Assumes .include "devices.xyw" + +; Printing subroutine +; a8 -- | [xy] +.label print8 + POPx + ; If value is less than 10, print single digit + PSHx $A LTH print8.single JCNw + ; Divide by 10: stack becomes [remainder, quotient] + PSHx $A DIV + ; Save remainder to y for later + SWP POPy + ; Call print8 recursively with quotient (still on stack) + print8 JSRw + ; Print the saved remainder + PSHy $30 ADD terminal.output STB + CLRxy + RTS + .label print8.single + ; Print single digit + PSHx $30 ADD terminal.output STB + CLRx + RTS
M
test.sh
→
test.sh
@@ -7,7 +7,7 @@ "run_test helloworld.xyw assert_output_equals \$'Hello, World!\\n'"
"run_test d6.xyw assert_output_in_range 1 6" "run_test on-error.xyw assert_output_equals \$'Error: Division by zero.\\nError: Unknown error.\\n'" "run_test print-args.xyw test1 test2 -- assert_output_equals \$'print-args.xim, test1, test2\\n'" - "run_test factorial.xyw assert_output_equals \$'120\\n'" + "run_test factorial8.xyw assert_output_equals \$'120\\n'" ) ### Helper functions