How the Computer Works (based on X86/Linux)

本文通过一个简单的C语言程序,逐步展示了如何将其转换为32位Linux汇编代码,并详细解析了函数调用过程中的堆栈变化及寄存器使用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注明

李振业
原创作品转载请注明出处
《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

一、实验过程

首先创建一个C语言文件

vi main.c

接着编写一段C语言代码

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

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

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

结束保存并退出

shift+:
wq

利用gcc的功能,将上面的代码编译成32位的汇编代码

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

这样就将main.c编译成main.s文件,如下图





bwlq分别代表8位、16位、32位和64位,而这里的指令是以l结尾,说明该文件内容的确是32位的汇编代码。
将所有以点开头的内容删除,留下来的就是纯汇编代码。


二、汇编分析

分析前先顺便明白以下知识:
1. EIP:Instruction Pointer 是如同指针一样指向内存的某一块区域,E开头则是因为为32位的系统
2. 堆栈是计算机中非常基础性的东西
3. CPU在实际取指令时根据CS:eip来准确定位一个指令
4. 寄存器模式,以%开头的寄存器标示符
5. 立即数是以$开头的寄存器标志符
6. 直接寻址:直接访问一个指定的内存地址的数据
7. 间接寻址:将寄存器的值作为一个内存地址来访问内存
8. 变址寻址:在间接寻址的时候改变寄存器的数值
9. Linux内核使用的是AT&T汇编格式
10.eip寄存器不能被直接修改,只能通过特殊指令间接修改
11.函数调用堆栈是由逻辑上多个堆栈叠加起来的
12.函数的返回值默认使用eax寄存器储存返回给上一级函数

设执行前的栈如图所示,esp与ebp均为0

 

 


从代码里的行数分析:

18-19:(main开始) 入栈 一 = ebp0,esp下移0→1,ebp下移0→1

20:esp下移1→2

21:二 = 6

22:三 = eip(23),esp下移2→3,eip跳转到f(第8行)

9-10:入栈 四 = ebp1,esp下移3→4,ebp下移1→4

11:esp下移4→5

12:变址寻址,eax = 6

13:五=6

14:六=eip(15),esp下移5→6,eip跳转到g(第1行)

2-3:入栈 七=ebp4,esp下移6→7,ebp下移4→7

4:变址寻址,eax=6

5:eax=eax+32=38

6:出栈 ebp上移7→4,esp上移7→6

7:esp上移6→5,eip(15)

15:执行leave,esp上移5→4,ebp上移4→1,esp再上移4→3

16:esp上移3→2,eip(23)

23:eax=eax+1=39

24:执行leave,esp上移2→1,ebp上移1→0,esp再上移1→0

25:ret,结束

可看出栈的执行情况如下,最后经过了从入栈到出栈的过程,eax的值为39

一 = ebp0

二 = 6

三 = eip(23)

四 = ebp1

五 = 6

六 = eip(15)

七 = ebp4

 


三、总结

没学过汇编和操作系统原理直接学Linux内核分析的确比较吃力,还好坚持下来和做完作业与测试。
希望能再接再厉,继续深入了解。

(venv) E:\pycharm\study\dorm_face_recognition\model_training>pip install dlib Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting dlib Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/f4/f8949b18ec1df2ef05fc2ea1d1dd82ff2d050b8704b7d0d088017315c221/dlib-20.0.0.tar.gz (3.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3/3.3 MB 6.6 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Building wheels for collected packages: dlib Building wheel for dlib (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for dlib (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [48 lines of output] running bdist_wheel running build running build_ext Traceback (most recent call last): File "E:\python3.9.13\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "E:\python3.9.13\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "E:\pycharm\study\venv\Scripts\cmake.exe\__main__.py", line 4, in <module> ModuleNotFoundError: No module named 'cmake' ================================================================================ ================================================================================ ================================================================================ CMake is not installed on your system! Or it is possible some broken copy of cmake is installed on your system. It is unfortunately very common for python package managers to include broken copies of cmake. So if the error above this refers to some file path to a cmake file inside a python or anaconda or miniconda path then you should delete that broken copy of cmake from your computer. Instead, please get an official copy of cmake from one of these known good sources of an official cmake: - cmake.org (this is how windows users should get cmake) - apt install cmake (for Ubuntu or Debian based systems) - yum install cmake (for Redhat or CenOS based systems) On a linux machine you can run `which cmake` to see what cmake you are actually using. If it tells you it's some cmake from any kind of python packager delete it and install an official cmake. More generally, cmake is not installed if when you open a terminal window and type cmake --version you get an error. So you can use that as a very basic test to see if you have cmake installed. That is, if cmake --version doesn't run from the same terminal window from which you are reading this error message, then you have not installed cmake. Windows users should take note that they need to tell the cmake installer to add cmake to their PATH. Since you can't run commands that are not in your PATH. This is how the PATH works on Linux as well, but failing to add cmake to the PATH is a particularly common problem on windows and rarely a problem on Linux. ================================================================================ ================================================================================ ================================================================================ [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for dlib Failed to build dlib ERROR: Could not build wheels for dlib, which is required to install pyproject.toml-based projects
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值