- 先研究全局量
编译器vs2015 , x86 , release编译。
#include "stdafx.h"
int x = 100;
const int y = 0xff;
char * str1 = "str1 globle";
const char* str2 = "str2 globle";
int a = x;
int main()
{
int z = x + y;
int m = 0x55;
int n = 0x44;
int l = m + n;
printf("%d", l);
printf("%d", z);
a = x*m + n*y + a;
printf(str1);
printf(str2);
return 0;
}
.text:00401000 ; int __cdecl main()
.text:00401000 _main proc near ; CODE XREF: __scrt_common_main_seh+F4p
.text:00401000 push 99h
.text:00401005 push offset _Format ; "%d"
.text:0040100A call _printf
.text:0040100F push 163h
.text:00401014 push offset _Format ; "%d"
.text:00401019 call _printf
.text:0040101E add ?a@@3HA, 64F0h ; int a
.text:00401028 push offset aStr1Globle ; "str1 globle"
.text:0040102D call _printf
.text:00401032 push offset aStr2Globle ; "str2 globle"
.text:00401037 call _printf
.text:0040103C add esp, 18h
.text:0040103F xor eax, eax
.text:00401041 retn
.text:00401041 _main endp
全局变量位于数据节
.data:00403018 ?a@@3HA dd 64h ; DATA XREF: _main+1Ew
全局常量位于只读数据节
.rdata:004020F8 ; char aStr1Globle[]
.rdata:004020F8 aStr1Globle db 'str1 globle',0 ; DATA XREF: _main+28o
.rdata:004020F8 ; .rdata:char * str1o
.rdata:00402104 ; char aStr2Globle[]
.rdata:00402104 aStr2Globle db 'str2 globle',0 ; DATA XREF: _main+32o
.rdata:00402104 ; .rdata:char const * const str2o
.rdata:00402110 ; char Format[]
.rdata:00402110 _Format db '%d',0 ; DATA XREF: _main+5o
其他常量或者变量之所以没有显示出来,是因为编译器直接将它们优化掉了。