汇编及C/C++汇编调用约定讲解
> 汇编及C/C++汇编调用约定(汇总帖)
> 汇编编译和gdb调试命令列表
> gdb TUI使用方法
> 汇编C语言调用约定(标准函数)
> 汇编C语言调用约定(递归函数)
> C++内存模型以及寄存器指针rsp和rbp
1. 递归函数
- 基线条件:递归停止的地方
- fac函数:
.type factorial, @function factorial: pushl %ebp movl %esp, %ebp # 4(%ebp) 保存返回值地址 movl 8(%ebp), %eax cmp $1, %eax je end_factorial decl %eax pushl %eax call factorial movl 8(%ebp), %ebx imull %ebx, %eax end_factorial: movl %ebp, %esp popl %ebp ret
2. 递归函数完整示例
.section .data
.section .text
.globl _start
.globl factorial
_start:
pushl $4
call factorial
add $4, %esp
movl %eax, %ebx
movl $1, %eax
int $0x80
.type factorial, @function
factorial:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
cmp $1, %eax
je end_factorial
decl %eax
pushl %eax
call factorial
movl 8(%ebp), %ebx
imull %ebx, %eax
end_factorial:
movl %ebp, %esp
popl %ebp
ret
3. 递归函数调试
- 关于递归我这里在纸上写了一个单步执行时栈的状态: