How do you get assembler output from C/C++ source

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?

16条回答
浪荡孟婆
2楼-- · 2018-12-31 07:24

Use "-S" as an option. It displays the assembly output in the terminal.

查看更多
人气声优
3楼-- · 2018-12-31 07:24

recently i wanted to know the assembly of each functions in a program
this is how i did it.

$ gcc main.c                      // main.c source file
$ gdb a.exe                       // gdb a.out in linux
  (gdb) disass main               // note here main is a function
                                  // similary it can be done for other functions
查看更多
不流泪的眼
4楼-- · 2018-12-31 07:25

Here is a solution for C using gcc :

gcc -S program.c && gcc program.c -o output
  1. Here the first part stores the assembly output of the program in the same file name as Program but with a changed .s extension, you can open it as any normal text file.

  2. The second part here compiles your program for actual usage and generates an executable for your Program with a specified file name.

The program.c used above is the name of your program and output is the name of the executable you want to generate.

BTW It's my First post on StackOverFlow :-}

查看更多
初与友歌
5楼-- · 2018-12-31 07:26

The following command line is from Christian Garbin's blog

g++ -g -O -Wa,-aslh horton_ex2_05.cpp >list.txt

I ran G++ from a DOS window on Win-XP, against a routine that contains an implicit cast

c:\gpp_code>g++ -g -O -Wa,-aslh horton_ex2_05.cpp >list.txt
horton_ex2_05.cpp: In function `int main()':
horton_ex2_05.cpp:92: warning: assignment to `int' from `double'

The output is asssembled generated code iterspersed with the original C++ code (the C++ code is shown as comments in the generated asm stream)

  16:horton_ex2_05.cpp **** using std::setw;
  17:horton_ex2_05.cpp ****
  18:horton_ex2_05.cpp **** void disp_Time_Line (void);
  19:horton_ex2_05.cpp ****
  20:horton_ex2_05.cpp **** int main(void)
  21:horton_ex2_05.cpp **** {
 164                    %ebp
 165                            subl $128,%esp
?GAS LISTING C:\DOCUME~1\CRAIGM~1\LOCALS~1\Temp\ccx52rCc.s
166 0128 55                    call ___main
167 0129 89E5          .stabn 68,0,21,LM2-_main
168 012b 81EC8000      LM2:
168      0000
169 0131 E8000000      LBB2:
169      00
170                    .stabn 68,0,25,LM3-_main
171                    LM3:
172                            movl $0,-16(%ebp)
查看更多
登录 后发表回答