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.
20 #define BPL 8 /* bytes per line of hex dump */
22 static const char *help
=
23 "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
24 " [-e bytes] [-k start,bytes] file\n"
25 " -a or -i activates auto (intelligent) sync\n"
26 " -u sets USE32 (32-bit mode)\n"
27 " -b 16 or -b 32 sets number of bits too\n"
28 " -h displays this text\n"
29 " -r displays the version number\n"
30 " -e skips <bytes> bytes of header\n"
31 " -k avoids disassembling <bytes> bytes from position <start>\n";
33 static void output_ins (unsigned long, unsigned char *, int, char *);
34 static void skip (unsigned long dist
, FILE *fp
);
36 int main(int argc
, char **argv
) {
37 unsigned char buffer
[INSN_MAX
* 2], *p
, *q
;
40 char *filename
= NULL
;
41 unsigned long nextsync
, synclen
, initskip
= 0L;
54 char *v
, *vv
, *p
= *++argv
;
57 while (*p
) switch (tolower(*p
)) {
58 case 'a': /* auto or intelligent sync */
64 fprintf(stderr
, help
);
67 fprintf(stderr
, "NDISASM version " NASM_VER
"\n");
74 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
76 fprintf(stderr
, "%s: `-b' requires an argument\n", pname
);
81 else if (!strcmp(v
, "32"))
84 fprintf(stderr
, "%s: argument to `-b' should"
85 " be `16' or `32'\n", pname
);
87 p
= ""; /* force to next argument */
89 case 'o': /* origin */
90 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
92 fprintf(stderr
, "%s: `-o' requires an argument\n", pname
);
95 offset
= readnum (v
, &rn_error
);
97 fprintf(stderr
, "%s: `-o' requires a numeric argument\n",
101 p
= ""; /* force to next argument */
103 case 's': /* sync point */
104 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
106 fprintf(stderr
, "%s: `-s' requires an argument\n", pname
);
109 add_sync (readnum (v
, &rn_error
), 0L);
111 fprintf(stderr
, "%s: `-s' requires a numeric argument\n",
115 p
= ""; /* force to next argument */
117 case 'e': /* skip a header */
118 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
120 fprintf(stderr
, "%s: `-e' requires an argument\n", pname
);
123 initskip
= readnum (v
, &rn_error
);
125 fprintf(stderr
, "%s: `-e' requires a numeric argument\n",
129 p
= ""; /* force to next argument */
131 case 'k': /* skip a region */
132 v
= p
[1] ? p
+1 : --argc
? *++argv
: NULL
;
134 fprintf(stderr
, "%s: `-k' requires an argument\n", pname
);
139 fprintf(stderr
, "%s: `-k' requires two numbers separated"
140 " by a comma\n", pname
);
144 nextsync
= readnum (v
, &rn_error
);
146 fprintf(stderr
, "%s: `-k' requires numeric arguments\n",
150 synclen
= readnum (vv
, &rn_error
);
152 fprintf(stderr
, "%s: `-k' requires numeric arguments\n",
156 add_sync (nextsync
, synclen
);
157 p
= ""; /* force to next argument */
160 } else if (!filename
) {
163 fprintf(stderr
, "%s: more than one filename specified\n", pname
);
169 fprintf(stderr
, help
, pname
);
173 fp
= fopen(filename
, "rb");
175 fprintf(stderr
, "%s: unable to open `%s': %s\n",
176 pname
, filename
, strerror(errno
));
183 * This main loop is really horrible, and wants rewriting with
184 * an axe. It'll stay the way it is for a while though, until I
189 nextsync
= next_sync (offset
, &synclen
);
191 unsigned long to_read
= buffer
+sizeof(buffer
)-p
;
192 if (to_read
> nextsync
-offset
-(p
-q
))
193 to_read
= nextsync
-offset
-(p
-q
);
194 lenread
= fread (p
, 1, to_read
, fp
);
196 eof
= TRUE
; /* help along systems with bad feof */
198 if (offset
== nextsync
) {
200 printf("%08lX skipping 0x%lX bytes\n", offset
, synclen
);
205 nextsync
= next_sync (offset
, &synclen
);
207 while (p
> q
&& (p
- q
>= INSN_MAX
|| lenread
== 0)) {
208 lendis
= disasm (q
, outbuf
, bits
, offset
, autosync
);
209 if (!lendis
|| lendis
> (p
- q
) ||
210 lendis
> nextsync
-offset
)
211 lendis
= eatbyte (q
, outbuf
);
212 output_ins (offset
, q
, lendis
, outbuf
);
216 if (q
>= buffer
+INSN_MAX
) {
217 unsigned char *r
= buffer
, *s
= q
;
224 } while (lenread
> 0 || !(eof
|| feof(fp
)));
229 static void output_ins (unsigned long offset
, unsigned char *data
,
230 int datalen
, char *insn
) {
232 printf("%08lX ", offset
);
235 while (datalen
> 0 && bytes
< BPL
) {
236 printf("%02X", *data
++);
241 printf("%*s%s\n", (BPL
+1-bytes
)*2, "", insn
);
243 while (datalen
> 0) {
246 while (datalen
> 0 && bytes
< BPL
) {
247 printf("%02X", *data
++);
256 * Skip a certain amount of data in a file, either by seeking if
257 * possible, or if that fails then by reading and discarding.
259 static void skip (unsigned long dist
, FILE *fp
) {
260 char buffer
[256]; /* should fit on most stacks :-) */
263 * Got to be careful with fseek: at least one fseek I've tried
264 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
265 * ftell... horrible but apparently necessary.
267 if (fseek (fp
, dist
+ftell(fp
), SEEK_SET
)) {
269 unsigned long len
= (dist
< sizeof(buffer
) ?
270 dist
: sizeof(buffer
));
271 if (fread (buffer
, 1, len
, fp
) < len
) {