-1

Possible Duplicate:
How do you get assembler output from C/C++ source in gcc?

I have a simple question.

I have some native C++ code written for an Android application.

Is there any way I can see what assembly code is generated by my GCC compiler when I compile my project in Eclipse?

Community
  • 1
  • 1
revolutionary
  • 3,314
  • 4
  • 36
  • 53
  • Hi Brian, this is a different question as it is in the context of Android's NDK. I am using the GCC compiler through a cygwin and eclipse setup. So its not clear how I should pass extra parameters to the GCC compiler in cygwin which is using a makefile in my projects to compile .c and c++ files.. Let me know if you have any clue for doing that – revolutionary May 09 '12 at 04:33

1 Answers1

9

You can do it in multiple ways.

  1. Add -S flag to LOCAL_CFLAGS in your Android.mk. Then run ndk-build. But this will fail on first file, so you won't get assembler code on later files.
  2. Invoke arm-linux-androideabi-gcc -S file.cpp manually from commandline on needed file. Additionally you could need to pass correct includes defines. Run ndk-build V=1 to see exactly what commandline it uses
  3. run build normally to get shared library (.so file). And run arm-linux-androideabi-objdump -d libYourLibrary.so >disasm.txt. This will dissasembly whole library, not just one .cpp file.
Mārtiņš Možeiko
  • 12,733
  • 2
  • 45
  • 45
  • Thanks Martins. #1 does not work for me as you said it fails the link process. For #2, I tried the gcc -s option but dont know how to give the correct include defines?? The -V option on NDK-build is not recognised as valid. For #3, I found out how to use objdump on my object .o files. This is working fine, and I use an additional "-l" option to see the line numbers of the corresponding .cpp files. Now, the dump file contains assembler code for every .cpp line but some lines are repeated multiple times. Is this becoz of calling other functions on those lines ? Please do answer. – revolutionary May 09 '12 at 13:23
  • some output example: D:\Development\Workspace\Quake3\Quake3/jni/engine/frustum.cpp:253 20: 2302 movs r3, #2 D:\Development\Workspace\Quake3\Quake3/jni/engine/frustum.cpp:251 22: 9106 str r1, [sp, #24] 24: 6892 ldr r2, [r2, #8] 26: 9205 str r2, [sp, #20] 28: 6a40 ldr r0, [r0, #36] ; 0x24 D:\Development\Workspace\Quake3\Quake3/jni/engine/frustum.cpp:256 2a: 2200 movs r2, #0 2c: 9201 str r2, [sp, #4] D:\Development\Workspace\Quake3\Quake3/jni/engine/frustum.cpp:253 2e: 930b str r3, [sp, #44] ; 0x2c 30: 1c06 adds r6, r0, #0 – revolutionary May 09 '12 at 17:32
  • Please help me my lovely people – revolutionary May 09 '12 at 19:37
  • Of course link process will fail. Because -S instructs gcc to generate assembly files, not object files. You just open object file in any text editor and you'll assembly for your C/C++ code. About invalid -V, that is my mistake, I my answer for correct argument. – Mārtiņš Možeiko May 09 '12 at 21:14
  • Martins, can you please look at the code that i have pasted in a text editor? I just want to point out how the some line no.s for c++ files are being repated with different asssembley codes... – revolutionary May 09 '12 at 23:42
  • Use some code paste service like http://pastie.org/ or https://gist.github.com/ to preserve code formatting. Otherwise it is code not readable. But generally - do you understand that for optimized code there is no 1-to-1 relation of C/C++ code and assembly code? You should compile without optimizations if you want easy to read assembler code. – Mārtiņš Možeiko May 10 '12 at 00:36
  • Hi Martins, thanks for the info. Please see the code here http://pastie.org/3887882. You will see that line # 257 is repeated twice. This happens for many cases. If there is no one to one relation between c++ and assembley code, i wonder how to make sense of my assembley code then .... – revolutionary May 10 '12 at 05:47
  • Functions can also be inlined - if you use one function two times in another, that what line numbers do you assign to inlined assembler code? You must think about optimizations, modern compiler are very smart and know a lot of "tricks". What exactly are you trying to accomplish here? – Mārtiņš Možeiko May 10 '12 at 06:21
  • Well, i need to replace my c++ code with an optimal assembley code. This will be used in a rendering loop. So it has to be as fast as possible for higher frame rates. I need to compare the assembley code generated by the gcc for my c++ source files and replace with a hand-crafted assembley version... – revolutionary May 10 '12 at 06:25
  • No offense, but I'm 99.99% sure that C/C++ code will be more efficient (faster) than your hand written assembly, if you are surprised by this assembly output... – Mārtiņš Možeiko May 10 '12 at 07:05
  • Hi Martins, thanks for the advice but there are always ways to save a few instructions and do it lesser than what the compiler does if you have the dump. – revolutionary May 10 '12 at 08:40
  • Yes, but the question is - does it make a difference in performance. Lesser is not always faster. – Mārtiņš Možeiko May 10 '12 at 09:02
  • yes, you are right, its about using the right instructions that take less cpu cycle time. The places where you can save some cycles are if-conditions in c++. I have certain conditions which i can optimize at assembley level so there is some hope. But lets see how it goes. – revolutionary May 10 '12 at 16:10