BPS
BPS

Reputation: 1231

How to pass array of strings as parameter to function?

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 :)

Upvotes: 1

Views: 175

Answers (1)

Jester
Jester

Reputation: 58792

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.

Upvotes: 2

Related Questions