-
Got this question via web-chat and I figured I'd post it here for others to benefit from. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There's two main ways you could do this. First if you just want opcodes for a generic instruction you can use the built-in assembler, like: >>> bv.arch.assemble("mov eax, eax")
b'\x89\xc0' Or if you don't have an open binary view, something like: >>> list(Architecture)
[<arch: aarch64>, <arch: armv7>, <arch: thumb2>, <arch: armv7eb>, <arch: thumb2eb>, <arch: mipsel32>, <arch: mips32>, <arch: ppc>, <arch: ppc64>, <arch: ppc_le>, <arch: ppc64_le>, <arch: x86_16>, <arch: x86>, <arch: x86_64>, <arch: msp430>, <arch: m16c>, <arch: M68000>, <arch: M68008>, <arch: M68010>, <arch: M68020>, <arch: M68030>, <arch: M68040>, <arch: M68LC040>, <arch: M68EC040>, <arch: M68330>, <arch: M68340>]
>>> Architecture['armv7'].assemble('nop').hex()
'00f020e3' Note that there's an optional second parameter for the assemble api. Second, if you're looking to find the opcode of an existing instruction with a binary, you'd use something like: >>> bv.read(here, bv.get_instruction_length(here))
b'\xc7\x05\xec\xd3F\x00\x01\x00\x00\x00' In that example, I'm combining the BinaryView read with the get_instruction_length API to determine how much to read. Also, I'm taking advantage of the here alias available to the scripting console when you have an address selected, but you could easily replace the address with one you got via some other mechanism. Note that I do NOT recommend using the get_disassembly API along with the assemble API as that can have trouble with relative code-references on some architectures. |
Beta Was this translation helpful? Give feedback.
There's two main ways you could do this. First if you just want opcodes for a generic instruction you can use the built-in assembler, like:
Or if you don't have an open binary view, something like: