# Makefile
# Kazuya Sakai, Ph.D
# Tokyo Metropolitan University
# Mar. 31, 2018

SRC1 = hello1
SRC2 = hello2
SRC3 = hello3
OBJ    = $(SRC1) $(SRC2) $(SRC3)
FILE   = Makefile *.txt *.asm
DIR	= helloworld_asm

all: hello1 hello2 hello3

hello1:
	nasm -f elf64 -o $(SRC1).o $(SRC1).asm
	ld hello1.o -o $(SRC1)
	objdump -D -M intel $(SRC1)

hello2:
	nasm -f elf64 -o $(SRC2).o $(SRC2).asm
	ld hello2.o -o $(SRC2)
	objdump -D -M intel $(SRC2)

hello3:
	nasm -f elf64 -o $(SRC3).o $(SRC3).asm
	ld hello3.o -o $(SRC3)
	objdump -D -M intel $(SRC3)

code1:hello1
	objdump -M intel -d $(SRC1) | grep '^ ' | cut -f2 | perl -pe 's/(\w{2})\s+/\\x\1/g'

code2:hello2
	objdump -M intel -d $(SRC2) | grep '^ ' | cut -f2 | perl -pe 's/(\w{2})\s+/\\x\1/g'

code3:hello3
	objdump -M intel -d $(SRC3) | grep '^ ' | cut -f2 | perl -pe 's/(\w{2})\s+/\\x\1/g'

pkg:
	# directory
	if [ ! -d $(DIR) ]; then \
		mkdir $(DIR); \
	fi
	
	# copy files
	for var in $(FILE); do\
		cp $$var $(DIR)/$$var; \
	done \
	
	# tar.gz
	tar zcvf $(DIR).tar.gz $(DIR)
	rm -r $(DIR)

clean:
	rm $(OBJ) *.o

