数组和指针的区别
作者:互联网
// test.c
void test()
{
char array[4] = {0};
*(array + 0) = 0x00;
array[1] = 0x11;
*(array + 2) = 0x22;
array[3] = 0x33;
char *pointer = array;
pointer[0] = 0x00;
pointer[1] = 0x11;
pointer[2] = 0x22;
pointer[3] = 0x33;
}
// Makefile
test.o: test.c
gcc -c -O0 -m32 -masm=intel -std=c99 -ggdb3 -fno-stack-protector -fcf-protection=none -mmanual-endbr -fno-PIE -o test.o test.c
dump32: test.o
objdump -Mintel -Mi386 -d test.o
clean:
rm test.o
test.o: file format elf32-i386
Disassembly of section .text:
00000000 <test>:
0: 55 push ebp
1: 89 e5 mov ebp,esp
3: 83 ec 10 sub esp,0x10
6: c7 45 f8 00 00 00 00 mov DWORD PTR [ebp-0x8],0x0
d: c6 45 f8 00 mov BYTE PTR [ebp-0x8],0x0
11: c6 45 f9 11 mov BYTE PTR [ebp-0x7],0x11
15: c6 45 fa 22 mov BYTE PTR [ebp-0x6],0x22
19: c6 45 fb 33 mov BYTE PTR [ebp-0x5],0x33
1d: 8d 45 f8 lea eax,[ebp-0x8]
20: 89 45 fc mov DWORD PTR [ebp-0x4],eax
23: 8b 45 fc mov eax,DWORD PTR [ebp-0x4]
26: c6 00 00 mov BYTE PTR [eax],0x0
29: 8b 45 fc mov eax,DWORD PTR [ebp-0x4]
2c: 83 c0 01 add eax,0x1
2f: c6 00 11 mov BYTE PTR [eax],0x11
32: 8b 45 fc mov eax,DWORD PTR [ebp-0x4]
35: 83 c0 02 add eax,0x2
38: c6 00 22 mov BYTE PTR [eax],0x22
3b: 8b 45 fc mov eax,DWORD PTR [ebp-0x4]
3e: 83 c0 03 add eax,0x3
41: c6 00 33 mov BYTE PTR [eax],0x33
44: 90 nop
45: c9 leave
46: c3 ret
以上展示了最简单的以数字下标的方式向“数组”和“指针指向的内容”存数据。
可以看出:
array
在内存中的位置是ebp - 0x08
至ebp - 0x05
四个字节,array
的存取,直接以ebp - 0x08
为基准。
pointer
在内存中的位置是DWORD PTR ebp - 0x04
,其中存储着array
的地址,通过pointer
存取array
时,需要进行“解地址”计算。
在c语言中,array
和pointer
用起来几乎一样。
但如果通过inline assembly
将array
和pointer
传入,那么array
就是[ebp - 0x08]
,直接代表了第0个元素,访问第N个元素需要:
lea eax, %[array]
add eax, N
mov [eax], 0xabcd
pointer
就是[ebp - 0x04]
,其中存储着array
的地址,访问第N个元素:
mov eax, %[pointer]
add eax, N
mov [eax], 0xabcd
标签:区别,45,mov,eax,ebp,数组,array,PTR,指针 来源: https://www.cnblogs.com/wotent/p/15914344.html