1 /* disasm.c where all the _work_ gets done in the Netwide Disassembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 27/iii/95 by Simon Tatham
26 * Flags that go into the `segment' field of `insn' structures
29 #define SEG_RELATIVE 1
36 #define SEG_SIGNED 128
43 uint8_t osize
; /* Operand size */
44 uint8_t asize
; /* Address size */
45 uint8_t osp
; /* Operand size prefix present */
46 uint8_t asp
; /* Address size prefix present */
47 uint8_t rep
; /* Rep prefix present */
48 uint8_t seg
; /* Segment override prefix present */
49 uint8_t lock
; /* Lock prefix present */
50 uint8_t vex
[3]; /* VEX prefix present */
51 uint8_t vex_m
; /* VEX.M field */
53 uint8_t vex_lp
; /* VEX.LP fields */
54 uint32_t rex
; /* REX prefix present */
57 #define getu8(x) (*(uint8_t *)(x))
59 /* Littleendian CPU which can handle unaligned references */
60 #define getu16(x) (*(uint16_t *)(x))
61 #define getu32(x) (*(uint32_t *)(x))
62 #define getu64(x) (*(uint64_t *)(x))
64 static uint16_t getu16(uint8_t *data
)
66 return (uint16_t)data
[0] + ((uint16_t)data
[1] << 8);
68 static uint32_t getu32(uint8_t *data
)
70 return (uint32_t)getu16(data
) + ((uint32_t)getu16(data
+2) << 16);
72 static uint64_t getu64(uint8_t *data
)
74 return (uint64_t)getu32(data
) + ((uint64_t)getu32(data
+4) << 32);
78 #define gets8(x) ((int8_t)getu8(x))
79 #define gets16(x) ((int16_t)getu16(x))
80 #define gets32(x) ((int32_t)getu32(x))
81 #define gets64(x) ((int64_t)getu64(x))
83 /* Important: regval must already have been adjusted for rex extensions */
84 static enum reg_enum
whichreg(int32_t regflags
, int regval
, int rex
)
86 if (!(regflags
& (REGISTER
|REGMEM
)))
87 return 0; /* Registers not permissible?! */
91 if (!(REG_AL
& ~regflags
))
93 if (!(REG_AX
& ~regflags
))
95 if (!(REG_EAX
& ~regflags
))
97 if (!(REG_RAX
& ~regflags
))
99 if (!(REG_DL
& ~regflags
))
101 if (!(REG_DX
& ~regflags
))
103 if (!(REG_EDX
& ~regflags
))
105 if (!(REG_RDX
& ~regflags
))
107 if (!(REG_CL
& ~regflags
))
109 if (!(REG_CX
& ~regflags
))
111 if (!(REG_ECX
& ~regflags
))
113 if (!(REG_RCX
& ~regflags
))
115 if (!(FPU0
& ~regflags
))
117 if (!(XMM0
& ~regflags
))
119 if (!(YMM0
& ~regflags
))
121 if (!(REG_CS
& ~regflags
))
122 return (regval
== 1) ? R_CS
: 0;
123 if (!(REG_DESS
& ~regflags
))
124 return (regval
== 0 || regval
== 2
125 || regval
== 3 ? nasm_rd_sreg
[regval
] : 0);
126 if (!(REG_FSGS
& ~regflags
))
127 return (regval
== 4 || regval
== 5 ? nasm_rd_sreg
[regval
] : 0);
128 if (!(REG_SEG67
& ~regflags
))
129 return (regval
== 6 || regval
== 7 ? nasm_rd_sreg
[regval
] : 0);
131 /* All the entries below look up regval in an 16-entry array */
132 if (regval
< 0 || regval
> 15)
135 if (!(REG8
& ~regflags
)) {
137 return nasm_rd_reg8_rex
[regval
];
139 return nasm_rd_reg8
[regval
];
141 if (!(REG16
& ~regflags
))
142 return nasm_rd_reg16
[regval
];
143 if (!(REG32
& ~regflags
))
144 return nasm_rd_reg32
[regval
];
145 if (!(REG64
& ~regflags
))
146 return nasm_rd_reg64
[regval
];
147 if (!(REG_SREG
& ~regflags
))
148 return nasm_rd_sreg
[regval
& 7]; /* Ignore REX */
149 if (!(REG_CREG
& ~regflags
))
150 return nasm_rd_creg
[regval
];
151 if (!(REG_DREG
& ~regflags
))
152 return nasm_rd_dreg
[regval
];
153 if (!(REG_TREG
& ~regflags
)) {
155 return 0; /* TR registers are ill-defined with rex */
156 return nasm_rd_treg
[regval
];
158 if (!(FPUREG
& ~regflags
))
159 return nasm_rd_fpureg
[regval
& 7]; /* Ignore REX */
160 if (!(MMXREG
& ~regflags
))
161 return nasm_rd_mmxreg
[regval
& 7]; /* Ignore REX */
162 if (!(XMMREG
& ~regflags
))
163 return nasm_rd_xmmreg
[regval
];
164 if (!(YMMREG
& ~regflags
))
165 return nasm_rd_ymmreg
[regval
];
171 * Process a DREX suffix
173 static uint8_t *do_drex(uint8_t *data
, insn
*ins
)
175 uint8_t drex
= *data
++;
176 operand
*dst
= &ins
->oprs
[ins
->drexdst
];
178 if ((drex
& 8) != ((ins
->rex
& REX_OC
) ? 8 : 0))
179 return NULL
; /* OC0 mismatch */
180 ins
->rex
= (ins
->rex
& ~7) | (drex
& 7);
182 dst
->segment
= SEG_RMREG
;
183 dst
->basereg
= drex
>> 4;
189 * Process an effective address (ModRM) specification.
191 static uint8_t *do_ea(uint8_t *data
, int modrm
, int asize
,
192 int segsize
, operand
* op
, insn
*ins
)
194 int mod
, rm
, scale
, index
, base
;
198 mod
= (modrm
>> 6) & 03;
201 if (mod
!= 3 && rm
== 4 && asize
!= 16)
204 if (ins
->rex
& REX_D
) {
205 data
= do_drex(data
, ins
);
211 if (mod
== 3) { /* pure register version */
212 op
->basereg
= rm
+(rex
& REX_B
? 8 : 0);
213 op
->segment
|= SEG_RMREG
;
222 * <mod> specifies the displacement size (none, byte or
223 * word), and <rm> specifies the register combination.
224 * Exception: mod=0,rm=6 does not specify [BP] as one might
225 * expect, but instead specifies [disp16].
227 op
->indexreg
= op
->basereg
= -1;
228 op
->scale
= 1; /* always, in 16 bits */
259 if (rm
== 6 && mod
== 0) { /* special case */
263 mod
= 2; /* fake disp16 */
267 op
->segment
|= SEG_NODISP
;
270 op
->segment
|= SEG_DISP8
;
271 op
->offset
= (int8_t)*data
++;
274 op
->segment
|= SEG_DISP16
;
275 op
->offset
= *data
++;
276 op
->offset
|= ((unsigned)*data
++) << 8;
282 * Once again, <mod> specifies displacement size (this time
283 * none, byte or *dword*), while <rm> specifies the base
284 * register. Again, [EBP] is missing, replaced by a pure
285 * disp32 (this time that's mod=0,rm=*5*) in 32-bit mode,
286 * and RIP-relative addressing in 64-bit mode.
289 * indicates not a single base register, but instead the
290 * presence of a SIB byte...
292 int a64
= asize
== 64;
297 op
->basereg
= nasm_rd_reg64
[rm
| ((rex
& REX_B
) ? 8 : 0)];
299 op
->basereg
= nasm_rd_reg32
[rm
| ((rex
& REX_B
) ? 8 : 0)];
301 if (rm
== 5 && mod
== 0) {
303 op
->eaflags
|= EAF_REL
;
304 op
->segment
|= SEG_RELATIVE
;
305 mod
= 2; /* fake disp32 */
309 op
->disp_size
= asize
;
312 mod
= 2; /* fake disp32 */
315 if (rm
== 4) { /* process SIB */
316 scale
= (sib
>> 6) & 03;
317 index
= (sib
>> 3) & 07;
320 op
->scale
= 1 << scale
;
322 if (index
== 4 && !(rex
& REX_X
))
323 op
->indexreg
= -1; /* ESP/RSP cannot be an index */
325 op
->indexreg
= nasm_rd_reg64
[index
| ((rex
& REX_X
) ? 8 : 0)];
327 op
->indexreg
= nasm_rd_reg32
[index
| ((rex
& REX_X
) ? 8 : 0)];
329 if (base
== 5 && mod
== 0) {
331 mod
= 2; /* Fake disp32 */
333 op
->basereg
= nasm_rd_reg64
[base
| ((rex
& REX_B
) ? 8 : 0)];
335 op
->basereg
= nasm_rd_reg32
[base
| ((rex
& REX_B
) ? 8 : 0)];
343 op
->segment
|= SEG_NODISP
;
346 op
->segment
|= SEG_DISP8
;
347 op
->offset
= gets8(data
);
351 op
->segment
|= SEG_DISP32
;
352 op
->offset
= gets32(data
);
361 * Determine whether the instruction template in t corresponds to the data
362 * stream in data. Return the number of bytes matched if so.
364 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
366 static int matches(const struct itemplate
*t
, uint8_t *data
,
367 const struct prefix_info
*prefix
, int segsize
, insn
*ins
)
369 uint8_t *r
= (uint8_t *)(t
->code
);
370 uint8_t *origdata
= data
;
371 bool a_used
= false, o_used
= false;
372 enum prefixes drep
= 0;
373 uint8_t lock
= prefix
->lock
;
374 int osize
= prefix
->osize
;
375 int asize
= prefix
->asize
;
378 int s_field_for
= -1; /* No 144/154 series code encountered */
380 int regmask
= (segsize
== 64) ? 15 : 7;
382 for (i
= 0; i
< MAX_OPERANDS
; i
++) {
383 ins
->oprs
[i
].segment
= ins
->oprs
[i
].disp_size
=
384 (segsize
== 64 ? SEG_64BIT
: segsize
== 32 ? SEG_32BIT
: 0);
387 ins
->rex
= prefix
->rex
;
388 memset(ins
->prefixes
, 0, sizeof ins
->prefixes
);
390 if (t
->flags
& (segsize
== 64 ? IF_NOLONG
: IF_LONG
))
393 if (prefix
->rep
== 0xF2)
395 else if (prefix
->rep
== 0xF3)
398 while ((c
= *r
++) != 0) {
399 opx
= &ins
->oprs
[c
& 3];
413 ins
->oprs
[0].basereg
= 0;
416 ins
->oprs
[0].basereg
= 2;
419 ins
->oprs
[0].basereg
= 3;
429 ins
->oprs
[0].basereg
= 4;
432 ins
->oprs
[0].basereg
= 5;
442 ins
->oprs
[0].basereg
= 0;
445 ins
->oprs
[0].basereg
= 1;
448 ins
->oprs
[0].basereg
= 2;
451 ins
->oprs
[0].basereg
= 3;
461 ins
->oprs
[0].basereg
= 4;
464 ins
->oprs
[0].basereg
= 5;
473 int t
= *r
++, d
= *data
++;
474 if (d
< t
|| d
> t
+ 7)
477 opx
->basereg
= (d
-t
)+
478 (ins
->rex
& REX_B
? 8 : 0);
479 opx
->segment
|= SEG_RMREG
;
486 opx
->offset
= (int8_t)*data
++;
487 opx
->segment
|= SEG_SIGNED
;
491 opx
->offset
= *data
++;
495 opx
->offset
= *data
++;
499 opx
->offset
= getu16(data
);
505 opx
->offset
= getu32(data
);
508 opx
->offset
= getu16(data
);
511 if (segsize
!= asize
)
512 opx
->disp_size
= asize
;
517 opx
->offset
= getu32(data
);
524 opx
->offset
= getu16(data
);
530 opx
->offset
= getu32(data
);
536 opx
->offset
= getu64(data
);
544 opx
->offset
= gets8(data
++);
545 opx
->segment
|= SEG_RELATIVE
;
549 opx
->offset
= getu64(data
);
554 opx
->offset
= gets16(data
);
556 opx
->segment
|= SEG_RELATIVE
;
557 opx
->segment
&= ~SEG_32BIT
;
561 opx
->segment
|= SEG_RELATIVE
;
563 opx
->offset
= gets16(data
);
565 opx
->segment
&= ~(SEG_32BIT
|SEG_64BIT
);
566 } else if (osize
== 32) {
567 opx
->offset
= gets32(data
);
569 opx
->segment
&= ~SEG_64BIT
;
570 opx
->segment
|= SEG_32BIT
;
572 if (segsize
!= osize
) {
574 (opx
->type
& ~SIZE_MASK
)
575 | ((osize
== 16) ? BITS16
: BITS32
);
580 opx
->offset
= gets32(data
);
582 opx
->segment
|= SEG_32BIT
| SEG_RELATIVE
;
591 opx
->segment
|= SEG_RMREG
;
592 data
= do_ea(data
, modrm
, asize
, segsize
,
593 &ins
->oprs
[(c
>> 3) & 3], ins
);
596 opx
->basereg
= ((modrm
>> 3)&7)+
597 (ins
->rex
& REX_R
? 8 : 0);
602 if (s_field_for
== (c
& 3)) {
603 opx
->offset
= gets8(data
);
606 opx
->offset
= getu16(data
);
613 s_field_for
= (*data
& 0x02) ? c
& 3 : -1;
614 if ((*data
++ & ~0x02) != *r
++)
619 if (s_field_for
== (c
& 3)) {
620 opx
->offset
= gets8(data
);
623 opx
->offset
= getu32(data
);
630 ins
->drexdst
= c
& 3;
634 ins
->rex
|= REX_D
|REX_OC
;
635 ins
->drexdst
= c
& 3;
639 data
= do_drex(data
, ins
);
646 uint8_t ximm
= *data
++;
648 ins
->oprs
[c
>> 3].basereg
= (ximm
>> 4) & regmask
;
649 ins
->oprs
[c
>> 3].segment
|= SEG_RMREG
;
650 ins
->oprs
[c
& 7].offset
= ximm
& 15;
656 uint8_t ximm
= *data
++;
662 ins
->oprs
[c
>> 4].basereg
= (ximm
>> 4) & regmask
;
663 ins
->oprs
[c
>> 4].segment
|= SEG_RMREG
;
669 uint8_t ximm
= *data
++;
672 ins
->oprs
[c
].basereg
= (ximm
>> 4) & regmask
;
673 ins
->oprs
[c
].segment
|= SEG_RMREG
;
687 if (((modrm
>> 3) & 07) != (c
& 07))
688 return false; /* spare field doesn't match up */
689 data
= do_ea(data
, modrm
, asize
, segsize
,
690 &ins
->oprs
[(c
>> 3) & 07], ins
);
701 if ((prefix
->rex
& (REX_V
|REX_D
|REX_P
)) != REX_V
)
704 if ((vexm
& 0x1f) != prefix
->vex_m
)
707 switch (vexwlp
& 030) {
709 if (prefix
->rex
& REX_W
)
713 if (!(prefix
->rex
& REX_W
))
717 case 020: /* VEX.W is a don't care */
724 if ((vexwlp
& 007) != prefix
->vex_lp
)
727 opx
->segment
|= SEG_RMREG
;
728 opx
->basereg
= prefix
->vex_v
;
738 if ((prefix
->rex
& (REX_V
|REX_D
|REX_P
)) != REX_V
)
741 if ((vexm
& 0x1f) != prefix
->vex_m
)
744 switch (vexwlp
& 030) {
746 if (ins
->rex
& REX_W
)
750 if (!(ins
->rex
& REX_W
))
754 break; /* Need to do anything special here? */
757 if ((vexwlp
& 007) != prefix
->vex_lp
)
760 if (prefix
->vex_v
!= 0)
782 if (asize
!= segsize
)
796 if (prefix
->rex
& REX_B
)
801 if (prefix
->rex
& REX_X
)
806 if (prefix
->rex
& REX_R
)
811 if (prefix
->rex
& REX_W
)
830 if (osize
!= (segsize
== 16) ? 16 : 32)
837 ins
->rex
|= REX_W
; /* 64-bit only instruction */
843 if (!(ins
->rex
& (REX_P
|REX_W
)) || osize
!= 64)
850 int t
= *r
++, d
= *data
++;
851 if (d
< t
|| d
> t
+ 15)
854 ins
->condition
= d
- t
;
864 if (prefix
->rep
!= 0xF2)
870 if (prefix
->rep
!= 0xF3)
895 if (prefix
->osp
|| prefix
->rep
)
900 if (!prefix
->osp
|| prefix
->rep
)
906 if (prefix
->osp
|| prefix
->rep
!= 0xf2)
912 if (prefix
->osp
|| prefix
->rep
!= 0xf3)
940 return false; /* Unknown code */
944 if (!vex_ok
&& (ins
->rex
& REX_V
))
947 /* REX cannot be combined with DREX or VEX */
948 if ((ins
->rex
& (REX_D
|REX_V
)) && (prefix
->rex
& REX_P
))
952 * Check for unused rep or a/o prefixes.
954 for (i
= 0; i
< t
->operands
; i
++) {
955 if (ins
->oprs
[i
].segment
!= SEG_RMREG
)
960 if (ins
->prefixes
[PPS_LREP
])
962 ins
->prefixes
[PPS_LREP
] = P_LOCK
;
965 if (ins
->prefixes
[PPS_LREP
])
967 ins
->prefixes
[PPS_LREP
] = drep
;
970 if (osize
!= ((segsize
== 16) ? 16 : 32)) {
971 enum prefixes pfx
= 0;
985 if (ins
->prefixes
[PPS_OSIZE
])
987 ins
->prefixes
[PPS_OSIZE
] = pfx
;
990 if (!a_used
&& asize
!= segsize
) {
991 if (ins
->prefixes
[PPS_ASIZE
])
993 ins
->prefixes
[PPS_ASIZE
] = asize
== 16 ? P_A16
: P_A32
;
996 /* Fix: check for redundant REX prefixes */
998 return data
- origdata
;
1001 /* Condition names for disassembly, sorted by x86 code */
1002 static const char * const condition_name
[16] = {
1003 "o", "no", "c", "nc", "z", "nz", "na", "a",
1004 "s", "ns", "pe", "po", "l", "nl", "ng", "g"
1007 int32_t disasm(uint8_t *data
, char *output
, int outbufsize
, int segsize
,
1008 int32_t offset
, int autosync
, uint32_t prefer
)
1010 const struct itemplate
* const *p
, * const *best_p
;
1011 const struct disasm_index
*ix
;
1013 int length
, best_length
= 0;
1015 int i
, slen
, colon
, n
;
1019 uint32_t goodness
, best
;
1021 struct prefix_info prefix
;
1024 memset(&ins
, 0, sizeof ins
);
1027 * Scan for prefixes.
1029 memset(&prefix
, 0, sizeof prefix
);
1030 prefix
.asize
= segsize
;
1031 prefix
.osize
= (segsize
== 64) ? 32 : segsize
;
1038 while (!end_prefix
) {
1042 prefix
.rep
= *data
++;
1046 prefix
.lock
= *data
++;
1050 segover
= "cs", prefix
.seg
= *data
++;
1053 segover
= "ss", prefix
.seg
= *data
++;
1056 segover
= "ds", prefix
.seg
= *data
++;
1059 segover
= "es", prefix
.seg
= *data
++;
1062 segover
= "fs", prefix
.seg
= *data
++;
1065 segover
= "gs", prefix
.seg
= *data
++;
1069 prefix
.osize
= (segsize
== 16) ? 32 : 16;
1070 prefix
.osp
= *data
++;
1073 prefix
.asize
= (segsize
== 32) ? 16 : 32;
1074 prefix
.asp
= *data
++;
1079 if (segsize
== 64 || (data
[1] & 0xc0) == 0xc0) {
1080 prefix
.vex
[0] = *data
++;
1081 prefix
.vex
[1] = *data
++;
1085 if (prefix
.vex
[0] == 0xc4) {
1086 prefix
.vex
[2] = *data
++;
1087 prefix
.rex
|= (~prefix
.vex
[1] >> 5) & 7; /* REX_RXB */
1088 prefix
.rex
|= (prefix
.vex
[2] >> (7-3)) & REX_W
;
1089 prefix
.vex_m
= prefix
.vex
[1] & 0x1f;
1090 prefix
.vex_v
= (~prefix
.vex
[2] >> 3) & 15;
1091 prefix
.vex_lp
= prefix
.vex
[2] & 7;
1093 prefix
.rex
|= (~prefix
.vex
[1] >> (7-2)) & REX_R
;
1095 prefix
.vex_v
= (~prefix
.vex
[1] >> 3) & 15;
1096 prefix
.vex_lp
= prefix
.vex
[1] & 7;
1099 ix
= itable_VEX
[prefix
.vex_m
][prefix
.vex_lp
];
1120 if (segsize
== 64) {
1121 prefix
.rex
= *data
++;
1122 if (prefix
.rex
& REX_W
)
1134 best
= -1; /* Worst possible */
1136 best_pref
= INT_MAX
;
1139 return 0; /* No instruction table at all... */
1143 while (ix
->n
== -1) {
1144 ix
= (const struct disasm_index
*)ix
->p
+ *dp
++;
1147 p
= (const struct itemplate
* const *)ix
->p
;
1148 for (n
= ix
->n
; n
; n
--, p
++) {
1149 if ((length
= matches(*p
, data
, &prefix
, segsize
, &tmp_ins
))) {
1152 * Final check to make sure the types of r/m match up.
1153 * XXX: Need to make sure this is actually correct.
1155 for (i
= 0; i
< (*p
)->operands
; i
++) {
1156 if (!((*p
)->opd
[i
] & SAME_AS
) &&
1158 /* If it's a mem-only EA but we have a
1160 ((tmp_ins
.oprs
[i
].segment
& SEG_RMREG
) &&
1161 !(MEMORY
& ~(*p
)->opd
[i
])) ||
1162 /* If it's a reg-only EA but we have a memory
1164 (!(tmp_ins
.oprs
[i
].segment
& SEG_RMREG
) &&
1165 !(REG_EA
& ~(*p
)->opd
[i
]) &&
1166 !((*p
)->opd
[i
] & REG_SMASK
)) ||
1167 /* Register type mismatch (eg FS vs REG_DESS):
1169 ((((*p
)->opd
[i
] & (REGISTER
| FPUREG
)) ||
1170 (tmp_ins
.oprs
[i
].segment
& SEG_RMREG
)) &&
1171 !whichreg((*p
)->opd
[i
],
1172 tmp_ins
.oprs
[i
].basereg
, tmp_ins
.rex
))
1180 * Note: we always prefer instructions which incorporate
1181 * prefixes in the instructions themselves. This is to allow
1182 * e.g. PAUSE to be preferred to REP NOP, and deal with
1183 * MMX/SSE instructions where prefixes are used to select
1184 * between MMX and SSE register sets or outright opcode
1189 goodness
= ((*p
)->flags
& IF_PFMASK
) ^ prefer
;
1191 for (i
= 0; i
< MAXPREFIX
; i
++)
1192 if (tmp_ins
.prefixes
[i
])
1194 if (nprefix
< best_pref
||
1195 (nprefix
== best_pref
&& goodness
< best
)) {
1196 /* This is the best one found so far */
1199 best_pref
= nprefix
;
1200 best_length
= length
;
1208 return 0; /* no instruction was matched */
1210 /* Pick the best match */
1212 length
= best_length
;
1216 /* TODO: snprintf returns the value that the string would have if
1217 * the buffer were long enough, and not the actual length of
1218 * the returned string, so each instance of using the return
1219 * value of snprintf should actually be checked to assure that
1220 * the return value is "sane." Maybe a macro wrapper could
1221 * be used for that purpose.
1223 for (i
= 0; i
< MAXPREFIX
; i
++)
1224 switch (ins
.prefixes
[i
]) {
1226 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "lock ");
1229 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "rep ");
1232 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "repe ");
1235 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "repne ");
1238 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "a16 ");
1241 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "a32 ");
1244 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "a64 ");
1247 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "o16 ");
1250 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "o32 ");
1253 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "o64 ");
1260 if (i
>= FIRST_COND_OPCODE
)
1261 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s%s",
1262 nasm_insn_names
[i
], condition_name
[ins
.condition
]);
1264 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1265 nasm_insn_names
[i
]);
1268 length
+= data
- origdata
; /* fix up for prefixes */
1269 for (i
= 0; i
< (*p
)->operands
; i
++) {
1270 opflags_t t
= (*p
)->opd
[i
];
1271 const operand
*o
= &ins
.oprs
[i
];
1275 o
= &ins
.oprs
[t
& ~SAME_AS
];
1276 t
= (*p
)->opd
[t
& ~SAME_AS
];
1279 output
[slen
++] = (colon
? ':' : i
== 0 ? ' ' : ',');
1282 if (o
->segment
& SEG_RELATIVE
) {
1283 offs
+= offset
+ length
;
1285 * sort out wraparound
1287 if (!(o
->segment
& (SEG_32BIT
|SEG_64BIT
)))
1289 else if (segsize
!= 64)
1293 * add sync marker, if autosync is on
1304 if ((t
& (REGISTER
| FPUREG
)) ||
1305 (o
->segment
& SEG_RMREG
)) {
1307 reg
= whichreg(t
, o
->basereg
, ins
.rex
);
1309 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "to ");
1310 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1311 nasm_reg_names
[reg
-EXPR_REG_START
]);
1312 } else if (!(UNITY
& ~t
)) {
1313 output
[slen
++] = '1';
1314 } else if (t
& IMMEDIATE
) {
1317 snprintf(output
+ slen
, outbufsize
- slen
, "byte ");
1318 if (o
->segment
& SEG_SIGNED
) {
1321 output
[slen
++] = '-';
1323 output
[slen
++] = '+';
1325 } else if (t
& BITS16
) {
1327 snprintf(output
+ slen
, outbufsize
- slen
, "word ");
1328 } else if (t
& BITS32
) {
1330 snprintf(output
+ slen
, outbufsize
- slen
, "dword ");
1331 } else if (t
& BITS64
) {
1333 snprintf(output
+ slen
, outbufsize
- slen
, "qword ");
1334 } else if (t
& NEAR
) {
1336 snprintf(output
+ slen
, outbufsize
- slen
, "near ");
1337 } else if (t
& SHORT
) {
1339 snprintf(output
+ slen
, outbufsize
- slen
, "short ");
1342 snprintf(output
+ slen
, outbufsize
- slen
, "0x%"PRIx64
"",
1344 } else if (!(MEM_OFFS
& ~t
)) {
1346 snprintf(output
+ slen
, outbufsize
- slen
,
1347 "[%s%s%s0x%"PRIx64
"]",
1348 (segover
? segover
: ""),
1349 (segover
? ":" : ""),
1350 (o
->disp_size
== 64 ? "qword " :
1351 o
->disp_size
== 32 ? "dword " :
1352 o
->disp_size
== 16 ? "word " : ""), offs
);
1354 } else if (!(REGMEM
& ~t
)) {
1355 int started
= false;
1358 snprintf(output
+ slen
, outbufsize
- slen
, "byte ");
1361 snprintf(output
+ slen
, outbufsize
- slen
, "word ");
1364 snprintf(output
+ slen
, outbufsize
- slen
, "dword ");
1367 snprintf(output
+ slen
, outbufsize
- slen
, "qword ");
1370 snprintf(output
+ slen
, outbufsize
- slen
, "tword ");
1373 snprintf(output
+ slen
, outbufsize
- slen
, "oword ");
1376 snprintf(output
+ slen
, outbufsize
- slen
, "yword ");
1378 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "far ");
1381 snprintf(output
+ slen
, outbufsize
- slen
, "near ");
1382 output
[slen
++] = '[';
1384 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1385 (o
->disp_size
== 64 ? "qword " :
1386 o
->disp_size
== 32 ? "dword " :
1387 o
->disp_size
== 16 ? "word " :
1389 if (o
->eaflags
& EAF_REL
)
1390 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "rel ");
1393 snprintf(output
+ slen
, outbufsize
- slen
, "%s:",
1397 if (o
->basereg
!= -1) {
1398 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1399 nasm_reg_names
[(o
->basereg
-EXPR_REG_START
)]);
1402 if (o
->indexreg
!= -1) {
1404 output
[slen
++] = '+';
1405 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1406 nasm_reg_names
[(o
->indexreg
-EXPR_REG_START
)]);
1409 snprintf(output
+ slen
, outbufsize
- slen
, "*%d",
1415 if (o
->segment
& SEG_DISP8
) {
1417 uint8_t offset
= offs
;
1418 if ((int8_t)offset
< 0) {
1425 snprintf(output
+ slen
, outbufsize
- slen
, "%s0x%"PRIx8
"",
1427 } else if (o
->segment
& SEG_DISP16
) {
1429 uint16_t offset
= offs
;
1430 if ((int16_t)offset
< 0 && started
) {
1434 prefix
= started
? "+" : "";
1437 snprintf(output
+ slen
, outbufsize
- slen
,
1438 "%s0x%"PRIx16
"", prefix
, offset
);
1439 } else if (o
->segment
& SEG_DISP32
) {
1440 if (prefix
.asize
== 64) {
1442 uint64_t offset
= (int64_t)(int32_t)offs
;
1443 if ((int32_t)offs
< 0 && started
) {
1447 prefix
= started
? "+" : "";
1450 snprintf(output
+ slen
, outbufsize
- slen
,
1451 "%s0x%"PRIx64
"", prefix
, offset
);
1454 uint32_t offset
= offs
;
1455 if ((int32_t) offset
< 0 && started
) {
1459 prefix
= started
? "+" : "";
1462 snprintf(output
+ slen
, outbufsize
- slen
,
1463 "%s0x%"PRIx32
"", prefix
, offset
);
1466 output
[slen
++] = ']';
1469 snprintf(output
+ slen
, outbufsize
- slen
, "<operand%d>",
1473 output
[slen
] = '\0';
1474 if (segover
) { /* unused segment override */
1476 int count
= slen
+ 1;
1478 p
[count
+ 3] = p
[count
];
1479 strncpy(output
, segover
, 2);
1485 int32_t eatbyte(uint8_t *data
, char *output
, int outbufsize
)
1487 snprintf(output
, outbufsize
, "db 0x%02X", *data
);