For a lab, I need to know the hex instructions for assembly that I wrote. For example I have bang.s:

movl 0xaaaaaaaa 0xbbbbbbbb
push 0xcccccccc
ret

Now I want to get the hex code for these instructions. How can I use cc and/or objdump to accomplish this?

I’ve tried:

objdump -S bang.s

but I get "File format not recognized"

有帮助吗?

解决方案

Here's what I do.

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

In the above example, it does not have to be an object file. It could be an executable file.

Sometimes, I just need to know the byte sequence for or two instructions. In that case, I use the following.

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

Hope this helps.

其他提示

You'll need to compile(assemble) your .s file first, then run objdump on the resulting .o file.

cc or gcc normally knows how to build assembly files, so:

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

(Ofcourse, your dump.s must be valid assembly so you can build it, what you posted here isn't)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top