all repos — xyw @ 88877124cdbbd5971f945225c8ec4a7589014f6b

A minimal virtual machine and assembler for terminals.

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

; Main program: calculate and print factorial of 5
$05 fact JSRw 
putd JSRw
NL
end JMPw

; Factorial subroutine
; a8 -- | [xy]
.label fact
  ; Save argument to both registers
  POPxy
  .label fact.loop 
    PSHy $00 EQU fact.end0 JCNw
    PSHy $01 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
    $01
    CLRxy
    RTS
  .label fact.end1
    ; Return result
    PSHx
    CLRxy
    RTS

.include "lib/putd.xyw"

.label end