Files
RomWBW/Source/Images/d_cowgol/u0/FACT.COW
Wayne Warthen a8a5a85c5c Update Cowgol Disk Image, Issue #506
Updated Cowgol disk image with the latest distribution from Ladislau Szilagyi.

Co-Authored-By: ladislau szilagyi <87603175+Laci1953@users.noreply.github.com>
2025-03-29 14:06:34 -07:00

33 lines
610 B
Plaintext

include "misc.coh";
var fp: int16;
sub factorial(n: int16): (ret: int16) is
var tmp: int16;
if n == 1 then
ret := 1;
else
# ret := n * factorial(n - 1);
@asm "ld hl,(", n, ")";
@asm "push hl";
n := n - 1;
@asm "ld hl,(", n, ")";
@asm "ld ix,(", fp, ")";
@asm "ld de, 1f";
@asm "push de";
@asm "jp (ix)";
@asm "1:";
@asm "ld (", tmp, "),hl"; #tmp = factorial(n-1)
@asm "pop hl";
@asm "ld (", n, "),hl";
ret := n * tmp;
end if;
end sub;
#setup pointer to factorial
@asm "ld hl,", factorial;
@asm "ld (", fp, "),hl";
print_i16(factorial(5));