internal.doc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. Internals of the Netwide Assembler
  2. ==================================
  3. The Netwide Assembler is intended to be a modular, re-usable x86
  4. assembler, which can be embedded in other programs, for example as
  5. the back end to a compiler.
  6. The assembler is composed of modules. The interfaces between them
  7. look like:
  8. +--- preproc.c ----+
  9. | |
  10. +---- parser.c ----+
  11. | | |
  12. | float.c |
  13. | |
  14. +--- assemble.c ---+
  15. | | |
  16. nasm.c ---+ insnsa.c +--- nasmlib.c
  17. | |
  18. +--- listing.c ----+
  19. | |
  20. +---- labels.c ----+
  21. | |
  22. +--- outform.c ----+
  23. | |
  24. +----- *out.c -----+
  25. In other words, each of `preproc.c', `parser.c', `assemble.c',
  26. `labels.c', `listing.c', `outform.c' and each of the output format
  27. modules `*out.c' are independent modules, which do not directly
  28. inter-communicate except through the main program.
  29. The Netwide *Disassembler* is not intended to be particularly
  30. portable or reusable or anything, however. So I won't bother
  31. documenting it here. :-)
  32. nasmlib.c
  33. ---------
  34. This is a library module; it contains simple library routines which
  35. may be referenced by all other modules. Among these are a set of
  36. wrappers around the standard `malloc' routines, which will report a
  37. fatal error if they run out of memory, rather than returning NULL.
  38. preproc.c
  39. ---------
  40. This contains a macro preprocessor, which takes a file name as input
  41. and returns a sequence of preprocessed source lines. The only symbol
  42. exported from the module is `nasmpp', which is a data structure of
  43. type `Preproc', declared in nasm.h. This structure contains pointers
  44. to all the functions designed to be callable from outside the
  45. module.
  46. parser.c
  47. --------
  48. This contains a source-line parser. It parses `canonical' assembly
  49. source lines, containing some combination of the `label', `opcode',
  50. `operand' and `comment' fields: it does not process directives or
  51. macros. It exports two functions: `parse_line' and `cleanup_insn'.
  52. `parse_line' is the main parser function: you pass it a source line
  53. in ASCII text form, and it returns you an `insn' structure
  54. containing all the details of the instruction on that line. The
  55. parameters it requires are:
  56. - The location (segment, offset) where the instruction on this line
  57. will eventually be placed. This is necessary in order to evaluate
  58. expressions containing the Here token, `$'.
  59. - A function which can be called to retrieve the value of any
  60. symbols the source line references.
  61. - Which pass the assembler is on: an undefined symbol only causes an
  62. error condition on pass two.
  63. - The source line to be parsed.
  64. - A structure to fill with the results of the parse.
  65. - A function which can be called to report errors.
  66. Some instructions (DB, DW, DD for example) can require an arbitrary
  67. amount of storage, and so some of the members of the resulting
  68. `insn' structure will be dynamically allocated. The other function
  69. exported by `parser.c' is `cleanup_insn', which can be called to
  70. deallocate any dynamic storage associated with the results of a
  71. parse.
  72. names.c
  73. -------
  74. This doesn't count as a module - it defines a few arrays which are
  75. shared between NASM and NDISASM, so it's a separate file which is
  76. #included by both parser.c and disasm.c.
  77. float.c
  78. -------
  79. This is essentially a library module: it exports one function,
  80. `float_const', which converts an ASCII representation of a
  81. floating-point number into an x86-compatible binary representation,
  82. without using any built-in floating-point arithmetic (so it will run
  83. on any platform, portably). It calls nothing, and is called only by
  84. `parser.c'. Note that the function `float_const' must be passed an
  85. error reporting routine.
  86. assemble.c
  87. ----------
  88. This module contains the code generator: it translates `insn'
  89. structures as returned from the parser module into actual generated
  90. code which can be placed in an output file. It exports two
  91. functions, `assemble' and `insn_size'.
  92. `insn_size' is designed to be called on pass one of assembly: it
  93. takes an `insn' structure as input, and returns the amount of space
  94. that would be taken up if the instruction described in the structure
  95. were to be converted to real machine code. `insn_size' also requires
  96. to be told the location (as a segment/offset pair) where the
  97. instruction would be assembled, the mode of assembly (16/32 bit
  98. default), and a function it can call to report errors.
  99. `assemble' is designed to be called on pass two: it takes all the
  100. parameters that `insn_size' does, but has an extra parameter which
  101. is an output driver. `assemble' actually converts the input
  102. instruction into machine code, and outputs the machine code by means
  103. of calling the `output' function of the driver.
  104. insnsa.c
  105. --------
  106. This is another library module: it exports one very big array of
  107. instruction translations. It is generated automatically from the
  108. insns.dat file by the insns.pl script.
  109. labels.c
  110. --------
  111. This module contains a label manager. It exports six functions:
  112. `init_labels' should be called before any other function in the
  113. module. `cleanup_labels' may be called after all other use of the
  114. module has finished, to deallocate storage.
  115. `define_label' is called to define new labels: you pass it the name
  116. of the label to be defined, and the (segment,offset) pair giving the
  117. value of the label. It is also passed an error-reporting function,
  118. and an output driver structure (so that it can call the output
  119. driver's label-definition function). `define_label' mentally
  120. prepends the name of the most recently defined non-local label to
  121. any label beginning with a period.
  122. `define_label_stub' is designed to be called in pass two, once all
  123. the labels have already been defined: it does nothing except to
  124. update the "most-recently-defined-non-local-label" status, so that
  125. references to local labels in pass two will work correctly.
  126. `declare_as_global' is used to declare that a label should be
  127. global. It must be called _before_ the label in question is defined.
  128. Finally, `lookup_label' attempts to translate a label name into a
  129. (segment,offset) pair. It returns non-zero on success.
  130. The label manager module is (theoretically :) restartable: after
  131. calling `cleanup_labels', you can call `init_labels' again, and
  132. start a new assembly with a new set of symbols.
  133. listing.c
  134. ---------
  135. This file contains the listing file generator. The interface to the
  136. module is through the one symbol it exports, `nasmlist', which is a
  137. structure containing six function pointers. The calling semantics of
  138. these functions isn't terribly well thought out, as yet, but it
  139. works (just about) so it's going to get left alone for now...
  140. outform.c
  141. ---------
  142. This small module contains a set of routines to manage a list of
  143. output formats, and select one given a keyword. It contains three
  144. small routines: `ofmt_register' which registers an output driver as
  145. part of the managed list, `ofmt_list' which lists the available
  146. drivers on stdout, and `ofmt_find' which tries to find the driver
  147. corresponding to a given name.
  148. The output modules
  149. ------------------
  150. Each of the output modules, `outbin.o', `outelf.o' and so on,
  151. exports only one symbol, which is an output driver data structure
  152. containing pointers to all the functions needed to produce output
  153. files of the appropriate type.
  154. The exception to this is `outcoff.o', which exports _two_ output
  155. driver structures, since COFF and Win32 object file formats are very
  156. similar and most of the code is shared between them.
  157. nasm.c
  158. ------
  159. This is the main program: it calls all the functions in the above
  160. modules, and puts them together to form a working assembler. We
  161. hope. :-)
  162. Segment Mechanism
  163. -----------------
  164. In NASM, the term `segment' is used to separate the different
  165. sections/segments/groups of which an object file is composed.
  166. Essentially, every address NASM is capable of understanding is
  167. expressed as an offset from the beginning of some segment.
  168. The defining property of a segment is that if two symbols are
  169. declared in the same segment, then the distance between them is
  170. fixed at assembly time. Hence every externally-declared variable
  171. must be declared in its own segment, since none of the locations of
  172. these are known, and so no distances may be computed at assembly
  173. time.
  174. The special segment value NO_SEG (-1) is used to denote an absolute
  175. value, e.g. a constant whose value does not depend on relocation,
  176. such as the _size_ of a data object.
  177. Apart from NO_SEG, segment indices all have their least significant
  178. bit clear, if they refer to actual in-memory segments. For each
  179. segment of this type, there is an auxiliary segment value, defined
  180. to be the same number but with the LSB set, which denotes the
  181. segment-base value of that segment, for object formats which support
  182. it (Microsoft .OBJ, for example).
  183. Hence, if `textsym' is declared in a code segment with index 2, then
  184. referencing `SEG textsym' would return zero offset from
  185. segment-index 3. Or, in object formats which don't understand such
  186. references, it would return an error instead.
  187. The next twist is SEG_ABS. Some symbols may be declared with a
  188. segment value of SEG_ABS plus a 16-bit constant: this indicates that
  189. they are far-absolute symbols, such as the BIOS keyboard buffer
  190. under MS-DOS, which always resides at 0040h:001Eh. Far-absolutes are
  191. handled with care in the parser, since they are supposed to evaluate
  192. simply to their offset part within expressions, but applying SEG to
  193. one should yield its segment part. A far-absolute should never find
  194. its way _out_ of the parser, unless it is enclosed in a WRT clause,
  195. in which case Microsoft 16-bit object formats will want to know
  196. about it.
  197. Porting Issues
  198. --------------
  199. We have tried to write NASM in portable ANSI C: we do not assume
  200. little-endianness or any hardware characteristics (in order that
  201. NASM should work as a cross-assembler for x86 platforms, even when
  202. run on other, stranger machines).
  203. Assumptions we _have_ made are:
  204. - We assume that `short' is at least 16 bits, and `long' at least
  205. 32. This really _shouldn't_ be a problem, since Kernighan and
  206. Ritchie tell us we are entitled to do so.
  207. - We rely on having more than 6 characters of significance on
  208. externally linked symbols in the NASM sources. This may get fixed
  209. at some point. We haven't yet come across a linker brain-dead
  210. enough to get it wrong anyway.
  211. - We assume that `fopen' using the mode "wb" can be used to write
  212. binary data files. This may be wrong on systems like VMS, with a
  213. strange file system. Though why you'd want to run NASM on VMS is
  214. beyond me anyway.
  215. That's it. Subject to those caveats, NASM should be completely
  216. portable. If not, we _really_ want to know about it.
  217. Porting Non-Issues
  218. ------------------
  219. The following is _not_ a portability problem, although it looks like
  220. one.
  221. - When compiling with some versions of DJGPP, you may get errors
  222. such as `warning: ANSI C forbids braced-groups within
  223. expressions'. This isn't NASM's fault - the problem seems to be
  224. that DJGPP's definitions of the <ctype.h> macros include a
  225. GNU-specific C extension. So when compiling using -ansi and
  226. -pedantic, DJGPP complains about its own header files. It isn't a
  227. problem anyway, since it still generates correct code.