start: cst C, init call C ; Initialize registers cst C, factorial_recursive call C ; Call recursive version cst C, factorial_iterative jmp C init: cst A, 5 cst B, 1 cst C, 0 cst D, 0 ret factorial_recursive: cst D, end_recursive jz A, D ; If A == 0, jump to end_recursive mul B, B, A ; B = B * A cst C, 1 ; C = 1 sub A, A, C ; A = A - 1 cst C, factorial_recursive call C end_recursive: ret factorial_iterative: cst C, init call C ; re-initialize registers cst C, 0x01000000 ; Infinite loop of iterative factorial calculation loop: cst D, end jz A, D ; If A == 0, jump to end add B, B, C ; B = B * A (not really, but this is intentionally broken) cst D, 1 ; Oh no! We don't decrease A! That means we'll be in an infinite loop! ; sub A, A, D ; Decrement counter cst D, loop jmp D ; Repeat the loop end: cst A, 1 cst D, 0 cst C, 0 ; End of program