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 or -v 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]) {
63 switch (tolower(*p)) {
64 case 'a': /* auto or intelligent sync */
70 fprintf(stderr, help);
75 "NDISASM version %s compiled " __DATE__ "\n",
83 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
85 fprintf(stderr, "%s: `-b' requires an argument\n",
91 else if (!strcmp(v, "32"))
94 fprintf(stderr, "%s: argument to `-b' should"
95 " be `16' or `32'\n", pname);
97 p = ""; /* force to next argument */
99 case 'o': /* origin */
100 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
102 fprintf(stderr, "%s: `-o' requires an argument\n",
106 offset = readnum(v, &rn_error);
109 "%s: `-o' requires a numeric argument\n",
113 p = ""; /* force to next argument */
115 case 's': /* sync point */
116 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
118 fprintf(stderr, "%s: `-s' requires an argument\n",
122 add_sync(readnum(v, &rn_error), 0L);
125 "%s: `-s' requires a numeric argument\n",
129 p = ""; /* force to next argument */
131 case 'e': /* skip a header */
132 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
134 fprintf(stderr, "%s: `-e' requires an argument\n",
138 initskip = readnum(v, &rn_error);
141 "%s: `-e' requires a numeric argument\n",
145 p = ""; /* force to next argument */
147 case 'k': /* skip a region */
148 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
150 fprintf(stderr, "%s: `-k' requires an argument\n",
157 "%s: `-k' requires two numbers separated"
158 " by a comma\n", pname);
162 nextsync = readnum(v, &rn_error);
165 "%s: `-k' requires numeric arguments\n",
169 synclen = readnum(vv, &rn_error);
172 "%s: `-k' requires numeric arguments\n",
176 add_sync(nextsync, synclen);
177 p = ""; /* force to next argument */
179 case 'p': /* preferred vendor */
180 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
182 fprintf(stderr, "%s: `-p' requires an argument\n",
186 if (!strcmp(v, "intel")) {
187 prefer = 0; /* Default */
188 } else if (!strcmp(v, "amd")) {
189 prefer = IF_AMD | IF_3DNOW;
190 } else if (!strcmp(v, "cyrix")) {
191 prefer = IF_CYRIX | IF_3DNOW;
192 } else if (!strcmp(v, "idt") || !strcmp(v, "centaur")
193 || !strcmp(v, "winchip")) {
197 "%s: unknown vendor `%s' specified with `-p'\n",
201 p = ""; /* force to next argument */
204 fprintf(stderr, "%s: unrecognised option `-%c'\n",
208 } else if (!filename) {
211 fprintf(stderr, "%s: more than one filename specified\n",
218 fprintf(stderr, help, pname);
222 if (strcmp(filename, "-")) {
223 fp = fopen(filename, "rb");
225 fprintf(stderr, "%s: unable to open `%s': %s\n",
226 pname, filename, strerror(errno));
236 * This main loop is really horrible, and wants rewriting with
237 * an axe. It'll stay the way it is for a while though, until I
242 nextsync = next_sync(offset, &synclen);
244 unsigned long to_read = buffer + sizeof(buffer) - p;
245 if (to_read > nextsync - offset - (p - q))
246 to_read = nextsync - offset - (p - q);
248 lenread = fread(p, 1, to_read, fp);
250 eof = TRUE; /* help along systems with bad feof */
254 if ((unsigned long)offset == nextsync) {
256 fprintf(stdout, "%08lX skipping 0x%lX bytes\n", offset,
262 nextsync = next_sync(offset, &synclen);
264 while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
266 disasm(q, outbuf, sizeof(outbuf), bits, offset, autosync,
268 if (!lendis || lendis > (p - q)
269 || (unsigned long)lendis > nextsync - offset)
270 lendis = eatbyte(q, outbuf, sizeof(outbuf));
271 output_ins(offset, q, lendis, outbuf);
275 if (q >= buffer + INSN_MAX) {
276 unsigned char *r = buffer, *s = q;
283 } while (lenread > 0 || !(eof || feof(fp)));
291 static void output_ins(unsigned long offset, unsigned char *data,
292 int datalen, char *insn)
295 fprintf(stdout, "%08lX ", offset);
298 while (datalen > 0 && bytes < BPL) {
299 fprintf(stdout, "%02X", *data++);
304 fprintf(stdout, "%*s%s\n", (BPL + 1 - bytes) * 2, "", insn);
306 while (datalen > 0) {
307 fprintf(stdout, " -");
309 while (datalen > 0 && bytes < BPL) {
310 fprintf(stdout, "%02X", *data++);
314 fprintf(stdout, "\n");
319 * Skip a certain amount of data in a file, either by seeking if
320 * possible, or if that fails then by reading and discarding.
322 static void skip(unsigned long dist, FILE * fp)
324 char buffer[256]; /* should fit on most stacks :-) */
327 * Got to be careful with fseek: at least one fseek I've tried
328 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
329 * ftell... horrible but apparently necessary.
331 if (fseek(fp, dist + ftell(fp), SEEK_SET)) {
333 unsigned long len = (dist < sizeof(buffer) ?
334 dist : sizeof(buffer));
335 if (fread(buffer, 1, len, fp) < len) {