Tuesday, October 22, 2019

pnasm64 - Migrate C Code to NASM

Nasm is decently structured assembly - if you ever are interested in harnessing the true power of a new chipset you'll probably need to write assembly at some point.
alias pnasm64='_(){ local sel="";local file="${1}"; (($#<1))||[[ "${1}" =~ ^- || ! -e "${file}" ]]&&echo "Usage: pnasm64 <your.c> [-t (compile and link)]"&&return 1;rm -f "${file%.c}.o" "${file%.c}.asm";echo "Decompiling into object code.";gcc -fno-asynchronous-unwind-tables -s -c "${file}" -o "${file%.c}.o";echo "Converting to nasm.";objconv -fnasm "${file%.c}.o"; sed -i -e "s/align=1//g" -e "s/[a-z]*execute//g" -e "s/: *function//g" -e "/default *rel/d" "${file%.c}.asm";[[ -n "${2}" && "${2}" =~ "-t" ]]&&echo "Compiling with nasm..."&&nasm -felf64 -o "${file%.c}2.o" "${file%.c}.asm"&&echo "Linking with gcc..."&&gcc -fno-pie -no-pie -o "${file%.c}" "${file%.c}2.o";};_' 
To run this you'll need nasm, gcc from the repos - and objconv from: https://www.agner.org/optimize/
And this will only work out-of-the-box on simple C programs, to add libraries you'll need to hack it up and make it your own. Get the latest on GitHub.

No comments:

Post a Comment