ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/mon/src/mon_x86.cpp
Revision: 1.5
Committed: 2000-04-24T13:10:22Z (24 years, 7 months ago) by cebix
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
- added binary dump command
- converted to automake

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * mon_x86.cpp - 80x86 disassembler
3     *
4 cebix 1.5 * mon (C) 1997-2000 Christian Bauer, Marc Hellwig
5 cebix 1.2 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 cebix 1.1 */
20    
21 cebix 1.2 /*
22     * TODO
23     * - tons of missing instructions
24     */
25    
26     #include "sysdeps.h"
27 cebix 1.1
28     #include "mon.h"
29     #include "mon_x86.h"
30    
31    
32     // Memory access functions
33     static uint16 read_uint16(uint8 *&m)
34     {
35     uint16 val = (m[1] << 8) | m[0];
36     m += 2;
37     return val;
38     }
39    
40     static int16 read_int16(uint8 *&m)
41     {
42     int16 val = (m[1] << 8) | m[0];
43     m += 2;
44     return val;
45     }
46    
47     static uint32 read_uint32(uint8 *&m)
48     {
49     uint32 val = (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
50     m += 4;
51     return val;
52     }
53    
54    
55     // String constants
56     static const char *gp_reg[2][8] = {
57 cebix 1.3 {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"},
58     {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"}
59 cebix 1.1 };
60    
61    
62     // Prototypes
63     static void disass0f(char *&mnem, char *s, uint8 *&m, bool &mode32);
64    
65    
66     /*
67     * Disassemble one instruction, return number of bytes
68     */
69    
70     int disass_x86(FILE *f, uint32 adr, uint8 *m)
71     {
72     uint8 *orig_m = m;
73     char *mnem;
74     char s[256] = "";
75     bool mode32 = true;
76     int32 disp;
77    
78     // Decode primary opcode
79     next_opcode:
80     uint8 opcode = *m++;
81     switch (opcode) {
82     case 0x04:
83     mnem = "add";
84     arith_alimm:
85     sprintf(s, "al,$%02x", *m++);
86     break;
87     case 0x05:
88     mnem = "add";
89     arith_aximm:
90     if (mode32)
91 cebix 1.3 sprintf(s, "eax,$%08x", read_uint32(m));
92 cebix 1.1 else
93     sprintf(s, "ax,$%04x", read_uint16(m));
94     break;
95    
96     case 0x06: mnem = "push\tes"; break;
97     case 0x07: mnem = "pop\tes"; break;
98     case 0x0c: mnem = "or"; goto arith_alimm;
99     case 0x0d: mnem = "or"; goto arith_aximm;
100     case 0x0e: mnem = "push\tcs"; break;
101     case 0x0f: disass0f(mnem, s, m, mode32); break;
102     case 0x14: mnem = "adc"; goto arith_alimm;
103     case 0x15: mnem = "adc"; goto arith_aximm;
104     case 0x16: mnem = "push\tss"; break;
105     case 0x17: mnem = "pop\tss"; break;
106     case 0x1c: mnem = "sbb"; goto arith_alimm;
107     case 0x1d: mnem = "sbb"; goto arith_aximm;
108     case 0x1e: mnem = "push\tds"; break;
109     case 0x1f: mnem = "pop\tds"; break;
110     case 0x24: mnem = "and"; goto arith_alimm;
111     case 0x25: mnem = "and"; goto arith_aximm;
112     case 0x26: mnem = "(seg es prefix)"; break;
113     case 0x27: mnem = "daa"; break;
114     case 0x2c: mnem = "sub"; goto arith_alimm;
115     case 0x2d: mnem = "sub"; goto arith_aximm;
116     case 0x2e: mnem = "(seg cs prefix)"; break;
117     case 0x2f: mnem = "das"; break;
118     case 0x34: mnem = "xor"; goto arith_alimm;
119     case 0x35: mnem = "xor"; goto arith_aximm;
120     case 0x36: mnem = "(seg ss prefix)"; break;
121     case 0x37: mnem = "aaa"; break;
122     case 0x3c: mnem = "cmp"; goto arith_alimm;
123     case 0x3d: mnem = "cmp"; goto arith_aximm;
124     case 0x3e: mnem = "(seg ds prefix)"; break;
125     case 0x3f: mnem = "aas"; break;
126    
127     case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
128     mnem = "inc"; sprintf(s, "%s", gp_reg[mode32][opcode & 7]); break;
129     case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
130     mnem = "dec"; sprintf(s, "%s", gp_reg[mode32][opcode & 7]); break;
131     case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
132     mnem = "push"; sprintf(s, "%s", gp_reg[mode32][opcode & 7]); break;
133     case 0x58: case 0x59: case 0x5a: case 0x5b: case 0x5c: case 0x5d: case 0x5e: case 0x5f:
134     mnem = "pop"; sprintf(s, "%s", gp_reg[mode32][opcode & 7]); break;
135    
136 cebix 1.4 case 0x60: mnem = (char *)(mode32 ? "pushad" : "pusha"); break;
137     case 0x61: mnem = (char *)(mode32 ? "popad" : "popa"); break;
138 cebix 1.1 case 0x64: mnem = "(seg fs prefix)"; break;
139     case 0x65: mnem = "(seg gs prefix)"; break;
140     case 0x66: mode32 = !mode32; goto next_opcode; //!! error checking
141     case 0x67: mnem = "(adr size prefix)"; break;
142    
143     case 0x70:
144     mnem = "jo";
145     jcc_bdisp:
146     disp = (int8)*m++;
147 cebix 1.3 sprintf(s, "$%08x", disp + adr + (m - orig_m));
148 cebix 1.1 break;
149     case 0x71: mnem = "jno"; goto jcc_bdisp;
150     case 0x72: mnem = "jc"; goto jcc_bdisp;
151     case 0x73: mnem = "jnc"; goto jcc_bdisp;
152     case 0x74: mnem = "je"; goto jcc_bdisp;
153     case 0x75: mnem = "jne"; goto jcc_bdisp;
154     case 0x76: mnem = "jbe"; goto jcc_bdisp;
155     case 0x77: mnem = "jnbe"; goto jcc_bdisp;
156     case 0x78: mnem = "js"; goto jcc_bdisp;
157     case 0x79: mnem = "jns"; goto jcc_bdisp;
158     case 0x7a: mnem = "jpe"; goto jcc_bdisp;
159     case 0x7b: mnem = "jpo"; goto jcc_bdisp;
160     case 0x7c: mnem = "jl"; goto jcc_bdisp;
161     case 0x7d: mnem = "jge"; goto jcc_bdisp;
162     case 0x7e: mnem = "jle"; goto jcc_bdisp;
163     case 0x7f: mnem = "jg"; goto jcc_bdisp;
164    
165     case 0x90: mnem = "nop"; break;
166     case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
167     mnem = "xchg"; sprintf(s, "%s,%s", mode32 ? "eax" : "ax", gp_reg[mode32][opcode & 7]); break;
168    
169 cebix 1.4 case 0x98: mnem = (char *)(mode32 ? "cwde" : "cbw"); break;
170     case 0x99: mnem = (char *)(mode32 ? "cdq" : "cwd"); break;
171 cebix 1.1 case 0x9b: mnem = "wait"; break;
172 cebix 1.4 case 0x9c: mnem = (char *)(mode32 ? "pushfd" : "pushf"); break;
173     case 0x9d: mnem = (char *)(mode32 ? "popfd" : "popf"); break;
174 cebix 1.1 case 0x9e: mnem = "sahf"; break;
175     case 0x9f: mnem = "lahf"; break;
176 cebix 1.3 case 0xa0: mnem = "mov"; sprintf(s, "al,byte ptr $%08x", read_uint32(m)); break;
177     case 0xa1: mnem = "mov"; sprintf(s, mode32 ? "eax,dword ptr $%08x" : "ax,word ptr $%08x", read_uint32(m)); break;
178     case 0xa2: mnem = "mov"; sprintf(s, "byte ptr $%08x,al", read_uint32(m)); break;
179     case 0xa3: mnem = "mov"; sprintf(s, mode32 ? "dword ptr $%08x,eax" : "word ptr $%08x,ax", read_uint32(m)); break;
180 cebix 1.1 case 0xa4: mnem = "movsb"; break;
181 cebix 1.4 case 0xa5: mnem = (char *)(mode32 ? "movsd" : "movsw"); break;
182 cebix 1.1 case 0xa6: mnem = "cmpsb"; break;
183 cebix 1.4 case 0xa7: mnem = (char *)(mode32 ? "cmpsd" : "cmpsw"); break;
184 cebix 1.1 case 0xa8: mnem = "test"; goto arith_alimm;
185     case 0xa9: mnem = "test"; goto arith_aximm;
186     case 0xaa: mnem = "stosb"; break;
187 cebix 1.4 case 0xab: mnem = (char *)(mode32 ? "stosd" : "stosw"); break;
188 cebix 1.1 case 0xac: mnem = "lodsb"; break;
189 cebix 1.4 case 0xad: mnem = (char *)(mode32 ? "lodsd" : "lodsw"); break;
190 cebix 1.1 case 0xae: mnem = "scasb"; break;
191 cebix 1.4 case 0xaf: mnem = (char *)(mode32 ? "scasd" : "scasw"); break;
192 cebix 1.1 case 0xb0: mnem = "mov"; sprintf(s, "al,$%02x", *m++); break;
193     case 0xb1: mnem = "mov"; sprintf(s, "cl,$%02x", *m++); break;
194     case 0xb2: mnem = "mov"; sprintf(s, "dl,$%02x", *m++); break;
195     case 0xb3: mnem = "mov"; sprintf(s, "bl,$%02x", *m++); break;
196     case 0xb4: mnem = "mov"; sprintf(s, "ah,$%02x", *m++); break;
197     case 0xb5: mnem = "mov"; sprintf(s, "ch,$%02x", *m++); break;
198     case 0xb6: mnem = "mov"; sprintf(s, "dh,$%02x", *m++); break;
199     case 0xb7: mnem = "mov"; sprintf(s, "bh,$%02x", *m++); break;
200    
201     case 0xb8: case 0xb9: case 0xba: case 0xbb: case 0xbc: case 0xbd: case 0xbe: case 0xbf:
202     mnem = "mov";
203     if (mode32)
204 cebix 1.3 sprintf(s, "%s,$%08x", gp_reg[true][opcode & 7], read_uint32(m));
205 cebix 1.1 else
206     sprintf(s, "%s,$%04x", gp_reg[false][opcode & 7], read_uint16(m));
207     break;
208    
209     case 0xc2: mnem = "ret"; sprintf(s, "near $%04x", read_uint16(m)); break;
210     case 0xc3: mnem = "ret\tnear"; break;
211     case 0xc9: mnem = "leave"; break;
212     case 0xca: mnem = "ret"; sprintf(s, "far $%04x", read_uint16(m)); break;
213     case 0xcb: mnem = "ret\tfar"; break;
214     case 0xcc: mnem = "int3"; break;
215     case 0xcd: mnem = "int"; sprintf(s, "$%02x", *m++); break;
216     case 0xce: mnem = "into"; break;
217     case 0xcf: mnem = "iret"; break;
218     case 0xd4: mnem = "aam"; m++; break;
219     case 0xd5: mnem = "aad"; m++; break;
220     case 0xd7: mnem = "xlat"; break;
221     case 0xe0: mnem = "loopnz"; goto jcc_bdisp;
222     case 0xe1: mnem = "loopz"; goto jcc_bdisp;
223     case 0xe2: mnem = "loop"; goto jcc_bdisp;
224 cebix 1.4 case 0xe3: mnem = (char *)(mode32 ? "jecxz" : "jcxz"); goto jcc_bdisp;
225 cebix 1.1 case 0xe4: mnem = "in"; sprintf(s, "al,$%02x", *m++); break;
226     case 0xe5: mnem = "in"; sprintf(s, "%s,$%02x", mode32 ? "eax" : "ax", *m++); break;
227     case 0xe6: mnem = "out"; sprintf(s, "$%02x,al", *m++); break;
228     case 0xe7: mnem = "out"; sprintf(s, "$%02x,%s", *m++, mode32 ? "eax" : "ax"); break;
229    
230     case 0xe8:
231     mnem = "call";
232     jump_near:
233     disp = read_uint32(m);
234 cebix 1.3 sprintf(s, "near $%08x", disp + adr + (m - orig_m));
235 cebix 1.1 break;
236     case 0xe9: mnem = "jmp"; goto jump_near;
237     case 0xeb:
238     mnem = "jmp";
239     disp = (int8)*m++;
240 cebix 1.3 sprintf(s, "near $%08x", disp + adr + (m - orig_m));
241 cebix 1.1 break;
242    
243     case 0xec: mnem = "in\tal,dx"; break;
244 cebix 1.4 case 0xed: mnem = (char *)(mode32 ? "in\teax,dx" : "in\tax,dx"); break;
245 cebix 1.1 case 0xee: mnem = "out\tdx,al"; break;
246 cebix 1.4 case 0xef: mnem = (char *)(mode32 ? "out\tdx,eax" : "out\tdx,ax"); break;
247 cebix 1.1 case 0xf0: mnem = "lock"; break;
248     case 0xf2: mnem = "repne"; break;
249     case 0xf3: mnem = "rep"; break;
250     case 0xf4: mnem = "hlt"; break;
251     case 0xf5: mnem = "cmc"; break;
252     case 0xf8: mnem = "clc"; break;
253     case 0xf9: mnem = "stc"; break;
254     case 0xfa: mnem = "cli"; break;
255     case 0xfb: mnem = "sti"; break;
256     case 0xfc: mnem = "cld"; break;
257     case 0xfd: mnem = "std"; break;
258    
259     default:
260     mnem = "?";
261     break;
262     }
263    
264     // Print opcodes and mnemonics
265     int size = m - orig_m;
266     for (int i=0; i<7; i++)
267     if (i < size)
268     fprintf(f, "%02x ", orig_m[i]);
269     else
270     fprintf(f, " ");
271     fprintf(f, "%s\t%s\n", mnem, s);
272     return size;
273     }
274    
275    
276     /*
277     * Disassemble instruction with primary opcode 0x0f
278     */
279    
280     static void disass0f(char *&mnem, char *s, uint8 *&m, bool &mode32)
281     {
282     // Decode secondary opcode
283     uint8 opcode = *m++;
284     switch (opcode) {
285     case 0x06: mnem = "clts"; break;
286     case 0x08: mnem = "invd"; break;
287     case 0x09: mnem = "wbinvd"; break;
288     case 0x0b: mnem = "ud2"; break;
289     case 0x30: mnem = "wrmsr"; break;
290     case 0x31: mnem = "rdtsc"; break;
291     case 0x32: mnem = "rdmsr"; break;
292     case 0x33: mnem = "rdpmc"; break;
293     case 0xa0: mnem = "push\tfs"; break;
294     case 0xa1: mnem = "pop\tfs"; break;
295     case 0xa2: mnem = "cpuid"; break;
296     case 0xa8: mnem = "push\tgs"; break;
297     case 0xa9: mnem = "pop\tgs"; break;
298     case 0xaa: mnem = "rsm"; break;
299    
300     case 0xc8: case 0xc9: case 0xca: case 0xcb: case 0xcc: case 0xcd: case 0xce: case 0xcf:
301     mnem = "bswap"; sprintf(s, "%s", gp_reg[true][opcode & 7]); break;
302    
303     default:
304     mnem = "?";
305     break;
306     }
307     }