Debian Codegraph @

src/debug/debug_disasm.cpp

raw
1
2 /*
3 Ripped out some stuff from the mame releae to only make it for 386's
4 Changed some variables to use the standard DOSBox data types
5 Added my callback opcode
6
7 */
8
9 /*
10 * 2asm: Convert binary files to 80*86 assembler. Version 1.00
11 * Adapted by Andrea Mazzoleni for use with MAME
12 * HJB 990321:
13 * Changed output of hex values from 0xxxxh to $xxxx format
14 * Removed "ptr" from "byte ptr", "word ptr" and "dword ptr"
15 */
16
17 /* 2asm comments
18
19 License:
20
21 This program is free software; you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 Comments:
31
32 The code was originally snaffled from the GNU C++ debugger, as ported
33 to DOS by DJ Delorie and Kent Williams ([email protected]).
34 Extensively modified by Robin Hilliard in Jan and May 1992.
35
36 This source compiles under Turbo C v2.01. The disassembler is entirely
37 table driven so it's fairly easy to change to suit your own tastes.
38
39 The instruction table has been modified to correspond with that in
40 `Programmer's Technical Reference: The Processor and Coprocessor',
41 Robert L. Hummel, Ziff-Davis Press, 1992. Missing (read "undocumented")
42 instructions were added and many mistakes and omissions corrected.
43
44
45 Health warning:
46
47 When writing and degbugging this code, I didn't have (and still don't have)
48 a 32-bit disassembler to compare this guy's output with. It's therefore
49 quite likely that bugs will appear when disassembling instructions which use
50 the 386 and 486's native 32 bit mode. It seems to work fine in 16 bit mode.
51
52 Any comments/updates/bug reports to:
53
54 Robin Hilliard, Lough Guitane, Killarney, Co. Kerry, Ireland.
55 Tel: [+353] 64-54014
56 Internet: [email protected]
57 Compu$erve: 100042, 1237
58
59 If you feel like registering, and possibly get notices of updates and
60 other items of software, then send me a post card of your home town.
61
62 Thanks and enjoy!
63
64 */
65 #include "dosbox.h"
66 #if C_DEBUG
67 #include <stdio.h>
68 #include <string.h>
69 #include <stdarg.h>
70 #include <stdlib.h>
71 #include "mem.h"
72
73 typedef Bit8u UINT8;
74 typedef Bit16u UINT16;
75 typedef Bit32u UINT32;
76
77 typedef Bit8s INT8;
78 typedef Bit16s INT16;
79 typedef Bit32s INT32;
80
81
82 /* Little endian uint read */
83 #define le_uint8(ptr) (*(UINT8*)ptr)
84
85 INLINE UINT16 le_uint16(const void* ptr) {
86 const UINT8* ptr8 = (const UINT8*)ptr;
87 return (UINT16)ptr8[0] | (UINT16)ptr8[1] << 8;
88 }
89 INLINE UINT32 le_uint32(const void* ptr) {
90 const UINT8* ptr8 = (const UINT8*)ptr;
91 return (UINT32)ptr8[0] | (UINT32)ptr8[1] << 8 | (UINT32)ptr8[2] << 16 | (UINT32)ptr8[3] << 24;
92 }
93
94 /* Little endian int read */
95 #define le_int8(ptr) ((INT8)le_uint8(ptr))
96 #define le_int16(ptr) ((INT16)le_uint16(ptr))
97 #define le_int32(ptr) ((INT32)le_uint32(ptr))
98
99 #define fp_segment(dw) ((dw >> 16) & 0xFFFFU)
100 #define fp_offset(dw) (dw & 0xFFFFU)
101 #define fp_addr(seg, off) ( (seg<<4)+off )
102
103 static UINT8 must_do_size; /* used with size of operand */
104 static int wordop; /* dealing with word or byte operand */
105
106 static int instruction_offset;
107 //static UINT16 instruction_segment;
108
109 static char* ubufs; /* start of buffer */
110 static char* ubufp; /* last position of buffer */
111 static int invalid_opcode = 0;
112 static int first_space = 1;
113
114 static int prefix; /* segment override prefix byte */
115 static int modrmv; /* flag for getting modrm byte */
116 static int sibv; /* flag for getting sib byte */
117 static int opsize; /* just like it says ... */
118 static int addrsize;
119 static int addr32bit=0;
120
121 /* some defines for extracting instruction bit fields from bytes */
122
123 #define MOD(a) (((a)>>6)&7)
124 #define REG(a) (((a)>>3)&7)
125 #define RM(a) ((a)&7)
126 #define SCALE(a) (((a)>>6)&7)
127 #define INDEX(a) (((a)>>3)&7)
128 #define BASE(a) ((a)&7)
129
130 /* Percent tokens in strings:
131 First char after '%':
132 A - direct address
133 C - reg of r/m picks control register
134 D - reg of r/m picks debug register
135 E - r/m picks operand
136 F - flags register
137 G - reg of r/m picks general register
138 I - immediate data
139 J - relative IP offset
140 + K - call/jmp distance
141 M - r/m picks memory
142 O - no r/m, offset only
143 R - mod of r/m picks register only
144 S - reg of r/m picks segment register
145 T - reg of r/m picks test register
146 X - DS:ESI
147 Y - ES:EDI
148 2 - prefix of two-byte opcode
149 + e - put in 'e' if use32 (second char is part of reg name)
150 + put in 'w' for use16 or 'd' for use32 (second char is 'w')
151 + j - put in 'e' in jcxz if prefix==0x66
152 f - floating point (second char is esc value)
153 g - do r/m group 'n', n==0..7
154 p - prefix
155 s - size override (second char is a,o)
156 + d - put d if double arg, nothing otherwise (pushfd, popfd &c)
157 + w - put w if word, d if double arg, nothing otherwise (lodsw/lodsd)
158 + P - simple prefix
159
160 Second char after '%':
161 a - two words in memory (BOUND)
162 b - byte
163 c - byte or word
164 d - dword
165 + f - far call/jmp
166 + n - near call/jmp
167 p - 32 or 48 bit pointer
168 + q - byte/word thingy
169 s - six byte pseudo-descriptor
170 v - word or dword
171 w - word
172 + x - sign extended byte
173 F - use floating regs in mod/rm
174 1-8 - group number, esc value, etc
175 */
176
177 /* watch out for aad && aam with odd operands */
178
179 static char const* (*opmap1)[256];
180
181 static char const * op386map1[256] = {
182 /* 0 */
183 "add %Eb,%Gb", "add %Ev,%Gv", "add %Gb,%Eb", "add %Gv,%Ev",
184 "add al,%Ib", "add %eax,%Iv", "push es", "pop es",
185 "or %Eb,%Gb", "or %Ev,%Gv", "or %Gb,%Eb", "or %Gv,%Ev",
186 "or al,%Ib", "or %eax,%Iv", "push cs", "%2 ",
187 /* 1 */
188 "adc %Eb,%Gb", "adc %Ev,%Gv", "adc %Gb,%Eb", "adc %Gv,%Ev",
189 "adc al,%Ib", "adc %eax,%Iv", "push ss", "pop ss",
190 "sbb %Eb,%Gb", "sbb %Ev,%Gv", "sbb %Gb,%Eb", "sbb %Gv,%Ev",
191 "sbb al,%Ib", "sbb %eax,%Iv", "push ds", "pop ds",
192 /* 2 */
193 "and %Eb,%Gb", "and %Ev,%Gv", "and %Gb,%Eb", "and %Gv,%Ev",
194 "and al,%Ib", "and %eax,%Iv", "%pe", "daa",
195 "sub %Eb,%Gb", "sub %Ev,%Gv", "sub %Gb,%Eb", "sub %Gv,%Ev",
196 "sub al,%Ib", "sub %eax,%Iv", "%pc", "das",
197 /* 3 */
198 "xor %Eb,%Gb", "xor %Ev,%Gv", "xor %Gb,%Eb", "xor %Gv,%Ev",
199 "xor al,%Ib", "xor %eax,%Iv", "%ps", "aaa",
200 "cmp %Eb,%Gb", "cmp %Ev,%Gv", "cmp %Gb,%Eb", "cmp %Gv,%Ev",
201 "cmp al,%Ib", "cmp %eax,%Iv", "%pd", "aas",
202 /* 4 */
203 "inc %eax", "inc %ecx", "inc %edx", "inc %ebx",
204 "inc %esp", "inc %ebp", "inc %esi", "inc %edi",
205 "dec %eax", "dec %ecx", "dec %edx", "dec %ebx",
206 "dec %esp", "dec %ebp", "dec %esi", "dec %edi",
207 /* 5 */
208 "push %eax", "push %ecx", "push %edx", "push %ebx",
209 "push %esp", "push %ebp", "push %esi", "push %edi",
210 "pop %eax", "pop %ecx", "pop %edx", "pop %ebx",
211 "pop %esp", "pop %ebp", "pop %esi", "pop %edi",
212 /* 6 */
213 "pusha%d ", "popa%d ", "bound %Gv,%Ma", "arpl %Ew,%Rw",
214 "%pf", "%pg", "%so", "%sa",
215 "push %Iv", "imul %Gv,%Ev,%Iv","push %Ix", "imul %Gv,%Ev,%Ib",
216 "insb", "ins%ew", "outsb", "outs%ew",
217 /* 7 */
218 "jo %Jb", "jno %Jb", "jc %Jb", "jnc %Jb",
219 "je %Jb", "jne %Jb", "jbe %Jb", "ja %Jb",
220 "js %Jb", "jns %Jb", "jpe %Jb", "jpo %Jb",
221 "jl %Jb", "jge %Jb", "jle %Jb", "jg %Jb",
222 /* 8 */
223 "%g0 %Eb,%Ib", "%g0 %Ev,%Iv", "%g0 %Eb,%Ib", "%g0 %Ev,%Ix",
224 "test %Eb,%Gb", "test %Ev,%Gv", "xchg %Eb,%Gb", "xchg %Ev,%Gv",
225 "mov %Eb,%Gb", "mov %Ev,%Gv", "mov %Gb,%Eb", "mov %Gv,%Ev",
226 "mov %Ew,%Sw", "lea %Gv,%M ", "mov %Sw,%Ew", "pop %Ev",
227 /* 9 */
228 "nop", "xchg %ecx,%eax", "xchg %edx,%eax", "xchg %ebx,%eax",
229 "xchg %esp,%eax", "xchg %ebp,%eax", "xchg %esi,%eax", "xchg %edi,%eax",
230 "cbw", "cwd", "call %Ap", "fwait",
231 "pushf%d ", "popf%d ", "sahf", "lahf",
232 /* a */
233 "mov al,%Oc", "mov %eax,%Ov", "mov %Oc,al", "mov %Ov,%eax",
234 "%P movsb", "%P movs%w", "%P cmpsb", "%P cmps%w ",
235 "test al,%Ib", "test %eax,%Iv", "%P stosb", "%P stos%w ",
236 "%P lodsb", "%P lods%w ", "%P scasb", "%P scas%w ",
237 /* b */
238 "mov al,%Ib", "mov cl,%Ib", "mov dl,%Ib", "mov bl,%Ib",
239 "mov ah,%Ib", "mov ch,%Ib", "mov dh,%Ib", "mov bh,%Ib",
240 "mov %eax,%Iv", "mov %ecx,%Iv", "mov %edx,%Iv", "mov %ebx,%Iv",
241 "mov %esp,%Iv", "mov %ebp,%Iv", "mov %esi,%Iv", "mov %edi,%Iv",
242 /* c */
243 "%g1 %Eb,%Ib", "%g1 %Ev,%Ib", "ret %Iw", "ret",
244 "les %Gv,%Mp", "lds %Gv,%Mp", "mov %Eb,%Ib", "mov %Ev,%Iv",
245 "enter %Iw,%Ib", "leave", "retf %Iw", "retf",
246 "int 03", "int %Ib", "into", "iret",
247 /* d */
248 "%g1 %Eb,1", "%g1 %Ev,1", "%g1 %Eb,cl", "%g1 %Ev,cl",
249 "aam ; %Ib", "aad ; %Ib", "setalc", "xlat",
250 #if 0
251 "esc 0,%Ib", "esc 1,%Ib", "esc 2,%Ib", "esc 3,%Ib",
252 "esc 4,%Ib", "esc 5,%Ib", "esc 6,%Ib", "esc 7,%Ib",
253 #else
254 "%f0", "%f1", "%f2", "%f3",
255 "%f4", "%f5", "%f6", "%f7",
256 #endif
257 /* e */
258 "loopne %Jb", "loope %Jb", "loop %Jb", "j%j cxz %Jb",
259 "in al,%Ib", "in %eax,%Ib", "out %Ib,al", "out %Ib,%eax",
260 "call %Jv", "jmp %Jv", "jmp %Ap", "jmp %Ks%Jb",
261 "in al,dx", "in %eax,dx", "out dx,al", "out dx,%eax",
262 /* f */
263 "lock %p ", "icebp", "repne %p ", "repe %p ",
264 "hlt", "cmc", "%g2", "%g2",
265 "clc", "stc", "cli", "sti",
266 "cld", "std", "%g3", "%g4"
267 };
268
269 static char const *second[] = {
270 /* 0 */
271 "%g5", "%g6", "lar %Gv,%Ew", "lsl %Gv,%Ew",
272 0, "[loadall]", "clts", "[loadall]",
273 "invd", "wbinvd", 0, "UD2",
274 0, 0, 0, 0,
275 /* 1 */
276 "mov %Eb,%Gb", "mov %Ev,%Gv", "mov %Gb,%Eb", "mov %Gv,%Ev",
277 0, 0, 0, 0,
278 0, 0, 0, 0,
279 0, 0, 0, 0,
280 /* 2 */
281 "mov %Rd,%Cd", "mov %Rd,%Dd", "mov %Cd,%Rd", "mov %Dd,%Rd",
282 "mov %Rd,%Td", 0, "mov %Td,%Rd", 0,
283 0, 0, 0, 0,
284 0, 0, 0, 0,
285 /* 3 */
286 0, 0, 0, 0, 0, 0, 0, 0,
287 0, 0, 0, 0, 0, 0, 0, 0,
288 /* 4 */
289 0, 0, 0, 0, 0, 0, 0, 0,
290 0, 0, 0, 0, 0, 0, 0, 0,
291 /* 5 */
292 0, 0, 0, 0, 0, 0, 0, 0,
293 0, 0, 0, 0, 0, 0, 0, 0,
294 /* 6 */
295 0, 0, 0, 0, 0, 0, 0, 0,
296 0, 0, 0, 0, 0, 0, 0, 0,
297 /* 7 */
298 0, 0, 0, 0, 0, 0, 0, 0,
299 0, 0, 0, 0, 0, 0, 0, 0,
300 /* 8 */
301 "jo %Jv", "jno %Jv", "jb %Jv", "jnb %Jv",
302 "jz %Jv", "jnz %Jv", "jbe %Jv", "ja %Jv",
303 "js %Jv", "jns %Jv", "jp %Jv", "jnp %Jv",
304 "jl %Jv", "jge %Jv", "jle %Jv", "jg %Jv",
305 /* 9 */
306 "seto %Eb", "setno %Eb", "setc %Eb", "setnc %Eb",
307 "setz %Eb", "setnz %Eb", "setbe %Eb", "setnbe %Eb",
308 "sets %Eb", "setns %Eb", "setp %Eb", "setnp %Eb",
309 "setl %Eb", "setge %Eb", "setle %Eb", "setg %Eb",
310 /* a */
311 "push fs", "pop fs", "cpuid", "bt %Ev,%Gv",
312 "shld %Ev,%Gv,%Ib", "shld %Ev,%Gv,cl", 0, 0,
313 "push gs", "pop gs", 0, "bts %Ev,%Gv",
314 "shrd %Ev,%Gv,%Ib", "shrd %Ev,%Gv,cl", 0, "imul %Gv,%Ev",
315 /* b */
316 "cmpxchg %Eb,%Gb", "cmpxchg %Ev,%Gv", "lss %Mp", "btr %Ev,%Gv",
317 "lfs %Mp", "lgs %Mp", "movzx %Gv,%Eb", "movzx %Gv,%Ew",
318 0, 0, "%g7 %Ev,%Ib", "btc %Ev,%Gv",
319 "bsf %Gv,%Ev", "bsr %Gv,%Ev", "movsx %Gv,%Eb", "movsx %Gv,%Ew",
320 /* c */
321 "xadd %Eb,%Gb", "xadd %Ev,%Gv", 0, 0,
322 0, 0, 0, 0,
323 "bswap eax", "bswap ecx", "bswap edx", "bswap ebx",
324 "bswap esp", "bswap ebp", "bswap esi", "bswap edi",
325 /* d */
326 0, 0, 0, 0, 0, 0, 0, 0,
327 0, 0, 0, 0, 0, 0, 0, 0,
328 /* e */
329 0, 0, 0, 0, 0, 0, 0, 0,
330 0, 0, 0, 0, 0, 0, 0, 0,
331 /* f */
332 0, 0, 0, 0, 0, 0, 0, 0,
333 0, 0, 0, 0, 0, 0, 0, 0,
334 };
335
336 static char const *groups[][8] = { /* group 0 is group 3 for %Ev set */
337 /* 0 */
338 { "add", "or", "adc", "sbb",
339 "and", "sub", "xor", "cmp" },
340 /* 1 */
341 { "rol", "ror", "rcl", "rcr",
342 "shl", "shr", "shl", "sar" },
343 /* 2 */ /* v v*/
344 { "test %Eq,%Iq", "test %Eq,%Iq", "not %Ec", "neg %Ec",
345 "mul %Ec", "imul %Ec", "div %Ec", "idiv %Ec" },
346 /* 3 */
347 { "inc %Eb", "dec %Eb", 0, 0,
348 0, 0, 0, "callback %Iw" },
349 /* 4 */
350 { "inc %Ev", "dec %Ev", "call %Kn%Ev", "call %Kf%Ep",
351 "jmp %Kn%Ev", "jmp %Kf%Ep", "push %Ev", 0 },
352 /* 5 */
353 { "sldt %Ew", "str %Ew", "lldt %Ew", "ltr %Ew",
354 "verr %Ew", "verw %Ew", 0, 0 },
355 /* 6 */
356 { "sgdt %Ms", "sidt %Ms", "lgdt %Ms", "lidt %Ms",
357 "smsw %Ew", 0, "lmsw %Ew", "invlpg" },
358 /* 7 */
359 { 0, 0, 0, 0,
360 "bt", "bts", "btr", "btc" }
361 };
362
363 /* zero here means invalid. If first entry starts with '*', use st(i) */
364 /* no assumed %EFs here. Indexed by RM(modrm()) */
365 static char const *f0[] = { 0, 0, 0, 0, 0, 0, 0, 0};
366 static char const *fop_8[] = { "*fld st,%GF" };
367 static char const *fop_9[] = { "*fxch st,%GF" };
368 static char const *fop_10[] = { "fnop", 0, 0, 0, 0, 0, 0, 0 };
369 static char const *fop_11[] = { "*fst st,%GF" };
370 static char const *fop_12[] = { "fchs", "fabs", 0, 0, "ftst", "fxam", 0, 0 };
371 static char const *fop_13[] = { "fld1", "fldl2t", "fldl2e", "fldpi",
372 "fldlg2", "fldln2", "fldz", 0 };
373 static char const *fop_14[] = { "f2xm1", "fyl2x", "fptan", "fpatan",
374 "fxtract", "fprem1", "fdecstp", "fincstp" };
375 static char const *fop_15[] = { "fprem", "fyl2xp1", "fsqrt", "fsincos",
376 "frndint", "fscale", "fsin", "fcos" };
377 static char const *fop_21[] = { 0, "fucompp", 0, 0, 0, 0, 0, 0 };
378 static char const *fop_28[] = { "[fneni]", "[fndis]", "fclex", "finit", "[fnsetpm]", "[frstpm]", 0, 0 };
379 static char const *fop_32[] = { "*fadd %GF,st" };
380 static char const *fop_33[] = { "*fmul %GF,st" };
381 static char const *fop_34[] = { "*fcom %GF,st" };
382 static char const *fop_35[] = { "*fcomp %GF,st" };
383 static char const *fop_36[] = { "*fsubr %GF,st" };
384 static char const *fop_37[] = { "*fsub %GF,st" };
385 static char const *fop_38[] = { "*fdivr %GF,st" };
386 static char const *fop_39[] = { "*fdiv %GF,st" };
387 static char const *fop_40[] = { "*ffree %GF" };
388 static char const *fop_41[] = { "*fxch %GF" };
389 static char const *fop_42[] = { "*fst %GF" };
390 static char const *fop_43[] = { "*fstp %GF" };
391 static char const *fop_44[] = { "*fucom %GF" };
392 static char const *fop_45[] = { "*fucomp %GF" };
393 static char const *fop_48[] = { "*faddp %GF,st" };
394 static char const *fop_49[] = { "*fmulp %GF,st" };
395 static char const *fop_50[] = { "*fcomp %GF,st" };
396 static char const *fop_51[] = { 0, "fcompp", 0, 0, 0, 0, 0, 0 };
397 static char const *fop_52[] = { "*fsubrp %GF,st" };
398 static char const *fop_53[] = { "*fsubp %GF,st" };
399 static char const *fop_54[] = { "*fdivrp %GF,st" };
400 static char const *fop_55[] = { "*fdivp %GF,st" };
401 static char const *fop_56[] = { "*ffreep %GF" };
402 static char const *fop_60[] = { "fstsw ax", 0, 0, 0, 0, 0, 0, 0 };
403
404 static char const **fspecial[] = { /* 0=use st(i), 1=undefined 0 in fop_* means undefined */
405 0, 0, 0, 0, 0, 0, 0, 0,
406 fop_8, fop_9, fop_10, fop_11, fop_12, fop_13, fop_14, fop_15,
407 f0, f0, f0, f0, f0, fop_21, f0, f0,
408 f0, f0, f0, f0, fop_28, f0, f0, f0,
409 fop_32, fop_33, fop_34, fop_35, fop_36, fop_37, fop_38, fop_39,
410 fop_40, fop_41, fop_42, fop_43, fop_44, fop_45, f0, f0,
411 fop_48, fop_49, fop_50, fop_51, fop_52, fop_53, fop_54, fop_55,
412 fop_56, f0, f0, f0, fop_60, f0, f0, f0,
413 };
414
415 static const char *floatops[] = { /* assumed " %EF" at end of each. mod != 3 only */
416 /*00*/ "fadd", "fmul", "fcom", "fcomp",
417 "fsub", "fsubr", "fdiv", "fdivr",
418 /*08*/ "fld", 0, "fst", "fstp",
419 "fldenv", "fldcw", "fstenv", "fstcw",
420 /*16*/ "fiadd", "fimul", "ficomw", "ficompw",
421 "fisub", "fisubr", "fidiv", "fidivr",
422 /*24*/ "fild", 0, "fist", "fistp",
423 "frstor", "fldt", 0, "fstpt",
424 /*32*/ "faddq", "fmulq", "fcomq", "fcompq",
425 "fsubq", "fsubrq", "fdivq", "fdivrq",
426 /*40*/ "fldq", 0, "fstq", "fstpq",
427 0, 0, "fsave", "fstsw",
428 /*48*/ "fiaddw", "fimulw", "ficomw", "ficompw",
429 "fisubw", "fisubrw", "fidivw", "fidivr",
430 /*56*/ "fildw", 0, "fistw", "fistpw",
431 "fbldt", "fildq", "fbstpt", "fistpq"
432 };
433
434 static char *addr_to_hex(UINT32 addr, int splitup) {
435 static char buffer[11];
436
437 if (splitup) {
438 if (fp_segment(addr)==0 || fp_offset(addr)==0xffff) /* 'coz of wraparound */
439 sprintf(buffer, "%04X", (unsigned)fp_offset(addr) );
440 else
441 sprintf(buffer, "%04X:%04X", (unsigned)fp_segment(addr), (unsigned)fp_offset(addr) );
442 } else {
443 #if 0
444 /* Pet outcommented, reducing address size to 4
445 when segment is 0 or 0xffff */
446 if (fp_segment(addr)==0 || fp_segment(addr)==0xffff) /* 'coz of wraparound */
447 sprintf(buffer, "%04X", (unsigned)fp_offset(addr) );
448 else
449 #endif
450
451 sprintf(buffer, "%08X", addr );
452
453 }
454
455 return buffer;
456 }
457
458 static PhysPt getbyte_mac;
459 static PhysPt startPtr;
460
461 static UINT8 getbyte(void) {
462 return mem_readb(getbyte_mac++);
463 }
464
465 /*
466 only one modrm or sib byte per instruction, tho' they need to be
467 returned a few times...
468 */
469
470 static int modrm(void)
471 {
472 if (modrmv == -1)
473 modrmv = getbyte();
474 return modrmv;
475 }
476
477 static int sib(void)
478 {
479 if (sibv == -1)
480 sibv = getbyte();
481 return sibv;
482 }
483
484 /*------------------------------------------------------------------------*/
485
486 static void uprintf(char const *s, ...)
487 {
488 va_list arg_ptr;
489 va_start (arg_ptr, s);
490 vsprintf(ubufp, s, arg_ptr);
491 va_end(arg_ptr);
492 while (*ubufp)
493 ubufp++;
494 }
495
496 static void uputchar(char c)
497 {
498 *ubufp++ = c;
499 *ubufp = 0;
500 }
501
502 /*------------------------------------------------------------------------*/
503
504 static int bytes(char c)
505 {
506 switch (c) {
507 case 'b':
508 return 1;
509 case 'w':
510 return 2;
511 case 'd':
512 return 4;
513 case 'v':
514 if (opsize == 32)
515 return 4;
516 else
517 return 2;
518 }
519 return 0;
520 }
521
522 /*------------------------------------------------------------------------*/
523 static void outhex(char subtype, int extend, int optional, int defsize, int sign)
524 {
525 int n=0, s=0, i;
526 INT32 delta = 0;
527 unsigned char buff[6];
528 char *name;
529 char signchar;
530
531 switch (subtype) {
532 case 'q':
533 if (wordop) {
534 if (opsize==16) {
535 n = 2;
536 } else {
537 n = 4;
538 }
539 } else {
540 n = 1;
541 }
542 break;
543
544 case 'a':
545 break;
546 case 'x':
547 extend = 2;
548 n = 1;
549 break;
550 case 'b':
551 n = 1;
552 break;
553 case 'w':
554 n = 2;
555 break;
556 case 'd':
557 n = 4;
558 break;
559 case 's':
560 n = 6;
561 break;
562 case 'c':
563 case 'v':
564 if (defsize == 32)
565 n = 4;
566 else
567 n = 2;
568 break;
569 case 'p':
570 if (defsize == 32)
571 n = 6;
572 else
573 n = 4;
574 s = 1;
575 break;
576 }
577 for (i=0; i<n; i++)
578 buff[i] = getbyte();
579 for (; i<extend; i++)
580 buff[i] = (buff[i-1] & 0x80) ? 0xff : 0;
581 if (s) {
582 uprintf("%02X%02X:", (unsigned)buff[n-1], (unsigned)buff[n-2]);
583 n -= 2;
584 }
585 switch (n) {
586 case 1:
587 delta = le_int8(buff);
588 break;
589 case 2:
590 delta = le_int16(buff);
591 break;
592 case 4:
593 delta = le_int32(buff);
594 break;
595 }
596 if (extend > n) {
597 if (subtype!='x') {
598 if (delta<0) {
599 delta = -delta;
600 signchar = '-';
601 } else
602 signchar = '+';
603 if (delta || !optional)
604 uprintf("%c%0*lX", (char)signchar, (int)(extend), (long)delta);
605 } else {
606 if (extend==2)
607 delta = (UINT16)delta;
608 uprintf("%0.*lX", (int)(2*extend), (long)delta );
609 }
610 return;
611 }
612 if ((n == 4) && !sign) {
613 name = addr_to_hex(delta, 0);
614 uprintf("%s", name);
615 return;
616 }
617 switch (n) {
618 case 1:
619 if (sign && (char)delta<0) {
620 delta = -delta;
621 signchar = '-';
622 } else
623 signchar = '+';
624 if (sign)
625 uprintf("%c%02lX", (char)signchar, delta & 0xFFL);
626 else
627 uprintf("%02lX", delta & 0xFFL);
628 break;
629
630 case 2:
631 if (sign && delta<0) {
632 signchar = '-';
633 delta = -delta;
634 } else
635 signchar = '+';
636 if (sign)
637 uprintf("%c%04lX", (char)signchar, delta & 0xFFFFL);
638 else
639 uprintf("%04lX", delta & 0xFFFFL);
640 break;
641
642 case 4:
643 if (sign && delta<0) {
644 delta = -delta;
645 signchar = '-';
646 } else
647 signchar = '+';
648 if (sign)
649 uprintf("%c%08lX", (char)signchar, delta & 0xFFFFFFFFL);
650 else
651 uprintf("%08lX", delta & 0xFFFFFFFFL);
652 break;
653 }
654 }
655
656
657 /*------------------------------------------------------------------------*/
658
659 static void reg_name(int regnum, char size)
660 {
661 if (size == 'F') { /* floating point register? */
662 uprintf("st(%d)", regnum);
663 return;
664 }
665 if ((((size == 'c') || (size == 'v')) && (opsize == 32)) || (size == 'd'))
666 uputchar('e');
667 if ((size=='q' || size == 'b' || size=='c') && !wordop) {
668 uputchar("acdbacdb"[regnum]);
669 uputchar("llllhhhh"[regnum]);
670 } else {
671 uputchar("acdbsbsd"[regnum]);
672 uputchar("xxxxppii"[regnum]);
673 }
674 }
675
676
677 /*------------------------------------------------------------------------*/
678
679 static void ua_str(char const *str);
680
681 static void do_sib(int m)
682 {
683 int s, i, b;
684
685 s = SCALE(sib());
686 i = INDEX(sib());
687 b = BASE(sib());
688 switch (b) { /* pick base */
689 case 0: ua_str("%p:[eax"); break;
690 case 1: ua_str("%p:[ecx"); break;
691 case 2: ua_str("%p:[edx"); break;
692 case 3: ua_str("%p:[ebx"); break;
693 case 4: ua_str("%p:[esp"); break;
694 case 5:
695 if (m == 0) {
696 ua_str("%p:[");
697 outhex('d', 4, 0, addrsize, 0);
698 } else {
699 ua_str("%p:[ebp");
700 }
701 break;
702 case 6: ua_str("%p:[esi"); break;
703 case 7: ua_str("%p:[edi"); break;
704 }
705 switch (i) { /* and index */
706 case 0: uprintf("+eax"); break;
707 case 1: uprintf("+ecx"); break;
708 case 2: uprintf("+edx"); break;
709 case 3: uprintf("+ebx"); break;
710 case 4: break;
711 case 5: uprintf("+ebp"); break;
712 case 6: uprintf("+esi"); break;
713 case 7: uprintf("+edi"); break;
714 }
715 if (i != 4) {
716 switch (s) { /* and scale */
717 case 0: /*uprintf("");*/ break;
718 case 1: uprintf("*2"); break;
719 case 2: uprintf("*4"); break;
720 case 3: uprintf("*8"); break;
721 }
722 }
723 }
724
725
726
727 /*------------------------------------------------------------------------*/
728 static void do_modrm(char subtype)
729 {
730 int mod = MOD(modrm());
731 int rm = RM(modrm());
732 int extend = (addrsize == 32) ? 4 : 2;
733
734 if (mod == 3) { /* specifies two registers */
735 reg_name(rm, subtype);
736 return;
737 }
738 if (must_do_size) {
739 if (wordop) {
740 if (addrsize==32 || opsize==32) { /* then must specify size */
741 ua_str("dword ");
742 } else {
743 ua_str("word ");
744 }
745 } else {
746 ua_str("byte ");
747 }
748 }
749 if ((mod == 0) && (rm == 5) && (addrsize == 32)) {/* mem operand with 32 bit ofs */
750 ua_str("%p:[");
751 outhex('d', extend, 0, addrsize, 0);
752 uputchar(']');
753 return;
754 }
755 if ((mod == 0) && (rm == 6) && (addrsize == 16)) { /* 16 bit dsplcmnt */
756 ua_str("%p:[");
757 outhex('w', extend, 0, addrsize, 0);
758 uputchar(']');
759 return;
760 }
761 if ((addrsize != 32) || (rm != 4))
762 ua_str("%p:[");
763 if (addrsize == 16) {
764 switch (rm) {
765 case 0: uprintf("bx+si"); break;
766 case 1: uprintf("bx+di"); break;
767 case 2: uprintf("bp+si"); break;
768 case 3: uprintf("bp+di"); break;
769 case 4: uprintf("si"); break;
770 case 5: uprintf("di"); break;
771 case 6: uprintf("bp"); break;
772 case 7: uprintf("bx"); break;
773 }
774 } else {
775 switch (rm) {
776 case 0: uprintf("eax"); break;
777 case 1: uprintf("ecx"); break;
778 case 2: uprintf("edx"); break;
779 case 3: uprintf("ebx"); break;
780 case 4: do_sib(mod); break;
781 case 5: uprintf("ebp"); break;
782 case 6: uprintf("esi"); break;
783 case 7: uprintf("edi"); break;
784 }
785 }
786 switch (mod) {
787 case 1:
788 outhex('b', extend, 1, addrsize, 0);
789 break;
790 case 2:
791 outhex('v', extend, 1, addrsize, 1);
792 break;
793 }
794 uputchar(']');
795 }
796
797
798
799 /*------------------------------------------------------------------------*/
800 static void floating_point(int e1)
801 {
802 int esc = e1*8 + REG(modrm());
803
804 if ((MOD(modrm()) == 3)&&fspecial[esc]) {
805 if (fspecial[esc][0]) {
806 if (fspecial[esc][0][0] == '*') {
807 ua_str(fspecial[esc][0]+1);
808 } else {
809 ua_str(fspecial[esc][RM(modrm())]);
810 }
811 } else {
812 ua_str(floatops[esc]);
813 ua_str(" %EF");
814 }
815 } else {
816 ua_str(floatops[esc]);
817 ua_str(" %EF");
818 }
819 }
820
821
822 /*------------------------------------------------------------------------*/
823 /* Main table driver */
824
825 #define INSTRUCTION_SIZE (int)getbyte_mac - (int)startPtr
826
827 static void percent(char type, char subtype)
828 {
829 INT32 vofs = 0;
830 char *name=NULL;
831 int extend = (addrsize == 32) ? 4 : 2;
832 UINT8 c;
833
834 switch (type) {
835 case 'A': /* direct address */
836 outhex(subtype, extend, 0, addrsize, 0);
837 break;
838
839 case 'C': /* reg(r/m) picks control reg */
840 uprintf("CR%d", REG(modrm()));
841 must_do_size = 0;
842 break;
843
844 case 'D': /* reg(r/m) picks debug reg */
845 uprintf("DR%d", REG(modrm()));
846 must_do_size = 0;
847 break;
848
849 case 'E': /* r/m picks operand */
850 do_modrm(subtype);
851 break;
852
853 case 'G': /* reg(r/m) picks register */
854 if (subtype == 'F') /* 80*87 operand? */
855 reg_name(RM(modrm()), subtype);
856 else
857 reg_name(REG(modrm()), subtype);
858 must_do_size = 0;
859 break;
860
861 case 'I': /* immed data */
862 outhex(subtype, 0, 0, opsize, 0);
863 break;
864
865 case 'J': /* relative IP offset */
866 switch (bytes(subtype)) { /* sizeof offset value */
867 case 1:
868 vofs = (INT8)getbyte();
869 name = addr_to_hex(vofs+instruction_offset+INSTRUCTION_SIZE,0);
870 break;
871 case 2:
872 vofs = getbyte();
873 vofs += getbyte()<<8;
874 vofs = (INT16)vofs;
875 name = addr_to_hex(vofs+instruction_offset+INSTRUCTION_SIZE,0);
876 break;
877 /* i386 */
878 case 4:
879 vofs = (UINT32)getbyte(); /* yuk! */
880 vofs |= (UINT32)getbyte() << 8;
881 vofs |= (UINT32)getbyte() << 16;
882 vofs |= (UINT32)getbyte() << 24;
883 name = addr_to_hex(vofs+instruction_offset+INSTRUCTION_SIZE,(addrsize == 32)?0:1);
884 break;
885 }
886 if (vofs<0)
887 uprintf("%s ($-%x)", name, -vofs);
888 else
889 uprintf("%s ($+%x)", name, vofs);
890 break;
891
892 case 'K':
893 switch (subtype) {
894 case 'f':
895 ua_str("far ");
896 break;
897 case 'n':
898 ua_str("near ");
899 break;
900 case 's':
901 ua_str("short ");
902 break;
903 }
904 break;
905
906 case 'M': /* r/m picks memory */
907 do_modrm(subtype);
908 break;
909
910 case 'O': /* offset only */
911 ua_str("%p:[");
912 outhex(subtype, extend, 0, addrsize, 0);
913 uputchar(']');
914 break;
915
916 case 'P': /* prefix byte (rh) */
917 ua_str("%p:");
918 break;
919
920 case 'R': /* mod(r/m) picks register */
921 reg_name(RM(modrm()), subtype); /* rh */
922 must_do_size = 0;
923 break;
924
925 case 'S': /* reg(r/m) picks segment reg */
926 uputchar("ecsdfg"[REG(modrm())]);
927 uputchar('s');
928 must_do_size = 0;
929 break;
930
931 case 'T': /* reg(r/m) picks T reg */
932 uprintf("tr%d", REG(modrm()));
933 must_do_size = 0;
934 break;
935
936 case 'X': /* ds:si type operator */
937 uprintf("ds:[");
938 if (addrsize == 32)
939 uputchar('e');
940 uprintf("si]");
941 break;
942
943 case 'Y': /* es:di type operator */
944 uprintf("es:[");
945 if (addrsize == 32)
946 uputchar('e');
947 uprintf("di]");
948 break;
949
950 case '2': /* old [pop cs]! now indexes */
951 ua_str(second[getbyte()]); /* instructions in 386/486 */
952 break;
953
954 case 'g': /* modrm group `subtype' (0--7) */
955 ua_str(groups[subtype-'0'][REG(modrm())]);
956 break;
957
958 case 'd': /* sizeof operand==dword? */
959 if (opsize == 32)
960 uputchar('d');
961 uputchar(subtype);
962 break;
963
964 case 'w': /* insert explicit size specifier */
965 if (opsize == 32)
966 uputchar('d');
967 else
968 uputchar('w');
969 uputchar(subtype);
970 break;
971
972 case 'e': /* extended reg name */
973 if (opsize == 32) {
974 if (subtype == 'w')
975 uputchar('d');
976 else {
977 uputchar('e');
978 uputchar(subtype);
979 }
980 } else
981 uputchar(subtype);
982 break;
983
984 case 'f': /* '87 opcode */
985 floating_point(subtype-'0');
986 break;
987
988 case 'j':
989 if (addrsize==32 || opsize==32) /* both of them?! */
990 uputchar('e');
991 break;
992
993 case 'p': /* prefix byte */
994 switch (subtype) {
995 case 'c':
996 case 'd':
997 case 'e':
998 case 'f':
999 case 'g':
1000 case 's':
1001 prefix = subtype;
1002 c = getbyte();
1003 wordop = c & 1;
1004 ua_str((*opmap1)[c]);
1005 break;
1006 case ':':
1007 if (prefix)
1008 uprintf("%cs:", prefix);
1009 break;
1010 case ' ':
1011 c = getbyte();
1012 wordop = c & 1;
1013 ua_str((*opmap1)[c]);
1014 break;
1015 }
1016 break;
1017
1018 case 's': /* size override */
1019 switch (subtype) {
1020 case 'a':
1021 addrsize = 48 - addrsize;
1022 c = getbyte();
1023 wordop = c & 1;
1024 ua_str((*opmap1)[c]);
1025 /* ua_str(opmap1[getbyte()]); */
1026 break;
1027 case 'o':
1028 opsize = 48 - opsize;
1029 c = getbyte();
1030 wordop = c & 1;
1031 ua_str((*opmap1)[c]);
1032 /* ua_str(opmap1[getbyte()]); */
1033 break;
1034 }
1035 break;
1036 }
1037 }
1038
1039
1040 static void ua_str(char const *str)
1041 {
1042 char c;
1043
1044 if (str == 0) {
1045 invalid_opcode = 1;
1046 uprintf("?");
1047 return;
1048 }
1049
1050 if (strpbrk(str, "CDFGRST")) /* specifiers for registers=>no size 2b specified */
1051 must_do_size = 0;
1052
1053 while ((c = *str++) != 0) {
1054 if (c == ' ' && first_space)
1055 {
1056 first_space = 0;
1057 do
1058 {
1059 uputchar(' ');
1060 } while ( (int)(ubufp - ubufs) < 5 );
1061 }
1062 else
1063 if (c == '%') {
1064 c = *str++;
1065 percent(c, *str++);
1066 } else {
1067 uputchar(c);
1068 }
1069 }
1070 }
1071
1072
1073 Bitu DasmI386(char* buffer, PhysPt pc, Bitu cur_ip, bool bit32)
1074 {
1075 Bitu c;
1076
1077
1078 instruction_offset = cur_ip;
1079 /* input buffer */
1080 startPtr = pc;
1081 getbyte_mac = pc;
1082
1083 /* output buffer */
1084 ubufs = buffer;
1085 ubufp = buffer;
1086 first_space = 1;
1087
1088 addr32bit=1;
1089
1090 prefix = 0;
1091 modrmv = sibv = -1; /* set modrm and sib flags */
1092 if (bit32) opsize = addrsize = 32;
1093 else opsize = addrsize = 16;
1094 c = getbyte();
1095 wordop = c & 1;
1096 must_do_size = 1;
1097 invalid_opcode = 0;
1098 opmap1=&op386map1;
1099 ua_str(op386map1[c]);
1100
1101 if (invalid_opcode) {
1102 /* restart output buffer */
1103 ubufp = buffer;
1104 /* invalid instruction, use db xx */
1105 uprintf("db %02X", (unsigned)c);
1106 return 1;
1107 }
1108
1109 return getbyte_mac-pc;
1110 }
1111
1112 int DasmLastOperandSize()
1113 {
1114 return opsize;
1115 };
1116
1117
1118 #endif
1119