1 /* udis86 - libudis86/syn-intel.c
3 * Copyright (c) 2002-2009 Vivek Thampi
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 /* -----------------------------------------------------------------------------
33 * opr_cast() - Prints an operand cast.
34 * -----------------------------------------------------------------------------
37 opr_cast(struct ud* u, struct ud_operand* op)
40 case 8: mkasm(u, "byte " ); break;
41 case 16: mkasm(u, "word " ); break;
42 case 32: mkasm(u, "dword "); break;
43 case 64: mkasm(u, "qword "); break;
44 case 80: mkasm(u, "tword "); break;
53 /* -----------------------------------------------------------------------------
54 * gen_operand() - Generates assembly output for each operand.
55 * -----------------------------------------------------------------------------
57 static void gen_operand(struct ud* u, struct ud_operand* op, int syn_cast)
61 mkasm(u, ud_reg_tab[op->base - UD_R_AL]);
74 mkasm(u, "%s:", ud_reg_tab[u->pfx_seg - UD_R_AL]);
77 mkasm(u, "%s", ud_reg_tab[op->base - UD_R_AL]);
84 mkasm(u, "%s", ud_reg_tab[op->index - UD_R_AL]);
89 mkasm(u, "*%d", op->scale);
91 if (op->offset == 8) {
92 if (op->lval.sbyte < 0)
93 mkasm(u, "-0x%x", -op->lval.sbyte);
94 else mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.sbyte);
96 else if (op->offset == 16)
97 mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.uword);
98 else if (op->offset == 32) {
99 if (u->adr_mode == 64) {
100 if (op->lval.sdword < 0)
101 mkasm(u, "-0x%x", -op->lval.sdword);
102 else mkasm(u, "%s0x%x", (op_f) ? "+" : "", op->lval.sdword);
104 else mkasm(u, "%s0x%lx", (op_f) ? "+" : "", op->lval.udword);
106 else if (op->offset == 64)
107 mkasm(u, "%s0x" FMT64 "x", (op_f) ? "+" : "", op->lval.uqword);
114 if (syn_cast) opr_cast(u, op);
116 case 8: mkasm(u, "0x%x", op->lval.ubyte); break;
117 case 16: mkasm(u, "0x%x", op->lval.uword); break;
118 case 32: mkasm(u, "0x%lx", op->lval.udword); break;
119 case 64: mkasm(u, "0x" FMT64 "x", op->lval.uqword); break;
125 if (syn_cast) opr_cast(u, op);
128 mkasm(u, "0x" FMT64 "x", u->pc + op->lval.sbyte);
131 mkasm(u, "0x" FMT64 "x", u->pc + op->lval.sword);
134 mkasm(u, "0x" FMT64 "x", u->pc + op->lval.sdword);
143 mkasm(u, "word 0x%x:0x%x", op->lval.ptr.seg,
144 op->lval.ptr.off & 0xFFFF);
147 mkasm(u, "dword 0x%x:0x%lx", op->lval.ptr.seg,
154 if (syn_cast) opr_cast(u, op);
155 mkasm(u, "%d", op->lval.udword);
162 /* =============================================================================
163 * translates to intel syntax
164 * =============================================================================
166 extern void ud_translate_intel(struct ud* u)
170 /* check if P_OSO prefix is used */
171 if (! P_OSO(u->itab_entry->prefix) && u->pfx_opr) {
172 switch (u->dis_mode) {
183 /* check if P_ASO prefix was used */
184 if (! P_ASO(u->itab_entry->prefix) && u->pfx_adr) {
185 switch (u->dis_mode) {
204 if (u->implicit_addr && u->pfx_seg)
205 mkasm(u, "%s ", ud_reg_tab[u->pfx_seg - UD_R_AL]);
207 /* print the instruction mnemonic */
208 mkasm(u, "%s ", ud_lookup_mnemonic(u->mnemonic));
211 if (u->operand[0].type != UD_NONE) {
212 gen_operand(u, &u->operand[0], u->c1);
215 if (u->operand[1].type != UD_NONE) {
217 gen_operand(u, &u->operand[1], u->c2);
221 if (u->operand[2].type != UD_NONE) {
223 gen_operand(u, &u->operand[2], u->c3);