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 "lib/macros.xyw"
; Main program: calculate and print factorial of 7
$0007 fact JSRw
putdw JSRw
NL
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 $0000 EQUw LBYTE fact.end0 JCNw
PSHyw $0001 EQUw LBYTE 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
CLRxyw
RTS
.label fact.end1
; Return result
PSHxw
CLRxyw
RTS
.include "lib/putdw.xyw"
.label end
|