NASM 0.98bf
[nasm/avx512.git] / ndisasm.c
blob53de839c953046bdc26f8ffb6baeb027c31ab28a
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.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <errno.h>
15 #include "insns.h"
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "sync.h"
19 #include "disasm.h"
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;
41 char outbuf[256];
42 char *pname = *argv;
43 char *filename = NULL;
44 unsigned long nextsync, synclen, initskip = 0L;
45 int lenread;
46 long lendis;
47 int autosync = FALSE;
48 int bits = 16;
49 int eof = FALSE;
50 unsigned long prefer = 0;
51 int rn_error;
52 long offset;
53 FILE *fp;
55 offset = 0;
56 init_sync();
58 while (--argc) {
59 char *v, *vv, *p = *++argv;
60 if (*p == '-' && p[1]) {
61 p++;
62 while (*p) switch (tolower(*p)) {
63 case 'a': /* auto or intelligent sync */
64 case 'i':
65 autosync = TRUE;
66 p++;
67 break;
68 case 'h':
69 fprintf(stderr, help);
70 return 0;
71 case 'r':
72 fprintf(stderr, "NDISASM version " NASM_VER "\n");
73 return 0;
74 case 'u': /* USE32 */
75 bits = 32;
76 p++;
77 break;
78 case 'b': /* bits */
79 v = p[1] ? p+1 : --argc ? *++argv : NULL;
80 if (!v) {
81 fprintf(stderr, "%s: `-b' requires an argument\n", pname);
82 return 1;
84 if (!strcmp(v, "16"))
85 bits = 16;
86 else if (!strcmp(v, "32"))
87 bits = 32;
88 else {
89 fprintf(stderr, "%s: argument to `-b' should"
90 " be `16' or `32'\n", pname);
92 p = ""; /* force to next argument */
93 break;
94 case 'o': /* origin */
95 v = p[1] ? p+1 : --argc ? *++argv : NULL;
96 if (!v) {
97 fprintf(stderr, "%s: `-o' requires an argument\n", pname);
98 return 1;
100 offset = readnum (v, &rn_error);
101 if (rn_error) {
102 fprintf(stderr, "%s: `-o' requires a numeric argument\n",
103 pname);
104 return 1;
106 p = ""; /* force to next argument */
107 break;
108 case 's': /* sync point */
109 v = p[1] ? p+1 : --argc ? *++argv : NULL;
110 if (!v) {
111 fprintf(stderr, "%s: `-s' requires an argument\n", pname);
112 return 1;
114 add_sync (readnum (v, &rn_error), 0L);
115 if (rn_error) {
116 fprintf(stderr, "%s: `-s' requires a numeric argument\n",
117 pname);
118 return 1;
120 p = ""; /* force to next argument */
121 break;
122 case 'e': /* skip a header */
123 v = p[1] ? p+1 : --argc ? *++argv : NULL;
124 if (!v) {
125 fprintf(stderr, "%s: `-e' requires an argument\n", pname);
126 return 1;
128 initskip = readnum (v, &rn_error);
129 if (rn_error) {
130 fprintf(stderr, "%s: `-e' requires a numeric argument\n",
131 pname);
132 return 1;
134 p = ""; /* force to next argument */
135 break;
136 case 'k': /* skip a region */
137 v = p[1] ? p+1 : --argc ? *++argv : NULL;
138 if (!v) {
139 fprintf(stderr, "%s: `-k' requires an argument\n", pname);
140 return 1;
142 vv = strchr(v, ',');
143 if (!vv) {
144 fprintf(stderr, "%s: `-k' requires two numbers separated"
145 " by a comma\n", pname);
146 return 1;
148 *vv++ = '\0';
149 nextsync = readnum (v, &rn_error);
150 if (rn_error) {
151 fprintf(stderr, "%s: `-k' requires numeric arguments\n",
152 pname);
153 return 1;
155 synclen = readnum (vv, &rn_error);
156 if (rn_error) {
157 fprintf(stderr, "%s: `-k' requires numeric arguments\n",
158 pname);
159 return 1;
161 add_sync (nextsync, synclen);
162 p = ""; /* force to next argument */
163 break;
164 case 'p': /* preferred vendor */
165 v = p[1] ? p+1 : --argc ? *++argv : NULL;
166 if (!v) {
167 fprintf(stderr, "%s: `-p' requires an argument\n", pname);
168 return 1;
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") ) {
178 prefer = IF_3DNOW;
179 } else {
180 fprintf(stderr, "%s: unknown vendor `%s' specified with `-p'\n", pname, v);
181 return 1;
183 p = ""; /* force to next argument */
184 break;
185 default: /*bf*/
186 fprintf(stderr, "%s: unrecognised option `-%c'\n",
187 pname, *p);
188 return 1;
190 } else if (!filename) {
191 filename = p;
192 } else {
193 fprintf(stderr, "%s: more than one filename specified\n", pname);
194 return 1;
198 if (!filename) {
199 fprintf(stderr, help, pname);
200 return 0;
203 if (strcmp(filename, "-")) {
204 fp = fopen(filename, "rb");
205 if (!fp) {
206 fprintf(stderr, "%s: unable to open `%s': %s\n",
207 pname, filename, strerror(errno));
208 return 1;
210 } else
211 fp = stdin;
213 if (initskip > 0)
214 skip (initskip, fp);
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
219 * find the energy...
222 p = q = buffer;
223 nextsync = next_sync (offset, &synclen);
224 do {
225 unsigned long to_read = buffer+sizeof(buffer)-p;
226 if (to_read > nextsync-offset-(p-q))
227 to_read = nextsync-offset-(p-q);
228 if (to_read) {
229 lenread = fread (p, 1, to_read, fp);
230 if (lenread == 0)
231 eof = TRUE; /* help along systems with bad feof */
232 } else
233 lenread = 0;
234 p += lenread;
235 if ((unsigned long)offset == nextsync) {
236 if (synclen) {
237 printf("%08lX skipping 0x%lX bytes\n", offset, synclen);
238 offset += synclen;
239 skip (synclen, fp);
241 p = q = buffer;
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);
250 q += lendis;
251 offset += lendis;
253 if (q >= buffer+INSN_MAX) {
254 unsigned char *r = buffer, *s = q;
255 int count = p - q;
256 while (count--)
257 *r++ = *s++;
258 p -= (q - buffer);
259 q = buffer;
261 } while (lenread > 0 || !(eof || feof(fp)));
263 if (fp != stdin)
264 fclose (fp);
266 return 0;
269 static void output_ins (unsigned long offset, unsigned char *data,
270 int datalen, char *insn)
272 int bytes;
273 printf("%08lX ", offset);
275 bytes = 0;
276 while (datalen > 0 && bytes < BPL) {
277 printf("%02X", *data++);
278 bytes++;
279 datalen--;
282 printf("%*s%s\n", (BPL+1-bytes)*2, "", insn);
284 while (datalen > 0) {
285 printf(" -");
286 bytes = 0;
287 while (datalen > 0 && bytes < BPL) {
288 printf("%02X", *data++);
289 bytes++;
290 datalen--;
292 printf("\n");
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)) {
310 while (dist > 0) {
311 unsigned long len = (dist < sizeof(buffer) ?
312 dist : sizeof(buffer));
313 if (fread (buffer, 1, len, fp) < len) {
314 perror("fread");
315 exit(1);
317 dist -= len;