rsp rbp 寄存器用途
·
概念
在最新的 x86_x64 架构中,通常用 rbp、rsp 这两个寄存器来保存进程栈的状态(需要硬件支持)。
- 其中 rbp 保存的是栈中当前执行函数的基本地址,当前执行函数所有存储在栈上的数据都要靠 rbp 指针加上偏移量来读取。
- 而 rsp 就是常说的栈指针,它永远指向一个进程的栈顶。
实例
hello.c
#include <stdio.h>
int add(int a, int b)
{
int c;
c = a + b;
return c;
}
int main(int argc, char *argv[])
{
int sum;
sum = add(3, 5);
printf("sum = %d\n", sum);
return 0;
}
编译
gcc -g hello.c -o hello.out
gdb 调试
$ gdb hello.out
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from hello.out...
(gdb) info reg rsp rbp
The program has no registers now.
(gdb) start
Temporary breakpoint 1 at 0x1167: file hello.c, line 13.
Starting program: /home/liyongjun/project/c/C_study/asm/hello3/hello.out
Temporary breakpoint 1, main (argc=21845, argv=0x0) at hello.c:13
13 {
(gdb) info reg rsp rbp
rsp 0x7fffffffdcd8 0x7fffffffdcd8
rbp 0x0 0x0
(gdb) list
8
9 return c;
10 }
11
12 int main(int argc, char *argv[])
13 {
14 int sum;
15
16 sum = add(3, 5);
17 printf("sum = %d\n", sum);
(gdb) ni
0x000055555555516b 13 {
(gdb)
0x000055555555516c 13 {
(gdb)
0x000055555555516f 13 {
(gdb)
0x0000555555555173 13 {
(gdb)
0x0000555555555176 13 {
(gdb)
16 sum = add(3, 5);
(gdb)
0x000055555555517f 16 sum = add(3, 5);
(gdb)
0x0000555555555184 16 sum = add(3, 5);
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/liyongjun/project/c/C_study/asm/hello3/hello.out
sum = 8
[Inferior 1 (process 27787) exited normally]
(gdb) info b
No breakpoints or watchpoints.
(gdb) b *main *add
Argument to arithmetic operation not a number or boolean.
(gdb) b *main
Breakpoint 2 at 0x555555555167: file hello.c, line 13.
(gdb) b *add
Breakpoint 3 at 0x555555555149: file hello.c, line 4.
(gdb) info reg rsp rbp
The program has no registers now.
(gdb) run
Starting program: /home/liyongjun/project/c/C_study/asm/hello3/hello.out
Breakpoint 2, main (argc=21845, argv=0x0) at hello.c:13
13 {
(gdb) info reg rsp rbp
rsp 0x7fffffffdcd8 0x7fffffffdcd8
rbp 0x0 0x0
(gdb) info reg
rax 0x555555555167 93824992235879
rbx 0x5555555551b0 93824992235952
rcx 0x5555555551b0 93824992235952
rdx 0x7fffffffddd8 140737488346584
rsi 0x7fffffffddc8 140737488346568
rdi 0x1 1
rbp 0x0 0x0
rsp 0x7fffffffdcd8 0x7fffffffdcd8
r8 0x0 0
r9 0x7ffff7fe0d60 140737354009952
r10 0x7ffff7ffcf68 140737354125160
r11 0x206 518
r12 0x555555555060 93824992235616
r13 0x7fffffffddc0 140737488346560
r14 0x0 0
r15 0x0 0
rip 0x555555555167 0x555555555167 <main>
eflags 0x246 [ PF ZF IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
(gdb) set disassemble-next-line on
(gdb) ni
0x000055555555516b 13 {
0x0000555555555167 <main+0>: f3 0f 1e fa endbr64
=> 0x000055555555516b <main+4>: 55 push rbp
0x000055555555516c <main+5>: 48 89 e5 mov rbp,rsp
0x000055555555516f <main+8>: 48 83 ec 20 sub rsp,0x20
0x0000555555555173 <main+12>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
0x0000555555555176 <main+15>: 48 89 75 e0 mov QWORD PTR [rbp-0x20],rsi
(gdb)
0x000055555555516c 13 {
0x0000555555555167 <main+0>: f3 0f 1e fa endbr64
0x000055555555516b <main+4>: 55 push rbp
=> 0x000055555555516c <main+5>: 48 89 e5 mov rbp,rsp
0x000055555555516f <main+8>: 48 83 ec 20 sub rsp,0x20
0x0000555555555173 <main+12>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
0x0000555555555176 <main+15>: 48 89 75 e0 mov QWORD PTR [rbp-0x20],rsi
(gdb) info reg rsp rbp
rsp 0x7fffffffdcd0 0x7fffffffdcd0
rbp 0x0 0x0
(gdb) x /8xh 0x7fffffffdcd8
0x7fffffffdcd8: 0xf083 0xf7dd 0x7fff 0x0000 0x0060 0x0000 0x0001 0x0000
(gdb) x /8xb 0x7fffffffdcd8
0x7fffffffdcd8: 0x83 0xf0 0xdd 0xf7 0xff 0x7f 0x00 0x00
(gdb)
0x7fffffffdce0: 0x60 0x00 0x00 0x00 0x01 0x00 0x00 0x00
(gdb) x /8xb 0x7fffffffdcd8
0x7fffffffdcd8: 0x83 0xf0 0xdd 0xf7 0xff 0x7f 0x00 0x00
(gdb) x /8xb *0x7fffffffdcd8
0xfffffffff7ddf083: Cannot access memory at address 0xfffffffff7ddf083
(gdb) x /8xb 0x7fffffffdcd8
0x7fffffffdcd8: 0x83 0xf0 0xdd 0xf7 0xff 0x7f 0x00 0x00
(gdb) x /8xb 0x7fffffffdcd0
0x7fffffffdcd0: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
(gdb) ni
0x000055555555516f 13 {
0x0000555555555167 <main+0>: f3 0f 1e fa endbr64
0x000055555555516b <main+4>: 55 push rbp
0x000055555555516c <main+5>: 48 89 e5 mov rbp,rsp
=> 0x000055555555516f <main+8>: 48 83 ec 20 sub rsp,0x20
0x0000555555555173 <main+12>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
0x0000555555555176 <main+15>: 48 89 75 e0 mov QWORD PTR [rbp-0x20],rsi
(gdb) info reg rsp rbp
rsp 0x7fffffffdcd0 0x7fffffffdcd0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
(gdb) info reg rsp rbp
rsp 0x7fffffffdcd0 0x7fffffffdcd0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
(gdb) ni
0x0000555555555173 13 {
0x0000555555555167 <main+0>: f3 0f 1e fa endbr64
0x000055555555516b <main+4>: 55 push rbp
0x000055555555516c <main+5>: 48 89 e5 mov rbp,rsp
0x000055555555516f <main+8>: 48 83 ec 20 sub rsp,0x20
=> 0x0000555555555173 <main+12>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
0x0000555555555176 <main+15>: 48 89 75 e0 mov QWORD PTR [rbp-0x20],rsi
(gdb) info reg rsp rbp
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
(gdb) info reg
rax 0x555555555167 93824992235879
rbx 0x5555555551b0 93824992235952
rcx 0x5555555551b0 93824992235952
rdx 0x7fffffffddd8 140737488346584
rsi 0x7fffffffddc8 140737488346568
rdi 0x1 1
rbp 0x7fffffffdcd0 0x7fffffffdcd0
rsp 0x7fffffffdcb0 0x7fffffffdcb0
r8 0x0 0
r9 0x7ffff7fe0d60 140737354009952
r10 0x7ffff7ffcf68 140737354125160
r11 0x206 518
r12 0x555555555060 93824992235616
r13 0x7fffffffddc0 140737488346560
r14 0x0 0
r15 0x0 0
rip 0x555555555173 0x555555555173 <main+12>
eflags 0x202 [ IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
(gdb) info reg rsp rbp edi rsi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x1 1
rsi 0x7fffffffddc8 140737488346568
(gdb) ni
0x0000555555555176 13 {
0x0000555555555167 <main+0>: f3 0f 1e fa endbr64
0x000055555555516b <main+4>: 55 push rbp
0x000055555555516c <main+5>: 48 89 e5 mov rbp,rsp
0x000055555555516f <main+8>: 48 83 ec 20 sub rsp,0x20
0x0000555555555173 <main+12>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
=> 0x0000555555555176 <main+15>: 48 89 75 e0 mov QWORD PTR [rbp-0x20],rsi
(gdb) info reg rsp rbp edi rsi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x1 1
rsi 0x7fffffffddc8 140737488346568
(gdb) x /8xb 0x7fffffffdcb4
0x7fffffffdcb4: 0x00 0x00 0x00 0x00 0x60 0x50 0x55 0x55
(gdb) x /8xb 0x7fffffffdcbc
0x7fffffffdcbc: 0x01 0x00 0x00 0x00 0xc0 0xdd 0xff 0xff
(gdb) x /4xb 0x7fffffffdcbc
0x7fffffffdcbc: 0x01 0x00 0x00 0x00
(gdb) x /4xb 0x7fffffffdcb0
0x7fffffffdcb0: 0x00 0x00 0x00 0x00
(gdb) x /8xb 0x7fffffffdcb0
0x7fffffffdcb0: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
(gdb) info reg rsp rbp edi rsi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x1 1
rsi 0x7fffffffddc8 140737488346568
(gdb) ni
16 sum = add(3, 5);
=> 0x000055555555517a <main+19>: be 05 00 00 00 mov esi,0x5
0x000055555555517f <main+24>: bf 03 00 00 00 mov edi,0x3
0x0000555555555184 <main+29>: e8 c0 ff ff ff call 0x555555555149 <add>
0x0000555555555189 <main+34>: 89 45 fc mov DWORD PTR [rbp-0x4],eax
(gdb) info reg rsp rbp edi rsi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x1 1
rsi 0x7fffffffddc8 140737488346568
(gdb) x /8xb 0x7fffffffdcb0
0x7fffffffdcb0: 0xc8 0xdd 0xff 0xff 0xff 0x7f 0x00 0x00
(gdb) info reg rsp rbp edi esi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x1 1
esi 0xffffddc8 -8760
(gdb) ni
0x000055555555517f 16 sum = add(3, 5);
0x000055555555517a <main+19>: be 05 00 00 00 mov esi,0x5
=> 0x000055555555517f <main+24>: bf 03 00 00 00 mov edi,0x3
0x0000555555555184 <main+29>: e8 c0 ff ff ff call 0x555555555149 <add>
0x0000555555555189 <main+34>: 89 45 fc mov DWORD PTR [rbp-0x4],eax
(gdb) ni
0x0000555555555184 16 sum = add(3, 5);
0x000055555555517a <main+19>: be 05 00 00 00 mov esi,0x5
0x000055555555517f <main+24>: bf 03 00 00 00 mov edi,0x3
=> 0x0000555555555184 <main+29>: e8 c0 ff ff ff call 0x555555555149 <add>
0x0000555555555189 <main+34>: 89 45 fc mov DWORD PTR [rbp-0x4],eax
(gdb) info reg rsp rbp edi esi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x5 5
(gdb) ni
Breakpoint 3, add (a=32767, b=-7863) at hello.c:4
4 {
=> 0x0000555555555149 <add+0>: f3 0f 1e fa endbr64
0x000055555555514d <add+4>: 55 push rbp
0x000055555555514e <add+5>: 48 89 e5 mov rbp,rsp
0x0000555555555151 <add+8>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
0x0000555555555154 <add+11>: 89 75 e8 mov DWORD PTR [rbp-0x18],esi
(gdb) info reg rsp rbp edi esi
rsp 0x7fffffffdca8 0x7fffffffdca8
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x5 5
(gdb) x /8xb 0x7fffffffdca8
0x7fffffffdca8: 0x89 0x51 0x55 0x55 0x55 0x55 0x00 0x00
(gdb) x /8xh 0x7fffffffdca8
0x7fffffffdca8: 0x5189 0x5555 0x5555 0x0000 0xddc8 0xffff 0x7fff 0x0000
(gdb) x /8xg 0x7fffffffdca8
0x7fffffffdca8: 0x0000555555555189 0x00007fffffffddc8
0x7fffffffdcb8: 0x0000000155555060 0x00007fffffffddc0
0x7fffffffdcc8: 0x0000000000000000 0x0000000000000000
0x7fffffffdcd8: 0x00007ffff7ddf083 0x0000000100000060
(gdb) x /1xg 0x7fffffffdca8
0x7fffffffdca8: 0x0000555555555189
(gdb) x /8xg 0x7fffffffdcd0
0x7fffffffdcd0: 0x0000000000000000 0x00007ffff7ddf083
0x7fffffffdce0: 0x0000000100000060 0x00007fffffffddc8
0x7fffffffdcf0: 0x00000001f7fa37a0 0x0000555555555167
0x7fffffffdd00: 0x00005555555551b0 0x54caaf15e847e821
(gdb) x /1xg 0x7fffffffdcd0
0x7fffffffdcd0: 0x0000000000000000
(gdb) info reg rsp rbp edi esi
rsp 0x7fffffffdca8 0x7fffffffdca8
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x5 5
(gdb) ni
0x000055555555514d 4 {
0x0000555555555149 <add+0>: f3 0f 1e fa endbr64
=> 0x000055555555514d <add+4>: 55 push rbp
0x000055555555514e <add+5>: 48 89 e5 mov rbp,rsp
0x0000555555555151 <add+8>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
0x0000555555555154 <add+11>: 89 75 e8 mov DWORD PTR [rbp-0x18],esi
(gdb) ni
0x000055555555514e 4 {
0x0000555555555149 <add+0>: f3 0f 1e fa endbr64
0x000055555555514d <add+4>: 55 push rbp
=> 0x000055555555514e <add+5>: 48 89 e5 mov rbp,rsp
0x0000555555555151 <add+8>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
0x0000555555555154 <add+11>: 89 75 e8 mov DWORD PTR [rbp-0x18],esi
(gdb) info reg rsp rbp edi esi
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x5 5
(gdb) ni
0x0000555555555151 4 {
0x0000555555555149 <add+0>: f3 0f 1e fa endbr64
0x000055555555514d <add+4>: 55 push rbp
0x000055555555514e <add+5>: 48 89 e5 mov rbp,rsp
=> 0x0000555555555151 <add+8>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
0x0000555555555154 <add+11>: 89 75 e8 mov DWORD PTR [rbp-0x18],esi
(gdb) info reg rsp rbp edi esi
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
(gdb) disassemble add
Dump of assembler code for function add:
0x0000555555555149 <+0>: endbr64
0x000055555555514d <+4>: push rbp
0x000055555555514e <+5>: mov rbp,rsp
=> 0x0000555555555151 <+8>: mov DWORD PTR [rbp-0x14],edi
0x0000555555555154 <+11>: mov DWORD PTR [rbp-0x18],esi
0x0000555555555157 <+14>: mov edx,DWORD PTR [rbp-0x14]
0x000055555555515a <+17>: mov eax,DWORD PTR [rbp-0x18]
0x000055555555515d <+20>: add eax,edx
0x000055555555515f <+22>: mov DWORD PTR [rbp-0x4],eax
0x0000555555555162 <+25>: mov eax,DWORD PTR [rbp-0x4]
0x0000555555555165 <+28>: pop rbp
0x0000555555555166 <+29>: ret
End of assembler dump.
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
eax 0x55555167 1431654759
edx 0xffffddd8 -8744
(gdb) ni
0x0000555555555154 4 {
0x0000555555555149 <add+0>: f3 0f 1e fa endbr64
0x000055555555514d <add+4>: 55 push rbp
0x000055555555514e <add+5>: 48 89 e5 mov rbp,rsp
0x0000555555555151 <add+8>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
=> 0x0000555555555154 <add+11>: 89 75 e8 mov DWORD PTR [rbp-0x18],esi
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
eax 0x55555167 1431654759
edx 0xffffddd8 -8744
(gdb) ni
7 c = a + b;
=> 0x0000555555555157 <add+14>: 8b 55 ec mov edx,DWORD PTR [rbp-0x14]
0x000055555555515a <add+17>: 8b 45 e8 mov eax,DWORD PTR [rbp-0x18]
0x000055555555515d <add+20>: 01 d0 add eax,edx
0x000055555555515f <add+22>: 89 45 fc mov DWORD PTR [rbp-0x4],eax
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
eax 0x55555167 1431654759
edx 0xffffddd8 -8744
(gdb) ni
0x000055555555515a 7 c = a + b;
0x0000555555555157 <add+14>: 8b 55 ec mov edx,DWORD PTR [rbp-0x14]
=> 0x000055555555515a <add+17>: 8b 45 e8 mov eax,DWORD PTR [rbp-0x18]
0x000055555555515d <add+20>: 01 d0 add eax,edx
0x000055555555515f <add+22>: 89 45 fc mov DWORD PTR [rbp-0x4],eax
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
eax 0x55555167 1431654759
edx 0x3 3
(gdb) ni
0x000055555555515d 7 c = a + b;
0x0000555555555157 <add+14>: 8b 55 ec mov edx,DWORD PTR [rbp-0x14]
0x000055555555515a <add+17>: 8b 45 e8 mov eax,DWORD PTR [rbp-0x18]
=> 0x000055555555515d <add+20>: 01 d0 add eax,edx
0x000055555555515f <add+22>: 89 45 fc mov DWORD PTR [rbp-0x4],eax
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
eax 0x5 5
edx 0x3 3
(gdb) ni
0x000055555555515f 7 c = a + b;
0x0000555555555157 <add+14>: 8b 55 ec mov edx,DWORD PTR [rbp-0x14]
0x000055555555515a <add+17>: 8b 45 e8 mov eax,DWORD PTR [rbp-0x18]
0x000055555555515d <add+20>: 01 d0 add eax,edx
=> 0x000055555555515f <add+22>: 89 45 fc mov DWORD PTR [rbp-0x4],eax
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
eax 0x8 8
edx 0x3 3
(gdb) disassemble add
Dump of assembler code for function add:
0x0000555555555149 <+0>: endbr64
0x000055555555514d <+4>: push rbp
0x000055555555514e <+5>: mov rbp,rsp
0x0000555555555151 <+8>: mov DWORD PTR [rbp-0x14],edi
0x0000555555555154 <+11>: mov DWORD PTR [rbp-0x18],esi
0x0000555555555157 <+14>: mov edx,DWORD PTR [rbp-0x14]
0x000055555555515a <+17>: mov eax,DWORD PTR [rbp-0x18]
0x000055555555515d <+20>: add eax,edx
=> 0x000055555555515f <+22>: mov DWORD PTR [rbp-0x4],eax
0x0000555555555162 <+25>: mov eax,DWORD PTR [rbp-0x4]
0x0000555555555165 <+28>: pop rbp
0x0000555555555166 <+29>: ret
End of assembler dump.
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
eax 0x8 8
edx 0x3 3
(gdb) x /1xw 0x7fffffffdc9c
0x7fffffffdc9c: 0x00005555
(gdb) ni
9 return c;
=> 0x0000555555555162 <add+25>: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
(gdb) x /1xw 0x7fffffffdc9c
0x7fffffffdc9c: 0x00000008
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
eax 0x8 8
edx 0x3 3
(gdb) ni
10 }
=> 0x0000555555555165 <add+28>: 5d pop rbp
0x0000555555555166 <add+29>: c3 ret
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca0 0x7fffffffdca0
rbp 0x7fffffffdca0 0x7fffffffdca0
edi 0x3 3
esi 0x5 5
eax 0x8 8
edx 0x3 3
(gdb) ni
0x0000555555555166 10 }
0x0000555555555165 <add+28>: 5d pop rbp
=> 0x0000555555555166 <add+29>: c3 ret
(gdb) info reg rsp rbp edi esi eax edx
rsp 0x7fffffffdca8 0x7fffffffdca8
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x5 5
eax 0x8 8
edx 0x3 3
(gdb) info reg rsp rbp edi esi eax edx cs rip
rsp 0x7fffffffdca8 0x7fffffffdca8
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x5 5
eax 0x8 8
edx 0x3 3
cs 0x33 51
rip 0x555555555166 0x555555555166 <add+29>
(gdb) ni
0x0000555555555189 in main (argc=1, argv=0x7fffffffddc8) at hello.c:16
16 sum = add(3, 5);
0x000055555555517a <main+19>: be 05 00 00 00 mov esi,0x5
0x000055555555517f <main+24>: bf 03 00 00 00 mov edi,0x3
0x0000555555555184 <main+29>: e8 c0 ff ff ff call 0x555555555149 <add>
=> 0x0000555555555189 <main+34>: 89 45 fc mov DWORD PTR [rbp-0x4],eax
(gdb) info reg rsp rbp edi esi eax edx cs rip
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x5 5
eax 0x8 8
edx 0x3 3
cs 0x33 51
rip 0x555555555189 0x555555555189 <main+34>
(gdb) disassemble main
Dump of assembler code for function main:
0x0000555555555167 <+0>: endbr64
0x000055555555516b <+4>: push rbp
0x000055555555516c <+5>: mov rbp,rsp
0x000055555555516f <+8>: sub rsp,0x20
0x0000555555555173 <+12>: mov DWORD PTR [rbp-0x14],edi
0x0000555555555176 <+15>: mov QWORD PTR [rbp-0x20],rsi
0x000055555555517a <+19>: mov esi,0x5
0x000055555555517f <+24>: mov edi,0x3
0x0000555555555184 <+29>: call 0x555555555149 <add>
=> 0x0000555555555189 <+34>: mov DWORD PTR [rbp-0x4],eax
0x000055555555518c <+37>: mov eax,DWORD PTR [rbp-0x4]
0x000055555555518f <+40>: mov esi,eax
0x0000555555555191 <+42>: lea rdi,[rip+0xe6c] # 0x555555556004
0x0000555555555198 <+49>: mov eax,0x0
0x000055555555519d <+54>: call 0x555555555050 <printf@plt>
0x00005555555551a2 <+59>: mov eax,0x0
0x00005555555551a7 <+64>: leave
0x00005555555551a8 <+65>: ret
End of assembler dump.
(gdb) info reg rsp rbp edi esi eax edx cs rip
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x5 5
eax 0x8 8
edx 0x3 3
cs 0x33 51
rip 0x555555555189 0x555555555189 <main+34>
(gdb) x /1xw 0x7fffffffdccc
0x7fffffffdccc: 0x00000000
(gdb) ni
17 printf("sum = %d\n", sum);
=> 0x000055555555518c <main+37>: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
0x000055555555518f <main+40>: 89 c6 mov esi,eax
0x0000555555555191 <main+42>: 48 8d 3d 6c 0e 00 00 lea rdi,[rip+0xe6c] # 0x555555556004
0x0000555555555198 <main+49>: b8 00 00 00 00 mov eax,0x0
0x000055555555519d <main+54>: e8 ae fe ff ff call 0x555555555050 <printf@plt>
(gdb) x /1xw 0x7fffffffdccc
0x7fffffffdccc: 0x00000008
(gdb) ni
0x000055555555518f 17 printf("sum = %d\n", sum);
0x000055555555518c <main+37>: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
=> 0x000055555555518f <main+40>: 89 c6 mov esi,eax
0x0000555555555191 <main+42>: 48 8d 3d 6c 0e 00 00 lea rdi,[rip+0xe6c] # 0x555555556004
0x0000555555555198 <main+49>: b8 00 00 00 00 mov eax,0x0
0x000055555555519d <main+54>: e8 ae fe ff ff call 0x555555555050 <printf@plt>
(gdb) x /1xw 0x7fffffffdccc
0x7fffffffdccc: 0x00000008
(gdb) info reg rsp rbp edi esi eax edx cs rip esi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x5 5
eax 0x8 8
edx 0x3 3
cs 0x33 51
rip 0x55555555518f 0x55555555518f <main+40>
esi 0x5 5
(gdb) ni
0x0000555555555191 17 printf("sum = %d\n", sum);
0x000055555555518c <main+37>: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
0x000055555555518f <main+40>: 89 c6 mov esi,eax
=> 0x0000555555555191 <main+42>: 48 8d 3d 6c 0e 00 00 lea rdi,[rip+0xe6c] # 0x555555556004
0x0000555555555198 <main+49>: b8 00 00 00 00 mov eax,0x0
0x000055555555519d <main+54>: e8 ae fe ff ff call 0x555555555050 <printf@plt>
(gdb) info reg rsp rbp edi esi eax edx cs rip esi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x8 8
eax 0x8 8
edx 0x3 3
cs 0x33 51
rip 0x555555555191 0x555555555191 <main+42>
esi 0x8 8
(gdb) info reg rsp rbp edi esi eax edx cs rip esi rdi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x3 3
esi 0x8 8
eax 0x8 8
edx 0x3 3
cs 0x33 51
rip 0x555555555191 0x555555555191 <main+42>
esi 0x8 8
rdi 0x3 3
(gdb) disassemble main
Dump of assembler code for function main:
0x0000555555555167 <+0>: endbr64
0x000055555555516b <+4>: push rbp
0x000055555555516c <+5>: mov rbp,rsp
0x000055555555516f <+8>: sub rsp,0x20
0x0000555555555173 <+12>: mov DWORD PTR [rbp-0x14],edi
0x0000555555555176 <+15>: mov QWORD PTR [rbp-0x20],rsi
0x000055555555517a <+19>: mov esi,0x5
0x000055555555517f <+24>: mov edi,0x3
0x0000555555555184 <+29>: call 0x555555555149 <add>
0x0000555555555189 <+34>: mov DWORD PTR [rbp-0x4],eax
0x000055555555518c <+37>: mov eax,DWORD PTR [rbp-0x4]
0x000055555555518f <+40>: mov esi,eax
=> 0x0000555555555191 <+42>: lea rdi,[rip+0xe6c] # 0x555555556004
0x0000555555555198 <+49>: mov eax,0x0
0x000055555555519d <+54>: call 0x555555555050 <printf@plt>
0x00005555555551a2 <+59>: mov eax,0x0
0x00005555555551a7 <+64>: leave
0x00005555555551a8 <+65>: ret
End of assembler dump.
(gdb) ni
0x0000555555555198 17 printf("sum = %d\n", sum);
0x000055555555518c <main+37>: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
0x000055555555518f <main+40>: 89 c6 mov esi,eax
0x0000555555555191 <main+42>: 48 8d 3d 6c 0e 00 00 lea rdi,[rip+0xe6c] # 0x555555556004
=> 0x0000555555555198 <main+49>: b8 00 00 00 00 mov eax,0x0
0x000055555555519d <main+54>: e8 ae fe ff ff call 0x555555555050 <printf@plt>
(gdb) info reg rsp rbp edi esi eax edx cs rip esi rdi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0x55556004 1431658500
esi 0x8 8
eax 0x8 8
edx 0x3 3
cs 0x33 51
rip 0x555555555198 0x555555555198 <main+49>
esi 0x8 8
rdi 0x555555556004 93824992239620
(gdb) ni
0x000055555555519d 17 printf("sum = %d\n", sum);
0x000055555555518c <main+37>: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
0x000055555555518f <main+40>: 89 c6 mov esi,eax
0x0000555555555191 <main+42>: 48 8d 3d 6c 0e 00 00 lea rdi,[rip+0xe6c] # 0x555555556004
0x0000555555555198 <main+49>: b8 00 00 00 00 mov eax,0x0
=> 0x000055555555519d <main+54>: e8 ae fe ff ff call 0x555555555050 <printf@plt>
(gdb) ni
sum = 8
19 return 0;
=> 0x00005555555551a2 <main+59>: b8 00 00 00 00 mov eax,0x0
(gdb) disassemble main
Dump of assembler code for function main:
0x0000555555555167 <+0>: endbr64
0x000055555555516b <+4>: push rbp
0x000055555555516c <+5>: mov rbp,rsp
0x000055555555516f <+8>: sub rsp,0x20
0x0000555555555173 <+12>: mov DWORD PTR [rbp-0x14],edi
0x0000555555555176 <+15>: mov QWORD PTR [rbp-0x20],rsi
0x000055555555517a <+19>: mov esi,0x5
0x000055555555517f <+24>: mov edi,0x3
0x0000555555555184 <+29>: call 0x555555555149 <add>
0x0000555555555189 <+34>: mov DWORD PTR [rbp-0x4],eax
0x000055555555518c <+37>: mov eax,DWORD PTR [rbp-0x4]
0x000055555555518f <+40>: mov esi,eax
0x0000555555555191 <+42>: lea rdi,[rip+0xe6c] # 0x555555556004
0x0000555555555198 <+49>: mov eax,0x0
0x000055555555519d <+54>: call 0x555555555050 <printf@plt>
=> 0x00005555555551a2 <+59>: mov eax,0x0
0x00005555555551a7 <+64>: leave
0x00005555555551a8 <+65>: ret
End of assembler dump.
(gdb) info reg rsp rbp edi esi eax edx cs rip esi rdi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0xf7fa97e0 -134572064
esi 0x555592a0 1431671456
eax 0x8 8
edx 0x0 0
cs 0x33 51
rip 0x5555555551a2 0x5555555551a2 <main+59>
esi 0x555592a0 1431671456
rdi 0x7ffff7fa97e0 140737353783264
(gdb) ni
20 }
=> 0x00005555555551a7 <main+64>: c9 leave
0x00005555555551a8 <main+65>: c3 ret
(gdb) info reg rsp rbp edi esi eax edx cs rip esi rdi
rsp 0x7fffffffdcb0 0x7fffffffdcb0
rbp 0x7fffffffdcd0 0x7fffffffdcd0
edi 0xf7fa97e0 -134572064
esi 0x555592a0 1431671456
eax 0x0 0
edx 0x0 0
cs 0x33 51
rip 0x5555555551a7 0x5555555551a7 <main+64>
esi 0x555592a0 1431671456
rdi 0x7ffff7fa97e0 140737353783264
(gdb) ni
0x00005555555551a8 20 }
0x00005555555551a7 <main+64>: c9 leave
=> 0x00005555555551a8 <main+65>: c3 ret
(gdb) info reg rsp rbp edi esi eax edx cs rip esi rdi
rsp 0x7fffffffdcd8 0x7fffffffdcd8
rbp 0x0 0x0
edi 0xf7fa97e0 -134572064
esi 0x555592a0 1431671456
eax 0x0 0
edx 0x0 0
cs 0x33 51
rip 0x5555555551a8 0x5555555551a8 <main+65>
esi 0x555592a0 1431671456
rdi 0x7ffff7fa97e0 140737353783264
(gdb) info reg rsp rbp edi esi eax edx cs rip esi rdi
rsp 0x7fffffffdcd8 0x7fffffffdcd8
rbp 0x0 0x0
edi 0xf7fa97e0 -134572064
esi 0x555592a0 1431671456
eax 0x0 0
edx 0x0 0
cs 0x33 51
rip 0x5555555551a8 0x5555555551a8 <main+65>
esi 0x555592a0 1431671456
rdi 0x7ffff7fa97e0 140737353783264
(gdb) ni
__libc_start_main (main=0x555555555167 <main>, argc=1, argv=0x7fffffffddc8, init=<optimized out>, fini=<optimized out>,
rtld_fini=<optimized out>, stack_end=0x7fffffffddb8) at ../csu/libc-start.c:342
342 ../csu/libc-start.c: 没有那个文件或目录.
=> 0x00007ffff7ddf083 <__libc_start_main+243>: 89 c7 mov edi,eax
0x00007ffff7ddf085 <__libc_start_main+245>: e8 b6 29 02 00 call 0x7ffff7e01a40 <__GI_exit>
(gdb) info reg rsp rbp edi esi eax edx cs rip esi rdi
rsp 0x7fffffffdce0 0x7fffffffdce0
rbp 0x0 0x0
edi 0xf7fa97e0 -134572064
esi 0x555592a0 1431671456
eax 0x0 0
edx 0x0 0
cs 0x33 51
rip 0x7ffff7ddf083 0x7ffff7ddf083 <__libc_start_main+243>
esi 0x555592a0 1431671456
rdi 0x7ffff7fa97e0 140737353783264
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/liyongjun/project/c/C_study/asm/hello3/hello.out
Breakpoint 2, main (argc=21845, argv=0x0) at hello.c:13
13 {
=> 0x0000555555555167 <main+0>: f3 0f 1e fa endbr64
0x000055555555516b <main+4>: 55 push rbp
0x000055555555516c <main+5>: 48 89 e5 mov rbp,rsp
0x000055555555516f <main+8>: 48 83 ec 20 sub rsp,0x20
0x0000555555555173 <main+12>: 89 7d ec mov DWORD PTR [rbp-0x14],edi
0x0000555555555176 <main+15>: 48 89 75 e0 mov QWORD PTR [rbp-0x20],rsi
(gdb) info reg rsp rbp edi esi eax edx cs rip esi rdi
rsp 0x7fffffffdcd8 0x7fffffffdcd8
rbp 0x0 0x0
edi 0x1 1
esi 0xffffddc8 -8760
eax 0x55555167 1431654759
edx 0xffffddd8 -8744
cs 0x33 51
rip 0x555555555167 0x555555555167 <main>
esi 0xffffddc8 -8760
rdi 0x1 1
(gdb)
堆栈变化情况(蓝色字体为执行完左上方汇编指令后的变化)
更多推荐
所有评论(0)