Home > Backend Development > C++ > How Can I Get Assembly Code Output from GCC Compiling C/C Source Code?

How Can I Get Assembly Code Output from GCC Compiling C/C Source Code?

Susan Sarandon
Release: 2024-12-27 06:51:09
Original
527 people have browsed it

How Can I Get Assembly Code Output from GCC Compiling C/C   Source Code?

Getting Assembler Output from C/C Source in GCC

To analyze how code is compiled, it's beneficial to retrieve the emitted assembly code. In GCC, this can be achieved using the -S option.

gcc -S helloworld.c
Copy after login

This will invoke the preprocessor and initial compilation, but halt before running the assembler.

For more comprehensive assembly output, consider using -fverbose-asm alongside -O0. However, note that this works best at default optimization levels.

The default output file is helloworld.s, but can be customized using -o. Additionally, -o - can be used to direct output directly to standard output.

gcc -S -o my_asm_output.s helloworld.c
Copy after login

If only the object file is available, objdump with the --disassemble option can be employed:

objdump -S --disassemble helloworld > helloworld.dump
Copy after login

This will interleave source lines with disassembly output. Enabling debugging (-g) and avoiding stripping can enhance the disassembly details.

For even more customization, consider options such as -rwC (symbol relocations), -Mintel (Intel syntax), and -r (important for object files with placeholder symbol references).

For instance, the following command provides a detailed dump using Intel syntax:

objdump -drwC -Mintel -S foo.o | less
Copy after login

The above is the detailed content of How Can I Get Assembly Code Output from GCC Compiling C/C Source Code?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template