1
內部訪問參數I具有main.c
文件c - 接收功能
int boyut(const char* string);
char greeting[6] = {"Helle"};
int main(){
greeting[5] = 0x00;
int a = boyut(greeting);
return 0;
}
int boyut(const char* string){
int len=0;
while(string[len]){
len++;
}
return len;
}
我與GCC
命令gcc -Wall -m32 -nostdlib main.c -o main.o
當我檢查拆卸,我看到變量greeting
置於.data
段編譯它。在致電boyut
之前,它沒有被推入堆棧。在boyut
函數內部,它的作用就像變量greeting
處於堆棧段。所以這個變量實際上不在函數內部被訪問。爲什麼它會生成這樣的代碼?我該如何解決這個問題?
Disassembly of section .text:
080480f8 <main>:
80480f8: 55 push ebp
80480f9: 89 e5 mov ebp,esp
80480fb: 83 ec 18 sub esp,0x18
80480fe: c6 05 05 a0 04 08 00 mov BYTE PTR ds:0x804a005,0x0
8048105: 83 ec 0c sub esp,0xc
8048108: 68 00 a0 04 08 push 0x804a000
804810d: e8 0d 00 00 00 call 804811f <boyut>
8048112: 83 c4 10 add esp,0x10
8048115: 89 45 f4 mov DWORD PTR [ebp-0xc],eax
8048118: b8 00 00 00 00 mov eax,0x0
804811d: c9 leave
804811e: c3 ret
0804811f <boyut>:
804811f: 55 push ebp
8048120: 89 e5 mov ebp,esp
8048122: 83 ec 10 sub esp,0x10
8048125: c7 45 fc 00 00 00 00 mov DWORD PTR [ebp-0x4],0x0
804812c: eb 04 jmp 8048132 <boyut+0x13>
804812e: 83 45 fc 01 add DWORD PTR [ebp-0x4],0x1
8048132: 8b 55 fc mov edx,DWORD PTR [ebp-0x4]
8048135: 8b 45 08 mov eax,DWORD PTR [ebp+0x8]
8048138: 01 d0 add eax,edx
804813a: 0f b6 00 movzx eax,BYTE PTR [eax]
804813d: 84 c0 test al,al
804813f: 75 ed jne 804812e <boyut+0xf>
8048141: 8b 45 fc mov eax,DWORD PTR [ebp-0x4]
8048144: c9 leave
8048145: c3 ret
main.o: file format elf32-i386
Contents of section .data:
804a000 48656c6c 6500 Helle.
用'gcc -Wall -m32 -O1 -fverbose-asm -S main.c'編譯,然後查看生成的'main.s'彙編文件 –
並研究系統的ABI規範 –
'push 0x804a000' < =這似乎在推送「問候」的地址。當然,如果你檢查'.o'文件,你不應該忘記你仍然需要鏈接(從而符號重定位)。我想你應該看到一個佔位符'推$ 0x0'。 – dhke