assembly64-bitgnu-assembler

GNU AS ASM to bytecode dump


Some time ago I created this function to dump a bytecode given an ASM instruction:

show_bytecode(){ echo "$@" | as -o temp.o && ld --oformat binary -o temp.bin temp.o 2>/dev/null && xxd --ps temp.bin && rm temp.o temp.bin; }

So I was able to use it like:

show_bytecode "mov %r8, %r10"
4d89c2

I worked for a while, but I have no idea why(probably different GNU AS version), currently it is dumping some garbage before the bytecode:

$ show_bytecode "mov %r8, %r10" | wc 
    137     137    8335
$ show_bytecode "mov %r8, %r10"
040000002000000005000000474e5500020001c004000000000000000000
0000010001c0040000000100000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000
... a lot of zeros ...
000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000004d89c2

Looking for the man pages I was unable to spot what to do to restrict the bytecode to only the result that matters, in this case 4d89c2 like it was before.

Anyone know what this noise is and how to remove it?

Would be nice to make it clean, but if you know other better solution to print a instruction bytecode from an ASM code, it can be used too.


Solution

  • Unless you specifically need linking, just use objcopy instead:

    show_bytecode(){ echo "$@" | as -o temp.o && objcopy -O binary -j .text temp.o temp.bin 2>/dev/null && xxd --ps temp.bin && rm temp.o temp.bin; }
    

    Of course as itself can output a listing which may be good enough for your purposes especially if you just want a human readable output:

    echo "$@" | as -al -o /dev/null - | tail -n +4