linux反汇编理解

来源:互联网 发布:如何提高党性知乎 通俗 编辑:程序博客网 时间:2024/05/22 10:31

Linux反汇编代码理解  

2011-04-27 23:20:14|  分类:Assemble |字号 订阅

~~~~~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 <+0>: push %ebp
0x080483f7 <+1>: mov %esp,%ebp
0x080483f9 <+3>: sub $0x8,%esp
0x080483fc <+6>: movl $0x4,0x4(%esp)
0x08048404 <+14>: movl $0x5,(%esp)
0x0804840b <+21>: call 0x80483b4 <triangle>
0x08048410 <+26>: leave
0x08048411 <+27>: ret
End of assembler dump.
(gdb) disass triangle
Dump of assembler code for function triangle:
0x080483b4 <+0>: push %ebp
0x080483b5 <+1>: mov %esp,%ebp
0x080483b7 <+3>: sub $0x20,%esp
0x080483ba <+6>: movl $0x0,-0x18(%ebp)
0x080483c1 <+13>: movl $0x1,-0x14(%ebp)
0x080483c8 <+20>: movl $0x2,-0x10(%ebp)
0x080483cf <+27>: movl $0x3,-0xc(%ebp)
0x080483d6 <+34>: movl $0x4,-0x8(%ebp)
0x080483dd <+41>: mov 0x8(%ebp),%eax
0x080483e0 <+44>: imul 0xc(%ebp),%eax
0x080483e4 <+48>: mov %eax,%edx
0x080483e6 <+50>: shr $0x1f,%edx
0x080483e9 <+53>: lea (%edx,%eax,1),%eax
0x080483ec <+56>: sar %eax
0x080483ee <+58>: mov %eax,-0x4(%ebp)
0x080483f1 <+61>: mov -0x4(%ebp),%eax
0x080483f4 <+64>: leave
0x080483f5 <+65>: ret
End of assembler dump.

~~~~~栈使用情况

Linux反汇编代码理解 - . - Welcome to the hell

~~~~~部分汇编代码解释

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 <triangle> ;跳转到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


仍然不明白width*height/2是怎么执行的……