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

📄 nasmdoc5.htm

📁 nasm手册 大家可以看看 对要写汇编程序的帮助很大
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>NASM Manual</title></head><body><h1 align=center>The Netwide Assembler: NASM</h1><p align=center><a href="nasmdoc6.html">Next Chapter</a> |<a href="nasmdoc4.html">Previous Chapter</a> |<a href="nasmdoc0.html">Contents</a> |<a href="nasmdoci.html">Index</a><h2><a name="chapter-5">Chapter 5: Assembler Directives</a></h2><p>NASM, though it attempts to avoid the bureaucracy of assemblers likeMASM and TASM, is nevertheless forced to support a <em>few</em> directives.These are described in this chapter.<p>NASM's directives come in two types: <em>user-level</em> directives and<em>primitive</em> directives. Typically, each directive has a user-levelform and a primitive form. In almost all cases, we recommend that users usethe user-level forms of the directives, which are implemented as macroswhich call the primitive forms.<p>Primitive directives are enclosed in square brackets; user-leveldirectives are not.<p>In addition to the universal directives described in this chapter, eachobject file format can optionally supply extra directives in order tocontrol particular features of that file format. These<em>format-specific</em> directives are documented along with the formatsthat implement them, in <a href="nasmdoc6.html">chapter 6</a>.<h3><a name="section-5.1">5.1 <code><nobr>BITS</nobr></code>: Specifying Target Processor Mode</a></h3><p>The <code><nobr>BITS</nobr></code> directive specifies whether NASMshould generate code designed to run on a processor operating in 16-bitmode, or code designed to run on a processor operating in 32-bit mode. Thesyntax is <code><nobr>BITS 16</nobr></code> or<code><nobr>BITS 32</nobr></code>.<p>In most cases, you should not need to use <code><nobr>BITS</nobr></code>explicitly. The <code><nobr>aout</nobr></code>,<code><nobr>coff</nobr></code>, <code><nobr>elf</nobr></code> and<code><nobr>win32</nobr></code> object formats, which are designed for usein 32-bit operating systems, all cause NASM to select 32-bit mode bydefault. The <code><nobr>obj</nobr></code> object format allows you tospecify each segment you define as either <code><nobr>USE16</nobr></code>or <code><nobr>USE32</nobr></code>, and NASM will set its operating modeaccordingly, so the use of the <code><nobr>BITS</nobr></code> directive isonce again unnecessary.<p>The most likely reason for using the <code><nobr>BITS</nobr></code>directive is to write 32-bit code in a flat binary file; this is becausethe <code><nobr>bin</nobr></code> output format defaults to 16-bit mode inanticipation of it being used most frequently to write DOS<code><nobr>.COM</nobr></code> programs, DOS <code><nobr>.SYS</nobr></code>device drivers and boot loader software.<p>You do <em>not</em> need to specify <code><nobr>BITS 32</nobr></code>merely in order to use 32-bit instructions in a 16-bit DOS program; if youdo, the assembler will generate incorrect code because it will be writingcode targeted at a 32-bit platform, to be run on a 16-bit one.<p>When NASM is in <code><nobr>BITS 16</nobr></code> state, instructionswhich use 32-bit data are prefixed with an 0x66 byte, and those referringto 32-bit addresses have an 0x67 prefix. In<code><nobr>BITS 32</nobr></code> state, the reverse is true: 32-bitinstructions require no prefixes, whereas instructions using 16-bit dataneed an 0x66 and those working on 16-bit addresses need an 0x67.<p>The <code><nobr>BITS</nobr></code> directive has an exactly equivalentprimitive form, <code><nobr>[BITS 16]</nobr></code> and<code><nobr>[BITS 32]</nobr></code>. The user-level form is a macro whichhas no function other than to call the primitive form.<p>Note that the space is neccessary, <code><nobr>BITS32</nobr></code> will<em>not</em> work!<h4><a name="section-5.1.1">5.1.1 <code><nobr>USE16</nobr></code> &amp; <code><nobr>USE32</nobr></code>: Aliases for BITS</a></h4><p>The `<code><nobr>USE16</nobr></code>' and`<code><nobr>USE32</nobr></code>' directives can be used in place of`<code><nobr>BITS 16</nobr></code>' and`<code><nobr>BITS 32</nobr></code>', for compatibility with otherassemblers.<h3><a name="section-5.2">5.2 <code><nobr>SECTION</nobr></code> or <code><nobr>SEGMENT</nobr></code>: Changing and Defining Sections</a></h3><p>The <code><nobr>SECTION</nobr></code> directive(<code><nobr>SEGMENT</nobr></code> is an exactly equivalent synonym)changes which section of the output file the code you write will beassembled into. In some object file formats, the number and names ofsections are fixed; in others, the user may make up as many as they wish.Hence <code><nobr>SECTION</nobr></code> may sometimes give an errormessage, or may define a new section, if you try to switch to a sectionthat does not (yet) exist.<p>The Unix object formats, and the <code><nobr>bin</nobr></code> objectformat (but see <a href="nasmdoc6.html#section-6.1.3">section 6.1.3</a>,all support the standardised section names <code><nobr>.text</nobr></code>,<code><nobr>.data</nobr></code> and <code><nobr>.bss</nobr></code> for thecode, data and uninitialised-data sections. The<code><nobr>obj</nobr></code> format, by contrast, does not recognise thesesection names as being special, and indeed will strip off the leadingperiod of any section name that has one.<h4><a name="section-5.2.1">5.2.1 The <code><nobr>__SECT__</nobr></code> Macro</a></h4><p>The <code><nobr>SECTION</nobr></code> directive is unusual in that itsuser-level form functions differently from its primitive form. Theprimitive form, <code><nobr>[SECTION xyz]</nobr></code>, simply switchesthe current target section to the one given. The user-level form,<code><nobr>SECTION xyz</nobr></code>, however, first defines thesingle-line macro <code><nobr>__SECT__</nobr></code> to be the primitive<code><nobr>[SECTION]</nobr></code> directive which it is about to issue,and then issues it. So the user-level directive<p><pre>        SECTION .text</pre><p>expands to the two lines<p><pre>%define __SECT__        [SECTION .text]         [SECTION .text]</pre><p>Users may find it useful to make use of this in their own macros. Forexample, the <code><nobr>writefile</nobr></code> macro defined in<a href="nasmdoc4.html#section-4.3.3">section 4.3.3</a> can be usefullyrewritten in the following more sophisticated form:<p><pre>%macro  writefile 2+         [section .data]   %%str:        db      %2   %%endstr:         __SECT__         mov     dx,%%str         mov     cx,%%endstr-%%str         mov     bx,%1         mov     ah,0x40         int     0x21 %endmacro</pre><p>This form of the macro, once passed a string to output, first switchestemporarily to the data section of the file, using the primitive form ofthe <code><nobr>SECTION</nobr></code> directive so as not to modify<code><nobr>__SECT__</nobr></code>. It then declares its string in the datasection, and then invokes <code><nobr>__SECT__</nobr></code> to switch backto <em>whichever</em> section the user was previously working in. It thusavoids the need, in the previous version of the macro, to include a<code><nobr>JMP</nobr></code> instruction to jump over the data, and alsodoes not fail if, in a complicated <code><nobr>OBJ</nobr></code> formatmodule, the user could potentially be assembling the code in any of severalseparate code sections.<h3><a name="section-5.3">5.3 <code><nobr>ABSOLUTE</nobr></code>: Defining Absolute Labels</a></h3><p>The <code><nobr>ABSOLUTE</nobr></code> directive can be thought of as analternative form of <code><nobr>SECTION</nobr></code>: it causes thesubsequent code to be directed at no physical section, but at thehypothetical section starting at the given absolute address. The onlyinstructions you can use in this mode are the<code><nobr>RESB</nobr></code> family.<p><code><nobr>ABSOLUTE</nobr></code> is used as follows:<p><pre>absolute 0x1A     kbuf_chr    resw    1     kbuf_free   resw    1     kbuf        resw    16</pre><p>This example describes a section of the PC BIOS data area, at segmentaddress 0x40: the above code defines <code><nobr>kbuf_chr</nobr></code> tobe 0x1A, <code><nobr>kbuf_free</nobr></code> to be 0x1C, and<code><nobr>kbuf</nobr></code> to be 0x1E.<p>The user-level form of <code><nobr>ABSOLUTE</nobr></code>, like that of<code><nobr>SECTION</nobr></code>, redefines the

⌨️ 快捷键说明

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