aboutsummaryrefslogtreecommitdiffstats
path: root/doc/internal.doc
blob: 88f06bc244d4a9b17690737c1e2fb04f580452ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
Internals of the Netwide Assembler
==================================

The Netwide Assembler is intended to be a modular, re-usable x86
assembler, which can be embedded in other programs, for example as
the back end to a compiler.

The assembler is composed of modules. The interfaces between them
look like:

		  +--- preproc.c ----+
		  |		     |
		  +---- parser.c ----+
		  |	   |         |
		  |     float.c      |
		  |		     |
		  +--- assemble.c ---+
		  |        |         |
	nasm.c ---+     insnsa.c     +--- nasmlib.c
		  |		     |
		  +--- listing.c ----+
		  |		     |
		  +---- labels.c ----+
		  |		     |
		  +--- outform.c ----+
		  |		     |
		  +----- *out.c -----+

In other words, each of `preproc.c', `parser.c', `assemble.c',
`labels.c', `listing.c', `outform.c' and each of the output format
modules `*out.c' are independent modules, which do not directly
inter-communicate except through the main program.

The Netwide *Disassembler* is not intended to be particularly
portable or reusable or anything, however. So I won't bother
documenting it here. :-)

nasmlib.c
---------

This is a library module; it contains simple library routines which
may be referenced by all other modules. Among these are a set of
wrappers around the standard `malloc' routines, which will report a
fatal error if they run out of memory, rather than returning NULL.

preproc.c
---------

This contains a macro preprocessor, which takes a file name as input
and returns a sequence of preprocessed source lines. The only symbol
exported from the module is `nasmpp', which is a data structure of
type `Preproc', declared in nasm.h. This structure contains pointers
to all the functions designed to be callable from outside the
module.

parser.c
--------

This contains a source-line parser. It parses `canonical' assembly
source lines, containing some combination of the `label', `opcode',
`operand' and `comment' fields: it does not process directives or
macros. It exports two functions: `parse_line' and `cleanup_insn'.

`parse_line' is the main parser function: you pass it a source line
in ASCII text form, and it returns you an `insn' structure
containing all the details of the instruction on that line. The
parameters it requires are:

- The location (segment, offset) where the instruction on this line
  will eventually be placed. This is necessary in order to evaluate
  expressions containing the Here token, `$'.

- A function which can be called to retrieve the value of any
  symbols the source line references.

- Which pass the assembler is on: an undefined symbol only causes an
  error condition on pass two.

- The source line to be parsed.

- A structure to fill with the results of the parse.

- A function which can be called to report errors.

Some instructions (DB, DW, DD for example) can require an arbitrary
amount of storage, and so some of the members of the resulting
`insn' structure will be dynamically allocated. The other function
exported by `parser.c' is `cleanup_insn', which can be called to
deallocate any dynamic storage associated with the results of a
parse.

names.c
-------

This doesn't count as a module - it defines a few arrays which are
shared between NASM and NDISASM, so it's a separate file which is
#included by both parser.c and disasm.c.

float.c
-------

This is essentially a library module: it exports one function,
`float_const', which converts an ASCII representation of a
floating-point number into an x86-compatible binary representation,
without using any built-in floating-point arithmetic (so it will run
on any platform, portably). It calls nothing, and is called only by
`parser.c'. Note that the function `float_const' must be passed an
error reporting routine.

assemble.c
----------

This module contains the code generator: it translates `insn'
structures as returned from the parser module into actual generated
code which can be placed in an output file. It exports two
functions, `assemble' and `insn_size'.

`insn_size' is designed to be called on pass one of assembly: it
takes an `insn' structure as input, and returns the amount of space
that would be taken up if the instruction described in the structure
were to be converted to real machine code. `insn_size' also requires
to be told the location (as a segment/offset pair) where the
instruction would be assembled, the mode of assembly (16/32 bit
default), and a function it can call to report errors.

`assemble' is designed to be called on pass two: it takes all the
parameters that `insn_size' does, but has an extra parameter which
is an output driver. `assemble' actually converts the input
instruction into machine code, and outputs the machine code by means
of calling the `output' function of the driver.

insnsa.c
--------

This is another library module: it exports one very big array of
instruction translations. It is generated automatically from the
insns.dat file by the insns.pl script.

labels.c
--------

This module contains a label manager. It exports six functions:

`init_labels' should be called before any other function in the
module. `cleanup_labels' may be called after all other use of the
module has finished, to deallocate storage.

`define_label' is called to define new labels: you pass it the name
of the label to be defined, and the (segment,offset) pair giving the
value of the label. It is also passed an error-reporting function,
and an output driver structure (so that it can call the output
driver's label-definition function). `define_label' mentally
prepends the name of the most recently defined non-local label to
any label beginning with a period.

`define_label_stub' is designed to be called in pass two, once all
the labels have already been defined: it does nothing except to
update the "most-recently-defined-non-local-label" status, so that
references to local labels in pass two will work correctly.

`declare_as_global' is used to declare that a label should be
global. It must be called _before_ the label in question is defined.

Finally, `lookup_label' attempts to translate a label name into a
(segment,offset) pair. It returns non-zero on success.

The label manager module is (theoretically :) restartable: after
calling `cleanup_labels', you can call `init_labels' again, and
start a new assembly with a new set of symbols.

listing.c
---------

This file contains the listing file generator. The interface to the
module is through the one symbol it exports, `nasmlist', which is a
structure containing six function pointers. The calling semantics of
these functions isn't terribly well thought out, as yet, but it
works (just about) so it's going to get left alone for now...

outform.c
---------

This small module contains a set of routines to manage a list of
output formats, and select one given a keyword. It contains three
small routines: `ofmt_register' which registers an output driver as
part of the managed list, `ofmt_list' which lists the available
drivers on stdout, and `ofmt_find' which tries to find the driver
corresponding to a given name.

The output modules
------------------

Each of the output modules, `outbin.o', `outelf.o' and so on,
exports only one symbol, which is an output driver data structure
containing pointers to all the functions needed to produce output
files of the appropriate type.

The exception to this is `outcoff.o', which exports _two_ output
driver structures, since COFF and Win32 object file formats are very
similar and most of the code is shared between them.

nasm.c
------

This is the main program: it calls all the functions in the above
modules, and puts them together to form a working assembler. We
hope. :-)

Segment Mechanism
-----------------

In NASM, the term `segment' is used to separate the different
sections/segments/groups of which an object file is composed.
Essentially, every address NASM is capable of understanding is
expressed as an offset from the beginning of some segment.

The defining property of a segment is that if two symbols are
declared in the same segment, then the distance between them is
fixed at assembly time. Hence every externally-declared variable
must be declared in its own segment, since none of the locations of
these are known, and so no distances may be computed at assembly
time.

The special segment value NO_SEG (-1) is used to denote an absolute
value, e.g. a constant whose value does not depend on relocation,
such as the _size_ of a data object.

Apart from NO_SEG, segment indices all have their least significant
bit clear, if they refer to actual in-memory segments. For each
segment of this type, there is an auxiliary segment value, defined
to be the same number but with the LSB set, which denotes the
segment-base value of that segment, for object formats which support
it (Microsoft .OBJ, for example).

Hence, if `textsym' is declared in a code segment with index 2, then
referencing `SEG textsym' would return zero offset from
segment-index 3. Or, in object formats which don't understand such
references, it would return an error instead.

The next twist is SEG_ABS. Some symbols may be declared with a
segment value of SEG_ABS plus a 16-bit constant: this indicates that
they are far-absolute symbols, such as the BIOS keyboard buffer
under MS-DOS, which always resides at 0040h:001Eh. Far-absolutes are
handled with care in the parser, since they are supposed to evaluate
simply to their offset part within expressions, but applying SEG to
one should yield its segment part. A far-absolute should never find
its way _out_ of the parser, unless it is enclosed in a WRT clause,
in which case Microsoft 16-bit object formats will want to know
about it.

Porting Issues
--------------

We have tried to write NASM in portable ANSI C: we do not assume
little-endianness or any hardware characteristics (in order that
NASM should work as a cross-assembler for x86 platforms, even when
run on other, stranger machines).

Assumptions we _have_ made are:

- We assume that `short' is at least 16 bits, and `long' at least
  32. This really _shouldn't_ be a problem, since Kernighan and
  Ritchie tell us we are entitled to do so.

- We rely on having more than 6 characters of significance on
  externally linked symbols in the NASM sources. This may get fixed
  at some point. We haven't yet come across a linker brain-dead
  enough to get it wrong anyway.

- We assume that `fopen' using the mode "wb" can be used to write
  binary data files. This may be wrong on systems like VMS, with a
  strange file system. Though why you'd want to run NASM on VMS is
  beyond me anyway.

That's it. Subject to those caveats, NASM should be completely
portable. If not, we _really_ want to know about it.

Porting Non-Issues
------------------

The following is _not_ a portability problem, although it looks like
one.

- When compiling with some versions of DJGPP, you may get errors
  such as `warning: ANSI C forbids braced-groups within
  expressions'. This isn't NASM's fault - the problem seems to be
  that DJGPP's definitions of the <ctype.h> macros include a
  GNU-specific C extension. So when compiling using -ansi and
  -pedantic, DJGPP complains about its own header files. It isn't a
  problem anyway, since it still generates correct code.