最近想了解一下c是如何编译为汇编语言的
首先写了一个相当简单的c
#include "stdio.h"
int main(){
int a = 1;
int b = 2;
int c = a + b;
int d = 3 + 4;
printf("%d %d",c,d);
return 0;
}
使用gcc -S
编译为汇编语言:
.file "hello.c"
.text
.section .rodata
.LC0:
.string "%d %d"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $1, -16(%rbp)
movl $2, -12(%rbp)
movl -16(%rbp), %edx
movl -12(%rbp), %eax
addl %edx, %eax
movl %eax, -8(%rbp)
movl $7, -4(%rbp)
movl -4(%rbp), %edx
movl -8(%rbp), %eax
movl %eax, %esi
leaq .LC0(%rip), %rdi
movl $0, %eax
call printf@PLT
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 9.1.0"
.section .note.GNU-stack,"",@progbits
以”点”做为前缀的指令都是用来指导汇编器的命令。
精简一下差不多这样: