Skip to content

Instantly share code, notes, and snippets.

@sebastiancarlos
Last active December 15, 2024 18:36
Show Gist options
  • Save sebastiancarlos/b43786c552ccb288f7b53a6242fca1c8 to your computer and use it in GitHub Desktop.
Save sebastiancarlos/b43786c552ccb288f7b53a6242fca1c8 to your computer and use it in GitHub Desktop.
runasm - run assembly scripts from your terminal without compiling
#!/usr/bin/env bash
# All my gist code is licensed under the MIT license.
# Video demo: https://www.youtube.com/watch?v=kNKuRdoaNII
# runasm - Assemble, link, and run multiple assembly files, then delete them.
if [[ $# -eq 0 ]]; then
echo "Usage: runasm <file1.s> [<file2.s> ...]"
echo " - Assemble, link, and run multiple assembly files, then delete them."
echo " - Name of executable is the name of the first file without extension."
exit 1
fi
object_files=()
executable_file=${1%.*}
for assembly_file in "$@"; do
# Avengers, assemble!
object_file="${assembly_file%.*}.o"
as "${assembly_file}" -o "${object_file}"
if [[ $? -ne 0 ]]; then
exit 1
fi
object_files+=("${object_file}")
done
# Link
ld "${object_files[@]}" -o "${executable_file}"
if [[ $? -ne 0 ]]; then
exit 1
fi
# Run, remove created files, and return exit code
./"${executable_file}"
exit_code=$?
rm "${object_files[@]}" "${executable_file}" > /dev/null 2>&1
exit "${exit_code}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment