GCC 内存对齐
来源:互联网 发布:ubuntu 中文输入 编辑:程序博客网 时间:2024/06/07 11:49
在学习C 汇编中发现了一个奇怪的现象,如下
int foo1(int m,int n) { int p=m*n; return p; } int foo(int a,int b) { int c=a+1; int d=b+2; int e=foo1(c,d); return e; } int main() { int result=foo(3,4); return 0; }
生成的汇编代码如下
.file "foo.c" .text .globl foo1 .type foo1, @functionfoo1:.LFB0: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $16, %esp movl 8(%ebp), %eax imull 12(%ebp), %eax movl %eax, -4(%ebp) movl -4(%ebp), %eax leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc.LFE0: .size foo1, .-foo1 .globl foo .type foo, @functionfoo:.LFB1: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $16, %esp movl 8(%ebp), %eax addl $1, %eax movl %eax, -12(%ebp) movl 12(%ebp), %eax addl $2, %eax movl %eax, -8(%ebp) pushl -8(%ebp) pushl -12(%ebp) call foo1 addl $8, %esp movl %eax, -4(%ebp) movl -4(%ebp), %eax leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc.LFE1: .size foo, .-foo .globl main .type main, @functionmain:.LFB2: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $16, %esp pushl $4 pushl $3 call foo addl $8, %esp movl %eax, -4(%ebp) movl $0, %eax leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc.LFE2: .size main, .-main .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609" .section .note.GNU-stack,"",@progbits
代码使用32位编译的,一个int的字节是4
把上面的帧栈画出来
汇编中有一行
subl $16 %esp
也就是开辟了 四”块”栈区,但实际上,foo1函数中只有一个local variable —– p
后询问大佬得知了如标题,同时又认识了一本book:)
图片出自《深入理解计算机系统》
阅读全文
0 0
- gcc的内存对齐
- gcc 的内存对齐
- gcc 内存对齐
- GCC 内存对齐
- gcc 中的"__attribute__ ((packed))" gcc移植,内存对齐.结构体对齐
- gcc 中的"__attribute__ ((packed))" gcc移植,内存对齐.结构体对齐-转
- Arm结构体gcc内存边界对齐问题(zt)
- gcc 中结构体(struct)内存对齐问题分析
- 关于gcc中内存对齐的c写法(attribute)
- struct内存对齐:gcc与VC的差别
- GCC对齐与VS对齐
- 关键字 对齐 内存对齐
- 内存对齐
- 内存对齐
- 内存对齐
- 内存对齐
- 内存对齐
- 内存对齐
- 《利用Python进行数据分析》第五章 pandas的基本功能
- ssh_day01_06-eclipse安装svn插件
- 蓝牙5.0 mesh技术分享(二)
- 变量比较
- Training Set
- GCC 内存对齐
- ssh_day01_07-eclipse的svn操作(添加项目到svn)
- Monitor项目开发走过的路~实现一个类似命令行形式的TextView
- 数据库的数据类型优化
- ssh_day01_08-eclipse的svn操作(下载项目到工作空间)
- 学习路线
- 解救臃肿的代码,代码封装利器自定义注解深入剖析
- 用形参操控实参时区分地址和内容
- Python&Opencv&Pycharm安装及环境配置