Doing assembly programming in GNU/Linux



Basic knowledge of opcodes is required for you to understand this page well.
This page is not going to discuss the manual assembly(the one we used to do on an SDK-86 or whatever board).
I am taking it as granted that you are having a computer system running with
GNULinux & NASM installed on it.
This whole page will discuss assembly language with NASM &
is not going to teach how to use libc calls in your assembly programs.


Now-a-days it is hard to find PURE assembly related stuff (you can easily find a lot of of stuff on High Level Assembly(HLA) & assembly programming using libc calls). A reason behind this, may be, is that people want to write programs without diving into the system level details. This may have potential drawbacks, besides the benefit that it takes much less time to learn a High Level Language (HLL). The one thing that assembly language programming demands is true devotion & lots of time.

Whenever we are do assembly on a computer system we require a software known as assembler to change our assembly language instructions into machine level instructions, very much similar to use a compiler or an interpreter in case of a HLL like Pascal, C, or C++ .

Here on GNULinux we are going to use NASM.
GNULinux is 32-bit, runs in protected mode, has flat memory model, & uses the ELF format for binaries.
Only the kernel and device drivers are allowed to access hardware directly.

NASM(Netwide ASseMbler)
NASM is an open-source assembler that follows the INTEL syntax rather than AT&T syntax. NASM supports a range of object file formats, including GNULinux's a.out and ELF, NetBSD/FreeBSD, COFF, Microsoft 16-bit OBJ and Win32. It will also output plain binary files. Its syntax is designed to be simple and easy to understand. It supports Pentium, P6 and MMX opcodes, and has macro capability.

You can write your assembly program in any text editor that is comfortable in generating ASCII code.
After writing your whole program you have to save it with the extension .asm .
So if you want to name your program XYZ then it should be saved as XYZ.asm
To turn your XYZ.asm file into an executable one under GNULinux & then finally to execute it you have to run the following set of three commands :
$nasm -f elf XXX.asm       ; for producing XXX.o ELF object file
$ld -s -o XXX XXX.o       ; for producing XXX executable
$exact_path_from_root/XXX       ; for running(executing) Ex. /home/guest/XXX


Coming over to the syntax of NASM i.e. how we will write our program in GNULinux so that NASM will assemble & link it, with the help of ld(GNU Linker), without any errors or warnings.
A program can be divided into sections:
.text for your code(read only),
.data for your data(read-write),
.bss for uninitialized data(read-write);
There can be a few other standard sections, as well as some user defined sections, but there's a rare need to use them & they are out of our interest here.
A program must have at least .text section.
Looking from a wide angle each statement i.e. each instruction of an assembly code consists of


labelinstoperandcomments
;Example Program
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .data
msg db "I'm a true hacker",0xa ;String to be printed
len equ $ - msg ;Length of the string
section .text
global _start ; we must export the entry point to the ELF linker or loader.
;They conventionally recognize _start as their entry point. Use ld -e foo to override the default.
_start:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
movedx, len ;third argument: message length
mov ecx, msg ;second argument: pointer to message to write
mov ebx, 1 ;first argument: file handle(stdout)
mov eax, 4 ;system call number (sys_write)
int0x80 ; call kernel
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
movebx, 0
moveax, 1 ;system call number (sys_exit)
int 0x80
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

In Nasm, a label (without square brackets) is interpreted as the address of the data in memory. If the label is placed inside square brackets ([]), it is interpreted as the data currently at the address. All the labels end with colon (:). The label _start is the entry point of the program. Thus there must be a entry point called _start which is declared global. Note: When a character is singled quoted, the corresponding ASCII value is used.


In GNULinux you don't have the DOS(Disk Operating System) & BIOS(Basic Input/Output System) function calls available to you but instead you have system calls.
So while programming under GNULinux your favourite INT 21h, INT 10h will not be doing what they used to do under DOS & will be serving some other tasks.Leave this at the moment that what tasks they are performing here'n GNULinux.

Coming straight to system calls - These are analogous to function calls but can be used without any software interrupt.
System calls in GNULinux are done with the help of int 80h. To invoke a GNULinux system call you have to - pass the system call number(opcode) in the EAX register, the parameters(operands), if required, into registers EBX(first parameter), ECX(second parameter), EDX(third parameter), ESI(fourth parameter) & EDI(fifth parameter) & then write int 0x80.
Note: Certain GNULinux 2.4(kernel) calls pass a sixth parameter in EBP. Calls compatible with the earlier versions of the kernel pass six or more parameters in a parameter block & pass the address of the parameter block in EBX.

Linux system calls



Adding some contents to this page is really tough for me as I am not very much hi-fi in assembly. I have to work for weeks on a program to work :-(

The following is a program to add two integer numbers:

labelinstoperandcomments
;Program to print the sum of two integers; sum should be less than 65536
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .data
buf db4 ;Declaring a buffer
section .data
crlfdb 0x0D0A
section .text
global _start ; we must export the entry point to the ELF linker or loader.
;They conventionally recognize _start as their entry point. Use ld -e foo to override the default.
_start:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
movax, 65530 ;moving the first number into the accumulator
mov bx, 5 ;moving the second number into the bx register
add ax,bx ;adding the two nuumbers & the sum to remain in the accumulator
mov bx,10 ;
movsi,0 ;
disp1: mov dx,0 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
divbx
pushdx ;
inc si
or ax,ax
jnz disp1
disp2pop dx;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
add dl,'0'
mov [buf],dl
mov edx,1
mov ecx,buf
mov ebx,1
mov eax,4
int 80h
dec si
jnz disp2
mov edx,1
mov ecx,crlf
mov ebx,1
mov eax,4
int 0x80
mov ebx,0
mov eax,1
int 80h








Recommended for You »

  1. Zindagi Phool Si Muskurati Rahe Har Khushi Aapke Daaman Mein Jagmagati ..
  2. Fir Kahi Dooor Se Ek Baar Sataa De Mujhe Meri Tanhai ..
  3. Birth Is The Start Of Life Beauty Is The Art Of ..
  4. A Good Laugh And A Long Sleep Are The Two Best ..
  5. Taare Aasmaan Mein Chamakte Hain Baadal Itni Dooor Hain Fir Bhi ..
  6. Einstein Newton Maxwell Faraday Kirchoff These Are The People Whose 10 Minute Enjoyment Wasted Our Childhood ..
  7. 2 Beggars Met 2 Software Engineer Met Both Asked Each Other ..
  8. I Don T Listen To People S Opinions I Have People Around Me ..
  9. The Notepad Of Our Life May Not Be Backspaced But Yes ..
  10. English Mother Good Night Dear Hindi Mother Shubh Raatri Beta Punjabi Mother ..

Share & Let Everyone Read









Draw shape below and click submit button to send us your message:

About Us


Our logo expands to iOLdot - Ik Oankaar Lazeez Dimension of Texting which tries to reflect our ideology.

The purpose of this website is to develop a Dimension to Texting through the Aesthetics of Words by providing Unique, Decent, Pleasant, Pure, Gentle, Clean, Refined, Inoffensive Thought Provoking Wisdom Quotes, Funny Jokes, Shayari, Motivational SMS, Greetings, Wishes, Proverbs, Dohe, Love Messages & much more.. We also encourage you to be part of this journey & share your creative content with us. Play your flute here..



What's more


moderated-content-kids-safe

Moderated Content

Safe for people of all Age Groups including Children.
sms-api-always-win

API

Display SMS on your Website or Blog at Zero Cost.
rate-content

SMS Rating

Gives You Power to Rate Content.
« Stay In Touch »