Skip to content

Commit

Permalink
python: replace op_index() with op_find(), which straightly returns t…
Browse files Browse the repository at this point in the history
…he desired operand rather than its cumbersome index
  • Loading branch information
aquynh committed Dec 6, 2013
1 parent f1618bc commit dcde7e7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions bindings/python/capstone/capstone.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,21 +238,27 @@ def __init__(self, csh, all_info, arch):

self.csh = csh

# get the last error code
def errno():
return _cs.cs_errno(self.csh)

# get the register name, given the register ID
def reg_name(self, reg_id):
return _cs.cs_reg_name(self.csh, reg_id)

# get the instruction string
def insn_name(self):
return _cs.cs_insn_name(self.csh, self.id)

# verify if this insn belong to group with id as @group_id
def group(self, group_id):
return group_id in self.groups

# verify if this instruction implicitly read register @reg_id
def reg_read(self, reg_id):
return reg_id in self.regs_read

# verify if this instruction implicitly modified register @reg_id
def reg_write(self, reg_id):
return reg_id in self.regs_write

Expand All @@ -264,13 +270,14 @@ def op_count(self, op_type):
c += 1
return c

def op_index(self, op_type, position):
# get the operand at position @position of all operands having the same type @op_type
def op_find(self, op_type, position):
c = 0
for op in self.operands:
if op.type == op_type:
c += 1
if c == position:
return self.operands.index(op)
return op


class Cs(object):
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/test_x86.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def print_insn_detail(mode, insn):
if count > 0:
print("\timm_count: %u" %count)
for i in xrange(count):
index = insn.op_index(X86_OP_IMM, i + 1)
print("\t\timms[%u]: 0x%s" %(i+1, to_x(insn.operands[index].value.imm)))
op = insn.op_find(X86_OP_IMM, i + 1)
print("\t\timms[%u]: 0x%s" %(i+1, to_x(op.value.imm)))

if len(insn.operands) > 0:
print("\top_count: %u" %len(insn.operands))
Expand Down

0 comments on commit dcde7e7

Please sign in to comment.