"Fossies" - the Fresh Open Source Software Archive

Member "nasm-2.15.05/doc/html/nasmdo13.html" (28 Aug 2020, 5637 Bytes) of package /linux/misc/nasm-2.15.05-xdoc.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    3 <html xmlns="http://www.w3.org/1999/xhtml">
    4 <head>
    5 <title>NASM - The Netwide Assembler</title>
    6 <link href="nasmdoc.css" rel="stylesheet" type="text/css" />
    7 <link href="local.css" rel="stylesheet" type="text/css" />
    8 </head>
    9 <body>
   10 <ul class="navbar">
   11 <li class="first"><a class="prev" href="nasmdo12.html">Chapter 12</a></li>
   12 <li><a class="next" href="nasmdoca.html">Appendix A</a></li>
   13 <li><a class="toc" href="nasmdoc0.html">Contents</a></li>
   14 <li class="last"><a class="index" href="nasmdoci.html">Index</a></li>
   15 </ul>
   16 <div class="title">
   17 <h1>NASM - The Netwide Assembler</h1>
   18 <span class="subtitle">version 2.15.05</span>
   19 </div>
   20 <div class="contents"
   21 >
   22 <h2 id="chapter-13">Chapter 13: Troubleshooting</h2>
   23 <p>This chapter describes some of the common problems that users have been
   24 known to encounter with NASM, and answers them. If you think you have found
   25 a bug in NASM, please see <a href="nasmdoce.html#section-E.2">section
   26 E.2</a>.</p>
   27 <h3 id="section-13.1">13.1 Common Problems</h3>
   28 <h4 id="section-13.1.1">13.1.1 NASM Generates Inefficient Code</h4>
   29 <p>We sometimes get `bug' reports about NASM generating inefficient, or
   30 even `wrong', code on instructions such as <code>ADD ESP,8</code>. This is
   31 a deliberate design feature, connected to predictability of output: NASM,
   32 on seeing <code>ADD ESP,8</code>, will generate the form of the instruction
   33 which leaves room for a 32-bit offset. You need to code
   34 <code>ADD ESP,BYTE 8</code> if you want the space-efficient form of the
   35 instruction. This isn't a bug, it's user error: if you prefer to have NASM
   36 produce the more efficient code automatically enable optimization with the
   37 <code>-O</code> option (see <a href="nasmdoc2.html#section-2.1.24">section
   38 2.1.24</a>).</p>
   39 <h4 id="section-13.1.2">13.1.2 My Jumps are Out of Range</h4>
   40 <p>Similarly, people complain that when they issue conditional jumps (which
   41 are <code>SHORT</code> by default) that try to jump too far, NASM reports
   42 `short jump out of range' instead of making the jumps longer.</p>
   43 <p>This, again, is partly a predictability issue, but in fact has a more
   44 practical reason as well. NASM has no means of being told what type of
   45 processor the code it is generating will be run on; so it cannot decide for
   46 itself that it should generate <code>Jcc NEAR</code> type instructions,
   47 because it doesn't know that it's working for a 386 or above.
   48 Alternatively, it could replace the out-of-range short <code>JNE</code>
   49 instruction with a very short <code>JE</code> instruction that jumps over a
   50 <code>JMP NEAR</code>; this is a sensible solution for processors below a
   51 386, but hardly efficient on processors which have good branch prediction
   52 <em>and</em> could have used <code>JNE NEAR</code> instead. So, once again,
   53 it's up to the user, not the assembler, to decide what instructions should
   54 be generated. See <a href="nasmdoc2.html#section-2.1.24">section
   55 2.1.24</a>.</p>
   56 <h4 id="section-13.1.3">13.1.3 <code>ORG</code> Doesn't Work</h4>
   57 <p>People writing boot sector programs in the <code>bin</code> format often
   58 complain that <code>ORG</code> doesn't work the way they'd like: in order
   59 to place the <code>0xAA55</code> signature word at the end of a 512-byte
   60 boot sector, people who are used to MASM tend to code</p>
   61 <pre>
   62         ORG 0 
   63 
   64         ; some boot sector code 
   65 
   66         ORG 510 
   67         DW 0xAA55
   68 </pre>
   69 <p>This is not the intended use of the <code>ORG</code> directive in NASM,
   70 and will not work. The correct way to solve this problem in NASM is to use
   71 the <code>TIMES</code> directive, like this:</p>
   72 <pre>
   73         ORG 0 
   74 
   75         ; some boot sector code 
   76 
   77         TIMES 510-($-$$) DB 0 
   78         DW 0xAA55
   79 </pre>
   80 <p>The <code>TIMES</code> directive will insert exactly enough zero bytes
   81 into the output to move the assembly point up to 510. This method also has
   82 the advantage that if you accidentally fill your boot sector too full, NASM
   83 will catch the problem at assembly time and report it, so you won't end up
   84 with a boot sector that you have to disassemble to find out what's wrong
   85 with it.</p>
   86 <h4 id="section-13.1.4">13.1.4 <code>TIMES</code> Doesn't Work</h4>
   87 <p>The other common problem with the above code is people who write the
   88 <code>TIMES</code> line as</p>
   89 <pre>
   90         TIMES 510-$ DB 0
   91 </pre>
   92 <p>by reasoning that <code>$</code> should be a pure number, just like 510,
   93 so the difference between them is also a pure number and can happily be fed
   94 to <code>TIMES</code>.</p>
   95 <p>NASM is a <em>modular</em> assembler: the various component parts are
   96 designed to be easily separable for re-use, so they don't exchange
   97 information unnecessarily. In consequence, the <code>bin</code> output
   98 format, even though it has been told by the <code>ORG</code> directive that
   99 the <code>.text</code> section should start at 0, does not pass that
  100 information back to the expression evaluator. So from the evaluator's point
  101 of view, <code>$</code> isn't a pure number: it's an offset from a section
  102 base. Therefore the difference between <code>$</code> and 510 is also not a
  103 pure number, but involves a section base. Values involving section bases
  104 cannot be passed as arguments to <code>TIMES</code>.</p>
  105 <p>The solution, as in the previous section, is to code the
  106 <code>TIMES</code> line in the form</p>
  107 <pre>
  108         TIMES 510-($-$$) DB 0
  109 </pre>
  110 <p>in which <code>$</code> and <code>$$</code> are offsets from the same
  111 section base, and so their difference is a pure number. This will solve the
  112 problem and generate sensible code.</p>
  113 </div>
  114 </body>
  115 </html>