本文共 1941 字,大约阅读时间需要 6 分钟。
~~~~~C语言代码example.c
int triangle( int width, int height)
{
int arr{0,1,2,3,4};
int area;
area = width * height /2;
return (area);
}
void main()
{
triangle(5,4);
}
~~~~~gdb反汇编代码
$ gdb example
(gdb) disass main
Dump of assembler code for function main:
0x080483f6 : push %ebp
0x080483f7 : mov %esp,%ebp
0x080483f9 : sub $0x8,%esp
0x080483fc : movl $0x4,0x4(%esp)
0x08048404 : movl $0x5,(%esp)
0x0804840b : call 0x80483b4 0x08048410 : leave
0x08048411 : ret
End of assembler dump.
(gdb) disass triangle
Dump of assembler code for function triangle:
0x080483b4 : push %ebp
0x080483b5 : mov %esp,%ebp
0x080483b7 : sub $0x20,%esp
0x080483ba : movl $0x0,-0x18(%ebp)
0x080483c1 : movl $0x1,-0x14(%ebp)
0x080483c8 : movl $0x2,-0x10(%ebp)
0x080483cf : movl $0x3,-0xc(%ebp)
0x080483d6 : movl $0x4,-0x8(%ebp)
0x080483dd : mov 0x8(%ebp),%eax
0x080483e0 : imul 0xc(%ebp),%eax
0x080483e4 : mov %eax,%edx
0x080483e6 : shr $0x1f,%edx
0x080483e9 : lea (%edx,%eax,1),%eax
0x080483ec : sar %eax
0x080483ee : mov %eax,-0x4(%ebp)
0x080483f1 : mov -0x4(%ebp),%eax
0x080483f4 : leave
0x080483f5 : ret
End of assembler dump.
~~~~~栈使用情况
~~~~~部分汇编代码解释
main:
mov %esp,%ebp ;esp-->ebp
sub $0x8,%esp ;esp-8-->esp
movl $0x4,0x4(%esp) ;4-->esp+4
movl $0x5,(%esp) ;5-->esp
call 0x80483b4 ;跳转到0x80483b4,同时将下一条指令的地址(0x08048410)压栈(即ret)
triangle:
sub $0x20,%esp ;esp-20-->esp
movl $0x0,-0x18(%ebp) ;0-->ebp-18
movl $0x1,-0x14(%ebp) ;1-->ebp-14
movl $0x2,-0x10(%ebp) ;2-->ebp-10
movl $0x3,-0xc(%ebp) ;3-->ebp-c
movl $0x4,-0x8(%ebp) ;4-->ebp-8
mov 0x8(%ebp),%eax ;ebp+8(即param1:5)-->eax
imul 0xc(%ebp),%eax ;ebp+c(即param2:4)*eax(即param1:5)
mov %eax,%edx
shr $0x1f,%edx ;逻辑右移(高位补0)
lea (%edx,%eax,1),%eax
sar %eax ;算术右移
mov %eax,-0x4(%ebp) ;把运算结果放入area变量中
mov -0x4(%ebp),%eax
leave
ret
enter等价于push %ebp
mov %esp,%ebp
leave等价于mov %ebp,%esp
pop %ebp
ret num等价于pop %eip
add num,%esp
movl variable,%eax ;把variable作为一个地址,取地址为variable处的值赋给eax
movl $variable,%eax ;把variable作为一个立即数赋给eax
转载地址:http://nrqhp.baihongyu.com/