简体   繁体   English

将asm指令编码为操作码

[英]Encode asm instructions to opcodes

I need to encode a few instructions like 我需要编写一些像

mov eax, edx
inc edx

to the corresponding x86_64 opcodes. 到相应的x86_64操作码。 Is there any library (not an entire asm compiler) to accomplish that easily? 是否有任何库(不是一个完整的asm编译器)可以轻松实现这一目标?

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

in case you already compiled it into a binary (from your asm or c with embedded asm): 如果你已经将它编译成二进制文件(从你的asm或c嵌入asm):

objdump -S your_binary, it will list each instruction with its binary code. objdump -S your_binary,它将列出每条指令及其二进制代码。

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. 如果“someplace_else”远离跳跃本身,那么它也可能是绝对跳跃,在这种情况下,您的“搜索”将必须考虑它与搜索位置的关系 - 因为该序列对于每个地址都是不同的。

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]. 我已经编写了汇编程序和反汇编程序,如果您不必处理可重定位的地址和文件格式,并且您不知道这些奇怪的定义[直到您研究了格式的200页定义,那么这并不难]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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