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

📄 nasmdoc7.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="nasmdoc8.html">Next Chapter</a> |<a href="nasmdoc6.html">Previous Chapter</a> |<a href="nasmdoc0.html">Contents</a> |<a href="nasmdoci.html">Index</a><h2><a name="chapter-7">Chapter 7: Writing 16-bit Code (DOS, Windows 3/3.1)</a></h2><p>This chapter attempts to cover some of the common issues encounteredwhen writing 16-bit code to run under <code><nobr>MS-DOS</nobr></code> or<code><nobr>Windows 3.x</nobr></code>. It covers how to link programs toproduce <code><nobr>.EXE</nobr></code> or <code><nobr>.COM</nobr></code>files, how to write <code><nobr>.SYS</nobr></code> device drivers, and howto interface assembly language code with 16-bit C compilers and withBorland Pascal.<h3><a name="section-7.1">7.1 Producing <code><nobr>.EXE</nobr></code> Files</a></h3><p>Any large program written under DOS needs to be built as a<code><nobr>.EXE</nobr></code> file: only <code><nobr>.EXE</nobr></code>files have the necessary internal structure required to span more than one64K segment. Windows programs, also, have to be built as<code><nobr>.EXE</nobr></code> files, since Windows does not support the<code><nobr>.COM</nobr></code> format.<p>In general, you generate <code><nobr>.EXE</nobr></code> files by usingthe <code><nobr>obj</nobr></code> output format to produce one or more<code><nobr>.OBJ</nobr></code> files, and then linking them together usinga linker. However, NASM also supports the direct generation of simple DOS<code><nobr>.EXE</nobr></code> files using the<code><nobr>bin</nobr></code> output format (by using<code><nobr>DB</nobr></code> and <code><nobr>DW</nobr></code> to constructthe <code><nobr>.EXE</nobr></code> file header), and a macro package issupplied to do this. Thanks to Yann Guidon for contributing the code forthis.<p>NASM may also support <code><nobr>.EXE</nobr></code> natively as anotheroutput format in future releases.<h4><a name="section-7.1.1">7.1.1 Using the <code><nobr>obj</nobr></code> Format To Generate <code><nobr>.EXE</nobr></code> Files</a></h4><p>This section describes the usual method of generating<code><nobr>.EXE</nobr></code> files by linking<code><nobr>.OBJ</nobr></code> files together.<p>Most 16-bit programming language packages come with a suitable linker;if you have none of these, there is a free linker called VAL, available in<code><nobr>LZH</nobr></code> archive format from<a href="ftp://x2ftp.oulu.fi/pub/msdos/programming/lang/"><code><nobr>x2ftp.oulu.fi</nobr></code></a>.An LZH archiver can be found at<a href="ftp://ftp.simtel.net/pub/simtelnet/msdos/arcers"><code><nobr>ftp.simtel.net</nobr></code></a>.There is another `free' linker (though this one doesn't come with sources)called FREELINK, available from<a href="http://www.pcorner.com/tpc/old/3-101.html"><code><nobr>www.pcorner.com</nobr></code></a>.A third, <code><nobr>djlink</nobr></code>, written by DJ Delorie, isavailable at<a href="http://www.delorie.com/djgpp/16bit/djlink/"><code><nobr>www.delorie.com</nobr></code></a>.A fourth linker, <code><nobr>ALINK</nobr></code>, written by Anthony A.J.Williams, is available at<a href="http://alink.sourceforge.net"><code><nobr>alink.sourceforge.net</nobr></code></a>.<p>When linking several <code><nobr>.OBJ</nobr></code> files into a<code><nobr>.EXE</nobr></code> file, you should ensure that exactly one ofthem has a start point defined (using the <code><nobr>..start</nobr></code>special symbol defined by the <code><nobr>obj</nobr></code> format: see<a href="nasmdoc6.html#section-6.2.6">section 6.2.6</a>). If no moduledefines a start point, the linker will not know what value to give theentry-point field in the output file header; if more than one defines astart point, the linker will not know <em>which</em> value to use.<p>An example of a NASM source file which can be assembled to a<code><nobr>.OBJ</nobr></code> file and linked on its own to a<code><nobr>.EXE</nobr></code> is given here. It demonstrates the basicprinciples of defining a stack, initialising the segment registers, anddeclaring a start point. This file is also provided in the<code><nobr>test</nobr></code> subdirectory of the NASM archives, under thename <code><nobr>objexe.asm</nobr></code>.<p><pre>segment code ..start:         mov     ax,data         mov     ds,ax         mov     ax,stack         mov     ss,ax         mov     sp,stacktop</pre><p>This initial piece of code sets up <code><nobr>DS</nobr></code> to pointto the data segment, and initialises <code><nobr>SS</nobr></code> and<code><nobr>SP</nobr></code> to point to the top of the provided stack.Notice that interrupts are implicitly disabled for one instruction after amove into <code><nobr>SS</nobr></code>, precisely for this situation, sothat there's no chance of an interrupt occurring between the loads of<code><nobr>SS</nobr></code> and <code><nobr>SP</nobr></code> and nothaving a stack to execute on.<p>Note also that the special symbol <code><nobr>..start</nobr></code> isdefined at the beginning of this code, which means that will be the entrypoint into the resulting executable file.<p><pre>        mov     dx,hello         mov     ah,9         int     0x21</pre><p>The above is the main program: load <code><nobr>DS:DX</nobr></code> witha pointer to the greeting message (<code><nobr>hello</nobr></code> isimplicitly relative to the segment <code><nobr>data</nobr></code>, whichwas loaded into <code><nobr>DS</nobr></code> in the setup code, so the fullpointer is valid), and call the DOS print-string function.<p><pre>        mov     ax,0x4c00         int     0x21</pre><p>This terminates the program using another DOS system call.<p><pre>segment data hello:  db      'hello, world', 13, 10, '$'</pre><p>The data segment contains the string we want to display.<p><pre>segment stack stack         resb 64 stacktop:</pre><p>The above code declares a stack segment containing 64 bytes ofuninitialised stack space, and points <code><nobr>stacktop</nobr></code> atthe top of it. The directive <code><nobr>segment stack stack</nobr></code>defines a segment <em>called</em> <code><nobr>stack</nobr></code>, and alsoof <em>type</em> <code><nobr>STACK</nobr></code>. The latter is notnecessary to the correct running of the program, but linkers are likely toissue warnings or errors if your program has no segment of type<code><nobr>STACK</nobr></code>.<p>The above file, when assembled into a <code><nobr>.OBJ</nobr></code>file, will link on its own to a valid <code><nobr>.EXE</nobr></code> file,which when run will print `hello, world' and then exit.<h4><a name="section-7.1.2">7.1.2 Using the <code><nobr>bin</nobr></code> Format To Generate <code><nobr>.EXE</nobr></code> Files</a></h4><p>The <code><nobr>.EXE</nobr></code> file format is simple enough thatit's possible to build a <code><nobr>.EXE</nobr></code> file by writing apure-binary program and sticking a 32-byte header on the front. This headeris simple enough that it can be generated using<code><nobr>DB</nobr></code> and <code><nobr>DW</nobr></code> commands byNASM itself, so that you can use the <code><nobr>bin</nobr></code> outputformat to directly generate <code><nobr>.EXE</nobr></code> files.<p>Included in the NASM archives, in the <code><nobr>misc</nobr></code>subdirectory, is a file <code><nobr>exebin.mac</nobr></code> of macros. Itdefines three macros: <code><nobr>EXE_begin</nobr></code>,<code><nobr>EXE_stack</nobr></code> and <code><nobr>EXE_end</nobr></code>.<p>To produce a <code><nobr>.EXE</nobr></code> file using this method, youshould start by using <code><nobr>%include</nobr></code> to load the<code><nobr>exebin.mac</nobr></code> macro package into your source file.You should then issue the <code><nobr>EXE_begin</nobr></code> macro call(which takes no arguments) to generate the file header data. Then writecode as normal for the <code><nobr>bin</nobr></code> format - you can useall three standard sections <code><nobr>.text</nobr></code>,<code><nobr>.data</nobr></code> and <code><nobr>.bss</nobr></code>. At theend of the file you should call the <code><nobr>EXE_end</nobr></code> macro(again, no arguments), which defines some symbols to mark section sizes,and these symbols are referred to in the header code generated by<code><nobr>EXE_begin</nobr></code>.<p>In this model, the code you end up writing starts at<code><nobr>0x100</nobr></code>, just like a <code><nobr>.COM</nobr></code>file - in fact, if you strip off the 32-byte header from the resulting<code><nobr>.EXE</nobr></code> file, you will have a valid<code><nobr>.COM</nobr></code> program. All the segment bases are the same,so you are limited to a 64K program, again just like a<code><nobr>.COM</nobr></code> file. Note that an<code><nobr>ORG</nobr></code> directive is issued by the<code><nobr>EXE_begin</nobr></code> macro, so you should not explicitlyissue one of your own.<p>You can't directly refer to your segment base value, unfortunately,since this would require a relocation in the header, and things would get alot more complicated. So you should get your segment base by copying it outof <code><nobr>CS</nobr></code> instead.<p>On entry to your <code><nobr>.EXE</nobr></code> file,<code><nobr>SS:SP</nobr></code> are already set up to point to the top of a2Kb stack. You can adjust the default stack size of 2Kb by calling the<code><nobr>EXE_stack</nobr></code> macro. For example, to change the stacksize of your program to 64 bytes, you would call<code><nobr>EXE_stack 64</nobr></code>.<p>A sample program which generates a <code><nobr>.EXE</nobr></code> filein this way is given in the <code><nobr>test</nobr></code> subdirectory ofthe NASM archive, as <code><nobr>binexe.asm</nobr></code>.<h3><a name="section-7.2">7.2 Producing <code><nobr>.COM</nobr></code> Files</a></h3><p>While large DOS programs must be written as<code><nobr>.EXE</nobr></code> files, small ones are often better writtenas <code><nobr>.COM</nobr></code> files. <code><nobr>.COM</nobr></code>files are pure binary, and therefore most easily produced using the<code><nobr>bin</nobr></code> output format.<h4><a name="section-7.2.1">7.2.1 Using the <code><nobr>bin</nobr></code> Format To Generate <code><nobr>.COM</nobr></code> Files</a></h4><p><code><nobr>.COM</nobr></code> files expect to be loaded at offset<code><nobr>100h</nobr></code> into their segment (though the segment maychange). Execution then begins at <code><nobr>100h</nobr></code>, i.e.right at the start of the program. So to write a<code><nobr>.COM</nobr></code> program, you would create a source filelooking like<p><pre>        org 100h section .text start:         ; put your code here section .data         ; put data items here section .bss         ; put uninitialised data here</pre><p>The <code><nobr>bin</nobr></code> format puts the<code><nobr>.text</nobr></code> section first in the file, so you candeclare data or BSS items before beginning to write code if you want to andthe code will still end up at the front of the file where it belongs.<p>The BSS (uninitialised data) section does not take up space in the<code><nobr>.COM</nobr></code> file itself: instead, addresses of BSS itemsare resolved to point at space beyond the end of the file, on the groundsthat this will be free memory when the program is run. Therefore you shouldnot rely on your BSS being initialised to all zeros when you run.<p>To assemble the above program, you should use a command line like<p><pre>nasm myprog.asm -fbin -o myprog.com</pre><p>The <code><nobr>bin</nobr></code> format would produce a file called<code><nobr>myprog</nobr></code> if no explicit output file name werespecified, so you have to override it and give the desired file name.<h4><a name="section-7.2.2">7.2.2 Using the <code><nobr>obj</nobr></code> Format To Generate <code><nobr>.COM</nobr></code> Files</a></h4><p>If you are writing a <code><nobr>.COM</nobr></code> program as more thanone module, you may wish to assemble several <code><nobr>.OBJ</nobr></code>files and link them together into a <code><nobr>.COM</nobr></code> program.You can do this, provided you have a linker capable of outputting<code><nobr>.COM</nobr></code> files directly (TLINK does this), oralternatively a converter program such as <code><nobr>EXE2BIN</nobr></code>to transform the <code><nobr>.EXE</nobr></code> file output from the linkerinto a <code><nobr>.COM</nobr></code> file.<p>If you do this, you need to take care of several things:<ul><li>The first object file containing code should start its code segmentwith a line like <code><nobr>RESB 100h</nobr></code>. This is to ensurethat the code begins at offset <code><nobr>100h</nobr></code> relative tothe beginning of the code segment, so that the linker or converter programdoes not have to adjust address references within the file when generatingthe <code><nobr>.COM</nobr></code> file. Other assemblers use an<code><nobr>ORG</nobr></code> directive for this purpose, but<code><nobr>ORG</nobr></code> in NASM is a format-specific directive to the<code><nobr>bin</nobr></code> output format, and does not mean the samething as it does in MASM-compatible assemblers.<li>You don't need to define a stack segment.<li>All your segments should be in the same group, so that every time yourcode or data references a symbol offset, all offsets are relative to thesame segment base. This is because, when a <code><nobr>.COM</nobr></code>file is loaded, all the segment registers contain the same value.</ul><h3><a name="section-7.3">7.3 Producing <code><nobr>.SYS</nobr></code> Files</a></h3><p>MS-DOS device drivers - <code><nobr>.SYS</nobr></code> files - are purebinary files, similar to <code><nobr>.COM</nobr></code> files, except thatthey start at origin zero rather than <code><nobr>100h</nobr></code>.Therefore, if you are writing a device driver using the<code><nobr>bin</nobr></code> format, you do not need the<code><nobr>ORG</nobr></code> directive, since the default origin for<code><nobr>bin</nobr></code> is zero. Similarly, if you are using<code><nobr>obj</nobr></code>, you do not need the<code><nobr>RESB 100h</nobr></code> at the start of your code segment.<p><code><nobr>.SYS</nobr></code> files start with a header structure,containing pointers to the various routines inside the driver which do thework. This structure should be defined at the start of the code segment,even though it is not actually code.<p>For more information on the format of <code><nobr>.SYS</nobr></code>files, and the data which has to go in the header structure, a list ofbooks is given in the Frequently Asked Questions list for the newsgroup<a href="news:comp.os.msdos.programmer"><code><nobr>comp.os.msdos.programmer</nobr></code></a>.<h3><a name="section-7.4">7.4 Interfacing to 16-bit C Programs</a></h3><p>This section covers the basics of writing assembly routines that call,or are called from, C programs. To do this, you would typically write anassembly module as a <code><nobr>.OBJ</nobr></code> file, and link it withyour C modules to produce a mixed-language program.<h4><a name="section-7.4.1">7.4.1 External Symbol Names</a></h4><p>C compilers have the convention that the names of all global symbols

⌨️ 快捷键说明

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