-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble.sh
executable file
·70 lines (53 loc) · 1.64 KB
/
assemble.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/sh
RED="\e[0;31m"
PURPLE='\e[0;34m'
GREEN='\e[0;32m'
NC='\e[0m' # No Color
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
intro="Assemble and Run program using nasm and with gcc linker. It assembles using nasm and links using gcc to binary."
author="Author: Abdullah AL Shohag <[email protected]>\n\
Github: https://github.com/HackerShohag"
usage="Usagae:\n\t${0} filename \t\tto tassemble, link and run"
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo -e ${intro} "\n\n" ${usage} "\n\n"${author}
exit 0
fi
file="$1"
filename=$(basename $file .asm)
if [ $# -eq 0 ] ; then
echo -e "${RED}${BOLD}ERROR: Please, specify a filename.${NORMAL}${NC}\n"
echo $usage "\n\n"${author}
exit 0
fi
if ! [ -f $file ]; then
echo -e "${RED}${BOLD}ERROR: \"$file\" does not exist.${NORMAL}${NC}\n"
echo $usage "\n\n"${author}
exit 0
fi
ext="${file##*.}"
if ! [ "$ext" = "asm" ]; then
echo -e "${RED}${BOLD}Provide a valid file (*.asm)${NORMAL}${NC}"
exit 0
fi
echo -- Found file $file
# assemble
echo -e "${PURPLE}${BOLD}NASM:${NORMAL}${PURPLE} Assebmling $file${NORMAL}${NC}"
nasmerr=$(nasm -f elf64 $file -o $filename.o)
if ! [ $? -eq 0 ]; then
echo $nasmerr
exit 0
fi
echo -e "${GREEN}${BOLD}NASM:${NORMAL}${GREEN} Assebmled $file"
#link
echo -e "${PURPLE}${BOLD}GCC:${NORMAL}${PURPLE} Linking $filename.o using ld${NORMAL}${NC}"
linkerr=$(ld -o $filename $filename.o)
if ! [ $? -eq 0 ]; then
echo $linkerr
exit 0
fi
echo -e "${GREEN}${BOLD}GCC:${NORMAL}${GREEN} Linked $filename.o to $filename${NORMAL}${NC}"
# build message
echo -e "${GREEN}${BOLD}Assemble: Built target $filename${NORMAL}${NC}"
# clean
rm $filename.o