In general, programming in assembler in Linux is not very common and is done by him, perhaps, assembler fans. Today we will talk about programming in assembler and toolkit.
What we need:
- FASM. Take on the flatassembler.net version for Linux
- ald. We take on ald.sourceforge.net
- shed. We take on shed.sourceforge.net
- ld. There are in most distributions
Actually everyone chooses the tools for himself. I chose these for me.
FASM installation
After downloading the archive off. Site unpack it:
tar zxvf fasm-1.69.11.tgz
In the folder we will have a binary file fasm, which we can use for compilation. For convenience, you can create a symlink to it:
')
sudo ln -s /home/username/fasm/fasm /usr/local/bin
ald and shed are installed no more difficult:
$ ./configure
$ make
# make install
As a result, we will have 3 useful tools for programming in assembler.
System calls
Like most other operating systems, Linux provides so-called. API - a set of useful functions for the programmer. In most cases, the system function is called using the 80h interrupt. It should be noted that Linux uses the fastcall convention of passing parameters. According to it, parameters are passed through registers (in windows, for example, stdcall is used, where parameters are passed through the stack). The number of the function being called is put in eax, and the parameters in the registers:
Parameter Number / Register
1 / ebx
2 / ecx
3 / edx
4 / esi
5 / edi
6 / ebp
As you can see everything is not so difficult. You can find out the number of the system function, its description and parameters, at least
here . Take
sys_exit
for example. As you can see on that page, it has one parameter - the return code and it has the sequence number 1. Thus, we can call it with the following code:
mov eax, 1 ; 1 -
sub ebx, ebx ; ( mov ebx, 0)
int 80h ; 80h
I hope that everything is clear.
Hello, World!
Well then. We will not write anything, because everything is written for us :) In the
fasm/examples/elfexe
there is a file hello.asm, which contains the following code:
; fasm demonstration of writing simple ELF executable
format ELF executable 3
entry start
segment readable executable
start:
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,msg_size
int 0x80
mov eax,1
xor ebx,ebx
int 0x80
segment readable writeable
msg db 'Hello world!',0xA
msg_size = $-msg
As you can see, 2 system functions are called here -
sys_write
(with sequence number 4) and
sys_exit
.
sys_write
takes 3 parameters — an output stream handle (1 - stdout), a pointer to a string, and a string size. The number of the function itself, as already mentioned, we must put in eax. The
sys_exit
function
sys_exit
already been
sys_exit
. You can
fasm hello.asm
this miracle like this:
fasm hello.asm
(but not necessary, because there is a binary in the same place where the source is located).
Let's see what's inside
I think it's time to look at our binary. To begin with, let's use the hex editor to see what we did. Run the command:
shed hello

We see our entire program, data, elf-header. Not bad? Now we look at our program in the debugger. We type in the console:
ald hello
We should greet the line with a proposal to enter a command. You can find the list of commands by typing
help
or get help from a separate command by typing
help command
. Disassembling our program is possible with the
disassemble
command (or its alias is "
d
"). You will see a disassembled listing of your program. The address on the left, the command itself on the right, and the command opcode in the middle.
You can get the dump command
dump
(strange, but it is not in the output command
help
).

Now let's try working with the
next
command. Execute it and in response you will be shown the values ​​of the registers, the set flags, as well as the address, opcode and disassembled command that should be executed next. Try to execute commands and watch for changes in flags and registers. After calling the first interruption, you should see “Hello world!” On your screen.
The purpose of this article was to show the basics of programming in assembler in linux, and not programming in assembler in general. I hope that you have learned something useful from here.
Ps. The first article on Habré.
useful links
asm.sourceforge.netwww.int80h.org