Author Topic: Learning how to use NASM  (Read 13562 times)

Offline cyrus_nasm32

  • New Member
  • Posts: 1
Learning how to use NASM
« on: March 09, 2011, 06:21:19 AM »
I downloaded nasm 2.09.04 win32 installer. Iam using Windows XP SP3. Iam use to programming in Java, do I need to setup path for nasm, how? Can I write my codes in note pad then compile it with NASM later? When I click the NASM icon in my desktop, it will open a command line but the tutorial pdf doesn't exactly tell me anything how to it. I use the HELP command but that didn't put me in the right direction or I haven't yet figure it out. please help. tnx!

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Learning how to use NASM
« Reply #1 on: March 09, 2011, 12:34:25 PM »
do I need to setup path for nasm, how?

yes, click Start, right-click My Computer, select properties.  Go to Advanced tab, click Environment Variables.  Add the location to where you installed nasm to your path

Can I write my codes in note pad then compile it with NASM later?

Yes, any ASCII editor will work.  You may also want to check out some of these editors that are tailored towards assembly programming:
http://www.asmcommunity.net/board/index.php?board=24.0

Finally, I recommend you read a few assembler tutorials on the net before diving into assembly programming if you have no prior experience...

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Learning how to use NASM
« Reply #2 on: March 10, 2011, 03:52:05 AM »
Once you've got a command prompt, or other opportunity to enter a command line, you're almost there. "nasm -h" will give you the options, I've put it in a different format here:

http://home.myfairpoint.net/fbkotler/clswitch.html

This is somewhat obsolete, but probably "close enough" to start with. The most important option is probably the output format. Nasm defaults to "-f bin", if you don't specify... which may be what you want, if you're starting with a .com file (obsolete, but easy to do because no linker is necessary). You should probably get in the habit of specifying an output format, even if the default is right - it won't always be. That may be all you need, but for a .com file you also want to specify an output filename...

Code: [Select]
nasm -f bin -o myfile.com myfile.asm

Don't be confused by this - the "-o" switch only controls the name of the output file - changing it to "-o myfile.exe" will not give you an .exe format file! The "-f" switch is "the boss". In most output formats, the default output filename is acceptable - unless you want to name it something else, the "-f" switch may be all you need. There are many other options for flexibility and convenience - see the Fine Manual.

You mention having the "tutorial pdf". I'm not sure what that is. The Manual? That should tell you all you need to know. Quite a bit more than you need to know, actually. You probably don't want to read it "cover to cover". I keep meaning to write a "quickstart index" into the Manual. I'll probably never get around to it...

I suppose you should read the installation instructions (with the "installer", you may not even need that):

http://www.nasm.us/xdoc/2.09.06/html/nasmdoc1.html#section-1.3.1

Then I suppose you'll want to read "Command Line Syntax":

http://www.nasm.us/xdoc/2.09.06/html/nasmdoc2.html#section-2.1

Read it at least as far as the "-f" switch (you can skip over the Unix cruft). Before you can actually issue a command line, you'll need a file to assemble. At this point, "what OS?" matters. You may want to skip ahead and read up on the output format appropriate to your OS. The required/accepted source code varies slightly.

If your OS will run a .com file, it's an easy format to start with, though you probably don't want to spend a whole lot of time "learning dos".

http://www.nasm.us/xdoc/2.09.06/html/nasmdoc7.html#section-7.1

The "org" directive is an important concept. You can skip over the part about multiple section support for now, but you might want to read the section about "map" files. Might help you understand where Nasm is putting your stuff, if it isn't clear. This may help, too.

http://www.nasm.us/xdoc/2.09.06/html/nasmdoc8.html#section-8.2

The nice thing about .com files is that, once Nasm assembles it, you're done and ready to run it. Then fix the bugs. :)

If you're running Windows, you probably want to write Windows programs - "-f obj" will do it, but "-f win32" is probably preferable(?):

http://www.nasm.us/xdoc/2.09.06/html/nasmdoc7.html#section-7.5

It isn't that assembly language for Windows is "hard", particularly. In fact, it's so boring that much of the work is usually relegated to macros - "invoke" and "proc" and such. Push, push, push, call SomeAPI, push, push push, call SomeOtherAPI gets boring quick. For the workings of the APIs, you'll have to consult MS - not a "Nasm question".

If you're running some other OS, you can probably figure out which section to read. You may want to skip back to "The Nasm Language" at this point:

http://www.nasm.us/xdoc/2.09.06/html/nasmdoc3.html

Read as much as you need to so you can write something to assemble, or understand an example you've found. There's much more to the Manual, but you can leave it until you need it, mostly.

The Manual is intended to describe how to use Nasm, not to serve as an introduction to assembly language. For that, you'll want something different. Perhaps Dr. Carter's tutorial:

http://www.drpaulcarter.com/pcasm

Or perhaps you'd pick up the NASMX package and work through the examples there:

http://sourceforge.net/projects/nasmx/

That should give you something to chew on. :)

Best,
Frank