昨天被问到了尾递归及编译器对它的处理相关,一直对它没有研究过,解释得很含糊。
回来查了下,记录如下:.file "rec.c" .text .globl recurve_tail .type recurve_tail, @function recurve_tail: pushl %ebp movl %esp, %ebp subl $24, %esp cmpl $1, 8(%ebp) je .L2 movl 12(%ebp), %eax movl %eax, %edx imull 8(%ebp), %edx movl 8(%ebp), %eax subl $1, %eax movl %edx, 4(%esp) movl %eax, (%esp) call recurve_tail movl %eax, -4(%ebp) jmp .L3 .L2: movl 12(%ebp), %eax movl %eax, -4(%ebp) .L3: movl -4(%ebp), %eax leave ret .size recurve_tail, .-recurve_tail .ident "GCC: (Debian 4.3.2-1.1) 4.3.2" .section .note.GNU-stack,"",@progbits 未进行优化,与普通递归处理方式相同,新开辟了栈;再看-O3优化结果: .file "rec.c" .text .p2align 4,,15 .globl recurve_tail .type recurve_tail, @function recurve_tail: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl $1, %edx je .L2 .p2align 4,,7 .p2align 3 .L5: imull %edx, %eax subl $1, %edx cmpl $1, %edx jne .L5 .L2: popl %ebp ret .size recurve_tail, .-recurve_tail .ident "GCC: (Debian 4.3.2-1.1) 4.3.2" .section .note.GNU-stack,"",@progbits
原文出处:http://blog.csdn.net/hilyoo/article/details/4445476