<p>You write a function and, looking at the resulting assembly, you see it can be improved.</p>

<p>You would like to keep the function you wrote, for readability, but you would like to substitute your own assembly for the compiler's. Is there any way to establish a relationship between your high-livel language function and the new assembly?</p>

[{'answer_id': 64348, 'author': 'Daniel Papasian', 'author_id': 7548, 'author_profile': 'https://Stackoverflow.com/users/7548', 'pm_score': 2, 'selected': False, 'text': "<p>None, I suppose.  You've rejected the compiler's work in favor of your own.  You might as well throw out the function you wrote in the compiled language, because now all you have is your assembler in that platform.</p>\n\n<p>I would highly advise against engaging in this kind of optimization because unless you're sure, via profiling and analysis, that you truly are making a difference.  </p>\n"}, {'answer_id': 64398, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>It depends on the language you wrote your function in.  Some languages like C are very low-level, translating each function call or statement to specific assembly statements.  If you did use C, you can replace your function with inline assembly to improve performance.</p>\n\n<p>Other high-level languages may convert each statement into macro routines or other more complex calls on the assembly side.  Certain optimizations (like tail recursion, loop unrolling, etc) can be implemented easily on the source side, but others (like making more efficient use of the register file) may be impossible (again, depending on the language and the compiler you're using).</p>\n"}, {'answer_id': 64405, 'author': 'DGentry', 'author_id': 4761, 'author_profile': 'https://Stackoverflow.com/users/4761', 'pm_score': 1, 'selected': False, 'text': "<p>Its tough to say there is any relationship between modified assembly and the source which generated the unmodified version. It will certainly confuse debugging tools: register contents will no longer match the source variables they were supposed to correspond to.</p>\n\n<p>There are a number of places in packet processing code where I've examined the generated assembly and gone back to change the original source code in order to improve the result. Re-arranging source can reduce the number of branches, __attribute__ and compiler arguments can align branch points and functions to reduce I$ misses. In desperate cases a little inline assembly can be used, so that the binary can still be compiled from source.</p>\n"}, {'answer_id': 64411, 'author': 'TK.', 'author_id': 1816, 'author_profile': 'https://Stackoverflow.com/users/1816', 'pm_score': 3, 'selected': True, 'text': "<p>If you are looking at the assembly, then its fair to assume that you have a good understanding about how code gets compiled down. If you have this knowledge, then its sometimes possible to 'reverse enginer' the changes back up into the original language but its often better not to bother. </p>\n\n<p>The optimisations that you make are likely to be very small in comparison to the time and effort required in first making these changes. I would suggest that you leave this kind of work to the compiler and go have a cup of tea. If the changes are significant, and the performance is critical, (as say in the embedded world) then you might want to mix the normal code with the assemblar in some fashion, however, on most computers and chips the performance is usually sufficient to avoid this headache. </p>\n\n<p>If you <em>really</em> need more performance, then optimise the code not the assembly.</p>\n"}, {'answer_id': 64414, 'author': 'James Aylett', 'author_id': 6302, 'author_profile': 'https://Stackoverflow.com/users/6302', 'pm_score': 1, 'selected': False, 'text': '<p>Something you could try is to separate your original function into its own file, and provide a make rule to build the assembler from there. Then update the assembler file with your improved version, and provide a make rule to build an object file from the assembler file. Then change your link rules to include that object file.</p>\n\n<p>If you only ever change the assembler file, that will keep on being used. If you ever change the original higher-level language file, the assembler file will be rebuilt and the object file built from the new (unimproved) version.</p>\n\n<p>This gives you a relationship between the two; you probably want to add a warning comment at the top of the higher-level language file to warn about the behaviour. Using some form of VCS will give you the ability to recover the improved assembler file if you make a mistake here.</p>\n'}, {'answer_id': 64460, 'author': 'spoulson', 'author_id': 3347, 'author_profile': 'https://Stackoverflow.com/users/3347', 'pm_score': 1, 'selected': False, 'text': '<p>If you\'re writing a native compiled app in Visual C++, there are two methods:</p>\n\n<ol>\n<li>Use the <code>__asm { }</code> block and write your assembler in there.</li>\n<li>Write your functions in <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7a1c9da0-0510-44a2-b042-7ef370530c64&amp;displaylang=en" rel="nofollow noreferrer">MASM</a> assembler, assemble to .obj, and link it as an static library.  In your C/C++ code, declare the function with an <code>extern "C"</code> declaration.</li>\n</ol>\n\n<p>Other C/C++ compilers have similar approaches.</p>\n'}, {'answer_id': 64467, 'author': 'Yes - that Jake.', 'author_id': 5287, 'author_profile': 'https://Stackoverflow.com/users/5287', 'pm_score': 1, 'selected': False, 'text': "<p>In this situation, you generally have two options: optimize the code or rewrite the compiler. I can't see where breaking the link between source and op is ever going to be the correct solution.</p>\n"}]
['https://Stackoverflow.com/questions/64321', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8522/']