Get Opcode from shell

You’re hacking some binary with your favourite debugger/hex-editor and want to replace some bytes with the opcodes of mov eax, 0x0 but you didn’t know the corresponding bytes of this assembler-statement? So the following simple script will help you out.

You call it like

$ opcode "mov eax,0x0"

and you’ll get the following output:

0000000 b8 00 00 00 00

opcode is just a simple shell-wrapper which is using nasm to get the corresponding opcode. You can use it as base to add additional extensions like various architectures and so on.

Hex-output is done by the od command (linux-standard)

Here’s the source of opcode:

# Usage: opcode "mov eax,0x0"
# Output:
# 0000000 b8 00 00 00 00
# 0000005

my_opcode() {
        echo -e "BITS 64\n $*" > /tmp/$ID.tmp.S && nasm -f bin /tmp/$ID.tmp.S -o /tmp/$ID.tmp.o && od -t x1 /tmp/$ID.tmp.o
        rm -f /tmp/$ID.tmp.o /tmp/$ID.tmp.S

my_opcode $@

Use cut&paste to copy the text from above and save it to a file called opcode (or if you prefer some extension)