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.
22 #define BPL 8 /* bytes per line of hex dump */
24 static const char *help
=
25 "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
26 " [-e bytes] [-k start,bytes] [-p vendor] file\n"
27 " -a or -i activates auto (intelligent) sync\n"
29 " -b 16, -b 32 or -b 64 sets the processor mode\n"
30 " -h displays this text\n"
31 " -r or -v displays the version number\n"
32 " -e skips <bytes> bytes of header\n"
33 " -k avoids disassembling <bytes> bytes from position <start>\n"
34 " -p selects the preferred vendor instruction set (intel, amd, cyrix, idt)\n";
36 static void output_ins(uint32_t, uint8_t *, int, char *);
37 static void skip(uint32_t dist
, FILE * fp
);
39 int main(int argc
, char **argv
)
41 char buffer
[INSN_MAX
* 2], *p
, *ep
, *q
;
44 char *filename
= NULL
;
45 uint32_t nextsync
, synclen
, initskip
= 0L;
60 char *v
, *vv
, *p
= *++argv
;
61 if (*p
== '-' && p
[1]) {
64 switch (tolower(*p
)) {
65 case 'a': /* auto or intelligent sync */
71 fprintf(stderr
, help
);
76 "NDISASM version %s compiled " __DATE__
"\n",
79 case 'u': /* -u for -b 32, -uu for -b 64 */
85 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
87 fprintf(stderr
, "%s: `-b' requires an argument\n",
91 b
= strtoul(v
, &ep
, 10);
92 if (*ep
|| !(bits
== 16 || bits
== 32 || bits
== 64)) {
93 fprintf(stderr
, "%s: argument to `-b' should"
94 " be 16, 32 or 64\n", pname
);
98 p
= ""; /* force to next argument */
100 case 'o': /* origin */
101 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
103 fprintf(stderr
, "%s: `-o' requires an argument\n",
107 offset
= readnum(v
, &rn_error
);
110 "%s: `-o' requires a numeric argument\n",
114 p
= ""; /* force to next argument */
116 case 's': /* sync point */
117 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
119 fprintf(stderr
, "%s: `-s' requires an argument\n",
123 add_sync(readnum(v
, &rn_error
), 0L);
126 "%s: `-s' requires a numeric argument\n",
130 p
= ""; /* force to next argument */
132 case 'e': /* skip a header */
133 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
135 fprintf(stderr
, "%s: `-e' requires an argument\n",
139 initskip
= readnum(v
, &rn_error
);
142 "%s: `-e' requires a numeric argument\n",
146 p
= ""; /* force to next argument */
148 case 'k': /* skip a region */
149 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
151 fprintf(stderr
, "%s: `-k' requires an argument\n",
158 "%s: `-k' requires two numbers separated"
159 " by a comma\n", pname
);
163 nextsync
= readnum(v
, &rn_error
);
166 "%s: `-k' requires numeric arguments\n",
170 synclen
= readnum(vv
, &rn_error
);
173 "%s: `-k' requires numeric arguments\n",
177 add_sync(nextsync
, synclen
);
178 p
= ""; /* force to next argument */
180 case 'p': /* preferred vendor */
181 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
183 fprintf(stderr
, "%s: `-p' requires an argument\n",
187 if (!strcmp(v
, "intel")) {
188 prefer
= 0; /* Default */
189 } else if (!strcmp(v
, "amd")) {
190 prefer
= IF_AMD
| IF_3DNOW
;
191 } else if (!strcmp(v
, "cyrix")) {
192 prefer
= IF_CYRIX
| IF_3DNOW
;
193 } else if (!strcmp(v
, "idt") || !strcmp(v
, "centaur")
194 || !strcmp(v
, "winchip")) {
198 "%s: unknown vendor `%s' specified with `-p'\n",
202 p
= ""; /* force to next argument */
205 fprintf(stderr
, "%s: unrecognised option `-%c'\n",
209 } else if (!filename
) {
212 fprintf(stderr
, "%s: more than one filename specified\n",
219 fprintf(stderr
, help
, pname
);
223 if (strcmp(filename
, "-")) {
224 fp
= fopen(filename
, "rb");
226 fprintf(stderr
, "%s: unable to open `%s': %s\n",
227 pname
, filename
, strerror(errno
));
237 * This main loop is really horrible, and wants rewriting with
238 * an axe. It'll stay the way it is for a while though, until I
243 nextsync
= next_sync(offset
, &synclen
);
245 uint32_t to_read
= buffer
+ sizeof(buffer
) - p
;
246 if (to_read
> nextsync
- offset
- (p
- q
))
247 to_read
= nextsync
- offset
- (p
- q
);
249 lenread
= fread(p
, 1, to_read
, fp
);
251 eof
= TRUE
; /* help along systems with bad feof */
255 if ((uint32_t)offset
== nextsync
) {
257 fprintf(stdout
, "%08"PRIX32
" skipping 0x%"PRIX32
" bytes\n", offset
, synclen
);
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 || (uint32_t)lendis
> nextsync
- offset
)
270 lendis
= eatbyte(q
, outbuf
, sizeof(outbuf
));
271 output_ins(offset
, q
, lendis
, outbuf
);
275 if (q
>= buffer
+ INSN_MAX
) {
276 uint8_t *r
= buffer
, *s
= q
;
283 } while (lenread
> 0 || !(eof
|| feof(fp
)));
291 static void output_ins(uint32_t offset
, uint8_t *data
,
292 int datalen
, char *insn
)
295 fprintf(stdout
, "%08"PRIX32
" ", 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(uint32_t 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 uint32_t len
= (dist
< sizeof(buffer
) ?
334 dist
: sizeof(buffer
));
335 if (fread(buffer
, 1, len
, fp
) < len
) {