installing and running assembler on debian

The program nasm or “netwide assembler” is the first step. If you don’t know if it’s installed Just enter whereis nasm on a command prompt. If a single line “nasm’ is returned you’ll need to install the program. That is one simple command on most distributions. On Debian it is sudo apt install nasm, which should install the binary into /user/bin. Make sure that nasm is on the system path which it should be after the install.

compile, link and execute assembler on linux

Write your assembler code (the hard part) and save it as a file with .asm as a suffix. Then execute the nasm commands sudo nasm -f elf hello.asm (creates the object file or object code). Then execute sudo ld -m elf_i386 -s -o hello hello.o which will link the object code to produce an executable file. Then simply type ./hello to execute. Below is the source code for Hello. world! The object file is actually the machine code created or compiled from the source. The executable file is a composite of “linked” object files.


Download the following example asm file with this link
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     0x80                                ;call kernel
    mov     eax,1                               ;system call number (sys_exit)
    int     0x80                                ;call kernel

section     .data
msg     db  'Hello, world!',0xa                 ;our string
len     equ $ - msg                             ;length of our string
compile, link and execute assembler on linux

Above is another example of the following code assembled, linked then executed.

Download stars.asm with this link
section	.text
   global _start	 ;must be declared for linker (gcc)
	
_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	0x80     ;call kernel
   mov	edx,9    ;message length
   mov	ecx,s2   ;message to write
   mov	ebx,1    ;file descriptor (stdout)
   mov	eax,4    ;system call number (sys_write)
   int	0x80     ;call kernel
   mov	eax,1    ;system call number (sys_exit)
   int	0x80     ;call kernel
	
section	.data
msg db 'Displaying 9 stars',0xa ;a message
len equ $ - msg  ;length of message
s2 times 9 db '*'

Feel free to comment