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.
18 #define BPL 8 /* bytes per line of hex dump */
20 static const char *help
=
21 "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
22 " [-e bytes] [-k start,bytes] file\n"
23 " -a or -i activates auto (intelligent) sync\n"
24 " -u sets USE32 (32-bit mode)\n"
25 " -b 16 or -b 32 sets number of bits too\n"
26 " -h displays this text\n"
27 " -r displays the version number\n"
28 " -e skips <bytes> bytes of header\n"
29 " -k avoids disassembling <bytes> bytes from position <start>\n";
31 static void output_ins (unsigned long, unsigned char *, int, char *);
32 static void skip (unsigned long dist
, FILE *fp
);
34 int main(int argc
, char **argv
) {
35 unsigned char buffer
[INSN_MAX
* 2], *p
, *q
;
38 char *filename
= NULL
;
39 unsigned long nextsync
, synclen
, initskip
= 0L;
51 char *v
, *vv
, *p
= *++argv
;
54 while (*p
) switch (tolower(*p
)) {
55 case 'a': /* auto or intelligent sync */
61 fprintf(stderr
, help
);
65 fprintf(stderr
, "NDISASM version " NASM_VER
"\n");
73 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
75 fprintf(stderr
, "%s: `-b' requires an argument\n", pname
);
80 else if (!strcmp(v
, "32"))
83 fprintf(stderr
, "%s: argument to `-b' should"
84 " be `16' or `32'\n", pname
);
86 p
= ""; /* force to next argument */
88 case 'o': /* origin */
89 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
91 fprintf(stderr
, "%s: `-o' requires an argument\n", pname
);
94 offset
= readnum (v
, &rn_error
);
96 fprintf(stderr
, "%s: `-o' requires a numeric argument\n",
100 p
= ""; /* force to next argument */
102 case 's': /* sync point */
103 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
105 fprintf(stderr
, "%s: `-s' requires an argument\n", pname
);
108 add_sync (readnum (v
, &rn_error
), 0L);
110 fprintf(stderr
, "%s: `-s' requires a numeric argument\n",
114 p
= ""; /* force to next argument */
116 case 'e': /* skip a header */
117 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
119 fprintf(stderr
, "%s: `-e' requires an argument\n", pname
);
122 initskip
= readnum (v
, &rn_error
);
124 fprintf(stderr
, "%s: `-e' requires a numeric argument\n",
128 p
= ""; /* force to next argument */
130 case 'k': /* skip a region */
131 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
133 fprintf(stderr
, "%s: `-k' requires an argument\n", pname
);
138 fprintf(stderr
, "%s: `-k' requires two numbers separated"
139 " by a comma\n", pname
);
143 nextsync
= readnum (v
, &rn_error
);
145 fprintf(stderr
, "%s: `-k' requires numeric arguments\n",
149 synclen
= readnum (vv
, &rn_error
);
151 fprintf(stderr
, "%s: `-k' requires numeric arguments\n",
155 add_sync (nextsync
, synclen
);
156 p
= ""; /* force to next argument */
159 } else if (!filename
) {
162 fprintf(stderr
, "%s: more than one filename specified\n", pname
);
168 fprintf(stderr
, help
, pname
);
172 fp
= fopen(filename
, "rb");
177 * This main loop is really horrible, and wants rewriting with
178 * an axe. It'll stay the way it is for a while though, until I
183 nextsync
= next_sync (offset
, &synclen
);
185 unsigned long to_read
= buffer
+sizeof(buffer
)-p
;
186 if (to_read
> nextsync
-offset
-(p
-q
))
187 to_read
= nextsync
-offset
-(p
-q
);
188 lenread
= fread (p
, 1, to_read
, fp
);
190 if (offset
== nextsync
) {
192 printf("%08lX skipping 0x%lX bytes\n", offset
, synclen
);
197 nextsync
= next_sync (offset
, &synclen
);
199 while (p
> q
&& (p
- q
>= INSN_MAX
|| lenread
== 0)) {
200 lendis
= disasm (q
, outbuf
, bits
, offset
, autosync
);
201 if (!lendis
|| lendis
> (p
- q
) ||
202 lendis
> nextsync
-offset
)
203 lendis
= eatbyte (q
, outbuf
);
204 output_ins (offset
, q
, lendis
, outbuf
);
208 if (q
>= buffer
+INSN_MAX
) {
209 unsigned char *r
= buffer
, *s
= q
;
216 } while (lenread
> 0 || !feof(fp
));
221 static void output_ins (unsigned long offset
, unsigned char *data
,
222 int datalen
, char *insn
) {
224 printf("%08lX ", offset
);
227 while (datalen
> 0 && bytes
< BPL
) {
228 printf("%02X", *data
++);
233 printf("%*s%s\n", (BPL
+1-bytes
)*2, "", insn
);
235 while (datalen
> 0) {
238 while (datalen
> 0 && bytes
< BPL
) {
239 printf("%02X", *data
++);
248 * Skip a certain amount of data in a file, either by seeking if
249 * possible, or if that fails then by reading and discarding.
251 static void skip (unsigned long dist
, FILE *fp
) {
252 char buffer
[256]; /* should fit on most stacks :-) */
255 * Got to be careful with fseek: at least one fseek I've tried
256 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
257 * ftell... horrible but apparently necessary.
259 if (fseek (fp
, dist
+ftell(fp
), SEEK_SET
)) {
261 unsigned long len
= (dist
< sizeof(buffer
) ?
262 dist
: sizeof(buffer
));
263 if (fread (buffer
, 1, len
, fp
) < len
) {