/* 
 * File:        factr.s
 * Description: A recursive factorial function.
 */
        .arch   armv8-a

        .global factorial

        .text
        .align  2

factorial:                              // The main function
        stp     fp, lr, [sp,-16]!
        mov     fp, sp
        sub     sp, sp, #16             // Allocate space for a local variable

	cmp     x0, #0                  // Test for 0
        beq     zero
        
        str     x0, [fp, -8]            // Store n on the stack
        sub     x0, x0, 1               // Decrement n
        bl      factorial               // Do a recursive call

        ldr     x1, [fp, -8]            // Multiply the return value by n
        mul     x0, x0, x1
        b       done
zero:
        mov     x0, 1
done:
	add     sp, sp, #16             // Return from fact
        ldp     fp, lr, [sp], 16
        ret
