NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: annonymous on December 19, 2011, 09:23:34 PM

Title: 64bit
Post by: annonymous on December 19, 2011, 09:23:34 PM
I wanted to start programming in asm. I am a C programmer, so that should help. Well anywho, i wanted to know if i could code in Nasm if i am using a 64 bit system. Everything i have found so far suggests that i may only use Nasm with a 32 bit system. I really hope that this is not true. I DO NOT want to use AT&T syntax.

If so does anyone have a great place to learn from? Like a tutorial or PDF?
Title: Re: 64bit
Post by: Frank Kotler on December 19, 2011, 09:31:49 PM
Dunno what you've "found so far", but Nasm has been capable of 64-bit assembly for some time...


Title: Re: 64bit
Post by: annonymous on December 19, 2011, 09:44:00 PM
Nasm has been capable of 64bit for some time? That's a relief! So I look at all of that Frank, and it is pretty overwhelming! Is there a more beginner friendly tutorial specifically catered to 64bit? Thanks.

Yeah I think I will be better off learning 32 bit first. Then move to 64bit later on, if I feel the need.

Someone suggested to download a 32 bit version of Linux to run on my 64 bit platform. Exactly what i intend on doing too. Still leaves me empty handed. Still no beginner friendly tutorial for Nasm. That link that you provided Frank, isn't sitting well with me, sorry.

Can anyone please provide a beginner friendly tutorial so i can start. Thanks.
Title: Re: 64bit
Post by: Frank Kotler on December 20, 2011, 04:56:51 AM
As I'm still running 32-bit hardware, I'm not entirely certain what 64-bit Linux will (or will not) allow you to do. I'm pretty sure you can still run 32-bit code with no problem. You shouldn't need to install 32-bit Linux. However, your gcc and ld will (I think) be expecting to generate 64-bit code, unless instructed otherwise. I think adding "-m32" to a gcc command line, and "-melf-i386" to an ld command line will accomplish that...

I agree that you'd be better off to start by learning 32-bit assembly first - mainly because tutorials for 64-bit coding seem to ASSume that you know 32-bit, and mainly address the changes for 64-bit (many!).

For a 32-bit tutorial, try:

In order to be portable between Windows/Linux/etc., this uses the C library (printf and scanf mostly) for I/O. Make sure to get the Linux version of the example code! As I recall, the first thing you'll need to do is assemble asm_io.asm. To Nasm, "-f elf" and "-f elf32" are the same thing. Dr. Carter's instructions probably say "-f elf", but you might want to use "-f elf32" for "clarity"(?).

Code: [Select]
nasm -f elf32 -d ELF_TYPE asm_io.asm

The "ELF_TYPE" definition simply changes "_printf" to "printf", etc. As you probably know, gcc and ld don't use underscores on external symbols. Nasm has a feature (added after Dr. Carter wrote his book) which will do the "opposite" - you write "main" and "printf", etc., and "--prefix _" will add leading underscores to make it "portable" to Windows, and if you're using Watcom C, "--postfix" will add trailing underscores...

That should give you asm_io.o (if all goes well) which you can link with your asm gem, providing functions read_int, print_int, print_string, etc. (see the source for all of them) that you can use...

Then assemble first.asm...
Code: [Select]
nasm -f elf32 -d ELF_TYPE first.asm
... and link it with driver.c and asm_io.o from above. driver.c just calls asm_main, the entrypoint of your assembly code...
Code: [Select]
gcc -m32 -o first driver.c first.o asm_io.o

If that doesn't work, pay attention to what goes wrong and get back to us.

There's a list of other tutorials (not all for Nasm) at:

The first one, by Konstantin Boldyshev (who first straighened me out on how to do asm in Linux) looks quite good!

When you get ready to move to 64-bit, there's an example here:

I know there's a "better" one - more examples - but I can't find the bookmark at the moment. Anyone?