opcode
opcode() {
if [[ "$1" == "" ]]; then
echo "together:"
echo "usage: opcode 'mov \$1, %eax'"
echo "usage: opcode 'mov \$1, %eax; inc %eax'"
echo ""
echo "one-at-a-time:"
echo "usage: opcode_series 'mov \$1, %eax; inc %eax'"
else
local n="$(mktemp)"
local assembly_fn="$n.s"
local object_fn="$n.o"
asm="$*"
echo " $asm"
echo -e ".section .text\n$asm" > "$assembly_fn" && as -msyntax=att "$assembly_fn" -o "$object_fn" && readelf --hex-dump=.text "$object_fn" | tail -n +3
rm -f "$assembly_fn" "$object_fn"
fi
}
opcode_series
opcode_series() {
if [[ "$1" == "" ]]; then
echo "one-at-a-time:"
echo "usage: opcode_series 'mov \$1, %eax; inc %eax'"
echo ""
echo "together:"
echo "usage: opcode 'mov \$1, %eax'"
echo "usage: opcode 'mov \$1, %eax; inc %eax'"
else
IFS=$';' read -r -a lines <<< "$*"
for line in "${lines[@]}"; do
line="${line#"${line%%[![:space:]]*}"}" # remove leading spaces
opcode "$line"
done
fi
}
Test Area
It can be handy to have this little test script for messing around with them.
test_opcode.sh
#!/bin/bash
echo "----------------------------------------"
echo "test opcode generation - all together"
echo "----------------------------------------"
opcode() {
if [[ "$1" == "" ]]; then
echo "together:"
echo "usage: opcode 'mov \$1, %eax'"
echo "usage: opcode 'mov \$1, %eax; inc %eax'"
echo ""
echo "one-at-a-time:"
echo "usage: opcode_series 'mov \$1, %eax; inc %eax'"
else
local n="$(mktemp)"
local assembly_fn="$n.s"
local object_fn="$n.o"
asm="$*"
echo " $asm"
echo -e ".section .text\n$asm" > "$assembly_fn" && as -msyntax=att "$assembly_fn" -o "$object_fn" && readelf --hex-dump=.text "$object_fn" | tail -n +3
rm -f "$assembly_fn" "$object_fn"
fi
}
opcode 'movl $1, %eax'
opcode 'mov $1, %eax; inc %eax'
echo "----------------------------------------"
echo "test opcode generation - one-at-a-time"
echo "----------------------------------------"
opcode_series() {
if [[ "$1" == "" ]]; then
echo "one-at-a-time:"
echo "usage: opcode_series 'mov \$1, %eax; inc %eax'"
echo ""
echo "together:"
echo "usage: opcode 'mov \$1, %eax'"
echo "usage: opcode 'mov \$1, %eax; inc %eax'"
else
IFS=$';' read -r -a lines <<< "$*"
for line in "${lines[@]}"; do
line="${line#"${line%%[![:space:]]*}"}" # remove leading spaces
opcode "$line"
done
fi
}
opcode_series 'mov $1, %eax; inc %eax'
Output of Test
What pops out before you start messing with it :)
bo@pop:~/dev/bin/test$ test_opcode.sh
----------------------------------------
test opcode generation - all together
----------------------------------------
movl $1, %eax
0x00000000 b8010000 00 .....
mov $1, %eax; inc %eax
0x00000000 b8010000 00ffc0 .......
----------------------------------------
test opcode generation - one-at-a-time
----------------------------------------
mov $1, %eax
0x00000000 b8010000 00 .....
inc %eax
0x00000000 ffc0 ..