1 /* ndisasm.c the Netwide Disassembler main module
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
21 #define BPL 8 /* bytes per line of hex dump */
23 static const char *help
=
24 "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
25 " [-e bytes] [-k start,bytes] [-p vendor] file\n"
26 " -a or -i activates auto (intelligent) sync\n"
27 " -u sets USE32 (32-bit mode)\n"
28 " -b 16 or -b 32 sets number of bits too\n"
29 " -h displays this text\n"
30 " -r displays the version number\n"
31 " -e skips <bytes> bytes of header\n"
32 " -k avoids disassembling <bytes> bytes from position <start>\n"
33 " -p selects the preferred vendor instruction set (intel, amd, cyrix, idt)\n";
35 static void output_ins (unsigned long, unsigned char *, int, char *);
36 static void skip (unsigned long dist
, FILE *fp
);
38 int main(int argc
, char **argv
)
40 unsigned char buffer
[INSN_MAX
* 2], *p
, *q
;
43 char *filename
= NULL
;
44 unsigned long nextsync
, synclen
, initskip
= 0L;
49 unsigned long prefer
= 0;
58 char *v
, *vv
, *p
= *++argv
;
59 if (*p
== '-' && p
[1]) {
61 while (*p
) switch (tolower(*p
)) {
62 case 'a': /* auto or intelligent sync */
68 fprintf(stderr
, help
);
71 fprintf(stderr
, "NDISASM version " NASM_VER
"\n");
78 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
80 fprintf(stderr
, "%s: `-b' requires an argument\n", pname
);
85 else if (!strcmp(v
, "32"))
88 fprintf(stderr
, "%s: argument to `-b' should"
89 " be `16' or `32'\n", pname
);
91 p
= ""; /* force to next argument */
93 case 'o': /* origin */
94 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
96 fprintf(stderr
, "%s: `-o' requires an argument\n", pname
);
99 offset
= readnum (v
, &rn_error
);
101 fprintf(stderr
, "%s: `-o' requires a numeric argument\n",
105 p
= ""; /* force to next argument */
107 case 's': /* sync point */
108 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
110 fprintf(stderr
, "%s: `-s' requires an argument\n", pname
);
113 add_sync (readnum (v
, &rn_error
), 0L);
115 fprintf(stderr
, "%s: `-s' requires a numeric argument\n",
119 p
= ""; /* force to next argument */
121 case 'e': /* skip a header */
122 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
124 fprintf(stderr
, "%s: `-e' requires an argument\n", pname
);
127 initskip
= readnum (v
, &rn_error
);
129 fprintf(stderr
, "%s: `-e' requires a numeric argument\n",
133 p
= ""; /* force to next argument */
135 case 'k': /* skip a region */
136 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
138 fprintf(stderr
, "%s: `-k' requires an argument\n", pname
);
143 fprintf(stderr
, "%s: `-k' requires two numbers separated"
144 " by a comma\n", pname
);
148 nextsync
= readnum (v
, &rn_error
);
150 fprintf(stderr
, "%s: `-k' requires numeric arguments\n",
154 synclen
= readnum (vv
, &rn_error
);
156 fprintf(stderr
, "%s: `-k' requires numeric arguments\n",
160 add_sync (nextsync
, synclen
);
161 p
= ""; /* force to next argument */
163 case 'p': /* preferred vendor */
164 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
166 fprintf(stderr
, "%s: `-p' requires an argument\n", pname
);
169 if ( !strcmp(v
, "intel") ) {
170 prefer
= 0; /* Default */
171 } else if ( !strcmp(v
, "amd") ) {
172 prefer
= IF_AMD
|IF_3DNOW
;
173 } else if ( !strcmp(v
, "cyrix") ) {
174 prefer
= IF_CYRIX
|IF_3DNOW
;
175 } else if ( !strcmp(v
, "idt") || !strcmp(v
, "centaur") ||
176 !strcmp(v
, "winchip") ) {
179 fprintf(stderr
, "%s: unknown vendor `%s' specified with `-p'\n", pname
, v
);
182 p
= ""; /* force to next argument */
185 } else if (!filename
) {
188 fprintf(stderr
, "%s: more than one filename specified\n", pname
);
194 fprintf(stderr
, help
, pname
);
198 if (strcmp(filename
, "-")) {
199 fp
= fopen(filename
, "rb");
201 fprintf(stderr
, "%s: unable to open `%s': %s\n",
202 pname
, filename
, strerror(errno
));
212 * This main loop is really horrible, and wants rewriting with
213 * an axe. It'll stay the way it is for a while though, until I
218 nextsync
= next_sync (offset
, &synclen
);
220 unsigned long to_read
= buffer
+sizeof(buffer
)-p
;
221 if (to_read
> nextsync
-offset
-(p
-q
))
222 to_read
= nextsync
-offset
-(p
-q
);
224 lenread
= fread (p
, 1, to_read
, fp
);
226 eof
= TRUE
; /* help along systems with bad feof */
230 if (offset
== nextsync
) {
232 printf("%08lX skipping 0x%lX bytes\n", offset
, synclen
);
237 nextsync
= next_sync (offset
, &synclen
);
239 while (p
> q
&& (p
- q
>= INSN_MAX
|| lenread
== 0)) {
240 lendis
= disasm (q
, outbuf
, bits
, offset
, autosync
, prefer
);
241 if (!lendis
|| lendis
> (p
- q
) ||
242 lendis
> nextsync
-offset
)
243 lendis
= eatbyte (q
, outbuf
);
244 output_ins (offset
, q
, lendis
, outbuf
);
248 if (q
>= buffer
+INSN_MAX
) {
249 unsigned char *r
= buffer
, *s
= q
;
256 } while (lenread
> 0 || !(eof
|| feof(fp
)));
264 static void output_ins (unsigned long offset
, unsigned char *data
,
265 int datalen
, char *insn
)
268 printf("%08lX ", offset
);
271 while (datalen
> 0 && bytes
< BPL
) {
272 printf("%02X", *data
++);
277 printf("%*s%s\n", (BPL
+1-bytes
)*2, "", insn
);
279 while (datalen
> 0) {
282 while (datalen
> 0 && bytes
< BPL
) {
283 printf("%02X", *data
++);
292 * Skip a certain amount of data in a file, either by seeking if
293 * possible, or if that fails then by reading and discarding.
295 static void skip (unsigned long dist
, FILE *fp
)
297 char buffer
[256]; /* should fit on most stacks :-) */
300 * Got to be careful with fseek: at least one fseek I've tried
301 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
302 * ftell... horrible but apparently necessary.
304 if (fseek (fp
, dist
+ftell(fp
), SEEK_SET
)) {
306 unsigned long len
= (dist
< sizeof(buffer
) ?
307 dist
: sizeof(buffer
));
308 if (fread (buffer
, 1, len
, fp
) < len
) {