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;
50 unsigned long prefer
= 0;
59 char *v
, *vv
, *p
= *++argv
;
60 if (*p
== '-' && p
[1]) {
62 while (*p
) switch (tolower(*p
)) {
63 case 'a': /* auto or intelligent sync */
69 fprintf(stderr
, help
);
72 fprintf(stderr
, "NDISASM version " NASM_VER
"\n");
79 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
81 fprintf(stderr
, "%s: `-b' requires an argument\n", pname
);
86 else if (!strcmp(v
, "32"))
89 fprintf(stderr
, "%s: argument to `-b' should"
90 " be `16' or `32'\n", pname
);
92 p
= ""; /* force to next argument */
94 case 'o': /* origin */
95 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
97 fprintf(stderr
, "%s: `-o' requires an argument\n", pname
);
100 offset
= readnum (v
, &rn_error
);
102 fprintf(stderr
, "%s: `-o' requires a numeric argument\n",
106 p
= ""; /* force to next argument */
108 case 's': /* sync point */
109 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
111 fprintf(stderr
, "%s: `-s' requires an argument\n", pname
);
114 add_sync (readnum (v
, &rn_error
), 0L);
116 fprintf(stderr
, "%s: `-s' requires a numeric argument\n",
120 p
= ""; /* force to next argument */
122 case 'e': /* skip a header */
123 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
125 fprintf(stderr
, "%s: `-e' requires an argument\n", pname
);
128 initskip
= readnum (v
, &rn_error
);
130 fprintf(stderr
, "%s: `-e' requires a numeric argument\n",
134 p
= ""; /* force to next argument */
136 case 'k': /* skip a region */
137 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
139 fprintf(stderr
, "%s: `-k' requires an argument\n", pname
);
144 fprintf(stderr
, "%s: `-k' requires two numbers separated"
145 " by a comma\n", pname
);
149 nextsync
= readnum (v
, &rn_error
);
151 fprintf(stderr
, "%s: `-k' requires numeric arguments\n",
155 synclen
= readnum (vv
, &rn_error
);
157 fprintf(stderr
, "%s: `-k' requires numeric arguments\n",
161 add_sync (nextsync
, synclen
);
162 p
= ""; /* force to next argument */
164 case 'p': /* preferred vendor */
165 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
167 fprintf(stderr
, "%s: `-p' requires an argument\n", pname
);
170 if ( !strcmp(v
, "intel") ) {
171 prefer
= 0; /* Default */
172 } else if ( !strcmp(v
, "amd") ) {
173 prefer
= IF_AMD
|IF_3DNOW
;
174 } else if ( !strcmp(v
, "cyrix") ) {
175 prefer
= IF_CYRIX
|IF_3DNOW
;
176 } else if ( !strcmp(v
, "idt") || !strcmp(v
, "centaur") ||
177 !strcmp(v
, "winchip") ) {
180 fprintf(stderr
, "%s: unknown vendor `%s' specified with `-p'\n", pname
, v
);
183 p
= ""; /* force to next argument */
186 fprintf(stderr
, "%s: unrecognised option `-%c'\n",
190 } else if (!filename
) {
193 fprintf(stderr
, "%s: more than one filename specified\n", pname
);
199 fprintf(stderr
, help
, pname
);
203 if (strcmp(filename
, "-")) {
204 fp
= fopen(filename
, "rb");
206 fprintf(stderr
, "%s: unable to open `%s': %s\n",
207 pname
, filename
, strerror(errno
));
217 * This main loop is really horrible, and wants rewriting with
218 * an axe. It'll stay the way it is for a while though, until I
223 nextsync
= next_sync (offset
, &synclen
);
225 unsigned long to_read
= buffer
+sizeof(buffer
)-p
;
226 if (to_read
> nextsync
-offset
-(p
-q
))
227 to_read
= nextsync
-offset
-(p
-q
);
229 lenread
= fread (p
, 1, to_read
, fp
);
231 eof
= TRUE
; /* help along systems with bad feof */
235 if ((unsigned long)offset
== nextsync
) {
237 printf("%08lX skipping 0x%lX bytes\n", offset
, synclen
);
242 nextsync
= next_sync (offset
, &synclen
);
244 while (p
> q
&& (p
- q
>= INSN_MAX
|| lenread
== 0)) {
245 lendis
= disasm (q
, outbuf
, bits
, offset
, autosync
, prefer
);
246 if (!lendis
|| lendis
> (p
- q
) ||
247 (unsigned long)lendis
> nextsync
-offset
)
248 lendis
= eatbyte (q
, outbuf
);
249 output_ins (offset
, q
, lendis
, outbuf
);
253 if (q
>= buffer
+INSN_MAX
) {
254 unsigned char *r
= buffer
, *s
= q
;
261 } while (lenread
> 0 || !(eof
|| feof(fp
)));
269 static void output_ins (unsigned long offset
, unsigned char *data
,
270 int datalen
, char *insn
)
273 printf("%08lX ", offset
);
276 while (datalen
> 0 && bytes
< BPL
) {
277 printf("%02X", *data
++);
282 printf("%*s%s\n", (BPL
+1-bytes
)*2, "", insn
);
284 while (datalen
> 0) {
287 while (datalen
> 0 && bytes
< BPL
) {
288 printf("%02X", *data
++);
297 * Skip a certain amount of data in a file, either by seeking if
298 * possible, or if that fails then by reading and discarding.
300 static void skip (unsigned long dist
, FILE *fp
)
302 char buffer
[256]; /* should fit on most stacks :-) */
305 * Got to be careful with fseek: at least one fseek I've tried
306 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
307 * ftell... horrible but apparently necessary.
309 if (fseek (fp
, dist
+ftell(fp
), SEEK_SET
)) {
311 unsigned long len
= (dist
< sizeof(buffer
) ?
312 dist
: sizeof(buffer
));
313 if (fread (buffer
, 1, len
, fp
) < len
) {