How to run assembler from the command line?

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

How to run assembler from the command line?

cdg
The book instructs us to run the assembler by supplying the name of the input file to it as a command line argument, thus:

prompt> Assembler Prog.asm

I'm able to write a program in Python which loads a text file containing assembly, translates it into machine code, and writes the result to a text file. I do this by supplying the name of the input file and the name of the output file in the Python code itself. So translating different text files requires me to modify those lines of the Python code.

I'm just not sure how to get the Python program to run from the command line, with the name of the input file supplied as an argument, rather than specified in the Python program itself. I assume this is a general question about command line, rather than about the Hack platform, but I wasn't able to find an answer online. Could you direct me to an appropriate resource?

Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: How to run assembler from the command line?

cadet1620
Administrator
To get command line arguments from a Python program, you want to use sys.argv.

File printargs.py:
import sys

def main():
    print(sys.argv)

main()

sys.argv returns the command line arguments in a list.  sys.argv[0] is the name of the Python program that's running.
[D:/tmp]
% printargs this is a test
['printargs.py', 'this', 'is', 'a', 'test']
[D:/tmp]
%

On Windows, to make Python programs runnable as commands as shown above, see this post.

(You can also just pass the arguments to the program after the program name if you are using the "python" command to run your program.)
[D:/tmp]
% python printargs.py this is a test
['printargs.py', 'this', 'is', 'a', 'test']
[D:/tmp]
%
--Mark

cdg
Reply | Threaded
Open this post in threaded view
|

Re: How to run assembler from the command line?

cdg
Thanks for your quick and helpful reply. I'm still a bit confused. I hope my question is not taking us too far afield. In case it's relevant, I'm using the Enthought Canopy (64-bit) distribution on an Asus Zenbook running Windows 10.

I've added ".py" to the PATHEXT environment variable and the directory containing my python programs (namely, "C:\Users\Cosmo\Desktop\nand2tetris\nand2tetris\projects\06") to the Path environment variable. But things aren't behaving as I'd like.

File hello.py:

  print 'hello world'

In the command prompt, I get the following results (where the current directory is not the directory containing hello.py) :

> hello
This opens the file hello.py in Canopy, but doesn't execute it.

> python hello
python: can't open file 'hello': [Errno 2] No such file or directory

> python C:\Users\Cosmo\Desktop\nand2tetris\nand2tetris\projects\06\hello
python: can't open file 'C:\Users\Cosmo\Desktop\nand2tetris\nand2tetris\projects\06\hello': [Errno 2] No such file or directory

> python C:\Users\Cosmo\Desktop\nand2tetris\nand2tetris\projects\06\hello.py
hello world

So when I don't put "python" at the start, it knows to look in the right directory to find hello.py, and it doesn't need the file extension; but it just opens the file, rather than executing it. When I do put "python" at the start, I have to give it the full path and the file extension of hello.py. What I'd like to do is get:

> hello
hello world

The ultimate aim is to run:

> Assembler Prog.asm
Reply | Threaded
Open this post in threaded view
|

Re: How to run assembler from the command line?

cadet1620
Administrator
You need to change the file association for .py files from your editor to python.exe.

I'm not a Windows 10 user -- there are tools I need to do my job that do not and never will run on Win 10 so I prefer to stay at Win 7.

On Windows 7 you can right-click on a .py file in Explorer and the popup menu has "Open with... > Choose default program" which brings up a dialog that will let you browse to find your python.exe.

It sounds like Microsoft may have changed this in Win10.  Here are a couple pages that look like they may be helpful.
  http://www.cnet.com/how-to/how-to-set-default-programs-in-windows-10/
  http://www.howtogeek.com/223144/how-to-set-your-default-apps-in-windows-10/

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

Re: How to run assembler from the command line?

cdg
Yes, that solved it. The default was Canopy but I changed it to python.exe and now get:

> hello
hello world

as I wanted. You can change defaults on Win10 the same way as Win7; I just didn't realize that that was the problem. Thank you for your help!

On Wed, 6 Jul 2016 at 18:44 cadet1620 [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote:
I'm not a Windows 10 user -- there are tools I need to do my job that do not and never will run on Win 10 so I prefer to stay at Win 7.

On Windows 7 you can right-click on a .py file in Explorer and the popup menu has "Open with... > Choose default program" which brings up a dialog that will let you browse to find your Pythonl.exe.

IT sounds like Microsoft may have changed this in Win10.  Here are a couple pages that look like they may be helpful.
  http://www.cnet.com/how-to/how-to-set-default-programs-in-windows-10/
  http://www.howtogeek.com/223144/how-to-set-your-default-apps-in-windows-10/

--Mark


If you reply to this email, your message will be added to the discussion below:
To unsubscribe from How to run assembler from the command line?, click here.
NAML