I'm writing some methods in C that must run very efficiently, so I'd like to manually edit some of the assembly code that is automatically generated. I know how to read the assembly code using gdb or "objdump -d myfile.o", but can anyone tell me how to edit this code? (Will probably just be minor tweaks.)
Asked
Active
Viewed 4,884 times
1
-
1As you already got some answers, you should also note that modern CPUs and compilers are very good, so it's not that simple as 'okay, i'll use assembler, i'll run a lot faster'. In fact, it's quite hard to write a code that will execute faster than compiler generated, - and it's quite a room for optimization in C. – keltar Dec 06 '12 at 06:38
-
@user - This assumes that you can easily find some tweaks that the compiler writers are not aware of. Otherwise they would already have put this into the compiler's optimizer pass. What are the odds? – Bo Persson Dec 06 '12 at 12:19
-
We'll see how this pans out, but to quote my prof: "Your manual efforts are needed for optimizations the compiler can't do. You have knowledge about the code that is deeper than what the compiler can glean. You know where you can cache results and avoid unnecessary re-computation. ... [Eg] If you know that one of the variables to a particular multiply/divide operation is always some power of two ..., you can convert into the equivalent shift. ... you measure carefully ... which passages are critical so you know where to be ultra-aggressive and where not to bother." – embirico Dec 07 '12 at 05:20
-
I don't believe this statement is still true nowadays. Also, bad example. C have shifts - and compiler most likely will use them when it sees ordinary power-of-two divisions/multiplications, even without special code. Good place for [inline] assembler is to use one very specific instruction - but usually there is intrinsic available to do that. Even more, CPU can reorder your code itself - and even assembly can't change that - oftenly to reduce memory latency. So it's really good chance that hand-written assembly code will be slower thatn compiler's one. – keltar Dec 07 '12 at 09:52
-
And please don't get it wrong - i'm absolutely NOT against assembly. But i'm quite sure that good C code is just as fast. But, to write good C code you have to understand how compiler generates assembly - so it's definitevely worthwile expertise. – keltar Dec 07 '12 at 10:14
-
Hey, thanks for the input. Managed to get it running fast enough with just pre-compiler optimizations :) – embirico Dec 21 '12 at 15:52
3 Answers
4
gcc have -S switch, which stops compilation on after assembly generation phase. Then you cound edit resulting file and manually call assembly (with gas, for example)

keltar
- 17,711
- 2
- 37
- 42
-
tip: rename the output from `.s` to `.S`, so you don't accidentally overwrite your hand-modified asm source file. You might also want to use some of the suggestions from [this Q&A](http://stackoverflow.com/questions/38552116/how-to-remove-noise-from-gcc-clang-assembly-output) to remove noisy compiler directives and leave just the real code. (But if you strip directives, remember to add back in .p2align directives where appropriate). – Peter Cordes Sep 14 '16 at 23:38
2
Sure. It's called "inline assembly", and most compilers support it.
Here's an example using GCC:

paulsm4
- 114,292
- 17
- 138
- 190
-
Before coding inline assembly, one should look at the assembly listing generated by the compiler. This listing should show the calling conventions, such as the registers used to pass parameters. Only optimize if the human can do a better job than the compiler's assembly code. – Thomas Matthews Dec 06 '12 at 04:12
-1
There's no way you can do it. Compiler generates code directly, there is no assembler involved; assembly listing is an aux product that can be generated, can be not. You can take asm listing and assemble it yourself, however.
If you want to improve efficiency of your code, the best way to follow is, as stated before, inline asm; or write an assembly proc, make an object file and link it.

Aleksey Ivchenko
- 299
- 1
- 8
-
2-1: Many compilers can generate either an intermediate assembly language file or interwoven assembly with C listing. This assembly listing is a high enough demanded feature that compilers usually don't go directly from C language to machine code. – Thomas Matthews Dec 06 '12 at 04:14
-
Also, many people recommend writing the function in pure assembly as an assembly language file rather than as inlined assembly code. – Thomas Matthews Dec 06 '12 at 04:14
-
Why -1? Just as I said - it can be either generated, or not - it depends. I have recommended editing and assembling assembly output of compiler as well as writing in asm and linking. Can you give me an example of a mainstream compiler that generates assembly file as a main product to be sent to assembler afterwards rather than direct code generation (I mean object file for sure, not executable)? – Aleksey Ivchenko Dec 06 '12 at 11:52