home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 11 / IOPROG_11.ISO / docs / assemb~1 < prev    next >
Encoding:
Text File  |  1997-09-14  |  47.5 KB  |  1,193 lines

  1.   Assembly HOWTO
  2.   Franτois-RenΘ Rideau rideau@ens.fr
  3.   v0.4j, 7 September 1997
  4.  
  5.   This is the Linux Assembly HOWTO.  This document describes how to pro¡
  6.   gram in assembly using FREE programming tools, focusing on development
  7.   for or from the Linux Operating System on i386 platforms.  Included
  8.   material may or may not be applicable to other hardware and/or soft¡
  9.   ware platforms.  Contributions about these would be gladly accepted.
  10.   keywords: assembly, assembler, free, macroprocessor, preprocessor,
  11.   asm, inline asm, 32-bit, x86, i386, gas, as86, nasm
  12.  
  13.   1.  INTRODUCTION
  14.  
  15.   1.1.  Legal Blurp
  16.  
  17.   Copyright (C) 1996, 1997 Francois-Rene Rideau.
  18.  
  19.   You can freely distribute this document, provided the original
  20.   document is pointed to, any modification is clearly indicated as such.
  21.  
  22.   1.2.  IMPORTANT NOTE
  23.  
  24.   This is expectedly the last release I'll make of this document.
  25.   There's one candidate new maintainer, but until he really takes the
  26.   HOWTO over, I'll accept feedback.
  27.  
  28.   You are especially invited to ask questions, to answer to questions,
  29.   to correct given answers, to add new FAQ answers, to give pointers to
  30.   other software, to point the current maintainer to bugs or
  31.   deficiencies in the pages.  If you're motivated, you could even TAKE
  32.   OVER THE MAINTENANCE OF THE FAQ.  In one word, contribute!
  33.  
  34.   To contribute, please contact whoever appears to maintain the
  35.   Assembly-HOWTO.  Current maintainers are Franτois-RenΘ Rideau
  36.   <mailto:rideau@clipper.ens.fr> and now Paul Anderson
  37.   <mailto:paul@geeky1.ebtech.net>.
  38.  
  39.   1.3.  Foreword
  40.  
  41.   This document aims at answering frequently asked questions of people
  42.   who program or want to program 32-bit x86 assembly using free
  43.   assemblers, particularly under the Linux operating system.  It may
  44.   also point to other documents about non-free, non-x86, or non-32-bit
  45.   assemblers, though such is not its primary goal.
  46.  
  47.   Because the main interest of assembly programming is to build to write
  48.   the guts of operating systems, languages, and games, where a C
  49.   compiler fails to provide the needed expressivity (performance is more
  50.   and more seldom an issue), we stress on development of such software.
  51.  
  52.   1.3.1.  How to use this document
  53.  
  54.   This document contains answers to some frequently asked questions.  At
  55.   many places, Universal Resource Locators (URL) are given for some
  56.   software or documentation repository.  Please see that the most useful
  57.   repositories are mirrored, and that by accessing a nearer mirror site,
  58.   you relieve the whole Internet from unneeded network traffic, while
  59.   saving your own precious time.  Particularly, there are large
  60.   repositories all over the world, that mirror other popular
  61.   repositories.  You should learn and note what are those places near
  62.   you (networkwise).  Sometimes, the list of mirrors is listed in a
  63.   file, or in a login message. Please heed the advice.  Else, you should
  64.   ask archie about the software you're looking for...
  65.  
  66.   The most recent version for this documents sits in
  67.  
  68.   <http://www.eleves.ens.fr:8080/home/rideau/Assembly-HOWTO> or
  69.   <http://www.eleves.ens.fr:8080/home/rideau/Assembly-HOWTO.sgml>
  70.  
  71.   but what's in Linux HOWTO repositories should be fairly up to date,
  72.   too (I can't know):
  73.  
  74.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/> (?)
  75.  
  76.   A french translation of this HOWTO (or most presumably an earlier
  77.   version of it) might sit around
  78.  
  79.   <ftp://ftp.ibp.fr/pub/linux/french/HOWTO/>
  80.  
  81.   1.3.2.  Other related documents
  82.  
  83.   ╖  If you don't know what free software is, please do read carefully
  84.      the GNU General Public License, which is used in a lot of free
  85.      software, and a model for most; it generally comes in a file named
  86.      COPYING, with a library version in a file named COPYING.LIB.
  87.      Litterature from the FSF (free software foundation) might help you,
  88.      too.
  89.  
  90.   ╖  Particularly, the interesting kind of free software comes with
  91.      sources that you can consult and correct, or sometimes even borrow
  92.      from.  Read your particular license carefully, and do comply to it.
  93.  
  94.   ╖  There is a FAQ for comp.lang.asm.x86 that answers generic questions
  95.      about x86 assembly programming, and questions about some commercial
  96.      assemblers in a 16-bit DOS environment.  Some of it apply to free
  97.      32-bit asm programming, so you may want to read this FAQ...
  98.  
  99.      <http://www2.dgsys.com/~raymoon/faq/asmfaq.zip>
  100.  
  101.   ╖  FAQs and docs exist about programming on your favorite platform,
  102.      whichever it is, that you should consult for platform-specific
  103.      issues not directly related to programming in assembler.
  104.  
  105.   1.4.  History
  106.  
  107.   Each version includes a few fixes and minor corrections, which needs
  108.   not be repeatedly mentionned every time.
  109.  
  110.      Version 0.1     23 Apr 1996
  111.         Francois-Rene "FarΘ" Rideau <rideau@ens.fr> creates and
  112.         publishes the first mini-HOWTO, because ``I'm sick of answering
  113.         ever the same questions on comp.lang.asm.x86''
  114.  
  115.      Version 0.2     4 May 1996
  116.         *
  117.  
  118.      Version 0.3c    15 Jun 1996
  119.         *
  120.  
  121.      Version 0.3f    17 Oct 1996
  122.         found -fasm option to enable GCC inline assembler w/o -O
  123.         optimizations
  124.  
  125.      Version 0.3g    2 Nov 1996
  126.         Created the History. Added pointers in cross-compiling section.
  127.         Added section about I/O programming under Linux (particularly
  128.         video).
  129.  
  130.      Version 0.3h    6 Nov 1996
  131.         more about cross-compiling -- See on sunsite: devel/msdos/
  132.  
  133.      Version 0.3i    16 Nov 1996
  134.         NASM is getting pretty slick
  135.  
  136.      Version 0.3j    24 Nov 1996
  137.         point to french translated version
  138.  
  139.      Version 0.3k    19 Dec 1996
  140.         What? I had forgotten to point to terse???
  141.  
  142.      Version 0.3l    11 Jan 1997
  143.         ?
  144.  
  145.      Version 0.4pre1 13 Jan 1997
  146.         text mini-HOWTO transformed into a full linuxdoc-sgml HOWTO, to
  147.         see what the SGML tools are like.
  148.  
  149.      Version 0.4     20 Jan 1997
  150.         first release of the HOWTO as such.
  151.  
  152.      Version 0.4a    20 Jan 1997
  153.         CREDITS section added
  154.  
  155.      Version 0.4b    3 Feb 1997
  156.         NASM moved: now is before AS86
  157.  
  158.      Version 0.4c    9 Feb 1997
  159.         Added section "DO YOU NEED ASSEMBLY?"
  160.  
  161.      Version 0.4d    28 Feb 1997
  162.         Vapor announce of a new Assembly-HOWTO maintainer.
  163.  
  164.      Version 0.4e    13 Mar 1997
  165.         Release for DrLinux
  166.  
  167.      Version 0.4f    20 Mar 1997
  168.         *
  169.  
  170.      Version 0.4g    30 Mar 1997
  171.         *
  172.  
  173.      Version 0.4h    19 Jun 1997
  174.         still more on "how not to use assembly"; updates on NASM, GAS.
  175.  
  176.      Version 0.4i    17 July 1997
  177.         info on 16-bit mode access from Linux.
  178.  
  179.      Version 0.4j    7 September 1997
  180.         introducing Paul Anderson as new maintainer.
  181.  
  182.         This is yet another last-release-by-FarΘ-before-new-maintainer-
  183.         takes-over (?)
  184.  
  185.   1.5.  Credits
  186.  
  187.   I would like to thanks the following persons, by order of appearance:
  188.  
  189.   ╖  Linus Torvalds <mailto:buried.alive@in.mail> for Linux
  190.  
  191.   ╖  Bruce Evans <mailto:bde@zeta.org.au> for bcc from which as86 is
  192.      extracted
  193.  
  194.   ╖  Simon Tatham <mailto:anakin@poboxes.com> and Julian Hall
  195.      <mailto:jules@earthcorp.com> for NASM
  196.  
  197.   ╖  Jim Neil <mailto:jim-neil@digital.net> for Terse
  198.  
  199.   ╖  Greg Hankins <mailto:gregh@sunsite.unc.edu> for maintaining HOWTOs
  200.  
  201.   ╖  Raymond Moon <mailto:raymoon@moonware.dgsys.com> for his FAQ
  202.  
  203.   ╖  Eric Dumas <mailto:dumas@excalibur.ibp.fr> for his translation of
  204.      the mini-HOWTO into french (sad thing for the original author to be
  205.      french and write in english)
  206.  
  207.   ╖  All the people who have contributed ideas, remarks, and moral
  208.      support.
  209.  
  210.   2.  DO YOU NEED ASSEMBLY?
  211.  
  212.   Well, I wouldn't want to interfere with what you're doing, but here
  213.   are a few advice from hard-earned experience.
  214.  
  215.   2.1.  Pros and Cons
  216.  
  217.   2.1.1.  The advantages of Assembly
  218.  
  219.   Assembly can express very low-level things:
  220.  
  221.   ╖  you can access machine-dependent registers and I/O.
  222.  
  223.   ╖  you can control the exact behavior of code in critical sections
  224.      that might involve hardware or I/O lock-ups
  225.  
  226.   ╖  you can break the conventions of your usual compiler, which might
  227.      allow some optimizations (like temporarily breaking rules about GC,
  228.      threading, etc).
  229.  
  230.   ╖  get access to unusual programming modes of your processor (e.g. 16
  231.      bit code for startup or BIOS interface on Intel PCs)
  232.  
  233.   ╖  you can build interfaces between code fragments using incompatible
  234.      conventions (e.g. produced by different compilers, or separated by
  235.      a low-level interface).
  236.  
  237.   ╖  you can produce reasonably fast code for tight loops to cope with a
  238.      bad non-optimizing compiler (but then, there are free optimizing
  239.      compilers available!)
  240.  
  241.   ╖  you can produce hand-optimized code that's perfectly tuned for your
  242.      particular hardware setup, though not to anyone else's.
  243.  
  244.   ╖  you can write some code for your new language's optimizing compiler
  245.      (that's something few will ever do, and even they, not often).
  246.  
  247.   2.1.2.  The disadvantages of Assembly
  248.  
  249.   Assembly is a very low-level language (the lowest above hand-coding
  250.   the binary instruction patterns).  This means
  251.  
  252.   ╖  it's long and tedious to write initially,
  253.  
  254.   ╖  it's very bug-prone,
  255.  
  256.   ╖  your bugs will be very difficult to chase,
  257.  
  258.   ╖  it's very difficult to understand and modify, i.e. to maintain.
  259.  
  260.   ╖  the result is very non-portable to other architectures, existing or
  261.      future,
  262.  
  263.   ╖  your code will be optimized only for a certain implementation of a
  264.      same architecture: for instance, among Intel-compatible platforms,
  265.      each CPU design and variation (bus width, cache size, relative
  266.      speed of CPU/cache/RAM/Bus/disks presence of FPU, MMX extensions,
  267.      etc) implies potentially completely different optimization
  268.      techniques.  CPU designs already include Intel 386, 486, Pentium,
  269.      PPro, Pentium II; Cyrix 5x86, 6x86; AMD K5, K6.  New designs keep
  270.      appearing, so don't expect either this listing or your code to be
  271.      up-to-date.
  272.  
  273.   ╖  your code might also be unportable accross different OS platforms
  274.      on the same architecture, by lack of proper tools.  (well, GAS
  275.      seems to work on all platforms; NASM seem to work or be workable on
  276.      all intel platforms).
  277.  
  278.   ╖  you spend more time on a few details, and can't focus on small and
  279.      large algorithmic design, that are known to bring the largest part
  280.      of the speed up.  [e.g. you might spend some time building very
  281.      fast list/array manipulation primitives in assembly; only a hash
  282.      table would have sped up your program much more; or, in another
  283.      context, a binary tree; or some high-level structure distributed
  284.      over a cluster of CPUs]
  285.  
  286.   ╖  a small change in algorithmic design might completely invalidate
  287.      all your existing assembly code.  So that either you're ready (and
  288.      able) to rewrite it all, or you're tied to a particular algorithmic
  289.      design;
  290.  
  291.   ╖  On code that ain't too far from what's in standard benchmarks,
  292.      commercial optimizing compilers outperform hand-coded assembly
  293.      (well, that's less true on the x86 architecture than on RISC
  294.      architectures, and perhaps less true for widely available/free
  295.      compilers; anyway, for typical C code, GCC is fairly good);
  296.  
  297.   ╖  And in any case, as says moderator John Levine on comp.compilers,
  298.      ``compilers make it a lot easier to use complex data structures,
  299.      and compilers don't get bored halfway through and generate reliably
  300.      pretty good code.''  They will also correctly propagate code
  301.      transformations throughout the whole (huge) program when optimizing
  302.      code between procedures and module boundaries.
  303.  
  304.   2.1.3.  Assessment
  305.  
  306.   All in all, you might find that though using assembly is sometimes
  307.   needed, and might even be useful in a few cases where it is not,
  308.   you'll want to:
  309.  
  310.   ╖  minimize the use of assembly code,
  311.  
  312.   ╖  encapsulate this code in well-defined interfaces
  313.  
  314.   ╖  have your assembly code automatically generated from patterns
  315.      expressed in a  higher-level language than assembly (from
  316.      ``macros'' to a high-level language)
  317.  
  318.   ╖  have automatic tools translate these programs into assembly code
  319.  
  320.   ╖  have this code be optimized if possible
  321.  
  322.   ╖  All of the above, i.e. write (an extension to) an optimizing
  323.      compiler back-end.
  324.  
  325.   Even in cases when Assembly is needed (e.g. OS development), you'll
  326.   find that not so much of it is, and that the above principles hold.
  327.  
  328.   See the sources for the Linux kernel about it: as little assembly as
  329.   needed, resulting in a fast, reliable, portable, maintainable OS.
  330.   Even a successful game like DOOM was massively written in C, with a
  331.   tiny part only being written in assembly for speed up.
  332.  
  333.   2.2.  How to NOT use Assembly
  334.  
  335.   2.2.1.  General procedure to achieve efficient code
  336.  
  337.   As says Charles Fiterman on comp.compilers about human vs computer-
  338.   generated assembly code,
  339.  
  340.   ``The human should always win and here is why.
  341.  
  342.   ╖  First the human writes the whole thing in a high level language.
  343.  
  344.   ╖  Second he profiles it to find the hot spots where it spends its
  345.      time.
  346.  
  347.   ╖  Third he has the compiler produce assembly for those small sections
  348.      of code.
  349.  
  350.   ╖  Fourth he hand tunes them looking for tiny improvements over the
  351.      machine generated code.
  352.  
  353.      The human wins because he can use the machine.''
  354.  
  355.   2.2.2.  Languages with optimizing compilers
  356.  
  357.   For instance, languages like ObjectiveCAML, SML, CommonLISP, Scheme,
  358.   ADA, Pascal, C, C++, all have free optimizing compilers that'll
  359.   optimize the bulk of your programs, and often do better than hand-
  360.   coded assembly even for tight loops, while allowing you to focus on
  361.   higher-level details, and without forbidding you to grab a few percent
  362.   of extra performance once you've reached a stable design.  Of course,
  363.   there are also commercial optimizing compilers for most of these
  364.   languages, too!
  365.  
  366.   Some languages have compilers that produce C code, which can be
  367.   further optimized by a C compiler.  LISP, Scheme, Perl, and many other
  368.   are suches.  Speed is fairly good.
  369.  
  370.   2.2.3.  General procedure to speed your code up
  371.  
  372.   As for speeding code up, you should do it only for parts of a program
  373.   that a profiling tool has consistently identified as being a
  374.   performance bottleneck.
  375.  
  376.   Hence, if you identify some code portion as being too slow, you should
  377.  
  378.   ╖  first try to use a better algorithm;
  379.  
  380.   ╖  then try to compile it rather than interpret it;
  381.  
  382.   ╖  then try to enable optimization from your compiler;
  383.  
  384.   ╖  then give the compiler hints about how to optimize (typing
  385.      information in LISP; register usage with GCC; lots of options in
  386.      most compilers, etc).
  387.  
  388.   ╖  then possibly fallback to assembly programming
  389.  
  390.   Finally, before you end up writing assembly, you should inspect
  391.   generated code, to check that the problem really is with bad code
  392.   generation, as this might really not be the case: compiler-generated
  393.   code might be better than what you'd have written, particularly on
  394.   modern pipelined architectures!  Slow parts of a program might be
  395.   intrinsically so.  Perhaps a completely different approach to the
  396.   problem might help, then.
  397.  
  398.   2.2.4.  Inspecting compiler-generated code
  399.  
  400.   There are many reasons to inspect compiler-generated assembly code.
  401.   Here are what you'll do with such code:
  402.  
  403.   ╖  check whether generated code can be obviously enhanced with hand-
  404.      coded assembly
  405.  
  406.   ╖  when that's the case, start from generated code and modify it
  407.      instead of starting from scratch
  408.  
  409.   ╖  more generally, use generated code as stubs to modify, which at
  410.      least gets right the way your assembly routines interface to the
  411.      external world
  412.  
  413.   ╖  track down bugs in your compiler (hopefully rarer)
  414.  
  415.   The standard way to have assembly code be generated is to invoke your
  416.   compiler with the -S flag.  This works with most Unix compilers,
  417.   including the GNU C Compiler (GCC), but YMMV.  As for GCC, it will
  418.   produce more understandable assembly code with the -fverbose-asm
  419.   command-line option.  Of course, if you want to get good assembly
  420.   code, don't forget your usual optimization options and hints!
  421.  
  422.   3.  ASSEMBLERS
  423.  
  424.   3.1.  GCC Inline Assembly
  425.  
  426.   The well-known GNU C/C++ Compiler (GCC), an optimizing 32-bit compiler
  427.   at the heart of the GNU project, supports the x86 architecture quite
  428.   well, and includes the ability to insert assembly code in C programs,
  429.   in such a way that register allocation can be either specified or left
  430.   to GCC.  GCC works on most available platforms, notably Linux, *BSD,
  431.   VSTa, OS/2, *DOS, Win*, etc.
  432.  
  433.   3.1.1.  Where to find GCC
  434.  
  435.   The original GCC site is
  436.  
  437.   <ftp://prep.ai.mit.edu/pub/gnu/>
  438.  
  439.   together with all the released application software from the GNU
  440.   project.  However, there exists a lot of mirrors.
  441.  
  442.   However, sources adapted to your favorite OS, and binaries precompiled
  443.   for it, should be found at your usual FTP sites.
  444.  
  445.   For GCC under Linux, see around
  446.  
  447.   <http://www.linux.org.uk/>
  448.  
  449.   For most popular DOS port of GCC is named DJGPP, and can be found in
  450.   directories of such name in FTP sites. See:
  451.  
  452.   <http://www.delorie.com/djgpp/>
  453.  
  454.   There is also a port of GCC to OS/2 named EMX, that also works under
  455.   DOS, and includes lots of unix-emulation library routines.  See
  456.   around:
  457.  
  458.   <http://www.leo.org/pub/comp/os/os2/gnu/emx+gcc/>
  459.  
  460.   <http://warp.eecs.berkeley.edu/os2/software/shareware/emx.html>
  461.  
  462.   <ftp://ftp-os2.cdrom.com/pub/os2/emx09c/>
  463.  
  464.   3.1.2.  Where to find docs for GCC Inline Asm
  465.  
  466.   The documentation of GCC includes documentation files in texinfo
  467.   format, that you can convert to tex, compile (with tex), and print,
  468.   convert to interactive emacs .info format and browse, convert (with
  469.   the right tools) to whatever you like, or just read as is.  The .info
  470.   files are generally found on any good installation for GCC.
  471.  
  472.   The right section to look for is: C Extensions::Extended Asm::
  473.  
  474.   Section Invoking GCC::Submodel Options::i386 Options:: might help too.
  475.   Particularly, it gives the i386 specific constraint names for
  476.   registers: abcdSDB correspond to %eax, %ebx, %ecx, %edx, %esi, %edi,
  477.   %ebp respectively (no letter for %esp).
  478.  
  479.   The DJGPP Games resource (not only for game hackers) has this page
  480.   specifically about assembly:
  481.  
  482.   <http://www.rt66.com/~brennan/djgpp/djgpp_asm.html>
  483.  
  484.   Finally, there is a web page called, ``DJGPP Quick ASM Programming
  485.   Guide'', that covers URLs to FAQs, AT&T x86 ASM Syntax, Some inline
  486.   ASM information, and converting .obj/.lib files:
  487.  
  488.   <http://remus.rutgers.edu/~avly/djasm.html>
  489.  
  490.   GCC depends on GAS for assembling, and follow its syntax (see below);
  491.   do mind that inline asm needs percent characters to be quoted so they
  492.   be passed to GAS.  See the section about GAS below.
  493.  
  494.   Find lots of useful examples in the linux/include/asm-i386/
  495.   subdirectory of the sources for the Linux kernel.
  496.  
  497.   3.1.3.  Invoking GCC to have it properly inline assembly code ?
  498.  
  499.   Be sure to invoke GCC with the -O flag (or -O2, -O3, etc), to enable
  500.   optimizations and inline assembly.  If you don't, your code may
  501.   compile, but not run properly!!!  Actually (kudos to Tim Potter,
  502.   timbo@moshpit.air.net.au), it is enough to use the -fasm flag, which
  503.   is part of all the features enabled by -O.  So if you have problems
  504.   with buggy optimizations in your particular implementation/version of
  505.   GCC, you can still use inline asm.  Similarly, use -fno-asm to disable
  506.   inline assembly (why would you?).
  507.  
  508.   More generally, good compile flags for GCC on the x86 platform are
  509.  
  510.   ______________________________________________________________________
  511.           gcc -O2 -fomit-frame-pointer -m386
  512.   ______________________________________________________________________
  513.  
  514.   -O2 is the good optimization level. Optimizing besides it yields code
  515.   that is a lot larger, but only a bit faster; such overoptimizationn
  516.   might be useful for tight loops only (if any), which you may be doing
  517.   in assembly anyway; if you need that, do it just for the few routines
  518.   that need it.
  519.  
  520.   -fomit-frame-pointer allows generated code to skip the stupid frame
  521.   pointer maintenance, which makes code smaller and faster, and frees a
  522.   register for further optimizations.  It precludes the easy use of
  523.   debugging tools (gdb), but when you use these, you just don't care
  524.   about size and speed anymore anyway.
  525.  
  526.   -m386 yields more compact code, without any measurable slowdown, (note
  527.   that small code also means less disk I/O and faster execution) but
  528.   perhaps on the above-mentioned tight loops; you might appreciate
  529.   -mpentium for special pentium-optimizing GCC targetting a specifically
  530.   pentium platform.
  531.  
  532.   To optimize even more, option -mregparm=2 and/or corresponding
  533.   function attribute might help, but might pose lots of problems when
  534.   linking to foreign code...
  535.  
  536.   Note that you can add make these flags the default by editing file
  537.   /usr/lib/gcc-lib/i486-linux/2.7.2.2/specs or wherever that is on your
  538.   system.
  539.  
  540.   3.2.  GAS
  541.  
  542.   GAS is the GNU Assembler, that GCC relies upon, with
  543.  
  544.   3.2.1.  Where to find it
  545.  
  546.   Find it at the same place where you found GCC, in a package named
  547.   binutils.
  548.  
  549.   3.2.2.  What is this AT&T syntax
  550.  
  551.   Because GAS was invented to support a 32-bit unix compiler, it uses
  552.   standard ``AT&T'' syntax, which resembles a lot the syntax for
  553.   standard m68k assemblers.  This syntax is no worse, no better than the
  554.   ``Intel'' syntax.  It's just different.  When you get used to it, you
  555.   find it much more regular than the Intel syntax, though a bit boring.
  556.  
  557.   A program exists to help you convert programs from TASM syntax to AT&T
  558.   syntax. See
  559.  
  560.   <ftp://x2ftp.oulu.fi/pub/msdos/programming/convert/ta2asv08.zip>
  561.  
  562.   A file gas.doc or as.doc (still around the same place as you found
  563.   GAS, if not in the GAS source package itself) describes the syntax.
  564.   Of course, the ultimate documentation is the sources themselves!
  565.  
  566.   One place for it is in FTP directory
  567.  
  568.   <ftp://sunsite.unc.edu/pub/Linux/GCC/>
  569.  
  570.   <ftp://sunsite.doc.ic.ac.uk/packages/linux/sunsite.unc-mirror/GCC/>
  571.   (?)
  572.  
  573.   Again, the sources for Linux (the OS kernel), come in as good
  574.   examples; see under linux/arch/i386, the following files: kernel/*.S,
  575.   boot/compressed/*.S, mathemu/*.S
  576.  
  577.   If you are writing kind of a language, a thread package, etc you might
  578.   as well see how other languages (OCaml, gforth, etc), or thread
  579.   packages (QuickThreads, MIT pthreads, LinuxThreads, etc), or whatever,
  580.   do it.
  581.  
  582.   Finally, just compiling a C program to assembly might show you the
  583.   syntax for the kind of instructions you want.  See section ``Do you
  584.   need Assembly?'' above.
  585.  
  586.   3.2.3.  Limited 16-bit mode
  587.  
  588.   GAS is a 32-bit assembler, meant to support a 32-bit compiler.  It
  589.   currently has only limited support for 16-bit mode, which consists in
  590.   prepending the 32-bit prefixes to instructions, so you write 32-bit
  591.   code that runs in 16-bit mode on a 32 bit CPU.  In both modes, it
  592.   supports 16-bit register usage, but what is unsupported is 16-bit
  593.   addressing.  Use the directive code16 and code32 to switch between
  594.   modes.  Note that an inline assembly statement asm("code16\n") will
  595.   allow GCC to produce 32-bit code that'll run in real mode!
  596.  
  597.   Bryan Ford (please confirm?) has added most code needed to fully
  598.   support 16-bit mode programming with GAS, but because it's not much
  599.   used and documented yet, bugs might lurk in, so watch for them,
  600.   report'em, fix'em.
  601.  
  602.   A cheap solution is to define macros (see below) that somehow produce
  603.   the binary encoding for just the 16-bit mode instructions you need
  604.   (almost nothing if you use code16 as above, and can safely assume the
  605.   code will run on a 32-bit capable x86 CPU).  To find the proper
  606.   encoding, you can get inspiration from the sources of 16-bit capable
  607.   assemblers for the encoding.
  608.  
  609.   3.3.  GASP
  610.  
  611.   GASP is the GAS Preprocessor.  It adds macros and some nice syntax to
  612.   GAS.
  613.  
  614.   3.3.1.  Where to find GASP
  615.  
  616.   GASP comes together with GAS in the GNU binutils archive.
  617.  
  618.   3.3.2.  How it works
  619.  
  620.   It works as a filter, much like cpp and the like.  I have no idea on
  621.   details, but it comes with its own texinfo documentation, so just
  622.   browse them (in .info), print them, grok them.  GASP|GAS Looks like a
  623.   regular macro-assembler to me.
  624.  
  625.   3.4.  NASM
  626.  
  627.   The Netwide Assembler project is producing yet another assembler,
  628.   written in C, that should be modular enough to eventually support all
  629.   known syntaxes and object formats.
  630.  
  631.   3.4.1.  Where to find NASM
  632.  
  633.   <http://www.cryogen.com/Nasm>
  634.  
  635.   Binary release on your usual sunsite mirror in devel/lang/asm/ Should
  636.   also be available as .rpm or .deb in your usual RedHat/Debian
  637.   distributions' contrib.
  638.  
  639.   3.4.2.  What it does
  640.  
  641.   At the time this HOWTO is written, current NASM version is 0.95.
  642.  
  643.   The syntax is very simple (simplified MASM style).  Now has integrated
  644.   macroprocessing support.
  645.  
  646.   Supported object file formats are bin, aout, coff, elf, as86, (DOS)
  647.   obj, win32, (their own format) rdf.
  648.  
  649.   NASM can be used as a backend for the free LCC compiler (support files
  650.   included).
  651.  
  652.   Surely NASM evolves too fast for this HOWTO to be kept up to date.
  653.   Unless you're using BCC as a 16-bit compiler (which is out of scope of
  654.   this 32-bit HOWTO), you should use NASM instead of say AS86 or MASM,
  655.   because it is actively supported online, and runs on all platforms.
  656.   Note: NASM also comes with a disassembler, NDISASM.
  657.  
  658.   Its hand-written parser is much faster than GAS, though of course, it
  659.   doesn't support three bazillion different architectures.  For the x86
  660.   target, it should be the assembler of choice...
  661.  
  662.   3.5.  AS86
  663.  
  664.   AS86 is a 80x86 assembler, both 16-bit and 32-bit, part of Bruce
  665.   Evans' C Compiler (BCC).  It has mostly Intel-syntax, though it
  666.   differs slightly as for addressing modes.
  667.  
  668.   3.5.1.  Where to get AS86
  669.  
  670.   A completely outdated version of AS86 is distributed by HJLu just to
  671.   compile the Linux kernel, in a package named bin86 (current version
  672.   0.4), available in any Linux GCC repository.  But I advise no one to
  673.   use it for anything else but compiling Linux.  This version supports
  674.   only a hacked minix object file format, which is not supported by the
  675.   GNU binutils or anything, and it has a few bugs in 32-bit mode, so you
  676.   really should better keep it only for compiling Linux.
  677.  
  678.   The most recent versions by Bruce Evans (bde@zeta.org.au) are
  679.   published together with the FreeBSD distribution.  Well, they were: I
  680.   could not find the sources from distribution 2.1 on :( Hence, I put
  681.   the sources at my place:
  682.  
  683.   <http:///www.eleves.ens.fr:8080/home/rideau/files/bcc-95.3.12.src.tgz>
  684.  
  685.   The Linux/8086 (aka ELKS) project is somehow maintaining bcc (though I
  686.   don't think they included the 32-bit patches).  See around
  687.   <http://www.linux.org.uk/Linux8086.html> <ftp://linux.mit.edu/>
  688.  
  689.   Among other things, these versions, unlike HJLu's, supports Linux GNU
  690.   a.out format, so you can link you code to Linux programs, and/or use
  691.   the usual tools from the GNU binutil package to manipulate your data.
  692.   This version can co-exist without any harm with the previous one (see
  693.   question 2.4.4 below).
  694.  
  695.   BCC from 12 march 1995 and earlier version has a misfeature that makes
  696.   all segment pushing/popping 16-bit, which is quite annoying when
  697.   programming in 32-bit mode.  A patch is published in the Tunes project
  698.   <http://www.eleves.ens.fr:8080/home/rideau/Tunes/> subpage
  699.   files/tgz/tunes.0.0.0.25.src.tgz in unpacked subdirectory LLL/i386/
  700.   The patch should also be in available directly from
  701.   <http://www.eleves.ens.fr:8080/home/rideau/files/as86.bcc.patch.gz>
  702.   Bruce Evans accepted this patch, so if there is a more recent version
  703.   of bcc somewhere someday, the patch should have been included...
  704.  
  705.   3.5.2.  How to invoke the assembler?
  706.  
  707.   Here's the GNU Makefile entry for using bcc to transform .s asm into
  708.   both GNU a.out .o object and .l listing:
  709.  
  710.   ______________________________________________________________________
  711.   %.o %.l:        %.s
  712.           bcc -3 -G -c -A-d -A-l -A$*.l -o $*.o $<
  713.   ______________________________________________________________________
  714.  
  715.   Remove the %.l, -A-l, and -A$*.l, if you don't want any listing.  If
  716.   you want something else than GNU a.out, you can see the docs of bcc
  717.   about the other supported formats, and/or use the objcopy utility from
  718.   the GNU binutils package.
  719.  
  720.   3.5.3.  Where to find docs
  721.  
  722.   The docs are what is included in the bcc package.  Man pages are also
  723.   available somewhere on the FreeBSD site.  When in doubt, the sources
  724.   themselves are often a good docs: it's not very well commented, but
  725.   the programming style is clear.  You might try to see how as86 is used
  726.   in Tunes 0.0.0.25...
  727.  
  728.   3.5.4.  What if I can't compile Linux anymore with this new version ?
  729.  
  730.   Linus is buried alive in mail, and my patch for compiling Linux with a
  731.   Linux a.out as86 didn't make it to him (!).  Now, this shouldn't
  732.   matter: just keep your as86 from the bin86 package in /usr/bin, and
  733.   put the good as86 as /usr/local/libexec/i386/bcc/as where it should
  734.   be. You never need explicitly call this ``good'' as86, because bcc
  735.   does everything right, including conversion to Linux a.out, when
  736.   invoked with the right options; so assemble files exclusively with bcc
  737.   as a frontend, not directly with as86.
  738.  
  739.   3.6.  OTHER ASSEMBLERS
  740.  
  741.   These are other, non-regular, options, in case the previous didn't
  742.   satisfy you (why ?), that I don't recommend in the usual (?) case, but
  743.   that could be useful if the assembler is part of what you're designing
  744.   (i.e. an OS or development environment).
  745.  
  746.   3.6.1.  Win32Forth assembler
  747.  
  748.   Win32Forth is a free 32-bit FORTH system that successfully runs under
  749.   Win32s, Win95, Win/NT.  It includes a free 32-bit assembler (either
  750.   prefix or postfix syntax) integrated to the assembler.  Macro
  751.   processing is done with the full power of the reflective language
  752.   FORTH; however, the only supported input and output contexts is
  753.   Win32For itself (no dumping of .obj file -- you could add that
  754.   yourself, of course).  Find it at
  755.   <ftp://ftp.forth.org/pub/Forth/win32for/>
  756.  
  757.   3.6.2.  Terse
  758.  
  759.   Terse is a programming tool that provides THE most compact assembler
  760.   syntax for the intel x86 family!  See <http://www.terse.com>
  761.  
  762.   3.6.3.  Non-free and/or Non-32bit x86 assemblers.
  763.  
  764.   You may find more about them, together with the basics of x86 assembly
  765.   programming, in Raymond Moon's FAQ for comp.lang.asm.x86
  766.   <http://www2.dgsys.com/~raymoon/faq/asmfaq.zip>
  767.  
  768.   Note that all DOS-based assemblers should work inside the Linux DOS
  769.   Emulator, as well as other similar emulators, so that if you already
  770.   own one, you can still use it inside a real OS.  Recent DOS-based
  771.   assemblers also support COFF and/or other object file formats that are
  772.   supported by the GNU BFD library, so that you can use them together
  773.   with your free 32-bit tools, perhaps using GNU objcopy (part of the
  774.   binutils) as a conversion filter.
  775.  
  776.   4.  METAPROGRAMMING/MACROPROCESSING
  777.  
  778.   Assembly programming is a bore, but for critical parts of programs.
  779.   You should use the appropriate tool for the right task, so don't
  780.   choose assembly when it's not fit; C, OCAML, perl, Scheme, might be a
  781.   better choice for most of your programming.  However, there are cases
  782.   when these tools do not give a fine enough control on the machine, and
  783.   assembly is useful or needed.  In those case, you'll appreciate a
  784.   system of macroprocessing and metaprogramming that'll allow recurring
  785.   patterns to be defined once, and reused multiple times, by
  786.   automatically inline expansion, which allows safer programming, easier
  787.   modification, etc.  A ``plain'' assembler is often not enough, even
  788.   when one is doing only small routines to link with C.
  789.  
  790.   4.1.  What's integrated into the above
  791.  
  792.   Yes I know this section does not contain much useful up-to-date
  793.   information.  Feel free to contribute what you discover the hard
  794.   way...
  795.  
  796.   4.1.1.  GCC
  797.  
  798.   GCC allows (and requires) you to specify register constraints in your
  799.   ``inline assembly'' code, so the optimizer always know about it; thus,
  800.   inline assembly code is really made of patterns, not forcibly exact
  801.   code.
  802.  
  803.   Then, you can make put your assembly into CPP macros, so anyone can
  804.   use it in as any C function/macro.  and inline C functions Inline
  805.   functions resemble macros very much, but are sometimes cleaner to use.
  806.   Beware that in those cases, code will be duplicated, so only local
  807.   labels (of 1: style) should be defined in that asm code.  However, a
  808.   macro would allow the name for a non local defined label to be passed.
  809.   Also, note that some bug in your code or in GCC may appear when
  810.   inlining functions with asm code where the register constraints
  811.   weren't declared properly and/or confuse GCC.
  812.  
  813.   Lastly, the C language itself may be considered as a good abstraction
  814.   to assembly programming, which relieves you from most of the trouble
  815.   of assembling.
  816.  
  817.   Beware that some optimizations that involve passing arguments to
  818.   functions through registers may make those functions unsuitable to be
  819.   called from assembly in the standard way, least you give them the
  820.   attribute asmlinkage.  See the linux kernel sources for examples.
  821.  
  822.   4.1.2.  GAS
  823.  
  824.   GAS has absolutely NO macro capability included (well had; I've been
  825.   told later versions have some -- read the texinfo docs).  However, GCC
  826.   and passes .S files through CPP before to feed them to GAS.  Files
  827.   whose name ends in .s are the generated ones, and are passed directly
  828.   to GAS.  Again and again, see Linux sources for examples.
  829.  
  830.   4.1.3.  GASP
  831.  
  832.   It adds all the usual macroassembly tricks to GAS.  See its texinfo
  833.   docs.
  834.  
  835.   4.1.4.  NASM
  836.  
  837.   NASM has some macro support that keeps getting improved.  See
  838.   according docs.  If you have some bright idea, you might wanna contact
  839.   the authors, as they are actively developing it.  Meanwhile, see about
  840.   external filters below.
  841.  
  842.   4.1.5.  AS86
  843.  
  844.   It has some simple macro support, but I couldn't find docs.  Now the
  845.   sources are very straightforward, so if you're interested, you should
  846.   understand them easily.  If you need more than the basics, you should
  847.   use an external filter (see below in section 3.2)
  848.  
  849.   4.1.6.  OTHER ASSEMBLERS
  850.  
  851.   Win32FORTH: CODE and END-CODE are macros that do not switch from
  852.   interpretation mode, so you have access to the full power of FORTH
  853.   words, immediate or not, while assembling.
  854.  
  855.   TUNES: it doesn't work yet, but the Scheme language is a real high-
  856.   level language that allows arbitrary meta-programming.
  857.  
  858.   4.2.  External Filters
  859.  
  860.   Whatever is the macro support from your assembler, or whatever
  861.   language you use (even C !), if the language is not expressive enough
  862.   to you, you can have files passed through an external filter with a
  863.   Makefile rule like that:
  864.  
  865.   ______________________________________________________________________
  866.   %.s:    %.S other_dependencies
  867.           $(FILTER) $(FILTER_OPTIONS) < $< > $@
  868.   ______________________________________________________________________
  869.  
  870.   4.2.1.  CPP
  871.  
  872.   CPP is truely not very expressive, but it's enough for easy things,
  873.   it's standard, and called transparently by GCC.
  874.  
  875.   As an example of its limitations, you can't declare objects so that
  876.   destructors are automatically called at the end of the declaring
  877.   block, you can't co-declared data and the code to process it, etc.
  878.  
  879.   CPP came with your C compiler. If you could make it without one, don't
  880.   bother fetching any (though I wonder how you could).  GCC (see above)
  881.   is a free C compiler you could have fetched.
  882.  
  883.   4.2.2.  M4
  884.  
  885.   M4 gives you the full power of macroprocessing, with a Turing
  886.   equivalent language, recursion, regular expressions, etc.  You can do
  887.   with it everything that CPP cannot.
  888.  
  889.   See macro4th/This4th from <ftp://ftp.forth.org/pub/Forth/> in
  890.   Reviewed/ ANS/ (?), or the Tunes 0.0.0.25 sources as examples of
  891.   advanced macroprogramming using m4.
  892.  
  893.   However, its fucked up quoting semantics force you to use explicit
  894.   continuation-passing tail-recursive macro style if you want to do
  895.   advanced macro programming (which is remindful of TeX -- BTW, has
  896.   anyone tried to use TeX as a macroprocessor for anything else than
  897.   typesetting ?).  This is NOT worse than CPP that does not allow
  898.   quoting and recursion anyway.
  899.  
  900.   The right version of m4 to get is GNU m4 1.4 (or later if exists),
  901.   which has the most features and the least bugs or limitations of all.
  902.  
  903.   4.2.3.  Macroprocessing with yer own filter
  904.  
  905.   You can write your own simple macro-expansion filter with the usual
  906.   tools: perl, awk, sed, etc.  That's quick to do, and you control
  907.   everything.  But of course, any power in macroprocessing must be
  908.   earned the hard way.
  909.  
  910.   4.2.4.  Metaprogramming
  911.  
  912.   Instead of using an external filter that expands macros, one way to do
  913.   things is to write programs that write part or all of other programs.
  914.  
  915.   For instance, you could use a program outputing source code to
  916.   generate sine/cosine/whatever lookup tables, to extract a source-form
  917.   representation of a binary file, to compile your bitmaps into fast
  918.   display routines, to extract documentation,
  919.   initialization/finalization code, description tables, as well as
  920.   normal code from the same source files, to have customized assembly
  921.   code, generated from a perl/shell/scheme script that does arbitrary
  922.   processing, (particularly useful when some kind of data must be
  923.   mirrored at into many cross-referencing tables and code chunks).  etc.
  924.  
  925.   Think about it !
  926.  
  927.   4.2.4.1.  Backends from existing compilers
  928.  
  929.   Compilers like SML/NJ, Objective CAML, MIT-Scheme, etc, do have their
  930.   own generic assembler backend, which you might or not want to use, if
  931.   you intend to generate code semi-automatically from the according
  932.   languages.
  933.  
  934.   4.2.4.2.  The New-Jersey Machine-Code Toolkit
  935.  
  936.   There is a project, using the programming language Icon, to build a
  937.   basis for producing assembly-manipulating code.  See around
  938.   <http://www.cs.virginia.edu/~nr/toolkit/>
  939.  
  940.   4.2.4.3.  Tunes
  941.  
  942.   The Tunes OS project is developping its own assembler as an extension
  943.   to the Scheme language, as part of its development process.  It
  944.   doesn't run at all yet, though help is welcome.
  945.  
  946.   The assembler manipulates symbolic syntax trees, so it could equally
  947.   serve as the basis for a assembly syntax translator, a disassembler, a
  948.   common assembler/compiler back-end, etc.  Also, the full power of a
  949.   real language, Scheme, make it unchallenged as for
  950.   macroprocessing/metaprograming.
  951.  
  952.   <http://www.eleves.ens.fr:8080/home/rideau/Tunes/>
  953.  
  954.   5.  CALLING CONVENTIONS
  955.  
  956.   5.1.  Linux
  957.  
  958.   5.1.1.  Linking to GCC
  959.  
  960.   That's the preferred way.  Check GCC docs and examples from Linux
  961.   kernel .S files that go through gas (not those that go through as86).
  962.  
  963.   32-bit arguments are pushed down stack in reverse order (hence
  964.   accessed/popped in the right order) above the 32-bit near return
  965.   address.  %ebp, %esi, %edi, %ebx are preserved, %eax holds the result,
  966.   or %edx:%eax for 64-bit results.
  967.  
  968.   FP stack: I'm not sure, but I think it's result in st(0), whole stack
  969.   callee-saved.
  970.  
  971.   Note that GCC has options to modify the calling conventions by
  972.   reserving registers, having arguments in registers, not assuming the
  973.   FPU, etc. Check the i386 info pages.
  974.  
  975.   Beware that you must then declare the cdecl attribute for a function
  976.   that will follow standard GCC calling conventions (I don't know what
  977.   it does with modified calling conventions).  See in the GCC info pages
  978.   the section: C Extensions::Extended Asm::
  979.  
  980.   5.1.2.  ELF vs a.out problems
  981.  
  982.   Some C compilers prepend an underscore before every symbol, while
  983.   others do not.
  984.  
  985.   Particularly, Linux a.out GCC does such prepending, while Linux ELF
  986.   GCC does not.
  987.  
  988.   If you need cope with both behaviors at once, see how existing
  989.   packages do.  For instance, get an old Linux source tree, the Elk,
  990.   qthreads, or OCAML...
  991.  
  992.   You can also override the implicit C->asm renaming by inserting
  993.   statements like
  994.  
  995.   ______________________________________________________________________
  996.           void foo asm("bar") (void);
  997.   ______________________________________________________________________
  998.  
  999.   to be sure that the C function foo will be called really bar in assem¡
  1000.   bly.
  1001.  
  1002.   Note that the utility objcopy, from the binutils package, should allow
  1003.   you to transform your a.out objects into ELF objects, and perhaps the
  1004.   contrary too, in some cases.  More generally, it will do lots of file
  1005.   format conversions.
  1006.  
  1007.   5.1.3.  Direct Linux syscalls
  1008.  
  1009.   This is specifically NOT recommended, because it may change, it's not
  1010.   portable, it's a burden to write, it's redundant with the libc effort,
  1011.   AND it precludes fixes and extensions that are made to the libc, like,
  1012.   for instance the zlibc package, that does on-the-fly transparent
  1013.   decompression of gzip-compressed files.  The standard, recommended way
  1014.   to call Linux system services is, and will stay, to go through the
  1015.   libc.
  1016.  
  1017.   Shared objects should keep your stuff small.  And if you really want
  1018.   smaller binaries, do use #! stuff, with the interpreter having all the
  1019.   overhead you want to keep out of your binaries.
  1020.  
  1021.   Now, if for some reason, you don't want to link to the libc, go get
  1022.   the libc and understand how it works!  After all, you're pretending to
  1023.   replace it, ain't you?  You might take a look at how my eforth 1.0c
  1024.   <ftp://ftp.forth.org/pub/Forth/Linux/linux-eforth-1.0c.tgz> does it.
  1025.  
  1026.   The sources for Linux come in handy, too, particularly the
  1027.   asm/unistd.h header file, that describes how to do system calls...
  1028.  
  1029.   Basically, you issue an int $0x80, with the __NR_syscallname number
  1030.   (from asm/unistd.h) in %eax, and parameters (up to five) in %ebx,
  1031.   %ecx, %edx, %esi, %edi respectively.  Result is returned in %eax, with
  1032.   a negative result being an error whose opposite is what libc would put
  1033.   in errno.  The user-stack is not touched, so you needn't have a valid
  1034.   one when doing a syscall.
  1035.  
  1036.   5.1.4.  I/O under Linux
  1037.  
  1038.   If you want to do direct I/O under Linux, either it's something very
  1039.   simple that needn't OS arbitration, and you should see the IO-Port-
  1040.   Programming mini-HOWTO; or it needs a kernel device driver, and you
  1041.   should try to learn more about kernel hacking, device driver
  1042.   development, kernel modules, etc, for which there are other excellent
  1043.   HOWTOs and documents from the LDP.
  1044.  
  1045.   Particularly, if what you want is Graphics programming, then do join
  1046.   the GGI project: <http://synergy.caltech.edu/~ggi/>
  1047.   <http://sunserver1.rz.uni-duesseldorf.de/~becka/doc/scrdrv.html>
  1048.  
  1049.   Anyway, in all these cases, you'll be better off using GCC inline
  1050.   assembly with the macros from linux/asm/*.h than writing full assembly
  1051.   source files.
  1052.  
  1053.   5.1.5.  Accessing 16-bit drivers from Linux/i386
  1054.  
  1055.   Such thing is theoretically possible (proof: see how DOSEMU can
  1056.   selectively grant hardware port access to programs),and I've heard
  1057.   rumors that someone somewhere did actually do it (in the PCI driver?
  1058.   Some VESA access stuff? ISA PnP? dunno).  If you have some more
  1059.   precise information on that, you'll be most welcome.  Anyway, good
  1060.   places to look for more information are the Linux kernel sources,
  1061.   DOSEMU sources (and other programs in the DOSEMU repository
  1062.   <ftp://tsx-11.mit.edu/pub/linux/ALPHA/dosemu/>), and sources for
  1063.   various low-level programs under Linux...  (perhaps GGI if they do
  1064.   VESA support).
  1065.  
  1066.   Basically, you must either use 16-bit protected mode or vm86 mode.
  1067.  
  1068.   The first is simpler to setup, but only works with well-behaved code
  1069.   that won't do any kind of segment arithmetics or absolute segment
  1070.   addressing (particularly addressing segment 0), unless by chance it
  1071.   happens that all segments used can be setup in advance in the LDT.
  1072.  
  1073.   The later allows for more "compatibility" with vanilla 16-bit
  1074.   environments, but requires more complicated handling.
  1075.  
  1076.   In both cases, before you can jump to 16-bit code, you must
  1077.  
  1078.   ╖  mmap any absolute address used in the 16-bit code (such as ROM,
  1079.      video buffers, DMA targets, and memory-mapped I/O) from /dev/mem to
  1080.      your process' address space,
  1081.  
  1082.   ╖  setup the LDT and/or vm86 mode monitor.
  1083.  
  1084.   ╖  grab proper I/O permissions from the kernel (see the above section)
  1085.  
  1086.   Again, carefully read the source for the stuff contributed to the
  1087.   DOSEMU repository above, particularly these mini-emulators for running
  1088.   ELKS and/or simple .COM programs under Linux/i386.
  1089.  
  1090.   5.2.  DOS
  1091.  
  1092.   Most DOS extenders come with some interface to DOS services.  Read
  1093.   their docs about that, but often, they just simulate int $0x21 and
  1094.   such, so you do ``as if'' you were in real mode (I doubt they have
  1095.   more than stubs and extend things to work with 32-bit operands; they
  1096.   most likely will just reflect the interrupt into the real-mode or vm86
  1097.   handler).
  1098.  
  1099.   Docs about DPMI and such (and much more) can be found on
  1100.   <ftp://x2ftp.oulu.fi/pub/msdos/programming/>
  1101.  
  1102.   DJGPP comes with its own (limited) glibc
  1103.   derivative/subset/replacement, too.
  1104.  
  1105.   It is possible to cross-compile from Linux to DOS, see the
  1106.   devel/msdos/ directory of your local FTP mirror for sunsite.unc.edu
  1107.   Also see the MOSS dos-extender from the Flux project in utah.
  1108.  
  1109.   Other documents and FAQs are more DOS-centered.  We do not recommend
  1110.   DOS development.
  1111.  
  1112.   5.3.  Winblows and suches
  1113.  
  1114.   Hey, this document covers only free software.  Ring me when Winblows
  1115.   becomes free, or when there are free dev tools for it!
  1116.  
  1117.   Well, after all there is: Cygnus Solutions <http://www.cygnus.com> has
  1118.   developped the cygwin32.dll library, for GNU programs to run on
  1119.   MacroShit platforms.  Thus, you can use GCC, GAS, all the GNU tools,
  1120.   and many other Unix applications.  Have a look around their homepage.
  1121.   I (FarΘ) don't intend to expand on Losedoze programming, but I'm sure
  1122.   you can find lots of documents about it everywhere...
  1123.  
  1124.   5.4.  Yer very own OS
  1125.  
  1126.   Control being what attract many programmers to assembly, want of OS
  1127.   development is often what leads to or stems from assembly hacking.
  1128.   Note that any system that allows self-development could be qualified
  1129.   an "OS" even though it might run "on top" of an underlying system that
  1130.   does I/O (much like Linux over Mach, OpenGenera over Unix, etc).
  1131.   Hence, for easier debugging purpose, you might like to develop your
  1132.   ``OS'' first as a process running on top of Linux (despite the
  1133.   slowness), then use the Flux OS kit
  1134.   <http://ww.cs.utah.edu/projects/flux/> (which grants use of Linux and
  1135.   BSD drivers in yer own OS), to make it standalone.  When your OS is
  1136.   stable, it's still time to write your own hardware drivers if you
  1137.   really love that.
  1138.  
  1139.   This HOWTO will not itself cover topics such as Boot loader code &
  1140.   getting into 32-bit mode, Handling Interrupts, The basics about intel
  1141.   ``protected mode'' or ``V86/R86'' braindeadness, defining your object
  1142.   format and calling conventions.  The main place where to find reliable
  1143.   information about that all is source code of existing OSes and
  1144.   bootloaders.  Lots of pointers lie in the following WWW page:
  1145.   <http://www.eleves.ens.fr:8080/home/rideau/Tunes/Review/OSes.html>
  1146.  
  1147.   6.  TODO & POINTERS
  1148.  
  1149.   ╖  fill incomplete sections
  1150.  
  1151.   ╖  add more pointers to software and docs
  1152.  
  1153.   ╖  add simple examples from real life to illustrate the syntax, power,
  1154.      and limitations of each proposed solution.
  1155.  
  1156.   ╖  ask people to help with this HOWTO
  1157.  
  1158.   ╖  find someone who has got some time to takeover the maintenance
  1159.  
  1160.   ╖  perhaps give a few words for assembly on other platforms?
  1161.  
  1162.   ╖  A few pointers (in addition to those already in the rest of the
  1163.      HOWTO)
  1164.  
  1165.   ╖  pentium manuals <http://www.intel.com/design/pentium/manuals/>
  1166.  
  1167.   ╖  cpu bugs in the x86 family <http://www.xs4all.nl/~feldmann>
  1168.  
  1169.   ╖  hornet.eng.ufl.edu for assembly coders <http://www.eng.ufl.edu/ftp>
  1170.  
  1171.   ╖  ftp.luth.se <ftp://ftp.luth.se/pub/msdos/demos/code/>
  1172.  
  1173.   ╖  PM FAQ <ftp://zfja-gate.fuw.edu.pl/cpu/protect.mod>
  1174.  
  1175.   ╖  80x86 Assembly Page <http://www.fys.ruu.nl/~faber/Amain.html>
  1176.  
  1177.   ╖  Courseware <http://www.cit.ac.nz/smac/csware.htm>
  1178.  
  1179.   ╖  game programming <http://www.ee.ucl.ac.uk/~phart/gameprog.html>
  1180.  
  1181.   ╖  And of course, do use your usual Internet Search Tools to look for
  1182.      more information, and tell me anything interesting you find!
  1183.  
  1184.   Authors' .sig:
  1185.  
  1186.   --    ,                                         ,           _ v    ~  ^  --
  1187.   -- Fare -- rideau@clipper.ens.fr -- Francois-Rene Rideau -- +)ang-Vu Ban --
  1188.   --                                      '                   / .          --
  1189.   Join the TUNES project for a computing system based on computing freedom !
  1190.                    TUNES is a Useful, Not Expedient System
  1191.   WWW page at URL: http://www.eleves.ens.fr:8080/home/rideau/Tunes/
  1192.  
  1193.