Linux Assembly!

      
      An assembly program on a Linux platform can be compiled using NASMNASM is an assembler and dissembler for the Intel x86 architecture and is commonly used to create 16-bit,32-bit and 64-bit programs. It is available on multiple operating systems like Linux or Windows.



How to write and execute an assembly program in Linux:

Step 1. Create a source file

   You can use any text editor to create your source file for NASM such as Gedit, KWrite,XEmacs or using cat command. 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 Linux machine.
  • Use the following command line to assemble your source file:  
     nasm -f elf hello.asm

     In the example, the saved .asm file is called hello.asm
  • This will create a object file named hello.o in the current directory.


Step 3. Creating the executable file

  • This invloves two cases:
The program begins with a procedure called "_start". This means that the program has its own point of entry without the use of the main function. However, we'll need to use the "l" to create the executable.

      ld hello.o -o hello
 

The program begins with a procedure called "main". We will need to use gcc to create the executable file.

      gcc hello.o -o hello

Step 4. Program Execution
  • To run the program called "hello", just type this command:

      ./hello

Sample Hello World Program:

cat > hello.asm                                         

section     .text
global      _start                            ;must be declared for linker(ld)

_start:                                         ;tell linker entry point

    mov     edx,len                             ;message length
    mov     ecx,msg                             ;message to write
    mov     ebx,1                               ;file descriptor (stdout)
    mov     eax,4                               ;system call number(sys_write)
    int     80h                                ;call kernel

    mov     eax,1                               ;system call number (sys_exit)
    int     80h                                ;call kernel

section     .data

msg     db  'Hello, world!',0xa                 ;our string
len     equ $ - msg                             ;length of our string

nasm -f elf hello.asm

ld hello.o -o hello

gcc hello.o -o hello

./hello

Output: Hello world!







References:
https://www.youtube.com/watch?v=OaXRNKMFkY8
http://asm.sourceforge.net/intro/hello.html

Comments