all repos — xyw @ 255124f7b8554d4fec148bff092a035124b8f678

A minimal virtual machine and assembler for terminals.

examples/factorial16.xyw

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
.include "devices.xyw"

; Main program: calculate and print factorial of 7
$00 $07 fact JSRw 
print16 JSRw
$0A terminal.output STB
end JMPw

; Factorial subroutine
; a16 -- | [xy]
.label fact
  ; Save argument to both registers
  POPxyw
  .label fact.loop 
    ; Need to work with 16-bit comparisons
    ; but JCN only works with 8-bit values, so remove high byte first
    PSHyw $00 $00 EQUw SWP POP fact.end0 JCNw
    PSHyw $00 $01 EQUw SWP POP fact.end1 JCNw
    ; Decrement y and multiply
    DECyw PSHxw PSHyw MULxw POPxw
    fact.loop JMPw
  .label fact.end0
    ; Edge case: factorial of 0 is 1
    $1
    CLRxyw
    RTS
  .label fact.end1
    ; Return result
    PSHxw
    CLRxyw
    RTS

.include "print16.xyw"

.label end