Skip to content

实验一 :分析汇编代码理解计算机是如何工作的 #2

@ejacky

Description

@ejacky

张健飞
《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

编写 C 代码

int g(int x)
{
  return x + 3;
}

int f(int x)
{
  return g(x);
}

int main(void)
{
  return f(8) + 1;
}

编译代码

gcc –S –o main.s main.c -m32

生成汇编代码

linux first s

简化该汇编代码

去掉中间代码

linux first simple

main:
1 形成堆栈
2 分配一个内存空间
3 传 参数 8
4 调用 函数 f
5 返回值 + 1
6 离开
f:
1 形成堆栈
2 分配空间
3 传参数 8
4 寄存器 eax 中的值, 给 esp 的内存中
5 调用 函数 g
6 离开
g:
1 形成堆栈
2 传递参数 8 给 eax
3 eax + 3
4 返回 ebp 的值

分析计算机工作过程

c 代码 通过 gcc 编译 形成 响应的 汇编代码。 汇编代码中完成 寄存器 和 访存 操作, 并在 CPU 中执行相应指令。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions