/* fact7.s - recursive factorial */ /* function main that calls fact(7) and prints the result */ .globl main main: push %rbp /* create stack frame */ movq %rsp, %rbp movq $7, %rdi call fact /* call fact(7) */ movq $mystr, %rdi /* arg1 = mystr */ movq %rax, %rsi /* arg2 = return value of fact */ call printf /* call printf(mystr, result) */ xorl %eax, %eax /* return 0 */ pop %rbp ret /* recursive function fact to compute the factorial * equivalent to the following C function: * * long fact(long n) { * long n1 = n-1; * if (n1 == 0) * return 1; * else * return n * fact(n1); * } */ fact: push %rbp /* create stack frame */ movq %rsp, %rbp push %rbx /* save rbx (used to hold n) */ movq %rdi, %rbx /* store n */ decq %rdi /* n1-- */ jz basecase /* if 0, goto basecase */ call fact /* call fact(n1) */ imulq %rbx, %rax /* multiply result by n */ jmp end basecase: movq $1, %rax /* return 1 */ end: pop %rbx /* restore rbx */ pop %rbp /* remove stack frame */ ret .section .rodata mystr: .string "fact(7) = %ld\n"