At about 22 years ago, Assembly Language is my second language after BASICA. I used Borland Turbo Assembler and Microsoft Debug on that time. I think Debug one of the very powerful tools for hacking. I like hacking. With Assembly, you can order the machine to do anything you want and go directly to every ports in the machine.
I just figure out, is there any Assembly Language in Linux?
Because, usually Assembly Language use DOS Services. Meanwhile, there is no DOS in Linux.
I finally found a good application, named NASM (Net wide Assembler). It’s a Assembly Language compiler in Linux. I’m still new with NASM, so I just take the sample codes from http://leto.net/writing/nasm.php.
NASM doesn’t have a text editor, so you have to type your code using other text editor then save the file with .asm file extension.
There are few main differences to be considered when you use assembly language in Linux:
-
In DOS mostly you use interrupt int 21h, int 10h and int 16h. In Linux, everything is handled by kernel with int 80h.
-
Linux is a 32-bit protected mode operating system, so you have to use extended 32-bit registers, such as EAX, EBX, ECX and so on. In 16-bit register, you still use AX, BX,CX etc …. registers.
-
In 32-bit programming, you don’t have to think about segments at all because it runs in the flat memory model.
-
I think no body write DOS assembly anymore. Hey… it’s 16 bit.
How I can get Nasm?
NASM is listed in Ubuntu Repository, so just download it using apt.
taufanlubis@toshiba:~$ sudo apt-get install nasm
[sudo] password for taufanlubis:
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages were automatically installed and are no longer required:
mplayer-skins mplayer libggi2 libgii1 libgii1-target-x youtube-dl
Use ‘apt-get autoremove’ to remove them.
The following NEW packages will be installed:
nasm
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 1557kB of archives.
After unpacking 2855kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com gutsy/main nasm 0.98.38-1.2build1 [1557kB]
Fetched 1557kB in 36s (42.8kB/s)
Selecting previously deselected package nasm.
(Reading database … 126048 files and directories currently installed.)
Unpacking nasm (from …/nasm_0.98.38-1.2build1_i386.deb) …
Setting up nasm (0.98.38-1.2build1) …
taufanlubis@toshiba:~$
Write your first Linux Assembly Language Application
When I learn a new computer language, the first thing I want to know is, how to display a ‘Hello World!!!’. I don’t want to know about the structure, variables, codes etc. Just do a simple thing, type, compile and see it run. That’s it. Then you can go further. It’s the same when you teach a baby to speak. We don’t teach any grammar. We give samples.
So, here it is.
Step 1. Type the code
You can use, vim, vi, gedit etc. The extension must be .asm.
taufanlubis@toshiba:~$ sudo vim hello.asm
[sudo] password for taufanlubis:
section .data
hello: db ‘Hello world!’,10
helloLen: equ $-hello
section .text
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,hello
mov edx,helloLen
int 80h
mov eax,1
mov ebx,0
int 80h
Press ‘Esc‘ button and type ‘:wq‘ to save the file.
Actually, I’m new myself in Assembly Programming in Linux. I took the codes above from the NASM website. I just wanna show you that we can do Assembly Programming in Linux. If you want to know deeper you can go
to this link: ……………………..
Step 2. Compile .asm (source code) to .o (object)
taufanlubis@toshiba:~$ nasm -f elf hello.asm
Step 3. Link the object file to produce an executable file.
taufanlubis@toshiba:~$ ld -s -o hello hello.o
Now, you have 3 files, hello.asm (your source code), hello.o (your object file) and hello (your program).
taufanlubis@toshiba:~$ ls -l hello*
-rwxr-xr-x 1 taufanlubis taufanlubis 440 2008-01-17 20:38 hello
-rw-r–r– 1 root root 203 2008-01-17 20:33 hello.asm
-rw-r–r– 1 taufanlubis taufanlubis 736 2008-01-17 20:37 hello.o
Step 3. Ready to run
You can run your program by typing:
taufanlubis@toshiba:~$ ./hello
Hello world!
taufanlubis@toshiba:~$
Congratulations! You have just written your first assembly program in Linux.
If you want to know more details about nasm and ld, you can see the manual.
taufanlubis@toshiba:~$ man ld
taufanlubis@toshiba:~$ man nasm
hi
sounds interesting…..
I have knowledge in C and I am doing now .net programming, is it easy for me to learn assembly?? I am just a college fresh student and computer enthusiast, as you have said you can easily go with every port of the machine, and want to learn about that…
can you list some nice resources for assembly?
thanks…..:)
We are student of MCS and laern more about the assembly language.
Assembly is a very difficult language programming that’s why it’s categorized as Low level language. If you are a technician who designed a hardware then it’s a recommended language. But if you are a programmer or a student, you can learn it as a reference.
In Linux, if you need to do port programming, you can use GAMBAS or gcc.
I suggest to use gambas, it’s easier.
Programming Assembly in Linux is different with in MS-DOS. In Linux you deal with a kernel meanwhile in DOS you deal with DOS services.
There few books and websites describe about Assembly programming in Linux. You can use Google by typing “NASM”.
guys, do you know any online sources for NASM codes?
especially for the interrupt and service settings.
There are two sites that I like for assembler info.
http://www.linuxasmbly.org
http://www.members.save-net.com/jko%40save-net.com/asm/
I work on the latter site and it is growing very fast.
jeff
Greetings,
I keep getting error on the second line of your sample code?
Is this an actual working code? do you have one?
Regards,
Mike
Greetings,
I resolved the problem. It was a syntax related. Thank you very much for setting up this site for Assembly programmers.
Please let me know if I can help.
Regards,
Mike
Although it is a bit late to post comments for this post, I really wanna say thanks to you !
You just taught me for my first assembly program !
Instead of just showing the code, try to explain it a little.
first think of a command line
printf “Hello World” > /dev/stdout
there are 6 registers
eax contains the pointer for what kernel system call
ebx first argument
ecx 2nd argument
edx 3rd argument
esx 4th argument
edi 5th argument
int 80h tells the kernel to run the stack
mov eax, 4
The above command will put 4 into eax register, which is the function
it is sys_write
mov eax,4 – tells kernel function sys_write
mov ebx,1 – this is stdout – if you know nix based systems, 2 is stderr
mov ecx,hello – pointer to string
mov edx,helloLen – pointer to length
int 80h – tell kernel to run stack
mov eax,1 – tells kernel function sys_exit
mov ebx,0 – tell it to return 0, like c++
int 80h – tell kernel to run stack
so on a standard command line (bash for example) would look like
printf “${variable:0:$length}” > /dev/stdout
sys_write “string” >/dev/stdout
instead of going from left to right, it goes down, like japeneese
If you have more questions, please email me at staxjp [AT] gmail.com
To learn more about system calls, or kernel handler go to google and type “Linux System Call Table”
man can you tell us how to save data to a file in assembly
where can i get tutorial about assembly in linux n where can i get GAMBAS?
thank you.
but when i am running the “hello world.asm” file , a warning came…..
ld: warning: cannot find entry symbol _start; not setting start address
even though i tried to run then it diaplayed….
segmentation fault
I’m new to Nasm and got this error using the hello.asm above…
jay@aerial-link:~$ sudo vim hello.asm
jay@aerial-link:~$ nasm -f elf hello.asm
hello.asm:2: error: expression syntax error
what should i do?
oh i got it…
it must be…
section .data
hello: db ‘Hello world!’,10 ; ‘Hello world!’ plus a linefeed character
helloLen: equ $-hello ; Length of the ‘Hello world!’ string
; (I’ll explain soon)
section .text
global _start
_start:
mov eax,4 ; The system call for write (sys_write)
mov ebx,1 ; File descriptor 1 – standard output
mov ecx,hello ; Put the offset of hello in ecx
mov edx,helloLen ; helloLen is a constant, so we don’t need to say
; mov edx,[helloLen] to get it’s actual value
int 80h ; Call the kernel
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h
salamo alikom
can you pls show me how i can compile 16 bit assembly code in 32 bit linux/bsd environment .
WHY do you use ‘sudo’ at all in this?
That is crazy.
i am a student..
i need to submit a game written in assembly language for my final project.. and the game is a PAIR/MATCHING GAME.. i don’t have any idea how to start.. anybody has an idea how to code this game??? HELP.. please
[…] Programming Language – NASM (Assembly language in Linux) « Taufan Lubis – Ubuntu L… – March 14th ( tags: nasm assembly linux example guide tutorial ) […]
it can’t compile my .asm file . when i typed thats code it show me: given below
nasm: fatal: unable to open input file `F.asm’
my assembly code look like this
.MODEL SMALL
.STACK 100H
.DATA
MSG DB ‘HELLO WORLD!$’
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG
MOV AH,9
INT 21H
MOV AH ,4CH
INT 21H
MAIN ENDP
END MAIN
i have used ubuntu 11.04
sorry my english is not good but i hope you can understand.
thank you,I’m new in ubuntu so I have to learn much.
sweet little tutorial for linux.
I think pictures tell a big story. That is way I made a series of linux nasm tutorials on my youtube channel.
For video tutorials, check out http://www.youtube.com/soliduscode.
Not to be self-serving, I think it explains in great details what assembly is, the stack frame, and how certain assembly instructions work.
hi im mike can you help me..i need a codes that the output is snake games
nasm:fatal:unable to open output file ‘ex05.com’ i have problem when i run my program