Assembler don't know where to start

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Assembler don't know where to start

Connor_Walsh
Hey everyone,

I'm starting the Assembler and I'm just completely stuck. I can't even figure out what language I want to write it in. I have very little experience with file input and output. I have the most experience in Java, and minor experience in C and python. Python seemed to be a popular choice because of it's simplicity and many built in functions. But in any language I'm completely lost in how to deal with files.

I kind of have a general idea of what I need to do. I want to open the file, take each line and cut out all the junk like whitespace and comments and such. I'm not sure how to go about this, and what I do with the line after its been purged of junk. Do I put it in a new file that I later use to translate to machine code? Does it just modify the original input file?

Any help is much appreciated, thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Assembler don't know where to start

cadet1620
Administrator
Here's a simple Python program that copies one file to another file. If you are running on a Mac or Linux system it should already be able to run the python program from the command line. If you are running on windows see this post.

Note that the readline() functions returns an empty string at end-of-file.

copylines.py:
-----8<----------
#!/usr/bin/python

import sys
import os


def main():
    if len(sys.argv) != 3:
        print ("Usage: copylines.py infile outfile")
        return

    sourceName = sys.argv[1]
    destName = sys.argv[2]
    sourceFile = open(sourceName, 'r')
    destFile = open(destName, 'w')
   
    line = sourceFile.readline()
    while len(line) > 0:
        destFile.write (line)
        line = sourceFile.readline()
       
    sourceFile.close()
    destFile.close()
   
main()            
-----8<----------

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Assembler don't know where to start

Connor_Walsh
Thanks for the example Mark! I've been playing around with it and I think I've made some progress. A new problem I can't seem to figure out with python is when I capture a line from the file and try to send it to another function I get the error "AttributeError: 'list' object has no attribute 'commandType' "

This is a bit of the code I'm using (python 2.7.6 on windows):

def main(self):
    with open('add.asm') as fp:
       for line in fp:
            #do some stuff/checks
             self.commandType(line)

and I have a function named commandType that takes a parameter and checks what kind of command it is, but as soon as this function is called I get the error. I assumed that my line variable would just be a string, but maybe not?

My commandType function

def commandType(self, line):
   #do some stuff

Thanks again for any help!
Reply | Threaded
Open this post in threaded view
|

Re: Assembler don't know where to start

cadet1620
Administrator
Connor_Walsh wrote
This is a bit of the code I'm using (python 2.7.6 on windows):

def main(self):
    with open('add.asm') as fp:
       for line in fp:
            #do some stuff/checks
             self.commandType(line)
...
def commandType(self, line):
   #do some stuff
"AttributeError: 'list' object has no attribute 'commandType' "

This sounds like the 'self' parameter passed to main is a list object, not your assembler class object.

For debugging, you can print the type of variable 'x' using 'print(type(x))'

Are both main() and commandType() members of the same class? How is main() being called?

If you want to, you can email me your code and I'll take a look at it.

--Mark