all repos — xyw @ 09909160cf03d85143caee8059e5d650b24658ac

A minimal virtual machine and assembler for terminals.

Refactored library functions and macros.
h3rald h3rald@h3rald.com
Fri, 06 Feb 2026 08:12:03 +0100
commit

09909160cf03d85143caee8059e5d650b24658ac

parent

b4405494fd2f54383c8bcc2357b0e6b6e0b34d02

M examples/d6.xywexamples/d6.xyw

@@ -1,4 +1,4 @@

-.include "devices.xyw" +.include "_macros.xyw" ; Get random number and calculate modulo 6 system.random LDB $6 DIV POP
M examples/devices.xywexamples/_macros.xyw

@@ -1,3 +1,5 @@

+;;; Devices + ;system .macro system.state $00 .macro system.error $01
M examples/factorial16.xywexamples/factorial16.xyw

@@ -1,8 +1,8 @@

-.include "devices.xyw" +.include "_macros.xyw" ; Main program: calculate and print factorial of 7 $00 $07 fact JSRw -print16 JSRw +PUTDw JSRw $0A terminal.output STB end JMPw

@@ -30,6 +30,6 @@ PSHxw

CLRxyw RTS -.include "print16.xyw" +.include "lib/PUTDw.xyw" .label end
M examples/factorial8.xywexamples/factorial8.xyw

@@ -1,8 +1,8 @@

-.include "devices.xyw" +.include "_macros.xyw" ; Main program: calculate and print factorial of 5 $5 fact JSRw -print8 JSRw +PUTD JSRw $0A terminal.output STB end JMPw

@@ -28,6 +28,6 @@ PSHx

CLRxy RTS -.include "print8.xyw" +.include "lib/PUTD.xyw" .label end
M examples/helloworld.xywexamples/helloworld.xyw

@@ -1,5 +1,5 @@

; include device macros -.include "devices.xyw" +.include "_macros.xyw" ; main program hello print JSRw
A examples/lib/PUTS.xyw

@@ -0,0 +1,11 @@

+; .include "_macros.xyw" + +; Print a string starting at address +; a16 -- | [xw] +.label PUTS + POPxw + .label PUTS.loop + LDBxw terminal.output STB + INCxw LDBxw PUTS.loop JCNw + CLRxw + RTS
M examples/on-error.xywexamples/on-error.xyw

@@ -1,4 +1,4 @@

-.include "devices.xyw" +.include "_macros.xyw" ; register on error handler on_error system.on_error STW

@@ -10,23 +10,15 @@

; on error handler .label on_error system.error LDB $1 EQU on_error.div-by-zero.print JCNw - unknown-error print JSRw + unknown-error PUTS JSRw $0 system.error STB RTS .label on_error.div-by-zero.print - div-by-zero-error print JSRw + div-by-zero-error PUTS JSRw $3 system.error STB RTS -; printing subroutine -; a16 -- -.label print - POPxw - .label print.loop - LDBxw terminal.output STB - INCxw LDBxw print.loop JCNw - CLRxw - RTS +.include "lib/PUTS.xyw" .label div-by-zero-error .string "Error: Division by zero.\n"
M examples/print-args.xywexamples/print-args.xyw

@@ -1,4 +1,4 @@

-.include "devices.xyw" +.include "_macros.xyw" ; Set on_argument event handler print-args terminal.on_argument STW
M examples/print-stdin.xywexamples/print-stdin.xyw

@@ -1,5 +1,5 @@

-.include "devices.xyw" +.include "_macros.xyw" ; Set on_keypress event handler print-key terminal.on_keypress STW
M examples/print16.xywexamples/lib/PUTDw.xyw

@@ -1,21 +1,21 @@

-; .include "devices.xyw" +; .include "_macros.xyw" -; Print a 16bit integer +; Print a 16bit integer as decimal ; a16 -- | [x] -.label print16 +.label PUTDw 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 + PSHxw $00 $0A LTHw SWP POP PUTDw.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 + ; Call PUTDw recursively with quotient (on top, consumed by POPxw) + PUTDw 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 + .label PUTDw.single ; Print single digit (need low byte of XW since value is 0-9) PSHxw SWP POP $30 ADD terminal.output STB CLRxw
M examples/print8.xywexamples/lib/PUTD.xyw

@@ -1,20 +1,20 @@

-; .include "devices.xyw" +; .include "_macros.xyw" -; Print an 8bit integer +; Print an 8bit integer as decimal ; a8 -- | [x] -.label print8 +.label PUTD POPx ; If value is less than 10, print single digit - PSHx $0A LTH print8.single JCNw + PSHx $0A LTH PUTD.single JCNw ; Divide by 10: stack becomes [remainder, quotient] with quotient on TOP PSHx $0A DIV - ; Call print8 recursively with quotient (on top, consumed by POPx) - print8 JSRw + ; Call PUTD recursively with quotient (on top, consumed by POPx) + PUTD JSRw ; Remainder is still on stack, print it $30 ADD terminal.output STB CLRx RTS - .label print8.single + .label PUTD.single ; Print single digit PSHx $30 ADD terminal.output STB CLRx
M xyw-vscode/syntaxes/xyw.tmLanguage.jsonxyw-vscode/syntaxes/xyw.tmLanguage.json

@@ -157,7 +157,12 @@ "patterns": [

{ "comment": "Device identifiers: system.*, terminal.*, file.*, clock.*, beeper.*", "name": "support.type.device.xyw", - "match": "\\b(system|terminal|file|clock|beeper)\\.[a-zA-Z0-9_.\\-]*\\b" + "match": "\\b(system|terminal|file|clock|beeper|)\\.[a-zA-Z0-9_.\\-]*\\b" + }, + { + "comment": "Library subroutines", + "name": "keyword.other.library.xyw", + "match": "\\b(PUTD|PUTDw|PUTS)\\b" }, { "comment": "Label/macro references: starts with letter/underscore, can contain letters, numbers, underscores, dashes, or dots",
M xyw.vimxyw.vim

@@ -33,6 +33,9 @@

" Device identifiers (system.*, terminal.*, file.*, clock.*, beeper.*) syntax match xywDeviceId "\<\(system\|terminal\|file\|clock\|beeper\)\.[a-zA-Z0-9_.\-]*\>" +" Library subroutines +syntax match xywLibrary "\<\(PUTD\|PUTDw\|PUTS\)\>" + " Instructions - BRK and RTS have no modifiers syntax match xywInstruction "\<\(BRK\|RTS\)\>"

@@ -55,6 +58,7 @@ highlight default link xywEscape SpecialChar

highlight default link xywHexNumber Number highlight default link xywBinNumber Number highlight default link xywInstruction Statement +highlight default link xywLibrary Keyword highlight default link xywDeviceId Type highlight default link xywIdentifier Identifier