⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nasmdoc3.htm

📁 nasm手册 大家可以看看 对要写汇编程序的帮助很大
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html><head><title>NASM Manual</title></head><body><h1 align=center>The Netwide Assembler: NASM</h1><p align=center><a href="nasmdoc4.html">Next Chapter</a> |<a href="nasmdoc2.html">Previous Chapter</a> |<a href="nasmdoc0.html">Contents</a> |<a href="nasmdoci.html">Index</a><h2><a name="chapter-3">Chapter 3: The NASM Language</a></h2><h3><a name="section-3.1">3.1 Layout of a NASM Source Line</a></h3><p>Like most assemblers, each NASM source line contains (unless it is amacro, a preprocessor directive or an assembler directive: see<a href="nasmdoc4.html">chapter 4</a> and <a href="nasmdoc5.html">chapter5</a>) some combination of the four fields<p><pre>label:    instruction operands        ; comment</pre><p>As usual, most of these fields are optional; the presence or absence ofany combination of a label, an instruction and a comment is allowed. Ofcourse, the operand field is either required or forbidden by the presenceand nature of the instruction field.<p>NASM uses backslash (\) as the line continuation character; if a lineends with backslash, the next line is considered to be a part of thebackslash-ended line.<p>NASM places no restrictions on white space within a line: labels mayhave white space before them, or instructions may have no space beforethem, or anything. The colon after a label is also optional. (Note thatthis means that if you intend to code <code><nobr>lodsb</nobr></code> aloneon a line, and type <code><nobr>lodab</nobr></code> by accident, thenthat's still a valid source line which does nothing but define a label.Running NASM with the command-line option<code><nobr>-w+orphan-labels</nobr></code> will cause it to warn you if youdefine a label alone on a line without a trailing colon.)<p>Valid characters in labels are letters, numbers,<code><nobr>_</nobr></code>, <code><nobr>$</nobr></code>,<code><nobr>#</nobr></code>, <code><nobr>@</nobr></code>,<code><nobr>~</nobr></code>, <code><nobr>.</nobr></code>, and<code><nobr>?</nobr></code>. The only characters which may be used as the<em>first</em> character of an identifier are letters,<code><nobr>.</nobr></code> (with special meaning: see<a href="#section-3.9">section 3.9</a>), <code><nobr>_</nobr></code> and<code><nobr>?</nobr></code>. An identifier may also be prefixed with a<code><nobr>$</nobr></code> to indicate that it is intended to be read asan identifier and not a reserved word; thus, if some other module you arelinking with defines a symbol called <code><nobr>eax</nobr></code>, you canrefer to <code><nobr>$eax</nobr></code> in NASM code to distinguish thesymbol from the register.<p>The instruction field may contain any machine instruction: Pentium andP6 instructions, FPU instructions, MMX instructions and even undocumentedinstructions are all supported. The instruction may be prefixed by<code><nobr>LOCK</nobr></code>, <code><nobr>REP</nobr></code>,<code><nobr>REPE</nobr></code>/<code><nobr>REPZ</nobr></code> or<code><nobr>REPNE</nobr></code>/<code><nobr>REPNZ</nobr></code>, in theusual way. Explicit address-size and operand-size prefixes<code><nobr>A16</nobr></code>, <code><nobr>A32</nobr></code>,<code><nobr>O16</nobr></code> and <code><nobr>O32</nobr></code> areprovided - one example of their use is given in<a href="nasmdoc9.html">chapter 9</a>. You can also use the name of asegment register as an instruction prefix: coding<code><nobr>es mov [bx],ax</nobr></code> is equivalent to coding<code><nobr>mov [es:bx],ax</nobr></code>. We recommend the latter syntax,since it is consistent with other syntactic features of the language, butfor instructions such as <code><nobr>LODSB</nobr></code>, which has nooperands and yet can require a segment override, there is no cleansyntactic way to proceed apart from <code><nobr>es lodsb</nobr></code>.<p>An instruction is not required to use a prefix: prefixes such as<code><nobr>CS</nobr></code>, <code><nobr>A32</nobr></code>,<code><nobr>LOCK</nobr></code> or <code><nobr>REPE</nobr></code> can appearon a line by themselves, and NASM will just generate the prefix bytes.<p>In addition to actual machine instructions, NASM also supports a numberof pseudo-instructions, described in <a href="#section-3.2">section3.2</a>.<p>Instruction operands may take a number of forms: they can be registers,described simply by the register name (e.g. <code><nobr>ax</nobr></code>,<code><nobr>bp</nobr></code>, <code><nobr>ebx</nobr></code>,<code><nobr>cr0</nobr></code>: NASM does not use the<code><nobr>gas</nobr></code>-style syntax in which register names must beprefixed by a <code><nobr>%</nobr></code> sign), or they can be effectiveaddresses (see <a href="#section-3.3">section 3.3</a>), constants(<a href="#section-3.4">section 3.4</a>) or expressions(<a href="#section-3.5">section 3.5</a>).<p>For floating-point instructions, NASM accepts a wide range of syntaxes:you can use two-operand forms like MASM supports, or you can use NASM'snative single-operand forms in most cases. Details of all forms of eachsupported instruction are given in <a href="nasmdocb.html">appendix B</a>.For example, you can code:<p><pre>        fadd    st1             ; this sets st0 := st0 + st1         fadd    st0,st1         ; so does this         fadd    st1,st0         ; this sets st1 := st1 + st0         fadd    to st1          ; so does this</pre><p>Almost any floating-point instruction that references memory must useone of the prefixes <code><nobr>DWORD</nobr></code>,<code><nobr>QWORD</nobr></code> or <code><nobr>TWORD</nobr></code> toindicate what size of memory operand it refers to.<h3><a name="section-3.2">3.2 Pseudo-Instructions</a></h3><p>Pseudo-instructions are things which, though not real x86 machineinstructions, are used in the instruction field anyway because that's themost convenient place to put them. The current pseudo-instructions are<code><nobr>DB</nobr></code>, <code><nobr>DW</nobr></code>,<code><nobr>DD</nobr></code>, <code><nobr>DQ</nobr></code> and<code><nobr>DT</nobr></code>, their uninitialised counterparts<code><nobr>RESB</nobr></code>, <code><nobr>RESW</nobr></code>,<code><nobr>RESD</nobr></code>, <code><nobr>RESQ</nobr></code> and<code><nobr>REST</nobr></code>, the <code><nobr>INCBIN</nobr></code>command, the <code><nobr>EQU</nobr></code> command, and the<code><nobr>TIMES</nobr></code> prefix.<h4><a name="section-3.2.1">3.2.1 <code><nobr>DB</nobr></code> and friends: Declaring Initialised Data</a></h4><p><code><nobr>DB</nobr></code>, <code><nobr>DW</nobr></code>,<code><nobr>DD</nobr></code>, <code><nobr>DQ</nobr></code> and<code><nobr>DT</nobr></code> are used, much as in MASM, to declareinitialised data in the output file. They can be invoked in a wide range ofways:<p><pre>      db    0x55                ; just the byte 0x55       db    0x55,0x56,0x57      ; three bytes in succession       db    'a',0x55            ; character constants are OK       db    'hello',13,10,'$'   ; so are string constants       dw    0x1234              ; 0x34 0x12       dw    'a'                 ; 0x61 0x00 (it's just a number)       dw    'ab'                ; 0x61 0x62 (character constant)       dw    'abc'               ; 0x61 0x62 0x63 0x00 (string)       dd    0x12345678          ; 0x78 0x56 0x34 0x12       dd    1.234567e20         ; floating-point constant       dq    1.234567e20         ; double-precision float       dt    1.234567e20         ; extended-precision float</pre><p><code><nobr>DQ</nobr></code> and <code><nobr>DT</nobr></code> do notaccept numeric constants or string constants as operands.<h4><a name="section-3.2.2">3.2.2 <code><nobr>RESB</nobr></code> and friends: Declaring Uninitialised Data</a></h4><p><code><nobr>RESB</nobr></code>, <code><nobr>RESW</nobr></code>,<code><nobr>RESD</nobr></code>, <code><nobr>RESQ</nobr></code> and<code><nobr>REST</nobr></code> are designed to be used in the BSS sectionof a module: they declare <em>uninitialised</em> storage space. Each takesa single operand, which is the number of bytes, words, doublewords orwhatever to reserve. As stated in<a href="nasmdoc2.html#section-2.2.7">section 2.2.7</a>, NASM does notsupport the MASM/TASM syntax of reserving uninitialised space by writing<code><nobr>DW ?</nobr></code> or similar things: this is what it doesinstead. The operand to a <code><nobr>RESB</nobr></code>-typepseudo-instruction is a <em>critical expression</em>: see<a href="#section-3.8">section 3.8</a>.<p>For example:<p><pre>buffer:         resb    64              ; reserve 64 bytes wordvar:        resw    1               ; reserve a word realarray       resq    10              ; array of ten reals</pre><h4><a name="section-3.2.3">3.2.3 <code><nobr>INCBIN</nobr></code>: Including External Binary Files</a></h4><p><code><nobr>INCBIN</nobr></code> is borrowed from the old Amigaassembler DevPac: it includes a binary file verbatim into the output file.This can be handy for (for example) including graphics and sound datadirectly into a game executable file. It can be called in one of thesethree ways:<p><pre>    incbin  "file.dat"             ; include the whole file     incbin  "file.dat",1024        ; skip the first 1024 bytes     incbin  "file.dat",1024,512    ; skip the first 1024, and                                    ; actually include at most 512</pre><h4><a name="section-3.2.4">3.2.4 <code><nobr>EQU</nobr></code>: Defining Constants</a></h4><p><code><nobr>EQU</nobr></code> defines a symbol to a given constantvalue: when <code><nobr>EQU</nobr></code> is used, the source line mustcontain a label. The action of <code><nobr>EQU</nobr></code> is to definethe given label name to the value of its (only) operand. This definition isabsolute, and cannot change later. So, for example,<p><pre>message         db      'hello, world' msglen          equ     $-message</pre><p>defines <code><nobr>msglen</nobr></code> to be the constant 12.<code><nobr>msglen</nobr></code> may not then be redefined later. This isnot a preprocessor definition either: the value of<code><nobr>msglen</nobr></code> is evaluated <em>once</em>, using thevalue of <code><nobr>$</nobr></code> (see <a href="#section-3.5">section3.5</a> for an explanation of <code><nobr>$</nobr></code>) at the point ofdefinition, rather than being evaluated wherever it is referenced and usingthe value of <code><nobr>$</nobr></code> at the point of reference. Notethat the operand to an <code><nobr>EQU</nobr></code> is also a criticalexpression (<a href="#section-3.8">section 3.8</a>).<h4><a name="section-3.2.5">3.2.5 <code><nobr>TIMES</nobr></code>: Repeating Instructions or Data</a></h4><p>The <code><nobr>TIMES</nobr></code> prefix causes the instruction to beassembled multiple times. This is partly present as NASM's equivalent ofthe <code><nobr>DUP</nobr></code> syntax supported by MASM-compatibleassemblers, in that you can code<p><pre>zerobuf:        times 64 db 0</pre><p>or similar things; but <code><nobr>TIMES</nobr></code> is more versatilethan that. The argument to <code><nobr>TIMES</nobr></code> is not just anumeric constant, but a numeric <em>expression</em>, so you can do thingslike<p><pre>buffer: db      'hello, world'         times 64-$+buffer db ' '</pre><p>which will store exactly enough spaces to make the total length of<code><nobr>buffer</nobr></code> up to 64. Finally,<code><nobr>TIMES</nobr></code> can be applied to ordinary instructions, soyou can code trivial unrolled loops in it:<p><pre>        times 100 movsb</pre><p>Note that there is no effective difference between<code><nobr>times 100 resb 1</nobr></code> and<code><nobr>resb 100</nobr></code>, except that the latter will beassembled about 100 times faster due to the internal structure of theassembler.<p>The operand to <code><nobr>TIMES</nobr></code>, like that of<code><nobr>EQU</nobr></code> and those of <code><nobr>RESB</nobr></code>and friends, is a critical expression (<a href="#section-3.8">section3.8</a>).<p>Note also that <code><nobr>TIMES</nobr></code> can't be applied tomacros: the reason for this is that <code><nobr>TIMES</nobr></code> isprocessed after the macro phase, which allows the argument to<code><nobr>TIMES</nobr></code> to contain expressions such as

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -