Assembly HOWTO
Assembly HOWTO
Konstantin Boldyshev
Linux Assembly <[email protected]>
FrancoisRene Rideau
Tunes project <[email protected]> 0.6g Edition Version 0.6g Copyright 19992006 Konstantin Boldyshev Copyright 19961999 FrancoisRene Rideau $Date: 2006/02/11 08:26:26 $
This is the Linux Assembly HOWTO, version 0.6g. This document describes how to program in assembly language using free programming tools, focusing on development for or from the Linux Operating System, mostly on IA32 (i386) platform. Included material may or may not be applicable to other hardware and/or software platforms.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1; with no Invariant Sections, with no FrontCover Texts, and no BackCover texts.
Table of Contents
Chapter 1. Introduction ......................................................................................................................................1 1.1. Legal Blurb.......................................................................................................................................1 1.2. Foreword....................................................................................................................................1 Chapter 2. Do you need assembly?....................................................................................................................3 2.1. Pros and Cons...................................................................................................................................3 2.1.1. The advantages of Assembly ...................................................................................................3 2.1.2. The disadvantages of Assembly..............................................................................................3 2.1.3. Assessment..............................................................................................................................4 2.2. How to NOT use Assembly..............................................................................................................4 2.2.1. General procedure to achieve efficient code...........................................................................4 2.2.2. Languages with optimizing compilers....................................................................................5 2.2.3. General procedure to speed your code up...............................................................................5 2.2.4. Inspecting compilergenerated code.......................................................................................5 2.3. Linux and assembly..........................................................................................................................6 Chapter 3. Assemblers........................................................................................................................................7 3.1. GCC Inline Assembly.......................................................................................................................7 3.1.1. Where to find GCC ..................................................................................................................7 3.1.2. Where to find docs for GCC Inline Asm .................................................................................7 3.1.3. Invoking GCC to build proper inline assembly code..............................................................7 3.1.4. Macro support.........................................................................................................................8 . 3.2. GAS..................................................................................................................................................9 3.2.1. Where to find it.......................................................................................................................9 . 3.2.2. What is this AT&T syntax .......................................................................................................9 3.2.3. Intel syntax............................................................................................................................10 3.2.4. 16bit mode ...........................................................................................................................10 3.2.5. Macro support.......................................................................................................................10 . 3.3. NASM.............................................................................................................................................11 3.3.1. Where to find NASM............................................................................................................11 3.3.2. What it does...........................................................................................................................11 3.4. Other Assemblers............................................................................................................................11 3.4.1. AS86......................................................................................................................................11 3.4.2. YASM...................................................................................................................................12 3.4.3. FASM....................................................................................................................................12 3.4.4. OSIMPA (SHASM)..............................................................................................................12 3.4.5. AASM...................................................................................................................................13 3.4.6. TDASM.................................................................................................................................13 3.4.7. HLA.......................................................................................................................................13 3.4.8. TALC .....................................................................................................................................13 3.4.9. Free Pascal ............................................................................................................................14 . 3.4.10. Win32Forth assembler........................................................................................................14 3.4.11. Terse....................................................................................................................................14 3.4.12. Nonfree and/or Non32bit x86 assemblers......................................................................15 Chapter 4. Metaprogramming.........................................................................................................................16 4.1. External filters .................................................................................................................................16 4.1.1. CPP........................................................................................................................................16 i
Table of Contents
Chapter 4. Metaprogramming 4.1.2. M4.........................................................................................................................................16 4.1.3. Macroprocessing with your own filter..................................................................................17 4.2. Metaprogramming ...........................................................................................................................17 4.2.1. Backends from compilers......................................................................................................17 4.2.2. The NewJersey MachineCode Toolkit.............................................................................17 4.2.3. TUNES..................................................................................................................................17 Chapter 5. Calling conventions ........................................................................................................................18 5.1. Linux...............................................................................................................................................18 5.1.1. Linking to GCC.....................................................................................................................18 5.1.2. ELF vs a.out problems ...........................................................................................................18 5.1.3. Direct Linux syscalls.............................................................................................................18 5.1.4. Hardware I/O under Linux....................................................................................................20 5.1.5. Accessing 16bit drivers from Linux/i386...........................................................................21 5.2. DOS and Windows.........................................................................................................................21 5.3. Your own OS..................................................................................................................................22 Chapter 6. Quick start......................................................................................................................................23 6.1. Introduction.....................................................................................................................................23 6.1.1. Tools you need......................................................................................................................23 6.2. Hello, world!...................................................................................................................................23 6.2.1. Program layout......................................................................................................................23 6.2.2. NASM (hello.asm)................................................................................................................23 6.2.3. GAS (hello.S)........................................................................................................................24 6.3. Building an executable ....................................................................................................................24 6.3.1. Producing object code...........................................................................................................24 6.3.2. Producing executable............................................................................................................25 6.4. MIPS Example................................................................................................................................25 Chapter 7. Resources........................................................................................................................................27 7.1. Pointers...........................................................................................................................................27 7.2. Mailing list...............................................................................................................................27 Chapter 8. Frequently Asked Questions.........................................................................................................28 Appendix A. History.........................................................................................................................................34 Appendix B. Acknowledgements.....................................................................................................................37 Appendix C. Endorsements ..............................................................................................................................38 Appendix D. GNU Free Documentation License...........................................................................................39
ii
Chapter 1. Introduction
You can skip this chapter if you are familiar with HOWTOs, or just hate to read all this assemblyunrelated crap.
1.1. Legal Blurb
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License Version 1.1; with no Invariant Sections, with no FrontCover Texts, and no BackCover texts. A copy of the license is included in the GNU Free Documentation License appendix. The most recent official version of this document is available from the Linux Assembly and LDP sites. If you are reading a fewmonthsold copy, consider checking the above URLs for a new version.
1.2. Foreword
This document aims answering questions of those who program or want to program 32bit x86 assembly using free software, particularly under the Linux operating system. At many places Universal Resource Locators (URL) are given for some software or documentation repository. This document also points to other documents about nonfree, nonx86, or non32bit assemblers, although this is not its primary goal. Also note that there are FAQs and docs about programming on your favorite platform (whatever it is), which you should consult for platformspecific issues, not related directly to assembly programming. Because the main interest of assembly programming is to build the guts of operating systems, interpreters, compilers, and games, where C compiler fails to provide the needed expressiveness (performance is more and more seldom as issue), we are focusing on development of such kind of software. If you don't know what free software is, please do read carefully the GNU General Public License (GPL or copyleft), which is used in a lot of free software, and is the model for most of their licenses. It generally comes in a file named COPYING (or COPYING.LIB). Literature from the Free Software Foundation (FSF) might help you too. Particularly, the interesting feature of free software is that it comes with source code which you can consult and correct, or sometimes even borrow from. Read your particular license carefully and do comply to it.
1.3. Contributions
This is an interactively evolving document: you are especially invited to ask questions, to answer questions, to correct given answers, to give pointers to new software, to point the current maintainer to bugs or deficiencies in the pages. In one word, contribute! To contribute, please contact the maintainer. At the time of writing, it is Konstantin Boldyshev and no more FrancoisRene Rideau (since version 0.5). I (Fare) had been looking for some time for a serious hacker to replace me as maintainer of this document, and am pleased to announce Konstantin as my worthy successor.
Chapter 1. Introduction
Korean translation of this HOWTO is avalilable at http://kldp.org/HOWTO/html/AssemblyHOWTO/. Turkish translation of this HOWTO is available at http://belgeler.org/howto/assemblyhowto.html. Incomplete Russian translation is available at http://volgograd.lug.ru/wiki/GrableVodstvo/articles/AssembleInLinux/. Also, there was a French translation of the early HOWTO versions, but I can't find it now.
Chapter 1. Introduction
Linux Assembly HOWTO much more; or, in another context, a binary tree; or some highlevel structure distributed over a cluster of CPUs) a small change in algorithmic design might completely invalidate all your existing assembly code. So that either you're ready (and able) to rewrite it all, or you're tied to a particular algorithmic design On code that ain't too far from what's in standard benchmarks, commercial optimizing compilers outperform handcoded assembly (well, that's less true on the x86 architecture than on RISC architectures, and perhaps less true for widely available/free compilers; anyway, for typical C code, GCC is fairly good); And in any case, as moderator John Levine says on comp.compilers, "compilers make it a lot easier to use complex data structures, and compilers don't get bored halfway through and generate reliably pretty good code." They will also correctly propagate code transformations throughout the whole (huge) program when optimizing code between procedures and module boundaries.
2.1.3. Assessment
All in all, you might find that though using assembly is sometimes needed, and might even be useful in a few cases where it is not, you'll want to: minimize use of assembly code encapsulate this code in welldefined interfaces have your assembly code automatically generated from patterns expressed in a higherlevel language than assembly (e.g. GCC inline assembly macros) have automatic tools translate these programs into assembly code have this code be optimized if possible All of the above, i.e. write (an extension to) an optimizing compiler backend. Even when assembly is needed (e.g. OS development), you'll find that not so much of it is required, and that the above principles retain. See the Linux kernel sources concerning this: as little assembly as needed, resulting in a fast, reliable, portable, maintainable OS. Even a successful game like DOOM was almost massively written in C, with a tiny part only being written in assembly for speed up.
First the human writes the whole thing in a high level language. Second he profiles it to find the hot spots where it spends its time. Third he has the compiler produce assembly for those small sections of cod Chapter 2. Do you need assembly? 4
Fourth he hand tunes them looking for tiny improvements over the machine g The human wins because he can use the machine.
Linux Assembly HOWTO with most Unix compilers, including the GNU C Compiler (GCC), but YMMV. As for GCC, it will produce more understandable assembly code with the fverboseasm commandline option. Of course, if you want to get good assembly code, don't forget your usual optimization options and hints!
Chapter 3. Assemblers
3.1. GCC Inline Assembly
The wellknown GNU C/C++ Compiler (GCC), an optimizing 32bit compiler at the heart of the GNU project, supports the x86 architecture quite well, and includes the ability to insert assembly code in C programs, in such a way that register allocation can be either specified or left to GCC. GCC works on most available platforms, notably Linux, *BSD, VSTa, OS/2, *DOS, Win*, etc.
Linux Assembly HOWTO may compile, but not link properly, since it will be looking for noninlined extern functions in the libraries against which your program is being linked! Another way is to link against libraries that include fallback versions of the routines. Inline assembly can be disabled with fnoasm, which will have the compiler die when using extended inline asm syntax, or else generate calls to an external function named asm() that the linker can't resolve. To counter such flag, fasm restores treatment of the asm keyword. More generally, good compile flags for GCC on the x86 platform are gcc O2 fomitframepointer W Wall O2 is the good optimization level in most cases. Optimizing besides it takes more time, and yields code that is much larger, but only a bit faster; such overoptimization might be useful for tight loops only (if any), which you may be doing in assembly anyway. In cases when you need really strong compiler optimization for a few files, do consider using up to O6. fomitframepointer allows generated code to skip the stupid frame pointer maintenance, which makes code smaller and faster, and frees a register for further optimizations. It precludes the easy use of debugging tools (gdb), but when you use these, you just don't care about size and speed anymore anyway. W Wall enables all useful warnings and helps you to catch obvious stupid errors. You can add some CPUspecific m486 or such flag so that GCC will produce code that is more adapted to your precise CPU. Note that modern GCC has mpentium and such flags (and PGCC has even more), whereas GCC 2.7.x and older versions do not. A good choice of CPUspecific flags should be in the Linux kernel. Check the TeXinfo documentation of your current GCC installation for more. m386 will help optimize for size, hence also for speed on computers whose memory is tight and/or loaded, since big programs cause swap, which more than counters any "optimization" intended by the larger code. In such settings, it might be useful to stop using C, and use instead a language that favors code factorization, such as a functional language and/or FORTH, and use a bytecode or wordcode based implementation. Note that you can vary code generation flags from file to file, so performancecritical files will use maximum optimization, whereas other files will be optimized for size. To optimize even more, option mregparm=2 and/or corresponding function attribute might help, but might pose lots of problems when linking to foreign code, including libc. There are ways to correctly declare foreign functions so the right call sequences be generated, or you might want to recompile the foreign libraries to use the same registerbased calling convention... Note that you can add make these flags the default by editing file /usr/lib/gcclib/i486linux/2.7.2.3/specs or wherever that is on your system (better not add W Wall there, though). The exact location of the GCC specs files on system can be found by gcc v.
Chapter 3. Assemblers
Linux Assembly HOWTO Thus, you can put your assembly into CPP macros, and inline C functions, so anyone can use it in as any C function/macro. Inline functions resemble macros very much, but are sometimes cleaner to use. Beware that in all those cases, code will be duplicated, so only local labels (of 1: style) should be defined in that asm code. However, a macro would allow the name for a non local defined label to be passed as a parameter (or else, you should use additional metaprogramming methods). Also, note that propagating inline asm code will spread potential bugs in them; so watch out doubly for register constraints in such inline asm code. Lastly, the C language itself may be considered as a good abstraction to assembly programming, which relieves you from most of the trouble of assembling.
3.2. GAS
GAS is the GNU Assembler, that GCC relies upon.
Chapter 3. Assemblers
Linux Assembly HOWTO Note: There are few programs which may help you to convert source code between AT&T and Intel assembler syntaxes; some of the are capable of performing conversion in both directions. GAS has comprehensive documentation in TeXinfo format, which comes at least with the source distribution. Browse extracted .info pages with Emacs or whatever. There used to be a file named gas.doc or as.doc around the GAS source package, but it was merged into the TeXinfo docs. Of course, in case of doubt, the ultimate documentation is the sources themselves! A section that will particularly interest you is Machine Dependencies::i386Dependent:: Again, the sources for Linux (the OS kernel) come in as excellent examples; see under linux/arch/i386/ the following files: kernel/*.S, boot/compressed/*.S, mathemu/*.S. If you are writing kind of a language, a thread package, etc., you might as well see how other languages ( OCaml, Gforth, etc.), or thread packages (QuickThreads, MIT pthreads, LinuxThreads, etc), or whatever else do it. Finally, just compiling a C program to assembly might show you the syntax for the kind of instructions you want. See section Do you need assembly? above.
Chapter 3. Assemblers
10
3.3. NASM
The Netwide Assembler project provides cool i386 assembler, written in C, that should be modular enough to eventually support all known syntaxes and object formats.
3.4.1. AS86
AS86 is a 80x86 assembler (16bit and 32bit) with integrated macro support. It has mostly Intelsyntax, though it differs slightly as for addressing modes. Some time ago it was used in a several projects, including the Linux kernel, but eventually most of those projects have moved to GAS or NASM. AFAIK, only ELKS continues to use it.
Chapter 3. Assemblers
11
Linux Assembly HOWTO AS86 can be found at http://www.cix.co.uk/~mayday/, in bin86 package with linker (ld86), or as separate archive. Documentation is available as the man page and as.doc from the source package. When in doubt, the source code itself is often a good doc: though it is not very well commented, the programming style is straightforward. You might try to see how as86 is(was) used in ELKS, LILO, or Tunes 0.0.0.25... A completely outdated version 0.4 of AS86 is distributed by HJLu just to compile the Linux kernel versions prior to 2.4, in a package named bin86, available in any Linux GCC repository. But I advise no one to use it for anything else but compiling Linux. This version supports only a hacked minix object file format, which is not supported by the GNU binutils or anything, and it has a few bugs in 32bit mode, so you really should better keep it only for compiling Linux. Using AS86 with BCC Here's the GNU Makefile entry for using BCC to transform .s asm into both a.out .o object and .l listing:
%.o %.l: %.s bcc 3 G c Ad Al A$*.l o $*.o $<
Remove the %.l, Al, and A$*.l, if you don't want any listing. If you want something else than a.out, you can examine BCC docs about the other supported formats, and/or use the objcopy utility from the GNU binutils package.
3.4.2. YASM
YASM is a complete rewrite of the NASM assembler under the GNU GPL (some portions are under the "new" BSD License). It is designed from the ground up to allow for multiple syntaxes to be supported (eg, NASM, TASM, GAS, etc.) in addition to multiple output object formats. Another primary module of the overall design is an optimizer module. It looks promising; it is under heavy development, and you may want to take part. See http://www.tortall.net/projects/yasm/.
3.4.3. FASM
FASM (flat assembler) is a fast, efficient 80x86 assembler that runs in 'flat real mode'. Unlike many other 80x86 assemblers, FASM only requires the source code to include the information it really needs. It is written in itself and is very small and fast. It runs on DOS/Windows/Linux and can produce flat binary, DOS EXE, Win32 PE, COFF and Linux ELF output. See http://flatassembler.net.
Chapter 3. Assemblers
12
3.4.5. AASM
Aasm is an advanced assembler designed to support several target architectures. It has been designed to be easily extended and, should be considered as a good alternative to monolithic assembler development for each new target CPUs and binary file formats. Aasm should make assembly programming easier for developer, by providing a set of advanced features including symbol scopes, an expressions engine, big integer support, macro capability, numerous and accurate warning messages... Its dynamic modular architecture enables Aasm to extend its set of features with plugins by taking advantages of dynamic libraries. The input module supports Intel syntax (like nasm, tasm, masm, etc.). The x86 assembler module supports all opcodes up to P6 including MMX, SSE and 3DNow! extensions. FCPU and SPARC assembler modules are under development. Several output modules are available for ELF, COFF, IntelHex, and raw binary formats. http://savannah.nongnu.org/projects/aasm/
3.4.6. TDASM
The Table Driven Assembler (TDASM) is a free portable cross assembler for any kind of assembly language. It should be possible to use it as a compiler to any target microprocessor using a table that defines the compilation process. It is available from http://www.penguin.cz/~niki/tdasm/.
3.4.7. HLA
HLA is a High Level Assembly language. It uses a high level language like syntax (similar to Pascal, C/C++, and other HLLs) for variable declarations, procedure declarations, and procedure calls. It uses a modified assembly language syntax for the standard machine instructions. It also provides several high level language style control structures (if, while, repeat..until, etc.) that help you write much more readable code. HLA is free and comes with source, Linux and Win32 versions available. On Win32 you need MASM and a 32bit version of MSlink on Win32, on Linux you nee GAS, because HLA produces specified assembler code and uses that assembler for final assembling and linking.
3.4.8. TALC
TALC is another free MASM/Win32 based compiler (however it supports ELF output, does it?). TAL stands for Typed Assembly Language. It extends traditional untyped assembly languages with typing annotations, memory management primitives, and a sound set of typing rules, to guarantee the memory safety, control flow safety,and type safety of TAL programs. Moreover, the typing constructs are expressive enough to encode most source language programming features including records and structures, arrays, higherorder and polymorphic functions, exceptions, abstract data types, subtyping, and modules. Just as importantly, TAL is flexible enough to admit many lowlevel compiler optimizations. Consequently, TAL is an ideal target platform for typedirected compilers that want to produce verifiably safe code for use in secure mobile code applications or extensible operating system kernels. Chapter 3. Assemblers 13
3.4.11. Terse
Terse is a programming tool that provides THE most compact assembler syntax for the x86 family! However, it is evil proprietary software. It is said that there was a project for a free clone somewhere, that was abandoned after worthless pretenses that the syntax would be owned by the original author. Thus, if you're looking for a nifty programming project related to assembly hacking, I invite you to develop a tersesyntax frontend to NASM, if you like that syntax. As an interesting historic remark, on comp.compilers, 1999/07/11 19:36:51, the moderator wrote: "There's no reason that assemblers have to have awful syntax. About 30 years ago I used Niklaus Wirth's PL360, which was basically a S/360 assembler with Algol syntax and a a little syntactic sugar like while loops that turned into the obvious branches. It really was an assembler, e.g., you had to write out your expressions with explicit assignments of values to registers, but it was nice. Wirth used it to write Algol W, a small fast Algol subset, which was a predecessor to Pascal. As is so often the case, Algol W was a significant improvement over many of its successors. John"
Chapter 3. Assemblers
14
Chapter 3. Assemblers
15
Chapter 4. Metaprogramming
Assembly programming is a bore, but for critical parts of programs. You should use the appropriate tool for the right task, so don't choose assembly when it does not fit; C, OCaml, perl, Scheme, might be a better choice in the most cases. However, there are cases when these tools do not give fine enough control on the machine, and assembly is useful or needed. In these cases you'll appreciate a system of macroprocessing and metaprogramming that allows recurring patterns to be factored each into one indefinitely reusable definition, which allows safer programming, automatic propagation of pattern modification, etc. Plain assembler often is not enough, even when one is doing only small routines to link with C.
4.1.1. CPP
CPP is truly not very expressive, but it's enough for easy things, it's standard, and called transparently by GCC. As an example of its limitations, you can't declare objects so that destructors are automatically called at the end of the declaring block; you don't have diversions or scoping, etc. CPP comes with any C compiler. However, considering how mediocre it is, stay away from it if by chance you can make it without C.
4.1.2. M4
M4 gives you the full power of macroprocessing, with a Turing equivalent language, recursion, regular expressions, etc. You can do with it everything that CPP cannot. See macro4th (this4th) or the Tunes 0.0.0.25 sources as examples of advanced macroprogramming using m4. However, its disfunctional quoting and unquoting semantics force you to use explicit continuationpassing tailrecursive macro style if you want to do advanced macro programming (which is remindful of TeX BTW, has anyone tried to use TeX as a macroprocessor for anything else than typesetting ?). This is NOT worse than CPP that does not allow quoting and recursion anyway. The right version of M4 to get is GNU m4 1.4 (or later if exists), which has the most features and the least bugs or limitations of all. m4 is designed to be slow for anything but the simplest uses, which might still be ok for most assembly programming (you are not writing millionlines assembly programs, are you?). Chapter 4. Metaprogramming 16
4.2. Metaprogramming
Instead of using an external filter that expands macros, one way to do things is to write programs that write part or all of other programs. For instance, you could use a program outputting source code to generate sine/cosine/whatever lookup tables, to extract a sourceform representation of a binary file, to compile your bitmaps into fast display routines, to extract documentation, initialization/finalization code, description tables, as well as normal code from the same source files, to have customized assembly code, generated from a perl/shell/scheme script that does arbitrary processing, to propagate data defined at one point only into several crossreferencing tables and code chunks. etc. Think about it!
4.2.3. TUNES
The TUNES Project for a Free Reflective Computing System is developing its own assembler as an extension to the Scheme language, as part of its development process. It doesn't run at all yet, though help is welcome. The assembler manipulates abstract syntax trees, so it could equally serve as the basis for a assembly syntax translator, a disassembler, a common assembler/compiler backend, etc. Also, the full power of a real language, Scheme, make it unchallenged as for macroprocessing/metaprogramming.
Chapter 4. Metaprogramming
17
to be sure that the C function foo() will be called really bar in assembly. Note that the objcopy utility from the binutils package should allow you to transform your a.out objects into ELF objects, and perhaps the contrary too, in some cases. More generally, it will do lots of file format conversions.
Linux Assembly HOWTO a question of taste/belief than something practical. Remember, Linux is aiming to be POSIX compliant, so does libc. This means that syntax of almost all libc "system calls" exactly matches syntax of real kernel system calls (and vice versa). Besides, GNU libc(glibc) becomes slower and slower from version to version, and eats more and more memory; and so, cases of using direct system calls become quite usual. However, the main drawback of throwing libc away is that you will possibly need to implement several libc specific functions (that are not just syscall wrappers) on your own (printf() and Co.), and you are ready for that, aren't you? :) Here is summary of direct system calls pros and cons. Pros: the smallest possible size; squeezing the last byte out of the system the highest possible speed; squeezing cycles out of your favorite benchmark full control: you can adapt your program/library to your specific language or memory requirements or whatever no pollution by libc cruft no pollution by C calling conventions (if you're developing your own language or environment) static binaries make you independent from libc upgrades or crashes, or from dangling #! path to an interpreter (and are faster) just for the fun out of it (don't you get a kick out of assembly programming?) Cons: If any other program on your computer uses the libc, then duplicating the libc code will actually wastes memory, not saves it. Services redundantly implemented in many static binaries are a waste of memory. But you can make your libc replacement a shared library. Size is much better saved by having some kind of bytecode, wordcode, or structure interpreter than by writing everything in assembly. (the interpreter itself could be written either in C or assembly.) The best way to keep multiple binaries small is to not have multiple binaries, but instead to have an interpreter process files with #! prefix. This is how OCaml works when used in wordcode mode (as opposed to optimized native code mode), and it is compatible with using the libc. This is also how Tom Christiansen's Perl PowerTools reimplementation of unix utilities works. Finally, one last way to keep things small, that doesn't depend on an external file with a hardcoded path, be it library or interpreter, is to have only one binary, and have multiplynamed hard or soft links to it: the same binary will provide everything you need in an optimal space, with no redundancy of subroutines or useless binary headers; it will dispatch its specific behavior according to its argv[0]; in case it isn't called with a recognized name, it might default to a shell, and be possibly thus also usable as an interpreter! You cannot benefit from the many functionalities that libc provides besides mere linux syscalls: that is, functionality described in section 3 of the manual pages, as opposed to section 2, such as malloc, threads, locale, password, highlevel network management, etc. Therefore, you might have to reimplement large parts of libc, from printf() to malloc() and gethostbyname. It's redundant with the libc effort, and can be quite boring sometimes. Note that some people have already reimplemented "light" replacements for parts of the libc check them out! (Redhat's minilibc, Rick Hohensee's libsys, Felix von Leitner's dietlibc, Christian Fowelin's libASM, asmutils project is working on pure assembly libc) Static libraries prevent you to benefit from libc upgrades as well as from libc addons such as the zlibc package, that does onthefly transparent decompression of gzipcompressed files.
19
Linux Assembly HOWTO The few instructions added by the libc can be a ridiculously small speed overhead as compared to the cost of a system call. If speed is a concern, your main problem is in your usage of system calls, not in their wrapper's implementation. Using the standard assembly API for system calls is much slower than using the libc API when running in microkernel versions of Linux such as L4Linux, that have their own faster calling convention, and pay high conventiontranslation overhead when using the standard one (L4Linux comes with libc recompiled with their syscall API; of course, you could recompile your code with their API, too). See previous discussion for general speed optimization issue. If syscalls are too slow to you, you might want to hack the kernel sources (in C) instead of staying in userland. If you've pondered the above pros and cons, and still want to use direct syscalls, then here is some advice. You can easily define your system calling functions in a portable way in C (as opposed to unportable using assembly), by including asm/unistd.h, and using provided macros. Since you're trying to replace it, go get the sources for the libc, and grok them. (And if you think you can do better, then send feedback to the authors!) As an example of pure assembly code that does everything you want, examine Linux assembly resources. Basically, you issue an int 0x80, with the __NR_syscallname number (from asm/unistd.h) in eax, and parameters (up to six) in ebx, ecx, edx, esi, edi, ebp respectively. Result is returned in eax, with a negative result being an error, whose opposite is what libc would put into errno. The userstack is not touched, so you needn't have a valid one when doing a syscall. Passing sixth parameter in ebp appeared in Linux 2.4, previous Linux versions understand only 5 parameters in registers. Linux Kernel Internals, and especially How System Calls Are Implemented on i386 Architecture? chapter will give you more robust overview. As for the invocation arguments passed to a process upon startup, the general principle is that the stack originally contains the number of arguments argc, then the list of pointers that constitute *argv, then a nullterminated sequence of nullterminated variable=value strings for the environment. For more details, do examine Linux assembly resources, read the sources of C startup code from your libc (crt0.S or crt1.S), or those from the Linux kernel (exec.c and binfmt_*.c in linux/fs/).
Linux Assembly HOWTO to achieve efficiency is not free software. Any taker for a replacement? Anyway, in all these cases, you'll be better when using GCC inline assembly with the macros from linux/asm/*.h than writing full assembly source files.
21
Linux Assembly HOWTO Windows and Co. This document is not about Windows programming, you can find lots of documents about it everywhere... The thing you should know is that there is the cygwin32.dll library, for GNU programs to run on Win32 platform; thus, you can use GCC, GAS, all the GNU tools, and many other Unix applications.
22
global _start
23
.global _start
_start: # write our string to stdout movl movl movl movl int # and exit movl movl int .data msg: .ascii "Hello, world!\n" len = . msg # our dear string # length of our dear string $0,%ebx $1,%eax $0x80 # first argument: exit code # system call number (sys_exit) # call kernel # section declaration $len,%edx $msg,%ecx $1,%ebx $4,%eax $0x80 # # # # # third argument: message length second argument: pointer to message to write first argument: file handle (stdout) system call number (sys_write) call kernel
24
This will finally build hello executable. Hey, try to run it... Works? That's it. Pretty simple.
# This is my first MIPSRISC assembly program! # To compile this program type: # > gcc o hello hello.S non_shared # This program compiles without errors or warnings # on a PlayStation2 MIPS R5900 (EE Core). # EE stands for Emotion Engine...lame! # # # # The non_shared option tells gcc that we`re not interrested in compiling relocatable code. If we were, we would need to follow the PIC ABI calling conventions and other protocols. // ...for human readable register names // ...for system serivices
hello: length:
main: # # #
.rdata # begin readonly data segment .align 2 # because of the way memory is built .asciz "Hello, world!\n" # a null terminated string .align 4 # because of the way memory is built .word . hello # length = IC (helloaddr) .text # begin code segment .globl main # for gcc/ld linking .ent main # for gdb debugging info. # We must specify non_shared to gcc or we`ll need these 3 lines that fallow. .set noreorder # disable instruction reordering .cpload t9 # PIC ABI crap (function prologue) .set reorder # reenable instruction reordering move a0,$0 # load stdout fd la a1,hello # load string address
25
26
Chapter 7. Resources
7.1. Pointers
http://linuxassembly.org/resources.html Do visit it, and get plenty of pointers to assembly projects, tools, tutorials, documentation, guides, etc, concerning different UNIX operating systems and CPUs. Because it evolves quickly, I will no longer duplicate it here. If you are new to assembly in general, here are few starting pointers: The Art Of Assembly x86 assembly FAQ (use Google) CoreWars, a fun way to learn assembly in general Usenet: comp.lang.asm.x86; alt.lang.asm
7.2. Mailing list
If you're are interested in Linux/UNIX assembly programming (or have questions, or are just curious) I especially invite you to join Linux assembly programming mailing list. This is an open discussion of assembly programming under Linux, *BSD, BeOS, or any other UNIX/POSIX like OS; also it is not limited to x86 assembly (Alpha, Sparc, PPC and other hackers are welcome too!). Mailing list address is <[email protected]>. To subscribe send a messgage to <[email protected]> with the following line in the body of the message:
subscribe linuxassembly
Chapter 7. Resources
27
28
8.2. How do I debug pure assembly code under Linux? There's an early version of the Assembly Language Debugger, which is designed to work with assembly code, and is portable enough to run on Linux and *BSD. It is already functional and should be the right choice, check it out! You can also try gdb ;). Although it is sourcelevel debugger, it can be used to debug pure assembly code, and with some trickery you can make gdb to do what you need (unfortunately, nasm 'g' switch does not generate proper debug info for gdb; this is nasm bug, I think). Here's an answer from Dmitry Bakhvalov:
Personally, I use gdb for debugging asmutils. Try this: 1) Use the following stuff to compile: $ nasm f elf g smth.asm $ ld o smth smth.o 2) Fire up gdb: $ gdb smth 3) In gdb: (gdb) disassemble _start Place a breakpoint at _start+1 (If placed at _start the breakpoint wouldnt work, dunno why) (gdb) b *0x8048075 To step thru the code I use the following macro: (gdb)define n >ni >printf "eax=%x ebx=%x ...etc...",$eax,$ebx,...etc... >disassemble $pc $pc+15 >end Then start the program with r command and debug with n. Hope this helps.
If you want to set breakpoints across your code, you can just use int 3 instruction as breakpoint (instead of entering address manually in gdb). If you're using gas, you should consult gas and gdb related tutorials. 8.3. Any other useful debugging tools?
29
Linux Assembly HOWTO Definitely strace can help a lot (ktrace and kdump on FreeBSD), it is used to trace system calls and signals. Read its manual page (man strace) and strace help output for details. 8.4. How do I access BIOS functions from Linux (BSD, BeOS, etc)? Short answer is noway. This is protected mode, use OS services instead. Again, you can't use int 0x10, int 0x13, etc. Fortunately almost everything can be implemented by means of system calls or library functions. In the worst case you may go through direct port access, or make a kernel patch to implement needed functionality, or use LRMI library to access BIOS functions. 8.5. Is it possible to write kernel modules in assembly? Yes, indeed it is. While in general it is not a good idea (it hardly will speedup anything), there may be a need of such wizardy. The process of writing a module itself is not that hard a module must have some predefined global function, it may also need to call some external functions from the kernel. Examine kernel source code (that can be built as module) for details. Meanwhile, here's an example of a minimum dumb kernel module (module.asm) (source is based on example by mammon_ from APJ #8):
section .text global init_module global cleanup_module global kernel_version extern printk init_module: push call pop xor ret
cleanup_module: push dword str2 call printk pop eax ret str1 str2 kernel_version db db db "init_module done",0xa,0 "cleanup_module done",0xa,0 "2.2.18",0
The only thing this example does is reporting its actions. Modify kernel_version to match yours, and build module with:
$ nasm f elf o module.m module.asm $ ld r o module.o module.m
Now you can play with it using insmod/rmmod/lsmod (root privilidged are required); a lot of fun, huh? 8.6. How do I allocate memory dynamically?
30
section .text ; ;allocate memory ; %define LIMIT mov call cmp je add mov call cmp je cmp je 0x4000000 ebx,0 sys_brk eax,1 erro1 eax,LIMIT ebx,eax sys_brk eax,1 erro1 eax,var1+1 erro1 ; about 100Megs ; get bottom of data segment
; ok?
; ok?
; ;use allocated memory ; ; ; ; ; now eax contains bottom of data segment save bottom eax=beginning of data segment
mov mov repeat: mov inc cmp jne ; ;free memory ; mov call cmp je
31
Linux Assembly HOWTO 8.7. I can't understand how to use select system call! An answer from Patrick Mochel:
When you call sys_open, you get back a file descriptor, which is simply an index into a table of all the open file descriptors that your process has. stdin, stdout, and stderr are always 0, 1, and 2, respectively, because that is the order in which they are always open for your process from there. Also, notice that the first file descriptor that you open yourself (w/o first closing any of those magic three descriptors) is always 3, and they increment from there. Understanding the index scheme will explain what select does. When you call select, you are saying that you are waiting certain file descriptors to read from, certain ones to write from, and certain ones to watch from exceptions from. Your process can have up to 1024 file descriptors open, so an fd_set is just a bit mask describing which file descriptors are valid for each operation. Make sense? Since each fd that you have open is just an index, and it only needs to be on or off for each fd_set, you need only 1024 bits for an fd_set structure. 1024 / 32 = 32 longs needed to represent the structure. Now, for the loose example. Suppose you want to read from a file descriptor (w/o timeout). Allocate the equivalent to an fd_set. .data my_fds: times 32 dd 0 open the file descriptor that you want to read from. set that bit in the fd_set structure. First, you need to figure out which of the 32 dwords the bit is in. Then, use bts to set the bit in that dword. bts will do a modulo 32 when setting the bit. That's why you need to first figure out which dword to start with. mov edx, 0 mov ebx, 32 div ebx lea ebx, my_fds bts ebx[eax * 4], edx repeat the last step for any file descriptors you want to read from. repeat the entire exercise for either of the other two fd_sets if you want action from them. That leaves two other parts of the equation the n paramter and the timeout parameter. I'll leave the timeout parameter as an exercise for the reader (yes, I'm lazy), but I'll briefly talk about the n parameter. It is the value of the largest file descriptor you are selecting from (from any of the fd_sets), plus one. Why plus one? Well, because it's easy to determine a mask from that value. Suppose that there is data available on x file descriptors, but the highest one you care about is (n 1). Since
32
33
Appendix A. History
Each version includes a few fixes and minor corrections, that need not to be repeatedly mentioned every time. Revision History Revision 0.6g 11 Feb 2006 Revised by: konst Added AASM, updated FASM, added MIPS example to Quick Start section, added URLs to Turkish and Russian translations, misc URL updates Revision 0.6f 17 Aug 2002 Revised by: konst Added FASM, added URL to Korean translation, added URL to SVR4 i386 ABI specs, update on HLA/Linux, small fix in hello.S example, misc URL updates Revision 0.6e 12 Jan 2002 Revised by: konst Added URL describing GAS Intel syntax; Added OSIMPA(former SHASM); Added YASM; FAQ update. Revision 0.6d 18 Mar 2001 Revised by: konst Added Free Pascal; new NASM URL again Revision 0.6c 15 Feb 2001 Revised by: konst Added SHASM; new answer in FAQ, new NASM URL, new mailing list address Revision 0.6b 21 Jan 2001 Revised by: konst new questions in FAQ, corrected few URLs Revision 0.6a 10 Dec 2000 Revised by: konst Remade section on AS86 (thanks to Holluby Istvan for pointing out obsolete information). Fixed several URLs that can be incorrectly rendered from sgml to html. Revision 0.6 11 Nov 2000 Revised by: konst HOWTO is completely rewritten using DocBook DTD. Layout is totally rearranged; too much changes to list them here. Revision 0.5n 07 Nov 2000 Revised by: konst Added question regarding kernel modules to FAQ, fixed NASM URLs, GAS has Intel syntax too Revision 0.5m 22 Oct 2000 Revised by: konst Linux 2.4 system calls can have 6 args, Added ALD note to FAQ, fixed mailing list subscribe address Revision 0.5l 23 Aug 2000 Revised by: konst Added TDASM, updates on NASM Revision 0.5k 11 Jul 2000 Revised by: konst Few additions to FAQ Revision 0.5j 14 Jun 2000 Revised by: konst Complete rearrangement of Introduction and Resources sections. FAQ added to Resources, misc cleanups and additions. Revision 0.5i 04 May 2000 Revised by: konst Added HLA, TALC; rearrangements in Resources, Quick Start Assemblers sections. Few new pointers. Revision 0.5h 09 Apr 2000 Revised by: konst finally managed to state LDP license on document, new resources added, misc fixes Revision 0.5g 26 Mar 2000 Revised by: konst new resources on different CPUs Revision 0.5f 02 Mar 2000 Revised by: konst Appendix A. History 34
Linux Assembly HOWTO new resources, misc corrections Revision 0.5e 10 Feb 2000 Revised by: konst URL updates, changes in GAS example Revision 0.5d 01 Feb 2000 Revised by: konst Resources (former "Pointers") section completely redone, various URL updates. Revision 0.5c 05 Dec 1999 Revised by: konst New pointers, updates and some rearrangements. Rewrite of sgml source. Revision 0.5b 19 Sep 1999 Revised by: konst Discussion about libc or not libc continues. New web pointers and and overall updates. Revision 0.5a 01 Aug 1999 Revised by: konst Quick Start section rearranged, added GAS example. Several new web pointers. Revision 0.5 01 Aug 1999 Revised by: konstfare GAS has 16bit mode. New maintainer (at last): Konstantin Boldyshev. Discussion about libc or not libc. Added Quick Start section with examples of assembly code. Revision 0.4q 22 Jun 1999 Revised by: fare process argument passing (argc, argv, environ) in assembly. This is yet another "last release by Fare before new maintainer takes over". Nobody knows who might be the new maintainer. Revision 0.4p 06 Jun 1999 Revised by: fare clean up and updates Revision 0.4o 01 Dec 1998 Revised by: fare Revision 0.4m corrections about gcc invocation Revision 0.4l release for LSL 6th edition Revision 0.4k Revision 0.4j 23 Mar 1998 16 Nov 1997 19 Oct 1997 07 Sep 1997 Revised by: fare Revised by: fare Revised by: fare Revised by: fare Revised by: fare Revised by: fare Revised by: fare Revised by: fare Revised by: fare Revised by: fare Revised by: fare Revised by: fare Revised by: fare 35
Revision 0.4i 17 Jul 1997 info on 16bit mode access from Linux Revision 0.4h 19 Jun 1997 still more on "how not to use assembly"; updates on NASM, GAS. Revision 0.4g 30 Mar 1997 Revision 0.4f 20 Mar 1997
Revision 0.4e 13 Mar 1997 Release for DrLinux Revision 0.4d 28 Feb 1997 Vapor announce of a new AssemblyHOWTO maintainer Revision 0.4c 09 Feb 1997 Added section Do you need assembly?. Revision 0.4b 03 Feb 1997 NASM moved: now is before AS86 Revision 0.4a 20 Jan 1997 Appendix A. History
Linux Assembly HOWTO CREDITS section added Revision 0.4 20 Jan 1997 Revised by: fare first release of the HOWTO as such Revision 0.4pre1 13 Jan 1997 Revised by: fare text miniHOWTO transformed into a full linuxdocsgml HOWTO, to see what the SGML tools are like Revision 0.3l 11 Jan 1997 Revised by: fare Revision 0.3k 19 Dec 1996 Revised by: fare What? I had forgotten to point to terse??? Revision 0.3j 24 Nov 1996 Revised by: fare point to French translated version Revision 0.3i 16 Nov 1996 Revised by: fare NASM is getting pretty slick Revision 0.3h 06 Nov 1996 Revised by: fare more about crosscompiling See on sunsite: devel/msdos/ Revision 0.3g 02 Nov 1996 Revised by: fare Created the History. Added pointers in crosscompiling section. Added section about I/O programming under Linux (particularly video). Revision 0.3f 17 Oct 1996 Revised by: fare Revision 0.3c Revision 0.2 15 Jun 1996 04 May 1996 Revised by: fare Revised by: fare
Revision 0.1 23 Apr 1996 Revised by: fare FrancoisRene "Fare" Rideau creates and publishes the first miniHOWTO, because "I'm sick of answering ever the same questions on comp.lang.asm.x86"
Appendix A. History
36
Appendix B. Acknowledgements
I would like to thank all the people who have contributed ideas, answers, remarks, and moral support, and additionally the following persons, by order of appearance: Linus Torvalds for Linux Bruce Evans for bcc from which as86 is extracted Simon Tatham and Julian Hall for NASM Greg Hankins and now Tim Bynum for maintaining HOWTOs Raymond Moon for his FAQ Eric Dumas for his translation of the miniHOWTO into French (sad thing for the original author to be French and write in English) Paul Anderson and Rahim Azizarab for helping me, if not for taking over the HOWTO Marc Lehman for his insight on GCC invocation Abhijit MenonSen for helping me figure out the argument passing convention
Appendix B. Acknowledgements
37
Appendix C. Endorsements
This version of the document is endorsed by Konstantin Boldyshev. Modifications (including translations) must remove this appendix according to the license agreement. $Id: AssemblyHOWTO.sgml,v 1.8 2006/02/11 08:26:26 konst Exp $
Appendix C. Endorsements
38
Linux Assembly HOWTO directly and straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup has been designed to thwart or discourage subsequent modification by readers is not Transparent. A copy that is not "Transparent" is called "Opaque". Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standardconforming simple HTML designed for human modification. Opaque formats include PostScript, PDF, proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machinegenerated HTML produced by some word processors for output purposes only. The "Title Page" means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, "Title Page" means the text near the most prominent appearance of the work's title, preceding the beginning of the body of the text. 2. VERBATIM COPYING You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3. You may also lend copies, under the same conditions stated above, and you may publicly display copies. 3. COPYING IN QUANTITY If you publish printed copies of the Document numbering more than 100, and the Document's license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts: FrontCover Texts on the front cover, and BackCover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects. If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages. If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a machinereadable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a publiclyaccessible computernetwork location containing a complete Transparent copy of the Document, free of added material, which the general networkusing public has access to download anonymously at no charge using publicstandard network protocols. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public. Appendix D. GNU Free Documentation License 40
Linux Assembly HOWTO It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document. 4. MODIFICATIONS You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Version filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version: A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original publisher of that version gives permission. B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if it has less than five). C. State on the Title page the name of the publisher of the Modified Version, as the publisher. D. Preserve all the copyright notices of the Document. E. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices. F. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the Addendum below. G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document's license notice. H. Include an unaltered copy of this License. I. Preserve the section entitled "History", and its title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section entitled "History" in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence. J. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the "History" section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission. K. In any section entitled "Acknowledgements" or "Dedications", preserve the section's title, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein. L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles. M. Delete any section entitled "Endorsements". Such a section may not be included in the Modified Version. N. Do not retitle any existing section as "Endorsements" or to conflict in title with any Invariant Section. If the Modified Version includes new frontmatter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version's license notice. These titles must be distinct from any other section titles.
41
Linux Assembly HOWTO You may add a section entitled "Endorsements", provided it contains nothing but endorsements of your Modified Version by various partiesfor example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard. You may add a passage of up to five words as a FrontCover Text, and a passage of up to 25 words as a BackCover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of FrontCover Text and one of BackCover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one. The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version. 5. COMBINING DOCUMENTS You may combine the Document with other documents released under this License, under the terms defined in section 4 above for modified versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice. The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work. In the combination, you must combine any sections entitled "History" in the various original documents, forming one section entitled "History"; likewise combine any sections entitled "Acknowledgements", and any sections entitled "Dedications". You must delete all sections entitled "Endorsements." 6. COLLECTIONS OF DOCUMENTS You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects. You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document. 7. AGGREGATION WITH INDEPENDENT WORKS A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, does not as a whole count as a Modified Version of the Document, provided no compilation copyright is claimed for the compilation. Such a compilation is called an "aggregate", and this License does not apply to the other selfcontained works thus compiled with the Document, on account of their being thus compiled, if they are not themselves derivative works of the Document. If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one quarter of the entire aggregate, the Document's Cover Texts may be placed on covers that surround only the Document within the aggregate. Otherwise they must appear on Appendix D. GNU Free Documentation License 42
Linux Assembly HOWTO covers around the whole aggregate. 8. TRANSLATION Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License provided that you also include the original English version of this License. In case of a disagreement between the translation and the original English version of this License, the original English version will prevail. 9. TERMINATION You may not copy, modify, sublicense, or distribute the Document except as expressly provided for under this License. Any other attempt to copy, modify, sublicense or distribute the Document is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 10. FUTURE REVISIONS OF THIS LICENSE The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See http://www.gnu.org/copyleft/. Each version of the License is given a distinguishing version number. If the Document specifies that a particular numbered version of this License "or any later version" applies to it, you have the option of following the terms and conditions either of that specified version or of any later version that has been published (not as a draft) by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published (not as a draft) by the Free Software Foundation. How to use this License for your documents To use this License in a document you have written, include a copy of the License in the document and put the following copyright and license notices just after the title page:
Copyright (c) YEAR YOUR NAME. Permission is granted to copy, distribute and/or modify this documen under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being LIST THEIR TITLES, with the FrontCover Texts being LIST, and with the BackCover Texts being LI A copy of the license is included in the section entitled "GNU Free Documentation License". If you have no Invariant Sections, write "with no Invariant Sections" instead of saying which ones are invariant. If you have no FrontCover Texts, write "no FrontCover Texts" instead of "FrontCover Texts being LIST"; likewise for BackCover Texts. If your document contains nontrivial examples of program code, we recommend releasing these examples in parallel under your choice of free software license, such as the GNU General Public License, to permit their use in free software.
43