#!/usr/bin/env python

#

#  nopSembler.py -- Written by Coty R Miller 13/FEB/17

#

#  usage: nopSembler.py hello.asm

#

#  Out puts "nops.bin"

#

#  Demo code -- http://cotyrmiller.com/code/hello.asm

#  "You can use any instruction you want, as long as it's NOP!"

#==================================================================

import sys

import binascii



binary = ['00000000']*30

bc = 0

Instructions = {"NOP" : '10010000'}

Comments = ";"


print "NOP Assembler version: nop"   # We are NOP!


if len(sys.argv) > 1:                # If user provided a file name.

    file2load = sys.argv[1]          # Find desired file to load

    

    try:                             # TRY to...

        myfile = open(file2load)     # Load the file.

    except IOError as e:             # If we can't find it, error.

        print "Could nop load file! '%s' Check the file name." % (file2load)

    else:

        # Make an ugly buffer!

        ASMFile = [""]*30

        a = 0

        # Sift the file and omit new lines!

        for line in myfile:

            tmpLine = " "

            symbolsToOmit = "\n"

            # We only load 1 line at a time!

            for i in line:

                if(i not in symbolsToOmit):

                    tmpLine+=i

            # Give afor mentioned read line it's own string!

            ASMFile[a] = tmpLine

            a = a + 1 # Increment a!

            

        # Break apart all of the lines!

        codePoint = [] # A buffer!


        # Parse the File!

        for i in range(0, len(ASMFile)):

            line = ASMFile[i].split(" ")

            

            # Parse the Line!

            alreadyUsedOpCodeOnLine = 0

            for j in range(0, len(line)):

                # Check for comments!

                if line[j] == Comments:

                    break

                # Check for empty Spaces!

                elif line[j] != "":

                    # Check for instructions!

                    if line[j] in Instructions:

                        # Check if an NOP was already used on the line!

                        if alreadyUsedOpCodeOnLine == 0:

                            # Do the thing!

                            binary[bc] = Instructions[line[j]]

                            bc = bc + 1

                            alreadyUsedOpCodeOnLine = 1

                        else:

                            print "ERROR ON LINE", i+1, "ONLY ONE NOP PER LINE!"

                    else:

                        print "ERROR ON LINE", i+1, "You can use any opcode you want as long as it's NOP!"

                        sys.exit(0)

        # Export the binary.

        h = 0

        # Open the file

        outputFile = open("nops.bin","wb")


        # Count the opcodes!

        for i in range(0, 29):

            if binary[i] != '00000000':

                h = h + 1


        # Put up with pythons ugly binary file writting BS

        for i in range(0, h):

            a = binary[i]

            byte = int(a, 2)

            outputFile.write("%c" % byte)


        # Ensured the file is saved!

        outputFile.close()

else:

    print "No system argument used."


#end