作者: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