1

How can I pass array of strings as parameter to function in assembler? For example lets say I want to call execve() function which looks like this:

int execve(const char *filename, char *const argv[], char *const envp[]);

so I do this:

test.asm

format elf executable

entry main

main:
    mov eax, 11 ; execve - executes program
    mov ebx, filename   ; label name is address of string variable
    mov ecx, args       ; label name is address of array of strings?
    mov edx, 0          ; NULL
    int 80h

    mov eax, 1  ;exit
    int 80h
    ret

filename db '/bin/ls', 0        ; path to program
args db '/bin/ls', 0, '.', 0, 0 ; array should end with empty string to
                                ; indicate end of array

makefile

all:
    ~/apps/fasm/fasm ./test.asm

But when I run my program execve() fails to execute requested program and strace ./test shows this message:

execve("/bin/ls", [0x6e69622f, 0x736c2f, 0x2e], [/* 0 vars */]) = -1 EFAULT (Bad address)

How to properly pass "args" variable to execve function?

Thanks :)

2
  • To find out how something is done, write it in C and look at compiler output. Commented Mar 27, 2016 at 14:27
  • Use lea ebx, [ filename ] to move the address of a label into a register. Commented May 13, 2016 at 14:39

1 Answer 1

2

Do you know how this works in C? Strings are pointers, and a string array is an array of pointers. Thus you need to do something like:

filename db '/bin/ls', 0 
dot db '.', 0
args dd filename, dot, 0

Notice that args is dd to get pointer size items, and it is filled with the addresses of strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.