Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Instead of leaving some code at the top-level of the file, you should wrap it into a function. It let you test and re-use it more easily. You should also make use of the if __name__ == '__main__': idiomif __name__ == '__main__': idiom:

Instead of leaving some code at the top-level of the file, you should wrap it into a function. It let you test and re-use it more easily. You should also make use of the if __name__ == '__main__': idiom:

Instead of leaving some code at the top-level of the file, you should wrap it into a function. It let you test and re-use it more easily. You should also make use of the if __name__ == '__main__': idiom:

added 125 characters in body
Source Link

There might not be a real need to perform 2 passes over the input file. Whenever a symbol cannot be resolved, store it in a dictionnary as a key and its associated value should be a list of every line this symbol was encountered. Use dict.setdefault(symbol, []) for that. This may require that you modify value so that unresolved symbols doesn't terminate the program but you can tell they don't exist. dict.get(key) might help here as it return the value associated to the key if it exist in the dictionary or None if it doesn't.

There might not be a real need to perform 2 passes over the input file. Whenever a symbol cannot be resolved, store it in a dictionnary as a key and its associated value should be a list of every line this symbol was encountered. Use dict.setdefault(symbol, []) for that. This may require that you modify value so that unresolved symbols doesn't terminate the program but you can tell they don't exist.

There might not be a real need to perform 2 passes over the input file. Whenever a symbol cannot be resolved, store it in a dictionnary as a key and its associated value should be a list of every line this symbol was encountered. Use dict.setdefault(symbol, []) for that. This may require that you modify value so that unresolved symbols doesn't terminate the program but you can tell they don't exist. dict.get(key) might help here as it return the value associated to the key if it exist in the dictionary or None if it doesn't.

Source Link

#Some simplifications

  • You can initialize the symbols dictionnary in one instruction:

      # Memory map
      symbols = {
          "IN0": 0xfff8,
          "IN1": 0xfff9,
          "OUT0": 0xfffa,
          "OUT1": 0xfffb,
      }
    
  • The format template string, when applied to a number, can take a base specifyier. So '{:b}'.format(x) will pretty much return the same thing than bin(x) except without the '0b' prefix. You can thus turn to_bin into:

      def to_bin(n, bits):
          return "{:0>{}b}".format(n & 2**bits-1, bits)
    

    As regard to applying the bitmask to limit the length of the output, you also have the possibility to cut the string afterwards:

      def to_bin(n, bits):
          return "{:0>{}b}".format(n, bits)[-bits:]
    

    I find it somewhat clearer of what is going on, but it might be slower. You’ll need to time it if it ever turns out to be an issue.

  • When dealing with formating stuff using a template like '{:<xxx>}', if <xxx> does not contain any other parameter, it might be clearer to use the format function directly. Combine that with the fact that the print function can be used to write in files, you can turn:

      fileOut.write("{:04x}".format(int(asm, 2)) + "\n")
    

    into

      print(format(int(asm, 2), '04x'), file=fileOut)
    
  • You can use the "magic" base 0 of the int function to let python automatically "guess" the base of your number:

      >>> int('0b101', 0)
      5
      >>> int('0x1f', 0)
      31
      >>> int('42', 0)
      42
    

    Note however, that python can't disambiguate between octal and decimal if the string contains only digits but starts with a '0':

      >>> int('0644', 0)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      ValueError: invalid literal for int() with base 0: '0644'
      >>> int('0o644', 0)
      420
      >>> int('644', 0)
      644
    

    It may not apply to you, so you could simplify value to:

      def value(s):
          try:
              return int(s, 0)
          except ValueError:
              try:
                  return symbols[s] # get address
              except KeyError:
                  sys.exit("Error: undefined symbol \"{}\"".format(s))
    

    A few other improvements here: use of EAFP to make the intent more direct (let's convert this value into an integer; it doesn't work? let's pick its address; still doesn't work? then give up). And use of sys.exit instead of quit that should only be used within an interactive interpreter. exit has the advantage, if passed a string as parameter, to print it to stderr and to exit with a non-zero status code. Same improvement can be made to the "invalid instruction" near the end.

  • You appear to have duplicated code to extract out comments and empty lines from your input file. Why not extract this behaviour into a function instead? This will allow you to avoid the call to seek too. And to avoid filling up the memory with the whole file at once, let's write a generator instead:

      def filter_out_comments(filename):
          with open(filename) as f:
              for line_num, line in enumerate(fileIn, start=1):
                  tokens = line.split("#")[0].split()
                  if tokens:
                      yield tokens, line_num
    

    And use it like:

      with open("Programs/"+sys.argv[1]+".asm", "w+") as fileOut:
          print("### First Pass - Mapping Symbols to Addresses ###")
          address = 0
          for tokens, line_num in filter_out_comments("Programs/" + sys.argv[1]):
              if tokens[0][-1] == ":": # found symbol
          ...
          print("### Second Pass - Translating into machine code ###")
          address = 0
          for tokens, line_num in filter_out_comments("Programs/" + sys.argv[1]):
              asm = ""
              if tokens[0] in RRR and len(tokens) == 4:
                  ...
    

#Some improvements

Instead of leaving some code at the top-level of the file, you should wrap it into a function. It let you test and re-use it more easily. You should also make use of the if __name__ == '__main__': idiom:

def compile_asm(filename)
    with open(filename + ".asm", "w+") as fileOut:
        print("### First Pass - Mapping Symbols to Addresses ###")
        address = 0
        for tokens, line_num in filter_out_comments(filename):
            if tokens[0][-1] == ":": # found symbol
        ...
        print("### Second Pass - Translating into machine code ###")
        address = 0
        for tokens, line_num in filter_out_comments(filename):
            asm = ""
            if tokens[0] in RRR and len(tokens) == 4:
                ...


if __name__ == '__main__':
    compile_asm("Programs/" + sys.argv[1])

Second, you should document your code a bit more, especially when sharing it like that, as it may be sometimes obscure why you are doing things like you do. It makes sense eventually but it would be easier to understand with a few comments and some docstrings.

And, lastly, follow PEP8, the official coding style, if you want your code to look like Python code.

#One pass algorithm

There might not be a real need to perform 2 passes over the input file. Whenever a symbol cannot be resolved, store it in a dictionnary as a key and its associated value should be a list of every line this symbol was encountered. Use dict.setdefault(symbol, []) for that. This may require that you modify value so that unresolved symbols doesn't terminate the program but you can tell they don't exist.

Whenever a new symbol is discovered, check if it exist in this dictionary and patch each line accordingly. Then delete it from the dictionary. If the dictionnary is not empty at the end, you had unresolved symbols…

For it to work, though, you may need to store at least the incomplete lines and all that follows in memory. Depending on your needs, it is a tradeoff that may or may not be acceptable.