- UID
- 1
- 精华
- 积分
- 76361
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
发表于 2018-6-30 18:56:19
|
显示全部楼层
这里可以帮你补充一个具体的例子。- void very_simple_process(int foo, int bar, int baz)
- {
- char buf[256];
- printf("Process start.\n");
- fprintf(stdout, "This program should not be compiled by using link-time optimization.\n");
- sprintf(buf, "Because it may be inlined due to it will be called only few times.\n\n");
- fputs(buf, stdout);
- printf("The result of foo + bar + baz could be %d + %d + %d,\n", foo, bar, baz);
- printf("Which should be %d.\n", foo + bar + baz);
- printf("Process end.\n");
- }
- // 如果我用中文写
- void 非常简单的一个程序(int 甲, int 乙, int 丙)
- {
- char 缓冲区[256];
- printf("程序开始。\n");
- fprintf(stdout, "你不能用链接时间优化来优化这个程序。\n");
- sprintf(缓冲区, "因为这个程序大概会因为被调用的次数太少而被内联。\n\n");
- fputs(缓冲区, stdout);
- printf("甲+乙+丙的结果,应该是%d + %d + %d,\n", foo, bar, baz);
- printf("它大概应该等于%d\n", foo + bar + baz);
- printf("程序结束。\n");
- }
复制代码 如果我用汇编写,我大概会这样写:- segment .rdata
- str1 db "Process start.", 0xa
- str2 db "This program should not be compiled by using link-time optimization.", 0xa
- str3 db "Because it may be inlined due to it will be called only few times.", 0xa, 0xa
- str4 db "The result of foo + bar + baz could be %d + %d + %d,", 0xa
- str5 db "Which should be %d.", 0xa
- str6 db "Process end.", 0xa
- segment .text
- _very_simple_process:
- push rbx
- push rsi
- push rdi
- sub rsp, 256 ;buf
- mov rbx, rcx
- mov rsi, rdx
- mov rdi, r8
- mov rcx, str1
- call _printf
- mov rcx, [_stdout]
- mov rdx, str2
- call _fprintf
- lea rcx, [rsp] ;指向buf
- mov rdx, str3
- call _sprintf
- lea rcx, [rsp] ;指向buf
- mov rdx, [_stdout]
- call _fputs
- mov rcx, str4
- mov edx, ebx
- mov r8d, esi
- mov r9d, edi
- call _printf
- mov rcx, str5
- lea edx, [ebx + esi]
- add edx, edi
- call _printf
- mov rcx, str6
- call _printf
- add rsp, 256
- pop rdi
- pop rsi
- pop rbx
- ret
复制代码 在这个示例里面,栈上的内存布局大概如下图所示:
然后rsp的位置在最左边。
|
|