all repos — xyw @ f1a59986d12233f629ea0a41cc92ba89aacb3a12

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
.include "lib/macros.xyw"

; Main program: calculate and print factorial of 7
$0007 fact JSRw 
putdw JSRw
NL
HLT

; Factorial subroutine
; a16 -[xy]-
.label fact
  ; Save argument to both registers
  POPxyw
  .label fact.loop 
    ; Need to work with 16-bit comparisons
    PSHyw $0000 EQUw fact.end0 JCNw
    PSHyw $0001 EQUw 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
    $0001
    RTS
  .label fact.end1
    ; Return result
    PSHxw
    RTS

.include "lib/putdw.xyw"