home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / DOC / HOWTO / MINI / ASSEMBLY < prev    next >
Encoding:
Text File  |  1996-06-23  |  25.6 KB  |  754 lines

  1. Assembly HOWTO
  2.     aka *Free* 32-bit x86 Assembly FAQ
  3.     aka Linux x86 Assembly HOWTO
  4.  
  5. Francois-Rene Rideau rideau@ens.fr
  6. v0.3c, 15 Jun 1996
  7.  
  8.  
  9. HOWTO program in x86 assembly using only *FREE* programming tools.
  10.  
  11.  
  12. Keywords: assembly, assembler, free, macroprocessor, preprocessor,
  13. asm, inline asm, 32-bit, x86, i386, gas, as86, nasm
  14.  
  15.  
  16.  
  17. LEGAL BLURP:
  18.  
  19. Copyright (C) 1996 Francois-Rene Rideau.
  20.   You can freely distribute this document,
  21.   provided no modification is done to the text,
  22.   but annotations that are clearly indicated as such.
  23.   You can freely ask me to distribute the document otherwise.
  24.   The Linux Documentation Project maintainers
  25.   are free to do anything with this document,
  26.   that all other LDP documents simultaneously allow,
  27.   which they can understand optimistically as for a fix-point meaning.
  28.  
  29.  
  30. IMPORTANT NOTE:
  31.  
  32. This is still a *VERY PRELIMINARY* version for this document.
  33.    You (hey, that's *you* I'm talking to, so please listen!)
  34. are especially invited
  35. to ask questions,
  36. to answer to questions,
  37. to correct given answers,
  38. to add new FAQ answers,
  39. to give pointers to other software,
  40. to insult the current maintainer (me),
  41. and TO TAKE OVER THE MAINTENANCE OF THE FAQ in his place (mine),
  42. because I have other things to do...
  43.    For any of these, please contact me
  44.     mailto:rideau@ens.fr
  45. Perhaps we can convince Raymond Moon to add a section to his FAQ
  46. for comp.lang.asm.x86... ?
  47.  
  48.  
  49. 1. INTRODUCTION.
  50.  
  51. This document aims at answering frequently asked questions of people
  52. who program or want to program 32-bit x86 assembly using *free* assemblers,
  53. particularly under the Linux operating system.
  54. It may also point to other documents about
  55. non-free, non-x86, or non-32-bit assemblers,
  56. though it is not its primary goal.
  57.  
  58. Because the main interest of assembly programming is to build to write
  59. the guts of operating systems, languages, and games,
  60. where a C compiler fails to provide the needed expressivity
  61. (performance is more and more seldom an issue),
  62. we stress on development of such software.
  63.  
  64.  
  65. 1.1 How to use this document
  66.  
  67. This document contains answers to some frequently asked questions.
  68. At many places, Universal Resource Locators (URL) are given for some
  69. software or documentation repository.
  70. Please see that the most useful repositories are mirrored,
  71. and that by accessing a nearer mirror site,
  72. you relieve the whole Internet from unneeded network traffic,
  73. while saving your own precious time.
  74. Particularly, there are large repositories all over the world,
  75. that mirror other popular repositories.
  76. You should learn and note what are those places near you (networkwise).
  77. Sometimes, the list of mirrors is listed in a file,
  78. or in a login message. Please heed the advice.
  79. Else, you should ask archie about the software you're looking for...
  80.  
  81. The most recent version for this documents sits in
  82.     http://www.eleves.ens.fr:8080/home/rideau/Assembly
  83. but what's in Linux HOWTO repositories *should* be fairly up to date, too
  84. (I can't know):
  85.     ftp://sunsite.unc.edu/pub/linux/docs/HOWTO/ (?)
  86.  
  87.  
  88. 1.2 Other related documents
  89.  
  90. * If you don't know what *free* software is,
  91. please do read *carefully* the GNU General Public License,
  92. which is used in a lot of free software, and a model for most;
  93. it generally comes in a file named "COPYING",
  94. with a library version in a file named "COPYING.LIB".
  95. Litterature from the FSF (free software foundation)
  96. might help you, too.
  97.  
  98. * Particularly, the interesting kind of free software
  99. comes with sources that you can consult and correct,
  100. or sometimes even borrow from.
  101. Read your particular license carefully, and do comply to it.
  102.  
  103. * There is a FAQ for comp.lang.asm.x86 that answers generic questions
  104. about x86 assembly programming, and questions about some commercial
  105. assemblers in a 16-bit DOS environment.
  106. Some of it apply to free 32-bit asm programming, so you may want
  107. to read this FAQ...
  108.     http://www2.dgsys.com/~raymoon/faq/asmfaq.zip
  109.  
  110. * FAQs and docs exist about programming on your favorite platform,
  111. whichever it is, that you should consult for platform-specific issues
  112. not directly related to programming in assembler.
  113.  
  114.  
  115. 2. ASSEMBLERS.
  116.  
  117.  
  118. 2.1 GCC Inline Assembly
  119.  
  120. The well-known GNU C/C++ Compiler (GCC),
  121. an optimizing 32-bit compiler at the heart of the GNU project,
  122. supports the x86 architecture quite well,
  123. and includes the ability to insert assembly code in C programs,
  124. in such a way that register allocation can be either specified or left to GCC.
  125. GCC works on most available platforms,
  126. notably Linux, *BSD, VSTa, OS/2, *DOS, Win*, etc.
  127.  
  128.  
  129. 2.1.1 Where to find GCC
  130.  
  131. The original GCC site is
  132.     ftp://prep.ai.mit.edu/pub/gnu/
  133. together with all the released application software from the GNU project.
  134. However, there exists a lot of mirrors.
  135.  
  136. However, sources adapted to your favorite OS, and binaries precompiled for it,
  137. should be found at your usual FTP sites.
  138.  
  139. For GCC under Linux, see around
  140.     http://www.linux.org.uk/
  141.  
  142. For most popular DOS port of GCC is named DJGPP,
  143. and can be found in directories of such name in FTP sites. See:
  144.     http://www.delorie.com/djgpp/
  145.  
  146. There is also a port of GCC to OS/2 named EMX, that also works under DOS;
  147. see around:
  148.     http://www.leo.org/pub/comp/os/os2/gnu/emx+gcc/
  149.     http://warp.eecs.berkeley.edu/os2/software/shareware/emx.html
  150.  
  151.  
  152. 2.1.2 Where to find docs for GCC Inline Asm
  153.  
  154. The documentation of GCC includes documentation files in texinfo format,
  155. that you can convert to tex, compile (with tex), and print,
  156. convert to interactive emacs .info format and browse,
  157. convert (with the right tools) to whatever you like,
  158. or just read as is.
  159. The .info files are generally found on any good installation for GCC.
  160.  
  161. The right section to look for is:
  162.     C Extensions::Extended Asm::
  163.  
  164. Section
  165.     Invoking GCC::Submodel Options::i386 Options::
  166. might help too. Particularly, it gives the i386 specific constraint
  167. names for registers:
  168. abcdSDB correspond to %eax, %ebx, %ecx, %edx, %esi, %edi, %ebp
  169. respectively (no letter for %esp).
  170.  
  171. A URL for this document and section, as converted in HTML format, is
  172.     http://www.cygnus.com/doc/usegcc_89.html#SEC92
  173.  
  174. The DJGPP Games resource (not only for game hackers) has this page
  175. specifically about assembly:
  176.     http://www.rt66.com/~brennan/djgpp/djgpp_asm.html
  177.  
  178. Finally, there is a web page called, "DJGPP Quick ASM Programming Guide",
  179. that covers URLs to FAQs, AT&T x86 ASM Syntax, Some inline ASM information,
  180. and converting .obj/.lib files:                
  181.     http://remus.rutgers.edu/~avly/djasm.html
  182.                     
  183.  
  184. GCC depends on GAS for assembling, and follow its syntax (see below);
  185. do mind that inline asm needs percent characters to be quoted
  186. so they be passed to GAS.
  187. See the section about GAS below.
  188.  
  189. Find *lots* of useful examples in the linux/include/asm-i386/
  190. subdirectory of the sources for the free Linux OS.
  191.  
  192.  
  193.  
  194. 2.1.3 How should I invoke GCC for it to properly inline my assembly code ?
  195.  
  196. Be sure to invoke GCC with the "-O" flag,
  197. to enable optimizations and inline assembly.
  198. If you don't, your code may compile, but not run properly!!!
  199.  
  200. More generally, good compile flags for GCC on the x86 platform are
  201.     gcc -O2 -fomit-frame-pointer -m386
  202.  
  203. -O2 is the good optimization level. Optimizing besides it yields
  204. code that is a lot larger, but only a bit faster;
  205. such overoptimizationn might be useful for tight loops only (if any),
  206. which you may be doing in assembly anyway;
  207. if you need that, do it just for the few routines that need it.
  208.  
  209. -fomit-frame-pointer allows generated code to skip the stupid
  210. frame pointer maintenance, which makes code smaller and faster,
  211. and frees a register for further optimizations.
  212. It precludes the easy use of debugging tools (gdb), but when you use these,
  213. you just don't care about size and speed anymore anyway.
  214.  
  215. -m386 yields more compact code, without any measurable slowdown,
  216. (note that small code also means less disk I/O and faster execution)
  217. but perhaps on the above-mentioned tight loops.
  218.  
  219. To optimize even more, option -mregparm=2 and/or corresponding function
  220. attribute might help, but might pose lots of problems
  221. when linking to foreign code...
  222.  
  223. Note that you can add make these flags the default by editing file
  224.     /usr/lib/gcc-lib/i486-linux/2.7.2/specs
  225. or wherever that is on your system.
  226.  
  227.  
  228. 2.2 GAS
  229.  
  230. GAS is the GNU Assembler, that GCC relies upon,
  231. with 
  232.  
  233.  
  234. 2.2.1 Where to find it
  235.  
  236. Find it at the same place where you found GCC,
  237. in a package named binutils.
  238.  
  239.  
  240. 2.2.2 What is this AT&T syntax
  241.  
  242. Because GAS was invented to support a 32-bit unix compiler,
  243. it uses standard "AT&T" syntax,
  244. which resembles a lot the syntax for standard 680x0 assemblers.
  245. This syntax is no worse, no better than the "Intel" syntax.
  246. It's just different.
  247. When you get used to it,
  248. you find it much more regular than the Intel syntax,
  249. though a bit boring.
  250.  
  251. A program exists to help you convert programs 
  252. from TASM syntax to AT&T syntax. See
  253.     ftp://x2ftp.oulu.fi/pub/msdos/programming/convert/ta2asv08.zip
  254.  
  255. A file gas.doc or as.doc
  256. (still around the same place as you found GAS,
  257. if not in the GAS source package itself)
  258. describes the syntax.
  259.  
  260. One place for it is in FTP directory
  261.     ftp://sunsite.unc.edu/pub/linux/GCC/
  262.     ftp://sunsite.doc.ic.ac.uk/packages/linux/sunsite.unc-mirror/GCC/ (?)
  263.  
  264. Again, the sources for the Linux OS come in as good examples;
  265. see under linux/arch/i386, the following files:
  266. kernel/entry.S, kernel/head.S, boot/compressed/head.S, mathemu/*.S
  267.  
  268.  
  269. 2.2.3 Limited 16-bit mode
  270.  
  271. GAS is a 32-bit assembler, meant to support a 32-bit compiler.
  272. It currently has only limited support for 16-bit mode,
  273. which consists in prepending the 32-bit prefixes to instructions,
  274. so you write 32-bit code that runs in 16-bit mode on a 32 bit CPU.
  275. In both modes, it supports 16-bit register usage,
  276. but what is unsupported is 16-bit addressing.
  277.    Use the directive "code16" and "code32" to switch between modes.
  278. Note that an inline assembly statement asm("code16\n")
  279. will allow 32-bit GCC to produce code that'll run in real mode!
  280.    Feel free to add full 16-bit support if you think you need it.
  281.  
  282.  
  283.  
  284. 2.3 GASP
  285.  
  286. GASP is the GAS Preprocessor.
  287. It adds macros and some nice syntax to GAS.
  288.  
  289.  
  290. 2.3.1 Where to find GASP
  291.  
  292. I don't know exactly. See at the GNU repository (prep.ai.mit.edu & mirrors).
  293. Perhaps together with GAS in the binutils package ?
  294.  
  295.  
  296. 2.3.2 How it works
  297.  
  298. I have no idea, but it comes with its own texinfo documentation,
  299. so just print them, or browse the .info files...
  300. Looks like a regular macro-assembler to me.
  301.  
  302.  
  303.  
  304. 2.4 AS86
  305.  
  306. AS86 is a 80x86 assembler, both 16-bit and 32-bit,
  307. part of Bruce Evans' C Compiler (BCC).
  308. It has mostly Intel-syntax, though it differs slightly as for addressing modes.
  309.  
  310.  
  311. 2.4.1 Where to get AS86
  312.  
  313. A completely outdated version of AS86 is distributed by HJLu
  314. just to compile the Linux kernel,
  315. in a package named bin86 (current version 0.3),
  316. available in any Linux GCC repository.
  317. But I advise no one to use it for anything else but compiling Linux.
  318. This version supports only a hacked minix object file format,
  319. and has a few bugs in 32-bit mode, so you better keep it only for
  320. compiling Linux.
  321.  
  322. The most recent versions are published together with the FreeBSD distribution.
  323. I got it from
  324. ftp://ftp.ibp.fr/pub/FreeBSD/packages-2.1/development/bcc-95.3.12.tgz
  325. But your mirror may vary, and the version may evolve.
  326. Among other things, it supports Linux GNU a.out format,
  327. so you can link you code to Linux programs, and/or use the usual
  328. tools from the GNU binutil package to manipulate your data.
  329. This version can co-exist without any harm with the previous one
  330. (see question 2.4.4 below).
  331.  
  332. BCC from 12 march 1995 and earlier version has a misfeature
  333. that makes all segment pushing/popping 16-bit, which is quite
  334. annoying when programming in 32-bit mode.
  335. A patch is published in the Tunes project
  336.     http://www.eleves.ens.fr:8080/home/rideau/Tunes/
  337. subpage
  338.     files/tunes.0.0.0.25.src.tgz
  339. in unpacked subdirectory
  340.     LLL/i386/
  341. The patch should also be in available directly from
  342.     http://www.eleves.ens.fr:8080/home/rideau/files/as86.bcc.patch.gz
  343. Bruce Evans accepted this patch, so if there is a more recent version of
  344. bcc somewhere someday, the patch should have been included...
  345.  
  346.  
  347. Note for DOS users (?):
  348. * To compile it under DOS you might have to define POSIX_HEADERS_MISSING.
  349. * For bcc/as, if you're not using DJGPP, you will have to rename a variable
  350.  named "far" in function mcall() in file mops.c,
  351.  because some DOS compilers think "far" is a reserved keyword.
  352.  Note that you need link with typeconf.obj from the bcc/ld directory...
  353. * For bcc/ld, you'll need have a copy of a.out.h and ar.h;
  354.  DJGPP has them, but other C compilers may require that you steal them
  355.  from any GCC (under DOS, Linux, VSTa, etc).
  356. * For bcc/ld, you need define the BSD_A_OUT macro for all files,
  357.  and edit writebin.c so it define STANDARD_GNU_A_OUT
  358.  and includes your copy of linux a.out.h with a usable DOS name.
  359. * Linux a.out in turns includes asm/a.out.h, so you must manage to get it
  360.  included, too. 16-bit compilers require that you edit the asm/a.out.h
  361.  to modify a 24-bit bitfield into an equivalently sized set of
  362.  bitfields lesser than 24 bits (ld doesn't use that field).
  363. * I didn't try cc1, but it should be quite doable to compile it, too,
  364.  if you like it; however, you'll have to rewrite part of the bcc frontend,
  365.  or use cc1 directly, because it relies on the fork()/exec()/wait() treble
  366.  to launch cc1, as, and ld when compiling.
  367. * precompiled DOS versions can be found on
  368.     http://www.eleves.ens.fr:8080/home/rideau/files/asld86.zip
  369.  If you would recompile them with free compilers and send me,
  370.  I'd appreciate a lot.
  371.  
  372.  
  373. 2.4.2 Where to find docs
  374.  
  375. The docs are what is included in the bcc package.
  376. Man pages are also available somewhere on the FreeBSD site.
  377. When in doubt, the sources themselves are often a good docs:
  378. it's not very well commented, but the programming style is clear.
  379. You /might/ try to see how as86 is used in Tunes 0.0.0.25...
  380.  
  381.  
  382. 2.4.3 How to invoke the assembler ?
  383.  
  384. Here's the GNU Makefile entry for using bcc to transform
  385. .s asm into GNU a.out .o object and .l listing:
  386.  
  387. %.o %.l:        %.s
  388.         bcc -3 -G -c -A-d -A-l -A$*.l -o $*.o $<
  389.  
  390. Remove the "%.l", "-A-l", and "-A$*.l", if you don't want any listing.
  391. If you want something else than GNU a.out,
  392. you can see the docs of bcc about the other supported formats,
  393. and/or use the objcopy utility from the GNU binutils package.
  394.  
  395.  
  396. 2.4.3 What if I can't compile Linux anymore with this new version ?
  397.  
  398. Linus is buried alive in mail, and my patch for compiling Linux
  399. with a Linux a.out as86 didn't make it to him (!).
  400. Now, this shouldn't matter: just keep your as86 from the bin86 package
  401. in /usr/bin, and put the good as86 as /usr/local/libexec/i386/bcc/as
  402. where it should be. You never need explicitly call this "good" as86,
  403. because bcc does everything right, including conversion to Linux a.out,
  404. when invoked with the right options;
  405. so assemble files exclusively with bcc as a frontend, not directly with as86.
  406.  
  407.  
  408.  
  409. 2.5 OTHER ASSEMBLERS
  410.  
  411. These are other, non-regular, options,
  412. in case the previous didn't satisfy you (why ?),
  413. that I don't recommend in the usual (?) case,
  414. but that could be useful if the assembler is part
  415. of what you're designing (i.e. an OS or development environment).
  416.  
  417.  
  418. 2.5.1 Win32Forth assembler
  419.  
  420. Win32Forth is a *free* 32-bit FORTH system
  421. that successfully runs under Win32s, Win95, Win/NT.
  422. It includes a free 32-bit assembler (either prefix or postfix syntax)
  423. integrated to the assembler.
  424. Macro processing is done by the full power of the reflective FORTH language,
  425. but the only supported input and output contexts is Win32For itself.
  426. Find it at
  427.     ftp://ftp.forth.org/pub/Forth/win32for/
  428.  
  429.  
  430. 2.5.2 NASM
  431.  
  432. The Netwide Assembler project tries to produce yet another assembler,
  433. written in C, that would be modular enough
  434. to eventually support all known syntaxes and object formats.
  435.  
  436. Current version runs just fine,
  437. but only some very simple syntax, and plain binary output;
  438. No integrated macroprocessing.
  439.  
  440. Surely NASM evolves too fast for this HOWTO to be up to date;
  441. still don't expect it to have all the required features currently,
  442. unless you're ready to help implement it.
  443.  
  444.     http://www.dcs.warwick.ac.uk/~jules/nasm1.html
  445.  
  446.  
  447. 2.5.3 Tunes
  448.  
  449. The Tunes OS project is developping its own assembler
  450. as an extension to the Scheme language,
  451. as part of its development process.
  452. It doesn't run at all yet, though help is welcome.
  453.  
  454. The assembler manipulates symbolic syntax trees,
  455. so it could equally serve as the basis for a assembly syntax translator,
  456. a disassembler, a common assembler/compiler back-end, etc.
  457. Also, the full power of a real languae, Scheme,
  458. make it unchallenged as for macroprocessing/metaprograming
  459.     http://www.eleves.ens.fr:8080/home/rideau/Tunes/
  460.  
  461.  
  462. 2.5.4 Non-free and/or Non-32bit x86 assemblers.
  463.  
  464. You may find more about them,
  465. together with the basics of x86 assembly programming,
  466. in Raymond Moon's FAQ for comp.lang.asm.x86
  467.     http://www2.dgsys.com/~raymoon/faq/asmfaq.zip
  468.  
  469.  
  470.  
  471.  
  472. 3. METAPROGRAMMING/MACROPROCESSING
  473.  
  474. Assembly programming is a bore,
  475. but for critical parts of programs.
  476.    You should use the appropriate tool for the right task,
  477. so don't choose assembly when it's not fit;
  478. C, OCAML, perl, Scheme, might be a better choice for most
  479. of your programming.
  480.    However, there are cases when these tools do not give
  481. a fine enough control on the machine, and assembly is useful or needed.
  482. In those case, you'll appreciate a system of macroprocessing and
  483. metaprogramming that'll allow recurring patterns to be defined
  484. once, and reused multiple times, by automatically inline expansion,
  485. which allows safer programming, easier modification, etc.
  486. A "plain" assembler is often not enough,
  487. even when one is doing only small routines to link with C.
  488.  
  489.  
  490. 3.1 What's integrated into the above
  491.  
  492.  
  493. 3.1.1 GCC
  494.  
  495. GCC allows (and requires) you to specify register constraints
  496. in your "inline assembly" code, so the optimizer always know about it;
  497. thus, inline assembly code is really made of patterns,
  498. not forcibly exact code.
  499.  
  500. Then, you can make put your assembly into CPP macros,
  501. so anyone can use it in as any C function/macro.
  502. and inline C functions
  503. Inline functions resemble macros very much, but are sometimes cleaner to use.
  504. Beware that in those cases, code will be duplicated,
  505. so only local labels (of "1:" style) should be defined in that asm code.
  506. However, a macro would allow the name for a non local defined label
  507. to be passed.
  508. Also, note that some bug in your code or in GCC may appear
  509. when inlining functions with asm code where the register constraints
  510. weren't declared properly and/or confuse GCC.
  511.  
  512. Lastly, the C language itself may be considered as a good abstraction
  513. to assembly programming,
  514. which relieves you from most of the trouble of assembling.
  515.  
  516. Beware that some optimizations that involve passing arguments to functions
  517. through registers may make those functions unsuitable to be called from
  518. assembly in the standard way, least you give them the attribute asmlinkage.
  519. See the linux kernel sources for examples.
  520.  
  521.  
  522. 3.1.2 GAS
  523.  
  524. GAS has absolutely NO macro capability included.
  525. However, GCC and passes .S files through CPP before to feed them to GAS.
  526. .s files are the generated ones, and passed directly to GAS.
  527. Again and again, see Linux sources for examples.
  528.  
  529.  
  530. 3.1.3 GASP
  531.  
  532. It adds all the usual macroassembly tricks to GAS.
  533. See its texinfo docs.
  534.  
  535.  
  536. 3.1.4 AS86
  537.  
  538. It has some simple macro support, but I couldn't find docs.
  539. Now the sources are very straightforward,
  540. so if you're interested, you should understand them easily.
  541. If you need more than the basics, you should use an external filter
  542. (see below in section 3.2)
  543.  
  544.  
  545. 3.1.5 OTHER ASSEMBLERS
  546.  
  547. Win32FORTH:
  548. CODE and END-CODE are macros that do not switch from interpretation mode,
  549. so you have access to the full power of FORTH words, immediate or not,
  550. while assembling.
  551.  
  552. NASM:
  553. no macro support yet, see about external filters below.
  554.  
  555. TUNES:
  556. it doesn't work yet, but the Scheme language is a real high-level language
  557. that allows arbitrary meta-programming.
  558.  
  559.  
  560.  
  561. 3.2 EXTERNAL FILTERS
  562.  
  563. Whatever is the macro support from your assembler,
  564. or whatever language you use (even C !),
  565. if the language is not expressive enough to you,
  566. you can have files passed through an external filter
  567. with a Makefile rule like that:
  568.  
  569. %.s:    %.S other_dependencies
  570.         $(FILTER) $(FILTER_OPTIONS) < $< > $@
  571.  
  572.  
  573. 3.2.1 CPP
  574.  
  575. CPP is truely not very expressive, but it's enough for easy things,
  576. it's standard, and called transparently by GCC.
  577.  
  578. As an example of its limitations, you can't declare objects so that
  579. destructors are automatically called at the end of the declaring block,
  580. you can't co-declared data and the code to process it, etc.
  581.  
  582. CPP came with your C compiler. If you could make it without one,
  583. don't bother fetching any (though I wonder how you could).
  584. GCC (see above) is a free C compiler you could have fetched.
  585.  
  586.  
  587. 3.2.2 M4
  588.  
  589. M4 gives you the full power of macroprocessing,
  590. with a Turing equivalent language, recursion, regular expressions, etc.
  591. You can do with it everything that CPP cannot.
  592.  
  593. See macro4th/This4th from
  594.     ftp://ftp.forth.org/pub/Forth/ in Reviewed/ ANS/ (?),
  595. or the Tunes 0.0.0.25 sources as examples
  596. of advanced macroprogramming using m4.
  597.  
  598. However, its fucked up quoting semantics force you to use
  599. explicit continuation-passing tail-recursive macro style if
  600. you want to do *advanced* macro programming
  601. (which is remindful of TeX -- BTW, has anyone tried to use TeX as
  602. a macroprocessor for anything else than typesetting ?).
  603. This is NOT worse than CPP that does not allow quoting and recursion anyway.
  604.  
  605. The right version of m4 to get is GNU m4 1.4 (or later if exists),
  606. which has the most features and the least bugs or limitations of all.
  607.  
  608.  
  609. 3.2.3 Macroprocessing with yer own filter
  610.  
  611. You can write your own simple macro-expansion filter
  612. with the usual tools: perl, awk, sed, etc.
  613. That's quick to do, and you control everything.
  614. But of course, any power in macroprocessing must be earned the hard way.
  615.  
  616.  
  617. 3.2.4 Metaprogramming
  618.  
  619. Instead of using an external filter that expands macros,
  620. one way to do things is to write programs that write part
  621. or all of other programs.
  622.  
  623. For instance, you could use a program outputing source code
  624. to generate sine/cosine/whatever lookup tables,
  625. to extract a source-form representation of a binary file,
  626. to compile your bitmaps into fast display routines,
  627. to extract documentation, initialization/finalization code,
  628. description tables, as well as normal code from the same source files,
  629. to have customized assembly code, generated from a perl/shell/scheme script
  630. that does arbitrary processing,
  631. (particularly useful when some kind of data must be mirrored at
  632. into many cross-referencing tables and code chunks).
  633. etc.
  634.  
  635. Think about it !
  636.  
  637.  
  638.  
  639. 4. CALLING CONVENTIONS
  640.  
  641.  
  642. 4.1 Linux
  643.  
  644.  
  645. 4.1.1 Linking to GCC
  646.  
  647. That's the preferred way.
  648.  
  649. 32-bit arguments are pushed on stack in reverse order
  650. (hence popped in the right order)
  651. above the 32-bit near return address.
  652. %ebp,%esi,%edi,%ebx are preserved,
  653. %eax holds the result, or %edx:%eax for 64-bit results.
  654.  
  655. FP stack: I'm not sure,
  656. but I think it's result in st(0), whole stack callee-save.
  657.  
  658. GCC has options to modify the calling conventions
  659. by reserving registers, having arguments in registers,
  660. not assuming the FPU, etc. Check the i386 info pages.
  661.  
  662. Beware that you must put declare asmlinkage attribute for a function
  663. you want GCC to provide these standard conventions to
  664. (I don't know what it does with modified calling conventions).
  665.  
  666.  
  667. 4.1.2 ELF vs a.out problems
  668.  
  669. Some C compilers prepend an underscore before every symbol,
  670. while others do not.
  671.  
  672. Particularly, Linux a.out GCC does such prepending,
  673. while Linux ELF GCC does not.
  674. See how the Linux source tree manages that itself
  675. (linux/include/linux/linkage.h).
  676.  
  677. You can override the C->asm renaming by inserting statements like
  678.         void foo asm("bar") (void);
  679. to be sure that the C function foo will be called really bar in assembly.
  680.  
  681. Note that the utility objcopy, from the binutils package,
  682. should allow you to transform your a.out objects into ELF objects,
  683. and perhaps the contrary too, in some cases.
  684.  
  685.  
  686. 4.1.3 Direct Linux syscalls
  687.  
  688. This is NOT recommended, because it may change, it's not portable,
  689. AND it precludes libc fixes and extensions, like, for instance zlibc
  690. (that does on-the-fly transparent decompression of gzip-compressed files).
  691. The standard, recommended way to call Linux system services is to
  692. go through the libc.
  693.  
  694. Now, if you don't want to link to the libc,
  695. you should see how linux-eforth-1.0c.tgz does it
  696.     ftp://ftp.forth.org/pub/Forth/Linux/
  697. The sources for Linux come in handy, too,
  698. particularly the asm/unistd.h header file,
  699. that describes how to do system calls...
  700.  
  701. Basically, you issue an int $0x80,
  702. with the __NR_syscallname number (from asm/unistd.h) in %eax,
  703. and parameters (up to five) in %ebx, %ecx, %edx, %esi, %edi respectively.
  704. Result is returned in %eax, with a negative result being an error
  705. whose opposite is what libc would put in errno.
  706. The user-stack is not touched,
  707. so you needn't have a valid one when doing a syscall.
  708.  
  709.  
  710. 4.2 DOS
  711.  
  712. Most DOS extenders come with some interface to DOS services.
  713. Read their docs about that,
  714. but often, they just simulate int 0x21 and such,
  715. so you do "as if" you were in real mode
  716. (I doubt they have stubs to have things work with 32-bit operands
  717. by calling 16-bit DOS services as needed).
  718.  
  719. Docs about DPMI and such can be found on
  720.     ftp://x2ftp.oulu.fi/pub/msdos/programming/
  721.  
  722. DJGPP comes with its own (limited) libc replacement, too.
  723.  
  724. It is possible to cross-compile from Linux to DOS,
  725. but the existing patches were written for a.out GCC.
  726. Perhaps recent ELF GCCs need no patch,
  727. perhaps they need new patches; I just dunno.
  728.  
  729.  
  730.  
  731. 4.3 YOUR OWN OS
  732. [That's what many asm programmers talk about]
  733.  
  734. 4.3.1 Boot loader code & getting into 32-bit mode
  735. 4.3.2 The basics about protection
  736. 4.3.3 Handling Interrupts
  737. 4.3.4 V86/R86 mode for using 16-bit system services.
  738. 4.3.5 Where to find info about it all.
  739.  
  740. [Please add pointers to other documents to this section]
  741.  
  742. The main source for information is sources of existing OSes.
  743. Lots of pointers lie in the following WWW page:
  744.     http://www.eleves.ens.fr:8080/home/rideau/Tunes/Review/OSes.html
  745.  
  746.  
  747. 5. TODO
  748. * fill incomplete sections
  749. * add more pointers to software...
  750. * add simple examples from real life to illustrate the syntax, power,
  751. and limitations of each proposed solution.
  752.  
  753.  
  754.