简体   繁体   中英

Encode asm instructions to opcodes

I need to encode a few instructions like

mov eax, edx
inc edx

to the corresponding x86_64 opcodes. Is there any library (not an entire asm compiler) to accomplish that easily?

您可以使用开源FASMNASM并使用其解析器。

in case you already compiled it into a binary (from your asm or c with embedded asm):

objdump -S your_binary, it will list each instruction with its binary code.

Assuming you are just after translating simple instructions, writing a simple assembler wouldn't be THAT much work. I've done it before - and you probably have most of the logic and tables for your disassembler component (such as a table of opcodes to instruction name and register number to name - just use that in reverse). I don't necessarily mean that you can just use the table directly in reverse, but the content of the tables re-arranged in a suitable way should do most of the hard work not too bad.

What gets difficult is symbols and relocation and such things. But since you probably don't really need that for "find this sequence of code", I guess you could do without those parts. You also don't need to generate object files to some specification - you just need a set of bytes.

Now, it would get a little bit more tricky if you wanted to find:

here:
     inc eax
     jnz here
     jmp someplace_else
....
...
someplace_else:
     ....

since you'd have to encode the jumps to the their relative location - at the very least, it would require a two-pass approach, to first figure the length of the instructions, then a the actual filling in of the jump targets. If "someplace_else" is far from the jump itself, it may also be an absolute jump, in which case your "search" would have to undertstand how that relates to the location it's searching at - since that sequence would be different for every single address.

I've written both assemblers and disassemblers, and it's not TERRIBLY hard if you don't have to deal with relocatable addresses and file formats with weird defintions that you don't know [until you've studied the 200 page definition of the format].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM