Skip to content

Instantly share code, notes, and snippets.

@lodi-g
Last active December 6, 2016 19:55
Show Gist options
  • Save lodi-g/be9c4ce721521d557deef434663a6e3e to your computer and use it in GitHub Desktop.
Save lodi-g/be9c4ce721521d557deef434663a6e3e to your computer and use it in GitHub Desktop.
Get assembly from source file easily.
#!/bin/bash
# Needed variables
OBJFILE_PATH="/tmp/asm.o"
SCRIPT_NAME=$(basename $0)
ASSEMBLY_DEFAULT="AT&T"
CFLAGS_DEFAULT="-g"
# Help message
function disp_help()
{
echo "Usage: $SCRIPT_NAME [FILE] [CFLAGS] [ASSEMBLY]"
echo "Compile FILE with CFLAGS and displays the assembly using ASSEMBLY syntax."
echo "CFLAGS is, by default, $CFLAGS_DEFAULT."
echo "ASSEMBLY is, by default, $ASSEMBLY_DEFAULT."
echo "Example: $SCRIPT_NAME file.c \"-Iinclude/ -Wall -Wextra\" \"intel\""
}
# Basic args handling
if [ -z $1 ]; then
echo -e "Usage: $SCRIPT_NAME [FILE] [CFLAGS] [ASSEMBLY]"
echo "Try '$SCRIPT_NAME --help' for more informations."
exit 1
else
if [ $1 = "--help" ] || [ $1 = "-h" ]; then
disp_help
exit 0
elif [ ! -f $1 ]; then
echo "File not found. Check path."
exit 1
fi
fi
# Compiling
if [ -z $2 ]; then
gcc $CFLAGS_DEFAULT -c $1 -o $OBJFILE_PATH
else
gcc $CFLAGS_DEFAULT $2 -c $1 -o $OBJFILE_PATH
fi
# Objdump
if [ $? -eq 0 ]; then
if [ -z $3 ]; then
objdump -d -M $ASSEMBLY_DEFAULT -S $OBJFILE_PATH
else
objdump -d -M $3 -S $OBJFILE_PATH
fi
else
echo -e "\nCompile error."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment