typedef example
#include <stdio.h>
int main(){
int a=1;
int b=15;
int c=a+b;
}
用一下命令编译成.s,其中“-S”用于指定仅编译,“-O0”用于阻止编译器代码优化
gcc -S -O0 -o a.s a.c
结果如下:
.file "a.c"
.text
.def __main; .scl 2; .type 32; .endef
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $48, %rsp
.seh_stackalloc 48
.seh_endprologue
call __main
movl $1, -4(%rbp)
movl $15, -8(%rbp)
movl -4(%rbp), %edx
movl -8(%rbp), %eax
addl %edx, %eax
movl %eax, -12(%rbp)
movl $0, %eax
addq $48, %rsp
popq %rbp
ret
.seh_endproc
.ident "GCC: (GNU) 9.2.0"
使用typedef的版本:
#include <stdio.h>
typedef int Intenger;
int main(){
Intenger a=1;
Intenger b=15;
Intenger c=a+b;
}
编译:
gcc -S -O0 -o b.s b.c
结果:
.file "b.c"
.text
.def __main; .scl 2; .type 32; .endef
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $48, %rsp
.seh_stackalloc 48
.seh_endprologue
call __main
movl $1, -4(%rbp)
movl $15, -8(%rbp)
movl -4(%rbp), %edx
movl -8(%rbp), %eax
addl %edx, %eax
movl %eax, -12(%rbp)
movl $0, %eax
addq $48, %rsp
popq %rbp
ret
.seh_endproc
.ident "GCC: (GNU) 9.2.0"
用Windows的fc命令对比两次结果:
>fc a.s b.s
正在比较文件 a.s 和 B.S
***** a.s
.file "a.c"
.text
***** B.S
.file "b.c"
.text
*****
除了文件名,没有任何不同,typedef并不会影响汇编代码