2014-03-05 20 views
1

对于实验室,我需要知道我编写的十六进制指令。例如我有bang.s将gcc装配到十六进制

movl 0xaaaaaaaa 0xbbbbbbbb 
push 0xcccccccc 
ret 

现在我想要得到这些指令的十六进制代码。我如何使用cc和/或objdump来完成此操作?

我已经试过:

objdump -S bang.s 

,但我得到 “文件格式无法识别”

+1

无论你使用什么汇编程序,它肯定都会有一个列表选项? – TonyK

回答

5

这是我做的。

1. Compile the assembly file. 
2. Disassemble the object file (objdump -d objFile.o > objFile.asm) 

在上面的例子中,它不一定是一个目标文件。它可能是一个可执行文件。

有时候,我只需要知道字节序列或两条指令。在这种情况下,我使用以下内容。

1. In my .bashrc file (Linux), add the lines ... 

opcode32() { 
    echo $* > tmp.S && gcc -c tmp.S -o tmp.o && objdump -d tmp.o 
    rm -f tmp.o tmp.S 
} 

2. From the command line, use opcode32 'instruction'. For example ... 
     opcode32 'pushl %ecx' 
    This yields the following output ... 

tmp.o:  file format elf32-i386 


Disassembly of section .text: 

00000000 <.text>: 
    0: 51      push %ecx 

希望这会有所帮助。

2

您需要首先编译(汇编)您的.s文件,然后在生成的.o文件上运行objdump。

CC或GCC通常知道如何建立汇编文件,所以:

cc -c bang.s 
objdump -d bang.o 

(Ofcourse,你dump.s必须是有效的组件,所以你可以建造它,你张贴在这里是不是)