1. 编译, 汇编, 链接
2. 寄存器
在x86-64系统中:
3. 内存的表示方式
4. 程序的运行原理
01: 程序运行时内存的状态:
更详细一点的图:
程序运行时,代码片段被储存在虚拟内存的 read only data 中
02: 代码的执行原理
How the instructions are executed?
- %rip stores an address of memory,
- from the address, machine can read a whole instruction once
- then execute it
- increase %rip
%rip is also called program counter (PC)
PC逐行执行指令的图例:
小知识点:机器是怎么知道call指令的地址在哪里呢?
注意到二进制代码中的42,代表的是下一条指令的地址加上42,就是跳转的地址
在这个例子中,就是400549+42=40058b,这个就是右边汇编语句对应的地址
之所以会这样,是因为机器执行完400548这条指令后,%rip移动到400549,这时候call指令发挥作用,将%rip+42,移动到需要执行的代码片段
03: 栈区
栈区存的内容:
- 函数的返回地址
- 调用函数时所传的(部分)参数
- 局部变量
关于函数调用与栈的详细关系,见我的另一篇博客 函数调用的实现