Skip to content

Instantly share code, notes, and snippets.

@mkasimd
Created November 15, 2019 19:47
Show Gist options
  • Save mkasimd/0a2249647f2908181ec353cfcb799a78 to your computer and use it in GitHub Desktop.
Save mkasimd/0a2249647f2908181ec353cfcb799a78 to your computer and use it in GitHub Desktop.
This is a shell script enabling some useful options when working with assembly
#!/bin/bash
# Title: Assembling Script
# Version: 0.99
# Release: 06 Feb 2019
# Author: M. Kasim Doenmez
#
# running options:
##
## -c : compile C code and convert into ASM
## -c[0-3] : Compile with a set optimization level.
## -d : generate C driver (object file)
## -o : get ASM from existing object file
## -a : convert ASM to ELF object file
## -h : show all options and their usage
## -v : show version, author and path info
#
# REQUIRES:
## NASM: Assembler
## GCC: C/++ Compiler
## VIM: VI Improved editor
## Objconv: Object File Converter
# check for arguments
if [ $# == 0 ];
then
echo $0: usage: $0 -h
exit 1
fi
version="0.99"
# retrieve asked option and convert to lower case
option=${1,,}
# Set aliases: 'h', 'v', '-c3'
if [ $option == "h" ];
then
option="-h"
elif [ $option == "v" ];
then
option="-v"
elif [ $option == "-c3" ];
then
option="-c"
fi
# seperate file name and file extension from the entered filename
filename=${2%.*}
ext=${2##*.}
case "$option"
in
"-a")
echo "Compiling ${filename}.${ext} to ${filename}.o"
nasm -f elf ${filename}.${ext}
exit 0;;
"-c")
echo "Converting ${filename}.${ext} to ${filename}-${ext}.asm"
gcc -m32 -fno-asynchronous-unwind-tables -std=c99 -O3 -s -c -o ${filename}-${ext}.o ${filename}.${ext}
objconv -fnasm ${filename}-${ext}.o
rm ${filename}-${ext}.o
vim -c "%s/\(: function\)\|\(align\).\(.*\)\|ALIGN.\(.\)*//g" ${filename}-${ext}.asm
exit 0;;
"-c0")
echo "Converting ${filename}.${ext} to ${filename}-${ext}.asm"
gcc -m32 -fno-asynchronous-unwind-tables -std=c99 -O0 -s -c -o ${filename}-${ext}.o ${filename}.${ext}
objconv -fnasm ${filename}-${ext}.o
rm ${filename}-${ext}.o
vim -c "%s/\(: function\)\|\(align\).\(.*\)\|ALIGN.\(.\)*//g" ${filename}-${ext}.asm
exit 0;;
"-c1")
echo "Converting ${filename}.${ext} to ${filename}-${ext}.asm"
gcc -m32 -fno-asynchronous-unwind-tables -std=c99 -O1 -s -c -o ${filename}-${ext}.o ${filename}.${ext}
objconv -fnasm ${filename}-${ext}.o
rm ${filename}-${ext}.o
vim -c "%s/\(: function\)\|\(align\).\(.*\)\|ALIGN.\(.\)*//g" ${filename}-${ext}.asm
exit 0;;
"-c2")
echo "Converting ${filename}.${ext} to ${filename}-${ext}.asm"
gcc -m32 -fno-asynchronous-unwind-tables -std=c99 -O2 -s -c -o ${filename}-${ext}.o ${filename}.${ext}
objconv -fnasm ${filename}-${ext}.o
rm ${filename}-${ext}.o
vim -c "%s/\(: function\)\|\(align\).\(.*\)\|ALIGN.\(.\)*//g" ${filename}-${ext}.asm
exit 0;;
"-d")
echo "Compiling ${filename}.${ext} driver to ${filename}.o"
gcc -m32 -c ${filename}.${ext}
exit 0;;
"-o")
echo "Disassembling ${filename}.${ext} to ${filename}.asm"
objconv -fnasm ${filename}.${ext}
vim -c "%s/\(: function\)\|\(align\).\(.*\)\|ALIGN.\(.\)*//g" ${filename}.asm
exit 0;;
"-h")
echo "Assembling Script v${version}"
echo ""
echo "Option Usage Description"
echo "-a : asm -a file.asm Compile an ASM-Code in NASM syntax to an Object file"
echo "-c : asm -c file.c Convert a C-Code into ASM in NASM syntax"
echo "-c0: asm -c0 file.c Same as '-c', but no compiler optimization"
echo "-c1: asm -c1 file.c Same as above, but slightly optimized"
echo "-c2: asm -c2 file.c Same as above, but more optimized"
echo "-c3: asm -c3 file.c Same as '-c'. Highest optimization level"
echo "-d : asm -d driver.c Compiles the driver into an object file"
echo "-o : asm -o file.o Disassemble an Object file into ASM in NASM syntax"
echo "-h : asm -h Show this message"
echo "-v : asm -v Show version, author and path information"
echo ""
echo "NOTE: The entered file extension doesn't have to be the standard '.asm', '.c' or '.o'."
echo " But it is recommended to use the standards as the functionality cannot be guaranteed."
echo " Note that this script is rather made for private purposes. It comes with no warranty."
echo ""
echo "ATTENTION: This script can only compile and disassemble, but won't link any object files!"
echo " Use 'gcc -m32 -o %output% %obj1% %obj2% ...' to link object files.'"
echo ""
echo "REQUIRES:"
echo "VIM, NASM, GCC, Objconv"
exit 0;;
"-v")
echo "Assembling Script v${version}"
echo "Created by M. Kasim Doenmez"
echo ""
echo "in:"
pwd
exit 0;;
*)
echo "Invalid option entered"
echo $0: usage: $0 -h
exit 1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment