公告
财富商城
积分规则
提问
发文
2018-12-31 06:33发布
宁负流年不负卿
How does one do this?
If I want to analyze how something is getting compiled, how would I get the emitted assembly code?
Here are the steps to see/print the assembly code of any C program on your Windows
console /terminal/ command prompt :
Write a C program in a C code editor like codeblocks and save it with an extention .c
Compile and run it.
Once run successfully, go to the folder where you have installed your gcc compiler and give the
following command to get a ' .s ' file of the ' .c' file
C:\ gcc> gcc -S complete path of the C file ENTER
An example command ( as in my case)
C:\gcc> gcc -S D:\Aa_C_Certified\alternate_letters.c
This outputs a ' .s' file of the original ' .c' file
4 . After this , type the following command
C;\gcc> cpp filename.s ENTER
Example command ( as in my case)
C;\gcc> cpp alternate_letters.s
This will print/output the entire Assembly language code of your C program.
This will generate the asm code with the C code + line numbers interweaved to more easily see what lines generate what code.
# create assembler code: c++ -S -fverbose-asm -g -O2 test.cc -o test.s # create asm interlaced with source lines: as -alhnd test.s > test.lst
Found in Algorithms for programmers, page 3 (which is the overall 15th page of the PDF).
Use the -S switch
g++ -S main.cpp
or also with gcc
gcc -S main.c
Also see this
Use the -S option:
gcc -S program.c
If you're looking for LLVM assembly:
llvm-gcc -emit-llvm -S hello.c
I don't see this possibility among answers, probably because the question is from 2008, but in 2018 you can use Matt Goldbolt's online website https://godbolt.org
You can also locally git clone and run his project https://github.com/mattgodbolt/compiler-explorer
最多设置5个标签!
Here are the steps to see/print the assembly code of any C program on your Windows
console /terminal/ command prompt :
Write a C program in a C code editor like codeblocks and save it with an extention .c
Compile and run it.
Once run successfully, go to the folder where you have installed your gcc compiler and give the
following command to get a ' .s ' file of the ' .c' file
C:\ gcc> gcc -S complete path of the C file ENTER
An example command ( as in my case)
C:\gcc> gcc -S D:\Aa_C_Certified\alternate_letters.c
This outputs a ' .s' file of the original ' .c' file
4 . After this , type the following command
C;\gcc> cpp filename.s ENTER
Example command ( as in my case)
C;\gcc> cpp alternate_letters.s
This will print/output the entire Assembly language code of your C program.
This will generate the asm code with the C code + line numbers interweaved to more easily see what lines generate what code.
Found in Algorithms for programmers, page 3 (which is the overall 15th page of the PDF).
Use the -S switch
or also with gcc
Also see this
Use the -S option:
If you're looking for LLVM assembly:
I don't see this possibility among answers, probably because the question is from 2008, but in 2018 you can use Matt Goldbolt's online website https://godbolt.org
You can also locally git clone and run his project https://github.com/mattgodbolt/compiler-explorer