GCC-S选项(生成汇编文件)

更新时间:2023-07-01 19:10:45 阅读: 评论:0

qq昵称英文名GCC-S选项(⽣成汇编⽂件)
GCC -S 选项 (⽣成汇编⽂件)
编译器的核⼼任务是把 C 程序翻译成机器的汇编语⾔ (asmbly language)。汇编语⾔是⼈类可以阅读的编程语⾔,也是相当接近实际机器码的语⾔。由此导致每种 CPU 架构都有不同的汇编语⾔。
GCC 是⼀个适合多种 CPU 架构的编译器,不会把 C 程序语句直接翻译成⽬标机器的汇编语⾔,⽽是在输⼊语⾔和输出汇编语⾔之间,利⽤⼀个中间语⾔,称为 Register Transfer Language (RTL,寄存器传输语⾔)。借助于这个抽象层,在任何背景下,编译器可以选择最经济的⽅式对给定的操作编码。
在交互⽂件中针对⽬标机器的抽象描述,为编译器重新定向到新架构提供了⼀个结构化的⽅式。从 GCC ⽤户⾓度来看,我们可以忽略这个中间步骤。
通常情况下,GCC 把汇编语⾔输出存储到临时⽂件中,并且在汇编器执⾏完后⽴刻删除它们。但是可以使⽤ -S 选项,让编译程序在⽣成汇编语⾔输出之后⽴刻停⽌。
如果没有指定输出⽂件名,那么采⽤ -S 选项的 GCC 编译过程会为每个被编译的输⼊⽂件⽣成以 .s 作为后缀的汇编语⾔⽂件。
$ gcc -S function_printf.c
$ gcc -S hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc -S hello_world.c
hello_world.c: In function ‘main’:
hello_world.c:15:5: warning: implicit declaration of function ‘function_printf’ [-Wimplicit-function-declaration]
function_printf();
^
汉娜姐妹strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 20
drwxrwxr-x 2 strong strong 4096 Sep  4 21:32 ./
drwxrwxr-x 6 strong strong 4096 Sep  4 20:16 ../
-rw-rw-r-- 1 strong strong  452 Sep  4 20:18 function_printf.c
-rw-rw-r-- 1 strong strong  503 Sep  4 20:18 hello_world.c
-rw-rw-r-- 1 strong strong  504 Sep  4 21:32 hello_world.s
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc -S function_printf.c
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 24
drwxrwxr-x 2 strong strong 4096 Sep  4 21:32 ./
drwxrwxr-x 6 strong strong 4096 Sep  4 20:16 ../
-
rw-rw-r-- 1 strong strong  452 Sep  4 20:18 function_printf.c
-rw-rw-r-- 1 strong strong  517 Sep  4 21:32 function_printf.s
-rw-rw-r-- 1 strong strong  503 Sep  4 20:18 hello_world.c
-rw-rw-r-- 1 strong strong  504 Sep  4 21:32 hello_world.s
strong@foreverstrong:~/ForeverStrong/hello_world$
编译器预处理 *.c,将其翻译成汇编语⾔,并将结果存储在 *.s ⽂件中。
如果想把 C 语⾔变量的名称作为汇编语⾔语句中的注释,可以加上 -fverbo-asm 选项:
$ gcc -S -fverbo-asm function_printf.c
$ gcc -S -fverbo-asm hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 16
drwxrwxr-x 2 strong strong 4096 Sep  4 21:33 ./
drwxrwxr-x 6 strong strong 4096 Sep  4 20:16 ../
-rw-rw-r-- 1 strong strong  452 Sep  4 20:18 function_printf.c
-rw-rw-r-- 1 strong strong  503 Sep  4 20:18 hello_world.c
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc -S -fverbo-asm hello_world.c
hello_world.c: In function ‘main’:
hello_world.c:15:5: warning: implicit declaration of function ‘function_printf’ [-Wimplicit-function-declaration]    function_printf();
^
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ gcc -S -fverbo-asm function_printf.c
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ ll
total 24
randydrwxrwxr-x 2 strong strong 4096 Sep  4 21:33 ./
drwxrwxr-x 6 strong strong 4096 Sep  4 20:16 ../
-rw-rw-r-- 1 strong strong  452 Sep  4 20:18 function_printf.c
-rw-rw-r-- 1 strong strong 2723 Sep  4 21:33 function_printf.s
-rw-rw-r-- 1 strong strong  503 Sep  4 20:18 hello_world.c
-rw-rw-r-- 1 strong strong 2721 Sep  4 21:33 hello_world.s
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ cat function_printf.s
.
file "function_printf.c"
# GNU C11 (Ubuntu 5.4.0-6ubuntu1~16.04.9) version 5.4.0 20160609 (x86_64-linux-gnu)
# compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3 # GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
# options pasd:  -imultiarch x86_64-linux-gnu function_printf.c
# -mtune=generic -march=x86-64 -fverbo-asm -fstack-protector-strong
# -Wformat -Wformat-curity
# options enabled:  -faggressive-loop-optimizations
# -fasynchronous-unwind-tables -fauto-inc-dec -fchkp-check-incomplete-type
# -fchkp-check-read -fchkp-check-write -fchkp-instrument-calls
# -fchkp-narrow-bounds -fchkp-optimize -fchkp-store-bounds
# -fchkp-u-static-bounds -fchkp-u-static-const-bounds
# -fchkp-u-wrappers -fcommon -fdelete-null-pointer-checks
feat
# -fdwarf2-cfi-asm -fearly-inlining -feliminate-unud-debug-types
# -ffunction-c -fgc-lm -fgnu-runtime -fgnu-unique -fident
# -finline-atomics -fira-hoist-pressure -fira-share-save-slots
# -fira-share-spill-slots -fivopts -fkeep-static-consts
# -fleading-underscore -flifetime-d -flto-odr-type-merging -fmath-errno
# -fmerge-debug-strings -fpeephole -fprefetch-loop-arrays
# -freg-struct-return -fsched-critical-path-heuristic
# -fsched-dep-count-heuristic -fsched-group-heuristic -fsched-interblockshe is so
# -fsched-last-insn-heuristic -fsched-rank-heuristic -fsched-spec
# -fsched-spec-insn-heuristic -fsched-stalled-insns-dep -fschedule-fusion
# -fmantic-interposition -fshow-column -fsigned-zeros
# -fsplit-ivs-in-unroller -fstack-protector-strong -fstdarg-opt
# -fstrict-volatile-bitfields -fsync-libcalls -ftrapping-math
# -ftree-coalesce-vars -ftree-clim -ftree-forwprop -ftree-loop-if-convert
# -ftree-loop-im -ftree-loop-ivcanon -ftree-loop-optimize
# -ftree-parallelize-loops= -ftree-phiprop -ftree-reassoc -ftree-scev-cprop
# -funit-at-a-time -funwind-tables -fverbo-asm -fzero-initialized-in-bss
# -m128bit-long-double -m64 -m80387 -malign-stringops
# -mavx256-split-unaligned-load -mavx256-split-unaligned-store
# -mfancy-math-387 -mfp-ret-in-387 -mfxsr -mglibc -mieee-fp
# -mlong-double-80 -mmmx -mno-s4 -mpush-args -mred-zone -ms -ms2
# -mtls-direct-g-refs -mvzeroupper
.ction .rodata
.LC0:
.string "Hello Printf"
.text
.globl function_printf
.type function_printf, @function
function_printf:
.LFB0:
.cfi_startproc
pushq %rbp #
.
cfi_def_cfa_offt 16
.cfi_offt 6, -16
.cfi_def_cfa_register 6
movl $.LC0, %edi #,
call puts #
nop
sliding doorspopq %rbp #
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.
size function_printf, .-function_printf
.ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"
.ction .note.GNU-stack,"",@progbits
strong@foreverstrong:~/ForeverStrong/hello_world$
strong@foreverstrong:~/ForeverStrong/hello_world$ cat hello_world.s
.file "hello_world.c"
# GNU C11 (Ubuntu 5.4.0-6ubuntu1~16.04.9) version 5.4.0 20160609 (x86_64-linux-gnu)
# compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3 # GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
# options pasd:  -imultiarch x86_64-linux-gnu hello_world.c
# -mtune=generic -march=x86-64 -fverbo-asm -fstack-protector-strong
# -Wformat -Wformat-curity
# options enabled:  -faggressive-loop-optimizations
# -fasynchronous-unwind-tables -fauto-inc-dec -fchkp-check-incomplete-type
# -fchkp-check-read -fchkp-check-write -fchkp-instrument-calls
# -fchkp-narrow-bounds -fchkp-optimize -fchkp-store-bounds
# -fchkp-u-static-bounds -fchkp-u-static-const-bounds
# -fchkp-u-wrappers -fcommon -fdelete-null-pointer-checks
# -fdwarf2-cfi-asm -fearly-inlining -feliminate-unud-debug-types
# -ffunction-c -fgc-lm -fgnu-runtime -fgnu-unique -fident
# -finline-atomics -fira-hoist-pressure -fira-share-save-slots
# -fira-share-spill-slots -fivopts -fkeep-static-consts
# -fleading-underscore -flifetime-d -flto-odr-type-merging -fmath-errno
# -fmerge-debug-strings -fpeephole -fprefetch-loop-arrays
# -freg-struct-return -fsched-critical-path-heuristic
# -fsched-dep-count-heuristic -fsched-group-heuristic -fsched-interblock
# -fsched-last-insn-heuristic -fsched-rank-heuristic -fsched-spec
# -fsched-spec-insn-heuristic -fsched-stalled-insns-dep -fschedule-fusion
# -fmantic-interposition -fshow-column -fsigned-zeros
bec高级报名时间# -fsplit-ivs-in-unroller -fstack-protector-strong -fstdarg-opt
# -fstrict-volatile-bitfields -fsync-libcalls -ftrapping-math
# -ftree-coalesce-vars -ftree-clim -ftree-forwprop -ftree-loop-if-convert
# -ftree-loop-im -ftree-loop-ivcanon -ftree-loop-optimize
scratched# -ftree-parallelize-loops= -ftree-phiprop -ftree-reassoc -ftree-scev-cprop
# -funit-at-a-time -funwind-tables -fverbo-asm -fzero-initialized-in-bss
# -m128bit-long-double -m64 -m80387 -malign-stringops
# -mavx256-split-unaligned-load -mavx256-split-unaligned-store
# -mfancy-math-387 -mfp-ret-in-387 -mfxsr -mglibc -mieee-fp
# -mlong-double-80 -mmmx -mno-s4 -mpush-args -mred-zone -ms -ms2
# -mtls-direct-g-refs -mvzeroupper
.ction .rodata万圣节 英文
.LC0:
.string "Hello World"
.text
.globl main
.
type main, @function
main:
.LFB2:
.cfi_startproc
pushq %rbp #
.cfi_def_cfa_offt 16
.cfi_offt 6, -16
movq %rsp, %rbp #,
.cfi_def_cfa_register 6
call function_printf #
政府工作报告英文movl $.LC0, %edi #,
call puts #
movl $0, %eax #, D.2811
popq %rbp #
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size main, .-main
.ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609" .ction .note.GNU-stack,"",@progbits
strong@foreverstrong:~/ForeverStrong/hello_world$

本文发布于:2023-07-01 19:10:45,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/163940.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:汇编   编译器   输出   作为   架构   机器   抽象   语句
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图