《Linux内核分析》mooc第一周笔记

本文通过一个简单的C语言程序,详细分析了其在Linux内核环境下编译成汇编代码后的运行机制,特别关注了堆栈的变化过程及CPU寄存器的状态。

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

作者:30erli
原创作品转载请注明出处 + 《Linux内核分析》MOOC课程

一 实验要求:

  • 题目自拟,内容围绕计算机是如何工作的进行;
  • 博客中需要使用实验截图;
  • 博客内容中需要仔细分析汇编代码的工作过程中堆栈的变化;
  • 总结部分需要阐明自己对“计算机是如何工作的”理解。

二 实验代码

int f(int x)
{
  return x + 3;
}
int main(void)
{
  return f(18) + 1;
}

三 实验过程

实验环境 VMware 10.0.2 build-1744117 14.04.1-Ubuntu gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.1)
编译代码
gcc -g main.c -o main -m32
使用gdb进行调试 gdb main

(gdb) start
Temporary breakpoint 1 at 0x80483fe: file main.c, line 8.
Starting program: /home/jackchen/桌面/vivi/mooc_mengning/main 

Temporary breakpoint 1, main () at main.c:8
8     return f(18) + 1;
(gdb) disassemble 
Dump of assembler code for function main:
   0x080483f8 <+0>: push   %ebp
            subl   $0x4,%esp
            movl   %ebp,(%esp)
   0x080483f9 <+1>: mov%esp,%ebp
   0x080483fb <+3>: sub$0x4,%esp
=> 0x080483fe <+6>: movl   $0x12,(%esp)
   0x08048405 <+13>:    call   0x80483ed <f>
            pushl  %eip
                subl    $0x4,%esp
                movl    %eip,(%esp)
            movl   $0x80483ed,%eip
   0x0804840a <+18>:    add$0x1,%eax
   0x0804840d <+21>:    leave  
            movl   %ebp,%esp
            popl   %ebp
                movl    (%esp),%ebp
                addl    $0x4,%esp
   0x0804840e <+22>:    ret
            popl   %eip
                movl    (%esp),%eip
                addl    $0x4,%esp
End of assembler dump.
(gdb) disassemble f
Dump of assembler code for function f:
   0x080483ed <+0>: push   %ebp
            subl   $0x4,%esp
            movl   %ebp,(%esp)          
   0x080483ee <+1>: mov%esp,%ebp
   0x080483f0 <+3>: mov0x8(%ebp),%eax
   0x080483f3 <+6>: add$0x3,%eax
   0x080483f6 <+9>: pop%ebp
            movl    (%esp),%ebp
            addl    $0x4,%esp
   0x080483f7 <+10>:    ret
            popl   %eip
                movl    (%esp),%eip
                addl    $0x4,%esp
End of assembler dump.

此时cpu寄存器的状态为:

(gdb) i r
eax0x1  1
ecx0xfe900e12   -24113646
edx0xffffcff4   -12300
ebx0xf7fbb000   -134500352
esp0xffffcfc4   0xffffcfc4
ebp0xffffcfc8   0xffffcfc8
esi0x0  0
edi0x0  0
eip0x80483fe    0x80483fe <main+6>
eflags 0x282    [ SF IF ]
cs 0x23 35
ss 0x2b 43
ds 0x2b 43
es 0x2b 43
fs 0x0  0
gs 0x63 99

728889-20160226182112677-861926866.jpg

转载于:https://www.cnblogs.com/jshdaxia/p/5221461.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值