gpt4 book ai didi

c - 堆栈在 32 位和 64 位处理器中有何不同

转载 作者:太空宇宙 更新时间:2023-11-04 01:58:25 25 4
gpt4 key购买 nike

我在 gdb 中针对 32 位和 64 位英特尔处理器反汇编了以下代码。

void main() {
5 char *args[2];
6
7 args[0] = "/bin/sh";
8 args[1] = NULL;
9 execve(args[0],args,NULL);
10 exit(0);
11 }

生成的汇编代码如下。

对于 64 位

Dump of assembler code for function main:
0x000000000040105e <+0>: push %rbp
0x000000000040105f <+1>: mov %rsp,%rbp
0x0000000000401062 <+4>: sub $0x10,%rsp
0x0000000000401066 <+8>: movq $0x493564,-0x10(%rbp) ; <- this line
0x000000000040106e <+16>: movq $0x0,-0x8(%rbp)
0x0000000000401076 <+24>: mov -0x10(%rbp),%rax
0x000000000040107a <+28>: lea -0x10(%rbp),%rcx
0x000000000040107e <+32>: mov $0x0,%edx
0x0000000000401083 <+37>: mov %rcx,%rsi
0x0000000000401086 <+40>: mov %rax,%rdi
0x0000000000401089 <+43>: callq 0x433510 <execve>
0x000000000040108e <+48>: mov $0x0,%edi
0x0000000000401093 <+53>: callq 0x407560 <exit>

对于 32 位

Dump of assembler code for function main:
0x8000130 <main>: pushl %ebp
0x8000131 <main+1>: movl %esp,%ebp
0x8000133 <main+3>: subl $0x8,%esp
0x8000136 <main+6>: movl $0x80027b8,0xfffffff8(%ebp) ; <- this line
0x800013d <main+13>: movl $0x0,0xfffffffc(%ebp)
0x8000144 <main+20>: pushl $0x0
0x8000146 <main+22>: leal 0xfffffff8(%ebp),%eax
0x8000149 <main+25>: pushl %eax
0x800014a <main+26>: movl 0xfffffff8(%ebp),%eax
0x800014d <main+29>: pushl %eax
0x800014e <main+30>: call 0x80002bc <__execve>
0x8000153 <main+35>: addl $0xc,%esp
0x8000156 <main+38>: movl %ebp,%esp
0x8000158 <main+40>: popl %ebp
0x8000159 <main+41>: ret
End of assembler dump.

上面我标注的汇编代码是我没看懂的地方。 64 位系统声明从 rbp 偏移 16 位(我想学习的位、字节或字),而 32 位系统声明从 ebp 偏移 8 位。有人可以解释堆栈指针在 32 位和 64 位系统中如何前进的区别以及它是什么单位(位、字节或字)。

最佳答案

char * args[2];

是两个指针的数组。在 32 位机器上,指针(地址,但不一定)是 32 位宽,而在 64 位机器上,指针(不是地址)是 64 位宽。

这意味着,当前堆栈帧中堆栈上的区域必须是 2 * pointer_width,这使得 32 位机器为 8,而 16 用于 64 位机器。

在你的代码下面你可以看到

movl   $0x80027b8, -0x8(%ebp)
movl $0x0, -0x4(%ebp)

... 到指针的 32 位双字移动(一个字符串地址和一个 NULL)。

另一方面,64 位代码需要两个双字(8 字节)来存储地址。

movq   $0x0, -0x8(%rbp)
movq $0x493564, -0x10(%rbp)

关于c - 堆栈在 32 位和 64 位处理器中有何不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30546451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com