Compiling an assembly program with Nasm

Last update on May 13, 2009 10:33 AM by jak58
Published by deri58

Compiling an assembly program with Nasm







Intro


Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture and is commonly used to create 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. It is available for multiple operating systems like Linux or Windows, for example.
An assembler can assemble the assembly code. (sic)
Link:http://en.kioskea.net/telecharger/telecharger 1025 nasm
Unfortunately, it means two things
  • Assembly language lets you program in machine language from mnemonics
  • Software that will transform your code directly into machine language understood by the processor.




This trick does not teach you to program with Nasm but to create an executable for Linux and Windows from a source code fromNASM.

With LInux



Step 1. Create a source file


You do not need an editor to create a specific source file for NASM.You can choose gedit, kwrite, ... xemacs

When you save your file, give it the extension .asm

Step 2. Assemble the source file

For this step, you will need nasm software installed on your machine. If you're running Debian or Ubuntu, simply type the command

sudo apt-get install nasm





If you have another Linux distribution, you must use the package manager of your distribution (eg urpmi, yum, emerge ... etc.) or by downloading nasm compile the archive from the official site.

Now to assemble your source file.
Go to command line in the directory where your source file that we call test.asm for example. Here is the line type:

nasm -f elf test.asm




This will create a file named test.o in the current directory. This file is not executable, it is still an object file, ie a need to link with libraries which it will depend for example, the standard library libc.

Step 3. Creation of the executable


Now that we have our object file named test.o we create our executable.
Two cases are presented here:
  • Your program begins with a procedure called "_start". This means that your program has its own point of entry without the use of the “main” function. However, you'll need to use the "l" to create your executable:


ld test.o -o test
  • Your program begins with a procedure called "main". You will need to use gcc to create your executable:



gcc test.o -o test



Our executable is created, it is tested and is in the current directory.


Step 4. Program Execution


To run the program called "test", just type this command:

. / test 

With Windows


Under the windows the convention for calling parameter is not the same, the function is called must clean the stack itself Similarly, the main function is not available under Wndows, it must be replaced by WinMain.
If your entry point is _start or main, it should be changed to "_WinMain @ 16" and change the "ret" at the end of the procedure for entry into "ret 16".
Example of a correct source file on Windows:

section .text
	global _WinMain@16

_WinMain@16:
	mov eax, 0
	ret 16	





Step 1. Install the necessary software


We will first install nasm. Go to the http://en.kioskea.net/telecharger/telecharger 1025 nasm. Keep an archive in a corner, it will be used later.

The most difficult step will be to install MingW which is a free development environment for Windows.
At this address http://gd.tuwien.ac.at/gnu/mingw/?fisel=0-9,a-z,A-Z, choose the latest version of MinGW.
Run this installer. Don’t update atthe beginning, say not. Leave all options selected by default and then wait for the installation.

Now we'll insert nasm in the development environment MinGW.

Unpack the archive nasm, you should get a folder containing, among other things, a file named nasm.exe
Copy this file in the directory C: \ MinGW \ bin

Step 2. Create a source file

Like Linux, there is no need to use a specific publisher to create a source file for NASM.
You can use the notepad but beware, the notepad will tend to added. txt extension to file it creates. To remove any ambiguity, I recommend you view the extensions of your files.
Avoid in any case, word processors such as Word or WordPad.

If you wish, you can also use an editor that uses syntax of nasm such NasmEdit IDE (free).

In any case, I advise you to give the extension. asm to your source.

Step 3. Assemble the source file

Open the command windows (type cmd.exe in "Run" from the Start menu or directly "cmd" in the "search" of the logo windows in Windows Vista.
With this command interpreter, you must go to the folder containing your source file with the command "cd".
Once you are in this directory, assemble your source file (the call test.asm) with this command:
nasm -f win32 test.asm -o test.o




You now have an object that is not executable, but it will soon proceed to the final stage.

Step 4. Creation and execution of the program

From your command window, type the final command to create the executable:


ld test.o -o test.exe

It should be okay.
Best answers for « Compiling an assembly program with Nasm » in :
Compiling a program in C with Dev C++ on Vista Show Compiling a program in C with Dev C++ on Vista Compiling a C source code under Vista with Dev C++, can be a quite difficult procedure. Quick solution: the g++ compiler To be used with Dev-PSV: Go to Tools/compiler options and...
Assembling multiple PDF files ShowAssembling multiple PDF files Intro Steps: Intro PDF creator is a software allowing you to print several files into one with its printer function. Download link http://en.kioskea.net/telecharger/telecharger-40-pdf...
Assembly - Multiplication by a constant ShowAssembly - Multiplication by a constant Short intro Multiplication under assembly Short intro Assembly languages are set of low-level languages for programming computers and were first developed in the 1950s and are mostly known as...
X86 assembly occurrence of a character Showx86 assembly occurrence of a character Introduction Issue Solution Explanation Introduction The small assembly exercise below is for (Intel and AMD 32-bit) x86 architectures and uses the NASM syntax , an assembler, available...
Programming languages ShowProgramming language A "programming language" is a language designed to describe a set of consecutive actions to be executed by a computer. A programming language is therefore a practical way for us (humans) to give instructions to a...
PC Assembly ShowComputer Assembly A PC computer is a modular type of computer, it can be assembled using hardware components made by different manufacturers, so as to have a custom built computer according to one's specific needs. The following components are...
Linux - Compiling the kernel ShowCompiling the kernel In this article, compiling the kernel under Linux is explained. The following explanations are based on version 2.4.20 of kernel, i.e. the most recent version of the kernel at the time this article was written (March 2003)....