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