2

From disassembled code, I want to extract the hexadecimal of instruction, as boxed in the figure below

enter image description here

plus, I want to get distinct value of each opcode and its operand, which means sort of

mov => 1 bl => 57 like thing.

I'm not sure this is the one, but I've found something like this

enter image description here

in Ghidra/Processors/ARM/data/manuals/ARM.idx

1 Answer 1

2

I'm going to assume you have an Address object (I'll just use currentAddress) .

With an address you can get the Instruction at that address. Then read that many bytes from Instruction object.

from ghidra.program.flatapi import FlatProgramAPI

api = FlatProgramAPI(currentProgram, monitor)
instruction = api.getInstructionAt(currentAddress)
ibytes = insturction.getBytes()
print(binascii.hexlify(ibytes))

You can also inspect the operands with instruction.getOpObjects(). Or look through the GhidraAPI to see what else you do with the operands. I'm not exactly sure what you are hoping to get out it.

1
  • Awesome! Your answer for hexcode is just what I want! About opcode and object, the value itself is not important for me, I just want each opcode/operand to be distinct, no same value, cuz my purpose is to use those as a token for ML I'll look up instruction.getOpObjects() and update if I find what I want!
    – rabbit
    Commented Jan 16, 2023 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.