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

📄 nasmdoc6.htm

📁 nasm手册 大家可以看看 对要写汇编程序的帮助很大
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<html><head><title>NASM Manual</title></head><body><h1 align=center>The Netwide Assembler: NASM</h1><p align=center><a href="nasmdoc7.html">Next Chapter</a> |<a href="nasmdoc5.html">Previous Chapter</a> |<a href="nasmdoc0.html">Contents</a> |<a href="nasmdoci.html">Index</a><h2><a name="chapter-6">Chapter 6: Output Formats</a></h2><p>NASM is a portable assembler, designed to be able to compile on any ANSIC-supporting platform and produce output to run on a variety of Intel x86operating systems. For this reason, it has a large number of availableoutput formats, selected using the <code><nobr>-f</nobr></code> option onthe NASM command line. Each of these formats, along with its extensions tothe base NASM syntax, is detailed in this chapter.<p>As stated in <a href="nasmdoc2.html#section-2.1.1">section 2.1.1</a>,NASM chooses a default name for your output file based on the input filename and the chosen output format. This will be generated by removing theextension (<code><nobr>.asm</nobr></code>, <code><nobr>.s</nobr></code>, orwhatever you like to use) from the input file name, and substituting anextension defined by the output format. The extensions are given with eachformat below.<h3><a name="section-6.1">6.1 <code><nobr>bin</nobr></code>: Flat-Form Binary Output</a></h3><p>The <code><nobr>bin</nobr></code> format does not produce object files:it generates nothing in the output file except the code you wrote. Such`pure binary' files are used by MS-DOS: <code><nobr>.COM</nobr></code>executables and <code><nobr>.SYS</nobr></code> device drivers are purebinary files. Pure binary output is also useful for operating system andboot loader development.<p>The <code><nobr>bin</nobr></code> format supports multiple sectionnames. For details of how nasm handles sections in the<code><nobr>bin</nobr></code> format, see <a href="#section-6.1.3">section6.1.3</a>.<p>Using the <code><nobr>bin</nobr></code> format puts NASM by default into16-bit mode (see <a href="nasmdoc5.html#section-5.1">section 5.1</a>). Inorder to use <code><nobr>bin</nobr></code> to write 32-bit code such as anOS kernel, you need to explicitly issue the<code><nobr>BITS 32</nobr></code> directive.<p><code><nobr>bin</nobr></code> has no default output file name extension:instead, it leaves your file name as it is once the original extension hasbeen removed. Thus, the default is for NASM to assemble<code><nobr>binprog.asm</nobr></code> into a binary file called<code><nobr>binprog</nobr></code>.<h4><a name="section-6.1.1">6.1.1 <code><nobr>ORG</nobr></code>: Binary File Program Origin</a></h4><p>The <code><nobr>bin</nobr></code> format provides an additionaldirective to the list given in <a href="nasmdoc5.html">chapter 5</a>:<code><nobr>ORG</nobr></code>. The function of the<code><nobr>ORG</nobr></code> directive is to specify the origin addresswhich NASM will assume the program begins at when it is loaded into memory.<p>For example, the following code will generate the longword<code><nobr>0x00000104</nobr></code>:<p><pre>        org     0x100         dd      label label:</pre><p>Unlike the <code><nobr>ORG</nobr></code> directive provided byMASM-compatible assemblers, which allows you to jump around in the objectfile and overwrite code you have already generated, NASM's<code><nobr>ORG</nobr></code> does exactly what the directive says:<em>origin</em>. Its sole function is to specify one offset which is addedto all internal address references within the section; it does not permitany of the trickery that MASM's version does. See<a href="nasmdo10.html#section-10.1.3">section 10.1.3</a> for furthercomments.<h4><a name="section-6.1.2">6.1.2 <code><nobr>bin</nobr></code> Extensions to the <code><nobr>SECTION</nobr></code> Directive</a></h4><p>The <code><nobr>bin</nobr></code> output format extends the<code><nobr>SECTION</nobr></code> (or <code><nobr>SEGMENT</nobr></code>)directive to allow you to specify the alignment requirements of segments.This is done by appending the <code><nobr>ALIGN</nobr></code> qualifier tothe end of the section-definition line. For example,<p><pre>section .data   align=16</pre><p>switches to the section <code><nobr>.data</nobr></code> and alsospecifies that it must be aligned on a 16-byte boundary.<p>The parameter to <code><nobr>ALIGN</nobr></code> specifies how many lowbits of the section start address must be forced to zero. The alignmentvalue given may be any power of two.<h4><a name="section-6.1.3">6.1.3 <code><nobr>Multisection</nobr></code> support for the BIN format.</a></h4><p>The <code><nobr>bin</nobr></code> format allows the use of multiplesections, of arbitrary names, besides the "known"<code><nobr>.text</nobr></code>, <code><nobr>.data</nobr></code>, and<code><nobr>.bss</nobr></code> names.<ul><li>Sections may be designated <code><nobr>progbits</nobr></code> or<code><nobr>nobits</nobr></code>. Default is<code><nobr>progbits</nobr></code> (except <code><nobr>.bss</nobr></code>,which defaults to <code><nobr>nobits</nobr></code>, of course).<li>Sections can be aligned at a specified boundary following the previoussection with <code><nobr>align=</nobr></code>, or at an arbitrarybyte-granular position with <code><nobr>start=</nobr></code>.<li>Sections can be given a virtual start address, which will be used forthe calculation of all memory references within that section with<code><nobr>vstart=</nobr></code>.<li>Sections can be ordered using<code><nobr>follows=</nobr></code><code><nobr>&lt;section&gt;</nobr></code>or<code><nobr>vfollows=</nobr></code><code><nobr>&lt;section&gt;</nobr></code>as an alternative to specifying an explicit start address.<li>Arguments to <code><nobr>org</nobr></code>,<code><nobr>start</nobr></code>, <code><nobr>vstart</nobr></code>, and<code><nobr>align=</nobr></code> are critical expressions. See<a href="nasmdoc3.html#section-3.8">section 3.8</a>. E.g.<code><nobr>align=(1 &lt;&lt; ALIGN_SHIFT)</nobr></code> -<code><nobr>ALIGN_SHIFT</nobr></code> must be defined before it is usedhere.<li>Any code which comes before an explicit<code><nobr>SECTION</nobr></code> directive is directed by default into the<code><nobr>.text</nobr></code> section.<li>If an <code><nobr>ORG</nobr></code> statement is not given,<code><nobr>ORG 0</nobr></code> is used by default.<li>The <code><nobr>.bss</nobr></code> section will be placed after thelast <code><nobr>progbits</nobr></code> section, unless<code><nobr>start=</nobr></code>, <code><nobr>vstart=</nobr></code>,<code><nobr>follows=</nobr></code>, or <code><nobr>vfollows=</nobr></code>has been specified.<li>All sections are aligned on dword boundaries, unless a differentalignment has been specified.<li>Sections may not overlap.<li>Nasm creates the<code><nobr>section.&lt;secname&gt;.start</nobr></code> for each section,which may be used in your code.</ul><h4><a name="section-6.1.4">6.1.4 Map files</a></h4><p>Map files can be generated in <code><nobr>-f bin</nobr></code> format bymeans of the <code><nobr>[map]</nobr></code> option. Map types of<code><nobr>all</nobr></code> (default), <code><nobr>brief</nobr></code>,<code><nobr>sections</nobr></code>, <code><nobr>segments</nobr></code>, or<code><nobr>symbols</nobr></code> may be specified. Output may be directedto <code><nobr>stdout</nobr></code> (default),<code><nobr>stderr</nobr></code>, or a specified file. E.g.<code><nobr>[map symbols myfile.map]</nobr></code>. No "user form" exists,the square brackets must be used.<h3><a name="section-6.2">6.2 <code><nobr>obj</nobr></code>: Microsoft OMF Object Files</a></h3><p>The <code><nobr>obj</nobr></code> file format (NASM calls it<code><nobr>obj</nobr></code> rather than <code><nobr>omf</nobr></code> forhistorical reasons) is the one produced by MASM and TASM, which istypically fed to 16-bit DOS linkers to produce<code><nobr>.EXE</nobr></code> files. It is also the format used by OS/2.<p><code><nobr>obj</nobr></code> provides a default output file-nameextension of <code><nobr>.obj</nobr></code>.<p><code><nobr>obj</nobr></code> is not exclusively a 16-bit format,though: NASM has full support for the 32-bit extensions to the format. Inparticular, 32-bit <code><nobr>obj</nobr></code> format files are used byBorland's Win32 compilers, instead of using Microsoft's newer<code><nobr>win32</nobr></code> object file format.<p>The <code><nobr>obj</nobr></code> format does not define any specialsegment names: you can call your segments anything you like. Typical namesfor segments in <code><nobr>obj</nobr></code> format files are<code><nobr>CODE</nobr></code>, <code><nobr>DATA</nobr></code> and<code><nobr>BSS</nobr></code>.<p>If your source file contains code before specifying an explicit<code><nobr>SEGMENT</nobr></code> directive, then NASM will invent its ownsegment called <code><nobr>__NASMDEFSEG</nobr></code> for you.<p>When you define a segment in an <code><nobr>obj</nobr></code> file, NASMdefines the segment name as a symbol as well, so that you can access thesegment address of the segment. So, for example:<p><pre>segment data dvar:   dw      1234 segment code function:         mov     ax,data         ; get segment address of data         mov     ds,ax           ; and move it into DS         inc     word [dvar]     ; now this reference will work         ret</pre><p>The <code><nobr>obj</nobr></code> format also enables the use of the<code><nobr>SEG</nobr></code> and <code><nobr>WRT</nobr></code> operators,so that you can write code which does things like<p><pre>extern  foo       mov   ax,seg foo            ; get preferred segment of foo       mov   ds,ax       mov   ax,data               ; a different segment       mov   es,ax       mov   ax,[ds:foo]           ; this accesses `foo'       mov   [es:foo wrt data],bx  ; so does this</pre><h4><a name="section-6.2.1">6.2.1 <code><nobr>obj</nobr></code> Extensions to the <code><nobr>SEGMENT</nobr></code> Directive</a></h4><p>The <code><nobr>obj</nobr></code> output format extends the<code><nobr>SEGMENT</nobr></code> (or <code><nobr>SECTION</nobr></code>)directive to allow you to specify various properties of the segment you aredefining. This is done by appending extra qualifiers to the end of thesegment-definition line. For example,<p><pre>segment code private align=16</pre><p>defines the segment <code><nobr>code</nobr></code>, but also declares itto be a private segment, and requires that the portion of it described inthis code module must be aligned on a 16-byte boundary.<p>The available qualifiers are:<ul><li><code><nobr>PRIVATE</nobr></code>, <code><nobr>PUBLIC</nobr></code>,<code><nobr>COMMON</nobr></code> and <code><nobr>STACK</nobr></code>specify the combination characteristics of the segment.<code><nobr>PRIVATE</nobr></code> segments do not get combined with anyothers by the linker; <code><nobr>PUBLIC</nobr></code> and<code><nobr>STACK</nobr></code> segments get concatenated together at linktime; and <code><nobr>COMMON</nobr></code> segments all get overlaid on topof each other rather than stuck end-to-end.<li><code><nobr>ALIGN</nobr></code> is used, as shown above, to specify howmany low bits of the segment start address must be forced to zero. Thealignment value given may be any power of two from 1 to 4096; in reality,the only values supported are 1, 2, 4, 16, 256 and 4096, so if 8 isspecified it will be rounded up to 16, and 32, 64 and 128 will all be

⌨️ 快捷键说明

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