Posted: . At: 9:55 AM. This was 12 months ago. Post ID: 17997
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to call a Linux executable within an Asssember program.


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.

ls.asm
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:

  1. Save the code in a file named ls.asm.
  2. Assemble the code using NASM assembler: nasm -f elf32 ls.asm -o ls.o.
  3. Link the object file to create an executable: ld -m elf_i386 ls.o -o psme.
  4. 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.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.