Interleaving C code with assembly


#1

Is there a way to generate assembly code with the corresponding C code interleaved similar to the --interleave command for the armcc compiler used in Keil? http://www.keil.com/support/man/docs/armcc/armcc_chr1359124927770.htm


(Jim Wilson) #2

The assembler has a -a option to enable listings, which by default is -ahls which gives high-level code, assembly code, and symbols in the listing file. You can use this from gcc by using -Wa,a to pass the option to the assembler. You will also have to use -g to make it work, since it uses debug info to map assembly code back to the C code.

objdump can produce listings with -drS, which disassembles, shows relocs, and shows the original source. Like the above, it needs -g from the compiler to work.

Note that with an optimizing compiler there is not a linear one to one mapping of sources lines to assembly code in the output. Sometimes source lines are duplicated (e.g. loop unrolling). Sometimes source lines are reordered (basic block reorg). Sometimes source lines are intermixed (instruction scheduling). Sometimes source lines are optimized together in hard to understand ways. The result is that these listing options are more likely to be confusing than helpful with optimization, as there is no good way to display the end result with a simple linear listing file. You are better off using a debugger and stepping through code


#3

Hi Jim,

Thanks a lot for the detailed explanation and suggestions. I will take a look at those assembler options you mentioned!


(Jim Wilson) #4

Actually that should be -Wa,-a for the assembler via the compiler. And you probably want to pipe the output to a file.