[go: up one dir, main page]

Menu

[r10]: / htdocs / howto / howtonot.html  Maximize  Restore  History

Download this file

141 lines (130 with data), 7.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>How to NOT use Assembly</title><meta name="generator" content="DocBook XSL Stylesheets V1.76.1"><meta name="keywords" content="assembly, assembler, asm, inline, 32-bit, IA-32, i386, x86, nasm, gas, as, as86, yasm, fasm, shasm, osimpa, OS, Linux, Unix, kernel, system, libc, glibc, system call, interrupt, small, fast, embedded, hardware, port, macroprocessor, metaprogramming, preprocessor"><link rel="home" href="Assembly-HOWTO.html" title="Linux Assembly HOWTO"><link rel="up" href="doyouneed.html" title="Chapter 2. Do you need assembly?"><link rel="prev" href="doyouneed.html" title="Chapter 2. Do you need assembly?"><link rel="next" href="landa.html" title="Linux and assembly"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">How to NOT use Assembly</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="doyouneed.html">Prev</a> </td><th width="60%" align="center">Chapter 2. Do you need assembly?</th><td width="20%" align="right"> <a accesskey="n" href="landa.html">Next</a></td></tr></table><hr></div><div class="section" title="How to NOT use Assembly"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="idp108768"></a>How to NOT use Assembly</h2></div></div></div>
<div class="section" title="General procedure to achieve efficient code"><div class="titlepage"><div><div><h3 class="title"><a name="idp109712"></a>General procedure to achieve efficient code</h3></div></div></div>
<p>
As Charles Fiterman says on
<a class="ulink" href="news:comp.compilers" target="_top">comp.compilers</a>
about human vs computer-generated assembly code:
</p>
<div class="blockquote"><blockquote class="blockquote">
<div class="literallayout"><p><br>
The human should always win and here is why.<br>
<br>
First the human writes the whole thing in a high level language.<br>
Second he profiles it to find the hot spots where it spends its time.<br>
Third he has the compiler produce assembly for those small sections of code.<br>
Fourth he hand tunes them looking for tiny improvements over the machine generated code.<br>
<br>
The human wins because he can use the machine.<br>
</p></div>
</blockquote></div>
</div>
<div class="section" title="Languages with optimizing compilers"><div class="titlepage"><div><div><h3 class="title"><a name="idp112544"></a>Languages with optimizing compilers</h3></div></div></div>
<p>
Languages like ObjectiveCAML, SML, CommonLISP, Scheme, ADA, Pascal, C, C++,
among others, all have free optimizing compilers
that will optimize the bulk of your programs,
and often do better than hand-coded assembly even for tight loops,
while allowing you to focus on higher-level details,
and without forbidding you to grab
a few percent of extra performance in the above-mentioned way,
once you've reached a stable design.
Of course, there are also commercial optimizing compilers
for most of these languages, too!
</p>
<p>
Some languages have compilers that produce C code,
which can be further optimized by a C compiler:
LISP, Scheme, Perl, and many other.
Speed is fairly good.
</p>
</div>
<div class="section" title="General procedure to speed your code up"><div class="titlepage"><div><div><h3 class="title"><a name="idp114656"></a>General procedure to speed your code up</h3></div></div></div>
<p>
As for speeding code up,
you should do it only for parts of a program
that a profiling tool has consistently identified
as being a performance bottleneck.
</p>
<p>
Hence, if you identify some code portion as being too slow, you should
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
<p>
first try to use a better algorithm;
</p>
</li><li class="listitem">
<p>
then try to compile it rather than interpret it;
</p>
</li><li class="listitem">
<p>
then try to enable and tweak optimization from your compiler;
</p>
</li><li class="listitem">
<p>
then give the compiler hints about how to optimize
(typing information in LISP; register usage with GCC;
lots of options in most compilers, etc).
</p>
</li><li class="listitem">
<p>
then possibly fallback to assembly programming
</p>
</li></ul></div><p>
</p>
<p>
Finally, before you end up writing assembly,
you should inspect generated code,
to check that the problem really is with bad code generation,
as this might really not be the case:
compiler-generated code might be better than what you'd have written,
particularly on modern multi-pipelined architectures!
Slow parts of a program might be intrinsically so.
The biggest problems on modern architectures with fast processors
are due to delays from memory access, cache-misses, TLB-misses,
and page-faults;
register optimization becomes useless,
and you'll more profitably re-think data structures and threading
to achieve better locality in memory access.
Perhaps a completely different approach to the problem might help, then.
</p>
</div>
<div class="section" title="Inspecting compiler-generated code"><div class="titlepage"><div><div><h3 class="title"><a name="idp121792"></a>Inspecting compiler-generated code</h3></div></div></div>
<p>
There are many reasons to inspect compiler-generated assembly code.
Here is what you'll do with such code:
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
<p>
check whether generated code
can be obviously enhanced with hand-coded assembly
(or by tweaking compiler switches)
</p>
</li><li class="listitem">
<p>
when that's the case,
start from generated code and modify it
instead of starting from scratch
</p>
</li><li class="listitem">
<p>
more generally, use generated code as stubs to modify,
which at least gets right the way
your assembly routines interface to the external world
</p>
</li><li class="listitem">
<p>
track down bugs in your compiler (hopefully the rarer)
</p>
</li></ul></div><p>
</p>
<p>
The standard way to have assembly code be generated
is to invoke your compiler with the <code class="option">-S</code> flag.
This works with most Unix compilers,
including the GNU C Compiler (GCC), but YMMV.
As for GCC, it will produce more understandable assembly code with
the <code class="option">-fverbose-asm</code> command-line option.
Of course, if you want to get good assembly code,
don't forget your usual optimization options and hints!
</p>
</div>
</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="doyouneed.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="doyouneed.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="landa.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 2. Do you need assembly? </td><td width="20%" align="center"><a accesskey="h" href="Assembly-HOWTO.html">Home</a></td><td width="40%" align="right" valign="top"> Linux and assembly</td></tr></table></div></body></html>