汇编输出汉字我的计算机工作正常,用汇编来解释“计算机是怎么工作的”

本文详细分析了一段C代码如何通过gcc编译成32位汇编,并展示了在执行过程中堆栈的变化。从main函数开始,通过pushl, movl等指令处理参数和调用子函数,最终返回结果。文章揭示了计算机如何按照指令执行并利用寄存器辅助运算的过程。

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

本文章为《Linux内核分析》实验报告

梁永锐

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

-----

以下为要分析的c代码

int g(intx)

{return x + 36;

}int f(intx)

{return g(x) * 4;

}int main(void)

{return f(81) + 12;

}

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

编译成32位汇编后(去了点开头的行

g:pushl %ebp

movl %esp, %ebp

movl8(%ebp), %eax

addl $36, %eax

popl %ebpret

f:pushl %ebp

movl %esp, %ebp

pushl8(%ebp)callg

addl $4, %esp

sall $2, %eaxleave

ret

main:pushl %ebp

movl %esp, %ebp

pushl $81

callf

addl $4, %esp

addl $12, %eaxleave

ret

一.实验截图:

ae6968208e264839bae597468f00fd40.jpg

60ee840e1fbd423e9ab3ae554328b77e.jpg

8e06eef7f2134b5bb1f52d27b2b99445.jpg

二. 汇编代码的工作过程中堆栈的变化

从main开始

pushl %ebp

movl %esp, %ebp

也就是enter指令(不知道为什么这里并没有把他写成enter而是分开写了)

这两条指令执行后我们假设,ebp,esp均为100(以下内容用ebp=100,  esp=100这种方法表示)

pushl $81

ebp=100, esp = 96,[96]=81([96]=81, 表示位置96的地方存了81这个数, 以下均用这种方法表示)

call f

ebp = 100, esp = 92, [92]= 22(这里用行号做eip)

跳到了f

pushl %ebp

ebp = 100, esp = 88, [88]=100

movl %esp, %ebp

ebp = 88, esp = 88

pushl 8(%ebp)

ebp = 88, esp = 84, [84] = 81

call g

ebp = 88, esp = 80, [80] = 13

然后调到了g函数

pushl %ebp

ebp = 88, esp = 76, [76] = 88

movl %esp, %ebp

ebp = 76, esp = 76

movl 8(%ebp), %eax

把传入的参数放在eax中, eax = 81

addl $36, %eax

eax = 81 + 36 = 117

popl %ebp

ebp = 88, esp = 80

ret

ebp = 88, esp = 84, eip = 13

此时就跳到13行了

addl $4, %esp

弹出传入的参数,不赋值

ebp = 88, esp = 88

sall $2, %eax

编译器很智能,乘4,自动变成了左移两位, eax = 117 << 2 = 468

leave

leave就是

movl %ebp, %esp  : ebp = 88, esp = 88

popl %ebp : ebp = 100, esp = 92

ret

ebp = 100, esp = 94, eip = 22

跳到第22行

addl $4, %esp

弹出参数

esp = 96

addl $12, %eax

eax = 468 + 12 = 480

leave

ret

回到main函数之前的堆栈和eip

三.计算机是如何工作的

简单的说,计算机就是按照ip一条条的执行指令,执行的时候需要一些寄存器做辅助,ip有时候也会跳。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值