Calling a Linux command from within an Assembler program is very easy. This example calls /bin/ps from within an Assembler program. This is a very neat trick.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | section .data cmd db '/bin/ps', 0 section .text global _start _start: ; Execute the "ls" command mov eax, 11 ; System call number for execve mov ebx, cmd ; Address of the command string xor ecx, ecx ; No command line arguments xor edx, edx ; No environment variables int 0x80 ; Exit the program mov eax, 1 xor ebx, ebx int 0x80 |
To assemble and run this program on Linux, you can follow these steps:
- Save the code in a file named
ls.asm
. - Assemble the code using NASM assembler:
nasm -f elf32 ls.asm -o ls.o
. - Link the object file to create an executable:
ld -m elf_i386 ls.o -o
psme. - Run the program:
./
psme.
This is the output you will get from this program.
┗━━━━━━━━━━┓ john@localhost ~/Documents ┗━━━━━━━━━━━━━╾ ╍▷ ./lsme PID TTY TIME CMD 5926 pts/0 00:00:00 bash 78883 pts/0 00:00:00 ps |
This is a simple way to call a Linux command in asm. This might be a very useful programming tip.