Sometimes there is a need for our program to launch some other program or a system command. To do this, use the sys_execve system function (number 11). It accepts the following parameters:
Let's try to write a simple example - printing a message with the echo program.
; Launching other programs using assembly under Linux ; ; Author: Bogdan D., bogdandr (at) op . pl ; ; assemble: ; ; nasm -f elf -o exec_linux.o exec_linux.asm ; ld -o exec_linux exec_linux.o section .text global _start _start: mov eax, 11 ; sys_execve function number mov ebx, komenda ; file to run mov ecx, argumenty ; address of parameters list mov edx, srodowisko ; address of environment variables int 80h mov eax, 4 mov ebx, 1 mov ecx, info mov edx, info_dl int 80h ; display a message mov eax, 1 xor ebx, ebx int 80h ; exit the program section .data komenda db "/bin/echo", 0 ; program to run info db "Program executed.", 10 ; message to print info_dl equ $ - info argumenty dd komenda ; argv[0] is the program name dd arg1 ; argv[1] dd 0 ; end of parameter list arg1 db "Czesc!", 0 ; first command-line parameter srodowisko dd home ; one environment variable dd 0 ; end of environment variables home db "HOME=/home/bogdan", 0 ; example $HOME ; environment variable
One thing should immediately be noticed:
the Program executed
message is not displayed.
The reason for this is that if the sys_execve function
executes without any errors, it ... will not return to the out calling program
(just like it says on the man page: man execve). Major drawback, but it can be
easily removed by using threads or functions like sys_fork
or sys_clone, to run a separate thread or process, which in
turn calls sys_execve.