Using exvvp on nasm

I am currently looking into NASM, possibly Linux system calls. I'm trying to copy a process and call a linux utility, but having the same problems with execvp, I don't know how to pass arguments to it. How can I make it right?

SECTION .data
    cmd_cat: db '/bin/cat', 0
    arg_cat: db 'log.txt', 0
    cat: dd cmd_cat, arg_cat, 0

     fd1: dw 0, 0
     pipe_error_message: db 'pipe error occured', 0xa
     pipe_error_message_length: equ $ - pipe_error_message

     fork_error_message: db 'fork error occured', 0xa
     fork_error_message_length: equ $ - fork_error_message
SECTION .text
GLOBAL _start:
_start:
     ;call pipe(fd1)
     ;42 - pipe system call number
     mov eax, 42
     mov ebx, fd1

     ;call kernel to execute
     int 080h
     cmp eax, 0
     jne pipe_error


     ;call pipe(fd2)
      mov eax, 42
      mov ebx, fd1
      int 080h
      cmp eax, 0
      jne pipe_error

      ;fork()
      mov eax, 2
      int 080h
      cmp eax, -1
      je fork_error
      jnz child_cat 
      call exit

 ;displays error message and finishes the programm when something is wrong with pipe
 pipe_error:
     mov edx, pipe_error_message_length
     mov ecx, pipe_error_message
     call sys_write
     call exit

 ;displays error message and finishes the programm when something is wrong with fork
 fork_error:
      mov edx, fork_error_message_length
      mov ecx, fork_error_message
      call sys_write
      call exit

 ;sys_write(unsigned int fd, const char __user *buf, size_t count);
 sys_write:
      mov ebx, 1               
      mov eax, 4               
      int 080h


 ;exit(0)
 exit: 
      mov  eax,1
      mov  ebx,0
      int 080h

 child_cat:
      mov ebx, [fd1]
      mov eax, 6
      int 080h

      ;dup2(fds[1],1)
      mov ecx, 1
      mov ebx, [fds + 4]
      mov eax, 63
      int 080h

      mov eax, 11
      mov ebx, cmd_cat
      mov ecx, cat
      int 080h

      

+3


source to share


1 answer


The following code works for me in a gas collector. I explained what it does, so hopefully someone else can provide the nasm translation.



.text

    .global _start

_start:

    movl $0xb, %eax          # system call 0xb (execve) goes in eax

    movl $arg0, %ebx         # put the _address_ of the command string 
                             # in ebx (we are providing a pointer)

    movl $ptrarray, %ecx     # put the _address_ of the array of pointers 
                             # to arguments in ecx (again, a pointer)

    movl $0, %edx            # put a literal zero in edx (we don't have 
                             # environment variables to pass, so we give
                             # a null pointer)

    int $0x80                # run the system call

.data


ptrarray:                    # This is the array of pointers to command line 
                             # arguments
    .long arg0, arg1, 0      # The first element is a _pointer_ to the command 
                             # The second element is a _pointer_ to an argument
                             # The third is a null pointer to indicate no more

arg0:                        # This is the command string
    .asciz "/bin/cat"
arg1:                        # This is the argument string
    .asciz "file.txt"

      

+1


source







All Articles