3 from __future__
import print_function
16 o need more testing on M68K_AM_*_DISP
21 objdump_cmd_example =
'm68k-atari-mint-objdump -b binary -D -mm68k --adjust-vma 0x30664 u/m68k.bin'
22 objdump_dumpheader_fmt =
"""
23 %s: file format binary
26 Disassembly of section .data:
31 M68000_CODE = b
"\x04\x40\x00\x40"
34 (CS_ARCH_M68K, CS_MODE_BIG_ENDIAN | CS_MODE_M68K_060, M68000_CODE,
"M68060-32 (Big-endian)"),
42 str += format(
"%02x%02x " % (b[i], b[i+1]))
47 if op_reg == M68K_REG_A7:
49 if op_reg == M68K_REG_A6:
51 return '%' + insn.reg_name(op_reg)
54 return bitstring.Bits(uint=value, length=8).
unpack(
'int')[0]
57 return bitstring.Bits(uint=value, length=16).
unpack(
'int')[0]
61 return 0xffffffffffffff00 + value
66 return 0xffff0000 + value
71 return 0xffffffffffff0000 + value
86 while (i < 7
and (data & (1 << (i + 1)))):
90 if len(buffer)
or len(str):
93 str += format(
"%%%s%d" % (prefix, first))
95 str += format(
"-%%%s%d" % (prefix, first + run_length))
101 data = op.register_bits
110 map_index_size_str = { 0:
'w', 1 :
'l' }
113 if op.address_mode == M68K_AM_NONE:
114 if op.type == M68K_OP_REG_BITS:
116 if op.type == M68K_OP_REG_PAIR:
118 if op.type == M68K_OP_REG:
121 if op.address_mode == M68K_AM_REG_DIRECT_DATA:
123 if op.address_mode == M68K_AM_REG_DIRECT_ADDR:
125 if op.address_mode == M68K_AM_REGI_ADDR:
127 if op.address_mode == M68K_AM_REGI_ADDR_POST_INC:
129 if op.address_mode == M68K_AM_REGI_ADDR_PRE_DEC:
131 if op.address_mode == M68K_AM_REGI_ADDR_DISP:
135 str += format(
"@(%d)" %
s16(op.mem.disp))
138 if op.address_mode == M68K_AM_PCI_DISP:
139 return format(
"%%pc@(0x%x)" % (
extsign1616(op.mem.disp + 2)))
140 if op.address_mode == M68K_AM_ABSOLUTE_DATA_SHORT:
141 return format(
"0x%x" % (
extsign1616(op.imm & 0xffff)))
142 if op.address_mode == M68K_AM_ABSOLUTE_DATA_LONG:
143 return format(
"0x%x" % (op.imm & 0xffffffff))
144 if op.address_mode == M68K_AM_IMMEDIATE:
145 if insn.op_size.type == M68K_SIZE_TYPE_FPU:
146 map_fpu_size_str = { M68K_FPU_SIZE_SINGLE : op.simm, M68K_FPU_SIZE_DOUBLE : op.dimm }
147 return format(
"#%f" % (insn.op_size.fpu_size[map_fpu_size_str]))
148 return format(
"#$%x" % (op.imm))
150 if op.address_mode
in [ M68K_AM_PCI_INDEX_8_BIT_DISP, M68K_AM_AREGI_INDEX_8_BIT_DISP ]:
152 if op.register_bits == 2:
154 if op.register_bits == 4:
157 str =
dump_op_reg(insn, op.mem.base_reg) +
"@(" +
"{0:016x}".format(disp) +
"," +
dump_op_reg(insn, op.mem.index_reg) +
":" + map_index_size_str[op.mem.index_size]
159 str += format(
":%u" % (op.register_bits))
163 if op.address_mode
in [ M68K_AM_PCI_INDEX_BASE_DISP, M68K_AM_AREGI_INDEX_BASE_DISP ]:
164 str += format(
"%s" % (
dump_op_reg(insn, op.mem.base_reg) ))
167 str +=
"," +
dump_op_reg(insn, op.mem.index_reg) +
":" + map_index_size_str[op.mem.index_size]
169 str += format(
":%u" % (op.register_bits))
173 if op.mem.in_disp > 0:
174 str += format(
"$%x" % ( op.mem.in_disp))
178 if op.address_mode == M68K_AM_PCI_INDEX_BASE_DISP:
180 if op.mem.index_size:
184 str += format(
"pc,%s%s.%s" % (
dump_op_reg(insn, op.mem.index_reg)), s_spacing, str_size)
186 if op.mem.base_reg != M68K_REG_INVALID:
187 str += format(
"a%d,%s" % ( op.mem.base_reg - M68K_REG_A0, s_spacing))
189 if op.mem.index_size:
193 str += format(
"%s.%s" % (
dump_op_reg(insn, op.mem.index_reg), str_size))
196 str += format(
"%s*%s%d)" % ( s_spacing, s_spacing, op.mem.scale))
205 if op.address_mode
in [ M68K_AM_PC_MEMI_POST_INDEX, M68K_AM_PC_MEMI_PRE_INDEX, M68K_AM_MEMI_PRE_INDEX, M68K_AM_MEMI_POST_INDEX]:
207 str += format(
"%s" % (
dump_op_reg(insn, op.mem.base_reg) ))
209 value = op.mem.in_disp
210 if op.mem.in_disp & 0x8000:
211 value = 0xffffffffffff0000 + op.mem.in_disp
212 str += format(
"@(%016x)@(%016x)" % (value, op.mem.out_disp))
216 if op.mem.in_disp > 0:
217 str += format(
"$%x" % ( op.mem.in_disp))
219 if op.mem.base_reg != M68K_REG_INVALID:
220 if op.mem.in_disp > 0:
221 str += format(
",%s%s" % ( s_spacing,
dump_op_reg(insn, op.mem.base_reg)))
223 str += format(
"%s" % (
dump_op_reg(insn, op.mem.base_reg)))
225 if op.address_mode
in [ M68K_AM_MEMI_POST_INDEX, M68K_AM_PC_MEMI_POST_INDEX]:
228 if op.mem.index_reg != M68K_REG_INVALID:
230 if op.mem.index_size:
234 str += format(
",%s%s.%s" % ( s_spacing,
dump_op_reg(insn, op.mem.index_reg), str_size))
236 str += format(
"%s*%s%d" % ( s_spacing, s_spacing, op.mem.scale))
237 if op.address_mode
in [ M68K_AM_MEMI_PRE_INDEX, M68K_AM_PC_MEMI_PRE_INDEX]:
239 if op.mem.out_disp > 0:
240 str += format(
",%s$%x" % ( s_spacing, op.mem.out_disp))
246 return format(
"%d:%d" % ( op.mem.offset, op.mem.width))
249 if op.address_mode == M68K_AM_AREGI_INDEX_BASE_DISP:
250 if op.mem.index_size:
255 return dump_op_reg(insn, op.mem.base_reg) +
"@(" +
"{0:016b}".format(bits) +
"," +
dump_op_reg(insn, op.mem.index_reg) +
":" + str_size +
")"
262 map_address_mode_str = {
264 1 :
"M68K_AM_REG_DIRECT_DATA",
265 2 :
"M68K_AM_REG_DIRECT_ADDR",
266 3 :
"M68K_AM_REGI_ADDR",
267 4 :
"M68K_AM_REGI_ADDR_POST_INC",
268 5 :
"M68K_AM_REGI_ADDR_PRE_DEC",
269 6 :
"M68K_AM_REGI_ADDR_DISP",
270 7 :
"M68K_AM_AREGI_INDEX_8_BIT_DISP",
271 8 :
"M68K_AM_AREGI_INDEX_BASE_DISP",
272 9 :
"M68K_AM_MEMI_POST_INDEX",
273 10 :
"M68K_AM_MEMI_PRE_INDEX",
274 11 :
"M68K_AM_PCI_DISP",
275 12 :
"M68K_AM_PCI_INDEX_8_BIT_DISP",
276 13 :
"M68K_AM_PCI_INDEX_BASE_DISP",
277 14 :
"M68K_AM_PC_MEMI_POST_INDEX",
278 15 :
"M68K_AM_PC_MEMI_PRE_INDEX",
279 16 :
"M68K_AM_ABSOLUTE_DATA_SHORT",
280 17 :
"M68K_AM_ABSOLUTE_DATA_LONG",
281 18 :
"M68K_AM_IMMEDIATE",
288 0 :
"M68K_OP_INVALID",
293 5 :
"M68K_OP_REG_BITS",
294 6 :
"M68K_OP_REG_PAIR",
299 if len(sys.argv) > 3:
300 print(
"id %d type %s address_mode %s" % (insn.id, map_op_str[op.type], map_address_mode_str[op.address_mode]))
305 mnemonic = insn.insn_name()
308 while i <
len(insn.operands):
311 op = insn.operands[i]
314 if insn.id == M68K_INS_INVALID:
315 return format(
"0x%04x" % (op.imm))
316 if op.type == M68K_OP_REG:
318 if str_op_reg ==
'' or op.address_mode == M68K_AM_REG_DIRECT_ADDR:
321 if op.type == M68K_OP_IMM:
322 str_op_imm = format(
"#%u" % (op.imm))
323 if mnemonic
in [
"bkpt"]:
324 str_op_imm = format(
"%u" % (op.imm))
325 signed_insn = [
"move",
"moveq",
"cmp",
"cmpi",
"ori",
"bclr",
"pack",
"unpk",
"sub",
"add" ]
326 if mnemonic
in signed_insn:
327 if insn.op_size.size == 1
or mnemonic ==
"moveq":
328 str_op_imm = format(
"#%d" %
s8(op.imm))
329 if insn.op_size.size == 2
or mnemonic ==
"pack":
330 str_op_imm = format(
"#%d" %
s16(op.imm))
331 if insn.op_size.size == 4:
332 str_op_imm = format(
"#%d" % (op.imm))
334 dbxx_insn = [
"dbt",
"dbf",
"dbhi",
"dbls",
"dbcc",
"dbcs",
"dbne",
"dbeq",
"dbvc",
"dbvs",
"dbpl",
"dbmi",
"dbge",
"dblt",
"dbgt",
"dble",
"dbra" ]
335 if is_branch(insn)
or mnemonic
in dbxx_insn:
336 str_op_imm = format(
"0x%x" % (op.imm & 0xffffffff))
338 if op.type == M68K_OP_MEM:
341 str_op_mem = format(
"0x%x" % (op.imm))
343 if op.type
in [ M68K_OP_REG_BITS, M68K_OP_REG_PAIR ]:
354 mnemonic = insn.insn_name()
355 branch_insn = [
"bsr",
"bra",
"bhi",
"bls",
"bcc",
"bcs",
"bne",
"beq",
"bvc",
"bvs",
"bpl",
"bmi",
"bge",
"blt",
"bgt",
"ble" ];
356 return mnemonic
in branch_insn
360 if insn.id == M68K_INS_INVALID:
362 mnemonic = insn.insn_name()
363 ext = { 0:
'', 1:
'b', 2:
'w', 4:
'l' }
365 ext.update({ 1:
's', 2:
'w', 4:
'l' })
367 no_size = [
"pea",
"lea",
"bset",
"bclr",
"bchg",
"btst",
"nbcd",
"abcd",
"sbcd",
"exg",
"scc",
"sls",
"scs",
"shi" ]
368 sxx_insn = [
"st",
"sf",
"shi",
"sls",
"scc",
"scs",
"sne",
"seq",
"svc",
"svs",
"spl",
"smi",
"sge",
"slt",
"sgt",
"sle",
"stop" ]
371 if mnemonic
in no_size:
372 ext.update({ 0:
'', 1:
'', 2:
'', 4:
'' })
373 return mnemonic + ext[insn.op_size.size]
384 if len(insn.operands) > 0:
390 delta =
min(insn.size, 6)
391 print(
" %x:\t%s " % (insn.address+delta,
dump_bytes(insn._raw.bytes[delta:],
min(insn.size-delta, 6))))
395 print(objdump_dumpheader_fmt % (filename, address))
399 for (arch, mode, code, comment)
in all_tests:
400 filename =
"/dev/stdin"
402 if len(sys.argv) > 1:
403 filename = sys.argv[1]
404 if len(sys.argv) > 2:
405 address =
int(sys.argv[2],16)
406 if len(sys.argv) > 3:
409 with open(filename,
"rb")
as f:
418 for insn
in md.disasm(code, address):
422 print(
"ERROR: %s" % e)
425 if __name__ ==
'__main__':
static void registerPair(SStream *O, const cs_m68k_op *op)
def printRegbitsRange(buffer, data, prefix)
def dump_op_reg(insn, op_reg)
def print_objdump_dumpheader(filename='', address=0)
def print_insn_detail_np(insn)
static int unpack(libgdbr_t *g, struct parse_ctx *ctx, int len)