- #include <stdlib.h>
- #include <stdio.h>
- int main()
- {
- int m = 1;
- int n = 2;
- int r = m + n;
- return r;
- }
未使用优化:
[root@ShiGuang test]# gcc -S main.c
- .file "main.c"
- .text
- .globl main
- .type main, @function
- main:
- pushl %ebp
- movl %esp, %ebp
- subl $16, %esp
- movl $1, -4(%ebp)
- movl $2, -8(%ebp)
- movl -8(%ebp), %eax
- movl -4(%ebp), %edx
- leal (%edx,%eax), %eax
- movl %eax, -12(%ebp)
- movl -12(%ebp), %eax
- leave
- ret
- .size main, .-main
- .ident "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"
- .section .note.GNU-stack,"",@progbits
[root@ShiGuang test]# gcc -S main.c -O2
- .file "main.c"
- .text
- .p2align 4,,15
- globl main
- .type main, @function
- ain:
- pushl %ebp
- movl $3, %eax
- movl %esp, %ebp
- popl %ebp
- ret
- .size main, .-main
- .ident "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"
- .section .note.GNU-stack,"",@progbits
我们从上面的两段汇编代码就能看出。
