作者lungswu (宅爸爸)
看板C_and_CPP
標題Re: [問題] c請益
時間Thu Sep 6 12:31:04 2012
※ 引述《slind (你別對著螢幕傻笑,很蠢耶)》之銘言:
: void main(){
: int b[3];
: memset(b, 0, sizeof(b));
: char *a=(char*)b;
: int i;
: for(i=0; i<9; i++){
: a[i] = 0x5f;
: }
: printf("%x %x %x\n", b[0], b[1], b[2]);
: }
: 請問這段是怎麼運作的? 越詳細越好 感謝
我想它最詳細的運作方式是這樣的,再講下去要查Micro Instruction了
void main(){
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 48 83 ec 20 sub $0x20,%rsp
int b[3];
memset(b, 0, sizeof(b));
8: 48 8d 45 e0 lea -0x20(%rbp),%rax
c: ba 0c 00 00 00 mov $0xc,%edx
11: be 00 00 00 00 mov $0x0,%esi
16: 48 89 c7 mov %rax,%rdi
19: e8 00 00 00 00 callq 1e <main+0x1e>
char *a=(char*)b;
1e: 48 8d 45 e0 lea -0x20(%rbp),%rax
22: 48 89 45 f0 mov %rax,-0x10(%rbp)
19: e8 00 00 00 00 callq 1e <main+0x1e>
char *a=(char*)b;
1e: 48 8d 45 e0 lea -0x20(%rbp),%rax
22: 48 89 45 f0 mov %rax,-0x10(%rbp)
int i;
for(i=0; i<9; i++){
26: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp)
2d: eb 10 jmp 3f <main+0x3f>
a[i] = 0x5f;
2f: 8b 45 fc mov -0x4(%rbp),%eax
32: 48 98 cltq
34: 48 03 45 f0 add -0x10(%rbp),%rax
38: c6 00 5f movb $0x5f,(%rax)
int b[3];
memset(b, 0, sizeof(b));
char *a=(char*)b;
int i;
for(i=0; i<9; i++){
3b: 83 45 fc 01 addl $0x1,-0x4(%rbp)
3f: 83 7d fc 08 cmpl $0x8,-0x4(%rbp)
43: 7e ea jle 2f <main+0x2f>
a[i] = 0x5f;
}
printf("%x %x %x\n", b[0], b[1], b[2]);
45: 8b 4d e8 mov -0x18(%rbp),%ecx
48: 8b 55 e4 mov -0x1c(%rbp),%edx
4b: 8b 45 e0 mov -0x20(%rbp),%eax
4e: 89 c6 mov %eax,%esi
50: bf 00 00 00 00 mov $0x0,%edi
55: b8 00 00 00 00 mov $0x0,%eax
5a: e8 00 00 00 00 callq 5f <main+0x5f>
}
5f: c9 leaveq
60: c3 retq
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 134.122.249.1
→ BombCat:再再講下去要查NPN、PNP電晶體了 09/06 12:38
→ purincess:可以來個intel syntax的不要at&t syntax的嗎XDDD 09/06 14:08
推 diabloevagto:詳細推 09/06 14:31
推 stupid0319:64位元版本 09/06 14:34
※ 編輯: lungswu 來自: 134.122.249.1 (09/06 19:37)
推 VictorTom:push....XD 09/06 20:38
→ yzpdal:一目了然真清楚 謝謝解釋 09/06 23:25