【发布时间】:2016-03-27 13:59:56
【问题描述】:
如何将字符串数组作为参数传递给汇编器中的函数? 例如,假设我想调用如下所示的 execve() 函数:
int execve(const char *filename, char *const argv[], char *const envp[]);
所以我这样做:
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
制作文件
all:
~/apps/fasm/fasm ./test.asm
但是当我运行我的程序 execve() 无法执行请求的程序并且 strace ./test 显示此消息:
execve("/bin/ls", [0x6e69622f, 0x736c2f, 0x2e], [/* 0 vars */]) = -1 EFAULT (Bad address)
如何正确地将“args”变量传递给 execve 函数?
谢谢:)
【问题讨论】:
-
要了解某事是如何完成的,请用 C 语言编写并查看编译器输出。
-
使用
lea ebx, [ filename ]将标签的地址移动到寄存器中。