0.本周进行了网易 mooc 公开课 《linux内核分析》课程,撰写此博客,一为完成做业,二为记录学习收获。linux
命令解释: push %eax <==> sub $4 ,%esp movl %eax,(%esp) pop % eax <==> movl (%esp),%eax addl $4,%esp call 0x12345 <==> push %eip movl $012345,%eip ret <==> popl %eip enter <==> pushl %ebp movl %esp,%ebp leave <==> movl %ebp,%esp popl %ebp
1.c程序代码:函数
int g(int x,int y) { return x + y; } int f(int x) { return g(x , 2*x); } int main(void) { return f(2) + 3; }
2.使用gcc –S –o main.s main.c -m32
将C程序汇编成32位汇编代码:学习
1. g: 2. pushl %ebp 3. movl %esp, %ebp 4. movl 12(%ebp), %eax 5. movl 8(%ebp), %edx 6. addl %edx, %eax 7. popl %ebp 8. ret 9. f: 10. pushl %ebp 11. movl %esp, %ebp 12. subl $8, %esp 13. movl 8(%ebp), %eax 14. addl %eax, %eax 15. movl %eax, 4(%esp) 16. movl 8(%ebp), %eax 17. movl %eax, (%esp) 18. call g 19. leave 20. ret 21. main: 22. pushl %ebp 23. movl %esp, %ebp 24. subl $4, %esp 25. movl $2, (%esp) 26. call f 27. addl $3, %eax 28. leave 29. ret
3.程序分析spa
(1)pushl %ebp
(2)movl %esp, %ebp
(3)subl $4, %esp
(4)movl $2, (%esp)
这里是将当即数2存到esp指向的内存地址中code
(5)call f
(6)pushl %ebp
(7)movl %esp, %ebp
(8)subl $8, %esp
(9)movl 8(%ebp), %eax
(10)addl %eax, %eax
(11)movl %eax, 4(%esp)
(12)movl 8(%ebp), %eax
(13)movl %eax, (%esp)
(14)call g
(15) pushl %ebp
(16)movl %esp, %ebp
(17) movl 12(%ebp), %eax
(18)movl 8(%ebp), %edx
(19)addl %edx, %eax
(20)popl %ebp
(21)ret
(22) leave
(23) ret
(24)addl $3, %eax
(25)leave
(26)ret
最终返回 eax = 9图片
4.学习总结
计算机经过执行存储的程序指令来进行工做,在程序执行过程当中,堆栈存储了临时的数据,以便进行函数的调用和返回,由ebp和esp肯定的临时栈对当前程序的执行环境进行了明确。在本次做业的完成过程当中,加深了对堆栈变化的理解,对于计算机内部寄存器、存储器之间完美有序的配合,实现程序执行的机制有了更深入的理解,是一次颇有意义的实践。ip
bintasong 原创做品转载请注明出处《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000内存